GlobalObject.h 7.9 KB

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