Op.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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/ScriptFunction.h>
  15. #include <LibJS/Runtime/Value.h>
  16. namespace JS::Bytecode {
  17. String Instruction::to_string(Bytecode::Executable const& executable) const
  18. {
  19. #define __BYTECODE_OP(op) \
  20. case Instruction::Type::op: \
  21. return static_cast<Bytecode::Op::op const&>(*this).to_string(executable);
  22. switch (type()) {
  23. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  24. default:
  25. VERIFY_NOT_REACHED();
  26. }
  27. #undef __BYTECODE_OP
  28. }
  29. }
  30. namespace JS::Bytecode::Op {
  31. void Load::execute(Bytecode::Interpreter& interpreter) const
  32. {
  33. interpreter.accumulator() = interpreter.reg(m_src);
  34. }
  35. void LoadImmediate::execute(Bytecode::Interpreter& interpreter) const
  36. {
  37. interpreter.accumulator() = m_value;
  38. }
  39. void Store::execute(Bytecode::Interpreter& interpreter) const
  40. {
  41. interpreter.reg(m_dst) = interpreter.accumulator();
  42. }
  43. static Value abstract_inequals(GlobalObject& global_object, Value src1, Value src2)
  44. {
  45. return Value(!abstract_eq(global_object, src1, src2));
  46. }
  47. static Value abstract_equals(GlobalObject& global_object, Value src1, Value src2)
  48. {
  49. return Value(abstract_eq(global_object, src1, src2));
  50. }
  51. static Value typed_inequals(GlobalObject&, Value src1, Value src2)
  52. {
  53. return Value(!strict_eq(src1, src2));
  54. }
  55. static Value typed_equals(GlobalObject&, Value src1, Value src2)
  56. {
  57. return Value(strict_eq(src1, src2));
  58. }
  59. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  60. void OpTitleCase::execute(Bytecode::Interpreter& interpreter) const \
  61. { \
  62. auto lhs = interpreter.reg(m_lhs_reg); \
  63. auto rhs = interpreter.accumulator(); \
  64. interpreter.accumulator() = op_snake_case(interpreter.global_object(), lhs, rhs); \
  65. } \
  66. String OpTitleCase::to_string(Bytecode::Executable const&) const \
  67. { \
  68. return String::formatted(#OpTitleCase " {}", m_lhs_reg); \
  69. }
  70. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  71. static Value not_(GlobalObject&, Value value)
  72. {
  73. return Value(!value.to_boolean());
  74. }
  75. static Value typeof_(GlobalObject& global_object, Value value)
  76. {
  77. return js_string(global_object.vm(), value.typeof());
  78. }
  79. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  80. void OpTitleCase::execute(Bytecode::Interpreter& interpreter) const \
  81. { \
  82. interpreter.accumulator() = op_snake_case(interpreter.global_object(), interpreter.accumulator()); \
  83. } \
  84. String OpTitleCase::to_string(Bytecode::Executable const&) const \
  85. { \
  86. return #OpTitleCase; \
  87. }
  88. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  89. void NewBigInt::execute(Bytecode::Interpreter& interpreter) const
  90. {
  91. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), m_bigint);
  92. }
  93. void NewArray::execute(Bytecode::Interpreter& interpreter) const
  94. {
  95. Vector<Value> elements;
  96. elements.ensure_capacity(m_element_count);
  97. for (size_t i = 0; i < m_element_count; i++)
  98. elements.append(interpreter.reg(m_elements[i]));
  99. interpreter.accumulator() = Array::create_from(interpreter.global_object(), elements);
  100. }
  101. void NewString::execute(Bytecode::Interpreter& interpreter) const
  102. {
  103. interpreter.accumulator() = js_string(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  104. }
  105. void NewObject::execute(Bytecode::Interpreter& interpreter) const
  106. {
  107. interpreter.accumulator() = Object::create_empty(interpreter.global_object());
  108. }
  109. void ConcatString::execute(Bytecode::Interpreter& interpreter) const
  110. {
  111. interpreter.reg(m_lhs) = add(interpreter.global_object(), interpreter.reg(m_lhs), interpreter.accumulator());
  112. }
  113. void GetVariable::execute(Bytecode::Interpreter& interpreter) const
  114. {
  115. interpreter.accumulator() = interpreter.vm().get_variable(interpreter.current_executable().get_string(m_identifier), interpreter.global_object());
  116. }
  117. void SetVariable::execute(Bytecode::Interpreter& interpreter) const
  118. {
  119. interpreter.vm().set_variable(interpreter.current_executable().get_string(m_identifier), interpreter.accumulator(), interpreter.global_object());
  120. }
  121. void GetById::execute(Bytecode::Interpreter& interpreter) const
  122. {
  123. if (auto* object = interpreter.accumulator().to_object(interpreter.global_object()))
  124. interpreter.accumulator() = object->get(interpreter.current_executable().get_string(m_property));
  125. }
  126. void PutById::execute(Bytecode::Interpreter& interpreter) const
  127. {
  128. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  129. object->put(interpreter.current_executable().get_string(m_property), interpreter.accumulator());
  130. }
  131. void Jump::execute(Bytecode::Interpreter& interpreter) const
  132. {
  133. interpreter.jump(*m_true_target);
  134. }
  135. void JumpConditional::execute(Bytecode::Interpreter& interpreter) const
  136. {
  137. VERIFY(m_true_target.has_value());
  138. VERIFY(m_false_target.has_value());
  139. auto result = interpreter.accumulator();
  140. if (result.to_boolean())
  141. interpreter.jump(m_true_target.value());
  142. else
  143. interpreter.jump(m_false_target.value());
  144. }
  145. void JumpNullish::execute(Bytecode::Interpreter& interpreter) const
  146. {
  147. VERIFY(m_true_target.has_value());
  148. VERIFY(m_false_target.has_value());
  149. auto result = interpreter.accumulator();
  150. if (result.is_nullish())
  151. interpreter.jump(m_true_target.value());
  152. else
  153. interpreter.jump(m_false_target.value());
  154. }
  155. void Call::execute(Bytecode::Interpreter& interpreter) const
  156. {
  157. auto callee = interpreter.reg(m_callee);
  158. if (!callee.is_function()) {
  159. TODO();
  160. }
  161. auto& function = callee.as_function();
  162. auto this_value = interpreter.reg(m_this_value);
  163. Value return_value;
  164. if (m_argument_count == 0) {
  165. return_value = interpreter.vm().call(function, this_value);
  166. } else {
  167. MarkedValueList argument_values { interpreter.vm().heap() };
  168. for (size_t i = 0; i < m_argument_count; ++i) {
  169. argument_values.append(interpreter.reg(m_arguments[i]));
  170. }
  171. return_value = interpreter.vm().call(function, this_value, move(argument_values));
  172. }
  173. interpreter.accumulator() = return_value;
  174. }
  175. void EnterScope::execute(Bytecode::Interpreter& interpreter) const
  176. {
  177. auto& vm = interpreter.vm();
  178. auto& global_object = interpreter.global_object();
  179. for (auto& declaration : m_scope_node.functions())
  180. vm.current_scope()->put_to_scope(declaration.name(), { js_undefined(), DeclarationKind::Var });
  181. for (auto& declaration : m_scope_node.functions()) {
  182. auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), vm.current_scope(), declaration.is_strict_mode());
  183. vm.set_variable(declaration.name(), function, global_object);
  184. }
  185. // FIXME: Process variable declarations.
  186. // FIXME: Whatever else JS::Interpreter::enter_scope() does.
  187. }
  188. void Return::execute(Bytecode::Interpreter& interpreter) const
  189. {
  190. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  191. }
  192. void Increment::execute(Bytecode::Interpreter& interpreter) const
  193. {
  194. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  195. if (interpreter.vm().exception())
  196. return;
  197. if (old_value.is_number())
  198. interpreter.accumulator() = Value(old_value.as_double() + 1);
  199. else
  200. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  201. }
  202. void Decrement::execute(Bytecode::Interpreter& interpreter) const
  203. {
  204. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  205. if (interpreter.vm().exception())
  206. return;
  207. if (old_value.is_number())
  208. interpreter.accumulator() = Value(old_value.as_double() - 1);
  209. else
  210. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  211. }
  212. void Throw::execute(Bytecode::Interpreter& interpreter) const
  213. {
  214. interpreter.vm().throw_exception(interpreter.global_object(), interpreter.accumulator());
  215. }
  216. String Load::to_string(Bytecode::Executable const&) const
  217. {
  218. return String::formatted("Load {}", m_src);
  219. }
  220. String LoadImmediate::to_string(Bytecode::Executable const&) const
  221. {
  222. return String::formatted("LoadImmediate {}", m_value);
  223. }
  224. String Store::to_string(Bytecode::Executable const&) const
  225. {
  226. return String::formatted("Store {}", m_dst);
  227. }
  228. String NewBigInt::to_string(Bytecode::Executable const&) const
  229. {
  230. return String::formatted("NewBigInt \"{}\"", m_bigint.to_base10());
  231. }
  232. String NewArray::to_string(Bytecode::Executable const&) const
  233. {
  234. StringBuilder builder;
  235. builder.append("NewArray");
  236. if (m_element_count != 0) {
  237. builder.append(" [");
  238. for (size_t i = 0; i < m_element_count; ++i) {
  239. builder.appendff("{}", m_elements[i]);
  240. if (i != m_element_count - 1)
  241. builder.append(',');
  242. }
  243. builder.append(']');
  244. }
  245. return builder.to_string();
  246. }
  247. String NewString::to_string(Bytecode::Executable const& executable) const
  248. {
  249. return String::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  250. }
  251. String NewObject::to_string(Bytecode::Executable const&) const
  252. {
  253. return "NewObject";
  254. }
  255. String ConcatString::to_string(Bytecode::Executable const&) const
  256. {
  257. return String::formatted("ConcatString {}", m_lhs);
  258. }
  259. String GetVariable::to_string(Bytecode::Executable const& executable) const
  260. {
  261. return String::formatted("GetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  262. }
  263. String SetVariable::to_string(Bytecode::Executable const& executable) const
  264. {
  265. return String::formatted("SetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  266. }
  267. String PutById::to_string(Bytecode::Executable const& executable) const
  268. {
  269. return String::formatted("PutById base:{}, property:{} ({})", m_base, m_property, executable.string_table->get(m_property));
  270. }
  271. String GetById::to_string(Bytecode::Executable const& executable) const
  272. {
  273. return String::formatted("GetById {} ({})", m_property, executable.string_table->get(m_property));
  274. }
  275. String Jump::to_string(Bytecode::Executable const&) const
  276. {
  277. if (m_true_target.has_value())
  278. return String::formatted("Jump {}", *m_true_target);
  279. return String::formatted("Jump <empty>");
  280. }
  281. String JumpConditional::to_string(Bytecode::Executable const&) const
  282. {
  283. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  284. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  285. return String::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  286. }
  287. String JumpNullish::to_string(Bytecode::Executable const&) const
  288. {
  289. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  290. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  291. return String::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  292. }
  293. String Call::to_string(Bytecode::Executable const&) const
  294. {
  295. StringBuilder builder;
  296. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  297. if (m_argument_count != 0) {
  298. builder.append(", arguments:[");
  299. for (size_t i = 0; i < m_argument_count; ++i) {
  300. builder.appendff("{}", m_arguments[i]);
  301. if (i != m_argument_count - 1)
  302. builder.append(',');
  303. }
  304. builder.append(']');
  305. }
  306. return builder.to_string();
  307. }
  308. String EnterScope::to_string(Bytecode::Executable const&) const
  309. {
  310. return "EnterScope";
  311. }
  312. String Return::to_string(Bytecode::Executable const&) const
  313. {
  314. return "Return";
  315. }
  316. String Increment::to_string(Bytecode::Executable const&) const
  317. {
  318. return "Increment";
  319. }
  320. String Decrement::to_string(Bytecode::Executable const&) const
  321. {
  322. return "Decrement";
  323. }
  324. String Throw::to_string(Bytecode::Executable const&) const
  325. {
  326. return "Throw";
  327. }
  328. }