Interpreter.cpp 111 KB

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