Interpreter.cpp 90 KB

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