AbstractOperations.h 6.6 KB

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