Op.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/AST.h>
  7. #include <LibJS/Bytecode/Interpreter.h>
  8. #include <LibJS/Bytecode/Op.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/ScriptFunction.h>
  11. #include <LibJS/Runtime/Value.h>
  12. namespace JS::Bytecode::Op {
  13. void Load::execute(Bytecode::Interpreter& interpreter) const
  14. {
  15. interpreter.reg(m_dst) = m_value;
  16. }
  17. void Add::execute(Bytecode::Interpreter& interpreter) const
  18. {
  19. interpreter.reg(m_dst) = add(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  20. }
  21. void Sub::execute(Bytecode::Interpreter& interpreter) const
  22. {
  23. interpreter.reg(m_dst) = sub(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  24. }
  25. void LessThan::execute(Bytecode::Interpreter& interpreter) const
  26. {
  27. interpreter.reg(m_dst) = less_than(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  28. }
  29. void AbstractInequals::execute(Bytecode::Interpreter& interpreter) const
  30. {
  31. interpreter.reg(m_dst) = Value(!abstract_eq(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2)));
  32. }
  33. void NewString::execute(Bytecode::Interpreter& interpreter) const
  34. {
  35. interpreter.reg(m_dst) = js_string(interpreter.vm(), m_string);
  36. }
  37. void NewObject::execute(Bytecode::Interpreter& interpreter) const
  38. {
  39. interpreter.reg(m_dst) = Object::create_empty(interpreter.global_object());
  40. }
  41. void GetVariable::execute(Bytecode::Interpreter& interpreter) const
  42. {
  43. interpreter.reg(m_dst) = interpreter.vm().get_variable(m_identifier, interpreter.global_object());
  44. }
  45. void SetVariable::execute(Bytecode::Interpreter& interpreter) const
  46. {
  47. interpreter.vm().set_variable(m_identifier, interpreter.reg(m_src), interpreter.global_object());
  48. }
  49. void GetById::execute(Bytecode::Interpreter& interpreter) const
  50. {
  51. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  52. interpreter.reg(m_dst) = object->get(m_property);
  53. }
  54. void PutById::execute(Bytecode::Interpreter& interpreter) const
  55. {
  56. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  57. object->put(m_property, interpreter.reg(m_src));
  58. }
  59. void Jump::execute(Bytecode::Interpreter& interpreter) const
  60. {
  61. interpreter.jump(m_target);
  62. }
  63. void JumpIfFalse::execute(Bytecode::Interpreter& interpreter) const
  64. {
  65. VERIFY(m_target.has_value());
  66. auto result = interpreter.reg(m_result);
  67. if (!result.as_bool())
  68. interpreter.jump(m_target.value());
  69. }
  70. void JumpIfTrue::execute(Bytecode::Interpreter& interpreter) const
  71. {
  72. VERIFY(m_target.has_value());
  73. auto result = interpreter.reg(m_result);
  74. if (result.as_bool())
  75. interpreter.jump(m_target.value());
  76. }
  77. void Call::execute(Bytecode::Interpreter& interpreter) const
  78. {
  79. auto callee = interpreter.reg(m_callee);
  80. if (!callee.is_function()) {
  81. TODO();
  82. }
  83. auto& function = callee.as_function();
  84. auto this_value = interpreter.reg(m_this_value);
  85. Value return_value;
  86. if (m_arguments.is_empty()) {
  87. return_value = interpreter.vm().call(function, this_value);
  88. } else {
  89. MarkedValueList argument_values { interpreter.vm().heap() };
  90. for (auto& arg : m_arguments) {
  91. argument_values.append(interpreter.reg(arg));
  92. }
  93. return_value = interpreter.vm().call(function, this_value, move(argument_values));
  94. }
  95. interpreter.reg(m_dst) = return_value;
  96. }
  97. void EnterScope::execute(Bytecode::Interpreter& interpreter) const
  98. {
  99. auto& vm = interpreter.vm();
  100. auto& global_object = interpreter.global_object();
  101. for (auto& declaration : m_scope_node.functions())
  102. vm.current_scope()->put_to_scope(declaration.name(), { js_undefined(), DeclarationKind::Var });
  103. for (auto& declaration : m_scope_node.functions()) {
  104. auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), vm.current_scope(), declaration.is_strict_mode());
  105. vm.set_variable(declaration.name(), function, global_object);
  106. }
  107. // FIXME: Process variable declarations.
  108. // FIXME: Whatever else JS::Interpreter::enter_scope() does.
  109. }
  110. void Return::execute(Bytecode::Interpreter& interpreter) const
  111. {
  112. auto return_value = m_argument.has_value() ? interpreter.reg(m_argument.value()) : js_undefined();
  113. interpreter.do_return(return_value);
  114. }
  115. String Load::to_string() const
  116. {
  117. return String::formatted("Load dst:{}, value:{}", m_dst, m_value.to_string_without_side_effects());
  118. }
  119. String Add::to_string() const
  120. {
  121. return String::formatted("Add dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  122. }
  123. String Sub::to_string() const
  124. {
  125. return String::formatted("Sub dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  126. }
  127. String LessThan::to_string() const
  128. {
  129. return String::formatted("LessThan dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  130. }
  131. String AbstractInequals::to_string() const
  132. {
  133. return String::formatted("AbstractInequals dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  134. }
  135. String NewString::to_string() const
  136. {
  137. return String::formatted("NewString dst:{}, string:\"{}\"", m_dst, m_string);
  138. }
  139. String NewObject::to_string() const
  140. {
  141. return String::formatted("NewObject dst:{}", m_dst);
  142. }
  143. String GetVariable::to_string() const
  144. {
  145. return String::formatted("GetVariable dst:{}, identifier:{}", m_dst, m_identifier);
  146. }
  147. String SetVariable::to_string() const
  148. {
  149. return String::formatted("SetVariable identifier:{}, src:{}", m_identifier, m_src);
  150. }
  151. String PutById::to_string() const
  152. {
  153. return String::formatted("PutById base:{}, property:{}, src:{}", m_base, m_property, m_src);
  154. }
  155. String GetById::to_string() const
  156. {
  157. return String::formatted("GetById dst:{}, base:{}, property:{}", m_dst, m_base, m_property);
  158. }
  159. String Jump::to_string() const
  160. {
  161. return String::formatted("Jump {}", m_target);
  162. }
  163. String JumpIfFalse::to_string() const
  164. {
  165. if (m_target.has_value())
  166. return String::formatted("JumpIfFalse result:{}, target:{}", m_result, m_target.value());
  167. return String::formatted("JumpIfFalse result:{}, target:<empty>", m_result);
  168. }
  169. String JumpIfTrue::to_string() const
  170. {
  171. if (m_target.has_value())
  172. return String::formatted("JumpIfTrue result:{}, target:{}", m_result, m_target.value());
  173. return String::formatted("JumpIfTrue result:{}, target:<empty>", m_result);
  174. }
  175. String Call::to_string() const
  176. {
  177. StringBuilder builder;
  178. builder.appendff("Call dst:{}, callee:{}, this:{}", m_dst, m_callee, m_this_value);
  179. if (!m_arguments.is_empty()) {
  180. builder.append(", arguments:[");
  181. for (size_t i = 0; i < m_arguments.size(); ++i) {
  182. builder.appendff("{}", m_arguments[i]);
  183. if (i != m_arguments.size() - 1)
  184. builder.append(',');
  185. }
  186. builder.append(']');
  187. }
  188. return builder.to_string();
  189. }
  190. String EnterScope::to_string() const
  191. {
  192. return "EnterScope";
  193. }
  194. String Return::to_string() const
  195. {
  196. if (m_argument.has_value())
  197. return String::formatted("Return {}", m_argument.value());
  198. return "Return";
  199. }
  200. }