Interpreter.cpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  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/JIT/Compiler.h>
  18. #include <LibJS/JIT/NativeExecutable.h>
  19. #include <LibJS/Runtime/AbstractOperations.h>
  20. #include <LibJS/Runtime/Array.h>
  21. #include <LibJS/Runtime/BigInt.h>
  22. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  23. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  24. #include <LibJS/Runtime/Environment.h>
  25. #include <LibJS/Runtime/FunctionEnvironment.h>
  26. #include <LibJS/Runtime/GlobalEnvironment.h>
  27. #include <LibJS/Runtime/GlobalObject.h>
  28. #include <LibJS/Runtime/Iterator.h>
  29. #include <LibJS/Runtime/MathObject.h>
  30. #include <LibJS/Runtime/NativeFunction.h>
  31. #include <LibJS/Runtime/ObjectEnvironment.h>
  32. #include <LibJS/Runtime/Realm.h>
  33. #include <LibJS/Runtime/Reference.h>
  34. #include <LibJS/Runtime/RegExpObject.h>
  35. #include <LibJS/Runtime/Value.h>
  36. #include <LibJS/Runtime/ValueInlines.h>
  37. #include <LibJS/SourceTextModule.h>
  38. namespace JS::Bytecode {
  39. bool g_dump_bytecode = false;
  40. NonnullOwnPtr<CallFrame> CallFrame::create(size_t register_count)
  41. {
  42. size_t allocation_size = sizeof(CallFrame) + sizeof(Value) * register_count;
  43. auto* memory = malloc(allocation_size);
  44. VERIFY(memory);
  45. auto call_frame = adopt_own(*new (memory) CallFrame);
  46. call_frame->register_count = register_count;
  47. for (auto i = 0u; i < register_count; ++i)
  48. new (&call_frame->register_values[i]) Value();
  49. return call_frame;
  50. }
  51. Interpreter::Interpreter(VM& vm)
  52. : m_vm(vm)
  53. {
  54. }
  55. Interpreter::~Interpreter()
  56. {
  57. }
  58. void Interpreter::visit_edges(Cell::Visitor& visitor)
  59. {
  60. for (auto& frame : m_call_frames) {
  61. frame.visit([&](auto& value) { value->visit_edges(visitor); });
  62. }
  63. }
  64. // 16.1.6 ScriptEvaluation ( scriptRecord ), https://tc39.es/ecma262/#sec-runtime-semantics-scriptevaluation
  65. ThrowCompletionOr<Value> Interpreter::run(Script& script_record, JS::GCPtr<Environment> lexical_environment_override)
  66. {
  67. auto& vm = this->vm();
  68. // 1. Let globalEnv be scriptRecord.[[Realm]].[[GlobalEnv]].
  69. auto& global_environment = script_record.realm().global_environment();
  70. // 2. Let scriptContext be a new ECMAScript code execution context.
  71. auto script_context = ExecutionContext::create(vm.heap());
  72. // 3. Set the Function of scriptContext to null.
  73. // NOTE: This was done during execution context construction.
  74. // 4. Set the Realm of scriptContext to scriptRecord.[[Realm]].
  75. script_context->realm = &script_record.realm();
  76. // 5. Set the ScriptOrModule of scriptContext to scriptRecord.
  77. script_context->script_or_module = NonnullGCPtr<Script>(script_record);
  78. // 6. Set the VariableEnvironment of scriptContext to globalEnv.
  79. script_context->variable_environment = &global_environment;
  80. // 7. Set the LexicalEnvironment of scriptContext to globalEnv.
  81. script_context->lexical_environment = &global_environment;
  82. // Non-standard: Override the lexical environment if requested.
  83. if (lexical_environment_override)
  84. script_context->lexical_environment = lexical_environment_override;
  85. // 8. Set the PrivateEnvironment of scriptContext to null.
  86. // NOTE: This isn't in the spec, but we require it.
  87. script_context->is_strict_mode = script_record.parse_node().is_strict_mode();
  88. // FIXME: 9. Suspend the currently running execution context.
  89. // 10. Push scriptContext onto the execution context stack; scriptContext is now the running execution context.
  90. TRY(vm.push_execution_context(*script_context, {}));
  91. // 11. Let script be scriptRecord.[[ECMAScriptCode]].
  92. auto& script = script_record.parse_node();
  93. // 12. Let result be Completion(GlobalDeclarationInstantiation(script, globalEnv)).
  94. auto instantiation_result = script.global_declaration_instantiation(vm, global_environment);
  95. Completion result = instantiation_result.is_throw_completion() ? instantiation_result.throw_completion() : normal_completion({});
  96. // 13. If result.[[Type]] is normal, then
  97. if (result.type() == Completion::Type::Normal) {
  98. auto executable_result = JS::Bytecode::Generator::generate(vm, script);
  99. if (executable_result.is_error()) {
  100. if (auto error_string = executable_result.error().to_string(); error_string.is_error())
  101. result = vm.template throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory));
  102. else if (error_string = String::formatted("TODO({})", error_string.value()); error_string.is_error())
  103. result = vm.template throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory));
  104. else
  105. result = JS::throw_completion(JS::InternalError::create(realm(), error_string.release_value()));
  106. } else {
  107. auto executable = executable_result.release_value();
  108. if (g_dump_bytecode)
  109. executable->dump();
  110. // a. Set result to the result of evaluating script.
  111. auto result_or_error = run_and_return_frame(*executable, nullptr);
  112. if (result_or_error.value.is_error())
  113. result = result_or_error.value.release_error();
  114. else
  115. result = result_or_error.frame->registers()[0];
  116. }
  117. }
  118. // 14. If result.[[Type]] is normal and result.[[Value]] is empty, then
  119. if (result.type() == Completion::Type::Normal && !result.value().has_value()) {
  120. // a. Set result to NormalCompletion(undefined).
  121. result = normal_completion(js_undefined());
  122. }
  123. // FIXME: 15. Suspend scriptContext and remove it from the execution context stack.
  124. vm.pop_execution_context();
  125. // 16. Assert: The execution context stack is not empty.
  126. VERIFY(!vm.execution_context_stack().is_empty());
  127. // FIXME: 17. Resume the context that is now on the top of the execution context stack as the running execution context.
  128. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  129. // in which case this is a no-op.
  130. // 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.
  131. // https://tc39.es/ecma262/#sec-jobs for jobs and https://tc39.es/ecma262/#_ref_3508 for ClearKeptObjects
  132. // finish_execution_generation is particularly an issue for LibWeb, as the HTML spec wants to run it specifically after performing a microtask checkpoint.
  133. // The promise and registry cleanup queues don't cause LibWeb an issue, as LibWeb overrides the hooks that push onto these queues.
  134. vm.run_queued_promise_jobs();
  135. vm.run_queued_finalization_registry_cleanup_jobs();
  136. vm.finish_execution_generation();
  137. // 18. Return ? result.
  138. if (result.is_abrupt()) {
  139. VERIFY(result.type() == Completion::Type::Throw);
  140. return result.release_error();
  141. }
  142. VERIFY(result.value().has_value());
  143. return *result.value();
  144. }
  145. ThrowCompletionOr<Value> Interpreter::run(SourceTextModule& module)
  146. {
  147. // FIXME: This is not a entry point as defined in the spec, but is convenient.
  148. // To avoid work we use link_and_eval_module however that can already be
  149. // dangerous if the vm loaded other modules.
  150. auto& vm = this->vm();
  151. TRY(vm.link_and_eval_module(Badge<Bytecode::Interpreter> {}, module));
  152. vm.run_queued_promise_jobs();
  153. vm.run_queued_finalization_registry_cleanup_jobs();
  154. return js_undefined();
  155. }
  156. void Interpreter::run_bytecode()
  157. {
  158. auto* locals = vm().running_execution_context().locals.data();
  159. auto* registers = this->registers().data();
  160. auto& accumulator = this->accumulator();
  161. for (;;) {
  162. start:
  163. auto pc = InstructionStreamIterator { m_current_block->instruction_stream(), m_current_executable };
  164. TemporaryChange temp_change { m_pc, Optional<InstructionStreamIterator&>(pc) };
  165. bool will_return = false;
  166. bool will_yield = false;
  167. ThrowCompletionOr<void> result;
  168. while (!pc.at_end()) {
  169. auto& instruction = *pc;
  170. switch (instruction.type()) {
  171. case Instruction::Type::GetLocal: {
  172. auto& local = locals[static_cast<Op::GetLocal const&>(instruction).index()];
  173. if (local.is_empty()) {
  174. auto const& variable_name = vm().running_execution_context().function->local_variables_names()[static_cast<Op::GetLocal const&>(instruction).index()];
  175. result = vm().throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, variable_name);
  176. break;
  177. }
  178. accumulator = local;
  179. break;
  180. }
  181. case Instruction::Type::SetLocal:
  182. locals[static_cast<Op::SetLocal const&>(instruction).index()] = accumulator;
  183. break;
  184. case Instruction::Type::Load:
  185. accumulator = registers[static_cast<Op::Load const&>(instruction).src().index()];
  186. break;
  187. case Instruction::Type::Store:
  188. registers[static_cast<Op::Store const&>(instruction).dst().index()] = accumulator;
  189. break;
  190. case Instruction::Type::LoadImmediate:
  191. accumulator = static_cast<Op::LoadImmediate const&>(instruction).value();
  192. break;
  193. case Instruction::Type::Jump:
  194. m_current_block = &static_cast<Op::Jump const&>(instruction).true_target()->block();
  195. goto start;
  196. case Instruction::Type::JumpConditional:
  197. if (accumulator.to_boolean())
  198. m_current_block = &static_cast<Op::Jump const&>(instruction).true_target()->block();
  199. else
  200. m_current_block = &static_cast<Op::Jump const&>(instruction).false_target()->block();
  201. goto start;
  202. case Instruction::Type::JumpNullish:
  203. if (accumulator.is_nullish())
  204. m_current_block = &static_cast<Op::Jump const&>(instruction).true_target()->block();
  205. else
  206. m_current_block = &static_cast<Op::Jump const&>(instruction).false_target()->block();
  207. goto start;
  208. case Instruction::Type::JumpUndefined:
  209. if (accumulator.is_undefined())
  210. m_current_block = &static_cast<Op::Jump const&>(instruction).true_target()->block();
  211. else
  212. m_current_block = &static_cast<Op::Jump const&>(instruction).false_target()->block();
  213. goto start;
  214. case Instruction::Type::EnterUnwindContext:
  215. enter_unwind_context();
  216. m_current_block = &static_cast<Op::EnterUnwindContext const&>(instruction).entry_point().block();
  217. goto start;
  218. case Instruction::Type::ContinuePendingUnwind: {
  219. if (auto exception = reg(Register::exception()); !exception.is_empty()) {
  220. result = throw_completion(exception);
  221. break;
  222. }
  223. if (!saved_return_value().is_empty()) {
  224. do_return(saved_return_value());
  225. break;
  226. }
  227. auto const* old_scheduled_jump = call_frame().previously_scheduled_jumps.take_last();
  228. if (m_scheduled_jump) {
  229. // FIXME: If we `break` or `continue` in the finally, we need to clear
  230. // this field
  231. // Same goes for popping an old_scheduled_jump form the stack
  232. m_current_block = exchange(m_scheduled_jump, nullptr);
  233. } else {
  234. m_current_block = &static_cast<Op::ContinuePendingUnwind const&>(instruction).resume_target().block();
  235. // set the scheduled jump to the old value if we continue
  236. // where we left it
  237. m_scheduled_jump = old_scheduled_jump;
  238. }
  239. goto start;
  240. }
  241. case Instruction::Type::ScheduleJump: {
  242. m_scheduled_jump = &static_cast<Op::ScheduleJump const&>(instruction).target().block();
  243. auto const* finalizer = m_current_block->finalizer();
  244. VERIFY(finalizer);
  245. m_current_block = finalizer;
  246. goto start;
  247. }
  248. default:
  249. result = instruction.execute(*this);
  250. break;
  251. }
  252. if (result.is_error()) [[unlikely]] {
  253. reg(Register::exception()) = *result.throw_completion().value();
  254. m_scheduled_jump = {};
  255. auto const* handler = m_current_block->handler();
  256. auto const* finalizer = m_current_block->finalizer();
  257. if (!handler && !finalizer)
  258. return;
  259. auto& unwind_context = unwind_contexts().last();
  260. VERIFY(unwind_context.executable == m_current_executable);
  261. if (handler) {
  262. m_current_block = handler;
  263. goto start;
  264. }
  265. if (finalizer) {
  266. m_current_block = finalizer;
  267. // If an exception was thrown inside the corresponding `catch` block, we need to rethrow it
  268. // from the `finally` block. But if the exception is from the `try` block, and has already been
  269. // handled by `catch`, we swallow it.
  270. if (!unwind_context.handler_called)
  271. reg(Register::exception()) = {};
  272. goto start;
  273. }
  274. // 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.
  275. // If you run into this, you probably forgot to remove the current unwind_context somewhere.
  276. VERIFY_NOT_REACHED();
  277. }
  278. if (!reg(Register::return_value()).is_empty()) {
  279. will_return = true;
  280. // Note: A `yield` statement will not go through a finally statement,
  281. // hence we need to set a flag to not do so,
  282. // but we generate a Yield Operation in the case of returns in
  283. // generators as well, so we need to check if it will actually
  284. // continue or is a `return` in disguise
  285. will_yield = (instruction.type() == Instruction::Type::Yield && static_cast<Op::Yield const&>(instruction).continuation().has_value()) || instruction.type() == Instruction::Type::Await;
  286. break;
  287. }
  288. ++pc;
  289. }
  290. if (auto const* finalizer = m_current_block->finalizer(); finalizer && !will_yield) {
  291. auto& unwind_context = unwind_contexts().last();
  292. VERIFY(unwind_context.executable == m_current_executable);
  293. reg(Register::saved_return_value()) = reg(Register::return_value());
  294. reg(Register::return_value()) = {};
  295. m_current_block = finalizer;
  296. // the unwind_context will be pop'ed when entering the finally block
  297. continue;
  298. }
  299. if (pc.at_end())
  300. break;
  301. if (will_return)
  302. break;
  303. }
  304. }
  305. Interpreter::ValueAndFrame Interpreter::run_and_return_frame(Executable& executable, BasicBlock const* entry_point, CallFrame* in_frame)
  306. {
  307. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable);
  308. TemporaryChange restore_executable { m_current_executable, &executable };
  309. TemporaryChange restore_saved_jump { m_scheduled_jump, static_cast<BasicBlock const*>(nullptr) };
  310. VERIFY(!vm().execution_context_stack().is_empty());
  311. TemporaryChange restore_current_block { m_current_block, entry_point ?: executable.basic_blocks.first() };
  312. if (in_frame)
  313. push_call_frame(in_frame);
  314. else
  315. push_call_frame(CallFrame::create(executable.number_of_registers));
  316. vm().execution_context_stack().last()->executable = &executable;
  317. if (auto native_executable = executable.get_or_create_native_executable()) {
  318. auto block_index = 0;
  319. if (entry_point)
  320. block_index = executable.basic_blocks.find_first_index_if([&](auto const& block) { return block.ptr() == entry_point; }).value();
  321. native_executable->run(vm(), block_index);
  322. #if 0
  323. for (size_t i = 0; i < vm().running_execution_context().local_variables.size(); ++i) {
  324. dbgln("%{}: {}", i, vm().running_execution_context().local_variables[i]);
  325. }
  326. #endif
  327. } else {
  328. run_bytecode();
  329. }
  330. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable);
  331. if constexpr (JS_BYTECODE_DEBUG) {
  332. for (size_t i = 0; i < registers().size(); ++i) {
  333. String value_string;
  334. if (registers()[i].is_empty())
  335. value_string = "(empty)"_string;
  336. else
  337. value_string = registers()[i].to_string_without_side_effects();
  338. dbgln("[{:3}] {}", i, value_string);
  339. }
  340. }
  341. auto return_value = js_undefined();
  342. if (!reg(Register::return_value()).is_empty())
  343. return_value = reg(Register::return_value());
  344. else if (!reg(Register::saved_return_value()).is_empty())
  345. return_value = reg(Register::saved_return_value());
  346. auto exception = reg(Register::exception());
  347. auto frame = pop_call_frame();
  348. // NOTE: The return value from a called function is put into $0 in the caller context.
  349. if (!m_call_frames.is_empty())
  350. call_frame().registers()[0] = return_value;
  351. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  352. // in which case this is a no-op.
  353. vm().run_queued_promise_jobs();
  354. vm().finish_execution_generation();
  355. if (!exception.is_empty()) {
  356. if (auto* call_frame = frame.get_pointer<NonnullOwnPtr<CallFrame>>())
  357. return { throw_completion(exception), move(*call_frame) };
  358. return { throw_completion(exception), nullptr };
  359. }
  360. if (auto* call_frame = frame.get_pointer<NonnullOwnPtr<CallFrame>>())
  361. return { return_value, move(*call_frame) };
  362. return { return_value, nullptr };
  363. }
  364. void Interpreter::enter_unwind_context()
  365. {
  366. unwind_contexts().empend(
  367. m_current_executable,
  368. vm().running_execution_context().lexical_environment);
  369. call_frame().previously_scheduled_jumps.append(m_scheduled_jump);
  370. m_scheduled_jump = nullptr;
  371. }
  372. void Interpreter::leave_unwind_context()
  373. {
  374. unwind_contexts().take_last();
  375. }
  376. void Interpreter::catch_exception()
  377. {
  378. accumulator() = reg(Register::exception());
  379. reg(Register::exception()) = {};
  380. auto& context = unwind_contexts().last();
  381. VERIFY(!context.handler_called);
  382. VERIFY(context.executable == &current_executable());
  383. context.handler_called = true;
  384. vm().running_execution_context().lexical_environment = context.lexical_environment;
  385. }
  386. void Interpreter::enter_object_environment(Object& object)
  387. {
  388. auto& old_environment = vm().running_execution_context().lexical_environment;
  389. saved_lexical_environment_stack().append(old_environment);
  390. vm().running_execution_context().lexical_environment = new_object_environment(object, true, old_environment);
  391. }
  392. ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM& vm, ASTNode const& node, FunctionKind kind, DeprecatedFlyString const& name)
  393. {
  394. auto executable_result = Bytecode::Generator::generate(vm, node, kind);
  395. if (executable_result.is_error())
  396. return vm.throw_completion<InternalError>(ErrorType::NotImplemented, TRY_OR_THROW_OOM(vm, executable_result.error().to_string()));
  397. auto bytecode_executable = executable_result.release_value();
  398. bytecode_executable->name = name;
  399. if (Bytecode::g_dump_bytecode)
  400. bytecode_executable->dump();
  401. return bytecode_executable;
  402. }
  403. Realm& Interpreter::realm()
  404. {
  405. return *m_vm.current_realm();
  406. }
  407. void Interpreter::push_call_frame(Variant<NonnullOwnPtr<CallFrame>, CallFrame*> frame)
  408. {
  409. m_call_frames.append(move(frame));
  410. m_current_call_frame = this->call_frame().registers();
  411. reg(Register::return_value()) = {};
  412. }
  413. Variant<NonnullOwnPtr<CallFrame>, CallFrame*> Interpreter::pop_call_frame()
  414. {
  415. auto frame = m_call_frames.take_last();
  416. m_current_call_frame = m_call_frames.is_empty() ? Span<Value> {} : this->call_frame().registers();
  417. return frame;
  418. }
  419. }
  420. namespace JS::Bytecode {
  421. ByteString Instruction::to_byte_string(Bytecode::Executable const& executable) const
  422. {
  423. #define __BYTECODE_OP(op) \
  424. case Instruction::Type::op: \
  425. return static_cast<Bytecode::Op::op const&>(*this).to_byte_string_impl(executable);
  426. switch (type()) {
  427. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  428. default:
  429. VERIFY_NOT_REACHED();
  430. }
  431. #undef __BYTECODE_OP
  432. }
  433. }
  434. namespace JS::Bytecode::Op {
  435. ThrowCompletionOr<void> Load::execute_impl(Bytecode::Interpreter&) const
  436. {
  437. // Handled in the interpreter loop.
  438. __builtin_unreachable();
  439. }
  440. ThrowCompletionOr<void> LoadImmediate::execute_impl(Bytecode::Interpreter&) const
  441. {
  442. // Handled in the interpreter loop.
  443. __builtin_unreachable();
  444. }
  445. ThrowCompletionOr<void> Store::execute_impl(Bytecode::Interpreter&) const
  446. {
  447. // Handled in the interpreter loop.
  448. __builtin_unreachable();
  449. }
  450. static ThrowCompletionOr<Value> loosely_inequals(VM& vm, Value src1, Value src2)
  451. {
  452. return Value(!TRY(is_loosely_equal(vm, src1, src2)));
  453. }
  454. static ThrowCompletionOr<Value> loosely_equals(VM& vm, Value src1, Value src2)
  455. {
  456. return Value(TRY(is_loosely_equal(vm, src1, src2)));
  457. }
  458. static ThrowCompletionOr<Value> strict_inequals(VM&, Value src1, Value src2)
  459. {
  460. return Value(!is_strictly_equal(src1, src2));
  461. }
  462. static ThrowCompletionOr<Value> strict_equals(VM&, Value src1, Value src2)
  463. {
  464. return Value(is_strictly_equal(src1, src2));
  465. }
  466. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  467. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  468. { \
  469. auto& vm = interpreter.vm(); \
  470. auto lhs = interpreter.reg(m_lhs_reg); \
  471. auto rhs = interpreter.accumulator(); \
  472. interpreter.accumulator() = TRY(op_snake_case(vm, lhs, rhs)); \
  473. return {}; \
  474. } \
  475. ByteString OpTitleCase::to_byte_string_impl(Bytecode::Executable const&) const \
  476. { \
  477. return ByteString::formatted(#OpTitleCase " {}", m_lhs_reg); \
  478. }
  479. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  480. static ThrowCompletionOr<Value> not_(VM&, Value value)
  481. {
  482. return Value(!value.to_boolean());
  483. }
  484. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  485. {
  486. return PrimitiveString::create(vm, value.typeof());
  487. }
  488. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  489. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  490. { \
  491. auto& vm = interpreter.vm(); \
  492. interpreter.accumulator() = TRY(op_snake_case(vm, interpreter.accumulator())); \
  493. return {}; \
  494. } \
  495. ByteString OpTitleCase::to_byte_string_impl(Bytecode::Executable const&) const \
  496. { \
  497. return #OpTitleCase; \
  498. }
  499. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  500. ThrowCompletionOr<void> NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
  501. {
  502. auto& vm = interpreter.vm();
  503. interpreter.accumulator() = BigInt::create(vm, m_bigint);
  504. return {};
  505. }
  506. ThrowCompletionOr<void> NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  507. {
  508. auto array = MUST(Array::create(interpreter.realm(), 0));
  509. for (size_t i = 0; i < m_element_count; i++) {
  510. auto& value = interpreter.reg(Register(m_elements[0].index() + i));
  511. array->indexed_properties().put(i, value, default_attributes);
  512. }
  513. interpreter.accumulator() = array;
  514. return {};
  515. }
  516. ThrowCompletionOr<void> NewPrimitiveArray::execute_impl(Bytecode::Interpreter& interpreter) const
  517. {
  518. auto array = MUST(Array::create(interpreter.realm(), 0));
  519. for (size_t i = 0; i < m_values.size(); i++)
  520. array->indexed_properties().put(i, m_values[i], default_attributes);
  521. interpreter.accumulator() = array;
  522. return {};
  523. }
  524. ThrowCompletionOr<void> Append::execute_impl(Bytecode::Interpreter& interpreter) const
  525. {
  526. return append(interpreter.vm(), interpreter.reg(m_lhs), interpreter.accumulator(), m_is_spread);
  527. }
  528. ThrowCompletionOr<void> ImportCall::execute_impl(Bytecode::Interpreter& interpreter) const
  529. {
  530. auto& vm = interpreter.vm();
  531. auto specifier = interpreter.reg(m_specifier);
  532. auto options_value = interpreter.reg(m_options);
  533. interpreter.accumulator() = TRY(perform_import_call(vm, specifier, options_value));
  534. return {};
  535. }
  536. ThrowCompletionOr<void> IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const
  537. {
  538. interpreter.accumulator() = TRY(iterator_to_array(interpreter.vm(), interpreter.accumulator()));
  539. return {};
  540. }
  541. ThrowCompletionOr<void> NewString::execute_impl(Bytecode::Interpreter& interpreter) const
  542. {
  543. interpreter.accumulator() = PrimitiveString::create(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  544. return {};
  545. }
  546. ThrowCompletionOr<void> NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  547. {
  548. auto& vm = interpreter.vm();
  549. auto& realm = *vm.current_realm();
  550. interpreter.accumulator() = Object::create(realm, realm.intrinsics().object_prototype());
  551. return {};
  552. }
  553. ThrowCompletionOr<void> NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
  554. {
  555. interpreter.accumulator() = new_regexp(
  556. interpreter.vm(),
  557. interpreter.current_executable().regex_table->get(m_regex_index),
  558. interpreter.current_executable().get_string(m_source_index),
  559. interpreter.current_executable().get_string(m_flags_index));
  560. return {};
  561. }
  562. #define JS_DEFINE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  563. ThrowCompletionOr<void> New##ErrorName::execute_impl(Bytecode::Interpreter& interpreter) const \
  564. { \
  565. auto& vm = interpreter.vm(); \
  566. auto& realm = *vm.current_realm(); \
  567. interpreter.accumulator() = ErrorName::create(realm, interpreter.current_executable().get_string(m_error_string)); \
  568. return {}; \
  569. } \
  570. ByteString New##ErrorName::to_byte_string_impl(Bytecode::Executable const& executable) const \
  571. { \
  572. return ByteString::formatted("New" #ErrorName " {} (\"{}\")", m_error_string, executable.string_table->get(m_error_string)); \
  573. }
  574. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DEFINE_NEW_BUILTIN_ERROR_OP)
  575. ThrowCompletionOr<void> CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const
  576. {
  577. auto& vm = interpreter.vm();
  578. auto& realm = *vm.current_realm();
  579. auto from_object = interpreter.reg(m_from_object);
  580. auto to_object = Object::create(realm, realm.intrinsics().object_prototype());
  581. HashTable<PropertyKey> excluded_names;
  582. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  583. excluded_names.set(TRY(interpreter.reg(m_excluded_names[i]).to_property_key(vm)));
  584. }
  585. TRY(to_object->copy_data_properties(vm, from_object, excluded_names));
  586. interpreter.accumulator() = to_object;
  587. return {};
  588. }
  589. ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  590. {
  591. auto& vm = interpreter.vm();
  592. auto string = TRY(interpreter.accumulator().to_primitive_string(vm));
  593. interpreter.reg(m_lhs) = PrimitiveString::create(vm, interpreter.reg(m_lhs).as_string(), string);
  594. return {};
  595. }
  596. ThrowCompletionOr<void> GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  597. {
  598. interpreter.accumulator() = TRY(get_variable(
  599. interpreter,
  600. interpreter.current_executable().get_identifier(m_identifier),
  601. interpreter.current_executable().environment_variable_caches[m_cache_index]));
  602. return {};
  603. }
  604. ThrowCompletionOr<void> GetCalleeAndThisFromEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  605. {
  606. auto callee_and_this = TRY(get_callee_and_this_from_environment(
  607. interpreter,
  608. interpreter.current_executable().get_identifier(m_identifier),
  609. interpreter.current_executable().environment_variable_caches[m_cache_index]));
  610. interpreter.reg(m_callee_reg) = callee_and_this.callee;
  611. interpreter.reg(m_this_reg) = callee_and_this.this_value;
  612. return {};
  613. }
  614. ThrowCompletionOr<void> GetGlobal::execute_impl(Bytecode::Interpreter& interpreter) const
  615. {
  616. interpreter.accumulator() = TRY(get_global(
  617. interpreter,
  618. interpreter.current_executable().get_identifier(m_identifier),
  619. interpreter.current_executable().global_variable_caches[m_cache_index]));
  620. return {};
  621. }
  622. ThrowCompletionOr<void> GetLocal::execute_impl(Bytecode::Interpreter&) const
  623. {
  624. // Handled in the interpreter loop.
  625. __builtin_unreachable();
  626. }
  627. ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  628. {
  629. auto& vm = interpreter.vm();
  630. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  631. auto reference = TRY(vm.resolve_binding(string));
  632. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  633. return {};
  634. }
  635. ThrowCompletionOr<void> CreateLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  636. {
  637. auto make_and_swap_envs = [&](auto& old_environment) {
  638. GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
  639. swap(old_environment, environment);
  640. return environment;
  641. };
  642. interpreter.saved_lexical_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().lexical_environment));
  643. return {};
  644. }
  645. ThrowCompletionOr<void> EnterObjectEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  646. {
  647. auto object = TRY(interpreter.accumulator().to_object(interpreter.vm()));
  648. interpreter.enter_object_environment(*object);
  649. return {};
  650. }
  651. ThrowCompletionOr<void> Catch::execute_impl(Bytecode::Interpreter& interpreter) const
  652. {
  653. interpreter.catch_exception();
  654. return {};
  655. }
  656. ThrowCompletionOr<void> CreateVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  657. {
  658. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  659. return create_variable(interpreter.vm(), name, m_mode, m_is_global, m_is_immutable, m_is_strict);
  660. }
  661. ThrowCompletionOr<void> SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  662. {
  663. auto& vm = interpreter.vm();
  664. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  665. TRY(set_variable(vm,
  666. name,
  667. interpreter.accumulator(),
  668. m_mode,
  669. m_initialization_mode,
  670. interpreter.current_executable().environment_variable_caches[m_cache_index]));
  671. return {};
  672. }
  673. ThrowCompletionOr<void> SetLocal::execute_impl(Bytecode::Interpreter&) const
  674. {
  675. // Handled in the interpreter loop.
  676. __builtin_unreachable();
  677. }
  678. ThrowCompletionOr<void> GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  679. {
  680. auto base_value = interpreter.accumulator();
  681. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  682. interpreter.accumulator() = TRY(get_by_id(interpreter.vm(), interpreter.current_executable().get_identifier(m_property), base_value, base_value, cache));
  683. return {};
  684. }
  685. ThrowCompletionOr<void> GetByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  686. {
  687. auto base_value = interpreter.accumulator();
  688. auto this_value = interpreter.reg(m_this_value);
  689. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  690. interpreter.accumulator() = TRY(get_by_id(interpreter.vm(), interpreter.current_executable().get_identifier(m_property), base_value, this_value, cache));
  691. return {};
  692. }
  693. ThrowCompletionOr<void> GetPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const
  694. {
  695. auto& vm = interpreter.vm();
  696. auto const& name = interpreter.current_executable().get_identifier(m_property);
  697. auto base_value = interpreter.accumulator();
  698. auto private_reference = make_private_reference(vm, base_value, name);
  699. interpreter.accumulator() = TRY(private_reference.get_value(vm));
  700. return {};
  701. }
  702. ThrowCompletionOr<void> HasPrivateId::execute_impl(Bytecode::Interpreter& interpreter) const
  703. {
  704. auto& vm = interpreter.vm();
  705. if (!interpreter.accumulator().is_object())
  706. return vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject);
  707. auto private_environment = vm.running_execution_context().private_environment;
  708. VERIFY(private_environment);
  709. auto private_name = private_environment->resolve_private_identifier(interpreter.current_executable().get_identifier(m_property));
  710. interpreter.accumulator() = Value(interpreter.accumulator().as_object().private_element_find(private_name) != nullptr);
  711. return {};
  712. }
  713. ThrowCompletionOr<void> PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  714. {
  715. auto& vm = interpreter.vm();
  716. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  717. auto value = interpreter.accumulator();
  718. auto base = interpreter.reg(m_base);
  719. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  720. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  721. TRY(put_by_property_key(vm, base, base, value, name, m_kind, &cache));
  722. interpreter.accumulator() = value;
  723. return {};
  724. }
  725. ThrowCompletionOr<void> PutByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  726. {
  727. auto& vm = interpreter.vm();
  728. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  729. auto value = interpreter.accumulator();
  730. auto base = interpreter.reg(m_base);
  731. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  732. auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index];
  733. TRY(put_by_property_key(vm, base, interpreter.reg(m_this_value), value, name, m_kind, &cache));
  734. interpreter.accumulator() = value;
  735. return {};
  736. }
  737. ThrowCompletionOr<void> PutPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const
  738. {
  739. auto& vm = interpreter.vm();
  740. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  741. auto value = interpreter.accumulator();
  742. auto object = TRY(interpreter.reg(m_base).to_object(vm));
  743. auto name = interpreter.current_executable().get_identifier(m_property);
  744. auto private_reference = make_private_reference(vm, object, name);
  745. TRY(private_reference.put_value(vm, value));
  746. interpreter.accumulator() = value;
  747. return {};
  748. }
  749. ThrowCompletionOr<void> DeleteById::execute_impl(Bytecode::Interpreter& interpreter) const
  750. {
  751. auto base_value = interpreter.accumulator();
  752. interpreter.accumulator() = TRY(Bytecode::delete_by_id(interpreter, base_value, m_property));
  753. return {};
  754. }
  755. ThrowCompletionOr<void> DeleteByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  756. {
  757. auto& vm = interpreter.vm();
  758. auto base_value = interpreter.accumulator();
  759. auto const& identifier = interpreter.current_executable().get_identifier(m_property);
  760. bool strict = vm.in_strict_mode();
  761. auto reference = Reference { base_value, identifier, interpreter.reg(m_this_value), strict };
  762. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  763. return {};
  764. }
  765. ThrowCompletionOr<void> Jump::execute_impl(Bytecode::Interpreter&) const
  766. {
  767. // Handled in the interpreter loop.
  768. __builtin_unreachable();
  769. }
  770. ThrowCompletionOr<void> ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const
  771. {
  772. auto& cached_this_value = interpreter.reg(Register::this_value());
  773. if (cached_this_value.is_empty()) {
  774. // OPTIMIZATION: Because the value of 'this' cannot be reassigned during a function execution, it's
  775. // resolved once and then saved for subsequent use.
  776. auto& vm = interpreter.vm();
  777. cached_this_value = TRY(vm.resolve_this_binding());
  778. }
  779. interpreter.accumulator() = cached_this_value;
  780. return {};
  781. }
  782. // https://tc39.es/ecma262/#sec-makesuperpropertyreference
  783. ThrowCompletionOr<void> ResolveSuperBase::execute_impl(Bytecode::Interpreter& interpreter) const
  784. {
  785. auto& vm = interpreter.vm();
  786. // 1. Let env be GetThisEnvironment().
  787. auto& env = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  788. // 2. Assert: env.HasSuperBinding() is true.
  789. VERIFY(env.has_super_binding());
  790. // 3. Let baseValue be ? env.GetSuperBase().
  791. interpreter.accumulator() = TRY(env.get_super_base());
  792. return {};
  793. }
  794. ThrowCompletionOr<void> GetNewTarget::execute_impl(Bytecode::Interpreter& interpreter) const
  795. {
  796. interpreter.accumulator() = interpreter.vm().get_new_target();
  797. return {};
  798. }
  799. ThrowCompletionOr<void> GetImportMeta::execute_impl(Bytecode::Interpreter& interpreter) const
  800. {
  801. interpreter.accumulator() = interpreter.vm().get_import_meta();
  802. return {};
  803. }
  804. ThrowCompletionOr<void> JumpConditional::execute_impl(Bytecode::Interpreter&) const
  805. {
  806. // Handled in the interpreter loop.
  807. __builtin_unreachable();
  808. }
  809. ThrowCompletionOr<void> JumpNullish::execute_impl(Bytecode::Interpreter&) const
  810. {
  811. // Handled in the interpreter loop.
  812. __builtin_unreachable();
  813. }
  814. ThrowCompletionOr<void> JumpUndefined::execute_impl(Bytecode::Interpreter&) const
  815. {
  816. // Handled in the interpreter loop.
  817. __builtin_unreachable();
  818. }
  819. static ThrowCompletionOr<Value> dispatch_builtin_call(Bytecode::Interpreter& interpreter, Bytecode::Builtin builtin, Register first_argument)
  820. {
  821. switch (builtin) {
  822. case Builtin::MathAbs:
  823. return TRY(MathObject::abs_impl(interpreter.vm(), interpreter.reg(first_argument)));
  824. case Builtin::MathLog:
  825. return TRY(MathObject::log_impl(interpreter.vm(), interpreter.reg(first_argument)));
  826. case Builtin::MathPow: {
  827. auto exponent = interpreter.reg(Register { first_argument.index() + 1 });
  828. return TRY(MathObject::pow_impl(interpreter.vm(), interpreter.reg(first_argument), exponent));
  829. }
  830. case Builtin::MathExp:
  831. return TRY(MathObject::exp_impl(interpreter.vm(), interpreter.reg(first_argument)));
  832. case Builtin::MathCeil:
  833. return TRY(MathObject::ceil_impl(interpreter.vm(), interpreter.reg(first_argument)));
  834. case Builtin::MathFloor:
  835. return TRY(MathObject::floor_impl(interpreter.vm(), interpreter.reg(first_argument)));
  836. case Builtin::MathRound:
  837. return TRY(MathObject::round_impl(interpreter.vm(), interpreter.reg(first_argument)));
  838. case Builtin::MathSqrt:
  839. return TRY(MathObject::sqrt_impl(interpreter.vm(), interpreter.reg(first_argument)));
  840. case Bytecode::Builtin::__Count:
  841. VERIFY_NOT_REACHED();
  842. }
  843. VERIFY_NOT_REACHED();
  844. }
  845. ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) const
  846. {
  847. auto callee = interpreter.reg(m_callee);
  848. TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string()));
  849. if (m_builtin.has_value() && m_argument_count == Bytecode::builtin_argument_count(m_builtin.value()) && interpreter.realm().get_builtin_value(m_builtin.value()) == callee) {
  850. interpreter.accumulator() = TRY(dispatch_builtin_call(interpreter, m_builtin.value(), m_first_argument));
  851. return {};
  852. }
  853. interpreter.accumulator() = TRY(perform_call(interpreter, interpreter.reg(m_this_value), call_type(), callee, interpreter.registers().slice(m_first_argument.index(), m_argument_count)));
  854. return {};
  855. }
  856. ThrowCompletionOr<void> CallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  857. {
  858. auto callee = interpreter.reg(m_callee);
  859. TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string()));
  860. auto argument_values = argument_list_evaluation(interpreter.vm(), interpreter.accumulator());
  861. interpreter.accumulator() = TRY(perform_call(interpreter, interpreter.reg(m_this_value), call_type(), callee, move(argument_values)));
  862. return {};
  863. }
  864. // 13.3.7.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  865. ThrowCompletionOr<void> SuperCallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  866. {
  867. interpreter.accumulator() = TRY(super_call_with_argument_array(interpreter.vm(), interpreter.accumulator(), m_is_synthetic));
  868. return {};
  869. }
  870. ThrowCompletionOr<void> NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  871. {
  872. auto& vm = interpreter.vm();
  873. interpreter.accumulator() = new_function(vm, m_function_node, m_lhs_name, m_home_object);
  874. return {};
  875. }
  876. ThrowCompletionOr<void> Return::execute_impl(Bytecode::Interpreter& interpreter) const
  877. {
  878. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  879. return {};
  880. }
  881. ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  882. {
  883. auto& vm = interpreter.vm();
  884. auto old_value = interpreter.accumulator();
  885. // OPTIMIZATION: Fast path for Int32 values.
  886. if (old_value.is_int32()) {
  887. auto integer_value = old_value.as_i32();
  888. if (integer_value != NumericLimits<i32>::max()) [[likely]] {
  889. interpreter.accumulator() = Value { integer_value + 1 };
  890. return {};
  891. }
  892. }
  893. old_value = TRY(old_value.to_numeric(vm));
  894. if (old_value.is_number())
  895. interpreter.accumulator() = Value(old_value.as_double() + 1);
  896. else
  897. interpreter.accumulator() = BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  898. return {};
  899. }
  900. ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  901. {
  902. auto& vm = interpreter.vm();
  903. auto old_value = TRY(interpreter.accumulator().to_numeric(vm));
  904. if (old_value.is_number())
  905. interpreter.accumulator() = Value(old_value.as_double() - 1);
  906. else
  907. interpreter.accumulator() = BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  908. return {};
  909. }
  910. ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  911. {
  912. return throw_completion(interpreter.accumulator());
  913. }
  914. ThrowCompletionOr<void> ThrowIfNotObject::execute_impl(Bytecode::Interpreter& interpreter) const
  915. {
  916. auto& vm = interpreter.vm();
  917. if (!interpreter.accumulator().is_object())
  918. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, interpreter.accumulator().to_string_without_side_effects());
  919. return {};
  920. }
  921. ThrowCompletionOr<void> ThrowIfNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  922. {
  923. auto& vm = interpreter.vm();
  924. auto value = interpreter.accumulator();
  925. if (value.is_nullish())
  926. return vm.throw_completion<TypeError>(ErrorType::NotObjectCoercible, value.to_string_without_side_effects());
  927. return {};
  928. }
  929. ThrowCompletionOr<void> EnterUnwindContext::execute_impl(Bytecode::Interpreter&) const
  930. {
  931. // Handled in the interpreter loop.
  932. __builtin_unreachable();
  933. }
  934. ThrowCompletionOr<void> ScheduleJump::execute_impl(Bytecode::Interpreter&) const
  935. {
  936. // Handled in the interpreter loop.
  937. __builtin_unreachable();
  938. }
  939. ThrowCompletionOr<void> LeaveLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  940. {
  941. interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
  942. return {};
  943. }
  944. ThrowCompletionOr<void> LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  945. {
  946. interpreter.leave_unwind_context();
  947. return {};
  948. }
  949. ThrowCompletionOr<void> ContinuePendingUnwind::execute_impl(Bytecode::Interpreter&) const
  950. {
  951. // Handled in the interpreter loop.
  952. __builtin_unreachable();
  953. }
  954. ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  955. {
  956. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  957. auto object = Object::create(interpreter.realm(), nullptr);
  958. object->define_direct_property("result", yielded_value, JS::default_attributes);
  959. if (m_continuation_label.has_value())
  960. // FIXME: If we get a pointer, which is not accurately representable as a double
  961. // will cause this to explode
  962. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes);
  963. else
  964. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  965. object->define_direct_property("isAwait", Value(false), JS::default_attributes);
  966. interpreter.do_return(object);
  967. return {};
  968. }
  969. ThrowCompletionOr<void> Await::execute_impl(Bytecode::Interpreter& interpreter) const
  970. {
  971. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  972. auto object = Object::create(interpreter.realm(), nullptr);
  973. object->define_direct_property("result", yielded_value, JS::default_attributes);
  974. // FIXME: If we get a pointer, which is not accurately representable as a double
  975. // will cause this to explode
  976. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label.block()))), JS::default_attributes);
  977. object->define_direct_property("isAwait", Value(true), JS::default_attributes);
  978. interpreter.do_return(object);
  979. return {};
  980. }
  981. ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  982. {
  983. interpreter.accumulator() = TRY(get_by_value(interpreter.vm(), interpreter.reg(m_base), interpreter.accumulator()));
  984. return {};
  985. }
  986. ThrowCompletionOr<void> GetByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  987. {
  988. auto& vm = interpreter.vm();
  989. // NOTE: Get the property key from the accumulator before side effects have a chance to overwrite it.
  990. auto property_key_value = interpreter.accumulator();
  991. auto object = TRY(interpreter.reg(m_base).to_object(vm));
  992. auto property_key = TRY(property_key_value.to_property_key(vm));
  993. interpreter.accumulator() = TRY(object->internal_get(property_key, interpreter.reg(m_this_value)));
  994. return {};
  995. }
  996. ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  997. {
  998. auto& vm = interpreter.vm();
  999. auto value = interpreter.accumulator();
  1000. TRY(put_by_value(vm, interpreter.reg(m_base), interpreter.reg(m_property), interpreter.accumulator(), m_kind));
  1001. interpreter.accumulator() = value;
  1002. return {};
  1003. }
  1004. ThrowCompletionOr<void> PutByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  1005. {
  1006. auto& vm = interpreter.vm();
  1007. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  1008. auto value = interpreter.accumulator();
  1009. auto base = interpreter.reg(m_base);
  1010. auto property_key = m_kind != PropertyKind::Spread ? TRY(interpreter.reg(m_property).to_property_key(vm)) : PropertyKey {};
  1011. TRY(put_by_property_key(vm, base, interpreter.reg(m_this_value), value, property_key, m_kind));
  1012. interpreter.accumulator() = value;
  1013. return {};
  1014. }
  1015. ThrowCompletionOr<void> DeleteByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  1016. {
  1017. auto base_value = interpreter.reg(m_base);
  1018. auto property_key_value = interpreter.accumulator();
  1019. interpreter.accumulator() = TRY(delete_by_value(interpreter, base_value, property_key_value));
  1020. return {};
  1021. }
  1022. ThrowCompletionOr<void> DeleteByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  1023. {
  1024. // NOTE: Get the property key from the accumulator before side effects have a chance to overwrite it.
  1025. auto property_key_value = interpreter.accumulator();
  1026. auto base_value = interpreter.reg(m_base);
  1027. auto this_value = interpreter.reg(m_this_value);
  1028. interpreter.accumulator() = TRY(delete_by_value_with_this(interpreter, base_value, property_key_value, this_value));
  1029. return {};
  1030. }
  1031. ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  1032. {
  1033. auto& vm = interpreter.vm();
  1034. interpreter.accumulator() = TRY(get_iterator(vm, interpreter.accumulator(), m_hint));
  1035. return {};
  1036. }
  1037. ThrowCompletionOr<void> GetObjectFromIteratorRecord::execute_impl(Bytecode::Interpreter& interpreter) const
  1038. {
  1039. auto& iterator_record = verify_cast<IteratorRecord>(interpreter.reg(m_iterator_record).as_object());
  1040. interpreter.reg(m_object) = iterator_record.iterator;
  1041. return {};
  1042. }
  1043. ThrowCompletionOr<void> GetNextMethodFromIteratorRecord::execute_impl(Bytecode::Interpreter& interpreter) const
  1044. {
  1045. auto& iterator_record = verify_cast<IteratorRecord>(interpreter.reg(m_iterator_record).as_object());
  1046. interpreter.reg(m_next_method) = iterator_record.next_method;
  1047. return {};
  1048. }
  1049. ThrowCompletionOr<void> GetMethod::execute_impl(Bytecode::Interpreter& interpreter) const
  1050. {
  1051. auto& vm = interpreter.vm();
  1052. auto identifier = interpreter.current_executable().get_identifier(m_property);
  1053. auto method = TRY(interpreter.accumulator().get_method(vm, identifier));
  1054. interpreter.accumulator() = method ?: js_undefined();
  1055. return {};
  1056. }
  1057. ThrowCompletionOr<void> GetObjectPropertyIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  1058. {
  1059. interpreter.accumulator() = TRY(get_object_property_iterator(interpreter.vm(), interpreter.accumulator()));
  1060. return {};
  1061. }
  1062. ThrowCompletionOr<void> IteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1063. {
  1064. auto& vm = interpreter.vm();
  1065. auto& iterator = verify_cast<IteratorRecord>(interpreter.accumulator().as_object());
  1066. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1067. TRY(iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} }));
  1068. return {};
  1069. }
  1070. ThrowCompletionOr<void> AsyncIteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1071. {
  1072. auto& vm = interpreter.vm();
  1073. auto& iterator = verify_cast<IteratorRecord>(interpreter.accumulator().as_object());
  1074. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1075. TRY(async_iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} }));
  1076. return {};
  1077. }
  1078. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  1079. {
  1080. auto& vm = interpreter.vm();
  1081. auto& iterator = verify_cast<IteratorRecord>(interpreter.accumulator().as_object());
  1082. interpreter.accumulator() = TRY(iterator_next(vm, iterator));
  1083. return {};
  1084. }
  1085. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const
  1086. {
  1087. interpreter.accumulator() = TRY(new_class(interpreter.vm(), interpreter.accumulator(), m_class_expression, m_lhs_name));
  1088. return {};
  1089. }
  1090. // 13.5.3.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-typeof-operator-runtime-semantics-evaluation
  1091. ThrowCompletionOr<void> TypeofVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  1092. {
  1093. auto& vm = interpreter.vm();
  1094. interpreter.accumulator() = TRY(typeof_variable(vm, interpreter.current_executable().get_identifier(m_identifier)));
  1095. return {};
  1096. }
  1097. ThrowCompletionOr<void> TypeofLocal::execute_impl(Bytecode::Interpreter& interpreter) const
  1098. {
  1099. auto& vm = interpreter.vm();
  1100. auto const& value = vm.running_execution_context().local(m_index);
  1101. interpreter.accumulator() = PrimitiveString::create(vm, value.typeof());
  1102. return {};
  1103. }
  1104. ThrowCompletionOr<void> ToNumeric::execute_impl(Bytecode::Interpreter& interpreter) const
  1105. {
  1106. interpreter.accumulator() = TRY(interpreter.accumulator().to_numeric(interpreter.vm()));
  1107. return {};
  1108. }
  1109. ThrowCompletionOr<void> BlockDeclarationInstantiation::execute_impl(Bytecode::Interpreter& interpreter) const
  1110. {
  1111. auto& vm = interpreter.vm();
  1112. auto old_environment = vm.running_execution_context().lexical_environment;
  1113. interpreter.saved_lexical_environment_stack().append(old_environment);
  1114. vm.running_execution_context().lexical_environment = new_declarative_environment(*old_environment);
  1115. m_scope_node.block_declaration_instantiation(vm, vm.running_execution_context().lexical_environment);
  1116. return {};
  1117. }
  1118. ByteString Load::to_byte_string_impl(Bytecode::Executable const&) const
  1119. {
  1120. return ByteString::formatted("Load {}", m_src);
  1121. }
  1122. ByteString LoadImmediate::to_byte_string_impl(Bytecode::Executable const&) const
  1123. {
  1124. return ByteString::formatted("LoadImmediate {}", m_value);
  1125. }
  1126. ByteString Store::to_byte_string_impl(Bytecode::Executable const&) const
  1127. {
  1128. return ByteString::formatted("Store {}", m_dst);
  1129. }
  1130. ByteString NewBigInt::to_byte_string_impl(Bytecode::Executable const&) const
  1131. {
  1132. return ByteString::formatted("NewBigInt \"{}\"", m_bigint.to_base_deprecated(10));
  1133. }
  1134. ByteString NewArray::to_byte_string_impl(Bytecode::Executable const&) const
  1135. {
  1136. StringBuilder builder;
  1137. builder.append("NewArray"sv);
  1138. if (m_element_count != 0) {
  1139. builder.appendff(" [{}-{}]", m_elements[0], m_elements[1]);
  1140. }
  1141. return builder.to_byte_string();
  1142. }
  1143. ByteString NewPrimitiveArray::to_byte_string_impl(Bytecode::Executable const&) const
  1144. {
  1145. return ByteString::formatted("NewPrimitiveArray {}"sv, m_values.span());
  1146. }
  1147. ByteString Append::to_byte_string_impl(Bytecode::Executable const&) const
  1148. {
  1149. if (m_is_spread)
  1150. return ByteString::formatted("Append lhs: **{}", m_lhs);
  1151. return ByteString::formatted("Append lhs: {}", m_lhs);
  1152. }
  1153. ByteString IteratorToArray::to_byte_string_impl(Bytecode::Executable const&) const
  1154. {
  1155. return "IteratorToArray";
  1156. }
  1157. ByteString NewString::to_byte_string_impl(Bytecode::Executable const& executable) const
  1158. {
  1159. return ByteString::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  1160. }
  1161. ByteString NewObject::to_byte_string_impl(Bytecode::Executable const&) const
  1162. {
  1163. return "NewObject";
  1164. }
  1165. ByteString NewRegExp::to_byte_string_impl(Bytecode::Executable const& executable) const
  1166. {
  1167. return ByteString::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  1168. }
  1169. ByteString CopyObjectExcludingProperties::to_byte_string_impl(Bytecode::Executable const&) const
  1170. {
  1171. StringBuilder builder;
  1172. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  1173. if (m_excluded_names_count != 0) {
  1174. builder.append(" excluding:["sv);
  1175. builder.join(", "sv, ReadonlySpan<Register>(m_excluded_names, m_excluded_names_count));
  1176. builder.append(']');
  1177. }
  1178. return builder.to_byte_string();
  1179. }
  1180. ByteString ConcatString::to_byte_string_impl(Bytecode::Executable const&) const
  1181. {
  1182. return ByteString::formatted("ConcatString {}", m_lhs);
  1183. }
  1184. ByteString GetCalleeAndThisFromEnvironment::to_byte_string_impl(Bytecode::Executable const& executable) const
  1185. {
  1186. return ByteString::formatted("GetCalleeAndThisFromEnvironment {} -> callee: {}, this:{} ", executable.identifier_table->get(m_identifier), m_callee_reg, m_this_reg);
  1187. }
  1188. ByteString GetVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1189. {
  1190. return ByteString::formatted("GetVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1191. }
  1192. ByteString GetGlobal::to_byte_string_impl(Bytecode::Executable const& executable) const
  1193. {
  1194. return ByteString::formatted("GetGlobal {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1195. }
  1196. ByteString GetLocal::to_byte_string_impl(Bytecode::Executable const&) const
  1197. {
  1198. return ByteString::formatted("GetLocal {}", m_index);
  1199. }
  1200. ByteString DeleteVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1201. {
  1202. return ByteString::formatted("DeleteVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1203. }
  1204. ByteString CreateLexicalEnvironment::to_byte_string_impl(Bytecode::Executable const&) const
  1205. {
  1206. return "CreateLexicalEnvironment"sv;
  1207. }
  1208. ByteString CreateVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1209. {
  1210. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1211. return ByteString::formatted("CreateVariable env:{} immutable:{} global:{} {} ({})", mode_string, m_is_immutable, m_is_global, m_identifier, executable.identifier_table->get(m_identifier));
  1212. }
  1213. ByteString EnterObjectEnvironment::to_byte_string_impl(Executable const&) const
  1214. {
  1215. return ByteString::formatted("EnterObjectEnvironment");
  1216. }
  1217. ByteString SetVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1218. {
  1219. auto initialization_mode_name = m_initialization_mode == InitializationMode::Initialize ? "Initialize" : "Set";
  1220. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1221. return ByteString::formatted("SetVariable env:{} init:{} {} ({})", mode_string, initialization_mode_name, m_identifier, executable.identifier_table->get(m_identifier));
  1222. }
  1223. ByteString SetLocal::to_byte_string_impl(Bytecode::Executable const&) const
  1224. {
  1225. return ByteString::formatted("SetLocal {}", m_index);
  1226. }
  1227. static StringView property_kind_to_string(PropertyKind kind)
  1228. {
  1229. switch (kind) {
  1230. case PropertyKind::Getter:
  1231. return "getter"sv;
  1232. case PropertyKind::Setter:
  1233. return "setter"sv;
  1234. case PropertyKind::KeyValue:
  1235. return "key-value"sv;
  1236. case PropertyKind::DirectKeyValue:
  1237. return "direct-key-value"sv;
  1238. case PropertyKind::Spread:
  1239. return "spread"sv;
  1240. case PropertyKind::ProtoSetter:
  1241. return "proto-setter"sv;
  1242. }
  1243. VERIFY_NOT_REACHED();
  1244. }
  1245. ByteString PutById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1246. {
  1247. auto kind = property_kind_to_string(m_kind);
  1248. return ByteString::formatted("PutById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  1249. }
  1250. ByteString PutByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1251. {
  1252. auto kind = property_kind_to_string(m_kind);
  1253. return ByteString::formatted("PutByIdWithThis kind:{} base:{}, property:{} ({}) this_value:{}", kind, m_base, m_property, executable.identifier_table->get(m_property), m_this_value);
  1254. }
  1255. ByteString PutPrivateById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1256. {
  1257. auto kind = property_kind_to_string(m_kind);
  1258. return ByteString::formatted("PutPrivateById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  1259. }
  1260. ByteString GetById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1261. {
  1262. return ByteString::formatted("GetById {} ({})", m_property, executable.identifier_table->get(m_property));
  1263. }
  1264. ByteString GetByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1265. {
  1266. return ByteString::formatted("GetByIdWithThis {} ({}) this_value:{}", m_property, executable.identifier_table->get(m_property), m_this_value);
  1267. }
  1268. ByteString GetPrivateById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1269. {
  1270. return ByteString::formatted("GetPrivateById {} ({})", m_property, executable.identifier_table->get(m_property));
  1271. }
  1272. ByteString HasPrivateId::to_byte_string_impl(Bytecode::Executable const& executable) const
  1273. {
  1274. return ByteString::formatted("HasPrivateId {} ({})", m_property, executable.identifier_table->get(m_property));
  1275. }
  1276. ByteString DeleteById::to_byte_string_impl(Bytecode::Executable const& executable) const
  1277. {
  1278. return ByteString::formatted("DeleteById {} ({})", m_property, executable.identifier_table->get(m_property));
  1279. }
  1280. ByteString DeleteByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const
  1281. {
  1282. return ByteString::formatted("DeleteByIdWithThis {} ({}) this_value:{}", m_property, executable.identifier_table->get(m_property), m_this_value);
  1283. }
  1284. ByteString Jump::to_byte_string_impl(Bytecode::Executable const&) const
  1285. {
  1286. if (m_true_target.has_value())
  1287. return ByteString::formatted("Jump {}", *m_true_target);
  1288. return ByteString::formatted("Jump <empty>");
  1289. }
  1290. ByteString JumpConditional::to_byte_string_impl(Bytecode::Executable const&) const
  1291. {
  1292. auto true_string = m_true_target.has_value() ? ByteString::formatted("{}", *m_true_target) : "<empty>";
  1293. auto false_string = m_false_target.has_value() ? ByteString::formatted("{}", *m_false_target) : "<empty>";
  1294. return ByteString::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  1295. }
  1296. ByteString JumpNullish::to_byte_string_impl(Bytecode::Executable const&) const
  1297. {
  1298. auto true_string = m_true_target.has_value() ? ByteString::formatted("{}", *m_true_target) : "<empty>";
  1299. auto false_string = m_false_target.has_value() ? ByteString::formatted("{}", *m_false_target) : "<empty>";
  1300. return ByteString::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  1301. }
  1302. ByteString JumpUndefined::to_byte_string_impl(Bytecode::Executable const&) const
  1303. {
  1304. auto true_string = m_true_target.has_value() ? ByteString::formatted("{}", *m_true_target) : "<empty>";
  1305. auto false_string = m_false_target.has_value() ? ByteString::formatted("{}", *m_false_target) : "<empty>";
  1306. return ByteString::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  1307. }
  1308. static StringView call_type_to_string(CallType type)
  1309. {
  1310. switch (type) {
  1311. case CallType::Call:
  1312. return ""sv;
  1313. case CallType::Construct:
  1314. return " (Construct)"sv;
  1315. case CallType::DirectEval:
  1316. return " (DirectEval)"sv;
  1317. }
  1318. VERIFY_NOT_REACHED();
  1319. }
  1320. ByteString Call::to_byte_string_impl(Bytecode::Executable const& executable) const
  1321. {
  1322. auto type = call_type_to_string(m_type);
  1323. if (m_builtin.has_value())
  1324. return ByteString::formatted("Call{} callee:{}, this:{}, first_arg:{} (builtin {})", type, m_callee, m_this_value, m_first_argument, m_builtin.value());
  1325. if (m_expression_string.has_value())
  1326. return ByteString::formatted("Call{} callee:{}, this:{}, first_arg:{} ({})", type, m_callee, m_this_value, m_first_argument, executable.get_string(m_expression_string.value()));
  1327. return ByteString::formatted("Call{} callee:{}, this:{}, first_arg:{}", type, m_callee, m_first_argument, m_this_value);
  1328. }
  1329. ByteString CallWithArgumentArray::to_byte_string_impl(Bytecode::Executable const& executable) const
  1330. {
  1331. auto type = call_type_to_string(m_type);
  1332. if (m_expression_string.has_value())
  1333. return ByteString::formatted("CallWithArgumentArray{} callee:{}, this:{}, arguments:[...acc] ({})", type, m_callee, m_this_value, executable.get_string(m_expression_string.value()));
  1334. return ByteString::formatted("CallWithArgumentArray{} callee:{}, this:{}, arguments:[...acc]", type, m_callee, m_this_value);
  1335. }
  1336. ByteString SuperCallWithArgumentArray::to_byte_string_impl(Bytecode::Executable const&) const
  1337. {
  1338. return "SuperCallWithArgumentArray arguments:[...acc]"sv;
  1339. }
  1340. ByteString NewFunction::to_byte_string_impl(Bytecode::Executable const&) const
  1341. {
  1342. StringBuilder builder;
  1343. builder.append("NewFunction"sv);
  1344. if (m_function_node.has_name())
  1345. builder.appendff(" name:{}"sv, m_function_node.name());
  1346. if (m_lhs_name.has_value())
  1347. builder.appendff(" lhs_name:{}"sv, m_lhs_name.value());
  1348. if (m_home_object.has_value())
  1349. builder.appendff(" home_object:{}"sv, m_home_object.value());
  1350. return builder.to_byte_string();
  1351. }
  1352. ByteString NewClass::to_byte_string_impl(Bytecode::Executable const&) const
  1353. {
  1354. StringBuilder builder;
  1355. auto name = m_class_expression.name();
  1356. builder.appendff("NewClass '{}'"sv, name.is_null() ? ""sv : name);
  1357. if (m_lhs_name.has_value())
  1358. builder.appendff(" lhs_name:{}"sv, m_lhs_name.value());
  1359. return builder.to_byte_string();
  1360. }
  1361. ByteString Return::to_byte_string_impl(Bytecode::Executable const&) const
  1362. {
  1363. return "Return";
  1364. }
  1365. ByteString Increment::to_byte_string_impl(Bytecode::Executable const&) const
  1366. {
  1367. return "Increment";
  1368. }
  1369. ByteString Decrement::to_byte_string_impl(Bytecode::Executable const&) const
  1370. {
  1371. return "Decrement";
  1372. }
  1373. ByteString Throw::to_byte_string_impl(Bytecode::Executable const&) const
  1374. {
  1375. return "Throw";
  1376. }
  1377. ByteString ThrowIfNotObject::to_byte_string_impl(Bytecode::Executable const&) const
  1378. {
  1379. return "ThrowIfNotObject";
  1380. }
  1381. ByteString ThrowIfNullish::to_byte_string_impl(Bytecode::Executable const&) const
  1382. {
  1383. return "ThrowIfNullish";
  1384. }
  1385. ByteString EnterUnwindContext::to_byte_string_impl(Bytecode::Executable const&) const
  1386. {
  1387. return ByteString::formatted("EnterUnwindContext entry:{}", m_entry_point);
  1388. }
  1389. ByteString ScheduleJump::to_byte_string_impl(Bytecode::Executable const&) const
  1390. {
  1391. return ByteString::formatted("ScheduleJump {}", m_target);
  1392. }
  1393. ByteString LeaveLexicalEnvironment::to_byte_string_impl(Bytecode::Executable const&) const
  1394. {
  1395. return "LeaveLexicalEnvironment"sv;
  1396. }
  1397. ByteString LeaveUnwindContext::to_byte_string_impl(Bytecode::Executable const&) const
  1398. {
  1399. return "LeaveUnwindContext";
  1400. }
  1401. ByteString ContinuePendingUnwind::to_byte_string_impl(Bytecode::Executable const&) const
  1402. {
  1403. return ByteString::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  1404. }
  1405. ByteString Yield::to_byte_string_impl(Bytecode::Executable const&) const
  1406. {
  1407. if (m_continuation_label.has_value())
  1408. return ByteString::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  1409. return ByteString::formatted("Yield return");
  1410. }
  1411. ByteString Await::to_byte_string_impl(Bytecode::Executable const&) const
  1412. {
  1413. return ByteString::formatted("Await continuation:@{}", m_continuation_label.block().name());
  1414. }
  1415. ByteString GetByValue::to_byte_string_impl(Bytecode::Executable const&) const
  1416. {
  1417. return ByteString::formatted("GetByValue base:{}", m_base);
  1418. }
  1419. ByteString GetByValueWithThis::to_byte_string_impl(Bytecode::Executable const&) const
  1420. {
  1421. return ByteString::formatted("GetByValueWithThis base:{} this_value:{}", m_base, m_this_value);
  1422. }
  1423. ByteString PutByValue::to_byte_string_impl(Bytecode::Executable const&) const
  1424. {
  1425. auto kind = property_kind_to_string(m_kind);
  1426. return ByteString::formatted("PutByValue kind:{} base:{}, property:{}", kind, m_base, m_property);
  1427. }
  1428. ByteString PutByValueWithThis::to_byte_string_impl(Bytecode::Executable const&) const
  1429. {
  1430. auto kind = property_kind_to_string(m_kind);
  1431. return ByteString::formatted("PutByValueWithThis kind:{} base:{}, property:{} this_value:{}", kind, m_base, m_property, m_this_value);
  1432. }
  1433. ByteString DeleteByValue::to_byte_string_impl(Bytecode::Executable const&) const
  1434. {
  1435. return ByteString::formatted("DeleteByValue base:{}", m_base);
  1436. }
  1437. ByteString DeleteByValueWithThis::to_byte_string_impl(Bytecode::Executable const&) const
  1438. {
  1439. return ByteString::formatted("DeleteByValueWithThis base:{} this_value:{}", m_base, m_this_value);
  1440. }
  1441. ByteString GetIterator::to_byte_string_impl(Executable const&) const
  1442. {
  1443. auto hint = m_hint == IteratorHint::Sync ? "sync" : "async";
  1444. return ByteString::formatted("GetIterator hint:{}", hint);
  1445. }
  1446. ByteString GetMethod::to_byte_string_impl(Bytecode::Executable const& executable) const
  1447. {
  1448. return ByteString::formatted("GetMethod {} ({})", m_property, executable.identifier_table->get(m_property));
  1449. }
  1450. ByteString GetObjectPropertyIterator::to_byte_string_impl(Bytecode::Executable const&) const
  1451. {
  1452. return "GetObjectPropertyIterator";
  1453. }
  1454. ByteString IteratorClose::to_byte_string_impl(Bytecode::Executable const&) const
  1455. {
  1456. if (!m_completion_value.has_value())
  1457. return ByteString::formatted("IteratorClose completion_type={} completion_value=<empty>", to_underlying(m_completion_type));
  1458. auto completion_value_string = m_completion_value->to_string_without_side_effects();
  1459. return ByteString::formatted("IteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string);
  1460. }
  1461. ByteString AsyncIteratorClose::to_byte_string_impl(Bytecode::Executable const&) const
  1462. {
  1463. if (!m_completion_value.has_value())
  1464. return ByteString::formatted("AsyncIteratorClose completion_type={} completion_value=<empty>", to_underlying(m_completion_type));
  1465. auto completion_value_string = m_completion_value->to_string_without_side_effects();
  1466. return ByteString::formatted("AsyncIteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string);
  1467. }
  1468. ByteString IteratorNext::to_byte_string_impl(Executable const&) const
  1469. {
  1470. return "IteratorNext";
  1471. }
  1472. ByteString ResolveThisBinding::to_byte_string_impl(Bytecode::Executable const&) const
  1473. {
  1474. return "ResolveThisBinding"sv;
  1475. }
  1476. ByteString ResolveSuperBase::to_byte_string_impl(Bytecode::Executable const&) const
  1477. {
  1478. return "ResolveSuperBase"sv;
  1479. }
  1480. ByteString GetNewTarget::to_byte_string_impl(Bytecode::Executable const&) const
  1481. {
  1482. return "GetNewTarget"sv;
  1483. }
  1484. ByteString GetImportMeta::to_byte_string_impl(Bytecode::Executable const&) const
  1485. {
  1486. return "GetImportMeta"sv;
  1487. }
  1488. ByteString TypeofVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
  1489. {
  1490. return ByteString::formatted("TypeofVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1491. }
  1492. ByteString TypeofLocal::to_byte_string_impl(Bytecode::Executable const&) const
  1493. {
  1494. return ByteString::formatted("TypeofLocal {}", m_index);
  1495. }
  1496. ByteString ToNumeric::to_byte_string_impl(Bytecode::Executable const&) const
  1497. {
  1498. return "ToNumeric"sv;
  1499. }
  1500. ByteString BlockDeclarationInstantiation::to_byte_string_impl(Bytecode::Executable const&) const
  1501. {
  1502. return "BlockDeclarationInstantiation"sv;
  1503. }
  1504. ByteString ImportCall::to_byte_string_impl(Bytecode::Executable const&) const
  1505. {
  1506. return ByteString::formatted("ImportCall specifier:{} options:{}"sv, m_specifier, m_options);
  1507. }
  1508. ByteString Catch::to_byte_string_impl(Bytecode::Executable const&) const
  1509. {
  1510. return "Catch"sv;
  1511. }
  1512. ByteString GetObjectFromIteratorRecord::to_byte_string_impl(Bytecode::Executable const&) const
  1513. {
  1514. return ByteString::formatted("GetObjectFromIteratorRecord object:{} <- iterator_record:{}", m_object, m_iterator_record);
  1515. }
  1516. ByteString GetNextMethodFromIteratorRecord::to_byte_string_impl(Bytecode::Executable const&) const
  1517. {
  1518. return ByteString::formatted("GetNextMethodFromIteratorRecord next_method:{} <- iterator_record:{}", m_next_method, m_iterator_record);
  1519. }
  1520. }