Interpreter.cpp 90 KB

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