Interpreter.cpp 70 KB

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