Op.cpp 24 KB

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