GlobalObject.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <LibJS/Heap/Heap.h>
  28. #include <LibJS/Runtime/ScopeObject.h>
  29. #include <LibJS/Runtime/VM.h>
  30. namespace JS {
  31. class GlobalObject : public ScopeObject {
  32. JS_OBJECT(GlobalObject, ScopeObject);
  33. public:
  34. explicit GlobalObject();
  35. virtual void initialize_global_object();
  36. virtual ~GlobalObject() override;
  37. virtual Optional<Variable> get_from_scope(const FlyString&) const override;
  38. virtual void put_to_scope(const FlyString&, Variable) override;
  39. virtual bool has_this_binding() const override;
  40. virtual Value get_this_binding(GlobalObject&) const override;
  41. Console& console() { return *m_console; }
  42. Shape* empty_object_shape() { return m_empty_object_shape; }
  43. Shape* new_object_shape() { return m_new_object_shape; }
  44. Shape* new_script_function_prototype_object_shape() { return m_new_script_function_prototype_object_shape; }
  45. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
  46. ProxyConstructor* proxy_constructor() { return m_proxy_constructor; }
  47. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  48. ConstructorName* snake_name##_constructor() { return m_##snake_name##_constructor; } \
  49. Object* snake_name##_prototype() { return m_##snake_name##_prototype; }
  50. JS_ENUMERATE_BUILTIN_TYPES
  51. #undef __JS_ENUMERATE
  52. #define __JS_ENUMERATE(ClassName, snake_name) \
  53. Object* snake_name##_prototype() { return m_##snake_name##_prototype; }
  54. JS_ENUMERATE_ITERATOR_PROTOTYPES
  55. #undef __JS_ENUMERATE
  56. protected:
  57. virtual void visit_edges(Visitor&) override;
  58. template<typename ConstructorType>
  59. void initialize_constructor(const FlyString& property_name, ConstructorType*&, Object* prototype);
  60. template<typename ConstructorType>
  61. void add_constructor(const FlyString& property_name, ConstructorType*&, Object* prototype);
  62. private:
  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. NonnullOwnPtr<Console> m_console;
  70. Shape* m_empty_object_shape { nullptr };
  71. Shape* m_new_object_shape { nullptr };
  72. Shape* m_new_script_function_prototype_object_shape { nullptr };
  73. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
  74. ProxyConstructor* m_proxy_constructor { nullptr };
  75. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  76. ConstructorName* m_##snake_name##_constructor { nullptr }; \
  77. Object* m_##snake_name##_prototype { nullptr };
  78. JS_ENUMERATE_BUILTIN_TYPES
  79. #undef __JS_ENUMERATE
  80. #define __JS_ENUMERATE(ClassName, snake_name) \
  81. Object* m_##snake_name##_prototype { nullptr };
  82. JS_ENUMERATE_ITERATOR_PROTOTYPES
  83. #undef __JS_ENUMERATE
  84. };
  85. template<typename ConstructorType>
  86. inline void GlobalObject::initialize_constructor(const FlyString& property_name, ConstructorType*& constructor, Object* prototype)
  87. {
  88. auto& vm = this->vm();
  89. constructor = heap().allocate<ConstructorType>(*this, *this);
  90. constructor->define_property(vm.names.name, js_string(heap(), property_name), Attribute::Configurable);
  91. if (vm.exception())
  92. return;
  93. if (prototype) {
  94. prototype->define_property(vm.names.constructor, constructor, Attribute::Writable | Attribute::Configurable);
  95. if (vm.exception())
  96. return;
  97. }
  98. }
  99. template<typename ConstructorType>
  100. inline void GlobalObject::add_constructor(const FlyString& property_name, ConstructorType*& constructor, Object* prototype)
  101. {
  102. initialize_constructor(property_name, constructor, prototype);
  103. define_property(property_name, constructor, Attribute::Writable | Attribute::Configurable);
  104. }
  105. inline GlobalObject* Shape::global_object() const
  106. {
  107. return static_cast<GlobalObject*>(m_global_object);
  108. }
  109. }