Interpreter.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  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. NonnullOwnPtr<CallFrame> CallFrame::create(size_t register_count)
  39. {
  40. size_t allocation_size = sizeof(CallFrame) + sizeof(Value) * register_count;
  41. auto* memory = malloc(allocation_size);
  42. VERIFY(memory);
  43. auto call_frame = adopt_own(*new (memory) CallFrame);
  44. call_frame->register_count = register_count;
  45. for (auto i = 0u; i < register_count; ++i)
  46. new (&call_frame->register_values[i]) Value();
  47. return call_frame;
  48. }
  49. Interpreter::Interpreter(VM& vm)
  50. : m_vm(vm)
  51. {
  52. }
  53. Interpreter::~Interpreter()
  54. {
  55. }
  56. void Interpreter::visit_edges(Cell::Visitor& visitor)
  57. {
  58. for (auto& frame : m_call_frames) {
  59. frame.visit([&](auto& value) { value->visit_edges(visitor); });
  60. }
  61. }
  62. // 16.1.6 ScriptEvaluation ( scriptRecord ), https://tc39.es/ecma262/#sec-runtime-semantics-scriptevaluation
  63. ThrowCompletionOr<Value> Interpreter::run(Script& script_record, JS::GCPtr<Environment> lexical_environment_override)
  64. {
  65. auto& vm = this->vm();
  66. // 1. Let globalEnv be scriptRecord.[[Realm]].[[GlobalEnv]].
  67. auto& global_environment = script_record.realm().global_environment();
  68. // 2. Let scriptContext be a new ECMAScript code execution context.
  69. auto script_context = ExecutionContext::create(vm.heap());
  70. // 3. Set the Function of scriptContext to null.
  71. // NOTE: This was done during execution context construction.
  72. // 4. Set the Realm of scriptContext to scriptRecord.[[Realm]].
  73. script_context->realm = &script_record.realm();
  74. // 5. Set the ScriptOrModule of scriptContext to scriptRecord.
  75. script_context->script_or_module = NonnullGCPtr<Script>(script_record);
  76. // 6. Set the VariableEnvironment of scriptContext to globalEnv.
  77. script_context->variable_environment = &global_environment;
  78. // 7. Set the LexicalEnvironment of scriptContext to globalEnv.
  79. script_context->lexical_environment = &global_environment;
  80. // Non-standard: Override the lexical environment if requested.
  81. if (lexical_environment_override)
  82. script_context->lexical_environment = lexical_environment_override;
  83. // 8. Set the PrivateEnvironment of scriptContext to null.
  84. // NOTE: This isn't in the spec, but we require it.
  85. script_context->is_strict_mode = script_record.parse_node().is_strict_mode();
  86. // FIXME: 9. Suspend the currently running execution context.
  87. // 10. Push scriptContext onto the execution context stack; scriptContext is now the running execution context.
  88. TRY(vm.push_execution_context(*script_context, {}));
  89. // 11. Let script be scriptRecord.[[ECMAScriptCode]].
  90. auto& script = script_record.parse_node();
  91. // 12. Let result be Completion(GlobalDeclarationInstantiation(script, globalEnv)).
  92. auto instantiation_result = script.global_declaration_instantiation(vm, global_environment);
  93. Completion result = instantiation_result.is_throw_completion() ? instantiation_result.throw_completion() : normal_completion({});
  94. // 13. If result.[[Type]] is normal, then
  95. if (result.type() == Completion::Type::Normal) {
  96. auto executable_result = JS::Bytecode::Generator::generate(vm, script);
  97. if (executable_result.is_error()) {
  98. if (auto error_string = executable_result.error().to_string(); error_string.is_error())
  99. result = vm.template throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory));
  100. else if (error_string = String::formatted("TODO({})", error_string.value()); error_string.is_error())
  101. result = vm.template throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory));
  102. else
  103. result = JS::throw_completion(JS::InternalError::create(realm(), error_string.release_value()));
  104. } else {
  105. auto executable = executable_result.release_value();
  106. if (g_dump_bytecode)
  107. executable->dump();
  108. // a. Set result to the result of evaluating script.
  109. auto result_or_error = run_and_return_frame(*executable, nullptr);
  110. if (result_or_error.value.is_error())
  111. result = result_or_error.value.release_error();
  112. else
  113. result = result_or_error.frame->registers()[0];
  114. }
  115. }
  116. // 14. If result.[[Type]] is normal and result.[[Value]] is empty, then
  117. if (result.type() == Completion::Type::Normal && !result.value().has_value()) {
  118. // a. Set result to NormalCompletion(undefined).
  119. result = normal_completion(js_undefined());
  120. }
  121. // FIXME: 15. Suspend scriptContext and remove it from the execution context stack.
  122. vm.pop_execution_context();
  123. // 16. Assert: The execution context stack is not empty.
  124. VERIFY(!vm.execution_context_stack().is_empty());
  125. // FIXME: 17. Resume the context that is now on the top of the execution context stack as the running execution context.
  126. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  127. // in which case this is a no-op.
  128. // 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.
  129. // https://tc39.es/ecma262/#sec-jobs for jobs and https://tc39.es/ecma262/#_ref_3508 for ClearKeptObjects
  130. // finish_execution_generation is particularly an issue for LibWeb, as the HTML spec wants to run it specifically after performing a microtask checkpoint.
  131. // The promise and registry cleanup queues don't cause LibWeb an issue, as LibWeb overrides the hooks that push onto these queues.
  132. vm.run_queued_promise_jobs();
  133. vm.run_queued_finalization_registry_cleanup_jobs();
  134. vm.finish_execution_generation();
  135. // 18. Return ? result.
  136. if (result.is_abrupt()) {
  137. VERIFY(result.type() == Completion::Type::Throw);
  138. return result.release_error();
  139. }
  140. VERIFY(result.value().has_value());
  141. return *result.value();
  142. }
  143. ThrowCompletionOr<Value> Interpreter::run(SourceTextModule& module)
  144. {
  145. // FIXME: This is not a entry point as defined in the spec, but is convenient.
  146. // To avoid work we use link_and_eval_module however that can already be
  147. // dangerous if the vm loaded other modules.
  148. auto& vm = this->vm();
  149. TRY(vm.link_and_eval_module(Badge<Bytecode::Interpreter> {}, module));
  150. vm.run_queued_promise_jobs();
  151. vm.run_queued_finalization_registry_cleanup_jobs();
  152. return js_undefined();
  153. }
  154. void Interpreter::run_bytecode()
  155. {
  156. auto* locals = vm().running_execution_context().locals.data();
  157. auto* registers = this->registers().data();
  158. auto& accumulator = this->accumulator();
  159. for (;;) {
  160. start:
  161. auto pc = InstructionStreamIterator { m_current_block->instruction_stream(), m_current_executable };
  162. TemporaryChange temp_change { m_pc, Optional<InstructionStreamIterator&>(pc) };
  163. bool will_return = false;
  164. bool will_yield = false;
  165. ThrowCompletionOr<void> result;
  166. while (!pc.at_end()) {
  167. auto& instruction = *pc;
  168. switch (instruction.type()) {
  169. case Instruction::Type::GetLocal: {
  170. auto& local = locals[static_cast<Op::GetLocal const&>(instruction).index()];
  171. if (local.is_empty()) {
  172. auto const& variable_name = vm().running_execution_context().function->local_variables_names()[static_cast<Op::GetLocal const&>(instruction).index()];
  173. result = vm().throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, variable_name);
  174. break;
  175. }
  176. accumulator = local;
  177. break;
  178. }
  179. case Instruction::Type::SetLocal:
  180. locals[static_cast<Op::SetLocal const&>(instruction).index()] = accumulator;
  181. break;
  182. case Instruction::Type::Load:
  183. accumulator = registers[static_cast<Op::Load const&>(instruction).src().index()];
  184. break;
  185. case Instruction::Type::Store:
  186. registers[static_cast<Op::Store const&>(instruction).dst().index()] = accumulator;
  187. break;
  188. case Instruction::Type::LoadImmediate:
  189. accumulator = static_cast<Op::LoadImmediate const&>(instruction).value();
  190. break;
  191. case Instruction::Type::Jump:
  192. m_current_block = &static_cast<Op::Jump const&>(instruction).true_target()->block();
  193. goto start;
  194. case Instruction::Type::JumpConditional:
  195. if (accumulator.to_boolean())
  196. m_current_block = &static_cast<Op::Jump const&>(instruction).true_target()->block();
  197. else
  198. m_current_block = &static_cast<Op::Jump const&>(instruction).false_target()->block();
  199. goto start;
  200. case Instruction::Type::JumpNullish:
  201. if (accumulator.is_nullish())
  202. m_current_block = &static_cast<Op::Jump const&>(instruction).true_target()->block();
  203. else
  204. m_current_block = &static_cast<Op::Jump const&>(instruction).false_target()->block();
  205. goto start;
  206. case Instruction::Type::JumpUndefined:
  207. if (accumulator.is_undefined())
  208. m_current_block = &static_cast<Op::Jump const&>(instruction).true_target()->block();
  209. else
  210. m_current_block = &static_cast<Op::Jump const&>(instruction).false_target()->block();
  211. goto start;
  212. case Instruction::Type::EnterUnwindContext:
  213. enter_unwind_context();
  214. m_current_block = &static_cast<Op::EnterUnwindContext const&>(instruction).entry_point().block();
  215. goto start;
  216. case Instruction::Type::ContinuePendingUnwind: {
  217. if (auto exception = reg(Register::exception()); !exception.is_empty()) {
  218. result = throw_completion(exception);
  219. break;
  220. }
  221. if (!saved_return_value().is_empty()) {
  222. do_return(saved_return_value());
  223. break;
  224. }
  225. auto const* old_scheduled_jump = call_frame().previously_scheduled_jumps.take_last();
  226. if (m_scheduled_jump) {
  227. // FIXME: If we `break` or `continue` in the finally, we need to clear
  228. // this field
  229. // Same goes for popping an old_scheduled_jump form the stack
  230. m_current_block = exchange(m_scheduled_jump, nullptr);
  231. } else {
  232. m_current_block = &static_cast<Op::ContinuePendingUnwind const&>(instruction).resume_target().block();
  233. // set the scheduled jump to the old value if we continue
  234. // where we left it
  235. m_scheduled_jump = old_scheduled_jump;
  236. }
  237. goto start;
  238. }
  239. case Instruction::Type::ScheduleJump: {
  240. m_scheduled_jump = &static_cast<Op::ScheduleJump const&>(instruction).target().block();
  241. auto const* finalizer = m_current_block->finalizer();
  242. VERIFY(finalizer);
  243. m_current_block = finalizer;
  244. goto start;
  245. }
  246. default:
  247. result = instruction.execute(*this);
  248. break;
  249. }
  250. if (result.is_error()) [[unlikely]] {
  251. reg(Register::exception()) = *result.throw_completion().value();
  252. m_scheduled_jump = {};
  253. auto const* handler = m_current_block->handler();
  254. auto const* finalizer = m_current_block->finalizer();
  255. if (!handler && !finalizer)
  256. return;
  257. auto& unwind_context = unwind_contexts().last();
  258. VERIFY(unwind_context.executable == m_current_executable);
  259. if (handler) {
  260. m_current_block = handler;
  261. goto start;
  262. }
  263. if (finalizer) {
  264. m_current_block = finalizer;
  265. // If an exception was thrown inside the corresponding `catch` block, we need to rethrow it
  266. // from the `finally` block. But if the exception is from the `try` block, and has already been
  267. // handled by `catch`, we swallow it.
  268. if (!unwind_context.handler_called)
  269. reg(Register::exception()) = {};
  270. goto start;
  271. }
  272. // An unwind context with no handler or finalizer? We have nowhere to jump, and continuing on will make us crash on the next `Call` to a non-native function if there's an exception! So let's crash here instead.
  273. // If you run into this, you probably forgot to remove the current unwind_context somewhere.
  274. VERIFY_NOT_REACHED();
  275. }
  276. if (!reg(Register::return_value()).is_empty()) {
  277. will_return = true;
  278. // Note: A `yield` statement will not go through a finally statement,
  279. // hence we need to set a flag to not do so,
  280. // but we generate a Yield Operation in the case of returns in
  281. // generators as well, so we need to check if it will actually
  282. // continue or is a `return` in disguise
  283. will_yield = (instruction.type() == Instruction::Type::Yield && static_cast<Op::Yield const&>(instruction).continuation().has_value()) || instruction.type() == Instruction::Type::Await;
  284. break;
  285. }
  286. ++pc;
  287. }
  288. if (auto const* finalizer = m_current_block->finalizer(); finalizer && !will_yield) {
  289. auto& unwind_context = unwind_contexts().last();
  290. VERIFY(unwind_context.executable == m_current_executable);
  291. reg(Register::saved_return_value()) = reg(Register::return_value());
  292. reg(Register::return_value()) = {};
  293. m_current_block = finalizer;
  294. // the unwind_context will be pop'ed when entering the finally block
  295. continue;
  296. }
  297. if (pc.at_end())
  298. break;
  299. if (will_return)
  300. break;
  301. }
  302. }
  303. Interpreter::ValueAndFrame Interpreter::run_and_return_frame(Executable& executable, BasicBlock const* entry_point, CallFrame* in_frame)
  304. {
  305. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable);
  306. TemporaryChange restore_executable { m_current_executable, &executable };
  307. TemporaryChange restore_saved_jump { m_scheduled_jump, static_cast<BasicBlock const*>(nullptr) };
  308. VERIFY(!vm().execution_context_stack().is_empty());
  309. TemporaryChange restore_current_block { m_current_block, entry_point ?: executable.basic_blocks.first() };
  310. if (in_frame)
  311. push_call_frame(in_frame);
  312. else
  313. push_call_frame(CallFrame::create(executable.number_of_registers));
  314. vm().execution_context_stack().last()->executable = &executable;
  315. run_bytecode();
  316. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable);
  317. if constexpr (JS_BYTECODE_DEBUG) {
  318. for (size_t i = 0; i < registers().size(); ++i) {
  319. String value_string;
  320. if (registers()[i].is_empty())
  321. value_string = "(empty)"_string;
  322. else
  323. value_string = registers()[i].to_string_without_side_effects();
  324. dbgln("[{:3}] {}", i, value_string);
  325. }
  326. }
  327. auto return_value = js_undefined();
  328. if (!reg(Register::return_value()).is_empty())
  329. return_value = reg(Register::return_value());
  330. else if (!reg(Register::saved_return_value()).is_empty())
  331. return_value = reg(Register::saved_return_value());
  332. auto exception = reg(Register::exception());
  333. auto frame = pop_call_frame();
  334. // NOTE: The return value from a called function is put into $0 in the caller context.
  335. if (!m_call_frames.is_empty())
  336. call_frame().registers()[0] = return_value;
  337. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  338. // in which case this is a no-op.
  339. vm().run_queued_promise_jobs();
  340. vm().finish_execution_generation();
  341. if (!exception.is_empty()) {
  342. if (auto* call_frame = frame.get_pointer<NonnullOwnPtr<CallFrame>>())
  343. return { throw_completion(exception), move(*call_frame) };
  344. return { throw_completion(exception), nullptr };
  345. }
  346. if (auto* call_frame = frame.get_pointer<NonnullOwnPtr<CallFrame>>())
  347. return { return_value, move(*call_frame) };
  348. return { return_value, nullptr };
  349. }
  350. void Interpreter::enter_unwind_context()
  351. {
  352. unwind_contexts().empend(
  353. m_current_executable,
  354. vm().running_execution_context().lexical_environment);
  355. call_frame().previously_scheduled_jumps.append(m_scheduled_jump);
  356. m_scheduled_jump = nullptr;
  357. }
  358. void Interpreter::leave_unwind_context()
  359. {
  360. unwind_contexts().take_last();
  361. }
  362. void Interpreter::catch_exception()
  363. {
  364. accumulator() = reg(Register::exception());
  365. reg(Register::exception()) = {};
  366. auto& context = unwind_contexts().last();
  367. VERIFY(!context.handler_called);
  368. VERIFY(context.executable == &current_executable());
  369. context.handler_called = true;
  370. vm().running_execution_context().lexical_environment = context.lexical_environment;
  371. }
  372. void Interpreter::enter_object_environment(Object& object)
  373. {
  374. auto& old_environment = vm().running_execution_context().lexical_environment;
  375. saved_lexical_environment_stack().append(old_environment);
  376. vm().running_execution_context().lexical_environment = new_object_environment(object, true, old_environment);
  377. }
  378. ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM& vm, ASTNode const& node, FunctionKind kind, DeprecatedFlyString const& name)
  379. {
  380. auto executable_result = Bytecode::Generator::generate(vm, node, kind);
  381. if (executable_result.is_error())
  382. return vm.throw_completion<InternalError>(ErrorType::NotImplemented, TRY_OR_THROW_OOM(vm, executable_result.error().to_string()));
  383. auto bytecode_executable = executable_result.release_value();
  384. bytecode_executable->name = name;
  385. if (Bytecode::g_dump_bytecode)
  386. bytecode_executable->dump();
  387. return bytecode_executable;
  388. }
  389. Realm& Interpreter::realm()
  390. {
  391. return *m_vm.current_realm();
  392. }
  393. void Interpreter::push_call_frame(Variant<NonnullOwnPtr<CallFrame>, CallFrame*> frame)
  394. {
  395. m_call_frames.append(move(frame));
  396. m_current_call_frame = this->call_frame().registers();
  397. reg(Register::return_value()) = {};
  398. }
  399. Variant<NonnullOwnPtr<CallFrame>, CallFrame*> Interpreter::pop_call_frame()
  400. {
  401. auto frame = m_call_frames.take_last();
  402. m_current_call_frame = m_call_frames.is_empty() ? Span<Value> {} : this->call_frame().registers();
  403. return frame;
  404. }
  405. }
  406. namespace JS::Bytecode {
  407. ByteString Instruction::to_byte_string(Bytecode::Executable const& executable) const
  408. {
  409. #define __BYTECODE_OP(op) \
  410. case Instruction::Type::op: \
  411. return static_cast<Bytecode::Op::op const&>(*this).to_byte_string_impl(executable);
  412. switch (type()) {
  413. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  414. default:
  415. VERIFY_NOT_REACHED();
  416. }
  417. #undef __BYTECODE_OP
  418. }
  419. }
  420. namespace JS::Bytecode::Op {
  421. ThrowCompletionOr<void> Load::execute_impl(Bytecode::Interpreter&) const
  422. {
  423. // Handled in the interpreter loop.
  424. __builtin_unreachable();
  425. }
  426. ThrowCompletionOr<void> LoadImmediate::execute_impl(Bytecode::Interpreter&) const
  427. {
  428. // Handled in the interpreter loop.
  429. __builtin_unreachable();
  430. }
  431. ThrowCompletionOr<void> Store::execute_impl(Bytecode::Interpreter&) const
  432. {
  433. // Handled in the interpreter loop.
  434. __builtin_unreachable();
  435. }
  436. static ThrowCompletionOr<Value> loosely_inequals(VM& vm, Value src1, Value src2)
  437. {
  438. return Value(!TRY(is_loosely_equal(vm, src1, src2)));
  439. }
  440. static ThrowCompletionOr<Value> loosely_equals(VM& vm, Value src1, Value src2)
  441. {
  442. return Value(TRY(is_loosely_equal(vm, src1, src2)));
  443. }
  444. static ThrowCompletionOr<Value> strict_inequals(VM&, Value src1, Value src2)
  445. {
  446. return Value(!is_strictly_equal(src1, src2));
  447. }
  448. static ThrowCompletionOr<Value> strict_equals(VM&, Value src1, Value src2)
  449. {
  450. return Value(is_strictly_equal(src1, src2));
  451. }
  452. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  453. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  454. { \
  455. auto& vm = interpreter.vm(); \
  456. auto lhs = interpreter.reg(m_lhs_reg); \
  457. auto rhs = interpreter.accumulator(); \
  458. interpreter.accumulator() = TRY(op_snake_case(vm, lhs, rhs)); \
  459. return {}; \
  460. } \
  461. ByteString OpTitleCase::to_byte_string_impl(Bytecode::Executable const&) const \
  462. { \
  463. return ByteString::formatted(#OpTitleCase " {}", m_lhs_reg); \
  464. }
  465. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  466. static ThrowCompletionOr<Value> not_(VM&, Value value)
  467. {
  468. return Value(!value.to_boolean());
  469. }
  470. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  471. {
  472. return PrimitiveString::create(vm, value.typeof());
  473. }
  474. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  475. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  476. { \
  477. auto& vm = interpreter.vm(); \
  478. interpreter.accumulator() = TRY(op_snake_case(vm, interpreter.accumulator())); \
  479. return {}; \
  480. } \
  481. ByteString OpTitleCase::to_byte_string_impl(Bytecode::Executable const&) const \
  482. { \
  483. return #OpTitleCase; \
  484. }
  485. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  486. ThrowCompletionOr<void> NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
  487. {
  488. auto& vm = interpreter.vm();
  489. interpreter.accumulator() = BigInt::create(vm, m_bigint);
  490. return {};
  491. }
  492. ThrowCompletionOr<void> NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  493. {
  494. auto array = MUST(Array::create(interpreter.realm(), 0));
  495. for (size_t i = 0; i < m_element_count; i++) {
  496. auto& value = interpreter.reg(Register(m_elements[0].index() + i));
  497. array->indexed_properties().put(i, value, default_attributes);
  498. }
  499. interpreter.accumulator() = array;
  500. return {};
  501. }
  502. ThrowCompletionOr<void> NewPrimitiveArray::execute_impl(Bytecode::Interpreter& interpreter) const
  503. {
  504. auto array = MUST(Array::create(interpreter.realm(), 0));
  505. for (size_t i = 0; i < m_values.size(); i++)
  506. array->indexed_properties().put(i, m_values[i], default_attributes);
  507. interpreter.accumulator() = array;
  508. return {};
  509. }
  510. ThrowCompletionOr<void> Append::execute_impl(Bytecode::Interpreter& interpreter) const
  511. {
  512. return append(interpreter.vm(), interpreter.reg(m_lhs), interpreter.accumulator(), m_is_spread);
  513. }
  514. ThrowCompletionOr<void> ImportCall::execute_impl(Bytecode::Interpreter& interpreter) const
  515. {
  516. auto& vm = interpreter.vm();
  517. auto specifier = interpreter.reg(m_specifier);
  518. auto options_value = interpreter.reg(m_options);
  519. interpreter.accumulator() = TRY(perform_import_call(vm, specifier, options_value));
  520. return {};
  521. }
  522. ThrowCompletionOr<void> IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const
  523. {
  524. interpreter.accumulator() = TRY(iterator_to_array(interpreter.vm(), interpreter.accumulator()));
  525. return {};
  526. }
  527. ThrowCompletionOr<void> NewString::execute_impl(Bytecode::Interpreter& interpreter) const
  528. {
  529. interpreter.accumulator() = PrimitiveString::create(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  530. return {};
  531. }
  532. ThrowCompletionOr<void> NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  533. {
  534. auto& vm = interpreter.vm();
  535. auto& realm = *vm.current_realm();
  536. interpreter.accumulator() = Object::create(realm, realm.intrinsics().object_prototype());
  537. return {};
  538. }
  539. ThrowCompletionOr<void> NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
  540. {
  541. interpreter.accumulator() = new_regexp(
  542. interpreter.vm(),
  543. interpreter.current_executable().regex_table->get(m_regex_index),
  544. interpreter.current_executable().get_string(m_source_index),
  545. interpreter.current_executable().get_string(m_flags_index));
  546. return {};
  547. }
  548. #define JS_DEFINE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  549. ThrowCompletionOr<void> New##ErrorName::execute_impl(Bytecode::Interpreter& interpreter) const \
  550. { \
  551. auto& vm = interpreter.vm(); \
  552. auto& realm = *vm.current_realm(); \
  553. interpreter.accumulator() = ErrorName::create(realm, interpreter.current_executable().get_string(m_error_string)); \
  554. return {}; \
  555. } \
  556. ByteString New##ErrorName::to_byte_string_impl(Bytecode::Executable const& executable) const \
  557. { \
  558. return ByteString::formatted("New" #ErrorName " {} (\"{}\")", m_error_string, executable.string_table->get(m_error_string)); \
  559. }
  560. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DEFINE_NEW_BUILTIN_ERROR_OP)
  561. ThrowCompletionOr<void> CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const
  562. {
  563. auto& vm = interpreter.vm();
  564. auto& realm = *vm.current_realm();
  565. auto from_object = interpreter.reg(m_from_object);
  566. auto to_object = Object::create(realm, realm.intrinsics().object_prototype());
  567. HashTable<PropertyKey> excluded_names;
  568. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  569. excluded_names.set(TRY(interpreter.reg(m_excluded_names[i]).to_property_key(vm)));
  570. }
  571. TRY(to_object->copy_data_properties(vm, from_object, excluded_names));
  572. interpreter.accumulator() = to_object;
  573. return {};
  574. }
  575. ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  576. {
  577. auto& vm = interpreter.vm();
  578. auto string = TRY(interpreter.accumulator().to_primitive_string(vm));
  579. interpreter.reg(m_lhs) = PrimitiveString::create(vm, interpreter.reg(m_lhs).as_string(), string);
  580. return {};
  581. }
  582. ThrowCompletionOr<void> GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  583. {
  584. interpreter.accumulator() = TRY(get_variable(
  585. interpreter,
  586. interpreter.current_executable().get_identifier(m_identifier),
  587. interpreter.current_executable().environment_variable_caches[m_cache_index]));
  588. return {};
  589. }
  590. ThrowCompletionOr<void> GetCalleeAndThisFromEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  591. {
  592. auto callee_and_this = TRY(get_callee_and_this_from_environment(
  593. interpreter,
  594. interpreter.current_executable().get_identifier(m_identifier),
  595. interpreter.current_executable().environment_variable_caches[m_cache_index]));
  596. interpreter.reg(m_callee_reg) = callee_and_this.callee;
  597. interpreter.reg(m_this_reg) = callee_and_this.this_value;
  598. return {};
  599. }
  600. ThrowCompletionOr<void> GetGlobal::execute_impl(Bytecode::Interpreter& interpreter) const
  601. {
  602. interpreter.accumulator() = TRY(get_global(
  603. interpreter,
  604. interpreter.current_executable().get_identifier(m_identifier),
  605. interpreter.current_executable().global_variable_caches[m_cache_index]));
  606. return {};
  607. }
  608. ThrowCompletionOr<void> GetLocal::execute_impl(Bytecode::Interpreter&) const
  609. {
  610. // Handled in the interpreter loop.
  611. __builtin_unreachable();
  612. }
  613. ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  614. {
  615. auto& vm = interpreter.vm();
  616. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  617. auto reference = TRY(vm.resolve_binding(string));
  618. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  619. return {};
  620. }
  621. ThrowCompletionOr<void> CreateLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  622. {
  623. auto make_and_swap_envs = [&](auto& old_environment) {
  624. GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
  625. swap(old_environment, environment);
  626. return environment;
  627. };
  628. interpreter.saved_lexical_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().lexical_environment));
  629. return {};
  630. }
  631. ThrowCompletionOr<void> EnterObjectEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  632. {
  633. auto object = TRY(interpreter.accumulator().to_object(interpreter.vm()));
  634. interpreter.enter_object_environment(*object);
  635. return {};
  636. }
  637. ThrowCompletionOr<void> Catch::execute_impl(Bytecode::Interpreter& interpreter) const
  638. {
  639. interpreter.catch_exception();
  640. return {};
  641. }
  642. ThrowCompletionOr<void> CreateVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  643. {
  644. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  645. return create_variable(interpreter.vm(), name, m_mode, m_is_global, m_is_immutable, m_is_strict);
  646. }
  647. ThrowCompletionOr<void> SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  648. {
  649. auto& vm = interpreter.vm();
  650. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  651. TRY(set_variable(vm,
  652. name,
  653. interpreter.accumulator(),
  654. m_mode,
  655. m_initialization_mode,
  656. interpreter.current_executable().environment_variable_caches[m_cache_index]));
  657. return {};
  658. }
  659. ThrowCompletionOr<void> SetLocal::execute_impl(Bytecode::Interpreter&) const
  660. {
  661. // Handled in the interpreter loop.
  662. __builtin_unreachable();
  663. }
  664. ThrowCompletionOr<void> GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  665. {
  666. auto base_value = interpreter.accumulator();
  667. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  668. interpreter.accumulator() = TRY(get_by_id(interpreter.vm(), interpreter.current_executable().get_identifier(m_property), base_value, base_value, cache));
  669. return {};
  670. }
  671. ThrowCompletionOr<void> GetByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  672. {
  673. auto base_value = interpreter.accumulator();
  674. auto this_value = interpreter.reg(m_this_value);
  675. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  676. interpreter.accumulator() = TRY(get_by_id(interpreter.vm(), interpreter.current_executable().get_identifier(m_property), base_value, this_value, cache));
  677. return {};
  678. }
  679. ThrowCompletionOr<void> GetPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const
  680. {
  681. auto& vm = interpreter.vm();
  682. auto const& name = interpreter.current_executable().get_identifier(m_property);
  683. auto base_value = interpreter.accumulator();
  684. auto private_reference = make_private_reference(vm, base_value, name);
  685. interpreter.accumulator() = TRY(private_reference.get_value(vm));
  686. return {};
  687. }
  688. ThrowCompletionOr<void> HasPrivateId::execute_impl(Bytecode::Interpreter& interpreter) const
  689. {
  690. auto& vm = interpreter.vm();
  691. if (!interpreter.accumulator().is_object())
  692. return vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject);
  693. auto private_environment = vm.running_execution_context().private_environment;
  694. VERIFY(private_environment);
  695. auto private_name = private_environment->resolve_private_identifier(interpreter.current_executable().get_identifier(m_property));
  696. interpreter.accumulator() = Value(interpreter.accumulator().as_object().private_element_find(private_name) != nullptr);
  697. return {};
  698. }
  699. ThrowCompletionOr<void> PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  700. {
  701. auto& vm = interpreter.vm();
  702. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  703. auto value = interpreter.accumulator();
  704. auto base = interpreter.reg(m_base);
  705. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  706. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  707. TRY(put_by_property_key(vm, base, base, value, name, m_kind, &cache));
  708. interpreter.accumulator() = value;
  709. return {};
  710. }
  711. ThrowCompletionOr<void> PutByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  712. {
  713. auto& vm = interpreter.vm();
  714. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  715. auto value = interpreter.accumulator();
  716. auto base = interpreter.reg(m_base);
  717. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  718. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  719. TRY(put_by_property_key(vm, base, interpreter.reg(m_this_value), value, name, m_kind, &cache));
  720. interpreter.accumulator() = value;
  721. return {};
  722. }
  723. ThrowCompletionOr<void> PutPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const
  724. {
  725. auto& vm = interpreter.vm();
  726. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  727. auto value = interpreter.accumulator();
  728. auto object = TRY(interpreter.reg(m_base).to_object(vm));
  729. auto name = interpreter.current_executable().get_identifier(m_property);
  730. auto private_reference = make_private_reference(vm, object, name);
  731. TRY(private_reference.put_value(vm, value));
  732. interpreter.accumulator() = value;
  733. return {};
  734. }
  735. ThrowCompletionOr<void> DeleteById::execute_impl(Bytecode::Interpreter& interpreter) const
  736. {
  737. auto base_value = interpreter.accumulator();
  738. interpreter.accumulator() = TRY(Bytecode::delete_by_id(interpreter, base_value, m_property));
  739. return {};
  740. }
  741. ThrowCompletionOr<void> DeleteByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  742. {
  743. auto& vm = interpreter.vm();
  744. auto base_value = interpreter.accumulator();
  745. auto const& identifier = interpreter.current_executable().get_identifier(m_property);
  746. bool strict = vm.in_strict_mode();
  747. auto reference = Reference { base_value, identifier, interpreter.reg(m_this_value), strict };
  748. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  749. return {};
  750. }
  751. ThrowCompletionOr<void> Jump::execute_impl(Bytecode::Interpreter&) const
  752. {
  753. // Handled in the interpreter loop.
  754. __builtin_unreachable();
  755. }
  756. ThrowCompletionOr<void> ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const
  757. {
  758. auto& cached_this_value = interpreter.reg(Register::this_value());
  759. if (cached_this_value.is_empty()) {
  760. // OPTIMIZATION: Because the value of 'this' cannot be reassigned during a function execution, it's
  761. // resolved once and then saved for subsequent use.
  762. auto& vm = interpreter.vm();
  763. cached_this_value = TRY(vm.resolve_this_binding());
  764. }
  765. interpreter.accumulator() = cached_this_value;
  766. return {};
  767. }
  768. // https://tc39.es/ecma262/#sec-makesuperpropertyreference
  769. ThrowCompletionOr<void> ResolveSuperBase::execute_impl(Bytecode::Interpreter& interpreter) const
  770. {
  771. auto& vm = interpreter.vm();
  772. // 1. Let env be GetThisEnvironment().
  773. auto& env = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  774. // 2. Assert: env.HasSuperBinding() is true.
  775. VERIFY(env.has_super_binding());
  776. // 3. Let baseValue be ? env.GetSuperBase().
  777. interpreter.accumulator() = TRY(env.get_super_base());
  778. return {};
  779. }
  780. ThrowCompletionOr<void> GetNewTarget::execute_impl(Bytecode::Interpreter& interpreter) const
  781. {
  782. interpreter.accumulator() = interpreter.vm().get_new_target();
  783. return {};
  784. }
  785. ThrowCompletionOr<void> GetImportMeta::execute_impl(Bytecode::Interpreter& interpreter) const
  786. {
  787. interpreter.accumulator() = interpreter.vm().get_import_meta();
  788. return {};
  789. }
  790. ThrowCompletionOr<void> JumpConditional::execute_impl(Bytecode::Interpreter&) const
  791. {
  792. // Handled in the interpreter loop.
  793. __builtin_unreachable();
  794. }
  795. ThrowCompletionOr<void> JumpNullish::execute_impl(Bytecode::Interpreter&) const
  796. {
  797. // Handled in the interpreter loop.
  798. __builtin_unreachable();
  799. }
  800. ThrowCompletionOr<void> JumpUndefined::execute_impl(Bytecode::Interpreter&) const
  801. {
  802. // Handled in the interpreter loop.
  803. __builtin_unreachable();
  804. }
  805. static ThrowCompletionOr<Value> dispatch_builtin_call(Bytecode::Interpreter& interpreter, Bytecode::Builtin builtin, Register first_argument)
  806. {
  807. switch (builtin) {
  808. case Builtin::MathAbs:
  809. return TRY(MathObject::abs_impl(interpreter.vm(), interpreter.reg(first_argument)));
  810. case Builtin::MathLog:
  811. return TRY(MathObject::log_impl(interpreter.vm(), interpreter.reg(first_argument)));
  812. case Builtin::MathPow: {
  813. auto exponent = interpreter.reg(Register { first_argument.index() + 1 });
  814. return TRY(MathObject::pow_impl(interpreter.vm(), interpreter.reg(first_argument), exponent));
  815. }
  816. case Builtin::MathExp:
  817. return TRY(MathObject::exp_impl(interpreter.vm(), interpreter.reg(first_argument)));
  818. case Builtin::MathCeil:
  819. return TRY(MathObject::ceil_impl(interpreter.vm(), interpreter.reg(first_argument)));
  820. case Builtin::MathFloor:
  821. return TRY(MathObject::floor_impl(interpreter.vm(), interpreter.reg(first_argument)));
  822. case Builtin::MathRound:
  823. return TRY(MathObject::round_impl(interpreter.vm(), interpreter.reg(first_argument)));
  824. case Builtin::MathSqrt:
  825. return TRY(MathObject::sqrt_impl(interpreter.vm(), interpreter.reg(first_argument)));
  826. case Bytecode::Builtin::__Count:
  827. VERIFY_NOT_REACHED();
  828. }
  829. VERIFY_NOT_REACHED();
  830. }
  831. ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) const
  832. {
  833. auto callee = interpreter.reg(m_callee);
  834. TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string()));
  835. if (m_builtin.has_value() && m_argument_count == Bytecode::builtin_argument_count(m_builtin.value()) && interpreter.realm().get_builtin_value(m_builtin.value()) == callee) {
  836. interpreter.accumulator() = TRY(dispatch_builtin_call(interpreter, m_builtin.value(), m_first_argument));
  837. return {};
  838. }
  839. interpreter.accumulator() = TRY(perform_call(interpreter, interpreter.reg(m_this_value), call_type(), callee, interpreter.registers().slice(m_first_argument.index(), m_argument_count)));
  840. return {};
  841. }
  842. ThrowCompletionOr<void> CallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  843. {
  844. auto callee = interpreter.reg(m_callee);
  845. TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string()));
  846. auto argument_values = argument_list_evaluation(interpreter.vm(), interpreter.accumulator());
  847. interpreter.accumulator() = TRY(perform_call(interpreter, interpreter.reg(m_this_value), call_type(), callee, move(argument_values)));
  848. return {};
  849. }
  850. // 13.3.7.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  851. ThrowCompletionOr<void> SuperCallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  852. {
  853. interpreter.accumulator() = TRY(super_call_with_argument_array(interpreter.vm(), interpreter.accumulator(), m_is_synthetic));
  854. return {};
  855. }
  856. ThrowCompletionOr<void> NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  857. {
  858. auto& vm = interpreter.vm();
  859. interpreter.accumulator() = new_function(vm, m_function_node, m_lhs_name, m_home_object);
  860. return {};
  861. }
  862. ThrowCompletionOr<void> Return::execute_impl(Bytecode::Interpreter& interpreter) const
  863. {
  864. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  865. return {};
  866. }
  867. ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  868. {
  869. auto& vm = interpreter.vm();
  870. auto old_value = interpreter.accumulator();
  871. // OPTIMIZATION: Fast path for Int32 values.
  872. if (old_value.is_int32()) {
  873. auto integer_value = old_value.as_i32();
  874. if (integer_value != NumericLimits<i32>::max()) [[likely]] {
  875. interpreter.accumulator() = Value { integer_value + 1 };
  876. return {};
  877. }
  878. }
  879. old_value = TRY(old_value.to_numeric(vm));
  880. if (old_value.is_number())
  881. interpreter.accumulator() = Value(old_value.as_double() + 1);
  882. else
  883. interpreter.accumulator() = BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  884. return {};
  885. }
  886. ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  887. {
  888. auto& vm = interpreter.vm();
  889. auto old_value = TRY(interpreter.accumulator().to_numeric(vm));
  890. if (old_value.is_number())
  891. interpreter.accumulator() = Value(old_value.as_double() - 1);
  892. else
  893. interpreter.accumulator() = BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  894. return {};
  895. }
  896. ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  897. {
  898. return throw_completion(interpreter.accumulator());
  899. }
  900. ThrowCompletionOr<void> ThrowIfNotObject::execute_impl(Bytecode::Interpreter& interpreter) const
  901. {
  902. auto& vm = interpreter.vm();
  903. if (!interpreter.accumulator().is_object())
  904. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, interpreter.accumulator().to_string_without_side_effects());
  905. return {};
  906. }
  907. ThrowCompletionOr<void> ThrowIfNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  908. {
  909. auto& vm = interpreter.vm();
  910. auto value = interpreter.accumulator();
  911. if (value.is_nullish())
  912. return vm.throw_completion<TypeError>(ErrorType::NotObjectCoercible, value.to_string_without_side_effects());
  913. return {};
  914. }
  915. ThrowCompletionOr<void> EnterUnwindContext::execute_impl(Bytecode::Interpreter&) const
  916. {
  917. // Handled in the interpreter loop.
  918. __builtin_unreachable();
  919. }
  920. ThrowCompletionOr<void> ScheduleJump::execute_impl(Bytecode::Interpreter&) const
  921. {
  922. // Handled in the interpreter loop.
  923. __builtin_unreachable();
  924. }
  925. ThrowCompletionOr<void> LeaveLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  926. {
  927. interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
  928. return {};
  929. }
  930. ThrowCompletionOr<void> LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  931. {
  932. interpreter.leave_unwind_context();
  933. return {};
  934. }
  935. ThrowCompletionOr<void> ContinuePendingUnwind::execute_impl(Bytecode::Interpreter&) const
  936. {
  937. // Handled in the interpreter loop.
  938. __builtin_unreachable();
  939. }
  940. ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  941. {
  942. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  943. auto object = Object::create(interpreter.realm(), nullptr);
  944. object->define_direct_property("result", yielded_value, JS::default_attributes);
  945. if (m_continuation_label.has_value())
  946. // FIXME: If we get a pointer, which is not accurately representable as a double
  947. // will cause this to explode
  948. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes);
  949. else
  950. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  951. object->define_direct_property("isAwait", Value(false), JS::default_attributes);
  952. interpreter.do_return(object);
  953. return {};
  954. }
  955. ThrowCompletionOr<void> Await::execute_impl(Bytecode::Interpreter& interpreter) const
  956. {
  957. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  958. auto object = Object::create(interpreter.realm(), nullptr);
  959. object->define_direct_property("result", yielded_value, JS::default_attributes);
  960. // FIXME: If we get a pointer, which is not accurately representable as a double
  961. // will cause this to explode
  962. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label.block()))), JS::default_attributes);
  963. object->define_direct_property("isAwait", Value(true), JS::default_attributes);
  964. interpreter.do_return(object);
  965. return {};
  966. }
  967. ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  968. {
  969. interpreter.accumulator() = TRY(get_by_value(interpreter.vm(), interpreter.reg(m_base), interpreter.accumulator()));
  970. return {};
  971. }
  972. ThrowCompletionOr<void> GetByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  973. {
  974. auto& vm = interpreter.vm();
  975. // NOTE: Get the property key from the accumulator before side effects have a chance to overwrite it.
  976. auto property_key_value = interpreter.accumulator();
  977. auto object = TRY(interpreter.reg(m_base).to_object(vm));
  978. auto property_key = TRY(property_key_value.to_property_key(vm));
  979. interpreter.accumulator() = TRY(object->internal_get(property_key, interpreter.reg(m_this_value)));
  980. return {};
  981. }
  982. ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  983. {
  984. auto& vm = interpreter.vm();
  985. auto value = interpreter.accumulator();
  986. TRY(put_by_value(vm, interpreter.reg(m_base), interpreter.reg(m_property), interpreter.accumulator(), m_kind));
  987. interpreter.accumulator() = value;
  988. return {};
  989. }
  990. ThrowCompletionOr<void> PutByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  991. {
  992. auto& vm = interpreter.vm();
  993. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  994. auto value = interpreter.accumulator();
  995. auto base = interpreter.reg(m_base);
  996. auto property_key = m_kind != PropertyKind::Spread ? TRY(interpreter.reg(m_property).to_property_key(vm)) : PropertyKey {};
  997. TRY(put_by_property_key(vm, base, interpreter.reg(m_this_value), value, property_key, m_kind));
  998. interpreter.accumulator() = value;
  999. return {};
  1000. }
  1001. ThrowCompletionOr<void> DeleteByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  1002. {
  1003. auto base_value = interpreter.reg(m_base);
  1004. auto property_key_value = interpreter.accumulator();
  1005. interpreter.accumulator() = TRY(delete_by_value(interpreter, base_value, property_key_value));
  1006. return {};
  1007. }
  1008. ThrowCompletionOr<void> DeleteByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  1009. {
  1010. // NOTE: Get the property key from the accumulator before side effects have a chance to overwrite it.
  1011. auto property_key_value = interpreter.accumulator();
  1012. auto base_value = interpreter.reg(m_base);
  1013. auto this_value = interpreter.reg(m_this_value);
  1014. interpreter.accumulator() = TRY(delete_by_value_with_this(interpreter, base_value, property_key_value, this_value));
  1015. return {};
  1016. }
  1017. ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  1018. {
  1019. auto& vm = interpreter.vm();
  1020. interpreter.accumulator() = TRY(get_iterator(vm, interpreter.accumulator(), m_hint));
  1021. return {};
  1022. }
  1023. ThrowCompletionOr<void> GetObjectFromIteratorRecord::execute_impl(Bytecode::Interpreter& interpreter) const
  1024. {
  1025. auto& iterator_record = verify_cast<IteratorRecord>(interpreter.reg(m_iterator_record).as_object());
  1026. interpreter.reg(m_object) = iterator_record.iterator;
  1027. return {};
  1028. }
  1029. ThrowCompletionOr<void> GetNextMethodFromIteratorRecord::execute_impl(Bytecode::Interpreter& interpreter) const
  1030. {
  1031. auto& iterator_record = verify_cast<IteratorRecord>(interpreter.reg(m_iterator_record).as_object());
  1032. interpreter.reg(m_next_method) = iterator_record.next_method;
  1033. return {};
  1034. }
  1035. ThrowCompletionOr<void> GetMethod::execute_impl(Bytecode::Interpreter& interpreter) const
  1036. {
  1037. auto& vm = interpreter.vm();
  1038. auto identifier = interpreter.current_executable().get_identifier(m_property);
  1039. auto method = TRY(interpreter.accumulator().get_method(vm, identifier));
  1040. interpreter.accumulator() = method ?: js_undefined();
  1041. return {};
  1042. }
  1043. ThrowCompletionOr<void> GetObjectPropertyIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  1044. {
  1045. interpreter.accumulator() = TRY(get_object_property_iterator(interpreter.vm(), interpreter.accumulator()));
  1046. return {};
  1047. }
  1048. ThrowCompletionOr<void> IteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1049. {
  1050. auto& vm = interpreter.vm();
  1051. auto& iterator = verify_cast<IteratorRecord>(interpreter.accumulator().as_object());
  1052. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1053. TRY(iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} }));
  1054. return {};
  1055. }
  1056. ThrowCompletionOr<void> AsyncIteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1057. {
  1058. auto& vm = interpreter.vm();
  1059. auto& iterator = verify_cast<IteratorRecord>(interpreter.accumulator().as_object());
  1060. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1061. TRY(async_iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} }));
  1062. return {};
  1063. }
  1064. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  1065. {
  1066. auto& vm = interpreter.vm();
  1067. auto& iterator = verify_cast<IteratorRecord>(interpreter.accumulator().as_object());
  1068. interpreter.accumulator() = TRY(iterator_next(vm, iterator));
  1069. return {};
  1070. }
  1071. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const
  1072. {
  1073. interpreter.accumulator() = TRY(new_class(interpreter.vm(), interpreter.accumulator(), m_class_expression, m_lhs_name));
  1074. return {};
  1075. }
  1076. // 13.5.3.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-typeof-operator-runtime-semantics-evaluation
  1077. ThrowCompletionOr<void> TypeofVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  1078. {
  1079. auto& vm = interpreter.vm();
  1080. interpreter.accumulator() = TRY(typeof_variable(vm, interpreter.current_executable().get_identifier(m_identifier)));
  1081. return {};
  1082. }
  1083. ThrowCompletionOr<void> TypeofLocal::execute_impl(Bytecode::Interpreter& interpreter) const
  1084. {
  1085. auto& vm = interpreter.vm();
  1086. auto const& value = vm.running_execution_context().local(m_index);
  1087. interpreter.accumulator() = PrimitiveString::create(vm, value.typeof());
  1088. return {};
  1089. }
  1090. ThrowCompletionOr<void> ToNumeric::execute_impl(Bytecode::Interpreter& interpreter) const
  1091. {
  1092. interpreter.accumulator() = TRY(interpreter.accumulator().to_numeric(interpreter.vm()));
  1093. return {};
  1094. }
  1095. ThrowCompletionOr<void> BlockDeclarationInstantiation::execute_impl(Bytecode::Interpreter& interpreter) const
  1096. {
  1097. auto& vm = interpreter.vm();
  1098. auto old_environment = vm.running_execution_context().lexical_environment;
  1099. interpreter.saved_lexical_environment_stack().append(old_environment);
  1100. vm.running_execution_context().lexical_environment = new_declarative_environment(*old_environment);
  1101. m_scope_node.block_declaration_instantiation(vm, vm.running_execution_context().lexical_environment);
  1102. return {};
  1103. }
  1104. ByteString Load::to_byte_string_impl(Bytecode::Executable const&) const
  1105. {
  1106. return ByteString::formatted("Load {}", m_src);
  1107. }
  1108. ByteString LoadImmediate::to_byte_string_impl(Bytecode::Executable const&) const
  1109. {
  1110. return ByteString::formatted("LoadImmediate {}", m_value);
  1111. }
  1112. ByteString Store::to_byte_string_impl(Bytecode::Executable const&) const
  1113. {
  1114. return ByteString::formatted("Store {}", m_dst);
  1115. }
  1116. ByteString NewBigInt::to_byte_string_impl(Bytecode::Executable const&) const
  1117. {
  1118. return ByteString::formatted("NewBigInt \"{}\"", m_bigint.to_base_deprecated(10));
  1119. }
  1120. ByteString NewArray::to_byte_string_impl(Bytecode::Executable const&) const
  1121. {
  1122. StringBuilder builder;
  1123. builder.append("NewArray"sv);
  1124. if (m_element_count != 0) {
  1125. builder.appendff(" [{}-{}]", m_elements[0], m_elements[1]);
  1126. }
  1127. return builder.to_byte_string();
  1128. }
  1129. ByteString NewPrimitiveArray::to_byte_string_impl(Bytecode::Executable const&) const
  1130. {
  1131. return ByteString::formatted("NewPrimitiveArray {}"sv, m_values.span());
  1132. }
  1133. ByteString Append::to_byte_string_impl(Bytecode::Executable const&) const
  1134. {
  1135. if (m_is_spread)
  1136. return ByteString::formatted("Append lhs: **{}", m_lhs);
  1137. return ByteString::formatted("Append lhs: {}", m_lhs);
  1138. }
  1139. ByteString IteratorToArray::to_byte_string_impl(Bytecode::Executable const&) const
  1140. {
  1141. return "IteratorToArray";
  1142. }
  1143. ByteString NewString::to_byte_string_impl(Bytecode::Executable const& executable) const
  1144. {
  1145. return ByteString::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  1146. }
  1147. ByteString NewObject::to_byte_string_impl(Bytecode::Executable const&) const
  1148. {
  1149. return "NewObject";
  1150. }
  1151. ByteString NewRegExp::to_byte_string_impl(Bytecode::Executable const& executable) const
  1152. {
  1153. return ByteString::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  1154. }
  1155. ByteString CopyObjectExcludingProperties::to_byte_string_impl(Bytecode::Executable const&) const
  1156. {
  1157. StringBuilder builder;
  1158. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  1159. if (m_excluded_names_count != 0) {
  1160. builder.append(" excluding:["sv);
  1161. builder.join(", "sv, ReadonlySpan<Register>(m_excluded_names, m_excluded_names_count));
  1162. builder.append(']');
  1163. }
  1164. return builder.to_byte_string();
  1165. }
  1166. ByteString ConcatString::to_byte_string_impl(Bytecode::Executable const&) const
  1167. {
  1168. return ByteString::formatted("ConcatString {}", m_lhs);
  1169. }
  1170. ByteString GetCalleeAndThisFromEnvironment::to_byte_string_impl(Bytecode::Executable const& executable) const
  1171. {
  1172. return ByteString::formatted("GetCalleeAndThisFromEnvironment {} -> callee: {}, this:{} ", executable.identifier_table->get(m_identifier), m_callee_reg, m_this_reg);
  1173. }
  1174. ByteString GetVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1175. {
  1176. return ByteString::formatted("GetVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1177. }
  1178. ByteString GetGlobal::to_byte_string_impl(Bytecode::Executable const& executable) const
  1179. {
  1180. return ByteString::formatted("GetGlobal {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1181. }
  1182. ByteString GetLocal::to_byte_string_impl(Bytecode::Executable const&) const
  1183. {
  1184. return ByteString::formatted("GetLocal {}", m_index);
  1185. }
  1186. ByteString DeleteVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1187. {
  1188. return ByteString::formatted("DeleteVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1189. }
  1190. ByteString CreateLexicalEnvironment::to_byte_string_impl(Bytecode::Executable const&) const
  1191. {
  1192. return "CreateLexicalEnvironment"sv;
  1193. }
  1194. ByteString CreateVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1195. {
  1196. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1197. return ByteString::formatted("CreateVariable env:{} immutable:{} global:{} {} ({})", mode_string, m_is_immutable, m_is_global, m_identifier, executable.identifier_table->get(m_identifier));
  1198. }
  1199. ByteString EnterObjectEnvironment::to_byte_string_impl(Executable const&) const
  1200. {
  1201. return ByteString::formatted("EnterObjectEnvironment");
  1202. }
  1203. ByteString SetVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1204. {
  1205. auto initialization_mode_name = m_initialization_mode == InitializationMode::Initialize ? "Initialize" : "Set";
  1206. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1207. return ByteString::formatted("SetVariable env:{} init:{} {} ({})", mode_string, initialization_mode_name, m_identifier, executable.identifier_table->get(m_identifier));
  1208. }
  1209. ByteString SetLocal::to_byte_string_impl(Bytecode::Executable const&) const
  1210. {
  1211. return ByteString::formatted("SetLocal {}", m_index);
  1212. }
  1213. static StringView property_kind_to_string(PropertyKind kind)
  1214. {
  1215. switch (kind) {
  1216. case PropertyKind::Getter:
  1217. return "getter"sv;
  1218. case PropertyKind::Setter:
  1219. return "setter"sv;
  1220. case PropertyKind::KeyValue:
  1221. return "key-value"sv;
  1222. case PropertyKind::DirectKeyValue:
  1223. return "direct-key-value"sv;
  1224. case PropertyKind::Spread:
  1225. return "spread"sv;
  1226. case PropertyKind::ProtoSetter:
  1227. return "proto-setter"sv;
  1228. }
  1229. VERIFY_NOT_REACHED();
  1230. }
  1231. ByteString PutById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1232. {
  1233. auto kind = property_kind_to_string(m_kind);
  1234. return ByteString::formatted("PutById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  1235. }
  1236. ByteString PutByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1237. {
  1238. auto kind = property_kind_to_string(m_kind);
  1239. return ByteString::formatted("PutByIdWithThis kind:{} base:{}, property:{} ({}) this_value:{}", kind, m_base, m_property, executable.identifier_table->get(m_property), m_this_value);
  1240. }
  1241. ByteString PutPrivateById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1242. {
  1243. auto kind = property_kind_to_string(m_kind);
  1244. return ByteString::formatted("PutPrivateById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  1245. }
  1246. ByteString GetById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1247. {
  1248. return ByteString::formatted("GetById {} ({})", m_property, executable.identifier_table->get(m_property));
  1249. }
  1250. ByteString GetByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1251. {
  1252. return ByteString::formatted("GetByIdWithThis {} ({}) this_value:{}", m_property, executable.identifier_table->get(m_property), m_this_value);
  1253. }
  1254. ByteString GetPrivateById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1255. {
  1256. return ByteString::formatted("GetPrivateById {} ({})", m_property, executable.identifier_table->get(m_property));
  1257. }
  1258. ByteString HasPrivateId::to_byte_string_impl(Bytecode::Executable const& executable) const
  1259. {
  1260. return ByteString::formatted("HasPrivateId {} ({})", m_property, executable.identifier_table->get(m_property));
  1261. }
  1262. ByteString DeleteById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1263. {
  1264. return ByteString::formatted("DeleteById {} ({})", m_property, executable.identifier_table->get(m_property));
  1265. }
  1266. ByteString DeleteByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1267. {
  1268. return ByteString::formatted("DeleteByIdWithThis {} ({}) this_value:{}", m_property, executable.identifier_table->get(m_property), m_this_value);
  1269. }
  1270. ByteString Jump::to_byte_string_impl(Bytecode::Executable const&) const
  1271. {
  1272. if (m_true_target.has_value())
  1273. return ByteString::formatted("Jump {}", *m_true_target);
  1274. return ByteString::formatted("Jump <empty>");
  1275. }
  1276. ByteString JumpConditional::to_byte_string_impl(Bytecode::Executable const&) const
  1277. {
  1278. auto true_string = m_true_target.has_value() ? ByteString::formatted("{}", *m_true_target) : "<empty>";
  1279. auto false_string = m_false_target.has_value() ? ByteString::formatted("{}", *m_false_target) : "<empty>";
  1280. return ByteString::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  1281. }
  1282. ByteString JumpNullish::to_byte_string_impl(Bytecode::Executable const&) const
  1283. {
  1284. auto true_string = m_true_target.has_value() ? ByteString::formatted("{}", *m_true_target) : "<empty>";
  1285. auto false_string = m_false_target.has_value() ? ByteString::formatted("{}", *m_false_target) : "<empty>";
  1286. return ByteString::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  1287. }
  1288. ByteString JumpUndefined::to_byte_string_impl(Bytecode::Executable const&) const
  1289. {
  1290. auto true_string = m_true_target.has_value() ? ByteString::formatted("{}", *m_true_target) : "<empty>";
  1291. auto false_string = m_false_target.has_value() ? ByteString::formatted("{}", *m_false_target) : "<empty>";
  1292. return ByteString::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  1293. }
  1294. static StringView call_type_to_string(CallType type)
  1295. {
  1296. switch (type) {
  1297. case CallType::Call:
  1298. return ""sv;
  1299. case CallType::Construct:
  1300. return " (Construct)"sv;
  1301. case CallType::DirectEval:
  1302. return " (DirectEval)"sv;
  1303. }
  1304. VERIFY_NOT_REACHED();
  1305. }
  1306. ByteString Call::to_byte_string_impl(Bytecode::Executable const& executable) const
  1307. {
  1308. auto type = call_type_to_string(m_type);
  1309. if (m_builtin.has_value())
  1310. return ByteString::formatted("Call{} callee:{}, this:{}, first_arg:{} (builtin {})", type, m_callee, m_this_value, m_first_argument, m_builtin.value());
  1311. if (m_expression_string.has_value())
  1312. return ByteString::formatted("Call{} callee:{}, this:{}, first_arg:{} ({})", type, m_callee, m_this_value, m_first_argument, executable.get_string(m_expression_string.value()));
  1313. return ByteString::formatted("Call{} callee:{}, this:{}, first_arg:{}", type, m_callee, m_first_argument, m_this_value);
  1314. }
  1315. ByteString CallWithArgumentArray::to_byte_string_impl(Bytecode::Executable const& executable) const
  1316. {
  1317. auto type = call_type_to_string(m_type);
  1318. if (m_expression_string.has_value())
  1319. return ByteString::formatted("CallWithArgumentArray{} callee:{}, this:{}, arguments:[...acc] ({})", type, m_callee, m_this_value, executable.get_string(m_expression_string.value()));
  1320. return ByteString::formatted("CallWithArgumentArray{} callee:{}, this:{}, arguments:[...acc]", type, m_callee, m_this_value);
  1321. }
  1322. ByteString SuperCallWithArgumentArray::to_byte_string_impl(Bytecode::Executable const&) const
  1323. {
  1324. return "SuperCallWithArgumentArray arguments:[...acc]"sv;
  1325. }
  1326. ByteString NewFunction::to_byte_string_impl(Bytecode::Executable const&) const
  1327. {
  1328. StringBuilder builder;
  1329. builder.append("NewFunction"sv);
  1330. if (m_function_node.has_name())
  1331. builder.appendff(" name:{}"sv, m_function_node.name());
  1332. if (m_lhs_name.has_value())
  1333. builder.appendff(" lhs_name:{}"sv, m_lhs_name.value());
  1334. if (m_home_object.has_value())
  1335. builder.appendff(" home_object:{}"sv, m_home_object.value());
  1336. return builder.to_byte_string();
  1337. }
  1338. ByteString NewClass::to_byte_string_impl(Bytecode::Executable const&) const
  1339. {
  1340. StringBuilder builder;
  1341. auto name = m_class_expression.name();
  1342. builder.appendff("NewClass '{}'"sv, name.is_null() ? ""sv : name);
  1343. if (m_lhs_name.has_value())
  1344. builder.appendff(" lhs_name:{}"sv, m_lhs_name.value());
  1345. return builder.to_byte_string();
  1346. }
  1347. ByteString Return::to_byte_string_impl(Bytecode::Executable const&) const
  1348. {
  1349. return "Return";
  1350. }
  1351. ByteString Increment::to_byte_string_impl(Bytecode::Executable const&) const
  1352. {
  1353. return "Increment";
  1354. }
  1355. ByteString Decrement::to_byte_string_impl(Bytecode::Executable const&) const
  1356. {
  1357. return "Decrement";
  1358. }
  1359. ByteString Throw::to_byte_string_impl(Bytecode::Executable const&) const
  1360. {
  1361. return "Throw";
  1362. }
  1363. ByteString ThrowIfNotObject::to_byte_string_impl(Bytecode::Executable const&) const
  1364. {
  1365. return "ThrowIfNotObject";
  1366. }
  1367. ByteString ThrowIfNullish::to_byte_string_impl(Bytecode::Executable const&) const
  1368. {
  1369. return "ThrowIfNullish";
  1370. }
  1371. ByteString EnterUnwindContext::to_byte_string_impl(Bytecode::Executable const&) const
  1372. {
  1373. return ByteString::formatted("EnterUnwindContext entry:{}", m_entry_point);
  1374. }
  1375. ByteString ScheduleJump::to_byte_string_impl(Bytecode::Executable const&) const
  1376. {
  1377. return ByteString::formatted("ScheduleJump {}", m_target);
  1378. }
  1379. ByteString LeaveLexicalEnvironment::to_byte_string_impl(Bytecode::Executable const&) const
  1380. {
  1381. return "LeaveLexicalEnvironment"sv;
  1382. }
  1383. ByteString LeaveUnwindContext::to_byte_string_impl(Bytecode::Executable const&) const
  1384. {
  1385. return "LeaveUnwindContext";
  1386. }
  1387. ByteString ContinuePendingUnwind::to_byte_string_impl(Bytecode::Executable const&) const
  1388. {
  1389. return ByteString::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  1390. }
  1391. ByteString Yield::to_byte_string_impl(Bytecode::Executable const&) const
  1392. {
  1393. if (m_continuation_label.has_value())
  1394. return ByteString::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  1395. return ByteString::formatted("Yield return");
  1396. }
  1397. ByteString Await::to_byte_string_impl(Bytecode::Executable const&) const
  1398. {
  1399. return ByteString::formatted("Await continuation:@{}", m_continuation_label.block().name());
  1400. }
  1401. ByteString GetByValue::to_byte_string_impl(Bytecode::Executable const&) const
  1402. {
  1403. return ByteString::formatted("GetByValue base:{}", m_base);
  1404. }
  1405. ByteString GetByValueWithThis::to_byte_string_impl(Bytecode::Executable const&) const
  1406. {
  1407. return ByteString::formatted("GetByValueWithThis base:{} this_value:{}", m_base, m_this_value);
  1408. }
  1409. ByteString PutByValue::to_byte_string_impl(Bytecode::Executable const&) const
  1410. {
  1411. auto kind = property_kind_to_string(m_kind);
  1412. return ByteString::formatted("PutByValue kind:{} base:{}, property:{}", kind, m_base, m_property);
  1413. }
  1414. ByteString PutByValueWithThis::to_byte_string_impl(Bytecode::Executable const&) const
  1415. {
  1416. auto kind = property_kind_to_string(m_kind);
  1417. return ByteString::formatted("PutByValueWithThis kind:{} base:{}, property:{} this_value:{}", kind, m_base, m_property, m_this_value);
  1418. }
  1419. ByteString DeleteByValue::to_byte_string_impl(Bytecode::Executable const&) const
  1420. {
  1421. return ByteString::formatted("DeleteByValue base:{}", m_base);
  1422. }
  1423. ByteString DeleteByValueWithThis::to_byte_string_impl(Bytecode::Executable const&) const
  1424. {
  1425. return ByteString::formatted("DeleteByValueWithThis base:{} this_value:{}", m_base, m_this_value);
  1426. }
  1427. ByteString GetIterator::to_byte_string_impl(Executable const&) const
  1428. {
  1429. auto hint = m_hint == IteratorHint::Sync ? "sync" : "async";
  1430. return ByteString::formatted("GetIterator hint:{}", hint);
  1431. }
  1432. ByteString GetMethod::to_byte_string_impl(Bytecode::Executable const& executable) const
  1433. {
  1434. return ByteString::formatted("GetMethod {} ({})", m_property, executable.identifier_table->get(m_property));
  1435. }
  1436. ByteString GetObjectPropertyIterator::to_byte_string_impl(Bytecode::Executable const&) const
  1437. {
  1438. return "GetObjectPropertyIterator";
  1439. }
  1440. ByteString IteratorClose::to_byte_string_impl(Bytecode::Executable const&) const
  1441. {
  1442. if (!m_completion_value.has_value())
  1443. return ByteString::formatted("IteratorClose completion_type={} completion_value=<empty>", to_underlying(m_completion_type));
  1444. auto completion_value_string = m_completion_value->to_string_without_side_effects();
  1445. return ByteString::formatted("IteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string);
  1446. }
  1447. ByteString AsyncIteratorClose::to_byte_string_impl(Bytecode::Executable const&) const
  1448. {
  1449. if (!m_completion_value.has_value())
  1450. return ByteString::formatted("AsyncIteratorClose completion_type={} completion_value=<empty>", to_underlying(m_completion_type));
  1451. auto completion_value_string = m_completion_value->to_string_without_side_effects();
  1452. return ByteString::formatted("AsyncIteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string);
  1453. }
  1454. ByteString IteratorNext::to_byte_string_impl(Executable const&) const
  1455. {
  1456. return "IteratorNext";
  1457. }
  1458. ByteString ResolveThisBinding::to_byte_string_impl(Bytecode::Executable const&) const
  1459. {
  1460. return "ResolveThisBinding"sv;
  1461. }
  1462. ByteString ResolveSuperBase::to_byte_string_impl(Bytecode::Executable const&) const
  1463. {
  1464. return "ResolveSuperBase"sv;
  1465. }
  1466. ByteString GetNewTarget::to_byte_string_impl(Bytecode::Executable const&) const
  1467. {
  1468. return "GetNewTarget"sv;
  1469. }
  1470. ByteString GetImportMeta::to_byte_string_impl(Bytecode::Executable const&) const
  1471. {
  1472. return "GetImportMeta"sv;
  1473. }
  1474. ByteString TypeofVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1475. {
  1476. return ByteString::formatted("TypeofVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1477. }
  1478. ByteString TypeofLocal::to_byte_string_impl(Bytecode::Executable const&) const
  1479. {
  1480. return ByteString::formatted("TypeofLocal {}", m_index);
  1481. }
  1482. ByteString ToNumeric::to_byte_string_impl(Bytecode::Executable const&) const
  1483. {
  1484. return "ToNumeric"sv;
  1485. }
  1486. ByteString BlockDeclarationInstantiation::to_byte_string_impl(Bytecode::Executable const&) const
  1487. {
  1488. return "BlockDeclarationInstantiation"sv;
  1489. }
  1490. ByteString ImportCall::to_byte_string_impl(Bytecode::Executable const&) const
  1491. {
  1492. return ByteString::formatted("ImportCall specifier:{} options:{}"sv, m_specifier, m_options);
  1493. }
  1494. ByteString Catch::to_byte_string_impl(Bytecode::Executable const&) const
  1495. {
  1496. return "Catch"sv;
  1497. }
  1498. ByteString GetObjectFromIteratorRecord::to_byte_string_impl(Bytecode::Executable const&) const
  1499. {
  1500. return ByteString::formatted("GetObjectFromIteratorRecord object:{} <- iterator_record:{}", m_object, m_iterator_record);
  1501. }
  1502. ByteString GetNextMethodFromIteratorRecord::to_byte_string_impl(Bytecode::Executable const&) const
  1503. {
  1504. return ByteString::formatted("GetNextMethodFromIteratorRecord next_method:{} <- iterator_record:{}", m_next_method, m_iterator_record);
  1505. }
  1506. }