Op.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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/FunctionEnvironment.h>
  18. #include <LibJS/Runtime/GlobalEnvironment.h>
  19. #include <LibJS/Runtime/GlobalObject.h>
  20. #include <LibJS/Runtime/Iterator.h>
  21. #include <LibJS/Runtime/IteratorOperations.h>
  22. #include <LibJS/Runtime/NativeFunction.h>
  23. #include <LibJS/Runtime/ObjectEnvironment.h>
  24. #include <LibJS/Runtime/RegExpObject.h>
  25. #include <LibJS/Runtime/Value.h>
  26. namespace JS::Bytecode {
  27. String Instruction::to_string(Bytecode::Executable const& executable) const
  28. {
  29. #define __BYTECODE_OP(op) \
  30. case Instruction::Type::op: \
  31. return static_cast<Bytecode::Op::op const&>(*this).to_string_impl(executable);
  32. switch (type()) {
  33. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  34. default:
  35. VERIFY_NOT_REACHED();
  36. }
  37. #undef __BYTECODE_OP
  38. }
  39. }
  40. namespace JS::Bytecode::Op {
  41. static ThrowCompletionOr<void> put_by_property_key(Object* object, Value value, PropertyKey name, Bytecode::Interpreter& interpreter, PropertyKind kind)
  42. {
  43. auto& vm = interpreter.vm();
  44. if (kind == PropertyKind::Getter || kind == PropertyKind::Setter) {
  45. // The generator should only pass us functions for getters and setters.
  46. VERIFY(value.is_function());
  47. }
  48. switch (kind) {
  49. case PropertyKind::Getter: {
  50. auto& function = value.as_function();
  51. if (function.name().is_empty() && is<ECMAScriptFunctionObject>(function))
  52. static_cast<ECMAScriptFunctionObject*>(&function)->set_name(String::formatted("get {}", name));
  53. object->define_direct_accessor(name, &function, nullptr, Attribute::Configurable | Attribute::Enumerable);
  54. break;
  55. }
  56. case PropertyKind::Setter: {
  57. auto& function = value.as_function();
  58. if (function.name().is_empty() && is<ECMAScriptFunctionObject>(function))
  59. static_cast<ECMAScriptFunctionObject*>(&function)->set_name(String::formatted("set {}", name));
  60. object->define_direct_accessor(name, nullptr, &function, Attribute::Configurable | Attribute::Enumerable);
  61. break;
  62. }
  63. case PropertyKind::KeyValue: {
  64. bool succeeded = TRY(object->internal_set(name, interpreter.accumulator(), object));
  65. if (!succeeded && vm.in_strict_mode())
  66. return vm.throw_completion<TypeError>(ErrorType::ReferenceNullishSetProperty, name, interpreter.accumulator().to_string_without_side_effects());
  67. break;
  68. }
  69. case PropertyKind::Spread:
  70. TRY(object->copy_data_properties(vm, value, {}));
  71. break;
  72. case PropertyKind::ProtoSetter:
  73. if (value.is_object() || value.is_null())
  74. MUST(object->internal_set_prototype_of(value.is_object() ? &value.as_object() : nullptr));
  75. break;
  76. }
  77. return {};
  78. }
  79. ThrowCompletionOr<void> Load::execute_impl(Bytecode::Interpreter& interpreter) const
  80. {
  81. interpreter.accumulator() = interpreter.reg(m_src);
  82. return {};
  83. }
  84. ThrowCompletionOr<void> LoadImmediate::execute_impl(Bytecode::Interpreter& interpreter) const
  85. {
  86. interpreter.accumulator() = m_value;
  87. return {};
  88. }
  89. ThrowCompletionOr<void> Store::execute_impl(Bytecode::Interpreter& interpreter) const
  90. {
  91. interpreter.reg(m_dst) = interpreter.accumulator();
  92. return {};
  93. }
  94. static ThrowCompletionOr<Value> abstract_inequals(VM& vm, Value src1, Value src2)
  95. {
  96. return Value(!TRY(is_loosely_equal(vm, src1, src2)));
  97. }
  98. static ThrowCompletionOr<Value> abstract_equals(VM& vm, Value src1, Value src2)
  99. {
  100. return Value(TRY(is_loosely_equal(vm, src1, src2)));
  101. }
  102. static ThrowCompletionOr<Value> typed_inequals(VM&, Value src1, Value src2)
  103. {
  104. return Value(!is_strictly_equal(src1, src2));
  105. }
  106. static ThrowCompletionOr<Value> typed_equals(VM&, Value src1, Value src2)
  107. {
  108. return Value(is_strictly_equal(src1, src2));
  109. }
  110. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  111. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  112. { \
  113. auto& vm = interpreter.vm(); \
  114. auto lhs = interpreter.reg(m_lhs_reg); \
  115. auto rhs = interpreter.accumulator(); \
  116. interpreter.accumulator() = TRY(op_snake_case(vm, lhs, rhs)); \
  117. return {}; \
  118. } \
  119. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  120. { \
  121. return String::formatted(#OpTitleCase " {}", m_lhs_reg); \
  122. }
  123. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  124. static ThrowCompletionOr<Value> not_(VM&, Value value)
  125. {
  126. return Value(!value.to_boolean());
  127. }
  128. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  129. {
  130. return Value(js_string(vm, value.typeof()));
  131. }
  132. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  133. ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  134. { \
  135. auto& vm = interpreter.vm(); \
  136. interpreter.accumulator() = TRY(op_snake_case(vm, interpreter.accumulator())); \
  137. return {}; \
  138. } \
  139. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  140. { \
  141. return #OpTitleCase; \
  142. }
  143. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  144. ThrowCompletionOr<void> NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
  145. {
  146. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), m_bigint);
  147. return {};
  148. }
  149. ThrowCompletionOr<void> NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  150. {
  151. auto* array = MUST(Array::create(interpreter.realm(), 0));
  152. for (size_t i = 0; i < m_element_count; i++) {
  153. auto& value = interpreter.reg(Register(m_elements[0].index() + i));
  154. array->indexed_properties().put(i, value, default_attributes);
  155. }
  156. interpreter.accumulator() = array;
  157. return {};
  158. }
  159. // 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.
  160. // Make sure to put this into the accumulator before the iterator object disappears from the stack to prevent the members from being GC'd.
  161. static Object* iterator_to_object(VM& vm, Iterator iterator)
  162. {
  163. auto& realm = *vm.current_realm();
  164. auto* object = Object::create(realm, nullptr);
  165. object->define_direct_property(vm.names.iterator, iterator.iterator, 0);
  166. object->define_direct_property(vm.names.next, iterator.next_method, 0);
  167. object->define_direct_property(vm.names.done, Value(iterator.done), 0);
  168. return object;
  169. }
  170. static Iterator object_to_iterator(VM& vm, Object& object)
  171. {
  172. return Iterator {
  173. .iterator = &MUST(object.get(vm.names.iterator)).as_object(),
  174. .next_method = MUST(object.get(vm.names.next)),
  175. .done = MUST(object.get(vm.names.done)).as_bool()
  176. };
  177. }
  178. ThrowCompletionOr<void> IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const
  179. {
  180. auto& vm = interpreter.vm();
  181. auto iterator_object = TRY(interpreter.accumulator().to_object(vm));
  182. auto iterator = object_to_iterator(vm, *iterator_object);
  183. auto* array = MUST(Array::create(interpreter.realm(), 0));
  184. size_t index = 0;
  185. while (true) {
  186. auto* iterator_result = TRY(iterator_next(vm, iterator));
  187. auto complete = TRY(iterator_complete(vm, *iterator_result));
  188. if (complete) {
  189. interpreter.accumulator() = array;
  190. return {};
  191. }
  192. auto value = TRY(iterator_value(vm, *iterator_result));
  193. MUST(array->create_data_property_or_throw(index, value));
  194. index++;
  195. }
  196. return {};
  197. }
  198. ThrowCompletionOr<void> NewString::execute_impl(Bytecode::Interpreter& interpreter) const
  199. {
  200. interpreter.accumulator() = js_string(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  201. return {};
  202. }
  203. ThrowCompletionOr<void> NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  204. {
  205. auto& vm = interpreter.vm();
  206. auto& realm = *vm.current_realm();
  207. interpreter.accumulator() = Object::create(realm, realm.intrinsics().object_prototype());
  208. return {};
  209. }
  210. ThrowCompletionOr<void> NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
  211. {
  212. auto& vm = interpreter.vm();
  213. auto source = interpreter.current_executable().get_string(m_source_index);
  214. auto flags = interpreter.current_executable().get_string(m_flags_index);
  215. interpreter.accumulator() = TRY(regexp_create(vm, js_string(vm, source), js_string(vm, flags)));
  216. return {};
  217. }
  218. ThrowCompletionOr<void> CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const
  219. {
  220. auto& vm = interpreter.vm();
  221. auto& realm = *vm.current_realm();
  222. auto* from_object = TRY(interpreter.reg(m_from_object).to_object(vm));
  223. auto* to_object = Object::create(realm, realm.intrinsics().object_prototype());
  224. HashTable<Value, ValueTraits> excluded_names;
  225. for (size_t i = 0; i < m_excluded_names_count; ++i)
  226. excluded_names.set(interpreter.reg(m_excluded_names[i]));
  227. auto own_keys = TRY(from_object->internal_own_property_keys());
  228. for (auto& key : own_keys) {
  229. if (!excluded_names.contains(key)) {
  230. auto property_key = TRY(key.to_property_key(vm));
  231. auto property_value = TRY(from_object->get(property_key));
  232. to_object->define_direct_property(property_key, property_value, JS::default_attributes);
  233. }
  234. }
  235. interpreter.accumulator() = to_object;
  236. return {};
  237. }
  238. ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  239. {
  240. auto& vm = interpreter.vm();
  241. interpreter.reg(m_lhs) = TRY(add(vm, interpreter.reg(m_lhs), interpreter.accumulator()));
  242. return {};
  243. }
  244. ThrowCompletionOr<void> GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  245. {
  246. auto& vm = interpreter.vm();
  247. auto get_reference = [&]() -> ThrowCompletionOr<Reference> {
  248. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  249. if (m_cached_environment_coordinate.has_value()) {
  250. auto* environment = vm.running_execution_context().lexical_environment;
  251. for (size_t i = 0; i < m_cached_environment_coordinate->hops; ++i)
  252. environment = environment->outer_environment();
  253. VERIFY(environment);
  254. VERIFY(environment->is_declarative_environment());
  255. if (!environment->is_permanently_screwed_by_eval()) {
  256. return Reference { *environment, string, vm.in_strict_mode(), m_cached_environment_coordinate };
  257. }
  258. m_cached_environment_coordinate = {};
  259. }
  260. auto reference = TRY(vm.resolve_binding(string));
  261. if (reference.environment_coordinate().has_value())
  262. m_cached_environment_coordinate = reference.environment_coordinate();
  263. return reference;
  264. };
  265. auto reference = TRY(get_reference());
  266. interpreter.accumulator() = TRY(reference.get_value(vm));
  267. return {};
  268. }
  269. ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  270. {
  271. auto& vm = interpreter.vm();
  272. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  273. auto reference = TRY(vm.resolve_binding(string));
  274. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  275. return {};
  276. }
  277. ThrowCompletionOr<void> CreateEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  278. {
  279. auto make_and_swap_envs = [&](auto*& old_environment) {
  280. Environment* environment = new_declarative_environment(*old_environment);
  281. swap(old_environment, environment);
  282. return environment;
  283. };
  284. if (m_mode == EnvironmentMode::Lexical)
  285. interpreter.saved_lexical_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().lexical_environment));
  286. else if (m_mode == EnvironmentMode::Var)
  287. interpreter.saved_variable_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().variable_environment));
  288. return {};
  289. }
  290. ThrowCompletionOr<void> EnterObjectEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  291. {
  292. auto& vm = interpreter.vm();
  293. auto& old_environment = vm.running_execution_context().lexical_environment;
  294. interpreter.saved_lexical_environment_stack().append(old_environment);
  295. auto object = TRY(interpreter.accumulator().to_object(vm));
  296. vm.running_execution_context().lexical_environment = new_object_environment(*object, true, old_environment);
  297. return {};
  298. }
  299. ThrowCompletionOr<void> CreateVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  300. {
  301. auto& vm = interpreter.vm();
  302. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  303. if (m_mode == EnvironmentMode::Lexical) {
  304. VERIFY(!m_is_global);
  305. // Note: This is papering over an issue where "FunctionDeclarationInstantiation" creates these bindings for us.
  306. // Instead of crashing in there, we'll just raise an exception here.
  307. if (TRY(vm.lexical_environment()->has_binding(name)))
  308. return vm.throw_completion<InternalError>(String::formatted("Lexical environment already has binding '{}'", name));
  309. if (m_is_immutable)
  310. vm.lexical_environment()->create_immutable_binding(vm, name, vm.in_strict_mode());
  311. else
  312. vm.lexical_environment()->create_mutable_binding(vm, name, vm.in_strict_mode());
  313. } else {
  314. if (!m_is_global) {
  315. if (m_is_immutable)
  316. vm.variable_environment()->create_immutable_binding(vm, name, vm.in_strict_mode());
  317. else
  318. vm.variable_environment()->create_mutable_binding(vm, name, vm.in_strict_mode());
  319. } else {
  320. // NOTE: CreateVariable with m_is_global set to true is expected to only be used in GlobalDeclarationInstantiation currently, which only uses "false" for "can_be_deleted".
  321. // The only area that sets "can_be_deleted" to true is EvalDeclarationInstantiation, which is currently fully implemented in C++ and not in Bytecode.
  322. verify_cast<GlobalEnvironment>(vm.variable_environment())->create_global_var_binding(name, false);
  323. }
  324. }
  325. return {};
  326. }
  327. ThrowCompletionOr<void> SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  328. {
  329. auto& vm = interpreter.vm();
  330. auto const& name = interpreter.current_executable().get_identifier(m_identifier);
  331. auto environment = m_mode == EnvironmentMode::Lexical ? vm.running_execution_context().lexical_environment : vm.running_execution_context().variable_environment;
  332. auto reference = TRY(vm.resolve_binding(name, environment));
  333. switch (m_initialization_mode) {
  334. case InitializationMode::Initialize:
  335. TRY(reference.initialize_referenced_binding(vm, interpreter.accumulator()));
  336. break;
  337. case InitializationMode::Set:
  338. TRY(reference.put_value(vm, interpreter.accumulator()));
  339. break;
  340. case InitializationMode::InitializeOrSet:
  341. VERIFY(reference.is_environment_reference());
  342. VERIFY(reference.base_environment().is_declarative_environment());
  343. TRY(static_cast<DeclarativeEnvironment&>(reference.base_environment()).initialize_or_set_mutable_binding(vm, name, interpreter.accumulator()));
  344. break;
  345. }
  346. return {};
  347. }
  348. ThrowCompletionOr<void> GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  349. {
  350. auto& vm = interpreter.vm();
  351. auto* object = TRY(interpreter.accumulator().to_object(vm));
  352. interpreter.accumulator() = TRY(object->get(interpreter.current_executable().get_identifier(m_property)));
  353. return {};
  354. }
  355. ThrowCompletionOr<void> PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  356. {
  357. auto& vm = interpreter.vm();
  358. auto* object = TRY(interpreter.reg(m_base).to_object(vm));
  359. PropertyKey name = interpreter.current_executable().get_identifier(m_property);
  360. auto value = interpreter.accumulator();
  361. return put_by_property_key(object, value, name, interpreter, m_kind);
  362. }
  363. ThrowCompletionOr<void> DeleteById::execute_impl(Bytecode::Interpreter& interpreter) const
  364. {
  365. auto& vm = interpreter.vm();
  366. auto* object = TRY(interpreter.accumulator().to_object(vm));
  367. auto const& identifier = interpreter.current_executable().get_identifier(m_property);
  368. bool strict = vm.in_strict_mode();
  369. auto reference = Reference { object, identifier, {}, strict };
  370. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  371. return {};
  372. };
  373. ThrowCompletionOr<void> Jump::execute_impl(Bytecode::Interpreter& interpreter) const
  374. {
  375. interpreter.jump(*m_true_target);
  376. return {};
  377. }
  378. ThrowCompletionOr<void> ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const
  379. {
  380. auto& vm = interpreter.vm();
  381. interpreter.accumulator() = TRY(vm.resolve_this_binding());
  382. return {};
  383. }
  384. ThrowCompletionOr<void> GetNewTarget::execute_impl(Bytecode::Interpreter& interpreter) const
  385. {
  386. interpreter.accumulator() = interpreter.vm().get_new_target();
  387. return {};
  388. }
  389. void Jump::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  390. {
  391. if (m_true_target.has_value() && &m_true_target->block() == &from)
  392. m_true_target = Label { to };
  393. if (m_false_target.has_value() && &m_false_target->block() == &from)
  394. m_false_target = Label { to };
  395. }
  396. ThrowCompletionOr<void> JumpConditional::execute_impl(Bytecode::Interpreter& interpreter) const
  397. {
  398. VERIFY(m_true_target.has_value());
  399. VERIFY(m_false_target.has_value());
  400. auto result = interpreter.accumulator();
  401. if (result.to_boolean())
  402. interpreter.jump(m_true_target.value());
  403. else
  404. interpreter.jump(m_false_target.value());
  405. return {};
  406. }
  407. ThrowCompletionOr<void> JumpNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  408. {
  409. VERIFY(m_true_target.has_value());
  410. VERIFY(m_false_target.has_value());
  411. auto result = interpreter.accumulator();
  412. if (result.is_nullish())
  413. interpreter.jump(m_true_target.value());
  414. else
  415. interpreter.jump(m_false_target.value());
  416. return {};
  417. }
  418. ThrowCompletionOr<void> JumpUndefined::execute_impl(Bytecode::Interpreter& interpreter) const
  419. {
  420. VERIFY(m_true_target.has_value());
  421. VERIFY(m_false_target.has_value());
  422. auto result = interpreter.accumulator();
  423. if (result.is_undefined())
  424. interpreter.jump(m_true_target.value());
  425. else
  426. interpreter.jump(m_false_target.value());
  427. return {};
  428. }
  429. ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) const
  430. {
  431. auto& vm = interpreter.vm();
  432. auto callee = interpreter.reg(m_callee);
  433. if (m_type == CallType::Call && !callee.is_function())
  434. return vm.throw_completion<TypeError>(ErrorType::IsNotA, callee.to_string_without_side_effects(), "function"sv);
  435. if (m_type == CallType::Construct && !callee.is_constructor())
  436. return vm.throw_completion<TypeError>(ErrorType::IsNotA, callee.to_string_without_side_effects(), "constructor"sv);
  437. auto& function = callee.as_function();
  438. auto this_value = interpreter.reg(m_this_value);
  439. MarkedVector<Value> argument_values { vm.heap() };
  440. for (size_t i = 0; i < m_argument_count; ++i)
  441. argument_values.append(interpreter.reg(m_arguments[i]));
  442. Value return_value;
  443. if (m_type == CallType::Call)
  444. return_value = TRY(call(vm, function, this_value, move(argument_values)));
  445. else
  446. return_value = TRY(construct(vm, function, move(argument_values)));
  447. interpreter.accumulator() = return_value;
  448. return {};
  449. }
  450. // 13.3.7.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  451. ThrowCompletionOr<void> SuperCall::execute_impl(Bytecode::Interpreter& interpreter) const
  452. {
  453. auto& vm = interpreter.vm();
  454. // 1. Let newTarget be GetNewTarget().
  455. auto new_target = vm.get_new_target();
  456. // 2. Assert: Type(newTarget) is Object.
  457. VERIFY(new_target.is_object());
  458. // 3. Let func be GetSuperConstructor().
  459. auto* func = get_super_constructor(vm);
  460. // 4. Let argList be ? ArgumentListEvaluation of Arguments.
  461. MarkedVector<Value> arg_list { vm.heap() };
  462. if (m_is_synthetic) {
  463. auto const& value = interpreter.accumulator();
  464. VERIFY(value.is_object() && is<Array>(value.as_object()));
  465. auto const& array_value = static_cast<Array const&>(value.as_object());
  466. auto length = MUST(length_of_array_like(vm, array_value));
  467. for (size_t i = 0; i < length; ++i)
  468. arg_list.append(array_value.get_without_side_effects(PropertyKey { i }));
  469. } else {
  470. for (size_t i = 0; i < m_argument_count; ++i)
  471. arg_list.append(interpreter.reg(m_arguments[i]));
  472. }
  473. // 5. If IsConstructor(func) is false, throw a TypeError exception.
  474. if (!Value(func).is_constructor())
  475. return vm.throw_completion<TypeError>(ErrorType::NotAConstructor, "Super constructor");
  476. // 6. Let result be ? Construct(func, argList, newTarget).
  477. auto* result = TRY(construct(vm, static_cast<FunctionObject&>(*func), move(arg_list), &new_target.as_function()));
  478. // 7. Let thisER be GetThisEnvironment().
  479. auto& this_environment = verify_cast<FunctionEnvironment>(get_this_environment(vm));
  480. // 8. Perform ? thisER.BindThisValue(result).
  481. TRY(this_environment.bind_this_value(vm, result));
  482. // 9. Let F be thisER.[[FunctionObject]].
  483. auto& f = this_environment.function_object();
  484. // 10. Assert: F is an ECMAScript function object.
  485. // NOTE: This is implied by the strong C++ type.
  486. // 11. Perform ? InitializeInstanceElements(result, F).
  487. TRY(vm.initialize_instance_elements(*result, f));
  488. // 12. Return result.
  489. interpreter.accumulator() = result;
  490. return {};
  491. }
  492. ThrowCompletionOr<void> NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  493. {
  494. auto& vm = interpreter.vm();
  495. interpreter.accumulator() = ECMAScriptFunctionObject::create(interpreter.realm(), 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.contains_direct_call_to_eval(), m_function_node.is_arrow_function());
  496. return {};
  497. }
  498. ThrowCompletionOr<void> Return::execute_impl(Bytecode::Interpreter& interpreter) const
  499. {
  500. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  501. return {};
  502. }
  503. ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  504. {
  505. auto& vm = interpreter.vm();
  506. auto old_value = TRY(interpreter.accumulator().to_numeric(vm));
  507. if (old_value.is_number())
  508. interpreter.accumulator() = Value(old_value.as_double() + 1);
  509. else
  510. interpreter.accumulator() = js_bigint(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  511. return {};
  512. }
  513. ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  514. {
  515. auto& vm = interpreter.vm();
  516. auto old_value = TRY(interpreter.accumulator().to_numeric(vm));
  517. if (old_value.is_number())
  518. interpreter.accumulator() = Value(old_value.as_double() - 1);
  519. else
  520. interpreter.accumulator() = js_bigint(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  521. return {};
  522. }
  523. ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  524. {
  525. return throw_completion(interpreter.accumulator());
  526. }
  527. ThrowCompletionOr<void> EnterUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  528. {
  529. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  530. interpreter.jump(m_entry_point);
  531. return {};
  532. }
  533. void EnterUnwindContext::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  534. {
  535. if (&m_entry_point.block() == &from)
  536. m_entry_point = Label { to };
  537. if (m_handler_target.has_value() && &m_handler_target->block() == &from)
  538. m_handler_target = Label { to };
  539. if (m_finalizer_target.has_value() && &m_finalizer_target->block() == &from)
  540. m_finalizer_target = Label { to };
  541. }
  542. ThrowCompletionOr<void> FinishUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  543. {
  544. interpreter.leave_unwind_context();
  545. interpreter.jump(m_next_target);
  546. return {};
  547. }
  548. void FinishUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  549. {
  550. if (&m_next_target.block() == &from)
  551. m_next_target = Label { to };
  552. }
  553. ThrowCompletionOr<void> LeaveEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  554. {
  555. if (m_mode == EnvironmentMode::Lexical)
  556. interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
  557. if (m_mode == EnvironmentMode::Var)
  558. interpreter.vm().running_execution_context().variable_environment = interpreter.saved_variable_environment_stack().take_last();
  559. return {};
  560. }
  561. ThrowCompletionOr<void> LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  562. {
  563. interpreter.leave_unwind_context();
  564. return {};
  565. }
  566. ThrowCompletionOr<void> ContinuePendingUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  567. {
  568. return interpreter.continue_pending_unwind(m_resume_target);
  569. }
  570. void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  571. {
  572. if (&m_resume_target.block() == &from)
  573. m_resume_target = Label { to };
  574. }
  575. ThrowCompletionOr<void> PushDeclarativeEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  576. {
  577. auto* environment = interpreter.vm().heap().allocate_without_realm<DeclarativeEnvironment>(interpreter.vm().lexical_environment());
  578. interpreter.vm().running_execution_context().lexical_environment = environment;
  579. interpreter.vm().running_execution_context().variable_environment = environment;
  580. return {};
  581. }
  582. ThrowCompletionOr<void> Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  583. {
  584. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  585. auto object = Object::create(interpreter.realm(), nullptr);
  586. object->define_direct_property("result", yielded_value, JS::default_attributes);
  587. if (m_continuation_label.has_value())
  588. object->define_direct_property("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))), JS::default_attributes);
  589. else
  590. object->define_direct_property("continuation", Value(0), JS::default_attributes);
  591. interpreter.do_return(object);
  592. return {};
  593. }
  594. void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  595. {
  596. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  597. m_continuation_label = Label { to };
  598. }
  599. ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  600. {
  601. auto& vm = interpreter.vm();
  602. auto* object = TRY(interpreter.reg(m_base).to_object(vm));
  603. auto property_key = TRY(interpreter.accumulator().to_property_key(vm));
  604. interpreter.accumulator() = TRY(object->get(property_key));
  605. return {};
  606. }
  607. ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  608. {
  609. auto& vm = interpreter.vm();
  610. auto* object = TRY(interpreter.reg(m_base).to_object(vm));
  611. auto property_key = TRY(interpreter.reg(m_property).to_property_key(vm));
  612. return put_by_property_key(object, interpreter.accumulator(), property_key, interpreter, m_kind);
  613. }
  614. ThrowCompletionOr<void> DeleteByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  615. {
  616. auto& vm = interpreter.vm();
  617. auto* object = TRY(interpreter.reg(m_base).to_object(vm));
  618. auto property_key = TRY(interpreter.accumulator().to_property_key(vm));
  619. bool strict = vm.in_strict_mode();
  620. auto reference = Reference { object, property_key, {}, strict };
  621. interpreter.accumulator() = Value(TRY(reference.delete_(vm)));
  622. return {};
  623. }
  624. ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  625. {
  626. auto& vm = interpreter.vm();
  627. auto iterator = TRY(get_iterator(vm, interpreter.accumulator()));
  628. interpreter.accumulator() = iterator_to_object(vm, iterator);
  629. return {};
  630. }
  631. // 14.7.5.9 EnumerateObjectProperties ( O ), https://tc39.es/ecma262/#sec-enumerate-object-properties
  632. ThrowCompletionOr<void> GetObjectPropertyIterator::execute_impl(Bytecode::Interpreter& interpreter) const
  633. {
  634. // While the spec does provide an algorithm, it allows us to implement it ourselves so long as we meet the following invariants:
  635. // 1- Returned property keys do not include keys that are Symbols
  636. // 2- Properties of the target object may be deleted during enumeration. A property that is deleted before it is processed by the iterator's next method is ignored
  637. // 3- If new properties are added to the target object during enumeration, the newly added properties are not guaranteed to be processed in the active enumeration
  638. // 4- A property name will be returned by the iterator's next method at most once in any enumeration.
  639. // 5- Enumerating the properties of the target object includes enumerating properties of its prototype, and the prototype of the prototype, and so on, recursively;
  640. // but a property of a prototype is not processed if it has the same name as a property that has already been processed by the iterator's next method.
  641. // 6- The values of [[Enumerable]] attributes are not considered when determining if a property of a prototype object has already been processed.
  642. // 7- The enumerable property names of prototype objects must be obtained by invoking EnumerateObjectProperties passing the prototype object as the argument.
  643. // 8- EnumerateObjectProperties must obtain the own property keys of the target object by calling its [[OwnPropertyKeys]] internal method.
  644. // 9- Property attributes of the target object must be obtained by calling its [[GetOwnProperty]] internal method
  645. // Invariant 3 effectively allows the implementation to ignore newly added keys, and we do so (similar to other implementations).
  646. // Invariants 1 and 6 through 9 are implemented in `enumerable_own_property_names`, which implements the EnumerableOwnPropertyNames AO.
  647. auto& vm = interpreter.vm();
  648. auto* object = TRY(interpreter.accumulator().to_object(vm));
  649. // Note: While the spec doesn't explicitly require these to be ordered, it says that the values should be retrieved via OwnPropertyKeys,
  650. // so we just keep the order consistent anyway.
  651. OrderedHashTable<PropertyKey> properties;
  652. HashTable<Object*> seen_objects;
  653. // Collect all keys immediately (invariant no. 5)
  654. for (auto* object_to_check = object; object_to_check && !seen_objects.contains(object_to_check); object_to_check = TRY(object_to_check->internal_get_prototype_of())) {
  655. seen_objects.set(object_to_check);
  656. for (auto& key : TRY(object_to_check->enumerable_own_property_names(Object::PropertyKind::Key))) {
  657. properties.set(TRY(PropertyKey::from_value(vm, key)));
  658. }
  659. }
  660. Iterator iterator {
  661. .iterator = object,
  662. .next_method = NativeFunction::create(
  663. interpreter.realm(),
  664. [seen_items = HashTable<PropertyKey>(), items = move(properties)](VM& vm) mutable -> ThrowCompletionOr<Value> {
  665. auto& realm = *vm.current_realm();
  666. auto iterated_object_value = vm.this_value();
  667. if (!iterated_object_value.is_object())
  668. return vm.throw_completion<InternalError>("Invalid state for GetObjectPropertyIterator.next");
  669. auto& iterated_object = iterated_object_value.as_object();
  670. auto* result_object = Object::create(realm, nullptr);
  671. while (true) {
  672. if (items.is_empty()) {
  673. result_object->define_direct_property(vm.names.done, JS::Value(true), default_attributes);
  674. return result_object;
  675. }
  676. auto it = items.begin();
  677. auto key = *it;
  678. items.remove(it);
  679. // If the key was already seen, skip over it (invariant no. 4)
  680. auto result = seen_items.set(key);
  681. if (result != AK::HashSetResult::InsertedNewEntry)
  682. continue;
  683. // If the property is deleted, don't include it (invariant no. 2)
  684. if (!TRY(iterated_object.has_property(key)))
  685. continue;
  686. result_object->define_direct_property(vm.names.done, JS::Value(false), default_attributes);
  687. if (key.is_number())
  688. result_object->define_direct_property(vm.names.value, JS::Value(key.as_number()), default_attributes);
  689. else if (key.is_string())
  690. result_object->define_direct_property(vm.names.value, js_string(vm, key.as_string()), default_attributes);
  691. else
  692. VERIFY_NOT_REACHED(); // We should not have non-string/number keys.
  693. return result_object;
  694. }
  695. },
  696. 1,
  697. vm.names.next),
  698. .done = false,
  699. };
  700. interpreter.accumulator() = iterator_to_object(vm, move(iterator));
  701. return {};
  702. }
  703. ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
  704. {
  705. auto& vm = interpreter.vm();
  706. auto* iterator_object = TRY(interpreter.accumulator().to_object(vm));
  707. auto iterator = object_to_iterator(vm, *iterator_object);
  708. interpreter.accumulator() = TRY(iterator_next(vm, iterator));
  709. return {};
  710. }
  711. ThrowCompletionOr<void> IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const
  712. {
  713. auto& vm = interpreter.vm();
  714. auto* iterator_result = TRY(interpreter.accumulator().to_object(vm));
  715. auto complete = TRY(iterator_complete(vm, *iterator_result));
  716. interpreter.accumulator() = Value(complete);
  717. return {};
  718. }
  719. ThrowCompletionOr<void> IteratorResultValue::execute_impl(Bytecode::Interpreter& interpreter) const
  720. {
  721. auto& vm = interpreter.vm();
  722. auto* iterator_result = TRY(interpreter.accumulator().to_object(vm));
  723. interpreter.accumulator() = TRY(iterator_value(vm, *iterator_result));
  724. return {};
  725. }
  726. ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const
  727. {
  728. auto name = m_class_expression.name();
  729. auto scope = interpreter.ast_interpreter_scope();
  730. auto& ast_interpreter = scope.interpreter();
  731. auto class_object = TRY(m_class_expression.class_definition_evaluation(ast_interpreter, name, name.is_null() ? ""sv : name));
  732. interpreter.accumulator() = class_object;
  733. return {};
  734. }
  735. // 13.5.3.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-typeof-operator-runtime-semantics-evaluation
  736. ThrowCompletionOr<void> TypeofVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  737. {
  738. auto& vm = interpreter.vm();
  739. // 1. Let val be the result of evaluating UnaryExpression.
  740. auto const& string = interpreter.current_executable().get_identifier(m_identifier);
  741. auto reference = TRY(vm.resolve_binding(string));
  742. // 2. If val is a Reference Record, then
  743. // a. If IsUnresolvableReference(val) is true, return "undefined".
  744. if (reference.is_unresolvable()) {
  745. interpreter.accumulator() = js_string(vm, "undefined"sv);
  746. return {};
  747. }
  748. // 3. Set val to ? GetValue(val).
  749. auto value = TRY(reference.get_value(vm));
  750. // 4. NOTE: This step is replaced in section B.3.6.3.
  751. // 5. Return a String according to Table 41.
  752. interpreter.accumulator() = js_string(vm, value.typeof());
  753. return {};
  754. }
  755. String Load::to_string_impl(Bytecode::Executable const&) const
  756. {
  757. return String::formatted("Load {}", m_src);
  758. }
  759. String LoadImmediate::to_string_impl(Bytecode::Executable const&) const
  760. {
  761. return String::formatted("LoadImmediate {}", m_value);
  762. }
  763. String Store::to_string_impl(Bytecode::Executable const&) const
  764. {
  765. return String::formatted("Store {}", m_dst);
  766. }
  767. String NewBigInt::to_string_impl(Bytecode::Executable const&) const
  768. {
  769. return String::formatted("NewBigInt \"{}\"", m_bigint.to_base(10));
  770. }
  771. String NewArray::to_string_impl(Bytecode::Executable const&) const
  772. {
  773. StringBuilder builder;
  774. builder.append("NewArray"sv);
  775. if (m_element_count != 0) {
  776. builder.appendff(" [{}-{}]", m_elements[0], m_elements[1]);
  777. }
  778. return builder.to_string();
  779. }
  780. String IteratorToArray::to_string_impl(Bytecode::Executable const&) const
  781. {
  782. return "IteratorToArray";
  783. }
  784. String NewString::to_string_impl(Bytecode::Executable const& executable) const
  785. {
  786. return String::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  787. }
  788. String NewObject::to_string_impl(Bytecode::Executable const&) const
  789. {
  790. return "NewObject";
  791. }
  792. String NewRegExp::to_string_impl(Bytecode::Executable const& executable) const
  793. {
  794. return String::formatted("NewRegExp source:{} (\"{}\") flags:{} (\"{}\")", m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index));
  795. }
  796. String CopyObjectExcludingProperties::to_string_impl(Bytecode::Executable const&) const
  797. {
  798. StringBuilder builder;
  799. builder.appendff("CopyObjectExcludingProperties from:{}", m_from_object);
  800. if (m_excluded_names_count != 0) {
  801. builder.append(" excluding:["sv);
  802. builder.join(", "sv, Span<Register const>(m_excluded_names, m_excluded_names_count));
  803. builder.append(']');
  804. }
  805. return builder.to_string();
  806. }
  807. String ConcatString::to_string_impl(Bytecode::Executable const&) const
  808. {
  809. return String::formatted("ConcatString {}", m_lhs);
  810. }
  811. String GetVariable::to_string_impl(Bytecode::Executable const& executable) const
  812. {
  813. return String::formatted("GetVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  814. }
  815. String DeleteVariable::to_string_impl(Bytecode::Executable const& executable) const
  816. {
  817. return String::formatted("DeleteVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  818. }
  819. String CreateEnvironment::to_string_impl(Bytecode::Executable const&) const
  820. {
  821. auto mode_string = m_mode == EnvironmentMode::Lexical
  822. ? "Lexical"
  823. : "Variable";
  824. return String::formatted("CreateEnvironment mode:{}", mode_string);
  825. }
  826. String CreateVariable::to_string_impl(Bytecode::Executable const& executable) const
  827. {
  828. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  829. return String::formatted("CreateVariable env:{} immutable:{} global:{} {} ({})", mode_string, m_is_immutable, m_is_global, m_identifier, executable.identifier_table->get(m_identifier));
  830. }
  831. String EnterObjectEnvironment::to_string_impl(Executable const&) const
  832. {
  833. return String::formatted("EnterObjectEnvironment");
  834. }
  835. String SetVariable::to_string_impl(Bytecode::Executable const& executable) const
  836. {
  837. auto initialization_mode_name = m_initialization_mode == InitializationMode ::Initialize ? "Initialize"
  838. : m_initialization_mode == InitializationMode::Set ? "Set"
  839. : "InitializeOrSet";
  840. auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable";
  841. return String::formatted("SetVariable env:{} init:{} {} ({})", mode_string, initialization_mode_name, m_identifier, executable.identifier_table->get(m_identifier));
  842. }
  843. String PutById::to_string_impl(Bytecode::Executable const& executable) const
  844. {
  845. auto kind = m_kind == PropertyKind::Getter
  846. ? "getter"
  847. : m_kind == PropertyKind::Setter
  848. ? "setter"
  849. : "property";
  850. return String::formatted("PutById kind:{} base:{}, property:{} ({})", kind, m_base, m_property, executable.identifier_table->get(m_property));
  851. }
  852. String GetById::to_string_impl(Bytecode::Executable const& executable) const
  853. {
  854. return String::formatted("GetById {} ({})", m_property, executable.identifier_table->get(m_property));
  855. }
  856. String DeleteById::to_string_impl(Bytecode::Executable const& executable) const
  857. {
  858. return String::formatted("DeleteById {} ({})", m_property, executable.identifier_table->get(m_property));
  859. }
  860. String Jump::to_string_impl(Bytecode::Executable const&) const
  861. {
  862. if (m_true_target.has_value())
  863. return String::formatted("Jump {}", *m_true_target);
  864. return String::formatted("Jump <empty>");
  865. }
  866. String JumpConditional::to_string_impl(Bytecode::Executable const&) const
  867. {
  868. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  869. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  870. return String::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  871. }
  872. String JumpNullish::to_string_impl(Bytecode::Executable const&) const
  873. {
  874. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  875. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  876. return String::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  877. }
  878. String JumpUndefined::to_string_impl(Bytecode::Executable const&) const
  879. {
  880. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  881. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  882. return String::formatted("JumpUndefined undefined:{} not undefined:{}", true_string, false_string);
  883. }
  884. String Call::to_string_impl(Bytecode::Executable const&) const
  885. {
  886. StringBuilder builder;
  887. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  888. if (m_argument_count != 0) {
  889. builder.append(", arguments:["sv);
  890. builder.join(", "sv, Span<Register const>(m_arguments, m_argument_count));
  891. builder.append(']');
  892. }
  893. return builder.to_string();
  894. }
  895. String SuperCall::to_string_impl(Bytecode::Executable const&) const
  896. {
  897. StringBuilder builder;
  898. builder.append("SuperCall"sv);
  899. if (m_is_synthetic) {
  900. builder.append(" arguments:[...acc]"sv);
  901. } else if (m_argument_count != 0) {
  902. builder.append(" arguments:["sv);
  903. builder.join(", "sv, Span<Register const>(m_arguments, m_argument_count));
  904. builder.append(']');
  905. }
  906. return builder.to_string();
  907. }
  908. String NewFunction::to_string_impl(Bytecode::Executable const&) const
  909. {
  910. return "NewFunction";
  911. }
  912. String NewClass::to_string_impl(Bytecode::Executable const&) const
  913. {
  914. return "NewClass";
  915. }
  916. String Return::to_string_impl(Bytecode::Executable const&) const
  917. {
  918. return "Return";
  919. }
  920. String Increment::to_string_impl(Bytecode::Executable const&) const
  921. {
  922. return "Increment";
  923. }
  924. String Decrement::to_string_impl(Bytecode::Executable const&) const
  925. {
  926. return "Decrement";
  927. }
  928. String Throw::to_string_impl(Bytecode::Executable const&) const
  929. {
  930. return "Throw";
  931. }
  932. String EnterUnwindContext::to_string_impl(Bytecode::Executable const&) const
  933. {
  934. auto handler_string = m_handler_target.has_value() ? String::formatted("{}", *m_handler_target) : "<empty>";
  935. auto finalizer_string = m_finalizer_target.has_value() ? String::formatted("{}", *m_finalizer_target) : "<empty>";
  936. return String::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  937. }
  938. String FinishUnwind::to_string_impl(Bytecode::Executable const&) const
  939. {
  940. return String::formatted("FinishUnwind next:{}", m_next_target);
  941. }
  942. String LeaveEnvironment::to_string_impl(Bytecode::Executable const&) const
  943. {
  944. auto mode_string = m_mode == EnvironmentMode::Lexical
  945. ? "Lexical"
  946. : "Variable";
  947. return String::formatted("LeaveEnvironment env:{}", mode_string);
  948. }
  949. String LeaveUnwindContext::to_string_impl(Bytecode::Executable const&) const
  950. {
  951. return "LeaveUnwindContext";
  952. }
  953. String ContinuePendingUnwind::to_string_impl(Bytecode::Executable const&) const
  954. {
  955. return String::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  956. }
  957. String PushDeclarativeEnvironment::to_string_impl(Bytecode::Executable const& executable) const
  958. {
  959. StringBuilder builder;
  960. builder.append("PushDeclarativeEnvironment"sv);
  961. if (!m_variables.is_empty()) {
  962. builder.append(" {"sv);
  963. Vector<String> names;
  964. for (auto& it : m_variables)
  965. names.append(executable.get_string(it.key));
  966. builder.append('}');
  967. builder.join(", "sv, names);
  968. }
  969. return builder.to_string();
  970. }
  971. String Yield::to_string_impl(Bytecode::Executable const&) const
  972. {
  973. if (m_continuation_label.has_value())
  974. return String::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  975. return String::formatted("Yield return");
  976. }
  977. String GetByValue::to_string_impl(Bytecode::Executable const&) const
  978. {
  979. return String::formatted("GetByValue base:{}", m_base);
  980. }
  981. String PutByValue::to_string_impl(Bytecode::Executable const&) const
  982. {
  983. auto kind = m_kind == PropertyKind::Getter
  984. ? "getter"
  985. : m_kind == PropertyKind::Setter
  986. ? "setter"
  987. : "property";
  988. return String::formatted("PutByValue kind:{} base:{}, property:{}", kind, m_base, m_property);
  989. }
  990. String DeleteByValue::to_string_impl(Bytecode::Executable const&) const
  991. {
  992. return String::formatted("DeleteByValue base:{}", m_base);
  993. }
  994. String GetIterator::to_string_impl(Executable const&) const
  995. {
  996. return "GetIterator";
  997. }
  998. String GetObjectPropertyIterator::to_string_impl(Bytecode::Executable const&) const
  999. {
  1000. return "GetObjectPropertyIterator";
  1001. }
  1002. String IteratorNext::to_string_impl(Executable const&) const
  1003. {
  1004. return "IteratorNext";
  1005. }
  1006. String IteratorResultDone::to_string_impl(Executable const&) const
  1007. {
  1008. return "IteratorResultDone";
  1009. }
  1010. String IteratorResultValue::to_string_impl(Executable const&) const
  1011. {
  1012. return "IteratorResultValue";
  1013. }
  1014. String ResolveThisBinding::to_string_impl(Bytecode::Executable const&) const
  1015. {
  1016. return "ResolveThisBinding"sv;
  1017. }
  1018. String GetNewTarget::to_string_impl(Bytecode::Executable const&) const
  1019. {
  1020. return "GetNewTarget"sv;
  1021. }
  1022. String TypeofVariable::to_string_impl(Bytecode::Executable const& executable) const
  1023. {
  1024. return String::formatted("TypeofVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
  1025. }
  1026. }