AbstractOperations.h 8.2 KB

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