AbstractOperations.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/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. Object* get_super_constructor(VM&);
  23. ThrowCompletionOr<Reference> make_super_property_reference(GlobalObject&, Value actual_this, PropertyKey const&, bool strict);
  24. ThrowCompletionOr<Value> require_object_coercible(GlobalObject&, Value);
  25. ThrowCompletionOr<Value> call_impl(GlobalObject&, Value function, Value this_value, Optional<MarkedVector<Value>> = {});
  26. ThrowCompletionOr<Value> call_impl(GlobalObject&, FunctionObject& function, Value this_value, Optional<MarkedVector<Value>> = {});
  27. ThrowCompletionOr<Object*> construct_impl(GlobalObject&, FunctionObject&, Optional<MarkedVector<Value>> = {}, FunctionObject* new_target = nullptr);
  28. ThrowCompletionOr<size_t> length_of_array_like(GlobalObject&, Object const&);
  29. ThrowCompletionOr<MarkedVector<Value>> create_list_from_array_like(GlobalObject&, Value, Function<ThrowCompletionOr<void>(Value)> = {});
  30. ThrowCompletionOr<FunctionObject*> species_constructor(GlobalObject&, Object const&, FunctionObject& default_constructor);
  31. ThrowCompletionOr<Realm*> get_function_realm(GlobalObject&, FunctionObject const&);
  32. ThrowCompletionOr<void> initialize_bound_name(GlobalObject&, FlyString const&, Value, Environment*);
  33. bool is_compatible_property_descriptor(bool extensible, PropertyDescriptor const&, Optional<PropertyDescriptor> const& current);
  34. bool validate_and_apply_property_descriptor(Object*, PropertyKey const&, bool extensible, PropertyDescriptor const&, Optional<PropertyDescriptor> const& current);
  35. ThrowCompletionOr<Object*> get_prototype_from_constructor(GlobalObject&, FunctionObject const& constructor, Object* (GlobalObject::*intrinsic_default_prototype)());
  36. Object* create_unmapped_arguments_object(GlobalObject&, Span<Value> arguments);
  37. Object* create_mapped_arguments_object(GlobalObject&, FunctionObject&, Vector<FunctionNode::Parameter> const&, Span<Value> arguments, Environment&);
  38. Value canonical_numeric_index_string(GlobalObject&, PropertyKey const&);
  39. ThrowCompletionOr<String> get_substitution(GlobalObject&, Utf16View const& matched, Utf16View const& str, size_t position, Span<Value> captures, Value named_captures, Value replacement);
  40. enum class CallerMode {
  41. Strict,
  42. NonStrict
  43. };
  44. enum class EvalMode {
  45. Direct,
  46. Indirect
  47. };
  48. ThrowCompletionOr<Value> perform_eval(Value, GlobalObject&, CallerMode, EvalMode);
  49. ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, GlobalObject& global_object, Program const& program, Environment* variable_environment, Environment* lexical_environment, PrivateEnvironment* private_environment, bool strict);
  50. // 7.3.14 Call ( F, V [ , argumentsList ] ), https://tc39.es/ecma262/#sec-call
  51. ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, Value function, Value this_value, MarkedVector<Value> arguments_list)
  52. {
  53. return call_impl(global_object, function, this_value, move(arguments_list));
  54. }
  55. ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, Value function, Value this_value, Optional<MarkedVector<Value>> arguments_list)
  56. {
  57. return call_impl(global_object, function, this_value, move(arguments_list));
  58. }
  59. template<typename... Args>
  60. ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, Value function, Value this_value, Args&&... args)
  61. {
  62. if constexpr (sizeof...(Args) > 0) {
  63. MarkedVector<Value> arguments_list { global_object.heap() };
  64. (..., arguments_list.append(forward<Args>(args)));
  65. return call_impl(global_object, function, this_value, move(arguments_list));
  66. }
  67. return call_impl(global_object, function, this_value);
  68. }
  69. ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, FunctionObject& function, Value this_value, MarkedVector<Value> arguments_list)
  70. {
  71. return call_impl(global_object, function, this_value, move(arguments_list));
  72. }
  73. ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, FunctionObject& function, Value this_value, Optional<MarkedVector<Value>> arguments_list)
  74. {
  75. return call_impl(global_object, function, this_value, move(arguments_list));
  76. }
  77. template<typename... Args>
  78. ALWAYS_INLINE ThrowCompletionOr<Value> call(GlobalObject& global_object, FunctionObject& function, Value this_value, Args&&... args)
  79. {
  80. if constexpr (sizeof...(Args) > 0) {
  81. MarkedVector<Value> arguments_list { global_object.heap() };
  82. (..., arguments_list.append(forward<Args>(args)));
  83. return call_impl(global_object, function, this_value, move(arguments_list));
  84. }
  85. return call_impl(global_object, function, this_value);
  86. }
  87. // 7.3.15 Construct ( F [ , argumentsList [ , newTarget ] ] ), https://tc39.es/ecma262/#sec-construct
  88. template<typename... Args>
  89. ALWAYS_INLINE ThrowCompletionOr<Object*> construct(GlobalObject& global_object, FunctionObject& function, Args&&... args)
  90. {
  91. if constexpr (sizeof...(Args) > 0) {
  92. MarkedVector<Value> arguments_list { global_object.heap() };
  93. (..., arguments_list.append(forward<Args>(args)));
  94. return construct_impl(global_object, function, move(arguments_list));
  95. }
  96. return construct_impl(global_object, function);
  97. }
  98. ALWAYS_INLINE ThrowCompletionOr<Object*> construct(GlobalObject& global_object, FunctionObject& function, MarkedVector<Value> arguments_list, FunctionObject* new_target = nullptr)
  99. {
  100. return construct_impl(global_object, function, move(arguments_list), new_target);
  101. }
  102. ALWAYS_INLINE ThrowCompletionOr<Object*> construct(GlobalObject& global_object, FunctionObject& function, Optional<MarkedVector<Value>> arguments_list, FunctionObject* new_target = nullptr)
  103. {
  104. return construct_impl(global_object, function, move(arguments_list), new_target);
  105. }
  106. // 10.1.13 OrdinaryCreateFromConstructor ( constructor, intrinsicDefaultProto [ , internalSlotsList ] ), https://tc39.es/ecma262/#sec-ordinarycreatefromconstructor
  107. template<typename T, typename... Args>
  108. ThrowCompletionOr<T*> ordinary_create_from_constructor(GlobalObject& global_object, FunctionObject const& constructor, Object* (GlobalObject::*intrinsic_default_prototype)(), Args&&... args)
  109. {
  110. auto* prototype = TRY(get_prototype_from_constructor(global_object, constructor, intrinsic_default_prototype));
  111. return global_object.heap().allocate<T>(global_object, forward<Args>(args)..., *prototype);
  112. }
  113. // x modulo y, https://tc39.es/ecma262/#eqn-modulo
  114. template<typename T, typename U>
  115. auto modulo(T x, U y) requires(IsArithmetic<T>, IsArithmetic<U>)
  116. {
  117. // 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.
  118. VERIFY(y != 0);
  119. if constexpr (IsFloatingPoint<T> || IsFloatingPoint<U>) {
  120. if constexpr (IsFloatingPoint<U>)
  121. VERIFY(isfinite(y));
  122. return fmod(fmod(x, y) + y, y);
  123. } else {
  124. return ((x % y) + y) % y;
  125. }
  126. }
  127. auto modulo(Crypto::BigInteger auto const& x, Crypto::BigInteger auto const& y)
  128. {
  129. VERIFY(y != "0"_bigint);
  130. auto result = x.divided_by(y).remainder;
  131. if (result.is_negative())
  132. result = result.plus(y);
  133. return result;
  134. }
  135. }