2020-04-06 11:09:01 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2022-05-05 20:43:51 +02:00
|
|
|
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
|
2020-04-06 11:09:01 +02:00
|
|
|
*
|
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; }
|
|
|
|
|
2021-10-14 12:04:10 +01:00
|
|
|
Realm* associated_realm();
|
|
|
|
void set_associated_realm(Badge<Realm>, Realm&);
|
|
|
|
|
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
|
2022-05-05 20:43:51 +02:00
|
|
|
Object* async_from_sync_iterator_prototype() { return m_async_from_sync_iterator_prototype; }
|
2022-05-05 08:47:36 +02:00
|
|
|
Object* async_generator_prototype() { return m_async_generator_prototype; }
|
2022-05-05 20:43:51 +02:00
|
|
|
Object* generator_prototype() { return m_generator_prototype; }
|
2021-06-15 00:04:08 -07:00
|
|
|
|
2022-05-05 08:49:46 +02:00
|
|
|
// Alias for the AsyncGenerator Prototype Object used by the spec (%AsyncGeneratorFunction.prototype.prototype%)
|
|
|
|
Object* async_generator_function_prototype_prototype() { return m_async_generator_prototype; }
|
|
|
|
// Alias for the Generator Prototype Object used by the spec (%GeneratorFunction.prototype.prototype%)
|
|
|
|
Object* generator_function_prototype_prototype() { return m_generator_prototype; }
|
|
|
|
|
2022-01-30 01:08:42 +02:00
|
|
|
// Not included in JS_ENUMERATE_INTL_OBJECTS due to missing distinct constructor
|
2022-05-05 20:43:51 +02:00
|
|
|
Object* intl_segments_prototype() { return m_intl_segments_prototype; }
|
2022-01-30 01:08:42 +02:00
|
|
|
|
2021-07-25 19:37:42 +01:00
|
|
|
FunctionObject* array_prototype_values_function() const { return m_array_prototype_values_function; }
|
2021-12-04 18:56:44 -05:00
|
|
|
FunctionObject* date_constructor_now_function() const { return m_date_constructor_now_function; }
|
2021-06-27 21:48:34 +02:00
|
|
|
FunctionObject* eval_function() const { return m_eval_function; }
|
2022-01-27 14:49:55 +01:00
|
|
|
FunctionObject* json_parse_function() const { return m_json_parse_function; }
|
2022-04-11 22:47:50 +01:00
|
|
|
FunctionObject* object_prototype_to_string_function() const { return m_object_prototype_to_string_function; }
|
|
|
|
FunctionObject* throw_type_error_function() const { return m_throw_type_error_function; }
|
2021-06-28 03:53:35 +03:00
|
|
|
|
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>
|
2022-02-19 17:49:25 +02:00
|
|
|
void initialize_constructor(PropertyKey const&, ConstructorType*&, Object* prototype, PropertyAttributes = Attribute::Writable | Attribute::Configurable);
|
2020-05-02 19:18:55 +01:00
|
|
|
template<typename ConstructorType>
|
2021-10-24 16:01:24 +02:00
|
|
|
void add_constructor(PropertyKey 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; }
|
|
|
|
|
2021-10-29 17:04:01 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(gc);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(is_nan);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(is_finite);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(parse_float);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(parse_int);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(eval);
|
|
|
|
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);
|
|
|
|
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;
|
|
|
|
|
2021-10-14 12:04:10 +01:00
|
|
|
WeakPtr<Realm> m_associated_realm;
|
|
|
|
|
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
|
2022-05-05 20:43:51 +02:00
|
|
|
Object* m_async_from_sync_iterator_prototype { nullptr };
|
2022-05-05 08:47:36 +02:00
|
|
|
Object* m_async_generator_prototype { nullptr };
|
2022-05-05 20:43:51 +02:00
|
|
|
Object* m_generator_prototype { nullptr };
|
2021-06-22 17:16:08 +02:00
|
|
|
|
2022-01-30 01:08:42 +02:00
|
|
|
// Not included in JS_ENUMERATE_INTL_OBJECTS due to missing distinct constructor
|
2022-05-05 20:43:51 +02:00
|
|
|
Object* m_intl_segments_prototype { nullptr };
|
2022-01-30 01:08:42 +02:00
|
|
|
|
2021-07-25 19:37:42 +01:00
|
|
|
FunctionObject* m_array_prototype_values_function { nullptr };
|
2021-12-04 18:56:44 -05:00
|
|
|
FunctionObject* m_date_constructor_now_function { nullptr };
|
2021-07-25 19:37:42 +01:00
|
|
|
FunctionObject* m_eval_function { nullptr };
|
2022-01-27 14:49:55 +01:00
|
|
|
FunctionObject* m_json_parse_function { nullptr };
|
2022-04-11 22:47:50 +01:00
|
|
|
FunctionObject* m_object_prototype_to_string_function { nullptr };
|
|
|
|
FunctionObject* m_throw_type_error_function { nullptr };
|
2021-07-25 19:37:42 +01:00
|
|
|
|
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>
|
2022-02-19 17:49:25 +02:00
|
|
|
inline void GlobalObject::initialize_constructor(PropertyKey const& property_key, ConstructorType*& constructor, Object* prototype, PropertyAttributes attributes)
|
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);
|
2022-02-06 15:59:04 +00:00
|
|
|
constructor->define_direct_property(vm.names.name, js_string(heap(), property_key.as_string()), Attribute::Configurable);
|
2021-11-14 12:21:48 +00:00
|
|
|
if (prototype)
|
2022-02-19 17:49:25 +02:00
|
|
|
prototype->define_direct_property(vm.names.constructor, constructor, attributes);
|
2020-12-02 00:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ConstructorType>
|
2022-02-06 15:59:04 +00:00
|
|
|
inline void GlobalObject::add_constructor(PropertyKey const& property_key, 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)
|
2022-02-06 15:59:04 +00:00
|
|
|
initialize_constructor(property_key, constructor, prototype);
|
|
|
|
define_direct_property(property_key, 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>
|
2022-02-06 15:59:04 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE ThrowCompletionOr<Value> Value::invoke(GlobalObject& global_object, PropertyKey const& property_key, Args... args)
|
2021-08-09 16:45:43 +02:00
|
|
|
{
|
|
|
|
if constexpr (sizeof...(Args) > 0) {
|
2022-02-09 10:06:40 +00:00
|
|
|
MarkedVector<Value> arglist { global_object.vm().heap() };
|
2021-08-09 16:45:43 +02:00
|
|
|
(..., arglist.append(move(args)));
|
2022-02-06 15:59:04 +00:00
|
|
|
return invoke_internal(global_object, property_key, move(arglist));
|
2021-08-09 16:45:43 +02:00
|
|
|
}
|
|
|
|
|
2022-02-09 10:06:40 +00:00
|
|
|
return invoke_internal(global_object, property_key, Optional<MarkedVector<Value>> {});
|
2021-08-09 16:45:43 +02:00
|
|
|
}
|
|
|
|
|
2020-03-12 20:11:35 +01:00
|
|
|
}
|