Interpreter.cpp 92 KB

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