Op.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, 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/Bytecode/Interpreter.h>
  10. #include <LibJS/Bytecode/Op.h>
  11. #include <LibJS/Runtime/AbstractOperations.h>
  12. #include <LibJS/Runtime/Array.h>
  13. #include <LibJS/Runtime/BigInt.h>
  14. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  15. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  16. #include <LibJS/Runtime/Environment.h>
  17. #include <LibJS/Runtime/GlobalEnvironment.h>
  18. #include <LibJS/Runtime/GlobalObject.h>
  19. #include <LibJS/Runtime/Iterator.h>
  20. #include <LibJS/Runtime/IteratorOperations.h>
  21. #include <LibJS/Runtime/NativeFunction.h>
  22. #include <LibJS/Runtime/ObjectEnvironment.h>
  23. #include <LibJS/Runtime/RegExpObject.h>
  24. #include <LibJS/Runtime/Value.h>
  25. namespace JS::Bytecode {
  26. String Instruction::to_string(Bytecode::Executable const& executable) const
  27. {
  28. #define __BYTECODE_OP(op) \
  29. case Instruction::Type::op: \
  30. return static_cast<Bytecode::Op::op const&>(*this).to_string_impl(executable);
  31. switch (type()) {
  32. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  33. default:
  34. VERIFY_NOT_REACHED();
  35. }
  36. #undef __BYTECODE_OP
  37. }
  38. }
  39. namespace JS::Bytecode::Op {
  40. static ThrowCompletionOr<void> put_by_property_key(Object* object, Value value, PropertyKey name, Bytecode::Interpreter& interpreter, PropertyKind kind)
  41. {
  42. auto& vm = interpreter.vm();
  43. if (kind == PropertyKind::Getter || kind == PropertyKind::Setter) {
  44. // The generator should only pass us functions for getters and setters.
  45. VERIFY(value.is_function());
  46. }
  47. switch (kind) {
  48. case PropertyKind::Getter: {
  49. auto& function = value.as_function();
  50. if (function.name().is_empty() && is<ECMAScriptFunctionObject>(function))
  51. static_cast<ECMAScriptFunctionObject*>(&function)->set_name(String::formatted("get {}", name));
  52. object->define_direct_accessor(name, &function, nullptr, Attribute::Configurable | Attribute::Enumerable);
  53. break;
  54. }
  55. case PropertyKind::Setter: {
  56. auto& function = value.as_function();
  57. if (function.name().is_empty() && is<ECMAScriptFunctionObject>(function))
  58. static_cast<ECMAScriptFunctionObject*>(&function)->set_name(String::formatted("set {}", name));
  59. object->define_direct_accessor(name, nullptr, &function, Attribute::Configurable | Attribute::Enumerable);
  60. break;
  61. }
  62. case PropertyKind::KeyValue: {
  63. bool succeeded = TRY(object->internal_set(name, interpreter.accumulator(), object));
  64. if (!succeeded && vm.in_strict_mode())
  65. return vm.throw_completion<TypeError>(ErrorType::ReferenceNullishSetProperty, name, interpreter.accumulator().to_string_without_side_effects());
  66. break;
  67. }
  68. case PropertyKind::Spread:
  69. TRY(object->copy_data_properties(vm, value, {}));
  70. break;
  71. case PropertyKind::ProtoSetter:
  72. if (value.is_object() || value.is_null())
  73. MUST(object->internal_set_prototype_of(value.is_object() ? &value.as_object() : nullptr));
  74. break;
  75. }
  76. return {};
  77. }
  78. ThrowCompletionOr<void> Load::execute_impl(Bytecode::Interpreter& interpreter) const
  79. {
  80. interpreter.accumulator() = interpreter.reg(m_src);
  81. return {};
  82. }
  83. ThrowCompletionOr<void> LoadImmediate::execute_impl(Bytecode::Interpreter& interpreter) const
  84. {
  85. interpreter.accumulator() = m_value;
  86. return {};
  87. }
  88. ThrowCompletionOr<void> Store::execute_impl(Bytecode::Interpreter& interpreter) const
  89. {
  90. interpreter.reg(m_dst) = interpreter.accumulator();
  91. return {};
  92. }
  93. static ThrowCompletionOr<Value> abstract_inequals(VM& vm, Value src1, Value src2)
  94. {
  95. return Value(!TRY(is_loosely_equal(vm, src1, src2)));
  96. }
  97. static ThrowCompletionOr<Value> abstract_equals(VM& vm, Value src1, Value src2)
  98. {
  99. return Value(TRY(is_loosely_equal(vm, src1, src2)));
  100. }
  101. static ThrowCompletionOr<Value> typed_inequals(VM&, Value src1, Value src2)
  102. {
  103. return Value(!is_strictly_equal(src1, src2));
  104. }
  105. static ThrowCompletionOr<Value> typed_equals(VM&, Value src1, Value src2)
  106. {
  107. return Value(is_strictly_equal(src1, src2));
  108. }
  109. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  110. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  111. { \
  112. auto& vm = interpreter.vm(); \
  113. auto lhs = interpreter.reg(m_lhs_reg); \
  114. auto rhs = interpreter.accumulator(); \
  115. interpreter.accumulator() = TRY(op_snake_case(vm, lhs, rhs)); \
  116. return {}; \
  117. } \
  118. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  119. { \
  120. return String::formatted(#OpTitleCase " {}", m_lhs_reg); \
  121. }
  122. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  123. static ThrowCompletionOr<Value> not_(VM&, Value value)
  124. {
  125. return Value(!value.to_boolean());
  126. }
  127. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  128. {
  129. return Value(js_string(vm, value.typeof()));
  130. }
  131. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  132. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  133. { \
  134. auto& vm = interpreter.vm(); \
  135. interpreter.accumulator() = TRY(op_snake_case(vm, interpreter.accumulator())); \
  136. return {}; \
  137. } \
  138. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  139. { \
  140. return #OpTitleCase; \
  141. }
  142. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  143. ThrowCompletionOr<void> NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
  144. {
  145. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), m_bigint);
  146. return {};
  147. }
  148. ThrowCompletionOr<void> NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  149. {
  150. auto* array = MUST(Array::create(interpreter.realm(), 0));
  151. for (size_t i = 0; i < m_element_count; i++) {
  152. auto& value = interpreter.reg(Register(m_elements[0].index() + i));
  153. array->indexed_properties().put(i, value, default_attributes);
  154. }
  155. interpreter.accumulator() = array;
  156. return {};
  157. }
  158. // 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.
  159. // Make sure to put this into the accumulator before the iterator object disappears from the stack to prevent the members from being GC'd.
  160. static Object* iterator_to_object(GlobalObject& global_object, Iterator iterator)
  161. {
  162. auto& vm = global_object.vm();
  163. auto& realm = *global_object.associated_realm();
  164. auto* object = Object::create(realm, nullptr);
  165. object->define_direct_property(vm.names.iterator, iterator.iterator, 0);
  166. object->define_direct_property(vm.names.next, iterator.next_method, 0);
  167. object->define_direct_property(vm.names.done, Value(iterator.done), 0);
  168. return object;
  169. }
  170. static Iterator object_to_iterator(GlobalObject& global_object, Object& object)
  171. {
  172. auto& vm = global_object.vm();
  173. return Iterator {
  174. .iterator = &MUST(object.get(vm.names.iterator)).as_object(),
  175. .next_method = MUST(object.get(vm.names.next)),
  176. .done = MUST(object.get(vm.names.done)).as_bool()
  177. };
  178. }
  179. ThrowCompletionOr<void> IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const
  180. {
  181. auto& global_object = interpreter.global_object();
  182. auto& vm = interpreter.vm();
  183. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  184. auto iterator = object_to_iterator(global_object, *iterator_object);
  185. auto* array = MUST(Array::create(interpreter.realm(), 0));
  186. size_t index = 0;
  187. while (true) {
  188. auto* iterator_result = TRY(iterator_next(global_object, iterator));
  189. auto complete = TRY(iterator_complete(global_object, *iterator_result));
  190. if (complete) {
  191. interpreter.accumulator() = array;
  192. return {};
  193. }
  194. auto value = TRY(iterator_value(global_object, *iterator_result));
  195. MUST(array->create_data_property_or_throw(index, value));
  196. index++;
  197. }
  198. return {};
  199. }
  200. ThrowCompletionOr<void> NewString::execute_impl(Bytecode::Interpreter& interpreter) const
  201. {
  202. interpreter.accumulator() = js_string(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  203. return {};
  204. }
  205. ThrowCompletionOr<void> NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  206. {
  207. interpreter.accumulator() = Object::create(interpreter.realm(), interpreter.global_object().object_prototype());
  208. return {};
  209. }
  210. ThrowCompletionOr<void> NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
  211. {
  212. auto source = interpreter.current_executable().get_string(m_source_index);
  213. auto flags = interpreter.current_executable().get_string(m_flags_index);
  214. interpreter.accumulator() = TRY(regexp_create(interpreter.global_object(), js_string(interpreter.vm(), source), js_string(interpreter.vm(), flags)));
  215. return {};
  216. }
  217. ThrowCompletionOr<void> CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const
  218. {
  219. auto& vm = interpreter.vm();
  220. auto* from_object = TRY(interpreter.reg(m_from_object).to_object(vm));
  221. auto* to_object = Object::create(interpreter.realm(), interpreter.global_object().object_prototype());
  222. HashTable<Value, ValueTraits> excluded_names;
  223. for (size_t i = 0; i < m_excluded_names_count; ++i)
  224. excluded_names.set(interpreter.reg(m_excluded_names[i]));
  225. auto own_keys = TRY(from_object->internal_own_property_keys());
  226. for (auto& key : own_keys) {
  227. if (!excluded_names.contains(key)) {
  228. auto property_key = TRY(key.to_property_key(vm));
  229. auto property_value = TRY(from_object->get(property_key));
  230. to_object->define_direct_property(property_key, property_value, JS::default_attributes);
  231. }
  232. }
  233. interpreter.accumulator() = to_object;
  234. return {};
  235. }
  236. ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  237. {
  238. auto& vm = interpreter.vm();
  239. interpreter.reg(m_lhs) = TRY(add(vm, interpreter.reg(m_lhs), interpreter.accumulator()));
  240. return {};
  241. }
  242. ThrowCompletionOr<void> GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  243. {
  244. auto& vm = interpreter.vm();
  245. auto get_reference = [&]() -> ThrowCompletionOr<Reference> {
  246. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  247. if (m_cached_environment_coordinate.has_value()) {
  248. auto* environment = vm.running_execution_context().lexical_environment;
  249. for (size_t i = 0; i < m_cached_environment_coordinate->hops; ++i)
  250. environment = environment->outer_environment();
  251. VERIFY(environment);
  252. VERIFY(environment->is_declarative_environment());
  253. if (!environment->is_permanently_screwed_by_eval()) {
  254. return Reference { *environment, string, vm.in_strict_mode(), m_cached_environment_coordinate };
  255. }
  256. m_cached_environment_coordinate = {};
  257. }
  258. auto reference = TRY(vm.resolve_binding(string));
  259. if (reference.environment_coordinate().has_value())
  260. m_cached_environment_coordinate = reference.environment_coordinate();
  261. return reference;
  262. };
  263. auto reference = TRY(get_reference());
  264. interpreter.accumulator() = TRY(reference.get_value(vm));
  265. return {};
  266. }
  267. ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  268. {
  269. auto& vm = interpreter.vm();
  270. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  271. auto reference = TRY(vm.resolve_binding(string));
  272. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  273. return {};
  274. }
  275. ThrowCompletionOr<void> CreateEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  276. {
  277. auto make_and_swap_envs = [&](auto*& old_environment) {
  278. Environment* environment = new_declarative_environment(*old_environment);
  279. swap(old_environment, environment);
  280. return environment;
  281. };
  282. if (m_mode == EnvironmentMode::Lexical)
  283. interpreter.saved_lexical_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().lexical_environment));
  284. else if (m_mode == EnvironmentMode::Var)
  285. interpreter.saved_variable_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().variable_environment));
  286. return {};
  287. }
  288. ThrowCompletionOr<void> EnterObjectEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  289. {
  290. auto& vm = interpreter.vm();
  291. auto& old_environment = vm.running_execution_context().lexical_environment;
  292. interpreter.saved_lexical_environment_stack().append(old_environment);
  293. auto object = TRY(interpreter.accumulator().to_object(vm));
  294. vm.running_execution_context().lexical_environment = new_object_environment(*object, true, old_environment);
  295. return {};
  296. }
  297. ThrowCompletionOr<void> CreateVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  298. {
  299. auto& vm = interpreter.vm();
  300. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  301. if (m_mode == EnvironmentMode::Lexical) {
  302. VERIFY(!m_is_global);
  303. // Note: This is papering over an issue where "FunctionDeclarationInstantiation" creates these bindings for us.
  304. // Instead of crashing in there, we'll just raise an exception here.
  305. if (TRY(vm.lexical_environment()->has_binding(name)))
  306. return vm.throw_completion<InternalError>(String::formatted("Lexical environment already has binding '{}'", name));
  307. if (m_is_immutable)
  308. vm.lexical_environment()->create_immutable_binding(vm, name, vm.in_strict_mode());
  309. else
  310. vm.lexical_environment()->create_mutable_binding(vm, name, vm.in_strict_mode());
  311. } else {
  312. if (!m_is_global) {
  313. if (m_is_immutable)
  314. vm.variable_environment()->create_immutable_binding(vm, name, vm.in_strict_mode());
  315. else
  316. vm.variable_environment()->create_mutable_binding(vm, name, vm.in_strict_mode());
  317. } else {
  318. // 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".
  319. // The only area that sets "can_be_deleted" to true is EvalDeclarationInstantiation, which is currently fully implemented in C++ and not in Bytecode.
  320. verify_cast<GlobalEnvironment>(vm.variable_environment())->create_global_var_binding(name, false);
  321. }
  322. }
  323. return {};
  324. }
  325. ThrowCompletionOr<void> SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  326. {
  327. auto& vm = interpreter.vm();
  328. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  329. auto environment = m_mode == EnvironmentMode::Lexical ? vm.running_execution_context().lexical_environment : vm.running_execution_context().variable_environment;
  330. auto reference = TRY(vm.resolve_binding(name, environment));
  331. switch (m_initialization_mode) {
  332. case InitializationMode::Initialize:
  333. TRY(reference.initialize_referenced_binding(vm, interpreter.accumulator()));
  334. break;
  335. case InitializationMode::Set:
  336. TRY(reference.put_value(vm, interpreter.accumulator()));
  337. break;
  338. case InitializationMode::InitializeOrSet:
  339. VERIFY(reference.is_environment_reference());
  340. VERIFY(reference.base_environment().is_declarative_environment());
  341. TRY(static_cast<DeclarativeEnvironment&>(reference.base_environment()).initialize_or_set_mutable_binding(vm, name, interpreter.accumulator()));
  342. break;
  343. }
  344. return {};
  345. }
  346. ThrowCompletionOr<void> GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  347. {
  348. auto& vm = interpreter.vm();
  349. auto* object = TRY(interpreter.accumulator().to_object(vm));
  350. interpreter.accumulator() = TRY(object->get(interpreter.current_executable().get_identifier(m_property)));
  351. return {};
  352. }
  353. ThrowCompletionOr<void> PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  354. {
  355. auto& vm = interpreter.vm();
  356. auto* object = TRY(interpreter.reg(m_base).to_object(vm));
  357. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  358. auto value = interpreter.accumulator();
  359. return put_by_property_key(object, value, name, interpreter, m_kind);
  360. }
  361. ThrowCompletionOr<void> DeleteById::execute_impl(Bytecode::Interpreter& interpreter) const
  362. {
  363. auto& vm = interpreter.vm();
  364. auto* object = TRY(interpreter.accumulator().to_object(vm));
  365. auto const& identifier = interpreter.current_executable().get_identifier(m_property);
  366. bool strict = vm.in_strict_mode();
  367. auto reference = Reference { object, identifier, {}, strict };
  368. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  369. return {};
  370. };
  371. ThrowCompletionOr<void> Jump::execute_impl(Bytecode::Interpreter& interpreter) const
  372. {
  373. interpreter.jump(*m_true_target);
  374. return {};
  375. }
  376. ThrowCompletionOr<void> ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const
  377. {
  378. auto& vm = interpreter.vm();
  379. interpreter.accumulator() = TRY(vm.resolve_this_binding());
  380. return {};
  381. }
  382. ThrowCompletionOr<void> GetNewTarget::execute_impl(Bytecode::Interpreter& interpreter) const
  383. {
  384. interpreter.accumulator() = interpreter.vm().get_new_target();
  385. return {};
  386. }
  387. void Jump::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  388. {
  389. if (m_true_target.has_value() && &m_true_target->block() == &from)
  390. m_true_target = Label { to };
  391. if (m_false_target.has_value() && &m_false_target->block() == &from)
  392. m_false_target = Label { to };
  393. }
  394. ThrowCompletionOr<void> JumpConditional::execute_impl(Bytecode::Interpreter& interpreter) const
  395. {
  396. VERIFY(m_true_target.has_value());
  397. VERIFY(m_false_target.has_value());
  398. auto result = interpreter.accumulator();
  399. if (result.to_boolean())
  400. interpreter.jump(m_true_target.value());
  401. else
  402. interpreter.jump(m_false_target.value());
  403. return {};
  404. }
  405. ThrowCompletionOr<void> JumpNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  406. {
  407. VERIFY(m_true_target.has_value());
  408. VERIFY(m_false_target.has_value());
  409. auto result = interpreter.accumulator();
  410. if (result.is_nullish())
  411. interpreter.jump(m_true_target.value());
  412. else
  413. interpreter.jump(m_false_target.value());
  414. return {};
  415. }
  416. ThrowCompletionOr<void> JumpUndefined::execute_impl(Bytecode::Interpreter& interpreter) const
  417. {
  418. VERIFY(m_true_target.has_value());
  419. VERIFY(m_false_target.has_value());
  420. auto result = interpreter.accumulator();
  421. if (result.is_undefined())
  422. interpreter.jump(m_true_target.value());
  423. else
  424. interpreter.jump(m_false_target.value());
  425. return {};
  426. }
  427. ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) const
  428. {
  429. auto callee = interpreter.reg(m_callee);
  430. if (m_type == CallType::Call && !callee.is_function())
  431. return interpreter.vm().throw_completion<TypeError>(ErrorType::IsNotA, callee.to_string_without_side_effects(), "function"sv);
  432. if (m_type == CallType::Construct && !callee.is_constructor())
  433. return interpreter.vm().throw_completion<TypeError>(ErrorType::IsNotA, callee.to_string_without_side_effects(), "constructor"sv);
  434. auto& function = callee.as_function();
  435. auto this_value = interpreter.reg(m_this_value);
  436. MarkedVector<Value> argument_values { interpreter.vm().heap() };
  437. for (size_t i = 0; i < m_argument_count; ++i)
  438. argument_values.append(interpreter.reg(m_arguments[i]));
  439. Value return_value;
  440. if (m_type == CallType::Call)
  441. return_value = TRY(call(interpreter.global_object(), function, this_value, move(argument_values)));
  442. else
  443. return_value = TRY(construct(interpreter.global_object(), function, move(argument_values)));
  444. interpreter.accumulator() = return_value;
  445. return {};
  446. }
  447. ThrowCompletionOr<void> NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  448. {
  449. auto& vm = interpreter.vm();
  450. 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(), 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());
  451. return {};
  452. }
  453. ThrowCompletionOr<void> Return::execute_impl(Bytecode::Interpreter& interpreter) const
  454. {
  455. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  456. return {};
  457. }
  458. ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  459. {
  460. auto& vm = interpreter.vm();
  461. auto old_value = TRY(interpreter.accumulator().to_numeric(vm));
  462. if (old_value.is_number())
  463. interpreter.accumulator() = Value(old_value.as_double() + 1);
  464. else
  465. interpreter.accumulator() = js_bigint(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  466. return {};
  467. }
  468. ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  469. {
  470. auto& vm = interpreter.vm();
  471. auto old_value = TRY(interpreter.accumulator().to_numeric(vm));
  472. if (old_value.is_number())
  473. interpreter.accumulator() = Value(old_value.as_double() - 1);
  474. else
  475. interpreter.accumulator() = js_bigint(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  476. return {};
  477. }
  478. ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  479. {
  480. return throw_completion(interpreter.accumulator());
  481. }
  482. ThrowCompletionOr<void> EnterUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  483. {
  484. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  485. interpreter.jump(m_entry_point);
  486. return {};
  487. }
  488. void EnterUnwindContext::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  489. {
  490. if (&m_entry_point.block() == &from)
  491. m_entry_point = Label { to };
  492. if (m_handler_target.has_value() && &m_handler_target->block() == &from)
  493. m_handler_target = Label { to };
  494. if (m_finalizer_target.has_value() && &m_finalizer_target->block() == &from)
  495. m_finalizer_target = Label { to };
  496. }
  497. ThrowCompletionOr<void> FinishUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  498. {
  499. interpreter.leave_unwind_context();
  500. interpreter.jump(m_next_target);
  501. return {};
  502. }
  503. void FinishUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  504. {
  505. if (&m_next_target.block() == &from)
  506. m_next_target = Label { to };
  507. }
  508. ThrowCompletionOr<void> LeaveEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  509. {
  510. if (m_mode == EnvironmentMode::Lexical)
  511. interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
  512. if (m_mode == EnvironmentMode::Var)
  513. interpreter.vm().running_execution_context().variable_environment = interpreter.saved_variable_environment_stack().take_last();
  514. return {};
  515. }
  516. ThrowCompletionOr<void> LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  517. {
  518. interpreter.leave_unwind_context();
  519. return {};
  520. }
  521. ThrowCompletionOr<void> ContinuePendingUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  522. {
  523. return interpreter.continue_pending_unwind(m_resume_target);
  524. }
  525. void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  526. {
  527. if (&m_resume_target.block() == &from)
  528. m_resume_target = Label { to };
  529. }
  530. ThrowCompletionOr<void> PushDeclarativeEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  531. {
  532. auto* environment = interpreter.vm().heap().allocate_without_realm<DeclarativeEnvironment>(interpreter.vm().lexical_environment());
  533. interpreter.vm().running_execution_context().lexical_environment = environment;
  534. interpreter.vm().running_execution_context().variable_environment = environment;
  535. return {};
  536. }
  537. ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  538. {
  539. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  540. auto object = Object::create(interpreter.realm(), nullptr);
  541. object->define_direct_property("result", yielded_value, JS::default_attributes);
  542. if (m_continuation_label.has_value())
  543. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes);
  544. else
  545. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  546. interpreter.do_return(object);
  547. return {};
  548. }
  549. void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  550. {
  551. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  552. m_continuation_label = Label { to };
  553. }
  554. ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  555. {
  556. auto& vm = interpreter.vm();
  557. auto* object = TRY(interpreter.reg(m_base).to_object(vm));
  558. auto property_key = TRY(interpreter.accumulator().to_property_key(vm));
  559. interpreter.accumulator() = TRY(object->get(property_key));
  560. return {};
  561. }
  562. ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  563. {
  564. auto& vm = interpreter.vm();
  565. auto* object = TRY(interpreter.reg(m_base).to_object(vm));
  566. auto property_key = TRY(interpreter.reg(m_property).to_property_key(vm));
  567. return put_by_property_key(object, interpreter.accumulator(), property_key, interpreter, m_kind);
  568. }
  569. ThrowCompletionOr<void> DeleteByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  570. {
  571. auto& vm = interpreter.vm();
  572. auto* object = TRY(interpreter.reg(m_base).to_object(vm));
  573. auto property_key = TRY(interpreter.accumulator().to_property_key(vm));
  574. bool strict = vm.in_strict_mode();
  575. auto reference = Reference { object, property_key, {}, strict };
  576. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  577. return {};
  578. }
  579. ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  580. {
  581. auto iterator = TRY(get_iterator(interpreter.global_object(), interpreter.accumulator()));
  582. interpreter.accumulator() = iterator_to_object(interpreter.global_object(), iterator);
  583. return {};
  584. }
  585. // 14.7.5.9 EnumerateObjectProperties ( O ), https://tc39.es/ecma262/#sec-enumerate-object-properties
  586. ThrowCompletionOr<void> GetObjectPropertyIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  587. {
  588. // While the spec does provide an algorithm, it allows us to implement it ourselves so long as we meet the following invariants:
  589. // 1- Returned property keys do not include keys that are Symbols
  590. // 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
  591. // 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
  592. // 4- A property name will be returned by the iterator's next method at most once in any enumeration.
  593. // 5- Enumerating the properties of the target object includes enumerating properties of its prototype, and the prototype of the prototype, and so on, recursively;
  594. // 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.
  595. // 6- The values of [[Enumerable]] attributes are not considered when determining if a property of a prototype object has already been processed.
  596. // 7- The enumerable property names of prototype objects must be obtained by invoking EnumerateObjectProperties passing the prototype object as the argument.
  597. // 8- EnumerateObjectProperties must obtain the own property keys of the target object by calling its [[OwnPropertyKeys]] internal method.
  598. // 9- Property attributes of the target object must be obtained by calling its [[GetOwnProperty]] internal method
  599. // Invariant 3 effectively allows the implementation to ignore newly added keys, and we do so (similar to other implementations).
  600. // Invariants 1 and 6 through 9 are implemented in `enumerable_own_property_names`, which implements the EnumerableOwnPropertyNames AO.
  601. auto& vm = interpreter.vm();
  602. auto* object = TRY(interpreter.accumulator().to_object(vm));
  603. // Note: While the spec doesn't explicitly require these to be ordered, it says that the values should be retrieved via OwnPropertyKeys,
  604. // so we just keep the order consistent anyway.
  605. OrderedHashTable<PropertyKey> properties;
  606. HashTable<Object*> seen_objects;
  607. // Collect all keys immediately (invariant no. 5)
  608. for (auto* object_to_check = object; object_to_check && !seen_objects.contains(object_to_check); object_to_check = TRY(object_to_check->internal_get_prototype_of())) {
  609. seen_objects.set(object_to_check);
  610. for (auto& key : TRY(object_to_check->enumerable_own_property_names(Object::PropertyKind::Key))) {
  611. properties.set(TRY(PropertyKey::from_value(vm, key)));
  612. }
  613. }
  614. Iterator iterator {
  615. .iterator = object,
  616. .next_method = NativeFunction::create(
  617. interpreter.realm(),
  618. [seen_items = HashTable<PropertyKey>(), items = move(properties)](VM& vm, GlobalObject& global_object) mutable -> ThrowCompletionOr<Value> {
  619. auto& realm = *global_object.associated_realm();
  620. auto iterated_object_value = vm.this_value();
  621. if (!iterated_object_value.is_object())
  622. return vm.throw_completion<InternalError>("Invalid state for GetObjectPropertyIterator.next");
  623. auto& iterated_object = iterated_object_value.as_object();
  624. auto* result_object = Object::create(realm, nullptr);
  625. while (true) {
  626. if (items.is_empty()) {
  627. result_object->define_direct_property(vm.names.done, JS::Value(true), default_attributes);
  628. return result_object;
  629. }
  630. auto it = items.begin();
  631. auto key = *it;
  632. items.remove(it);
  633. // If the key was already seen, skip over it (invariant no. 4)
  634. auto result = seen_items.set(key);
  635. if (result != AK::HashSetResult::InsertedNewEntry)
  636. continue;
  637. // If the property is deleted, don't include it (invariant no. 2)
  638. if (!TRY(iterated_object.has_property(key)))
  639. continue;
  640. result_object->define_direct_property(vm.names.done, JS::Value(false), default_attributes);
  641. if (key.is_number())
  642. result_object->define_direct_property(vm.names.value, JS::Value(key.as_number()), default_attributes);
  643. else if (key.is_string())
  644. result_object->define_direct_property(vm.names.value, js_string(vm.heap(), key.as_string()), default_attributes);
  645. else
  646. VERIFY_NOT_REACHED(); // We should not have non-string/number keys.
  647. return result_object;
  648. }
  649. },
  650. 1,
  651. interpreter.vm().names.next),
  652. .done = false,
  653. };
  654. interpreter.accumulator() = iterator_to_object(interpreter.global_object(), move(iterator));
  655. return {};
  656. }
  657. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  658. {
  659. auto& vm = interpreter.vm();
  660. auto* iterator_object = TRY(interpreter.accumulator().to_object(vm));
  661. auto iterator = object_to_iterator(interpreter.global_object(), *iterator_object);
  662. interpreter.accumulator() = TRY(iterator_next(interpreter.global_object(), iterator));
  663. return {};
  664. }
  665. ThrowCompletionOr<void> IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const
  666. {
  667. auto& vm = interpreter.vm();
  668. auto* iterator_result = TRY(interpreter.accumulator().to_object(vm));
  669. auto complete = TRY(iterator_complete(interpreter.global_object(), *iterator_result));
  670. interpreter.accumulator() = Value(complete);
  671. return {};
  672. }
  673. ThrowCompletionOr<void> IteratorResultValue::execute_impl(Bytecode::Interpreter& interpreter) const
  674. {
  675. auto& vm = interpreter.vm();
  676. auto* iterator_result = TRY(interpreter.accumulator().to_object(vm));
  677. interpreter.accumulator() = TRY(iterator_value(interpreter.global_object(), *iterator_result));
  678. return {};
  679. }
  680. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const
  681. {
  682. auto name = m_class_expression.name();
  683. auto scope = interpreter.ast_interpreter_scope();
  684. auto& ast_interpreter = scope.interpreter();
  685. auto class_object = TRY(m_class_expression.class_definition_evaluation(ast_interpreter, name, name.is_null() ? ""sv : name));
  686. interpreter.accumulator() = class_object;
  687. return {};
  688. }
  689. // 13.5.3.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-typeof-operator-runtime-semantics-evaluation
  690. ThrowCompletionOr<void> TypeofVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  691. {
  692. auto& vm = interpreter.vm();
  693. // 1. Let val be the result of evaluating UnaryExpression.
  694. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  695. auto reference = TRY(vm.resolve_binding(string));
  696. // 2. If val is a Reference Record, then
  697. // a. If IsUnresolvableReference(val) is true, return "undefined".
  698. if (reference.is_unresolvable()) {
  699. interpreter.accumulator() = js_string(vm, "undefined"sv);
  700. return {};
  701. }
  702. // 3. Set val to ? GetValue(val).
  703. auto value = TRY(reference.get_value(vm));
  704. // 4. NOTE: This step is replaced in section B.3.6.3.
  705. // 5. Return a String according to Table 41.
  706. interpreter.accumulator() = js_string(vm, value.typeof());
  707. return {};
  708. }
  709. String Load::to_string_impl(Bytecode::Executable const&) const
  710. {
  711. return String::formatted("Load {}", m_src);
  712. }
  713. String LoadImmediate::to_string_impl(Bytecode::Executable const&) const
  714. {
  715. return String::formatted("LoadImmediate {}", m_value);
  716. }
  717. String Store::to_string_impl(Bytecode::Executable const&) const
  718. {
  719. return String::formatted("Store {}", m_dst);
  720. }
  721. String NewBigInt::to_string_impl(Bytecode::Executable const&) const
  722. {
  723. return String::formatted("NewBigInt \"{}\"", m_bigint.to_base(10));
  724. }
  725. String NewArray::to_string_impl(Bytecode::Executable const&) const
  726. {
  727. StringBuilder builder;
  728. builder.append("NewArray"sv);
  729. if (m_element_count != 0) {
  730. builder.appendff(" [{}-{}]", m_elements[0], m_elements[1]);
  731. }
  732. return builder.to_string();
  733. }
  734. String IteratorToArray::to_string_impl(Bytecode::Executable const&) const
  735. {
  736. return "IteratorToArray";
  737. }
  738. String NewString::to_string_impl(Bytecode::Executable const& executable) const
  739. {
  740. return String::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  741. }
  742. String NewObject::to_string_impl(Bytecode::Executable const&) const
  743. {
  744. return "NewObject";
  745. }
  746. String NewRegExp::to_string_impl(Bytecode::Executable const& executable) const
  747. {
  748. return String::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  749. }
  750. String CopyObjectExcludingProperties::to_string_impl(Bytecode::Executable const&) const
  751. {
  752. StringBuilder builder;
  753. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  754. if (m_excluded_names_count != 0) {
  755. builder.append(" excluding:["sv);
  756. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  757. builder.appendff("{}", m_excluded_names[i]);
  758. if (i != m_excluded_names_count - 1)
  759. builder.append(',');
  760. }
  761. builder.append(']');
  762. }
  763. return builder.to_string();
  764. }
  765. String ConcatString::to_string_impl(Bytecode::Executable const&) const
  766. {
  767. return String::formatted("ConcatString {}", m_lhs);
  768. }
  769. String GetVariable::to_string_impl(Bytecode::Executable const& executable) const
  770. {
  771. return String::formatted("GetVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  772. }
  773. String DeleteVariable::to_string_impl(Bytecode::Executable const& executable) const
  774. {
  775. return String::formatted("DeleteVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  776. }
  777. String CreateEnvironment::to_string_impl(Bytecode::Executable const&) const
  778. {
  779. auto mode_string = m_mode == EnvironmentMode::Lexical
  780. ? "Lexical"
  781. : "Variable";
  782. return String::formatted("CreateEnvironment mode:{}", mode_string);
  783. }
  784. String CreateVariable::to_string_impl(Bytecode::Executable const& executable) const
  785. {
  786. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  787. return String::formatted("CreateVariable env:{} immutable:{} global:{} {} ({})", mode_string, m_is_immutable, m_is_global, m_identifier, executable.identifier_table->get(m_identifier));
  788. }
  789. String EnterObjectEnvironment::to_string_impl(Executable const&) const
  790. {
  791. return String::formatted("EnterObjectEnvironment");
  792. }
  793. String SetVariable::to_string_impl(Bytecode::Executable const& executable) const
  794. {
  795. auto initialization_mode_name = m_initialization_mode == InitializationMode ::Initialize ? "Initialize"
  796. : m_initialization_mode == InitializationMode::Set ? "Set"
  797. : "InitializeOrSet";
  798. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  799. return String::formatted("SetVariable env:{} init:{} {} ({})", mode_string, initialization_mode_name, m_identifier, executable.identifier_table->get(m_identifier));
  800. }
  801. String PutById::to_string_impl(Bytecode::Executable const& executable) const
  802. {
  803. auto kind = m_kind == PropertyKind::Getter
  804. ? "getter"
  805. : m_kind == PropertyKind::Setter
  806. ? "setter"
  807. : "property";
  808. return String::formatted("PutById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  809. }
  810. String GetById::to_string_impl(Bytecode::Executable const& executable) const
  811. {
  812. return String::formatted("GetById {} ({})", m_property, executable.identifier_table->get(m_property));
  813. }
  814. String DeleteById::to_string_impl(Bytecode::Executable const& executable) const
  815. {
  816. return String::formatted("DeleteById {} ({})", m_property, executable.identifier_table->get(m_property));
  817. }
  818. String Jump::to_string_impl(Bytecode::Executable const&) const
  819. {
  820. if (m_true_target.has_value())
  821. return String::formatted("Jump {}", *m_true_target);
  822. return String::formatted("Jump <empty>");
  823. }
  824. String JumpConditional::to_string_impl(Bytecode::Executable const&) const
  825. {
  826. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  827. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  828. return String::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  829. }
  830. String JumpNullish::to_string_impl(Bytecode::Executable const&) const
  831. {
  832. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  833. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  834. return String::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  835. }
  836. String JumpUndefined::to_string_impl(Bytecode::Executable const&) const
  837. {
  838. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  839. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  840. return String::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  841. }
  842. String Call::to_string_impl(Bytecode::Executable const&) const
  843. {
  844. StringBuilder builder;
  845. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  846. if (m_argument_count != 0) {
  847. builder.append(", arguments:["sv);
  848. for (size_t i = 0; i < m_argument_count; ++i) {
  849. builder.appendff("{}", m_arguments[i]);
  850. if (i != m_argument_count - 1)
  851. builder.append(',');
  852. }
  853. builder.append(']');
  854. }
  855. return builder.to_string();
  856. }
  857. String NewFunction::to_string_impl(Bytecode::Executable const&) const
  858. {
  859. return "NewFunction";
  860. }
  861. String NewClass::to_string_impl(Bytecode::Executable const&) const
  862. {
  863. return "NewClass";
  864. }
  865. String Return::to_string_impl(Bytecode::Executable const&) const
  866. {
  867. return "Return";
  868. }
  869. String Increment::to_string_impl(Bytecode::Executable const&) const
  870. {
  871. return "Increment";
  872. }
  873. String Decrement::to_string_impl(Bytecode::Executable const&) const
  874. {
  875. return "Decrement";
  876. }
  877. String Throw::to_string_impl(Bytecode::Executable const&) const
  878. {
  879. return "Throw";
  880. }
  881. String EnterUnwindContext::to_string_impl(Bytecode::Executable const&) const
  882. {
  883. auto handler_string = m_handler_target.has_value() ? String::formatted("{}", *m_handler_target) : "<empty>";
  884. auto finalizer_string = m_finalizer_target.has_value() ? String::formatted("{}", *m_finalizer_target) : "<empty>";
  885. return String::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  886. }
  887. String FinishUnwind::to_string_impl(Bytecode::Executable const&) const
  888. {
  889. return String::formatted("FinishUnwind next:{}", m_next_target);
  890. }
  891. String LeaveEnvironment::to_string_impl(Bytecode::Executable const&) const
  892. {
  893. auto mode_string = m_mode == EnvironmentMode::Lexical
  894. ? "Lexical"
  895. : "Variable";
  896. return String::formatted("LeaveEnvironment env:{}", mode_string);
  897. }
  898. String LeaveUnwindContext::to_string_impl(Bytecode::Executable const&) const
  899. {
  900. return "LeaveUnwindContext";
  901. }
  902. String ContinuePendingUnwind::to_string_impl(Bytecode::Executable const&) const
  903. {
  904. return String::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  905. }
  906. String PushDeclarativeEnvironment::to_string_impl(Bytecode::Executable const& executable) const
  907. {
  908. StringBuilder builder;
  909. builder.append("PushDeclarativeEnvironment"sv);
  910. if (!m_variables.is_empty()) {
  911. builder.append(" {"sv);
  912. Vector<String> names;
  913. for (auto& it : m_variables)
  914. names.append(executable.get_string(it.key));
  915. builder.append('}');
  916. builder.join(", "sv, names);
  917. }
  918. return builder.to_string();
  919. }
  920. String Yield::to_string_impl(Bytecode::Executable const&) const
  921. {
  922. if (m_continuation_label.has_value())
  923. return String::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  924. return String::formatted("Yield return");
  925. }
  926. String GetByValue::to_string_impl(Bytecode::Executable const&) const
  927. {
  928. return String::formatted("GetByValue base:{}", m_base);
  929. }
  930. String PutByValue::to_string_impl(Bytecode::Executable const&) const
  931. {
  932. auto kind = m_kind == PropertyKind::Getter
  933. ? "getter"
  934. : m_kind == PropertyKind::Setter
  935. ? "setter"
  936. : "property";
  937. return String::formatted("PutByValue kind:{} base:{}, property:{}", kind, m_base, m_property);
  938. }
  939. String DeleteByValue::to_string_impl(Bytecode::Executable const&) const
  940. {
  941. return String::formatted("DeleteByValue base:{}", m_base);
  942. }
  943. String GetIterator::to_string_impl(Executable const&) const
  944. {
  945. return "GetIterator";
  946. }
  947. String GetObjectPropertyIterator::to_string_impl(Bytecode::Executable const&) const
  948. {
  949. return "GetObjectPropertyIterator";
  950. }
  951. String IteratorNext::to_string_impl(Executable const&) const
  952. {
  953. return "IteratorNext";
  954. }
  955. String IteratorResultDone::to_string_impl(Executable const&) const
  956. {
  957. return "IteratorResultDone";
  958. }
  959. String IteratorResultValue::to_string_impl(Executable const&) const
  960. {
  961. return "IteratorResultValue";
  962. }
  963. String ResolveThisBinding::to_string_impl(Bytecode::Executable const&) const
  964. {
  965. return "ResolveThisBinding"sv;
  966. }
  967. String GetNewTarget::to_string_impl(Bytecode::Executable const&) const
  968. {
  969. return "GetNewTarget"sv;
  970. }
  971. String TypeofVariable::to_string_impl(Bytecode::Executable const& executable) const
  972. {
  973. return String::formatted("TypeofVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  974. }
  975. }