Interpreter.cpp 71 KB

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