Op.cpp 34 KB

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