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