diff options
author | Linus Groh <mail@linusgroh.de> | 2021-09-12 11:32:14 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-12 15:53:48 +0200 |
commit | a05c998e69b2b7e524c70b34c9b5c44c48ef64ea (patch) | |
tree | 0d5542d8460f70ba89e63b97597d523e8a3de4e6 /Userland | |
parent | c5bd38252498e822ecb242e680f78e58448950a8 (diff) | |
download | serenity-a05c998e69b2b7e524c70b34c9b5c44c48ef64ea.zip |
LibWeb: Add A JS setter macro for [Replaceable] IDL properties
The [Replaceable] attribute "indicates that setting the corresponding
property on the platform object will result in an own property with the
same name being created on the object which has the value being
assigned. This property will shadow the accessor property corresponding
to the attribute, which exists on the interface prototype object."
(https://heycam.github.io/webidl/#Replaceable)
The spec doesn't tell how exactly this is supposed to be done, but other
engines just have a setter as well that just redefines the property
as a data descriptor when called, and returns undefined.
It's bound to the property name and requires an object of the correct
type, so I mirrored these constraints here. Storing the setter and
calling it multiple times will therefore just work.
Implementing this in the wrapper generator is left as an exercise for
the reader, this is going to be used in WindowObject, which isn't
generated from IDL yet.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/Bindings/Replaceable.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Bindings/Replaceable.h b/Userland/Libraries/LibWeb/Bindings/Replaceable.h new file mode 100644 index 0000000000..6617922fe9 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/Replaceable.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#define REPLACEABLE_PROPERTY_SETTER(ObjectType, property) \ + auto this_value = vm.this_value(global_object); \ + if (!this_value.is_object() || !is<ObjectType>(this_value.as_object())) { \ + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, #ObjectType); \ + return {}; \ + } \ + this_value.as_object().internal_define_own_property(#property, JS::PropertyDescriptor { .value = vm.argument(0), .writable = true }); \ + return JS::js_undefined(); |