Op.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, 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 <AK/HashTable.h>
  9. #include <LibJS/Bytecode/Interpreter.h>
  10. #include <LibJS/Bytecode/Op.h>
  11. #include <LibJS/Runtime/AbstractOperations.h>
  12. #include <LibJS/Runtime/Array.h>
  13. #include <LibJS/Runtime/BigInt.h>
  14. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  15. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  16. #include <LibJS/Runtime/Environment.h>
  17. #include <LibJS/Runtime/GlobalObject.h>
  18. #include <LibJS/Runtime/Iterator.h>
  19. #include <LibJS/Runtime/IteratorOperations.h>
  20. #include <LibJS/Runtime/RegExpObject.h>
  21. #include <LibJS/Runtime/Value.h>
  22. namespace JS::Bytecode {
  23. String Instruction::to_string(Bytecode::Executable const& executable) const
  24. {
  25. #define __BYTECODE_OP(op) \
  26. case Instruction::Type::op: \
  27. return static_cast<Bytecode::Op::op const&>(*this).to_string_impl(executable);
  28. switch (type()) {
  29. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  30. default:
  31. VERIFY_NOT_REACHED();
  32. }
  33. #undef __BYTECODE_OP
  34. }
  35. }
  36. namespace JS::Bytecode::Op {
  37. ThrowCompletionOr<void> Load::execute_impl(Bytecode::Interpreter& interpreter) const
  38. {
  39. interpreter.accumulator() = interpreter.reg(m_src);
  40. return {};
  41. }
  42. ThrowCompletionOr<void> LoadImmediate::execute_impl(Bytecode::Interpreter& interpreter) const
  43. {
  44. interpreter.accumulator() = m_value;
  45. return {};
  46. }
  47. ThrowCompletionOr<void> Store::execute_impl(Bytecode::Interpreter& interpreter) const
  48. {
  49. interpreter.reg(m_dst) = interpreter.accumulator();
  50. return {};
  51. }
  52. static ThrowCompletionOr<Value> abstract_inequals(GlobalObject& global_object, Value src1, Value src2)
  53. {
  54. return Value(!TRY(is_loosely_equal(global_object, src1, src2)));
  55. }
  56. static ThrowCompletionOr<Value> abstract_equals(GlobalObject& global_object, Value src1, Value src2)
  57. {
  58. return Value(TRY(is_loosely_equal(global_object, src1, src2)));
  59. }
  60. static ThrowCompletionOr<Value> typed_inequals(GlobalObject&, Value src1, Value src2)
  61. {
  62. return Value(!is_strictly_equal(src1, src2));
  63. }
  64. static ThrowCompletionOr<Value> typed_equals(GlobalObject&, Value src1, Value src2)
  65. {
  66. return Value(is_strictly_equal(src1, src2));
  67. }
  68. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  69. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  70. { \
  71. auto lhs = interpreter.reg(m_lhs_reg); \
  72. auto rhs = interpreter.accumulator(); \
  73. interpreter.accumulator() = TRY(op_snake_case(interpreter.global_object(), lhs, rhs)); \
  74. return {}; \
  75. } \
  76. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  77. { \
  78. return String::formatted(#OpTitleCase " {}", m_lhs_reg); \
  79. }
  80. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  81. static ThrowCompletionOr<Value> not_(GlobalObject&, Value value)
  82. {
  83. return Value(!value.to_boolean());
  84. }
  85. static ThrowCompletionOr<Value> typeof_(GlobalObject& global_object, Value value)
  86. {
  87. return Value(js_string(global_object.vm(), value.typeof()));
  88. }
  89. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  90. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  91. { \
  92. interpreter.accumulator() = TRY(op_snake_case(interpreter.global_object(), interpreter.accumulator())); \
  93. return {}; \
  94. } \
  95. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  96. { \
  97. return #OpTitleCase; \
  98. }
  99. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  100. ThrowCompletionOr<void> NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
  101. {
  102. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), m_bigint);
  103. return {};
  104. }
  105. ThrowCompletionOr<void> NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  106. {
  107. Vector<Value> elements;
  108. elements.ensure_capacity(m_element_count);
  109. for (size_t i = 0; i < m_element_count; i++)
  110. elements.append(interpreter.reg(m_elements[i]));
  111. interpreter.accumulator() = Array::create_from(interpreter.global_object(), elements);
  112. return {};
  113. }
  114. // FIXME: Since the accumulator is a Value, we store an object there and have to convert back and forth between that an Iterator records. Not great.
  115. // Make sure to put this into the accumulator before the iterator object disappears from the stack to prevent the members from being GC'd.
  116. static Object* iterator_to_object(GlobalObject& global_object, Iterator iterator)
  117. {
  118. auto& vm = global_object.vm();
  119. auto* object = Object::create(global_object, nullptr);
  120. object->define_direct_property(vm.names.iterator, iterator.iterator, 0);
  121. object->define_direct_property(vm.names.next, iterator.next_method, 0);
  122. object->define_direct_property(vm.names.done, Value(iterator.done), 0);
  123. return object;
  124. }
  125. static Iterator object_to_iterator(GlobalObject& global_object, Object& object)
  126. {
  127. auto& vm = global_object.vm();
  128. return Iterator {
  129. .iterator = &MUST(object.get(vm.names.iterator)).as_object(),
  130. .next_method = MUST(object.get(vm.names.next)),
  131. .done = MUST(object.get(vm.names.done)).as_bool()
  132. };
  133. }
  134. ThrowCompletionOr<void> IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const
  135. {
  136. auto& global_object = interpreter.global_object();
  137. auto iterator_object = TRY(interpreter.accumulator().to_object(global_object));
  138. auto iterator = object_to_iterator(global_object, *iterator_object);
  139. auto* array = MUST(Array::create(global_object, 0));
  140. size_t index = 0;
  141. while (true) {
  142. auto* iterator_result = TRY(iterator_next(global_object, iterator));
  143. auto complete = TRY(iterator_complete(global_object, *iterator_result));
  144. if (complete) {
  145. interpreter.accumulator() = array;
  146. return {};
  147. }
  148. auto value = TRY(iterator_value(global_object, *iterator_result));
  149. MUST(array->create_data_property_or_throw(index, value));
  150. index++;
  151. }
  152. return {};
  153. }
  154. ThrowCompletionOr<void> NewString::execute_impl(Bytecode::Interpreter& interpreter) const
  155. {
  156. interpreter.accumulator() = js_string(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  157. return {};
  158. }
  159. ThrowCompletionOr<void> NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  160. {
  161. interpreter.accumulator() = Object::create(interpreter.global_object(), interpreter.global_object().object_prototype());
  162. return {};
  163. }
  164. ThrowCompletionOr<void> NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
  165. {
  166. auto source = interpreter.current_executable().get_string(m_source_index);
  167. auto flags = interpreter.current_executable().get_string(m_flags_index);
  168. interpreter.accumulator() = TRY(regexp_create(interpreter.global_object(), js_string(interpreter.vm(), source), js_string(interpreter.vm(), flags)));
  169. return {};
  170. }
  171. ThrowCompletionOr<void> CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const
  172. {
  173. auto* from_object = TRY(interpreter.reg(m_from_object).to_object(interpreter.global_object()));
  174. auto* to_object = Object::create(interpreter.global_object(), interpreter.global_object().object_prototype());
  175. HashTable<Value, ValueTraits> excluded_names;
  176. for (size_t i = 0; i < m_excluded_names_count; ++i)
  177. excluded_names.set(interpreter.reg(m_excluded_names[i]));
  178. auto own_keys = TRY(from_object->internal_own_property_keys());
  179. for (auto& key : own_keys) {
  180. if (!excluded_names.contains(key)) {
  181. auto property_key = TRY(key.to_property_key(interpreter.global_object()));
  182. auto property_value = TRY(from_object->get(property_key));
  183. to_object->define_direct_property(property_key, property_value, JS::default_attributes);
  184. }
  185. }
  186. interpreter.accumulator() = to_object;
  187. return {};
  188. }
  189. ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  190. {
  191. interpreter.reg(m_lhs) = TRY(add(interpreter.global_object(), interpreter.reg(m_lhs), interpreter.accumulator()));
  192. return {};
  193. }
  194. ThrowCompletionOr<void> GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  195. {
  196. auto get_reference = [&]() -> ThrowCompletionOr<Reference> {
  197. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  198. if (m_cached_environment_coordinate.has_value()) {
  199. auto* environment = interpreter.vm().running_execution_context().lexical_environment;
  200. for (size_t i = 0; i < m_cached_environment_coordinate->hops; ++i)
  201. environment = environment->outer_environment();
  202. VERIFY(environment);
  203. VERIFY(environment->is_declarative_environment());
  204. if (!environment->is_permanently_screwed_by_eval()) {
  205. return Reference { *environment, string, interpreter.vm().in_strict_mode(), m_cached_environment_coordinate };
  206. }
  207. m_cached_environment_coordinate = {};
  208. }
  209. auto reference = TRY(interpreter.vm().resolve_binding(string));
  210. if (reference.environment_coordinate().has_value())
  211. m_cached_environment_coordinate = reference.environment_coordinate();
  212. return reference;
  213. };
  214. auto reference = TRY(get_reference());
  215. interpreter.accumulator() = TRY(reference.get_value(interpreter.global_object()));
  216. return {};
  217. }
  218. ThrowCompletionOr<void> CreateEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  219. {
  220. auto make_and_swap_envs = [&](auto*& old_environment) {
  221. Environment* environment = new_declarative_environment(*old_environment);
  222. swap(old_environment, environment);
  223. return environment;
  224. };
  225. if (m_mode == EnvironmentMode::Lexical)
  226. interpreter.saved_lexical_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().lexical_environment));
  227. else if (m_mode == EnvironmentMode::Var)
  228. interpreter.saved_variable_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().variable_environment));
  229. return {};
  230. }
  231. ThrowCompletionOr<void> CreateVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  232. {
  233. auto& vm = interpreter.vm();
  234. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  235. if (m_mode == EnvironmentMode::Lexical) {
  236. // Note: This is papering over an issue where "FunctionDeclarationInstantiation" creates these bindings for us.
  237. // Instead of crashing in there, we'll just raise an exception here.
  238. if (TRY(vm.lexical_environment()->has_binding(name)))
  239. return vm.throw_completion<InternalError>(interpreter.global_object(), String::formatted("Lexical environment already has binding '{}'", name));
  240. if (m_is_immutable)
  241. vm.lexical_environment()->create_immutable_binding(interpreter.global_object(), name, vm.in_strict_mode());
  242. else
  243. vm.lexical_environment()->create_mutable_binding(interpreter.global_object(), name, vm.in_strict_mode());
  244. } else {
  245. if (m_is_immutable)
  246. vm.variable_environment()->create_immutable_binding(interpreter.global_object(), name, vm.in_strict_mode());
  247. else
  248. vm.variable_environment()->create_mutable_binding(interpreter.global_object(), name, vm.in_strict_mode());
  249. }
  250. return {};
  251. }
  252. ThrowCompletionOr<void> SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  253. {
  254. auto& vm = interpreter.vm();
  255. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  256. auto environment = m_mode == EnvironmentMode::Lexical ? vm.running_execution_context().lexical_environment : vm.running_execution_context().variable_environment;
  257. auto reference = TRY(vm.resolve_binding(name, environment));
  258. switch (m_initialization_mode) {
  259. case InitializationMode::Initialize:
  260. TRY(reference.initialize_referenced_binding(interpreter.global_object(), interpreter.accumulator()));
  261. break;
  262. case InitializationMode::Set:
  263. TRY(reference.put_value(interpreter.global_object(), interpreter.accumulator()));
  264. break;
  265. case InitializationMode::InitializeOrSet:
  266. VERIFY(reference.is_environment_reference());
  267. VERIFY(reference.base_environment().is_declarative_environment());
  268. TRY(static_cast<DeclarativeEnvironment&>(reference.base_environment()).initialize_or_set_mutable_binding(interpreter.global_object(), name, interpreter.accumulator()));
  269. break;
  270. }
  271. return {};
  272. }
  273. ThrowCompletionOr<void> GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  274. {
  275. auto* object = TRY(interpreter.accumulator().to_object(interpreter.global_object()));
  276. interpreter.accumulator() = TRY(object->get(interpreter.current_executable().get_identifier(m_property)));
  277. return {};
  278. }
  279. ThrowCompletionOr<void> PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  280. {
  281. auto* object = TRY(interpreter.reg(m_base).to_object(interpreter.global_object()));
  282. TRY(object->set(interpreter.current_executable().get_identifier(m_property), interpreter.accumulator(), Object::ShouldThrowExceptions::Yes));
  283. return {};
  284. }
  285. ThrowCompletionOr<void> Jump::execute_impl(Bytecode::Interpreter& interpreter) const
  286. {
  287. interpreter.jump(*m_true_target);
  288. return {};
  289. }
  290. ThrowCompletionOr<void> ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const
  291. {
  292. interpreter.accumulator() = TRY(interpreter.vm().resolve_this_binding(interpreter.global_object()));
  293. return {};
  294. }
  295. void Jump::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  296. {
  297. if (m_true_target.has_value() && &m_true_target->block() == &from)
  298. m_true_target = Label { to };
  299. if (m_false_target.has_value() && &m_false_target->block() == &from)
  300. m_false_target = Label { to };
  301. }
  302. ThrowCompletionOr<void> JumpConditional::execute_impl(Bytecode::Interpreter& interpreter) const
  303. {
  304. VERIFY(m_true_target.has_value());
  305. VERIFY(m_false_target.has_value());
  306. auto result = interpreter.accumulator();
  307. if (result.to_boolean())
  308. interpreter.jump(m_true_target.value());
  309. else
  310. interpreter.jump(m_false_target.value());
  311. return {};
  312. }
  313. ThrowCompletionOr<void> JumpNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  314. {
  315. VERIFY(m_true_target.has_value());
  316. VERIFY(m_false_target.has_value());
  317. auto result = interpreter.accumulator();
  318. if (result.is_nullish())
  319. interpreter.jump(m_true_target.value());
  320. else
  321. interpreter.jump(m_false_target.value());
  322. return {};
  323. }
  324. ThrowCompletionOr<void> JumpUndefined::execute_impl(Bytecode::Interpreter& interpreter) const
  325. {
  326. VERIFY(m_true_target.has_value());
  327. VERIFY(m_false_target.has_value());
  328. auto result = interpreter.accumulator();
  329. if (result.is_undefined())
  330. interpreter.jump(m_true_target.value());
  331. else
  332. interpreter.jump(m_false_target.value());
  333. return {};
  334. }
  335. ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) const
  336. {
  337. auto callee = interpreter.reg(m_callee);
  338. if (!callee.is_function())
  339. return interpreter.vm().throw_completion<TypeError>(interpreter.global_object(), ErrorType::IsNotA, callee.to_string_without_side_effects(), "function"sv);
  340. auto& function = callee.as_function();
  341. auto this_value = interpreter.reg(m_this_value);
  342. Value return_value;
  343. if (m_argument_count == 0 && m_type == CallType::Call) {
  344. auto return_value_or_error = call(interpreter.global_object(), function, this_value);
  345. if (!return_value_or_error.is_error())
  346. return_value = return_value_or_error.release_value();
  347. } else {
  348. MarkedVector<Value> argument_values { interpreter.vm().heap() };
  349. for (size_t i = 0; i < m_argument_count; ++i)
  350. argument_values.append(interpreter.reg(m_arguments[i]));
  351. if (m_type == CallType::Call)
  352. return_value = TRY(call(interpreter.global_object(), function, this_value, move(argument_values)));
  353. else
  354. return_value = TRY(construct(interpreter.global_object(), function, move(argument_values)));
  355. }
  356. interpreter.accumulator() = return_value;
  357. return {};
  358. }
  359. ThrowCompletionOr<void> NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  360. {
  361. auto& vm = interpreter.vm();
  362. interpreter.accumulator() = ECMAScriptFunctionObject::create(interpreter.global_object(), m_function_node.name(), m_function_node.source_text(), m_function_node.body(), m_function_node.parameters(), m_function_node.function_length(), vm.lexical_environment(), vm.running_execution_context().private_environment, m_function_node.kind(), m_function_node.is_strict_mode(), m_function_node.might_need_arguments_object(), m_function_node.is_arrow_function());
  363. return {};
  364. }
  365. ThrowCompletionOr<void> Return::execute_impl(Bytecode::Interpreter& interpreter) const
  366. {
  367. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  368. return {};
  369. }
  370. ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  371. {
  372. auto old_value = TRY(interpreter.accumulator().to_numeric(interpreter.global_object()));
  373. if (old_value.is_number())
  374. interpreter.accumulator() = Value(old_value.as_double() + 1);
  375. else
  376. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  377. return {};
  378. }
  379. ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  380. {
  381. auto old_value = TRY(interpreter.accumulator().to_numeric(interpreter.global_object()));
  382. if (old_value.is_number())
  383. interpreter.accumulator() = Value(old_value.as_double() - 1);
  384. else
  385. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  386. return {};
  387. }
  388. ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  389. {
  390. return throw_completion(interpreter.accumulator());
  391. }
  392. ThrowCompletionOr<void> EnterUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  393. {
  394. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  395. interpreter.jump(m_entry_point);
  396. return {};
  397. }
  398. void EnterUnwindContext::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  399. {
  400. if (&m_entry_point.block() == &from)
  401. m_entry_point = Label { to };
  402. if (m_handler_target.has_value() && &m_handler_target->block() == &from)
  403. m_handler_target = Label { to };
  404. if (m_finalizer_target.has_value() && &m_finalizer_target->block() == &from)
  405. m_finalizer_target = Label { to };
  406. }
  407. ThrowCompletionOr<void> FinishUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  408. {
  409. interpreter.leave_unwind_context();
  410. interpreter.jump(m_next_target);
  411. return {};
  412. }
  413. void FinishUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  414. {
  415. if (&m_next_target.block() == &from)
  416. m_next_target = Label { to };
  417. }
  418. ThrowCompletionOr<void> LeaveEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  419. {
  420. if (m_mode == EnvironmentMode::Lexical)
  421. interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
  422. if (m_mode == EnvironmentMode::Var)
  423. interpreter.vm().running_execution_context().variable_environment = interpreter.saved_variable_environment_stack().take_last();
  424. return {};
  425. }
  426. ThrowCompletionOr<void> LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  427. {
  428. interpreter.leave_unwind_context();
  429. return {};
  430. }
  431. ThrowCompletionOr<void> ContinuePendingUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  432. {
  433. return interpreter.continue_pending_unwind(m_resume_target);
  434. }
  435. void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  436. {
  437. if (&m_resume_target.block() == &from)
  438. m_resume_target = Label { to };
  439. }
  440. ThrowCompletionOr<void> PushDeclarativeEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  441. {
  442. auto* environment = interpreter.vm().heap().allocate_without_global_object<DeclarativeEnvironment>(interpreter.vm().lexical_environment());
  443. interpreter.vm().running_execution_context().lexical_environment = environment;
  444. interpreter.vm().running_execution_context().variable_environment = environment;
  445. return {};
  446. }
  447. ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  448. {
  449. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  450. auto object = JS::Object::create(interpreter.global_object(), nullptr);
  451. object->define_direct_property("result", yielded_value, JS::default_attributes);
  452. if (m_continuation_label.has_value())
  453. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes);
  454. else
  455. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  456. interpreter.do_return(object);
  457. return {};
  458. }
  459. void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  460. {
  461. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  462. m_continuation_label = Label { to };
  463. }
  464. ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  465. {
  466. auto* object = TRY(interpreter.reg(m_base).to_object(interpreter.global_object()));
  467. auto property_key = TRY(interpreter.accumulator().to_property_key(interpreter.global_object()));
  468. interpreter.accumulator() = TRY(object->get(property_key));
  469. return {};
  470. }
  471. ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  472. {
  473. auto* object = TRY(interpreter.reg(m_base).to_object(interpreter.global_object()));
  474. auto property_key = TRY(interpreter.reg(m_property).to_property_key(interpreter.global_object()));
  475. TRY(object->set(property_key, interpreter.accumulator(), Object::ShouldThrowExceptions::Yes));
  476. return {};
  477. }
  478. ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  479. {
  480. auto iterator = TRY(get_iterator(interpreter.global_object(), interpreter.accumulator()));
  481. interpreter.accumulator() = iterator_to_object(interpreter.global_object(), iterator);
  482. return {};
  483. }
  484. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  485. {
  486. auto* iterator_object = TRY(interpreter.accumulator().to_object(interpreter.global_object()));
  487. auto iterator = object_to_iterator(interpreter.global_object(), *iterator_object);
  488. interpreter.accumulator() = TRY(iterator_next(interpreter.global_object(), iterator));
  489. return {};
  490. }
  491. ThrowCompletionOr<void> IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const
  492. {
  493. auto* iterator_result = TRY(interpreter.accumulator().to_object(interpreter.global_object()));
  494. auto complete = TRY(iterator_complete(interpreter.global_object(), *iterator_result));
  495. interpreter.accumulator() = Value(complete);
  496. return {};
  497. }
  498. ThrowCompletionOr<void> IteratorResultValue::execute_impl(Bytecode::Interpreter& interpreter) const
  499. {
  500. auto* iterator_result = TRY(interpreter.accumulator().to_object(interpreter.global_object()));
  501. interpreter.accumulator() = TRY(iterator_value(interpreter.global_object(), *iterator_result));
  502. return {};
  503. }
  504. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const
  505. {
  506. auto name = m_class_expression.name();
  507. auto scope = interpreter.ast_interpreter_scope();
  508. auto& ast_interpreter = scope.interpreter();
  509. auto class_object = TRY(m_class_expression.class_definition_evaluation(ast_interpreter, interpreter.global_object(), name, name.is_null() ? "" : name));
  510. interpreter.accumulator() = class_object;
  511. return {};
  512. }
  513. String Load::to_string_impl(Bytecode::Executable const&) const
  514. {
  515. return String::formatted("Load {}", m_src);
  516. }
  517. String LoadImmediate::to_string_impl(Bytecode::Executable const&) const
  518. {
  519. return String::formatted("LoadImmediate {}", m_value);
  520. }
  521. String Store::to_string_impl(Bytecode::Executable const&) const
  522. {
  523. return String::formatted("Store {}", m_dst);
  524. }
  525. String NewBigInt::to_string_impl(Bytecode::Executable const&) const
  526. {
  527. return String::formatted("NewBigInt \"{}\"", m_bigint.to_base(10));
  528. }
  529. String NewArray::to_string_impl(Bytecode::Executable const&) const
  530. {
  531. StringBuilder builder;
  532. builder.append("NewArray");
  533. if (m_element_count != 0) {
  534. builder.append(" [");
  535. for (size_t i = 0; i < m_element_count; ++i) {
  536. builder.appendff("{}", m_elements[i]);
  537. if (i != m_element_count - 1)
  538. builder.append(',');
  539. }
  540. builder.append(']');
  541. }
  542. return builder.to_string();
  543. }
  544. String IteratorToArray::to_string_impl(const Bytecode::Executable&) const
  545. {
  546. return "IteratorToArray";
  547. }
  548. String NewString::to_string_impl(Bytecode::Executable const& executable) const
  549. {
  550. return String::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  551. }
  552. String NewObject::to_string_impl(Bytecode::Executable const&) const
  553. {
  554. return "NewObject";
  555. }
  556. String NewRegExp::to_string_impl(Bytecode::Executable const& executable) const
  557. {
  558. return String::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  559. }
  560. String CopyObjectExcludingProperties::to_string_impl(const Bytecode::Executable&) const
  561. {
  562. StringBuilder builder;
  563. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  564. if (m_excluded_names_count != 0) {
  565. builder.append(" excluding:[");
  566. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  567. builder.appendff("{}", m_excluded_names[i]);
  568. if (i != m_excluded_names_count - 1)
  569. builder.append(',');
  570. }
  571. builder.append(']');
  572. }
  573. return builder.to_string();
  574. }
  575. String ConcatString::to_string_impl(Bytecode::Executable const&) const
  576. {
  577. return String::formatted("ConcatString {}", m_lhs);
  578. }
  579. String GetVariable::to_string_impl(Bytecode::Executable const& executable) const
  580. {
  581. return String::formatted("GetVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  582. }
  583. String CreateEnvironment::to_string_impl(Bytecode::Executable const&) const
  584. {
  585. auto mode_string = m_mode == EnvironmentMode::Lexical
  586. ? "Lexical"
  587. : "Variable";
  588. return String::formatted("CreateEnvironment mode:{}", mode_string);
  589. }
  590. String CreateVariable::to_string_impl(Bytecode::Executable const& executable) const
  591. {
  592. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  593. return String::formatted("CreateVariable env:{} immutable:{} {} ({})", mode_string, m_is_immutable, m_identifier, executable.identifier_table->get(m_identifier));
  594. }
  595. String SetVariable::to_string_impl(Bytecode::Executable const& executable) const
  596. {
  597. auto initialization_mode_name = m_initialization_mode == InitializationMode ::Initialize ? "Initialize"
  598. : m_initialization_mode == InitializationMode::Set ? "Set"
  599. : "InitializeOrSet";
  600. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  601. return String::formatted("SetVariable env:{} init:{} {} ({})", mode_string, initialization_mode_name, m_identifier, executable.identifier_table->get(m_identifier));
  602. }
  603. String PutById::to_string_impl(Bytecode::Executable const& executable) const
  604. {
  605. return String::formatted("PutById base:{}, property:{} ({})", m_base, m_property, executable.identifier_table->get(m_property));
  606. }
  607. String GetById::to_string_impl(Bytecode::Executable const& executable) const
  608. {
  609. return String::formatted("GetById {} ({})", m_property, executable.identifier_table->get(m_property));
  610. }
  611. String Jump::to_string_impl(Bytecode::Executable const&) const
  612. {
  613. if (m_true_target.has_value())
  614. return String::formatted("Jump {}", *m_true_target);
  615. return String::formatted("Jump <empty>");
  616. }
  617. String JumpConditional::to_string_impl(Bytecode::Executable const&) const
  618. {
  619. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  620. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  621. return String::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  622. }
  623. String JumpNullish::to_string_impl(Bytecode::Executable const&) const
  624. {
  625. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  626. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  627. return String::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  628. }
  629. String JumpUndefined::to_string_impl(Bytecode::Executable const&) const
  630. {
  631. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  632. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  633. return String::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  634. }
  635. String Call::to_string_impl(Bytecode::Executable const&) const
  636. {
  637. StringBuilder builder;
  638. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  639. if (m_argument_count != 0) {
  640. builder.append(", arguments:[");
  641. for (size_t i = 0; i < m_argument_count; ++i) {
  642. builder.appendff("{}", m_arguments[i]);
  643. if (i != m_argument_count - 1)
  644. builder.append(',');
  645. }
  646. builder.append(']');
  647. }
  648. return builder.to_string();
  649. }
  650. String NewFunction::to_string_impl(Bytecode::Executable const&) const
  651. {
  652. return "NewFunction";
  653. }
  654. String NewClass::to_string_impl(Bytecode::Executable const&) const
  655. {
  656. return "NewClass";
  657. }
  658. String Return::to_string_impl(Bytecode::Executable const&) const
  659. {
  660. return "Return";
  661. }
  662. String Increment::to_string_impl(Bytecode::Executable const&) const
  663. {
  664. return "Increment";
  665. }
  666. String Decrement::to_string_impl(Bytecode::Executable const&) const
  667. {
  668. return "Decrement";
  669. }
  670. String Throw::to_string_impl(Bytecode::Executable const&) const
  671. {
  672. return "Throw";
  673. }
  674. String EnterUnwindContext::to_string_impl(Bytecode::Executable const&) const
  675. {
  676. auto handler_string = m_handler_target.has_value() ? String::formatted("{}", *m_handler_target) : "<empty>";
  677. auto finalizer_string = m_finalizer_target.has_value() ? String::formatted("{}", *m_finalizer_target) : "<empty>";
  678. return String::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  679. }
  680. String FinishUnwind::to_string_impl(const Bytecode::Executable&) const
  681. {
  682. return String::formatted("FinishUnwind next:{}", m_next_target);
  683. }
  684. String LeaveEnvironment::to_string_impl(Bytecode::Executable const&) const
  685. {
  686. auto mode_string = m_mode == EnvironmentMode::Lexical
  687. ? "Lexical"
  688. : "Variable";
  689. return String::formatted("LeaveEnvironment env:{}", mode_string);
  690. }
  691. String LeaveUnwindContext::to_string_impl(Bytecode::Executable const&) const
  692. {
  693. return "LeaveUnwindContext";
  694. }
  695. String ContinuePendingUnwind::to_string_impl(Bytecode::Executable const&) const
  696. {
  697. return String::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  698. }
  699. String PushDeclarativeEnvironment::to_string_impl(const Bytecode::Executable& executable) const
  700. {
  701. StringBuilder builder;
  702. builder.append("PushDeclarativeEnvironment");
  703. if (!m_variables.is_empty()) {
  704. builder.append(" {");
  705. Vector<String> names;
  706. for (auto& it : m_variables)
  707. names.append(executable.get_string(it.key));
  708. builder.join(", ", names);
  709. builder.append("}");
  710. }
  711. return builder.to_string();
  712. }
  713. String Yield::to_string_impl(Bytecode::Executable const&) const
  714. {
  715. if (m_continuation_label.has_value())
  716. return String::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  717. return String::formatted("Yield return");
  718. }
  719. String GetByValue::to_string_impl(const Bytecode::Executable&) const
  720. {
  721. return String::formatted("GetByValue base:{}", m_base);
  722. }
  723. String PutByValue::to_string_impl(const Bytecode::Executable&) const
  724. {
  725. return String::formatted("PutByValue base:{}, property:{}", m_base, m_property);
  726. }
  727. String GetIterator::to_string_impl(Executable const&) const
  728. {
  729. return "GetIterator";
  730. }
  731. String IteratorNext::to_string_impl(Executable const&) const
  732. {
  733. return "IteratorNext";
  734. }
  735. String IteratorResultDone::to_string_impl(Executable const&) const
  736. {
  737. return "IteratorResultDone";
  738. }
  739. String IteratorResultValue::to_string_impl(Executable const&) const
  740. {
  741. return "IteratorResultValue";
  742. }
  743. String ResolveThisBinding::to_string_impl(Bytecode::Executable const&) const
  744. {
  745. return "ResolveThisBinding"sv;
  746. }
  747. }