Interpreter.cpp 93 KB

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