Op.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 = 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. auto return_value_or_error = interpreter.vm().call(function, this_value);
  246. if (!return_value_or_error.is_error())
  247. return_value = return_value_or_error.release_value();
  248. } else {
  249. MarkedValueList argument_values { interpreter.vm().heap() };
  250. for (size_t i = 0; i < m_argument_count; ++i) {
  251. argument_values.append(interpreter.reg(m_arguments[i]));
  252. }
  253. if (m_type == CallType::Call) {
  254. auto return_value_or_error = interpreter.vm().call(function, this_value, move(argument_values));
  255. if (!return_value_or_error.is_error())
  256. return_value = return_value_or_error.release_value();
  257. } else {
  258. return_value = interpreter.vm().construct(function, function, move(argument_values));
  259. }
  260. }
  261. interpreter.accumulator() = return_value;
  262. }
  263. void NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  264. {
  265. auto& vm = interpreter.vm();
  266. 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());
  267. }
  268. void Return::execute_impl(Bytecode::Interpreter& interpreter) const
  269. {
  270. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  271. }
  272. void Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  273. {
  274. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  275. if (interpreter.vm().exception())
  276. return;
  277. if (old_value.is_number())
  278. interpreter.accumulator() = Value(old_value.as_double() + 1);
  279. else
  280. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  281. }
  282. void Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  283. {
  284. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  285. if (interpreter.vm().exception())
  286. return;
  287. if (old_value.is_number())
  288. interpreter.accumulator() = Value(old_value.as_double() - 1);
  289. else
  290. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  291. }
  292. void Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  293. {
  294. interpreter.vm().throw_exception(interpreter.global_object(), interpreter.accumulator());
  295. }
  296. void EnterUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  297. {
  298. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  299. interpreter.jump(m_entry_point);
  300. }
  301. void EnterUnwindContext::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  302. {
  303. if (&m_entry_point.block() == &from)
  304. m_entry_point = Label { to };
  305. if (m_handler_target.has_value() && &m_handler_target->block() == &from)
  306. m_handler_target = Label { to };
  307. if (m_finalizer_target.has_value() && &m_finalizer_target->block() == &from)
  308. m_finalizer_target = Label { to };
  309. }
  310. void LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  311. {
  312. interpreter.leave_unwind_context();
  313. }
  314. void ContinuePendingUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  315. {
  316. interpreter.continue_pending_unwind(m_resume_target);
  317. }
  318. void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  319. {
  320. if (&m_resume_target.block() == &from)
  321. m_resume_target = Label { to };
  322. }
  323. void PushDeclarativeEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  324. {
  325. HashMap<FlyString, Variable> resolved_variables;
  326. for (auto& it : m_variables)
  327. resolved_variables.set(interpreter.current_executable().get_string(it.key), it.value);
  328. auto* environment = interpreter.vm().heap().allocate<DeclarativeEnvironment>(interpreter.global_object(), move(resolved_variables), interpreter.vm().lexical_environment());
  329. interpreter.vm().running_execution_context().lexical_environment = environment;
  330. interpreter.vm().running_execution_context().variable_environment = environment;
  331. }
  332. void Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  333. {
  334. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  335. auto object = JS::Object::create(interpreter.global_object(), nullptr);
  336. object->define_direct_property("result", yielded_value, JS::default_attributes);
  337. if (m_continuation_label.has_value())
  338. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes);
  339. else
  340. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  341. interpreter.do_return(object);
  342. }
  343. void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  344. {
  345. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  346. m_continuation_label = Label { to };
  347. }
  348. void GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  349. {
  350. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object())) {
  351. auto property_key = interpreter.accumulator().to_property_key(interpreter.global_object());
  352. if (interpreter.vm().exception())
  353. return;
  354. interpreter.accumulator() = object->get(property_key);
  355. }
  356. }
  357. void PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  358. {
  359. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object())) {
  360. auto property_key = interpreter.reg(m_property).to_property_key(interpreter.global_object());
  361. if (interpreter.vm().exception())
  362. return;
  363. object->set(property_key, interpreter.accumulator(), Object::ShouldThrowExceptions::Yes);
  364. }
  365. }
  366. void GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  367. {
  368. interpreter.accumulator() = get_iterator(interpreter.global_object(), interpreter.accumulator());
  369. }
  370. void IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  371. {
  372. if (auto* object = interpreter.accumulator().to_object(interpreter.global_object()))
  373. interpreter.accumulator() = iterator_next(*object);
  374. }
  375. void IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const
  376. {
  377. if (auto* iterator_result = interpreter.accumulator().to_object(interpreter.global_object()))
  378. interpreter.accumulator() = Value(iterator_complete(interpreter.global_object(), *iterator_result));
  379. }
  380. void IteratorResultValue::execute_impl(Bytecode::Interpreter& interpreter) const
  381. {
  382. if (auto* iterator_result = interpreter.accumulator().to_object(interpreter.global_object()))
  383. interpreter.accumulator() = iterator_value(interpreter.global_object(), *iterator_result);
  384. }
  385. void NewClass::execute_impl(Bytecode::Interpreter&) const
  386. {
  387. (void)m_class_expression;
  388. TODO();
  389. }
  390. String Load::to_string_impl(Bytecode::Executable const&) const
  391. {
  392. return String::formatted("Load {}", m_src);
  393. }
  394. String LoadImmediate::to_string_impl(Bytecode::Executable const&) const
  395. {
  396. return String::formatted("LoadImmediate {}", m_value);
  397. }
  398. String Store::to_string_impl(Bytecode::Executable const&) const
  399. {
  400. return String::formatted("Store {}", m_dst);
  401. }
  402. String NewBigInt::to_string_impl(Bytecode::Executable const&) const
  403. {
  404. return String::formatted("NewBigInt \"{}\"", m_bigint.to_base(10));
  405. }
  406. String NewArray::to_string_impl(Bytecode::Executable const&) const
  407. {
  408. StringBuilder builder;
  409. builder.append("NewArray");
  410. if (m_element_count != 0) {
  411. builder.append(" [");
  412. for (size_t i = 0; i < m_element_count; ++i) {
  413. builder.appendff("{}", m_elements[i]);
  414. if (i != m_element_count - 1)
  415. builder.append(',');
  416. }
  417. builder.append(']');
  418. }
  419. return builder.to_string();
  420. }
  421. String IteratorToArray::to_string_impl(const Bytecode::Executable&) const
  422. {
  423. return "IteratorToArray";
  424. }
  425. String NewString::to_string_impl(Bytecode::Executable const& executable) const
  426. {
  427. return String::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  428. }
  429. String NewObject::to_string_impl(Bytecode::Executable const&) const
  430. {
  431. return "NewObject";
  432. }
  433. String NewRegExp::to_string_impl(Bytecode::Executable const& executable) const
  434. {
  435. return String::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  436. }
  437. String CopyObjectExcludingProperties::to_string_impl(const Bytecode::Executable&) const
  438. {
  439. StringBuilder builder;
  440. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  441. if (m_excluded_names_count != 0) {
  442. builder.append(" excluding:[");
  443. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  444. builder.appendff("{}", m_excluded_names[i]);
  445. if (i != m_excluded_names_count - 1)
  446. builder.append(',');
  447. }
  448. builder.append(']');
  449. }
  450. return builder.to_string();
  451. }
  452. String ConcatString::to_string_impl(Bytecode::Executable const&) const
  453. {
  454. return String::formatted("ConcatString {}", m_lhs);
  455. }
  456. String GetVariable::to_string_impl(Bytecode::Executable const& executable) const
  457. {
  458. return String::formatted("GetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  459. }
  460. String SetVariable::to_string_impl(Bytecode::Executable const& executable) const
  461. {
  462. return String::formatted("SetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  463. }
  464. String PutById::to_string_impl(Bytecode::Executable const& executable) const
  465. {
  466. return String::formatted("PutById base:{}, property:{} ({})", m_base, m_property, executable.string_table->get(m_property));
  467. }
  468. String GetById::to_string_impl(Bytecode::Executable const& executable) const
  469. {
  470. return String::formatted("GetById {} ({})", m_property, executable.string_table->get(m_property));
  471. }
  472. String Jump::to_string_impl(Bytecode::Executable const&) const
  473. {
  474. if (m_true_target.has_value())
  475. return String::formatted("Jump {}", *m_true_target);
  476. return String::formatted("Jump <empty>");
  477. }
  478. String JumpConditional::to_string_impl(Bytecode::Executable const&) const
  479. {
  480. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  481. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  482. return String::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  483. }
  484. String JumpNullish::to_string_impl(Bytecode::Executable const&) const
  485. {
  486. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  487. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  488. return String::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  489. }
  490. String JumpUndefined::to_string_impl(Bytecode::Executable const&) const
  491. {
  492. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  493. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  494. return String::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  495. }
  496. String Call::to_string_impl(Bytecode::Executable const&) const
  497. {
  498. StringBuilder builder;
  499. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  500. if (m_argument_count != 0) {
  501. builder.append(", arguments:[");
  502. for (size_t i = 0; i < m_argument_count; ++i) {
  503. builder.appendff("{}", m_arguments[i]);
  504. if (i != m_argument_count - 1)
  505. builder.append(',');
  506. }
  507. builder.append(']');
  508. }
  509. return builder.to_string();
  510. }
  511. String NewFunction::to_string_impl(Bytecode::Executable const&) const
  512. {
  513. return "NewFunction";
  514. }
  515. String NewClass::to_string_impl(Bytecode::Executable const&) const
  516. {
  517. return "NewClass";
  518. }
  519. String Return::to_string_impl(Bytecode::Executable const&) const
  520. {
  521. return "Return";
  522. }
  523. String Increment::to_string_impl(Bytecode::Executable const&) const
  524. {
  525. return "Increment";
  526. }
  527. String Decrement::to_string_impl(Bytecode::Executable const&) const
  528. {
  529. return "Decrement";
  530. }
  531. String Throw::to_string_impl(Bytecode::Executable const&) const
  532. {
  533. return "Throw";
  534. }
  535. String EnterUnwindContext::to_string_impl(Bytecode::Executable const&) const
  536. {
  537. auto handler_string = m_handler_target.has_value() ? String::formatted("{}", *m_handler_target) : "<empty>";
  538. auto finalizer_string = m_finalizer_target.has_value() ? String::formatted("{}", *m_finalizer_target) : "<empty>";
  539. return String::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  540. }
  541. String LeaveUnwindContext::to_string_impl(Bytecode::Executable const&) const
  542. {
  543. return "LeaveUnwindContext";
  544. }
  545. String ContinuePendingUnwind::to_string_impl(Bytecode::Executable const&) const
  546. {
  547. return String::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  548. }
  549. String PushDeclarativeEnvironment::to_string_impl(const Bytecode::Executable& executable) const
  550. {
  551. StringBuilder builder;
  552. builder.append("PushDeclarativeEnvironment");
  553. if (!m_variables.is_empty()) {
  554. builder.append(" {");
  555. Vector<String> names;
  556. for (auto& it : m_variables)
  557. names.append(executable.get_string(it.key));
  558. builder.join(", ", names);
  559. builder.append("}");
  560. }
  561. return builder.to_string();
  562. }
  563. String Yield::to_string_impl(Bytecode::Executable const&) const
  564. {
  565. if (m_continuation_label.has_value())
  566. return String::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  567. return String::formatted("Yield return");
  568. }
  569. String GetByValue::to_string_impl(const Bytecode::Executable&) const
  570. {
  571. return String::formatted("GetByValue base:{}", m_base);
  572. }
  573. String PutByValue::to_string_impl(const Bytecode::Executable&) const
  574. {
  575. return String::formatted("PutByValue base:{}, property:{}", m_base, m_property);
  576. }
  577. String GetIterator::to_string_impl(Executable const&) const
  578. {
  579. return "GetIterator";
  580. }
  581. String IteratorNext::to_string_impl(Executable const&) const
  582. {
  583. return "IteratorNext";
  584. }
  585. String IteratorResultDone::to_string_impl(Executable const&) const
  586. {
  587. return "IteratorResultDone";
  588. }
  589. String IteratorResultValue::to_string_impl(Executable const&) const
  590. {
  591. return "IteratorResultValue";
  592. }
  593. }