Interpreter.cpp 112 KB

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