GlobalObject.h 4.8 KB

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