Op.cpp 24 KB

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