Op.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. MarkedVector<Value> argument_values { interpreter.vm().heap() };
  343. for (size_t i = 0; i < m_argument_count; ++i)
  344. argument_values.append(interpreter.reg(m_arguments[i]));
  345. Value return_value;
  346. if (m_type == CallType::Call)
  347. return_value = TRY(call(interpreter.global_object(), function, this_value, move(argument_values)));
  348. else
  349. return_value = TRY(construct(interpreter.global_object(), function, move(argument_values)));
  350. interpreter.accumulator() = return_value;
  351. return {};
  352. }
  353. ThrowCompletionOr<void> NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  354. {
  355. auto& vm = interpreter.vm();
  356. 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());
  357. return {};
  358. }
  359. ThrowCompletionOr<void> Return::execute_impl(Bytecode::Interpreter& interpreter) const
  360. {
  361. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  362. return {};
  363. }
  364. ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  365. {
  366. auto old_value = TRY(interpreter.accumulator().to_numeric(interpreter.global_object()));
  367. if (old_value.is_number())
  368. interpreter.accumulator() = Value(old_value.as_double() + 1);
  369. else
  370. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  371. return {};
  372. }
  373. ThrowCompletionOr<void> Decrement::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().minus(Crypto::SignedBigInteger { 1 }));
  380. return {};
  381. }
  382. ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  383. {
  384. return throw_completion(interpreter.accumulator());
  385. }
  386. ThrowCompletionOr<void> EnterUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  387. {
  388. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  389. interpreter.jump(m_entry_point);
  390. return {};
  391. }
  392. void EnterUnwindContext::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  393. {
  394. if (&m_entry_point.block() == &from)
  395. m_entry_point = Label { to };
  396. if (m_handler_target.has_value() && &m_handler_target->block() == &from)
  397. m_handler_target = Label { to };
  398. if (m_finalizer_target.has_value() && &m_finalizer_target->block() == &from)
  399. m_finalizer_target = Label { to };
  400. }
  401. ThrowCompletionOr<void> FinishUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  402. {
  403. interpreter.leave_unwind_context();
  404. interpreter.jump(m_next_target);
  405. return {};
  406. }
  407. void FinishUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  408. {
  409. if (&m_next_target.block() == &from)
  410. m_next_target = Label { to };
  411. }
  412. ThrowCompletionOr<void> LeaveEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  413. {
  414. if (m_mode == EnvironmentMode::Lexical)
  415. interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
  416. if (m_mode == EnvironmentMode::Var)
  417. interpreter.vm().running_execution_context().variable_environment = interpreter.saved_variable_environment_stack().take_last();
  418. return {};
  419. }
  420. ThrowCompletionOr<void> LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  421. {
  422. interpreter.leave_unwind_context();
  423. return {};
  424. }
  425. ThrowCompletionOr<void> ContinuePendingUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  426. {
  427. return interpreter.continue_pending_unwind(m_resume_target);
  428. }
  429. void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  430. {
  431. if (&m_resume_target.block() == &from)
  432. m_resume_target = Label { to };
  433. }
  434. ThrowCompletionOr<void> PushDeclarativeEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  435. {
  436. auto* environment = interpreter.vm().heap().allocate_without_global_object<DeclarativeEnvironment>(interpreter.vm().lexical_environment());
  437. interpreter.vm().running_execution_context().lexical_environment = environment;
  438. interpreter.vm().running_execution_context().variable_environment = environment;
  439. return {};
  440. }
  441. ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  442. {
  443. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  444. auto object = JS::Object::create(interpreter.global_object(), nullptr);
  445. object->define_direct_property("result", yielded_value, JS::default_attributes);
  446. if (m_continuation_label.has_value())
  447. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes);
  448. else
  449. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  450. interpreter.do_return(object);
  451. return {};
  452. }
  453. void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  454. {
  455. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  456. m_continuation_label = Label { to };
  457. }
  458. ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  459. {
  460. auto* object = TRY(interpreter.reg(m_base).to_object(interpreter.global_object()));
  461. auto property_key = TRY(interpreter.accumulator().to_property_key(interpreter.global_object()));
  462. interpreter.accumulator() = TRY(object->get(property_key));
  463. return {};
  464. }
  465. ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  466. {
  467. auto* object = TRY(interpreter.reg(m_base).to_object(interpreter.global_object()));
  468. auto property_key = TRY(interpreter.reg(m_property).to_property_key(interpreter.global_object()));
  469. TRY(object->set(property_key, interpreter.accumulator(), Object::ShouldThrowExceptions::Yes));
  470. return {};
  471. }
  472. ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  473. {
  474. auto iterator = TRY(get_iterator(interpreter.global_object(), interpreter.accumulator()));
  475. interpreter.accumulator() = iterator_to_object(interpreter.global_object(), iterator);
  476. return {};
  477. }
  478. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  479. {
  480. auto* iterator_object = TRY(interpreter.accumulator().to_object(interpreter.global_object()));
  481. auto iterator = object_to_iterator(interpreter.global_object(), *iterator_object);
  482. interpreter.accumulator() = TRY(iterator_next(interpreter.global_object(), iterator));
  483. return {};
  484. }
  485. ThrowCompletionOr<void> IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const
  486. {
  487. auto* iterator_result = TRY(interpreter.accumulator().to_object(interpreter.global_object()));
  488. auto complete = TRY(iterator_complete(interpreter.global_object(), *iterator_result));
  489. interpreter.accumulator() = Value(complete);
  490. return {};
  491. }
  492. ThrowCompletionOr<void> IteratorResultValue::execute_impl(Bytecode::Interpreter& interpreter) const
  493. {
  494. auto* iterator_result = TRY(interpreter.accumulator().to_object(interpreter.global_object()));
  495. interpreter.accumulator() = TRY(iterator_value(interpreter.global_object(), *iterator_result));
  496. return {};
  497. }
  498. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const
  499. {
  500. auto name = m_class_expression.name();
  501. auto scope = interpreter.ast_interpreter_scope();
  502. auto& ast_interpreter = scope.interpreter();
  503. auto class_object = TRY(m_class_expression.class_definition_evaluation(ast_interpreter, interpreter.global_object(), name, name.is_null() ? "" : name));
  504. interpreter.accumulator() = class_object;
  505. return {};
  506. }
  507. String Load::to_string_impl(Bytecode::Executable const&) const
  508. {
  509. return String::formatted("Load {}", m_src);
  510. }
  511. String LoadImmediate::to_string_impl(Bytecode::Executable const&) const
  512. {
  513. return String::formatted("LoadImmediate {}", m_value);
  514. }
  515. String Store::to_string_impl(Bytecode::Executable const&) const
  516. {
  517. return String::formatted("Store {}", m_dst);
  518. }
  519. String NewBigInt::to_string_impl(Bytecode::Executable const&) const
  520. {
  521. return String::formatted("NewBigInt \"{}\"", m_bigint.to_base(10));
  522. }
  523. String NewArray::to_string_impl(Bytecode::Executable const&) const
  524. {
  525. StringBuilder builder;
  526. builder.append("NewArray");
  527. if (m_element_count != 0) {
  528. builder.append(" [");
  529. for (size_t i = 0; i < m_element_count; ++i) {
  530. builder.appendff("{}", m_elements[i]);
  531. if (i != m_element_count - 1)
  532. builder.append(',');
  533. }
  534. builder.append(']');
  535. }
  536. return builder.to_string();
  537. }
  538. String IteratorToArray::to_string_impl(const Bytecode::Executable&) const
  539. {
  540. return "IteratorToArray";
  541. }
  542. String NewString::to_string_impl(Bytecode::Executable const& executable) const
  543. {
  544. return String::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  545. }
  546. String NewObject::to_string_impl(Bytecode::Executable const&) const
  547. {
  548. return "NewObject";
  549. }
  550. String NewRegExp::to_string_impl(Bytecode::Executable const& executable) const
  551. {
  552. return String::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  553. }
  554. String CopyObjectExcludingProperties::to_string_impl(const Bytecode::Executable&) const
  555. {
  556. StringBuilder builder;
  557. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  558. if (m_excluded_names_count != 0) {
  559. builder.append(" excluding:[");
  560. for (size_t i = 0; i < m_excluded_names_count; ++i) {
  561. builder.appendff("{}", m_excluded_names[i]);
  562. if (i != m_excluded_names_count - 1)
  563. builder.append(',');
  564. }
  565. builder.append(']');
  566. }
  567. return builder.to_string();
  568. }
  569. String ConcatString::to_string_impl(Bytecode::Executable const&) const
  570. {
  571. return String::formatted("ConcatString {}", m_lhs);
  572. }
  573. String GetVariable::to_string_impl(Bytecode::Executable const& executable) const
  574. {
  575. return String::formatted("GetVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  576. }
  577. String CreateEnvironment::to_string_impl(Bytecode::Executable const&) const
  578. {
  579. auto mode_string = m_mode == EnvironmentMode::Lexical
  580. ? "Lexical"
  581. : "Variable";
  582. return String::formatted("CreateEnvironment mode:{}", mode_string);
  583. }
  584. String CreateVariable::to_string_impl(Bytecode::Executable const& executable) const
  585. {
  586. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  587. return String::formatted("CreateVariable env:{} immutable:{} {} ({})", mode_string, m_is_immutable, m_identifier, executable.identifier_table->get(m_identifier));
  588. }
  589. String SetVariable::to_string_impl(Bytecode::Executable const& executable) const
  590. {
  591. auto initialization_mode_name = m_initialization_mode == InitializationMode ::Initialize ? "Initialize"
  592. : m_initialization_mode == InitializationMode::Set ? "Set"
  593. : "InitializeOrSet";
  594. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  595. return String::formatted("SetVariable env:{} init:{} {} ({})", mode_string, initialization_mode_name, m_identifier, executable.identifier_table->get(m_identifier));
  596. }
  597. String PutById::to_string_impl(Bytecode::Executable const& executable) const
  598. {
  599. return String::formatted("PutById base:{}, property:{} ({})", m_base, m_property, executable.identifier_table->get(m_property));
  600. }
  601. String GetById::to_string_impl(Bytecode::Executable const& executable) const
  602. {
  603. return String::formatted("GetById {} ({})", m_property, executable.identifier_table->get(m_property));
  604. }
  605. String Jump::to_string_impl(Bytecode::Executable const&) const
  606. {
  607. if (m_true_target.has_value())
  608. return String::formatted("Jump {}", *m_true_target);
  609. return String::formatted("Jump <empty>");
  610. }
  611. String JumpConditional::to_string_impl(Bytecode::Executable const&) const
  612. {
  613. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  614. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  615. return String::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  616. }
  617. String JumpNullish::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("JumpNullish null:{} nonnull:{}", true_string, false_string);
  622. }
  623. String JumpUndefined::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("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  628. }
  629. String Call::to_string_impl(Bytecode::Executable const&) const
  630. {
  631. StringBuilder builder;
  632. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  633. if (m_argument_count != 0) {
  634. builder.append(", arguments:[");
  635. for (size_t i = 0; i < m_argument_count; ++i) {
  636. builder.appendff("{}", m_arguments[i]);
  637. if (i != m_argument_count - 1)
  638. builder.append(',');
  639. }
  640. builder.append(']');
  641. }
  642. return builder.to_string();
  643. }
  644. String NewFunction::to_string_impl(Bytecode::Executable const&) const
  645. {
  646. return "NewFunction";
  647. }
  648. String NewClass::to_string_impl(Bytecode::Executable const&) const
  649. {
  650. return "NewClass";
  651. }
  652. String Return::to_string_impl(Bytecode::Executable const&) const
  653. {
  654. return "Return";
  655. }
  656. String Increment::to_string_impl(Bytecode::Executable const&) const
  657. {
  658. return "Increment";
  659. }
  660. String Decrement::to_string_impl(Bytecode::Executable const&) const
  661. {
  662. return "Decrement";
  663. }
  664. String Throw::to_string_impl(Bytecode::Executable const&) const
  665. {
  666. return "Throw";
  667. }
  668. String EnterUnwindContext::to_string_impl(Bytecode::Executable const&) const
  669. {
  670. auto handler_string = m_handler_target.has_value() ? String::formatted("{}", *m_handler_target) : "<empty>";
  671. auto finalizer_string = m_finalizer_target.has_value() ? String::formatted("{}", *m_finalizer_target) : "<empty>";
  672. return String::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  673. }
  674. String FinishUnwind::to_string_impl(const Bytecode::Executable&) const
  675. {
  676. return String::formatted("FinishUnwind next:{}", m_next_target);
  677. }
  678. String LeaveEnvironment::to_string_impl(Bytecode::Executable const&) const
  679. {
  680. auto mode_string = m_mode == EnvironmentMode::Lexical
  681. ? "Lexical"
  682. : "Variable";
  683. return String::formatted("LeaveEnvironment env:{}", mode_string);
  684. }
  685. String LeaveUnwindContext::to_string_impl(Bytecode::Executable const&) const
  686. {
  687. return "LeaveUnwindContext";
  688. }
  689. String ContinuePendingUnwind::to_string_impl(Bytecode::Executable const&) const
  690. {
  691. return String::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  692. }
  693. String PushDeclarativeEnvironment::to_string_impl(const Bytecode::Executable& executable) const
  694. {
  695. StringBuilder builder;
  696. builder.append("PushDeclarativeEnvironment");
  697. if (!m_variables.is_empty()) {
  698. builder.append(" {");
  699. Vector<String> names;
  700. for (auto& it : m_variables)
  701. names.append(executable.get_string(it.key));
  702. builder.join(", ", names);
  703. builder.append("}");
  704. }
  705. return builder.to_string();
  706. }
  707. String Yield::to_string_impl(Bytecode::Executable const&) const
  708. {
  709. if (m_continuation_label.has_value())
  710. return String::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  711. return String::formatted("Yield return");
  712. }
  713. String GetByValue::to_string_impl(const Bytecode::Executable&) const
  714. {
  715. return String::formatted("GetByValue base:{}", m_base);
  716. }
  717. String PutByValue::to_string_impl(const Bytecode::Executable&) const
  718. {
  719. return String::formatted("PutByValue base:{}, property:{}", m_base, m_property);
  720. }
  721. String GetIterator::to_string_impl(Executable const&) const
  722. {
  723. return "GetIterator";
  724. }
  725. String IteratorNext::to_string_impl(Executable const&) const
  726. {
  727. return "IteratorNext";
  728. }
  729. String IteratorResultDone::to_string_impl(Executable const&) const
  730. {
  731. return "IteratorResultDone";
  732. }
  733. String IteratorResultValue::to_string_impl(Executable const&) const
  734. {
  735. return "IteratorResultValue";
  736. }
  737. String ResolveThisBinding::to_string_impl(Bytecode::Executable const&) const
  738. {
  739. return "ResolveThisBinding"sv;
  740. }
  741. }