Interpreter.cpp 88 KB

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