Interpreter.cpp 111 KB

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