Op.cpp 15 KB

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