Interpreter.cpp 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257
  1. /*
  2. * Copyright (c) 2021-2024, 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/Runtime/AbstractOperations.h>
  18. #include <LibJS/Runtime/Array.h>
  19. #include <LibJS/Runtime/BigInt.h>
  20. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  21. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  22. #include <LibJS/Runtime/Environment.h>
  23. #include <LibJS/Runtime/FunctionEnvironment.h>
  24. #include <LibJS/Runtime/GlobalEnvironment.h>
  25. #include <LibJS/Runtime/GlobalObject.h>
  26. #include <LibJS/Runtime/Iterator.h>
  27. #include <LibJS/Runtime/MathObject.h>
  28. #include <LibJS/Runtime/NativeFunction.h>
  29. #include <LibJS/Runtime/ObjectEnvironment.h>
  30. #include <LibJS/Runtime/Realm.h>
  31. #include <LibJS/Runtime/Reference.h>
  32. #include <LibJS/Runtime/RegExpObject.h>
  33. #include <LibJS/Runtime/Value.h>
  34. #include <LibJS/Runtime/ValueInlines.h>
  35. #include <LibJS/SourceTextModule.h>
  36. namespace JS::Bytecode {
  37. bool g_dump_bytecode = false;
  38. static ByteString format_operand(StringView name, Operand operand, Bytecode::Executable const& executable)
  39. {
  40. StringBuilder builder;
  41. if (!name.is_empty())
  42. builder.appendff("\033[32m{}\033[0m:", name);
  43. switch (operand.type()) {
  44. case Operand::Type::Register:
  45. builder.appendff("\033[33mreg{}\033[0m", operand.index());
  46. break;
  47. case Operand::Type::Local:
  48. // FIXME: Show local name.
  49. builder.appendff("\033[34mloc{}\033[0m", operand.index());
  50. break;
  51. case Operand::Type::Constant: {
  52. builder.append("\033[36m"sv);
  53. auto value = executable.constants[operand.index()];
  54. if (value.is_empty())
  55. builder.append("<Empty>"sv);
  56. else if (value.is_boolean())
  57. builder.appendff("Bool({})", value.as_bool() ? "true"sv : "false"sv);
  58. else if (value.is_int32())
  59. builder.appendff("Int32({})", value.as_i32());
  60. else if (value.is_double())
  61. builder.appendff("Double({})", value.as_double());
  62. else if (value.is_string())
  63. builder.appendff("String(\"{}\")", value.as_string().utf8_string_view());
  64. else if (value.is_undefined())
  65. builder.append("Undefined"sv);
  66. else if (value.is_null())
  67. builder.append("Null"sv);
  68. else
  69. builder.appendff("Value: {}", value);
  70. builder.append("\033[0m"sv);
  71. break;
  72. }
  73. default:
  74. VERIFY_NOT_REACHED();
  75. }
  76. return builder.to_byte_string();
  77. }
  78. static ByteString format_operand_list(StringView name, ReadonlySpan<Operand> operands, Bytecode::Executable const& executable)
  79. {
  80. StringBuilder builder;
  81. if (!name.is_empty())
  82. builder.appendff(", \033[32m{}\033[0m:[", name);
  83. for (size_t i = 0; i < operands.size(); ++i) {
  84. if (i != 0)
  85. builder.append(", "sv);
  86. builder.appendff("{}", format_operand(""sv, operands[i], executable));
  87. }
  88. builder.append("]"sv);
  89. return builder.to_byte_string();
  90. }
  91. NonnullOwnPtr<CallFrame> CallFrame::create(size_t register_count)
  92. {
  93. size_t allocation_size = sizeof(CallFrame) + sizeof(Value) * register_count;
  94. auto* memory = malloc(allocation_size);
  95. VERIFY(memory);
  96. auto call_frame = adopt_own(*new (memory) CallFrame);
  97. call_frame->register_count = register_count;
  98. for (auto i = 0u; i < register_count; ++i)
  99. new (&call_frame->register_values[i]) Value();
  100. return call_frame;
  101. }
  102. Interpreter::Interpreter(VM& vm)
  103. : m_vm(vm)
  104. {
  105. }
  106. Interpreter::~Interpreter()
  107. {
  108. }
  109. void Interpreter::visit_edges(Cell::Visitor& visitor)
  110. {
  111. for (auto& frame : m_call_frames) {
  112. frame.visit([&](auto& value) { value->visit_edges(visitor); });
  113. }
  114. }
  115. ALWAYS_INLINE Value Interpreter::get(Operand op) const
  116. {
  117. switch (op.type()) {
  118. case Operand::Type::Register:
  119. return reg(Register { op.index() });
  120. case Operand::Type::Local:
  121. return vm().running_execution_context().locals[op.index()];
  122. case Operand::Type::Constant:
  123. return current_executable().constants[op.index()];
  124. }
  125. __builtin_unreachable();
  126. }
  127. ALWAYS_INLINE void Interpreter::set(Operand op, Value value)
  128. {
  129. switch (op.type()) {
  130. case Operand::Type::Register:
  131. reg(Register { op.index() }) = value;
  132. return;
  133. case Operand::Type::Local:
  134. vm().running_execution_context().locals[op.index()] = value;
  135. return;
  136. case Operand::Type::Constant:
  137. VERIFY_NOT_REACHED();
  138. }
  139. __builtin_unreachable();
  140. }
  141. // 16.1.6 ScriptEvaluation ( scriptRecord ), https://tc39.es/ecma262/#sec-runtime-semantics-scriptevaluation
  142. ThrowCompletionOr<Value> Interpreter::run(Script& script_record, JS::GCPtr<Environment> lexical_environment_override)
  143. {
  144. auto& vm = this->vm();
  145. // 1. Let globalEnv be scriptRecord.[[Realm]].[[GlobalEnv]].
  146. auto& global_environment = script_record.realm().global_environment();
  147. // 2. Let scriptContext be a new ECMAScript code execution context.
  148. auto script_context = ExecutionContext::create(vm.heap());
  149. // 3. Set the Function of scriptContext to null.
  150. // NOTE: This was done during execution context construction.
  151. // 4. Set the Realm of scriptContext to scriptRecord.[[Realm]].
  152. script_context->realm = &script_record.realm();
  153. // 5. Set the ScriptOrModule of scriptContext to scriptRecord.
  154. script_context->script_or_module = NonnullGCPtr<Script>(script_record);
  155. // 6. Set the VariableEnvironment of scriptContext to globalEnv.
  156. script_context->variable_environment = &global_environment;
  157. // 7. Set the LexicalEnvironment of scriptContext to globalEnv.
  158. script_context->lexical_environment = &global_environment;
  159. // Non-standard: Override the lexical environment if requested.
  160. if (lexical_environment_override)
  161. script_context->lexical_environment = lexical_environment_override;
  162. // 8. Set the PrivateEnvironment of scriptContext to null.
  163. // NOTE: This isn't in the spec, but we require it.
  164. script_context->is_strict_mode = script_record.parse_node().is_strict_mode();
  165. // FIXME: 9. Suspend the currently running execution context.
  166. // 10. Push scriptContext onto the execution context stack; scriptContext is now the running execution context.
  167. TRY(vm.push_execution_context(*script_context, {}));
  168. // 11. Let script be scriptRecord.[[ECMAScriptCode]].
  169. auto& script = script_record.parse_node();
  170. // 12. Let result be Completion(GlobalDeclarationInstantiation(script, globalEnv)).
  171. auto instantiation_result = script.global_declaration_instantiation(vm, global_environment);
  172. Completion result = instantiation_result.is_throw_completion() ? instantiation_result.throw_completion() : normal_completion({});
  173. // 13. If result.[[Type]] is normal, then
  174. if (result.type() == Completion::Type::Normal) {
  175. auto executable_result = JS::Bytecode::Generator::generate(vm, script, {});
  176. if (executable_result.is_error()) {
  177. if (auto error_string = executable_result.error().to_string(); error_string.is_error())
  178. result = vm.template throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory));
  179. else if (error_string = String::formatted("TODO({})", error_string.value()); error_string.is_error())
  180. result = vm.template throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory));
  181. else
  182. result = JS::throw_completion(JS::InternalError::create(realm(), error_string.release_value()));
  183. } else {
  184. auto executable = executable_result.release_value();
  185. if (g_dump_bytecode)
  186. executable->dump();
  187. // a. Set result to the result of evaluating script.
  188. auto result_or_error = run_and_return_frame(*executable, nullptr);
  189. if (result_or_error.value.is_error())
  190. result = result_or_error.value.release_error();
  191. else
  192. result = result_or_error.frame->registers()[0];
  193. }
  194. }
  195. // 14. If result.[[Type]] is normal and result.[[Value]] is empty, then
  196. if (result.type() == Completion::Type::Normal && !result.value().has_value()) {
  197. // a. Set result to NormalCompletion(undefined).
  198. result = normal_completion(js_undefined());
  199. }
  200. // FIXME: 15. Suspend scriptContext and remove it from the execution context stack.
  201. vm.pop_execution_context();
  202. // 16. Assert: The execution context stack is not empty.
  203. VERIFY(!vm.execution_context_stack().is_empty());
  204. // FIXME: 17. Resume the context that is now on the top of the execution context stack as the running execution context.
  205. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  206. // in which case this is a no-op.
  207. // 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.
  208. // https://tc39.es/ecma262/#sec-jobs for jobs and https://tc39.es/ecma262/#_ref_3508 for ClearKeptObjects
  209. // finish_execution_generation is particularly an issue for LibWeb, as the HTML spec wants to run it specifically after performing a microtask checkpoint.
  210. // The promise and registry cleanup queues don't cause LibWeb an issue, as LibWeb overrides the hooks that push onto these queues.
  211. vm.run_queued_promise_jobs();
  212. vm.run_queued_finalization_registry_cleanup_jobs();
  213. vm.finish_execution_generation();
  214. // 18. Return ? result.
  215. if (result.is_abrupt()) {
  216. VERIFY(result.type() == Completion::Type::Throw);
  217. return result.release_error();
  218. }
  219. VERIFY(result.value().has_value());
  220. return *result.value();
  221. }
  222. ThrowCompletionOr<Value> Interpreter::run(SourceTextModule& module)
  223. {
  224. // FIXME: This is not a entry point as defined in the spec, but is convenient.
  225. // To avoid work we use link_and_eval_module however that can already be
  226. // dangerous if the vm loaded other modules.
  227. auto& vm = this->vm();
  228. TRY(vm.link_and_eval_module(Badge<Bytecode::Interpreter> {}, module));
  229. vm.run_queued_promise_jobs();
  230. vm.run_queued_finalization_registry_cleanup_jobs();
  231. return js_undefined();
  232. }
  233. void Interpreter::run_bytecode()
  234. {
  235. auto* locals = vm().running_execution_context().locals.data();
  236. auto& accumulator = this->accumulator();
  237. for (;;) {
  238. start:
  239. auto pc = InstructionStreamIterator { m_current_block->instruction_stream(), m_current_executable };
  240. TemporaryChange temp_change { m_pc, Optional<InstructionStreamIterator&>(pc) };
  241. bool will_return = false;
  242. bool will_yield = false;
  243. ThrowCompletionOr<void> result;
  244. while (!pc.at_end()) {
  245. auto& instruction = *pc;
  246. switch (instruction.type()) {
  247. case Instruction::Type::SetLocal:
  248. locals[static_cast<Op::SetLocal const&>(instruction).index()] = get(static_cast<Op::SetLocal const&>(instruction).src());
  249. break;
  250. case Instruction::Type::Mov:
  251. set(static_cast<Op::Mov const&>(instruction).dst(), get(static_cast<Op::Mov const&>(instruction).src()));
  252. break;
  253. case Instruction::Type::End:
  254. accumulator = get(static_cast<Op::End const&>(instruction).value());
  255. return;
  256. case Instruction::Type::Jump:
  257. m_current_block = &static_cast<Op::Jump const&>(instruction).true_target()->block();
  258. goto start;
  259. case Instruction::Type::JumpIf:
  260. if (get(static_cast<Op::JumpIf const&>(instruction).condition()).to_boolean())
  261. m_current_block = &static_cast<Op::JumpIf const&>(instruction).true_target()->block();
  262. else
  263. m_current_block = &static_cast<Op::JumpIf const&>(instruction).false_target()->block();
  264. goto start;
  265. case Instruction::Type::JumpNullish:
  266. if (get(static_cast<Op::JumpNullish const&>(instruction).condition()).is_nullish())
  267. m_current_block = &static_cast<Op::Jump const&>(instruction).true_target()->block();
  268. else
  269. m_current_block = &static_cast<Op::Jump const&>(instruction).false_target()->block();
  270. goto start;
  271. case Instruction::Type::JumpUndefined:
  272. if (get(static_cast<Op::JumpUndefined const&>(instruction).condition()).is_undefined())
  273. m_current_block = &static_cast<Op::Jump const&>(instruction).true_target()->block();
  274. else
  275. m_current_block = &static_cast<Op::Jump const&>(instruction).false_target()->block();
  276. goto start;
  277. case Instruction::Type::EnterUnwindContext:
  278. enter_unwind_context();
  279. m_current_block = &static_cast<Op::EnterUnwindContext const&>(instruction).entry_point().block();
  280. goto start;
  281. case Instruction::Type::ContinuePendingUnwind: {
  282. if (auto exception = reg(Register::exception()); !exception.is_empty()) {
  283. result = throw_completion(exception);
  284. break;
  285. }
  286. if (!saved_return_value().is_empty()) {
  287. do_return(saved_return_value());
  288. break;
  289. }
  290. auto const* old_scheduled_jump = call_frame().previously_scheduled_jumps.take_last();
  291. if (m_scheduled_jump) {
  292. // FIXME: If we `break` or `continue` in the finally, we need to clear
  293. // this field
  294. // Same goes for popping an old_scheduled_jump form the stack
  295. m_current_block = exchange(m_scheduled_jump, nullptr);
  296. } else {
  297. m_current_block = &static_cast<Op::ContinuePendingUnwind const&>(instruction).resume_target().block();
  298. // set the scheduled jump to the old value if we continue
  299. // where we left it
  300. m_scheduled_jump = old_scheduled_jump;
  301. }
  302. goto start;
  303. }
  304. case Instruction::Type::ScheduleJump: {
  305. m_scheduled_jump = &static_cast<Op::ScheduleJump const&>(instruction).target().block();
  306. auto const* finalizer = m_current_block->finalizer();
  307. VERIFY(finalizer);
  308. m_current_block = finalizer;
  309. goto start;
  310. }
  311. default:
  312. result = instruction.execute(*this);
  313. break;
  314. }
  315. if (result.is_error()) [[unlikely]] {
  316. reg(Register::exception()) = *result.throw_completion().value();
  317. m_scheduled_jump = {};
  318. auto const* handler = m_current_block->handler();
  319. auto const* finalizer = m_current_block->finalizer();
  320. if (!handler && !finalizer)
  321. return;
  322. auto& unwind_context = unwind_contexts().last();
  323. VERIFY(unwind_context.executable == m_current_executable);
  324. if (handler) {
  325. m_current_block = handler;
  326. goto start;
  327. }
  328. if (finalizer) {
  329. m_current_block = finalizer;
  330. // If an exception was thrown inside the corresponding `catch` block, we need to rethrow it
  331. // from the `finally` block. But if the exception is from the `try` block, and has already been
  332. // handled by `catch`, we swallow it.
  333. if (!unwind_context.handler_called)
  334. reg(Register::exception()) = {};
  335. goto start;
  336. }
  337. // 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.
  338. // If you run into this, you probably forgot to remove the current unwind_context somewhere.
  339. VERIFY_NOT_REACHED();
  340. }
  341. if (!reg(Register::return_value()).is_empty()) {
  342. will_return = true;
  343. // Note: A `yield` statement will not go through a finally statement,
  344. // hence we need to set a flag to not do so,
  345. // but we generate a Yield Operation in the case of returns in
  346. // generators as well, so we need to check if it will actually
  347. // continue or is a `return` in disguise
  348. will_yield = (instruction.type() == Instruction::Type::Yield && static_cast<Op::Yield const&>(instruction).continuation().has_value()) || instruction.type() == Instruction::Type::Await;
  349. break;
  350. }
  351. ++pc;
  352. }
  353. if (auto const* finalizer = m_current_block->finalizer(); finalizer && !will_yield) {
  354. auto& unwind_context = unwind_contexts().last();
  355. VERIFY(unwind_context.executable == m_current_executable);
  356. reg(Register::saved_return_value()) = reg(Register::return_value());
  357. reg(Register::return_value()) = {};
  358. m_current_block = finalizer;
  359. // the unwind_context will be pop'ed when entering the finally block
  360. continue;
  361. }
  362. if (pc.at_end())
  363. break;
  364. if (will_return)
  365. break;
  366. }
  367. }
  368. Interpreter::ValueAndFrame Interpreter::run_and_return_frame(Executable& executable, BasicBlock const* entry_point, CallFrame* in_frame)
  369. {
  370. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable);
  371. TemporaryChange restore_executable { m_current_executable, &executable };
  372. TemporaryChange restore_saved_jump { m_scheduled_jump, static_cast<BasicBlock const*>(nullptr) };
  373. TemporaryChange restore_realm { m_realm, vm().current_realm() };
  374. TemporaryChange restore_global_object { m_global_object, &m_realm->global_object() };
  375. TemporaryChange restore_global_declarative_environment { m_global_declarative_environment, &m_realm->global_environment().declarative_record() };
  376. VERIFY(!vm().execution_context_stack().is_empty());
  377. TemporaryChange restore_current_block { m_current_block, entry_point ?: executable.basic_blocks.first() };
  378. if (in_frame)
  379. push_call_frame(in_frame);
  380. else
  381. push_call_frame(CallFrame::create(executable.number_of_registers));
  382. vm().execution_context_stack().last()->executable = &executable;
  383. run_bytecode();
  384. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable);
  385. if constexpr (JS_BYTECODE_DEBUG) {
  386. for (size_t i = 0; i < registers().size(); ++i) {
  387. String value_string;
  388. if (registers()[i].is_empty())
  389. value_string = "(empty)"_string;
  390. else
  391. value_string = registers()[i].to_string_without_side_effects();
  392. dbgln("[{:3}] {}", i, value_string);
  393. }
  394. }
  395. auto return_value = js_undefined();
  396. if (!reg(Register::return_value()).is_empty())
  397. return_value = reg(Register::return_value());
  398. else if (!reg(Register::saved_return_value()).is_empty())
  399. return_value = reg(Register::saved_return_value());
  400. auto exception = reg(Register::exception());
  401. auto frame = pop_call_frame();
  402. // NOTE: The return value from a called function is put into $0 in the caller context.
  403. if (!m_call_frames.is_empty())
  404. call_frame().registers()[0] = return_value;
  405. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  406. // in which case this is a no-op.
  407. vm().run_queued_promise_jobs();
  408. vm().finish_execution_generation();
  409. if (!exception.is_empty()) {
  410. if (auto* call_frame = frame.get_pointer<NonnullOwnPtr<CallFrame>>())
  411. return { throw_completion(exception), move(*call_frame) };
  412. return { throw_completion(exception), nullptr };
  413. }
  414. if (auto* call_frame = frame.get_pointer<NonnullOwnPtr<CallFrame>>())
  415. return { return_value, move(*call_frame) };
  416. return { return_value, nullptr };
  417. }
  418. void Interpreter::enter_unwind_context()
  419. {
  420. unwind_contexts().empend(
  421. m_current_executable,
  422. vm().running_execution_context().lexical_environment);
  423. call_frame().previously_scheduled_jumps.append(m_scheduled_jump);
  424. m_scheduled_jump = nullptr;
  425. }
  426. void Interpreter::leave_unwind_context()
  427. {
  428. unwind_contexts().take_last();
  429. }
  430. void Interpreter::catch_exception(Operand dst)
  431. {
  432. set(dst, reg(Register::exception()));
  433. reg(Register::exception()) = {};
  434. auto& context = unwind_contexts().last();
  435. VERIFY(!context.handler_called);
  436. VERIFY(context.executable == &current_executable());
  437. context.handler_called = true;
  438. vm().running_execution_context().lexical_environment = context.lexical_environment;
  439. }
  440. void Interpreter::enter_object_environment(Object& object)
  441. {
  442. auto& old_environment = vm().running_execution_context().lexical_environment;
  443. saved_lexical_environment_stack().append(old_environment);
  444. vm().running_execution_context().lexical_environment = new_object_environment(object, true, old_environment);
  445. }
  446. ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM& vm, ASTNode const& node, ReadonlySpan<FunctionParameter> parameters, FunctionKind kind, DeprecatedFlyString const& name)
  447. {
  448. auto executable_result = Bytecode::Generator::generate(vm, node, parameters, kind);
  449. if (executable_result.is_error())
  450. return vm.throw_completion<InternalError>(ErrorType::NotImplemented, TRY_OR_THROW_OOM(vm, executable_result.error().to_string()));
  451. auto bytecode_executable = executable_result.release_value();
  452. bytecode_executable->name = name;
  453. if (Bytecode::g_dump_bytecode)
  454. bytecode_executable->dump();
  455. return bytecode_executable;
  456. }
  457. void Interpreter::push_call_frame(Variant<NonnullOwnPtr<CallFrame>, CallFrame*> frame)
  458. {
  459. m_call_frames.append(move(frame));
  460. m_current_call_frame = this->call_frame().registers();
  461. reg(Register::return_value()) = {};
  462. }
  463. Variant<NonnullOwnPtr<CallFrame>, CallFrame*> Interpreter::pop_call_frame()
  464. {
  465. auto frame = m_call_frames.take_last();
  466. m_current_call_frame = m_call_frames.is_empty() ? Span<Value> {} : this->call_frame().registers();
  467. return frame;
  468. }
  469. }
  470. namespace JS::Bytecode {
  471. ByteString Instruction::to_byte_string(Bytecode::Executable const& executable) const
  472. {
  473. #define __BYTECODE_OP(op) \
  474. case Instruction::Type::op: \
  475. return static_cast<Bytecode::Op::op const&>(*this).to_byte_string_impl(executable);
  476. switch (type()) {
  477. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  478. default:
  479. VERIFY_NOT_REACHED();
  480. }
  481. #undef __BYTECODE_OP
  482. }
  483. }
  484. namespace JS::Bytecode::Op {
  485. static void dump_object(Object& o, HashTable<Object const*>& seen, int indent = 0)
  486. {
  487. if (seen.contains(&o))
  488. return;
  489. seen.set(&o);
  490. for (auto& it : o.shape().property_table()) {
  491. auto value = o.get_direct(it.value.offset);
  492. dbgln("{} {} -> {}", String::repeated(' ', indent).release_value(), it.key.to_display_string(), value);
  493. if (value.is_object()) {
  494. dump_object(value.as_object(), seen, indent + 2);
  495. }
  496. }
  497. }
  498. ThrowCompletionOr<void> Dump::execute_impl(Bytecode::Interpreter& interpreter) const
  499. {
  500. auto value = interpreter.get(m_value);
  501. dbgln("(DUMP) {}: {}", m_text, value);
  502. if (value.is_object()) {
  503. HashTable<Object const*> seen;
  504. dump_object(value.as_object(), seen);
  505. }
  506. return {};
  507. }
  508. ThrowCompletionOr<void> End::execute_impl(Bytecode::Interpreter&) const
  509. {
  510. // Handled in the interpreter loop.
  511. __builtin_unreachable();
  512. }
  513. static ThrowCompletionOr<Value> loosely_inequals(VM& vm, Value src1, Value src2)
  514. {
  515. return Value(!TRY(is_loosely_equal(vm, src1, src2)));
  516. }
  517. static ThrowCompletionOr<Value> loosely_equals(VM& vm, Value src1, Value src2)
  518. {
  519. return Value(TRY(is_loosely_equal(vm, src1, src2)));
  520. }
  521. static ThrowCompletionOr<Value> strict_inequals(VM&, Value src1, Value src2)
  522. {
  523. return Value(!is_strictly_equal(src1, src2));
  524. }
  525. static ThrowCompletionOr<Value> strict_equals(VM&, Value src1, Value src2)
  526. {
  527. return Value(is_strictly_equal(src1, src2));
  528. }
  529. #define JS_DEFINE_EXECUTE_FOR_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  530. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  531. { \
  532. auto& vm = interpreter.vm(); \
  533. auto lhs = interpreter.get(m_lhs); \
  534. auto rhs = interpreter.get(m_rhs); \
  535. interpreter.set(m_dst, TRY(op_snake_case(vm, lhs, rhs))); \
  536. return {}; \
  537. }
  538. #define JS_DEFINE_TO_BYTE_STRING_FOR_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  539. ByteString OpTitleCase::to_byte_string_impl(Bytecode::Executable const& executable) const \
  540. { \
  541. return ByteString::formatted(#OpTitleCase " {}, {}, {}", \
  542. format_operand("dst"sv, m_dst, executable), \
  543. format_operand("lhs"sv, m_lhs, executable), \
  544. format_operand("rhs"sv, m_rhs, executable)); \
  545. }
  546. JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(JS_DEFINE_EXECUTE_FOR_COMMON_BINARY_OP)
  547. JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(JS_DEFINE_TO_BYTE_STRING_FOR_COMMON_BINARY_OP)
  548. JS_ENUMERATE_COMMON_BINARY_OPS_WITH_FAST_PATH(JS_DEFINE_TO_BYTE_STRING_FOR_COMMON_BINARY_OP)
  549. ThrowCompletionOr<void> Add::execute_impl(Bytecode::Interpreter& interpreter) const
  550. {
  551. auto& vm = interpreter.vm();
  552. auto const lhs = interpreter.get(m_lhs);
  553. auto const rhs = interpreter.get(m_rhs);
  554. if (lhs.is_number() && rhs.is_number()) {
  555. if (lhs.is_int32() && rhs.is_int32()) {
  556. if (!Checked<i32>::addition_would_overflow(lhs.as_i32(), rhs.as_i32())) {
  557. interpreter.set(m_dst, Value(lhs.as_i32() + rhs.as_i32()));
  558. return {};
  559. }
  560. }
  561. interpreter.set(m_dst, Value(lhs.as_double() + rhs.as_double()));
  562. return {};
  563. }
  564. interpreter.set(m_dst, TRY(add(vm, lhs, rhs)));
  565. return {};
  566. }
  567. ThrowCompletionOr<void> Mul::execute_impl(Bytecode::Interpreter& interpreter) const
  568. {
  569. auto& vm = interpreter.vm();
  570. auto const lhs = interpreter.get(m_lhs);
  571. auto const rhs = interpreter.get(m_rhs);
  572. if (lhs.is_number() && rhs.is_number()) {
  573. if (lhs.is_int32() && rhs.is_int32()) {
  574. if (!Checked<i32>::multiplication_would_overflow(lhs.as_i32(), rhs.as_i32())) {
  575. interpreter.set(m_dst, Value(lhs.as_i32() * rhs.as_i32()));
  576. return {};
  577. }
  578. }
  579. interpreter.set(m_dst, Value(lhs.as_double() * rhs.as_double()));
  580. return {};
  581. }
  582. interpreter.set(m_dst, TRY(mul(vm, lhs, rhs)));
  583. return {};
  584. }
  585. ThrowCompletionOr<void> Sub::execute_impl(Bytecode::Interpreter& interpreter) const
  586. {
  587. auto& vm = interpreter.vm();
  588. auto const lhs = interpreter.get(m_lhs);
  589. auto const rhs = interpreter.get(m_rhs);
  590. if (lhs.is_number() && rhs.is_number()) {
  591. if (lhs.is_int32() && rhs.is_int32()) {
  592. if (!Checked<i32>::addition_would_overflow(lhs.as_i32(), -rhs.as_i32())) {
  593. interpreter.set(m_dst, Value(lhs.as_i32() - rhs.as_i32()));
  594. return {};
  595. }
  596. }
  597. interpreter.set(m_dst, Value(lhs.as_double() - rhs.as_double()));
  598. return {};
  599. }
  600. interpreter.set(m_dst, TRY(sub(vm, lhs, rhs)));
  601. return {};
  602. }
  603. ThrowCompletionOr<void> BitwiseXor::execute_impl(Bytecode::Interpreter& interpreter) const
  604. {
  605. auto& vm = interpreter.vm();
  606. auto const lhs = interpreter.get(m_lhs);
  607. auto const rhs = interpreter.get(m_rhs);
  608. if (lhs.is_int32() && rhs.is_int32()) {
  609. interpreter.set(m_dst, Value(lhs.as_i32() ^ rhs.as_i32()));
  610. return {};
  611. }
  612. interpreter.set(m_dst, TRY(bitwise_xor(vm, lhs, rhs)));
  613. return {};
  614. }
  615. ThrowCompletionOr<void> BitwiseAnd::execute_impl(Bytecode::Interpreter& interpreter) const
  616. {
  617. auto& vm = interpreter.vm();
  618. auto const lhs = interpreter.get(m_lhs);
  619. auto const rhs = interpreter.get(m_rhs);
  620. if (lhs.is_int32() && rhs.is_int32()) {
  621. interpreter.set(m_dst, Value(lhs.as_i32() & rhs.as_i32()));
  622. return {};
  623. }
  624. interpreter.set(m_dst, TRY(bitwise_and(vm, lhs, rhs)));
  625. return {};
  626. }
  627. ThrowCompletionOr<void> BitwiseOr::execute_impl(Bytecode::Interpreter& interpreter) const
  628. {
  629. auto& vm = interpreter.vm();
  630. auto const lhs = interpreter.get(m_lhs);
  631. auto const rhs = interpreter.get(m_rhs);
  632. if (lhs.is_int32() && rhs.is_int32()) {
  633. interpreter.set(m_dst, Value(lhs.as_i32() | rhs.as_i32()));
  634. return {};
  635. }
  636. interpreter.set(m_dst, TRY(bitwise_or(vm, lhs, rhs)));
  637. return {};
  638. }
  639. ThrowCompletionOr<void> UnsignedRightShift::execute_impl(Bytecode::Interpreter& interpreter) const
  640. {
  641. auto& vm = interpreter.vm();
  642. auto const lhs = interpreter.get(m_lhs);
  643. auto const rhs = interpreter.get(m_rhs);
  644. if (lhs.is_int32() && rhs.is_int32() && lhs.as_i32() >= 0 && rhs.as_i32() >= 0) {
  645. auto const shift_count = static_cast<u32>(rhs.as_i32()) % 32;
  646. interpreter.set(m_dst, Value(static_cast<u32>(lhs.as_i32()) >> shift_count));
  647. return {};
  648. }
  649. interpreter.set(m_dst, TRY(unsigned_right_shift(vm, lhs, rhs)));
  650. return {};
  651. }
  652. ThrowCompletionOr<void> RightShift::execute_impl(Bytecode::Interpreter& interpreter) const
  653. {
  654. auto& vm = interpreter.vm();
  655. auto const lhs = interpreter.get(m_lhs);
  656. auto const rhs = interpreter.get(m_rhs);
  657. if (lhs.is_int32() && rhs.is_int32() && rhs.as_i32() >= 0) {
  658. auto const shift_count = static_cast<u32>(rhs.as_i32()) % 32;
  659. interpreter.set(m_dst, Value(lhs.as_i32() >> shift_count));
  660. return {};
  661. }
  662. interpreter.set(m_dst, TRY(right_shift(vm, lhs, rhs)));
  663. return {};
  664. }
  665. ThrowCompletionOr<void> LessThan::execute_impl(Bytecode::Interpreter& interpreter) const
  666. {
  667. auto& vm = interpreter.vm();
  668. auto const lhs = interpreter.get(m_lhs);
  669. auto const rhs = interpreter.get(m_rhs);
  670. if (lhs.is_int32() && rhs.is_int32()) {
  671. interpreter.set(m_dst, Value(lhs.as_i32() < rhs.as_i32()));
  672. return {};
  673. }
  674. interpreter.set(m_dst, TRY(less_than(vm, lhs, rhs)));
  675. return {};
  676. }
  677. ThrowCompletionOr<void> LessThanEquals::execute_impl(Bytecode::Interpreter& interpreter) const
  678. {
  679. auto& vm = interpreter.vm();
  680. auto const lhs = interpreter.get(m_lhs);
  681. auto const rhs = interpreter.get(m_rhs);
  682. if (lhs.is_int32() && rhs.is_int32()) {
  683. interpreter.set(m_dst, Value(lhs.as_i32() <= rhs.as_i32()));
  684. return {};
  685. }
  686. interpreter.set(m_dst, TRY(less_than_equals(vm, lhs, rhs)));
  687. return {};
  688. }
  689. ThrowCompletionOr<void> GreaterThan::execute_impl(Bytecode::Interpreter& interpreter) const
  690. {
  691. auto& vm = interpreter.vm();
  692. auto const lhs = interpreter.get(m_lhs);
  693. auto const rhs = interpreter.get(m_rhs);
  694. if (lhs.is_int32() && rhs.is_int32()) {
  695. interpreter.set(m_dst, Value(lhs.as_i32() > rhs.as_i32()));
  696. return {};
  697. }
  698. interpreter.set(m_dst, TRY(greater_than(vm, lhs, rhs)));
  699. return {};
  700. }
  701. ThrowCompletionOr<void> GreaterThanEquals::execute_impl(Bytecode::Interpreter& interpreter) const
  702. {
  703. auto& vm = interpreter.vm();
  704. auto const lhs = interpreter.get(m_lhs);
  705. auto const rhs = interpreter.get(m_rhs);
  706. if (lhs.is_int32() && rhs.is_int32()) {
  707. interpreter.set(m_dst, Value(lhs.as_i32() >= rhs.as_i32()));
  708. return {};
  709. }
  710. interpreter.set(m_dst, TRY(greater_than_equals(vm, lhs, rhs)));
  711. return {};
  712. }
  713. static ThrowCompletionOr<Value> not_(VM&, Value value)
  714. {
  715. return Value(!value.to_boolean());
  716. }
  717. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  718. {
  719. return PrimitiveString::create(vm, value.typeof());
  720. }
  721. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  722. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  723. { \
  724. auto& vm = interpreter.vm(); \
  725. interpreter.set(dst(), TRY(op_snake_case(vm, interpreter.get(src())))); \
  726. return {}; \
  727. } \
  728. ByteString OpTitleCase::to_byte_string_impl(Bytecode::Executable const& executable) const \
  729. { \
  730. return ByteString::formatted(#OpTitleCase " {}, {}", \
  731. format_operand("dst"sv, dst(), executable), \
  732. format_operand("src"sv, src(), executable)); \
  733. }
  734. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  735. ThrowCompletionOr<void> NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
  736. {
  737. auto& vm = interpreter.vm();
  738. interpreter.set(dst(), BigInt::create(vm, m_bigint));
  739. return {};
  740. }
  741. ThrowCompletionOr<void> NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  742. {
  743. auto array = MUST(Array::create(interpreter.realm(), 0));
  744. for (size_t i = 0; i < m_element_count; i++) {
  745. auto& value = interpreter.reg(Register(m_elements[0].index() + i));
  746. array->indexed_properties().put(i, value, default_attributes);
  747. }
  748. interpreter.set(dst(), array);
  749. return {};
  750. }
  751. ThrowCompletionOr<void> NewPrimitiveArray::execute_impl(Bytecode::Interpreter& interpreter) const
  752. {
  753. auto array = MUST(Array::create(interpreter.realm(), 0));
  754. for (size_t i = 0; i < m_values.size(); i++)
  755. array->indexed_properties().put(i, m_values[i], default_attributes);
  756. interpreter.set(dst(), array);
  757. return {};
  758. }
  759. ThrowCompletionOr<void> ArrayAppend::execute_impl(Bytecode::Interpreter& interpreter) const
  760. {
  761. return append(interpreter.vm(), interpreter.get(dst()), interpreter.get(src()), m_is_spread);
  762. }
  763. ThrowCompletionOr<void> ImportCall::execute_impl(Bytecode::Interpreter& interpreter) const
  764. {
  765. auto& vm = interpreter.vm();
  766. auto specifier = interpreter.get(m_specifier);
  767. auto options_value = interpreter.get(m_options);
  768. interpreter.set(dst(), TRY(perform_import_call(vm, specifier, options_value)));
  769. return {};
  770. }
  771. ThrowCompletionOr<void> IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const
  772. {
  773. interpreter.set(dst(), TRY(iterator_to_array(interpreter.vm(), interpreter.get(iterator()))));
  774. return {};
  775. }
  776. ThrowCompletionOr<void> NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  777. {
  778. auto& vm = interpreter.vm();
  779. auto& realm = *vm.current_realm();
  780. interpreter.set(dst(), Object::create(realm, realm.intrinsics().object_prototype()));
  781. return {};
  782. }
  783. ThrowCompletionOr<void> NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
  784. {
  785. interpreter.set(dst(),
  786. new_regexp(
  787. interpreter.vm(),
  788. interpreter.current_executable().regex_table->get(m_regex_index),
  789. interpreter.current_executable().get_string(m_source_index),
  790. interpreter.current_executable().get_string(m_flags_index)));
  791. return {};
  792. }
  793. #define JS_DEFINE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  794. ThrowCompletionOr<void> New##ErrorName::execute_impl(Bytecode::Interpreter& interpreter) const \
  795. { \
  796. auto& vm = interpreter.vm(); \
  797. auto& realm = *vm.current_realm(); \
  798. interpreter.set(dst(), ErrorName::create(realm, interpreter.current_executable().get_string(m_error_string))); \
  799. return {}; \
  800. } \
  801. ByteString New##ErrorName::to_byte_string_impl(Bytecode::Executable const& executable) const \
  802. { \
  803. return ByteString::formatted("New" #ErrorName " {}, {}", \
  804. format_operand("dst"sv, m_dst, executable), \
  805. executable.string_table->get(m_error_string)); \
  806. }
  807. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DEFINE_NEW_BUILTIN_ERROR_OP)
  808. ThrowCompletionOr<void> CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const
  809. {
  810. auto& vm = interpreter.vm();
  811. auto& realm = *vm.current_realm();
  812. auto from_object = interpreter.get(m_from_object);
  813. auto to_object = Object::create(realm, realm.intrinsics().object_prototype());
  814. HashTable<PropertyKey> excluded_names;
  815. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  816. excluded_names.set(TRY(interpreter.get(m_excluded_names[i]).to_property_key(vm)));
  817. }
  818. TRY(to_object->copy_data_properties(vm, from_object, excluded_names));
  819. interpreter.set(dst(), to_object);
  820. return {};
  821. }
  822. ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  823. {
  824. auto& vm = interpreter.vm();
  825. auto string = TRY(interpreter.get(src()).to_primitive_string(vm));
  826. interpreter.set(dst(), PrimitiveString::create(vm, interpreter.get(dst()).as_string(), string));
  827. return {};
  828. }
  829. ThrowCompletionOr<void> GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  830. {
  831. interpreter.set(dst(), TRY(get_variable(interpreter, interpreter.current_executable().get_identifier(m_identifier), interpreter.current_executable().environment_variable_caches[m_cache_index])));
  832. return {};
  833. }
  834. ThrowCompletionOr<void> GetCalleeAndThisFromEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  835. {
  836. auto callee_and_this = TRY(get_callee_and_this_from_environment(
  837. interpreter,
  838. interpreter.current_executable().get_identifier(m_identifier),
  839. interpreter.current_executable().environment_variable_caches[m_cache_index]));
  840. interpreter.set(m_callee, callee_and_this.callee);
  841. interpreter.set(m_this_value, callee_and_this.this_value);
  842. return {};
  843. }
  844. ThrowCompletionOr<void> GetGlobal::execute_impl(Bytecode::Interpreter& interpreter) const
  845. {
  846. interpreter.set(dst(), TRY(get_global(interpreter, interpreter.current_executable().get_identifier(m_identifier), interpreter.current_executable().global_variable_caches[m_cache_index])));
  847. return {};
  848. }
  849. ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  850. {
  851. auto& vm = interpreter.vm();
  852. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  853. auto reference = TRY(vm.resolve_binding(string));
  854. interpreter.set(dst(), Value(TRY(reference.delete_(vm))));
  855. return {};
  856. }
  857. ThrowCompletionOr<void> CreateLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  858. {
  859. auto make_and_swap_envs = [&](auto& old_environment) {
  860. GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
  861. swap(old_environment, environment);
  862. return environment;
  863. };
  864. interpreter.saved_lexical_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().lexical_environment));
  865. return {};
  866. }
  867. ThrowCompletionOr<void> EnterObjectEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  868. {
  869. auto object = TRY(interpreter.get(m_object).to_object(interpreter.vm()));
  870. interpreter.enter_object_environment(*object);
  871. return {};
  872. }
  873. ThrowCompletionOr<void> Catch::execute_impl(Bytecode::Interpreter& interpreter) const
  874. {
  875. interpreter.catch_exception(dst());
  876. return {};
  877. }
  878. ThrowCompletionOr<void> CreateVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  879. {
  880. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  881. return create_variable(interpreter.vm(), name, m_mode, m_is_global, m_is_immutable, m_is_strict);
  882. }
  883. ThrowCompletionOr<void> SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  884. {
  885. auto& vm = interpreter.vm();
  886. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  887. TRY(set_variable(vm,
  888. name,
  889. interpreter.get(src()),
  890. m_mode,
  891. m_initialization_mode,
  892. interpreter.current_executable().environment_variable_caches[m_cache_index]));
  893. return {};
  894. }
  895. ThrowCompletionOr<void> SetLocal::execute_impl(Bytecode::Interpreter&) const
  896. {
  897. // Handled in the interpreter loop.
  898. __builtin_unreachable();
  899. }
  900. ThrowCompletionOr<void> GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  901. {
  902. auto base_value = interpreter.get(base());
  903. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  904. interpreter.set(dst(), TRY(get_by_id(interpreter.vm(), interpreter.current_executable().get_identifier(m_property), base_value, base_value, cache)));
  905. return {};
  906. }
  907. ThrowCompletionOr<void> GetByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  908. {
  909. auto base_value = interpreter.get(m_base);
  910. auto this_value = interpreter.get(m_this_value);
  911. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  912. interpreter.set(dst(), TRY(get_by_id(interpreter.vm(), interpreter.current_executable().get_identifier(m_property), base_value, this_value, cache)));
  913. return {};
  914. }
  915. ThrowCompletionOr<void> GetPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const
  916. {
  917. auto& vm = interpreter.vm();
  918. auto const& name = interpreter.current_executable().get_identifier(m_property);
  919. auto base_value = interpreter.get(m_base);
  920. auto private_reference = make_private_reference(vm, base_value, name);
  921. interpreter.set(dst(), TRY(private_reference.get_value(vm)));
  922. return {};
  923. }
  924. ThrowCompletionOr<void> HasPrivateId::execute_impl(Bytecode::Interpreter& interpreter) const
  925. {
  926. auto& vm = interpreter.vm();
  927. auto base = interpreter.get(m_base);
  928. if (!base.is_object())
  929. return vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject);
  930. auto private_environment = vm.running_execution_context().private_environment;
  931. VERIFY(private_environment);
  932. auto private_name = private_environment->resolve_private_identifier(interpreter.current_executable().get_identifier(m_property));
  933. interpreter.set(dst(), Value(base.as_object().private_element_find(private_name) != nullptr));
  934. return {};
  935. }
  936. ThrowCompletionOr<void> PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  937. {
  938. auto& vm = interpreter.vm();
  939. auto value = interpreter.get(m_src);
  940. auto base = interpreter.get(m_base);
  941. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  942. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  943. TRY(put_by_property_key(vm, base, base, value, name, m_kind, &cache));
  944. return {};
  945. }
  946. ThrowCompletionOr<void> PutByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  947. {
  948. auto& vm = interpreter.vm();
  949. auto value = interpreter.get(m_src);
  950. auto base = interpreter.get(m_base);
  951. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  952. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  953. TRY(put_by_property_key(vm, base, interpreter.get(m_this_value), value, name, m_kind, &cache));
  954. return {};
  955. }
  956. ThrowCompletionOr<void> PutPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const
  957. {
  958. auto& vm = interpreter.vm();
  959. auto value = interpreter.get(m_src);
  960. auto object = TRY(interpreter.get(m_base).to_object(vm));
  961. auto name = interpreter.current_executable().get_identifier(m_property);
  962. auto private_reference = make_private_reference(vm, object, name);
  963. TRY(private_reference.put_value(vm, value));
  964. return {};
  965. }
  966. ThrowCompletionOr<void> DeleteById::execute_impl(Bytecode::Interpreter& interpreter) const
  967. {
  968. auto base_value = interpreter.get(m_base);
  969. interpreter.set(dst(), TRY(Bytecode::delete_by_id(interpreter, base_value, m_property)));
  970. return {};
  971. }
  972. ThrowCompletionOr<void> DeleteByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  973. {
  974. auto& vm = interpreter.vm();
  975. auto base_value = interpreter.get(m_base);
  976. auto const& identifier = interpreter.current_executable().get_identifier(m_property);
  977. bool strict = vm.in_strict_mode();
  978. auto reference = Reference { base_value, identifier, interpreter.get(m_this_value), strict };
  979. interpreter.set(dst(), Value(TRY(reference.delete_(vm))));
  980. return {};
  981. }
  982. ThrowCompletionOr<void> Jump::execute_impl(Bytecode::Interpreter&) const
  983. {
  984. // Handled in the interpreter loop.
  985. __builtin_unreachable();
  986. }
  987. ThrowCompletionOr<void> ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const
  988. {
  989. auto& cached_this_value = interpreter.reg(Register::this_value());
  990. if (cached_this_value.is_empty()) {
  991. // OPTIMIZATION: Because the value of 'this' cannot be reassigned during a function execution, it's
  992. // resolved once and then saved for subsequent use.
  993. auto& vm = interpreter.vm();
  994. cached_this_value = TRY(vm.resolve_this_binding());
  995. }
  996. interpreter.set(dst(), cached_this_value);
  997. return {};
  998. }
  999. // https://tc39.es/ecma262/#sec-makesuperpropertyreference
  1000. ThrowCompletionOr<void> ResolveSuperBase::execute_impl(Bytecode::Interpreter& interpreter) const
  1001. {
  1002. auto& vm = interpreter.vm();
  1003. // 1. Let env be GetThisEnvironment().
  1004. auto& env = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  1005. // 2. Assert: env.HasSuperBinding() is true.
  1006. VERIFY(env.has_super_binding());
  1007. // 3. Let baseValue be ? env.GetSuperBase().
  1008. interpreter.set(dst(), TRY(env.get_super_base()));
  1009. return {};
  1010. }
  1011. ThrowCompletionOr<void> GetNewTarget::execute_impl(Bytecode::Interpreter& interpreter) const
  1012. {
  1013. interpreter.set(dst(), interpreter.vm().get_new_target());
  1014. return {};
  1015. }
  1016. ThrowCompletionOr<void> GetImportMeta::execute_impl(Bytecode::Interpreter& interpreter) const
  1017. {
  1018. interpreter.set(dst(), interpreter.vm().get_import_meta());
  1019. return {};
  1020. }
  1021. ThrowCompletionOr<void> JumpIf::execute_impl(Bytecode::Interpreter&) const
  1022. {
  1023. // Handled in the interpreter loop.
  1024. __builtin_unreachable();
  1025. }
  1026. ThrowCompletionOr<void> JumpUndefined::execute_impl(Bytecode::Interpreter&) const
  1027. {
  1028. // Handled in the interpreter loop.
  1029. __builtin_unreachable();
  1030. }
  1031. ThrowCompletionOr<void> JumpNullish::execute_impl(Bytecode::Interpreter&) const
  1032. {
  1033. // Handled in the interpreter loop.
  1034. __builtin_unreachable();
  1035. }
  1036. ThrowCompletionOr<void> Mov::execute_impl(Bytecode::Interpreter&) const
  1037. {
  1038. // Handled in the interpreter loop.
  1039. __builtin_unreachable();
  1040. }
  1041. static ThrowCompletionOr<Value> dispatch_builtin_call(Bytecode::Interpreter& interpreter, Bytecode::Builtin builtin, ReadonlySpan<Operand> arguments)
  1042. {
  1043. switch (builtin) {
  1044. case Builtin::MathAbs:
  1045. return TRY(MathObject::abs_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1046. case Builtin::MathLog:
  1047. return TRY(MathObject::log_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1048. case Builtin::MathPow:
  1049. return TRY(MathObject::pow_impl(interpreter.vm(), interpreter.get(arguments[0]), interpreter.get(arguments[1])));
  1050. case Builtin::MathExp:
  1051. return TRY(MathObject::exp_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1052. case Builtin::MathCeil:
  1053. return TRY(MathObject::ceil_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1054. case Builtin::MathFloor:
  1055. return TRY(MathObject::floor_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1056. case Builtin::MathRound:
  1057. return TRY(MathObject::round_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1058. case Builtin::MathSqrt:
  1059. return TRY(MathObject::sqrt_impl(interpreter.vm(), interpreter.get(arguments[0])));
  1060. case Bytecode::Builtin::__Count:
  1061. VERIFY_NOT_REACHED();
  1062. }
  1063. VERIFY_NOT_REACHED();
  1064. }
  1065. ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) const
  1066. {
  1067. auto callee = interpreter.get(m_callee);
  1068. TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string()));
  1069. if (m_builtin.has_value()
  1070. && m_argument_count == Bytecode::builtin_argument_count(m_builtin.value())
  1071. && callee.is_object()
  1072. && interpreter.realm().get_builtin_value(m_builtin.value()) == &callee.as_object()) {
  1073. interpreter.set(dst(), TRY(dispatch_builtin_call(interpreter, m_builtin.value(), { m_arguments, m_argument_count })));
  1074. return {};
  1075. }
  1076. Vector<Value> argument_values;
  1077. argument_values.ensure_capacity(m_argument_count);
  1078. for (size_t i = 0; i < m_argument_count; ++i)
  1079. argument_values.unchecked_append(interpreter.get(m_arguments[i]));
  1080. interpreter.set(dst(), TRY(perform_call(interpreter, interpreter.get(m_this_value), call_type(), callee, argument_values)));
  1081. return {};
  1082. }
  1083. ThrowCompletionOr<void> CallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  1084. {
  1085. auto callee = interpreter.get(m_callee);
  1086. TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string()));
  1087. auto argument_values = argument_list_evaluation(interpreter.vm(), interpreter.get(arguments()));
  1088. interpreter.set(dst(), TRY(perform_call(interpreter, interpreter.get(m_this_value), call_type(), callee, move(argument_values))));
  1089. return {};
  1090. }
  1091. // 13.3.7.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  1092. ThrowCompletionOr<void> SuperCallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  1093. {
  1094. interpreter.set(dst(), TRY(super_call_with_argument_array(interpreter.vm(), interpreter.get(arguments()), m_is_synthetic)));
  1095. return {};
  1096. }
  1097. ThrowCompletionOr<void> NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  1098. {
  1099. auto& vm = interpreter.vm();
  1100. interpreter.set(dst(), new_function(vm, m_function_node, m_lhs_name, m_home_object));
  1101. return {};
  1102. }
  1103. ThrowCompletionOr<void> Return::execute_impl(Bytecode::Interpreter& interpreter) const
  1104. {
  1105. if (m_value.has_value())
  1106. interpreter.do_return(interpreter.get(*m_value));
  1107. else
  1108. interpreter.do_return(js_undefined());
  1109. return {};
  1110. }
  1111. ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  1112. {
  1113. auto& vm = interpreter.vm();
  1114. auto old_value = interpreter.get(dst());
  1115. // OPTIMIZATION: Fast path for Int32 values.
  1116. if (old_value.is_int32()) {
  1117. auto integer_value = old_value.as_i32();
  1118. if (integer_value != NumericLimits<i32>::max()) [[likely]] {
  1119. interpreter.set(dst(), Value { integer_value + 1 });
  1120. return {};
  1121. }
  1122. }
  1123. old_value = TRY(old_value.to_numeric(vm));
  1124. if (old_value.is_number())
  1125. interpreter.set(dst(), Value(old_value.as_double() + 1));
  1126. else
  1127. interpreter.set(dst(), BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 })));
  1128. return {};
  1129. }
  1130. ThrowCompletionOr<void> PostfixIncrement::execute_impl(Bytecode::Interpreter& interpreter) const
  1131. {
  1132. auto& vm = interpreter.vm();
  1133. auto old_value = interpreter.get(m_src);
  1134. // OPTIMIZATION: Fast path for Int32 values.
  1135. if (old_value.is_int32()) {
  1136. auto integer_value = old_value.as_i32();
  1137. if (integer_value != NumericLimits<i32>::max()) [[likely]] {
  1138. interpreter.set(m_dst, old_value);
  1139. interpreter.set(m_src, Value { integer_value + 1 });
  1140. return {};
  1141. }
  1142. }
  1143. old_value = TRY(old_value.to_numeric(vm));
  1144. interpreter.set(m_dst, old_value);
  1145. if (old_value.is_number())
  1146. interpreter.set(m_src, Value(old_value.as_double() + 1));
  1147. else
  1148. interpreter.set(m_src, BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 })));
  1149. return {};
  1150. }
  1151. ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  1152. {
  1153. auto& vm = interpreter.vm();
  1154. auto old_value = interpreter.get(dst());
  1155. old_value = TRY(old_value.to_numeric(vm));
  1156. if (old_value.is_number())
  1157. interpreter.set(dst(), Value(old_value.as_double() - 1));
  1158. else
  1159. interpreter.set(dst(), BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 })));
  1160. return {};
  1161. }
  1162. ThrowCompletionOr<void> PostfixDecrement::execute_impl(Bytecode::Interpreter& interpreter) const
  1163. {
  1164. auto& vm = interpreter.vm();
  1165. auto old_value = interpreter.get(m_src);
  1166. old_value = TRY(old_value.to_numeric(vm));
  1167. interpreter.set(m_dst, old_value);
  1168. if (old_value.is_number())
  1169. interpreter.set(m_src, Value(old_value.as_double() - 1));
  1170. else
  1171. interpreter.set(m_src, BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 })));
  1172. return {};
  1173. }
  1174. ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  1175. {
  1176. return throw_completion(interpreter.get(src()));
  1177. }
  1178. ThrowCompletionOr<void> ThrowIfNotObject::execute_impl(Bytecode::Interpreter& interpreter) const
  1179. {
  1180. auto& vm = interpreter.vm();
  1181. auto src = interpreter.get(m_src);
  1182. if (!src.is_object())
  1183. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, src.to_string_without_side_effects());
  1184. return {};
  1185. }
  1186. ThrowCompletionOr<void> ThrowIfNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  1187. {
  1188. auto& vm = interpreter.vm();
  1189. auto value = interpreter.get(m_src);
  1190. if (value.is_nullish())
  1191. return vm.throw_completion<TypeError>(ErrorType::NotObjectCoercible, value.to_string_without_side_effects());
  1192. return {};
  1193. }
  1194. ThrowCompletionOr<void> ThrowIfTDZ::execute_impl(Bytecode::Interpreter& interpreter) const
  1195. {
  1196. auto& vm = interpreter.vm();
  1197. auto value = interpreter.get(m_src);
  1198. if (value.is_empty())
  1199. return vm.throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, value.to_string_without_side_effects());
  1200. return {};
  1201. }
  1202. ThrowCompletionOr<void> EnterUnwindContext::execute_impl(Bytecode::Interpreter&) const
  1203. {
  1204. // Handled in the interpreter loop.
  1205. __builtin_unreachable();
  1206. }
  1207. ThrowCompletionOr<void> ScheduleJump::execute_impl(Bytecode::Interpreter&) const
  1208. {
  1209. // Handled in the interpreter loop.
  1210. __builtin_unreachable();
  1211. }
  1212. ThrowCompletionOr<void> LeaveLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  1213. {
  1214. interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
  1215. return {};
  1216. }
  1217. ThrowCompletionOr<void> LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  1218. {
  1219. interpreter.leave_unwind_context();
  1220. return {};
  1221. }
  1222. ThrowCompletionOr<void> ContinuePendingUnwind::execute_impl(Bytecode::Interpreter&) const
  1223. {
  1224. // Handled in the interpreter loop.
  1225. __builtin_unreachable();
  1226. }
  1227. ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  1228. {
  1229. auto yielded_value = interpreter.get(m_value).value_or(js_undefined());
  1230. auto object = Object::create(interpreter.realm(), nullptr);
  1231. object->define_direct_property("result", yielded_value, JS::default_attributes);
  1232. if (m_continuation_label.has_value())
  1233. // FIXME: If we get a pointer, which is not accurately representable as a double
  1234. // will cause this to explode
  1235. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes);
  1236. else
  1237. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  1238. object->define_direct_property("isAwait", Value(false), JS::default_attributes);
  1239. interpreter.do_return(object);
  1240. return {};
  1241. }
  1242. ThrowCompletionOr<void> Await::execute_impl(Bytecode::Interpreter& interpreter) const
  1243. {
  1244. auto yielded_value = interpreter.get(m_argument).value_or(js_undefined());
  1245. auto object = Object::create(interpreter.realm(), nullptr);
  1246. object->define_direct_property("result", yielded_value, JS::default_attributes);
  1247. // FIXME: If we get a pointer, which is not accurately representable as a double
  1248. // will cause this to explode
  1249. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label.block()))), JS::default_attributes);
  1250. object->define_direct_property("isAwait", Value(true), JS::default_attributes);
  1251. interpreter.do_return(object);
  1252. return {};
  1253. }
  1254. ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  1255. {
  1256. interpreter.set(dst(), TRY(get_by_value(interpreter.vm(), interpreter.get(m_base), interpreter.get(m_property))));
  1257. return {};
  1258. }
  1259. ThrowCompletionOr<void> GetByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  1260. {
  1261. auto& vm = interpreter.vm();
  1262. auto property_key_value = interpreter.get(m_property);
  1263. auto object = TRY(interpreter.get(m_base).to_object(vm));
  1264. auto property_key = TRY(property_key_value.to_property_key(vm));
  1265. interpreter.set(dst(), TRY(object->internal_get(property_key, interpreter.get(m_this_value))));
  1266. return {};
  1267. }
  1268. ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  1269. {
  1270. auto& vm = interpreter.vm();
  1271. auto value = interpreter.get(m_src);
  1272. TRY(put_by_value(vm, interpreter.get(m_base), interpreter.get(m_property), value, m_kind));
  1273. return {};
  1274. }
  1275. ThrowCompletionOr<void> PutByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  1276. {
  1277. auto& vm = interpreter.vm();
  1278. auto value = interpreter.get(m_src);
  1279. auto base = interpreter.get(m_base);
  1280. auto property_key = m_kind != PropertyKind::Spread ? TRY(interpreter.get(m_property).to_property_key(vm)) : PropertyKey {};
  1281. TRY(put_by_property_key(vm, base, interpreter.get(m_this_value), value, property_key, m_kind));
  1282. return {};
  1283. }
  1284. ThrowCompletionOr<void> DeleteByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  1285. {
  1286. auto base_value = interpreter.get(m_base);
  1287. auto property_key_value = interpreter.get(m_property);
  1288. interpreter.set(dst(), TRY(delete_by_value(interpreter, base_value, property_key_value)));
  1289. return {};
  1290. }
  1291. ThrowCompletionOr<void> DeleteByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  1292. {
  1293. auto property_key_value = interpreter.get(m_property);
  1294. auto base_value = interpreter.get(m_base);
  1295. auto this_value = interpreter.get(m_this_value);
  1296. interpreter.set(dst(), TRY(delete_by_value_with_this(interpreter, base_value, property_key_value, this_value)));
  1297. return {};
  1298. }
  1299. ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  1300. {
  1301. auto& vm = interpreter.vm();
  1302. interpreter.set(dst(), TRY(get_iterator(vm, interpreter.get(iterable()), m_hint)));
  1303. return {};
  1304. }
  1305. ThrowCompletionOr<void> GetObjectFromIteratorRecord::execute_impl(Bytecode::Interpreter& interpreter) const
  1306. {
  1307. auto& iterator_record = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object());
  1308. interpreter.set(m_object, iterator_record.iterator);
  1309. return {};
  1310. }
  1311. ThrowCompletionOr<void> GetNextMethodFromIteratorRecord::execute_impl(Bytecode::Interpreter& interpreter) const
  1312. {
  1313. auto& iterator_record = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object());
  1314. interpreter.set(m_next_method, iterator_record.next_method);
  1315. return {};
  1316. }
  1317. ThrowCompletionOr<void> GetMethod::execute_impl(Bytecode::Interpreter& interpreter) const
  1318. {
  1319. auto& vm = interpreter.vm();
  1320. auto identifier = interpreter.current_executable().get_identifier(m_property);
  1321. auto method = TRY(interpreter.get(m_object).get_method(vm, identifier));
  1322. interpreter.set(dst(), method ?: js_undefined());
  1323. return {};
  1324. }
  1325. ThrowCompletionOr<void> GetObjectPropertyIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  1326. {
  1327. interpreter.set(dst(), TRY(get_object_property_iterator(interpreter.vm(), interpreter.get(object()))));
  1328. return {};
  1329. }
  1330. ThrowCompletionOr<void> IteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1331. {
  1332. auto& vm = interpreter.vm();
  1333. auto& iterator = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object());
  1334. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1335. TRY(iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} }));
  1336. return {};
  1337. }
  1338. ThrowCompletionOr<void> AsyncIteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1339. {
  1340. auto& vm = interpreter.vm();
  1341. auto& iterator = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object());
  1342. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1343. TRY(async_iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} }));
  1344. return {};
  1345. }
  1346. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  1347. {
  1348. auto& vm = interpreter.vm();
  1349. auto& iterator_record = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object());
  1350. interpreter.set(dst(), TRY(iterator_next(vm, iterator_record)));
  1351. return {};
  1352. }
  1353. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const
  1354. {
  1355. Value super_class;
  1356. if (m_super_class.has_value())
  1357. super_class = interpreter.get(m_super_class.value());
  1358. interpreter.set(dst(), TRY(new_class(interpreter.vm(), super_class, m_class_expression, m_lhs_name)));
  1359. return {};
  1360. }
  1361. // 13.5.3.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-typeof-operator-runtime-semantics-evaluation
  1362. ThrowCompletionOr<void> TypeofVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  1363. {
  1364. auto& vm = interpreter.vm();
  1365. interpreter.set(dst(), TRY(typeof_variable(vm, interpreter.current_executable().get_identifier(m_identifier))));
  1366. return {};
  1367. }
  1368. ThrowCompletionOr<void> BlockDeclarationInstantiation::execute_impl(Bytecode::Interpreter& interpreter) const
  1369. {
  1370. auto& vm = interpreter.vm();
  1371. auto old_environment = vm.running_execution_context().lexical_environment;
  1372. interpreter.saved_lexical_environment_stack().append(old_environment);
  1373. vm.running_execution_context().lexical_environment = new_declarative_environment(*old_environment);
  1374. m_scope_node.block_declaration_instantiation(vm, vm.running_execution_context().lexical_environment);
  1375. return {};
  1376. }
  1377. ByteString Mov::to_byte_string_impl(Bytecode::Executable const& executable) const
  1378. {
  1379. return ByteString::formatted("Mov {}, {}",
  1380. format_operand("dst"sv, m_dst, executable),
  1381. format_operand("src"sv, m_src, executable));
  1382. }
  1383. ByteString NewBigInt::to_byte_string_impl(Bytecode::Executable const& executable) const
  1384. {
  1385. return ByteString::formatted("NewBigInt {}, {}",
  1386. format_operand("dst"sv, dst(), executable),
  1387. m_bigint.to_base_deprecated(10));
  1388. }
  1389. ByteString NewArray::to_byte_string_impl(Bytecode::Executable const& executable) const
  1390. {
  1391. StringBuilder builder;
  1392. builder.appendff("NewArray {}", format_operand("dst"sv, dst(), executable));
  1393. if (m_element_count != 0) {
  1394. builder.appendff(", [{}-{}]", format_operand("from"sv, m_elements[0], executable), format_operand("to"sv, m_elements[1], executable));
  1395. }
  1396. return builder.to_byte_string();
  1397. }
  1398. ByteString NewPrimitiveArray::to_byte_string_impl(Bytecode::Executable const& executable) const
  1399. {
  1400. return ByteString::formatted("NewPrimitiveArray {}, {}"sv,
  1401. format_operand("dst"sv, dst(), executable),
  1402. m_values.span());
  1403. }
  1404. ByteString ArrayAppend::to_byte_string_impl(Bytecode::Executable const& executable) const
  1405. {
  1406. return ByteString::formatted("Append {}, {}{}",
  1407. format_operand("dst"sv, dst(), executable),
  1408. format_operand("src"sv, src(), executable),
  1409. m_is_spread ? " **"sv : ""sv);
  1410. }
  1411. ByteString IteratorToArray::to_byte_string_impl(Bytecode::Executable const& executable) const
  1412. {
  1413. return ByteString::formatted("IteratorToArray {}, {}",
  1414. format_operand("dst"sv, dst(), executable),
  1415. format_operand("iterator"sv, iterator(), executable));
  1416. }
  1417. ByteString NewObject::to_byte_string_impl(Bytecode::Executable const& executable) const
  1418. {
  1419. return ByteString::formatted("NewObject {}", format_operand("dst"sv, dst(), executable));
  1420. }
  1421. ByteString NewRegExp::to_byte_string_impl(Bytecode::Executable const& executable) const
  1422. {
  1423. return ByteString::formatted("NewRegExp {}, source:{} (\"{}\") flags:{} (\"{}\")",
  1424. format_operand("dst"sv, dst(), executable),
  1425. m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  1426. }
  1427. ByteString CopyObjectExcludingProperties::to_byte_string_impl(Bytecode::Executable const& executable) const
  1428. {
  1429. StringBuilder builder;
  1430. builder.appendff("CopyObjectExcludingProperties {}, {}",
  1431. format_operand("dst"sv, dst(), executable),
  1432. format_operand("from"sv, m_from_object, executable));
  1433. if (m_excluded_names_count != 0) {
  1434. builder.append(" excluding:["sv);
  1435. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  1436. if (i != 0)
  1437. builder.append(", "sv);
  1438. builder.append(format_operand("#"sv, m_excluded_names[i], executable));
  1439. }
  1440. builder.append(']');
  1441. }
  1442. return builder.to_byte_string();
  1443. }
  1444. ByteString ConcatString::to_byte_string_impl(Bytecode::Executable const& executable) const
  1445. {
  1446. return ByteString::formatted("ConcatString {}, {}",
  1447. format_operand("dst"sv, dst(), executable),
  1448. format_operand("src"sv, src(), executable));
  1449. }
  1450. ByteString GetCalleeAndThisFromEnvironment::to_byte_string_impl(Bytecode::Executable const& executable) const
  1451. {
  1452. return ByteString::formatted("GetCalleeAndThisFromEnvironment {}, {} <- {}",
  1453. format_operand("callee"sv, m_callee, executable),
  1454. format_operand("this"sv, m_this_value, executable),
  1455. executable.identifier_table->get(m_identifier));
  1456. }
  1457. ByteString GetVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1458. {
  1459. return ByteString::formatted("GetVariable {}, {}",
  1460. format_operand("dst"sv, dst(), executable),
  1461. executable.identifier_table->get(m_identifier));
  1462. }
  1463. ByteString GetGlobal::to_byte_string_impl(Bytecode::Executable const& executable) const
  1464. {
  1465. return ByteString::formatted("GetGlobal {}, {}", format_operand("dst"sv, dst(), executable),
  1466. executable.identifier_table->get(m_identifier));
  1467. }
  1468. ByteString DeleteVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1469. {
  1470. return ByteString::formatted("DeleteVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1471. }
  1472. ByteString CreateLexicalEnvironment::to_byte_string_impl(Bytecode::Executable const&) const
  1473. {
  1474. return "CreateLexicalEnvironment"sv;
  1475. }
  1476. ByteString CreateVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1477. {
  1478. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1479. return ByteString::formatted("CreateVariable env:{} immutable:{} global:{} {} ({})", mode_string, m_is_immutable, m_is_global, m_identifier, executable.identifier_table->get(m_identifier));
  1480. }
  1481. ByteString EnterObjectEnvironment::to_byte_string_impl(Executable const& executable) const
  1482. {
  1483. return ByteString::formatted("EnterObjectEnvironment {}",
  1484. format_operand("object"sv, m_object, executable));
  1485. }
  1486. ByteString SetVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1487. {
  1488. auto initialization_mode_name = m_initialization_mode == InitializationMode::Initialize ? "Initialize" : "Set";
  1489. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1490. return ByteString::formatted("SetVariable {}, {}, env:{} init:{}",
  1491. executable.identifier_table->get(m_identifier),
  1492. format_operand("src"sv, src(), executable),
  1493. mode_string, initialization_mode_name);
  1494. }
  1495. ByteString SetLocal::to_byte_string_impl(Bytecode::Executable const& executable) const
  1496. {
  1497. return ByteString::formatted("SetLocal {}, {}",
  1498. format_operand("dst"sv, dst(), executable),
  1499. format_operand("src"sv, src(), executable));
  1500. }
  1501. static StringView property_kind_to_string(PropertyKind kind)
  1502. {
  1503. switch (kind) {
  1504. case PropertyKind::Getter:
  1505. return "getter"sv;
  1506. case PropertyKind::Setter:
  1507. return "setter"sv;
  1508. case PropertyKind::KeyValue:
  1509. return "key-value"sv;
  1510. case PropertyKind::DirectKeyValue:
  1511. return "direct-key-value"sv;
  1512. case PropertyKind::Spread:
  1513. return "spread"sv;
  1514. case PropertyKind::ProtoSetter:
  1515. return "proto-setter"sv;
  1516. }
  1517. VERIFY_NOT_REACHED();
  1518. }
  1519. ByteString PutById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1520. {
  1521. auto kind = property_kind_to_string(m_kind);
  1522. return ByteString::formatted("PutById {}, {}, {}, kind:{}",
  1523. format_operand("base"sv, m_base, executable),
  1524. executable.identifier_table->get(m_property),
  1525. format_operand("src"sv, m_src, executable),
  1526. kind);
  1527. }
  1528. ByteString PutByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1529. {
  1530. auto kind = property_kind_to_string(m_kind);
  1531. return ByteString::formatted("PutByIdWithThis {}, {}, {}, {}, kind:{}",
  1532. format_operand("base"sv, m_base, executable),
  1533. executable.identifier_table->get(m_property),
  1534. format_operand("src"sv, m_src, executable),
  1535. format_operand("this"sv, m_this_value, executable),
  1536. kind);
  1537. }
  1538. ByteString PutPrivateById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1539. {
  1540. auto kind = property_kind_to_string(m_kind);
  1541. return ByteString::formatted(
  1542. "PutPrivateById {}, {}, {}, kind:{} ",
  1543. format_operand("base"sv, m_base, executable),
  1544. executable.identifier_table->get(m_property),
  1545. format_operand("src"sv, m_src, executable),
  1546. kind);
  1547. }
  1548. ByteString GetById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1549. {
  1550. return ByteString::formatted("GetById {}, {}, {}",
  1551. format_operand("dst"sv, m_dst, executable),
  1552. format_operand("base"sv, m_base, executable),
  1553. executable.identifier_table->get(m_property));
  1554. }
  1555. ByteString GetByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1556. {
  1557. return ByteString::formatted("GetByIdWithThis {}, {}, {}, {}",
  1558. format_operand("dst"sv, m_dst, executable),
  1559. format_operand("base"sv, m_base, executable),
  1560. executable.identifier_table->get(m_property),
  1561. format_operand("this"sv, m_this_value, executable));
  1562. }
  1563. ByteString GetPrivateById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1564. {
  1565. return ByteString::formatted("GetPrivateById {}, {}, {}",
  1566. format_operand("dst"sv, m_dst, executable),
  1567. format_operand("base"sv, m_base, executable),
  1568. executable.identifier_table->get(m_property));
  1569. }
  1570. ByteString HasPrivateId::to_byte_string_impl(Bytecode::Executable const& executable) const
  1571. {
  1572. return ByteString::formatted("HasPrivateId {}, {}, {}",
  1573. format_operand("dst"sv, m_dst, executable),
  1574. format_operand("base"sv, m_base, executable),
  1575. executable.identifier_table->get(m_property));
  1576. }
  1577. ByteString DeleteById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1578. {
  1579. return ByteString::formatted("DeleteById {}, {}, {}",
  1580. format_operand("dst"sv, m_dst, executable),
  1581. format_operand("base"sv, m_base, executable),
  1582. executable.identifier_table->get(m_property));
  1583. }
  1584. ByteString DeleteByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1585. {
  1586. return ByteString::formatted("DeleteByIdWithThis {}, {}, {}, {}",
  1587. format_operand("dst"sv, m_dst, executable),
  1588. format_operand("base"sv, m_base, executable),
  1589. executable.identifier_table->get(m_property),
  1590. format_operand("this"sv, m_this_value, executable));
  1591. }
  1592. ByteString Jump::to_byte_string_impl(Bytecode::Executable const&) const
  1593. {
  1594. if (m_true_target.has_value())
  1595. return ByteString::formatted("Jump {}", *m_true_target);
  1596. return ByteString::formatted("Jump <empty>");
  1597. }
  1598. ByteString JumpIf::to_byte_string_impl(Bytecode::Executable const& executable) const
  1599. {
  1600. auto true_string = m_true_target.has_value() ? ByteString::formatted("{}", *m_true_target) : "<empty>";
  1601. auto false_string = m_false_target.has_value() ? ByteString::formatted("{}", *m_false_target) : "<empty>";
  1602. return ByteString::formatted("JumpIf {}, \033[32mtrue\033[0m:{} \033[32mfalse\033[0m:{}",
  1603. format_operand("condition"sv, m_condition, executable),
  1604. true_string, false_string);
  1605. }
  1606. ByteString JumpNullish::to_byte_string_impl(Bytecode::Executable const& executable) const
  1607. {
  1608. auto true_string = m_true_target.has_value() ? ByteString::formatted("{}", *m_true_target) : "<empty>";
  1609. auto false_string = m_false_target.has_value() ? ByteString::formatted("{}", *m_false_target) : "<empty>";
  1610. return ByteString::formatted("JumpNullish {}, null:{} nonnull:{}",
  1611. format_operand("condition"sv, m_condition, executable),
  1612. true_string, false_string);
  1613. }
  1614. ByteString JumpUndefined::to_byte_string_impl(Bytecode::Executable const& executable) const
  1615. {
  1616. auto true_string = m_true_target.has_value() ? ByteString::formatted("{}", *m_true_target) : "<empty>";
  1617. auto false_string = m_false_target.has_value() ? ByteString::formatted("{}", *m_false_target) : "<empty>";
  1618. return ByteString::formatted("JumpUndefined {}, undefined:{} defined:{}",
  1619. format_operand("condition"sv, m_condition, executable),
  1620. true_string, false_string);
  1621. }
  1622. static StringView call_type_to_string(CallType type)
  1623. {
  1624. switch (type) {
  1625. case CallType::Call:
  1626. return ""sv;
  1627. case CallType::Construct:
  1628. return " (Construct)"sv;
  1629. case CallType::DirectEval:
  1630. return " (DirectEval)"sv;
  1631. }
  1632. VERIFY_NOT_REACHED();
  1633. }
  1634. ByteString Call::to_byte_string_impl(Bytecode::Executable const& executable) const
  1635. {
  1636. auto type = call_type_to_string(m_type);
  1637. StringBuilder builder;
  1638. builder.appendff("Call{} {}, {}, {}"sv,
  1639. type,
  1640. format_operand("dst"sv, m_dst, executable),
  1641. format_operand("callee"sv, m_callee, executable),
  1642. format_operand("this"sv, m_this_value, executable));
  1643. builder.append(format_operand_list("args"sv, { m_arguments, m_argument_count }, executable));
  1644. if (m_builtin.has_value()) {
  1645. builder.appendff(", (builtin:{})", m_builtin.value());
  1646. }
  1647. if (m_expression_string.has_value()) {
  1648. builder.appendff(", `{}`", executable.get_string(m_expression_string.value()));
  1649. }
  1650. return builder.to_byte_string();
  1651. }
  1652. ByteString CallWithArgumentArray::to_byte_string_impl(Bytecode::Executable const& executable) const
  1653. {
  1654. auto type = call_type_to_string(m_type);
  1655. StringBuilder builder;
  1656. builder.appendff("CallWithArgumentArray{} {}, {}, {}, {}",
  1657. type,
  1658. format_operand("dst"sv, m_dst, executable),
  1659. format_operand("callee"sv, m_callee, executable),
  1660. format_operand("this"sv, m_this_value, executable),
  1661. format_operand("arguments"sv, m_arguments, executable));
  1662. if (m_expression_string.has_value())
  1663. builder.appendff(" ({})", executable.get_string(m_expression_string.value()));
  1664. return builder.to_byte_string();
  1665. }
  1666. ByteString SuperCallWithArgumentArray::to_byte_string_impl(Bytecode::Executable const& executable) const
  1667. {
  1668. return ByteString::formatted("SuperCallWithArgumentArray {}, {}",
  1669. format_operand("dst"sv, m_dst, executable),
  1670. format_operand("arguments"sv, m_arguments, executable));
  1671. }
  1672. ByteString NewFunction::to_byte_string_impl(Bytecode::Executable const& executable) const
  1673. {
  1674. StringBuilder builder;
  1675. builder.appendff("NewFunction {}",
  1676. format_operand("dst"sv, m_dst, executable));
  1677. if (m_function_node.has_name())
  1678. builder.appendff(" name:{}"sv, m_function_node.name());
  1679. if (m_lhs_name.has_value())
  1680. builder.appendff(" lhs_name:{}"sv, executable.get_identifier(m_lhs_name.value()));
  1681. if (m_home_object.has_value())
  1682. builder.appendff(", {}"sv, format_operand("home_object"sv, m_home_object.value(), executable));
  1683. return builder.to_byte_string();
  1684. }
  1685. ByteString NewClass::to_byte_string_impl(Bytecode::Executable const& executable) const
  1686. {
  1687. StringBuilder builder;
  1688. auto name = m_class_expression.name();
  1689. builder.appendff("NewClass {}",
  1690. format_operand("dst"sv, m_dst, executable));
  1691. if (m_super_class.has_value())
  1692. builder.appendff(", {}", format_operand("super_class"sv, *m_super_class, executable));
  1693. if (!name.is_empty())
  1694. builder.appendff(", {}", name);
  1695. if (m_lhs_name.has_value())
  1696. builder.appendff(", lhs_name:{}"sv, m_lhs_name.value());
  1697. return builder.to_byte_string();
  1698. }
  1699. ByteString Return::to_byte_string_impl(Bytecode::Executable const& executable) const
  1700. {
  1701. if (m_value.has_value())
  1702. return ByteString::formatted("Return {}", format_operand("value"sv, m_value.value(), executable));
  1703. return "Return";
  1704. }
  1705. ByteString Increment::to_byte_string_impl(Bytecode::Executable const& executable) const
  1706. {
  1707. return ByteString::formatted("Increment {}", format_operand("dst"sv, m_dst, executable));
  1708. }
  1709. ByteString PostfixIncrement::to_byte_string_impl(Bytecode::Executable const& executable) const
  1710. {
  1711. return ByteString::formatted("PostfixIncrement {}, {}",
  1712. format_operand("dst"sv, m_dst, executable),
  1713. format_operand("src"sv, m_src, executable));
  1714. }
  1715. ByteString Decrement::to_byte_string_impl(Bytecode::Executable const& executable) const
  1716. {
  1717. return ByteString::formatted("Decrement {}", format_operand("dst"sv, m_dst, executable));
  1718. }
  1719. ByteString PostfixDecrement::to_byte_string_impl(Bytecode::Executable const& executable) const
  1720. {
  1721. return ByteString::formatted("PostfixDecrement {}, {}",
  1722. format_operand("dst"sv, m_dst, executable),
  1723. format_operand("src"sv, m_src, executable));
  1724. }
  1725. ByteString Throw::to_byte_string_impl(Bytecode::Executable const& executable) const
  1726. {
  1727. return ByteString::formatted("Throw {}",
  1728. format_operand("src"sv, m_src, executable));
  1729. }
  1730. ByteString ThrowIfNotObject::to_byte_string_impl(Bytecode::Executable const& executable) const
  1731. {
  1732. return ByteString::formatted("ThrowIfNotObject {}",
  1733. format_operand("src"sv, m_src, executable));
  1734. }
  1735. ByteString ThrowIfNullish::to_byte_string_impl(Bytecode::Executable const& executable) const
  1736. {
  1737. return ByteString::formatted("ThrowIfNullish {}",
  1738. format_operand("src"sv, m_src, executable));
  1739. }
  1740. ByteString ThrowIfTDZ::to_byte_string_impl(Bytecode::Executable const& executable) const
  1741. {
  1742. return ByteString::formatted("ThrowIfTDZ {}",
  1743. format_operand("src"sv, m_src, executable));
  1744. }
  1745. ByteString EnterUnwindContext::to_byte_string_impl(Bytecode::Executable const&) const
  1746. {
  1747. return ByteString::formatted("EnterUnwindContext entry:{}", m_entry_point);
  1748. }
  1749. ByteString ScheduleJump::to_byte_string_impl(Bytecode::Executable const&) const
  1750. {
  1751. return ByteString::formatted("ScheduleJump {}", m_target);
  1752. }
  1753. ByteString LeaveLexicalEnvironment::to_byte_string_impl(Bytecode::Executable const&) const
  1754. {
  1755. return "LeaveLexicalEnvironment"sv;
  1756. }
  1757. ByteString LeaveUnwindContext::to_byte_string_impl(Bytecode::Executable const&) const
  1758. {
  1759. return "LeaveUnwindContext";
  1760. }
  1761. ByteString ContinuePendingUnwind::to_byte_string_impl(Bytecode::Executable const&) const
  1762. {
  1763. return ByteString::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  1764. }
  1765. ByteString Yield::to_byte_string_impl(Bytecode::Executable const& executable) const
  1766. {
  1767. if (m_continuation_label.has_value()) {
  1768. return ByteString::formatted("Yield continuation:@{}, {}",
  1769. m_continuation_label->block().name(),
  1770. format_operand("value"sv, m_value, executable));
  1771. }
  1772. return ByteString::formatted("Yield return {}",
  1773. format_operand("value"sv, m_value, executable));
  1774. }
  1775. ByteString Await::to_byte_string_impl(Bytecode::Executable const& executable) const
  1776. {
  1777. return ByteString::formatted("Await {}, continuation:@{}",
  1778. format_operand("argument"sv, m_argument, executable),
  1779. m_continuation_label.block().name());
  1780. }
  1781. ByteString GetByValue::to_byte_string_impl(Bytecode::Executable const& executable) const
  1782. {
  1783. return ByteString::formatted("GetByValue {}, {}, {}",
  1784. format_operand("dst"sv, m_dst, executable),
  1785. format_operand("base"sv, m_base, executable),
  1786. format_operand("property"sv, m_property, executable));
  1787. }
  1788. ByteString GetByValueWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1789. {
  1790. return ByteString::formatted("GetByValueWithThis {}, {}, {}",
  1791. format_operand("dst"sv, m_dst, executable),
  1792. format_operand("base"sv, m_base, executable),
  1793. format_operand("property"sv, m_property, executable));
  1794. }
  1795. ByteString PutByValue::to_byte_string_impl(Bytecode::Executable const& executable) const
  1796. {
  1797. auto kind = property_kind_to_string(m_kind);
  1798. return ByteString::formatted("PutByValue {}, {}, {}, kind:{}",
  1799. format_operand("base"sv, m_base, executable),
  1800. format_operand("property"sv, m_property, executable),
  1801. format_operand("src"sv, m_src, executable),
  1802. kind);
  1803. }
  1804. ByteString PutByValueWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1805. {
  1806. auto kind = property_kind_to_string(m_kind);
  1807. return ByteString::formatted("PutByValueWithThis {}, {}, {}, {}, kind:{}",
  1808. format_operand("base"sv, m_base, executable),
  1809. format_operand("property"sv, m_property, executable),
  1810. format_operand("src"sv, m_src, executable),
  1811. format_operand("this"sv, m_this_value, executable),
  1812. kind);
  1813. }
  1814. ByteString DeleteByValue::to_byte_string_impl(Bytecode::Executable const& executable) const
  1815. {
  1816. return ByteString::formatted("DeleteByValue {}, {}, {}",
  1817. format_operand("dst"sv, dst(), executable),
  1818. format_operand("base"sv, m_base, executable),
  1819. format_operand("property"sv, m_property, executable));
  1820. }
  1821. ByteString DeleteByValueWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1822. {
  1823. return ByteString::formatted("DeleteByValueWithThis {}, {}, {}, {}",
  1824. format_operand("dst"sv, dst(), executable),
  1825. format_operand("base"sv, m_base, executable),
  1826. format_operand("property"sv, m_property, executable),
  1827. format_operand("this"sv, m_this_value, executable));
  1828. }
  1829. ByteString GetIterator::to_byte_string_impl(Executable const& executable) const
  1830. {
  1831. auto hint = m_hint == IteratorHint::Sync ? "sync" : "async";
  1832. return ByteString::formatted("GetIterator {}, {}, hint:{}",
  1833. format_operand("dst"sv, m_dst, executable),
  1834. format_operand("iterable"sv, m_iterable, executable),
  1835. hint);
  1836. }
  1837. ByteString GetMethod::to_byte_string_impl(Bytecode::Executable const& executable) const
  1838. {
  1839. return ByteString::formatted("GetMethod {}, {}, {}",
  1840. format_operand("dst"sv, m_dst, executable),
  1841. format_operand("object"sv, m_object, executable),
  1842. executable.identifier_table->get(m_property));
  1843. }
  1844. ByteString GetObjectPropertyIterator::to_byte_string_impl(Bytecode::Executable const& executable) const
  1845. {
  1846. return ByteString::formatted("GetObjectPropertyIterator {}, {}",
  1847. format_operand("dst"sv, dst(), executable),
  1848. format_operand("object"sv, object(), executable));
  1849. }
  1850. ByteString IteratorClose::to_byte_string_impl(Bytecode::Executable const& executable) const
  1851. {
  1852. if (!m_completion_value.has_value())
  1853. return ByteString::formatted("IteratorClose {}, completion_type={} completion_value=<empty>",
  1854. format_operand("iterator_record"sv, m_iterator_record, executable),
  1855. to_underlying(m_completion_type));
  1856. auto completion_value_string = m_completion_value->to_string_without_side_effects();
  1857. return ByteString::formatted("IteratorClose {}, completion_type={} completion_value={}",
  1858. format_operand("iterator_record"sv, m_iterator_record, executable),
  1859. to_underlying(m_completion_type), completion_value_string);
  1860. }
  1861. ByteString AsyncIteratorClose::to_byte_string_impl(Bytecode::Executable const& executable) const
  1862. {
  1863. if (!m_completion_value.has_value()) {
  1864. return ByteString::formatted("AsyncIteratorClose {}, completion_type:{} completion_value:<empty>",
  1865. format_operand("iterator_record"sv, m_iterator_record, executable),
  1866. to_underlying(m_completion_type));
  1867. }
  1868. return ByteString::formatted("AsyncIteratorClose {}, completion_type:{}, completion_value:{}",
  1869. format_operand("iterator_record"sv, m_iterator_record, executable),
  1870. to_underlying(m_completion_type), m_completion_value);
  1871. }
  1872. ByteString IteratorNext::to_byte_string_impl(Executable const& executable) const
  1873. {
  1874. return ByteString::formatted("IteratorNext {}, {}",
  1875. format_operand("dst"sv, m_dst, executable),
  1876. format_operand("iterator_record"sv, m_iterator_record, executable));
  1877. }
  1878. ByteString ResolveThisBinding::to_byte_string_impl(Bytecode::Executable const& executable) const
  1879. {
  1880. return ByteString::formatted("ResolveThisBinding {}", format_operand("dst"sv, m_dst, executable));
  1881. }
  1882. ByteString ResolveSuperBase::to_byte_string_impl(Bytecode::Executable const& executable) const
  1883. {
  1884. return ByteString::formatted("ResolveSuperBase {}",
  1885. format_operand("dst"sv, m_dst, executable));
  1886. }
  1887. ByteString GetNewTarget::to_byte_string_impl(Bytecode::Executable const& executable) const
  1888. {
  1889. return ByteString::formatted("GetNewTarget {}", format_operand("dst"sv, m_dst, executable));
  1890. }
  1891. ByteString GetImportMeta::to_byte_string_impl(Bytecode::Executable const& executable) const
  1892. {
  1893. return ByteString::formatted("GetImportMeta {}", format_operand("dst"sv, m_dst, executable));
  1894. }
  1895. ByteString TypeofVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1896. {
  1897. return ByteString::formatted("TypeofVariable {}, {}",
  1898. format_operand("dst"sv, m_dst, executable),
  1899. executable.identifier_table->get(m_identifier));
  1900. }
  1901. ByteString BlockDeclarationInstantiation::to_byte_string_impl(Bytecode::Executable const&) const
  1902. {
  1903. return "BlockDeclarationInstantiation"sv;
  1904. }
  1905. ByteString ImportCall::to_byte_string_impl(Bytecode::Executable const& executable) const
  1906. {
  1907. return ByteString::formatted("ImportCall {}, {}, {}",
  1908. format_operand("dst"sv, m_dst, executable),
  1909. format_operand("specifier"sv, m_specifier, executable),
  1910. format_operand("options"sv, m_options, executable));
  1911. }
  1912. ByteString Catch::to_byte_string_impl(Bytecode::Executable const& executable) const
  1913. {
  1914. return ByteString::formatted("Catch {}",
  1915. format_operand("dst"sv, m_dst, executable));
  1916. }
  1917. ByteString GetObjectFromIteratorRecord::to_byte_string_impl(Bytecode::Executable const& executable) const
  1918. {
  1919. return ByteString::formatted("GetObjectFromIteratorRecord {}, {}",
  1920. format_operand("object"sv, m_object, executable),
  1921. format_operand("iterator_record"sv, m_iterator_record, executable));
  1922. }
  1923. ByteString GetNextMethodFromIteratorRecord::to_byte_string_impl(Bytecode::Executable const& executable) const
  1924. {
  1925. return ByteString::formatted("GetNextMethodFromIteratorRecord {}, {}",
  1926. format_operand("next_method"sv, m_next_method, executable),
  1927. format_operand("iterator_record"sv, m_iterator_record, executable));
  1928. }
  1929. ByteString End::to_byte_string_impl(Bytecode::Executable const& executable) const
  1930. {
  1931. return ByteString::formatted("End {}", format_operand("value"sv, m_value, executable));
  1932. }
  1933. ByteString Dump::to_byte_string_impl(Bytecode::Executable const& executable) const
  1934. {
  1935. return ByteString::formatted("Dump '{}', {}", m_text,
  1936. format_operand("value"sv, m_value, executable));
  1937. }
  1938. }