Op.cpp 24 KB

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