AbstractOperations.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2020-2022, 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, 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. enum class CanonicalIndexMode {
  51. DetectNumericRoundtrip,
  52. IgnoreNumericRoundtrip,
  53. };
  54. ThrowCompletionOr<CanonicalIndex> canonical_numeric_index_string(VM&, PropertyKey const&, CanonicalIndexMode needs_numeric);
  55. ThrowCompletionOr<String> get_substitution(VM&, Utf16View const& matched, Utf16View const& str, size_t position, Span<Value> captures, Value named_captures, Value replacement);
  56. enum class CallerMode {
  57. Strict,
  58. NonStrict
  59. };
  60. enum class EvalMode {
  61. Direct,
  62. Indirect
  63. };
  64. ThrowCompletionOr<Value> perform_eval(VM&, Value, CallerMode, EvalMode);
  65. ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, Program const& program, Environment* variable_environment, Environment* lexical_environment, PrivateEnvironment* private_environment, bool strict);
  66. // 7.3.14 Call ( F, V [ , argumentsList ] ), https://tc39.es/ecma262/#sec-call
  67. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, Value function, Value this_value, MarkedVector<Value> arguments_list)
  68. {
  69. return call_impl(vm, function, this_value, move(arguments_list));
  70. }
  71. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, Value function, Value this_value, Optional<MarkedVector<Value>> arguments_list)
  72. {
  73. return call_impl(vm, function, this_value, move(arguments_list));
  74. }
  75. template<typename... Args>
  76. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, Value function, Value this_value, Args&&... args)
  77. {
  78. if constexpr (sizeof...(Args) > 0) {
  79. MarkedVector<Value> arguments_list { vm.heap() };
  80. (..., arguments_list.append(forward<Args>(args)));
  81. return call_impl(vm, function, this_value, move(arguments_list));
  82. }
  83. return call_impl(vm, function, this_value);
  84. }
  85. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, FunctionObject& function, Value this_value, MarkedVector<Value> arguments_list)
  86. {
  87. return call_impl(vm, function, this_value, move(arguments_list));
  88. }
  89. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, FunctionObject& function, Value this_value, Optional<MarkedVector<Value>> arguments_list)
  90. {
  91. return call_impl(vm, function, this_value, move(arguments_list));
  92. }
  93. template<typename... Args>
  94. ALWAYS_INLINE ThrowCompletionOr<Value> call(VM& vm, FunctionObject& function, Value this_value, Args&&... args)
  95. {
  96. if constexpr (sizeof...(Args) > 0) {
  97. MarkedVector<Value> arguments_list { vm.heap() };
  98. (..., arguments_list.append(forward<Args>(args)));
  99. return call_impl(vm, function, this_value, move(arguments_list));
  100. }
  101. return call_impl(vm, function, this_value);
  102. }
  103. // 7.3.15 Construct ( F [ , argumentsList [ , newTarget ] ] ), https://tc39.es/ecma262/#sec-construct
  104. template<typename... Args>
  105. ALWAYS_INLINE ThrowCompletionOr<NonnullGCPtr<Object>> construct(VM& vm, FunctionObject& function, Args&&... args)
  106. {
  107. if constexpr (sizeof...(Args) > 0) {
  108. MarkedVector<Value> arguments_list { vm.heap() };
  109. (..., arguments_list.append(forward<Args>(args)));
  110. return construct_impl(vm, function, move(arguments_list));
  111. }
  112. return construct_impl(vm, function);
  113. }
  114. ALWAYS_INLINE ThrowCompletionOr<NonnullGCPtr<Object>> construct(VM& vm, FunctionObject& function, MarkedVector<Value> arguments_list, FunctionObject* new_target = nullptr)
  115. {
  116. return construct_impl(vm, function, move(arguments_list), new_target);
  117. }
  118. ALWAYS_INLINE ThrowCompletionOr<NonnullGCPtr<Object>> construct(VM& vm, FunctionObject& function, Optional<MarkedVector<Value>> arguments_list, FunctionObject* new_target = nullptr)
  119. {
  120. return construct_impl(vm, function, move(arguments_list), new_target);
  121. }
  122. // 10.1.13 OrdinaryCreateFromConstructor ( constructor, intrinsicDefaultProto [ , internalSlotsList ] ), https://tc39.es/ecma262/#sec-ordinarycreatefromconstructor
  123. template<typename T, typename... Args>
  124. ThrowCompletionOr<NonnullGCPtr<T>> ordinary_create_from_constructor(VM& vm, FunctionObject const& constructor, Object* (Intrinsics::*intrinsic_default_prototype)(), Args&&... args)
  125. {
  126. auto& realm = *vm.current_realm();
  127. auto* prototype = TRY(get_prototype_from_constructor(vm, constructor, intrinsic_default_prototype));
  128. return MUST_OR_THROW_OOM(realm.heap().allocate<T>(realm, forward<Args>(args)..., *prototype));
  129. }
  130. // 14.1 MergeLists ( a, b ), https://tc39.es/proposal-temporal/#sec-temporal-mergelists
  131. template<typename T>
  132. Vector<T> merge_lists(Vector<T> const& a, Vector<T> const& b)
  133. {
  134. // 1. Let merged be a new empty List.
  135. Vector<T> merged;
  136. // 2. For each element element of a, do
  137. for (auto const& element : a) {
  138. // a. If merged does not contain element, then
  139. if (!merged.contains_slow(element)) {
  140. // i. Append element to merged.
  141. merged.append(element);
  142. }
  143. }
  144. // 3. For each element element of b, do
  145. for (auto const& element : b) {
  146. // a. If merged does not contain element, then
  147. if (!merged.contains_slow(element)) {
  148. // i. Append element to merged.
  149. merged.append(element);
  150. }
  151. }
  152. // 4. Return merged.
  153. return merged;
  154. }
  155. // x modulo y, https://tc39.es/ecma262/#eqn-modulo
  156. template<Arithmetic T, Arithmetic U>
  157. auto modulo(T x, U y)
  158. {
  159. // 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.
  160. VERIFY(y != 0);
  161. if constexpr (IsFloatingPoint<T> || IsFloatingPoint<U>) {
  162. if constexpr (IsFloatingPoint<U>)
  163. VERIFY(isfinite(y));
  164. return fmod(fmod(x, y) + y, y);
  165. } else {
  166. return ((x % y) + y) % y;
  167. }
  168. }
  169. auto modulo(Crypto::BigInteger auto const& x, Crypto::BigInteger auto const& y)
  170. {
  171. VERIFY(!y.is_zero());
  172. auto result = x.divided_by(y).remainder;
  173. if (result.is_negative())
  174. result = result.plus(y);
  175. return result;
  176. }
  177. }