Interpreter.cpp 89 KB

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