Op.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/GlobalObject.h>
  11. #include <LibJS/Runtime/ScriptFunction.h>
  12. #include <LibJS/Runtime/Value.h>
  13. namespace JS::Bytecode {
  14. void Instruction::execute(Bytecode::Interpreter& interpreter) const
  15. {
  16. #define __BYTECODE_OP(op) \
  17. case Instruction::Type::op: \
  18. return static_cast<Bytecode::Op::op const&>(*this).execute(interpreter);
  19. switch (type()) {
  20. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  21. default:
  22. VERIFY_NOT_REACHED();
  23. }
  24. #undef __BYTECODE_OP
  25. }
  26. String Instruction::to_string() const
  27. {
  28. #define __BYTECODE_OP(op) \
  29. case Instruction::Type::op: \
  30. return static_cast<Bytecode::Op::op const&>(*this).to_string();
  31. switch (type()) {
  32. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  33. default:
  34. VERIFY_NOT_REACHED();
  35. }
  36. #undef __BYTECODE_OP
  37. }
  38. }
  39. namespace JS::Bytecode::Op {
  40. void Load::execute(Bytecode::Interpreter& interpreter) const
  41. {
  42. interpreter.reg(m_dst) = m_value;
  43. }
  44. void LoadRegister::execute(Bytecode::Interpreter& interpreter) const
  45. {
  46. interpreter.reg(m_dst) = interpreter.reg(m_src);
  47. }
  48. static Value abstract_inequals(GlobalObject& global_object, Value src1, Value src2)
  49. {
  50. return Value(!abstract_eq(global_object, src1, src2));
  51. }
  52. static Value abstract_equals(GlobalObject& global_object, Value src1, Value src2)
  53. {
  54. return Value(abstract_eq(global_object, src1, src2));
  55. }
  56. static Value typed_inequals(GlobalObject&, Value src1, Value src2)
  57. {
  58. return Value(!strict_eq(src1, src2));
  59. }
  60. static Value typed_equals(GlobalObject&, Value src1, Value src2)
  61. {
  62. return Value(strict_eq(src1, src2));
  63. }
  64. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  65. void OpTitleCase::execute(Bytecode::Interpreter& interpreter) const \
  66. { \
  67. interpreter.reg(m_dst) = op_snake_case(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2)); \
  68. } \
  69. String OpTitleCase::to_string() const \
  70. { \
  71. return String::formatted(#OpTitleCase " dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2); \
  72. }
  73. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  74. static Value not_(GlobalObject&, Value value)
  75. {
  76. return Value(!value.to_boolean());
  77. }
  78. static Value typeof_(GlobalObject& global_object, Value value)
  79. {
  80. return js_string(global_object.vm(), value.typeof());
  81. }
  82. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  83. void OpTitleCase::execute(Bytecode::Interpreter& interpreter) const \
  84. { \
  85. interpreter.reg(m_dst) = op_snake_case(interpreter.global_object(), interpreter.reg(m_src)); \
  86. } \
  87. String OpTitleCase::to_string() const \
  88. { \
  89. return String::formatted(#OpTitleCase " dst:{}, src:{}", m_dst, m_src); \
  90. }
  91. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  92. void NewString::execute(Bytecode::Interpreter& interpreter) const
  93. {
  94. interpreter.reg(m_dst) = js_string(interpreter.vm(), m_string);
  95. }
  96. void NewObject::execute(Bytecode::Interpreter& interpreter) const
  97. {
  98. interpreter.reg(m_dst) = Object::create_empty(interpreter.global_object());
  99. }
  100. void GetVariable::execute(Bytecode::Interpreter& interpreter) const
  101. {
  102. interpreter.reg(m_dst) = interpreter.vm().get_variable(m_identifier, interpreter.global_object());
  103. }
  104. void SetVariable::execute(Bytecode::Interpreter& interpreter) const
  105. {
  106. interpreter.vm().set_variable(m_identifier, interpreter.reg(m_src), interpreter.global_object());
  107. }
  108. void GetById::execute(Bytecode::Interpreter& interpreter) const
  109. {
  110. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  111. interpreter.reg(m_dst) = object->get(m_property);
  112. }
  113. void PutById::execute(Bytecode::Interpreter& interpreter) const
  114. {
  115. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  116. object->put(m_property, interpreter.reg(m_src));
  117. }
  118. void Jump::execute(Bytecode::Interpreter& interpreter) const
  119. {
  120. interpreter.jump(*m_target);
  121. }
  122. void JumpIfFalse::execute(Bytecode::Interpreter& interpreter) const
  123. {
  124. VERIFY(m_target.has_value());
  125. auto result = interpreter.reg(m_result);
  126. if (!result.as_bool())
  127. interpreter.jump(m_target.value());
  128. }
  129. void JumpIfTrue::execute(Bytecode::Interpreter& interpreter) const
  130. {
  131. VERIFY(m_target.has_value());
  132. auto result = interpreter.reg(m_result);
  133. if (result.as_bool())
  134. interpreter.jump(m_target.value());
  135. }
  136. void Call::execute(Bytecode::Interpreter& interpreter) const
  137. {
  138. auto callee = interpreter.reg(m_callee);
  139. if (!callee.is_function()) {
  140. TODO();
  141. }
  142. auto& function = callee.as_function();
  143. auto this_value = interpreter.reg(m_this_value);
  144. Value return_value;
  145. if (m_argument_count == 0) {
  146. return_value = interpreter.vm().call(function, this_value);
  147. } else {
  148. MarkedValueList argument_values { interpreter.vm().heap() };
  149. for (size_t i = 0; i < m_argument_count; ++i) {
  150. argument_values.append(interpreter.reg(m_arguments[i]));
  151. }
  152. return_value = interpreter.vm().call(function, this_value, move(argument_values));
  153. }
  154. interpreter.reg(m_dst) = return_value;
  155. }
  156. void EnterScope::execute(Bytecode::Interpreter& interpreter) const
  157. {
  158. auto& vm = interpreter.vm();
  159. auto& global_object = interpreter.global_object();
  160. for (auto& declaration : m_scope_node.functions())
  161. vm.current_scope()->put_to_scope(declaration.name(), { js_undefined(), DeclarationKind::Var });
  162. for (auto& declaration : m_scope_node.functions()) {
  163. auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), vm.current_scope(), declaration.is_strict_mode());
  164. vm.set_variable(declaration.name(), function, global_object);
  165. }
  166. // FIXME: Process variable declarations.
  167. // FIXME: Whatever else JS::Interpreter::enter_scope() does.
  168. }
  169. void Return::execute(Bytecode::Interpreter& interpreter) const
  170. {
  171. auto return_value = m_argument.has_value() ? interpreter.reg(m_argument.value()) : js_undefined();
  172. interpreter.do_return(return_value);
  173. }
  174. String Load::to_string() const
  175. {
  176. return String::formatted("Load dst:{}, value:{}", m_dst, m_value.to_string_without_side_effects());
  177. }
  178. String LoadRegister::to_string() const
  179. {
  180. return String::formatted("LoadRegister dst:{}, src:{}", m_dst, m_src);
  181. }
  182. String NewString::to_string() const
  183. {
  184. return String::formatted("NewString dst:{}, string:\"{}\"", m_dst, m_string);
  185. }
  186. String NewObject::to_string() const
  187. {
  188. return String::formatted("NewObject dst:{}", m_dst);
  189. }
  190. String GetVariable::to_string() const
  191. {
  192. return String::formatted("GetVariable dst:{}, identifier:{}", m_dst, m_identifier);
  193. }
  194. String SetVariable::to_string() const
  195. {
  196. return String::formatted("SetVariable identifier:{}, src:{}", m_identifier, m_src);
  197. }
  198. String PutById::to_string() const
  199. {
  200. return String::formatted("PutById base:{}, property:{}, src:{}", m_base, m_property, m_src);
  201. }
  202. String GetById::to_string() const
  203. {
  204. return String::formatted("GetById dst:{}, base:{}, property:{}", m_dst, m_base, m_property);
  205. }
  206. String Jump::to_string() const
  207. {
  208. return String::formatted("Jump {}", *m_target);
  209. }
  210. String JumpIfFalse::to_string() const
  211. {
  212. if (m_target.has_value())
  213. return String::formatted("JumpIfFalse result:{}, target:{}", m_result, m_target.value());
  214. return String::formatted("JumpIfFalse result:{}, target:<empty>", m_result);
  215. }
  216. String JumpIfTrue::to_string() const
  217. {
  218. if (m_target.has_value())
  219. return String::formatted("JumpIfTrue result:{}, target:{}", m_result, m_target.value());
  220. return String::formatted("JumpIfTrue result:{}, target:<empty>", m_result);
  221. }
  222. String Call::to_string() const
  223. {
  224. StringBuilder builder;
  225. builder.appendff("Call dst:{}, callee:{}, this:{}", m_dst, m_callee, m_this_value);
  226. if (m_argument_count != 0) {
  227. builder.append(", arguments:[");
  228. for (size_t i = 0; i < m_argument_count; ++i) {
  229. builder.appendff("{}", m_arguments[i]);
  230. if (i != m_argument_count - 1)
  231. builder.append(',');
  232. }
  233. builder.append(']');
  234. }
  235. return builder.to_string();
  236. }
  237. String EnterScope::to_string() const
  238. {
  239. return "EnterScope";
  240. }
  241. String Return::to_string() const
  242. {
  243. if (m_argument.has_value())
  244. return String::formatted("Return {}", m_argument.value());
  245. return "Return";
  246. }
  247. }