Op.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  1. /*
  2. * Copyright (c) 2021-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/HashTable.h>
  9. #include <LibJS/AST.h>
  10. #include <LibJS/Bytecode/Interpreter.h>
  11. #include <LibJS/Bytecode/Op.h>
  12. #include <LibJS/Runtime/AbstractOperations.h>
  13. #include <LibJS/Runtime/Array.h>
  14. #include <LibJS/Runtime/BigInt.h>
  15. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  16. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  17. #include <LibJS/Runtime/Environment.h>
  18. #include <LibJS/Runtime/FunctionEnvironment.h>
  19. #include <LibJS/Runtime/GlobalEnvironment.h>
  20. #include <LibJS/Runtime/GlobalObject.h>
  21. #include <LibJS/Runtime/Iterator.h>
  22. #include <LibJS/Runtime/IteratorOperations.h>
  23. #include <LibJS/Runtime/NativeFunction.h>
  24. #include <LibJS/Runtime/ObjectEnvironment.h>
  25. #include <LibJS/Runtime/Reference.h>
  26. #include <LibJS/Runtime/RegExpObject.h>
  27. #include <LibJS/Runtime/Value.h>
  28. namespace JS::Bytecode {
  29. DeprecatedString Instruction::to_deprecated_string(Bytecode::Executable const& executable) const
  30. {
  31. #define __BYTECODE_OP(op) \
  32. case Instruction::Type::op: \
  33. return static_cast<Bytecode::Op::op const&>(*this).to_deprecated_string_impl(executable);
  34. switch (type()) {
  35. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  36. default:
  37. VERIFY_NOT_REACHED();
  38. }
  39. #undef __BYTECODE_OP
  40. }
  41. }
  42. namespace JS::Bytecode::Op {
  43. static ThrowCompletionOr<void> put_by_property_key(VM& vm, Value base, Value this_value, Value value, PropertyKey name, PropertyKind kind)
  44. {
  45. auto object = TRY(base.to_object(vm));
  46. if (kind == PropertyKind::Getter || kind == PropertyKind::Setter) {
  47. // The generator should only pass us functions for getters and setters.
  48. VERIFY(value.is_function());
  49. }
  50. switch (kind) {
  51. case PropertyKind::Getter: {
  52. auto& function = value.as_function();
  53. if (function.name().is_empty() && is<ECMAScriptFunctionObject>(function))
  54. static_cast<ECMAScriptFunctionObject*>(&function)->set_name(DeprecatedString::formatted("get {}", name));
  55. object->define_direct_accessor(name, &function, nullptr, Attribute::Configurable | Attribute::Enumerable);
  56. break;
  57. }
  58. case PropertyKind::Setter: {
  59. auto& function = value.as_function();
  60. if (function.name().is_empty() && is<ECMAScriptFunctionObject>(function))
  61. static_cast<ECMAScriptFunctionObject*>(&function)->set_name(DeprecatedString::formatted("set {}", name));
  62. object->define_direct_accessor(name, nullptr, &function, Attribute::Configurable | Attribute::Enumerable);
  63. break;
  64. }
  65. case PropertyKind::KeyValue: {
  66. bool succeeded = TRY(object->internal_set(name, value, this_value));
  67. if (!succeeded && vm.in_strict_mode())
  68. return vm.throw_completion<TypeError>(ErrorType::ReferenceNullishSetProperty, name, TRY_OR_THROW_OOM(vm, base.to_string_without_side_effects()));
  69. break;
  70. }
  71. case PropertyKind::Spread:
  72. TRY(object->copy_data_properties(vm, value, {}));
  73. break;
  74. case PropertyKind::ProtoSetter:
  75. if (value.is_object() || value.is_null())
  76. MUST(object->internal_set_prototype_of(value.is_object() ? &value.as_object() : nullptr));
  77. break;
  78. }
  79. return {};
  80. }
  81. ThrowCompletionOr<void> Load::execute_impl(Bytecode::Interpreter& interpreter) const
  82. {
  83. interpreter.accumulator() = interpreter.reg(m_src);
  84. return {};
  85. }
  86. ThrowCompletionOr<void> LoadImmediate::execute_impl(Bytecode::Interpreter& interpreter) const
  87. {
  88. interpreter.accumulator() = m_value;
  89. return {};
  90. }
  91. ThrowCompletionOr<void> Store::execute_impl(Bytecode::Interpreter& interpreter) const
  92. {
  93. interpreter.reg(m_dst) = interpreter.accumulator();
  94. return {};
  95. }
  96. static ThrowCompletionOr<Value> abstract_inequals(VM& vm, Value src1, Value src2)
  97. {
  98. return Value(!TRY(is_loosely_equal(vm, src1, src2)));
  99. }
  100. static ThrowCompletionOr<Value> abstract_equals(VM& vm, Value src1, Value src2)
  101. {
  102. return Value(TRY(is_loosely_equal(vm, src1, src2)));
  103. }
  104. static ThrowCompletionOr<Value> typed_inequals(VM&, Value src1, Value src2)
  105. {
  106. return Value(!is_strictly_equal(src1, src2));
  107. }
  108. static ThrowCompletionOr<Value> typed_equals(VM&, Value src1, Value src2)
  109. {
  110. return Value(is_strictly_equal(src1, src2));
  111. }
  112. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  113. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  114. { \
  115. auto& vm = interpreter.vm(); \
  116. auto lhs = interpreter.reg(m_lhs_reg); \
  117. auto rhs = interpreter.accumulator(); \
  118. interpreter.accumulator() = TRY(op_snake_case(vm, lhs, rhs)); \
  119. return {}; \
  120. } \
  121. DeprecatedString OpTitleCase::to_deprecated_string_impl(Bytecode::Executable const&) const \
  122. { \
  123. return DeprecatedString::formatted(#OpTitleCase " {}", m_lhs_reg); \
  124. }
  125. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  126. static ThrowCompletionOr<Value> not_(VM&, Value value)
  127. {
  128. return Value(!value.to_boolean());
  129. }
  130. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  131. {
  132. return MUST_OR_THROW_OOM(PrimitiveString::create(vm, value.typeof()));
  133. }
  134. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  135. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  136. { \
  137. auto& vm = interpreter.vm(); \
  138. interpreter.accumulator() = TRY(op_snake_case(vm, interpreter.accumulator())); \
  139. return {}; \
  140. } \
  141. DeprecatedString OpTitleCase::to_deprecated_string_impl(Bytecode::Executable const&) const \
  142. { \
  143. return #OpTitleCase; \
  144. }
  145. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  146. ThrowCompletionOr<void> NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
  147. {
  148. auto& vm = interpreter.vm();
  149. interpreter.accumulator() = BigInt::create(vm, m_bigint);
  150. return {};
  151. }
  152. ThrowCompletionOr<void> NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  153. {
  154. auto array = MUST(Array::create(interpreter.realm(), 0));
  155. for (size_t i = 0; i < m_element_count; i++) {
  156. auto& value = interpreter.reg(Register(m_elements[0].index() + i));
  157. array->indexed_properties().put(i, value, default_attributes);
  158. }
  159. interpreter.accumulator() = array;
  160. return {};
  161. }
  162. ThrowCompletionOr<void> Append::execute_impl(Bytecode::Interpreter& interpreter) const
  163. {
  164. // Note: This OpCode is used to construct array literals and argument arrays for calls,
  165. // containing at least one spread element,
  166. // Iterating over such a spread element to unpack it has to be visible by
  167. // the user courtesy of
  168. // (1) https://tc39.es/ecma262/#sec-runtime-semantics-arrayaccumulation
  169. // SpreadElement : ... AssignmentExpression
  170. // 1. Let spreadRef be ? Evaluation of AssignmentExpression.
  171. // 2. Let spreadObj be ? GetValue(spreadRef).
  172. // 3. Let iteratorRecord be ? GetIterator(spreadObj).
  173. // 4. Repeat,
  174. // a. Let next be ? IteratorStep(iteratorRecord).
  175. // b. If next is false, return nextIndex.
  176. // c. Let nextValue be ? IteratorValue(next).
  177. // d. Perform ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(nextIndex)), nextValue).
  178. // e. Set nextIndex to nextIndex + 1.
  179. // (2) https://tc39.es/ecma262/#sec-runtime-semantics-argumentlistevaluation
  180. // ArgumentList : ... AssignmentExpression
  181. // 1. Let list be a new empty List.
  182. // 2. Let spreadRef be ? Evaluation of AssignmentExpression.
  183. // 3. Let spreadObj be ? GetValue(spreadRef).
  184. // 4. Let iteratorRecord be ? GetIterator(spreadObj).
  185. // 5. Repeat,
  186. // a. Let next be ? IteratorStep(iteratorRecord).
  187. // b. If next is false, return list.
  188. // c. Let nextArg be ? IteratorValue(next).
  189. // d. Append nextArg to list.
  190. // ArgumentList : ArgumentList , ... AssignmentExpression
  191. // 1. Let precedingArgs be ? ArgumentListEvaluation of ArgumentList.
  192. // 2. Let spreadRef be ? Evaluation of AssignmentExpression.
  193. // 3. Let iteratorRecord be ? GetIterator(? GetValue(spreadRef)).
  194. // 4. Repeat,
  195. // a. Let next be ? IteratorStep(iteratorRecord).
  196. // b. If next is false, return precedingArgs.
  197. // c. Let nextArg be ? IteratorValue(next).
  198. // d. Append nextArg to precedingArgs.
  199. auto& vm = interpreter.vm();
  200. // Note: We know from codegen, that lhs is a plain array with only indexed properties
  201. auto& lhs = interpreter.reg(m_lhs).as_array();
  202. auto lhs_size = lhs.indexed_properties().array_like_size();
  203. auto rhs = interpreter.accumulator();
  204. if (m_is_spread) {
  205. // ...rhs
  206. size_t i = lhs_size;
  207. TRY(get_iterator_values(vm, rhs, [&i, &lhs](Value iterator_value) -> Optional<Completion> {
  208. lhs.indexed_properties().put(i, iterator_value, default_attributes);
  209. ++i;
  210. return {};
  211. }));
  212. } else {
  213. lhs.indexed_properties().put(lhs_size, rhs, default_attributes);
  214. }
  215. return {};
  216. }
  217. ThrowCompletionOr<void> ImportCall::execute_impl(Bytecode::Interpreter& interpreter) const
  218. {
  219. auto& vm = interpreter.vm();
  220. auto specifier = interpreter.reg(m_specifier);
  221. auto options_value = interpreter.reg(m_options);
  222. interpreter.accumulator() = TRY(perform_import_call(vm, specifier, options_value));
  223. return {};
  224. }
  225. void ImportCall::replace_references_impl(Register from, Register to)
  226. {
  227. if (m_specifier == from)
  228. m_specifier = to;
  229. if (m_options == from)
  230. m_options = to;
  231. }
  232. // FIXME: Since the accumulator is a Value, we store an object there and have to convert back and forth between that an Iterator records. Not great.
  233. // Make sure to put this into the accumulator before the iterator object disappears from the stack to prevent the members from being GC'd.
  234. static Object* iterator_to_object(VM& vm, IteratorRecord iterator)
  235. {
  236. auto& realm = *vm.current_realm();
  237. auto object = Object::create(realm, nullptr);
  238. object->define_direct_property(vm.names.iterator, iterator.iterator, 0);
  239. object->define_direct_property(vm.names.next, iterator.next_method, 0);
  240. object->define_direct_property(vm.names.done, Value(iterator.done), 0);
  241. return object;
  242. }
  243. static IteratorRecord object_to_iterator(VM& vm, Object& object)
  244. {
  245. return IteratorRecord {
  246. .iterator = &MUST(object.get(vm.names.iterator)).as_object(),
  247. .next_method = MUST(object.get(vm.names.next)),
  248. .done = MUST(object.get(vm.names.done)).as_bool()
  249. };
  250. }
  251. ThrowCompletionOr<void> IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const
  252. {
  253. auto& vm = interpreter.vm();
  254. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  255. auto iterator = object_to_iterator(vm, iterator_object);
  256. auto array = MUST(Array::create(interpreter.realm(), 0));
  257. size_t index = 0;
  258. while (true) {
  259. auto iterator_result = TRY(iterator_next(vm, iterator));
  260. auto complete = TRY(iterator_complete(vm, iterator_result));
  261. if (complete) {
  262. interpreter.accumulator() = array;
  263. return {};
  264. }
  265. auto value = TRY(iterator_value(vm, iterator_result));
  266. MUST(array->create_data_property_or_throw(index, value));
  267. index++;
  268. }
  269. return {};
  270. }
  271. ThrowCompletionOr<void> NewString::execute_impl(Bytecode::Interpreter& interpreter) const
  272. {
  273. interpreter.accumulator() = PrimitiveString::create(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  274. return {};
  275. }
  276. ThrowCompletionOr<void> NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  277. {
  278. auto& vm = interpreter.vm();
  279. auto& realm = *vm.current_realm();
  280. interpreter.accumulator() = Object::create(realm, realm.intrinsics().object_prototype());
  281. return {};
  282. }
  283. ThrowCompletionOr<void> NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
  284. {
  285. auto& vm = interpreter.vm();
  286. auto source = interpreter.current_executable().get_string(m_source_index);
  287. auto flags = interpreter.current_executable().get_string(m_flags_index);
  288. interpreter.accumulator() = TRY(regexp_create(vm, PrimitiveString::create(vm, source), PrimitiveString::create(vm, flags)));
  289. return {};
  290. }
  291. #define JS_DEFINE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  292. ThrowCompletionOr<void> New##ErrorName::execute_impl(Bytecode::Interpreter& interpreter) const \
  293. { \
  294. auto& vm = interpreter.vm(); \
  295. auto& realm = *vm.current_realm(); \
  296. interpreter.accumulator() = MUST_OR_THROW_OOM(ErrorName::create(realm, interpreter.current_executable().get_string(m_error_string))); \
  297. return {}; \
  298. } \
  299. DeprecatedString New##ErrorName::to_deprecated_string_impl(Bytecode::Executable const& executable) const \
  300. { \
  301. return DeprecatedString::formatted("New" #ErrorName " {} (\"{}\")", m_error_string, executable.string_table->get(m_error_string)); \
  302. }
  303. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DEFINE_NEW_BUILTIN_ERROR_OP)
  304. ThrowCompletionOr<void> CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const
  305. {
  306. auto& vm = interpreter.vm();
  307. auto& realm = *vm.current_realm();
  308. auto from_object = interpreter.reg(m_from_object);
  309. auto to_object = Object::create(realm, realm.intrinsics().object_prototype());
  310. HashTable<PropertyKey> excluded_names;
  311. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  312. excluded_names.set(TRY(interpreter.reg(m_excluded_names[i]).to_property_key(vm)));
  313. }
  314. TRY(to_object->copy_data_properties(vm, from_object, excluded_names));
  315. interpreter.accumulator() = to_object;
  316. return {};
  317. }
  318. ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  319. {
  320. auto& vm = interpreter.vm();
  321. auto string = TRY(interpreter.accumulator().to_primitive_string(vm));
  322. interpreter.reg(m_lhs) = PrimitiveString::create(vm, interpreter.reg(m_lhs).as_string(), string);
  323. return {};
  324. }
  325. ThrowCompletionOr<void> GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  326. {
  327. auto& vm = interpreter.vm();
  328. auto get_reference = [&]() -> ThrowCompletionOr<Reference> {
  329. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  330. if (m_cached_environment_coordinate.has_value()) {
  331. Environment* environment = nullptr;
  332. bool coordinate_screwed_by_delete_in_global_environment = false;
  333. if (m_cached_environment_coordinate->index == EnvironmentCoordinate::global_marker) {
  334. environment = &interpreter.vm().current_realm()->global_environment();
  335. coordinate_screwed_by_delete_in_global_environment = !TRY(environment->has_binding(string));
  336. } else {
  337. environment = vm.running_execution_context().lexical_environment;
  338. for (size_t i = 0; i < m_cached_environment_coordinate->hops; ++i)
  339. environment = environment->outer_environment();
  340. VERIFY(environment);
  341. VERIFY(environment->is_declarative_environment());
  342. }
  343. if (!coordinate_screwed_by_delete_in_global_environment && !environment->is_permanently_screwed_by_eval()) {
  344. return Reference { *environment, string, vm.in_strict_mode(), m_cached_environment_coordinate };
  345. }
  346. m_cached_environment_coordinate = {};
  347. }
  348. auto reference = TRY(vm.resolve_binding(string));
  349. if (reference.environment_coordinate().has_value())
  350. m_cached_environment_coordinate = reference.environment_coordinate();
  351. return reference;
  352. };
  353. auto reference = TRY(get_reference());
  354. interpreter.accumulator() = TRY(reference.get_value(vm));
  355. return {};
  356. }
  357. ThrowCompletionOr<void> GetLocal::execute_impl(Bytecode::Interpreter& interpreter) const
  358. {
  359. auto& vm = interpreter.vm();
  360. if (vm.running_execution_context().local_variables[m_index].is_empty()) {
  361. auto const& variable_name = vm.running_execution_context().function->local_variables_names()[m_index];
  362. return interpreter.vm().throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, variable_name);
  363. }
  364. interpreter.accumulator() = vm.running_execution_context().local_variables[m_index];
  365. return {};
  366. }
  367. ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  368. {
  369. auto& vm = interpreter.vm();
  370. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  371. auto reference = TRY(vm.resolve_binding(string));
  372. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  373. return {};
  374. }
  375. ThrowCompletionOr<void> CreateLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  376. {
  377. auto make_and_swap_envs = [&](auto& old_environment) {
  378. GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
  379. swap(old_environment, environment);
  380. return environment;
  381. };
  382. interpreter.saved_lexical_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().lexical_environment));
  383. return {};
  384. }
  385. ThrowCompletionOr<void> EnterObjectEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  386. {
  387. auto& vm = interpreter.vm();
  388. auto& old_environment = vm.running_execution_context().lexical_environment;
  389. interpreter.saved_lexical_environment_stack().append(old_environment);
  390. auto object = TRY(interpreter.accumulator().to_object(vm));
  391. vm.running_execution_context().lexical_environment = new_object_environment(object, true, old_environment);
  392. return {};
  393. }
  394. ThrowCompletionOr<void> CreateVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  395. {
  396. auto& vm = interpreter.vm();
  397. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  398. if (m_mode == EnvironmentMode::Lexical) {
  399. VERIFY(!m_is_global);
  400. // Note: This is papering over an issue where "FunctionDeclarationInstantiation" creates these bindings for us.
  401. // Instead of crashing in there, we'll just raise an exception here.
  402. if (TRY(vm.lexical_environment()->has_binding(name)))
  403. return vm.throw_completion<InternalError>(TRY_OR_THROW_OOM(vm, String::formatted("Lexical environment already has binding '{}'", name)));
  404. if (m_is_immutable)
  405. return vm.lexical_environment()->create_immutable_binding(vm, name, vm.in_strict_mode());
  406. else
  407. return vm.lexical_environment()->create_mutable_binding(vm, name, vm.in_strict_mode());
  408. } else {
  409. if (!m_is_global) {
  410. if (m_is_immutable)
  411. return vm.variable_environment()->create_immutable_binding(vm, name, vm.in_strict_mode());
  412. else
  413. return vm.variable_environment()->create_mutable_binding(vm, name, vm.in_strict_mode());
  414. } else {
  415. // NOTE: CreateVariable with m_is_global set to true is expected to only be used in GlobalDeclarationInstantiation currently, which only uses "false" for "can_be_deleted".
  416. // The only area that sets "can_be_deleted" to true is EvalDeclarationInstantiation, which is currently fully implemented in C++ and not in Bytecode.
  417. return verify_cast<GlobalEnvironment>(vm.variable_environment())->create_global_var_binding(name, false);
  418. }
  419. }
  420. return {};
  421. }
  422. ThrowCompletionOr<void> SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  423. {
  424. auto& vm = interpreter.vm();
  425. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  426. auto environment = m_mode == EnvironmentMode::Lexical ? vm.running_execution_context().lexical_environment : vm.running_execution_context().variable_environment;
  427. auto reference = TRY(vm.resolve_binding(name, environment));
  428. switch (m_initialization_mode) {
  429. case InitializationMode::Initialize:
  430. TRY(reference.initialize_referenced_binding(vm, interpreter.accumulator()));
  431. break;
  432. case InitializationMode::Set:
  433. TRY(reference.put_value(vm, interpreter.accumulator()));
  434. break;
  435. case InitializationMode::InitializeOrSet:
  436. VERIFY(reference.is_environment_reference());
  437. VERIFY(reference.base_environment().is_declarative_environment());
  438. TRY(static_cast<DeclarativeEnvironment&>(reference.base_environment()).initialize_or_set_mutable_binding(vm, name, interpreter.accumulator()));
  439. break;
  440. }
  441. return {};
  442. }
  443. ThrowCompletionOr<void> SetLocal::execute_impl(Bytecode::Interpreter& interpreter) const
  444. {
  445. interpreter.vm().running_execution_context().local_variables[m_index] = interpreter.accumulator();
  446. return {};
  447. }
  448. static ThrowCompletionOr<void> get_by_id(Bytecode::Interpreter& interpreter, IdentifierTableIndex property, Value base_value, Value this_value)
  449. {
  450. auto& vm = interpreter.vm();
  451. auto const& name = interpreter.current_executable().get_identifier(property);
  452. // OPTIMIZATION: For various primitives we can avoid actually creating a new object for them.
  453. GCPtr<Object> base_obj;
  454. if (base_value.is_string()) {
  455. auto string_value = TRY(base_value.as_string().get(vm, name));
  456. if (string_value.has_value()) {
  457. interpreter.accumulator() = *string_value;
  458. return {};
  459. }
  460. base_obj = vm.current_realm()->intrinsics().string_prototype();
  461. } else if (base_value.is_number()) {
  462. base_obj = vm.current_realm()->intrinsics().number_prototype();
  463. } else if (base_value.is_boolean()) {
  464. base_obj = vm.current_realm()->intrinsics().boolean_prototype();
  465. } else {
  466. base_obj = TRY(base_value.to_object(vm));
  467. }
  468. interpreter.accumulator() = TRY(base_obj->internal_get(name, this_value));
  469. return {};
  470. }
  471. ThrowCompletionOr<void> GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  472. {
  473. auto base_value = interpreter.accumulator();
  474. return get_by_id(interpreter, m_property, base_value, base_value);
  475. }
  476. ThrowCompletionOr<void> GetByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  477. {
  478. auto base_value = interpreter.accumulator();
  479. auto this_value = interpreter.reg(m_this_value);
  480. return get_by_id(interpreter, m_property, base_value, this_value);
  481. }
  482. ThrowCompletionOr<void> GetPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const
  483. {
  484. auto& vm = interpreter.vm();
  485. auto const& name = interpreter.current_executable().get_identifier(m_property);
  486. auto base_value = interpreter.accumulator();
  487. auto private_reference = make_private_reference(vm, base_value, name);
  488. interpreter.accumulator() = TRY(private_reference.get_value(vm));
  489. return {};
  490. }
  491. ThrowCompletionOr<void> HasPrivateId::execute_impl(Bytecode::Interpreter& interpreter) const
  492. {
  493. auto& vm = interpreter.vm();
  494. if (!interpreter.accumulator().is_object())
  495. return vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject);
  496. auto private_environment = vm.running_execution_context().private_environment;
  497. VERIFY(private_environment);
  498. auto private_name = private_environment->resolve_private_identifier(interpreter.current_executable().get_identifier(m_property));
  499. interpreter.accumulator() = Value(interpreter.accumulator().as_object().private_element_find(private_name) != nullptr);
  500. return {};
  501. }
  502. ThrowCompletionOr<void> PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  503. {
  504. auto& vm = interpreter.vm();
  505. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  506. auto value = interpreter.accumulator();
  507. auto base = interpreter.reg(m_base);
  508. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  509. TRY(put_by_property_key(vm, base, base, value, name, m_kind));
  510. interpreter.accumulator() = value;
  511. return {};
  512. }
  513. ThrowCompletionOr<void> PutByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  514. {
  515. auto& vm = interpreter.vm();
  516. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  517. auto value = interpreter.accumulator();
  518. auto base = interpreter.reg(m_base);
  519. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  520. TRY(put_by_property_key(vm, base, interpreter.reg(m_this_value), value, name, m_kind));
  521. interpreter.accumulator() = value;
  522. return {};
  523. }
  524. ThrowCompletionOr<void> PutPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const
  525. {
  526. auto& vm = interpreter.vm();
  527. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  528. auto value = interpreter.accumulator();
  529. auto object = TRY(interpreter.reg(m_base).to_object(vm));
  530. auto name = interpreter.current_executable().get_identifier(m_property);
  531. auto private_reference = make_private_reference(vm, object, name);
  532. TRY(private_reference.put_value(vm, value));
  533. interpreter.accumulator() = value;
  534. return {};
  535. }
  536. ThrowCompletionOr<void> DeleteById::execute_impl(Bytecode::Interpreter& interpreter) const
  537. {
  538. auto& vm = interpreter.vm();
  539. auto base_value = interpreter.accumulator();
  540. auto const& identifier = interpreter.current_executable().get_identifier(m_property);
  541. bool strict = vm.in_strict_mode();
  542. auto reference = Reference { base_value, identifier, {}, strict };
  543. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  544. return {};
  545. };
  546. ThrowCompletionOr<void> Jump::execute_impl(Bytecode::Interpreter& interpreter) const
  547. {
  548. interpreter.jump(*m_true_target);
  549. return {};
  550. }
  551. ThrowCompletionOr<void> ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const
  552. {
  553. auto& vm = interpreter.vm();
  554. interpreter.accumulator() = TRY(vm.resolve_this_binding());
  555. return {};
  556. }
  557. // https://tc39.es/ecma262/#sec-makesuperpropertyreference
  558. ThrowCompletionOr<void> ResolveSuperBase::execute_impl(Bytecode::Interpreter& interpreter) const
  559. {
  560. auto& vm = interpreter.vm();
  561. // 1. Let env be GetThisEnvironment().
  562. auto& env = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  563. // 2. Assert: env.HasSuperBinding() is true.
  564. VERIFY(env.has_super_binding());
  565. // 3. Let baseValue be ? env.GetSuperBase().
  566. interpreter.accumulator() = TRY(env.get_super_base());
  567. return {};
  568. }
  569. ThrowCompletionOr<void> GetNewTarget::execute_impl(Bytecode::Interpreter& interpreter) const
  570. {
  571. interpreter.accumulator() = interpreter.vm().get_new_target();
  572. return {};
  573. }
  574. void Jump::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  575. {
  576. if (m_true_target.has_value() && &m_true_target->block() == &from)
  577. m_true_target = Label { to };
  578. if (m_false_target.has_value() && &m_false_target->block() == &from)
  579. m_false_target = Label { to };
  580. }
  581. ThrowCompletionOr<void> JumpConditional::execute_impl(Bytecode::Interpreter& interpreter) const
  582. {
  583. VERIFY(m_true_target.has_value());
  584. VERIFY(m_false_target.has_value());
  585. auto result = interpreter.accumulator();
  586. if (result.to_boolean())
  587. interpreter.jump(m_true_target.value());
  588. else
  589. interpreter.jump(m_false_target.value());
  590. return {};
  591. }
  592. ThrowCompletionOr<void> JumpNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  593. {
  594. VERIFY(m_true_target.has_value());
  595. VERIFY(m_false_target.has_value());
  596. auto result = interpreter.accumulator();
  597. if (result.is_nullish())
  598. interpreter.jump(m_true_target.value());
  599. else
  600. interpreter.jump(m_false_target.value());
  601. return {};
  602. }
  603. ThrowCompletionOr<void> JumpUndefined::execute_impl(Bytecode::Interpreter& interpreter) const
  604. {
  605. VERIFY(m_true_target.has_value());
  606. VERIFY(m_false_target.has_value());
  607. auto result = interpreter.accumulator();
  608. if (result.is_undefined())
  609. interpreter.jump(m_true_target.value());
  610. else
  611. interpreter.jump(m_false_target.value());
  612. return {};
  613. }
  614. // 13.3.8.1 https://tc39.es/ecma262/#sec-runtime-semantics-argumentlistevaluation
  615. static MarkedVector<Value> argument_list_evaluation(Bytecode::Interpreter& interpreter)
  616. {
  617. // Note: Any spreading and actual evaluation is handled in preceding opcodes
  618. // Note: The spec uses the concept of a list, while we create a temporary array
  619. // in the preceding opcodes, so we have to convert in a manner that is not
  620. // visible to the user
  621. auto& vm = interpreter.vm();
  622. MarkedVector<Value> argument_values { vm.heap() };
  623. auto arguments = interpreter.accumulator();
  624. if (!(arguments.is_object() && is<Array>(arguments.as_object()))) {
  625. dbgln("[{}] Call arguments are not an array, but: {}", interpreter.debug_position(), MUST(arguments.to_string_without_side_effects()));
  626. interpreter.current_executable().dump();
  627. VERIFY_NOT_REACHED();
  628. }
  629. auto& argument_array = arguments.as_array();
  630. auto array_length = argument_array.indexed_properties().array_like_size();
  631. argument_values.ensure_capacity(array_length);
  632. for (size_t i = 0; i < array_length; ++i) {
  633. if (auto maybe_value = argument_array.indexed_properties().get(i); maybe_value.has_value())
  634. argument_values.append(maybe_value.release_value().value);
  635. else
  636. argument_values.append(js_undefined());
  637. }
  638. return argument_values;
  639. }
  640. static Completion throw_type_error_for_callee(Bytecode::Interpreter& interpreter, auto& call, StringView callee_type)
  641. {
  642. auto& vm = interpreter.vm();
  643. auto callee = interpreter.reg(call.callee());
  644. if (call.expression_string().has_value())
  645. return vm.throw_completion<TypeError>(ErrorType::IsNotAEvaluatedFrom, TRY_OR_THROW_OOM(vm, callee.to_string_without_side_effects()), callee_type, interpreter.current_executable().get_string(call.expression_string()->value()));
  646. return vm.throw_completion<TypeError>(ErrorType::IsNotA, TRY_OR_THROW_OOM(vm, callee.to_string_without_side_effects()), callee_type);
  647. }
  648. static ThrowCompletionOr<void> throw_if_needed_for_call(Interpreter& interpreter, auto& call, Value callee)
  649. {
  650. if (call.call_type() == CallType::Call && !callee.is_function())
  651. return throw_type_error_for_callee(interpreter, call, "function"sv);
  652. if (call.call_type() == CallType::Construct && !callee.is_constructor())
  653. return throw_type_error_for_callee(interpreter, call, "constructor"sv);
  654. return {};
  655. }
  656. static ThrowCompletionOr<void> perform_call(Interpreter& interpreter, auto& call, Value callee, MarkedVector<Value> argument_values)
  657. {
  658. auto& vm = interpreter.vm();
  659. auto this_value = interpreter.reg(call.this_value());
  660. auto& function = callee.as_function();
  661. Value return_value;
  662. if (call.call_type() == CallType::DirectEval) {
  663. if (callee == interpreter.realm().intrinsics().eval_function())
  664. return_value = TRY(perform_eval(vm, !argument_values.is_empty() ? argument_values[0].value_or(JS::js_undefined()) : js_undefined(), vm.in_strict_mode() ? CallerMode::Strict : CallerMode::NonStrict, EvalMode::Direct));
  665. else
  666. return_value = TRY(JS::call(vm, function, this_value, move(argument_values)));
  667. } else if (call.call_type() == CallType::Call)
  668. return_value = TRY(JS::call(vm, function, this_value, move(argument_values)));
  669. else
  670. return_value = TRY(construct(vm, function, move(argument_values)));
  671. interpreter.accumulator() = return_value;
  672. return {};
  673. }
  674. ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) const
  675. {
  676. auto& vm = interpreter.vm();
  677. auto callee = interpreter.reg(m_callee);
  678. TRY(throw_if_needed_for_call(interpreter, *this, callee));
  679. MarkedVector<Value> argument_values(vm.heap());
  680. argument_values.ensure_capacity(m_argument_count);
  681. for (u32 i = 0; i < m_argument_count; ++i) {
  682. argument_values.unchecked_append(interpreter.reg(Register { m_first_argument.index() + i }));
  683. }
  684. return perform_call(interpreter, *this, callee, move(argument_values));
  685. }
  686. ThrowCompletionOr<void> CallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  687. {
  688. auto callee = interpreter.reg(m_callee);
  689. TRY(throw_if_needed_for_call(interpreter, *this, callee));
  690. auto argument_values = argument_list_evaluation(interpreter);
  691. return perform_call(interpreter, *this, callee, move(argument_values));
  692. }
  693. // 13.3.7.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  694. ThrowCompletionOr<void> SuperCallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  695. {
  696. auto& vm = interpreter.vm();
  697. // 1. Let newTarget be GetNewTarget().
  698. auto new_target = vm.get_new_target();
  699. // 2. Assert: Type(newTarget) is Object.
  700. VERIFY(new_target.is_object());
  701. // 3. Let func be GetSuperConstructor().
  702. auto* func = get_super_constructor(vm);
  703. // 4. Let argList be ? ArgumentListEvaluation of Arguments.
  704. MarkedVector<Value> arg_list { vm.heap() };
  705. if (m_is_synthetic) {
  706. auto const& value = interpreter.accumulator();
  707. VERIFY(value.is_object() && is<Array>(value.as_object()));
  708. auto const& array_value = static_cast<Array const&>(value.as_object());
  709. auto length = MUST(length_of_array_like(vm, array_value));
  710. for (size_t i = 0; i < length; ++i)
  711. arg_list.append(array_value.get_without_side_effects(PropertyKey { i }));
  712. } else {
  713. arg_list = argument_list_evaluation(interpreter);
  714. }
  715. // 5. If IsConstructor(func) is false, throw a TypeError exception.
  716. if (!Value(func).is_constructor())
  717. return vm.throw_completion<TypeError>(ErrorType::NotAConstructor, "Super constructor");
  718. // 6. Let result be ? Construct(func, argList, newTarget).
  719. auto result = TRY(construct(vm, static_cast<FunctionObject&>(*func), move(arg_list), &new_target.as_function()));
  720. // 7. Let thisER be GetThisEnvironment().
  721. auto& this_environment = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  722. // 8. Perform ? thisER.BindThisValue(result).
  723. TRY(this_environment.bind_this_value(vm, result));
  724. // 9. Let F be thisER.[[FunctionObject]].
  725. auto& f = this_environment.function_object();
  726. // 10. Assert: F is an ECMAScript function object.
  727. // NOTE: This is implied by the strong C++ type.
  728. // 11. Perform ? InitializeInstanceElements(result, F).
  729. TRY(result->initialize_instance_elements(f));
  730. // 12. Return result.
  731. interpreter.accumulator() = result;
  732. return {};
  733. }
  734. ThrowCompletionOr<void> NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  735. {
  736. auto& vm = interpreter.vm();
  737. if (!m_function_node.has_name()) {
  738. DeprecatedFlyString name = {};
  739. if (m_lhs_name.has_value())
  740. name = interpreter.current_executable().get_identifier(m_lhs_name.value());
  741. interpreter.accumulator() = m_function_node.instantiate_ordinary_function_expression(vm, name);
  742. } else {
  743. interpreter.accumulator() = ECMAScriptFunctionObject::create(interpreter.realm(), m_function_node.name(), m_function_node.source_text(), m_function_node.body(), m_function_node.parameters(), m_function_node.function_length(), m_function_node.local_variables_names(), vm.lexical_environment(), vm.running_execution_context().private_environment, m_function_node.kind(), m_function_node.is_strict_mode(), m_function_node.might_need_arguments_object(), m_function_node.contains_direct_call_to_eval(), m_function_node.is_arrow_function());
  744. }
  745. if (m_home_object.has_value()) {
  746. auto home_object_value = interpreter.reg(m_home_object.value());
  747. static_cast<ECMAScriptFunctionObject&>(interpreter.accumulator().as_function()).set_home_object(&home_object_value.as_object());
  748. }
  749. return {};
  750. }
  751. ThrowCompletionOr<void> Return::execute_impl(Bytecode::Interpreter& interpreter) const
  752. {
  753. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  754. return {};
  755. }
  756. ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  757. {
  758. auto& vm = interpreter.vm();
  759. auto old_value = TRY(interpreter.accumulator().to_numeric(vm));
  760. if (old_value.is_number())
  761. interpreter.accumulator() = Value(old_value.as_double() + 1);
  762. else
  763. interpreter.accumulator() = BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  764. return {};
  765. }
  766. ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  767. {
  768. auto& vm = interpreter.vm();
  769. auto old_value = TRY(interpreter.accumulator().to_numeric(vm));
  770. if (old_value.is_number())
  771. interpreter.accumulator() = Value(old_value.as_double() - 1);
  772. else
  773. interpreter.accumulator() = BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  774. return {};
  775. }
  776. ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  777. {
  778. return throw_completion(interpreter.accumulator());
  779. }
  780. ThrowCompletionOr<void> ThrowIfNotObject::execute_impl(Bytecode::Interpreter& interpreter) const
  781. {
  782. auto& vm = interpreter.vm();
  783. if (!interpreter.accumulator().is_object())
  784. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, TRY_OR_THROW_OOM(vm, interpreter.accumulator().to_string_without_side_effects()));
  785. return {};
  786. }
  787. ThrowCompletionOr<void> ThrowIfNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  788. {
  789. auto& vm = interpreter.vm();
  790. auto value = interpreter.accumulator();
  791. if (value.is_nullish())
  792. return vm.throw_completion<TypeError>(ErrorType::NotObjectCoercible, TRY_OR_THROW_OOM(vm, value.to_string_without_side_effects()));
  793. return {};
  794. }
  795. ThrowCompletionOr<void> EnterUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  796. {
  797. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  798. interpreter.jump(m_entry_point);
  799. return {};
  800. }
  801. void NewFunction::replace_references_impl(Register from, Register to)
  802. {
  803. if (m_home_object == from)
  804. m_home_object = to;
  805. }
  806. void EnterUnwindContext::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  807. {
  808. if (&m_entry_point.block() == &from)
  809. m_entry_point = Label { to };
  810. if (m_handler_target.has_value() && &m_handler_target->block() == &from)
  811. m_handler_target = Label { to };
  812. if (m_finalizer_target.has_value() && &m_finalizer_target->block() == &from)
  813. m_finalizer_target = Label { to };
  814. }
  815. void CopyObjectExcludingProperties::replace_references_impl(Register from, Register to)
  816. {
  817. if (m_from_object == from)
  818. m_from_object = to;
  819. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  820. if (m_excluded_names[i] == from)
  821. m_excluded_names[i] = to;
  822. }
  823. }
  824. void Call::replace_references_impl(Register from, Register to)
  825. {
  826. if (m_callee == from)
  827. m_callee = to;
  828. if (m_this_value == from)
  829. m_this_value = to;
  830. if (m_first_argument == from)
  831. m_first_argument = to;
  832. }
  833. void CallWithArgumentArray::replace_references_impl(Register from, Register to)
  834. {
  835. if (m_callee == from)
  836. m_callee = to;
  837. if (m_this_value == from)
  838. m_this_value = to;
  839. }
  840. ThrowCompletionOr<void> ScheduleJump::execute_impl(Bytecode::Interpreter& interpreter) const
  841. {
  842. interpreter.schedule_jump(m_target);
  843. return {};
  844. }
  845. ThrowCompletionOr<void> LeaveLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  846. {
  847. interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
  848. return {};
  849. }
  850. ThrowCompletionOr<void> LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  851. {
  852. interpreter.leave_unwind_context();
  853. return {};
  854. }
  855. ThrowCompletionOr<void> ContinuePendingUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  856. {
  857. return interpreter.continue_pending_unwind(m_resume_target);
  858. }
  859. void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  860. {
  861. if (&m_resume_target.block() == &from)
  862. m_resume_target = Label { to };
  863. }
  864. ThrowCompletionOr<void> PushDeclarativeEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  865. {
  866. auto environment = interpreter.vm().heap().allocate_without_realm<DeclarativeEnvironment>(interpreter.vm().lexical_environment());
  867. interpreter.vm().running_execution_context().lexical_environment = environment;
  868. interpreter.vm().running_execution_context().variable_environment = environment;
  869. return {};
  870. }
  871. ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  872. {
  873. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  874. auto object = Object::create(interpreter.realm(), nullptr);
  875. object->define_direct_property("result", yielded_value, JS::default_attributes);
  876. if (m_continuation_label.has_value())
  877. // FIXME: If we get a pointer, which is not accurately representable as a double
  878. // will cause this to explode
  879. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes);
  880. else
  881. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  882. interpreter.do_return(object);
  883. return {};
  884. }
  885. void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  886. {
  887. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  888. m_continuation_label = Label { to };
  889. }
  890. ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  891. {
  892. auto& vm = interpreter.vm();
  893. // NOTE: Get the property key from the accumulator before side effects have a chance to overwrite it.
  894. auto property_key_value = interpreter.accumulator();
  895. auto object = TRY(interpreter.reg(m_base).to_object(vm));
  896. auto property_key = TRY(property_key_value.to_property_key(vm));
  897. interpreter.accumulator() = TRY(object->get(property_key));
  898. return {};
  899. }
  900. ThrowCompletionOr<void> GetByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  901. {
  902. auto& vm = interpreter.vm();
  903. // NOTE: Get the property key from the accumulator before side effects have a chance to overwrite it.
  904. auto property_key_value = interpreter.accumulator();
  905. auto object = TRY(interpreter.reg(m_base).to_object(vm));
  906. auto property_key = TRY(property_key_value.to_property_key(vm));
  907. interpreter.accumulator() = TRY(object->internal_get(property_key, interpreter.reg(m_this_value)));
  908. return {};
  909. }
  910. ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  911. {
  912. auto& vm = interpreter.vm();
  913. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  914. auto value = interpreter.accumulator();
  915. auto base = interpreter.reg(m_base);
  916. auto property_key = TRY(interpreter.reg(m_property).to_property_key(vm));
  917. TRY(put_by_property_key(vm, base, base, value, property_key, m_kind));
  918. interpreter.accumulator() = value;
  919. return {};
  920. }
  921. ThrowCompletionOr<void> PutByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  922. {
  923. auto& vm = interpreter.vm();
  924. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  925. auto value = interpreter.accumulator();
  926. auto base = interpreter.reg(m_base);
  927. auto property_key = TRY(interpreter.reg(m_property).to_property_key(vm));
  928. TRY(put_by_property_key(vm, base, interpreter.reg(m_this_value), value, property_key, m_kind));
  929. interpreter.accumulator() = value;
  930. return {};
  931. }
  932. ThrowCompletionOr<void> DeleteByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  933. {
  934. auto& vm = interpreter.vm();
  935. // NOTE: Get the property key from the accumulator before side effects have a chance to overwrite it.
  936. auto property_key_value = interpreter.accumulator();
  937. auto base_value = interpreter.reg(m_base);
  938. auto property_key = TRY(property_key_value.to_property_key(vm));
  939. bool strict = vm.in_strict_mode();
  940. auto reference = Reference { base_value, property_key, {}, strict };
  941. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  942. return {};
  943. }
  944. ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  945. {
  946. auto& vm = interpreter.vm();
  947. auto iterator = TRY(get_iterator(vm, interpreter.accumulator(), m_hint));
  948. interpreter.accumulator() = iterator_to_object(vm, iterator);
  949. return {};
  950. }
  951. ThrowCompletionOr<void> GetMethod::execute_impl(Bytecode::Interpreter& interpreter) const
  952. {
  953. auto& vm = interpreter.vm();
  954. auto identifier = interpreter.current_executable().get_identifier(m_property);
  955. auto method = TRY(interpreter.accumulator().get_method(vm, identifier));
  956. interpreter.accumulator() = method ?: js_undefined();
  957. return {};
  958. }
  959. // 14.7.5.9 EnumerateObjectProperties ( O ), https://tc39.es/ecma262/#sec-enumerate-object-properties
  960. ThrowCompletionOr<void> GetObjectPropertyIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  961. {
  962. // While the spec does provide an algorithm, it allows us to implement it ourselves so long as we meet the following invariants:
  963. // 1- Returned property keys do not include keys that are Symbols
  964. // 2- Properties of the target object may be deleted during enumeration. A property that is deleted before it is processed by the iterator's next method is ignored
  965. // 3- If new properties are added to the target object during enumeration, the newly added properties are not guaranteed to be processed in the active enumeration
  966. // 4- A property name will be returned by the iterator's next method at most once in any enumeration.
  967. // 5- Enumerating the properties of the target object includes enumerating properties of its prototype, and the prototype of the prototype, and so on, recursively;
  968. // but a property of a prototype is not processed if it has the same name as a property that has already been processed by the iterator's next method.
  969. // 6- The values of [[Enumerable]] attributes are not considered when determining if a property of a prototype object has already been processed.
  970. // 7- The enumerable property names of prototype objects must be obtained by invoking EnumerateObjectProperties passing the prototype object as the argument.
  971. // 8- EnumerateObjectProperties must obtain the own property keys of the target object by calling its [[OwnPropertyKeys]] internal method.
  972. // 9- Property attributes of the target object must be obtained by calling its [[GetOwnProperty]] internal method
  973. // Invariant 3 effectively allows the implementation to ignore newly added keys, and we do so (similar to other implementations).
  974. auto& vm = interpreter.vm();
  975. auto object = TRY(interpreter.accumulator().to_object(vm));
  976. // Note: While the spec doesn't explicitly require these to be ordered, it says that the values should be retrieved via OwnPropertyKeys,
  977. // so we just keep the order consistent anyway.
  978. OrderedHashTable<PropertyKey> properties;
  979. OrderedHashTable<PropertyKey> non_enumerable_properties;
  980. HashTable<NonnullGCPtr<Object>> seen_objects;
  981. // Collect all keys immediately (invariant no. 5)
  982. for (auto object_to_check = GCPtr { object.ptr() }; object_to_check && !seen_objects.contains(*object_to_check); object_to_check = TRY(object_to_check->internal_get_prototype_of())) {
  983. seen_objects.set(*object_to_check);
  984. for (auto& key : TRY(object_to_check->internal_own_property_keys())) {
  985. if (key.is_symbol())
  986. continue;
  987. auto property_key = TRY(PropertyKey::from_value(vm, key));
  988. // If there is a non-enumerable property higher up the prototype chain with the same key,
  989. // we mustn't include this property even if it's enumerable (invariant no. 5 and 6)
  990. if (non_enumerable_properties.contains(property_key))
  991. continue;
  992. if (properties.contains(property_key))
  993. continue;
  994. auto descriptor = TRY(object_to_check->internal_get_own_property(property_key));
  995. if (!*descriptor->enumerable)
  996. non_enumerable_properties.set(move(property_key));
  997. else
  998. properties.set(move(property_key));
  999. }
  1000. }
  1001. IteratorRecord iterator {
  1002. .iterator = object,
  1003. .next_method = NativeFunction::create(
  1004. interpreter.realm(),
  1005. [items = move(properties)](VM& vm) mutable -> ThrowCompletionOr<Value> {
  1006. auto& realm = *vm.current_realm();
  1007. auto iterated_object_value = vm.this_value();
  1008. if (!iterated_object_value.is_object())
  1009. return vm.throw_completion<InternalError>("Invalid state for GetObjectPropertyIterator.next"sv);
  1010. auto& iterated_object = iterated_object_value.as_object();
  1011. auto result_object = Object::create(realm, nullptr);
  1012. while (true) {
  1013. if (items.is_empty()) {
  1014. result_object->define_direct_property(vm.names.done, JS::Value(true), default_attributes);
  1015. return result_object;
  1016. }
  1017. auto key = items.take_first();
  1018. // If the property is deleted, don't include it (invariant no. 2)
  1019. if (!TRY(iterated_object.has_property(key)))
  1020. continue;
  1021. result_object->define_direct_property(vm.names.done, JS::Value(false), default_attributes);
  1022. if (key.is_number())
  1023. result_object->define_direct_property(vm.names.value, PrimitiveString::create(vm, TRY_OR_THROW_OOM(vm, String::number(key.as_number()))), default_attributes);
  1024. else if (key.is_string())
  1025. result_object->define_direct_property(vm.names.value, PrimitiveString::create(vm, key.as_string()), default_attributes);
  1026. else
  1027. VERIFY_NOT_REACHED(); // We should not have non-string/number keys.
  1028. return result_object;
  1029. }
  1030. },
  1031. 1,
  1032. vm.names.next),
  1033. .done = false,
  1034. };
  1035. interpreter.accumulator() = iterator_to_object(vm, move(iterator));
  1036. return {};
  1037. }
  1038. ThrowCompletionOr<void> IteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1039. {
  1040. auto& vm = interpreter.vm();
  1041. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  1042. auto iterator = object_to_iterator(vm, iterator_object);
  1043. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1044. TRY(iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} }));
  1045. return {};
  1046. }
  1047. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  1048. {
  1049. auto& vm = interpreter.vm();
  1050. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  1051. auto iterator = object_to_iterator(vm, iterator_object);
  1052. interpreter.accumulator() = TRY(iterator_next(vm, iterator));
  1053. return {};
  1054. }
  1055. ThrowCompletionOr<void> IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const
  1056. {
  1057. auto& vm = interpreter.vm();
  1058. auto iterator_result = TRY(interpreter.accumulator().to_object(vm));
  1059. auto complete = TRY(iterator_complete(vm, iterator_result));
  1060. interpreter.accumulator() = Value(complete);
  1061. return {};
  1062. }
  1063. ThrowCompletionOr<void> IteratorResultValue::execute_impl(Bytecode::Interpreter& interpreter) const
  1064. {
  1065. auto& vm = interpreter.vm();
  1066. auto iterator_result = TRY(interpreter.accumulator().to_object(vm));
  1067. interpreter.accumulator() = TRY(iterator_value(vm, iterator_result));
  1068. return {};
  1069. }
  1070. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const
  1071. {
  1072. auto& vm = interpreter.vm();
  1073. auto name = m_class_expression.name();
  1074. auto super_class = interpreter.accumulator();
  1075. // NOTE: NewClass expects classEnv to be active lexical environment
  1076. auto class_environment = vm.lexical_environment();
  1077. vm.running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
  1078. DeprecatedFlyString binding_name;
  1079. DeprecatedFlyString class_name;
  1080. if (!m_class_expression.has_name() && m_lhs_name.has_value()) {
  1081. class_name = interpreter.current_executable().get_identifier(m_lhs_name.value());
  1082. } else {
  1083. binding_name = name;
  1084. class_name = name.is_null() ? ""sv : name;
  1085. }
  1086. interpreter.accumulator() = TRY(m_class_expression.create_class_constructor(vm, class_environment, vm.lexical_environment(), super_class, binding_name, class_name));
  1087. return {};
  1088. }
  1089. // 13.5.3.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-typeof-operator-runtime-semantics-evaluation
  1090. ThrowCompletionOr<void> TypeofVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  1091. {
  1092. auto& vm = interpreter.vm();
  1093. // 1. Let val be the result of evaluating UnaryExpression.
  1094. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  1095. auto reference = TRY(vm.resolve_binding(string));
  1096. // 2. If val is a Reference Record, then
  1097. // a. If IsUnresolvableReference(val) is true, return "undefined".
  1098. if (reference.is_unresolvable()) {
  1099. interpreter.accumulator() = MUST_OR_THROW_OOM(PrimitiveString::create(vm, "undefined"sv));
  1100. return {};
  1101. }
  1102. // 3. Set val to ? GetValue(val).
  1103. auto value = TRY(reference.get_value(vm));
  1104. // 4. NOTE: This step is replaced in section B.3.6.3.
  1105. // 5. Return a String according to Table 41.
  1106. interpreter.accumulator() = MUST_OR_THROW_OOM(PrimitiveString::create(vm, value.typeof()));
  1107. return {};
  1108. }
  1109. ThrowCompletionOr<void> TypeofLocal::execute_impl(Bytecode::Interpreter& interpreter) const
  1110. {
  1111. auto& vm = interpreter.vm();
  1112. auto const& value = vm.running_execution_context().local_variables[m_index];
  1113. interpreter.accumulator() = MUST_OR_THROW_OOM(PrimitiveString::create(vm, value.typeof()));
  1114. return {};
  1115. }
  1116. ThrowCompletionOr<void> ToNumeric::execute_impl(Bytecode::Interpreter& interpreter) const
  1117. {
  1118. interpreter.accumulator() = TRY(interpreter.accumulator().to_numeric(interpreter.vm()));
  1119. return {};
  1120. }
  1121. ThrowCompletionOr<void> BlockDeclarationInstantiation::execute_impl(Bytecode::Interpreter& interpreter) const
  1122. {
  1123. auto& vm = interpreter.vm();
  1124. auto old_environment = vm.running_execution_context().lexical_environment;
  1125. interpreter.saved_lexical_environment_stack().append(old_environment);
  1126. vm.running_execution_context().lexical_environment = new_declarative_environment(*old_environment);
  1127. m_scope_node.block_declaration_instantiation(vm, vm.running_execution_context().lexical_environment);
  1128. return {};
  1129. }
  1130. DeprecatedString Load::to_deprecated_string_impl(Bytecode::Executable const&) const
  1131. {
  1132. return DeprecatedString::formatted("Load {}", m_src);
  1133. }
  1134. DeprecatedString LoadImmediate::to_deprecated_string_impl(Bytecode::Executable const&) const
  1135. {
  1136. return DeprecatedString::formatted("LoadImmediate {}", m_value);
  1137. }
  1138. DeprecatedString Store::to_deprecated_string_impl(Bytecode::Executable const&) const
  1139. {
  1140. return DeprecatedString::formatted("Store {}", m_dst);
  1141. }
  1142. DeprecatedString NewBigInt::to_deprecated_string_impl(Bytecode::Executable const&) const
  1143. {
  1144. return DeprecatedString::formatted("NewBigInt \"{}\"", m_bigint.to_base_deprecated(10));
  1145. }
  1146. DeprecatedString NewArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1147. {
  1148. StringBuilder builder;
  1149. builder.append("NewArray"sv);
  1150. if (m_element_count != 0) {
  1151. builder.appendff(" [{}-{}]", m_elements[0], m_elements[1]);
  1152. }
  1153. return builder.to_deprecated_string();
  1154. }
  1155. DeprecatedString Append::to_deprecated_string_impl(Bytecode::Executable const&) const
  1156. {
  1157. if (m_is_spread)
  1158. return DeprecatedString::formatted("Append lhs: **{}", m_lhs);
  1159. return DeprecatedString::formatted("Append lhs: {}", m_lhs);
  1160. }
  1161. DeprecatedString IteratorToArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1162. {
  1163. return "IteratorToArray";
  1164. }
  1165. DeprecatedString NewString::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1166. {
  1167. return DeprecatedString::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  1168. }
  1169. DeprecatedString NewObject::to_deprecated_string_impl(Bytecode::Executable const&) const
  1170. {
  1171. return "NewObject";
  1172. }
  1173. DeprecatedString NewRegExp::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1174. {
  1175. return DeprecatedString::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  1176. }
  1177. DeprecatedString CopyObjectExcludingProperties::to_deprecated_string_impl(Bytecode::Executable const&) const
  1178. {
  1179. StringBuilder builder;
  1180. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  1181. if (m_excluded_names_count != 0) {
  1182. builder.append(" excluding:["sv);
  1183. builder.join(", "sv, ReadonlySpan<Register>(m_excluded_names, m_excluded_names_count));
  1184. builder.append(']');
  1185. }
  1186. return builder.to_deprecated_string();
  1187. }
  1188. DeprecatedString ConcatString::to_deprecated_string_impl(Bytecode::Executable const&) const
  1189. {
  1190. return DeprecatedString::formatted("ConcatString {}", m_lhs);
  1191. }
  1192. DeprecatedString GetVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1193. {
  1194. return DeprecatedString::formatted("GetVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1195. }
  1196. DeprecatedString GetLocal::to_deprecated_string_impl(Bytecode::Executable const&) const
  1197. {
  1198. return DeprecatedString::formatted("GetLocal {}", m_index);
  1199. }
  1200. DeprecatedString DeleteVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1201. {
  1202. return DeprecatedString::formatted("DeleteVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1203. }
  1204. DeprecatedString CreateLexicalEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const
  1205. {
  1206. return "CreateLexicalEnvironment"sv;
  1207. }
  1208. DeprecatedString CreateVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1209. {
  1210. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1211. return DeprecatedString::formatted("CreateVariable env:{} immutable:{} global:{} {} ({})", mode_string, m_is_immutable, m_is_global, m_identifier, executable.identifier_table->get(m_identifier));
  1212. }
  1213. DeprecatedString EnterObjectEnvironment::to_deprecated_string_impl(Executable const&) const
  1214. {
  1215. return DeprecatedString::formatted("EnterObjectEnvironment");
  1216. }
  1217. DeprecatedString SetVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1218. {
  1219. auto initialization_mode_name = m_initialization_mode == InitializationMode ::Initialize ? "Initialize"
  1220. : m_initialization_mode == InitializationMode::Set ? "Set"
  1221. : "InitializeOrSet";
  1222. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1223. return DeprecatedString::formatted("SetVariable env:{} init:{} {} ({})", mode_string, initialization_mode_name, m_identifier, executable.identifier_table->get(m_identifier));
  1224. }
  1225. DeprecatedString SetLocal::to_deprecated_string_impl(Bytecode::Executable const&) const
  1226. {
  1227. return DeprecatedString::formatted("SetLocal {}", m_index);
  1228. }
  1229. DeprecatedString PutById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1230. {
  1231. auto kind = m_kind == PropertyKind::Getter
  1232. ? "getter"
  1233. : m_kind == PropertyKind::Setter
  1234. ? "setter"
  1235. : "property";
  1236. return DeprecatedString::formatted("PutById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  1237. }
  1238. DeprecatedString PutByIdWithThis::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1239. {
  1240. auto kind = m_kind == PropertyKind::Getter
  1241. ? "getter"
  1242. : m_kind == PropertyKind::Setter
  1243. ? "setter"
  1244. : "property";
  1245. return DeprecatedString::formatted("PutByIdWithThis kind:{} base:{}, property:{} ({}) this_value:{}", kind, m_base, m_property, executable.identifier_table->get(m_property), m_this_value);
  1246. }
  1247. DeprecatedString PutPrivateById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1248. {
  1249. auto kind = m_kind == PropertyKind::Getter
  1250. ? "getter"
  1251. : m_kind == PropertyKind::Setter
  1252. ? "setter"
  1253. : "property";
  1254. return DeprecatedString::formatted("PutPrivateById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  1255. }
  1256. DeprecatedString GetById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1257. {
  1258. return DeprecatedString::formatted("GetById {} ({})", m_property, executable.identifier_table->get(m_property));
  1259. }
  1260. DeprecatedString GetByIdWithThis::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1261. {
  1262. return DeprecatedString::formatted("GetByIdWithThis {} ({}) this_value:{}", m_property, executable.identifier_table->get(m_property), m_this_value);
  1263. }
  1264. DeprecatedString GetPrivateById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1265. {
  1266. return DeprecatedString::formatted("GetPrivateById {} ({})", m_property, executable.identifier_table->get(m_property));
  1267. }
  1268. DeprecatedString HasPrivateId::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1269. {
  1270. return DeprecatedString::formatted("HasPrivateId {} ({})", m_property, executable.identifier_table->get(m_property));
  1271. }
  1272. DeprecatedString DeleteById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1273. {
  1274. return DeprecatedString::formatted("DeleteById {} ({})", m_property, executable.identifier_table->get(m_property));
  1275. }
  1276. DeprecatedString Jump::to_deprecated_string_impl(Bytecode::Executable const&) const
  1277. {
  1278. if (m_true_target.has_value())
  1279. return DeprecatedString::formatted("Jump {}", *m_true_target);
  1280. return DeprecatedString::formatted("Jump <empty>");
  1281. }
  1282. DeprecatedString JumpConditional::to_deprecated_string_impl(Bytecode::Executable const&) const
  1283. {
  1284. auto true_string = m_true_target.has_value() ? DeprecatedString::formatted("{}", *m_true_target) : "<empty>";
  1285. auto false_string = m_false_target.has_value() ? DeprecatedString::formatted("{}", *m_false_target) : "<empty>";
  1286. return DeprecatedString::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  1287. }
  1288. DeprecatedString JumpNullish::to_deprecated_string_impl(Bytecode::Executable const&) const
  1289. {
  1290. auto true_string = m_true_target.has_value() ? DeprecatedString::formatted("{}", *m_true_target) : "<empty>";
  1291. auto false_string = m_false_target.has_value() ? DeprecatedString::formatted("{}", *m_false_target) : "<empty>";
  1292. return DeprecatedString::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  1293. }
  1294. DeprecatedString JumpUndefined::to_deprecated_string_impl(Bytecode::Executable const&) const
  1295. {
  1296. auto true_string = m_true_target.has_value() ? DeprecatedString::formatted("{}", *m_true_target) : "<empty>";
  1297. auto false_string = m_false_target.has_value() ? DeprecatedString::formatted("{}", *m_false_target) : "<empty>";
  1298. return DeprecatedString::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  1299. }
  1300. static StringView call_type_to_string(CallType type)
  1301. {
  1302. switch (type) {
  1303. case CallType::Call:
  1304. return ""sv;
  1305. case CallType::Construct:
  1306. return " (Construct)"sv;
  1307. case CallType::DirectEval:
  1308. return " (DirectEval)"sv;
  1309. }
  1310. VERIFY_NOT_REACHED();
  1311. }
  1312. DeprecatedString Call::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1313. {
  1314. auto type = call_type_to_string(m_type);
  1315. if (m_expression_string.has_value())
  1316. return DeprecatedString::formatted("Call{} callee:{}, this:{}, first_arg:{} ({})", type, m_callee, m_this_value, m_first_argument, executable.get_string(m_expression_string.value()));
  1317. return DeprecatedString::formatted("Call{} callee:{}, this:{}, first_arg:{}", type, m_callee, m_first_argument, m_this_value);
  1318. }
  1319. DeprecatedString CallWithArgumentArray::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1320. {
  1321. auto type = call_type_to_string(m_type);
  1322. if (m_expression_string.has_value())
  1323. return DeprecatedString::formatted("CallWithArgumentArray{} callee:{}, this:{}, arguments:[...acc] ({})", type, m_callee, m_this_value, executable.get_string(m_expression_string.value()));
  1324. return DeprecatedString::formatted("CallWithArgumentArray{} callee:{}, this:{}, arguments:[...acc]", type, m_callee, m_this_value);
  1325. }
  1326. DeprecatedString SuperCallWithArgumentArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1327. {
  1328. return "SuperCallWithArgumentArray arguments:[...acc]"sv;
  1329. }
  1330. DeprecatedString NewFunction::to_deprecated_string_impl(Bytecode::Executable const&) const
  1331. {
  1332. StringBuilder builder;
  1333. builder.append("NewFunction"sv);
  1334. if (m_function_node.has_name())
  1335. builder.appendff(" name:{}"sv, m_function_node.name());
  1336. if (m_lhs_name.has_value())
  1337. builder.appendff(" lhs_name:{}"sv, m_lhs_name.value());
  1338. if (m_home_object.has_value())
  1339. builder.appendff(" home_object:{}"sv, m_home_object.value());
  1340. return builder.to_deprecated_string();
  1341. }
  1342. DeprecatedString NewClass::to_deprecated_string_impl(Bytecode::Executable const&) const
  1343. {
  1344. StringBuilder builder;
  1345. auto name = m_class_expression.name();
  1346. builder.appendff("NewClass '{}'"sv, name.is_null() ? ""sv : name);
  1347. if (m_lhs_name.has_value())
  1348. builder.appendff(" lhs_name:{}"sv, m_lhs_name.value());
  1349. return builder.to_deprecated_string();
  1350. }
  1351. DeprecatedString Return::to_deprecated_string_impl(Bytecode::Executable const&) const
  1352. {
  1353. return "Return";
  1354. }
  1355. DeprecatedString Increment::to_deprecated_string_impl(Bytecode::Executable const&) const
  1356. {
  1357. return "Increment";
  1358. }
  1359. DeprecatedString Decrement::to_deprecated_string_impl(Bytecode::Executable const&) const
  1360. {
  1361. return "Decrement";
  1362. }
  1363. DeprecatedString Throw::to_deprecated_string_impl(Bytecode::Executable const&) const
  1364. {
  1365. return "Throw";
  1366. }
  1367. DeprecatedString ThrowIfNotObject::to_deprecated_string_impl(Bytecode::Executable const&) const
  1368. {
  1369. return "ThrowIfNotObject";
  1370. }
  1371. DeprecatedString ThrowIfNullish::to_deprecated_string_impl(Bytecode::Executable const&) const
  1372. {
  1373. return "ThrowIfNullish";
  1374. }
  1375. DeprecatedString EnterUnwindContext::to_deprecated_string_impl(Bytecode::Executable const&) const
  1376. {
  1377. auto handler_string = m_handler_target.has_value() ? DeprecatedString::formatted("{}", *m_handler_target) : "<empty>";
  1378. auto finalizer_string = m_finalizer_target.has_value() ? DeprecatedString::formatted("{}", *m_finalizer_target) : "<empty>";
  1379. return DeprecatedString::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  1380. }
  1381. DeprecatedString ScheduleJump::to_deprecated_string_impl(Bytecode::Executable const&) const
  1382. {
  1383. return DeprecatedString::formatted("ScheduleJump {}", m_target);
  1384. }
  1385. DeprecatedString LeaveLexicalEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const
  1386. {
  1387. return "LeaveLexicalEnvironment"sv;
  1388. }
  1389. DeprecatedString LeaveUnwindContext::to_deprecated_string_impl(Bytecode::Executable const&) const
  1390. {
  1391. return "LeaveUnwindContext";
  1392. }
  1393. DeprecatedString ContinuePendingUnwind::to_deprecated_string_impl(Bytecode::Executable const&) const
  1394. {
  1395. return DeprecatedString::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  1396. }
  1397. DeprecatedString PushDeclarativeEnvironment::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1398. {
  1399. StringBuilder builder;
  1400. builder.append("PushDeclarativeEnvironment"sv);
  1401. if (!m_variables.is_empty()) {
  1402. builder.append(" {"sv);
  1403. Vector<DeprecatedString> names;
  1404. for (auto& it : m_variables)
  1405. names.append(executable.get_string(it.key));
  1406. builder.append('}');
  1407. builder.join(", "sv, names);
  1408. }
  1409. return builder.to_deprecated_string();
  1410. }
  1411. DeprecatedString Yield::to_deprecated_string_impl(Bytecode::Executable const&) const
  1412. {
  1413. if (m_continuation_label.has_value())
  1414. return DeprecatedString::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  1415. return DeprecatedString::formatted("Yield return");
  1416. }
  1417. DeprecatedString GetByValue::to_deprecated_string_impl(Bytecode::Executable const&) const
  1418. {
  1419. return DeprecatedString::formatted("GetByValue base:{}", m_base);
  1420. }
  1421. DeprecatedString GetByValueWithThis::to_deprecated_string_impl(Bytecode::Executable const&) const
  1422. {
  1423. return DeprecatedString::formatted("GetByValueWithThis base:{} this_value:{}", m_base, m_this_value);
  1424. }
  1425. DeprecatedString PutByValue::to_deprecated_string_impl(Bytecode::Executable const&) const
  1426. {
  1427. auto kind = m_kind == PropertyKind::Getter
  1428. ? "getter"
  1429. : m_kind == PropertyKind::Setter
  1430. ? "setter"
  1431. : "property";
  1432. return DeprecatedString::formatted("PutByValue kind:{} base:{}, property:{}", kind, m_base, m_property);
  1433. }
  1434. DeprecatedString PutByValueWithThis::to_deprecated_string_impl(Bytecode::Executable const&) const
  1435. {
  1436. auto kind = m_kind == PropertyKind::Getter
  1437. ? "getter"
  1438. : m_kind == PropertyKind::Setter
  1439. ? "setter"
  1440. : "property";
  1441. return DeprecatedString::formatted("PutByValueWithThis kind:{} base:{}, property:{} this_value:{}", kind, m_base, m_property, m_this_value);
  1442. }
  1443. DeprecatedString DeleteByValue::to_deprecated_string_impl(Bytecode::Executable const&) const
  1444. {
  1445. return DeprecatedString::formatted("DeleteByValue base:{}", m_base);
  1446. }
  1447. DeprecatedString GetIterator::to_deprecated_string_impl(Executable const&) const
  1448. {
  1449. auto hint = m_hint == IteratorHint::Sync ? "sync" : "async";
  1450. return DeprecatedString::formatted("GetIterator hint:{}", hint);
  1451. }
  1452. DeprecatedString GetMethod::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1453. {
  1454. return DeprecatedString::formatted("GetMethod {} ({})", m_property, executable.identifier_table->get(m_property));
  1455. }
  1456. DeprecatedString GetObjectPropertyIterator::to_deprecated_string_impl(Bytecode::Executable const&) const
  1457. {
  1458. return "GetObjectPropertyIterator";
  1459. }
  1460. DeprecatedString IteratorClose::to_deprecated_string_impl(Bytecode::Executable const&) const
  1461. {
  1462. if (!m_completion_value.has_value())
  1463. return DeprecatedString::formatted("IteratorClose completion_type={} completion_value=<empty>", to_underlying(m_completion_type));
  1464. auto completion_value_string = m_completion_value->to_string_without_side_effects().release_value_but_fixme_should_propagate_errors();
  1465. return DeprecatedString::formatted("IteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string);
  1466. }
  1467. DeprecatedString IteratorNext::to_deprecated_string_impl(Executable const&) const
  1468. {
  1469. return "IteratorNext";
  1470. }
  1471. DeprecatedString IteratorResultDone::to_deprecated_string_impl(Executable const&) const
  1472. {
  1473. return "IteratorResultDone";
  1474. }
  1475. DeprecatedString IteratorResultValue::to_deprecated_string_impl(Executable const&) const
  1476. {
  1477. return "IteratorResultValue";
  1478. }
  1479. DeprecatedString ResolveThisBinding::to_deprecated_string_impl(Bytecode::Executable const&) const
  1480. {
  1481. return "ResolveThisBinding"sv;
  1482. }
  1483. DeprecatedString ResolveSuperBase::to_deprecated_string_impl(Bytecode::Executable const&) const
  1484. {
  1485. return "ResolveSuperBase"sv;
  1486. }
  1487. DeprecatedString GetNewTarget::to_deprecated_string_impl(Bytecode::Executable const&) const
  1488. {
  1489. return "GetNewTarget"sv;
  1490. }
  1491. DeprecatedString TypeofVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1492. {
  1493. return DeprecatedString::formatted("TypeofVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1494. }
  1495. DeprecatedString TypeofLocal::to_deprecated_string_impl(Bytecode::Executable const&) const
  1496. {
  1497. return DeprecatedString::formatted("TypeofLocal {}", m_index);
  1498. }
  1499. DeprecatedString ToNumeric::to_deprecated_string_impl(Bytecode::Executable const&) const
  1500. {
  1501. return "ToNumeric"sv;
  1502. }
  1503. DeprecatedString BlockDeclarationInstantiation::to_deprecated_string_impl(Bytecode::Executable const&) const
  1504. {
  1505. return "BlockDeclarationInstantiation"sv;
  1506. }
  1507. DeprecatedString ImportCall::to_deprecated_string_impl(Bytecode::Executable const&) const
  1508. {
  1509. return DeprecatedString::formatted("ImportCall specifier:{} options:{}"sv, m_specifier, m_options);
  1510. }
  1511. }