Op.cpp 45 KB

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