AbstractOperations.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/PrivateEnvironment.h>
  16. #include <LibJS/Runtime/Value.h>
  17. namespace JS {
  18. NonnullGCPtr<DeclarativeEnvironment> new_declarative_environment(Environment&);
  19. NonnullGCPtr<ObjectEnvironment> new_object_environment(Object&, bool is_with_environment, Environment*);
  20. NonnullGCPtr<FunctionEnvironment> new_function_environment(ECMAScriptFunctionObject&, Object* new_target);
  21. NonnullGCPtr<PrivateEnvironment> new_private_environment(VM& vm, PrivateEnvironment* outer);
  22. NonnullGCPtr<Environment> get_this_environment(VM&);
  23. bool can_be_held_weakly(Value);
  24. Object* get_super_constructor(VM&);
  25. ThrowCompletionOr<Reference> make_super_property_reference(VM&, Value actual_this, PropertyKey const&, bool strict);
  26. ThrowCompletionOr<Value> require_object_coercible(VM&, Value);
  27. ThrowCompletionOr<Value> call_impl(VM&, Value function, Value this_value, Optional<MarkedVector<Value>> = {});
  28. ThrowCompletionOr<Value> call_impl(VM&, FunctionObject& function, Value this_value, Optional<MarkedVector<Value>> = {});
  29. ThrowCompletionOr<NonnullGCPtr<Object>> construct_impl(VM&, FunctionObject&, Optional<MarkedVector<Value>> = {}, FunctionObject* new_target = nullptr);
  30. ThrowCompletionOr<size_t> length_of_array_like(VM&, Object const&);
  31. ThrowCompletionOr<MarkedVector<Value>> create_list_from_array_like(VM&, Value, Function<ThrowCompletionOr<void>(Value)> = {});
  32. ThrowCompletionOr<FunctionObject*> species_constructor(VM&, Object const&, FunctionObject& default_constructor);
  33. ThrowCompletionOr<Realm*> get_function_realm(VM&, FunctionObject const&);
  34. ThrowCompletionOr<void> initialize_bound_name(VM&, DeprecatedFlyString const&, Value, Environment*);
  35. bool is_compatible_property_descriptor(bool extensible, PropertyDescriptor const&, Optional<PropertyDescriptor> const& current);
  36. bool validate_and_apply_property_descriptor(Object*, PropertyKey const&, bool extensible, PropertyDescriptor const&, Optional<PropertyDescriptor> const& current);
  37. ThrowCompletionOr<Object*> get_prototype_from_constructor(VM&, FunctionObject const& constructor, NonnullGCPtr<Object> (Intrinsics::*intrinsic_default_prototype)());
  38. Object* create_unmapped_arguments_object(VM&, Span<Value> arguments);
  39. Object* create_mapped_arguments_object(VM&, FunctionObject&, Vector<FunctionParameter> const&, Span<Value> arguments, Environment&);
  40. struct DisposableResource {
  41. Value resource_value;
  42. NonnullGCPtr<FunctionObject> dispose_method;
  43. };
  44. ThrowCompletionOr<void> add_disposable_resource(VM&, Vector<DisposableResource>& disposable, Value, Environment::InitializeBindingHint, FunctionObject* = nullptr);
  45. ThrowCompletionOr<DisposableResource> create_disposable_resource(VM&, Value, Environment::InitializeBindingHint, FunctionObject* method = nullptr);
  46. ThrowCompletionOr<GCPtr<FunctionObject>> get_dispose_method(VM&, Value, Environment::InitializeBindingHint);
  47. Completion dispose(VM& vm, Value, NonnullGCPtr<FunctionObject> method);
  48. Completion dispose_resources(VM& vm, Vector<DisposableResource> const& disposable, Completion completion);
  49. Completion dispose_resources(VM& vm, GCPtr<DeclarativeEnvironment> disposable, Completion completion);
  50. ThrowCompletionOr<Value> perform_import_call(VM&, Value specifier, Value options_value);
  51. enum class CanonicalIndexMode {
  52. DetectNumericRoundtrip,
  53. IgnoreNumericRoundtrip,
  54. };
  55. ThrowCompletionOr<CanonicalIndex> canonical_numeric_index_string(VM&, PropertyKey const&, CanonicalIndexMode needs_numeric);
  56. ThrowCompletionOr<String> get_substitution(VM&, Utf16View const& matched, Utf16View const& str, size_t position, Span<Value> captures, Value named_captures, Value replacement);
  57. enum class CallerMode {
  58. Strict,
  59. NonStrict
  60. };
  61. enum class EvalMode {
  62. Direct,
  63. Indirect
  64. };
  65. ThrowCompletionOr<Value> perform_eval(VM&, Value, CallerMode, EvalMode);
  66. ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, Program const& program, Environment* variable_environment, Environment* lexical_environment, PrivateEnvironment* private_environment, bool strict);
  67. // 7.3.14 Call ( F, V [ , argumentsList ] ), https://tc39.es/ecma262/#sec-call
  68. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, Value function, Value this_value, MarkedVector<Value> arguments_list)
  69. {
  70. return call_impl(vm, function, this_value, move(arguments_list));
  71. }
  72. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, Value function, Value this_value, Optional<MarkedVector<Value>> arguments_list)
  73. {
  74. return call_impl(vm, function, this_value, move(arguments_list));
  75. }
  76. template<typename... Args>
  77. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, Value function, Value this_value, Args&&... args)
  78. {
  79. if constexpr (sizeof...(Args) > 0) {
  80. MarkedVector<Value> arguments_list { vm.heap() };
  81. (..., arguments_list.append(forward<Args>(args)));
  82. return call_impl(vm, function, this_value, move(arguments_list));
  83. }
  84. return call_impl(vm, function, this_value);
  85. }
  86. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, FunctionObject& function, Value this_value, MarkedVector<Value> arguments_list)
  87. {
  88. return call_impl(vm, function, this_value, move(arguments_list));
  89. }
  90. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, FunctionObject& function, Value this_value, Optional<MarkedVector<Value>> arguments_list)
  91. {
  92. return call_impl(vm, function, this_value, move(arguments_list));
  93. }
  94. template<typename... Args>
  95. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, FunctionObject& function, Value this_value, Args&&... args)
  96. {
  97. if constexpr (sizeof...(Args) > 0) {
  98. MarkedVector<Value> arguments_list { vm.heap() };
  99. (..., arguments_list.append(forward<Args>(args)));
  100. return call_impl(vm, function, this_value, move(arguments_list));
  101. }
  102. return call_impl(vm, function, this_value);
  103. }
  104. // 7.3.15 Construct ( F [ , argumentsList [ , newTarget ] ] ), https://tc39.es/ecma262/#sec-construct
  105. template<typename... Args>
  106. ALWAYS_INLINE ThrowCompletionOr<NonnullGCPtr<Object>> construct(VM& vm, FunctionObject& function, Args&&... args)
  107. {
  108. if constexpr (sizeof...(Args) > 0) {
  109. MarkedVector<Value> arguments_list { vm.heap() };
  110. (..., arguments_list.append(forward<Args>(args)));
  111. return construct_impl(vm, function, move(arguments_list));
  112. }
  113. return construct_impl(vm, function);
  114. }
  115. ALWAYS_INLINE ThrowCompletionOr<NonnullGCPtr<Object>> construct(VM& vm, FunctionObject& function, MarkedVector<Value> arguments_list, FunctionObject* new_target = nullptr)
  116. {
  117. return construct_impl(vm, function, move(arguments_list), new_target);
  118. }
  119. ALWAYS_INLINE ThrowCompletionOr<NonnullGCPtr<Object>> construct(VM& vm, FunctionObject& function, Optional<MarkedVector<Value>> arguments_list, FunctionObject* new_target = nullptr)
  120. {
  121. return construct_impl(vm, function, move(arguments_list), new_target);
  122. }
  123. // 10.1.13 OrdinaryCreateFromConstructor ( constructor, intrinsicDefaultProto [ , internalSlotsList ] ), https://tc39.es/ecma262/#sec-ordinarycreatefromconstructor
  124. template<typename T, typename... Args>
  125. ThrowCompletionOr<NonnullGCPtr<T>> ordinary_create_from_constructor(VM& vm, FunctionObject const& constructor, NonnullGCPtr<Object> (Intrinsics::*intrinsic_default_prototype)(), Args&&... args)
  126. {
  127. auto& realm = *vm.current_realm();
  128. auto* prototype = TRY(get_prototype_from_constructor(vm, constructor, intrinsic_default_prototype));
  129. return MUST_OR_THROW_OOM(realm.heap().allocate<T>(realm, forward<Args>(args)..., *prototype));
  130. }
  131. // 14.1 MergeLists ( a, b ), https://tc39.es/proposal-temporal/#sec-temporal-mergelists
  132. template<typename T>
  133. Vector<T> merge_lists(Vector<T> const& a, Vector<T> const& b)
  134. {
  135. // 1. Let merged be a new empty List.
  136. Vector<T> merged;
  137. // 2. For each element element of a, do
  138. for (auto const& element : a) {
  139. // a. If merged does not contain element, then
  140. if (!merged.contains_slow(element)) {
  141. // i. Append element to merged.
  142. merged.append(element);
  143. }
  144. }
  145. // 3. For each element element of b, do
  146. for (auto const& element : b) {
  147. // a. If merged does not contain element, then
  148. if (!merged.contains_slow(element)) {
  149. // i. Append element to merged.
  150. merged.append(element);
  151. }
  152. }
  153. // 4. Return merged.
  154. return merged;
  155. }
  156. // x modulo y, https://tc39.es/ecma262/#eqn-modulo
  157. template<Arithmetic T, Arithmetic U>
  158. auto modulo(T x, U y)
  159. {
  160. // 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.
  161. VERIFY(y != 0);
  162. if constexpr (IsFloatingPoint<T> || IsFloatingPoint<U>) {
  163. if constexpr (IsFloatingPoint<U>)
  164. VERIFY(isfinite(y));
  165. return fmod(fmod(x, y) + y, y);
  166. } else {
  167. return ((x % y) + y) % y;
  168. }
  169. }
  170. auto modulo(Crypto::BigInteger auto const& x, Crypto::BigInteger auto const& y)
  171. {
  172. VERIFY(!y.is_zero());
  173. auto result = x.divided_by(y).remainder;
  174. if (result.is_negative())
  175. result = result.plus(y);
  176. return result;
  177. }
  178. }