Op.cpp 11 KB

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