Interpreter.cpp 113 KB

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