Interpreter.cpp 116 KB

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