GlobalObject.h 7.9 KB

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