Op.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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/RegExpObject.h>
  21. #include <LibJS/Runtime/Value.h>
  22. namespace JS::Bytecode {
  23. String Instruction::to_string(Bytecode::Executable const& executable) const
  24. {
  25. #define __BYTECODE_OP(op) \
  26. case Instruction::Type::op: \
  27. return static_cast<Bytecode::Op::op const&>(*this).to_string_impl(executable);
  28. switch (type()) {
  29. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  30. default:
  31. VERIFY_NOT_REACHED();
  32. }
  33. #undef __BYTECODE_OP
  34. }
  35. }
  36. namespace JS::Bytecode::Op {
  37. ThrowCompletionOr<void> Load::execute_impl(Bytecode::Interpreter& interpreter) const
  38. {
  39. interpreter.accumulator() = interpreter.reg(m_src);
  40. return {};
  41. }
  42. ThrowCompletionOr<void> LoadImmediate::execute_impl(Bytecode::Interpreter& interpreter) const
  43. {
  44. interpreter.accumulator() = m_value;
  45. return {};
  46. }
  47. ThrowCompletionOr<void> Store::execute_impl(Bytecode::Interpreter& interpreter) const
  48. {
  49. interpreter.reg(m_dst) = interpreter.accumulator();
  50. return {};
  51. }
  52. static ThrowCompletionOr<Value> abstract_inequals(GlobalObject& global_object, Value src1, Value src2)
  53. {
  54. return Value(!TRY(is_loosely_equal(global_object, src1, src2)));
  55. }
  56. static ThrowCompletionOr<Value> abstract_equals(GlobalObject& global_object, Value src1, Value src2)
  57. {
  58. return Value(TRY(is_loosely_equal(global_object, src1, src2)));
  59. }
  60. static ThrowCompletionOr<Value> typed_inequals(GlobalObject&, Value src1, Value src2)
  61. {
  62. return Value(!is_strictly_equal(src1, src2));
  63. }
  64. static ThrowCompletionOr<Value> typed_equals(GlobalObject&, Value src1, Value src2)
  65. {
  66. return Value(is_strictly_equal(src1, src2));
  67. }
  68. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  69. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  70. { \
  71. auto lhs = interpreter.reg(m_lhs_reg); \
  72. auto rhs = interpreter.accumulator(); \
  73. interpreter.accumulator() = TRY(op_snake_case(interpreter.global_object(), lhs, rhs)); \
  74. return {}; \
  75. } \
  76. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  77. { \
  78. return String::formatted(#OpTitleCase " {}", m_lhs_reg); \
  79. }
  80. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  81. static ThrowCompletionOr<Value> not_(GlobalObject&, Value value)
  82. {
  83. return Value(!value.to_boolean());
  84. }
  85. static ThrowCompletionOr<Value> typeof_(GlobalObject& global_object, Value value)
  86. {
  87. return Value(js_string(global_object.vm(), value.typeof()));
  88. }
  89. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  90. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  91. { \
  92. interpreter.accumulator() = TRY(op_snake_case(interpreter.global_object(), interpreter.accumulator())); \
  93. return {}; \
  94. } \
  95. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  96. { \
  97. return #OpTitleCase; \
  98. }
  99. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  100. ThrowCompletionOr<void> NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
  101. {
  102. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), m_bigint);
  103. return {};
  104. }
  105. ThrowCompletionOr<void> NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  106. {
  107. Vector<Value> elements;
  108. elements.ensure_capacity(m_element_count);
  109. for (size_t i = 0; i < m_element_count; i++)
  110. elements.append(interpreter.reg(m_elements[i]));
  111. interpreter.accumulator() = Array::create_from(interpreter.global_object(), elements);
  112. return {};
  113. }
  114. // 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.
  115. // Make sure to put this into the accumulator before the iterator object disappears from the stack to prevent the members from being GC'd.
  116. static Object* iterator_to_object(GlobalObject& global_object, Iterator iterator)
  117. {
  118. auto& vm = global_object.vm();
  119. auto* object = Object::create(global_object, nullptr);
  120. object->define_direct_property(vm.names.iterator, iterator.iterator, 0);
  121. object->define_direct_property(vm.names.next, iterator.next_method, 0);
  122. object->define_direct_property(vm.names.done, Value(iterator.done), 0);
  123. return object;
  124. }
  125. static Iterator object_to_iterator(GlobalObject& global_object, Object& object)
  126. {
  127. auto& vm = global_object.vm();
  128. return Iterator {
  129. .iterator = &MUST(object.get(vm.names.iterator)).as_object(),
  130. .next_method = MUST(object.get(vm.names.next)),
  131. .done = MUST(object.get(vm.names.done)).as_bool()
  132. };
  133. }
  134. ThrowCompletionOr<void> IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const
  135. {
  136. auto& global_object = interpreter.global_object();
  137. auto iterator_object = TRY(interpreter.accumulator().to_object(global_object));
  138. auto iterator = object_to_iterator(global_object, *iterator_object);
  139. auto* array = MUST(Array::create(global_object, 0));
  140. size_t index = 0;
  141. while (true) {
  142. auto* iterator_result = TRY(iterator_next(global_object, iterator));
  143. auto complete = TRY(iterator_complete(global_object, *iterator_result));
  144. if (complete) {
  145. interpreter.accumulator() = array;
  146. return {};
  147. }
  148. auto value = TRY(iterator_value(global_object, *iterator_result));
  149. MUST(array->create_data_property_or_throw(index, value));
  150. index++;
  151. }
  152. return {};
  153. }
  154. ThrowCompletionOr<void> NewString::execute_impl(Bytecode::Interpreter& interpreter) const
  155. {
  156. interpreter.accumulator() = js_string(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  157. return {};
  158. }
  159. ThrowCompletionOr<void> NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  160. {
  161. interpreter.accumulator() = Object::create(interpreter.global_object(), interpreter.global_object().object_prototype());
  162. return {};
  163. }
  164. ThrowCompletionOr<void> NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
  165. {
  166. auto source = interpreter.current_executable().get_string(m_source_index);
  167. auto flags = interpreter.current_executable().get_string(m_flags_index);
  168. interpreter.accumulator() = TRY(regexp_create(interpreter.global_object(), js_string(interpreter.vm(), source), js_string(interpreter.vm(), flags)));
  169. return {};
  170. }
  171. ThrowCompletionOr<void> CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const
  172. {
  173. auto* from_object = TRY(interpreter.reg(m_from_object).to_object(interpreter.global_object()));
  174. auto* to_object = Object::create(interpreter.global_object(), interpreter.global_object().object_prototype());
  175. HashTable<Value, ValueTraits> excluded_names;
  176. for (size_t i = 0; i < m_excluded_names_count; ++i)
  177. excluded_names.set(interpreter.reg(m_excluded_names[i]));
  178. auto own_keys = TRY(from_object->internal_own_property_keys());
  179. for (auto& key : own_keys) {
  180. if (!excluded_names.contains(key)) {
  181. auto property_key = TRY(key.to_property_key(interpreter.global_object()));
  182. auto property_value = TRY(from_object->get(property_key));
  183. to_object->define_direct_property(property_key, property_value, JS::default_attributes);
  184. }
  185. }
  186. interpreter.accumulator() = to_object;
  187. return {};
  188. }
  189. ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  190. {
  191. interpreter.reg(m_lhs) = TRY(add(interpreter.global_object(), interpreter.reg(m_lhs), interpreter.accumulator()));
  192. return {};
  193. }
  194. ThrowCompletionOr<void> GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  195. {
  196. auto get_reference = [&]() -> ThrowCompletionOr<Reference> {
  197. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  198. if (m_cached_environment_coordinate.has_value()) {
  199. auto* environment = interpreter.vm().running_execution_context().lexical_environment;
  200. for (size_t i = 0; i < m_cached_environment_coordinate->hops; ++i)
  201. environment = environment->outer_environment();
  202. VERIFY(environment);
  203. VERIFY(environment->is_declarative_environment());
  204. if (!environment->is_permanently_screwed_by_eval()) {
  205. return Reference { *environment, string, interpreter.vm().in_strict_mode(), m_cached_environment_coordinate };
  206. }
  207. m_cached_environment_coordinate = {};
  208. }
  209. auto reference = TRY(interpreter.vm().resolve_binding(string));
  210. if (reference.environment_coordinate().has_value())
  211. m_cached_environment_coordinate = reference.environment_coordinate();
  212. return reference;
  213. };
  214. auto reference = TRY(get_reference());
  215. interpreter.accumulator() = TRY(reference.get_value(interpreter.global_object()));
  216. return {};
  217. }
  218. ThrowCompletionOr<void> SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  219. {
  220. auto& vm = interpreter.vm();
  221. auto reference = TRY(vm.resolve_binding(interpreter.current_executable().get_identifier(m_identifier)));
  222. TRY(reference.put_value(interpreter.global_object(), interpreter.accumulator()));
  223. return {};
  224. }
  225. ThrowCompletionOr<void> GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  226. {
  227. auto* object = TRY(interpreter.accumulator().to_object(interpreter.global_object()));
  228. interpreter.accumulator() = TRY(object->get(interpreter.current_executable().get_identifier(m_property)));
  229. return {};
  230. }
  231. ThrowCompletionOr<void> PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  232. {
  233. auto* object = TRY(interpreter.reg(m_base).to_object(interpreter.global_object()));
  234. TRY(object->set(interpreter.current_executable().get_identifier(m_property), interpreter.accumulator(), Object::ShouldThrowExceptions::Yes));
  235. return {};
  236. }
  237. ThrowCompletionOr<void> Jump::execute_impl(Bytecode::Interpreter& interpreter) const
  238. {
  239. interpreter.jump(*m_true_target);
  240. return {};
  241. }
  242. ThrowCompletionOr<void> ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const
  243. {
  244. interpreter.accumulator() = TRY(interpreter.vm().resolve_this_binding(interpreter.global_object()));
  245. return {};
  246. }
  247. void Jump::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  248. {
  249. if (m_true_target.has_value() && &m_true_target->block() == &from)
  250. m_true_target = Label { to };
  251. if (m_false_target.has_value() && &m_false_target->block() == &from)
  252. m_false_target = Label { to };
  253. }
  254. ThrowCompletionOr<void> JumpConditional::execute_impl(Bytecode::Interpreter& interpreter) const
  255. {
  256. VERIFY(m_true_target.has_value());
  257. VERIFY(m_false_target.has_value());
  258. auto result = interpreter.accumulator();
  259. if (result.to_boolean())
  260. interpreter.jump(m_true_target.value());
  261. else
  262. interpreter.jump(m_false_target.value());
  263. return {};
  264. }
  265. ThrowCompletionOr<void> JumpNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  266. {
  267. VERIFY(m_true_target.has_value());
  268. VERIFY(m_false_target.has_value());
  269. auto result = interpreter.accumulator();
  270. if (result.is_nullish())
  271. interpreter.jump(m_true_target.value());
  272. else
  273. interpreter.jump(m_false_target.value());
  274. return {};
  275. }
  276. ThrowCompletionOr<void> JumpUndefined::execute_impl(Bytecode::Interpreter& interpreter) const
  277. {
  278. VERIFY(m_true_target.has_value());
  279. VERIFY(m_false_target.has_value());
  280. auto result = interpreter.accumulator();
  281. if (result.is_undefined())
  282. interpreter.jump(m_true_target.value());
  283. else
  284. interpreter.jump(m_false_target.value());
  285. return {};
  286. }
  287. ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) const
  288. {
  289. auto callee = interpreter.reg(m_callee);
  290. if (!callee.is_function())
  291. return interpreter.vm().throw_completion<TypeError>(interpreter.global_object(), ErrorType::IsNotA, callee.to_string_without_side_effects(), "function"sv);
  292. auto& function = callee.as_function();
  293. auto this_value = interpreter.reg(m_this_value);
  294. Value return_value;
  295. if (m_argument_count == 0 && m_type == CallType::Call) {
  296. auto return_value_or_error = call(interpreter.global_object(), function, this_value);
  297. if (!return_value_or_error.is_error())
  298. return_value = return_value_or_error.release_value();
  299. } else {
  300. MarkedVector<Value> argument_values { interpreter.vm().heap() };
  301. for (size_t i = 0; i < m_argument_count; ++i)
  302. argument_values.append(interpreter.reg(m_arguments[i]));
  303. if (m_type == CallType::Call)
  304. return_value = TRY(call(interpreter.global_object(), function, this_value, move(argument_values)));
  305. else
  306. return_value = TRY(construct(interpreter.global_object(), function, move(argument_values)));
  307. }
  308. interpreter.accumulator() = return_value;
  309. return {};
  310. }
  311. ThrowCompletionOr<void> NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  312. {
  313. auto& vm = interpreter.vm();
  314. 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.is_arrow_function());
  315. return {};
  316. }
  317. ThrowCompletionOr<void> Return::execute_impl(Bytecode::Interpreter& interpreter) const
  318. {
  319. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  320. return {};
  321. }
  322. ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  323. {
  324. auto old_value = TRY(interpreter.accumulator().to_numeric(interpreter.global_object()));
  325. if (old_value.is_number())
  326. interpreter.accumulator() = Value(old_value.as_double() + 1);
  327. else
  328. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  329. return {};
  330. }
  331. ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  332. {
  333. auto old_value = TRY(interpreter.accumulator().to_numeric(interpreter.global_object()));
  334. if (old_value.is_number())
  335. interpreter.accumulator() = Value(old_value.as_double() - 1);
  336. else
  337. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  338. return {};
  339. }
  340. ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  341. {
  342. return throw_completion(interpreter.accumulator());
  343. }
  344. ThrowCompletionOr<void> EnterUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  345. {
  346. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  347. interpreter.jump(m_entry_point);
  348. return {};
  349. }
  350. void EnterUnwindContext::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  351. {
  352. if (&m_entry_point.block() == &from)
  353. m_entry_point = Label { to };
  354. if (m_handler_target.has_value() && &m_handler_target->block() == &from)
  355. m_handler_target = Label { to };
  356. if (m_finalizer_target.has_value() && &m_finalizer_target->block() == &from)
  357. m_finalizer_target = Label { to };
  358. }
  359. ThrowCompletionOr<void> FinishUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  360. {
  361. interpreter.leave_unwind_context();
  362. interpreter.jump(m_next_target);
  363. return {};
  364. }
  365. void FinishUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  366. {
  367. if (&m_next_target.block() == &from)
  368. m_next_target = Label { to };
  369. }
  370. ThrowCompletionOr<void> LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  371. {
  372. interpreter.leave_unwind_context();
  373. return {};
  374. }
  375. ThrowCompletionOr<void> ContinuePendingUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  376. {
  377. return interpreter.continue_pending_unwind(m_resume_target);
  378. }
  379. void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  380. {
  381. if (&m_resume_target.block() == &from)
  382. m_resume_target = Label { to };
  383. }
  384. ThrowCompletionOr<void> PushDeclarativeEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  385. {
  386. auto* environment = interpreter.vm().heap().allocate_without_global_object<DeclarativeEnvironment>(interpreter.vm().lexical_environment());
  387. interpreter.vm().running_execution_context().lexical_environment = environment;
  388. interpreter.vm().running_execution_context().variable_environment = environment;
  389. return {};
  390. }
  391. ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  392. {
  393. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  394. auto object = JS::Object::create(interpreter.global_object(), nullptr);
  395. object->define_direct_property("result", yielded_value, JS::default_attributes);
  396. if (m_continuation_label.has_value())
  397. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes);
  398. else
  399. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  400. interpreter.do_return(object);
  401. return {};
  402. }
  403. void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  404. {
  405. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  406. m_continuation_label = Label { to };
  407. }
  408. ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  409. {
  410. auto* object = TRY(interpreter.reg(m_base).to_object(interpreter.global_object()));
  411. auto property_key = TRY(interpreter.accumulator().to_property_key(interpreter.global_object()));
  412. interpreter.accumulator() = TRY(object->get(property_key));
  413. return {};
  414. }
  415. ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  416. {
  417. auto* object = TRY(interpreter.reg(m_base).to_object(interpreter.global_object()));
  418. auto property_key = TRY(interpreter.reg(m_property).to_property_key(interpreter.global_object()));
  419. TRY(object->set(property_key, interpreter.accumulator(), Object::ShouldThrowExceptions::Yes));
  420. return {};
  421. }
  422. ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  423. {
  424. auto iterator = TRY(get_iterator(interpreter.global_object(), interpreter.accumulator()));
  425. interpreter.accumulator() = iterator_to_object(interpreter.global_object(), iterator);
  426. return {};
  427. }
  428. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  429. {
  430. auto* iterator_object = TRY(interpreter.accumulator().to_object(interpreter.global_object()));
  431. auto iterator = object_to_iterator(interpreter.global_object(), *iterator_object);
  432. interpreter.accumulator() = TRY(iterator_next(interpreter.global_object(), iterator));
  433. return {};
  434. }
  435. ThrowCompletionOr<void> IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const
  436. {
  437. auto* iterator_result = TRY(interpreter.accumulator().to_object(interpreter.global_object()));
  438. auto complete = TRY(iterator_complete(interpreter.global_object(), *iterator_result));
  439. interpreter.accumulator() = Value(complete);
  440. return {};
  441. }
  442. ThrowCompletionOr<void> IteratorResultValue::execute_impl(Bytecode::Interpreter& interpreter) const
  443. {
  444. auto* iterator_result = TRY(interpreter.accumulator().to_object(interpreter.global_object()));
  445. interpreter.accumulator() = TRY(iterator_value(interpreter.global_object(), *iterator_result));
  446. return {};
  447. }
  448. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter&) const
  449. {
  450. (void)m_class_expression;
  451. TODO();
  452. }
  453. String Load::to_string_impl(Bytecode::Executable const&) const
  454. {
  455. return String::formatted("Load {}", m_src);
  456. }
  457. String LoadImmediate::to_string_impl(Bytecode::Executable const&) const
  458. {
  459. return String::formatted("LoadImmediate {}", m_value);
  460. }
  461. String Store::to_string_impl(Bytecode::Executable const&) const
  462. {
  463. return String::formatted("Store {}", m_dst);
  464. }
  465. String NewBigInt::to_string_impl(Bytecode::Executable const&) const
  466. {
  467. return String::formatted("NewBigInt \"{}\"", m_bigint.to_base(10));
  468. }
  469. String NewArray::to_string_impl(Bytecode::Executable const&) const
  470. {
  471. StringBuilder builder;
  472. builder.append("NewArray");
  473. if (m_element_count != 0) {
  474. builder.append(" [");
  475. for (size_t i = 0; i < m_element_count; ++i) {
  476. builder.appendff("{}", m_elements[i]);
  477. if (i != m_element_count - 1)
  478. builder.append(',');
  479. }
  480. builder.append(']');
  481. }
  482. return builder.to_string();
  483. }
  484. String IteratorToArray::to_string_impl(const Bytecode::Executable&) const
  485. {
  486. return "IteratorToArray";
  487. }
  488. String NewString::to_string_impl(Bytecode::Executable const& executable) const
  489. {
  490. return String::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  491. }
  492. String NewObject::to_string_impl(Bytecode::Executable const&) const
  493. {
  494. return "NewObject";
  495. }
  496. String NewRegExp::to_string_impl(Bytecode::Executable const& executable) const
  497. {
  498. return String::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  499. }
  500. String CopyObjectExcludingProperties::to_string_impl(const Bytecode::Executable&) const
  501. {
  502. StringBuilder builder;
  503. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  504. if (m_excluded_names_count != 0) {
  505. builder.append(" excluding:[");
  506. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  507. builder.appendff("{}", m_excluded_names[i]);
  508. if (i != m_excluded_names_count - 1)
  509. builder.append(',');
  510. }
  511. builder.append(']');
  512. }
  513. return builder.to_string();
  514. }
  515. String ConcatString::to_string_impl(Bytecode::Executable const&) const
  516. {
  517. return String::formatted("ConcatString {}", m_lhs);
  518. }
  519. String GetVariable::to_string_impl(Bytecode::Executable const& executable) const
  520. {
  521. return String::formatted("GetVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  522. }
  523. String SetVariable::to_string_impl(Bytecode::Executable const& executable) const
  524. {
  525. return String::formatted("SetVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  526. }
  527. String PutById::to_string_impl(Bytecode::Executable const& executable) const
  528. {
  529. return String::formatted("PutById base:{}, property:{} ({})", m_base, m_property, executable.identifier_table->get(m_property));
  530. }
  531. String GetById::to_string_impl(Bytecode::Executable const& executable) const
  532. {
  533. return String::formatted("GetById {} ({})", m_property, executable.identifier_table->get(m_property));
  534. }
  535. String Jump::to_string_impl(Bytecode::Executable const&) const
  536. {
  537. if (m_true_target.has_value())
  538. return String::formatted("Jump {}", *m_true_target);
  539. return String::formatted("Jump <empty>");
  540. }
  541. String JumpConditional::to_string_impl(Bytecode::Executable const&) const
  542. {
  543. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  544. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  545. return String::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  546. }
  547. String JumpNullish::to_string_impl(Bytecode::Executable const&) const
  548. {
  549. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  550. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  551. return String::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  552. }
  553. String JumpUndefined::to_string_impl(Bytecode::Executable const&) const
  554. {
  555. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  556. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  557. return String::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  558. }
  559. String Call::to_string_impl(Bytecode::Executable const&) const
  560. {
  561. StringBuilder builder;
  562. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  563. if (m_argument_count != 0) {
  564. builder.append(", arguments:[");
  565. for (size_t i = 0; i < m_argument_count; ++i) {
  566. builder.appendff("{}", m_arguments[i]);
  567. if (i != m_argument_count - 1)
  568. builder.append(',');
  569. }
  570. builder.append(']');
  571. }
  572. return builder.to_string();
  573. }
  574. String NewFunction::to_string_impl(Bytecode::Executable const&) const
  575. {
  576. return "NewFunction";
  577. }
  578. String NewClass::to_string_impl(Bytecode::Executable const&) const
  579. {
  580. return "NewClass";
  581. }
  582. String Return::to_string_impl(Bytecode::Executable const&) const
  583. {
  584. return "Return";
  585. }
  586. String Increment::to_string_impl(Bytecode::Executable const&) const
  587. {
  588. return "Increment";
  589. }
  590. String Decrement::to_string_impl(Bytecode::Executable const&) const
  591. {
  592. return "Decrement";
  593. }
  594. String Throw::to_string_impl(Bytecode::Executable const&) const
  595. {
  596. return "Throw";
  597. }
  598. String EnterUnwindContext::to_string_impl(Bytecode::Executable const&) const
  599. {
  600. auto handler_string = m_handler_target.has_value() ? String::formatted("{}", *m_handler_target) : "<empty>";
  601. auto finalizer_string = m_finalizer_target.has_value() ? String::formatted("{}", *m_finalizer_target) : "<empty>";
  602. return String::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  603. }
  604. String FinishUnwind::to_string_impl(const Bytecode::Executable&) const
  605. {
  606. return String::formatted("FinishUnwind next:{}", m_next_target);
  607. }
  608. String LeaveUnwindContext::to_string_impl(Bytecode::Executable const&) const
  609. {
  610. return "LeaveUnwindContext";
  611. }
  612. String ContinuePendingUnwind::to_string_impl(Bytecode::Executable const&) const
  613. {
  614. return String::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  615. }
  616. String PushDeclarativeEnvironment::to_string_impl(const Bytecode::Executable& executable) const
  617. {
  618. StringBuilder builder;
  619. builder.append("PushDeclarativeEnvironment");
  620. if (!m_variables.is_empty()) {
  621. builder.append(" {");
  622. Vector<String> names;
  623. for (auto& it : m_variables)
  624. names.append(executable.get_string(it.key));
  625. builder.join(", ", names);
  626. builder.append("}");
  627. }
  628. return builder.to_string();
  629. }
  630. String Yield::to_string_impl(Bytecode::Executable const&) const
  631. {
  632. if (m_continuation_label.has_value())
  633. return String::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  634. return String::formatted("Yield return");
  635. }
  636. String GetByValue::to_string_impl(const Bytecode::Executable&) const
  637. {
  638. return String::formatted("GetByValue base:{}", m_base);
  639. }
  640. String PutByValue::to_string_impl(const Bytecode::Executable&) const
  641. {
  642. return String::formatted("PutByValue base:{}, property:{}", m_base, m_property);
  643. }
  644. String GetIterator::to_string_impl(Executable const&) const
  645. {
  646. return "GetIterator";
  647. }
  648. String IteratorNext::to_string_impl(Executable const&) const
  649. {
  650. return "IteratorNext";
  651. }
  652. String IteratorResultDone::to_string_impl(Executable const&) const
  653. {
  654. return "IteratorResultDone";
  655. }
  656. String IteratorResultValue::to_string_impl(Executable const&) const
  657. {
  658. return "IteratorResultValue";
  659. }
  660. String ResolveThisBinding::to_string_impl(Bytecode::Executable const&) const
  661. {
  662. return "ResolveThisBinding"sv;
  663. }
  664. }