Op.cpp 12 KB

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