GlobalObject.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibJS/Heap/Heap.h>
  9. #include <LibJS/Runtime/Environment.h>
  10. #include <LibJS/Runtime/VM.h>
  11. namespace JS {
  12. class GlobalObject : public Object {
  13. JS_OBJECT(GlobalObject, Object);
  14. public:
  15. explicit GlobalObject();
  16. virtual void initialize_global_object();
  17. virtual ~GlobalObject() override;
  18. Console& console() { return *m_console; }
  19. Realm* associated_realm();
  20. void set_associated_realm(Badge<Realm>, Realm&);
  21. Shape* empty_object_shape() { return m_empty_object_shape; }
  22. Shape* new_object_shape() { return m_new_object_shape; }
  23. Shape* new_ordinary_function_prototype_object_shape() { return m_new_ordinary_function_prototype_object_shape; }
  24. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
  25. ProxyConstructor* proxy_constructor() { return m_proxy_constructor; }
  26. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct constructor
  27. Object* async_from_sync_iterator_prototype() { return m_async_from_sync_iterator_prototype; }
  28. Object* async_generator_prototype() { return m_async_generator_prototype; }
  29. Object* generator_prototype() { return m_generator_prototype; }
  30. // Alias for the AsyncGenerator Prototype Object used by the spec (%AsyncGeneratorFunction.prototype.prototype%)
  31. Object* async_generator_function_prototype_prototype() { return m_async_generator_prototype; }
  32. // Alias for the Generator Prototype Object used by the spec (%GeneratorFunction.prototype.prototype%)
  33. Object* generator_function_prototype_prototype() { return m_generator_prototype; }
  34. // Not included in JS_ENUMERATE_INTL_OBJECTS due to missing distinct constructor
  35. Object* intl_segments_prototype() { return m_intl_segments_prototype; }
  36. FunctionObject* array_prototype_values_function() const { return m_array_prototype_values_function; }
  37. FunctionObject* date_constructor_now_function() const { return m_date_constructor_now_function; }
  38. FunctionObject* eval_function() const { return m_eval_function; }
  39. FunctionObject* json_parse_function() const { return m_json_parse_function; }
  40. FunctionObject* object_prototype_to_string_function() const { return m_object_prototype_to_string_function; }
  41. FunctionObject* throw_type_error_function() const { return m_throw_type_error_function; }
  42. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  43. ConstructorName* snake_name##_constructor() { return m_##snake_name##_constructor; } \
  44. Object* snake_name##_prototype() { return m_##snake_name##_prototype; }
  45. JS_ENUMERATE_BUILTIN_TYPES
  46. #undef __JS_ENUMERATE
  47. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  48. Intl::ConstructorName* intl_##snake_name##_constructor() { return m_intl_##snake_name##_constructor; } \
  49. Object* intl_##snake_name##_prototype() { return m_intl_##snake_name##_prototype; }
  50. JS_ENUMERATE_INTL_OBJECTS
  51. #undef __JS_ENUMERATE
  52. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  53. Temporal::ConstructorName* temporal_##snake_name##_constructor() { return m_temporal_##snake_name##_constructor; } \
  54. Object* temporal_##snake_name##_prototype() { return m_temporal_##snake_name##_prototype; }
  55. JS_ENUMERATE_TEMPORAL_OBJECTS
  56. #undef __JS_ENUMERATE
  57. #define __JS_ENUMERATE(ClassName, snake_name) \
  58. Object* snake_name##_prototype() { return m_##snake_name##_prototype; }
  59. JS_ENUMERATE_ITERATOR_PROTOTYPES
  60. #undef __JS_ENUMERATE
  61. protected:
  62. virtual void visit_edges(Visitor&) override;
  63. template<typename ConstructorType>
  64. void initialize_constructor(PropertyKey const&, ConstructorType*&, Object* prototype, PropertyAttributes = Attribute::Writable | Attribute::Configurable);
  65. template<typename ConstructorType>
  66. void add_constructor(PropertyKey const&, ConstructorType*&, Object* prototype);
  67. private:
  68. virtual bool is_global_object() const final { return true; }
  69. JS_DECLARE_NATIVE_FUNCTION(gc);
  70. JS_DECLARE_NATIVE_FUNCTION(is_nan);
  71. JS_DECLARE_NATIVE_FUNCTION(is_finite);
  72. JS_DECLARE_NATIVE_FUNCTION(parse_float);
  73. JS_DECLARE_NATIVE_FUNCTION(parse_int);
  74. JS_DECLARE_NATIVE_FUNCTION(eval);
  75. JS_DECLARE_NATIVE_FUNCTION(encode_uri);
  76. JS_DECLARE_NATIVE_FUNCTION(decode_uri);
  77. JS_DECLARE_NATIVE_FUNCTION(encode_uri_component);
  78. JS_DECLARE_NATIVE_FUNCTION(decode_uri_component);
  79. JS_DECLARE_NATIVE_FUNCTION(escape);
  80. JS_DECLARE_NATIVE_FUNCTION(unescape);
  81. NonnullOwnPtr<Console> m_console;
  82. WeakPtr<Realm> m_associated_realm;
  83. Shape* m_empty_object_shape { nullptr };
  84. Shape* m_new_object_shape { nullptr };
  85. Shape* m_new_ordinary_function_prototype_object_shape { nullptr };
  86. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
  87. ProxyConstructor* m_proxy_constructor { nullptr };
  88. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct constructor
  89. Object* m_async_from_sync_iterator_prototype { nullptr };
  90. Object* m_async_generator_prototype { nullptr };
  91. Object* m_generator_prototype { nullptr };
  92. // Not included in JS_ENUMERATE_INTL_OBJECTS due to missing distinct constructor
  93. Object* m_intl_segments_prototype { nullptr };
  94. FunctionObject* m_array_prototype_values_function { nullptr };
  95. FunctionObject* m_date_constructor_now_function { nullptr };
  96. FunctionObject* m_eval_function { nullptr };
  97. FunctionObject* m_json_parse_function { nullptr };
  98. FunctionObject* m_object_prototype_to_string_function { nullptr };
  99. FunctionObject* m_throw_type_error_function { nullptr };
  100. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  101. ConstructorName* m_##snake_name##_constructor { nullptr }; \
  102. Object* m_##snake_name##_prototype { nullptr };
  103. JS_ENUMERATE_BUILTIN_TYPES
  104. #undef __JS_ENUMERATE
  105. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  106. Intl::ConstructorName* m_intl_##snake_name##_constructor { nullptr }; \
  107. Object* m_intl_##snake_name##_prototype { nullptr };
  108. JS_ENUMERATE_INTL_OBJECTS
  109. #undef __JS_ENUMERATE
  110. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  111. Temporal::ConstructorName* m_temporal_##snake_name##_constructor { nullptr }; \
  112. Object* m_temporal_##snake_name##_prototype { nullptr };
  113. JS_ENUMERATE_TEMPORAL_OBJECTS
  114. #undef __JS_ENUMERATE
  115. #define __JS_ENUMERATE(ClassName, snake_name) \
  116. Object* m_##snake_name##_prototype { nullptr };
  117. JS_ENUMERATE_ITERATOR_PROTOTYPES
  118. #undef __JS_ENUMERATE
  119. };
  120. template<typename ConstructorType>
  121. inline void GlobalObject::initialize_constructor(PropertyKey const& property_key, ConstructorType*& constructor, Object* prototype, PropertyAttributes attributes)
  122. {
  123. auto& vm = this->vm();
  124. constructor = heap().allocate<ConstructorType>(*this, *this);
  125. constructor->define_direct_property(vm.names.name, js_string(heap(), property_key.as_string()), Attribute::Configurable);
  126. if (prototype)
  127. prototype->define_direct_property(vm.names.constructor, constructor, attributes);
  128. }
  129. template<typename ConstructorType>
  130. inline void GlobalObject::add_constructor(PropertyKey const& property_key, ConstructorType*& constructor, Object* prototype)
  131. {
  132. // Some constructors are pre-initialized separately.
  133. if (!constructor)
  134. initialize_constructor(property_key, constructor, prototype);
  135. define_direct_property(property_key, constructor, Attribute::Writable | Attribute::Configurable);
  136. }
  137. inline GlobalObject* Shape::global_object() const
  138. {
  139. return static_cast<GlobalObject*>(m_global_object);
  140. }
  141. template<>
  142. inline bool Object::fast_is<GlobalObject>() const { return is_global_object(); }
  143. template<typename... Args>
  144. [[nodiscard]] ALWAYS_INLINE ThrowCompletionOr<Value> Value::invoke(GlobalObject& global_object, PropertyKey const& property_key, Args... args)
  145. {
  146. if constexpr (sizeof...(Args) > 0) {
  147. MarkedVector<Value> arglist { global_object.vm().heap() };
  148. (..., arglist.append(move(args)));
  149. return invoke_internal(global_object, property_key, move(arglist));
  150. }
  151. return invoke_internal(global_object, property_key, Optional<MarkedVector<Value>> {});
  152. }
  153. }