AbstractOperations.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. /*
  2. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/CharacterTypes.h>
  8. #include <AK/FloatingPointStringConversions.h>
  9. #include <AK/Function.h>
  10. #include <AK/Optional.h>
  11. #include <AK/Utf16View.h>
  12. #include <LibJS/Bytecode/Interpreter.h>
  13. #include <LibJS/Interpreter.h>
  14. #include <LibJS/Parser.h>
  15. #include <LibJS/Runtime/AbstractOperations.h>
  16. #include <LibJS/Runtime/Accessor.h>
  17. #include <LibJS/Runtime/ArgumentsObject.h>
  18. #include <LibJS/Runtime/Array.h>
  19. #include <LibJS/Runtime/BoundFunction.h>
  20. #include <LibJS/Runtime/Completion.h>
  21. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  22. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  23. #include <LibJS/Runtime/ErrorTypes.h>
  24. #include <LibJS/Runtime/FunctionEnvironment.h>
  25. #include <LibJS/Runtime/FunctionObject.h>
  26. #include <LibJS/Runtime/GlobalObject.h>
  27. #include <LibJS/Runtime/Object.h>
  28. #include <LibJS/Runtime/ObjectEnvironment.h>
  29. #include <LibJS/Runtime/PromiseCapability.h>
  30. #include <LibJS/Runtime/PromiseConstructor.h>
  31. #include <LibJS/Runtime/PropertyDescriptor.h>
  32. #include <LibJS/Runtime/PropertyKey.h>
  33. #include <LibJS/Runtime/ProxyObject.h>
  34. #include <LibJS/Runtime/Reference.h>
  35. #include <LibJS/Runtime/SuppressedError.h>
  36. namespace JS {
  37. // 7.2.1 RequireObjectCoercible ( argument ), https://tc39.es/ecma262/#sec-requireobjectcoercible
  38. ThrowCompletionOr<Value> require_object_coercible(VM& vm, Value value)
  39. {
  40. if (value.is_nullish())
  41. return vm.throw_completion<TypeError>(ErrorType::NotObjectCoercible, TRY_OR_THROW_OOM(vm, value.to_string_without_side_effects()));
  42. return value;
  43. }
  44. // 7.3.14 Call ( F, V [ , argumentsList ] ), https://tc39.es/ecma262/#sec-call
  45. ThrowCompletionOr<Value> call_impl(VM& vm, Value function, Value this_value, Optional<MarkedVector<Value>> arguments_list)
  46. {
  47. // 1. If argumentsList is not present, set argumentsList to a new empty List.
  48. if (!arguments_list.has_value())
  49. arguments_list = MarkedVector<Value> { vm.heap() };
  50. // 2. If IsCallable(F) is false, throw a TypeError exception.
  51. if (!function.is_function())
  52. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, TRY_OR_THROW_OOM(vm, function.to_string_without_side_effects()));
  53. // 3. Return ? F.[[Call]](V, argumentsList).
  54. return function.as_function().internal_call(this_value, move(*arguments_list));
  55. }
  56. ThrowCompletionOr<Value> call_impl(VM& vm, FunctionObject& function, Value this_value, Optional<MarkedVector<Value>> arguments_list)
  57. {
  58. // 1. If argumentsList is not present, set argumentsList to a new empty List.
  59. if (!arguments_list.has_value())
  60. arguments_list = MarkedVector<Value> { vm.heap() };
  61. // 2. If IsCallable(F) is false, throw a TypeError exception.
  62. // Note: Called with a FunctionObject ref
  63. // 3. Return ? F.[[Call]](V, argumentsList).
  64. return function.internal_call(this_value, move(*arguments_list));
  65. }
  66. // 7.3.15 Construct ( F [ , argumentsList [ , newTarget ] ] ), https://tc39.es/ecma262/#sec-construct
  67. ThrowCompletionOr<NonnullGCPtr<Object>> construct_impl(VM& vm, FunctionObject& function, Optional<MarkedVector<Value>> arguments_list, FunctionObject* new_target)
  68. {
  69. // 1. If newTarget is not present, set newTarget to F.
  70. if (!new_target)
  71. new_target = &function;
  72. // 2. If argumentsList is not present, set argumentsList to a new empty List.
  73. if (!arguments_list.has_value())
  74. arguments_list = MarkedVector<Value> { vm.heap() };
  75. // 3. Return ? F.[[Construct]](argumentsList, newTarget).
  76. return function.internal_construct(move(*arguments_list), *new_target);
  77. }
  78. // 7.3.19 LengthOfArrayLike ( obj ), https://tc39.es/ecma262/#sec-lengthofarraylike
  79. ThrowCompletionOr<size_t> length_of_array_like(VM& vm, Object const& object)
  80. {
  81. // 1. Return ℝ(? ToLength(? Get(obj, "length"))).
  82. return TRY(object.get(vm.names.length)).to_length(vm);
  83. }
  84. // 7.3.20 CreateListFromArrayLike ( obj [ , elementTypes ] ), https://tc39.es/ecma262/#sec-createlistfromarraylike
  85. ThrowCompletionOr<MarkedVector<Value>> create_list_from_array_like(VM& vm, Value value, Function<ThrowCompletionOr<void>(Value)> check_value)
  86. {
  87. // 1. If elementTypes is not present, set elementTypes to « Undefined, Null, Boolean, String, Symbol, Number, BigInt, Object ».
  88. // 2. If Type(obj) is not Object, throw a TypeError exception.
  89. if (!value.is_object())
  90. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, TRY_OR_THROW_OOM(vm, value.to_string_without_side_effects()));
  91. auto& array_like = value.as_object();
  92. // 3. Let len be ? LengthOfArrayLike(obj).
  93. auto length = TRY(length_of_array_like(vm, array_like));
  94. // 4. Let list be a new empty List.
  95. auto list = MarkedVector<Value> { vm.heap() };
  96. list.ensure_capacity(length);
  97. // 5. Let index be 0.
  98. // 6. Repeat, while index < len,
  99. for (size_t i = 0; i < length; ++i) {
  100. // a. Let indexName be ! ToString(𝔽(index)).
  101. auto index_name = PropertyKey { i };
  102. // b. Let next be ? Get(obj, indexName).
  103. auto next = TRY(array_like.get(index_name));
  104. // c. If Type(next) is not an element of elementTypes, throw a TypeError exception.
  105. if (check_value)
  106. TRY(check_value(next));
  107. // d. Append next as the last element of list.
  108. list.unchecked_append(next);
  109. }
  110. // 7. Return list.
  111. return ThrowCompletionOr(move(list));
  112. }
  113. // 7.3.23 SpeciesConstructor ( O, defaultConstructor ), https://tc39.es/ecma262/#sec-speciesconstructor
  114. ThrowCompletionOr<FunctionObject*> species_constructor(VM& vm, Object const& object, FunctionObject& default_constructor)
  115. {
  116. // 1. Let C be ? Get(O, "constructor").
  117. auto constructor = TRY(object.get(vm.names.constructor));
  118. // 2. If C is undefined, return defaultConstructor.
  119. if (constructor.is_undefined())
  120. return &default_constructor;
  121. // 3. If Type(C) is not Object, throw a TypeError exception.
  122. if (!constructor.is_object())
  123. return vm.throw_completion<TypeError>(ErrorType::NotAConstructor, TRY_OR_THROW_OOM(vm, constructor.to_string_without_side_effects()));
  124. // 4. Let S be ? Get(C, @@species).
  125. auto species = TRY(constructor.as_object().get(vm.well_known_symbol_species()));
  126. // 5. If S is either undefined or null, return defaultConstructor.
  127. if (species.is_nullish())
  128. return &default_constructor;
  129. // 6. If IsConstructor(S) is true, return S.
  130. if (species.is_constructor())
  131. return &species.as_function();
  132. // 7. Throw a TypeError exception.
  133. return vm.throw_completion<TypeError>(ErrorType::NotAConstructor, TRY_OR_THROW_OOM(vm, species.to_string_without_side_effects()));
  134. }
  135. // 7.3.25 GetFunctionRealm ( obj ), https://tc39.es/ecma262/#sec-getfunctionrealm
  136. ThrowCompletionOr<Realm*> get_function_realm(VM& vm, FunctionObject const& function)
  137. {
  138. // 1. If obj has a [[Realm]] internal slot, then
  139. if (function.realm()) {
  140. // a. Return obj.[[Realm]].
  141. return function.realm();
  142. }
  143. // 2. If obj is a bound function exotic object, then
  144. if (is<BoundFunction>(function)) {
  145. auto& bound_function = static_cast<BoundFunction const&>(function);
  146. // a. Let target be obj.[[BoundTargetFunction]].
  147. auto& target = bound_function.bound_target_function();
  148. // b. Return ? GetFunctionRealm(target).
  149. return get_function_realm(vm, target);
  150. }
  151. // 3. If obj is a Proxy exotic object, then
  152. if (is<ProxyObject>(function)) {
  153. auto& proxy = static_cast<ProxyObject const&>(function);
  154. // a. If obj.[[ProxyHandler]] is null, throw a TypeError exception.
  155. if (proxy.is_revoked())
  156. return vm.throw_completion<TypeError>(ErrorType::ProxyRevoked);
  157. // b. Let proxyTarget be obj.[[ProxyTarget]].
  158. auto& proxy_target = proxy.target();
  159. // c. Return ? GetFunctionRealm(proxyTarget).
  160. VERIFY(proxy_target.is_function());
  161. return get_function_realm(vm, static_cast<FunctionObject const&>(proxy_target));
  162. }
  163. // 4. Return the current Realm Record.
  164. return vm.current_realm();
  165. }
  166. // 8.5.2.1 InitializeBoundName ( name, value, environment ), https://tc39.es/ecma262/#sec-initializeboundname
  167. ThrowCompletionOr<void> initialize_bound_name(VM& vm, DeprecatedFlyString const& name, Value value, Environment* environment)
  168. {
  169. // 1. If environment is not undefined, then
  170. if (environment) {
  171. // FIXME: The normal is not included in the explicit resource management spec yet, so there is no spec link for it.
  172. // a. Perform ! environment.InitializeBinding(name, value, normal).
  173. MUST(environment->initialize_binding(vm, name, value, Environment::InitializeBindingHint::Normal));
  174. // b. Return unused.
  175. return {};
  176. }
  177. // 2. Else,
  178. else {
  179. // a. Let lhs be ? ResolveBinding(name).
  180. auto lhs = TRY(vm.resolve_binding(name));
  181. // b. Return ? PutValue(lhs, value).
  182. return TRY(lhs.put_value(vm, value));
  183. }
  184. VERIFY_NOT_REACHED();
  185. }
  186. // 10.1.6.2 IsCompatiblePropertyDescriptor ( Extensible, Desc, Current ), https://tc39.es/ecma262/#sec-iscompatiblepropertydescriptor
  187. bool is_compatible_property_descriptor(bool extensible, PropertyDescriptor const& descriptor, Optional<PropertyDescriptor> const& current)
  188. {
  189. // 1. Return ValidateAndApplyPropertyDescriptor(undefined, "", Extensible, Desc, Current).
  190. return validate_and_apply_property_descriptor(nullptr, "", extensible, descriptor, current);
  191. }
  192. // 10.1.6.3 ValidateAndApplyPropertyDescriptor ( O, P, extensible, Desc, current ), https://tc39.es/ecma262/#sec-validateandapplypropertydescriptor
  193. bool validate_and_apply_property_descriptor(Object* object, PropertyKey const& property_key, bool extensible, PropertyDescriptor const& descriptor, Optional<PropertyDescriptor> const& current)
  194. {
  195. // 1. Assert: IsPropertyKey(P) is true.
  196. VERIFY(property_key.is_valid());
  197. // 2. If current is undefined, then
  198. if (!current.has_value()) {
  199. // a. If extensible is false, return false.
  200. if (!extensible)
  201. return false;
  202. // b. If O is undefined, return true.
  203. if (object == nullptr)
  204. return true;
  205. // c. If IsAccessorDescriptor(Desc) is true, then
  206. if (descriptor.is_accessor_descriptor()) {
  207. // i. Create an own accessor property named P of object O whose [[Get]], [[Set]], [[Enumerable]], and [[Configurable]] attributes are set to the value of the corresponding field in Desc if Desc has that field, or to the attribute's default value otherwise.
  208. auto accessor = Accessor::create(object->vm(), descriptor.get.value_or(nullptr), descriptor.set.value_or(nullptr));
  209. object->storage_set(property_key, { accessor, descriptor.attributes() });
  210. }
  211. // d. Else,
  212. else {
  213. // i. Create an own data property named P of object O whose [[Value]], [[Writable]], [[Enumerable]], and [[Configurable]] attributes are set to the value of the corresponding field in Desc if Desc has that field, or to the attribute's default value otherwise.
  214. auto value = descriptor.value.value_or(js_undefined());
  215. object->storage_set(property_key, { value, descriptor.attributes() });
  216. }
  217. // e. Return true.
  218. return true;
  219. }
  220. // 3. Assert: current is a fully populated Property Descriptor.
  221. // 4. If Desc does not have any fields, return true.
  222. if (descriptor.is_empty())
  223. return true;
  224. // 5. If current.[[Configurable]] is false, then
  225. if (!*current->configurable) {
  226. // a. If Desc has a [[Configurable]] field and Desc.[[Configurable]] is true, return false.
  227. if (descriptor.configurable.has_value() && *descriptor.configurable)
  228. return false;
  229. // b. If Desc has an [[Enumerable]] field and SameValue(Desc.[[Enumerable]], current.[[Enumerable]]) is false, return false.
  230. if (descriptor.enumerable.has_value() && *descriptor.enumerable != *current->enumerable)
  231. return false;
  232. // c. If IsGenericDescriptor(Desc) is false and SameValue(IsAccessorDescriptor(Desc), IsAccessorDescriptor(current)) is false, return false.
  233. if (!descriptor.is_generic_descriptor() && (descriptor.is_accessor_descriptor() != current->is_accessor_descriptor()))
  234. return false;
  235. // d. If IsAccessorDescriptor(current) is true, then
  236. if (current->is_accessor_descriptor()) {
  237. // i. If Desc has a [[Get]] field and SameValue(Desc.[[Get]], current.[[Get]]) is false, return false.
  238. if (descriptor.get.has_value() && *descriptor.get != *current->get)
  239. return false;
  240. // ii. If Desc has a [[Set]] field and SameValue(Desc.[[Set]], current.[[Set]]) is false, return false.
  241. if (descriptor.set.has_value() && *descriptor.set != *current->set)
  242. return false;
  243. }
  244. // e. Else if current.[[Writable]] is false, then
  245. else if (!*current->writable) {
  246. // i. If Desc has a [[Writable]] field and Desc.[[Writable]] is true, return false.
  247. if (descriptor.writable.has_value() && *descriptor.writable)
  248. return false;
  249. // ii. If Desc has a [[Value]] field and SameValue(Desc.[[Value]], current.[[Value]]) is false, return false.
  250. if (descriptor.value.has_value() && (*descriptor.value != *current->value))
  251. return false;
  252. }
  253. }
  254. // 6. If O is not undefined, then
  255. if (object != nullptr) {
  256. // a. If IsDataDescriptor(current) is true and IsAccessorDescriptor(Desc) is true, then
  257. if (current->is_data_descriptor() && descriptor.is_accessor_descriptor()) {
  258. // i. If Desc has a [[Configurable]] field, let configurable be Desc.[[Configurable]], else let configurable be current.[[Configurable]].
  259. auto configurable = descriptor.configurable.value_or(*current->configurable);
  260. // ii. If Desc has a [[Enumerable]] field, let enumerable be Desc.[[Enumerable]], else let enumerable be current.[[Enumerable]].
  261. auto enumerable = descriptor.enumerable.value_or(*current->enumerable);
  262. // iii. Replace the property named P of object O with an accessor property having [[Configurable]] and [[Enumerable]] attributes set to configurable and enumerable, respectively, and each other attribute set to its corresponding value in Desc if present, otherwise to its default value.
  263. auto accessor = Accessor::create(object->vm(), descriptor.get.value_or(nullptr), descriptor.set.value_or(nullptr));
  264. PropertyAttributes attributes;
  265. attributes.set_enumerable(enumerable);
  266. attributes.set_configurable(configurable);
  267. object->storage_set(property_key, { accessor, attributes });
  268. }
  269. // b. Else if IsAccessorDescriptor(current) is true and IsDataDescriptor(Desc) is true, then
  270. else if (current->is_accessor_descriptor() && descriptor.is_data_descriptor()) {
  271. // i. If Desc has a [[Configurable]] field, let configurable be Desc.[[Configurable]], else let configurable be current.[[Configurable]].
  272. auto configurable = descriptor.configurable.value_or(*current->configurable);
  273. // ii. If Desc has a [[Enumerable]] field, let enumerable be Desc.[[Enumerable]], else let enumerable be current.[[Enumerable]].
  274. auto enumerable = descriptor.enumerable.value_or(*current->enumerable);
  275. // iii. Replace the property named P of object O with a data property having [[Configurable]] and [[Enumerable]] attributes set to configurable and enumerable, respectively, and each other attribute set to its corresponding value in Desc if present, otherwise to its default value.
  276. auto value = descriptor.value.value_or(js_undefined());
  277. PropertyAttributes attributes;
  278. attributes.set_writable(descriptor.writable.value_or(false));
  279. attributes.set_enumerable(enumerable);
  280. attributes.set_configurable(configurable);
  281. object->storage_set(property_key, { value, attributes });
  282. }
  283. // c. Else,
  284. else {
  285. // i. For each field of Desc, set the corresponding attribute of the property named P of object O to the value of the field.
  286. Value value;
  287. if (descriptor.is_accessor_descriptor() || (current->is_accessor_descriptor() && !descriptor.is_data_descriptor())) {
  288. auto getter = descriptor.get.value_or(current->get.value_or(nullptr));
  289. auto setter = descriptor.set.value_or(current->set.value_or(nullptr));
  290. value = Accessor::create(object->vm(), getter, setter);
  291. } else {
  292. value = descriptor.value.value_or(current->value.value_or({}));
  293. }
  294. PropertyAttributes attributes;
  295. attributes.set_writable(descriptor.writable.value_or(current->writable.value_or(false)));
  296. attributes.set_enumerable(descriptor.enumerable.value_or(current->enumerable.value_or(false)));
  297. attributes.set_configurable(descriptor.configurable.value_or(current->configurable.value_or(false)));
  298. object->storage_set(property_key, { value, attributes });
  299. }
  300. }
  301. // 7. Return true.
  302. return true;
  303. }
  304. // 10.1.14 GetPrototypeFromConstructor ( constructor, intrinsicDefaultProto ), https://tc39.es/ecma262/#sec-getprototypefromconstructor
  305. ThrowCompletionOr<Object*> get_prototype_from_constructor(VM& vm, FunctionObject const& constructor, NonnullGCPtr<Object> (Intrinsics::*intrinsic_default_prototype)())
  306. {
  307. // 1. Assert: intrinsicDefaultProto is this specification's name of an intrinsic object. The corresponding object must be an intrinsic that is intended to be used as the [[Prototype]] value of an object.
  308. // 2. Let proto be ? Get(constructor, "prototype").
  309. auto prototype = TRY(constructor.get(vm.names.prototype));
  310. // 3. If Type(proto) is not Object, then
  311. if (!prototype.is_object()) {
  312. // a. Let realm be ? GetFunctionRealm(constructor).
  313. auto* realm = TRY(get_function_realm(vm, constructor));
  314. // b. Set proto to realm's intrinsic object named intrinsicDefaultProto.
  315. prototype = (realm->intrinsics().*intrinsic_default_prototype)();
  316. }
  317. // 4. Return proto.
  318. return &prototype.as_object();
  319. }
  320. // 9.1.2.2 NewDeclarativeEnvironment ( E ), https://tc39.es/ecma262/#sec-newdeclarativeenvironment
  321. NonnullGCPtr<DeclarativeEnvironment> new_declarative_environment(Environment& environment)
  322. {
  323. auto& heap = environment.heap();
  324. // 1. Let env be a new Declarative Environment Record containing no bindings.
  325. // 2. Set env.[[OuterEnv]] to E.
  326. // 3. Return env.
  327. return heap.allocate_without_realm<DeclarativeEnvironment>(&environment);
  328. }
  329. // 9.1.2.3 NewObjectEnvironment ( O, W, E ), https://tc39.es/ecma262/#sec-newobjectenvironment
  330. NonnullGCPtr<ObjectEnvironment> new_object_environment(Object& object, bool is_with_environment, Environment* environment)
  331. {
  332. auto& heap = object.heap();
  333. // 1. Let env be a new Object Environment Record.
  334. // 2. Set env.[[BindingObject]] to O.
  335. // 3. Set env.[[IsWithEnvironment]] to W.
  336. // 4. Set env.[[OuterEnv]] to E.
  337. // 5. Return env.
  338. return heap.allocate_without_realm<ObjectEnvironment>(object, is_with_environment ? ObjectEnvironment::IsWithEnvironment::Yes : ObjectEnvironment::IsWithEnvironment::No, environment);
  339. }
  340. // 9.1.2.4 NewFunctionEnvironment ( F, newTarget ), https://tc39.es/ecma262/#sec-newfunctionenvironment
  341. NonnullGCPtr<FunctionEnvironment> new_function_environment(ECMAScriptFunctionObject& function, Object* new_target)
  342. {
  343. auto& heap = function.heap();
  344. // 1. Let env be a new function Environment Record containing no bindings.
  345. auto env = heap.allocate_without_realm<FunctionEnvironment>(function.environment());
  346. // 2. Set env.[[FunctionObject]] to F.
  347. env->set_function_object(function);
  348. // 3. If F.[[ThisMode]] is lexical, set env.[[ThisBindingStatus]] to lexical.
  349. if (function.this_mode() == ECMAScriptFunctionObject::ThisMode::Lexical)
  350. env->set_this_binding_status(FunctionEnvironment::ThisBindingStatus::Lexical);
  351. // 4. Else, set env.[[ThisBindingStatus]] to uninitialized.
  352. else
  353. env->set_this_binding_status(FunctionEnvironment::ThisBindingStatus::Uninitialized);
  354. // 5. Set env.[[NewTarget]] to newTarget.
  355. env->set_new_target(new_target ?: js_undefined());
  356. // 6. Set env.[[OuterEnv]] to F.[[Environment]].
  357. // NOTE: Done in step 1 via the FunctionEnvironment constructor.
  358. // 7. Return env.
  359. return env;
  360. }
  361. // 9.2.1.1 NewPrivateEnvironment ( outerPrivEnv ), https://tc39.es/ecma262/#sec-newprivateenvironment
  362. NonnullGCPtr<PrivateEnvironment> new_private_environment(VM& vm, PrivateEnvironment* outer)
  363. {
  364. // 1. Let names be a new empty List.
  365. // 2. Return the PrivateEnvironment Record { [[OuterPrivateEnvironment]]: outerPrivEnv, [[Names]]: names }.
  366. return vm.heap().allocate_without_realm<PrivateEnvironment>(outer);
  367. }
  368. // 9.4.3 GetThisEnvironment ( ), https://tc39.es/ecma262/#sec-getthisenvironment
  369. NonnullGCPtr<Environment> get_this_environment(VM& vm)
  370. {
  371. // 1. Let env be the running execution context's LexicalEnvironment.
  372. // 2. Repeat,
  373. for (auto* env = vm.lexical_environment(); env; env = env->outer_environment()) {
  374. // a. Let exists be env.HasThisBinding().
  375. // b. If exists is true, return env.
  376. if (env->has_this_binding())
  377. return *env;
  378. // c. Let outer be env.[[OuterEnv]].
  379. // d. Assert: outer is not null.
  380. // e. Set env to outer.
  381. }
  382. VERIFY_NOT_REACHED();
  383. }
  384. // 9.14 CanBeHeldWeakly ( v ), https://tc39.es/proposal-symbols-as-weakmap-keys/#sec-canbeheldweakly-abstract-operation
  385. bool can_be_held_weakly(Value value)
  386. {
  387. // 1. If Type(v) is Object, return true.
  388. if (value.is_object())
  389. return true;
  390. // 2. If Type(v) is Symbol, then
  391. if (value.is_symbol()) {
  392. // a. For each element e of the GlobalSymbolRegistry List (see 19.4.2.2), do
  393. // i. If SameValue(e.[[Symbol]], v) is true, return false.
  394. // b. Return true.
  395. return !value.as_symbol().is_global();
  396. }
  397. // 3. Return false.
  398. return false;
  399. }
  400. // 13.3.7.2 GetSuperConstructor ( ), https://tc39.es/ecma262/#sec-getsuperconstructor
  401. Object* get_super_constructor(VM& vm)
  402. {
  403. // 1. Let envRec be GetThisEnvironment().
  404. auto env = get_this_environment(vm);
  405. // 2. Assert: envRec is a function Environment Record.
  406. // 3. Let activeFunction be envRec.[[FunctionObject]].
  407. // 4. Assert: activeFunction is an ECMAScript function object.
  408. auto& active_function = verify_cast<FunctionEnvironment>(*env).function_object();
  409. // 5. Let superConstructor be ! activeFunction.[[GetPrototypeOf]]().
  410. auto* super_constructor = MUST(active_function.internal_get_prototype_of());
  411. // 6. Return superConstructor.
  412. return super_constructor;
  413. }
  414. // 13.3.7.3 MakeSuperPropertyReference ( actualThis, propertyKey, strict ), https://tc39.es/ecma262/#sec-makesuperpropertyreference
  415. ThrowCompletionOr<Reference> make_super_property_reference(VM& vm, Value actual_this, PropertyKey const& property_key, bool strict)
  416. {
  417. // 1. Let env be GetThisEnvironment().
  418. auto& env = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  419. // 2. Assert: env.HasSuperBinding() is true.
  420. VERIFY(env.has_super_binding());
  421. // 3. Let baseValue be ? env.GetSuperBase().
  422. auto base_value = TRY(env.get_super_base());
  423. // 4. Return the Reference Record { [[Base]]: baseValue, [[ReferencedName]]: propertyKey, [[Strict]]: strict, [[ThisValue]]: actualThis }.
  424. return Reference { base_value, property_key, actual_this, strict };
  425. }
  426. // 19.2.1.1 PerformEval ( x, strictCaller, direct ), https://tc39.es/ecma262/#sec-performeval
  427. ThrowCompletionOr<Value> perform_eval(VM& vm, Value x, CallerMode strict_caller, EvalMode direct)
  428. {
  429. // 1. Assert: If direct is false, then strictCaller is also false.
  430. VERIFY(direct == EvalMode::Direct || strict_caller == CallerMode::NonStrict);
  431. // 2. If Type(x) is not String, return x.
  432. if (!x.is_string())
  433. return x;
  434. // 3. Let evalRealm be the current Realm Record.
  435. auto& eval_realm = *vm.running_execution_context().realm;
  436. // 4. NOTE: In the case of a direct eval, evalRealm is the realm of both the caller of eval and of the eval function itself.
  437. // 5. Perform ? HostEnsureCanCompileStrings(evalRealm).
  438. TRY(vm.host_ensure_can_compile_strings(eval_realm));
  439. // 6. Let inFunction be false.
  440. bool in_function = false;
  441. // 7. Let inMethod be false.
  442. bool in_method = false;
  443. // 8. Let inDerivedConstructor be false.
  444. bool in_derived_constructor = false;
  445. // 9. Let inClassFieldInitializer be false.
  446. bool in_class_field_initializer = false;
  447. // 10. If direct is true, then
  448. if (direct == EvalMode::Direct) {
  449. // a. Let thisEnvRec be GetThisEnvironment().
  450. auto this_environment_record = get_this_environment(vm);
  451. // b. If thisEnvRec is a function Environment Record, then
  452. if (is<FunctionEnvironment>(*this_environment_record)) {
  453. auto& this_function_environment_record = static_cast<FunctionEnvironment&>(*this_environment_record);
  454. // i. Let F be thisEnvRec.[[FunctionObject]].
  455. auto& function = this_function_environment_record.function_object();
  456. // ii. Set inFunction to true.
  457. in_function = true;
  458. // iii. Set inMethod to thisEnvRec.HasSuperBinding().
  459. in_method = this_function_environment_record.has_super_binding();
  460. // iv. If F.[[ConstructorKind]] is derived, set inDerivedConstructor to true.
  461. if (function.constructor_kind() == ECMAScriptFunctionObject::ConstructorKind::Derived)
  462. in_derived_constructor = true;
  463. // v. Let classFieldInitializerName be F.[[ClassFieldInitializerName]].
  464. auto& class_field_initializer_name = function.class_field_initializer_name();
  465. // vi. If classFieldInitializerName is not empty, set inClassFieldInitializer to true.
  466. if (!class_field_initializer_name.has<Empty>())
  467. in_class_field_initializer = true;
  468. }
  469. }
  470. // 11. Perform the following substeps in an implementation-defined order, possibly interleaving parsing and error detection:
  471. // a. Let script be ParseText(StringToCodePoints(x), Script).
  472. // c. If script Contains ScriptBody is false, return undefined.
  473. // d. Let body be the ScriptBody of script.
  474. // NOTE: We do these next steps by passing initial state to the parser.
  475. // e. If inFunction is false, and body Contains NewTarget, throw a SyntaxError exception.
  476. // f. If inMethod is false, and body Contains SuperProperty, throw a SyntaxError exception.
  477. // g. If inDerivedConstructor is false, and body Contains SuperCall, throw a SyntaxError exception.
  478. // h. If inClassFieldInitializer is true, and ContainsArguments of body is true, throw a SyntaxError exception.
  479. auto& code_string = x.as_string();
  480. Parser::EvalInitialState initial_state {
  481. .in_eval_function_context = in_function,
  482. .allow_super_property_lookup = in_method,
  483. .allow_super_constructor_call = in_derived_constructor,
  484. .in_class_field_initializer = in_class_field_initializer,
  485. };
  486. Parser parser { Lexer { TRY(code_string.deprecated_string()) }, Program::Type::Script, move(initial_state) };
  487. auto program = parser.parse_program(strict_caller == CallerMode::Strict);
  488. // b. If script is a List of errors, throw a SyntaxError exception.
  489. if (parser.has_errors()) {
  490. auto& error = parser.errors()[0];
  491. return vm.throw_completion<SyntaxError>(TRY_OR_THROW_OOM(vm, error.to_string()));
  492. }
  493. bool strict_eval = false;
  494. // 12. If strictCaller is true, let strictEval be true.
  495. if (strict_caller == CallerMode::Strict)
  496. strict_eval = true;
  497. // 13. Else, let strictEval be IsStrict of script.
  498. else
  499. strict_eval = program->is_strict_mode();
  500. // 14. Let runningContext be the running execution context.
  501. // 15. NOTE: If direct is true, runningContext will be the execution context that performed the direct eval. If direct is false, runningContext will be the execution context for the invocation of the eval function.
  502. auto& running_context = vm.running_execution_context();
  503. Environment* lexical_environment;
  504. Environment* variable_environment;
  505. PrivateEnvironment* private_environment;
  506. // 16. If direct is true, then
  507. if (direct == EvalMode::Direct) {
  508. // a. Let lexEnv be NewDeclarativeEnvironment(runningContext's LexicalEnvironment).
  509. lexical_environment = new_declarative_environment(*running_context.lexical_environment);
  510. // b. Let varEnv be runningContext's VariableEnvironment.
  511. variable_environment = running_context.variable_environment;
  512. // c. Let privateEnv be runningContext's PrivateEnvironment.
  513. private_environment = running_context.private_environment;
  514. }
  515. // 17. Else,
  516. else {
  517. // a. Let lexEnv be NewDeclarativeEnvironment(evalRealm.[[GlobalEnv]]).
  518. lexical_environment = new_declarative_environment(eval_realm.global_environment());
  519. // b. Let varEnv be evalRealm.[[GlobalEnv]].
  520. variable_environment = &eval_realm.global_environment();
  521. // c. Let privateEnv be null.
  522. private_environment = nullptr;
  523. }
  524. // 18. If strictEval is true, set varEnv to lexEnv.
  525. if (strict_eval)
  526. variable_environment = lexical_environment;
  527. if (direct == EvalMode::Direct && !strict_eval) {
  528. // NOTE: Non-strict direct eval() forces us to deoptimize variable accesses.
  529. // Mark the variable environment chain as screwed since we will not be able
  530. // to rely on cached environment coordinates from this point on.
  531. variable_environment->set_permanently_screwed_by_eval();
  532. }
  533. // 19. If runningContext is not already suspended, suspend runningContext.
  534. // FIXME: We don't have this concept yet.
  535. // 20. Let evalContext be a new ECMAScript code execution context.
  536. ExecutionContext eval_context(vm.heap());
  537. // 21. Set evalContext's Function to null.
  538. // NOTE: This was done in the construction of eval_context.
  539. // 22. Set evalContext's Realm to evalRealm.
  540. eval_context.realm = &eval_realm;
  541. // 23. Set evalContext's ScriptOrModule to runningContext's ScriptOrModule.
  542. eval_context.script_or_module = running_context.script_or_module;
  543. // 24. Set evalContext's VariableEnvironment to varEnv.
  544. eval_context.variable_environment = variable_environment;
  545. // 25. Set evalContext's LexicalEnvironment to lexEnv.
  546. eval_context.lexical_environment = lexical_environment;
  547. // 26. Set evalContext's PrivateEnvironment to privateEnv.
  548. eval_context.private_environment = private_environment;
  549. // NOTE: This isn't in the spec, but we require it.
  550. eval_context.is_strict_mode = strict_eval;
  551. // 27. Push evalContext onto the execution context stack; evalContext is now the running execution context.
  552. TRY(vm.push_execution_context(eval_context, {}));
  553. // NOTE: We use a ScopeGuard to automatically pop the execution context when any of the `TRY`s below return a throw completion.
  554. ScopeGuard pop_guard = [&] {
  555. // FIXME: 31. Suspend evalContext and remove it from the execution context stack.
  556. // 32. Resume the context that is now on the top of the execution context stack as the running execution context.
  557. vm.pop_execution_context();
  558. };
  559. // 28. Let result be Completion(EvalDeclarationInstantiation(body, varEnv, lexEnv, privateEnv, strictEval)).
  560. TRY(eval_declaration_instantiation(vm, program, variable_environment, lexical_environment, private_environment, strict_eval));
  561. Optional<Value> eval_result;
  562. // 29. If result.[[Type]] is normal, then
  563. // a. Set result to the result of evaluating body.
  564. if (auto* bytecode_interpreter = vm.bytecode_interpreter_if_exists()) {
  565. auto executable_result = Bytecode::Generator::generate(program);
  566. if (executable_result.is_error())
  567. return vm.throw_completion<InternalError>(ErrorType::NotImplemented, TRY_OR_THROW_OOM(vm, executable_result.error().to_string()));
  568. auto executable = executable_result.release_value();
  569. executable->name = "eval"sv;
  570. if (Bytecode::g_dump_bytecode)
  571. executable->dump();
  572. auto result_or_error = bytecode_interpreter->run_and_return_frame(eval_realm, *executable, nullptr);
  573. if (result_or_error.value.is_error())
  574. return result_or_error.value.release_error();
  575. auto& result = result_or_error.frame->registers[0];
  576. if (!result.is_empty())
  577. eval_result = result;
  578. } else {
  579. auto& ast_interpreter = vm.interpreter();
  580. eval_result = TRY(program->execute(ast_interpreter));
  581. }
  582. // 30. If result.[[Type]] is normal and result.[[Value]] is empty, then
  583. // a. Set result to NormalCompletion(undefined).
  584. // NOTE: Step 31 and 32 is handled by `pop_guard` above.
  585. // 33. Return ? result.
  586. // NOTE: Step 33 is also performed with each use of `TRY` above.
  587. return eval_result.value_or(js_undefined());
  588. }
  589. // 19.2.1.3 EvalDeclarationInstantiation ( body, varEnv, lexEnv, privateEnv, strict ), https://tc39.es/ecma262/#sec-evaldeclarationinstantiation
  590. // 9.1.1.1 EvalDeclarationInstantiation ( body, varEnv, lexEnv, privateEnv, strict ), https://tc39.es/proposal-explicit-resource-management/#sec-evaldeclarationinstantiation
  591. ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, Program const& program, Environment* variable_environment, Environment* lexical_environment, PrivateEnvironment* private_environment, bool strict)
  592. {
  593. auto& realm = *vm.current_realm();
  594. GlobalEnvironment* global_var_environment = variable_environment->is_global_environment() ? static_cast<GlobalEnvironment*>(variable_environment) : nullptr;
  595. // 1. Let varNames be the VarDeclaredNames of body.
  596. // 2. Let varDeclarations be the VarScopedDeclarations of body.
  597. // 3. If strict is false, then
  598. if (!strict) {
  599. // a. If varEnv is a global Environment Record, then
  600. if (global_var_environment) {
  601. // i. For each element name of varNames, do
  602. TRY(program.for_each_var_declared_name([&](auto const& name) -> ThrowCompletionOr<void> {
  603. // 1. If varEnv.HasLexicalDeclaration(name) is true, throw a SyntaxError exception.
  604. if (global_var_environment->has_lexical_declaration(name))
  605. return vm.throw_completion<SyntaxError>(ErrorType::TopLevelVariableAlreadyDeclared, name);
  606. // 2. NOTE: eval will not create a global var declaration that would be shadowed by a global lexical declaration.
  607. return {};
  608. }));
  609. }
  610. // b. Let thisEnv be lexEnv.
  611. auto* this_environment = lexical_environment;
  612. // c. Assert: The following loop will terminate.
  613. // d. Repeat, while thisEnv is not the same as varEnv,
  614. while (this_environment != variable_environment) {
  615. // i. If thisEnv is not an object Environment Record, then
  616. if (!is<ObjectEnvironment>(*this_environment)) {
  617. // 1. NOTE: The environment of with statements cannot contain any lexical declaration so it doesn't need to be checked for var/let hoisting conflicts.
  618. // 2. For each element name of varNames, do
  619. TRY(program.for_each_var_declared_name([&](auto const& name) -> ThrowCompletionOr<void> {
  620. // a. If ! thisEnv.HasBinding(name) is true, then
  621. if (MUST(this_environment->has_binding(name))) {
  622. // i. Throw a SyntaxError exception.
  623. return vm.throw_completion<SyntaxError>(ErrorType::TopLevelVariableAlreadyDeclared, name);
  624. // FIXME: ii. NOTE: Annex B.3.4 defines alternate semantics for the above step.
  625. // In particular it only throw the syntax error if it is not an environment from a catchclause.
  626. }
  627. // b. NOTE: A direct eval will not hoist var declaration over a like-named lexical declaration.
  628. return {};
  629. }));
  630. }
  631. // ii. Set thisEnv to thisEnv.[[OuterEnv]].
  632. this_environment = this_environment->outer_environment();
  633. VERIFY(this_environment);
  634. }
  635. }
  636. // 4. Let privateIdentifiers be a new empty List.
  637. // 5. Let pointer be privateEnv.
  638. // 6. Repeat, while pointer is not null,
  639. // a. For each Private Name binding of pointer.[[Names]], do
  640. // i. If privateIdentifiers does not contain binding.[[Description]], append binding.[[Description]] to privateIdentifiers.
  641. // b. Set pointer to pointer.[[OuterPrivateEnvironment]].
  642. // 7. If AllPrivateIdentifiersValid of body with argument privateIdentifiers is false, throw a SyntaxError exception.
  643. // FIXME: Add Private identifiers check here.
  644. // 8. Let functionsToInitialize be a new empty List.
  645. Vector<FunctionDeclaration const&> functions_to_initialize;
  646. // 9. Let declaredFunctionNames be a new empty List.
  647. HashTable<DeprecatedFlyString> declared_function_names;
  648. // 10. For each element d of varDeclarations, in reverse List order, do
  649. TRY(program.for_each_var_function_declaration_in_reverse_order([&](FunctionDeclaration const& function) -> ThrowCompletionOr<void> {
  650. // a. If d is neither a VariableDeclaration nor a ForBinding nor a BindingIdentifier, then
  651. // i. Assert: d is either a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration.
  652. // Note: This is done by for_each_var_function_declaration_in_reverse_order.
  653. // ii. NOTE: If there are multiple function declarations for the same name, the last declaration is used.
  654. // iii. Let fn be the sole element of the BoundNames of d.
  655. // iv. If fn is not an element of declaredFunctionNames, then
  656. if (declared_function_names.set(function.name()) != AK::HashSetResult::InsertedNewEntry)
  657. return {};
  658. // 1. If varEnv is a global Environment Record, then
  659. if (global_var_environment) {
  660. // a. Let fnDefinable be ? varEnv.CanDeclareGlobalFunction(fn).
  661. auto function_definable = TRY(global_var_environment->can_declare_global_function(function.name()));
  662. // b. If fnDefinable is false, throw a TypeError exception.
  663. if (!function_definable)
  664. return vm.throw_completion<TypeError>(ErrorType::CannotDeclareGlobalFunction, function.name());
  665. }
  666. // 2. Append fn to declaredFunctionNames.
  667. // Note: Already done in step iv.
  668. // 3. Insert d as the first element of functionsToInitialize.
  669. // NOTE: Since prepending is much slower, we just append
  670. // and iterate in reverse order in step 17 below.
  671. functions_to_initialize.append(function);
  672. return {};
  673. }));
  674. // 11. NOTE: Annex B.3.2.3 adds additional steps at this point.
  675. // B.3.2.3 Changes to EvalDeclarationInstantiation, https://tc39.es/ecma262/#sec-web-compat-evaldeclarationinstantiation
  676. // 11. If strict is false, then
  677. if (!strict) {
  678. // a. Let declaredFunctionOrVarNames be the list-concatenation of declaredFunctionNames and declaredVarNames.
  679. // The spec here uses 'declaredVarNames' but that has not been declared yet.
  680. HashTable<DeprecatedFlyString> hoisted_functions;
  681. // b. For each FunctionDeclaration f that is directly contained in the StatementList of a Block, CaseClause, or DefaultClause Contained within body, do
  682. TRY(program.for_each_function_hoistable_with_annexB_extension([&](FunctionDeclaration& function_declaration) -> ThrowCompletionOr<void> {
  683. // i. Let F be StringValue of the BindingIdentifier of f.
  684. auto function_name = function_declaration.name();
  685. // ii. If replacing the FunctionDeclaration f with a VariableStatement that has F as a BindingIdentifier would not produce any Early Errors for body, then
  686. // Note: This is checked during parsing and for_each_function_hoistable_with_annexB_extension so it always passes here.
  687. // 1. Let bindingExists be false.
  688. // 2. Let thisEnv be lexEnv.
  689. auto* this_environment = lexical_environment;
  690. // 3. Assert: The following loop will terminate.
  691. // 4. Repeat, while thisEnv is not the same as varEnv,
  692. while (this_environment != variable_environment) {
  693. // a. If thisEnv is not an object Environment Record, then
  694. if (!is<ObjectEnvironment>(*this_environment)) {
  695. // i. If ! thisEnv.HasBinding(F) is true, then
  696. if (MUST(this_environment->has_binding(function_name))) {
  697. // i. Let bindingExists be true.
  698. // Note: When bindingExists is true we skip all the other steps.
  699. return {};
  700. }
  701. }
  702. // b. Set thisEnv to thisEnv.[[OuterEnv]].
  703. this_environment = this_environment->outer_environment();
  704. VERIFY(this_environment);
  705. }
  706. // Note: At this point bindingExists is false.
  707. // 5. If bindingExists is false and varEnv is a global Environment Record, then
  708. if (global_var_environment) {
  709. // a. If varEnv.HasLexicalDeclaration(F) is false, then
  710. if (!global_var_environment->has_lexical_declaration(function_name)) {
  711. // i. Let fnDefinable be ? varEnv.CanDeclareGlobalVar(F).
  712. if (!TRY(global_var_environment->can_declare_global_var(function_name)))
  713. return {};
  714. }
  715. // b. Else,
  716. else {
  717. // i. Let fnDefinable be false.
  718. return {};
  719. }
  720. }
  721. // 6. Else,
  722. // a. Let fnDefinable be true.
  723. // Note: At this point fnDefinable is true.
  724. // 7. If bindingExists is false and fnDefinable is true, then
  725. // a. If declaredFunctionOrVarNames does not contain F, then
  726. if (!declared_function_names.contains(function_name) && !hoisted_functions.contains(function_name)) {
  727. // i. If varEnv is a global Environment Record, then
  728. if (global_var_environment) {
  729. // i. Perform ? varEnv.CreateGlobalVarBinding(F, true).
  730. TRY(global_var_environment->create_global_var_binding(function_name, true));
  731. }
  732. // ii. Else,
  733. else {
  734. // i. Let bindingExists be ! varEnv.HasBinding(F).
  735. // ii. If bindingExists is false, then
  736. if (!MUST(variable_environment->has_binding(function_name))) {
  737. // i. Perform ! varEnv.CreateMutableBinding(F, true).
  738. MUST(variable_environment->create_mutable_binding(vm, function_name, true));
  739. // ii. Perform ! varEnv.InitializeBinding(F, undefined, normal).
  740. MUST(variable_environment->initialize_binding(vm, function_name, js_undefined(), Environment::InitializeBindingHint::Normal));
  741. }
  742. }
  743. }
  744. // iii. Append F to declaredFunctionOrVarNames.
  745. hoisted_functions.set(function_name);
  746. // b. When the FunctionDeclaration f is evaluated, perform the following steps in place of the FunctionDeclaration Evaluation algorithm provided in 15.2.6:
  747. // i. Let genv be the running execution context's VariableEnvironment.
  748. // ii. Let benv be the running execution context's LexicalEnvironment.
  749. // iii. Let fobj be ! benv.GetBindingValue(F, false).
  750. // iv. Perform ? genv.SetMutableBinding(F, fobj, false).
  751. // v. Return unused.
  752. function_declaration.set_should_do_additional_annexB_steps();
  753. return {};
  754. }));
  755. }
  756. // 12. Let declaredVarNames be a new empty List.
  757. HashTable<DeprecatedFlyString> declared_var_names;
  758. // 13. For each element d of varDeclarations, do
  759. TRY(program.for_each_var_scoped_variable_declaration([&](VariableDeclaration const& declaration) {
  760. // a. If d is a VariableDeclaration, a ForBinding, or a BindingIdentifier, then
  761. // Note: This is handled by for_each_var_scoped_variable_declaration.
  762. // i. For each String vn of the BoundNames of d, do
  763. return declaration.for_each_bound_name([&](auto const& name) -> ThrowCompletionOr<void> {
  764. // 1. If vn is not an element of declaredFunctionNames, then
  765. if (!declared_function_names.contains(name)) {
  766. // a. If varEnv is a global Environment Record, then
  767. if (global_var_environment) {
  768. // i. Let vnDefinable be ? varEnv.CanDeclareGlobalVar(vn).
  769. auto variable_definable = TRY(global_var_environment->can_declare_global_var(name));
  770. // ii. If vnDefinable is false, throw a TypeError exception.
  771. if (!variable_definable)
  772. return vm.throw_completion<TypeError>(ErrorType::CannotDeclareGlobalVariable, name);
  773. }
  774. // b. If vn is not an element of declaredVarNames, then
  775. // i. Append vn to declaredVarNames.
  776. declared_var_names.set(name);
  777. }
  778. return {};
  779. });
  780. }));
  781. // 14. NOTE: No abnormal terminations occur after this algorithm step unless varEnv is a global Environment Record and the global object is a Proxy exotic object.
  782. // 15. Let lexDeclarations be the LexicallyScopedDeclarations of body.
  783. // 16. For each element d of lexDeclarations, do
  784. TRY(program.for_each_lexically_scoped_declaration([&](Declaration const& declaration) {
  785. // a. NOTE: Lexically declared names are only instantiated here but not initialized.
  786. // b. For each element dn of the BoundNames of d, do
  787. return declaration.for_each_bound_name([&](auto const& name) -> ThrowCompletionOr<void> {
  788. // i. If IsConstantDeclaration of d is true, then
  789. if (declaration.is_constant_declaration()) {
  790. // 1. Perform ? lexEnv.CreateImmutableBinding(dn, true).
  791. TRY(lexical_environment->create_immutable_binding(vm, name, true));
  792. }
  793. // ii. Else,
  794. else {
  795. // 1. Perform ? lexEnv.CreateMutableBinding(dn, false).
  796. TRY(lexical_environment->create_mutable_binding(vm, name, false));
  797. }
  798. return {};
  799. });
  800. }));
  801. // 17. For each Parse Node f of functionsToInitialize, do
  802. // NOTE: We iterate in reverse order since we appended the functions
  803. // instead of prepending. We append because prepending is much slower
  804. // and we only use the created vector here.
  805. for (auto& declaration : functions_to_initialize.in_reverse()) {
  806. // a. Let fn be the sole element of the BoundNames of f.
  807. // b. Let fo be InstantiateFunctionObject of f with arguments lexEnv and privateEnv.
  808. auto function = ECMAScriptFunctionObject::create(realm, declaration.name(), declaration.source_text(), declaration.body(), declaration.parameters(), declaration.function_length(), declaration.local_variables_names(), lexical_environment, private_environment, declaration.kind(), declaration.is_strict_mode(), declaration.might_need_arguments_object());
  809. // c. If varEnv is a global Environment Record, then
  810. if (global_var_environment) {
  811. // i. Perform ? varEnv.CreateGlobalFunctionBinding(fn, fo, true).
  812. TRY(global_var_environment->create_global_function_binding(declaration.name(), function, true));
  813. }
  814. // d. Else,
  815. else {
  816. // i. Let bindingExists be ! varEnv.HasBinding(fn).
  817. auto binding_exists = MUST(variable_environment->has_binding(declaration.name()));
  818. // ii. If bindingExists is false, then
  819. if (!binding_exists) {
  820. // 1. NOTE: The following invocation cannot return an abrupt completion because of the validation preceding step 14.
  821. // 2. Perform ! varEnv.CreateMutableBinding(fn, true).
  822. MUST(variable_environment->create_mutable_binding(vm, declaration.name(), true));
  823. // 3. Perform ! varEnv.InitializeBinding(fn, fo, normal).
  824. MUST(variable_environment->initialize_binding(vm, declaration.name(), function, Environment::InitializeBindingHint::Normal));
  825. }
  826. // iii. Else,
  827. else {
  828. // 1. Perform ! varEnv.SetMutableBinding(fn, fo, false).
  829. MUST(variable_environment->set_mutable_binding(vm, declaration.name(), function, false));
  830. }
  831. }
  832. }
  833. // 18. For each String vn of declaredVarNames, do
  834. for (auto& var_name : declared_var_names) {
  835. // a. If varEnv is a global Environment Record, then
  836. if (global_var_environment) {
  837. // i. Perform ? varEnv.CreateGlobalVarBinding(vn, true).
  838. TRY(global_var_environment->create_global_var_binding(var_name, true));
  839. }
  840. // b. Else,
  841. else {
  842. // i. Let bindingExists be ! varEnv.HasBinding(vn).
  843. auto binding_exists = MUST(variable_environment->has_binding(var_name));
  844. // ii. If bindingExists is false, then
  845. if (!binding_exists) {
  846. // 1. NOTE: The following invocation cannot return an abrupt completion because of the validation preceding step 14.
  847. // 2. Perform ! varEnv.CreateMutableBinding(vn, true).
  848. MUST(variable_environment->create_mutable_binding(vm, var_name, true));
  849. // 3. Perform ! varEnv.InitializeBinding(vn, undefined, normal).
  850. MUST(variable_environment->initialize_binding(vm, var_name, js_undefined(), Environment::InitializeBindingHint::Normal));
  851. }
  852. }
  853. }
  854. // 19. Return unused.
  855. return {};
  856. }
  857. // 10.4.4.6 CreateUnmappedArgumentsObject ( argumentsList ), https://tc39.es/ecma262/#sec-createunmappedargumentsobject
  858. Object* create_unmapped_arguments_object(VM& vm, Span<Value> arguments)
  859. {
  860. auto& realm = *vm.current_realm();
  861. // 1. Let len be the number of elements in argumentsList.
  862. auto length = arguments.size();
  863. // 2. Let obj be OrdinaryObjectCreate(%Object.prototype%, « [[ParameterMap]] »).
  864. // 3. Set obj.[[ParameterMap]] to undefined.
  865. auto object = Object::create(realm, realm.intrinsics().object_prototype());
  866. object->set_has_parameter_map();
  867. // 4. Perform ! DefinePropertyOrThrow(obj, "length", PropertyDescriptor { [[Value]]: 𝔽(len), [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }).
  868. MUST(object->define_property_or_throw(vm.names.length, { .value = Value(length), .writable = true, .enumerable = false, .configurable = true }));
  869. // 5. Let index be 0.
  870. // 6. Repeat, while index < len,
  871. for (size_t index = 0; index < length; ++index) {
  872. // a. Let val be argumentsList[index].
  873. auto value = arguments[index];
  874. // b. Perform ! CreateDataPropertyOrThrow(obj, ! ToString(𝔽(index)), val).
  875. MUST(object->create_data_property_or_throw(index, value));
  876. // c. Set index to index + 1.
  877. }
  878. // 7. Perform ! DefinePropertyOrThrow(obj, @@iterator, PropertyDescriptor { [[Value]]: %Array.prototype.values%, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }).
  879. auto array_prototype_values = realm.intrinsics().array_prototype_values_function();
  880. MUST(object->define_property_or_throw(vm.well_known_symbol_iterator(), { .value = array_prototype_values, .writable = true, .enumerable = false, .configurable = true }));
  881. // 8. Perform ! DefinePropertyOrThrow(obj, "callee", PropertyDescriptor { [[Get]]: %ThrowTypeError%, [[Set]]: %ThrowTypeError%, [[Enumerable]]: false, [[Configurable]]: false }).
  882. auto throw_type_error = realm.intrinsics().throw_type_error_function();
  883. MUST(object->define_property_or_throw(vm.names.callee, { .get = throw_type_error, .set = throw_type_error, .enumerable = false, .configurable = false }));
  884. // 9. Return obj.
  885. return object;
  886. }
  887. // 10.4.4.7 CreateMappedArgumentsObject ( func, formals, argumentsList, env ), https://tc39.es/ecma262/#sec-createmappedargumentsobject
  888. Object* create_mapped_arguments_object(VM& vm, FunctionObject& function, Vector<FunctionParameter> const& formals, Span<Value> arguments, Environment& environment)
  889. {
  890. auto& realm = *vm.current_realm();
  891. // 1. Assert: formals does not contain a rest parameter, any binding patterns, or any initializers. It may contain duplicate identifiers.
  892. // 2. Let len be the number of elements in argumentsList.
  893. VERIFY(arguments.size() <= NumericLimits<i32>::max());
  894. i32 length = static_cast<i32>(arguments.size());
  895. // 3. Let obj be MakeBasicObject(« [[Prototype]], [[Extensible]], [[ParameterMap]] »).
  896. // 4. Set obj.[[GetOwnProperty]] as specified in 10.4.4.1.
  897. // 5. Set obj.[[DefineOwnProperty]] as specified in 10.4.4.2.
  898. // 6. Set obj.[[Get]] as specified in 10.4.4.3.
  899. // 7. Set obj.[[Set]] as specified in 10.4.4.4.
  900. // 8. Set obj.[[Delete]] as specified in 10.4.4.5.
  901. // 9. Set obj.[[Prototype]] to %Object.prototype%.
  902. auto object = vm.heap().allocate<ArgumentsObject>(realm, realm, environment).release_allocated_value_but_fixme_should_propagate_errors();
  903. // 14. Let index be 0.
  904. // 15. Repeat, while index < len,
  905. for (i32 index = 0; index < length; ++index) {
  906. // a. Let val be argumentsList[index].
  907. auto value = arguments[index];
  908. // b. Perform ! CreateDataPropertyOrThrow(obj, ! ToString(𝔽(index)), val).
  909. MUST(object->create_data_property_or_throw(index, value));
  910. // c. Set index to index + 1.
  911. }
  912. // 16. Perform ! DefinePropertyOrThrow(obj, "length", PropertyDescriptor { [[Value]]: 𝔽(len), [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }).
  913. MUST(object->define_property_or_throw(vm.names.length, { .value = Value(length), .writable = true, .enumerable = false, .configurable = true }));
  914. // 17. Let mappedNames be a new empty List.
  915. HashTable<DeprecatedFlyString> mapped_names;
  916. // 18. Set index to numberOfParameters - 1.
  917. // 19. Repeat, while index ≥ 0,
  918. VERIFY(formals.size() <= NumericLimits<i32>::max());
  919. for (i32 index = static_cast<i32>(formals.size()) - 1; index >= 0; --index) {
  920. // a. Let name be parameterNames[index].
  921. auto const& name = formals[index].binding.get<DeprecatedFlyString>();
  922. // b. If name is not an element of mappedNames, then
  923. if (mapped_names.contains(name))
  924. continue;
  925. // i. Add name as an element of the list mappedNames.
  926. mapped_names.set(name);
  927. // ii. If index < len, then
  928. if (index < length) {
  929. // 1. Let g be MakeArgGetter(name, env).
  930. // 2. Let p be MakeArgSetter(name, env).
  931. // 3. Perform ! map.[[DefineOwnProperty]](! ToString(𝔽(index)), PropertyDescriptor { [[Set]]: p, [[Get]]: g, [[Enumerable]]: false, [[Configurable]]: true }).
  932. object->parameter_map().define_native_accessor(
  933. realm,
  934. PropertyKey { index },
  935. [&environment, name](VM& vm) -> ThrowCompletionOr<Value> {
  936. return MUST(environment.get_binding_value(vm, name, false));
  937. },
  938. [&environment, name](VM& vm) {
  939. MUST(environment.set_mutable_binding(vm, name, vm.argument(0), false));
  940. return js_undefined();
  941. },
  942. Attribute::Configurable);
  943. }
  944. }
  945. // 20. Perform ! DefinePropertyOrThrow(obj, @@iterator, PropertyDescriptor { [[Value]]: %Array.prototype.values%, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }).
  946. auto array_prototype_values = realm.intrinsics().array_prototype_values_function();
  947. MUST(object->define_property_or_throw(vm.well_known_symbol_iterator(), { .value = array_prototype_values, .writable = true, .enumerable = false, .configurable = true }));
  948. // 21. Perform ! DefinePropertyOrThrow(obj, "callee", PropertyDescriptor { [[Value]]: func, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }).
  949. MUST(object->define_property_or_throw(vm.names.callee, { .value = &function, .writable = true, .enumerable = false, .configurable = true }));
  950. // 22. Return obj.
  951. return object;
  952. }
  953. // 7.1.21 CanonicalNumericIndexString ( argument ), https://tc39.es/ecma262/#sec-canonicalnumericindexstring
  954. ThrowCompletionOr<CanonicalIndex> canonical_numeric_index_string(VM& vm, PropertyKey const& property_key, CanonicalIndexMode mode)
  955. {
  956. // NOTE: If the property name is a number type (An implementation-defined optimized
  957. // property key type), it can be treated as a string property that has already been
  958. // converted successfully into a canonical numeric index.
  959. VERIFY(property_key.is_string() || property_key.is_number());
  960. if (property_key.is_number())
  961. return CanonicalIndex(CanonicalIndex::Type::Index, property_key.as_number());
  962. if (mode != CanonicalIndexMode::DetectNumericRoundtrip)
  963. return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
  964. auto& argument = property_key.as_string();
  965. // Handle trivial cases without a full round trip test
  966. // We do not need to check for argument == "0" at this point because we
  967. // already covered it with the is_number() == true path.
  968. if (argument.is_empty())
  969. return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
  970. u32 current_index = 0;
  971. if (argument.characters()[current_index] == '-') {
  972. current_index++;
  973. if (current_index == argument.length())
  974. return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
  975. }
  976. if (argument.characters()[current_index] == '0') {
  977. current_index++;
  978. if (current_index == argument.length())
  979. return CanonicalIndex(CanonicalIndex::Type::Numeric, 0);
  980. if (argument.characters()[current_index] != '.')
  981. return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
  982. current_index++;
  983. if (current_index == argument.length())
  984. return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
  985. }
  986. // Short circuit a few common cases
  987. if (argument == "Infinity"sv || argument == "-Infinity"sv || argument == "NaN"sv)
  988. return CanonicalIndex(CanonicalIndex::Type::Numeric, 0);
  989. // Short circuit any string that doesn't start with digits
  990. if (char first_non_zero = argument.characters()[current_index]; first_non_zero < '0' || first_non_zero > '9')
  991. return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
  992. // 2. Let n be ! ToNumber(argument).
  993. auto maybe_double = argument.to_double(AK::TrimWhitespace::No);
  994. if (!maybe_double.has_value())
  995. return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
  996. // FIXME: We return 0 instead of n but it might not observable?
  997. // 3. If SameValue(! ToString(n), argument) is true, return n.
  998. if (TRY_OR_THROW_OOM(vm, number_to_string(*maybe_double)) == argument.view())
  999. return CanonicalIndex(CanonicalIndex::Type::Numeric, 0);
  1000. // 4. Return undefined.
  1001. return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
  1002. }
  1003. // 22.1.3.19.1 GetSubstitution ( matched, str, position, captures, namedCaptures, replacementTemplate ), https://tc39.es/ecma262/#sec-getsubstitution
  1004. ThrowCompletionOr<String> get_substitution(VM& vm, Utf16View const& matched, Utf16View const& str, size_t position, Span<Value> captures, Value named_captures, Value replacement_template)
  1005. {
  1006. auto replace_string = TRY(replacement_template.to_utf16_string(vm));
  1007. auto replace_view = replace_string.view();
  1008. Utf16Data result;
  1009. for (size_t i = 0; i < replace_view.length_in_code_units(); ++i) {
  1010. u16 curr = replace_view.code_unit_at(i);
  1011. if ((curr != '$') || (i + 1 >= replace_view.length_in_code_units())) {
  1012. TRY_OR_THROW_OOM(vm, result.try_append(curr));
  1013. continue;
  1014. }
  1015. u16 next = replace_view.code_unit_at(i + 1);
  1016. if (next == '$') {
  1017. TRY_OR_THROW_OOM(vm, result.try_append('$'));
  1018. ++i;
  1019. } else if (next == '&') {
  1020. TRY_OR_THROW_OOM(vm, result.try_append(matched.data(), matched.length_in_code_units()));
  1021. ++i;
  1022. } else if (next == '`') {
  1023. auto substring = str.substring_view(0, position);
  1024. TRY_OR_THROW_OOM(vm, result.try_append(substring.data(), substring.length_in_code_units()));
  1025. ++i;
  1026. } else if (next == '\'') {
  1027. auto tail_pos = position + matched.length_in_code_units();
  1028. if (tail_pos < str.length_in_code_units()) {
  1029. auto substring = str.substring_view(tail_pos);
  1030. TRY_OR_THROW_OOM(vm, result.try_append(substring.data(), substring.length_in_code_units()));
  1031. }
  1032. ++i;
  1033. } else if (is_ascii_digit(next)) {
  1034. bool is_two_digits = (i + 2 < replace_view.length_in_code_units()) && is_ascii_digit(replace_view.code_unit_at(i + 2));
  1035. auto capture_position_string = TRY_OR_THROW_OOM(vm, replace_view.substring_view(i + 1, is_two_digits ? 2 : 1).to_utf8());
  1036. auto capture_position = capture_position_string.to_number<u32>();
  1037. if (capture_position.has_value() && (*capture_position > 0) && (*capture_position <= captures.size())) {
  1038. auto& value = captures[*capture_position - 1];
  1039. if (!value.is_undefined()) {
  1040. auto value_string = TRY(value.to_utf16_string(vm));
  1041. TRY_OR_THROW_OOM(vm, result.try_append(value_string.view().data(), value_string.length_in_code_units()));
  1042. }
  1043. i += is_two_digits ? 2 : 1;
  1044. } else {
  1045. TRY_OR_THROW_OOM(vm, result.try_append(curr));
  1046. }
  1047. } else if (next == '<') {
  1048. auto start_position = i + 2;
  1049. Optional<size_t> end_position;
  1050. for (size_t j = start_position; j < replace_view.length_in_code_units(); ++j) {
  1051. if (replace_view.code_unit_at(j) == '>') {
  1052. end_position = j;
  1053. break;
  1054. }
  1055. }
  1056. if (named_captures.is_undefined() || !end_position.has_value()) {
  1057. TRY_OR_THROW_OOM(vm, result.try_append(curr));
  1058. } else {
  1059. auto group_name_view = replace_view.substring_view(start_position, *end_position - start_position);
  1060. auto group_name = TRY_OR_THROW_OOM(vm, group_name_view.to_deprecated_string(Utf16View::AllowInvalidCodeUnits::Yes));
  1061. auto capture = TRY(named_captures.as_object().get(group_name));
  1062. if (!capture.is_undefined()) {
  1063. auto capture_string = TRY(capture.to_utf16_string(vm));
  1064. TRY_OR_THROW_OOM(vm, result.try_append(capture_string.view().data(), capture_string.length_in_code_units()));
  1065. }
  1066. i = *end_position;
  1067. }
  1068. } else {
  1069. TRY_OR_THROW_OOM(vm, result.try_append(curr));
  1070. }
  1071. }
  1072. return TRY_OR_THROW_OOM(vm, Utf16View { result }.to_utf8());
  1073. }
  1074. // 2.1.2 AddDisposableResource ( disposable, V, hint [ , method ] ), https://tc39.es/proposal-explicit-resource-management/#sec-adddisposableresource-disposable-v-hint-disposemethod
  1075. ThrowCompletionOr<void> add_disposable_resource(VM& vm, Vector<DisposableResource>& disposable, Value value, Environment::InitializeBindingHint hint, FunctionObject* method)
  1076. {
  1077. // NOTE: For now only sync is a valid hint
  1078. VERIFY(hint == Environment::InitializeBindingHint::SyncDispose);
  1079. Optional<DisposableResource> resource;
  1080. // 1. If method is not present then,
  1081. if (!method) {
  1082. // a. If V is null or undefined, return NormalCompletion(empty).
  1083. if (value.is_nullish())
  1084. return {};
  1085. // b. If Type(V) is not Object, throw a TypeError exception.
  1086. if (!value.is_object())
  1087. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, TRY_OR_THROW_OOM(vm, value.to_string_without_side_effects()));
  1088. // c. Let resource be ? CreateDisposableResource(V, hint).
  1089. resource = TRY(create_disposable_resource(vm, value, hint));
  1090. }
  1091. // 2. Else,
  1092. else {
  1093. // a. If V is null or undefined, then
  1094. if (value.is_nullish()) {
  1095. // i. Let resource be ? CreateDisposableResource(undefined, hint, method).
  1096. resource = TRY(create_disposable_resource(vm, js_undefined(), hint, method));
  1097. }
  1098. // b. Else,
  1099. else {
  1100. // i. If Type(V) is not Object, throw a TypeError exception.
  1101. if (!value.is_object())
  1102. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, TRY_OR_THROW_OOM(vm, value.to_string_without_side_effects()));
  1103. // ii. Let resource be ? CreateDisposableResource(V, hint, method).
  1104. resource = TRY(create_disposable_resource(vm, value, hint, method));
  1105. }
  1106. }
  1107. // 3. Append resource to disposable.[[DisposableResourceStack]].
  1108. VERIFY(resource.has_value());
  1109. disposable.append(resource.release_value());
  1110. // 4. Return NormalCompletion(empty).
  1111. return {};
  1112. }
  1113. // 2.1.3 CreateDisposableResource ( V, hint [ , method ] ), https://tc39.es/proposal-explicit-resource-management/#sec-createdisposableresource
  1114. ThrowCompletionOr<DisposableResource> create_disposable_resource(VM& vm, Value value, Environment::InitializeBindingHint hint, FunctionObject* method)
  1115. {
  1116. // 1. If method is not present, then
  1117. if (!method) {
  1118. // a. If V is undefined, throw a TypeError exception.
  1119. if (value.is_undefined())
  1120. return vm.throw_completion<TypeError>(ErrorType::IsUndefined, "value");
  1121. // b. Set method to ? GetDisposeMethod(V, hint).
  1122. method = TRY(get_dispose_method(vm, value, hint));
  1123. // c. If method is undefined, throw a TypeError exception.
  1124. if (!method)
  1125. return vm.throw_completion<TypeError>(ErrorType::NoDisposeMethod, TRY_OR_THROW_OOM(vm, value.to_string_without_side_effects()));
  1126. }
  1127. // 2. Else,
  1128. // a. If IsCallable(method) is false, throw a TypeError exception.
  1129. // NOTE: This is guaranteed to never occur from the type.
  1130. VERIFY(method);
  1131. // 3. Return the DisposableResource Record { [[ResourceValue]]: V, [[Hint]]: hint, [[DisposeMethod]]: method }.
  1132. // NOTE: Since we only support sync dispose we don't store the hint for now.
  1133. VERIFY(hint == Environment::InitializeBindingHint::SyncDispose);
  1134. return DisposableResource {
  1135. value,
  1136. *method
  1137. };
  1138. }
  1139. // 2.1.4 GetDisposeMethod ( V, hint ), https://tc39.es/proposal-explicit-resource-management/#sec-getdisposemethod
  1140. ThrowCompletionOr<GCPtr<FunctionObject>> get_dispose_method(VM& vm, Value value, Environment::InitializeBindingHint hint)
  1141. {
  1142. // NOTE: We only have sync dispose for now which means we ignore step 1.
  1143. VERIFY(hint == Environment::InitializeBindingHint::SyncDispose);
  1144. // 2. Else,
  1145. // a. Let method be ? GetMethod(V, @@dispose).
  1146. return TRY(value.get_method(vm, vm.well_known_symbol_dispose()));
  1147. }
  1148. // 2.1.5 Dispose ( V, hint, method ), https://tc39.es/proposal-explicit-resource-management/#sec-dispose
  1149. Completion dispose(VM& vm, Value value, NonnullGCPtr<FunctionObject> method)
  1150. {
  1151. // 1. Let result be ? Call(method, V).
  1152. [[maybe_unused]] auto result = TRY(call(vm, *method, value));
  1153. // NOTE: Hint can only be sync-dispose so we ignore step 2.
  1154. // 2. If hint is async-dispose and result is not undefined, then
  1155. // a. Perform ? Await(result).
  1156. // 3. Return undefined.
  1157. return js_undefined();
  1158. }
  1159. // 2.1.6 DisposeResources ( disposable, completion ), https://tc39.es/proposal-explicit-resource-management/#sec-disposeresources-disposable-completion-errors
  1160. Completion dispose_resources(VM& vm, Vector<DisposableResource> const& disposable, Completion completion)
  1161. {
  1162. // 1. If disposable is not undefined, then
  1163. // NOTE: At this point disposable is always defined.
  1164. // a. For each resource of disposable.[[DisposableResourceStack]], in reverse list order, do
  1165. for (auto const& resource : disposable.in_reverse()) {
  1166. // i. Let result be Dispose(resource.[[ResourceValue]], resource.[[Hint]], resource.[[DisposeMethod]]).
  1167. auto result = dispose(vm, resource.resource_value, resource.dispose_method);
  1168. // ii. If result.[[Type]] is throw, then
  1169. if (result.is_error()) {
  1170. // 1. If completion.[[Type]] is throw, then
  1171. if (completion.is_error()) {
  1172. // a. Set result to result.[[Value]].
  1173. // b. Let suppressed be completion.[[Value]].
  1174. auto suppressed = completion.value().value();
  1175. // c. Let error be a newly created SuppressedError object.
  1176. auto error = SuppressedError::create(*vm.current_realm());
  1177. // d. Perform ! DefinePropertyOrThrow(error, "error", PropertyDescriptor { [[Configurable]]: true, [[Enumerable]]: false, [[Writable]]: true, [[Value]]: result }).
  1178. MUST(error->define_property_or_throw(vm.names.error, { .value = result.value(), .writable = true, .enumerable = true, .configurable = true }));
  1179. // e. Perform ! DefinePropertyOrThrow(error, "suppressed", PropertyDescriptor { [[Configurable]]: true, [[Enumerable]]: false, [[Writable]]: true, [[Value]]: suppressed }).
  1180. MUST(error->define_property_or_throw(vm.names.suppressed, { .value = suppressed, .writable = true, .enumerable = false, .configurable = true }));
  1181. // f. Set completion to ThrowCompletion(error).
  1182. completion = throw_completion(error);
  1183. }
  1184. // 2. Else,
  1185. else {
  1186. // a. Set completion to result.
  1187. completion = result;
  1188. }
  1189. }
  1190. }
  1191. // 2. Return completion.
  1192. return completion;
  1193. }
  1194. Completion dispose_resources(VM& vm, GCPtr<DeclarativeEnvironment> disposable, Completion completion)
  1195. {
  1196. // 1. If disposable is not undefined, then
  1197. if (disposable)
  1198. return dispose_resources(vm, disposable->disposable_resource_stack(), completion);
  1199. // 2. Return completion.
  1200. return completion;
  1201. }
  1202. ThrowCompletionOr<Value> perform_import_call(VM& vm, Value specifier, Value options_value)
  1203. {
  1204. auto& realm = *vm.current_realm();
  1205. // 2.1.1.1 EvaluateImportCall ( specifierExpression [ , optionsExpression ] ), https://tc39.es/proposal-import-assertions/#sec-evaluate-import-call
  1206. // 1. Let referencingScriptOrModule be GetActiveScriptOrModule().
  1207. auto referencing_script_or_module = vm.get_active_script_or_module();
  1208. // 6. Let promiseCapability be ! NewPromiseCapability(%Promise%).
  1209. auto promise_capability = MUST(new_promise_capability(vm, realm.intrinsics().promise_constructor()));
  1210. // 7. Let specifierString be Completion(ToString(specifier)).
  1211. // 8. IfAbruptRejectPromise(specifierString, promiseCapability).
  1212. auto specifier_string = TRY_OR_REJECT_WITH_VALUE(vm, promise_capability, specifier.to_deprecated_string(vm));
  1213. // 9. Let assertions be a new empty List.
  1214. Vector<ModuleRequest::Assertion> assertions;
  1215. // 10. If options is not undefined, then
  1216. if (!options_value.is_undefined()) {
  1217. // a. If Type(options) is not Object,
  1218. if (!options_value.is_object()) {
  1219. auto error = TypeError::create(realm, TRY_OR_THROW_OOM(vm, String::formatted(ErrorType::NotAnObject.message(), "ImportOptions")));
  1220. // i. Perform ! Call(promiseCapability.[[Reject]], undefined, « a newly created TypeError object »).
  1221. MUST(call(vm, *promise_capability->reject(), js_undefined(), error));
  1222. // ii. Return promiseCapability.[[Promise]].
  1223. return Value { promise_capability->promise() };
  1224. }
  1225. // b. Let assertionsObj be Get(options, "assert").
  1226. // c. IfAbruptRejectPromise(assertionsObj, promiseCapability).
  1227. auto assertion_object = TRY_OR_REJECT_WITH_VALUE(vm, promise_capability, options_value.get(vm, vm.names.assert));
  1228. // d. If assertionsObj is not undefined,
  1229. if (!assertion_object.is_undefined()) {
  1230. // i. If Type(assertionsObj) is not Object,
  1231. if (!assertion_object.is_object()) {
  1232. auto error = TypeError::create(realm, TRY_OR_THROW_OOM(vm, String::formatted(ErrorType::NotAnObject.message(), "ImportOptionsAssertions")));
  1233. // 1. Perform ! Call(promiseCapability.[[Reject]], undefined, « a newly created TypeError object »).
  1234. MUST(call(vm, *promise_capability->reject(), js_undefined(), error));
  1235. // 2. Return promiseCapability.[[Promise]].
  1236. return Value { promise_capability->promise() };
  1237. }
  1238. // ii. Let keys be EnumerableOwnPropertyNames(assertionsObj, key).
  1239. // iii. IfAbruptRejectPromise(keys, promiseCapability).
  1240. auto keys = TRY_OR_REJECT_WITH_VALUE(vm, promise_capability, assertion_object.as_object().enumerable_own_property_names(Object::PropertyKind::Key));
  1241. // iv. Let supportedAssertions be ! HostGetSupportedImportAssertions().
  1242. auto supported_assertions = vm.host_get_supported_import_assertions();
  1243. // v. For each String key of keys,
  1244. for (auto const& key : keys) {
  1245. auto property_key = MUST(key.to_property_key(vm));
  1246. // 1. Let value be Get(assertionsObj, key).
  1247. // 2. IfAbruptRejectPromise(value, promiseCapability).
  1248. auto value = TRY_OR_REJECT_WITH_VALUE(vm, promise_capability, assertion_object.get(vm, property_key));
  1249. // 3. If Type(value) is not String, then
  1250. if (!value.is_string()) {
  1251. auto error = TypeError::create(realm, TRY_OR_THROW_OOM(vm, String::formatted(ErrorType::NotAString.message(), "Import Assertion option value")));
  1252. // a. Perform ! Call(promiseCapability.[[Reject]], undefined, « a newly created TypeError object »).
  1253. MUST(call(vm, *promise_capability->reject(), js_undefined(), error));
  1254. // b. Return promiseCapability.[[Promise]].
  1255. return Value { promise_capability->promise() };
  1256. }
  1257. // 4. If supportedAssertions contains key, then
  1258. if (supported_assertions.contains_slow(property_key.to_string())) {
  1259. // a. Append { [[Key]]: key, [[Value]]: value } to assertions.
  1260. assertions.empend(property_key.to_string(), TRY(value.as_string().deprecated_string()));
  1261. }
  1262. }
  1263. }
  1264. // e. Sort assertions by the code point order of the [[Key]] of each element. NOTE: This sorting is observable only in that hosts are prohibited from distinguishing among assertions by the order they occur in.
  1265. // Note: This is done when constructing the ModuleRequest.
  1266. }
  1267. // 11. Let moduleRequest be a new ModuleRequest Record { [[Specifier]]: specifierString, [[Assertions]]: assertions }.
  1268. ModuleRequest request { specifier_string, assertions };
  1269. // 12. Perform HostImportModuleDynamically(referencingScriptOrModule, moduleRequest, promiseCapability).
  1270. MUST_OR_THROW_OOM(vm.host_import_module_dynamically(referencing_script_or_module, move(request), promise_capability));
  1271. // 13. Return promiseCapability.[[Promise]].
  1272. return Value { promise_capability->promise() };
  1273. }
  1274. }