Interpreter.cpp 100 KB

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