Op.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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(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(Bytecode::Interpreter& interpreter) const
  34. {
  35. interpreter.accumulator() = interpreter.reg(m_src);
  36. }
  37. void LoadImmediate::execute(Bytecode::Interpreter& interpreter) const
  38. {
  39. interpreter.accumulator() = m_value;
  40. }
  41. void Store::execute(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(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(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(Bytecode::Interpreter& interpreter) const \
  83. { \
  84. interpreter.accumulator() = op_snake_case(interpreter.global_object(), interpreter.accumulator()); \
  85. } \
  86. String OpTitleCase::to_string(Bytecode::Executable const&) const \
  87. { \
  88. return #OpTitleCase; \
  89. }
  90. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  91. void NewBigInt::execute(Bytecode::Interpreter& interpreter) const
  92. {
  93. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), m_bigint);
  94. }
  95. void NewArray::execute(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(Bytecode::Interpreter& interpreter) const
  104. {
  105. interpreter.accumulator() = js_string(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  106. }
  107. void NewObject::execute(Bytecode::Interpreter& interpreter) const
  108. {
  109. interpreter.accumulator() = Object::create_empty(interpreter.global_object());
  110. }
  111. void ConcatString::execute(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(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(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(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));
  127. }
  128. void PutById::execute(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(Bytecode::Interpreter& interpreter) const
  134. {
  135. interpreter.jump(*m_true_target);
  136. }
  137. void JumpConditional::execute(Bytecode::Interpreter& interpreter) const
  138. {
  139. VERIFY(m_true_target.has_value());
  140. VERIFY(m_false_target.has_value());
  141. auto result = interpreter.accumulator();
  142. if (result.to_boolean())
  143. interpreter.jump(m_true_target.value());
  144. else
  145. interpreter.jump(m_false_target.value());
  146. }
  147. void JumpNullish::execute(Bytecode::Interpreter& interpreter) const
  148. {
  149. VERIFY(m_true_target.has_value());
  150. VERIFY(m_false_target.has_value());
  151. auto result = interpreter.accumulator();
  152. if (result.is_nullish())
  153. interpreter.jump(m_true_target.value());
  154. else
  155. interpreter.jump(m_false_target.value());
  156. }
  157. void Call::execute(Bytecode::Interpreter& interpreter) const
  158. {
  159. auto callee = interpreter.reg(m_callee);
  160. if (!callee.is_function()) {
  161. TODO();
  162. }
  163. auto& function = callee.as_function();
  164. auto this_value = interpreter.reg(m_this_value);
  165. Value return_value;
  166. if (m_argument_count == 0 && m_type == CallType::Call) {
  167. return_value = interpreter.vm().call(function, this_value);
  168. } else {
  169. MarkedValueList argument_values { interpreter.vm().heap() };
  170. for (size_t i = 0; i < m_argument_count; ++i) {
  171. argument_values.append(interpreter.reg(m_arguments[i]));
  172. }
  173. if (m_type == CallType::Call)
  174. return_value = interpreter.vm().call(function, this_value, move(argument_values));
  175. else
  176. return_value = interpreter.vm().construct(function, function, move(argument_values));
  177. }
  178. interpreter.accumulator() = return_value;
  179. }
  180. void NewFunction::execute(Bytecode::Interpreter& interpreter) const
  181. {
  182. auto& vm = interpreter.vm();
  183. 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());
  184. }
  185. void Return::execute(Bytecode::Interpreter& interpreter) const
  186. {
  187. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  188. }
  189. void Increment::execute(Bytecode::Interpreter& interpreter) const
  190. {
  191. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  192. if (interpreter.vm().exception())
  193. return;
  194. if (old_value.is_number())
  195. interpreter.accumulator() = Value(old_value.as_double() + 1);
  196. else
  197. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  198. }
  199. void Decrement::execute(Bytecode::Interpreter& interpreter) const
  200. {
  201. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  202. if (interpreter.vm().exception())
  203. return;
  204. if (old_value.is_number())
  205. interpreter.accumulator() = Value(old_value.as_double() - 1);
  206. else
  207. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  208. }
  209. void Throw::execute(Bytecode::Interpreter& interpreter) const
  210. {
  211. interpreter.vm().throw_exception(interpreter.global_object(), interpreter.accumulator());
  212. }
  213. void EnterUnwindContext::execute(Bytecode::Interpreter& interpreter) const
  214. {
  215. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  216. interpreter.jump(m_entry_point);
  217. }
  218. void LeaveUnwindContext::execute(Bytecode::Interpreter& interpreter) const
  219. {
  220. interpreter.leave_unwind_context();
  221. }
  222. void ContinuePendingUnwind::execute(Bytecode::Interpreter& interpreter) const
  223. {
  224. interpreter.continue_pending_unwind(m_resume_target);
  225. }
  226. void PushLexicalEnvironment::execute(Bytecode::Interpreter& interpreter) const
  227. {
  228. HashMap<FlyString, Variable> resolved_variables;
  229. for (auto& it : m_variables)
  230. resolved_variables.set(interpreter.current_executable().get_string(it.key), it.value);
  231. auto* block_lexical_environment = interpreter.vm().heap().allocate<LexicalEnvironment>(interpreter.global_object(), move(resolved_variables), interpreter.vm().current_scope());
  232. interpreter.vm().call_frame().scope = block_lexical_environment;
  233. }
  234. void Yield::execute(Bytecode::Interpreter& interpreter) const
  235. {
  236. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  237. auto object = JS::Object::create_empty(interpreter.global_object());
  238. object->put("result", yielded_value);
  239. if (m_continuation_label.has_value())
  240. object->put("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))));
  241. else
  242. object->put("continuation", Value(0));
  243. interpreter.do_return(object);
  244. }
  245. void GetByValue::execute(Bytecode::Interpreter& interpreter) const
  246. {
  247. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object())) {
  248. auto property_key = interpreter.accumulator().to_property_key(interpreter.global_object());
  249. if (interpreter.vm().exception())
  250. return;
  251. interpreter.accumulator() = object->get(property_key);
  252. }
  253. }
  254. void PutByValue::execute(Bytecode::Interpreter& interpreter) const
  255. {
  256. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object())) {
  257. auto property_key = interpreter.reg(m_property).to_property_key(interpreter.global_object());
  258. if (interpreter.vm().exception())
  259. return;
  260. object->put(property_key, interpreter.accumulator());
  261. }
  262. }
  263. void LoadArgument::execute(Bytecode::Interpreter& interpreter) const
  264. {
  265. interpreter.accumulator() = interpreter.vm().argument(m_index);
  266. }
  267. String Load::to_string(Bytecode::Executable const&) const
  268. {
  269. return String::formatted("Load {}", m_src);
  270. }
  271. String LoadImmediate::to_string(Bytecode::Executable const&) const
  272. {
  273. return String::formatted("LoadImmediate {}", m_value);
  274. }
  275. String Store::to_string(Bytecode::Executable const&) const
  276. {
  277. return String::formatted("Store {}", m_dst);
  278. }
  279. String NewBigInt::to_string(Bytecode::Executable const&) const
  280. {
  281. return String::formatted("NewBigInt \"{}\"", m_bigint.to_base10());
  282. }
  283. String NewArray::to_string(Bytecode::Executable const&) const
  284. {
  285. StringBuilder builder;
  286. builder.append("NewArray");
  287. if (m_element_count != 0) {
  288. builder.append(" [");
  289. for (size_t i = 0; i < m_element_count; ++i) {
  290. builder.appendff("{}", m_elements[i]);
  291. if (i != m_element_count - 1)
  292. builder.append(',');
  293. }
  294. builder.append(']');
  295. }
  296. return builder.to_string();
  297. }
  298. String NewString::to_string(Bytecode::Executable const& executable) const
  299. {
  300. return String::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  301. }
  302. String NewObject::to_string(Bytecode::Executable const&) const
  303. {
  304. return "NewObject";
  305. }
  306. String ConcatString::to_string(Bytecode::Executable const&) const
  307. {
  308. return String::formatted("ConcatString {}", m_lhs);
  309. }
  310. String GetVariable::to_string(Bytecode::Executable const& executable) const
  311. {
  312. return String::formatted("GetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  313. }
  314. String SetVariable::to_string(Bytecode::Executable const& executable) const
  315. {
  316. return String::formatted("SetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  317. }
  318. String PutById::to_string(Bytecode::Executable const& executable) const
  319. {
  320. return String::formatted("PutById base:{}, property:{} ({})", m_base, m_property, executable.string_table->get(m_property));
  321. }
  322. String GetById::to_string(Bytecode::Executable const& executable) const
  323. {
  324. return String::formatted("GetById {} ({})", m_property, executable.string_table->get(m_property));
  325. }
  326. String Jump::to_string(Bytecode::Executable const&) const
  327. {
  328. if (m_true_target.has_value())
  329. return String::formatted("Jump {}", *m_true_target);
  330. return String::formatted("Jump <empty>");
  331. }
  332. String JumpConditional::to_string(Bytecode::Executable const&) const
  333. {
  334. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  335. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  336. return String::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  337. }
  338. String JumpNullish::to_string(Bytecode::Executable const&) const
  339. {
  340. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  341. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  342. return String::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  343. }
  344. String Call::to_string(Bytecode::Executable const&) const
  345. {
  346. StringBuilder builder;
  347. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  348. if (m_argument_count != 0) {
  349. builder.append(", arguments:[");
  350. for (size_t i = 0; i < m_argument_count; ++i) {
  351. builder.appendff("{}", m_arguments[i]);
  352. if (i != m_argument_count - 1)
  353. builder.append(',');
  354. }
  355. builder.append(']');
  356. }
  357. return builder.to_string();
  358. }
  359. String NewFunction::to_string(Bytecode::Executable const&) const
  360. {
  361. return "NewFunction";
  362. }
  363. String Return::to_string(Bytecode::Executable const&) const
  364. {
  365. return "Return";
  366. }
  367. String Increment::to_string(Bytecode::Executable const&) const
  368. {
  369. return "Increment";
  370. }
  371. String Decrement::to_string(Bytecode::Executable const&) const
  372. {
  373. return "Decrement";
  374. }
  375. String Throw::to_string(Bytecode::Executable const&) const
  376. {
  377. return "Throw";
  378. }
  379. String EnterUnwindContext::to_string(Bytecode::Executable const&) const
  380. {
  381. auto handler_string = m_handler_target.has_value() ? String::formatted("{}", *m_handler_target) : "<empty>";
  382. auto finalizer_string = m_finalizer_target.has_value() ? String::formatted("{}", *m_finalizer_target) : "<empty>";
  383. return String::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  384. }
  385. String LeaveUnwindContext::to_string(Bytecode::Executable const&) const
  386. {
  387. return "LeaveUnwindContext";
  388. }
  389. String ContinuePendingUnwind::to_string(Bytecode::Executable const&) const
  390. {
  391. return String::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  392. }
  393. String PushLexicalEnvironment::to_string(const Bytecode::Executable& executable) const
  394. {
  395. StringBuilder builder;
  396. builder.append("PushLexicalEnvironment");
  397. if (!m_variables.is_empty()) {
  398. builder.append(" {");
  399. Vector<String> names;
  400. for (auto& it : m_variables)
  401. names.append(executable.get_string(it.key));
  402. builder.join(", ", names);
  403. builder.append("}");
  404. }
  405. return builder.to_string();
  406. }
  407. String Yield::to_string(Bytecode::Executable const&) const
  408. {
  409. if (m_continuation_label.has_value())
  410. return String::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  411. return String::formatted("Yield return");
  412. }
  413. String GetByValue::to_string(const Bytecode::Executable&) const
  414. {
  415. return String::formatted("GetByValue base:{}", m_base);
  416. }
  417. String PutByValue::to_string(const Bytecode::Executable&) const
  418. {
  419. return String::formatted("PutByValue base:{}, property:{}", m_base, m_property);
  420. }
  421. String LoadArgument::to_string(const Bytecode::Executable&) const
  422. {
  423. return String::formatted("LoadArgument {}", m_index);
  424. }
  425. }