Interpreter.cpp 94 KB

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