GlobalObject.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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(Realm&);
  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(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() \
  44. { \
  45. return m_##snake_name##_constructor; \
  46. } \
  47. Object* snake_name##_prototype() \
  48. { \
  49. return m_##snake_name##_prototype; \
  50. }
  51. JS_ENUMERATE_BUILTIN_TYPES
  52. #undef __JS_ENUMERATE
  53. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  54. Intl::ConstructorName* intl_##snake_name##_constructor() \
  55. { \
  56. return m_intl_##snake_name##_constructor; \
  57. } \
  58. Object* intl_##snake_name##_prototype() \
  59. { \
  60. return m_intl_##snake_name##_prototype; \
  61. }
  62. JS_ENUMERATE_INTL_OBJECTS
  63. #undef __JS_ENUMERATE
  64. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  65. Temporal::ConstructorName* temporal_##snake_name##_constructor() \
  66. { \
  67. return m_temporal_##snake_name##_constructor; \
  68. } \
  69. Object* temporal_##snake_name##_prototype() \
  70. { \
  71. return m_temporal_##snake_name##_prototype; \
  72. }
  73. JS_ENUMERATE_TEMPORAL_OBJECTS
  74. #undef __JS_ENUMERATE
  75. #define __JS_ENUMERATE(ClassName, snake_name) \
  76. Object* snake_name##_prototype() \
  77. { \
  78. return m_##snake_name##_prototype; \
  79. }
  80. JS_ENUMERATE_ITERATOR_PROTOTYPES
  81. #undef __JS_ENUMERATE
  82. protected:
  83. virtual void visit_edges(Visitor&) override;
  84. template<typename ConstructorType>
  85. void initialize_constructor(PropertyKey const&, ConstructorType*&, Object* prototype, PropertyAttributes = Attribute::Writable | Attribute::Configurable);
  86. template<typename ConstructorType>
  87. void add_constructor(PropertyKey const&, ConstructorType*&, Object* prototype);
  88. private:
  89. virtual bool is_global_object() const final { return true; }
  90. JS_DECLARE_NATIVE_FUNCTION(gc);
  91. JS_DECLARE_NATIVE_FUNCTION(is_nan);
  92. JS_DECLARE_NATIVE_FUNCTION(is_finite);
  93. JS_DECLARE_NATIVE_FUNCTION(parse_float);
  94. JS_DECLARE_NATIVE_FUNCTION(parse_int);
  95. JS_DECLARE_NATIVE_FUNCTION(eval);
  96. JS_DECLARE_NATIVE_FUNCTION(encode_uri);
  97. JS_DECLARE_NATIVE_FUNCTION(decode_uri);
  98. JS_DECLARE_NATIVE_FUNCTION(encode_uri_component);
  99. JS_DECLARE_NATIVE_FUNCTION(decode_uri_component);
  100. JS_DECLARE_NATIVE_FUNCTION(escape);
  101. JS_DECLARE_NATIVE_FUNCTION(unescape);
  102. NonnullOwnPtr<Console> m_console;
  103. WeakPtr<Realm> m_associated_realm;
  104. Shape* m_empty_object_shape { nullptr };
  105. Shape* m_new_object_shape { nullptr };
  106. Shape* m_new_ordinary_function_prototype_object_shape { nullptr };
  107. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
  108. ProxyConstructor* m_proxy_constructor { nullptr };
  109. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct constructor
  110. Object* m_async_from_sync_iterator_prototype { nullptr };
  111. Object* m_async_generator_prototype { nullptr };
  112. Object* m_generator_prototype { nullptr };
  113. // Not included in JS_ENUMERATE_INTL_OBJECTS due to missing distinct constructor
  114. Object* m_intl_segments_prototype { nullptr };
  115. FunctionObject* m_array_prototype_values_function { nullptr };
  116. FunctionObject* m_date_constructor_now_function { nullptr };
  117. FunctionObject* m_eval_function { nullptr };
  118. FunctionObject* m_json_parse_function { nullptr };
  119. FunctionObject* m_object_prototype_to_string_function { nullptr };
  120. FunctionObject* m_throw_type_error_function { nullptr };
  121. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  122. ConstructorName* m_##snake_name##_constructor { nullptr }; \
  123. Object* m_##snake_name##_prototype { nullptr };
  124. JS_ENUMERATE_BUILTIN_TYPES
  125. #undef __JS_ENUMERATE
  126. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  127. Intl::ConstructorName* m_intl_##snake_name##_constructor { nullptr }; \
  128. Object* m_intl_##snake_name##_prototype { nullptr };
  129. JS_ENUMERATE_INTL_OBJECTS
  130. #undef __JS_ENUMERATE
  131. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  132. Temporal::ConstructorName* m_temporal_##snake_name##_constructor { nullptr }; \
  133. Object* m_temporal_##snake_name##_prototype { nullptr };
  134. JS_ENUMERATE_TEMPORAL_OBJECTS
  135. #undef __JS_ENUMERATE
  136. #define __JS_ENUMERATE(ClassName, snake_name) \
  137. Object* m_##snake_name##_prototype { nullptr };
  138. JS_ENUMERATE_ITERATOR_PROTOTYPES
  139. #undef __JS_ENUMERATE
  140. };
  141. template<typename ConstructorType>
  142. inline void GlobalObject::initialize_constructor(PropertyKey const& property_key, ConstructorType*& constructor, Object* prototype, PropertyAttributes attributes)
  143. {
  144. auto& vm = this->vm();
  145. auto& realm = *associated_realm();
  146. constructor = heap().allocate<ConstructorType>(realm, realm);
  147. constructor->define_direct_property(vm.names.name, js_string(heap(), property_key.as_string()), Attribute::Configurable);
  148. if (prototype)
  149. prototype->define_direct_property(vm.names.constructor, constructor, attributes);
  150. }
  151. template<typename ConstructorType>
  152. inline void GlobalObject::add_constructor(PropertyKey const& property_key, ConstructorType*& constructor, Object* prototype)
  153. {
  154. // Some constructors are pre-initialized separately.
  155. if (!constructor)
  156. initialize_constructor(property_key, constructor, prototype);
  157. define_direct_property(property_key, constructor, Attribute::Writable | Attribute::Configurable);
  158. }
  159. inline GlobalObject* Shape::global_object() const
  160. {
  161. return &static_cast<GlobalObject&>(m_realm.global_object());
  162. }
  163. template<>
  164. inline bool Object::fast_is<GlobalObject>() const { return is_global_object(); }
  165. template<typename... Args>
  166. [[nodiscard]] ALWAYS_INLINE ThrowCompletionOr<Value> Value::invoke(VM& vm, PropertyKey const& property_key, Args... args)
  167. {
  168. if constexpr (sizeof...(Args) > 0) {
  169. MarkedVector<Value> arglist { vm.heap() };
  170. (..., arglist.append(move(args)));
  171. return invoke_internal(vm, property_key, move(arglist));
  172. }
  173. return invoke_internal(vm, property_key, Optional<MarkedVector<Value>> {});
  174. }
  175. }