Interpreter.cpp 89 KB

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