Interpreter.cpp 98 KB

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