summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp
blob: f50c2e207e6d6837e40fc40dc340b62209e49906 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Function.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ArrayConstructor.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Shape.h>

namespace JS {

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

ArrayConstructor::~ArrayConstructor()
{
}

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

    // 23.1.2.4 Array.prototype, https://tc39.es/ecma262/#sec-array.prototype
    define_direct_property(vm.names.prototype, global_object.array_prototype(), 0);

    u8 attr = Attribute::Writable | Attribute::Configurable;
    define_native_function(vm.names.from, from, 1, attr);
    define_native_function(vm.names.isArray, is_array, 1, attr);
    define_native_function(vm.names.of, of, 0, attr);

    // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
    define_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);

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

// 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
Value ArrayConstructor::call()
{
    return construct(*this);
}

// 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
Value ArrayConstructor::construct(FunctionObject& new_target)
{
    auto& vm = this->vm();

    auto* proto = TRY_OR_DISCARD(get_prototype_from_constructor(global_object(), new_target, &GlobalObject::array_prototype));

    if (vm.argument_count() == 0)
        return Array::create(global_object(), 0, proto);

    if (vm.argument_count() == 1) {
        auto length = vm.argument(0);
        auto* array = Array::create(global_object(), 0, proto);
        size_t int_length;
        if (!length.is_number()) {
            MUST(array->create_data_property_or_throw(0, length));
            int_length = 1;
        } else {
            int_length = MUST(length.to_u32(global_object()));
            if (int_length != length.as_double()) {
                vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array");
                return {};
            }
        }
        TRY_OR_DISCARD(array->set(vm.names.length, Value(int_length), Object::ShouldThrowExceptions::Yes));
        return array;
    }

    auto* array = Array::create(global_object(), vm.argument_count(), proto);
    if (vm.exception())
        return {};

    for (size_t k = 0; k < vm.argument_count(); ++k)
        MUST(array->create_data_property_or_throw(k, vm.argument(k)));

    return array;
}

// 23.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-array.from
JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::from)
{
    auto constructor = vm.this_value(global_object);

    FunctionObject* map_fn = nullptr;
    if (!vm.argument(1).is_undefined()) {
        auto callback = vm.argument(1);
        if (!callback.is_function()) {
            vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
            return {};
        }
        map_fn = &callback.as_function();
    }

    auto this_arg = vm.argument(2);

    auto items = vm.argument(0);
    auto using_iterator = TRY_OR_DISCARD(items.get_method(global_object, *vm.well_known_symbol_iterator()));
    if (using_iterator) {
        Value array;
        if (constructor.is_constructor()) {
            array = vm.construct(constructor.as_function(), constructor.as_function(), {});
            if (vm.exception())
                return {};
        } else {
            array = Array::create(global_object, 0);
        }
        auto iterator = get_iterator(global_object, items, IteratorHint::Sync, using_iterator);
        if (vm.exception())
            return {};

        auto& array_object = array.as_object();

        size_t k = 0;
        while (true) {
            if (k >= MAX_ARRAY_LIKE_INDEX) {
                vm.throw_exception<TypeError>(global_object, ErrorType::ArrayMaxSize);
                iterator_close(*iterator);
                return {};
            }

            auto next = iterator_step(global_object, *iterator);
            if (vm.exception())
                return {};

            if (!next) {
                TRY_OR_DISCARD(array_object.set(vm.names.length, Value(k), Object::ShouldThrowExceptions::Yes));
                return array;
            }

            auto next_value = iterator_value(global_object, *next);
            if (vm.exception())
                return {};

            Value mapped_value;
            if (map_fn) {
                auto mapped_value_or_error = vm.call(*map_fn, this_arg, next_value, Value(k));
                if (mapped_value_or_error.is_error()) {
                    iterator_close(*iterator);
                    return {};
                }
                mapped_value = mapped_value_or_error.release_value();
            } else {
                mapped_value = next_value;
            }

            auto result_or_error = array_object.create_data_property_or_throw(k, mapped_value);
            if (result_or_error.is_error()) {
                iterator_close(*iterator);
                return {};
            }

            ++k;
        }
    }

    auto* array_like = MUST(items.to_object(global_object));

    auto length = TRY_OR_DISCARD(length_of_array_like(global_object, *array_like));

    Value array;
    if (constructor.is_constructor()) {
        MarkedValueList arguments(vm.heap());
        arguments.empend(length);
        array = vm.construct(constructor.as_function(), constructor.as_function(), move(arguments));
        if (vm.exception())
            return {};
    } else {
        array = Array::create(global_object, length);
        if (vm.exception())
            return {};
    }

    auto& array_object = array.as_object();

    for (size_t k = 0; k < length; ++k) {
        auto k_value = TRY_OR_DISCARD(array_like->get(k));
        Value mapped_value;
        if (map_fn)
            mapped_value = TRY_OR_DISCARD(vm.call(*map_fn, this_arg, k_value, Value(k)));
        else
            mapped_value = k_value;
        TRY_OR_DISCARD(array_object.create_data_property_or_throw(k, mapped_value));
    }

    TRY_OR_DISCARD(array_object.set(vm.names.length, Value(length), Object::ShouldThrowExceptions::Yes));

    return array;
}

// 23.1.2.2 Array.isArray ( arg ), https://tc39.es/ecma262/#sec-array.isarray
JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::is_array)
{
    auto value = vm.argument(0);
    return Value(TRY_OR_DISCARD(value.is_array(global_object)));
}

// 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of
JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::of)
{
    auto this_value = vm.this_value(global_object);
    Value array;
    if (this_value.is_constructor()) {
        MarkedValueList arguments(vm.heap());
        arguments.empend(vm.argument_count());
        array = vm.construct(this_value.as_function(), this_value.as_function(), move(arguments));
        if (vm.exception())
            return {};
    } else {
        array = Array::create(global_object, vm.argument_count());
        if (vm.exception())
            return {};
    }
    auto& array_object = array.as_object();
    for (size_t k = 0; k < vm.argument_count(); ++k)
        TRY_OR_DISCARD(array_object.create_data_property_or_throw(k, vm.argument(k)));
    TRY_OR_DISCARD(array_object.set(vm.names.length, Value(vm.argument_count()), Object::ShouldThrowExceptions::Yes));
    return array;
}

// 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::symbol_species_getter)
{
    return vm.this_value(global_object);
}

}