Просмотр исходного кода

LibJS: Convert Error::create() to NonnullGCPtr

Linus Groh 2 лет назад
Родитель
Сommit
d21ac9d820

+ 3 - 3
Userland/Libraries/LibJS/AST.cpp

@@ -3391,7 +3391,7 @@ Completion ImportCall::execute(Interpreter& interpreter) const
     if (!options_value.is_undefined()) {
         // a. If Type(options) is not Object,
         if (!options_value.is_object()) {
-            auto* error = TypeError::create(realm, DeprecatedString::formatted(ErrorType::NotAnObject.message(), "ImportOptions"));
+            auto error = TypeError::create(realm, DeprecatedString::formatted(ErrorType::NotAnObject.message(), "ImportOptions"));
             // i. Perform ! Call(promiseCapability.[[Reject]], undefined, « a newly created TypeError object »).
             MUST(call(vm, *promise_capability->reject(), js_undefined(), error));
 
@@ -3407,7 +3407,7 @@ Completion ImportCall::execute(Interpreter& interpreter) const
         if (!assertion_object.is_undefined()) {
             // i. If Type(assertionsObj) is not Object,
             if (!assertion_object.is_object()) {
-                auto* error = TypeError::create(realm, DeprecatedString::formatted(ErrorType::NotAnObject.message(), "ImportOptionsAssertions"));
+                auto error = TypeError::create(realm, DeprecatedString::formatted(ErrorType::NotAnObject.message(), "ImportOptionsAssertions"));
                 // 1. Perform ! Call(promiseCapability.[[Reject]], undefined, « a newly created TypeError object »).
                 MUST(call(vm, *promise_capability->reject(), js_undefined(), error));
 
@@ -3432,7 +3432,7 @@ Completion ImportCall::execute(Interpreter& interpreter) const
 
                 // 3. If Type(value) is not String, then
                 if (!value.is_string()) {
-                    auto* error = TypeError::create(realm, DeprecatedString::formatted(ErrorType::NotAString.message(), "Import Assertion option value"));
+                    auto error = TypeError::create(realm, DeprecatedString::formatted(ErrorType::NotAString.message(), "Import Assertion option value"));
                     // a. Perform ! Call(promiseCapability.[[Reject]], undefined, « a newly created TypeError object »).
                     MUST(call(vm, *promise_capability->reject(), js_undefined(), error));
 

+ 2 - 2
Userland/Libraries/LibJS/Runtime/AsyncFromSyncIteratorPrototype.cpp

@@ -135,7 +135,7 @@ JS_DEFINE_NATIVE_FUNCTION(AsyncFromSyncIteratorPrototype::return_)
 
     // 11. If Type(result) is not Object, then
     if (!result.is_object()) {
-        auto* error = TypeError::create(realm, DeprecatedString::formatted(ErrorType::NotAnObject.message(), "SyncIteratorReturnResult"));
+        auto error = TypeError::create(realm, DeprecatedString::formatted(ErrorType::NotAnObject.message(), "SyncIteratorReturnResult"));
         // a. Perform ! Call(promiseCapability.[[Reject]], undefined, « a newly created TypeError object »).
         MUST(call(vm, *promise_capability->reject(), js_undefined(), error));
         // b. Return promiseCapability.[[Promise]].
@@ -183,7 +183,7 @@ JS_DEFINE_NATIVE_FUNCTION(AsyncFromSyncIteratorPrototype::throw_)
 
     // 11. If Type(result) is not Object, then
     if (!result.is_object()) {
-        auto* error = TypeError::create(realm, DeprecatedString::formatted(ErrorType::NotAnObject.message(), "SyncIteratorThrowResult"));
+        auto error = TypeError::create(realm, DeprecatedString::formatted(ErrorType::NotAnObject.message(), "SyncIteratorThrowResult"));
         // a. Perform ! Call(promiseCapability.[[Reject]], undefined, « a newly created TypeError object »).
         MUST(call(vm, *promise_capability->reject(), js_undefined(), error));
 

+ 22 - 22
Userland/Libraries/LibJS/Runtime/Error.cpp

@@ -14,15 +14,15 @@
 
 namespace JS {
 
-Error* Error::create(Realm& realm)
+NonnullGCPtr<Error> Error::create(Realm& realm)
 {
-    return realm.heap().allocate<Error>(realm, *realm.intrinsics().error_prototype());
+    return *realm.heap().allocate<Error>(realm, *realm.intrinsics().error_prototype());
 }
 
-Error* Error::create(Realm& realm, DeprecatedString const& message)
+NonnullGCPtr<Error> Error::create(Realm& realm, DeprecatedString const& message)
 {
     auto& vm = realm.vm();
-    auto* error = Error::create(realm);
+    auto error = Error::create(realm);
     u8 attr = Attribute::Writable | Attribute::Configurable;
     error->define_direct_property(vm.names.message, PrimitiveString::create(vm, message), attr);
     return error;
@@ -98,24 +98,24 @@ DeprecatedString Error::stack_string() const
     return stack_string_builder.build();
 }
 
-#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType)              \
-    ClassName* ClassName::create(Realm& realm)                                                        \
-    {                                                                                                 \
-        return realm.heap().allocate<ClassName>(realm, *realm.intrinsics().snake_name##_prototype()); \
-    }                                                                                                 \
-                                                                                                      \
-    ClassName* ClassName::create(Realm& realm, DeprecatedString const& message)                       \
-    {                                                                                                 \
-        auto& vm = realm.vm();                                                                        \
-        auto* error = ClassName::create(realm);                                                       \
-        u8 attr = Attribute::Writable | Attribute::Configurable;                                      \
-        error->define_direct_property(vm.names.message, PrimitiveString::create(vm, message), attr);  \
-        return error;                                                                                 \
-    }                                                                                                 \
-                                                                                                      \
-    ClassName::ClassName(Object& prototype)                                                           \
-        : Error(prototype)                                                                            \
-    {                                                                                                 \
+#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType)               \
+    NonnullGCPtr<ClassName> ClassName::create(Realm& realm)                                            \
+    {                                                                                                  \
+        return *realm.heap().allocate<ClassName>(realm, *realm.intrinsics().snake_name##_prototype()); \
+    }                                                                                                  \
+                                                                                                       \
+    NonnullGCPtr<ClassName> ClassName::create(Realm& realm, DeprecatedString const& message)           \
+    {                                                                                                  \
+        auto& vm = realm.vm();                                                                         \
+        auto error = ClassName::create(realm);                                                         \
+        u8 attr = Attribute::Writable | Attribute::Configurable;                                       \
+        error->define_direct_property(vm.names.message, PrimitiveString::create(vm, message), attr);   \
+        return error;                                                                                  \
+    }                                                                                                  \
+                                                                                                       \
+    ClassName::ClassName(Object& prototype)                                                            \
+        : Error(prototype)                                                                             \
+    {                                                                                                  \
     }
 
 JS_ENUMERATE_NATIVE_ERRORS

+ 13 - 13
Userland/Libraries/LibJS/Runtime/Error.h

@@ -1,6 +1,6 @@
 /*
  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
+ * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -23,8 +23,8 @@ class Error : public Object {
     JS_OBJECT(Error, Object);
 
 public:
-    static Error* create(Realm&);
-    static Error* create(Realm&, DeprecatedString const& message);
+    static NonnullGCPtr<Error> create(Realm&);
+    static NonnullGCPtr<Error> create(Realm&, DeprecatedString const& message);
 
     virtual ~Error() override = default;
 
@@ -45,16 +45,16 @@ private:
 // NOTE: Making these inherit from Error is not required by the spec but
 //       our way of implementing the [[ErrorData]] internal slot, which is
 //       used in Object.prototype.toString().
-#define DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName) \
-    class ClassName final : public Error {                                          \
-        JS_OBJECT(ClassName, Error);                                                \
-                                                                                    \
-    public:                                                                         \
-        static ClassName* create(Realm&);                                           \
-        static ClassName* create(Realm&, DeprecatedString const& message);          \
-                                                                                    \
-        explicit ClassName(Object& prototype);                                      \
-        virtual ~ClassName() override = default;                                    \
+#define DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName)     \
+    class ClassName final : public Error {                                              \
+        JS_OBJECT(ClassName, Error);                                                    \
+                                                                                        \
+    public:                                                                             \
+        static NonnullGCPtr<ClassName> create(Realm&);                                  \
+        static NonnullGCPtr<ClassName> create(Realm&, DeprecatedString const& message); \
+                                                                                        \
+        explicit ClassName(Object& prototype);                                          \
+        virtual ~ClassName() override = default;                                        \
     };
 
 #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Promise.cpp

@@ -97,7 +97,7 @@ Promise::ResolvingFunctions Promise::create_resolving_functions()
             dbgln_if(PROMISE_DEBUG, "[Promise @ {} / PromiseResolvingFunction]: Promise can't be resolved with itself, rejecting with error", &promise);
 
             // a. Let selfResolutionError be a newly created TypeError object.
-            auto* self_resolution_error = TypeError::create(realm, "Cannot resolve promise with itself");
+            auto self_resolution_error = TypeError::create(realm, "Cannot resolve promise with itself");
 
             // b. Perform RejectPromise(promise, selfResolutionError).
             promise.reject(self_resolution_error);