Op.cpp 28 KB

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