Interpreter.cpp 88 KB

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