Interpreter.cpp 71 KB

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