Interpreter.cpp 89 KB

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