Skip to content

Truly independent web browser

LibWeb: Run observable array callbacks for property definitions

sideshowbarkerfa04b0d8f498
diff --git a/Libraries/LibJS/HostClassBuilder.h b/Libraries/LibJS/HostClassBuilder.h
index 5da35fcc2ae..9f858005600 100644
--- a/Libraries/LibJS/HostClassBuilder.h
+++ b/Libraries/LibJS/HostClassBuilder.h
@@ -303,8 +303,22 @@ ThrowCompletionOr<T> completion_from_abi(JSCompletion completion)
}
}
+template<typename Traits, typename Host>
+struct HostDefineOwnPropertyHookThunk {
+ static JSCompletion define_own_property(JSObject* object, JSPropertyKey property_key, JSPropertyDescriptor* abi_descriptor, JSPropertyDescriptor const* abi_precomputed_get_own_property)
+ {
+ auto descriptor = property_descriptor_from_abi(*abi_descriptor).release_value();
+ Optional<PropertyDescriptor> precomputed_get_own_property;
+ if (abi_precomputed_get_own_property)
+ precomputed_get_own_property = property_descriptor_from_abi(*abi_precomputed_get_own_property);
+ auto result = Traits::define_own_property(static_cast<Host&>(*object_from_abi(object)), property_key_from_abi(property_key), descriptor, abi_precomputed_get_own_property ? &precomputed_get_own_property : nullptr);
+ *abi_descriptor = property_descriptor_to_abi(descriptor);
+ return completion_to_abi(result);
+ }
+};
+
template<typename Traits>
-struct HostObjectHookThunks {
+struct HostObjectHookThunks : HostDefineOwnPropertyHookThunk<Traits, HostObject> {
static HostObject& host_object_from_abi(JSObject* object)
{
return static_cast<HostObject&>(*object_from_abi(object));
@@ -339,17 +353,6 @@ struct HostObjectHookThunks {
return normal_completion_to_abi(0);
}
- static JSCompletion define_own_property(JSObject* object, JSPropertyKey property_key, JSPropertyDescriptor* abi_descriptor, JSPropertyDescriptor const* abi_precomputed_get_own_property)
- {
- auto descriptor = property_descriptor_from_abi(*abi_descriptor).release_value();
- Optional<PropertyDescriptor> precomputed_get_own_property;
- if (abi_precomputed_get_own_property)
- precomputed_get_own_property = property_descriptor_from_abi(*abi_precomputed_get_own_property);
- auto result = Traits::define_own_property(host_object_from_abi(object), property_key_from_abi(property_key), descriptor, abi_precomputed_get_own_property ? &precomputed_get_own_property : nullptr);
- *abi_descriptor = property_descriptor_to_abi(descriptor);
- return completion_to_abi(result);
- }
-
static JSCompletion has_property(JSObject* object, JSPropertyKey property_key)
{
return completion_to_abi(Traits::has_property(host_object_from_abi(object), property_key_from_abi(property_key)));
@@ -430,7 +433,7 @@ struct HostFunctionHookThunks {
};
template<typename Traits>
-struct HostArrayHookThunks {
+struct HostArrayHookThunks : HostDefineOwnPropertyHookThunk<Traits, HostArray> {
static HostArray& host_array_from_abi(JSObject* array)
{
return static_cast<HostArray&>(*object_from_abi(array));
@@ -510,6 +513,8 @@ consteval JSHostArrayHooks make_host_array_hooks()
{
using Thunks = HostABI::HostArrayHookThunks<Traits>;
JSHostArrayHooks hooks {};
+ if constexpr (requires { &Traits::define_own_property; })
+ hooks.define_own_property = &Thunks::define_own_property;
if constexpr (requires { &Traits::set; })
hooks.set = &Thunks::set;
if constexpr (requires { &Traits::delete_property; })
diff --git a/Libraries/LibJS/HostObjectABI.h b/Libraries/LibJS/HostObjectABI.h
index 9978c73c1de..66ff53282ad 100644
--- a/Libraries/LibJS/HostObjectABI.h
+++ b/Libraries/LibJS/HostObjectABI.h
@@ -206,6 +206,7 @@ typedef struct JSHostFunctionHooks {
// The internal methods that kind JS_HOST_CLASS_ARRAY may replace. The rest are those of an Array exotic object.
typedef struct JSHostArrayHooks {
+ JSCompletion (*define_own_property)(JSObject*, JSPropertyKey, JSPropertyDescriptor* descriptor, JSPropertyDescriptor const* precomputed_get_own_property);
JSCompletion (*set)(JSObject*, JSPropertyKey, JSValue value, JSValue receiver, JSSetCacheMetadata*, uint8_t phase);
JSCompletion (*delete_property)(JSObject*, JSPropertyKey);
} JSHostArrayHooks;
diff --git a/Libraries/LibJS/Runtime/HostArray.cpp b/Libraries/LibJS/Runtime/HostArray.cpp
index 210b71c53c7..f5ea971e706 100644
--- a/Libraries/LibJS/Runtime/HostArray.cpp
+++ b/Libraries/LibJS/Runtime/HostArray.cpp
@@ -37,6 +37,15 @@ void HostArray::set_host_data(GC::Ptr<GC::Cell> host_data)
js_host_object_set_host_data(object_to_abi(*this), host_data.ptr());
}
+ThrowCompletionOr<bool> HostArray::array_define_own_property(PropertyKey const& property_key, PropertyDescriptor& descriptor, Optional<PropertyDescriptor>* precomputed_get_own_property)
+{
+ auto abi_descriptor = property_descriptor_to_abi(descriptor);
+ PrecomputedOwnPropertyForABI precomputed { precomputed_get_own_property };
+ auto result = js_host_array_array_define_own_property(vm_to_abi(vm()), object_to_abi(*this), property_key_to_abi(property_key), &abi_descriptor, precomputed.pointer());
+ update_property_descriptor_from_abi(descriptor, abi_descriptor);
+ return completion_from_abi<bool>(result);
+}
+
ThrowCompletionOr<bool> HostArray::array_set(PropertyKey const& property_key, Value value, Value receiver, CacheableSetPropertyMetadata* cacheable_metadata, PropertyLookupPhase phase)
{
return completion_from_abi<bool>(js_host_array_array_set(vm_to_abi(vm()), object_to_abi(*this), property_key_to_abi(property_key), value_to_abi(value), value_to_abi(receiver), set_cache_metadata_to_abi(cacheable_metadata), lookup_phase_to_abi(phase)));
diff --git a/Libraries/LibJS/Runtime/HostArray.h b/Libraries/LibJS/Runtime/HostArray.h
index eaa96da953e..afbc5affbfa 100644
--- a/Libraries/LibJS/Runtime/HostArray.h
+++ b/Libraries/LibJS/Runtime/HostArray.h
@@ -12,8 +12,7 @@
namespace JS {
-// An Array exotic object whose [[Set]] and [[Delete]] may come from a JSHostClass of kind JS_HOST_CLASS_ARRAY, with a
-// companion cell for any state of its own.
+// An Array exotic object with host-class hooks for its internal methods and a companion cell for its own state.
class JS_API HostArray : public Array {
public:
// The prototype defaults to %Array.prototype%.
@@ -23,7 +22,8 @@ public:
GC::Ptr<GC::Cell> host_data() const;
void set_host_data(GC::Ptr<GC::Cell> host_data);
- // The Array exotic object's own [[Set]] and [[Delete]], for hooks that add to them rather than replace them.
+ // The underlying Array operations that host-class hooks can delegate to.
+ ThrowCompletionOr<bool> array_define_own_property(PropertyKey const&, PropertyDescriptor&, Optional<PropertyDescriptor>* precomputed_get_own_property = nullptr);
ThrowCompletionOr<bool> array_set(PropertyKey const&, Value value, Value receiver, CacheableSetPropertyMetadata* = nullptr, PropertyLookupPhase = PropertyLookupPhase::OwnProperty);
ThrowCompletionOr<bool> array_delete(PropertyKey const&);
diff --git a/Libraries/LibJS/Rust/generate/layout_header.rs b/Libraries/LibJS/Rust/generate/layout_header.rs
index 01462027784..cb1a2eb1c91 100644
--- a/Libraries/LibJS/Rust/generate/layout_header.rs
+++ b/Libraries/LibJS/Rust/generate/layout_header.rs
@@ -469,7 +469,7 @@ pub fn generate() -> LayoutHeader {
preserve_identity,
});
check_host_object_abi_struct!(w, JSHostFunctionHooks { call, construct, finalize });
- check_host_object_abi_struct!(w, JSHostArrayHooks { set, delete_property });
+ check_host_object_abi_struct!(w, JSHostArrayHooks { define_own_property, set, delete_property });
check_host_object_abi_struct!(w, JSResolvedBinding { module, binding_name, r#type });
check_host_object_abi_struct!(w, JSHostModuleHooks { get_exported_names, resolve_export, initialize_environment, execute_module });
check_host_object_abi_struct!(w, JSHostClass { kind, flags, name, name_length, parent, hooks, user_data });
diff --git a/Libraries/LibJS/Rust/src/embedding/host/class_table.rs b/Libraries/LibJS/Rust/src/embedding/host/class_table.rs
index 955c09f781b..82bb33f8c61 100644
--- a/Libraries/LibJS/Rust/src/embedding/host/class_table.rs
+++ b/Libraries/LibJS/Rust/src/embedding/host/class_table.rs
@@ -61,6 +61,7 @@ static HOST_OBJECT_HOOKS_THAT_ARE_ALL_ORDINARY: JSHostObjectHooks = JSHostObject
};
static HOST_ARRAY_HOOKS_THAT_ARE_ALL_ORDINARY: JSHostArrayHooks = JSHostArrayHooks {
+ define_own_property: None,
set: None,
delete_property: None,
};
diff --git a/Libraries/LibJS/Rust/src/embedding/host/host_array.rs b/Libraries/LibJS/Rust/src/embedding/host/host_array.rs
index 5987261efcd..6604c61389f 100644
--- a/Libraries/LibJS/Rust/src/embedding/host/host_array.rs
+++ b/Libraries/LibJS/Rust/src/embedding/host/host_array.rs
@@ -36,8 +36,8 @@ use crate::gc::foreign::ForeignCellSlot;
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::host_class::{
- JS_HOST_CLASS_ARRAY, JSCompletion, JSHostArrayHooks, JSHostClass, JSObject, JSPropertyKey, JSSetCacheMetadata,
- JSVM, JSValue,
+ JS_HOST_CLASS_ARRAY, JSCompletion, JSHostArrayHooks, JSHostClass, JSObject, JSPropertyDescriptor, JSPropertyKey,
+ JSSetCacheMetadata, JSVM, JSValue,
};
use crate::layout::value::Value;
use crate::runtime::array::{ARRAY_OBJECT_METHODS, Array};
@@ -45,11 +45,11 @@ use crate::runtime::completion::ThrowCompletionOr;
use crate::runtime::object::{
CacheableSetPropertyMetadata, Object, ObjectMethods, PropertyLookupPhase, allocate_object_in,
};
+use crate::runtime::property_descriptor::PropertyDescriptor;
use crate::runtime::property_key::PropertyKey;
use crate::runtime::realm::Realm;
-/// An Array exotic object whose [[Set]] and [[Delete]] may come from its host class, with a C++ GC cell for any state
-/// of its own.
+/// An Array exotic object with host-class hooks for its internal methods and a C++ GC cell for its own state.
#[repr(C)]
#[derive(Trace)]
pub struct HostArray {
@@ -70,14 +70,16 @@ impl Deref for HostArray {
}
}
-/// The internal methods of the arrays of a host class table: those of an Array exotic object, with [[Set]] and
-/// [[Delete]] calling the table's hooks for them.
+/// The internal methods of an Array exotic object, with overrides from the host class table.
fn host_array_methods(table: &'static JSHostClass) -> &'static ObjectMethods {
let hooks = table.host_array_hooks();
- if hooks.set.is_none() && hooks.delete_property.is_none() {
+ if hooks.define_own_property.is_none() && hooks.set.is_none() && hooks.delete_property.is_none() {
return &ARRAY_OBJECT_METHODS;
}
let mut methods = ObjectMethods { ..ARRAY_OBJECT_METHODS };
+ if hooks.define_own_property.is_some() {
+ methods.internal_define_own_property = define_own_property_through_hook;
+ }
if hooks.set.is_some() {
methods.internal_set = set_through_hook;
}
@@ -104,6 +106,19 @@ fn hooks_of(object: &Object) -> &'static JSHostArrayHooks {
// The internal methods of a class whose table has a hook for them. The runtime holds no borrow of its state across
// the call, and lends the hook the array and key for its duration.
+fn define_own_property_through_hook(
+ object: &Object,
+ _vm: &Vm,
+ key: &PropertyKey,
+ descriptor: &mut PropertyDescriptor,
+ precomputed_get_own_property: Option<&Option<PropertyDescriptor>>,
+) -> ThrowCompletionOr<bool> {
+ let hook = hooks_of(object)
+ .define_own_property
+ .expect("the class has a [[DefineOwnProperty]] hook");
+ super::host_object::call_define_own_property_hook(hook, object, key, descriptor, precomputed_get_own_property)
+}
+
fn set_through_hook(
object: &Object,
_vm: &Vm,
@@ -210,6 +225,29 @@ pub unsafe extern "C" fn js_host_array_create(
}
}
+/// The Array exotic object's own [[DefineOwnProperty]], for a host array that stores values after its host callback.
+/// Borrows the key and descriptor. Main thread only.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn js_host_array_array_define_own_property(
+ vm: *mut JSVM,
+ object: *mut JSObject,
+ key: *const JSPropertyKey,
+ descriptor: *mut JSPropertyDescriptor,
+ precomputed_get_own_property: *const JSPropertyDescriptor,
+) -> JSCompletion {
+ // SAFETY: The caller supplies a host array and descriptors valid for this call.
+ unsafe {
+ crate::embedding::object::define_own_property_from_abi(
+ vm,
+ object,
+ key,
+ descriptor,
+ precomputed_get_own_property,
+ ARRAY_OBJECT_METHODS.internal_define_own_property,
+ )
+ }
+}
+
/// HostArray::array_set(): the [[Set]] of an Array exotic object on a host array, for a [[Set]] hook that adds to it
/// rather than replaces it. `cache_metadata` is null or the metadata the hook received, and the phase a
/// JS_PROPERTY_LOOKUP_PHASE_* value. Borrows the key. Main thread only.
diff --git a/Libraries/LibJS/Rust/src/embedding/host/host_object.rs b/Libraries/LibJS/Rust/src/embedding/host/host_object.rs
index 6580c5a36d2..5cfdac8165a 100644
--- a/Libraries/LibJS/Rust/src/embedding/host/host_object.rs
+++ b/Libraries/LibJS/Rust/src/embedding/host/host_object.rs
@@ -281,6 +281,16 @@ fn define_own_property_through_hook(
let hook = hooks_of(object)
.define_own_property
.expect("the class has a [[DefineOwnProperty]] hook");
+ call_define_own_property_hook(hook, object, key, descriptor, precomputed_get_own_property)
+}
+
+pub(super) fn call_define_own_property_hook(
+ hook: crate::layout::host_class::DefineOwnPropertyHook,
+ object: &Object,
+ key: &PropertyKey,
+ descriptor: &mut PropertyDescriptor,
+ precomputed_get_own_property: Option<&Option<PropertyDescriptor>>,
+) -> ThrowCompletionOr<bool> {
let mut abi_descriptor = property_descriptor_to_abi(Some(descriptor));
let abi_precomputed_get_own_property =
precomputed_get_own_property.map(|precomputed| property_descriptor_to_abi(precomputed.as_ref()));
diff --git a/Libraries/LibJS/Rust/src/embedding/object.rs b/Libraries/LibJS/Rust/src/embedding/object.rs
index 303037dfff1..56b9ee9d2d3 100644
--- a/Libraries/LibJS/Rust/src/embedding/object.rs
+++ b/Libraries/LibJS/Rust/src/embedding/object.rs
@@ -1260,7 +1260,7 @@ type DefineOwnProperty = fn(
/// # Safety
///
/// See the module documentation. `descriptor` must point to a present descriptor.
-unsafe fn define_own_property_from_abi(
+pub(crate) unsafe fn define_own_property_from_abi(
vm: *mut JSVM,
object: *mut JSObject,
key: *const JSPropertyKey,
diff --git a/Libraries/LibJS/Rust/src/layout/host_class.rs b/Libraries/LibJS/Rust/src/layout/host_class.rs
index eecafdb7f99..3122b8e2dcd 100644
--- a/Libraries/LibJS/Rust/src/layout/host_class.rs
+++ b/Libraries/LibJS/Rust/src/layout/host_class.rs
@@ -108,6 +108,13 @@ pub const JS_HOST_CLASS_SHARES_ALLOCATOR_WITH_PARENT: u32 = 1 << 9;
pub const JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE: u32 = 1 << 10;
pub const JS_HOST_CLASS_NOT_CACHEABLE_ACROSS_REALMS: u32 = 1 << 11;
+pub type DefineOwnPropertyHook = unsafe extern "C" fn(
+ object: *mut JSObject,
+ key: JSPropertyKey,
+ descriptor: *mut JSPropertyDescriptor,
+ precomputed_get_own_property: *const JSPropertyDescriptor,
+) -> JSCompletion;
+
#[repr(C)]
pub struct JSHostObjectHooks {
pub get_prototype_of: Option<unsafe extern "C" fn(object: *mut JSObject) -> JSCompletion>,
@@ -117,14 +124,7 @@ pub struct JSHostObjectHooks {
pub get_own_property: Option<
unsafe extern "C" fn(object: *mut JSObject, key: JSPropertyKey, out: *mut JSPropertyDescriptor) -> JSCompletion,
>,
- pub define_own_property: Option<
- unsafe extern "C" fn(
- object: *mut JSObject,
- key: JSPropertyKey,
- descriptor: *mut JSPropertyDescriptor,
- precomputed_get_own_property: *const JSPropertyDescriptor,
- ) -> JSCompletion,
- >,
+ pub define_own_property: Option<DefineOwnPropertyHook>,
pub has_property: Option<unsafe extern "C" fn(object: *mut JSObject, key: JSPropertyKey) -> JSCompletion>,
pub get: Option<
unsafe extern "C" fn(
@@ -164,6 +164,7 @@ pub struct JSHostFunctionHooks {
#[repr(C)]
pub struct JSHostArrayHooks {
+ pub define_own_property: Option<DefineOwnPropertyHook>,
pub set: Option<
unsafe extern "C" fn(
object: *mut JSObject,
diff --git a/Libraries/LibWeb/WebIDL/ObservableArray.cpp b/Libraries/LibWeb/WebIDL/ObservableArray.cpp
index 2b640f4291c..01264f6cd9b 100644
--- a/Libraries/LibWeb/WebIDL/ObservableArray.cpp
+++ b/Libraries/LibWeb/WebIDL/ObservableArray.cpp
@@ -24,8 +24,7 @@ struct ObservableArrayHostClassTraits {
return *observable_array;
}
- // NB: Object.defineProperty() and Object.preventExtensions() bypass these hooks, so the array can refuse a store that
- // the spec never refuses. This checks for that before any callback runs.
+ // Object.preventExtensions() can make the backing array refuse a store. Check before running the callbacks.
static bool can_store(JS::HostArray& array, JS::PropertyKey const& property_key)
{
auto descriptor = MUST(array.internal_get_own_property(property_key));
@@ -40,6 +39,69 @@ struct ObservableArrayHostClassTraits {
return !descriptor.has_value() || descriptor->configurable.value_or(false);
}
+ // https://webidl.spec.whatwg.org/#js-observable-array-defineProperty
+ static JS::ThrowCompletionOr<bool> define_own_property(JS::HostArray& array, JS::PropertyKey const& property_key, JS::PropertyDescriptor& descriptor, Optional<JS::PropertyDescriptor>* precomputed_get_own_property)
+ {
+ // 1. Let handler be the this value.
+ // 2. Let descriptor be ! ToPropertyDescriptor(descriptorObj).
+ // NB: LibJS passes the host array and the converted property descriptor to this hook.
+
+ // 3. If P is "length", then:
+ if (property_key == array.vm().names.length) {
+ // 1. If IsAccessorDescriptor(descriptor) is true, then return false.
+ if (descriptor.is_accessor_descriptor())
+ return false;
+
+ // 2. If descriptor.[[Configurable]] is present and has the value true, then return false.
+ if (descriptor.configurable.value_or(false))
+ return false;
+
+ // 3. If descriptor.[[Enumerable]] is present and has the value true, then return false.
+ if (descriptor.enumerable.value_or(false))
+ return false;
+
+ // 4. If descriptor.[[Writable]] is present and has the value false, then return false.
+ if (!descriptor.writable.value_or(true))
+ return false;
+
+ // 5. If descriptor.[[Value]] is present, then return the result of setting the length given handler and descriptor.[[Value]].
+ if (descriptor.value.has_value())
+ return set_the_length(array, *descriptor.value);
+
+ // 6. Return true.
+ return true;
+ }
+
+ // 4. If P is an array index, then:
+ if (property_key.is_number()) {
+ // 1. If IsAccessorDescriptor(descriptor) is true, then return false.
+ if (descriptor.is_accessor_descriptor())
+ return false;
+
+ // 2. If descriptor.[[Configurable]] is present and has the value false, then return false.
+ if (!descriptor.configurable.value_or(true))
+ return false;
+
+ // 3. If descriptor.[[Enumerable]] is present and has the value false, then return false.
+ if (!descriptor.enumerable.value_or(true))
+ return false;
+
+ // 4. If descriptor.[[Writable]] is present and has the value false, then return false.
+ if (!descriptor.writable.value_or(true))
+ return false;
+
+ // 5. If descriptor.[[Value]] is present, then return the result of setting the indexed value given handler, P, and descriptor.[[Value]].
+ if (descriptor.value.has_value())
+ return set_the_indexed_value(array, property_key.as_number(), *descriptor.value);
+
+ // 6. Return true.
+ return true;
+ }
+
+ // 5. Return ? O.[[DefineOwnProperty]](P, descriptor).
+ return array.array_define_own_property(property_key, descriptor, precomputed_get_own_property);
+ }
+
// https://webidl.spec.whatwg.org/#js-observable-array-set
static JS::ThrowCompletionOr<bool> set(JS::HostArray& array, JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver, JS::CacheableSetPropertyMetadata* metadata, JS::Object::PropertyLookupPhase phase)
{
@@ -88,7 +150,8 @@ struct ObservableArrayHostClassTraits {
// 5. Remove the last item from handler.[[BackingList]].
// 6. Return true.
- if (!TRY(array.array_set(vm.names.length, JS::Value(index), &array)))
+ JS::PropertyDescriptor descriptor { .value = JS::Value(index) };
+ if (!TRY(array.array_define_own_property(vm.names.length, descriptor)))
return false;
observable_array.did_change_backing_list();
return true;
@@ -137,7 +200,8 @@ struct ObservableArrayHostClassTraits {
TRY(observable_array.run_delete_an_indexed_value_callback(index_to_delete));
// 2. Remove the last item from handler.[[BackingList]].
- if (!TRY(array.array_set(vm.names.length, JS::Value(index_to_delete), &array)))
+ JS::PropertyDescriptor descriptor { .value = JS::Value(index_to_delete) };
+ if (!TRY(array.array_define_own_property(vm.names.length, descriptor)))
return false;
observable_array.did_change_backing_list();
@@ -194,7 +258,7 @@ struct ObservableArrayHostClassTraits {
descriptor.enumerable = true;
descriptor.configurable = true;
}
- if (!TRY(array.internal_define_own_property(property_key, descriptor)))
+ if (!TRY(array.array_define_own_property(property_key, descriptor)))
return false;
observable_array.did_change_backing_list();
return true;
@@ -279,7 +343,6 @@ JS::ThrowCompletionOr<void> ObservableArray::run_delete_an_indexed_value_callbac
{
if (!m_on_delete_an_indexed_value)
return {};
- // NB: Object.defineProperty() can leave holes in the array. A hole holds no value to delete.
auto deleted_value = element_value(index);
if (!deleted_value.has_value())
return {};
diff --git a/Tests/LibWeb/Crash/DOM/adopted-style-sheets-with-holes.html b/Tests/LibWeb/Crash/DOM/adopted-style-sheets-with-holes.html
index 93e22282d9d..3780a36bfe4 100644
--- a/Tests/LibWeb/Crash/DOM/adopted-style-sheets-with-holes.html
+++ b/Tests/LibWeb/Crash/DOM/adopted-style-sheets-with-holes.html
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<script>
document.adoptedStyleSheets = [new CSSStyleSheet()];
- Object.defineProperty(document.adoptedStyleSheets, "length", { value: 3 });
+ Reflect.defineProperty(document.adoptedStyleSheets, "length", { value: 3 });
document.adoptedStyleSheets = [];
</script>
diff --git a/Tests/LibWeb/Text/expected/DOM/adopted-style-sheets-observable-array-operations.txt b/Tests/LibWeb/Text/expected/DOM/adopted-style-sheets-observable-array-operations.txt
index 0e26d133c1c..823d0f4bf41 100644
--- a/Tests/LibWeb/Text/expected/DOM/adopted-style-sheets-observable-array-operations.txt
+++ b/Tests/LibWeb/Text/expected/DOM/adopted-style-sheets-observable-array-operations.txt
@@ -16,15 +16,16 @@ set null over red: TypeError
after a failed set: rgb(255, 0, 0), length: 1
set a non-constructed sheet over red: NotAllowedError
after a rejected set: rgb(255, 0, 0), length: 1
-replaced after defining a non-sheet: rgb(0, 0, 0), length: 0
-removed a style element's sheet: rgb(0, 0, 255)
+define a non-sheet: TypeError
+after defining a non-sheet: rgb(255, 0, 0), length: 1
+define a non-constructed sheet: NotAllowedError
+style element still applies: rgb(0, 0, 255)
set with a setter on the prototype: rgb(255, 0, 0), own element: true, setter calls: 0
cleared after that: rgb(0, 0, 0)
-shrink past a non-configurable sheet: false
-delete a non-configurable sheet: false
-after failed removals: rgb(255, 0, 0), length: 1
-shrink a non-writable length: false
-delete with a non-writable length: false
-after failed removals: rgb(255, 0, 0), length: 1
-append with a non-writable length: false
-after a failed append: rgb(255, 0, 0), length: 1
+make an index non-configurable: false
+make length non-writable: false
+after rejected descriptors: rgb(255, 0, 0), length: 1
+append after rejected descriptors: true
+delete after rejected descriptors: true
+shrink after rejected descriptors: true
+after shrinking: rgb(0, 0, 0), length: 0
diff --git a/Tests/LibWeb/Text/expected/DOM/adopted-style-sheets-replaced-after-define-property.txt b/Tests/LibWeb/Text/expected/DOM/adopted-style-sheets-replaced-after-define-property.txt
index 10aa38ece40..7026a642123 100644
--- a/Tests/LibWeb/Text/expected/DOM/adopted-style-sheets-replaced-after-define-property.txt
+++ b/Tests/LibWeb/Text/expected/DOM/adopted-style-sheets-replaced-after-define-property.txt
@@ -1,2 +1,3 @@
+define a non-writable index: TypeError
before replacing: rgb(255, 0, 0)
after replacing: rgb(0, 0, 0)
diff --git a/Tests/LibWeb/Text/expected/WebIDL/observable-array-define-property.txt b/Tests/LibWeb/Text/expected/WebIDL/observable-array-define-property.txt
new file mode 100644
index 00000000000..47373300848
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/WebIDL/observable-array-define-property.txt
@@ -0,0 +1,15 @@
+Replacement registered the font: true
+Truncation removed the font: true
+Append succeeded: true
+Rejected a hole: true
+Rejected length growth: true
+Rejected invalid descriptors on 0: true
+Accepted empty descriptor on 0: true
+Rejected invalid descriptors on length: true
+Accepted empty descriptor on length: true
+Empty descriptor created no index: true
+Appending created a writable element: true
+Ordinary property 01: 7
+Ordinary property 4294967295: 7
+Ordinary property Symbol(): 7
+Final length: 1
diff --git a/Tests/LibWeb/Text/expected/wpt-import/webidl/ecmascript-binding/observable-array-no-leak-of-internals.window.txt b/Tests/LibWeb/Text/expected/wpt-import/webidl/ecmascript-binding/observable-array-no-leak-of-internals.window.txt
new file mode 100644
index 00000000000..b9f15e42b2f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/webidl/ecmascript-binding/observable-array-no-leak-of-internals.window.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass ObservableArray's internals won't leak
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/webidl/ecmascript-binding/observable-array-ownkeys.window.txt b/Tests/LibWeb/Text/expected/wpt-import/webidl/ecmascript-binding/observable-array-ownkeys.window.txt
new file mode 100644
index 00000000000..d19a64c715d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/webidl/ecmascript-binding/observable-array-ownkeys.window.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass ObservableArray's ownKeys trap
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/DOM/adopted-style-sheets-observable-array-operations.html b/Tests/LibWeb/Text/input/DOM/adopted-style-sheets-observable-array-operations.html
index a6b0f3468b6..037d0f82df6 100644
--- a/Tests/LibWeb/Text/input/DOM/adopted-style-sheets-observable-array-operations.html
+++ b/Tests/LibWeb/Text/input/DOM/adopted-style-sheets-observable-array-operations.html
@@ -58,14 +58,11 @@
println(`after a rejected set: ${color()}, length: ${sheets().length}`);
document.adoptedStyleSheets = [red];
- Object.defineProperty(sheets(), 1, { value: 1, writable: true, enumerable: true, configurable: true });
+ attempt("define a non-sheet", () => Reflect.defineProperty(sheets(), 1, { value: 1 }));
+ println(`after defining a non-sheet: ${color()}, length: ${sheets().length}`);
+ attempt("define a non-constructed sheet", () => Reflect.defineProperty(sheets(), 0, { value: document.styleSheets[0] }));
+ println(`style element still applies: ${getComputedStyle(target).backgroundColor}`);
document.adoptedStyleSheets = [];
- println(`replaced after defining a non-sheet: ${color()}, length: ${sheets().length}`);
-
- document.adoptedStyleSheets = [];
- Object.defineProperty(sheets(), 0, { value: document.styleSheets[0], writable: true, enumerable: true, configurable: true });
- sheets().length = 0;
- println(`removed a style element's sheet: ${getComputedStyle(target).backgroundColor}`);
let setterCalls = 0;
const prototype = Object.create(Array.prototype, { 0: { set() { ++setterCalls; } } });
@@ -77,17 +74,12 @@
Object.setPrototypeOf(sheets(), Array.prototype);
document.adoptedStyleSheets = [red];
- Object.defineProperty(sheets(), 0, { configurable: false });
- attempt("shrink past a non-configurable sheet", () => Reflect.set(sheets(), "length", 0));
- attempt("delete a non-configurable sheet", () => Reflect.deleteProperty(sheets(), 0));
- println(`after failed removals: ${color()}, length: ${sheets().length}`);
-
- document.adoptedStyleSheets = [red];
- Object.defineProperty(sheets(), "length", { writable: false });
- attempt("shrink a non-writable length", () => Reflect.set(sheets(), "length", 0));
- attempt("delete with a non-writable length", () => Reflect.deleteProperty(sheets(), 0));
- println(`after failed removals: ${color()}, length: ${sheets().length}`);
- attempt("append with a non-writable length", () => Reflect.set(sheets(), 1, green));
- println(`after a failed append: ${color()}, length: ${sheets().length}`);
+ attempt("make an index non-configurable", () => Reflect.defineProperty(sheets(), 0, { configurable: false }));
+ attempt("make length non-writable", () => Reflect.defineProperty(sheets(), "length", { writable: false }));
+ println(`after rejected descriptors: ${color()}, length: ${sheets().length}`);
+ attempt("append after rejected descriptors", () => Reflect.set(sheets(), 1, green));
+ attempt("delete after rejected descriptors", () => Reflect.deleteProperty(sheets(), 1));
+ attempt("shrink after rejected descriptors", () => Reflect.set(sheets(), "length", 0));
+ println(`after shrinking: ${color()}, length: ${sheets().length}`);
});
</script>
diff --git a/Tests/LibWeb/Text/input/DOM/adopted-style-sheets-replaced-after-define-property.html b/Tests/LibWeb/Text/input/DOM/adopted-style-sheets-replaced-after-define-property.html
index 6433af9bbdb..4c4d23dadda 100644
--- a/Tests/LibWeb/Text/input/DOM/adopted-style-sheets-replaced-after-define-property.html
+++ b/Tests/LibWeb/Text/input/DOM/adopted-style-sheets-replaced-after-define-property.html
@@ -7,7 +7,12 @@
const second = new CSSStyleSheet();
second.replaceSync("#target { color: red }");
document.adoptedStyleSheets = [first, second];
- Object.defineProperty(document.adoptedStyleSheets, 0, { writable: false });
+ try {
+ Object.defineProperty(document.adoptedStyleSheets, 0, { writable: false });
+ println("defined a non-writable index");
+ } catch (error) {
+ println(`define a non-writable index: ${error.name}`);
+ }
println(`before replacing: ${getComputedStyle(target).color}`);
document.adoptedStyleSheets = [];
println(`after replacing: ${getComputedStyle(target).color}`);
diff --git a/Tests/LibWeb/Text/input/WebIDL/observable-array-define-property.html b/Tests/LibWeb/Text/input/WebIDL/observable-array-define-property.html
new file mode 100644
index 00000000000..b5cc24c970d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/WebIDL/observable-array-define-property.html
@@ -0,0 +1,39 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const sheets = document.adoptedStyleSheets;
+ const face = new CSSStyleSheet();
+ face.replaceSync('@font-face { font-family: AdoptedFace; src: url("../wpt-import/fonts/Ahem.ttf") }');
+ const empty = new CSSStyleSheet();
+ sheets.push(empty);
+
+ async function hasFace() {
+ const loaded = await document.fonts.load("16px AdoptedFace", "x");
+ return loaded.length === 1 && document.fonts.has(loaded[0]);
+ }
+
+ Object.defineProperty(sheets, 0, { value: face });
+ println("Replacement registered the font: " + await hasFace());
+ Object.defineProperty(sheets, "length", { value: 0 });
+ println("Truncation removed the font: " + !await hasFace());
+
+ println("Append succeeded: " + Reflect.defineProperty(sheets, 0, { value: empty }));
+ println("Rejected a hole: " + !Reflect.defineProperty(sheets, 2, { value: empty }));
+ println("Rejected length growth: " + !Reflect.defineProperty(sheets, "length", { value: 3 }));
+ for (const key of [0, "length"]) {
+ const index = key === 0;
+ const bad = [{ get() {} }, { writable: false }, { enumerable: !index }, { configurable: !index }];
+ println("Rejected invalid descriptors on " + key + ": " + bad.every(d => !Reflect.defineProperty(sheets, key, d)));
+ println("Accepted empty descriptor on " + key + ": " + Reflect.defineProperty(sheets, key, {}));
+ }
+ println("Empty descriptor created no index: " + (Reflect.defineProperty(sheets, 1, {}) && !Object.hasOwn(sheets, 1)));
+ println("Appending created a writable element: " + Object.getOwnPropertyDescriptor(sheets, 0).writable);
+ for (const key of ["01", "4294967295", Symbol()]) {
+ Object.defineProperty(sheets, key, { get() { return 7; } });
+ println("Ordinary property " + String(key) + ": " + sheets[key]);
+ }
+ println("Final length: " + sheets.length);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/webidl/ecmascript-binding/observable-array-no-leak-of-internals.window.html b/Tests/LibWeb/Text/input/wpt-import/webidl/ecmascript-binding/observable-array-no-leak-of-internals.window.html
new file mode 100644
index 00000000000..dce528cc3ab
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webidl/ecmascript-binding/observable-array-no-leak-of-internals.window.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../../webidl/ecmascript-binding/observable-array-no-leak-of-internals.window.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/webidl/ecmascript-binding/observable-array-no-leak-of-internals.window.js b/Tests/LibWeb/Text/input/wpt-import/webidl/ecmascript-binding/observable-array-no-leak-of-internals.window.js
new file mode 100644
index 00000000000..f93464005d0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webidl/ecmascript-binding/observable-array-no-leak-of-internals.window.js
@@ -0,0 +1,18 @@
+"use strict";
+
+test(() => {
+ const observableArray = document.adoptedStyleSheets;
+
+ let leaked_target = null;
+ let leaked_handler = null;
+
+ let target_leaker = (target) => { leaked_target = target; return null; };
+ Object.defineProperty(Object.prototype, "getPrototypeOf", {get: function() {
+ leaked_handler = this;
+ return target_leaker;
+ }})
+ Object.getPrototypeOf(observableArray);
+
+ assert_equals(leaked_target, null, "The proxy target leaked.");
+ assert_equals(leaked_handler, null, "The proxy handler leaked.");
+}, "ObservableArray's internals won't leak");
diff --git a/Tests/LibWeb/Text/input/wpt-import/webidl/ecmascript-binding/observable-array-ownkeys.window.html b/Tests/LibWeb/Text/input/wpt-import/webidl/ecmascript-binding/observable-array-ownkeys.window.html
new file mode 100644
index 00000000000..cf70fe33f34
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webidl/ecmascript-binding/observable-array-ownkeys.window.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../../webidl/ecmascript-binding/observable-array-ownkeys.window.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/webidl/ecmascript-binding/observable-array-ownkeys.window.js b/Tests/LibWeb/Text/input/wpt-import/webidl/ecmascript-binding/observable-array-ownkeys.window.js
new file mode 100644
index 00000000000..29b537c4750
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webidl/ecmascript-binding/observable-array-ownkeys.window.js
@@ -0,0 +1,34 @@
+"use strict";
+
+test(() => {
+ const observableArray = document.adoptedStyleSheets;
+ assert_array_equals(
+ Object.getOwnPropertyNames(observableArray),
+ ["length"],
+ "Initially only \"length\".");
+
+ observableArray["zzz"] = true;
+ observableArray["aaa"] = true;
+ assert_array_equals(
+ Object.getOwnPropertyNames(observableArray),
+ ["length", "zzz", "aaa"],
+ "Own properties whose key is a string have been added.");
+
+ observableArray[0] = new CSSStyleSheet();
+ observableArray[1] = new CSSStyleSheet();
+ assert_array_equals(
+ Object.getOwnPropertyNames(observableArray),
+ ["0", "1", "length", "zzz", "aaa"],
+ "Own properties whose key is an array index have been added.");
+
+ observableArray[Symbol.toStringTag] = "string_tag";
+ observableArray[Symbol.toPrimitive] = "primitive";
+ assert_array_equals(
+ Object.getOwnPropertyNames(observableArray),
+ ["0", "1", "length", "zzz", "aaa"],
+ "Own properties whose key is a symbol have been added (non-symbol).");
+ assert_array_equals(
+ Object.getOwnPropertySymbols(observableArray),
+ [Symbol.toStringTag, Symbol.toPrimitive],
+ "Own properties whose key is a symbol have been added (symbol).");
+}, "ObservableArray's ownKeys trap");