Op.cpp 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  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> DeleteByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  547. {
  548. auto& vm = interpreter.vm();
  549. auto base_value = interpreter.accumulator();
  550. auto const& identifier = interpreter.current_executable().get_identifier(m_property);
  551. bool strict = vm.in_strict_mode();
  552. auto reference = Reference { base_value, identifier, interpreter.reg(m_this_value), strict };
  553. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  554. return {};
  555. };
  556. ThrowCompletionOr<void> Jump::execute_impl(Bytecode::Interpreter& interpreter) const
  557. {
  558. interpreter.jump(*m_true_target);
  559. return {};
  560. }
  561. ThrowCompletionOr<void> ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const
  562. {
  563. auto& vm = interpreter.vm();
  564. interpreter.accumulator() = TRY(vm.resolve_this_binding());
  565. return {};
  566. }
  567. // https://tc39.es/ecma262/#sec-makesuperpropertyreference
  568. ThrowCompletionOr<void> ResolveSuperBase::execute_impl(Bytecode::Interpreter& interpreter) const
  569. {
  570. auto& vm = interpreter.vm();
  571. // 1. Let env be GetThisEnvironment().
  572. auto& env = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  573. // 2. Assert: env.HasSuperBinding() is true.
  574. VERIFY(env.has_super_binding());
  575. // 3. Let baseValue be ? env.GetSuperBase().
  576. interpreter.accumulator() = TRY(env.get_super_base());
  577. return {};
  578. }
  579. ThrowCompletionOr<void> GetNewTarget::execute_impl(Bytecode::Interpreter& interpreter) const
  580. {
  581. interpreter.accumulator() = interpreter.vm().get_new_target();
  582. return {};
  583. }
  584. void Jump::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  585. {
  586. if (m_true_target.has_value() && &m_true_target->block() == &from)
  587. m_true_target = Label { to };
  588. if (m_false_target.has_value() && &m_false_target->block() == &from)
  589. m_false_target = Label { to };
  590. }
  591. ThrowCompletionOr<void> JumpConditional::execute_impl(Bytecode::Interpreter& interpreter) const
  592. {
  593. VERIFY(m_true_target.has_value());
  594. VERIFY(m_false_target.has_value());
  595. auto result = interpreter.accumulator();
  596. if (result.to_boolean())
  597. interpreter.jump(m_true_target.value());
  598. else
  599. interpreter.jump(m_false_target.value());
  600. return {};
  601. }
  602. ThrowCompletionOr<void> JumpNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  603. {
  604. VERIFY(m_true_target.has_value());
  605. VERIFY(m_false_target.has_value());
  606. auto result = interpreter.accumulator();
  607. if (result.is_nullish())
  608. interpreter.jump(m_true_target.value());
  609. else
  610. interpreter.jump(m_false_target.value());
  611. return {};
  612. }
  613. ThrowCompletionOr<void> JumpUndefined::execute_impl(Bytecode::Interpreter& interpreter) const
  614. {
  615. VERIFY(m_true_target.has_value());
  616. VERIFY(m_false_target.has_value());
  617. auto result = interpreter.accumulator();
  618. if (result.is_undefined())
  619. interpreter.jump(m_true_target.value());
  620. else
  621. interpreter.jump(m_false_target.value());
  622. return {};
  623. }
  624. // 13.3.8.1 https://tc39.es/ecma262/#sec-runtime-semantics-argumentlistevaluation
  625. static MarkedVector<Value> argument_list_evaluation(Bytecode::Interpreter& interpreter)
  626. {
  627. // Note: Any spreading and actual evaluation is handled in preceding opcodes
  628. // Note: The spec uses the concept of a list, while we create a temporary array
  629. // in the preceding opcodes, so we have to convert in a manner that is not
  630. // visible to the user
  631. auto& vm = interpreter.vm();
  632. MarkedVector<Value> argument_values { vm.heap() };
  633. auto arguments = interpreter.accumulator();
  634. if (!(arguments.is_object() && is<Array>(arguments.as_object()))) {
  635. dbgln("[{}] Call arguments are not an array, but: {}", interpreter.debug_position(), MUST(arguments.to_string_without_side_effects()));
  636. interpreter.current_executable().dump();
  637. VERIFY_NOT_REACHED();
  638. }
  639. auto& argument_array = arguments.as_array();
  640. auto array_length = argument_array.indexed_properties().array_like_size();
  641. argument_values.ensure_capacity(array_length);
  642. for (size_t i = 0; i < array_length; ++i) {
  643. if (auto maybe_value = argument_array.indexed_properties().get(i); maybe_value.has_value())
  644. argument_values.append(maybe_value.release_value().value);
  645. else
  646. argument_values.append(js_undefined());
  647. }
  648. return argument_values;
  649. }
  650. static Completion throw_type_error_for_callee(Bytecode::Interpreter& interpreter, auto& call, StringView callee_type)
  651. {
  652. auto& vm = interpreter.vm();
  653. auto callee = interpreter.reg(call.callee());
  654. if (call.expression_string().has_value())
  655. 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()));
  656. return vm.throw_completion<TypeError>(ErrorType::IsNotA, TRY_OR_THROW_OOM(vm, callee.to_string_without_side_effects()), callee_type);
  657. }
  658. static ThrowCompletionOr<void> throw_if_needed_for_call(Interpreter& interpreter, auto& call, Value callee)
  659. {
  660. if (call.call_type() == CallType::Call && !callee.is_function())
  661. return throw_type_error_for_callee(interpreter, call, "function"sv);
  662. if (call.call_type() == CallType::Construct && !callee.is_constructor())
  663. return throw_type_error_for_callee(interpreter, call, "constructor"sv);
  664. return {};
  665. }
  666. static ThrowCompletionOr<void> perform_call(Interpreter& interpreter, auto& call, Value callee, MarkedVector<Value> argument_values)
  667. {
  668. auto& vm = interpreter.vm();
  669. auto this_value = interpreter.reg(call.this_value());
  670. auto& function = callee.as_function();
  671. Value return_value;
  672. if (call.call_type() == CallType::DirectEval) {
  673. if (callee == interpreter.realm().intrinsics().eval_function())
  674. 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));
  675. else
  676. return_value = TRY(JS::call(vm, function, this_value, move(argument_values)));
  677. } else if (call.call_type() == CallType::Call)
  678. return_value = TRY(JS::call(vm, function, this_value, move(argument_values)));
  679. else
  680. return_value = TRY(construct(vm, function, move(argument_values)));
  681. interpreter.accumulator() = return_value;
  682. return {};
  683. }
  684. ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) const
  685. {
  686. auto& vm = interpreter.vm();
  687. auto callee = interpreter.reg(m_callee);
  688. TRY(throw_if_needed_for_call(interpreter, *this, callee));
  689. MarkedVector<Value> argument_values(vm.heap());
  690. argument_values.ensure_capacity(m_argument_count);
  691. for (u32 i = 0; i < m_argument_count; ++i) {
  692. argument_values.unchecked_append(interpreter.reg(Register { m_first_argument.index() + i }));
  693. }
  694. return perform_call(interpreter, *this, callee, move(argument_values));
  695. }
  696. ThrowCompletionOr<void> CallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  697. {
  698. auto callee = interpreter.reg(m_callee);
  699. TRY(throw_if_needed_for_call(interpreter, *this, callee));
  700. auto argument_values = argument_list_evaluation(interpreter);
  701. return perform_call(interpreter, *this, callee, move(argument_values));
  702. }
  703. // 13.3.7.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  704. ThrowCompletionOr<void> SuperCallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
  705. {
  706. auto& vm = interpreter.vm();
  707. // 1. Let newTarget be GetNewTarget().
  708. auto new_target = vm.get_new_target();
  709. // 2. Assert: Type(newTarget) is Object.
  710. VERIFY(new_target.is_object());
  711. // 3. Let func be GetSuperConstructor().
  712. auto* func = get_super_constructor(vm);
  713. // 4. Let argList be ? ArgumentListEvaluation of Arguments.
  714. MarkedVector<Value> arg_list { vm.heap() };
  715. if (m_is_synthetic) {
  716. auto const& value = interpreter.accumulator();
  717. VERIFY(value.is_object() && is<Array>(value.as_object()));
  718. auto const& array_value = static_cast<Array const&>(value.as_object());
  719. auto length = MUST(length_of_array_like(vm, array_value));
  720. for (size_t i = 0; i < length; ++i)
  721. arg_list.append(array_value.get_without_side_effects(PropertyKey { i }));
  722. } else {
  723. arg_list = argument_list_evaluation(interpreter);
  724. }
  725. // 5. If IsConstructor(func) is false, throw a TypeError exception.
  726. if (!Value(func).is_constructor())
  727. return vm.throw_completion<TypeError>(ErrorType::NotAConstructor, "Super constructor");
  728. // 6. Let result be ? Construct(func, argList, newTarget).
  729. auto result = TRY(construct(vm, static_cast<FunctionObject&>(*func), move(arg_list), &new_target.as_function()));
  730. // 7. Let thisER be GetThisEnvironment().
  731. auto& this_environment = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  732. // 8. Perform ? thisER.BindThisValue(result).
  733. TRY(this_environment.bind_this_value(vm, result));
  734. // 9. Let F be thisER.[[FunctionObject]].
  735. auto& f = this_environment.function_object();
  736. // 10. Assert: F is an ECMAScript function object.
  737. // NOTE: This is implied by the strong C++ type.
  738. // 11. Perform ? InitializeInstanceElements(result, F).
  739. TRY(result->initialize_instance_elements(f));
  740. // 12. Return result.
  741. interpreter.accumulator() = result;
  742. return {};
  743. }
  744. ThrowCompletionOr<void> NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  745. {
  746. auto& vm = interpreter.vm();
  747. if (!m_function_node.has_name()) {
  748. DeprecatedFlyString name = {};
  749. if (m_lhs_name.has_value())
  750. name = interpreter.current_executable().get_identifier(m_lhs_name.value());
  751. interpreter.accumulator() = m_function_node.instantiate_ordinary_function_expression(vm, name);
  752. } else {
  753. 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());
  754. }
  755. if (m_home_object.has_value()) {
  756. auto home_object_value = interpreter.reg(m_home_object.value());
  757. static_cast<ECMAScriptFunctionObject&>(interpreter.accumulator().as_function()).set_home_object(&home_object_value.as_object());
  758. }
  759. return {};
  760. }
  761. ThrowCompletionOr<void> Return::execute_impl(Bytecode::Interpreter& interpreter) const
  762. {
  763. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  764. return {};
  765. }
  766. ThrowCompletionOr<void> Increment::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().plus(Crypto::SignedBigInteger { 1 }));
  774. return {};
  775. }
  776. ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  777. {
  778. auto& vm = interpreter.vm();
  779. auto old_value = TRY(interpreter.accumulator().to_numeric(vm));
  780. if (old_value.is_number())
  781. interpreter.accumulator() = Value(old_value.as_double() - 1);
  782. else
  783. interpreter.accumulator() = BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  784. return {};
  785. }
  786. ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  787. {
  788. return throw_completion(interpreter.accumulator());
  789. }
  790. ThrowCompletionOr<void> ThrowIfNotObject::execute_impl(Bytecode::Interpreter& interpreter) const
  791. {
  792. auto& vm = interpreter.vm();
  793. if (!interpreter.accumulator().is_object())
  794. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, TRY_OR_THROW_OOM(vm, interpreter.accumulator().to_string_without_side_effects()));
  795. return {};
  796. }
  797. ThrowCompletionOr<void> ThrowIfNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  798. {
  799. auto& vm = interpreter.vm();
  800. auto value = interpreter.accumulator();
  801. if (value.is_nullish())
  802. return vm.throw_completion<TypeError>(ErrorType::NotObjectCoercible, TRY_OR_THROW_OOM(vm, value.to_string_without_side_effects()));
  803. return {};
  804. }
  805. ThrowCompletionOr<void> EnterUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  806. {
  807. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  808. interpreter.jump(m_entry_point);
  809. return {};
  810. }
  811. void NewFunction::replace_references_impl(Register from, Register to)
  812. {
  813. if (m_home_object == from)
  814. m_home_object = to;
  815. }
  816. void EnterUnwindContext::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  817. {
  818. if (&m_entry_point.block() == &from)
  819. m_entry_point = Label { to };
  820. if (m_handler_target.has_value() && &m_handler_target->block() == &from)
  821. m_handler_target = Label { to };
  822. if (m_finalizer_target.has_value() && &m_finalizer_target->block() == &from)
  823. m_finalizer_target = Label { to };
  824. }
  825. void CopyObjectExcludingProperties::replace_references_impl(Register from, Register to)
  826. {
  827. if (m_from_object == from)
  828. m_from_object = to;
  829. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  830. if (m_excluded_names[i] == from)
  831. m_excluded_names[i] = to;
  832. }
  833. }
  834. void Call::replace_references_impl(Register from, Register to)
  835. {
  836. if (m_callee == from)
  837. m_callee = to;
  838. if (m_this_value == from)
  839. m_this_value = to;
  840. if (m_first_argument == from)
  841. m_first_argument = to;
  842. }
  843. void CallWithArgumentArray::replace_references_impl(Register from, Register to)
  844. {
  845. if (m_callee == from)
  846. m_callee = to;
  847. if (m_this_value == from)
  848. m_this_value = to;
  849. }
  850. ThrowCompletionOr<void> ScheduleJump::execute_impl(Bytecode::Interpreter& interpreter) const
  851. {
  852. interpreter.schedule_jump(m_target);
  853. return {};
  854. }
  855. ThrowCompletionOr<void> LeaveLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  856. {
  857. interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
  858. return {};
  859. }
  860. ThrowCompletionOr<void> LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  861. {
  862. interpreter.leave_unwind_context();
  863. return {};
  864. }
  865. ThrowCompletionOr<void> ContinuePendingUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  866. {
  867. return interpreter.continue_pending_unwind(m_resume_target);
  868. }
  869. void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  870. {
  871. if (&m_resume_target.block() == &from)
  872. m_resume_target = Label { to };
  873. }
  874. ThrowCompletionOr<void> PushDeclarativeEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  875. {
  876. auto environment = interpreter.vm().heap().allocate_without_realm<DeclarativeEnvironment>(interpreter.vm().lexical_environment());
  877. interpreter.vm().running_execution_context().lexical_environment = environment;
  878. interpreter.vm().running_execution_context().variable_environment = environment;
  879. return {};
  880. }
  881. ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  882. {
  883. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  884. auto object = Object::create(interpreter.realm(), nullptr);
  885. object->define_direct_property("result", yielded_value, JS::default_attributes);
  886. if (m_continuation_label.has_value())
  887. // FIXME: If we get a pointer, which is not accurately representable as a double
  888. // will cause this to explode
  889. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes);
  890. else
  891. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  892. interpreter.do_return(object);
  893. return {};
  894. }
  895. void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  896. {
  897. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  898. m_continuation_label = Label { to };
  899. }
  900. ThrowCompletionOr<void> GetByValue::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->get(property_key));
  908. return {};
  909. }
  910. ThrowCompletionOr<void> GetByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  911. {
  912. auto& vm = interpreter.vm();
  913. // NOTE: Get the property key from the accumulator before side effects have a chance to overwrite it.
  914. auto property_key_value = interpreter.accumulator();
  915. auto object = TRY(interpreter.reg(m_base).to_object(vm));
  916. auto property_key = TRY(property_key_value.to_property_key(vm));
  917. interpreter.accumulator() = TRY(object->internal_get(property_key, interpreter.reg(m_this_value)));
  918. return {};
  919. }
  920. ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  921. {
  922. auto& vm = interpreter.vm();
  923. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  924. auto value = interpreter.accumulator();
  925. auto base = interpreter.reg(m_base);
  926. auto property_key = TRY(interpreter.reg(m_property).to_property_key(vm));
  927. TRY(put_by_property_key(vm, base, base, value, property_key, m_kind));
  928. interpreter.accumulator() = value;
  929. return {};
  930. }
  931. ThrowCompletionOr<void> PutByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  932. {
  933. auto& vm = interpreter.vm();
  934. // NOTE: Get the value from the accumulator before side effects have a chance to overwrite it.
  935. auto value = interpreter.accumulator();
  936. auto base = interpreter.reg(m_base);
  937. auto property_key = TRY(interpreter.reg(m_property).to_property_key(vm));
  938. TRY(put_by_property_key(vm, base, interpreter.reg(m_this_value), value, property_key, m_kind));
  939. interpreter.accumulator() = value;
  940. return {};
  941. }
  942. ThrowCompletionOr<void> DeleteByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  943. {
  944. auto& vm = interpreter.vm();
  945. // NOTE: Get the property key from the accumulator before side effects have a chance to overwrite it.
  946. auto property_key_value = interpreter.accumulator();
  947. auto base_value = interpreter.reg(m_base);
  948. auto property_key = TRY(property_key_value.to_property_key(vm));
  949. bool strict = vm.in_strict_mode();
  950. auto reference = Reference { base_value, property_key, {}, strict };
  951. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  952. return {};
  953. }
  954. ThrowCompletionOr<void> DeleteByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const
  955. {
  956. auto& vm = interpreter.vm();
  957. // NOTE: Get the property key from the accumulator before side effects have a chance to overwrite it.
  958. auto property_key_value = interpreter.accumulator();
  959. auto base_value = interpreter.reg(m_base);
  960. auto property_key = TRY(property_key_value.to_property_key(vm));
  961. bool strict = vm.in_strict_mode();
  962. auto reference = Reference { base_value, property_key, interpreter.reg(m_this_value), strict };
  963. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  964. return {};
  965. };
  966. ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  967. {
  968. auto& vm = interpreter.vm();
  969. auto iterator = TRY(get_iterator(vm, interpreter.accumulator(), m_hint));
  970. interpreter.accumulator() = iterator_to_object(vm, iterator);
  971. return {};
  972. }
  973. ThrowCompletionOr<void> GetMethod::execute_impl(Bytecode::Interpreter& interpreter) const
  974. {
  975. auto& vm = interpreter.vm();
  976. auto identifier = interpreter.current_executable().get_identifier(m_property);
  977. auto method = TRY(interpreter.accumulator().get_method(vm, identifier));
  978. interpreter.accumulator() = method ?: js_undefined();
  979. return {};
  980. }
  981. // 14.7.5.9 EnumerateObjectProperties ( O ), https://tc39.es/ecma262/#sec-enumerate-object-properties
  982. ThrowCompletionOr<void> GetObjectPropertyIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  983. {
  984. // While the spec does provide an algorithm, it allows us to implement it ourselves so long as we meet the following invariants:
  985. // 1- Returned property keys do not include keys that are Symbols
  986. // 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
  987. // 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
  988. // 4- A property name will be returned by the iterator's next method at most once in any enumeration.
  989. // 5- Enumerating the properties of the target object includes enumerating properties of its prototype, and the prototype of the prototype, and so on, recursively;
  990. // 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.
  991. // 6- The values of [[Enumerable]] attributes are not considered when determining if a property of a prototype object has already been processed.
  992. // 7- The enumerable property names of prototype objects must be obtained by invoking EnumerateObjectProperties passing the prototype object as the argument.
  993. // 8- EnumerateObjectProperties must obtain the own property keys of the target object by calling its [[OwnPropertyKeys]] internal method.
  994. // 9- Property attributes of the target object must be obtained by calling its [[GetOwnProperty]] internal method
  995. // Invariant 3 effectively allows the implementation to ignore newly added keys, and we do so (similar to other implementations).
  996. auto& vm = interpreter.vm();
  997. auto object = TRY(interpreter.accumulator().to_object(vm));
  998. // Note: While the spec doesn't explicitly require these to be ordered, it says that the values should be retrieved via OwnPropertyKeys,
  999. // so we just keep the order consistent anyway.
  1000. OrderedHashTable<PropertyKey> properties;
  1001. OrderedHashTable<PropertyKey> non_enumerable_properties;
  1002. HashTable<NonnullGCPtr<Object>> seen_objects;
  1003. // Collect all keys immediately (invariant no. 5)
  1004. 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())) {
  1005. seen_objects.set(*object_to_check);
  1006. for (auto& key : TRY(object_to_check->internal_own_property_keys())) {
  1007. if (key.is_symbol())
  1008. continue;
  1009. auto property_key = TRY(PropertyKey::from_value(vm, key));
  1010. // If there is a non-enumerable property higher up the prototype chain with the same key,
  1011. // we mustn't include this property even if it's enumerable (invariant no. 5 and 6)
  1012. if (non_enumerable_properties.contains(property_key))
  1013. continue;
  1014. if (properties.contains(property_key))
  1015. continue;
  1016. auto descriptor = TRY(object_to_check->internal_get_own_property(property_key));
  1017. if (!*descriptor->enumerable)
  1018. non_enumerable_properties.set(move(property_key));
  1019. else
  1020. properties.set(move(property_key));
  1021. }
  1022. }
  1023. IteratorRecord iterator {
  1024. .iterator = object,
  1025. .next_method = NativeFunction::create(
  1026. interpreter.realm(),
  1027. [items = move(properties)](VM& vm) mutable -> ThrowCompletionOr<Value> {
  1028. auto& realm = *vm.current_realm();
  1029. auto iterated_object_value = vm.this_value();
  1030. if (!iterated_object_value.is_object())
  1031. return vm.throw_completion<InternalError>("Invalid state for GetObjectPropertyIterator.next"sv);
  1032. auto& iterated_object = iterated_object_value.as_object();
  1033. auto result_object = Object::create(realm, nullptr);
  1034. while (true) {
  1035. if (items.is_empty()) {
  1036. result_object->define_direct_property(vm.names.done, JS::Value(true), default_attributes);
  1037. return result_object;
  1038. }
  1039. auto key = items.take_first();
  1040. // If the property is deleted, don't include it (invariant no. 2)
  1041. if (!TRY(iterated_object.has_property(key)))
  1042. continue;
  1043. result_object->define_direct_property(vm.names.done, JS::Value(false), default_attributes);
  1044. if (key.is_number())
  1045. result_object->define_direct_property(vm.names.value, PrimitiveString::create(vm, TRY_OR_THROW_OOM(vm, String::number(key.as_number()))), default_attributes);
  1046. else if (key.is_string())
  1047. result_object->define_direct_property(vm.names.value, PrimitiveString::create(vm, key.as_string()), default_attributes);
  1048. else
  1049. VERIFY_NOT_REACHED(); // We should not have non-string/number keys.
  1050. return result_object;
  1051. }
  1052. },
  1053. 1,
  1054. vm.names.next),
  1055. .done = false,
  1056. };
  1057. interpreter.accumulator() = iterator_to_object(vm, move(iterator));
  1058. return {};
  1059. }
  1060. ThrowCompletionOr<void> IteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const
  1061. {
  1062. auto& vm = interpreter.vm();
  1063. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  1064. auto iterator = object_to_iterator(vm, iterator_object);
  1065. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1066. TRY(iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value, {} }));
  1067. return {};
  1068. }
  1069. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  1070. {
  1071. auto& vm = interpreter.vm();
  1072. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  1073. auto iterator = object_to_iterator(vm, iterator_object);
  1074. interpreter.accumulator() = TRY(iterator_next(vm, iterator));
  1075. return {};
  1076. }
  1077. ThrowCompletionOr<void> IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const
  1078. {
  1079. auto& vm = interpreter.vm();
  1080. auto iterator_result = TRY(interpreter.accumulator().to_object(vm));
  1081. auto complete = TRY(iterator_complete(vm, iterator_result));
  1082. interpreter.accumulator() = Value(complete);
  1083. return {};
  1084. }
  1085. ThrowCompletionOr<void> IteratorResultValue::execute_impl(Bytecode::Interpreter& interpreter) const
  1086. {
  1087. auto& vm = interpreter.vm();
  1088. auto iterator_result = TRY(interpreter.accumulator().to_object(vm));
  1089. interpreter.accumulator() = TRY(iterator_value(vm, iterator_result));
  1090. return {};
  1091. }
  1092. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const
  1093. {
  1094. auto& vm = interpreter.vm();
  1095. auto name = m_class_expression.name();
  1096. auto super_class = interpreter.accumulator();
  1097. // NOTE: NewClass expects classEnv to be active lexical environment
  1098. auto class_environment = vm.lexical_environment();
  1099. vm.running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
  1100. DeprecatedFlyString binding_name;
  1101. DeprecatedFlyString class_name;
  1102. if (!m_class_expression.has_name() && m_lhs_name.has_value()) {
  1103. class_name = interpreter.current_executable().get_identifier(m_lhs_name.value());
  1104. } else {
  1105. binding_name = name;
  1106. class_name = name.is_null() ? ""sv : name;
  1107. }
  1108. interpreter.accumulator() = TRY(m_class_expression.create_class_constructor(vm, class_environment, vm.lexical_environment(), super_class, binding_name, class_name));
  1109. return {};
  1110. }
  1111. // 13.5.3.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-typeof-operator-runtime-semantics-evaluation
  1112. ThrowCompletionOr<void> TypeofVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  1113. {
  1114. auto& vm = interpreter.vm();
  1115. // 1. Let val be the result of evaluating UnaryExpression.
  1116. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  1117. auto reference = TRY(vm.resolve_binding(string));
  1118. // 2. If val is a Reference Record, then
  1119. // a. If IsUnresolvableReference(val) is true, return "undefined".
  1120. if (reference.is_unresolvable()) {
  1121. interpreter.accumulator() = MUST_OR_THROW_OOM(PrimitiveString::create(vm, "undefined"sv));
  1122. return {};
  1123. }
  1124. // 3. Set val to ? GetValue(val).
  1125. auto value = TRY(reference.get_value(vm));
  1126. // 4. NOTE: This step is replaced in section B.3.6.3.
  1127. // 5. Return a String according to Table 41.
  1128. interpreter.accumulator() = MUST_OR_THROW_OOM(PrimitiveString::create(vm, value.typeof()));
  1129. return {};
  1130. }
  1131. ThrowCompletionOr<void> TypeofLocal::execute_impl(Bytecode::Interpreter& interpreter) const
  1132. {
  1133. auto& vm = interpreter.vm();
  1134. auto const& value = vm.running_execution_context().local_variables[m_index];
  1135. interpreter.accumulator() = MUST_OR_THROW_OOM(PrimitiveString::create(vm, value.typeof()));
  1136. return {};
  1137. }
  1138. ThrowCompletionOr<void> ToNumeric::execute_impl(Bytecode::Interpreter& interpreter) const
  1139. {
  1140. interpreter.accumulator() = TRY(interpreter.accumulator().to_numeric(interpreter.vm()));
  1141. return {};
  1142. }
  1143. ThrowCompletionOr<void> BlockDeclarationInstantiation::execute_impl(Bytecode::Interpreter& interpreter) const
  1144. {
  1145. auto& vm = interpreter.vm();
  1146. auto old_environment = vm.running_execution_context().lexical_environment;
  1147. interpreter.saved_lexical_environment_stack().append(old_environment);
  1148. vm.running_execution_context().lexical_environment = new_declarative_environment(*old_environment);
  1149. m_scope_node.block_declaration_instantiation(vm, vm.running_execution_context().lexical_environment);
  1150. return {};
  1151. }
  1152. DeprecatedString Load::to_deprecated_string_impl(Bytecode::Executable const&) const
  1153. {
  1154. return DeprecatedString::formatted("Load {}", m_src);
  1155. }
  1156. DeprecatedString LoadImmediate::to_deprecated_string_impl(Bytecode::Executable const&) const
  1157. {
  1158. return DeprecatedString::formatted("LoadImmediate {}", m_value);
  1159. }
  1160. DeprecatedString Store::to_deprecated_string_impl(Bytecode::Executable const&) const
  1161. {
  1162. return DeprecatedString::formatted("Store {}", m_dst);
  1163. }
  1164. DeprecatedString NewBigInt::to_deprecated_string_impl(Bytecode::Executable const&) const
  1165. {
  1166. return DeprecatedString::formatted("NewBigInt \"{}\"", m_bigint.to_base_deprecated(10));
  1167. }
  1168. DeprecatedString NewArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1169. {
  1170. StringBuilder builder;
  1171. builder.append("NewArray"sv);
  1172. if (m_element_count != 0) {
  1173. builder.appendff(" [{}-{}]", m_elements[0], m_elements[1]);
  1174. }
  1175. return builder.to_deprecated_string();
  1176. }
  1177. DeprecatedString Append::to_deprecated_string_impl(Bytecode::Executable const&) const
  1178. {
  1179. if (m_is_spread)
  1180. return DeprecatedString::formatted("Append lhs: **{}", m_lhs);
  1181. return DeprecatedString::formatted("Append lhs: {}", m_lhs);
  1182. }
  1183. DeprecatedString IteratorToArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1184. {
  1185. return "IteratorToArray";
  1186. }
  1187. DeprecatedString NewString::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1188. {
  1189. return DeprecatedString::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  1190. }
  1191. DeprecatedString NewObject::to_deprecated_string_impl(Bytecode::Executable const&) const
  1192. {
  1193. return "NewObject";
  1194. }
  1195. DeprecatedString NewRegExp::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1196. {
  1197. return DeprecatedString::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  1198. }
  1199. DeprecatedString CopyObjectExcludingProperties::to_deprecated_string_impl(Bytecode::Executable const&) const
  1200. {
  1201. StringBuilder builder;
  1202. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  1203. if (m_excluded_names_count != 0) {
  1204. builder.append(" excluding:["sv);
  1205. builder.join(", "sv, ReadonlySpan<Register>(m_excluded_names, m_excluded_names_count));
  1206. builder.append(']');
  1207. }
  1208. return builder.to_deprecated_string();
  1209. }
  1210. DeprecatedString ConcatString::to_deprecated_string_impl(Bytecode::Executable const&) const
  1211. {
  1212. return DeprecatedString::formatted("ConcatString {}", m_lhs);
  1213. }
  1214. DeprecatedString GetVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1215. {
  1216. return DeprecatedString::formatted("GetVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1217. }
  1218. DeprecatedString GetLocal::to_deprecated_string_impl(Bytecode::Executable const&) const
  1219. {
  1220. return DeprecatedString::formatted("GetLocal {}", m_index);
  1221. }
  1222. DeprecatedString DeleteVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1223. {
  1224. return DeprecatedString::formatted("DeleteVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1225. }
  1226. DeprecatedString CreateLexicalEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const
  1227. {
  1228. return "CreateLexicalEnvironment"sv;
  1229. }
  1230. DeprecatedString CreateVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1231. {
  1232. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1233. return DeprecatedString::formatted("CreateVariable env:{} immutable:{} global:{} {} ({})", mode_string, m_is_immutable, m_is_global, m_identifier, executable.identifier_table->get(m_identifier));
  1234. }
  1235. DeprecatedString EnterObjectEnvironment::to_deprecated_string_impl(Executable const&) const
  1236. {
  1237. return DeprecatedString::formatted("EnterObjectEnvironment");
  1238. }
  1239. DeprecatedString SetVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1240. {
  1241. auto initialization_mode_name = m_initialization_mode == InitializationMode ::Initialize ? "Initialize"
  1242. : m_initialization_mode == InitializationMode::Set ? "Set"
  1243. : "InitializeOrSet";
  1244. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  1245. return DeprecatedString::formatted("SetVariable env:{} init:{} {} ({})", mode_string, initialization_mode_name, m_identifier, executable.identifier_table->get(m_identifier));
  1246. }
  1247. DeprecatedString SetLocal::to_deprecated_string_impl(Bytecode::Executable const&) const
  1248. {
  1249. return DeprecatedString::formatted("SetLocal {}", m_index);
  1250. }
  1251. DeprecatedString PutById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1252. {
  1253. auto kind = m_kind == PropertyKind::Getter
  1254. ? "getter"
  1255. : m_kind == PropertyKind::Setter
  1256. ? "setter"
  1257. : "property";
  1258. return DeprecatedString::formatted("PutById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  1259. }
  1260. DeprecatedString PutByIdWithThis::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1261. {
  1262. auto kind = m_kind == PropertyKind::Getter
  1263. ? "getter"
  1264. : m_kind == PropertyKind::Setter
  1265. ? "setter"
  1266. : "property";
  1267. return DeprecatedString::formatted("PutByIdWithThis kind:{} base:{}, property:{} ({}) this_value:{}", kind, m_base, m_property, executable.identifier_table->get(m_property), m_this_value);
  1268. }
  1269. DeprecatedString PutPrivateById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1270. {
  1271. auto kind = m_kind == PropertyKind::Getter
  1272. ? "getter"
  1273. : m_kind == PropertyKind::Setter
  1274. ? "setter"
  1275. : "property";
  1276. return DeprecatedString::formatted("PutPrivateById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  1277. }
  1278. DeprecatedString GetById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1279. {
  1280. return DeprecatedString::formatted("GetById {} ({})", m_property, executable.identifier_table->get(m_property));
  1281. }
  1282. DeprecatedString GetByIdWithThis::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1283. {
  1284. return DeprecatedString::formatted("GetByIdWithThis {} ({}) this_value:{}", m_property, executable.identifier_table->get(m_property), m_this_value);
  1285. }
  1286. DeprecatedString GetPrivateById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1287. {
  1288. return DeprecatedString::formatted("GetPrivateById {} ({})", m_property, executable.identifier_table->get(m_property));
  1289. }
  1290. DeprecatedString HasPrivateId::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1291. {
  1292. return DeprecatedString::formatted("HasPrivateId {} ({})", m_property, executable.identifier_table->get(m_property));
  1293. }
  1294. DeprecatedString DeleteById::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1295. {
  1296. return DeprecatedString::formatted("DeleteById {} ({})", m_property, executable.identifier_table->get(m_property));
  1297. }
  1298. DeprecatedString DeleteByIdWithThis::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1299. {
  1300. return DeprecatedString::formatted("DeleteByIdWithThis {} ({}) this_value:{}", m_property, executable.identifier_table->get(m_property), m_this_value);
  1301. }
  1302. DeprecatedString Jump::to_deprecated_string_impl(Bytecode::Executable const&) const
  1303. {
  1304. if (m_true_target.has_value())
  1305. return DeprecatedString::formatted("Jump {}", *m_true_target);
  1306. return DeprecatedString::formatted("Jump <empty>");
  1307. }
  1308. DeprecatedString JumpConditional::to_deprecated_string_impl(Bytecode::Executable const&) const
  1309. {
  1310. auto true_string = m_true_target.has_value() ? DeprecatedString::formatted("{}", *m_true_target) : "<empty>";
  1311. auto false_string = m_false_target.has_value() ? DeprecatedString::formatted("{}", *m_false_target) : "<empty>";
  1312. return DeprecatedString::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  1313. }
  1314. DeprecatedString JumpNullish::to_deprecated_string_impl(Bytecode::Executable const&) const
  1315. {
  1316. auto true_string = m_true_target.has_value() ? DeprecatedString::formatted("{}", *m_true_target) : "<empty>";
  1317. auto false_string = m_false_target.has_value() ? DeprecatedString::formatted("{}", *m_false_target) : "<empty>";
  1318. return DeprecatedString::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  1319. }
  1320. DeprecatedString JumpUndefined::to_deprecated_string_impl(Bytecode::Executable const&) const
  1321. {
  1322. auto true_string = m_true_target.has_value() ? DeprecatedString::formatted("{}", *m_true_target) : "<empty>";
  1323. auto false_string = m_false_target.has_value() ? DeprecatedString::formatted("{}", *m_false_target) : "<empty>";
  1324. return DeprecatedString::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  1325. }
  1326. static StringView call_type_to_string(CallType type)
  1327. {
  1328. switch (type) {
  1329. case CallType::Call:
  1330. return ""sv;
  1331. case CallType::Construct:
  1332. return " (Construct)"sv;
  1333. case CallType::DirectEval:
  1334. return " (DirectEval)"sv;
  1335. }
  1336. VERIFY_NOT_REACHED();
  1337. }
  1338. DeprecatedString Call::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1339. {
  1340. auto type = call_type_to_string(m_type);
  1341. if (m_expression_string.has_value())
  1342. return DeprecatedString::formatted("Call{} callee:{}, this:{}, first_arg:{} ({})", type, m_callee, m_this_value, m_first_argument, executable.get_string(m_expression_string.value()));
  1343. return DeprecatedString::formatted("Call{} callee:{}, this:{}, first_arg:{}", type, m_callee, m_first_argument, m_this_value);
  1344. }
  1345. DeprecatedString CallWithArgumentArray::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1346. {
  1347. auto type = call_type_to_string(m_type);
  1348. if (m_expression_string.has_value())
  1349. return DeprecatedString::formatted("CallWithArgumentArray{} callee:{}, this:{}, arguments:[...acc] ({})", type, m_callee, m_this_value, executable.get_string(m_expression_string.value()));
  1350. return DeprecatedString::formatted("CallWithArgumentArray{} callee:{}, this:{}, arguments:[...acc]", type, m_callee, m_this_value);
  1351. }
  1352. DeprecatedString SuperCallWithArgumentArray::to_deprecated_string_impl(Bytecode::Executable const&) const
  1353. {
  1354. return "SuperCallWithArgumentArray arguments:[...acc]"sv;
  1355. }
  1356. DeprecatedString NewFunction::to_deprecated_string_impl(Bytecode::Executable const&) const
  1357. {
  1358. StringBuilder builder;
  1359. builder.append("NewFunction"sv);
  1360. if (m_function_node.has_name())
  1361. builder.appendff(" name:{}"sv, m_function_node.name());
  1362. if (m_lhs_name.has_value())
  1363. builder.appendff(" lhs_name:{}"sv, m_lhs_name.value());
  1364. if (m_home_object.has_value())
  1365. builder.appendff(" home_object:{}"sv, m_home_object.value());
  1366. return builder.to_deprecated_string();
  1367. }
  1368. DeprecatedString NewClass::to_deprecated_string_impl(Bytecode::Executable const&) const
  1369. {
  1370. StringBuilder builder;
  1371. auto name = m_class_expression.name();
  1372. builder.appendff("NewClass '{}'"sv, name.is_null() ? ""sv : name);
  1373. if (m_lhs_name.has_value())
  1374. builder.appendff(" lhs_name:{}"sv, m_lhs_name.value());
  1375. return builder.to_deprecated_string();
  1376. }
  1377. DeprecatedString Return::to_deprecated_string_impl(Bytecode::Executable const&) const
  1378. {
  1379. return "Return";
  1380. }
  1381. DeprecatedString Increment::to_deprecated_string_impl(Bytecode::Executable const&) const
  1382. {
  1383. return "Increment";
  1384. }
  1385. DeprecatedString Decrement::to_deprecated_string_impl(Bytecode::Executable const&) const
  1386. {
  1387. return "Decrement";
  1388. }
  1389. DeprecatedString Throw::to_deprecated_string_impl(Bytecode::Executable const&) const
  1390. {
  1391. return "Throw";
  1392. }
  1393. DeprecatedString ThrowIfNotObject::to_deprecated_string_impl(Bytecode::Executable const&) const
  1394. {
  1395. return "ThrowIfNotObject";
  1396. }
  1397. DeprecatedString ThrowIfNullish::to_deprecated_string_impl(Bytecode::Executable const&) const
  1398. {
  1399. return "ThrowIfNullish";
  1400. }
  1401. DeprecatedString EnterUnwindContext::to_deprecated_string_impl(Bytecode::Executable const&) const
  1402. {
  1403. auto handler_string = m_handler_target.has_value() ? DeprecatedString::formatted("{}", *m_handler_target) : "<empty>";
  1404. auto finalizer_string = m_finalizer_target.has_value() ? DeprecatedString::formatted("{}", *m_finalizer_target) : "<empty>";
  1405. return DeprecatedString::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  1406. }
  1407. DeprecatedString ScheduleJump::to_deprecated_string_impl(Bytecode::Executable const&) const
  1408. {
  1409. return DeprecatedString::formatted("ScheduleJump {}", m_target);
  1410. }
  1411. DeprecatedString LeaveLexicalEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const
  1412. {
  1413. return "LeaveLexicalEnvironment"sv;
  1414. }
  1415. DeprecatedString LeaveUnwindContext::to_deprecated_string_impl(Bytecode::Executable const&) const
  1416. {
  1417. return "LeaveUnwindContext";
  1418. }
  1419. DeprecatedString ContinuePendingUnwind::to_deprecated_string_impl(Bytecode::Executable const&) const
  1420. {
  1421. return DeprecatedString::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  1422. }
  1423. DeprecatedString PushDeclarativeEnvironment::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1424. {
  1425. StringBuilder builder;
  1426. builder.append("PushDeclarativeEnvironment"sv);
  1427. if (!m_variables.is_empty()) {
  1428. builder.append(" {"sv);
  1429. Vector<DeprecatedString> names;
  1430. for (auto& it : m_variables)
  1431. names.append(executable.get_string(it.key));
  1432. builder.append('}');
  1433. builder.join(", "sv, names);
  1434. }
  1435. return builder.to_deprecated_string();
  1436. }
  1437. DeprecatedString Yield::to_deprecated_string_impl(Bytecode::Executable const&) const
  1438. {
  1439. if (m_continuation_label.has_value())
  1440. return DeprecatedString::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  1441. return DeprecatedString::formatted("Yield return");
  1442. }
  1443. DeprecatedString GetByValue::to_deprecated_string_impl(Bytecode::Executable const&) const
  1444. {
  1445. return DeprecatedString::formatted("GetByValue base:{}", m_base);
  1446. }
  1447. DeprecatedString GetByValueWithThis::to_deprecated_string_impl(Bytecode::Executable const&) const
  1448. {
  1449. return DeprecatedString::formatted("GetByValueWithThis base:{} this_value:{}", m_base, m_this_value);
  1450. }
  1451. DeprecatedString PutByValue::to_deprecated_string_impl(Bytecode::Executable const&) const
  1452. {
  1453. auto kind = m_kind == PropertyKind::Getter
  1454. ? "getter"
  1455. : m_kind == PropertyKind::Setter
  1456. ? "setter"
  1457. : "property";
  1458. return DeprecatedString::formatted("PutByValue kind:{} base:{}, property:{}", kind, m_base, m_property);
  1459. }
  1460. DeprecatedString PutByValueWithThis::to_deprecated_string_impl(Bytecode::Executable const&) const
  1461. {
  1462. auto kind = m_kind == PropertyKind::Getter
  1463. ? "getter"
  1464. : m_kind == PropertyKind::Setter
  1465. ? "setter"
  1466. : "property";
  1467. return DeprecatedString::formatted("PutByValueWithThis kind:{} base:{}, property:{} this_value:{}", kind, m_base, m_property, m_this_value);
  1468. }
  1469. DeprecatedString DeleteByValue::to_deprecated_string_impl(Bytecode::Executable const&) const
  1470. {
  1471. return DeprecatedString::formatted("DeleteByValue base:{}", m_base);
  1472. }
  1473. DeprecatedString DeleteByValueWithThis::to_deprecated_string_impl(Bytecode::Executable const&) const
  1474. {
  1475. return DeprecatedString::formatted("DeleteByValueWithThis base:{} this_value:{}", m_base, m_this_value);
  1476. }
  1477. DeprecatedString GetIterator::to_deprecated_string_impl(Executable const&) const
  1478. {
  1479. auto hint = m_hint == IteratorHint::Sync ? "sync" : "async";
  1480. return DeprecatedString::formatted("GetIterator hint:{}", hint);
  1481. }
  1482. DeprecatedString GetMethod::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1483. {
  1484. return DeprecatedString::formatted("GetMethod {} ({})", m_property, executable.identifier_table->get(m_property));
  1485. }
  1486. DeprecatedString GetObjectPropertyIterator::to_deprecated_string_impl(Bytecode::Executable const&) const
  1487. {
  1488. return "GetObjectPropertyIterator";
  1489. }
  1490. DeprecatedString IteratorClose::to_deprecated_string_impl(Bytecode::Executable const&) const
  1491. {
  1492. if (!m_completion_value.has_value())
  1493. return DeprecatedString::formatted("IteratorClose completion_type={} completion_value=<empty>", to_underlying(m_completion_type));
  1494. auto completion_value_string = m_completion_value->to_string_without_side_effects().release_value_but_fixme_should_propagate_errors();
  1495. return DeprecatedString::formatted("IteratorClose completion_type={} completion_value={}", to_underlying(m_completion_type), completion_value_string);
  1496. }
  1497. DeprecatedString IteratorNext::to_deprecated_string_impl(Executable const&) const
  1498. {
  1499. return "IteratorNext";
  1500. }
  1501. DeprecatedString IteratorResultDone::to_deprecated_string_impl(Executable const&) const
  1502. {
  1503. return "IteratorResultDone";
  1504. }
  1505. DeprecatedString IteratorResultValue::to_deprecated_string_impl(Executable const&) const
  1506. {
  1507. return "IteratorResultValue";
  1508. }
  1509. DeprecatedString ResolveThisBinding::to_deprecated_string_impl(Bytecode::Executable const&) const
  1510. {
  1511. return "ResolveThisBinding"sv;
  1512. }
  1513. DeprecatedString ResolveSuperBase::to_deprecated_string_impl(Bytecode::Executable const&) const
  1514. {
  1515. return "ResolveSuperBase"sv;
  1516. }
  1517. DeprecatedString GetNewTarget::to_deprecated_string_impl(Bytecode::Executable const&) const
  1518. {
  1519. return "GetNewTarget"sv;
  1520. }
  1521. DeprecatedString TypeofVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
  1522. {
  1523. return DeprecatedString::formatted("TypeofVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1524. }
  1525. DeprecatedString TypeofLocal::to_deprecated_string_impl(Bytecode::Executable const&) const
  1526. {
  1527. return DeprecatedString::formatted("TypeofLocal {}", m_index);
  1528. }
  1529. DeprecatedString ToNumeric::to_deprecated_string_impl(Bytecode::Executable const&) const
  1530. {
  1531. return "ToNumeric"sv;
  1532. }
  1533. DeprecatedString BlockDeclarationInstantiation::to_deprecated_string_impl(Bytecode::Executable const&) const
  1534. {
  1535. return "BlockDeclarationInstantiation"sv;
  1536. }
  1537. DeprecatedString ImportCall::to_deprecated_string_impl(Bytecode::Executable const&) const
  1538. {
  1539. return DeprecatedString::formatted("ImportCall specifier:{} options:{}"sv, m_specifier, m_options);
  1540. }
  1541. }