AbstractOperations.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Concepts.h>
  8. #include <AK/Forward.h>
  9. #include <LibCrypto/Forward.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibJS/Heap/MarkedVector.h>
  12. #include <LibJS/Runtime/CanonicalIndex.h>
  13. #include <LibJS/Runtime/FunctionObject.h>
  14. #include <LibJS/Runtime/GlobalObject.h>
  15. #include <LibJS/Runtime/Iterator.h>
  16. #include <LibJS/Runtime/PrivateEnvironment.h>
  17. #include <LibJS/Runtime/Value.h>
  18. namespace JS {
  19. NonnullGCPtr<DeclarativeEnvironment> new_declarative_environment(Environment&);
  20. NonnullGCPtr<ObjectEnvironment> new_object_environment(Object&, bool is_with_environment, Environment*);
  21. NonnullGCPtr<FunctionEnvironment> new_function_environment(ECMAScriptFunctionObject&, Object* new_target);
  22. NonnullGCPtr<PrivateEnvironment> new_private_environment(VM& vm, PrivateEnvironment* outer);
  23. NonnullGCPtr<Environment> get_this_environment(VM&);
  24. bool can_be_held_weakly(Value);
  25. Object* get_super_constructor(VM&);
  26. ThrowCompletionOr<Reference> make_super_property_reference(VM&, Value actual_this, PropertyKey const&, bool strict);
  27. ThrowCompletionOr<Value> require_object_coercible(VM&, Value);
  28. ThrowCompletionOr<Value> call_impl(VM&, Value function, Value this_value, Optional<MarkedVector<Value>> = {});
  29. ThrowCompletionOr<Value> call_impl(VM&, FunctionObject& function, Value this_value, Optional<MarkedVector<Value>> = {});
  30. ThrowCompletionOr<NonnullGCPtr<Object>> construct_impl(VM&, FunctionObject&, Optional<MarkedVector<Value>> = {}, FunctionObject* new_target = nullptr);
  31. ThrowCompletionOr<size_t> length_of_array_like(VM&, Object const&);
  32. ThrowCompletionOr<MarkedVector<Value>> create_list_from_array_like(VM&, Value, Function<ThrowCompletionOr<void>(Value)> = {});
  33. ThrowCompletionOr<FunctionObject*> species_constructor(VM&, Object const&, FunctionObject& default_constructor);
  34. ThrowCompletionOr<Realm*> get_function_realm(VM&, FunctionObject const&);
  35. ThrowCompletionOr<void> initialize_bound_name(VM&, DeprecatedFlyString const&, Value, Environment*);
  36. bool is_compatible_property_descriptor(bool extensible, PropertyDescriptor const&, Optional<PropertyDescriptor> const& current);
  37. bool validate_and_apply_property_descriptor(Object*, PropertyKey const&, bool extensible, PropertyDescriptor const&, Optional<PropertyDescriptor> const& current);
  38. ThrowCompletionOr<Object*> get_prototype_from_constructor(VM&, FunctionObject const& constructor, NonnullGCPtr<Object> (Intrinsics::*intrinsic_default_prototype)());
  39. Object* create_unmapped_arguments_object(VM&, Span<Value> arguments);
  40. Object* create_mapped_arguments_object(VM&, FunctionObject&, Vector<FunctionParameter> const&, Span<Value> arguments, Environment&);
  41. struct DisposableResource {
  42. Value resource_value;
  43. NonnullGCPtr<FunctionObject> dispose_method;
  44. };
  45. ThrowCompletionOr<void> add_disposable_resource(VM&, Vector<DisposableResource>& disposable, Value, Environment::InitializeBindingHint, FunctionObject* = nullptr);
  46. ThrowCompletionOr<DisposableResource> create_disposable_resource(VM&, Value, Environment::InitializeBindingHint, FunctionObject* method = nullptr);
  47. ThrowCompletionOr<GCPtr<FunctionObject>> get_dispose_method(VM&, Value, Environment::InitializeBindingHint);
  48. Completion dispose(VM& vm, Value, NonnullGCPtr<FunctionObject> method);
  49. Completion dispose_resources(VM& vm, Vector<DisposableResource> const& disposable, Completion completion);
  50. Completion dispose_resources(VM& vm, GCPtr<DeclarativeEnvironment> disposable, Completion completion);
  51. ThrowCompletionOr<Value> perform_import_call(VM&, Value specifier, Value options_value);
  52. enum class CanonicalIndexMode {
  53. DetectNumericRoundtrip,
  54. IgnoreNumericRoundtrip,
  55. };
  56. [[nodiscard]] CanonicalIndex canonical_numeric_index_string(PropertyKey const&, CanonicalIndexMode needs_numeric);
  57. ThrowCompletionOr<String> get_substitution(VM&, Utf16View const& matched, Utf16View const& str, size_t position, Span<Value> captures, Value named_captures, Value replacement);
  58. enum class CallerMode {
  59. Strict,
  60. NonStrict
  61. };
  62. enum class EvalMode {
  63. Direct,
  64. Indirect
  65. };
  66. ThrowCompletionOr<Value> perform_eval(VM&, Value, CallerMode, EvalMode);
  67. ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, Program const& program, Environment* variable_environment, Environment* lexical_environment, PrivateEnvironment* private_environment, bool strict);
  68. // 7.3.14 Call ( F, V [ , argumentsList ] ), https://tc39.es/ecma262/#sec-call
  69. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, Value function, Value this_value, MarkedVector<Value> arguments_list)
  70. {
  71. return call_impl(vm, function, this_value, move(arguments_list));
  72. }
  73. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, Value function, Value this_value, Optional<MarkedVector<Value>> arguments_list)
  74. {
  75. return call_impl(vm, function, this_value, move(arguments_list));
  76. }
  77. template<typename... Args>
  78. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, Value function, Value this_value, Args&&... args)
  79. {
  80. if constexpr (sizeof...(Args) > 0) {
  81. MarkedVector<Value> arguments_list { vm.heap() };
  82. (..., arguments_list.append(forward<Args>(args)));
  83. return call_impl(vm, function, this_value, move(arguments_list));
  84. }
  85. return call_impl(vm, function, this_value);
  86. }
  87. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, FunctionObject& function, Value this_value, MarkedVector<Value> arguments_list)
  88. {
  89. return call_impl(vm, function, this_value, move(arguments_list));
  90. }
  91. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, FunctionObject& function, Value this_value, Optional<MarkedVector<Value>> arguments_list)
  92. {
  93. return call_impl(vm, function, this_value, move(arguments_list));
  94. }
  95. template<typename... Args>
  96. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, FunctionObject& function, Value this_value, Args&&... args)
  97. {
  98. if constexpr (sizeof...(Args) > 0) {
  99. MarkedVector<Value> arguments_list { vm.heap() };
  100. (..., arguments_list.append(forward<Args>(args)));
  101. return call_impl(vm, function, this_value, move(arguments_list));
  102. }
  103. return call_impl(vm, function, this_value);
  104. }
  105. // 7.3.15 Construct ( F [ , argumentsList [ , newTarget ] ] ), https://tc39.es/ecma262/#sec-construct
  106. template<typename... Args>
  107. ALWAYS_INLINE ThrowCompletionOr<NonnullGCPtr<Object>> construct(VM& vm, FunctionObject& function, Args&&... args)
  108. {
  109. if constexpr (sizeof...(Args) > 0) {
  110. MarkedVector<Value> arguments_list { vm.heap() };
  111. (..., arguments_list.append(forward<Args>(args)));
  112. return construct_impl(vm, function, move(arguments_list));
  113. }
  114. return construct_impl(vm, function);
  115. }
  116. ALWAYS_INLINE ThrowCompletionOr<NonnullGCPtr<Object>> construct(VM& vm, FunctionObject& function, MarkedVector<Value> arguments_list, FunctionObject* new_target = nullptr)
  117. {
  118. return construct_impl(vm, function, move(arguments_list), new_target);
  119. }
  120. ALWAYS_INLINE ThrowCompletionOr<NonnullGCPtr<Object>> construct(VM& vm, FunctionObject& function, Optional<MarkedVector<Value>> arguments_list, FunctionObject* new_target = nullptr)
  121. {
  122. return construct_impl(vm, function, move(arguments_list), new_target);
  123. }
  124. // 10.1.13 OrdinaryCreateFromConstructor ( constructor, intrinsicDefaultProto [ , internalSlotsList ] ), https://tc39.es/ecma262/#sec-ordinarycreatefromconstructor
  125. template<typename T, typename... Args>
  126. ThrowCompletionOr<NonnullGCPtr<T>> ordinary_create_from_constructor(VM& vm, FunctionObject const& constructor, NonnullGCPtr<Object> (Intrinsics::*intrinsic_default_prototype)(), Args&&... args)
  127. {
  128. auto& realm = *vm.current_realm();
  129. auto* prototype = TRY(get_prototype_from_constructor(vm, constructor, intrinsic_default_prototype));
  130. return MUST_OR_THROW_OOM(realm.heap().allocate<T>(realm, forward<Args>(args)..., *prototype));
  131. }
  132. // 14.1 MergeLists ( a, b ), https://tc39.es/proposal-temporal/#sec-temporal-mergelists
  133. template<typename T>
  134. Vector<T> merge_lists(Vector<T> const& a, Vector<T> const& b)
  135. {
  136. // 1. Let merged be a new empty List.
  137. Vector<T> merged;
  138. // 2. For each element element of a, do
  139. for (auto const& element : a) {
  140. // a. If merged does not contain element, then
  141. if (!merged.contains_slow(element)) {
  142. // i. Append element to merged.
  143. merged.append(element);
  144. }
  145. }
  146. // 3. For each element element of b, do
  147. for (auto const& element : b) {
  148. // a. If merged does not contain element, then
  149. if (!merged.contains_slow(element)) {
  150. // i. Append element to merged.
  151. merged.append(element);
  152. }
  153. }
  154. // 4. Return merged.
  155. return merged;
  156. }
  157. // 4.2 AddValueToKeyedGroup ( groups, key, value ), https://tc39.es/proposal-array-grouping/#sec-add-value-to-keyed-group
  158. template<typename GroupsType, typename KeyType>
  159. void add_value_to_keyed_group(VM& vm, GroupsType& groups, KeyType key, Value value)
  160. {
  161. // 1. For each Record { [[Key]], [[Elements]] } g of groups, do
  162. // a. If SameValue(g.[[Key]], key) is true, then
  163. // NOTE: This is performed in KeyedGroupTraits::equals for groupToMap and Traits<JS::PropertyKey>::equals for group.
  164. auto existing_elements_iterator = groups.find(key);
  165. if (existing_elements_iterator != groups.end()) {
  166. // i. Assert: exactly one element of groups meets this criteria.
  167. // NOTE: This is done on insertion into the hash map, as only `set` tells us if we overrode an entry.
  168. // ii. Append value as the last element of g.[[Elements]].
  169. existing_elements_iterator->value.append(value);
  170. // iii. Return unused.
  171. return;
  172. }
  173. // 2. Let group be the Record { [[Key]]: key, [[Elements]]: « value » }.
  174. MarkedVector<Value> new_elements { vm.heap() };
  175. new_elements.append(value);
  176. // 3. Append group as the last element of groups.
  177. auto result = groups.set(key, move(new_elements));
  178. VERIFY(result == AK::HashSetResult::InsertedNewEntry);
  179. // 4. Return unused.
  180. }
  181. // 4.1 GroupBy ( items, callbackfn, keyCoercion ), https://tc39.es/proposal-array-grouping/#sec-group-by
  182. template<typename GroupsType, typename KeyType>
  183. ThrowCompletionOr<GroupsType> group_by(VM& vm, Value items, Value callback_function)
  184. {
  185. // 1. Perform ? RequireObjectCoercible(items).
  186. TRY(require_object_coercible(vm, items));
  187. // 2. If IsCallable(callbackfn) is false, throw a TypeError exception.
  188. if (!callback_function.is_function())
  189. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, TRY_OR_THROW_OOM(vm, callback_function.to_string_without_side_effects()));
  190. // 3. Let groups be a new empty List.
  191. GroupsType groups;
  192. // 4. Let iteratorRecord be ? GetIterator(items).
  193. // FIXME: The Array Grouping proposal is out of date - the `kind` parameter is now required.
  194. auto iterator_record = TRY(get_iterator(vm, items, IteratorHint::Sync));
  195. // 5. Let k be 0.
  196. u64 k = 0;
  197. // 6. Repeat,
  198. while (true) {
  199. // a. If k ≥ 2^53 - 1, then
  200. if (k >= MAX_ARRAY_LIKE_INDEX) {
  201. // i. Let error be ThrowCompletion(a newly created TypeError object).
  202. auto error = vm.throw_completion<TypeError>(ErrorType::ArrayMaxSize);
  203. // ii. Return ? IteratorClose(iteratorRecord, error).
  204. return iterator_close(vm, iterator_record, move(error));
  205. }
  206. // b. Let next be ? IteratorStep(iteratorRecord).
  207. auto next = TRY(iterator_step(vm, iterator_record));
  208. // c. If next is false, then
  209. if (!next) {
  210. // i. Return groups.
  211. return ThrowCompletionOr<GroupsType> { move(groups) };
  212. }
  213. // d. Let value be ? IteratorValue(next).
  214. auto value = TRY(iterator_value(vm, *next));
  215. // e. Let key be Completion(Call(callbackfn, undefined, « value, 𝔽(k) »)).
  216. auto key = call(vm, callback_function, js_undefined(), value, Value(k));
  217. // f. IfAbruptCloseIterator(key, iteratorRecord).
  218. if (key.is_error())
  219. return Completion { *TRY(iterator_close(vm, iterator_record, key.release_error())) };
  220. // g. If keyCoercion is property, then
  221. if constexpr (IsSame<KeyType, PropertyKey>) {
  222. // i. Set key to Completion(ToPropertyKey(key)).
  223. auto property_key = key.value().to_property_key(vm);
  224. // ii. IfAbruptCloseIterator(key, iteratorRecord).
  225. if (property_key.is_error())
  226. return Completion { *TRY(iterator_close(vm, iterator_record, property_key.release_error())) };
  227. add_value_to_keyed_group(vm, groups, property_key.release_value(), value);
  228. }
  229. // h. Else,
  230. else {
  231. // i. Assert: keyCoercion is zero.
  232. static_assert(IsSame<KeyType, void>);
  233. // ii. If key is -0𝔽, set key to +0𝔽.
  234. if (key.value().is_negative_zero())
  235. key = Value(0);
  236. add_value_to_keyed_group(vm, groups, make_handle(key.release_value()), value);
  237. }
  238. // i. Perform AddValueToKeyedGroup(groups, key, value).
  239. // NOTE: This is dependent on the `key_coercion` template parameter and thus done separately in the branches above.
  240. // j. Set k to k + 1.
  241. ++k;
  242. }
  243. }
  244. // x modulo y, https://tc39.es/ecma262/#eqn-modulo
  245. template<Arithmetic T, Arithmetic U>
  246. auto modulo(T x, U y)
  247. {
  248. // The notation “x modulo y” (y must be finite and non-zero) computes a value k of the same sign as y (or zero) such that abs(k) < abs(y) and x - k = q × y for some integer q.
  249. VERIFY(y != 0);
  250. if constexpr (IsFloatingPoint<T> || IsFloatingPoint<U>) {
  251. if constexpr (IsFloatingPoint<U>)
  252. VERIFY(isfinite(y));
  253. return fmod(fmod(x, y) + y, y);
  254. } else {
  255. return ((x % y) + y) % y;
  256. }
  257. }
  258. auto modulo(Crypto::BigInteger auto const& x, Crypto::BigInteger auto const& y)
  259. {
  260. VERIFY(!y.is_zero());
  261. auto result = x.divided_by(y).remainder;
  262. if (result.is_negative())
  263. result = result.plus(y);
  264. return result;
  265. }
  266. }