Op.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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/ECMAScriptFunctionObject.h>
  15. #include <LibJS/Runtime/Environment.h>
  16. #include <LibJS/Runtime/GlobalObject.h>
  17. #include <LibJS/Runtime/IteratorOperations.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(!is_loosely_equal(global_object, src1, src2));
  50. }
  51. static Value abstract_equals(GlobalObject& global_object, Value src1, Value src2)
  52. {
  53. return Value(is_loosely_equal(global_object, src1, src2));
  54. }
  55. static Value typed_inequals(GlobalObject&, Value src1, Value src2)
  56. {
  57. return Value(!is_strictly_equal(src1, src2));
  58. }
  59. static Value typed_equals(GlobalObject&, Value src1, Value src2)
  60. {
  61. return Value(is_strictly_equal(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_or_error = from_object->internal_own_property_keys();
  159. if (own_keys_or_error.is_error())
  160. return;
  161. auto own_keys = own_keys_or_error.release_value();
  162. for (auto& key : own_keys) {
  163. if (!excluded_names.contains(key)) {
  164. auto property_name = PropertyName(key.to_property_key(interpreter.global_object()));
  165. auto property_value_or_error = from_object->get(property_name);
  166. if (property_value_or_error.is_error())
  167. return;
  168. auto property_value = property_value_or_error.release_value();
  169. to_object->define_direct_property(property_name, property_value, JS::default_attributes);
  170. }
  171. }
  172. interpreter.accumulator() = to_object;
  173. }
  174. void ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  175. {
  176. interpreter.reg(m_lhs) = add(interpreter.global_object(), interpreter.reg(m_lhs), interpreter.accumulator());
  177. }
  178. void GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  179. {
  180. auto& vm = interpreter.vm();
  181. auto reference = vm.resolve_binding(interpreter.current_executable().get_string(m_identifier));
  182. if (vm.exception())
  183. return;
  184. interpreter.accumulator() = reference.get_value(interpreter.global_object());
  185. }
  186. void SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  187. {
  188. auto& vm = interpreter.vm();
  189. auto reference = vm.resolve_binding(interpreter.current_executable().get_string(m_identifier));
  190. if (vm.exception())
  191. return;
  192. reference.put_value(interpreter.global_object(), interpreter.accumulator());
  193. }
  194. void GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  195. {
  196. if (auto* object = interpreter.accumulator().to_object(interpreter.global_object())) {
  197. auto value_or_error = object->get(interpreter.current_executable().get_string(m_property));
  198. if (value_or_error.is_error())
  199. return;
  200. interpreter.accumulator() = value_or_error.release_value();
  201. }
  202. }
  203. void PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  204. {
  205. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  206. object->set(interpreter.current_executable().get_string(m_property), interpreter.accumulator(), Object::ShouldThrowExceptions::Yes);
  207. }
  208. void Jump::execute_impl(Bytecode::Interpreter& interpreter) const
  209. {
  210. interpreter.jump(*m_true_target);
  211. }
  212. void Jump::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  213. {
  214. if (m_true_target.has_value() && &m_true_target->block() == &from)
  215. m_true_target = Label { to };
  216. if (m_false_target.has_value() && &m_false_target->block() == &from)
  217. m_false_target = Label { to };
  218. }
  219. void JumpConditional::execute_impl(Bytecode::Interpreter& interpreter) const
  220. {
  221. VERIFY(m_true_target.has_value());
  222. VERIFY(m_false_target.has_value());
  223. auto result = interpreter.accumulator();
  224. if (result.to_boolean())
  225. interpreter.jump(m_true_target.value());
  226. else
  227. interpreter.jump(m_false_target.value());
  228. }
  229. void JumpNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  230. {
  231. VERIFY(m_true_target.has_value());
  232. VERIFY(m_false_target.has_value());
  233. auto result = interpreter.accumulator();
  234. if (result.is_nullish())
  235. interpreter.jump(m_true_target.value());
  236. else
  237. interpreter.jump(m_false_target.value());
  238. }
  239. void JumpUndefined::execute_impl(Bytecode::Interpreter& interpreter) const
  240. {
  241. VERIFY(m_true_target.has_value());
  242. VERIFY(m_false_target.has_value());
  243. auto result = interpreter.accumulator();
  244. if (result.is_undefined())
  245. interpreter.jump(m_true_target.value());
  246. else
  247. interpreter.jump(m_false_target.value());
  248. }
  249. void Call::execute_impl(Bytecode::Interpreter& interpreter) const
  250. {
  251. auto callee = interpreter.reg(m_callee);
  252. if (!callee.is_function()) {
  253. TODO();
  254. }
  255. auto& function = callee.as_function();
  256. auto this_value = interpreter.reg(m_this_value);
  257. Value return_value;
  258. if (m_argument_count == 0 && m_type == CallType::Call) {
  259. auto return_value_or_error = interpreter.vm().call(function, this_value);
  260. if (!return_value_or_error.is_error())
  261. return_value = return_value_or_error.release_value();
  262. } else {
  263. MarkedValueList argument_values { interpreter.vm().heap() };
  264. for (size_t i = 0; i < m_argument_count; ++i) {
  265. argument_values.append(interpreter.reg(m_arguments[i]));
  266. }
  267. if (m_type == CallType::Call) {
  268. auto return_value_or_error = interpreter.vm().call(function, this_value, move(argument_values));
  269. if (!return_value_or_error.is_error())
  270. return_value = return_value_or_error.release_value();
  271. } else {
  272. return_value = interpreter.vm().construct(function, function, move(argument_values));
  273. }
  274. }
  275. interpreter.accumulator() = return_value;
  276. }
  277. void NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  278. {
  279. auto& vm = interpreter.vm();
  280. 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(), m_function_node.kind(), m_function_node.is_strict_mode(), m_function_node.is_arrow_function());
  281. }
  282. void Return::execute_impl(Bytecode::Interpreter& interpreter) const
  283. {
  284. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  285. }
  286. void Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  287. {
  288. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  289. if (interpreter.vm().exception())
  290. return;
  291. if (old_value.is_number())
  292. interpreter.accumulator() = Value(old_value.as_double() + 1);
  293. else
  294. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  295. }
  296. void Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  297. {
  298. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  299. if (interpreter.vm().exception())
  300. return;
  301. if (old_value.is_number())
  302. interpreter.accumulator() = Value(old_value.as_double() - 1);
  303. else
  304. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  305. }
  306. void Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  307. {
  308. interpreter.vm().throw_exception(interpreter.global_object(), interpreter.accumulator());
  309. }
  310. void EnterUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  311. {
  312. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  313. interpreter.jump(m_entry_point);
  314. }
  315. void EnterUnwindContext::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  316. {
  317. if (&m_entry_point.block() == &from)
  318. m_entry_point = Label { to };
  319. if (m_handler_target.has_value() && &m_handler_target->block() == &from)
  320. m_handler_target = Label { to };
  321. if (m_finalizer_target.has_value() && &m_finalizer_target->block() == &from)
  322. m_finalizer_target = Label { to };
  323. }
  324. void LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  325. {
  326. interpreter.leave_unwind_context();
  327. }
  328. void ContinuePendingUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  329. {
  330. interpreter.continue_pending_unwind(m_resume_target);
  331. }
  332. void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  333. {
  334. if (&m_resume_target.block() == &from)
  335. m_resume_target = Label { to };
  336. }
  337. void PushDeclarativeEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  338. {
  339. auto* environment = interpreter.vm().heap().allocate<DeclarativeEnvironment>(interpreter.global_object(), interpreter.vm().lexical_environment());
  340. interpreter.vm().running_execution_context().lexical_environment = environment;
  341. interpreter.vm().running_execution_context().variable_environment = environment;
  342. }
  343. void Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  344. {
  345. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  346. auto object = JS::Object::create(interpreter.global_object(), nullptr);
  347. object->define_direct_property("result", yielded_value, JS::default_attributes);
  348. if (m_continuation_label.has_value())
  349. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes);
  350. else
  351. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  352. interpreter.do_return(object);
  353. }
  354. void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  355. {
  356. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  357. m_continuation_label = Label { to };
  358. }
  359. void GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  360. {
  361. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object())) {
  362. auto property_key = interpreter.accumulator().to_property_key(interpreter.global_object());
  363. if (interpreter.vm().exception())
  364. return;
  365. auto value_or_error = object->get(property_key);
  366. if (value_or_error.is_error())
  367. return;
  368. interpreter.accumulator() = value_or_error.release_value();
  369. }
  370. }
  371. void PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  372. {
  373. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object())) {
  374. auto property_key = interpreter.reg(m_property).to_property_key(interpreter.global_object());
  375. if (interpreter.vm().exception())
  376. return;
  377. object->set(property_key, interpreter.accumulator(), Object::ShouldThrowExceptions::Yes);
  378. }
  379. }
  380. void GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  381. {
  382. interpreter.accumulator() = get_iterator(interpreter.global_object(), interpreter.accumulator());
  383. }
  384. void IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  385. {
  386. if (auto* object = interpreter.accumulator().to_object(interpreter.global_object()))
  387. interpreter.accumulator() = iterator_next(*object);
  388. }
  389. void IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const
  390. {
  391. if (auto* iterator_result = interpreter.accumulator().to_object(interpreter.global_object()))
  392. interpreter.accumulator() = Value(iterator_complete(interpreter.global_object(), *iterator_result));
  393. }
  394. void IteratorResultValue::execute_impl(Bytecode::Interpreter& interpreter) const
  395. {
  396. if (auto* iterator_result = interpreter.accumulator().to_object(interpreter.global_object()))
  397. interpreter.accumulator() = iterator_value(interpreter.global_object(), *iterator_result);
  398. }
  399. void NewClass::execute_impl(Bytecode::Interpreter&) const
  400. {
  401. (void)m_class_expression;
  402. TODO();
  403. }
  404. String Load::to_string_impl(Bytecode::Executable const&) const
  405. {
  406. return String::formatted("Load {}", m_src);
  407. }
  408. String LoadImmediate::to_string_impl(Bytecode::Executable const&) const
  409. {
  410. return String::formatted("LoadImmediate {}", m_value);
  411. }
  412. String Store::to_string_impl(Bytecode::Executable const&) const
  413. {
  414. return String::formatted("Store {}", m_dst);
  415. }
  416. String NewBigInt::to_string_impl(Bytecode::Executable const&) const
  417. {
  418. return String::formatted("NewBigInt \"{}\"", m_bigint.to_base(10));
  419. }
  420. String NewArray::to_string_impl(Bytecode::Executable const&) const
  421. {
  422. StringBuilder builder;
  423. builder.append("NewArray");
  424. if (m_element_count != 0) {
  425. builder.append(" [");
  426. for (size_t i = 0; i < m_element_count; ++i) {
  427. builder.appendff("{}", m_elements[i]);
  428. if (i != m_element_count - 1)
  429. builder.append(',');
  430. }
  431. builder.append(']');
  432. }
  433. return builder.to_string();
  434. }
  435. String IteratorToArray::to_string_impl(const Bytecode::Executable&) const
  436. {
  437. return "IteratorToArray";
  438. }
  439. String NewString::to_string_impl(Bytecode::Executable const& executable) const
  440. {
  441. return String::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  442. }
  443. String NewObject::to_string_impl(Bytecode::Executable const&) const
  444. {
  445. return "NewObject";
  446. }
  447. String NewRegExp::to_string_impl(Bytecode::Executable const& executable) const
  448. {
  449. return String::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  450. }
  451. String CopyObjectExcludingProperties::to_string_impl(const Bytecode::Executable&) const
  452. {
  453. StringBuilder builder;
  454. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  455. if (m_excluded_names_count != 0) {
  456. builder.append(" excluding:[");
  457. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  458. builder.appendff("{}", m_excluded_names[i]);
  459. if (i != m_excluded_names_count - 1)
  460. builder.append(',');
  461. }
  462. builder.append(']');
  463. }
  464. return builder.to_string();
  465. }
  466. String ConcatString::to_string_impl(Bytecode::Executable const&) const
  467. {
  468. return String::formatted("ConcatString {}", m_lhs);
  469. }
  470. String GetVariable::to_string_impl(Bytecode::Executable const& executable) const
  471. {
  472. return String::formatted("GetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  473. }
  474. String SetVariable::to_string_impl(Bytecode::Executable const& executable) const
  475. {
  476. return String::formatted("SetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  477. }
  478. String PutById::to_string_impl(Bytecode::Executable const& executable) const
  479. {
  480. return String::formatted("PutById base:{}, property:{} ({})", m_base, m_property, executable.string_table->get(m_property));
  481. }
  482. String GetById::to_string_impl(Bytecode::Executable const& executable) const
  483. {
  484. return String::formatted("GetById {} ({})", m_property, executable.string_table->get(m_property));
  485. }
  486. String Jump::to_string_impl(Bytecode::Executable const&) const
  487. {
  488. if (m_true_target.has_value())
  489. return String::formatted("Jump {}", *m_true_target);
  490. return String::formatted("Jump <empty>");
  491. }
  492. String JumpConditional::to_string_impl(Bytecode::Executable const&) const
  493. {
  494. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  495. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  496. return String::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  497. }
  498. String JumpNullish::to_string_impl(Bytecode::Executable const&) const
  499. {
  500. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  501. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  502. return String::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  503. }
  504. String JumpUndefined::to_string_impl(Bytecode::Executable const&) const
  505. {
  506. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  507. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  508. return String::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  509. }
  510. String Call::to_string_impl(Bytecode::Executable const&) const
  511. {
  512. StringBuilder builder;
  513. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  514. if (m_argument_count != 0) {
  515. builder.append(", arguments:[");
  516. for (size_t i = 0; i < m_argument_count; ++i) {
  517. builder.appendff("{}", m_arguments[i]);
  518. if (i != m_argument_count - 1)
  519. builder.append(',');
  520. }
  521. builder.append(']');
  522. }
  523. return builder.to_string();
  524. }
  525. String NewFunction::to_string_impl(Bytecode::Executable const&) const
  526. {
  527. return "NewFunction";
  528. }
  529. String NewClass::to_string_impl(Bytecode::Executable const&) const
  530. {
  531. return "NewClass";
  532. }
  533. String Return::to_string_impl(Bytecode::Executable const&) const
  534. {
  535. return "Return";
  536. }
  537. String Increment::to_string_impl(Bytecode::Executable const&) const
  538. {
  539. return "Increment";
  540. }
  541. String Decrement::to_string_impl(Bytecode::Executable const&) const
  542. {
  543. return "Decrement";
  544. }
  545. String Throw::to_string_impl(Bytecode::Executable const&) const
  546. {
  547. return "Throw";
  548. }
  549. String EnterUnwindContext::to_string_impl(Bytecode::Executable const&) const
  550. {
  551. auto handler_string = m_handler_target.has_value() ? String::formatted("{}", *m_handler_target) : "<empty>";
  552. auto finalizer_string = m_finalizer_target.has_value() ? String::formatted("{}", *m_finalizer_target) : "<empty>";
  553. return String::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  554. }
  555. String LeaveUnwindContext::to_string_impl(Bytecode::Executable const&) const
  556. {
  557. return "LeaveUnwindContext";
  558. }
  559. String ContinuePendingUnwind::to_string_impl(Bytecode::Executable const&) const
  560. {
  561. return String::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  562. }
  563. String PushDeclarativeEnvironment::to_string_impl(const Bytecode::Executable& executable) const
  564. {
  565. StringBuilder builder;
  566. builder.append("PushDeclarativeEnvironment");
  567. if (!m_variables.is_empty()) {
  568. builder.append(" {");
  569. Vector<String> names;
  570. for (auto& it : m_variables)
  571. names.append(executable.get_string(it.key));
  572. builder.join(", ", names);
  573. builder.append("}");
  574. }
  575. return builder.to_string();
  576. }
  577. String Yield::to_string_impl(Bytecode::Executable const&) const
  578. {
  579. if (m_continuation_label.has_value())
  580. return String::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  581. return String::formatted("Yield return");
  582. }
  583. String GetByValue::to_string_impl(const Bytecode::Executable&) const
  584. {
  585. return String::formatted("GetByValue base:{}", m_base);
  586. }
  587. String PutByValue::to_string_impl(const Bytecode::Executable&) const
  588. {
  589. return String::formatted("PutByValue base:{}, property:{}", m_base, m_property);
  590. }
  591. String GetIterator::to_string_impl(Executable const&) const
  592. {
  593. return "GetIterator";
  594. }
  595. String IteratorNext::to_string_impl(Executable const&) const
  596. {
  597. return "IteratorNext";
  598. }
  599. String IteratorResultDone::to_string_impl(Executable const&) const
  600. {
  601. return "IteratorResultDone";
  602. }
  603. String IteratorResultValue::to_string_impl(Executable const&) const
  604. {
  605. return "IteratorResultValue";
  606. }
  607. }