AbstractOperations.h 8.2 KB

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