2020-04-06 11:09:01 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-06 11:09:01 +02:00
|
|
|
*/
|
|
|
|
|
2020-03-12 20:11:35 +01:00
|
|
|
#pragma once
|
|
|
|
|
2020-05-02 20:43:44 +02:00
|
|
|
#include <LibJS/Heap/Heap.h>
|
2021-07-01 12:24:46 +02:00
|
|
|
#include <LibJS/Runtime/Environment.h>
|
2020-09-27 20:18:30 +02:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
2020-03-12 20:11:35 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-06-22 17:16:08 +02:00
|
|
|
class GlobalObject : public Object {
|
|
|
|
JS_OBJECT(GlobalObject, Object);
|
2020-06-21 15:14:02 +02:00
|
|
|
|
2020-03-12 20:11:35 +01:00
|
|
|
public:
|
2020-03-13 11:06:32 +01:00
|
|
|
explicit GlobalObject();
|
2021-03-17 16:52:26 +01:00
|
|
|
virtual void initialize_global_object();
|
2020-04-18 13:18:06 +02:00
|
|
|
|
2020-03-12 20:11:35 +01:00
|
|
|
virtual ~GlobalObject() override;
|
|
|
|
|
2020-09-29 21:15:06 +02:00
|
|
|
Console& console() { return *m_console; }
|
|
|
|
|
2020-04-18 13:56:13 +02:00
|
|
|
Shape* empty_object_shape() { return m_empty_object_shape; }
|
|
|
|
|
2020-10-17 23:13:37 +02:00
|
|
|
Shape* new_object_shape() { return m_new_object_shape; }
|
2021-06-27 22:15:58 +02:00
|
|
|
Shape* new_ordinary_function_prototype_object_shape() { return m_new_ordinary_function_prototype_object_shape; }
|
2020-10-17 23:13:37 +02:00
|
|
|
|
2020-11-30 22:50:43 +00:00
|
|
|
// Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
|
|
|
|
ProxyConstructor* proxy_constructor() { return m_proxy_constructor; }
|
|
|
|
|
2021-06-15 00:04:08 -07:00
|
|
|
// Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct constructor
|
|
|
|
GeneratorObjectPrototype* generator_object_prototype() { return m_generator_object_prototype; }
|
|
|
|
|
2021-07-25 19:37:42 +01:00
|
|
|
FunctionObject* array_prototype_values_function() const { return m_array_prototype_values_function; }
|
2021-06-27 21:48:34 +02:00
|
|
|
FunctionObject* eval_function() const { return m_eval_function; }
|
2021-07-27 00:21:16 +01:00
|
|
|
FunctionObject* temporal_time_zone_prototype_get_offset_nanoseconds_for_function() const { return m_temporal_time_zone_prototype_get_offset_nanoseconds_for_function; }
|
2021-06-28 03:53:35 +03:00
|
|
|
FunctionObject* throw_type_error_function() const { return m_throw_type_error_function; }
|
|
|
|
|
2020-12-01 21:05:25 +01:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
2020-04-18 13:18:06 +02:00
|
|
|
ConstructorName* snake_name##_constructor() { return m_##snake_name##_constructor; } \
|
|
|
|
Object* snake_name##_prototype() { return m_##snake_name##_prototype; }
|
2020-04-10 14:06:52 +02:00
|
|
|
JS_ENUMERATE_BUILTIN_TYPES
|
|
|
|
#undef __JS_ENUMERATE
|
2020-04-08 11:05:38 +02:00
|
|
|
|
2021-08-08 18:35:29 +01:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
|
|
|
|
Intl::ConstructorName* intl_##snake_name##_constructor() { return m_intl_##snake_name##_constructor; } \
|
|
|
|
Object* intl_##snake_name##_prototype() { return m_intl_##snake_name##_prototype; }
|
|
|
|
JS_ENUMERATE_INTL_OBJECTS
|
|
|
|
#undef __JS_ENUMERATE
|
|
|
|
|
2021-07-06 20:39:55 +01:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
|
|
|
|
Temporal::ConstructorName* temporal_##snake_name##_constructor() { return m_temporal_##snake_name##_constructor; } \
|
|
|
|
Object* temporal_##snake_name##_prototype() { return m_temporal_##snake_name##_prototype; }
|
|
|
|
JS_ENUMERATE_TEMPORAL_OBJECTS
|
|
|
|
#undef __JS_ENUMERATE
|
|
|
|
|
2020-07-09 14:58:20 -07:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name) \
|
|
|
|
Object* snake_name##_prototype() { return m_##snake_name##_prototype; }
|
|
|
|
JS_ENUMERATE_ITERATOR_PROTOTYPES
|
|
|
|
#undef __JS_ENUMERATE
|
|
|
|
|
2020-04-08 21:11:51 +02:00
|
|
|
protected:
|
2020-11-28 14:33:36 +01:00
|
|
|
virtual void visit_edges(Visitor&) override;
|
2020-04-08 21:11:51 +02:00
|
|
|
|
2020-12-02 00:23:40 +00:00
|
|
|
template<typename ConstructorType>
|
2021-06-13 18:59:07 +02:00
|
|
|
void initialize_constructor(PropertyName const&, ConstructorType*&, Object* prototype);
|
2020-05-02 19:18:55 +01:00
|
|
|
template<typename ConstructorType>
|
2021-06-13 18:59:07 +02:00
|
|
|
void add_constructor(PropertyName const&, ConstructorType*&, Object* prototype);
|
2020-05-02 19:18:55 +01:00
|
|
|
|
2020-03-12 20:11:35 +01:00
|
|
|
private:
|
2021-03-19 11:28:44 +01:00
|
|
|
virtual bool is_global_object() const final { return true; }
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(gc);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(is_nan);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(is_finite);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(parse_float);
|
2020-12-05 13:51:09 +01:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(parse_int);
|
2021-03-14 16:20:01 +01:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(eval);
|
2021-04-13 22:50:29 +03:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(encode_uri);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(decode_uri);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(encode_uri_component);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(decode_uri_component);
|
2021-06-05 19:21:15 +03:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(escape);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(unescape);
|
2020-04-08 11:05:38 +02:00
|
|
|
|
2020-09-29 21:15:06 +02:00
|
|
|
NonnullOwnPtr<Console> m_console;
|
|
|
|
|
2020-04-18 13:56:13 +02:00
|
|
|
Shape* m_empty_object_shape { nullptr };
|
2020-10-17 23:13:37 +02:00
|
|
|
Shape* m_new_object_shape { nullptr };
|
2021-06-27 22:15:58 +02:00
|
|
|
Shape* m_new_ordinary_function_prototype_object_shape { nullptr };
|
2020-04-18 13:56:13 +02:00
|
|
|
|
2020-11-30 22:50:43 +00:00
|
|
|
// Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
|
|
|
|
ProxyConstructor* m_proxy_constructor { nullptr };
|
|
|
|
|
2021-06-15 00:04:08 -07:00
|
|
|
// Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct constructor
|
|
|
|
GeneratorObjectPrototype* m_generator_object_prototype { nullptr };
|
2021-06-22 17:16:08 +02:00
|
|
|
|
2021-07-25 19:37:42 +01:00
|
|
|
FunctionObject* m_array_prototype_values_function { nullptr };
|
|
|
|
FunctionObject* m_eval_function { nullptr };
|
2021-07-27 00:21:16 +01:00
|
|
|
FunctionObject* m_temporal_time_zone_prototype_get_offset_nanoseconds_for_function { nullptr };
|
2021-07-25 19:37:42 +01:00
|
|
|
FunctionObject* m_throw_type_error_function { nullptr };
|
|
|
|
|
2020-12-01 21:05:25 +01:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
|
|
|
ConstructorName* m_##snake_name##_constructor { nullptr }; \
|
2020-04-18 13:18:06 +02:00
|
|
|
Object* m_##snake_name##_prototype { nullptr };
|
2020-04-10 14:06:52 +02:00
|
|
|
JS_ENUMERATE_BUILTIN_TYPES
|
|
|
|
#undef __JS_ENUMERATE
|
2020-07-09 14:58:20 -07:00
|
|
|
|
2021-08-08 18:35:29 +01:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
|
|
|
|
Intl::ConstructorName* m_intl_##snake_name##_constructor { nullptr }; \
|
|
|
|
Object* m_intl_##snake_name##_prototype { nullptr };
|
|
|
|
JS_ENUMERATE_INTL_OBJECTS
|
|
|
|
#undef __JS_ENUMERATE
|
|
|
|
|
2021-07-06 20:39:55 +01:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
|
|
|
|
Temporal::ConstructorName* m_temporal_##snake_name##_constructor { nullptr }; \
|
|
|
|
Object* m_temporal_##snake_name##_prototype { nullptr };
|
|
|
|
JS_ENUMERATE_TEMPORAL_OBJECTS
|
|
|
|
#undef __JS_ENUMERATE
|
|
|
|
|
2020-07-09 14:58:20 -07:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name) \
|
|
|
|
Object* m_##snake_name##_prototype { nullptr };
|
|
|
|
JS_ENUMERATE_ITERATOR_PROTOTYPES
|
|
|
|
#undef __JS_ENUMERATE
|
2020-03-12 20:11:35 +01:00
|
|
|
};
|
|
|
|
|
2020-05-02 20:43:44 +02:00
|
|
|
template<typename ConstructorType>
|
2021-06-13 18:59:07 +02:00
|
|
|
inline void GlobalObject::initialize_constructor(PropertyName const& property_name, ConstructorType*& constructor, Object* prototype)
|
2020-05-02 20:43:44 +02:00
|
|
|
{
|
2020-10-13 23:49:19 +02:00
|
|
|
auto& vm = this->vm();
|
2020-06-20 15:40:48 +02:00
|
|
|
constructor = heap().allocate<ConstructorType>(*this, *this);
|
2021-08-28 17:23:10 +01:00
|
|
|
constructor->define_direct_property_without_transition(vm.names.name, js_string(heap(), property_name.as_string()), Attribute::Configurable);
|
2020-10-13 23:49:19 +02:00
|
|
|
if (vm.exception())
|
2020-06-07 10:53:14 -07:00
|
|
|
return;
|
2020-11-30 22:50:43 +00:00
|
|
|
if (prototype) {
|
2021-08-28 17:23:10 +01:00
|
|
|
prototype->define_direct_property_without_transition(vm.names.constructor, constructor, Attribute::Writable | Attribute::Configurable);
|
2020-11-30 22:50:43 +00:00
|
|
|
if (vm.exception())
|
|
|
|
return;
|
|
|
|
}
|
2020-12-02 00:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ConstructorType>
|
2021-06-13 18:59:07 +02:00
|
|
|
inline void GlobalObject::add_constructor(PropertyName const& property_name, ConstructorType*& constructor, Object* prototype)
|
2020-12-02 00:23:40 +00:00
|
|
|
{
|
2021-06-11 18:03:24 +01:00
|
|
|
// Some constructors are pre-initialized separately.
|
|
|
|
if (!constructor)
|
|
|
|
initialize_constructor(property_name, constructor, prototype);
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(property_name, constructor, Attribute::Writable | Attribute::Configurable);
|
2020-05-02 20:43:44 +02:00
|
|
|
}
|
|
|
|
|
2021-01-05 12:02:59 +01:00
|
|
|
inline GlobalObject* Shape::global_object() const
|
|
|
|
{
|
|
|
|
return static_cast<GlobalObject*>(m_global_object);
|
|
|
|
}
|
|
|
|
|
2021-03-19 11:28:44 +01:00
|
|
|
template<>
|
|
|
|
inline bool Object::fast_is<GlobalObject>() const { return is_global_object(); }
|
|
|
|
|
2021-08-09 16:45:43 +02:00
|
|
|
template<typename... Args>
|
2021-09-23 20:56:28 +03:00
|
|
|
[[nodiscard]] ALWAYS_INLINE ThrowCompletionOr<Value> Value::invoke(GlobalObject& global_object, PropertyName const& property_name, Args... args)
|
2021-08-09 16:45:43 +02:00
|
|
|
{
|
|
|
|
if constexpr (sizeof...(Args) > 0) {
|
|
|
|
MarkedValueList arglist { global_object.vm().heap() };
|
|
|
|
(..., arglist.append(move(args)));
|
|
|
|
return invoke_internal(global_object, property_name, move(arglist));
|
|
|
|
}
|
|
|
|
|
|
|
|
return invoke_internal(global_object, property_name, Optional<MarkedValueList> {});
|
|
|
|
}
|
|
|
|
|
2020-03-12 20:11:35 +01:00
|
|
|
}
|