Op.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 <LibJS/AST.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/GlobalObject.h>
  14. #include <LibJS/Runtime/LexicalEnvironment.h>
  15. #include <LibJS/Runtime/ScopeObject.h>
  16. #include <LibJS/Runtime/ScriptFunction.h>
  17. #include <LibJS/Runtime/Value.h>
  18. namespace JS::Bytecode {
  19. String Instruction::to_string(Bytecode::Executable const& executable) const
  20. {
  21. #define __BYTECODE_OP(op) \
  22. case Instruction::Type::op: \
  23. return static_cast<Bytecode::Op::op const&>(*this).to_string_impl(executable);
  24. switch (type()) {
  25. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  26. default:
  27. VERIFY_NOT_REACHED();
  28. }
  29. #undef __BYTECODE_OP
  30. }
  31. }
  32. namespace JS::Bytecode::Op {
  33. void Load::execute_impl(Bytecode::Interpreter& interpreter) const
  34. {
  35. interpreter.accumulator() = interpreter.reg(m_src);
  36. }
  37. void LoadImmediate::execute_impl(Bytecode::Interpreter& interpreter) const
  38. {
  39. interpreter.accumulator() = m_value;
  40. }
  41. void Store::execute_impl(Bytecode::Interpreter& interpreter) const
  42. {
  43. interpreter.reg(m_dst) = interpreter.accumulator();
  44. }
  45. static Value abstract_inequals(GlobalObject& global_object, Value src1, Value src2)
  46. {
  47. return Value(!abstract_eq(global_object, src1, src2));
  48. }
  49. static Value abstract_equals(GlobalObject& global_object, Value src1, Value src2)
  50. {
  51. return Value(abstract_eq(global_object, src1, src2));
  52. }
  53. static Value typed_inequals(GlobalObject&, Value src1, Value src2)
  54. {
  55. return Value(!strict_eq(src1, src2));
  56. }
  57. static Value typed_equals(GlobalObject&, Value src1, Value src2)
  58. {
  59. return Value(strict_eq(src1, src2));
  60. }
  61. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  62. void OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  63. { \
  64. auto lhs = interpreter.reg(m_lhs_reg); \
  65. auto rhs = interpreter.accumulator(); \
  66. interpreter.accumulator() = op_snake_case(interpreter.global_object(), lhs, rhs); \
  67. } \
  68. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  69. { \
  70. return String::formatted(#OpTitleCase " {}", m_lhs_reg); \
  71. }
  72. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  73. static Value not_(GlobalObject&, Value value)
  74. {
  75. return Value(!value.to_boolean());
  76. }
  77. static Value typeof_(GlobalObject& global_object, Value value)
  78. {
  79. return js_string(global_object.vm(), value.typeof());
  80. }
  81. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  82. void OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  83. { \
  84. interpreter.accumulator() = op_snake_case(interpreter.global_object(), interpreter.accumulator()); \
  85. } \
  86. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  87. { \
  88. return #OpTitleCase; \
  89. }
  90. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  91. void NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
  92. {
  93. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), m_bigint);
  94. }
  95. void NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  96. {
  97. Vector<Value> elements;
  98. elements.ensure_capacity(m_element_count);
  99. for (size_t i = 0; i < m_element_count; i++)
  100. elements.append(interpreter.reg(m_elements[i]));
  101. interpreter.accumulator() = Array::create_from(interpreter.global_object(), elements);
  102. }
  103. void NewString::execute_impl(Bytecode::Interpreter& interpreter) const
  104. {
  105. interpreter.accumulator() = js_string(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  106. }
  107. void NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  108. {
  109. interpreter.accumulator() = Object::create(interpreter.global_object(), interpreter.global_object().object_prototype());
  110. }
  111. void ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  112. {
  113. interpreter.reg(m_lhs) = add(interpreter.global_object(), interpreter.reg(m_lhs), interpreter.accumulator());
  114. }
  115. void GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  116. {
  117. interpreter.accumulator() = interpreter.vm().get_variable(interpreter.current_executable().get_string(m_identifier), interpreter.global_object());
  118. }
  119. void SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  120. {
  121. interpreter.vm().set_variable(interpreter.current_executable().get_string(m_identifier), interpreter.accumulator(), interpreter.global_object());
  122. }
  123. void GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  124. {
  125. if (auto* object = interpreter.accumulator().to_object(interpreter.global_object()))
  126. interpreter.accumulator() = object->get(interpreter.current_executable().get_string(m_property)).value_or(js_undefined());
  127. }
  128. void PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  129. {
  130. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  131. object->put(interpreter.current_executable().get_string(m_property), interpreter.accumulator());
  132. }
  133. void Jump::execute_impl(Bytecode::Interpreter& interpreter) const
  134. {
  135. interpreter.jump(*m_true_target);
  136. }
  137. void Jump::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  138. {
  139. if (m_true_target.has_value() && &m_true_target->block() == &from)
  140. m_true_target = Label { to };
  141. if (m_false_target.has_value() && &m_false_target->block() == &from)
  142. m_false_target = Label { to };
  143. }
  144. void JumpConditional::execute_impl(Bytecode::Interpreter& interpreter) const
  145. {
  146. VERIFY(m_true_target.has_value());
  147. VERIFY(m_false_target.has_value());
  148. auto result = interpreter.accumulator();
  149. if (result.to_boolean())
  150. interpreter.jump(m_true_target.value());
  151. else
  152. interpreter.jump(m_false_target.value());
  153. }
  154. void JumpNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  155. {
  156. VERIFY(m_true_target.has_value());
  157. VERIFY(m_false_target.has_value());
  158. auto result = interpreter.accumulator();
  159. if (result.is_nullish())
  160. interpreter.jump(m_true_target.value());
  161. else
  162. interpreter.jump(m_false_target.value());
  163. }
  164. void JumpUndefined::execute_impl(Bytecode::Interpreter& interpreter) const
  165. {
  166. VERIFY(m_true_target.has_value());
  167. VERIFY(m_false_target.has_value());
  168. auto result = interpreter.accumulator();
  169. if (result.is_undefined())
  170. interpreter.jump(m_true_target.value());
  171. else
  172. interpreter.jump(m_false_target.value());
  173. }
  174. void Call::execute_impl(Bytecode::Interpreter& interpreter) const
  175. {
  176. auto callee = interpreter.reg(m_callee);
  177. if (!callee.is_function()) {
  178. TODO();
  179. }
  180. auto& function = callee.as_function();
  181. auto this_value = interpreter.reg(m_this_value);
  182. Value return_value;
  183. if (m_argument_count == 0 && m_type == CallType::Call) {
  184. return_value = interpreter.vm().call(function, this_value);
  185. } else {
  186. MarkedValueList argument_values { interpreter.vm().heap() };
  187. for (size_t i = 0; i < m_argument_count; ++i) {
  188. argument_values.append(interpreter.reg(m_arguments[i]));
  189. }
  190. if (m_type == CallType::Call)
  191. return_value = interpreter.vm().call(function, this_value, move(argument_values));
  192. else
  193. return_value = interpreter.vm().construct(function, function, move(argument_values));
  194. }
  195. interpreter.accumulator() = return_value;
  196. }
  197. void NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  198. {
  199. auto& vm = interpreter.vm();
  200. interpreter.accumulator() = ScriptFunction::create(interpreter.global_object(), m_function_node.name(), m_function_node.body(), m_function_node.parameters(), m_function_node.function_length(), vm.current_scope(), m_function_node.kind(), m_function_node.is_strict_mode(), m_function_node.is_arrow_function());
  201. }
  202. void Return::execute_impl(Bytecode::Interpreter& interpreter) const
  203. {
  204. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  205. }
  206. void Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  207. {
  208. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  209. if (interpreter.vm().exception())
  210. return;
  211. if (old_value.is_number())
  212. interpreter.accumulator() = Value(old_value.as_double() + 1);
  213. else
  214. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  215. }
  216. void Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  217. {
  218. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  219. if (interpreter.vm().exception())
  220. return;
  221. if (old_value.is_number())
  222. interpreter.accumulator() = Value(old_value.as_double() - 1);
  223. else
  224. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  225. }
  226. void Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  227. {
  228. interpreter.vm().throw_exception(interpreter.global_object(), interpreter.accumulator());
  229. }
  230. void EnterUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  231. {
  232. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  233. interpreter.jump(m_entry_point);
  234. }
  235. void EnterUnwindContext::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  236. {
  237. if (&m_entry_point.block() == &from)
  238. m_entry_point = Label { to };
  239. if (m_handler_target.has_value() && &m_handler_target->block() == &from)
  240. m_handler_target = Label { to };
  241. if (m_finalizer_target.has_value() && &m_finalizer_target->block() == &from)
  242. m_finalizer_target = Label { to };
  243. }
  244. void LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  245. {
  246. interpreter.leave_unwind_context();
  247. }
  248. void ContinuePendingUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  249. {
  250. interpreter.continue_pending_unwind(m_resume_target);
  251. }
  252. void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  253. {
  254. if (&m_resume_target.block() == &from)
  255. m_resume_target = Label { to };
  256. }
  257. void PushLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  258. {
  259. HashMap<FlyString, Variable> resolved_variables;
  260. for (auto& it : m_variables)
  261. resolved_variables.set(interpreter.current_executable().get_string(it.key), it.value);
  262. auto* block_lexical_environment = interpreter.vm().heap().allocate<LexicalEnvironment>(interpreter.global_object(), move(resolved_variables), interpreter.vm().current_scope());
  263. interpreter.vm().call_frame().scope = block_lexical_environment;
  264. }
  265. void Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  266. {
  267. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  268. auto object = JS::Object::create(interpreter.global_object(), nullptr);
  269. object->put("result", yielded_value);
  270. if (m_continuation_label.has_value())
  271. object->put("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))));
  272. else
  273. object->put("continuation", Value(0));
  274. interpreter.do_return(object);
  275. }
  276. void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  277. {
  278. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  279. m_continuation_label = Label { to };
  280. }
  281. void GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  282. {
  283. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object())) {
  284. auto property_key = interpreter.accumulator().to_property_key(interpreter.global_object());
  285. if (interpreter.vm().exception())
  286. return;
  287. interpreter.accumulator() = object->get(property_key).value_or(js_undefined());
  288. }
  289. }
  290. void PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  291. {
  292. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object())) {
  293. auto property_key = interpreter.reg(m_property).to_property_key(interpreter.global_object());
  294. if (interpreter.vm().exception())
  295. return;
  296. object->put(property_key, interpreter.accumulator());
  297. }
  298. }
  299. void LoadArgument::execute_impl(Bytecode::Interpreter& interpreter) const
  300. {
  301. interpreter.accumulator() = interpreter.vm().argument(m_index);
  302. }
  303. String Load::to_string_impl(Bytecode::Executable const&) const
  304. {
  305. return String::formatted("Load {}", m_src);
  306. }
  307. String LoadImmediate::to_string_impl(Bytecode::Executable const&) const
  308. {
  309. return String::formatted("LoadImmediate {}", m_value);
  310. }
  311. String Store::to_string_impl(Bytecode::Executable const&) const
  312. {
  313. return String::formatted("Store {}", m_dst);
  314. }
  315. String NewBigInt::to_string_impl(Bytecode::Executable const&) const
  316. {
  317. return String::formatted("NewBigInt \"{}\"", m_bigint.to_base10());
  318. }
  319. String NewArray::to_string_impl(Bytecode::Executable const&) const
  320. {
  321. StringBuilder builder;
  322. builder.append("NewArray");
  323. if (m_element_count != 0) {
  324. builder.append(" [");
  325. for (size_t i = 0; i < m_element_count; ++i) {
  326. builder.appendff("{}", m_elements[i]);
  327. if (i != m_element_count - 1)
  328. builder.append(',');
  329. }
  330. builder.append(']');
  331. }
  332. return builder.to_string();
  333. }
  334. String NewString::to_string_impl(Bytecode::Executable const& executable) const
  335. {
  336. return String::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  337. }
  338. String NewObject::to_string_impl(Bytecode::Executable const&) const
  339. {
  340. return "NewObject";
  341. }
  342. String ConcatString::to_string_impl(Bytecode::Executable const&) const
  343. {
  344. return String::formatted("ConcatString {}", m_lhs);
  345. }
  346. String GetVariable::to_string_impl(Bytecode::Executable const& executable) const
  347. {
  348. return String::formatted("GetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  349. }
  350. String SetVariable::to_string_impl(Bytecode::Executable const& executable) const
  351. {
  352. return String::formatted("SetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  353. }
  354. String PutById::to_string_impl(Bytecode::Executable const& executable) const
  355. {
  356. return String::formatted("PutById base:{}, property:{} ({})", m_base, m_property, executable.string_table->get(m_property));
  357. }
  358. String GetById::to_string_impl(Bytecode::Executable const& executable) const
  359. {
  360. return String::formatted("GetById {} ({})", m_property, executable.string_table->get(m_property));
  361. }
  362. String Jump::to_string_impl(Bytecode::Executable const&) const
  363. {
  364. if (m_true_target.has_value())
  365. return String::formatted("Jump {}", *m_true_target);
  366. return String::formatted("Jump <empty>");
  367. }
  368. String JumpConditional::to_string_impl(Bytecode::Executable const&) const
  369. {
  370. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  371. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  372. return String::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  373. }
  374. String JumpNullish::to_string_impl(Bytecode::Executable const&) const
  375. {
  376. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  377. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  378. return String::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  379. }
  380. String JumpUndefined::to_string_impl(Bytecode::Executable const&) const
  381. {
  382. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  383. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  384. return String::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  385. }
  386. String Call::to_string_impl(Bytecode::Executable const&) const
  387. {
  388. StringBuilder builder;
  389. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  390. if (m_argument_count != 0) {
  391. builder.append(", arguments:[");
  392. for (size_t i = 0; i < m_argument_count; ++i) {
  393. builder.appendff("{}", m_arguments[i]);
  394. if (i != m_argument_count - 1)
  395. builder.append(',');
  396. }
  397. builder.append(']');
  398. }
  399. return builder.to_string();
  400. }
  401. String NewFunction::to_string_impl(Bytecode::Executable const&) const
  402. {
  403. return "NewFunction";
  404. }
  405. String Return::to_string_impl(Bytecode::Executable const&) const
  406. {
  407. return "Return";
  408. }
  409. String Increment::to_string_impl(Bytecode::Executable const&) const
  410. {
  411. return "Increment";
  412. }
  413. String Decrement::to_string_impl(Bytecode::Executable const&) const
  414. {
  415. return "Decrement";
  416. }
  417. String Throw::to_string_impl(Bytecode::Executable const&) const
  418. {
  419. return "Throw";
  420. }
  421. String EnterUnwindContext::to_string_impl(Bytecode::Executable const&) const
  422. {
  423. auto handler_string = m_handler_target.has_value() ? String::formatted("{}", *m_handler_target) : "<empty>";
  424. auto finalizer_string = m_finalizer_target.has_value() ? String::formatted("{}", *m_finalizer_target) : "<empty>";
  425. return String::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  426. }
  427. String LeaveUnwindContext::to_string_impl(Bytecode::Executable const&) const
  428. {
  429. return "LeaveUnwindContext";
  430. }
  431. String ContinuePendingUnwind::to_string_impl(Bytecode::Executable const&) const
  432. {
  433. return String::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  434. }
  435. String PushLexicalEnvironment::to_string_impl(const Bytecode::Executable& executable) const
  436. {
  437. StringBuilder builder;
  438. builder.append("PushLexicalEnvironment");
  439. if (!m_variables.is_empty()) {
  440. builder.append(" {");
  441. Vector<String> names;
  442. for (auto& it : m_variables)
  443. names.append(executable.get_string(it.key));
  444. builder.join(", ", names);
  445. builder.append("}");
  446. }
  447. return builder.to_string();
  448. }
  449. String Yield::to_string_impl(Bytecode::Executable const&) const
  450. {
  451. if (m_continuation_label.has_value())
  452. return String::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  453. return String::formatted("Yield return");
  454. }
  455. String GetByValue::to_string_impl(const Bytecode::Executable&) const
  456. {
  457. return String::formatted("GetByValue base:{}", m_base);
  458. }
  459. String PutByValue::to_string_impl(const Bytecode::Executable&) const
  460. {
  461. return String::formatted("PutByValue base:{}, property:{}", m_base, m_property);
  462. }
  463. String LoadArgument::to_string_impl(const Bytecode::Executable&) const
  464. {
  465. return String::formatted("LoadArgument {}", m_index);
  466. }
  467. }