Op.cpp 46 KB

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