/src/serenity/Userland/Libraries/LibJS/Runtime/Array.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> |
3 | | * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <AK/Assertions.h> |
11 | | #include <AK/Concepts.h> |
12 | | #include <AK/Function.h> |
13 | | #include <AK/Span.h> |
14 | | #include <AK/Vector.h> |
15 | | #include <LibJS/Runtime/Completion.h> |
16 | | #include <LibJS/Runtime/GlobalObject.h> |
17 | | #include <LibJS/Runtime/Object.h> |
18 | | #include <LibJS/Runtime/VM.h> |
19 | | |
20 | | namespace JS { |
21 | | |
22 | | class Array : public Object { |
23 | | JS_OBJECT(Array, Object); |
24 | | JS_DECLARE_ALLOCATOR(Array); |
25 | | |
26 | | public: |
27 | | static ThrowCompletionOr<NonnullGCPtr<Array>> create(Realm&, u64 length, Object* prototype = nullptr); |
28 | | static NonnullGCPtr<Array> create_from(Realm&, Vector<Value> const&); |
29 | | static NonnullGCPtr<Array> create_from(Realm&, ReadonlySpan<Value> const&); |
30 | | |
31 | | // Non-standard but equivalent to CreateArrayFromList. |
32 | | template<typename T> |
33 | | static NonnullGCPtr<Array> create_from(Realm& realm, ReadonlySpan<T> elements, Function<Value(T const&)> map_fn) |
34 | 0 | { |
35 | 0 | auto values = MarkedVector<Value> { realm.heap() }; |
36 | 0 | values.ensure_capacity(elements.size()); |
37 | 0 | for (auto const& element : elements) |
38 | 0 | values.append(map_fn(element)); |
39 | |
|
40 | 0 | return Array::create_from(realm, values); |
41 | 0 | } Unexecuted instantiation: JS::NonnullGCPtr<JS::Array> JS::Array::create_from<AK::String>(JS::Realm&, AK::Span<AK::String const>, AK::Function<JS::Value (AK::String const&)>) Unexecuted instantiation: JS::NonnullGCPtr<JS::Array> JS::Array::create_from<unsigned char>(JS::Realm&, AK::Span<unsigned char const>, AK::Function<JS::Value (unsigned char const&)>) Unexecuted instantiation: JS::NonnullGCPtr<JS::Array> JS::Array::create_from<AK::StringView>(JS::Realm&, AK::Span<AK::StringView const>, AK::Function<JS::Value (AK::StringView const&)>) Unexecuted instantiation: JS::NonnullGCPtr<JS::Array> JS::Array::create_from<Locale::PluralCategory>(JS::Realm&, AK::Span<Locale::PluralCategory const>, AK::Function<JS::Value (Locale::PluralCategory const&)>) Unexecuted instantiation: JS::NonnullGCPtr<JS::Array> JS::Array::create_from<Web::Bindings::KeyUsage>(JS::Realm&, AK::Span<Web::Bindings::KeyUsage const>, AK::Function<JS::Value (Web::Bindings::KeyUsage const&)>) |
42 | | |
43 | | virtual ~Array() override = default; |
44 | | |
45 | | virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyKey const&) const override final; |
46 | | virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyKey const&, PropertyDescriptor const&, Optional<PropertyDescriptor>* precomputed_get_own_property = nullptr) override final; |
47 | | virtual ThrowCompletionOr<bool> internal_delete(PropertyKey const&) override; |
48 | | virtual ThrowCompletionOr<MarkedVector<Value>> internal_own_property_keys() const override final; |
49 | | |
50 | 0 | [[nodiscard]] bool length_is_writable() const { return m_length_writable; } |
51 | | |
52 | | protected: |
53 | | explicit Array(Object& prototype); |
54 | | |
55 | | private: |
56 | | ThrowCompletionOr<bool> set_length(PropertyDescriptor const&); |
57 | | |
58 | | bool m_length_writable { true }; |
59 | | }; |
60 | | |
61 | | enum class Holes { |
62 | | SkipHoles, |
63 | | ReadThroughHoles, |
64 | | }; |
65 | | |
66 | | ThrowCompletionOr<MarkedVector<Value>> sort_indexed_properties(VM&, Object const&, size_t length, Function<ThrowCompletionOr<double>(Value, Value)> const& sort_compare, Holes holes); |
67 | | ThrowCompletionOr<double> compare_array_elements(VM&, Value x, Value y, FunctionObject* comparefn); |
68 | | |
69 | | } |