Interpreter.cpp 99 KB

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