GlobalObject.h 5.6 KB

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