Interpreter.cpp 109 KB

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