AbstractOperations.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2020-2021, 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/AST.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. Object* get_super_constructor(VM&);
  24. ThrowCompletionOr<Reference> make_super_property_reference(GlobalObject&, Value actual_this, PropertyKey const&, bool strict);
  25. ThrowCompletionOr<Value> require_object_coercible(GlobalObject&, Value);
  26. ThrowCompletionOr<Value> call_impl(GlobalObject&, Value function, Value this_value, Optional<MarkedVector<Value>> = {});
  27. ThrowCompletionOr<Value> call_impl(GlobalObject&, FunctionObject& function, Value this_value, Optional<MarkedVector<Value>> = {});
  28. ThrowCompletionOr<Object*> construct_impl(GlobalObject&, FunctionObject&, Optional<MarkedVector<Value>> = {}, FunctionObject* new_target = nullptr);
  29. ThrowCompletionOr<size_t> length_of_array_like(GlobalObject&, Object const&);
  30. ThrowCompletionOr<MarkedVector<Value>> create_list_from_array_like(GlobalObject&, Value, Function<ThrowCompletionOr<void>(Value)> = {});
  31. ThrowCompletionOr<FunctionObject*> species_constructor(GlobalObject&, Object const&, FunctionObject& default_constructor);
  32. ThrowCompletionOr<Realm*> get_function_realm(GlobalObject&, FunctionObject const&);
  33. ThrowCompletionOr<void> initialize_bound_name(GlobalObject&, 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(GlobalObject&, FunctionObject const& constructor, Object* (GlobalObject::*intrinsic_default_prototype)());
  37. Object* create_unmapped_arguments_object(GlobalObject&, Span<Value> arguments);
  38. Object* create_mapped_arguments_object(GlobalObject&, FunctionObject&, Vector<FunctionNode::Parameter> 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(GlobalObject&, 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(Value, GlobalObject&, CallerMode, EvalMode);
  54. ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, GlobalObject& global_object, 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(GlobalObject& global_object, Value function, Value this_value, MarkedVector<Value> arguments_list)
  57. {
  58. return call_impl(global_object, function, this_value, move(arguments_list));
  59. }
  60. ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, Value function, Value this_value, Optional<MarkedVector<Value>> arguments_list)
  61. {
  62. return call_impl(global_object, function, this_value, move(arguments_list));
  63. }
  64. template<typename... Args>
  65. ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, Value function, Value this_value, Args&&... args)
  66. {
  67. if constexpr (sizeof...(Args) > 0) {
  68. MarkedVector<Value> arguments_list { global_object.heap() };
  69. (..., arguments_list.append(forward<Args>(args)));
  70. return call_impl(global_object, function, this_value, move(arguments_list));
  71. }
  72. return call_impl(global_object, function, this_value);
  73. }
  74. ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, FunctionObject& function, Value this_value, MarkedVector<Value> arguments_list)
  75. {
  76. return call_impl(global_object, function, this_value, move(arguments_list));
  77. }
  78. ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, FunctionObject& function, Value this_value, Optional<MarkedVector<Value>> arguments_list)
  79. {
  80. return call_impl(global_object, function, this_value, move(arguments_list));
  81. }
  82. template<typename... Args>
  83. ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, FunctionObject& function, Value this_value, Args&&... args)
  84. {
  85. if constexpr (sizeof...(Args) > 0) {
  86. MarkedVector<Value> arguments_list { global_object.heap() };
  87. (..., arguments_list.append(forward<Args>(args)));
  88. return call_impl(global_object, function, this_value, move(arguments_list));
  89. }
  90. return call_impl(global_object, 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(GlobalObject& global_object, FunctionObject& function, Args&&... args)
  95. {
  96. if constexpr (sizeof...(Args) > 0) {
  97. MarkedVector<Value> arguments_list { global_object.heap() };
  98. (..., arguments_list.append(forward<Args>(args)));
  99. return construct_impl(global_object, function, move(arguments_list));
  100. }
  101. return construct_impl(global_object, function);
  102. }
  103. ALWAYS_INLINE ThrowCompletionOr<Object*> construct(GlobalObject& global_object, FunctionObject& function, MarkedVector<Value> arguments_list, FunctionObject* new_target = nullptr)
  104. {
  105. return construct_impl(global_object, function, move(arguments_list), new_target);
  106. }
  107. ALWAYS_INLINE ThrowCompletionOr<Object*> construct(GlobalObject& global_object, FunctionObject& function, Optional<MarkedVector<Value>> arguments_list, FunctionObject* new_target = nullptr)
  108. {
  109. return construct_impl(global_object, 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(GlobalObject& global_object, FunctionObject const& constructor, Object* (GlobalObject::*intrinsic_default_prototype)(), Args&&... args)
  114. {
  115. auto* prototype = TRY(get_prototype_from_constructor(global_object, constructor, intrinsic_default_prototype));
  116. return global_object.heap().allocate<T>(global_object, forward<Args>(args)..., *prototype);
  117. }
  118. // x modulo y, https://tc39.es/ecma262/#eqn-modulo
  119. template<typename T, typename U>
  120. auto modulo(T x, U y) requires(IsArithmetic<T>, IsArithmetic<U>)
  121. {
  122. // 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.
  123. VERIFY(y != 0);
  124. if constexpr (IsFloatingPoint<T> || IsFloatingPoint<U>) {
  125. if constexpr (IsFloatingPoint<U>)
  126. VERIFY(isfinite(y));
  127. return fmod(fmod(x, y) + y, y);
  128. } else {
  129. return ((x % y) + y) % y;
  130. }
  131. }
  132. auto modulo(Crypto::BigInteger auto const& x, Crypto::BigInteger auto const& y)
  133. {
  134. VERIFY(y != "0"_bigint);
  135. auto result = x.divided_by(y).remainder;
  136. if (result.is_negative())
  137. result = result.plus(y);
  138. return result;
  139. }
  140. }