Interpreter.cpp 111 KB

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