Interpreter.cpp 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689
  1. /*
  2. * Copyright (c) 2021-2024, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/HashTable.h>
  8. #include <AK/TemporaryChange.h>
  9. #include <LibJS/AST.h>
  10. #include <LibJS/Bytecode/BasicBlock.h>
  11. #include <LibJS/Bytecode/CommonImplementations.h>
  12. #include <LibJS/Bytecode/Generator.h>
  13. #include <LibJS/Bytecode/Instruction.h>
  14. #include <LibJS/Bytecode/Interpreter.h>
  15. #include <LibJS/Bytecode/Label.h>
  16. #include <LibJS/Bytecode/Op.h>
  17. #include <LibJS/Runtime/AbstractOperations.h>
  18. #include <LibJS/Runtime/Array.h>
  19. #include <LibJS/Runtime/BigInt.h>
  20. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  21. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  22. #include <LibJS/Runtime/Environment.h>
  23. #include <LibJS/Runtime/FunctionEnvironment.h>
  24. #include <LibJS/Runtime/GlobalEnvironment.h>
  25. #include <LibJS/Runtime/GlobalObject.h>
  26. #include <LibJS/Runtime/Iterator.h>
  27. #include <LibJS/Runtime/MathObject.h>
  28. #include <LibJS/Runtime/NativeFunction.h>
  29. #include <LibJS/Runtime/ObjectEnvironment.h>
  30. #include <LibJS/Runtime/Realm.h>
  31. #include <LibJS/Runtime/Reference.h>
  32. #include <LibJS/Runtime/RegExpObject.h>
  33. #include <LibJS/Runtime/Value.h>
  34. #include <LibJS/Runtime/ValueInlines.h>
  35. #include <LibJS/SourceTextModule.h>
  36. namespace JS::Bytecode {
  37. bool g_dump_bytecode = false;
  38. static ByteString format_operand(StringView name, Operand operand, Bytecode::Executable const& executable)
  39. {
  40. StringBuilder builder;
  41. if (!name.is_empty())
  42. builder.appendff("\033[32m{}\033[0m:", name);
  43. switch (operand.type()) {
  44. case Operand::Type::Register:
  45. builder.appendff("\033[33mreg{}\033[0m", operand.index());
  46. break;
  47. case Operand::Type::Local:
  48. // FIXME: Show local name.
  49. builder.appendff("\033[34mloc{}\033[0m", operand.index());
  50. break;
  51. case Operand::Type::Constant: {
  52. builder.append("\033[36m"sv);
  53. auto value = executable.constants[operand.index()];
  54. if (value.is_empty())
  55. builder.append("<Empty>"sv);
  56. else if (value.is_boolean())
  57. builder.appendff("Bool({})", value.as_bool() ? "true"sv : "false"sv);
  58. else if (value.is_int32())
  59. builder.appendff("Int32({})", value.as_i32());
  60. else if (value.is_double())
  61. builder.appendff("Double({})", value.as_double());
  62. else if (value.is_bigint())
  63. builder.appendff("BigInt({})", value.as_bigint().to_byte_string());
  64. else if (value.is_string())
  65. builder.appendff("String(\"{}\")", value.as_string().utf8_string_view());
  66. else if (value.is_undefined())
  67. builder.append("Undefined"sv);
  68. else if (value.is_null())
  69. builder.append("Null"sv);
  70. else
  71. builder.appendff("Value: {}", value);
  72. builder.append("\033[0m"sv);
  73. break;
  74. }
  75. default:
  76. VERIFY_NOT_REACHED();
  77. }
  78. return builder.to_byte_string();
  79. }
  80. static ByteString format_operand_list(StringView name, ReadonlySpan<Operand> operands, Bytecode::Executable const& executable)
  81. {
  82. StringBuilder builder;
  83. if (!name.is_empty())
  84. builder.appendff("\033[32m{}\033[0m:[", name);
  85. for (size_t i = 0; i < operands.size(); ++i) {
  86. if (i != 0)
  87. builder.append(", "sv);
  88. builder.appendff("{}", format_operand(""sv, operands[i], executable));
  89. }
  90. builder.append("]"sv);
  91. return builder.to_byte_string();
  92. }
  93. static ByteString format_value_list(StringView name, ReadonlySpan<Value> values)
  94. {
  95. StringBuilder builder;
  96. if (!name.is_empty())
  97. builder.appendff("\033[32m{}\033[0m:[", name);
  98. builder.join(", "sv, values);
  99. builder.append("]"sv);
  100. return builder.to_byte_string();
  101. }
  102. ALWAYS_INLINE static ThrowCompletionOr<Value> loosely_inequals(VM& vm, Value src1, Value src2)
  103. {
  104. if (src1.tag() == src2.tag()) {
  105. if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish())
  106. return Value(src1.encoded() != src2.encoded());
  107. }
  108. return Value(!TRY(is_loosely_equal(vm, src1, src2)));
  109. }
  110. ALWAYS_INLINE static ThrowCompletionOr<Value> loosely_equals(VM& vm, Value src1, Value src2)
  111. {
  112. if (src1.tag() == src2.tag()) {
  113. if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish())
  114. return Value(src1.encoded() == src2.encoded());
  115. }
  116. return Value(TRY(is_loosely_equal(vm, src1, src2)));
  117. }
  118. ALWAYS_INLINE static ThrowCompletionOr<Value> strict_inequals(VM&, Value src1, Value src2)
  119. {
  120. if (src1.tag() == src2.tag()) {
  121. if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish())
  122. return Value(src1.encoded() != src2.encoded());
  123. }
  124. return Value(!is_strictly_equal(src1, src2));
  125. }
  126. ALWAYS_INLINE static ThrowCompletionOr<Value> strict_equals(VM&, Value src1, Value src2)
  127. {
  128. if (src1.tag() == src2.tag()) {
  129. if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish())
  130. return Value(src1.encoded() == src2.encoded());
  131. }
  132. return Value(is_strictly_equal(src1, src2));
  133. }
  134. Interpreter::Interpreter(VM& vm)
  135. : m_vm(vm)
  136. {
  137. }
  138. Interpreter::~Interpreter()
  139. {
  140. }
  141. ALWAYS_INLINE Value Interpreter::get(Operand op) const
  142. {
  143. switch (op.type()) {
  144. case Operand::Type::Register:
  145. return m_registers.data()[op.index()];
  146. case Operand::Type::Local:
  147. return m_locals.data()[op.index()];
  148. case Operand::Type::Constant:
  149. return m_constants.data()[op.index()];
  150. }
  151. __builtin_unreachable();
  152. }
  153. ALWAYS_INLINE void Interpreter::set(Operand op, Value value)
  154. {
  155. switch (op.type()) {
  156. case Operand::Type::Register:
  157. m_registers.data()[op.index()] = value;
  158. return;
  159. case Operand::Type::Local:
  160. m_locals.data()[op.index()] = value;
  161. return;
  162. case Operand::Type::Constant:
  163. break;
  164. }
  165. __builtin_unreachable();
  166. }
  167. // 16.1.6 ScriptEvaluation ( scriptRecord ), https://tc39.es/ecma262/#sec-runtime-semantics-scriptevaluation
  168. ThrowCompletionOr<Value> Interpreter::run(Script& script_record, JS::GCPtr<Environment> lexical_environment_override)
  169. {
  170. auto& vm = this->vm();
  171. // 1. Let globalEnv be scriptRecord.[[Realm]].[[GlobalEnv]].
  172. auto& global_environment = script_record.realm().global_environment();
  173. // 2. Let scriptContext be a new ECMAScript code execution context.
  174. auto script_context = ExecutionContext::create(vm.heap());
  175. // 3. Set the Function of scriptContext to null.
  176. // NOTE: This was done during execution context construction.
  177. // 4. Set the Realm of scriptContext to scriptRecord.[[Realm]].
  178. script_context->realm = &script_record.realm();
  179. // 5. Set the ScriptOrModule of scriptContext to scriptRecord.
  180. script_context->script_or_module = NonnullGCPtr<Script>(script_record);
  181. // 6. Set the VariableEnvironment of scriptContext to globalEnv.
  182. script_context->variable_environment = &global_environment;
  183. // 7. Set the LexicalEnvironment of scriptContext to globalEnv.
  184. script_context->lexical_environment = &global_environment;
  185. // Non-standard: Override the lexical environment if requested.
  186. if (lexical_environment_override)
  187. script_context->lexical_environment = lexical_environment_override;
  188. // 8. Set the PrivateEnvironment of scriptContext to null.
  189. // NOTE: This isn't in the spec, but we require it.
  190. script_context->is_strict_mode = script_record.parse_node().is_strict_mode();
  191. // FIXME: 9. Suspend the currently running execution context.
  192. // 10. Push scriptContext onto the execution context stack; scriptContext is now the running execution context.
  193. TRY(vm.push_execution_context(*script_context, {}));
  194. // 11. Let script be scriptRecord.[[ECMAScriptCode]].
  195. auto& script = script_record.parse_node();
  196. // 12. Let result be Completion(GlobalDeclarationInstantiation(script, globalEnv)).
  197. auto instantiation_result = script.global_declaration_instantiation(vm, global_environment);
  198. Completion result = instantiation_result.is_throw_completion() ? instantiation_result.throw_completion() : normal_completion({});
  199. // 13. If result.[[Type]] is normal, then
  200. if (result.type() == Completion::Type::Normal) {
  201. auto executable_result = JS::Bytecode::Generator::generate_from_ast_node(vm, script, {});
  202. if (executable_result.is_error()) {
  203. if (auto error_string = executable_result.error().to_string(); error_string.is_error())
  204. result = vm.template throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory));
  205. else if (error_string = String::formatted("TODO({})", error_string.value()); error_string.is_error())
  206. result = vm.template throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory));
  207. else
  208. result = JS::throw_completion(JS::InternalError::create(realm(), error_string.release_value()));
  209. } else {
  210. auto executable = executable_result.release_value();
  211. if (g_dump_bytecode)
  212. executable->dump();
  213. // a. Set result to the result of evaluating script.
  214. auto result_or_error = run_executable(*executable, {}, {});
  215. if (result_or_error.value.is_error())
  216. result = result_or_error.value.release_error();
  217. else
  218. result = result_or_error.return_register_value;
  219. }
  220. }
  221. // 14. If result.[[Type]] is normal and result.[[Value]] is empty, then
  222. if (result.type() == Completion::Type::Normal && !result.value().has_value()) {
  223. // a. Set result to NormalCompletion(undefined).
  224. result = normal_completion(js_undefined());
  225. }
  226. // FIXME: 15. Suspend scriptContext and remove it from the execution context stack.
  227. vm.pop_execution_context();
  228. // 16. Assert: The execution context stack is not empty.
  229. VERIFY(!vm.execution_context_stack().is_empty());
  230. // FIXME: 17. Resume the context that is now on the top of the execution context stack as the running execution context.
  231. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  232. // in which case this is a no-op.
  233. // FIXME: These three should be moved out of Interpreter::run and give the host an option to run these, as it's up to the host when these get run.
  234. // https://tc39.es/ecma262/#sec-jobs for jobs and https://tc39.es/ecma262/#_ref_3508 for ClearKeptObjects
  235. // finish_execution_generation is particularly an issue for LibWeb, as the HTML spec wants to run it specifically after performing a microtask checkpoint.
  236. // The promise and registry cleanup queues don't cause LibWeb an issue, as LibWeb overrides the hooks that push onto these queues.
  237. vm.run_queued_promise_jobs();
  238. vm.run_queued_finalization_registry_cleanup_jobs();
  239. vm.finish_execution_generation();
  240. // 18. Return ? result.
  241. if (result.is_abrupt()) {
  242. VERIFY(result.type() == Completion::Type::Throw);
  243. return result.release_error();
  244. }
  245. VERIFY(result.value().has_value());
  246. return *result.value();
  247. }
  248. ThrowCompletionOr<Value> Interpreter::run(SourceTextModule& module)
  249. {
  250. // FIXME: This is not a entry point as defined in the spec, but is convenient.
  251. // To avoid work we use link_and_eval_module however that can already be
  252. // dangerous if the vm loaded other modules.
  253. auto& vm = this->vm();
  254. TRY(vm.link_and_eval_module(Badge<Bytecode::Interpreter> {}, module));
  255. vm.run_queued_promise_jobs();
  256. vm.run_queued_finalization_registry_cleanup_jobs();
  257. return js_undefined();
  258. }
  259. Interpreter::HandleExceptionResponse Interpreter::handle_exception(size_t& program_counter, Value exception)
  260. {
  261. reg(Register::exception()) = exception;
  262. m_scheduled_jump = {};
  263. auto handlers = current_executable().exception_handlers_for_offset(program_counter);
  264. if (!handlers.has_value()) {
  265. return HandleExceptionResponse::ExitFromExecutable;
  266. }
  267. auto& handler = handlers->handler_offset;
  268. auto& finalizer = handlers->finalizer_offset;
  269. VERIFY(!vm().running_execution_context().unwind_contexts.is_empty());
  270. auto& unwind_context = vm().running_execution_context().unwind_contexts.last();
  271. VERIFY(unwind_context.executable == m_current_executable);
  272. if (handler.has_value()) {
  273. program_counter = handler.value();
  274. return HandleExceptionResponse::ContinueInThisExecutable;
  275. }
  276. if (finalizer.has_value()) {
  277. program_counter = finalizer.value();
  278. return HandleExceptionResponse::ContinueInThisExecutable;
  279. }
  280. VERIFY_NOT_REACHED();
  281. }
  282. // FIXME: GCC takes a *long* time to compile with flattening, and it will time out our CI. :|
  283. #if defined(AK_COMPILER_CLANG)
  284. # define FLATTEN_ON_CLANG FLATTEN
  285. #else
  286. # define FLATTEN_ON_CLANG
  287. #endif
  288. FLATTEN_ON_CLANG void Interpreter::run_bytecode(size_t entry_point)
  289. {
  290. if (vm().did_reach_stack_space_limit()) {
  291. reg(Register::exception()) = vm().throw_completion<InternalError>(ErrorType::CallStackSizeExceeded).release_value().value();
  292. return;
  293. }
  294. auto& running_execution_context = vm().running_execution_context();
  295. auto* arguments = running_execution_context.arguments.data();
  296. auto* locals = running_execution_context.locals.data();
  297. auto& accumulator = this->accumulator();
  298. auto& executable = current_executable();
  299. auto const* bytecode = executable.bytecode.data();
  300. size_t program_counter = entry_point;
  301. TemporaryChange change(m_program_counter, Optional<size_t&>(program_counter));
  302. // Declare a lookup table for computed goto with each of the `handle_*` labels
  303. // to avoid the overhead of a switch statement.
  304. // This is a GCC extension, but it's also supported by Clang.
  305. static void* const bytecode_dispatch_table[] = {
  306. #define SET_UP_LABEL(name) &&handle_##name,
  307. ENUMERATE_BYTECODE_OPS(SET_UP_LABEL)
  308. };
  309. #undef SET_UP_LABEL
  310. #define DISPATCH_NEXT(name) \
  311. do { \
  312. if constexpr (Op::name::IsVariableLength) \
  313. program_counter += instruction.length(); \
  314. else \
  315. program_counter += sizeof(Op::name); \
  316. auto& next_instruction = *reinterpret_cast<Instruction const*>(&bytecode[program_counter]); \
  317. goto* bytecode_dispatch_table[static_cast<size_t>(next_instruction.type())]; \
  318. } while (0)
  319. bool will_yield = false;
  320. for (;;) {
  321. start:
  322. for (;;) {
  323. goto* bytecode_dispatch_table[static_cast<size_t>((*reinterpret_cast<Instruction const*>(&bytecode[program_counter])).type())];
  324. handle_SetLocal: {
  325. auto& instruction = *reinterpret_cast<Op::SetLocal const*>(&bytecode[program_counter]);
  326. locals[instruction.index()] = get(instruction.src());
  327. DISPATCH_NEXT(SetLocal);
  328. }
  329. handle_GetArgument: {
  330. auto const& instruction = *reinterpret_cast<Op::GetArgument const*>(&bytecode[program_counter]);
  331. set(instruction.dst(), arguments[instruction.index()]);
  332. DISPATCH_NEXT(GetArgument);
  333. }
  334. handle_SetArgument: {
  335. auto const& instruction = *reinterpret_cast<Op::SetArgument const*>(&bytecode[program_counter]);
  336. arguments[instruction.index()] = get(instruction.src());
  337. DISPATCH_NEXT(SetArgument);
  338. }
  339. handle_Mov: {
  340. auto& instruction = *reinterpret_cast<Op::Mov const*>(&bytecode[program_counter]);
  341. set(instruction.dst(), get(instruction.src()));
  342. DISPATCH_NEXT(Mov);
  343. }
  344. handle_End: {
  345. auto& instruction = *reinterpret_cast<Op::End const*>(&bytecode[program_counter]);
  346. accumulator = get(instruction.value());
  347. return;
  348. }
  349. handle_Jump: {
  350. auto& instruction = *reinterpret_cast<Op::Jump const*>(&bytecode[program_counter]);
  351. program_counter = instruction.target().address();
  352. goto start;
  353. }
  354. handle_JumpIf: {
  355. auto& instruction = *reinterpret_cast<Op::JumpIf const*>(&bytecode[program_counter]);
  356. if (get(instruction.condition()).to_boolean())
  357. program_counter = instruction.true_target().address();
  358. else
  359. program_counter = instruction.false_target().address();
  360. goto start;
  361. }
  362. handle_JumpTrue: {
  363. auto& instruction = *reinterpret_cast<Op::JumpTrue const*>(&bytecode[program_counter]);
  364. if (get(instruction.condition()).to_boolean()) {
  365. program_counter = instruction.target().address();
  366. goto start;
  367. }
  368. DISPATCH_NEXT(JumpTrue);
  369. }
  370. handle_JumpFalse: {
  371. auto& instruction = *reinterpret_cast<Op::JumpFalse const*>(&bytecode[program_counter]);
  372. if (!get(instruction.condition()).to_boolean()) {
  373. program_counter = instruction.target().address();
  374. goto start;
  375. }
  376. DISPATCH_NEXT(JumpFalse);
  377. }
  378. handle_JumpNullish: {
  379. auto& instruction = *reinterpret_cast<Op::JumpNullish const*>(&bytecode[program_counter]);
  380. if (get(instruction.condition()).is_nullish())
  381. program_counter = instruction.true_target().address();
  382. else
  383. program_counter = instruction.false_target().address();
  384. goto start;
  385. }
  386. #define HANDLE_COMPARISON_OP(op_TitleCase, op_snake_case) \
  387. handle_Jump##op_TitleCase: \
  388. { \
  389. auto& instruction = *reinterpret_cast<Op::Jump##op_TitleCase const*>(&bytecode[program_counter]); \
  390. auto result = op_snake_case(vm(), get(instruction.lhs()), get(instruction.rhs())); \
  391. if (result.is_error()) { \
  392. if (handle_exception(program_counter, result.error_value()) == HandleExceptionResponse::ExitFromExecutable) \
  393. return; \
  394. goto start; \
  395. } \
  396. if (result.value().to_boolean()) \
  397. program_counter = instruction.true_target().address(); \
  398. else \
  399. program_counter = instruction.false_target().address(); \
  400. goto start; \
  401. }
  402. JS_ENUMERATE_COMPARISON_OPS(HANDLE_COMPARISON_OP)
  403. #undef HANDLE_COMPARISON_OP
  404. handle_JumpUndefined: {
  405. auto& instruction = *reinterpret_cast<Op::JumpUndefined const*>(&bytecode[program_counter]);
  406. if (get(instruction.condition()).is_undefined())
  407. program_counter = instruction.true_target().address();
  408. else
  409. program_counter = instruction.false_target().address();
  410. goto start;
  411. }
  412. handle_EnterUnwindContext: {
  413. auto& instruction = *reinterpret_cast<Op::EnterUnwindContext const*>(&bytecode[program_counter]);
  414. enter_unwind_context();
  415. program_counter = instruction.entry_point().address();
  416. goto start;
  417. }
  418. handle_ContinuePendingUnwind: {
  419. auto& instruction = *reinterpret_cast<Op::ContinuePendingUnwind const*>(&bytecode[program_counter]);
  420. if (auto exception = reg(Register::exception()); !exception.is_empty()) {
  421. if (handle_exception(program_counter, exception) == HandleExceptionResponse::ExitFromExecutable)
  422. return;
  423. goto start;
  424. }
  425. if (!saved_return_value().is_empty()) {
  426. do_return(saved_return_value());
  427. goto run_finalizer_and_return;
  428. }
  429. auto const old_scheduled_jump = running_execution_context.previously_scheduled_jumps.take_last();
  430. if (m_scheduled_jump.has_value()) {
  431. program_counter = m_scheduled_jump.value();
  432. m_scheduled_jump = {};
  433. } else {
  434. program_counter = instruction.resume_target().address();
  435. // set the scheduled jump to the old value if we continue
  436. // where we left it
  437. m_scheduled_jump = old_scheduled_jump;
  438. }
  439. goto start;
  440. }
  441. handle_ScheduleJump: {
  442. auto& instruction = *reinterpret_cast<Op::ScheduleJump const*>(&bytecode[program_counter]);
  443. m_scheduled_jump = instruction.target().address();
  444. auto finalizer = executable.exception_handlers_for_offset(program_counter).value().finalizer_offset;
  445. VERIFY(finalizer.has_value());
  446. program_counter = finalizer.value();
  447. goto start;
  448. }
  449. #define HANDLE_INSTRUCTION(name) \
  450. handle_##name: \
  451. { \
  452. auto& instruction = *reinterpret_cast<Op::name const*>(&bytecode[program_counter]); \
  453. { \
  454. auto result = instruction.execute_impl(*this); \
  455. if (result.is_error()) { \
  456. if (handle_exception(program_counter, result.error_value()) == HandleExceptionResponse::ExitFromExecutable) \
  457. return; \
  458. goto start; \
  459. } \
  460. } \
  461. DISPATCH_NEXT(name); \
  462. }
  463. #define HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(name) \
  464. handle_##name: \
  465. { \
  466. auto& instruction = *reinterpret_cast<Op::name const*>(&bytecode[program_counter]); \
  467. (void)instruction.execute_impl(*this); \
  468. DISPATCH_NEXT(name); \
  469. }
  470. HANDLE_INSTRUCTION(Add);
  471. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(AddPrivateName);
  472. HANDLE_INSTRUCTION(ArrayAppend);
  473. HANDLE_INSTRUCTION(AsyncIteratorClose);
  474. HANDLE_INSTRUCTION(BitwiseAnd);
  475. HANDLE_INSTRUCTION(BitwiseNot);
  476. HANDLE_INSTRUCTION(BitwiseOr);
  477. HANDLE_INSTRUCTION(BitwiseXor);
  478. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(BlockDeclarationInstantiation);
  479. HANDLE_INSTRUCTION(Call);
  480. HANDLE_INSTRUCTION(CallWithArgumentArray);
  481. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(Catch);
  482. HANDLE_INSTRUCTION(ConcatString);
  483. HANDLE_INSTRUCTION(CopyObjectExcludingProperties);
  484. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(CreateLexicalEnvironment);
  485. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(CreateVariableEnvironment);
  486. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(CreatePrivateEnvironment);
  487. HANDLE_INSTRUCTION(CreateVariable);
  488. HANDLE_INSTRUCTION(CreateRestParams);
  489. HANDLE_INSTRUCTION(CreateArguments);
  490. HANDLE_INSTRUCTION(Decrement);
  491. HANDLE_INSTRUCTION(DeleteById);
  492. HANDLE_INSTRUCTION(DeleteByIdWithThis);
  493. HANDLE_INSTRUCTION(DeleteByValue);
  494. HANDLE_INSTRUCTION(DeleteByValueWithThis);
  495. HANDLE_INSTRUCTION(DeleteVariable);
  496. HANDLE_INSTRUCTION(Div);
  497. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(Dump);
  498. HANDLE_INSTRUCTION(EnterObjectEnvironment);
  499. HANDLE_INSTRUCTION(Exp);
  500. HANDLE_INSTRUCTION(GetById);
  501. HANDLE_INSTRUCTION(GetByIdWithThis);
  502. HANDLE_INSTRUCTION(GetByValue);
  503. HANDLE_INSTRUCTION(GetByValueWithThis);
  504. HANDLE_INSTRUCTION(GetCalleeAndThisFromEnvironment);
  505. HANDLE_INSTRUCTION(GetGlobal);
  506. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(GetImportMeta);
  507. HANDLE_INSTRUCTION(GetIterator);
  508. HANDLE_INSTRUCTION(GetMethod);
  509. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(GetNewTarget);
  510. HANDLE_INSTRUCTION(GetNextMethodFromIteratorRecord);
  511. HANDLE_INSTRUCTION(GetObjectFromIteratorRecord);
  512. HANDLE_INSTRUCTION(GetObjectPropertyIterator);
  513. HANDLE_INSTRUCTION(GetPrivateById);
  514. HANDLE_INSTRUCTION(GetVariable);
  515. HANDLE_INSTRUCTION(GreaterThan);
  516. HANDLE_INSTRUCTION(GreaterThanEquals);
  517. HANDLE_INSTRUCTION(HasPrivateId);
  518. HANDLE_INSTRUCTION(ImportCall);
  519. HANDLE_INSTRUCTION(In);
  520. HANDLE_INSTRUCTION(Increment);
  521. HANDLE_INSTRUCTION(InstanceOf);
  522. HANDLE_INSTRUCTION(IteratorClose);
  523. HANDLE_INSTRUCTION(IteratorNext);
  524. HANDLE_INSTRUCTION(IteratorToArray);
  525. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(LeaveFinally);
  526. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(LeaveLexicalEnvironment);
  527. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(LeavePrivateEnvironment);
  528. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(LeaveUnwindContext);
  529. HANDLE_INSTRUCTION(LeftShift);
  530. HANDLE_INSTRUCTION(LessThan);
  531. HANDLE_INSTRUCTION(LessThanEquals);
  532. HANDLE_INSTRUCTION(LooselyEquals);
  533. HANDLE_INSTRUCTION(LooselyInequals);
  534. HANDLE_INSTRUCTION(Mod);
  535. HANDLE_INSTRUCTION(Mul);
  536. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(NewArray);
  537. HANDLE_INSTRUCTION(NewClass);
  538. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(NewFunction);
  539. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(NewObject);
  540. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(NewPrimitiveArray);
  541. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(NewRegExp);
  542. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(NewTypeError);
  543. HANDLE_INSTRUCTION(Not);
  544. HANDLE_INSTRUCTION(PostfixDecrement);
  545. HANDLE_INSTRUCTION(PostfixIncrement);
  546. HANDLE_INSTRUCTION(PutById);
  547. HANDLE_INSTRUCTION(PutByIdWithThis);
  548. HANDLE_INSTRUCTION(PutByValue);
  549. HANDLE_INSTRUCTION(PutByValueWithThis);
  550. HANDLE_INSTRUCTION(PutPrivateById);
  551. HANDLE_INSTRUCTION(ResolveSuperBase);
  552. HANDLE_INSTRUCTION(ResolveThisBinding);
  553. HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(RestoreScheduledJump);
  554. HANDLE_INSTRUCTION(RightShift);
  555. HANDLE_INSTRUCTION(SetVariable);
  556. HANDLE_INSTRUCTION(StrictlyEquals);
  557. HANDLE_INSTRUCTION(StrictlyInequals);
  558. HANDLE_INSTRUCTION(Sub);
  559. HANDLE_INSTRUCTION(SuperCallWithArgumentArray);
  560. HANDLE_INSTRUCTION(Throw);
  561. HANDLE_INSTRUCTION(ThrowIfNotObject);
  562. HANDLE_INSTRUCTION(ThrowIfNullish);
  563. HANDLE_INSTRUCTION(ThrowIfTDZ);
  564. HANDLE_INSTRUCTION(Typeof);
  565. HANDLE_INSTRUCTION(TypeofVariable);
  566. HANDLE_INSTRUCTION(UnaryMinus);
  567. HANDLE_INSTRUCTION(UnaryPlus);
  568. HANDLE_INSTRUCTION(UnsignedRightShift);
  569. handle_Await: {
  570. auto& instruction = *reinterpret_cast<Op::Await const*>(&bytecode[program_counter]);
  571. (void)instruction.execute_impl(*this);
  572. will_yield = true;
  573. goto run_finalizer_and_return;
  574. }
  575. handle_Return: {
  576. auto& instruction = *reinterpret_cast<Op::Return const*>(&bytecode[program_counter]);
  577. (void)instruction.execute_impl(*this);
  578. goto run_finalizer_and_return;
  579. }
  580. handle_Yield: {
  581. auto& instruction = *reinterpret_cast<Op::Yield const*>(&bytecode[program_counter]);
  582. (void)instruction.execute_impl(*this);
  583. // Note: A `yield` statement will not go through a finally statement,
  584. // hence we need to set a flag to not do so,
  585. // but we generate a Yield Operation in the case of returns in
  586. // generators as well, so we need to check if it will actually
  587. // continue or is a `return` in disguise
  588. will_yield = instruction.continuation().has_value();
  589. goto run_finalizer_and_return;
  590. }
  591. }
  592. }
  593. run_finalizer_and_return:
  594. if (!will_yield) {
  595. if (auto handlers = executable.exception_handlers_for_offset(program_counter); handlers.has_value()) {
  596. if (auto finalizer = handlers.value().finalizer_offset; finalizer.has_value()) {
  597. VERIFY(!running_execution_context.unwind_contexts.is_empty());
  598. auto& unwind_context = running_execution_context.unwind_contexts.last();
  599. VERIFY(unwind_context.executable == m_current_executable);
  600. reg(Register::saved_return_value()) = reg(Register::return_value());
  601. reg(Register::return_value()) = {};
  602. program_counter = finalizer.value();
  603. // the unwind_context will be pop'ed when entering the finally block
  604. goto start;
  605. }
  606. }
  607. }
  608. }
  609. Interpreter::ResultAndReturnRegister Interpreter::run_executable(Executable& executable, Optional<size_t> entry_point, Value initial_accumulator_value)
  610. {
  611. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable);
  612. TemporaryChange restore_executable { m_current_executable, GCPtr { executable } };
  613. TemporaryChange restore_saved_jump { m_scheduled_jump, Optional<size_t> {} };
  614. TemporaryChange restore_realm { m_realm, GCPtr { vm().current_realm() } };
  615. TemporaryChange restore_global_object { m_global_object, GCPtr { m_realm->global_object() } };
  616. TemporaryChange restore_global_declarative_environment { m_global_declarative_environment, GCPtr { m_realm->global_environment().declarative_record() } };
  617. VERIFY(!vm().execution_context_stack().is_empty());
  618. auto& running_execution_context = vm().running_execution_context();
  619. if (running_execution_context.registers.size() < executable.number_of_registers)
  620. running_execution_context.registers.resize(executable.number_of_registers);
  621. TemporaryChange restore_arguments { m_arguments, running_execution_context.arguments.span() };
  622. TemporaryChange restore_registers { m_registers, running_execution_context.registers.span() };
  623. TemporaryChange restore_locals { m_locals, running_execution_context.locals.span() };
  624. TemporaryChange restore_constants { m_constants, executable.constants.span() };
  625. reg(Register::accumulator()) = initial_accumulator_value;
  626. reg(Register::return_value()) = {};
  627. running_execution_context.executable = &executable;
  628. run_bytecode(entry_point.value_or(0));
  629. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable);
  630. if constexpr (JS_BYTECODE_DEBUG) {
  631. for (size_t i = 0; i < registers().size(); ++i) {
  632. String value_string;
  633. if (registers()[i].is_empty())
  634. value_string = "(empty)"_string;
  635. else
  636. value_string = registers()[i].to_string_without_side_effects();
  637. dbgln("[{:3}] {}", i, value_string);
  638. }
  639. }
  640. auto return_value = js_undefined();
  641. if (!reg(Register::return_value()).is_empty())
  642. return_value = reg(Register::return_value());
  643. else if (!reg(Register::saved_return_value()).is_empty())
  644. return_value = reg(Register::saved_return_value());
  645. auto exception = reg(Register::exception());
  646. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  647. // in which case this is a no-op.
  648. vm().run_queued_promise_jobs();
  649. vm().finish_execution_generation();
  650. if (!exception.is_empty())
  651. return { throw_completion(exception), vm().running_execution_context().registers[0] };
  652. return { return_value, vm().running_execution_context().registers[0] };
  653. }
  654. void Interpreter::enter_unwind_context()
  655. {
  656. auto& running_execution_context = vm().running_execution_context();
  657. running_execution_context.unwind_contexts.empend(
  658. m_current_executable,
  659. vm().running_execution_context().lexical_environment);
  660. running_execution_context.previously_scheduled_jumps.append(m_scheduled_jump);
  661. m_scheduled_jump = {};
  662. }
  663. void Interpreter::leave_unwind_context()
  664. {
  665. auto& running_execution_context = vm().running_execution_context();
  666. running_execution_context.unwind_contexts.take_last();
  667. }
  668. void Interpreter::catch_exception(Operand dst)
  669. {
  670. set(dst, reg(Register::exception()));
  671. reg(Register::exception()) = {};
  672. auto& running_execution_context = vm().running_execution_context();
  673. auto& context = running_execution_context.unwind_contexts.last();
  674. VERIFY(!context.handler_called);
  675. VERIFY(context.executable == &current_executable());
  676. context.handler_called = true;
  677. vm().running_execution_context().lexical_environment = context.lexical_environment;
  678. }
  679. void Interpreter::restore_scheduled_jump()
  680. {
  681. m_scheduled_jump = vm().running_execution_context().previously_scheduled_jumps.take_last();
  682. }
  683. void Interpreter::leave_finally()
  684. {
  685. reg(Register::exception()) = {};
  686. m_scheduled_jump = vm().running_execution_context().previously_scheduled_jumps.take_last();
  687. }
  688. void Interpreter::enter_object_environment(Object& object)
  689. {
  690. auto& running_execution_context = vm().running_execution_context();
  691. auto& old_environment = running_execution_context.lexical_environment;
  692. running_execution_context.saved_lexical_environments.append(old_environment);
  693. running_execution_context.lexical_environment = new_object_environment(object, true, old_environment);
  694. }
  695. ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM& vm, ASTNode const& node, FunctionKind kind, DeprecatedFlyString const& name)
  696. {
  697. auto executable_result = Bytecode::Generator::generate_from_ast_node(vm, node, kind);
  698. if (executable_result.is_error())
  699. return vm.throw_completion<InternalError>(ErrorType::NotImplemented, TRY_OR_THROW_OOM(vm, executable_result.error().to_string()));
  700. auto bytecode_executable = executable_result.release_value();
  701. bytecode_executable->name = name;
  702. if (Bytecode::g_dump_bytecode)
  703. bytecode_executable->dump();
  704. return bytecode_executable;
  705. }
  706. ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM& vm, ECMAScriptFunctionObject const& function)
  707. {
  708. auto const& name = function.name();
  709. auto executable_result = Bytecode::Generator::generate_from_function(vm, function);
  710. if (executable_result.is_error())
  711. return vm.throw_completion<InternalError>(ErrorType::NotImplemented, TRY_OR_THROW_OOM(vm, executable_result.error().to_string()));
  712. auto bytecode_executable = executable_result.release_value();
  713. bytecode_executable->name = name;
  714. if (Bytecode::g_dump_bytecode)
  715. bytecode_executable->dump();
  716. return bytecode_executable;
  717. }
  718. }
  719. namespace JS::Bytecode {
  720. ByteString Instruction::to_byte_string(Bytecode::Executable const& executable) const
  721. {
  722. #define __BYTECODE_OP(op) \
  723. case Instruction::Type::op: \
  724. return static_cast<Bytecode::Op::op const&>(*this).to_byte_string_impl(executable);
  725. switch (type()) {
  726. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  727. default:
  728. VERIFY_NOT_REACHED();
  729. }
  730. #undef __BYTECODE_OP
  731. }
  732. }
  733. namespace JS::Bytecode::Op {
  734. static void dump_object(Object& o, HashTable<Object const*>& seen, int indent = 0)
  735. {
  736. if (seen.contains(&o))
  737. return;
  738. seen.set(&o);
  739. for (auto& it : o.shape().property_table()) {
  740. auto value = o.get_direct(it.value.offset);
  741. dbgln("{} {} -> {}", String::repeated(' ', indent).release_value(), it.key.to_display_string(), value);
  742. if (value.is_object()) {
  743. dump_object(value.as_object(), seen, indent + 2);
  744. }
  745. }
  746. }
  747. void Dump::execute_impl(Bytecode::Interpreter& interpreter) const
  748. {
  749. auto value = interpreter.get(m_value);
  750. dbgln("(DUMP) {}: {}", m_text, value);
  751. if (value.is_object()) {
  752. HashTable<Object const*> seen;
  753. dump_object(value.as_object(), seen);
  754. }
  755. }
  756. ThrowCompletionOr<void> End::execute_impl(Bytecode::Interpreter&) const
  757. {
  758. // Handled in the interpreter loop.
  759. __builtin_unreachable();
  760. }
  761. #define JS_DEFINE_EXECUTE_FOR_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  762. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  763. { \
  764. auto& vm = interpreter.vm(); \
  765. auto lhs = interpreter.get(m_lhs); \
  766. auto rhs = interpreter.get(m_rhs); \
  767. interpreter.set(m_dst, TRY(op_snake_case(vm, lhs, rhs))); \
  768. return {}; \
  769. }
  770. #define JS_DEFINE_TO_BYTE_STRING_FOR_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  771. ByteString OpTitleCase::to_byte_string_impl(Bytecode::Executable const& executable) const \
  772. { \
  773. return ByteString::formatted(#OpTitleCase " {}, {}, {}", \
  774. format_operand("dst"sv, m_dst, executable), \
  775. format_operand("lhs"sv, m_lhs, executable), \
  776. format_operand("rhs"sv, m_rhs, executable)); \
  777. }
  778. JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(JS_DEFINE_EXECUTE_FOR_COMMON_BINARY_OP)
  779. JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(JS_DEFINE_TO_BYTE_STRING_FOR_COMMON_BINARY_OP)
  780. JS_ENUMERATE_COMMON_BINARY_OPS_WITH_FAST_PATH(JS_DEFINE_TO_BYTE_STRING_FOR_COMMON_BINARY_OP)
  781. ThrowCompletionOr<void> Add::execute_impl(Bytecode::Interpreter& interpreter) const
  782. {
  783. auto& vm = interpreter.vm();
  784. auto const lhs = interpreter.get(m_lhs);
  785. auto const rhs = interpreter.get(m_rhs);
  786. if (lhs.is_number() && rhs.is_number()) {
  787. if (lhs.is_int32() && rhs.is_int32()) {
  788. if (!Checked<i32>::addition_would_overflow(lhs.as_i32(), rhs.as_i32())) {
  789. interpreter.set(m_dst, Value(lhs.as_i32() + rhs.as_i32()));
  790. return {};
  791. }
  792. }
  793. interpreter.set(m_dst, Value(lhs.as_double() + rhs.as_double()));
  794. return {};
  795. }
  796. interpreter.set(m_dst, TRY(add(vm, lhs, rhs)));
  797. return {};
  798. }
  799. ThrowCompletionOr<void> Mul::execute_impl(Bytecode::Interpreter& interpreter) const
  800. {
  801. auto& vm = interpreter.vm();
  802. auto const lhs = interpreter.get(m_lhs);
  803. auto const rhs = interpreter.get(m_rhs);
  804. if (lhs.is_number() && rhs.is_number()) {
  805. if (lhs.is_int32() && rhs.is_int32()) {
  806. if (!Checked<i32>::multiplication_would_overflow(lhs.as_i32(), rhs.as_i32())) {
  807. interpreter.set(m_dst, Value(lhs.as_i32() * rhs.as_i32()));
  808. return {};
  809. }
  810. }
  811. interpreter.set(m_dst, Value(lhs.as_double() * rhs.as_double()));
  812. return {};
  813. }
  814. interpreter.set(m_dst, TRY(mul(vm, lhs, rhs)));
  815. return {};
  816. }
  817. ThrowCompletionOr<void> Sub::execute_impl(Bytecode::Interpreter& interpreter) const
  818. {
  819. auto& vm = interpreter.vm();
  820. auto const lhs = interpreter.get(m_lhs);
  821. auto const rhs = interpreter.get(m_rhs);
  822. if (lhs.is_number() && rhs.is_number()) {
  823. if (lhs.is_int32() && rhs.is_int32()) {
  824. if (!Checked<i32>::addition_would_overflow(lhs.as_i32(), -rhs.as_i32())) {
  825. interpreter.set(m_dst, Value(lhs.as_i32() - rhs.as_i32()));
  826. return {};
  827. }
  828. }
  829. interpreter.set(m_dst, Value(lhs.as_double() - rhs.as_double()));
  830. return {};
  831. }
  832. interpreter.set(m_dst, TRY(sub(vm, lhs, rhs)));
  833. return {};
  834. }
  835. ThrowCompletionOr<void> BitwiseXor::execute_impl(Bytecode::Interpreter& interpreter) const
  836. {
  837. auto& vm = interpreter.vm();
  838. auto const lhs = interpreter.get(m_lhs);
  839. auto const rhs = interpreter.get(m_rhs);
  840. if (lhs.is_int32() && rhs.is_int32()) {
  841. interpreter.set(m_dst, Value(lhs.as_i32() ^ rhs.as_i32()));
  842. return {};
  843. }
  844. interpreter.set(m_dst, TRY(bitwise_xor(vm, lhs, rhs)));
  845. return {};
  846. }
  847. ThrowCompletionOr<void> BitwiseAnd::execute_impl(Bytecode::Interpreter& interpreter) const
  848. {
  849. auto& vm = interpreter.vm();
  850. auto const lhs = interpreter.get(m_lhs);
  851. auto const rhs = interpreter.get(m_rhs);
  852. if (lhs.is_int32() && rhs.is_int32()) {
  853. interpreter.set(m_dst, Value(lhs.as_i32() & rhs.as_i32()));
  854. return {};
  855. }
  856. interpreter.set(m_dst, TRY(bitwise_and(vm, lhs, rhs)));
  857. return {};
  858. }
  859. ThrowCompletionOr<void> BitwiseOr::execute_impl(Bytecode::Interpreter& interpreter) const
  860. {
  861. auto& vm = interpreter.vm();
  862. auto const lhs = interpreter.get(m_lhs);
  863. auto const rhs = interpreter.get(m_rhs);
  864. if (lhs.is_int32() && rhs.is_int32()) {
  865. interpreter.set(m_dst, Value(lhs.as_i32() | rhs.as_i32()));
  866. return {};
  867. }
  868. interpreter.set(m_dst, TRY(bitwise_or(vm, lhs, rhs)));
  869. return {};
  870. }
  871. ThrowCompletionOr<void> UnsignedRightShift::execute_impl(Bytecode::Interpreter& interpreter) const
  872. {
  873. auto& vm = interpreter.vm();
  874. auto const lhs = interpreter.get(m_lhs);
  875. auto const rhs = interpreter.get(m_rhs);
  876. if (lhs.is_int32() && rhs.is_int32()) {
  877. auto const shift_count = static_cast<u32>(rhs.as_i32()) % 32;
  878. interpreter.set(m_dst, Value(static_cast<u32>(lhs.as_i32()) >> shift_count));
  879. return {};
  880. }
  881. interpreter.set(m_dst, TRY(unsigned_right_shift(vm, lhs, rhs)));
  882. return {};
  883. }
  884. ThrowCompletionOr<void> RightShift::execute_impl(Bytecode::Interpreter& interpreter) const
  885. {
  886. auto& vm = interpreter.vm();
  887. auto const lhs = interpreter.get(m_lhs);
  888. auto const rhs = interpreter.get(m_rhs);
  889. if (lhs.is_int32() && rhs.is_int32()) {
  890. auto const shift_count = static_cast<u32>(rhs.as_i32()) % 32;
  891. interpreter.set(m_dst, Value(lhs.as_i32() >> shift_count));
  892. return {};
  893. }
  894. interpreter.set(m_dst, TRY(right_shift(vm, lhs, rhs)));
  895. return {};
  896. }
  897. ThrowCompletionOr<void> LeftShift::execute_impl(Bytecode::Interpreter& interpreter) const
  898. {
  899. auto& vm = interpreter.vm();
  900. auto const lhs = interpreter.get(m_lhs);
  901. auto const rhs = interpreter.get(m_rhs);
  902. if (lhs.is_int32() && rhs.is_int32()) {
  903. auto const shift_count = static_cast<u32>(rhs.as_i32()) % 32;
  904. interpreter.set(m_dst, Value(lhs.as_i32() << shift_count));
  905. return {};
  906. }
  907. interpreter.set(m_dst, TRY(left_shift(vm, lhs, rhs)));
  908. return {};
  909. }
  910. ThrowCompletionOr<void> LessThan::execute_impl(Bytecode::Interpreter& interpreter) const
  911. {
  912. auto& vm = interpreter.vm();
  913. auto const lhs = interpreter.get(m_lhs);
  914. auto const rhs = interpreter.get(m_rhs);
  915. if (lhs.is_int32() && rhs.is_int32()) {
  916. interpreter.set(m_dst, Value(lhs.as_i32() < rhs.as_i32()));
  917. return {};
  918. }
  919. interpreter.set(m_dst, TRY(less_than(vm, lhs, rhs)));
  920. return {};
  921. }
  922. ThrowCompletionOr<void> LessThanEquals::execute_impl(Bytecode::Interpreter& interpreter) const
  923. {
  924. auto& vm = interpreter.vm();
  925. auto const lhs = interpreter.get(m_lhs);
  926. auto const rhs = interpreter.get(m_rhs);
  927. if (lhs.is_int32() && rhs.is_int32()) {
  928. interpreter.set(m_dst, Value(lhs.as_i32() <= rhs.as_i32()));
  929. return {};
  930. }
  931. interpreter.set(m_dst, TRY(less_than_equals(vm, lhs, rhs)));
  932. return {};
  933. }
  934. ThrowCompletionOr<void> GreaterThan::execute_impl(Bytecode::Interpreter& interpreter) const
  935. {
  936. auto& vm = interpreter.vm();
  937. auto const lhs = interpreter.get(m_lhs);
  938. auto const rhs = interpreter.get(m_rhs);
  939. if (lhs.is_int32() && rhs.is_int32()) {
  940. interpreter.set(m_dst, Value(lhs.as_i32() > rhs.as_i32()));
  941. return {};
  942. }
  943. interpreter.set(m_dst, TRY(greater_than(vm, lhs, rhs)));
  944. return {};
  945. }
  946. ThrowCompletionOr<void> GreaterThanEquals::execute_impl(Bytecode::Interpreter& interpreter) const
  947. {
  948. auto& vm = interpreter.vm();
  949. auto const lhs = interpreter.get(m_lhs);
  950. auto const rhs = interpreter.get(m_rhs);
  951. if (lhs.is_int32() && rhs.is_int32()) {
  952. interpreter.set(m_dst, Value(lhs.as_i32() >= rhs.as_i32()));
  953. return {};
  954. }
  955. interpreter.set(m_dst, TRY(greater_than_equals(vm, lhs, rhs)));
  956. return {};
  957. }
  958. static ThrowCompletionOr<Value> not_(VM&, Value value)
  959. {
  960. return Value(!value.to_boolean());
  961. }
  962. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  963. {
  964. return PrimitiveString::create(vm, value.typeof());
  965. }
  966. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  967. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  968. { \
  969. auto& vm = interpreter.vm(); \
  970. interpreter.set(dst(), TRY(op_snake_case(vm, interpreter.get(src())))); \
  971. return {}; \
  972. } \
  973. ByteString OpTitleCase::to_byte_string_impl(Bytecode::Executable const& executable) const \
  974. { \
  975. return ByteString::formatted(#OpTitleCase " {}, {}", \
  976. format_operand("dst"sv, dst(), executable), \
  977. format_operand("src"sv, src(), executable)); \
  978. }
  979. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  980. void NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  981. {
  982. auto array = MUST(Array::create(interpreter.realm(), 0));
  983. for (size_t i = 0; i < m_element_count; i++) {
  984. array->indexed_properties().put(i, interpreter.get(m_elements[i]), default_attributes);
  985. }
  986. interpreter.set(dst(), array);
  987. }
  988. void NewPrimitiveArray::execute_impl(Bytecode::Interpreter& interpreter) const
  989. {
  990. auto array = MUST(Array::create(interpreter.realm(), 0));
  991. for (size_t i = 0; i < m_element_count; i++)
  992. array->indexed_properties().put(i, m_elements[i], default_attributes);
  993. interpreter.set(dst(), array);
  994. }
  995. void AddPrivateName::execute_impl(Bytecode::Interpreter& interpreter) const
  996. {
  997. auto const& name = interpreter.current_executable().get_identifier(m_name);
  998. interpreter.vm().running_execution_context().private_environment->add_private_name(name);
  999. }
  1000. ThrowCompletionOr<void> ArrayAppend::execute_impl(Bytecode::Interpreter& interpreter) const
  1001. {
  1002. return append(interpreter.vm(), interpreter.get(dst()), interpreter.get(src()), m_is_spread);
  1003. }
  1004. ThrowCompletionOr<void> ImportCall::execute_impl(Bytecode::Interpreter& interpreter) const
  1005. {
  1006. auto& vm = interpreter.vm();
  1007. auto specifier = interpreter.get(m_specifier);
  1008. auto options_value = interpreter.get(m_options);
  1009. interpreter.set(dst(), TRY(perform_import_call(vm, specifier, options_value)));
  1010. return {};
  1011. }
  1012. ThrowCompletionOr<void> IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const
  1013. {
  1014. interpreter.set(dst(), TRY(iterator_to_array(interpreter.vm(), interpreter.get(iterator()))));
  1015. return {};
  1016. }
  1017. void NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  1018. {
  1019. auto& vm = interpreter.vm();
  1020. auto& realm = *vm.current_realm();
  1021. interpreter.set(dst(), Object::create(realm, realm.intrinsics().object_prototype()));
  1022. }
  1023. void NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
  1024. {
  1025. interpreter.set(dst(),
  1026. new_regexp(
  1027. interpreter.vm(),
  1028. interpreter.current_executable().regex_table->get(m_regex_index),
  1029. interpreter.current_executable().get_string(m_source_index),
  1030. interpreter.current_executable().get_string(m_flags_index)));
  1031. }
  1032. #define JS_DEFINE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  1033. void New##ErrorName::execute_impl(Bytecode::Interpreter& interpreter) const \
  1034. { \
  1035. auto& vm = interpreter.vm(); \
  1036. auto& realm = *vm.current_realm(); \
  1037. interpreter.set(dst(), ErrorName::create(realm, interpreter.current_executable().get_string(m_error_string))); \
  1038. } \
  1039. ByteString New##ErrorName::to_byte_string_impl(Bytecode::Executable const& executable) const \
  1040. { \
  1041. return ByteString::formatted("New" #ErrorName " {}, {}", \
  1042. format_operand("dst"sv, m_dst, executable), \
  1043. executable.string_table->get(m_error_string)); \
  1044. }
  1045. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DEFINE_NEW_BUILTIN_ERROR_OP)
  1046. ThrowCompletionOr<void> CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const
  1047. {
  1048. auto& vm = interpreter.vm();
  1049. auto& realm = *vm.current_realm();
  1050. auto from_object = interpreter.get(m_from_object);
  1051. auto to_object = Object::create(realm, realm.intrinsics().object_prototype());
  1052. HashTable<PropertyKey> excluded_names;
  1053. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  1054. excluded_names.set(TRY(interpreter.get(m_excluded_names[i]).to_property_key(vm)));
  1055. }
  1056. TRY(to_object->copy_data_properties(vm, from_object, excluded_names));
  1057. interpreter.set(dst(), to_object);
  1058. return {};
  1059. }
  1060. ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  1061. {
  1062. auto& vm = interpreter.vm();
  1063. auto string = TRY(interpreter.get(src()).to_primitive_string(vm));
  1064. interpreter.set(dst(), PrimitiveString::create(vm, interpreter.get(dst()).as_string(), string));
  1065. return {};
  1066. }
  1067. ThrowCompletionOr<void> GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  1068. {
  1069. interpreter.set(dst(), TRY(get_variable(interpreter, interpreter.current_executable().get_identifier(m_identifier), interpreter.current_executable().environment_variable_caches[m_cache_index])));
  1070. return {};
  1071. }
  1072. ThrowCompletionOr<void> GetCalleeAndThisFromEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  1073. {
  1074. auto callee_and_this = TRY(get_callee_and_this_from_environment(
  1075. interpreter,
  1076. interpreter.current_executable().get_identifier(m_identifier),
  1077. interpreter.current_executable().environment_variable_caches[m_cache_index]));
  1078. interpreter.set(m_callee, callee_and_this.callee);
  1079. interpreter.set(m_this_value, callee_and_this.this_value);
  1080. return {};
  1081. }
  1082. ThrowCompletionOr<void> GetGlobal::execute_impl(Bytecode::Interpreter& interpreter) const
  1083. {
  1084. interpreter.set(dst(), TRY(get_global(interpreter, interpreter.current_executable().get_identifier(m_identifier), interpreter.current_executable().global_variable_caches[m_cache_index])));
  1085. return {};
  1086. }
  1087. ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  1088. {
  1089. auto& vm = interpreter.vm();
  1090. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  1091. auto reference = TRY(vm.resolve_binding(string));
  1092. interpreter.set(dst(), Value(TRY(reference.delete_(vm))));
  1093. return {};
  1094. }
  1095. void CreateLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  1096. {
  1097. auto make_and_swap_envs = [&](auto& old_environment) {
  1098. auto declarative_environment = new_declarative_environment(*old_environment).ptr();
  1099. declarative_environment->ensure_capacity(m_capacity);
  1100. GCPtr<Environment> environment = declarative_environment;
  1101. swap(old_environment, environment);
  1102. return environment;
  1103. };
  1104. auto& running_execution_context = interpreter.vm().running_execution_context();
  1105. running_execution_context.saved_lexical_environments.append(make_and_swap_envs(running_execution_context.lexical_environment));
  1106. }
  1107. void CreatePrivateEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  1108. {
  1109. auto& running_execution_context = interpreter.vm().running_execution_context();
  1110. auto outer_private_environment = running_execution_context.private_environment;
  1111. running_execution_context.private_environment = new_private_environment(interpreter.vm(), outer_private_environment);
  1112. }
  1113. ThrowCompletionOr<void> CreateVariableEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  1114. {
  1115. auto& running_execution_context = interpreter.vm().running_execution_context();
  1116. auto var_environment = new_declarative_environment(*running_execution_context.lexical_environment);
  1117. var_environment->ensure_capacity(m_capacity);
  1118. running_execution_context.variable_environment = var_environment;
  1119. running_execution_context.lexical_environment = var_environment;
  1120. return {};
  1121. }
  1122. ThrowCompletionOr<void> EnterObjectEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  1123. {
  1124. auto object = TRY(interpreter.get(m_object).to_object(interpreter.vm()));
  1125. interpreter.enter_object_environment(*object);
  1126. return {};
  1127. }
  1128. void Catch::execute_impl(Bytecode::Interpreter& interpreter) const
  1129. {
  1130. interpreter.catch_exception(dst());
  1131. }
  1132. void LeaveFinally::execute_impl(Bytecode::Interpreter& interpreter) const
  1133. {
  1134. interpreter.leave_finally();
  1135. }
  1136. void RestoreScheduledJump::execute_impl(Bytecode::Interpreter& interpreter) const
  1137. {
  1138. interpreter.restore_scheduled_jump();
  1139. }
  1140. ThrowCompletionOr<void> CreateVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  1141. {
  1142. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  1143. return create_variable(interpreter.vm(), name, m_mode, m_is_global, m_is_immutable, m_is_strict);
  1144. }
  1145. ThrowCompletionOr<void> CreateRestParams::execute_impl(Bytecode::Interpreter& interpreter) const
  1146. {
  1147. auto const& arguments = interpreter.vm().running_execution_context().arguments;
  1148. auto arguments_count = interpreter.vm().running_execution_context().passed_argument_count;
  1149. auto array = MUST(Array::create(interpreter.realm(), 0));
  1150. for (size_t rest_index = m_rest_index; rest_index < arguments_count; ++rest_index)
  1151. array->indexed_properties().append(arguments[rest_index]);
  1152. interpreter.set(m_dst, array);
  1153. return {};
  1154. }
  1155. ThrowCompletionOr<void> CreateArguments::execute_impl(Bytecode::Interpreter& interpreter) const
  1156. {
  1157. auto const& function = interpreter.vm().running_execution_context().function;
  1158. auto const& arguments = interpreter.vm().running_execution_context().arguments;
  1159. auto const& environment = interpreter.vm().running_execution_context().lexical_environment;
  1160. auto passed_arguments = ReadonlySpan<Value> { arguments.data(), interpreter.vm().running_execution_context().passed_argument_count };
  1161. Object* arguments_object;
  1162. if (m_kind == Kind::Mapped) {
  1163. arguments_object = create_mapped_arguments_object(interpreter.vm(), *function, function->formal_parameters(), passed_arguments, *environment);
  1164. } else {
  1165. arguments_object = create_unmapped_arguments_object(interpreter.vm(), passed_arguments);
  1166. }
  1167. if (m_is_immutable) {
  1168. MUST(environment->create_immutable_binding(interpreter.vm(), interpreter.vm().names.arguments.as_string(), false));
  1169. } else {
  1170. MUST(environment->create_mutable_binding(interpreter.vm(), interpreter.vm().names.arguments.as_string(), false));
  1171. }
  1172. MUST(environment->initialize_binding(interpreter.vm(), interpreter.vm().names.arguments.as_string(), arguments_object, Environment::InitializeBindingHint::Normal));
  1173. return {};
  1174. }
  1175. ThrowCompletionOr<void> SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  1176. {
  1177. auto& vm = interpreter.vm();
  1178. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  1179. TRY(set_variable(vm,
  1180. name,
  1181. interpreter.get(src()),
  1182. m_mode,
  1183. m_initialization_mode,
  1184. interpreter.current_executable().environment_variable_caches[m_cache_index]));
  1185. return {};
  1186. }
  1187. ThrowCompletionOr<void> SetLocal::execute_impl(Bytecode::Interpreter&) const
  1188. {
  1189. // Handled in the interpreter loop.
  1190. __builtin_unreachable();
  1191. }
  1192. ThrowCompletionOr<void> SetArgument::execute_impl(Bytecode::Interpreter&) const
  1193. {
  1194. // Handled in the interpreter loop.
  1195. __builtin_unreachable();
  1196. }
  1197. ThrowCompletionOr<void> GetArgument::execute_impl(Bytecode::Interpreter&) const
  1198. {
  1199. // Handled in the interpreter loop.
  1200. __builtin_unreachable();
  1201. }
  1202. ThrowCompletionOr<void> GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  1203. {
  1204. auto base_identifier = interpreter.current_executable().get_identifier(m_base_identifier);
  1205. auto const& property_identifier = interpreter.current_executable().get_identifier(m_property);
  1206. auto base_value = interpreter.get(base());
  1207. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  1208. interpreter.set(dst(), TRY(get_by_id(interpreter.vm(), base_identifier, property_identifier, base_value, base_value, cache)));
  1209. return {};
  1210. }
  1211. ThrowCompletionOr<void> GetByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  1212. {
  1213. auto base_value = interpreter.get(m_base);
  1214. auto this_value = interpreter.get(m_this_value);
  1215. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  1216. interpreter.set(dst(), TRY(get_by_id(interpreter.vm(), {}, interpreter.current_executable().get_identifier(m_property), base_value, this_value, cache)));
  1217. return {};
  1218. }
  1219. ThrowCompletionOr<void> GetPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const
  1220. {
  1221. auto& vm = interpreter.vm();
  1222. auto const& name = interpreter.current_executable().get_identifier(m_property);
  1223. auto base_value = interpreter.get(m_base);
  1224. auto private_reference = make_private_reference(vm, base_value, name);
  1225. interpreter.set(dst(), TRY(private_reference.get_value(vm)));
  1226. return {};
  1227. }
  1228. ThrowCompletionOr<void> HasPrivateId::execute_impl(Bytecode::Interpreter& interpreter) const
  1229. {
  1230. auto& vm = interpreter.vm();
  1231. auto base = interpreter.get(m_base);
  1232. if (!base.is_object())
  1233. return vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject);
  1234. auto private_environment = vm.running_execution_context().private_environment;
  1235. VERIFY(private_environment);
  1236. auto private_name = private_environment->resolve_private_identifier(interpreter.current_executable().get_identifier(m_property));
  1237. interpreter.set(dst(), Value(base.as_object().private_element_find(private_name) != nullptr));
  1238. return {};
  1239. }
  1240. ThrowCompletionOr<void> PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  1241. {
  1242. auto& vm = interpreter.vm();
  1243. auto value = interpreter.get(m_src);
  1244. auto base = interpreter.get(m_base);
  1245. auto base_identifier = interpreter.current_executable().get_identifier(m_base_identifier);
  1246. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  1247. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  1248. TRY(put_by_property_key(vm, base, base, value, base_identifier, name, m_kind, &cache));
  1249. return {};
  1250. }
  1251. ThrowCompletionOr<void> PutByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  1252. {
  1253. auto& vm = interpreter.vm();
  1254. auto value = interpreter.get(m_src);
  1255. auto base = interpreter.get(m_base);
  1256. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  1257. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  1258. TRY(put_by_property_key(vm, base, interpreter.get(m_this_value), value, {}, name, m_kind, &cache));
  1259. return {};
  1260. }
  1261. ThrowCompletionOr<void> PutPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const
  1262. {
  1263. auto& vm = interpreter.vm();
  1264. auto value = interpreter.get(m_src);
  1265. auto object = TRY(interpreter.get(m_base).to_object(vm));
  1266. auto name = interpreter.current_executable().get_identifier(m_property);
  1267. auto private_reference = make_private_reference(vm, object, name);
  1268. TRY(private_reference.put_value(vm, value));
  1269. return {};
  1270. }
  1271. ThrowCompletionOr<void> DeleteById::execute_impl(Bytecode::Interpreter& interpreter) const
  1272. {
  1273. auto base_value = interpreter.get(m_base);
  1274. interpreter.set(dst(), TRY(Bytecode::delete_by_id(interpreter, base_value, m_property)));
  1275. return {};
  1276. }
  1277. ThrowCompletionOr<void> DeleteByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  1278. {
  1279. auto& vm = interpreter.vm();
  1280. auto base_value = interpreter.get(m_base);
  1281. auto const& identifier = interpreter.current_executable().get_identifier(m_property);
  1282. bool strict = vm.in_strict_mode();
  1283. auto reference = Reference { base_value, identifier, interpreter.get(m_this_value), strict };
  1284. interpreter.set(dst(), Value(TRY(reference.delete_(vm))));
  1285. return {};
  1286. }
  1287. ThrowCompletionOr<void> Jump::execute_impl(Bytecode::Interpreter&) const
  1288. {
  1289. // Handled in the interpreter loop.
  1290. __builtin_unreachable();
  1291. }
  1292. ThrowCompletionOr<void> ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const
  1293. {
  1294. auto& cached_this_value = interpreter.reg(Register::this_value());
  1295. if (cached_this_value.is_empty()) {
  1296. // OPTIMIZATION: Because the value of 'this' cannot be reassigned during a function execution, it's
  1297. // resolved once and then saved for subsequent use.
  1298. auto& vm = interpreter.vm();
  1299. cached_this_value = TRY(vm.resolve_this_binding());
  1300. }
  1301. interpreter.set(dst(), cached_this_value);
  1302. return {};
  1303. }
  1304. // https://tc39.es/ecma262/#sec-makesuperpropertyreference
  1305. ThrowCompletionOr<void> ResolveSuperBase::execute_impl(Bytecode::Interpreter& interpreter) const
  1306. {
  1307. auto& vm = interpreter.vm();
  1308. // 1. Let env be GetThisEnvironment().
  1309. auto& env = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  1310. // 2. Assert: env.HasSuperBinding() is true.
  1311. VERIFY(env.has_super_binding());
  1312. // 3. Let baseValue be ? env.GetSuperBase().
  1313. interpreter.set(dst(), TRY(env.get_super_base()));
  1314. return {};
  1315. }
  1316. void GetNewTarget::execute_impl(Bytecode::Interpreter& interpreter) const
  1317. {
  1318. interpreter.set(dst(), interpreter.vm().get_new_target());
  1319. }
  1320. void GetImportMeta::execute_impl(Bytecode::Interpreter& interpreter) const
  1321. {
  1322. interpreter.set(dst(), interpreter.vm().get_import_meta());
  1323. }
  1324. ThrowCompletionOr<void> JumpIf::execute_impl(Bytecode::Interpreter&) const
  1325. {
  1326. // Handled in the interpreter loop.
  1327. __builtin_unreachable();
  1328. }
  1329. ThrowCompletionOr<void> JumpTrue::execute_impl(Bytecode::Interpreter&) const
  1330. {
  1331. // Handled in the interpreter loop.
  1332. __builtin_unreachable();
  1333. }
  1334. ThrowCompletionOr<void> JumpFalse::execute_impl(Bytecode::Interpreter&) const
  1335. {
  1336. // Handled in the interpreter loop.
  1337. __builtin_unreachable();
  1338. }
  1339. ThrowCompletionOr<void> JumpUndefined::execute_impl(Bytecode::Interpreter&) const
  1340. {
  1341. // Handled in the interpreter loop.
  1342. __builtin_unreachable();
  1343. }
  1344. ThrowCompletionOr<void> JumpNullish::execute_impl(Bytecode::Interpreter&) const
  1345. {
  1346. // Handled in the interpreter loop.
  1347. __builtin_unreachable();
  1348. }
  1349. ThrowCompletionOr<void> Mov::execute_impl(Bytecode::Interpreter&) const
  1350. {
  1351. // Handled in the interpreter loop.
  1352. __builtin_unreachable();
  1353. }
  1354. static ThrowCompletionOr<Value> dispatch_builtin_call(Bytecode::Interpreter& interpreter, Bytecode::Builtin builtin, ReadonlySpan<Operand> arguments)
  1355. {
  1356. switch (builtin) {
  1357. case Builtin::MathAbs:
  1358. return TRY(MathObject::abs_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1359. case Builtin::MathLog:
  1360. return TRY(MathObject::log_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1361. case Builtin::MathPow:
  1362. return TRY(MathObject::pow_impl(interpreter.vm(), interpreter.get(arguments[0]), interpreter.get(arguments[1])));
  1363. case Builtin::MathExp:
  1364. return TRY(MathObject::exp_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1365. case Builtin::MathCeil:
  1366. return TRY(MathObject::ceil_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1367. case Builtin::MathFloor:
  1368. return TRY(MathObject::floor_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1369. case Builtin::MathRound:
  1370. return TRY(MathObject::round_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1371. case Builtin::MathSqrt:
  1372. return TRY(MathObject::sqrt_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1373. case Bytecode::Builtin::__Count:
  1374. VERIFY_NOT_REACHED();
  1375. }
  1376. VERIFY_NOT_REACHED();
  1377. }
  1378. ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) const
  1379. {
  1380. auto callee = interpreter.get(m_callee);
  1381. TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string()));
  1382. if (m_builtin.has_value()
  1383. && m_argument_count == Bytecode::builtin_argument_count(m_builtin.value())
  1384. && callee.is_object()
  1385. && interpreter.realm().get_builtin_value(m_builtin.value()) == &callee.as_object()) {
  1386. interpreter.set(dst(), TRY(dispatch_builtin_call(interpreter, m_builtin.value(), { m_arguments, m_argument_count })));
  1387. return {};
  1388. }
  1389. Vector<Value> argument_values;
  1390. argument_values.ensure_capacity(m_argument_count);
  1391. for (size_t i = 0; i < m_argument_count; ++i)
  1392. argument_values.unchecked_append(interpreter.get(m_arguments[i]));
  1393. interpreter.set(dst(), TRY(perform_call(interpreter, interpreter.get(m_this_value), call_type(), callee, argument_values)));
  1394. return {};
  1395. }
  1396. ThrowCompletionOr<void> CallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  1397. {
  1398. auto callee = interpreter.get(m_callee);
  1399. TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string()));
  1400. auto argument_values = argument_list_evaluation(interpreter.vm(), interpreter.get(arguments()));
  1401. interpreter.set(dst(), TRY(perform_call(interpreter, interpreter.get(m_this_value), call_type(), callee, move(argument_values))));
  1402. return {};
  1403. }
  1404. // 13.3.7.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  1405. ThrowCompletionOr<void> SuperCallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  1406. {
  1407. interpreter.set(dst(), TRY(super_call_with_argument_array(interpreter.vm(), interpreter.get(arguments()), m_is_synthetic)));
  1408. return {};
  1409. }
  1410. void NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  1411. {
  1412. auto& vm = interpreter.vm();
  1413. interpreter.set(dst(), new_function(vm, m_function_node, m_lhs_name, m_home_object));
  1414. }
  1415. void Return::execute_impl(Bytecode::Interpreter& interpreter) const
  1416. {
  1417. if (m_value.has_value())
  1418. interpreter.do_return(interpreter.get(*m_value));
  1419. else
  1420. interpreter.do_return(js_undefined());
  1421. }
  1422. ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  1423. {
  1424. auto& vm = interpreter.vm();
  1425. auto old_value = interpreter.get(dst());
  1426. // OPTIMIZATION: Fast path for Int32 values.
  1427. if (old_value.is_int32()) {
  1428. auto integer_value = old_value.as_i32();
  1429. if (integer_value != NumericLimits<i32>::max()) [[likely]] {
  1430. interpreter.set(dst(), Value { integer_value + 1 });
  1431. return {};
  1432. }
  1433. }
  1434. old_value = TRY(old_value.to_numeric(vm));
  1435. if (old_value.is_number())
  1436. interpreter.set(dst(), Value(old_value.as_double() + 1));
  1437. else
  1438. interpreter.set(dst(), BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 })));
  1439. return {};
  1440. }
  1441. ThrowCompletionOr<void> PostfixIncrement::execute_impl(Bytecode::Interpreter& interpreter) const
  1442. {
  1443. auto& vm = interpreter.vm();
  1444. auto old_value = interpreter.get(m_src);
  1445. // OPTIMIZATION: Fast path for Int32 values.
  1446. if (old_value.is_int32()) {
  1447. auto integer_value = old_value.as_i32();
  1448. if (integer_value != NumericLimits<i32>::max()) [[likely]] {
  1449. interpreter.set(m_dst, old_value);
  1450. interpreter.set(m_src, Value { integer_value + 1 });
  1451. return {};
  1452. }
  1453. }
  1454. old_value = TRY(old_value.to_numeric(vm));
  1455. interpreter.set(m_dst, old_value);
  1456. if (old_value.is_number())
  1457. interpreter.set(m_src, Value(old_value.as_double() + 1));
  1458. else
  1459. interpreter.set(m_src, BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 })));
  1460. return {};
  1461. }
  1462. ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  1463. {
  1464. auto& vm = interpreter.vm();
  1465. auto old_value = interpreter.get(dst());
  1466. old_value = TRY(old_value.to_numeric(vm));
  1467. if (old_value.is_number())
  1468. interpreter.set(dst(), Value(old_value.as_double() - 1));
  1469. else
  1470. interpreter.set(dst(), BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 })));
  1471. return {};
  1472. }
  1473. ThrowCompletionOr<void> PostfixDecrement::execute_impl(Bytecode::Interpreter& interpreter) const
  1474. {
  1475. auto& vm = interpreter.vm();
  1476. auto old_value = interpreter.get(m_src);
  1477. old_value = TRY(old_value.to_numeric(vm));
  1478. interpreter.set(m_dst, old_value);
  1479. if (old_value.is_number())
  1480. interpreter.set(m_src, Value(old_value.as_double() - 1));
  1481. else
  1482. interpreter.set(m_src, BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 })));
  1483. return {};
  1484. }
  1485. ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  1486. {
  1487. return throw_completion(interpreter.get(src()));
  1488. }
  1489. ThrowCompletionOr<void> ThrowIfNotObject::execute_impl(Bytecode::Interpreter& interpreter) const
  1490. {
  1491. auto& vm = interpreter.vm();
  1492. auto src = interpreter.get(m_src);
  1493. if (!src.is_object())
  1494. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, src.to_string_without_side_effects());
  1495. return {};
  1496. }
  1497. ThrowCompletionOr<void> ThrowIfNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  1498. {
  1499. auto& vm = interpreter.vm();
  1500. auto value = interpreter.get(m_src);
  1501. if (value.is_nullish())
  1502. return vm.throw_completion<TypeError>(ErrorType::NotObjectCoercible, value.to_string_without_side_effects());
  1503. return {};
  1504. }
  1505. ThrowCompletionOr<void> ThrowIfTDZ::execute_impl(Bytecode::Interpreter& interpreter) const
  1506. {
  1507. auto& vm = interpreter.vm();
  1508. auto value = interpreter.get(m_src);
  1509. if (value.is_empty())
  1510. return vm.throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, value.to_string_without_side_effects());
  1511. return {};
  1512. }
  1513. ThrowCompletionOr<void> EnterUnwindContext::execute_impl(Bytecode::Interpreter&) const
  1514. {
  1515. // Handled in the interpreter loop.
  1516. __builtin_unreachable();
  1517. }
  1518. ThrowCompletionOr<void> ScheduleJump::execute_impl(Bytecode::Interpreter&) const
  1519. {
  1520. // Handled in the interpreter loop.
  1521. __builtin_unreachable();
  1522. }
  1523. void LeaveLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  1524. {
  1525. auto& running_execution_context = interpreter.vm().running_execution_context();
  1526. running_execution_context.lexical_environment = running_execution_context.saved_lexical_environments.take_last();
  1527. }
  1528. void LeavePrivateEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  1529. {
  1530. auto& running_execution_context = interpreter.vm().running_execution_context();
  1531. running_execution_context.private_environment = running_execution_context.private_environment->outer_environment();
  1532. }
  1533. void LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  1534. {
  1535. interpreter.leave_unwind_context();
  1536. }
  1537. ThrowCompletionOr<void> ContinuePendingUnwind::execute_impl(Bytecode::Interpreter&) const
  1538. {
  1539. // Handled in the interpreter loop.
  1540. __builtin_unreachable();
  1541. }
  1542. ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  1543. {
  1544. auto yielded_value = interpreter.get(m_value).value_or(js_undefined());
  1545. auto object = Object::create(interpreter.realm(), nullptr);
  1546. object->define_direct_property("result", yielded_value, JS::default_attributes);
  1547. if (m_continuation_label.has_value())
  1548. // FIXME: If we get a pointer, which is not accurately representable as a double
  1549. // will cause this to explode
  1550. object->define_direct_property("continuation", Value(m_continuation_label->address()), JS::default_attributes);
  1551. else
  1552. object->define_direct_property("continuation", js_null(), JS::default_attributes);
  1553. object->define_direct_property("isAwait", Value(false), JS::default_attributes);
  1554. interpreter.do_return(object);
  1555. return {};
  1556. }
  1557. ThrowCompletionOr<void> Await::execute_impl(Bytecode::Interpreter& interpreter) const
  1558. {
  1559. auto yielded_value = interpreter.get(m_argument).value_or(js_undefined());
  1560. auto object = Object::create(interpreter.realm(), nullptr);
  1561. object->define_direct_property("result", yielded_value, JS::default_attributes);
  1562. // FIXME: If we get a pointer, which is not accurately representable as a double
  1563. // will cause this to explode
  1564. object->define_direct_property("continuation", Value(m_continuation_label.address()), JS::default_attributes);
  1565. object->define_direct_property("isAwait", Value(true), JS::default_attributes);
  1566. interpreter.do_return(object);
  1567. return {};
  1568. }
  1569. ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  1570. {
  1571. auto base_identifier = interpreter.current_executable().get_identifier(m_base_identifier);
  1572. interpreter.set(dst(), TRY(get_by_value(interpreter.vm(), base_identifier, interpreter.get(m_base), interpreter.get(m_property))));
  1573. return {};
  1574. }
  1575. ThrowCompletionOr<void> GetByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  1576. {
  1577. auto& vm = interpreter.vm();
  1578. auto property_key_value = interpreter.get(m_property);
  1579. auto object = TRY(interpreter.get(m_base).to_object(vm));
  1580. auto property_key = TRY(property_key_value.to_property_key(vm));
  1581. interpreter.set(dst(), TRY(object->internal_get(property_key, interpreter.get(m_this_value))));
  1582. return {};
  1583. }
  1584. ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  1585. {
  1586. auto& vm = interpreter.vm();
  1587. auto value = interpreter.get(m_src);
  1588. auto base_identifier = interpreter.current_executable().get_identifier(m_base_identifier);
  1589. TRY(put_by_value(vm, interpreter.get(m_base), base_identifier, interpreter.get(m_property), value, m_kind));
  1590. return {};
  1591. }
  1592. ThrowCompletionOr<void> PutByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  1593. {
  1594. auto& vm = interpreter.vm();
  1595. auto value = interpreter.get(m_src);
  1596. auto base = interpreter.get(m_base);
  1597. auto property_key = m_kind != PropertyKind::Spread ? TRY(interpreter.get(m_property).to_property_key(vm)) : PropertyKey {};
  1598. TRY(put_by_property_key(vm, base, interpreter.get(m_this_value), value, {}, property_key, m_kind));
  1599. return {};
  1600. }
  1601. ThrowCompletionOr<void> DeleteByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  1602. {
  1603. auto base_value = interpreter.get(m_base);
  1604. auto property_key_value = interpreter.get(m_property);
  1605. interpreter.set(dst(), TRY(delete_by_value(interpreter, base_value, property_key_value)));
  1606. return {};
  1607. }
  1608. ThrowCompletionOr<void> DeleteByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  1609. {
  1610. auto property_key_value = interpreter.get(m_property);
  1611. auto base_value = interpreter.get(m_base);
  1612. auto this_value = interpreter.get(m_this_value);
  1613. interpreter.set(dst(), TRY(delete_by_value_with_this(interpreter, base_value, property_key_value, this_value)));
  1614. return {};
  1615. }
  1616. ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  1617. {
  1618. auto& vm = interpreter.vm();
  1619. interpreter.set(dst(), TRY(get_iterator(vm, interpreter.get(iterable()), m_hint)));
  1620. return {};
  1621. }
  1622. ThrowCompletionOr<void> GetObjectFromIteratorRecord::execute_impl(Bytecode::Interpreter& interpreter) const
  1623. {
  1624. auto& iterator_record = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object());
  1625. interpreter.set(m_object, iterator_record.iterator);
  1626. return {};
  1627. }
  1628. ThrowCompletionOr<void> GetNextMethodFromIteratorRecord::execute_impl(Bytecode::Interpreter& interpreter) const
  1629. {
  1630. auto& iterator_record = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object());
  1631. interpreter.set(m_next_method, iterator_record.next_method);
  1632. return {};
  1633. }
  1634. ThrowCompletionOr<void> GetMethod::execute_impl(Bytecode::Interpreter& interpreter) const
  1635. {
  1636. auto& vm = interpreter.vm();
  1637. auto identifier = interpreter.current_executable().get_identifier(m_property);
  1638. auto method = TRY(interpreter.get(m_object).get_method(vm, identifier));
  1639. interpreter.set(dst(), method ?: js_undefined());
  1640. return {};
  1641. }
  1642. ThrowCompletionOr<void> GetObjectPropertyIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  1643. {
  1644. interpreter.set(dst(), TRY(get_object_property_iterator(interpreter.vm(), interpreter.get(object()))));
  1645. return {};
  1646. }
  1647. ThrowCompletionOr<void> IteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1648. {
  1649. auto& vm = interpreter.vm();
  1650. auto& iterator = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object());
  1651. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1652. TRY(iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value }));
  1653. return {};
  1654. }
  1655. ThrowCompletionOr<void> AsyncIteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1656. {
  1657. auto& vm = interpreter.vm();
  1658. auto& iterator = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object());
  1659. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1660. TRY(async_iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value }));
  1661. return {};
  1662. }
  1663. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  1664. {
  1665. auto& vm = interpreter.vm();
  1666. auto& iterator_record = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object());
  1667. interpreter.set(dst(), TRY(iterator_next(vm, iterator_record)));
  1668. return {};
  1669. }
  1670. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const
  1671. {
  1672. Value super_class;
  1673. if (m_super_class.has_value())
  1674. super_class = interpreter.get(m_super_class.value());
  1675. Vector<Value> element_keys;
  1676. for (size_t i = 0; i < m_element_keys_count; ++i) {
  1677. Value element_key;
  1678. if (m_element_keys[i].has_value())
  1679. element_key = interpreter.get(m_element_keys[i].value());
  1680. element_keys.append(element_key);
  1681. }
  1682. interpreter.set(dst(), TRY(new_class(interpreter.vm(), super_class, m_class_expression, m_lhs_name, element_keys)));
  1683. return {};
  1684. }
  1685. // 13.5.3.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-typeof-operator-runtime-semantics-evaluation
  1686. ThrowCompletionOr<void> TypeofVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  1687. {
  1688. auto& vm = interpreter.vm();
  1689. interpreter.set(dst(), TRY(typeof_variable(vm, interpreter.current_executable().get_identifier(m_identifier))));
  1690. return {};
  1691. }
  1692. void BlockDeclarationInstantiation::execute_impl(Bytecode::Interpreter& interpreter) const
  1693. {
  1694. auto& vm = interpreter.vm();
  1695. auto old_environment = vm.running_execution_context().lexical_environment;
  1696. auto& running_execution_context = vm.running_execution_context();
  1697. running_execution_context.saved_lexical_environments.append(old_environment);
  1698. running_execution_context.lexical_environment = new_declarative_environment(*old_environment);
  1699. m_scope_node.block_declaration_instantiation(vm, running_execution_context.lexical_environment);
  1700. }
  1701. ByteString Mov::to_byte_string_impl(Bytecode::Executable const& executable) const
  1702. {
  1703. return ByteString::formatted("Mov {}, {}",
  1704. format_operand("dst"sv, m_dst, executable),
  1705. format_operand("src"sv, m_src, executable));
  1706. }
  1707. ByteString NewArray::to_byte_string_impl(Bytecode::Executable const& executable) const
  1708. {
  1709. StringBuilder builder;
  1710. builder.appendff("NewArray {}", format_operand("dst"sv, dst(), executable));
  1711. if (m_element_count != 0) {
  1712. builder.appendff(", {}", format_operand_list("args"sv, { m_elements, m_element_count }, executable));
  1713. }
  1714. return builder.to_byte_string();
  1715. }
  1716. ByteString NewPrimitiveArray::to_byte_string_impl(Bytecode::Executable const& executable) const
  1717. {
  1718. return ByteString::formatted("NewPrimitiveArray {}, {}"sv,
  1719. format_operand("dst"sv, dst(), executable),
  1720. format_value_list("elements"sv, elements()));
  1721. }
  1722. ByteString AddPrivateName::to_byte_string_impl(Bytecode::Executable const& executable) const
  1723. {
  1724. return ByteString::formatted("AddPrivateName {}"sv, executable.identifier_table->get(m_name));
  1725. }
  1726. ByteString ArrayAppend::to_byte_string_impl(Bytecode::Executable const& executable) const
  1727. {
  1728. return ByteString::formatted("Append {}, {}{}",
  1729. format_operand("dst"sv, dst(), executable),
  1730. format_operand("src"sv, src(), executable),
  1731. m_is_spread ? " **"sv : ""sv);
  1732. }
  1733. ByteString IteratorToArray::to_byte_string_impl(Bytecode::Executable const& executable) const
  1734. {
  1735. return ByteString::formatted("IteratorToArray {}, {}",
  1736. format_operand("dst"sv, dst(), executable),
  1737. format_operand("iterator"sv, iterator(), executable));
  1738. }
  1739. ByteString NewObject::to_byte_string_impl(Bytecode::Executable const& executable) const
  1740. {
  1741. return ByteString::formatted("NewObject {}", format_operand("dst"sv, dst(), executable));
  1742. }
  1743. ByteString NewRegExp::to_byte_string_impl(Bytecode::Executable const& executable) const
  1744. {
  1745. return ByteString::formatted("NewRegExp {}, source:{} (\"{}\") flags:{} (\"{}\")",
  1746. format_operand("dst"sv, dst(), executable),
  1747. m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  1748. }
  1749. ByteString CopyObjectExcludingProperties::to_byte_string_impl(Bytecode::Executable const& executable) const
  1750. {
  1751. StringBuilder builder;
  1752. builder.appendff("CopyObjectExcludingProperties {}, {}",
  1753. format_operand("dst"sv, dst(), executable),
  1754. format_operand("from"sv, m_from_object, executable));
  1755. if (m_excluded_names_count != 0) {
  1756. builder.append(" excluding:["sv);
  1757. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  1758. if (i != 0)
  1759. builder.append(", "sv);
  1760. builder.append(format_operand("#"sv, m_excluded_names[i], executable));
  1761. }
  1762. builder.append(']');
  1763. }
  1764. return builder.to_byte_string();
  1765. }
  1766. ByteString ConcatString::to_byte_string_impl(Bytecode::Executable const& executable) const
  1767. {
  1768. return ByteString::formatted("ConcatString {}, {}",
  1769. format_operand("dst"sv, dst(), executable),
  1770. format_operand("src"sv, src(), executable));
  1771. }
  1772. ByteString GetCalleeAndThisFromEnvironment::to_byte_string_impl(Bytecode::Executable const& executable) const
  1773. {
  1774. return ByteString::formatted("GetCalleeAndThisFromEnvironment {}, {} <- {}",
  1775. format_operand("callee"sv, m_callee, executable),
  1776. format_operand("this"sv, m_this_value, executable),
  1777. executable.identifier_table->get(m_identifier));
  1778. }
  1779. ByteString GetVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1780. {
  1781. return ByteString::formatted("GetVariable {}, {}",
  1782. format_operand("dst"sv, dst(), executable),
  1783. executable.identifier_table->get(m_identifier));
  1784. }
  1785. ByteString GetGlobal::to_byte_string_impl(Bytecode::Executable const& executable) const
  1786. {
  1787. return ByteString::formatted("GetGlobal {}, {}", format_operand("dst"sv, dst(), executable),
  1788. executable.identifier_table->get(m_identifier));
  1789. }
  1790. ByteString DeleteVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1791. {
  1792. return ByteString::formatted("DeleteVariable {}", executable.identifier_table->get(m_identifier));
  1793. }
  1794. ByteString CreateLexicalEnvironment::to_byte_string_impl(Bytecode::Executable const&) const
  1795. {
  1796. return "CreateLexicalEnvironment"sv;
  1797. }
  1798. ByteString CreatePrivateEnvironment::to_byte_string_impl(Bytecode::Executable const&) const
  1799. {
  1800. return "CreatePrivateEnvironment"sv;
  1801. }
  1802. ByteString CreateVariableEnvironment::to_byte_string_impl(Bytecode::Executable const&) const
  1803. {
  1804. return "CreateVariableEnvironment"sv;
  1805. }
  1806. ByteString CreateVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1807. {
  1808. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1809. return ByteString::formatted("CreateVariable env:{} immutable:{} global:{} {}", mode_string, m_is_immutable, m_is_global, executable.identifier_table->get(m_identifier));
  1810. }
  1811. ByteString CreateRestParams::to_byte_string_impl(Bytecode::Executable const& executable) const
  1812. {
  1813. return ByteString::formatted("CreateRestParams {}, rest_index:{}", format_operand("dst"sv, m_dst, executable), m_rest_index);
  1814. }
  1815. ByteString CreateArguments::to_byte_string_impl(Bytecode::Executable const&) const
  1816. {
  1817. return ByteString::formatted("CreateArguments {} immutable:{}", m_kind == Kind::Mapped ? "mapped"sv : "unmapped"sv, m_is_immutable);
  1818. }
  1819. ByteString EnterObjectEnvironment::to_byte_string_impl(Executable const& executable) const
  1820. {
  1821. return ByteString::formatted("EnterObjectEnvironment {}",
  1822. format_operand("object"sv, m_object, executable));
  1823. }
  1824. ByteString SetVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1825. {
  1826. auto initialization_mode_name = m_initialization_mode == InitializationMode::Initialize ? "Initialize" : "Set";
  1827. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1828. return ByteString::formatted("SetVariable {}, {}, env:{} init:{}",
  1829. executable.identifier_table->get(m_identifier),
  1830. format_operand("src"sv, src(), executable),
  1831. mode_string, initialization_mode_name);
  1832. }
  1833. ByteString SetLocal::to_byte_string_impl(Bytecode::Executable const& executable) const
  1834. {
  1835. return ByteString::formatted("SetLocal {}, {}",
  1836. format_operand("dst"sv, dst(), executable),
  1837. format_operand("src"sv, src(), executable));
  1838. }
  1839. ByteString GetArgument::to_byte_string_impl(Bytecode::Executable const& executable) const
  1840. {
  1841. return ByteString::formatted("GetArgument {}, {}", index(), format_operand("dst"sv, dst(), executable));
  1842. }
  1843. ByteString SetArgument::to_byte_string_impl(Bytecode::Executable const& executable) const
  1844. {
  1845. return ByteString::formatted("SetArgument {}, {}", index(), format_operand("src"sv, src(), executable));
  1846. }
  1847. static StringView property_kind_to_string(PropertyKind kind)
  1848. {
  1849. switch (kind) {
  1850. case PropertyKind::Getter:
  1851. return "getter"sv;
  1852. case PropertyKind::Setter:
  1853. return "setter"sv;
  1854. case PropertyKind::KeyValue:
  1855. return "key-value"sv;
  1856. case PropertyKind::DirectKeyValue:
  1857. return "direct-key-value"sv;
  1858. case PropertyKind::Spread:
  1859. return "spread"sv;
  1860. case PropertyKind::ProtoSetter:
  1861. return "proto-setter"sv;
  1862. }
  1863. VERIFY_NOT_REACHED();
  1864. }
  1865. ByteString PutById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1866. {
  1867. auto kind = property_kind_to_string(m_kind);
  1868. return ByteString::formatted("PutById {}, {}, {}, kind:{}",
  1869. format_operand("base"sv, m_base, executable),
  1870. executable.identifier_table->get(m_property),
  1871. format_operand("src"sv, m_src, executable),
  1872. kind);
  1873. }
  1874. ByteString PutByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1875. {
  1876. auto kind = property_kind_to_string(m_kind);
  1877. return ByteString::formatted("PutByIdWithThis {}, {}, {}, {}, kind:{}",
  1878. format_operand("base"sv, m_base, executable),
  1879. executable.identifier_table->get(m_property),
  1880. format_operand("src"sv, m_src, executable),
  1881. format_operand("this"sv, m_this_value, executable),
  1882. kind);
  1883. }
  1884. ByteString PutPrivateById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1885. {
  1886. auto kind = property_kind_to_string(m_kind);
  1887. return ByteString::formatted(
  1888. "PutPrivateById {}, {}, {}, kind:{} ",
  1889. format_operand("base"sv, m_base, executable),
  1890. executable.identifier_table->get(m_property),
  1891. format_operand("src"sv, m_src, executable),
  1892. kind);
  1893. }
  1894. ByteString GetById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1895. {
  1896. return ByteString::formatted("GetById {}, {}, {}",
  1897. format_operand("dst"sv, m_dst, executable),
  1898. format_operand("base"sv, m_base, executable),
  1899. executable.identifier_table->get(m_property));
  1900. }
  1901. ByteString GetByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1902. {
  1903. return ByteString::formatted("GetByIdWithThis {}, {}, {}, {}",
  1904. format_operand("dst"sv, m_dst, executable),
  1905. format_operand("base"sv, m_base, executable),
  1906. executable.identifier_table->get(m_property),
  1907. format_operand("this"sv, m_this_value, executable));
  1908. }
  1909. ByteString GetPrivateById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1910. {
  1911. return ByteString::formatted("GetPrivateById {}, {}, {}",
  1912. format_operand("dst"sv, m_dst, executable),
  1913. format_operand("base"sv, m_base, executable),
  1914. executable.identifier_table->get(m_property));
  1915. }
  1916. ByteString HasPrivateId::to_byte_string_impl(Bytecode::Executable const& executable) const
  1917. {
  1918. return ByteString::formatted("HasPrivateId {}, {}, {}",
  1919. format_operand("dst"sv, m_dst, executable),
  1920. format_operand("base"sv, m_base, executable),
  1921. executable.identifier_table->get(m_property));
  1922. }
  1923. ByteString DeleteById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1924. {
  1925. return ByteString::formatted("DeleteById {}, {}, {}",
  1926. format_operand("dst"sv, m_dst, executable),
  1927. format_operand("base"sv, m_base, executable),
  1928. executable.identifier_table->get(m_property));
  1929. }
  1930. ByteString DeleteByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1931. {
  1932. return ByteString::formatted("DeleteByIdWithThis {}, {}, {}, {}",
  1933. format_operand("dst"sv, m_dst, executable),
  1934. format_operand("base"sv, m_base, executable),
  1935. executable.identifier_table->get(m_property),
  1936. format_operand("this"sv, m_this_value, executable));
  1937. }
  1938. ByteString Jump::to_byte_string_impl(Bytecode::Executable const&) const
  1939. {
  1940. return ByteString::formatted("Jump {}", m_target);
  1941. }
  1942. ByteString JumpIf::to_byte_string_impl(Bytecode::Executable const& executable) const
  1943. {
  1944. return ByteString::formatted("JumpIf {}, \033[32mtrue\033[0m:{} \033[32mfalse\033[0m:{}",
  1945. format_operand("condition"sv, m_condition, executable),
  1946. m_true_target,
  1947. m_false_target);
  1948. }
  1949. ByteString JumpTrue::to_byte_string_impl(Bytecode::Executable const& executable) const
  1950. {
  1951. return ByteString::formatted("JumpTrue {}, {}",
  1952. format_operand("condition"sv, m_condition, executable),
  1953. m_target);
  1954. }
  1955. ByteString JumpFalse::to_byte_string_impl(Bytecode::Executable const& executable) const
  1956. {
  1957. return ByteString::formatted("JumpFalse {}, {}",
  1958. format_operand("condition"sv, m_condition, executable),
  1959. m_target);
  1960. }
  1961. ByteString JumpNullish::to_byte_string_impl(Bytecode::Executable const& executable) const
  1962. {
  1963. return ByteString::formatted("JumpNullish {}, null:{} nonnull:{}",
  1964. format_operand("condition"sv, m_condition, executable),
  1965. m_true_target,
  1966. m_false_target);
  1967. }
  1968. #define HANDLE_COMPARISON_OP(op_TitleCase, op_snake_case) \
  1969. ByteString Jump##op_TitleCase::to_byte_string_impl(Bytecode::Executable const& executable) const \
  1970. { \
  1971. return ByteString::formatted("Jump" #op_TitleCase " {}, {}, true:{}, false:{}", \
  1972. format_operand("lhs"sv, m_lhs, executable), \
  1973. format_operand("rhs"sv, m_rhs, executable), \
  1974. m_true_target, \
  1975. m_false_target); \
  1976. }
  1977. JS_ENUMERATE_COMPARISON_OPS(HANDLE_COMPARISON_OP)
  1978. ByteString JumpUndefined::to_byte_string_impl(Bytecode::Executable const& executable) const
  1979. {
  1980. return ByteString::formatted("JumpUndefined {}, undefined:{} defined:{}",
  1981. format_operand("condition"sv, m_condition, executable),
  1982. m_true_target,
  1983. m_false_target);
  1984. }
  1985. static StringView call_type_to_string(CallType type)
  1986. {
  1987. switch (type) {
  1988. case CallType::Call:
  1989. return ""sv;
  1990. case CallType::Construct:
  1991. return " (Construct)"sv;
  1992. case CallType::DirectEval:
  1993. return " (DirectEval)"sv;
  1994. }
  1995. VERIFY_NOT_REACHED();
  1996. }
  1997. ByteString Call::to_byte_string_impl(Bytecode::Executable const& executable) const
  1998. {
  1999. auto type = call_type_to_string(m_type);
  2000. StringBuilder builder;
  2001. builder.appendff("Call{} {}, {}, {}, "sv,
  2002. type,
  2003. format_operand("dst"sv, m_dst, executable),
  2004. format_operand("callee"sv, m_callee, executable),
  2005. format_operand("this"sv, m_this_value, executable));
  2006. builder.append(format_operand_list("args"sv, { m_arguments, m_argument_count }, executable));
  2007. if (m_builtin.has_value()) {
  2008. builder.appendff(", (builtin:{})", m_builtin.value());
  2009. }
  2010. if (m_expression_string.has_value()) {
  2011. builder.appendff(", `{}`", executable.get_string(m_expression_string.value()));
  2012. }
  2013. return builder.to_byte_string();
  2014. }
  2015. ByteString CallWithArgumentArray::to_byte_string_impl(Bytecode::Executable const& executable) const
  2016. {
  2017. auto type = call_type_to_string(m_type);
  2018. StringBuilder builder;
  2019. builder.appendff("CallWithArgumentArray{} {}, {}, {}, {}",
  2020. type,
  2021. format_operand("dst"sv, m_dst, executable),
  2022. format_operand("callee"sv, m_callee, executable),
  2023. format_operand("this"sv, m_this_value, executable),
  2024. format_operand("arguments"sv, m_arguments, executable));
  2025. if (m_expression_string.has_value())
  2026. builder.appendff(" ({})", executable.get_string(m_expression_string.value()));
  2027. return builder.to_byte_string();
  2028. }
  2029. ByteString SuperCallWithArgumentArray::to_byte_string_impl(Bytecode::Executable const& executable) const
  2030. {
  2031. return ByteString::formatted("SuperCallWithArgumentArray {}, {}",
  2032. format_operand("dst"sv, m_dst, executable),
  2033. format_operand("arguments"sv, m_arguments, executable));
  2034. }
  2035. ByteString NewFunction::to_byte_string_impl(Bytecode::Executable const& executable) const
  2036. {
  2037. StringBuilder builder;
  2038. builder.appendff("NewFunction {}",
  2039. format_operand("dst"sv, m_dst, executable));
  2040. if (m_function_node.has_name())
  2041. builder.appendff(" name:{}"sv, m_function_node.name());
  2042. if (m_lhs_name.has_value())
  2043. builder.appendff(" lhs_name:{}"sv, executable.get_identifier(m_lhs_name.value()));
  2044. if (m_home_object.has_value())
  2045. builder.appendff(", {}"sv, format_operand("home_object"sv, m_home_object.value(), executable));
  2046. return builder.to_byte_string();
  2047. }
  2048. ByteString NewClass::to_byte_string_impl(Bytecode::Executable const& executable) const
  2049. {
  2050. StringBuilder builder;
  2051. auto name = m_class_expression.name();
  2052. builder.appendff("NewClass {}",
  2053. format_operand("dst"sv, m_dst, executable));
  2054. if (m_super_class.has_value())
  2055. builder.appendff(", {}", format_operand("super_class"sv, *m_super_class, executable));
  2056. if (!name.is_empty())
  2057. builder.appendff(", {}", name);
  2058. if (m_lhs_name.has_value())
  2059. builder.appendff(", lhs_name:{}"sv, executable.get_identifier(m_lhs_name.value()));
  2060. return builder.to_byte_string();
  2061. }
  2062. ByteString Return::to_byte_string_impl(Bytecode::Executable const& executable) const
  2063. {
  2064. if (m_value.has_value())
  2065. return ByteString::formatted("Return {}", format_operand("value"sv, m_value.value(), executable));
  2066. return "Return";
  2067. }
  2068. ByteString Increment::to_byte_string_impl(Bytecode::Executable const& executable) const
  2069. {
  2070. return ByteString::formatted("Increment {}", format_operand("dst"sv, m_dst, executable));
  2071. }
  2072. ByteString PostfixIncrement::to_byte_string_impl(Bytecode::Executable const& executable) const
  2073. {
  2074. return ByteString::formatted("PostfixIncrement {}, {}",
  2075. format_operand("dst"sv, m_dst, executable),
  2076. format_operand("src"sv, m_src, executable));
  2077. }
  2078. ByteString Decrement::to_byte_string_impl(Bytecode::Executable const& executable) const
  2079. {
  2080. return ByteString::formatted("Decrement {}", format_operand("dst"sv, m_dst, executable));
  2081. }
  2082. ByteString PostfixDecrement::to_byte_string_impl(Bytecode::Executable const& executable) const
  2083. {
  2084. return ByteString::formatted("PostfixDecrement {}, {}",
  2085. format_operand("dst"sv, m_dst, executable),
  2086. format_operand("src"sv, m_src, executable));
  2087. }
  2088. ByteString Throw::to_byte_string_impl(Bytecode::Executable const& executable) const
  2089. {
  2090. return ByteString::formatted("Throw {}",
  2091. format_operand("src"sv, m_src, executable));
  2092. }
  2093. ByteString ThrowIfNotObject::to_byte_string_impl(Bytecode::Executable const& executable) const
  2094. {
  2095. return ByteString::formatted("ThrowIfNotObject {}",
  2096. format_operand("src"sv, m_src, executable));
  2097. }
  2098. ByteString ThrowIfNullish::to_byte_string_impl(Bytecode::Executable const& executable) const
  2099. {
  2100. return ByteString::formatted("ThrowIfNullish {}",
  2101. format_operand("src"sv, m_src, executable));
  2102. }
  2103. ByteString ThrowIfTDZ::to_byte_string_impl(Bytecode::Executable const& executable) const
  2104. {
  2105. return ByteString::formatted("ThrowIfTDZ {}",
  2106. format_operand("src"sv, m_src, executable));
  2107. }
  2108. ByteString EnterUnwindContext::to_byte_string_impl(Bytecode::Executable const&) const
  2109. {
  2110. return ByteString::formatted("EnterUnwindContext entry:{}", m_entry_point);
  2111. }
  2112. ByteString ScheduleJump::to_byte_string_impl(Bytecode::Executable const&) const
  2113. {
  2114. return ByteString::formatted("ScheduleJump {}", m_target);
  2115. }
  2116. ByteString LeaveLexicalEnvironment::to_byte_string_impl(Bytecode::Executable const&) const
  2117. {
  2118. return "LeaveLexicalEnvironment"sv;
  2119. }
  2120. ByteString LeavePrivateEnvironment::to_byte_string_impl(Bytecode::Executable const&) const
  2121. {
  2122. return "LeavePrivateEnvironment"sv;
  2123. }
  2124. ByteString LeaveUnwindContext::to_byte_string_impl(Bytecode::Executable const&) const
  2125. {
  2126. return "LeaveUnwindContext";
  2127. }
  2128. ByteString ContinuePendingUnwind::to_byte_string_impl(Bytecode::Executable const&) const
  2129. {
  2130. return ByteString::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  2131. }
  2132. ByteString Yield::to_byte_string_impl(Bytecode::Executable const& executable) const
  2133. {
  2134. if (m_continuation_label.has_value()) {
  2135. return ByteString::formatted("Yield continuation:{}, {}",
  2136. m_continuation_label.value(),
  2137. format_operand("value"sv, m_value, executable));
  2138. }
  2139. return ByteString::formatted("Yield return {}",
  2140. format_operand("value"sv, m_value, executable));
  2141. }
  2142. ByteString Await::to_byte_string_impl(Bytecode::Executable const& executable) const
  2143. {
  2144. return ByteString::formatted("Await {}, continuation:{}",
  2145. format_operand("argument"sv, m_argument, executable),
  2146. m_continuation_label);
  2147. }
  2148. ByteString GetByValue::to_byte_string_impl(Bytecode::Executable const& executable) const
  2149. {
  2150. return ByteString::formatted("GetByValue {}, {}, {}",
  2151. format_operand("dst"sv, m_dst, executable),
  2152. format_operand("base"sv, m_base, executable),
  2153. format_operand("property"sv, m_property, executable));
  2154. }
  2155. ByteString GetByValueWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  2156. {
  2157. return ByteString::formatted("GetByValueWithThis {}, {}, {}",
  2158. format_operand("dst"sv, m_dst, executable),
  2159. format_operand("base"sv, m_base, executable),
  2160. format_operand("property"sv, m_property, executable));
  2161. }
  2162. ByteString PutByValue::to_byte_string_impl(Bytecode::Executable const& executable) const
  2163. {
  2164. auto kind = property_kind_to_string(m_kind);
  2165. return ByteString::formatted("PutByValue {}, {}, {}, kind:{}",
  2166. format_operand("base"sv, m_base, executable),
  2167. format_operand("property"sv, m_property, executable),
  2168. format_operand("src"sv, m_src, executable),
  2169. kind);
  2170. }
  2171. ByteString PutByValueWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  2172. {
  2173. auto kind = property_kind_to_string(m_kind);
  2174. return ByteString::formatted("PutByValueWithThis {}, {}, {}, {}, kind:{}",
  2175. format_operand("base"sv, m_base, executable),
  2176. format_operand("property"sv, m_property, executable),
  2177. format_operand("src"sv, m_src, executable),
  2178. format_operand("this"sv, m_this_value, executable),
  2179. kind);
  2180. }
  2181. ByteString DeleteByValue::to_byte_string_impl(Bytecode::Executable const& executable) const
  2182. {
  2183. return ByteString::formatted("DeleteByValue {}, {}, {}",
  2184. format_operand("dst"sv, dst(), executable),
  2185. format_operand("base"sv, m_base, executable),
  2186. format_operand("property"sv, m_property, executable));
  2187. }
  2188. ByteString DeleteByValueWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  2189. {
  2190. return ByteString::formatted("DeleteByValueWithThis {}, {}, {}, {}",
  2191. format_operand("dst"sv, dst(), executable),
  2192. format_operand("base"sv, m_base, executable),
  2193. format_operand("property"sv, m_property, executable),
  2194. format_operand("this"sv, m_this_value, executable));
  2195. }
  2196. ByteString GetIterator::to_byte_string_impl(Executable const& executable) const
  2197. {
  2198. auto hint = m_hint == IteratorHint::Sync ? "sync" : "async";
  2199. return ByteString::formatted("GetIterator {}, {}, hint:{}",
  2200. format_operand("dst"sv, m_dst, executable),
  2201. format_operand("iterable"sv, m_iterable, executable),
  2202. hint);
  2203. }
  2204. ByteString GetMethod::to_byte_string_impl(Bytecode::Executable const& executable) const
  2205. {
  2206. return ByteString::formatted("GetMethod {}, {}, {}",
  2207. format_operand("dst"sv, m_dst, executable),
  2208. format_operand("object"sv, m_object, executable),
  2209. executable.identifier_table->get(m_property));
  2210. }
  2211. ByteString GetObjectPropertyIterator::to_byte_string_impl(Bytecode::Executable const& executable) const
  2212. {
  2213. return ByteString::formatted("GetObjectPropertyIterator {}, {}",
  2214. format_operand("dst"sv, dst(), executable),
  2215. format_operand("object"sv, object(), executable));
  2216. }
  2217. ByteString IteratorClose::to_byte_string_impl(Bytecode::Executable const& executable) const
  2218. {
  2219. if (!m_completion_value.has_value())
  2220. return ByteString::formatted("IteratorClose {}, completion_type={} completion_value=<empty>",
  2221. format_operand("iterator_record"sv, m_iterator_record, executable),
  2222. to_underlying(m_completion_type));
  2223. auto completion_value_string = m_completion_value->to_string_without_side_effects();
  2224. return ByteString::formatted("IteratorClose {}, completion_type={} completion_value={}",
  2225. format_operand("iterator_record"sv, m_iterator_record, executable),
  2226. to_underlying(m_completion_type), completion_value_string);
  2227. }
  2228. ByteString AsyncIteratorClose::to_byte_string_impl(Bytecode::Executable const& executable) const
  2229. {
  2230. if (!m_completion_value.has_value()) {
  2231. return ByteString::formatted("AsyncIteratorClose {}, completion_type:{} completion_value:<empty>",
  2232. format_operand("iterator_record"sv, m_iterator_record, executable),
  2233. to_underlying(m_completion_type));
  2234. }
  2235. return ByteString::formatted("AsyncIteratorClose {}, completion_type:{}, completion_value:{}",
  2236. format_operand("iterator_record"sv, m_iterator_record, executable),
  2237. to_underlying(m_completion_type), m_completion_value);
  2238. }
  2239. ByteString IteratorNext::to_byte_string_impl(Executable const& executable) const
  2240. {
  2241. return ByteString::formatted("IteratorNext {}, {}",
  2242. format_operand("dst"sv, m_dst, executable),
  2243. format_operand("iterator_record"sv, m_iterator_record, executable));
  2244. }
  2245. ByteString ResolveThisBinding::to_byte_string_impl(Bytecode::Executable const& executable) const
  2246. {
  2247. return ByteString::formatted("ResolveThisBinding {}", format_operand("dst"sv, m_dst, executable));
  2248. }
  2249. ByteString ResolveSuperBase::to_byte_string_impl(Bytecode::Executable const& executable) const
  2250. {
  2251. return ByteString::formatted("ResolveSuperBase {}",
  2252. format_operand("dst"sv, m_dst, executable));
  2253. }
  2254. ByteString GetNewTarget::to_byte_string_impl(Bytecode::Executable const& executable) const
  2255. {
  2256. return ByteString::formatted("GetNewTarget {}", format_operand("dst"sv, m_dst, executable));
  2257. }
  2258. ByteString GetImportMeta::to_byte_string_impl(Bytecode::Executable const& executable) const
  2259. {
  2260. return ByteString::formatted("GetImportMeta {}", format_operand("dst"sv, m_dst, executable));
  2261. }
  2262. ByteString TypeofVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  2263. {
  2264. return ByteString::formatted("TypeofVariable {}, {}",
  2265. format_operand("dst"sv, m_dst, executable),
  2266. executable.identifier_table->get(m_identifier));
  2267. }
  2268. ByteString BlockDeclarationInstantiation::to_byte_string_impl(Bytecode::Executable const&) const
  2269. {
  2270. return "BlockDeclarationInstantiation"sv;
  2271. }
  2272. ByteString ImportCall::to_byte_string_impl(Bytecode::Executable const& executable) const
  2273. {
  2274. return ByteString::formatted("ImportCall {}, {}, {}",
  2275. format_operand("dst"sv, m_dst, executable),
  2276. format_operand("specifier"sv, m_specifier, executable),
  2277. format_operand("options"sv, m_options, executable));
  2278. }
  2279. ByteString Catch::to_byte_string_impl(Bytecode::Executable const& executable) const
  2280. {
  2281. return ByteString::formatted("Catch {}",
  2282. format_operand("dst"sv, m_dst, executable));
  2283. }
  2284. ByteString LeaveFinally::to_byte_string_impl(Bytecode::Executable const&) const
  2285. {
  2286. return ByteString::formatted("LeaveFinally");
  2287. }
  2288. ByteString RestoreScheduledJump::to_byte_string_impl(Bytecode::Executable const&) const
  2289. {
  2290. return ByteString::formatted("RestoreScheduledJump");
  2291. }
  2292. ByteString GetObjectFromIteratorRecord::to_byte_string_impl(Bytecode::Executable const& executable) const
  2293. {
  2294. return ByteString::formatted("GetObjectFromIteratorRecord {}, {}",
  2295. format_operand("object"sv, m_object, executable),
  2296. format_operand("iterator_record"sv, m_iterator_record, executable));
  2297. }
  2298. ByteString GetNextMethodFromIteratorRecord::to_byte_string_impl(Bytecode::Executable const& executable) const
  2299. {
  2300. return ByteString::formatted("GetNextMethodFromIteratorRecord {}, {}",
  2301. format_operand("next_method"sv, m_next_method, executable),
  2302. format_operand("iterator_record"sv, m_iterator_record, executable));
  2303. }
  2304. ByteString End::to_byte_string_impl(Bytecode::Executable const& executable) const
  2305. {
  2306. return ByteString::formatted("End {}", format_operand("value"sv, m_value, executable));
  2307. }
  2308. ByteString Dump::to_byte_string_impl(Bytecode::Executable const& executable) const
  2309. {
  2310. return ByteString::formatted("Dump '{}', {}", m_text,
  2311. format_operand("value"sv, m_value, executable));
  2312. }
  2313. }