AbstractOperations.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. * Copyright (c) 2020-2021, 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/Function.h>
  9. #include <AK/Optional.h>
  10. #include <AK/TemporaryChange.h>
  11. #include <AK/Utf16View.h>
  12. #include <LibJS/Interpreter.h>
  13. #include <LibJS/Parser.h>
  14. #include <LibJS/Runtime/AbstractOperations.h>
  15. #include <LibJS/Runtime/Accessor.h>
  16. #include <LibJS/Runtime/ArgumentsObject.h>
  17. #include <LibJS/Runtime/Array.h>
  18. #include <LibJS/Runtime/BoundFunction.h>
  19. #include <LibJS/Runtime/Completion.h>
  20. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  21. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  22. #include <LibJS/Runtime/ErrorTypes.h>
  23. #include <LibJS/Runtime/FunctionEnvironment.h>
  24. #include <LibJS/Runtime/FunctionObject.h>
  25. #include <LibJS/Runtime/GlobalObject.h>
  26. #include <LibJS/Runtime/Object.h>
  27. #include <LibJS/Runtime/ObjectEnvironment.h>
  28. #include <LibJS/Runtime/PropertyDescriptor.h>
  29. #include <LibJS/Runtime/PropertyName.h>
  30. #include <LibJS/Runtime/ProxyObject.h>
  31. #include <LibJS/Runtime/Reference.h>
  32. namespace JS {
  33. // 7.2.1 RequireObjectCoercible ( argument ), https://tc39.es/ecma262/#sec-requireobjectcoercible
  34. ThrowCompletionOr<Value> require_object_coercible(GlobalObject& global_object, Value value)
  35. {
  36. auto& vm = global_object.vm();
  37. if (value.is_nullish())
  38. return vm.throw_completion<TypeError>(global_object, ErrorType::NotObjectCoercible, value.to_string_without_side_effects());
  39. return value;
  40. }
  41. // 7.3.18 LengthOfArrayLike ( obj ), https://tc39.es/ecma262/#sec-lengthofarraylike
  42. ThrowCompletionOr<size_t> length_of_array_like(GlobalObject& global_object, Object const& object)
  43. {
  44. auto& vm = global_object.vm();
  45. auto result = object.get(vm.names.length);
  46. if (auto* exception = vm.exception())
  47. return throw_completion(exception->value());
  48. auto length = result.to_length(global_object);
  49. if (auto* exception = vm.exception())
  50. return throw_completion(exception->value());
  51. return length;
  52. }
  53. // 7.3.19 CreateListFromArrayLike ( obj [ , elementTypes ] ), https://tc39.es/ecma262/#sec-createlistfromarraylike
  54. ThrowCompletionOr<MarkedValueList> create_list_from_array_like(GlobalObject& global_object, Value value, Function<ThrowCompletionOr<void>(Value)> check_value)
  55. {
  56. auto& vm = global_object.vm();
  57. auto& heap = global_object.heap();
  58. // 1. If elementTypes is not present, set elementTypes to « Undefined, Null, Boolean, String, Symbol, Number, BigInt, Object ».
  59. // 2. If Type(obj) is not Object, throw a TypeError exception.
  60. if (!value.is_object())
  61. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, value.to_string_without_side_effects());
  62. auto& array_like = value.as_object();
  63. // 3. Let len be ? LengthOfArrayLike(obj).
  64. auto length = TRY(length_of_array_like(global_object, array_like));
  65. // 4. Let list be a new empty List.
  66. auto list = MarkedValueList { heap };
  67. // 5. Let index be 0.
  68. // 6. Repeat, while index < len,
  69. for (size_t i = 0; i < length; ++i) {
  70. // a. Let indexName be ! ToString(𝔽(index)).
  71. auto index_name = String::number(i);
  72. // b. Let next be ? Get(obj, indexName).
  73. auto next = array_like.get(index_name);
  74. if (auto* exception = vm.exception())
  75. return throw_completion(exception->value());
  76. // c. If Type(next) is not an element of elementTypes, throw a TypeError exception.
  77. if (check_value)
  78. TRY(check_value(next));
  79. // d. Append next as the last element of list.
  80. list.append(next);
  81. }
  82. // 7. Return list.
  83. return ThrowCompletionOr(move(list));
  84. }
  85. // 7.3.22 SpeciesConstructor ( O, defaultConstructor ), https://tc39.es/ecma262/#sec-speciesconstructor
  86. ThrowCompletionOr<FunctionObject*> species_constructor(GlobalObject& global_object, Object const& object, FunctionObject& default_constructor)
  87. {
  88. auto& vm = global_object.vm();
  89. // 1. Let C be ? Get(O, "constructor").
  90. auto constructor = object.get(vm.names.constructor);
  91. if (auto* exception = vm.exception())
  92. return throw_completion(exception->value());
  93. // 2. If C is undefined, return defaultConstructor.
  94. if (constructor.is_undefined())
  95. return &default_constructor;
  96. // 3. If Type(C) is not Object, throw a TypeError exception.
  97. if (!constructor.is_object())
  98. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects());
  99. // 4. Let S be ? Get(C, @@species).
  100. auto species = constructor.as_object().get(*vm.well_known_symbol_species());
  101. if (auto* exception = vm.exception())
  102. return throw_completion(exception->value());
  103. // 5. If S is either undefined or null, return defaultConstructor.
  104. if (species.is_nullish())
  105. return &default_constructor;
  106. // 6. If IsConstructor(S) is true, return S.
  107. if (species.is_constructor())
  108. return &species.as_function();
  109. // 7. Throw a TypeError exception.
  110. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAConstructor, species.to_string_without_side_effects());
  111. }
  112. // 7.3.24 GetFunctionRealm ( obj ), https://tc39.es/ecma262/#sec-getfunctionrealm
  113. ThrowCompletionOr<Realm*> get_function_realm(GlobalObject& global_object, FunctionObject const& function)
  114. {
  115. auto& vm = global_object.vm();
  116. // 1. Assert: ! IsCallable(obj) is true.
  117. // 2. If obj has a [[Realm]] internal slot, then
  118. if (function.realm()) {
  119. // a. Return obj.[[Realm]].
  120. return function.realm();
  121. }
  122. // 3. If obj is a bound function exotic object, then
  123. if (is<BoundFunction>(function)) {
  124. auto& bound_function = static_cast<BoundFunction const&>(function);
  125. // a. Let target be obj.[[BoundTargetFunction]].
  126. auto& target = bound_function.bound_target_function();
  127. // b. Return ? GetFunctionRealm(target).
  128. return get_function_realm(global_object, target);
  129. }
  130. // 4. If obj is a Proxy exotic object, then
  131. if (is<ProxyObject>(function)) {
  132. auto& proxy = static_cast<ProxyObject const&>(function);
  133. // a. If obj.[[ProxyHandler]] is null, throw a TypeError exception.
  134. if (proxy.is_revoked())
  135. return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyRevoked);
  136. // b. Let proxyTarget be obj.[[ProxyTarget]].
  137. auto& proxy_target = proxy.target();
  138. // c. Return ? GetFunctionRealm(proxyTarget).
  139. VERIFY(proxy_target.is_function());
  140. return get_function_realm(global_object, static_cast<FunctionObject const&>(proxy_target));
  141. }
  142. // 5. Return the current Realm Record.
  143. return vm.current_realm();
  144. }
  145. // 10.1.6.2 IsCompatiblePropertyDescriptor ( Extensible, Desc, Current ), https://tc39.es/ecma262/#sec-iscompatiblepropertydescriptor
  146. bool is_compatible_property_descriptor(bool extensible, PropertyDescriptor const& descriptor, Optional<PropertyDescriptor> const& current)
  147. {
  148. // 1. Return ValidateAndApplyPropertyDescriptor(undefined, undefined, Extensible, Desc, Current).
  149. return validate_and_apply_property_descriptor(nullptr, {}, extensible, descriptor, current);
  150. }
  151. // 10.1.6.3 ValidateAndApplyPropertyDescriptor ( O, P, extensible, Desc, current ), https://tc39.es/ecma262/#sec-validateandapplypropertydescriptor
  152. bool validate_and_apply_property_descriptor(Object* object, PropertyName const& property_name, bool extensible, PropertyDescriptor const& descriptor, Optional<PropertyDescriptor> const& current)
  153. {
  154. // 1. Assert: If O is not undefined, then IsPropertyKey(P) is true.
  155. if (object)
  156. VERIFY(property_name.is_valid());
  157. // 2. If current is undefined, then
  158. if (!current.has_value()) {
  159. // a. If extensible is false, return false.
  160. if (!extensible)
  161. return false;
  162. // b. Assert: extensible is true.
  163. // c. If IsGenericDescriptor(Desc) is true or IsDataDescriptor(Desc) is true, then
  164. if (descriptor.is_generic_descriptor() || descriptor.is_data_descriptor()) {
  165. // i. If O is not undefined, create an own data property named P of object O whose [[Value]], [[Writable]],
  166. // [[Enumerable]], and [[Configurable]] attribute values are described by Desc.
  167. // If the value of an attribute field of Desc is absent, the attribute of the newly created property is set
  168. // to its default value.
  169. if (object) {
  170. auto value = descriptor.value.value_or(js_undefined());
  171. object->storage_set(property_name, { value, descriptor.attributes() });
  172. }
  173. }
  174. // d. Else,
  175. else {
  176. // i. Assert: ! IsAccessorDescriptor(Desc) is true.
  177. VERIFY(descriptor.is_accessor_descriptor());
  178. // ii. If O is not undefined, create an own accessor property named P of object O whose [[Get]], [[Set]],
  179. // [[Enumerable]], and [[Configurable]] attribute values are described by Desc.
  180. // If the value of an attribute field of Desc is absent, the attribute of the newly created property is set
  181. // to its default value.
  182. if (object) {
  183. auto accessor = Accessor::create(object->vm(), descriptor.get.value_or(nullptr), descriptor.set.value_or(nullptr));
  184. object->storage_set(property_name, { accessor, descriptor.attributes() });
  185. }
  186. }
  187. // e. Return true.
  188. return true;
  189. }
  190. // 3. If every field in Desc is absent, return true.
  191. if (descriptor.is_empty())
  192. return true;
  193. // 4. If current.[[Configurable]] is false, then
  194. if (!*current->configurable) {
  195. // a. If Desc.[[Configurable]] is present and its value is true, return false.
  196. if (descriptor.configurable.has_value() && *descriptor.configurable)
  197. return false;
  198. // b. If Desc.[[Enumerable]] is present and ! SameValue(Desc.[[Enumerable]], current.[[Enumerable]]) is false, return false.
  199. if (descriptor.enumerable.has_value() && *descriptor.enumerable != *current->enumerable)
  200. return false;
  201. }
  202. // 5. If ! IsGenericDescriptor(Desc) is true, then
  203. if (descriptor.is_generic_descriptor()) {
  204. // a. NOTE: No further validation is required.
  205. }
  206. // 6. Else if ! SameValue(! IsDataDescriptor(current), ! IsDataDescriptor(Desc)) is false, then
  207. else if (current->is_data_descriptor() != descriptor.is_data_descriptor()) {
  208. // a. If current.[[Configurable]] is false, return false.
  209. if (!*current->configurable)
  210. return false;
  211. // b. If IsDataDescriptor(current) is true, then
  212. if (current->is_data_descriptor()) {
  213. // If O is not undefined, convert the property named P of object O from a data property to an accessor property.
  214. // Preserve the existing values of the converted property's [[Configurable]] and [[Enumerable]] attributes and
  215. // set the rest of the property's attributes to their default values.
  216. if (object) {
  217. auto accessor = Accessor::create(object->vm(), nullptr, nullptr);
  218. object->storage_set(property_name, { accessor, current->attributes() });
  219. }
  220. }
  221. // c. Else,
  222. else {
  223. // If O is not undefined, convert the property named P of object O from an accessor property to a data property.
  224. // Preserve the existing values of the converted property's [[Configurable]] and [[Enumerable]] attributes and
  225. // set the rest of the property's attributes to their default values.
  226. if (object) {
  227. auto value = js_undefined();
  228. object->storage_set(property_name, { value, current->attributes() });
  229. }
  230. }
  231. }
  232. // 7. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc) are both true, then
  233. else if (current->is_data_descriptor() && descriptor.is_data_descriptor()) {
  234. // a. If current.[[Configurable]] is false and current.[[Writable]] is false, then
  235. if (!*current->configurable && !*current->writable) {
  236. // i. If Desc.[[Writable]] is present and Desc.[[Writable]] is true, return false.
  237. if (descriptor.writable.has_value() && *descriptor.writable)
  238. return false;
  239. // ii. If Desc.[[Value]] is present and SameValue(Desc.[[Value]], current.[[Value]]) is false, return false.
  240. if (descriptor.value.has_value() && !same_value(*descriptor.value, *current->value))
  241. return false;
  242. // iii. Return true.
  243. return true;
  244. }
  245. }
  246. // 8. Else,
  247. else {
  248. // a. Assert: ! IsAccessorDescriptor(current) and ! IsAccessorDescriptor(Desc) are both true.
  249. VERIFY(current->is_accessor_descriptor());
  250. VERIFY(descriptor.is_accessor_descriptor());
  251. // b. If current.[[Configurable]] is false, then
  252. if (!*current->configurable) {
  253. // i. If Desc.[[Set]] is present and SameValue(Desc.[[Set]], current.[[Set]]) is false, return false.
  254. if (descriptor.set.has_value() && *descriptor.set != *current->set)
  255. return false;
  256. // ii. If Desc.[[Get]] is present and SameValue(Desc.[[Get]], current.[[Get]]) is false, return false.
  257. if (descriptor.get.has_value() && *descriptor.get != *current->get)
  258. return false;
  259. // iii. Return true.
  260. return true;
  261. }
  262. }
  263. // 9. If O is not undefined, then
  264. if (object) {
  265. // a. For each field of Desc that is present, set the corresponding attribute of the property named P of object O to the value of the field.
  266. Value value;
  267. if (descriptor.is_accessor_descriptor() || (current->is_accessor_descriptor() && !descriptor.is_data_descriptor())) {
  268. auto* getter = descriptor.get.value_or(current->get.value_or(nullptr));
  269. auto* setter = descriptor.set.value_or(current->set.value_or(nullptr));
  270. value = Accessor::create(object->vm(), getter, setter);
  271. } else {
  272. value = descriptor.value.value_or(current->value.value_or({}));
  273. }
  274. PropertyAttributes attributes;
  275. attributes.set_writable(descriptor.writable.value_or(current->writable.value_or(false)));
  276. attributes.set_enumerable(descriptor.enumerable.value_or(current->enumerable.value_or(false)));
  277. attributes.set_configurable(descriptor.configurable.value_or(current->configurable.value_or(false)));
  278. object->storage_set(property_name, { value, attributes });
  279. }
  280. // 10. Return true.
  281. return true;
  282. }
  283. // 10.1.14 GetPrototypeFromConstructor ( constructor, intrinsicDefaultProto ), https://tc39.es/ecma262/#sec-getprototypefromconstructor
  284. ThrowCompletionOr<Object*> get_prototype_from_constructor(GlobalObject& global_object, FunctionObject const& constructor, Object* (GlobalObject::*intrinsic_default_prototype)())
  285. {
  286. auto& vm = global_object.vm();
  287. // 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.
  288. // 2. Let proto be ? Get(constructor, "prototype").
  289. auto prototype = constructor.get(vm.names.prototype);
  290. if (auto* exception = vm.exception())
  291. return throw_completion(exception->value());
  292. // 3. If Type(proto) is not Object, then
  293. if (!prototype.is_object()) {
  294. // a. Let realm be ? GetFunctionRealm(constructor).
  295. auto* realm = TRY(get_function_realm(global_object, constructor));
  296. // b. Set proto to realm's intrinsic object named intrinsicDefaultProto.
  297. prototype = (realm->global_object().*intrinsic_default_prototype)();
  298. }
  299. // 4. Return proto.
  300. return &prototype.as_object();
  301. }
  302. // 9.1.2.2 NewDeclarativeEnvironment ( E ), https://tc39.es/ecma262/#sec-newdeclarativeenvironment
  303. DeclarativeEnvironment* new_declarative_environment(Environment& environment)
  304. {
  305. auto& global_object = environment.global_object();
  306. return global_object.heap().allocate<DeclarativeEnvironment>(global_object, &environment);
  307. }
  308. // 9.1.2.3 NewObjectEnvironment ( O, W, E ), https://tc39.es/ecma262/#sec-newobjectenvironment
  309. ObjectEnvironment* new_object_environment(Object& object, bool is_with_environment, Environment* environment)
  310. {
  311. auto& global_object = object.global_object();
  312. return global_object.heap().allocate<ObjectEnvironment>(global_object, object, is_with_environment ? ObjectEnvironment::IsWithEnvironment::Yes : ObjectEnvironment::IsWithEnvironment::No, environment);
  313. }
  314. // 9.4.3 GetThisEnvironment ( ), https://tc39.es/ecma262/#sec-getthisenvironment
  315. Environment& get_this_environment(VM& vm)
  316. {
  317. for (auto* env = vm.lexical_environment(); env; env = env->outer_environment()) {
  318. if (env->has_this_binding())
  319. return *env;
  320. }
  321. VERIFY_NOT_REACHED();
  322. }
  323. // 13.3.7.2 GetSuperConstructor ( ), https://tc39.es/ecma262/#sec-getsuperconstructor
  324. Object* get_super_constructor(VM& vm)
  325. {
  326. // 1. Let envRec be GetThisEnvironment().
  327. auto& env = get_this_environment(vm);
  328. // 2. Assert: envRec is a function Environment Record.
  329. // 3. Let activeFunction be envRec.[[FunctionObject]].
  330. // 4. Assert: activeFunction is an ECMAScript function object.
  331. auto& active_function = verify_cast<FunctionEnvironment>(env).function_object();
  332. // 5. Let superConstructor be ! activeFunction.[[GetPrototypeOf]]().
  333. auto* super_constructor = MUST(active_function.internal_get_prototype_of());
  334. // 6. Return superConstructor.
  335. return super_constructor;
  336. }
  337. // 13.3.7.3 MakeSuperPropertyReference ( actualThis, propertyKey, strict ), https://tc39.es/ecma262/#sec-makesuperpropertyreference
  338. ThrowCompletionOr<Reference> make_super_property_reference(GlobalObject& global_object, Value actual_this, StringOrSymbol const& property_key, bool strict)
  339. {
  340. auto& vm = global_object.vm();
  341. // 1. Let env be GetThisEnvironment().
  342. auto& env = verify_cast<FunctionEnvironment>(get_this_environment(vm));
  343. // 2. Assert: env.HasSuperBinding() is true.
  344. VERIFY(env.has_super_binding());
  345. // 3. Let baseValue be ? env.GetSuperBase().
  346. auto base_value = env.get_super_base();
  347. // 4. Let bv be ? RequireObjectCoercible(baseValue).
  348. auto bv = TRY(require_object_coercible(global_object, base_value));
  349. // 5. Return the Reference Record { [[Base]]: bv, [[ReferencedName]]: propertyKey, [[Strict]]: strict, [[ThisValue]]: actualThis }.
  350. // 6. NOTE: This returns a Super Reference Record.
  351. return Reference { bv, property_key, actual_this, strict };
  352. }
  353. // 19.2.1.1 PerformEval ( x, callerRealm, strictCaller, direct ), https://tc39.es/ecma262/#sec-performeval
  354. ThrowCompletionOr<Value> perform_eval(Value x, GlobalObject& caller_realm, CallerMode strict_caller, EvalMode direct)
  355. {
  356. VERIFY(direct == EvalMode::Direct || strict_caller == CallerMode::NonStrict);
  357. if (!x.is_string())
  358. return x;
  359. auto& vm = caller_realm.vm();
  360. auto& eval_realm = vm.running_execution_context().realm;
  361. auto& code_string = x.as_string();
  362. Parser parser { Lexer { code_string.string() } };
  363. auto program = parser.parse_program(strict_caller == CallerMode::Strict);
  364. if (parser.has_errors()) {
  365. auto& error = parser.errors()[0];
  366. return vm.throw_completion<SyntaxError>(caller_realm, error.to_string());
  367. }
  368. auto strict_eval = strict_caller == CallerMode::Strict;
  369. if (program->is_strict_mode())
  370. strict_eval = true;
  371. auto& running_context = vm.running_execution_context();
  372. Environment* lexical_environment;
  373. Environment* variable_environment;
  374. if (direct == EvalMode::Direct) {
  375. lexical_environment = new_declarative_environment(*running_context.lexical_environment);
  376. variable_environment = running_context.variable_environment;
  377. } else {
  378. lexical_environment = new_declarative_environment(eval_realm->global_environment());
  379. variable_environment = &eval_realm->global_environment();
  380. }
  381. if (strict_eval)
  382. variable_environment = lexical_environment;
  383. // 18. If runningContext is not already suspended, suspend runningContext.
  384. // FIXME: We don't have this concept yet.
  385. ExecutionContext eval_context(vm.heap());
  386. eval_context.realm = eval_realm;
  387. eval_context.variable_environment = variable_environment;
  388. eval_context.lexical_environment = lexical_environment;
  389. vm.push_execution_context(eval_context, eval_realm->global_object());
  390. ScopeGuard pop_guard = [&] {
  391. vm.pop_execution_context();
  392. };
  393. TRY(eval_declaration_instantiation(vm, eval_realm->global_object(), program, variable_environment, lexical_environment, strict_eval));
  394. auto& interpreter = vm.interpreter();
  395. TemporaryChange scope_change_strict(vm.running_execution_context().is_strict_mode, strict_eval);
  396. // Note: We specifically use evaluate_statements here since we don't want to use global_declaration_instantiation from Program::execute.
  397. auto eval_result = program->evaluate_statements(interpreter, caller_realm);
  398. if (auto* exception = vm.exception())
  399. return throw_completion(exception->value());
  400. else
  401. return eval_result.value_or(js_undefined());
  402. }
  403. // 19.2.1.3 EvalDeclarationInstantiation ( body, varEnv, lexEnv, privateEnv, strict ), https://tc39.es/ecma262/#sec-evaldeclarationinstantiation
  404. ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, GlobalObject& global_object, Program const& program, Environment* variable_environment, Environment* lexical_environment, bool strict)
  405. {
  406. // FIXME: I'm not sure if the global object is correct here. And this is quite a crucial spot!
  407. GlobalEnvironment* global_var_environment = variable_environment->is_global_environment() ? static_cast<GlobalEnvironment*>(variable_environment) : nullptr;
  408. if (!strict) {
  409. if (global_var_environment) {
  410. program.for_each_var_declared_name([&](auto const& name) {
  411. if (global_var_environment->has_lexical_declaration(name)) {
  412. vm.throw_exception<SyntaxError>(global_object, ErrorType::FixmeAddAnErrorStringWithMessage, "Var already declared lexically");
  413. return IterationDecision::Break;
  414. }
  415. return IterationDecision::Continue;
  416. });
  417. }
  418. auto* this_environment = lexical_environment;
  419. while (this_environment != variable_environment) {
  420. if (!is<ObjectEnvironment>(*this_environment)) {
  421. program.for_each_var_declared_name([&](auto const& name) {
  422. if (this_environment->has_binding(name)) {
  423. vm.throw_exception<SyntaxError>(global_object, ErrorType::FixmeAddAnErrorStringWithMessage, "Var already declared lexically");
  424. return IterationDecision::Break;
  425. }
  426. // FIXME: NOTE: Annex B.3.4 defines alternate semantics for the above step.
  427. // In particular it only throw the syntax error if it is not an environment from a catchclause.
  428. return IterationDecision::Continue;
  429. });
  430. if (auto* exception = vm.exception())
  431. return throw_completion(exception->value());
  432. }
  433. this_environment = this_environment->outer_environment();
  434. VERIFY(this_environment);
  435. }
  436. }
  437. HashTable<FlyString> declared_function_names;
  438. Vector<FunctionDeclaration const&> functions_to_initialize;
  439. program.for_each_var_function_declaration_in_reverse_order([&](FunctionDeclaration const& function) {
  440. if (declared_function_names.set(function.name()) != AK::HashSetResult::InsertedNewEntry)
  441. return IterationDecision::Continue;
  442. if (global_var_environment) {
  443. auto function_definable = global_var_environment->can_declare_global_function(function.name());
  444. if (vm.exception())
  445. return IterationDecision::Break;
  446. if (!function_definable) {
  447. vm.throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorStringWithMessage, "Cannot define global function");
  448. return IterationDecision::Break;
  449. }
  450. }
  451. functions_to_initialize.append(function);
  452. return IterationDecision::Continue;
  453. });
  454. if (auto* exception = vm.exception())
  455. return throw_completion(exception->value());
  456. if (!strict) {
  457. // The spec here uses 'declaredVarNames' but that has not been declared yet.
  458. HashTable<FlyString> hoisted_functions;
  459. program.for_each_function_hoistable_with_annexB_extension([&](FunctionDeclaration& function_declaration) {
  460. auto& function_name = function_declaration.name();
  461. auto* this_environment = lexical_environment;
  462. while (this_environment != variable_environment) {
  463. if (!is<ObjectEnvironment>(*this_environment) && this_environment->has_binding(function_name))
  464. return IterationDecision::Continue;
  465. this_environment = this_environment->outer_environment();
  466. VERIFY(this_environment);
  467. }
  468. if (global_var_environment) {
  469. if (global_var_environment->has_lexical_declaration(function_name))
  470. return IterationDecision::Continue;
  471. auto var_definable = global_var_environment->can_declare_global_var(function_name);
  472. if (vm.exception())
  473. return IterationDecision::Break;
  474. if (!var_definable)
  475. return IterationDecision::Continue;
  476. }
  477. if (!declared_function_names.contains(function_name) && !hoisted_functions.contains(function_name)) {
  478. if (global_var_environment) {
  479. global_var_environment->create_global_var_binding(function_name, true);
  480. if (vm.exception())
  481. return IterationDecision::Break;
  482. } else {
  483. if (!variable_environment->has_binding(function_name)) {
  484. variable_environment->create_mutable_binding(global_object, function_name, true);
  485. variable_environment->initialize_binding(global_object, function_name, js_undefined());
  486. VERIFY(!vm.exception());
  487. }
  488. }
  489. hoisted_functions.set(function_name);
  490. }
  491. function_declaration.set_should_do_additional_annexB_steps();
  492. return IterationDecision::Continue;
  493. });
  494. if (auto* exception = vm.exception())
  495. return throw_completion(exception->value());
  496. }
  497. HashTable<FlyString> declared_var_names;
  498. program.for_each_var_scoped_variable_declaration([&](VariableDeclaration const& declaration) {
  499. declaration.for_each_bound_name([&](auto const& name) {
  500. if (!declared_function_names.contains(name)) {
  501. if (global_var_environment) {
  502. auto variable_definable = global_var_environment->can_declare_global_var(name);
  503. if (vm.exception())
  504. return IterationDecision::Break;
  505. if (!variable_definable) {
  506. vm.throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorStringWithMessage, "Cannot define global var");
  507. return IterationDecision::Break;
  508. }
  509. }
  510. declared_var_names.set(name);
  511. }
  512. return IterationDecision::Continue;
  513. });
  514. if (vm.exception())
  515. return IterationDecision::Break;
  516. return IterationDecision::Continue;
  517. });
  518. if (auto* exception = vm.exception())
  519. return throw_completion(exception->value());
  520. // 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.
  521. program.for_each_lexically_scoped_declaration([&](Declaration const& declaration) {
  522. declaration.for_each_bound_name([&](auto const& name) {
  523. if (declaration.is_constant_declaration())
  524. lexical_environment->create_immutable_binding(global_object, name, true);
  525. else
  526. lexical_environment->create_mutable_binding(global_object, name, false);
  527. if (vm.exception())
  528. return IterationDecision::Break;
  529. return IterationDecision::Continue;
  530. });
  531. if (vm.exception())
  532. return IterationDecision::Break;
  533. return IterationDecision::Continue;
  534. });
  535. if (auto* exception = vm.exception())
  536. return throw_completion(exception->value());
  537. for (auto& declaration : functions_to_initialize) {
  538. auto* function = ECMAScriptFunctionObject::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), lexical_environment, declaration.kind(), declaration.is_strict_mode());
  539. if (global_var_environment) {
  540. global_var_environment->create_global_function_binding(declaration.name(), function, true);
  541. if (auto* exception = vm.exception())
  542. return throw_completion(exception->value());
  543. } else {
  544. auto binding_exists = variable_environment->has_binding(declaration.name());
  545. if (!binding_exists) {
  546. variable_environment->create_mutable_binding(global_object, declaration.name(), true);
  547. if (auto* exception = vm.exception())
  548. return throw_completion(exception->value());
  549. variable_environment->initialize_binding(global_object, declaration.name(), function);
  550. } else {
  551. variable_environment->set_mutable_binding(global_object, declaration.name(), function, false);
  552. }
  553. if (auto* exception = vm.exception())
  554. return throw_completion(exception->value());
  555. }
  556. }
  557. for (auto& var_name : declared_var_names) {
  558. if (global_var_environment) {
  559. global_var_environment->create_global_var_binding(var_name, true);
  560. if (auto* exception = vm.exception())
  561. return throw_completion(exception->value());
  562. } else {
  563. auto binding_exists = variable_environment->has_binding(var_name);
  564. if (!binding_exists) {
  565. variable_environment->create_mutable_binding(global_object, var_name, true);
  566. if (auto* exception = vm.exception())
  567. return throw_completion(exception->value());
  568. variable_environment->initialize_binding(global_object, var_name, js_undefined());
  569. if (auto* exception = vm.exception())
  570. return throw_completion(exception->value());
  571. }
  572. }
  573. }
  574. return {};
  575. }
  576. // 10.4.4.6 CreateUnmappedArgumentsObject ( argumentsList ), https://tc39.es/ecma262/#sec-createunmappedargumentsobject
  577. Object* create_unmapped_arguments_object(GlobalObject& global_object, Span<Value> arguments)
  578. {
  579. auto& vm = global_object.vm();
  580. // 1. Let len be the number of elements in argumentsList.
  581. auto length = arguments.size();
  582. // 2. Let obj be ! OrdinaryObjectCreate(%Object.prototype%, « [[ParameterMap]] »).
  583. // 3. Set obj.[[ParameterMap]] to undefined.
  584. auto* object = Object::create(global_object, global_object.object_prototype());
  585. object->set_has_parameter_map();
  586. // 4. Perform DefinePropertyOrThrow(obj, "length", PropertyDescriptor { [[Value]]: 𝔽(len), [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }).
  587. object->define_property_or_throw(vm.names.length, { .value = Value(length), .writable = true, .enumerable = false, .configurable = true });
  588. VERIFY(!vm.exception());
  589. // 5. Let index be 0.
  590. // 6. Repeat, while index < len,
  591. for (size_t index = 0; index < length; ++index) {
  592. // a. Let val be argumentsList[index].
  593. auto value = arguments[index];
  594. // b. Perform ! CreateDataPropertyOrThrow(obj, ! ToString(𝔽(index)), val).
  595. object->create_data_property_or_throw(index, value);
  596. VERIFY(!vm.exception());
  597. // c. Set index to index + 1.
  598. }
  599. // 7. Perform ! DefinePropertyOrThrow(obj, @@iterator, PropertyDescriptor { [[Value]]: %Array.prototype.values%, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }).
  600. auto* array_prototype_values = global_object.array_prototype_values_function();
  601. object->define_property_or_throw(*vm.well_known_symbol_iterator(), { .value = array_prototype_values, .writable = true, .enumerable = false, .configurable = true });
  602. VERIFY(!vm.exception());
  603. // 8. Perform ! DefinePropertyOrThrow(obj, "callee", PropertyDescriptor { [[Get]]: %ThrowTypeError%, [[Set]]: %ThrowTypeError%, [[Enumerable]]: false, [[Configurable]]: false }).
  604. auto* throw_type_error = global_object.throw_type_error_function();
  605. object->define_property_or_throw(vm.names.callee, { .get = throw_type_error, .set = throw_type_error, .enumerable = false, .configurable = false });
  606. VERIFY(!vm.exception());
  607. // 9. Return obj.
  608. return object;
  609. }
  610. // 10.4.4.7 CreateMappedArgumentsObject ( func, formals, argumentsList, env ), https://tc39.es/ecma262/#sec-createmappedargumentsobject
  611. Object* create_mapped_arguments_object(GlobalObject& global_object, FunctionObject& function, Vector<FunctionNode::Parameter> const& formals, Span<Value> arguments, Environment& environment)
  612. {
  613. auto& vm = global_object.vm();
  614. // 1. Assert: formals does not contain a rest parameter, any binding patterns, or any initializers. It may contain duplicate identifiers.
  615. // 2. Let len be the number of elements in argumentsList.
  616. VERIFY(arguments.size() <= NumericLimits<i32>::max());
  617. i32 length = static_cast<i32>(arguments.size());
  618. // 3. Let obj be ! MakeBasicObject(« [[Prototype]], [[Extensible]], [[ParameterMap]] »).
  619. // 4. Set obj.[[GetOwnProperty]] as specified in 10.4.4.1.
  620. // 5. Set obj.[[DefineOwnProperty]] as specified in 10.4.4.2.
  621. // 6. Set obj.[[Get]] as specified in 10.4.4.3.
  622. // 7. Set obj.[[Set]] as specified in 10.4.4.4.
  623. // 8. Set obj.[[Delete]] as specified in 10.4.4.5.
  624. // 9. Set obj.[[Prototype]] to %Object.prototype%.
  625. auto* object = vm.heap().allocate<ArgumentsObject>(global_object, global_object, environment);
  626. VERIFY(!vm.exception());
  627. // 14. Let index be 0.
  628. // 15. Repeat, while index < len,
  629. for (i32 index = 0; index < length; ++index) {
  630. // a. Let val be argumentsList[index].
  631. auto value = arguments[index];
  632. // b. Perform ! CreateDataPropertyOrThrow(obj, ! ToString(𝔽(index)), val).
  633. object->create_data_property_or_throw(index, value);
  634. VERIFY(!vm.exception());
  635. // c. Set index to index + 1.
  636. }
  637. // 16. Perform ! DefinePropertyOrThrow(obj, "length", PropertyDescriptor { [[Value]]: 𝔽(len), [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }).
  638. object->define_property_or_throw(vm.names.length, { .value = Value(length), .writable = true, .enumerable = false, .configurable = true });
  639. VERIFY(!vm.exception());
  640. // 17. Let mappedNames be a new empty List.
  641. HashTable<FlyString> mapped_names;
  642. // 18. Set index to numberOfParameters - 1.
  643. // 19. Repeat, while index ≥ 0,
  644. VERIFY(formals.size() <= NumericLimits<i32>::max());
  645. for (i32 index = static_cast<i32>(formals.size()) - 1; index >= 0; --index) {
  646. // a. Let name be parameterNames[index].
  647. auto const& name = formals[index].binding.get<FlyString>();
  648. // b. If name is not an element of mappedNames, then
  649. if (mapped_names.contains(name))
  650. continue;
  651. // i. Add name as an element of the list mappedNames.
  652. mapped_names.set(name);
  653. // ii. If index < len, then
  654. if (index < length) {
  655. // 1. Let g be MakeArgGetter(name, env).
  656. // 2. Let p be MakeArgSetter(name, env).
  657. // 3. Perform map.[[DefineOwnProperty]](! ToString(𝔽(index)), PropertyDescriptor { [[Set]]: p, [[Get]]: g, [[Enumerable]]: false, [[Configurable]]: true }).
  658. object->parameter_map().define_native_accessor(
  659. String::number(index),
  660. [&environment, name](VM&, GlobalObject& global_object_getter) -> Value {
  661. return environment.get_binding_value(global_object_getter, name, false);
  662. },
  663. [&environment, name](VM& vm, GlobalObject& global_object_setter) {
  664. environment.set_mutable_binding(global_object_setter, name, vm.argument(0), false);
  665. return js_undefined();
  666. },
  667. Attribute::Configurable);
  668. }
  669. }
  670. // 20. Perform ! DefinePropertyOrThrow(obj, @@iterator, PropertyDescriptor { [[Value]]: %Array.prototype.values%, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }).
  671. auto* array_prototype_values = global_object.array_prototype_values_function();
  672. object->define_property_or_throw(*vm.well_known_symbol_iterator(), { .value = array_prototype_values, .writable = true, .enumerable = false, .configurable = true });
  673. VERIFY(!vm.exception());
  674. // 21. Perform ! DefinePropertyOrThrow(obj, "callee", PropertyDescriptor { [[Value]]: func, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }).
  675. object->define_property_or_throw(vm.names.callee, { .value = &function, .writable = true, .enumerable = false, .configurable = true });
  676. VERIFY(!vm.exception());
  677. // 22. Return obj.
  678. return object;
  679. }
  680. // 7.1.21 CanonicalNumericIndexString ( argument ), https://tc39.es/ecma262/#sec-canonicalnumericindexstring
  681. Value canonical_numeric_index_string(GlobalObject& global_object, PropertyName const& property_name)
  682. {
  683. // NOTE: If the property name is a number type (An implementation-defined optimized
  684. // property key type), it can be treated as a string property that has already been
  685. // converted successfully into a canonical numeric index.
  686. VERIFY(property_name.is_string() || property_name.is_number());
  687. if (property_name.is_number())
  688. return Value(property_name.as_number());
  689. // 1. Assert: Type(argument) is String.
  690. auto argument = Value(js_string(global_object.vm(), property_name.as_string()));
  691. // 2. If argument is "-0", return -0𝔽.
  692. if (argument.as_string().string() == "-0")
  693. return Value(-0.0);
  694. // 3. Let n be ! ToNumber(argument).
  695. auto n = argument.to_number(global_object);
  696. // 4. If SameValue(! ToString(n), argument) is false, return undefined.
  697. if (!same_value(n.to_primitive_string(global_object), argument))
  698. return js_undefined();
  699. // 5. Return n.
  700. return n;
  701. }
  702. // 22.1.3.17.1 GetSubstitution ( matched, str, position, captures, namedCaptures, replacement ), https://tc39.es/ecma262/#sec-getsubstitution
  703. ThrowCompletionOr<String> get_substitution(GlobalObject& global_object, Utf16View const& matched, Utf16View const& str, size_t position, Span<Value> captures, Value named_captures, Value replacement)
  704. {
  705. auto& vm = global_object.vm();
  706. auto replace_string = replacement.to_utf16_string(global_object);
  707. if (auto* exception = vm.exception())
  708. return throw_completion(exception->value());
  709. auto replace_view = replace_string.view();
  710. StringBuilder result;
  711. for (size_t i = 0; i < replace_view.length_in_code_units(); ++i) {
  712. u16 curr = replace_view.code_unit_at(i);
  713. if ((curr != '$') || (i + 1 >= replace_view.length_in_code_units())) {
  714. result.append(curr);
  715. continue;
  716. }
  717. u16 next = replace_view.code_unit_at(i + 1);
  718. if (next == '$') {
  719. result.append('$');
  720. ++i;
  721. } else if (next == '&') {
  722. result.append(matched);
  723. ++i;
  724. } else if (next == '`') {
  725. auto substring = str.substring_view(0, position);
  726. result.append(substring);
  727. ++i;
  728. } else if (next == '\'') {
  729. auto tail_pos = position + matched.length_in_code_units();
  730. if (tail_pos < str.length_in_code_units()) {
  731. auto substring = str.substring_view(tail_pos);
  732. result.append(substring);
  733. }
  734. ++i;
  735. } else if (is_ascii_digit(next)) {
  736. bool is_two_digits = (i + 2 < replace_view.length_in_code_units()) && is_ascii_digit(replace_view.code_unit_at(i + 2));
  737. auto capture_postition_string = replace_view.substring_view(i + 1, is_two_digits ? 2 : 1).to_utf8();
  738. auto capture_position = capture_postition_string.to_uint();
  739. if (capture_position.has_value() && (*capture_position > 0) && (*capture_position <= captures.size())) {
  740. auto& value = captures[*capture_position - 1];
  741. if (!value.is_undefined()) {
  742. auto value_string = value.to_string(global_object);
  743. if (auto* exception = vm.exception())
  744. return throw_completion(exception->value());
  745. result.append(value_string);
  746. }
  747. i += is_two_digits ? 2 : 1;
  748. } else {
  749. result.append(curr);
  750. }
  751. } else if (next == '<') {
  752. auto start_position = i + 2;
  753. Optional<size_t> end_position;
  754. for (size_t j = start_position; j < replace_view.length_in_code_units(); ++j) {
  755. if (replace_view.code_unit_at(j) == '>') {
  756. end_position = j;
  757. break;
  758. }
  759. }
  760. if (named_captures.is_undefined() || !end_position.has_value()) {
  761. result.append(curr);
  762. } else {
  763. auto group_name_view = replace_view.substring_view(start_position, *end_position - start_position);
  764. auto group_name = group_name_view.to_utf8(Utf16View::AllowInvalidCodeUnits::Yes);
  765. auto capture = named_captures.as_object().get(group_name);
  766. if (auto* exception = vm.exception())
  767. return throw_completion(exception->value());
  768. if (!capture.is_undefined()) {
  769. auto capture_string = capture.to_string(global_object);
  770. if (auto* exception = vm.exception())
  771. return throw_completion(exception->value());
  772. result.append(capture_string);
  773. }
  774. i = *end_position;
  775. }
  776. } else {
  777. result.append(curr);
  778. }
  779. }
  780. return result.build();
  781. }
  782. }