Interpreter.cpp 90 KB

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