1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:57:43 +00:00

LibJS: Make intrinsics getters return NonnullGCPtr

Some of these are allocated upon initialization of the intrinsics, and
some lazily, but in neither case the getters actually return a nullptr.

This saves us a whole bunch of pointer dereferences (as NonnullGCPtr has
an `operator T&()`), and also has the interesting side effect of forcing
us to explicitly use the FunctionObject& overload of call(), as passing
a NonnullGCPtr is ambigous - it could implicitly be turned into a Value
_or_ a FunctionObject& (so we have to dereference manually).
This commit is contained in:
Linus Groh 2023-04-13 00:47:15 +02:00
parent ed9e2366da
commit b84f8fb55b
182 changed files with 564 additions and 567 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -12,7 +12,7 @@
namespace JS {
ErrorConstructor::ErrorConstructor(Realm& realm)
: NativeFunction(realm.vm().names.Error.as_string(), *realm.intrinsics().function_prototype())
: NativeFunction(realm.vm().names.Error.as_string(), realm.intrinsics().function_prototype())
{
}
@ -63,59 +63,59 @@ ThrowCompletionOr<NonnullGCPtr<Object>> ErrorConstructor::construct(FunctionObje
return error;
}
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
ConstructorName::ConstructorName(Realm& realm) \
: NativeFunction(realm.vm().names.ClassName.as_string(), *static_cast<Object*>(realm.intrinsics().error_constructor())) \
{ \
} \
\
ThrowCompletionOr<void> ConstructorName::initialize(Realm& realm) \
{ \
auto& vm = this->vm(); \
MUST_OR_THROW_OOM(NativeFunction::initialize(realm)); \
\
/* 20.5.6.2.1 NativeError.prototype, https://tc39.es/ecma262/#sec-nativeerror.prototype */ \
define_direct_property(vm.names.prototype, realm.intrinsics().snake_name##_prototype(), 0); \
\
define_direct_property(vm.names.length, Value(1), Attribute::Configurable); \
\
return {}; \
} \
\
ConstructorName::~ConstructorName() = default; \
\
/* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
ThrowCompletionOr<Value> ConstructorName::call() \
{ \
/* 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget. */ \
return TRY(construct(*this)); \
} \
\
/* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
ThrowCompletionOr<NonnullGCPtr<Object>> ConstructorName::construct(FunctionObject& new_target) \
{ \
auto& vm = this->vm(); \
\
auto message = vm.argument(0); \
auto options = vm.argument(1); \
\
/* 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%NativeError.prototype%", « [[ErrorData]] »). */ \
auto error = TRY(ordinary_create_from_constructor<ClassName>(vm, new_target, &Intrinsics::snake_name##_prototype)); \
\
/* 3. If message is not undefined, then */ \
if (!message.is_undefined()) { \
/* a. Let msg be ? ToString(message). */ \
auto msg = TRY(message.to_string(vm)); \
\
/* b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg). */ \
error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, move(msg))); \
} \
\
/* 4. Perform ? InstallErrorCause(O, options). */ \
TRY(error->install_error_cause(options)); \
\
/* 5. Return O. */ \
return error; \
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
ConstructorName::ConstructorName(Realm& realm) \
: NativeFunction(realm.vm().names.ClassName.as_string(), realm.intrinsics().error_constructor()) \
{ \
} \
\
ThrowCompletionOr<void> ConstructorName::initialize(Realm& realm) \
{ \
auto& vm = this->vm(); \
MUST_OR_THROW_OOM(NativeFunction::initialize(realm)); \
\
/* 20.5.6.2.1 NativeError.prototype, https://tc39.es/ecma262/#sec-nativeerror.prototype */ \
define_direct_property(vm.names.prototype, realm.intrinsics().snake_name##_prototype(), 0); \
\
define_direct_property(vm.names.length, Value(1), Attribute::Configurable); \
\
return {}; \
} \
\
ConstructorName::~ConstructorName() = default; \
\
/* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
ThrowCompletionOr<Value> ConstructorName::call() \
{ \
/* 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget. */ \
return TRY(construct(*this)); \
} \
\
/* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
ThrowCompletionOr<NonnullGCPtr<Object>> ConstructorName::construct(FunctionObject& new_target) \
{ \
auto& vm = this->vm(); \
\
auto message = vm.argument(0); \
auto options = vm.argument(1); \
\
/* 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%NativeError.prototype%", « [[ErrorData]] »). */ \
auto error = TRY(ordinary_create_from_constructor<ClassName>(vm, new_target, &Intrinsics::snake_name##_prototype)); \
\
/* 3. If message is not undefined, then */ \
if (!message.is_undefined()) { \
/* a. Let msg be ? ToString(message). */ \
auto msg = TRY(message.to_string(vm)); \
\
/* b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg). */ \
error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, move(msg))); \
} \
\
/* 4. Perform ? InstallErrorCause(O, options). */ \
TRY(error->install_error_cause(options)); \
\
/* 5. Return O. */ \
return error; \
}
JS_ENUMERATE_NATIVE_ERRORS