Op.cpp 32 KB

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