AbstractOperations.h 5.2 KB

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