GlobalObject.h 7.2 KB

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