2020-03-20 19:29:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2022-06-13 06:53:58 +00:00
|
|
|
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
|
2020-03-20 19:29:57 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-20 19:29:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-09-04 16:07:46 +00:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Function.h>
|
2022-01-30 21:52:40 +00:00
|
|
|
#include <AK/Span.h>
|
2021-09-04 16:07:46 +00:00
|
|
|
#include <AK/Vector.h>
|
2021-09-29 16:53:57 +00:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2021-09-04 16:07:46 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-20 19:29:57 +00:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-02-24 08:48:29 +00:00
|
|
|
class Array : public Object {
|
2020-06-21 13:14:02 +00:00
|
|
|
JS_OBJECT(Array, Object);
|
|
|
|
|
2020-03-20 19:29:57 +00:00
|
|
|
public:
|
2022-08-15 23:20:49 +00:00
|
|
|
static ThrowCompletionOr<Array*> create(Realm&, u64 length, Object* prototype = nullptr);
|
|
|
|
static Array* create_from(Realm&, Vector<Value> const&);
|
2021-09-04 16:07:46 +00:00
|
|
|
// Non-standard but equivalent to CreateArrayFromList.
|
|
|
|
template<typename T>
|
2022-08-15 23:20:49 +00:00
|
|
|
static Array* create_from(Realm& realm, Span<T const> elements, Function<Value(T const&)> map_fn)
|
2021-09-04 16:07:46 +00:00
|
|
|
{
|
2022-08-15 23:20:49 +00:00
|
|
|
auto values = MarkedVector<Value> { realm.heap() };
|
2021-09-04 16:07:46 +00:00
|
|
|
values.ensure_capacity(elements.size());
|
2022-02-07 14:12:41 +00:00
|
|
|
for (auto const& element : elements)
|
2021-09-04 16:07:46 +00:00
|
|
|
values.append(map_fn(element));
|
2022-02-07 14:12:41 +00:00
|
|
|
|
2022-08-15 23:20:49 +00:00
|
|
|
return Array::create_from(realm, values);
|
2021-09-04 16:07:46 +00:00
|
|
|
}
|
2020-04-17 16:24:01 +00:00
|
|
|
|
2022-03-14 16:25:06 +00:00
|
|
|
virtual ~Array() override = default;
|
2020-03-20 19:29:57 +00:00
|
|
|
|
2021-10-24 14:01:24 +00:00
|
|
|
virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyKey const&) const override;
|
|
|
|
virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyKey const&, PropertyDescriptor const&) override;
|
|
|
|
virtual ThrowCompletionOr<bool> internal_delete(PropertyKey const&) override;
|
2022-02-09 10:06:40 +00:00
|
|
|
virtual ThrowCompletionOr<MarkedVector<Value>> internal_own_property_keys() const override;
|
2021-07-07 00:50:19 +00:00
|
|
|
|
|
|
|
[[nodiscard]] bool length_is_writable() const { return m_length_writable; };
|
2020-06-20 14:25:12 +00:00
|
|
|
|
2022-08-28 21:51:28 +00:00
|
|
|
protected:
|
|
|
|
explicit Array(Object& prototype);
|
|
|
|
|
2020-03-20 19:29:57 +00:00
|
|
|
private:
|
2021-10-21 22:34:06 +00:00
|
|
|
ThrowCompletionOr<bool> set_length(PropertyDescriptor const&);
|
2021-07-07 00:50:19 +00:00
|
|
|
|
|
|
|
bool m_length_writable { true };
|
2020-03-20 19:29:57 +00:00
|
|
|
};
|
|
|
|
|
2022-08-21 16:54:08 +00:00
|
|
|
ThrowCompletionOr<double> compare_array_elements(VM&, Value x, Value y, FunctionObject* comparefn);
|
|
|
|
ThrowCompletionOr<MarkedVector<Value>> sort_indexed_properties(VM&, Object const&, size_t length, Function<ThrowCompletionOr<double>(Value, Value)> const& sort_compare, bool skip_holes);
|
2022-06-13 06:53:58 +00:00
|
|
|
|
2020-03-20 19:29:57 +00:00
|
|
|
}
|