GlobalObject.h 7.1 KB

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