Op.cpp 31 KB

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