Interpreter.cpp 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  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. DeprecatedString Instruction::to_deprecated_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_deprecated_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. DeprecatedString OpTitleCase::to_deprecated_string_impl(Bytecode::Executable const&) const \
  476. { \
  477. return DeprecatedString::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. DeprecatedString OpTitleCase::to_deprecated_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. DeprecatedString New##ErrorName::to_deprecated_string_impl(Bytecode::Executable const& executable) const \
  571. { \
  572. return DeprecatedString::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. auto iterator = TRY(get_iterator(vm, interpreter.accumulator(), m_hint));
  1032. interpreter.accumulator() = iterator_to_object(vm, iterator);
  1033. return {};
  1034. }
  1035. ThrowCompletionOr<void> GetMethod::execute_impl(Bytecode::Interpreter& interpreter) const
  1036. {
  1037. auto& vm = interpreter.vm();
  1038. auto identifier = interpreter.current_executable().get_identifier(m_property);
  1039. auto method = TRY(interpreter.accumulator().get_method(vm, identifier));
  1040. interpreter.accumulator() = method ?: js_undefined();
  1041. return {};
  1042. }
  1043. ThrowCompletionOr<void> GetObjectPropertyIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  1044. {
  1045. interpreter.accumulator() = TRY(get_object_property_iterator(interpreter.vm(), interpreter.accumulator()));
  1046. return {};
  1047. }
  1048. ThrowCompletionOr<void> IteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1049. {
  1050. auto& vm = interpreter.vm();
  1051. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  1052. auto iterator = object_to_iterator(vm, iterator_object);
  1053. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1054. TRY(iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} }));
  1055. return {};
  1056. }
  1057. ThrowCompletionOr<void> AsyncIteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1058. {
  1059. auto& vm = interpreter.vm();
  1060. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  1061. auto iterator = object_to_iterator(vm, iterator_object);
  1062. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1063. TRY(async_iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} }));
  1064. return {};
  1065. }
  1066. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  1067. {
  1068. auto& vm = interpreter.vm();
  1069. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  1070. auto iterator = object_to_iterator(vm, iterator_object);
  1071. interpreter.accumulator() = TRY(iterator_next(vm, iterator));
  1072. return {};
  1073. }
  1074. ThrowCompletionOr<void> IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const
  1075. {
  1076. auto& vm = interpreter.vm();
  1077. auto iterator_result = TRY(interpreter.accumulator().to_object(vm));
  1078. auto complete = TRY(iterator_complete(vm, iterator_result));
  1079. interpreter.accumulator() = Value(complete);
  1080. return {};
  1081. }
  1082. ThrowCompletionOr<void> IteratorResultValue::execute_impl(Bytecode::Interpreter& interpreter) const
  1083. {
  1084. auto& vm = interpreter.vm();
  1085. auto iterator_result = TRY(interpreter.accumulator().to_object(vm));
  1086. interpreter.accumulator() = TRY(iterator_value(vm, iterator_result));
  1087. return {};
  1088. }
  1089. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const
  1090. {
  1091. interpreter.accumulator() = TRY(new_class(interpreter.vm(), interpreter.accumulator(), m_class_expression, m_lhs_name));
  1092. return {};
  1093. }
  1094. // 13.5.3.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-typeof-operator-runtime-semantics-evaluation
  1095. ThrowCompletionOr<void> TypeofVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  1096. {
  1097. auto& vm = interpreter.vm();
  1098. interpreter.accumulator() = TRY(typeof_variable(vm, interpreter.current_executable().get_identifier(m_identifier)));
  1099. return {};
  1100. }
  1101. ThrowCompletionOr<void> TypeofLocal::execute_impl(Bytecode::Interpreter& interpreter) const
  1102. {
  1103. auto& vm = interpreter.vm();
  1104. auto const& value = vm.running_execution_context().local(m_index);
  1105. interpreter.accumulator() = PrimitiveString::create(vm, value.typeof());
  1106. return {};
  1107. }
  1108. ThrowCompletionOr<void> ToNumeric::execute_impl(Bytecode::Interpreter& interpreter) const
  1109. {
  1110. interpreter.accumulator() = TRY(interpreter.accumulator().to_numeric(interpreter.vm()));
  1111. return {};
  1112. }
  1113. ThrowCompletionOr<void> BlockDeclarationInstantiation::execute_impl(Bytecode::Interpreter& interpreter) const
  1114. {
  1115. auto& vm = interpreter.vm();
  1116. auto old_environment = vm.running_execution_context().lexical_environment;
  1117. interpreter.saved_lexical_environment_stack().append(old_environment);
  1118. vm.running_execution_context().lexical_environment = new_declarative_environment(*old_environment);
  1119. m_scope_node.block_declaration_instantiation(vm, vm.running_execution_context().lexical_environment);
  1120. return {};
  1121. }
  1122. DeprecatedString Load::to_deprecated_string_impl(Bytecode::Executable const&) const
  1123. {
  1124. return DeprecatedString::formatted("Load {}", m_src);
  1125. }
  1126. DeprecatedString LoadImmediate::to_deprecated_string_impl(Bytecode::Executable const&) const
  1127. {
  1128. return DeprecatedString::formatted("LoadImmediate {}", m_value);
  1129. }
  1130. DeprecatedString Store::to_deprecated_string_impl(Bytecode::Executable const&) const
  1131. {
  1132. return DeprecatedString::formatted("Store {}", m_dst);
  1133. }
  1134. DeprecatedString NewBigInt::to_deprecated_string_impl(Bytecode::Executable const&) const
  1135. {
  1136. return DeprecatedString::formatted("NewBigInt \"{}\"", m_bigint.to_base_deprecated(10));
  1137. }
  1138. DeprecatedString NewArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1139. {
  1140. StringBuilder builder;
  1141. builder.append("NewArray"sv);
  1142. if (m_element_count != 0) {
  1143. builder.appendff(" [{}-{}]", m_elements[0], m_elements[1]);
  1144. }
  1145. return builder.to_deprecated_string();
  1146. }
  1147. DeprecatedString NewPrimitiveArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1148. {
  1149. return DeprecatedString::formatted("NewPrimitiveArray {}"sv, m_values.span());
  1150. }
  1151. DeprecatedString Append::to_deprecated_string_impl(Bytecode::Executable const&) const
  1152. {
  1153. if (m_is_spread)
  1154. return DeprecatedString::formatted("Append lhs: **{}", m_lhs);
  1155. return DeprecatedString::formatted("Append lhs: {}", m_lhs);
  1156. }
  1157. DeprecatedString IteratorToArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1158. {
  1159. return "IteratorToArray";
  1160. }
  1161. DeprecatedString NewString::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1162. {
  1163. return DeprecatedString::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  1164. }
  1165. DeprecatedString NewObject::to_deprecated_string_impl(Bytecode::Executable const&) const
  1166. {
  1167. return "NewObject";
  1168. }
  1169. DeprecatedString NewRegExp::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1170. {
  1171. return DeprecatedString::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  1172. }
  1173. DeprecatedString CopyObjectExcludingProperties::to_deprecated_string_impl(Bytecode::Executable const&) const
  1174. {
  1175. StringBuilder builder;
  1176. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  1177. if (m_excluded_names_count != 0) {
  1178. builder.append(" excluding:["sv);
  1179. builder.join(", "sv, ReadonlySpan<Register>(m_excluded_names, m_excluded_names_count));
  1180. builder.append(']');
  1181. }
  1182. return builder.to_deprecated_string();
  1183. }
  1184. DeprecatedString ConcatString::to_deprecated_string_impl(Bytecode::Executable const&) const
  1185. {
  1186. return DeprecatedString::formatted("ConcatString {}", m_lhs);
  1187. }
  1188. DeprecatedString GetCalleeAndThisFromEnvironment::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1189. {
  1190. return DeprecatedString::formatted("GetCalleeAndThisFromEnvironment {} -> callee: {}, this:{} ", executable.identifier_table->get(m_identifier), m_callee_reg, m_this_reg);
  1191. }
  1192. DeprecatedString GetVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1193. {
  1194. return DeprecatedString::formatted("GetVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1195. }
  1196. DeprecatedString GetGlobal::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1197. {
  1198. return DeprecatedString::formatted("GetGlobal {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1199. }
  1200. DeprecatedString GetLocal::to_deprecated_string_impl(Bytecode::Executable const&) const
  1201. {
  1202. return DeprecatedString::formatted("GetLocal {}", m_index);
  1203. }
  1204. DeprecatedString DeleteVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1205. {
  1206. return DeprecatedString::formatted("DeleteVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1207. }
  1208. DeprecatedString CreateLexicalEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const
  1209. {
  1210. return "CreateLexicalEnvironment"sv;
  1211. }
  1212. DeprecatedString CreateVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1213. {
  1214. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1215. return DeprecatedString::formatted("CreateVariable env:{} immutable:{} global:{} {} ({})", mode_string, m_is_immutable, m_is_global, m_identifier, executable.identifier_table->get(m_identifier));
  1216. }
  1217. DeprecatedString EnterObjectEnvironment::to_deprecated_string_impl(Executable const&) const
  1218. {
  1219. return DeprecatedString::formatted("EnterObjectEnvironment");
  1220. }
  1221. DeprecatedString SetVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1222. {
  1223. auto initialization_mode_name = m_initialization_mode == InitializationMode::Initialize ? "Initialize" : "Set";
  1224. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1225. return DeprecatedString::formatted("SetVariable env:{} init:{} {} ({})", mode_string, initialization_mode_name, m_identifier, executable.identifier_table->get(m_identifier));
  1226. }
  1227. DeprecatedString SetLocal::to_deprecated_string_impl(Bytecode::Executable const&) const
  1228. {
  1229. return DeprecatedString::formatted("SetLocal {}", m_index);
  1230. }
  1231. static StringView property_kind_to_string(PropertyKind kind)
  1232. {
  1233. switch (kind) {
  1234. case PropertyKind::Getter:
  1235. return "getter"sv;
  1236. case PropertyKind::Setter:
  1237. return "setter"sv;
  1238. case PropertyKind::KeyValue:
  1239. return "key-value"sv;
  1240. case PropertyKind::DirectKeyValue:
  1241. return "direct-key-value"sv;
  1242. case PropertyKind::Spread:
  1243. return "spread"sv;
  1244. case PropertyKind::ProtoSetter:
  1245. return "proto-setter"sv;
  1246. }
  1247. VERIFY_NOT_REACHED();
  1248. }
  1249. DeprecatedString PutById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1250. {
  1251. auto kind = property_kind_to_string(m_kind);
  1252. return DeprecatedString::formatted("PutById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  1253. }
  1254. DeprecatedString PutByIdWithThis::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1255. {
  1256. auto kind = property_kind_to_string(m_kind);
  1257. return DeprecatedString::formatted("PutByIdWithThis kind:{} base:{}, property:{} ({}) this_value:{}", kind, m_base, m_property, executable.identifier_table->get(m_property), m_this_value);
  1258. }
  1259. DeprecatedString PutPrivateById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1260. {
  1261. auto kind = property_kind_to_string(m_kind);
  1262. return DeprecatedString::formatted("PutPrivateById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  1263. }
  1264. DeprecatedString GetById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1265. {
  1266. return DeprecatedString::formatted("GetById {} ({})", m_property, executable.identifier_table->get(m_property));
  1267. }
  1268. DeprecatedString GetByIdWithThis::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1269. {
  1270. return DeprecatedString::formatted("GetByIdWithThis {} ({}) this_value:{}", m_property, executable.identifier_table->get(m_property), m_this_value);
  1271. }
  1272. DeprecatedString GetPrivateById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1273. {
  1274. return DeprecatedString::formatted("GetPrivateById {} ({})", m_property, executable.identifier_table->get(m_property));
  1275. }
  1276. DeprecatedString HasPrivateId::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1277. {
  1278. return DeprecatedString::formatted("HasPrivateId {} ({})", m_property, executable.identifier_table->get(m_property));
  1279. }
  1280. DeprecatedString DeleteById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1281. {
  1282. return DeprecatedString::formatted("DeleteById {} ({})", m_property, executable.identifier_table->get(m_property));
  1283. }
  1284. DeprecatedString DeleteByIdWithThis::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1285. {
  1286. return DeprecatedString::formatted("DeleteByIdWithThis {} ({}) this_value:{}", m_property, executable.identifier_table->get(m_property), m_this_value);
  1287. }
  1288. DeprecatedString Jump::to_deprecated_string_impl(Bytecode::Executable const&) const
  1289. {
  1290. if (m_true_target.has_value())
  1291. return DeprecatedString::formatted("Jump {}", *m_true_target);
  1292. return DeprecatedString::formatted("Jump <empty>");
  1293. }
  1294. DeprecatedString JumpConditional::to_deprecated_string_impl(Bytecode::Executable const&) const
  1295. {
  1296. auto true_string = m_true_target.has_value() ? DeprecatedString::formatted("{}", *m_true_target) : "<empty>";
  1297. auto false_string = m_false_target.has_value() ? DeprecatedString::formatted("{}", *m_false_target) : "<empty>";
  1298. return DeprecatedString::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  1299. }
  1300. DeprecatedString JumpNullish::to_deprecated_string_impl(Bytecode::Executable const&) const
  1301. {
  1302. auto true_string = m_true_target.has_value() ? DeprecatedString::formatted("{}", *m_true_target) : "<empty>";
  1303. auto false_string = m_false_target.has_value() ? DeprecatedString::formatted("{}", *m_false_target) : "<empty>";
  1304. return DeprecatedString::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  1305. }
  1306. DeprecatedString JumpUndefined::to_deprecated_string_impl(Bytecode::Executable const&) const
  1307. {
  1308. auto true_string = m_true_target.has_value() ? DeprecatedString::formatted("{}", *m_true_target) : "<empty>";
  1309. auto false_string = m_false_target.has_value() ? DeprecatedString::formatted("{}", *m_false_target) : "<empty>";
  1310. return DeprecatedString::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  1311. }
  1312. static StringView call_type_to_string(CallType type)
  1313. {
  1314. switch (type) {
  1315. case CallType::Call:
  1316. return ""sv;
  1317. case CallType::Construct:
  1318. return " (Construct)"sv;
  1319. case CallType::DirectEval:
  1320. return " (DirectEval)"sv;
  1321. }
  1322. VERIFY_NOT_REACHED();
  1323. }
  1324. DeprecatedString Call::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1325. {
  1326. auto type = call_type_to_string(m_type);
  1327. if (m_builtin.has_value())
  1328. return DeprecatedString::formatted("Call{} callee:{}, this:{}, first_arg:{} (builtin {})", type, m_callee, m_this_value, m_first_argument, m_builtin.value());
  1329. if (m_expression_string.has_value())
  1330. return DeprecatedString::formatted("Call{} callee:{}, this:{}, first_arg:{} ({})", type, m_callee, m_this_value, m_first_argument, executable.get_string(m_expression_string.value()));
  1331. return DeprecatedString::formatted("Call{} callee:{}, this:{}, first_arg:{}", type, m_callee, m_first_argument, m_this_value);
  1332. }
  1333. DeprecatedString CallWithArgumentArray::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1334. {
  1335. auto type = call_type_to_string(m_type);
  1336. if (m_expression_string.has_value())
  1337. return DeprecatedString::formatted("CallWithArgumentArray{} callee:{}, this:{}, arguments:[...acc] ({})", type, m_callee, m_this_value, executable.get_string(m_expression_string.value()));
  1338. return DeprecatedString::formatted("CallWithArgumentArray{} callee:{}, this:{}, arguments:[...acc]", type, m_callee, m_this_value);
  1339. }
  1340. DeprecatedString SuperCallWithArgumentArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1341. {
  1342. return "SuperCallWithArgumentArray arguments:[...acc]"sv;
  1343. }
  1344. DeprecatedString NewFunction::to_deprecated_string_impl(Bytecode::Executable const&) const
  1345. {
  1346. StringBuilder builder;
  1347. builder.append("NewFunction"sv);
  1348. if (m_function_node.has_name())
  1349. builder.appendff(" name:{}"sv, m_function_node.name());
  1350. if (m_lhs_name.has_value())
  1351. builder.appendff(" lhs_name:{}"sv, m_lhs_name.value());
  1352. if (m_home_object.has_value())
  1353. builder.appendff(" home_object:{}"sv, m_home_object.value());
  1354. return builder.to_deprecated_string();
  1355. }
  1356. DeprecatedString NewClass::to_deprecated_string_impl(Bytecode::Executable const&) const
  1357. {
  1358. StringBuilder builder;
  1359. auto name = m_class_expression.name();
  1360. builder.appendff("NewClass '{}'"sv, name.is_null() ? ""sv : name);
  1361. if (m_lhs_name.has_value())
  1362. builder.appendff(" lhs_name:{}"sv, m_lhs_name.value());
  1363. return builder.to_deprecated_string();
  1364. }
  1365. DeprecatedString Return::to_deprecated_string_impl(Bytecode::Executable const&) const
  1366. {
  1367. return "Return";
  1368. }
  1369. DeprecatedString Increment::to_deprecated_string_impl(Bytecode::Executable const&) const
  1370. {
  1371. return "Increment";
  1372. }
  1373. DeprecatedString Decrement::to_deprecated_string_impl(Bytecode::Executable const&) const
  1374. {
  1375. return "Decrement";
  1376. }
  1377. DeprecatedString Throw::to_deprecated_string_impl(Bytecode::Executable const&) const
  1378. {
  1379. return "Throw";
  1380. }
  1381. DeprecatedString ThrowIfNotObject::to_deprecated_string_impl(Bytecode::Executable const&) const
  1382. {
  1383. return "ThrowIfNotObject";
  1384. }
  1385. DeprecatedString ThrowIfNullish::to_deprecated_string_impl(Bytecode::Executable const&) const
  1386. {
  1387. return "ThrowIfNullish";
  1388. }
  1389. DeprecatedString EnterUnwindContext::to_deprecated_string_impl(Bytecode::Executable const&) const
  1390. {
  1391. return DeprecatedString::formatted("EnterUnwindContext entry:{}", m_entry_point);
  1392. }
  1393. DeprecatedString ScheduleJump::to_deprecated_string_impl(Bytecode::Executable const&) const
  1394. {
  1395. return DeprecatedString::formatted("ScheduleJump {}", m_target);
  1396. }
  1397. DeprecatedString LeaveLexicalEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const
  1398. {
  1399. return "LeaveLexicalEnvironment"sv;
  1400. }
  1401. DeprecatedString LeaveUnwindContext::to_deprecated_string_impl(Bytecode::Executable const&) const
  1402. {
  1403. return "LeaveUnwindContext";
  1404. }
  1405. DeprecatedString ContinuePendingUnwind::to_deprecated_string_impl(Bytecode::Executable const&) const
  1406. {
  1407. return DeprecatedString::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  1408. }
  1409. DeprecatedString Yield::to_deprecated_string_impl(Bytecode::Executable const&) const
  1410. {
  1411. if (m_continuation_label.has_value())
  1412. return DeprecatedString::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  1413. return DeprecatedString::formatted("Yield return");
  1414. }
  1415. DeprecatedString Await::to_deprecated_string_impl(Bytecode::Executable const&) const
  1416. {
  1417. return DeprecatedString::formatted("Await continuation:@{}", m_continuation_label.block().name());
  1418. }
  1419. DeprecatedString GetByValue::to_deprecated_string_impl(Bytecode::Executable const&) const
  1420. {
  1421. return DeprecatedString::formatted("GetByValue base:{}", m_base);
  1422. }
  1423. DeprecatedString GetByValueWithThis::to_deprecated_string_impl(Bytecode::Executable const&) const
  1424. {
  1425. return DeprecatedString::formatted("GetByValueWithThis base:{} this_value:{}", m_base, m_this_value);
  1426. }
  1427. DeprecatedString PutByValue::to_deprecated_string_impl(Bytecode::Executable const&) const
  1428. {
  1429. auto kind = property_kind_to_string(m_kind);
  1430. return DeprecatedString::formatted("PutByValue kind:{} base:{}, property:{}", kind, m_base, m_property);
  1431. }
  1432. DeprecatedString PutByValueWithThis::to_deprecated_string_impl(Bytecode::Executable const&) const
  1433. {
  1434. auto kind = property_kind_to_string(m_kind);
  1435. return DeprecatedString::formatted("PutByValueWithThis kind:{} base:{}, property:{} this_value:{}", kind, m_base, m_property, m_this_value);
  1436. }
  1437. DeprecatedString DeleteByValue::to_deprecated_string_impl(Bytecode::Executable const&) const
  1438. {
  1439. return DeprecatedString::formatted("DeleteByValue base:{}", m_base);
  1440. }
  1441. DeprecatedString DeleteByValueWithThis::to_deprecated_string_impl(Bytecode::Executable const&) const
  1442. {
  1443. return DeprecatedString::formatted("DeleteByValueWithThis base:{} this_value:{}", m_base, m_this_value);
  1444. }
  1445. DeprecatedString GetIterator::to_deprecated_string_impl(Executable const&) const
  1446. {
  1447. auto hint = m_hint == IteratorHint::Sync ? "sync" : "async";
  1448. return DeprecatedString::formatted("GetIterator hint:{}", hint);
  1449. }
  1450. DeprecatedString GetMethod::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1451. {
  1452. return DeprecatedString::formatted("GetMethod {} ({})", m_property, executable.identifier_table->get(m_property));
  1453. }
  1454. DeprecatedString GetObjectPropertyIterator::to_deprecated_string_impl(Bytecode::Executable const&) const
  1455. {
  1456. return "GetObjectPropertyIterator";
  1457. }
  1458. DeprecatedString IteratorClose::to_deprecated_string_impl(Bytecode::Executable const&) const
  1459. {
  1460. if (!m_completion_value.has_value())
  1461. return DeprecatedString::formatted("IteratorClose 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 DeprecatedString::formatted("IteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string);
  1464. }
  1465. DeprecatedString AsyncIteratorClose::to_deprecated_string_impl(Bytecode::Executable const&) const
  1466. {
  1467. if (!m_completion_value.has_value())
  1468. return DeprecatedString::formatted("AsyncIteratorClose completion_type={} completion_value=<empty>", to_underlying(m_completion_type));
  1469. auto completion_value_string = m_completion_value->to_string_without_side_effects();
  1470. return DeprecatedString::formatted("AsyncIteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string);
  1471. }
  1472. DeprecatedString IteratorNext::to_deprecated_string_impl(Executable const&) const
  1473. {
  1474. return "IteratorNext";
  1475. }
  1476. DeprecatedString IteratorResultDone::to_deprecated_string_impl(Executable const&) const
  1477. {
  1478. return "IteratorResultDone";
  1479. }
  1480. DeprecatedString IteratorResultValue::to_deprecated_string_impl(Executable const&) const
  1481. {
  1482. return "IteratorResultValue";
  1483. }
  1484. DeprecatedString ResolveThisBinding::to_deprecated_string_impl(Bytecode::Executable const&) const
  1485. {
  1486. return "ResolveThisBinding"sv;
  1487. }
  1488. DeprecatedString ResolveSuperBase::to_deprecated_string_impl(Bytecode::Executable const&) const
  1489. {
  1490. return "ResolveSuperBase"sv;
  1491. }
  1492. DeprecatedString GetNewTarget::to_deprecated_string_impl(Bytecode::Executable const&) const
  1493. {
  1494. return "GetNewTarget"sv;
  1495. }
  1496. DeprecatedString GetImportMeta::to_deprecated_string_impl(Bytecode::Executable const&) const
  1497. {
  1498. return "GetImportMeta"sv;
  1499. }
  1500. DeprecatedString TypeofVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1501. {
  1502. return DeprecatedString::formatted("TypeofVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1503. }
  1504. DeprecatedString TypeofLocal::to_deprecated_string_impl(Bytecode::Executable const&) const
  1505. {
  1506. return DeprecatedString::formatted("TypeofLocal {}", m_index);
  1507. }
  1508. DeprecatedString ToNumeric::to_deprecated_string_impl(Bytecode::Executable const&) const
  1509. {
  1510. return "ToNumeric"sv;
  1511. }
  1512. DeprecatedString BlockDeclarationInstantiation::to_deprecated_string_impl(Bytecode::Executable const&) const
  1513. {
  1514. return "BlockDeclarationInstantiation"sv;
  1515. }
  1516. DeprecatedString ImportCall::to_deprecated_string_impl(Bytecode::Executable const&) const
  1517. {
  1518. return DeprecatedString::formatted("ImportCall specifier:{} options:{}"sv, m_specifier, m_options);
  1519. }
  1520. DeprecatedString Catch::to_deprecated_string_impl(Bytecode::Executable const&) const
  1521. {
  1522. return "Catch"sv;
  1523. }
  1524. }