Op.cpp 46 KB

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