summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/ErrorConstructor.cpp
blob: c992b48a47be8ff295737c2a998d1bc58c52b600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/ErrorConstructor.h>
#include <LibJS/Runtime/GlobalObject.h>

namespace JS {

ErrorConstructor::ErrorConstructor(GlobalObject& global_object)
    : NativeFunction(vm().names.Error.as_string(), *global_object.function_prototype())
{
}

void ErrorConstructor::initialize(GlobalObject& global_object)
{
    auto& vm = this->vm();
    NativeFunction::initialize(global_object);

    // 20.5.2.1 Error.prototype, https://tc39.es/ecma262/#sec-error.prototype
    define_direct_property(vm.names.prototype, global_object.error_prototype(), 0);

    define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
}

// 20.5.1.1 Error ( message ), https://tc39.es/ecma262/#sec-error-message
ThrowCompletionOr<Value> ErrorConstructor::call()
{
    return TRY(construct(*this));
}

// 20.5.1.1 Error ( message ), https://tc39.es/ecma262/#sec-error-message
ThrowCompletionOr<Object*> ErrorConstructor::construct(FunctionObject& new_target)
{
    auto& vm = this->vm();
    auto& global_object = this->global_object();

    auto* error = TRY(ordinary_create_from_constructor<Error>(global_object, new_target, &GlobalObject::error_prototype));

    if (!vm.argument(0).is_undefined()) {
        auto message = TRY(vm.argument(0).to_string(global_object));
        MUST(error->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(vm, message)));
    }

    TRY(error->install_error_cause(vm.argument(1)));

    return error;
}

#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType)                         \
    ConstructorName::ConstructorName(GlobalObject& global_object)                                                \
        : NativeFunction(*static_cast<Object*>(global_object.error_constructor()))                               \
    {                                                                                                            \
    }                                                                                                            \
                                                                                                                 \
    void ConstructorName::initialize(GlobalObject& global_object)                                                \
    {                                                                                                            \
        auto& vm = this->vm();                                                                                   \
        NativeFunction::initialize(global_object);                                                               \
                                                                                                                 \
        /* 20.5.6.2.1 NativeError.prototype,                                                                     \
           https://tc39.es/ecma262/#sec-nativeerror.prototype */                                                 \
        define_direct_property(vm.names.prototype, global_object.snake_name##_prototype(), 0);                   \
                                                                                                                 \
        define_direct_property(vm.names.length, Value(1), Attribute::Configurable);                              \
    }                                                                                                            \
                                                                                                                 \
    ConstructorName::~ConstructorName() { }                                                                      \
                                                                                                                 \
    /* 20.5.6.1.1 NativeError ( message ), https://tc39.es/ecma262/#sec-nativeerror */                           \
    ThrowCompletionOr<Value> ConstructorName::call()                                                             \
    {                                                                                                            \
        return TRY(construct(*this));                                                                            \
    }                                                                                                            \
                                                                                                                 \
    /* 20.5.6.1.1 NativeError ( message ), https://tc39.es/ecma262/#sec-nativeerror */                           \
    ThrowCompletionOr<Object*> ConstructorName::construct(FunctionObject& new_target)                            \
    {                                                                                                            \
        auto& vm = this->vm();                                                                                   \
        auto& global_object = this->global_object();                                                             \
                                                                                                                 \
        auto* error = TRY(ordinary_create_from_constructor<ClassName>(                                           \
            global_object, new_target, &GlobalObject::snake_name##_prototype));                                  \
                                                                                                                 \
        if (!vm.argument(0).is_undefined()) {                                                                    \
            auto message = TRY(vm.argument(0).to_string(global_object));                                         \
            MUST(error->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(vm, message))); \
        }                                                                                                        \
                                                                                                                 \
        TRY(error->install_error_cause(vm.argument(1)));                                                         \
                                                                                                                 \
        return error;                                                                                            \
    }

JS_ENUMERATE_NATIVE_ERRORS
#undef __JS_ENUMERATE

}