Interpreter.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  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. ThrowCompletionOr<NonnullRefPtr<Bytecode::Executable>> compile(VM& vm, ASTNode const& node, FunctionKind kind, DeprecatedFlyString const& name)
  375. {
  376. auto executable_result = Bytecode::Generator::generate(node, kind);
  377. if (executable_result.is_error())
  378. return vm.throw_completion<InternalError>(ErrorType::NotImplemented, TRY_OR_THROW_OOM(vm, executable_result.error().to_string()));
  379. auto bytecode_executable = executable_result.release_value();
  380. bytecode_executable->name = name;
  381. if (Bytecode::g_dump_bytecode)
  382. bytecode_executable->dump();
  383. return bytecode_executable;
  384. }
  385. Realm& Interpreter::realm()
  386. {
  387. return *m_vm.current_realm();
  388. }
  389. void Interpreter::push_call_frame(Variant<NonnullOwnPtr<CallFrame>, CallFrame*> frame, size_t register_count)
  390. {
  391. m_call_frames.append(move(frame));
  392. this->call_frame().registers.resize(register_count);
  393. m_current_call_frame = this->call_frame().registers;
  394. reg(Register::return_value()) = {};
  395. }
  396. Variant<NonnullOwnPtr<CallFrame>, CallFrame*> Interpreter::pop_call_frame()
  397. {
  398. auto frame = m_call_frames.take_last();
  399. m_current_call_frame = m_call_frames.is_empty() ? Span<Value> {} : this->call_frame().registers;
  400. return frame;
  401. }
  402. }
  403. namespace JS::Bytecode {
  404. DeprecatedString Instruction::to_deprecated_string(Bytecode::Executable const& executable) const
  405. {
  406. #define __BYTECODE_OP(op) \
  407. case Instruction::Type::op: \
  408. return static_cast<Bytecode::Op::op const&>(*this).to_deprecated_string_impl(executable);
  409. switch (type()) {
  410. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  411. default:
  412. VERIFY_NOT_REACHED();
  413. }
  414. #undef __BYTECODE_OP
  415. }
  416. }
  417. namespace JS::Bytecode::Op {
  418. ThrowCompletionOr<void> Load::execute_impl(Bytecode::Interpreter&) const
  419. {
  420. // Handled in the interpreter loop.
  421. __builtin_unreachable();
  422. }
  423. ThrowCompletionOr<void> LoadImmediate::execute_impl(Bytecode::Interpreter&) const
  424. {
  425. // Handled in the interpreter loop.
  426. __builtin_unreachable();
  427. }
  428. ThrowCompletionOr<void> Store::execute_impl(Bytecode::Interpreter&) const
  429. {
  430. // Handled in the interpreter loop.
  431. __builtin_unreachable();
  432. }
  433. static ThrowCompletionOr<Value> loosely_inequals(VM& vm, Value src1, Value src2)
  434. {
  435. return Value(!TRY(is_loosely_equal(vm, src1, src2)));
  436. }
  437. static ThrowCompletionOr<Value> loosely_equals(VM& vm, Value src1, Value src2)
  438. {
  439. return Value(TRY(is_loosely_equal(vm, src1, src2)));
  440. }
  441. static ThrowCompletionOr<Value> strict_inequals(VM&, Value src1, Value src2)
  442. {
  443. return Value(!is_strictly_equal(src1, src2));
  444. }
  445. static ThrowCompletionOr<Value> strict_equals(VM&, Value src1, Value src2)
  446. {
  447. return Value(is_strictly_equal(src1, src2));
  448. }
  449. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  450. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  451. { \
  452. auto& vm = interpreter.vm(); \
  453. auto lhs = interpreter.reg(m_lhs_reg); \
  454. auto rhs = interpreter.accumulator(); \
  455. interpreter.accumulator() = TRY(op_snake_case(vm, lhs, rhs)); \
  456. return {}; \
  457. } \
  458. DeprecatedString OpTitleCase::to_deprecated_string_impl(Bytecode::Executable const&) const \
  459. { \
  460. return DeprecatedString::formatted(#OpTitleCase " {}", m_lhs_reg); \
  461. }
  462. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  463. static ThrowCompletionOr<Value> not_(VM&, Value value)
  464. {
  465. return Value(!value.to_boolean());
  466. }
  467. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  468. {
  469. return PrimitiveString::create(vm, value.typeof());
  470. }
  471. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  472. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  473. { \
  474. auto& vm = interpreter.vm(); \
  475. interpreter.accumulator() = TRY(op_snake_case(vm, interpreter.accumulator())); \
  476. return {}; \
  477. } \
  478. DeprecatedString OpTitleCase::to_deprecated_string_impl(Bytecode::Executable const&) const \
  479. { \
  480. return #OpTitleCase; \
  481. }
  482. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  483. ThrowCompletionOr<void> NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
  484. {
  485. auto& vm = interpreter.vm();
  486. interpreter.accumulator() = BigInt::create(vm, m_bigint);
  487. return {};
  488. }
  489. ThrowCompletionOr<void> NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  490. {
  491. auto array = MUST(Array::create(interpreter.realm(), 0));
  492. for (size_t i = 0; i < m_element_count; i++) {
  493. auto& value = interpreter.reg(Register(m_elements[0].index() + i));
  494. array->indexed_properties().put(i, value, default_attributes);
  495. }
  496. interpreter.accumulator() = array;
  497. return {};
  498. }
  499. ThrowCompletionOr<void> Append::execute_impl(Bytecode::Interpreter& interpreter) const
  500. {
  501. return append(interpreter.vm(), interpreter.reg(m_lhs), interpreter.accumulator(), m_is_spread);
  502. }
  503. ThrowCompletionOr<void> ImportCall::execute_impl(Bytecode::Interpreter& interpreter) const
  504. {
  505. auto& vm = interpreter.vm();
  506. auto specifier = interpreter.reg(m_specifier);
  507. auto options_value = interpreter.reg(m_options);
  508. interpreter.accumulator() = TRY(perform_import_call(vm, specifier, options_value));
  509. return {};
  510. }
  511. ThrowCompletionOr<void> IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const
  512. {
  513. interpreter.accumulator() = TRY(iterator_to_array(interpreter.vm(), interpreter.accumulator()));
  514. return {};
  515. }
  516. ThrowCompletionOr<void> NewString::execute_impl(Bytecode::Interpreter& interpreter) const
  517. {
  518. interpreter.accumulator() = PrimitiveString::create(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  519. return {};
  520. }
  521. ThrowCompletionOr<void> NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  522. {
  523. auto& vm = interpreter.vm();
  524. auto& realm = *vm.current_realm();
  525. interpreter.accumulator() = Object::create(realm, realm.intrinsics().object_prototype());
  526. return {};
  527. }
  528. ThrowCompletionOr<void> NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
  529. {
  530. interpreter.accumulator() = new_regexp(
  531. interpreter.vm(),
  532. interpreter.current_executable().regex_table->get(m_regex_index),
  533. interpreter.current_executable().get_string(m_source_index),
  534. interpreter.current_executable().get_string(m_flags_index));
  535. return {};
  536. }
  537. #define JS_DEFINE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  538. ThrowCompletionOr<void> New##ErrorName::execute_impl(Bytecode::Interpreter& interpreter) const \
  539. { \
  540. auto& vm = interpreter.vm(); \
  541. auto& realm = *vm.current_realm(); \
  542. interpreter.accumulator() = ErrorName::create(realm, interpreter.current_executable().get_string(m_error_string)); \
  543. return {}; \
  544. } \
  545. DeprecatedString New##ErrorName::to_deprecated_string_impl(Bytecode::Executable const& executable) const \
  546. { \
  547. return DeprecatedString::formatted("New" #ErrorName " {} (\"{}\")", m_error_string, executable.string_table->get(m_error_string)); \
  548. }
  549. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DEFINE_NEW_BUILTIN_ERROR_OP)
  550. ThrowCompletionOr<void> CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const
  551. {
  552. auto& vm = interpreter.vm();
  553. auto& realm = *vm.current_realm();
  554. auto from_object = interpreter.reg(m_from_object);
  555. auto to_object = Object::create(realm, realm.intrinsics().object_prototype());
  556. HashTable<PropertyKey> excluded_names;
  557. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  558. excluded_names.set(TRY(interpreter.reg(m_excluded_names[i]).to_property_key(vm)));
  559. }
  560. TRY(to_object->copy_data_properties(vm, from_object, excluded_names));
  561. interpreter.accumulator() = to_object;
  562. return {};
  563. }
  564. ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  565. {
  566. auto& vm = interpreter.vm();
  567. auto string = TRY(interpreter.accumulator().to_primitive_string(vm));
  568. interpreter.reg(m_lhs) = PrimitiveString::create(vm, interpreter.reg(m_lhs).as_string(), string);
  569. return {};
  570. }
  571. ThrowCompletionOr<void> GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  572. {
  573. interpreter.accumulator() = TRY(get_variable(
  574. interpreter,
  575. interpreter.current_executable().get_identifier(m_identifier),
  576. interpreter.current_executable().environment_variable_caches[m_cache_index]));
  577. return {};
  578. }
  579. ThrowCompletionOr<void> GetCalleeAndThisFromEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  580. {
  581. auto callee_and_this = TRY(get_callee_and_this_from_environment(
  582. interpreter,
  583. interpreter.current_executable().get_identifier(m_identifier),
  584. interpreter.current_executable().environment_variable_caches[m_cache_index]));
  585. interpreter.reg(m_callee_reg) = callee_and_this.callee;
  586. interpreter.reg(m_this_reg) = callee_and_this.this_value;
  587. return {};
  588. }
  589. ThrowCompletionOr<void> GetGlobal::execute_impl(Bytecode::Interpreter& interpreter) const
  590. {
  591. interpreter.accumulator() = TRY(get_global(
  592. interpreter,
  593. interpreter.current_executable().get_identifier(m_identifier),
  594. interpreter.current_executable().global_variable_caches[m_cache_index]));
  595. return {};
  596. }
  597. ThrowCompletionOr<void> GetLocal::execute_impl(Bytecode::Interpreter&) const
  598. {
  599. // Handled in the interpreter loop.
  600. __builtin_unreachable();
  601. }
  602. ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  603. {
  604. auto& vm = interpreter.vm();
  605. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  606. auto reference = TRY(vm.resolve_binding(string));
  607. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  608. return {};
  609. }
  610. ThrowCompletionOr<void> CreateLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  611. {
  612. auto make_and_swap_envs = [&](auto& old_environment) {
  613. GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
  614. swap(old_environment, environment);
  615. return environment;
  616. };
  617. interpreter.saved_lexical_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().lexical_environment));
  618. return {};
  619. }
  620. ThrowCompletionOr<void> EnterObjectEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  621. {
  622. auto& vm = interpreter.vm();
  623. auto& old_environment = vm.running_execution_context().lexical_environment;
  624. interpreter.saved_lexical_environment_stack().append(old_environment);
  625. auto object = TRY(interpreter.accumulator().to_object(vm));
  626. vm.running_execution_context().lexical_environment = new_object_environment(object, true, old_environment);
  627. return {};
  628. }
  629. ThrowCompletionOr<void> Catch::execute_impl(Bytecode::Interpreter& interpreter) const
  630. {
  631. interpreter.catch_exception();
  632. return {};
  633. }
  634. ThrowCompletionOr<void> CreateVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  635. {
  636. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  637. return create_variable(interpreter.vm(), name, m_mode, m_is_global, m_is_immutable, m_is_strict);
  638. }
  639. ThrowCompletionOr<void> SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  640. {
  641. auto& vm = interpreter.vm();
  642. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  643. TRY(set_variable(vm, name, interpreter.accumulator(), m_mode, m_initialization_mode));
  644. return {};
  645. }
  646. ThrowCompletionOr<void> SetLocal::execute_impl(Bytecode::Interpreter&) const
  647. {
  648. // Handled in the interpreter loop.
  649. __builtin_unreachable();
  650. }
  651. ThrowCompletionOr<void> GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  652. {
  653. auto base_value = interpreter.accumulator();
  654. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  655. interpreter.accumulator() = TRY(get_by_id(interpreter.vm(), interpreter.current_executable().get_identifier(m_property), base_value, base_value, cache));
  656. return {};
  657. }
  658. ThrowCompletionOr<void> GetByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  659. {
  660. auto base_value = interpreter.accumulator();
  661. auto this_value = interpreter.reg(m_this_value);
  662. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  663. interpreter.accumulator() = TRY(get_by_id(interpreter.vm(), interpreter.current_executable().get_identifier(m_property), base_value, this_value, cache));
  664. return {};
  665. }
  666. ThrowCompletionOr<void> GetPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const
  667. {
  668. auto& vm = interpreter.vm();
  669. auto const& name = interpreter.current_executable().get_identifier(m_property);
  670. auto base_value = interpreter.accumulator();
  671. auto private_reference = make_private_reference(vm, base_value, name);
  672. interpreter.accumulator() = TRY(private_reference.get_value(vm));
  673. return {};
  674. }
  675. ThrowCompletionOr<void> HasPrivateId::execute_impl(Bytecode::Interpreter& interpreter) const
  676. {
  677. auto& vm = interpreter.vm();
  678. if (!interpreter.accumulator().is_object())
  679. return vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject);
  680. auto private_environment = vm.running_execution_context().private_environment;
  681. VERIFY(private_environment);
  682. auto private_name = private_environment->resolve_private_identifier(interpreter.current_executable().get_identifier(m_property));
  683. interpreter.accumulator() = Value(interpreter.accumulator().as_object().private_element_find(private_name) != nullptr);
  684. return {};
  685. }
  686. ThrowCompletionOr<void> PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  687. {
  688. auto& vm = interpreter.vm();
  689. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  690. auto value = interpreter.accumulator();
  691. auto base = interpreter.reg(m_base);
  692. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  693. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  694. TRY(put_by_property_key(vm, base, base, value, name, m_kind, &cache));
  695. interpreter.accumulator() = value;
  696. return {};
  697. }
  698. ThrowCompletionOr<void> PutByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  699. {
  700. auto& vm = interpreter.vm();
  701. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  702. auto value = interpreter.accumulator();
  703. auto base = interpreter.reg(m_base);
  704. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  705. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  706. TRY(put_by_property_key(vm, base, interpreter.reg(m_this_value), value, name, m_kind, &cache));
  707. interpreter.accumulator() = value;
  708. return {};
  709. }
  710. ThrowCompletionOr<void> PutPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const
  711. {
  712. auto& vm = interpreter.vm();
  713. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  714. auto value = interpreter.accumulator();
  715. auto object = TRY(interpreter.reg(m_base).to_object(vm));
  716. auto name = interpreter.current_executable().get_identifier(m_property);
  717. auto private_reference = make_private_reference(vm, object, name);
  718. TRY(private_reference.put_value(vm, value));
  719. interpreter.accumulator() = value;
  720. return {};
  721. }
  722. ThrowCompletionOr<void> DeleteById::execute_impl(Bytecode::Interpreter& interpreter) const
  723. {
  724. auto base_value = interpreter.accumulator();
  725. interpreter.accumulator() = TRY(Bytecode::delete_by_id(interpreter, base_value, m_property));
  726. return {};
  727. }
  728. ThrowCompletionOr<void> DeleteByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  729. {
  730. auto& vm = interpreter.vm();
  731. auto base_value = interpreter.accumulator();
  732. auto const& identifier = interpreter.current_executable().get_identifier(m_property);
  733. bool strict = vm.in_strict_mode();
  734. auto reference = Reference { base_value, identifier, interpreter.reg(m_this_value), strict };
  735. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  736. return {};
  737. }
  738. ThrowCompletionOr<void> Jump::execute_impl(Bytecode::Interpreter&) const
  739. {
  740. // Handled in the interpreter loop.
  741. __builtin_unreachable();
  742. }
  743. ThrowCompletionOr<void> ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const
  744. {
  745. auto& cached_this_value = interpreter.reg(Register::this_value());
  746. if (cached_this_value.is_empty()) {
  747. // OPTIMIZATION: Because the value of 'this' cannot be reassigned during a function execution, it's
  748. // resolved once and then saved for subsequent use.
  749. auto& vm = interpreter.vm();
  750. cached_this_value = TRY(vm.resolve_this_binding());
  751. }
  752. interpreter.accumulator() = cached_this_value;
  753. return {};
  754. }
  755. // https://tc39.es/ecma262/#sec-makesuperpropertyreference
  756. ThrowCompletionOr<void> ResolveSuperBase::execute_impl(Bytecode::Interpreter& interpreter) const
  757. {
  758. auto& vm = interpreter.vm();
  759. // 1. Let env be GetThisEnvironment().
  760. auto& env = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  761. // 2. Assert: env.HasSuperBinding() is true.
  762. VERIFY(env.has_super_binding());
  763. // 3. Let baseValue be ? env.GetSuperBase().
  764. interpreter.accumulator() = TRY(env.get_super_base());
  765. return {};
  766. }
  767. ThrowCompletionOr<void> GetNewTarget::execute_impl(Bytecode::Interpreter& interpreter) const
  768. {
  769. interpreter.accumulator() = interpreter.vm().get_new_target();
  770. return {};
  771. }
  772. ThrowCompletionOr<void> GetImportMeta::execute_impl(Bytecode::Interpreter& interpreter) const
  773. {
  774. interpreter.accumulator() = interpreter.vm().get_import_meta();
  775. return {};
  776. }
  777. ThrowCompletionOr<void> JumpConditional::execute_impl(Bytecode::Interpreter&) const
  778. {
  779. // Handled in the interpreter loop.
  780. __builtin_unreachable();
  781. }
  782. ThrowCompletionOr<void> JumpNullish::execute_impl(Bytecode::Interpreter&) const
  783. {
  784. // Handled in the interpreter loop.
  785. __builtin_unreachable();
  786. }
  787. ThrowCompletionOr<void> JumpUndefined::execute_impl(Bytecode::Interpreter&) const
  788. {
  789. // Handled in the interpreter loop.
  790. __builtin_unreachable();
  791. }
  792. ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) const
  793. {
  794. auto& vm = interpreter.vm();
  795. auto callee = interpreter.reg(m_callee);
  796. TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string()));
  797. MarkedVector<Value> argument_values(vm.heap());
  798. argument_values.ensure_capacity(m_argument_count);
  799. for (u32 i = 0; i < m_argument_count; ++i) {
  800. argument_values.unchecked_append(interpreter.reg(Register { m_first_argument.index() + i }));
  801. }
  802. interpreter.accumulator() = TRY(perform_call(interpreter, interpreter.reg(m_this_value), call_type(), callee, move(argument_values)));
  803. return {};
  804. }
  805. ThrowCompletionOr<void> CallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  806. {
  807. auto callee = interpreter.reg(m_callee);
  808. TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string()));
  809. auto argument_values = argument_list_evaluation(interpreter.vm(), interpreter.accumulator());
  810. interpreter.accumulator() = TRY(perform_call(interpreter, interpreter.reg(m_this_value), call_type(), callee, move(argument_values)));
  811. return {};
  812. }
  813. // 13.3.7.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  814. ThrowCompletionOr<void> SuperCallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  815. {
  816. interpreter.accumulator() = TRY(super_call_with_argument_array(interpreter.vm(), interpreter.accumulator(), m_is_synthetic));
  817. return {};
  818. }
  819. ThrowCompletionOr<void> NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  820. {
  821. auto& vm = interpreter.vm();
  822. interpreter.accumulator() = new_function(vm, m_function_node, m_lhs_name, m_home_object);
  823. return {};
  824. }
  825. ThrowCompletionOr<void> Return::execute_impl(Bytecode::Interpreter& interpreter) const
  826. {
  827. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  828. return {};
  829. }
  830. ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  831. {
  832. auto& vm = interpreter.vm();
  833. auto old_value = TRY(interpreter.accumulator().to_numeric(vm));
  834. if (old_value.is_number())
  835. interpreter.accumulator() = Value(old_value.as_double() + 1);
  836. else
  837. interpreter.accumulator() = BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  838. return {};
  839. }
  840. ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  841. {
  842. auto& vm = interpreter.vm();
  843. auto old_value = TRY(interpreter.accumulator().to_numeric(vm));
  844. if (old_value.is_number())
  845. interpreter.accumulator() = Value(old_value.as_double() - 1);
  846. else
  847. interpreter.accumulator() = BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  848. return {};
  849. }
  850. ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  851. {
  852. return throw_completion(interpreter.accumulator());
  853. }
  854. ThrowCompletionOr<void> ThrowIfNotObject::execute_impl(Bytecode::Interpreter& interpreter) const
  855. {
  856. auto& vm = interpreter.vm();
  857. if (!interpreter.accumulator().is_object())
  858. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, interpreter.accumulator().to_string_without_side_effects());
  859. return {};
  860. }
  861. ThrowCompletionOr<void> ThrowIfNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  862. {
  863. auto& vm = interpreter.vm();
  864. auto value = interpreter.accumulator();
  865. if (value.is_nullish())
  866. return vm.throw_completion<TypeError>(ErrorType::NotObjectCoercible, value.to_string_without_side_effects());
  867. return {};
  868. }
  869. ThrowCompletionOr<void> EnterUnwindContext::execute_impl(Bytecode::Interpreter&) const
  870. {
  871. // Handled in the interpreter loop.
  872. __builtin_unreachable();
  873. }
  874. ThrowCompletionOr<void> ScheduleJump::execute_impl(Bytecode::Interpreter&) const
  875. {
  876. // Handled in the interpreter loop.
  877. __builtin_unreachable();
  878. }
  879. ThrowCompletionOr<void> LeaveLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  880. {
  881. interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
  882. return {};
  883. }
  884. ThrowCompletionOr<void> LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  885. {
  886. interpreter.leave_unwind_context();
  887. return {};
  888. }
  889. ThrowCompletionOr<void> ContinuePendingUnwind::execute_impl(Bytecode::Interpreter&) const
  890. {
  891. // Handled in the interpreter loop.
  892. __builtin_unreachable();
  893. }
  894. ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  895. {
  896. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  897. auto object = Object::create(interpreter.realm(), nullptr);
  898. object->define_direct_property("result", yielded_value, JS::default_attributes);
  899. if (m_continuation_label.has_value())
  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. else
  904. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  905. object->define_direct_property("isAwait", Value(false), JS::default_attributes);
  906. interpreter.do_return(object);
  907. return {};
  908. }
  909. ThrowCompletionOr<void> Await::execute_impl(Bytecode::Interpreter& interpreter) const
  910. {
  911. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  912. auto object = Object::create(interpreter.realm(), nullptr);
  913. object->define_direct_property("result", yielded_value, JS::default_attributes);
  914. // FIXME: If we get a pointer, which is not accurately representable as a double
  915. // will cause this to explode
  916. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label.block()))), JS::default_attributes);
  917. object->define_direct_property("isAwait", Value(true), JS::default_attributes);
  918. interpreter.do_return(object);
  919. return {};
  920. }
  921. ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  922. {
  923. interpreter.accumulator() = TRY(get_by_value(interpreter.vm(), interpreter.reg(m_base), interpreter.accumulator()));
  924. return {};
  925. }
  926. ThrowCompletionOr<void> GetByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  927. {
  928. auto& vm = interpreter.vm();
  929. // NOTE: Get the property key from the accumulator before side effects have a chance to overwrite it.
  930. auto property_key_value = interpreter.accumulator();
  931. auto object = TRY(interpreter.reg(m_base).to_object(vm));
  932. auto property_key = TRY(property_key_value.to_property_key(vm));
  933. interpreter.accumulator() = TRY(object->internal_get(property_key, interpreter.reg(m_this_value)));
  934. return {};
  935. }
  936. ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  937. {
  938. auto& vm = interpreter.vm();
  939. auto value = interpreter.accumulator();
  940. TRY(put_by_value(vm, interpreter.reg(m_base), interpreter.reg(m_property), interpreter.accumulator(), m_kind));
  941. interpreter.accumulator() = value;
  942. return {};
  943. }
  944. ThrowCompletionOr<void> PutByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  945. {
  946. auto& vm = interpreter.vm();
  947. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  948. auto value = interpreter.accumulator();
  949. auto base = interpreter.reg(m_base);
  950. auto property_key = m_kind != PropertyKind::Spread ? TRY(interpreter.reg(m_property).to_property_key(vm)) : PropertyKey {};
  951. TRY(put_by_property_key(vm, base, interpreter.reg(m_this_value), value, property_key, m_kind));
  952. interpreter.accumulator() = value;
  953. return {};
  954. }
  955. ThrowCompletionOr<void> DeleteByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  956. {
  957. auto base_value = interpreter.reg(m_base);
  958. auto property_key_value = interpreter.accumulator();
  959. interpreter.accumulator() = TRY(delete_by_value(interpreter, base_value, property_key_value));
  960. return {};
  961. }
  962. ThrowCompletionOr<void> DeleteByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  963. {
  964. // NOTE: Get the property key from the accumulator before side effects have a chance to overwrite it.
  965. auto property_key_value = interpreter.accumulator();
  966. auto base_value = interpreter.reg(m_base);
  967. auto this_value = interpreter.reg(m_this_value);
  968. interpreter.accumulator() = TRY(delete_by_value_with_this(interpreter, base_value, property_key_value, this_value));
  969. return {};
  970. }
  971. ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  972. {
  973. auto& vm = interpreter.vm();
  974. auto iterator = TRY(get_iterator(vm, interpreter.accumulator(), m_hint));
  975. interpreter.accumulator() = iterator_to_object(vm, iterator);
  976. return {};
  977. }
  978. ThrowCompletionOr<void> GetMethod::execute_impl(Bytecode::Interpreter& interpreter) const
  979. {
  980. auto& vm = interpreter.vm();
  981. auto identifier = interpreter.current_executable().get_identifier(m_property);
  982. auto method = TRY(interpreter.accumulator().get_method(vm, identifier));
  983. interpreter.accumulator() = method ?: js_undefined();
  984. return {};
  985. }
  986. ThrowCompletionOr<void> GetObjectPropertyIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  987. {
  988. interpreter.accumulator() = TRY(get_object_property_iterator(interpreter.vm(), interpreter.accumulator()));
  989. return {};
  990. }
  991. ThrowCompletionOr<void> IteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  992. {
  993. auto& vm = interpreter.vm();
  994. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  995. auto iterator = object_to_iterator(vm, iterator_object);
  996. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  997. TRY(iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} }));
  998. return {};
  999. }
  1000. ThrowCompletionOr<void> AsyncIteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1001. {
  1002. auto& vm = interpreter.vm();
  1003. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  1004. auto iterator = object_to_iterator(vm, iterator_object);
  1005. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1006. TRY(async_iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} }));
  1007. return {};
  1008. }
  1009. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  1010. {
  1011. auto& vm = interpreter.vm();
  1012. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  1013. auto iterator = object_to_iterator(vm, iterator_object);
  1014. interpreter.accumulator() = TRY(iterator_next(vm, iterator));
  1015. return {};
  1016. }
  1017. ThrowCompletionOr<void> IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const
  1018. {
  1019. auto& vm = interpreter.vm();
  1020. auto iterator_result = TRY(interpreter.accumulator().to_object(vm));
  1021. auto complete = TRY(iterator_complete(vm, iterator_result));
  1022. interpreter.accumulator() = Value(complete);
  1023. return {};
  1024. }
  1025. ThrowCompletionOr<void> IteratorResultValue::execute_impl(Bytecode::Interpreter& interpreter) const
  1026. {
  1027. auto& vm = interpreter.vm();
  1028. auto iterator_result = TRY(interpreter.accumulator().to_object(vm));
  1029. interpreter.accumulator() = TRY(iterator_value(vm, iterator_result));
  1030. return {};
  1031. }
  1032. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const
  1033. {
  1034. interpreter.accumulator() = TRY(new_class(interpreter.vm(), interpreter.accumulator(), m_class_expression, m_lhs_name));
  1035. return {};
  1036. }
  1037. // 13.5.3.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-typeof-operator-runtime-semantics-evaluation
  1038. ThrowCompletionOr<void> TypeofVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  1039. {
  1040. auto& vm = interpreter.vm();
  1041. interpreter.accumulator() = TRY(typeof_variable(vm, interpreter.current_executable().get_identifier(m_identifier)));
  1042. return {};
  1043. }
  1044. ThrowCompletionOr<void> TypeofLocal::execute_impl(Bytecode::Interpreter& interpreter) const
  1045. {
  1046. auto& vm = interpreter.vm();
  1047. auto const& value = vm.running_execution_context().local_variables[m_index];
  1048. interpreter.accumulator() = PrimitiveString::create(vm, value.typeof());
  1049. return {};
  1050. }
  1051. ThrowCompletionOr<void> ToNumeric::execute_impl(Bytecode::Interpreter& interpreter) const
  1052. {
  1053. interpreter.accumulator() = TRY(interpreter.accumulator().to_numeric(interpreter.vm()));
  1054. return {};
  1055. }
  1056. ThrowCompletionOr<void> BlockDeclarationInstantiation::execute_impl(Bytecode::Interpreter& interpreter) const
  1057. {
  1058. auto& vm = interpreter.vm();
  1059. auto old_environment = vm.running_execution_context().lexical_environment;
  1060. interpreter.saved_lexical_environment_stack().append(old_environment);
  1061. vm.running_execution_context().lexical_environment = new_declarative_environment(*old_environment);
  1062. m_scope_node.block_declaration_instantiation(vm, vm.running_execution_context().lexical_environment);
  1063. return {};
  1064. }
  1065. DeprecatedString Load::to_deprecated_string_impl(Bytecode::Executable const&) const
  1066. {
  1067. return DeprecatedString::formatted("Load {}", m_src);
  1068. }
  1069. DeprecatedString LoadImmediate::to_deprecated_string_impl(Bytecode::Executable const&) const
  1070. {
  1071. return DeprecatedString::formatted("LoadImmediate {}", m_value);
  1072. }
  1073. DeprecatedString Store::to_deprecated_string_impl(Bytecode::Executable const&) const
  1074. {
  1075. return DeprecatedString::formatted("Store {}", m_dst);
  1076. }
  1077. DeprecatedString NewBigInt::to_deprecated_string_impl(Bytecode::Executable const&) const
  1078. {
  1079. return DeprecatedString::formatted("NewBigInt \"{}\"", m_bigint.to_base_deprecated(10));
  1080. }
  1081. DeprecatedString NewArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1082. {
  1083. StringBuilder builder;
  1084. builder.append("NewArray"sv);
  1085. if (m_element_count != 0) {
  1086. builder.appendff(" [{}-{}]", m_elements[0], m_elements[1]);
  1087. }
  1088. return builder.to_deprecated_string();
  1089. }
  1090. DeprecatedString Append::to_deprecated_string_impl(Bytecode::Executable const&) const
  1091. {
  1092. if (m_is_spread)
  1093. return DeprecatedString::formatted("Append lhs: **{}", m_lhs);
  1094. return DeprecatedString::formatted("Append lhs: {}", m_lhs);
  1095. }
  1096. DeprecatedString IteratorToArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1097. {
  1098. return "IteratorToArray";
  1099. }
  1100. DeprecatedString NewString::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1101. {
  1102. return DeprecatedString::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  1103. }
  1104. DeprecatedString NewObject::to_deprecated_string_impl(Bytecode::Executable const&) const
  1105. {
  1106. return "NewObject";
  1107. }
  1108. DeprecatedString NewRegExp::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1109. {
  1110. return DeprecatedString::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  1111. }
  1112. DeprecatedString CopyObjectExcludingProperties::to_deprecated_string_impl(Bytecode::Executable const&) const
  1113. {
  1114. StringBuilder builder;
  1115. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  1116. if (m_excluded_names_count != 0) {
  1117. builder.append(" excluding:["sv);
  1118. builder.join(", "sv, ReadonlySpan<Register>(m_excluded_names, m_excluded_names_count));
  1119. builder.append(']');
  1120. }
  1121. return builder.to_deprecated_string();
  1122. }
  1123. DeprecatedString ConcatString::to_deprecated_string_impl(Bytecode::Executable const&) const
  1124. {
  1125. return DeprecatedString::formatted("ConcatString {}", m_lhs);
  1126. }
  1127. DeprecatedString GetCalleeAndThisFromEnvironment::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1128. {
  1129. return DeprecatedString::formatted("GetCalleeAndThisFromEnvironment {} -> callee: {}, this:{} ", executable.identifier_table->get(m_identifier), m_callee_reg, m_this_reg);
  1130. }
  1131. DeprecatedString GetVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1132. {
  1133. return DeprecatedString::formatted("GetVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1134. }
  1135. DeprecatedString GetGlobal::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1136. {
  1137. return DeprecatedString::formatted("GetGlobal {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1138. }
  1139. DeprecatedString GetLocal::to_deprecated_string_impl(Bytecode::Executable const&) const
  1140. {
  1141. return DeprecatedString::formatted("GetLocal {}", m_index);
  1142. }
  1143. DeprecatedString DeleteVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1144. {
  1145. return DeprecatedString::formatted("DeleteVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1146. }
  1147. DeprecatedString CreateLexicalEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const
  1148. {
  1149. return "CreateLexicalEnvironment"sv;
  1150. }
  1151. DeprecatedString CreateVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1152. {
  1153. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1154. return DeprecatedString::formatted("CreateVariable env:{} immutable:{} global:{} {} ({})", mode_string, m_is_immutable, m_is_global, m_identifier, executable.identifier_table->get(m_identifier));
  1155. }
  1156. DeprecatedString EnterObjectEnvironment::to_deprecated_string_impl(Executable const&) const
  1157. {
  1158. return DeprecatedString::formatted("EnterObjectEnvironment");
  1159. }
  1160. DeprecatedString SetVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1161. {
  1162. auto initialization_mode_name = m_initialization_mode == InitializationMode::Initialize ? "Initialize" : "Set";
  1163. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1164. return DeprecatedString::formatted("SetVariable env:{} init:{} {} ({})", mode_string, initialization_mode_name, m_identifier, executable.identifier_table->get(m_identifier));
  1165. }
  1166. DeprecatedString SetLocal::to_deprecated_string_impl(Bytecode::Executable const&) const
  1167. {
  1168. return DeprecatedString::formatted("SetLocal {}", m_index);
  1169. }
  1170. static StringView property_kind_to_string(PropertyKind kind)
  1171. {
  1172. switch (kind) {
  1173. case PropertyKind::Getter:
  1174. return "getter"sv;
  1175. case PropertyKind::Setter:
  1176. return "setter"sv;
  1177. case PropertyKind::KeyValue:
  1178. return "key-value"sv;
  1179. case PropertyKind::DirectKeyValue:
  1180. return "direct-key-value"sv;
  1181. case PropertyKind::Spread:
  1182. return "spread"sv;
  1183. case PropertyKind::ProtoSetter:
  1184. return "proto-setter"sv;
  1185. }
  1186. VERIFY_NOT_REACHED();
  1187. }
  1188. DeprecatedString PutById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1189. {
  1190. auto kind = property_kind_to_string(m_kind);
  1191. return DeprecatedString::formatted("PutById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  1192. }
  1193. DeprecatedString PutByIdWithThis::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1194. {
  1195. auto kind = property_kind_to_string(m_kind);
  1196. return DeprecatedString::formatted("PutByIdWithThis kind:{} base:{}, property:{} ({}) this_value:{}", kind, m_base, m_property, executable.identifier_table->get(m_property), m_this_value);
  1197. }
  1198. DeprecatedString PutPrivateById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1199. {
  1200. auto kind = property_kind_to_string(m_kind);
  1201. return DeprecatedString::formatted("PutPrivateById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  1202. }
  1203. DeprecatedString GetById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1204. {
  1205. return DeprecatedString::formatted("GetById {} ({})", m_property, executable.identifier_table->get(m_property));
  1206. }
  1207. DeprecatedString GetByIdWithThis::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1208. {
  1209. return DeprecatedString::formatted("GetByIdWithThis {} ({}) this_value:{}", m_property, executable.identifier_table->get(m_property), m_this_value);
  1210. }
  1211. DeprecatedString GetPrivateById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1212. {
  1213. return DeprecatedString::formatted("GetPrivateById {} ({})", m_property, executable.identifier_table->get(m_property));
  1214. }
  1215. DeprecatedString HasPrivateId::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1216. {
  1217. return DeprecatedString::formatted("HasPrivateId {} ({})", m_property, executable.identifier_table->get(m_property));
  1218. }
  1219. DeprecatedString DeleteById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1220. {
  1221. return DeprecatedString::formatted("DeleteById {} ({})", m_property, executable.identifier_table->get(m_property));
  1222. }
  1223. DeprecatedString DeleteByIdWithThis::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1224. {
  1225. return DeprecatedString::formatted("DeleteByIdWithThis {} ({}) this_value:{}", m_property, executable.identifier_table->get(m_property), m_this_value);
  1226. }
  1227. DeprecatedString Jump::to_deprecated_string_impl(Bytecode::Executable const&) const
  1228. {
  1229. if (m_true_target.has_value())
  1230. return DeprecatedString::formatted("Jump {}", *m_true_target);
  1231. return DeprecatedString::formatted("Jump <empty>");
  1232. }
  1233. DeprecatedString JumpConditional::to_deprecated_string_impl(Bytecode::Executable const&) const
  1234. {
  1235. auto true_string = m_true_target.has_value() ? DeprecatedString::formatted("{}", *m_true_target) : "<empty>";
  1236. auto false_string = m_false_target.has_value() ? DeprecatedString::formatted("{}", *m_false_target) : "<empty>";
  1237. return DeprecatedString::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  1238. }
  1239. DeprecatedString JumpNullish::to_deprecated_string_impl(Bytecode::Executable const&) const
  1240. {
  1241. auto true_string = m_true_target.has_value() ? DeprecatedString::formatted("{}", *m_true_target) : "<empty>";
  1242. auto false_string = m_false_target.has_value() ? DeprecatedString::formatted("{}", *m_false_target) : "<empty>";
  1243. return DeprecatedString::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  1244. }
  1245. DeprecatedString JumpUndefined::to_deprecated_string_impl(Bytecode::Executable const&) const
  1246. {
  1247. auto true_string = m_true_target.has_value() ? DeprecatedString::formatted("{}", *m_true_target) : "<empty>";
  1248. auto false_string = m_false_target.has_value() ? DeprecatedString::formatted("{}", *m_false_target) : "<empty>";
  1249. return DeprecatedString::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  1250. }
  1251. static StringView call_type_to_string(CallType type)
  1252. {
  1253. switch (type) {
  1254. case CallType::Call:
  1255. return ""sv;
  1256. case CallType::Construct:
  1257. return " (Construct)"sv;
  1258. case CallType::DirectEval:
  1259. return " (DirectEval)"sv;
  1260. }
  1261. VERIFY_NOT_REACHED();
  1262. }
  1263. DeprecatedString Call::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1264. {
  1265. auto type = call_type_to_string(m_type);
  1266. if (m_expression_string.has_value())
  1267. return DeprecatedString::formatted("Call{} callee:{}, this:{}, first_arg:{} ({})", type, m_callee, m_this_value, m_first_argument, executable.get_string(m_expression_string.value()));
  1268. return DeprecatedString::formatted("Call{} callee:{}, this:{}, first_arg:{}", type, m_callee, m_first_argument, m_this_value);
  1269. }
  1270. DeprecatedString CallWithArgumentArray::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1271. {
  1272. auto type = call_type_to_string(m_type);
  1273. if (m_expression_string.has_value())
  1274. return DeprecatedString::formatted("CallWithArgumentArray{} callee:{}, this:{}, arguments:[...acc] ({})", type, m_callee, m_this_value, executable.get_string(m_expression_string.value()));
  1275. return DeprecatedString::formatted("CallWithArgumentArray{} callee:{}, this:{}, arguments:[...acc]", type, m_callee, m_this_value);
  1276. }
  1277. DeprecatedString SuperCallWithArgumentArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1278. {
  1279. return "SuperCallWithArgumentArray arguments:[...acc]"sv;
  1280. }
  1281. DeprecatedString NewFunction::to_deprecated_string_impl(Bytecode::Executable const&) const
  1282. {
  1283. StringBuilder builder;
  1284. builder.append("NewFunction"sv);
  1285. if (m_function_node.has_name())
  1286. builder.appendff(" name:{}"sv, m_function_node.name());
  1287. if (m_lhs_name.has_value())
  1288. builder.appendff(" lhs_name:{}"sv, m_lhs_name.value());
  1289. if (m_home_object.has_value())
  1290. builder.appendff(" home_object:{}"sv, m_home_object.value());
  1291. return builder.to_deprecated_string();
  1292. }
  1293. DeprecatedString NewClass::to_deprecated_string_impl(Bytecode::Executable const&) const
  1294. {
  1295. StringBuilder builder;
  1296. auto name = m_class_expression.name();
  1297. builder.appendff("NewClass '{}'"sv, name.is_null() ? ""sv : name);
  1298. if (m_lhs_name.has_value())
  1299. builder.appendff(" lhs_name:{}"sv, m_lhs_name.value());
  1300. return builder.to_deprecated_string();
  1301. }
  1302. DeprecatedString Return::to_deprecated_string_impl(Bytecode::Executable const&) const
  1303. {
  1304. return "Return";
  1305. }
  1306. DeprecatedString Increment::to_deprecated_string_impl(Bytecode::Executable const&) const
  1307. {
  1308. return "Increment";
  1309. }
  1310. DeprecatedString Decrement::to_deprecated_string_impl(Bytecode::Executable const&) const
  1311. {
  1312. return "Decrement";
  1313. }
  1314. DeprecatedString Throw::to_deprecated_string_impl(Bytecode::Executable const&) const
  1315. {
  1316. return "Throw";
  1317. }
  1318. DeprecatedString ThrowIfNotObject::to_deprecated_string_impl(Bytecode::Executable const&) const
  1319. {
  1320. return "ThrowIfNotObject";
  1321. }
  1322. DeprecatedString ThrowIfNullish::to_deprecated_string_impl(Bytecode::Executable const&) const
  1323. {
  1324. return "ThrowIfNullish";
  1325. }
  1326. DeprecatedString EnterUnwindContext::to_deprecated_string_impl(Bytecode::Executable const&) const
  1327. {
  1328. return DeprecatedString::formatted("EnterUnwindContext entry:{}", m_entry_point);
  1329. }
  1330. DeprecatedString ScheduleJump::to_deprecated_string_impl(Bytecode::Executable const&) const
  1331. {
  1332. return DeprecatedString::formatted("ScheduleJump {}", m_target);
  1333. }
  1334. DeprecatedString LeaveLexicalEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const
  1335. {
  1336. return "LeaveLexicalEnvironment"sv;
  1337. }
  1338. DeprecatedString LeaveUnwindContext::to_deprecated_string_impl(Bytecode::Executable const&) const
  1339. {
  1340. return "LeaveUnwindContext";
  1341. }
  1342. DeprecatedString ContinuePendingUnwind::to_deprecated_string_impl(Bytecode::Executable const&) const
  1343. {
  1344. return DeprecatedString::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  1345. }
  1346. DeprecatedString Yield::to_deprecated_string_impl(Bytecode::Executable const&) const
  1347. {
  1348. if (m_continuation_label.has_value())
  1349. return DeprecatedString::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  1350. return DeprecatedString::formatted("Yield return");
  1351. }
  1352. DeprecatedString Await::to_deprecated_string_impl(Bytecode::Executable const&) const
  1353. {
  1354. return DeprecatedString::formatted("Await continuation:@{}", m_continuation_label.block().name());
  1355. }
  1356. DeprecatedString GetByValue::to_deprecated_string_impl(Bytecode::Executable const&) const
  1357. {
  1358. return DeprecatedString::formatted("GetByValue base:{}", m_base);
  1359. }
  1360. DeprecatedString GetByValueWithThis::to_deprecated_string_impl(Bytecode::Executable const&) const
  1361. {
  1362. return DeprecatedString::formatted("GetByValueWithThis base:{} this_value:{}", m_base, m_this_value);
  1363. }
  1364. DeprecatedString PutByValue::to_deprecated_string_impl(Bytecode::Executable const&) const
  1365. {
  1366. auto kind = property_kind_to_string(m_kind);
  1367. return DeprecatedString::formatted("PutByValue kind:{} base:{}, property:{}", kind, m_base, m_property);
  1368. }
  1369. DeprecatedString PutByValueWithThis::to_deprecated_string_impl(Bytecode::Executable const&) const
  1370. {
  1371. auto kind = property_kind_to_string(m_kind);
  1372. return DeprecatedString::formatted("PutByValueWithThis kind:{} base:{}, property:{} this_value:{}", kind, m_base, m_property, m_this_value);
  1373. }
  1374. DeprecatedString DeleteByValue::to_deprecated_string_impl(Bytecode::Executable const&) const
  1375. {
  1376. return DeprecatedString::formatted("DeleteByValue base:{}", m_base);
  1377. }
  1378. DeprecatedString DeleteByValueWithThis::to_deprecated_string_impl(Bytecode::Executable const&) const
  1379. {
  1380. return DeprecatedString::formatted("DeleteByValueWithThis base:{} this_value:{}", m_base, m_this_value);
  1381. }
  1382. DeprecatedString GetIterator::to_deprecated_string_impl(Executable const&) const
  1383. {
  1384. auto hint = m_hint == IteratorHint::Sync ? "sync" : "async";
  1385. return DeprecatedString::formatted("GetIterator hint:{}", hint);
  1386. }
  1387. DeprecatedString GetMethod::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1388. {
  1389. return DeprecatedString::formatted("GetMethod {} ({})", m_property, executable.identifier_table->get(m_property));
  1390. }
  1391. DeprecatedString GetObjectPropertyIterator::to_deprecated_string_impl(Bytecode::Executable const&) const
  1392. {
  1393. return "GetObjectPropertyIterator";
  1394. }
  1395. DeprecatedString IteratorClose::to_deprecated_string_impl(Bytecode::Executable const&) const
  1396. {
  1397. if (!m_completion_value.has_value())
  1398. return DeprecatedString::formatted("IteratorClose completion_type={} completion_value=<empty>", to_underlying(m_completion_type));
  1399. auto completion_value_string = m_completion_value->to_string_without_side_effects();
  1400. return DeprecatedString::formatted("IteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string);
  1401. }
  1402. DeprecatedString AsyncIteratorClose::to_deprecated_string_impl(Bytecode::Executable const&) const
  1403. {
  1404. if (!m_completion_value.has_value())
  1405. return DeprecatedString::formatted("AsyncIteratorClose completion_type={} completion_value=<empty>", to_underlying(m_completion_type));
  1406. auto completion_value_string = m_completion_value->to_string_without_side_effects();
  1407. return DeprecatedString::formatted("AsyncIteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string);
  1408. }
  1409. DeprecatedString IteratorNext::to_deprecated_string_impl(Executable const&) const
  1410. {
  1411. return "IteratorNext";
  1412. }
  1413. DeprecatedString IteratorResultDone::to_deprecated_string_impl(Executable const&) const
  1414. {
  1415. return "IteratorResultDone";
  1416. }
  1417. DeprecatedString IteratorResultValue::to_deprecated_string_impl(Executable const&) const
  1418. {
  1419. return "IteratorResultValue";
  1420. }
  1421. DeprecatedString ResolveThisBinding::to_deprecated_string_impl(Bytecode::Executable const&) const
  1422. {
  1423. return "ResolveThisBinding"sv;
  1424. }
  1425. DeprecatedString ResolveSuperBase::to_deprecated_string_impl(Bytecode::Executable const&) const
  1426. {
  1427. return "ResolveSuperBase"sv;
  1428. }
  1429. DeprecatedString GetNewTarget::to_deprecated_string_impl(Bytecode::Executable const&) const
  1430. {
  1431. return "GetNewTarget"sv;
  1432. }
  1433. DeprecatedString GetImportMeta::to_deprecated_string_impl(Bytecode::Executable const&) const
  1434. {
  1435. return "GetImportMeta"sv;
  1436. }
  1437. DeprecatedString TypeofVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1438. {
  1439. return DeprecatedString::formatted("TypeofVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1440. }
  1441. DeprecatedString TypeofLocal::to_deprecated_string_impl(Bytecode::Executable const&) const
  1442. {
  1443. return DeprecatedString::formatted("TypeofLocal {}", m_index);
  1444. }
  1445. DeprecatedString ToNumeric::to_deprecated_string_impl(Bytecode::Executable const&) const
  1446. {
  1447. return "ToNumeric"sv;
  1448. }
  1449. DeprecatedString BlockDeclarationInstantiation::to_deprecated_string_impl(Bytecode::Executable const&) const
  1450. {
  1451. return "BlockDeclarationInstantiation"sv;
  1452. }
  1453. DeprecatedString ImportCall::to_deprecated_string_impl(Bytecode::Executable const&) const
  1454. {
  1455. return DeprecatedString::formatted("ImportCall specifier:{} options:{}"sv, m_specifier, m_options);
  1456. }
  1457. DeprecatedString Catch::to_deprecated_string_impl(Bytecode::Executable const&) const
  1458. {
  1459. return "Catch"sv;
  1460. }
  1461. }