Op.cpp 45 KB

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