Op.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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/AST.h>
  10. #include <LibJS/Bytecode/Interpreter.h>
  11. #include <LibJS/Bytecode/Op.h>
  12. #include <LibJS/Runtime/Array.h>
  13. #include <LibJS/Runtime/BigInt.h>
  14. #include <LibJS/Runtime/GlobalObject.h>
  15. #include <LibJS/Runtime/IteratorOperations.h>
  16. #include <LibJS/Runtime/LexicalEnvironment.h>
  17. #include <LibJS/Runtime/RegExpObject.h>
  18. #include <LibJS/Runtime/ScopeObject.h>
  19. #include <LibJS/Runtime/ScriptFunction.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 Value abstract_inequals(GlobalObject& global_object, Value src1, Value src2)
  49. {
  50. return Value(!abstract_eq(global_object, src1, src2));
  51. }
  52. static Value abstract_equals(GlobalObject& global_object, Value src1, Value src2)
  53. {
  54. return Value(abstract_eq(global_object, src1, src2));
  55. }
  56. static Value typed_inequals(GlobalObject&, Value src1, Value src2)
  57. {
  58. return Value(!strict_eq(src1, src2));
  59. }
  60. static Value typed_equals(GlobalObject&, Value src1, Value src2)
  61. {
  62. return Value(strict_eq(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. interpreter.accumulator() = op_snake_case(interpreter.global_object(), lhs, rhs); \
  70. } \
  71. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  72. { \
  73. return String::formatted(#OpTitleCase " {}", m_lhs_reg); \
  74. }
  75. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  76. static Value not_(GlobalObject&, Value value)
  77. {
  78. return Value(!value.to_boolean());
  79. }
  80. static Value typeof_(GlobalObject& global_object, Value value)
  81. {
  82. return js_string(global_object.vm(), value.typeof());
  83. }
  84. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  85. void OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  86. { \
  87. interpreter.accumulator() = op_snake_case(interpreter.global_object(), interpreter.accumulator()); \
  88. } \
  89. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  90. { \
  91. return #OpTitleCase; \
  92. }
  93. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  94. void NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
  95. {
  96. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), m_bigint);
  97. }
  98. void NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  99. {
  100. Vector<Value> elements;
  101. elements.ensure_capacity(m_element_count);
  102. for (size_t i = 0; i < m_element_count; i++)
  103. elements.append(interpreter.reg(m_elements[i]));
  104. interpreter.accumulator() = Array::create_from(interpreter.global_object(), elements);
  105. }
  106. void IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const
  107. {
  108. auto& global_object = interpreter.global_object();
  109. auto& vm = interpreter.vm();
  110. auto iterator = interpreter.accumulator().to_object(global_object);
  111. if (vm.exception())
  112. return;
  113. auto array = Array::create(global_object);
  114. size_t index = 0;
  115. while (true) {
  116. auto iterator_result = iterator_next(*iterator);
  117. if (!iterator_result)
  118. return;
  119. auto complete = iterator_complete(global_object, *iterator_result);
  120. if (vm.exception())
  121. return;
  122. if (complete) {
  123. interpreter.accumulator() = array;
  124. return;
  125. }
  126. auto value = iterator_value(global_object, *iterator_result);
  127. if (vm.exception())
  128. return;
  129. array->put(index, value);
  130. index++;
  131. }
  132. }
  133. void NewString::execute_impl(Bytecode::Interpreter& interpreter) const
  134. {
  135. interpreter.accumulator() = js_string(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  136. }
  137. void NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  138. {
  139. interpreter.accumulator() = Object::create(interpreter.global_object(), interpreter.global_object().object_prototype());
  140. }
  141. void NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
  142. {
  143. auto source = interpreter.current_executable().get_string(m_source_index);
  144. auto flags = interpreter.current_executable().get_string(m_flags_index);
  145. interpreter.accumulator() = RegExpObject::create(interpreter.global_object(), source, flags);
  146. }
  147. void CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const
  148. {
  149. auto* from_object = interpreter.reg(m_from_object).to_object(interpreter.global_object());
  150. if (interpreter.vm().exception())
  151. return;
  152. auto* to_object = Object::create(interpreter.global_object(), interpreter.global_object().object_prototype());
  153. HashTable<Value, ValueTraits> excluded_names;
  154. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  155. excluded_names.set(interpreter.reg(m_excluded_names[i]));
  156. if (interpreter.vm().exception())
  157. return;
  158. }
  159. auto own_keys = from_object->get_own_properties(Object::PropertyKind::Key, true);
  160. for (auto& key : own_keys) {
  161. if (!excluded_names.contains(key)) {
  162. auto property_name = PropertyName(key.to_property_key(interpreter.global_object()));
  163. auto property_value = from_object->get(property_name);
  164. if (interpreter.vm().exception())
  165. return;
  166. to_object->define_property(property_name, property_value);
  167. }
  168. }
  169. interpreter.accumulator() = to_object;
  170. }
  171. void ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  172. {
  173. interpreter.reg(m_lhs) = add(interpreter.global_object(), interpreter.reg(m_lhs), interpreter.accumulator());
  174. }
  175. void GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  176. {
  177. interpreter.accumulator() = interpreter.vm().get_variable(interpreter.current_executable().get_string(m_identifier), interpreter.global_object());
  178. }
  179. void SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  180. {
  181. interpreter.vm().set_variable(interpreter.current_executable().get_string(m_identifier), interpreter.accumulator(), interpreter.global_object());
  182. }
  183. void GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  184. {
  185. if (auto* object = interpreter.accumulator().to_object(interpreter.global_object()))
  186. interpreter.accumulator() = object->get(interpreter.current_executable().get_string(m_property)).value_or(js_undefined());
  187. }
  188. void PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  189. {
  190. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  191. object->put(interpreter.current_executable().get_string(m_property), interpreter.accumulator());
  192. }
  193. void Jump::execute_impl(Bytecode::Interpreter& interpreter) const
  194. {
  195. interpreter.jump(*m_true_target);
  196. }
  197. void Jump::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  198. {
  199. if (m_true_target.has_value() && &m_true_target->block() == &from)
  200. m_true_target = Label { to };
  201. if (m_false_target.has_value() && &m_false_target->block() == &from)
  202. m_false_target = Label { to };
  203. }
  204. void JumpConditional::execute_impl(Bytecode::Interpreter& interpreter) const
  205. {
  206. VERIFY(m_true_target.has_value());
  207. VERIFY(m_false_target.has_value());
  208. auto result = interpreter.accumulator();
  209. if (result.to_boolean())
  210. interpreter.jump(m_true_target.value());
  211. else
  212. interpreter.jump(m_false_target.value());
  213. }
  214. void JumpNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  215. {
  216. VERIFY(m_true_target.has_value());
  217. VERIFY(m_false_target.has_value());
  218. auto result = interpreter.accumulator();
  219. if (result.is_nullish())
  220. interpreter.jump(m_true_target.value());
  221. else
  222. interpreter.jump(m_false_target.value());
  223. }
  224. void JumpUndefined::execute_impl(Bytecode::Interpreter& interpreter) const
  225. {
  226. VERIFY(m_true_target.has_value());
  227. VERIFY(m_false_target.has_value());
  228. auto result = interpreter.accumulator();
  229. if (result.is_undefined())
  230. interpreter.jump(m_true_target.value());
  231. else
  232. interpreter.jump(m_false_target.value());
  233. }
  234. void Call::execute_impl(Bytecode::Interpreter& interpreter) const
  235. {
  236. auto callee = interpreter.reg(m_callee);
  237. if (!callee.is_function()) {
  238. TODO();
  239. }
  240. auto& function = callee.as_function();
  241. auto this_value = interpreter.reg(m_this_value);
  242. Value return_value;
  243. if (m_argument_count == 0 && m_type == CallType::Call) {
  244. return_value = interpreter.vm().call(function, this_value);
  245. } else {
  246. MarkedValueList argument_values { interpreter.vm().heap() };
  247. for (size_t i = 0; i < m_argument_count; ++i) {
  248. argument_values.append(interpreter.reg(m_arguments[i]));
  249. }
  250. if (m_type == CallType::Call)
  251. return_value = interpreter.vm().call(function, this_value, move(argument_values));
  252. else
  253. return_value = interpreter.vm().construct(function, function, move(argument_values));
  254. }
  255. interpreter.accumulator() = return_value;
  256. }
  257. void NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  258. {
  259. auto& vm = interpreter.vm();
  260. interpreter.accumulator() = ScriptFunction::create(interpreter.global_object(), m_function_node.name(), m_function_node.body(), m_function_node.parameters(), m_function_node.function_length(), vm.current_scope(), m_function_node.kind(), m_function_node.is_strict_mode(), m_function_node.is_arrow_function());
  261. }
  262. void Return::execute_impl(Bytecode::Interpreter& interpreter) const
  263. {
  264. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  265. }
  266. void Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  267. {
  268. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  269. if (interpreter.vm().exception())
  270. return;
  271. if (old_value.is_number())
  272. interpreter.accumulator() = Value(old_value.as_double() + 1);
  273. else
  274. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  275. }
  276. void Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  277. {
  278. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  279. if (interpreter.vm().exception())
  280. return;
  281. if (old_value.is_number())
  282. interpreter.accumulator() = Value(old_value.as_double() - 1);
  283. else
  284. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  285. }
  286. void Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  287. {
  288. interpreter.vm().throw_exception(interpreter.global_object(), interpreter.accumulator());
  289. }
  290. void EnterUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  291. {
  292. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  293. interpreter.jump(m_entry_point);
  294. }
  295. void EnterUnwindContext::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  296. {
  297. if (&m_entry_point.block() == &from)
  298. m_entry_point = Label { to };
  299. if (m_handler_target.has_value() && &m_handler_target->block() == &from)
  300. m_handler_target = Label { to };
  301. if (m_finalizer_target.has_value() && &m_finalizer_target->block() == &from)
  302. m_finalizer_target = Label { to };
  303. }
  304. void LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  305. {
  306. interpreter.leave_unwind_context();
  307. }
  308. void ContinuePendingUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  309. {
  310. interpreter.continue_pending_unwind(m_resume_target);
  311. }
  312. void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  313. {
  314. if (&m_resume_target.block() == &from)
  315. m_resume_target = Label { to };
  316. }
  317. void PushLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  318. {
  319. HashMap<FlyString, Variable> resolved_variables;
  320. for (auto& it : m_variables)
  321. resolved_variables.set(interpreter.current_executable().get_string(it.key), it.value);
  322. auto* block_lexical_environment = interpreter.vm().heap().allocate<LexicalEnvironment>(interpreter.global_object(), move(resolved_variables), interpreter.vm().current_scope());
  323. interpreter.vm().call_frame().scope = block_lexical_environment;
  324. }
  325. void Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  326. {
  327. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  328. auto object = JS::Object::create(interpreter.global_object(), nullptr);
  329. object->put("result", yielded_value);
  330. if (m_continuation_label.has_value())
  331. object->put("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))));
  332. else
  333. object->put("continuation", Value(0));
  334. interpreter.do_return(object);
  335. }
  336. void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  337. {
  338. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  339. m_continuation_label = Label { to };
  340. }
  341. void GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  342. {
  343. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object())) {
  344. auto property_key = interpreter.accumulator().to_property_key(interpreter.global_object());
  345. if (interpreter.vm().exception())
  346. return;
  347. interpreter.accumulator() = object->get(property_key).value_or(js_undefined());
  348. }
  349. }
  350. void PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  351. {
  352. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object())) {
  353. auto property_key = interpreter.reg(m_property).to_property_key(interpreter.global_object());
  354. if (interpreter.vm().exception())
  355. return;
  356. object->put(property_key, interpreter.accumulator());
  357. }
  358. }
  359. void LoadArgument::execute_impl(Bytecode::Interpreter& interpreter) const
  360. {
  361. interpreter.accumulator() = interpreter.vm().argument(m_index);
  362. }
  363. void GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  364. {
  365. interpreter.accumulator() = get_iterator(interpreter.global_object(), interpreter.accumulator());
  366. }
  367. void IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  368. {
  369. if (auto* object = interpreter.accumulator().to_object(interpreter.global_object()))
  370. interpreter.accumulator() = iterator_next(*object);
  371. }
  372. void IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const
  373. {
  374. if (auto* iterator_result = interpreter.accumulator().to_object(interpreter.global_object()))
  375. interpreter.accumulator() = Value(iterator_complete(interpreter.global_object(), *iterator_result));
  376. }
  377. void IteratorResultValue::execute_impl(Bytecode::Interpreter& interpreter) const
  378. {
  379. if (auto* iterator_result = interpreter.accumulator().to_object(interpreter.global_object()))
  380. interpreter.accumulator() = iterator_value(interpreter.global_object(), *iterator_result);
  381. }
  382. String Load::to_string_impl(Bytecode::Executable const&) const
  383. {
  384. return String::formatted("Load {}", m_src);
  385. }
  386. String LoadImmediate::to_string_impl(Bytecode::Executable const&) const
  387. {
  388. return String::formatted("LoadImmediate {}", m_value);
  389. }
  390. String Store::to_string_impl(Bytecode::Executable const&) const
  391. {
  392. return String::formatted("Store {}", m_dst);
  393. }
  394. String NewBigInt::to_string_impl(Bytecode::Executable const&) const
  395. {
  396. return String::formatted("NewBigInt \"{}\"", m_bigint.to_base10());
  397. }
  398. String NewArray::to_string_impl(Bytecode::Executable const&) const
  399. {
  400. StringBuilder builder;
  401. builder.append("NewArray");
  402. if (m_element_count != 0) {
  403. builder.append(" [");
  404. for (size_t i = 0; i < m_element_count; ++i) {
  405. builder.appendff("{}", m_elements[i]);
  406. if (i != m_element_count - 1)
  407. builder.append(',');
  408. }
  409. builder.append(']');
  410. }
  411. return builder.to_string();
  412. }
  413. String IteratorToArray::to_string_impl(const Bytecode::Executable&) const
  414. {
  415. return "IteratorToArray";
  416. }
  417. String NewString::to_string_impl(Bytecode::Executable const& executable) const
  418. {
  419. return String::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  420. }
  421. String NewObject::to_string_impl(Bytecode::Executable const&) const
  422. {
  423. return "NewObject";
  424. }
  425. String NewRegExp::to_string_impl(Bytecode::Executable const& executable) const
  426. {
  427. return String::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  428. }
  429. String CopyObjectExcludingProperties::to_string_impl(const Bytecode::Executable&) const
  430. {
  431. StringBuilder builder;
  432. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  433. if (m_excluded_names_count != 0) {
  434. builder.append(" excluding:[");
  435. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  436. builder.appendff("{}", m_excluded_names[i]);
  437. if (i != m_excluded_names_count - 1)
  438. builder.append(',');
  439. }
  440. builder.append(']');
  441. }
  442. return builder.to_string();
  443. }
  444. String ConcatString::to_string_impl(Bytecode::Executable const&) const
  445. {
  446. return String::formatted("ConcatString {}", m_lhs);
  447. }
  448. String GetVariable::to_string_impl(Bytecode::Executable const& executable) const
  449. {
  450. return String::formatted("GetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  451. }
  452. String SetVariable::to_string_impl(Bytecode::Executable const& executable) const
  453. {
  454. return String::formatted("SetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  455. }
  456. String PutById::to_string_impl(Bytecode::Executable const& executable) const
  457. {
  458. return String::formatted("PutById base:{}, property:{} ({})", m_base, m_property, executable.string_table->get(m_property));
  459. }
  460. String GetById::to_string_impl(Bytecode::Executable const& executable) const
  461. {
  462. return String::formatted("GetById {} ({})", m_property, executable.string_table->get(m_property));
  463. }
  464. String Jump::to_string_impl(Bytecode::Executable const&) const
  465. {
  466. if (m_true_target.has_value())
  467. return String::formatted("Jump {}", *m_true_target);
  468. return String::formatted("Jump <empty>");
  469. }
  470. String JumpConditional::to_string_impl(Bytecode::Executable const&) const
  471. {
  472. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  473. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  474. return String::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  475. }
  476. String JumpNullish::to_string_impl(Bytecode::Executable const&) const
  477. {
  478. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  479. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  480. return String::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  481. }
  482. String JumpUndefined::to_string_impl(Bytecode::Executable const&) const
  483. {
  484. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  485. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  486. return String::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  487. }
  488. String Call::to_string_impl(Bytecode::Executable const&) const
  489. {
  490. StringBuilder builder;
  491. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  492. if (m_argument_count != 0) {
  493. builder.append(", arguments:[");
  494. for (size_t i = 0; i < m_argument_count; ++i) {
  495. builder.appendff("{}", m_arguments[i]);
  496. if (i != m_argument_count - 1)
  497. builder.append(',');
  498. }
  499. builder.append(']');
  500. }
  501. return builder.to_string();
  502. }
  503. String NewFunction::to_string_impl(Bytecode::Executable const&) const
  504. {
  505. return "NewFunction";
  506. }
  507. String Return::to_string_impl(Bytecode::Executable const&) const
  508. {
  509. return "Return";
  510. }
  511. String Increment::to_string_impl(Bytecode::Executable const&) const
  512. {
  513. return "Increment";
  514. }
  515. String Decrement::to_string_impl(Bytecode::Executable const&) const
  516. {
  517. return "Decrement";
  518. }
  519. String Throw::to_string_impl(Bytecode::Executable const&) const
  520. {
  521. return "Throw";
  522. }
  523. String EnterUnwindContext::to_string_impl(Bytecode::Executable const&) const
  524. {
  525. auto handler_string = m_handler_target.has_value() ? String::formatted("{}", *m_handler_target) : "<empty>";
  526. auto finalizer_string = m_finalizer_target.has_value() ? String::formatted("{}", *m_finalizer_target) : "<empty>";
  527. return String::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  528. }
  529. String LeaveUnwindContext::to_string_impl(Bytecode::Executable const&) const
  530. {
  531. return "LeaveUnwindContext";
  532. }
  533. String ContinuePendingUnwind::to_string_impl(Bytecode::Executable const&) const
  534. {
  535. return String::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  536. }
  537. String PushLexicalEnvironment::to_string_impl(const Bytecode::Executable& executable) const
  538. {
  539. StringBuilder builder;
  540. builder.append("PushLexicalEnvironment");
  541. if (!m_variables.is_empty()) {
  542. builder.append(" {");
  543. Vector<String> names;
  544. for (auto& it : m_variables)
  545. names.append(executable.get_string(it.key));
  546. builder.join(", ", names);
  547. builder.append("}");
  548. }
  549. return builder.to_string();
  550. }
  551. String Yield::to_string_impl(Bytecode::Executable const&) const
  552. {
  553. if (m_continuation_label.has_value())
  554. return String::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  555. return String::formatted("Yield return");
  556. }
  557. String GetByValue::to_string_impl(const Bytecode::Executable&) const
  558. {
  559. return String::formatted("GetByValue base:{}", m_base);
  560. }
  561. String PutByValue::to_string_impl(const Bytecode::Executable&) const
  562. {
  563. return String::formatted("PutByValue base:{}, property:{}", m_base, m_property);
  564. }
  565. String LoadArgument::to_string_impl(const Bytecode::Executable&) const
  566. {
  567. return String::formatted("LoadArgument {}", m_index);
  568. }
  569. String GetIterator::to_string_impl(Executable const&) const
  570. {
  571. return "GetIterator";
  572. }
  573. String IteratorNext::to_string_impl(Executable const&) const
  574. {
  575. return "IteratorNext";
  576. }
  577. String IteratorResultDone::to_string_impl(Executable const&) const
  578. {
  579. return "IteratorResultDone";
  580. }
  581. String IteratorResultValue::to_string_impl(Executable const&) const
  582. {
  583. return "IteratorResultValue";
  584. }
  585. }