Interpreter.cpp 114 KB

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