Interpreter.cpp 71 KB

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