mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Auto merge of #7254 - frewsxcv:own-property-keys, r=Ms2ger
Initial implementation of ownPropertyKeys proxy handler Generates `SupportedPropertyNames` on DOM structs that should implement it. Most of them are unimplemented now (which can be implemented in later PRs), with the exception of `HTMLCollection`. Also added a couple relevant WPT tests. Closes #6390 Closes #2215 <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7254) <!-- Reviewable:end -->
This commit is contained in:
commit
d2a8c278ea
13 changed files with 237 additions and 21 deletions
|
@ -2473,7 +2473,7 @@ let traps = ProxyTraps {
|
|||
enter: None,
|
||||
getOwnPropertyDescriptor: Some(getOwnPropertyDescriptor),
|
||||
defineProperty: Some(%s),
|
||||
ownPropertyKeys: Some(proxyhandler::own_property_keys),
|
||||
ownPropertyKeys: Some(own_property_keys),
|
||||
delete_: Some(%s),
|
||||
enumerate: None,
|
||||
preventExtensions: Some(proxyhandler::prevent_extensions),
|
||||
|
@ -4178,6 +4178,59 @@ class CGDOMJSProxyHandler_delete(CGAbstractExternMethod):
|
|||
return CGGeneric(self.getBody())
|
||||
|
||||
|
||||
class CGDOMJSProxyHandler_ownPropertyKeys(CGAbstractExternMethod):
|
||||
def __init__(self, descriptor):
|
||||
args = [Argument('*mut JSContext', 'cx'),
|
||||
Argument('HandleObject', 'proxy'),
|
||||
Argument('*mut AutoIdVector', 'props')]
|
||||
CGAbstractExternMethod.__init__(self, descriptor, "own_property_keys", "u8", args)
|
||||
self.descriptor = descriptor
|
||||
|
||||
def getBody(self):
|
||||
body = dedent(
|
||||
"""
|
||||
let unwrapped_proxy = UnwrapProxy(proxy);
|
||||
""")
|
||||
|
||||
if self.descriptor.operations['IndexedGetter']:
|
||||
body += dedent(
|
||||
"""
|
||||
for i in 0..(*unwrapped_proxy).Length() {
|
||||
let rooted_jsid = RootedId::new(cx, int_to_jsid(i as i32));
|
||||
AppendToAutoIdVector(props, rooted_jsid.handle().get());
|
||||
}
|
||||
""")
|
||||
|
||||
if self.descriptor.operations['NamedGetter']:
|
||||
body += dedent(
|
||||
"""
|
||||
for name in (*unwrapped_proxy).SupportedPropertyNames() {
|
||||
let cstring = CString::new(name).unwrap();
|
||||
let jsstring = JS_InternString(cx, cstring.as_ptr());
|
||||
let mut rooted = RootedString::new(cx, jsstring);
|
||||
let jsid = INTERNED_STRING_TO_JSID(cx, rooted.handle().get());
|
||||
let rooted_jsid = RootedId::new(cx, jsid);
|
||||
AppendToAutoIdVector(props, rooted_jsid.handle().get());
|
||||
}
|
||||
""")
|
||||
|
||||
body += dedent(
|
||||
"""
|
||||
let expando = get_expando_object(proxy);
|
||||
if !expando.is_null() {
|
||||
let rooted_expando = RootedObject::new(cx, expando);
|
||||
GetPropertyKeys(cx, rooted_expando.handle(), JSITER_OWNONLY | JSITER_HIDDEN | JSITER_SYMBOLS, props);
|
||||
}
|
||||
|
||||
return JSTrue;
|
||||
""")
|
||||
|
||||
return body
|
||||
|
||||
def definition_body(self):
|
||||
return CGGeneric(self.getBody())
|
||||
|
||||
|
||||
class CGDOMJSProxyHandler_hasOwn(CGAbstractExternMethod):
|
||||
def __init__(self, descriptor):
|
||||
args = [Argument('*mut JSContext', 'cx'), Argument('HandleObject', 'proxy'),
|
||||
|
@ -4496,6 +4549,14 @@ class CGInterfaceTrait(CGThing):
|
|||
infallible = 'infallible' in descriptor.getExtendedAttributes(operation)
|
||||
if operation.isGetter():
|
||||
arguments = method_arguments(descriptor, rettype, arguments, trailing=("found", "&mut bool"))
|
||||
|
||||
# If this interface 'supports named properties', then we
|
||||
# should be able to access 'supported property names'
|
||||
#
|
||||
# WebIDL, Second Draft, section 3.2.4.5
|
||||
# https://heycam.github.io/webidl/#idl-named-properties
|
||||
if operation.isNamed():
|
||||
yield "SupportedPropertyNames", [], "Vec<DOMString>"
|
||||
else:
|
||||
arguments = method_arguments(descriptor, rettype, arguments)
|
||||
rettype = return_type(descriptor, rettype, infallible)
|
||||
|
@ -4600,6 +4661,7 @@ class CGDescriptor(CGThing):
|
|||
# cgThings.append(CGProxyIsProxy(descriptor))
|
||||
cgThings.append(CGProxyUnwrap(descriptor))
|
||||
cgThings.append(CGDOMJSProxyHandlerDOMClass(descriptor))
|
||||
cgThings.append(CGDOMJSProxyHandler_ownPropertyKeys(descriptor))
|
||||
cgThings.append(CGDOMJSProxyHandler_getOwnPropertyDescriptor(descriptor))
|
||||
cgThings.append(CGDOMJSProxyHandler_className(descriptor))
|
||||
cgThings.append(CGDOMJSProxyHandler_get(descriptor))
|
||||
|
@ -4958,6 +5020,7 @@ class CGBindingRoot(CGThing):
|
|||
'js::{JSCLASS_IS_GLOBAL, JSCLASS_RESERVED_SLOTS_SHIFT}',
|
||||
'js::{JSCLASS_RESERVED_SLOTS_MASK}',
|
||||
'js::{JSPROP_ENUMERATE, JSPROP_SHARED}',
|
||||
'js::{JSITER_OWNONLY, JSITER_HIDDEN, JSITER_SYMBOLS}',
|
||||
'js::jsapi::{JS_CallFunctionValue, JS_GetClass, JS_GetGlobalForObject}',
|
||||
'js::jsapi::{JS_GetObjectPrototype, JS_GetProperty, JS_GetPropertyById}',
|
||||
'js::jsapi::{JS_GetPropertyDescriptorById, JS_GetReservedSlot}',
|
||||
|
@ -4967,20 +5030,22 @@ class CGBindingRoot(CGThing):
|
|||
'js::jsapi::{JSClass, FreeOp, JSFreeOp, JSFunctionSpec, jsid}',
|
||||
'js::jsapi::{MutableHandleValue, MutableHandleObject, HandleObject, HandleValue, RootedObject}',
|
||||
'js::jsapi::{RootedValue, JSNativeWrapper, JSNative, JSObject, JSPropertyDescriptor}',
|
||||
'js::jsapi::{RootedId, JS_InternString, RootedString, INTERNED_STRING_TO_JSID}',
|
||||
'js::jsapi::{JSPropertySpec}',
|
||||
'js::jsapi::{JSString, JSTracer, JSJitInfo, JSJitInfo_OpType, JSJitInfo_AliasSet}',
|
||||
'js::jsapi::{MutableHandle, Handle, HandleId, JSType, JSValueType}',
|
||||
'js::jsapi::{SymbolCode, ObjectOpResult, HandleValueArray}',
|
||||
'js::jsapi::{JSJitGetterCallArgs, JSJitSetterCallArgs, JSJitMethodCallArgs, CallArgs}',
|
||||
'js::jsapi::{JSAutoCompartment, JSAutoRequest, JS_ComputeThis}',
|
||||
'js::jsapi::GetGlobalForObjectCrossCompartment',
|
||||
'js::jsapi::{GetGlobalForObjectCrossCompartment, AutoIdVector, GetPropertyKeys}',
|
||||
'js::jsval::JSVal',
|
||||
'js::jsval::{ObjectValue, ObjectOrNullValue, PrivateValue}',
|
||||
'js::jsval::{NullValue, UndefinedValue}',
|
||||
'js::glue::{CallJitMethodOp, CallJitGetterOp, CallJitSetterOp, CreateProxyHandler}',
|
||||
'js::glue::{GetProxyPrivate, NewProxyObject, ProxyTraps}',
|
||||
'js::glue::{RUST_FUNCTION_VALUE_TO_JITINFO}',
|
||||
'js::glue::{RUST_JS_NumberValue, RUST_JSID_IS_STRING}',
|
||||
'js::glue::{RUST_JS_NumberValue, RUST_JSID_IS_STRING, int_to_jsid}',
|
||||
'js::glue::AppendToAutoIdVector',
|
||||
'js::rust::GCMethods',
|
||||
'js::{JSTrue, JSFalse}',
|
||||
'dom::bindings',
|
||||
|
|
|
@ -11,7 +11,6 @@ use dom::bindings::utils::delete_property_by_id;
|
|||
use js::glue::GetProxyExtra;
|
||||
use js::glue::InvokeGetOwnPropertyDescriptor;
|
||||
use js::glue::{SetProxyExtra, GetProxyHandler};
|
||||
use js::jsapi::AutoIdVector;
|
||||
use js::jsapi::GetObjectProto;
|
||||
use js::jsapi::{Handle, HandleObject, HandleId, MutableHandle, RootedObject, ObjectOpResult};
|
||||
use js::jsapi::{JSContext, JSPropertyDescriptor, JSObject};
|
||||
|
@ -83,15 +82,6 @@ pub unsafe extern fn delete(cx: *mut JSContext, proxy: HandleObject, id: HandleI
|
|||
delete_property_by_id(cx, expando.handle(), id, bp)
|
||||
}
|
||||
|
||||
/// Stub for ownPropertyKeys
|
||||
pub unsafe extern fn own_property_keys(_cx: *mut JSContext,
|
||||
_proxy: HandleObject,
|
||||
_props: *mut AutoIdVector) -> u8 {
|
||||
// FIXME: implement this
|
||||
// https://github.com/servo/servo/issues/6390
|
||||
JSTrue
|
||||
}
|
||||
|
||||
/// Controls whether the Extensible bit can be changed
|
||||
pub unsafe extern fn prevent_extensions(_cx: *mut JSContext,
|
||||
_proxy: HandleObject,
|
||||
|
|
|
@ -1912,6 +1912,12 @@ impl<'a> DocumentMethods for &'a Document {
|
|||
collection.r().reflector().get_jsobject().get()
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/#document
|
||||
fn SupportedPropertyNames(self) -> Vec<DOMString> {
|
||||
// FIXME: unimplemented (https://github.com/servo/servo/issues/7273)
|
||||
vec![]
|
||||
}
|
||||
|
||||
global_event_handlers!();
|
||||
event_handler!(readystatechange, GetOnreadystatechange, SetOnreadystatechange);
|
||||
}
|
||||
|
|
|
@ -67,5 +67,10 @@ impl<'a> DOMStringMapMethods for &'a DOMStringMap {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#domstringmap
|
||||
fn SupportedPropertyNames(self) -> Vec<DOMString> {
|
||||
// FIXME: unimplemented (https://github.com/servo/servo/issues/7273)
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -227,5 +227,30 @@ impl<'a> HTMLCollectionMethods for &'a HTMLCollection {
|
|||
*found = maybe_elem.is_some();
|
||||
maybe_elem
|
||||
}
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#interface-htmlcollection
|
||||
fn SupportedPropertyNames(self) -> Vec<DOMString> {
|
||||
// Step 1
|
||||
let mut result = vec![];
|
||||
|
||||
// Step 2
|
||||
let ref filter = self.collection.1;
|
||||
let root = self.collection.0.root();
|
||||
let elems = HTMLCollection::traverse(root.r()).filter(|element| filter.filter(element.r(), root.r()));
|
||||
for elem in elems {
|
||||
// Step 2.1
|
||||
let id_attr = elem.get_string_attribute(&atom!("id"));
|
||||
if !id_attr.is_empty() && !result.contains(&id_attr) {
|
||||
result.push(id_attr)
|
||||
}
|
||||
// Step 2.2
|
||||
let name_attr = elem.get_string_attribute(&atom!("name"));
|
||||
if !name_attr.is_empty() && !result.contains(&name_attr) && *elem.namespace() == ns!(HTML) {
|
||||
result.push(name_attr)
|
||||
}
|
||||
}
|
||||
|
||||
// Step 3
|
||||
result
|
||||
}
|
||||
}
|
||||
|
|
|
@ -105,5 +105,9 @@ impl<'a> NamedNodeMapMethods for &'a NamedNodeMap {
|
|||
*found = item.is_some();
|
||||
item
|
||||
}
|
||||
}
|
||||
|
||||
fn SupportedPropertyNames(self) -> Vec<DOMString> {
|
||||
// FIXME: unimplemented (https://github.com/servo/servo/issues/7273)
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -134,6 +134,11 @@ impl<'a> StorageMethods for &'a Storage {
|
|||
fn NamedDeleter(self, name: DOMString) {
|
||||
self.RemoveItem(name);
|
||||
}
|
||||
|
||||
fn SupportedPropertyNames(self) -> Vec<DOMString> {
|
||||
// FIXME: unimplemented (https://github.com/servo/servo/issues/7273)
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
trait PrivateStorageHelpers {
|
||||
|
|
|
@ -16,7 +16,8 @@ pub struct TestBindingProxy {
|
|||
}
|
||||
|
||||
impl<'a> TestBindingProxyMethods for &'a TestBindingProxy {
|
||||
|
||||
fn Length(self) -> u32 {0}
|
||||
fn SupportedPropertyNames(self) -> Vec<DOMString> {vec![]}
|
||||
fn GetNamedItem(self, _: DOMString) -> DOMString {"".to_owned()}
|
||||
fn SetNamedItem(self, _: DOMString, _: DOMString) -> () {}
|
||||
fn GetItem(self, _: u32) -> DOMString {"".to_owned()}
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
// web pages.
|
||||
|
||||
interface TestBindingProxy : TestBinding {
|
||||
readonly attribute unsigned long length;
|
||||
|
||||
getter DOMString getNamedItem(DOMString name);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue