Interpreter.cpp 111 KB

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