GlobalObject.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. GlobalEnvironment& environment() { return *m_environment; }
  18. Console& console() { return *m_console; }
  19. Shape* empty_object_shape() { return m_empty_object_shape; }
  20. Shape* new_object_shape() { return m_new_object_shape; }
  21. Shape* new_ordinary_function_prototype_object_shape() { return m_new_ordinary_function_prototype_object_shape; }
  22. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
  23. ProxyConstructor* proxy_constructor() { return m_proxy_constructor; }
  24. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct constructor
  25. GeneratorObjectPrototype* generator_object_prototype() { return m_generator_object_prototype; }
  26. FunctionObject* array_prototype_values_function() const { return m_array_prototype_values_function; }
  27. FunctionObject* eval_function() const { return m_eval_function; }
  28. FunctionObject* temporal_time_zone_prototype_get_offset_nanoseconds_for_function() const { return m_temporal_time_zone_prototype_get_offset_nanoseconds_for_function; }
  29. FunctionObject* throw_type_error_function() const { return m_throw_type_error_function; }
  30. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  31. ConstructorName* snake_name##_constructor() { return m_##snake_name##_constructor; } \
  32. Object* snake_name##_prototype() { return m_##snake_name##_prototype; }
  33. JS_ENUMERATE_BUILTIN_TYPES
  34. #undef __JS_ENUMERATE
  35. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  36. Temporal::ConstructorName* temporal_##snake_name##_constructor() { return m_temporal_##snake_name##_constructor; } \
  37. Object* temporal_##snake_name##_prototype() { return m_temporal_##snake_name##_prototype; }
  38. JS_ENUMERATE_TEMPORAL_OBJECTS
  39. #undef __JS_ENUMERATE
  40. #define __JS_ENUMERATE(ClassName, snake_name) \
  41. Object* snake_name##_prototype() { return m_##snake_name##_prototype; }
  42. JS_ENUMERATE_ITERATOR_PROTOTYPES
  43. #undef __JS_ENUMERATE
  44. protected:
  45. virtual void visit_edges(Visitor&) override;
  46. template<typename ConstructorType>
  47. void initialize_constructor(PropertyName const&, ConstructorType*&, Object* prototype);
  48. template<typename ConstructorType>
  49. void add_constructor(PropertyName const&, ConstructorType*&, Object* prototype);
  50. private:
  51. virtual bool is_global_object() const final { return true; }
  52. JS_DECLARE_NATIVE_FUNCTION(gc);
  53. JS_DECLARE_NATIVE_FUNCTION(is_nan);
  54. JS_DECLARE_NATIVE_FUNCTION(is_finite);
  55. JS_DECLARE_NATIVE_FUNCTION(parse_float);
  56. JS_DECLARE_NATIVE_FUNCTION(parse_int);
  57. JS_DECLARE_NATIVE_FUNCTION(eval);
  58. JS_DECLARE_NATIVE_FUNCTION(encode_uri);
  59. JS_DECLARE_NATIVE_FUNCTION(decode_uri);
  60. JS_DECLARE_NATIVE_FUNCTION(encode_uri_component);
  61. JS_DECLARE_NATIVE_FUNCTION(decode_uri_component);
  62. JS_DECLARE_NATIVE_FUNCTION(escape);
  63. JS_DECLARE_NATIVE_FUNCTION(unescape);
  64. NonnullOwnPtr<Console> m_console;
  65. Shape* m_empty_object_shape { nullptr };
  66. Shape* m_new_object_shape { nullptr };
  67. Shape* m_new_ordinary_function_prototype_object_shape { nullptr };
  68. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
  69. ProxyConstructor* m_proxy_constructor { nullptr };
  70. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct constructor
  71. GeneratorObjectPrototype* m_generator_object_prototype { nullptr };
  72. GlobalEnvironment* m_environment { nullptr };
  73. FunctionObject* m_array_prototype_values_function { nullptr };
  74. FunctionObject* m_eval_function { nullptr };
  75. FunctionObject* m_temporal_time_zone_prototype_get_offset_nanoseconds_for_function { nullptr };
  76. FunctionObject* m_throw_type_error_function { nullptr };
  77. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  78. ConstructorName* m_##snake_name##_constructor { nullptr }; \
  79. Object* m_##snake_name##_prototype { nullptr };
  80. JS_ENUMERATE_BUILTIN_TYPES
  81. #undef __JS_ENUMERATE
  82. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  83. Temporal::ConstructorName* m_temporal_##snake_name##_constructor { nullptr }; \
  84. Object* m_temporal_##snake_name##_prototype { nullptr };
  85. JS_ENUMERATE_TEMPORAL_OBJECTS
  86. #undef __JS_ENUMERATE
  87. #define __JS_ENUMERATE(ClassName, snake_name) \
  88. Object* m_##snake_name##_prototype { nullptr };
  89. JS_ENUMERATE_ITERATOR_PROTOTYPES
  90. #undef __JS_ENUMERATE
  91. };
  92. template<typename ConstructorType>
  93. inline void GlobalObject::initialize_constructor(PropertyName const& property_name, ConstructorType*& constructor, Object* prototype)
  94. {
  95. auto& vm = this->vm();
  96. constructor = heap().allocate<ConstructorType>(*this, *this);
  97. constructor->define_direct_property(vm.names.name, js_string(heap(), property_name.as_string()), Attribute::Configurable);
  98. if (vm.exception())
  99. return;
  100. if (prototype) {
  101. prototype->define_direct_property(vm.names.constructor, constructor, Attribute::Writable | Attribute::Configurable);
  102. if (vm.exception())
  103. return;
  104. }
  105. }
  106. template<typename ConstructorType>
  107. inline void GlobalObject::add_constructor(PropertyName const& property_name, ConstructorType*& constructor, Object* prototype)
  108. {
  109. // Some constructors are pre-initialized separately.
  110. if (!constructor)
  111. initialize_constructor(property_name, constructor, prototype);
  112. define_direct_property(property_name, constructor, Attribute::Writable | Attribute::Configurable);
  113. }
  114. inline GlobalObject* Shape::global_object() const
  115. {
  116. return static_cast<GlobalObject*>(m_global_object);
  117. }
  118. template<>
  119. inline bool Object::fast_is<GlobalObject>() const { return is_global_object(); }
  120. }