/src/serenity/Userland/Libraries/LibWeb/WebIDL/ObservableArray.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibWeb/Bindings/ExceptionOrUtils.h> |
8 | | #include <LibWeb/WebIDL/ObservableArray.h> |
9 | | |
10 | | namespace Web::WebIDL { |
11 | | |
12 | | JS_DEFINE_ALLOCATOR(ObservableArray); |
13 | | |
14 | | JS::NonnullGCPtr<ObservableArray> ObservableArray::create(JS::Realm& realm) |
15 | 0 | { |
16 | 0 | auto prototype = realm.intrinsics().array_prototype(); |
17 | 0 | return realm.heap().allocate<ObservableArray>(realm, prototype); |
18 | 0 | } |
19 | | |
20 | | ObservableArray::ObservableArray(Object& prototype) |
21 | 0 | : JS::Array(prototype) |
22 | 0 | { |
23 | 0 | } |
24 | | |
25 | | void ObservableArray::visit_edges(JS::Cell::Visitor& visitor) |
26 | 0 | { |
27 | 0 | Base::visit_edges(visitor); |
28 | 0 | visitor.visit(m_on_set_an_indexed_value); |
29 | 0 | visitor.visit(m_on_delete_an_indexed_value); |
30 | 0 | } |
31 | | |
32 | | void ObservableArray::set_on_set_an_indexed_value_callback(SetAnIndexedValueCallbackFunction&& callback) |
33 | 0 | { |
34 | 0 | m_on_set_an_indexed_value = create_heap_function(heap(), move(callback)); |
35 | 0 | } |
36 | | |
37 | | void ObservableArray::set_on_delete_an_indexed_value_callback(DeleteAnIndexedValueCallbackFunction&& callback) |
38 | 0 | { |
39 | 0 | m_on_delete_an_indexed_value = create_heap_function(heap(), move(callback)); |
40 | 0 | } |
41 | | |
42 | | JS::ThrowCompletionOr<bool> ObservableArray::internal_set(JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver, JS::CacheablePropertyMetadata* metadata) |
43 | 0 | { |
44 | 0 | if (property_key.is_number() && m_on_set_an_indexed_value) |
45 | 0 | TRY(Bindings::throw_dom_exception_if_needed(vm(), [&] { return m_on_set_an_indexed_value->function()(value); })); |
46 | 0 | return TRY(Base::internal_set(property_key, value, receiver, metadata)); |
47 | 0 | } |
48 | | |
49 | | JS::ThrowCompletionOr<bool> ObservableArray::internal_delete(JS::PropertyKey const& property_key) |
50 | 0 | { |
51 | 0 | if (property_key.is_number() && m_on_delete_an_indexed_value) |
52 | 0 | TRY(Bindings::throw_dom_exception_if_needed(vm(), [&] { return m_on_delete_an_indexed_value->function()(); })); |
53 | 0 | return JS::Array::internal_delete(property_key); |
54 | 0 | } |
55 | | |
56 | | JS::ThrowCompletionOr<void> ObservableArray::append(JS::Value value) |
57 | 0 | { |
58 | 0 | if (m_on_set_an_indexed_value) |
59 | 0 | TRY(Bindings::throw_dom_exception_if_needed(vm(), [&] { return m_on_set_an_indexed_value->function()(value); })); |
60 | 0 | indexed_properties().append(value); |
61 | 0 | return {}; |
62 | 0 | } |
63 | | |
64 | | void ObservableArray::clear() |
65 | 0 | { |
66 | 0 | while (!indexed_properties().is_empty()) { |
67 | 0 | indexed_properties().storage()->take_first(); |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | } |