Op.cpp 34 KB

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