mirror of
https://github.com/servo/servo.git
synced 2025-07-24 15:50:21 +01:00
Make generated proxy handlers and DOM object hooks generic (#35292)
* script: Make DOM proxy handlers generic over DOM types. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * script: Make finalize and trace hooks generic over DOM types. Signed-off-by: Josh Matthews <josh@joshmatthews.net> --------- Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
parent
1e164738d8
commit
2bd96633d4
7 changed files with 72 additions and 46 deletions
|
@ -319,7 +319,12 @@ impl DedicatedWorkerGlobalScope {
|
|||
control_receiver,
|
||||
insecure_requests_policy,
|
||||
));
|
||||
unsafe { DedicatedWorkerGlobalScopeBinding::Wrap(SafeJSContext::from_ptr(cx), scope) }
|
||||
unsafe {
|
||||
DedicatedWorkerGlobalScopeBinding::Wrap::<crate::DomTypeHolder>(
|
||||
SafeJSContext::from_ptr(cx),
|
||||
scope,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// <https://html.spec.whatwg.org/multipage/#run-a-worker>
|
||||
|
|
|
@ -74,7 +74,7 @@ impl DissimilarOriginWindow {
|
|||
window_proxy: Dom::from_ref(window_proxy),
|
||||
location: Default::default(),
|
||||
});
|
||||
unsafe { DissimilarOriginWindowBinding::Wrap(cx, win) }
|
||||
unsafe { DissimilarOriginWindowBinding::Wrap::<crate::DomTypeHolder>(cx, win) }
|
||||
}
|
||||
|
||||
pub(crate) fn window_proxy(&self) -> DomRoot<WindowProxy> {
|
||||
|
|
|
@ -120,7 +120,12 @@ impl PaintWorkletGlobalScope {
|
|||
missing_image_urls: Vec::new(),
|
||||
}),
|
||||
});
|
||||
unsafe { PaintWorkletGlobalScopeBinding::Wrap(JSContext::from_ptr(runtime.cx()), global) }
|
||||
unsafe {
|
||||
PaintWorkletGlobalScopeBinding::Wrap::<crate::DomTypeHolder>(
|
||||
JSContext::from_ptr(runtime.cx()),
|
||||
global,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn image_cache(&self) -> Arc<dyn ImageCache> {
|
||||
|
|
|
@ -266,7 +266,12 @@ impl ServiceWorkerGlobalScope {
|
|||
control_receiver,
|
||||
closing,
|
||||
));
|
||||
unsafe { ServiceWorkerGlobalScopeBinding::Wrap(SafeJSContext::from_ptr(cx), scope) }
|
||||
unsafe {
|
||||
ServiceWorkerGlobalScopeBinding::Wrap::<crate::DomTypeHolder>(
|
||||
SafeJSContext::from_ptr(cx),
|
||||
scope,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// <https://w3c.github.io/ServiceWorker/#run-service-worker-algorithm>
|
||||
|
|
|
@ -51,7 +51,12 @@ impl TestWorkletGlobalScope {
|
|||
),
|
||||
lookup_table: Default::default(),
|
||||
});
|
||||
unsafe { TestWorkletGlobalScopeBinding::Wrap(JSContext::from_ptr(runtime.cx()), global) }
|
||||
unsafe {
|
||||
TestWorkletGlobalScopeBinding::Wrap::<crate::DomTypeHolder>(
|
||||
JSContext::from_ptr(runtime.cx()),
|
||||
global,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn perform_a_worklet_task(&self, task: TestWorkletTask) {
|
||||
|
|
|
@ -2874,7 +2874,9 @@ impl Window {
|
|||
theme: Cell::new(PrefersColorScheme::Light),
|
||||
});
|
||||
|
||||
unsafe { WindowBinding::Wrap(JSContext::from_ptr(runtime.cx()), win) }
|
||||
unsafe {
|
||||
WindowBinding::Wrap::<crate::DomTypeHolder>(JSContext::from_ptr(runtime.cx()), win)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn pipeline_id(&self) -> PipelineId {
|
||||
|
|
|
@ -2352,12 +2352,12 @@ class CGDOMJSClass(CGThing):
|
|||
args = {
|
||||
"domClass": DOMClass(self.descriptor),
|
||||
"enumerateHook": "None",
|
||||
"finalizeHook": FINALIZE_HOOK_NAME,
|
||||
"finalizeHook": f"{FINALIZE_HOOK_NAME}::<D>",
|
||||
"flags": "JSCLASS_FOREGROUND_FINALIZE",
|
||||
"name": str_to_cstr_ptr(self.descriptor.interface.identifier.name),
|
||||
"resolveHook": "None",
|
||||
"slots": "1",
|
||||
"traceHook": TRACE_HOOK_NAME,
|
||||
"traceHook": f"{TRACE_HOOK_NAME}::<D>",
|
||||
}
|
||||
if self.descriptor.isGlobal():
|
||||
assert not self.descriptor.weakReferenceable
|
||||
|
@ -2371,7 +2371,7 @@ class CGDOMJSClass(CGThing):
|
|||
return f"""
|
||||
static CLASS_OPS: ThreadUnsafeOnceLock<JSClassOps> = ThreadUnsafeOnceLock::new();
|
||||
|
||||
pub(crate) fn init_class_ops() {{
|
||||
pub(crate) fn init_class_ops<D: DomTypes>() {{
|
||||
CLASS_OPS.set(JSClassOps {{
|
||||
addProperty: None,
|
||||
delProperty: None,
|
||||
|
@ -2388,8 +2388,8 @@ pub(crate) fn init_class_ops() {{
|
|||
|
||||
static Class: ThreadUnsafeOnceLock<DOMJSClass> = ThreadUnsafeOnceLock::new();
|
||||
|
||||
pub(crate) fn init_domjs_class() {{
|
||||
init_class_ops();
|
||||
pub(crate) fn init_domjs_class<D: DomTypes>() {{
|
||||
init_class_ops::<D>();
|
||||
Class.set(DOMJSClass {{
|
||||
base: JSClass {{
|
||||
name: {args['name']},
|
||||
|
@ -3024,7 +3024,8 @@ class CGWrapMethod(CGAbstractMethod):
|
|||
retval = f'DomRoot<{descriptor.concreteType}>'
|
||||
CGAbstractMethod.__init__(self, descriptor, 'Wrap', retval, args,
|
||||
pub=True, unsafe=True,
|
||||
extra_decorators=['#[cfg_attr(crown, allow(crown::unrooted_must_root))]'])
|
||||
extra_decorators=['#[cfg_attr(crown, allow(crown::unrooted_must_root))]'],
|
||||
templateArgs=['D: DomTypes'])
|
||||
|
||||
def definition_body(self):
|
||||
unforgeable = CopyLegacyUnforgeablePropertiesToInstance(self.descriptor)
|
||||
|
@ -3117,7 +3118,8 @@ class CGWrapGlobalMethod(CGAbstractMethod):
|
|||
retval = f'DomRoot<{descriptor.concreteType}>'
|
||||
CGAbstractMethod.__init__(self, descriptor, 'Wrap', retval, args,
|
||||
pub=True, unsafe=True,
|
||||
extra_decorators=['#[cfg_attr(crown, allow(crown::unrooted_must_root))]'])
|
||||
extra_decorators=['#[cfg_attr(crown, allow(crown::unrooted_must_root))]'],
|
||||
templateArgs=['D: DomTypes'])
|
||||
self.properties = properties
|
||||
|
||||
def definition_body(self):
|
||||
|
@ -3139,7 +3141,7 @@ create_global_object(
|
|||
cx,
|
||||
&Class.get().base,
|
||||
raw.as_ptr() as *const libc::c_void,
|
||||
_trace,
|
||||
{TRACE_HOOK_NAME}::<D>,
|
||||
obj.handle_mut(),
|
||||
origin);
|
||||
assert!(!obj.is_null());
|
||||
|
@ -3216,7 +3218,7 @@ impl DomObjectWrap for {name} {{
|
|||
Option<HandleObject>,
|
||||
Box<Self>,
|
||||
CanGc,
|
||||
) -> Root<Dom<Self>> = Wrap;
|
||||
) -> Root<Dom<Self>> = Wrap::<crate::DomTypeHolder>;
|
||||
}}
|
||||
"""
|
||||
|
||||
|
@ -3240,7 +3242,7 @@ impl DomObjectIteratorWrap for {name} {{
|
|||
Option<HandleObject>,
|
||||
Box<IterableIterator<Self>>,
|
||||
CanGc,
|
||||
) -> Root<Dom<IterableIterator<Self>>> = Wrap;
|
||||
) -> Root<Dom<IterableIterator<Self>>> = Wrap::<crate::DomTypeHolder>;
|
||||
}}
|
||||
"""
|
||||
|
||||
|
@ -3703,11 +3705,11 @@ class CGDefineProxyHandler(CGAbstractMethod):
|
|||
customDefineProperty = 'proxyhandler::define_property'
|
||||
if self.descriptor.isMaybeCrossOriginObject() or self.descriptor.operations['IndexedSetter'] or \
|
||||
self.descriptor.operations['NamedSetter']:
|
||||
customDefineProperty = 'defineProperty'
|
||||
customDefineProperty = 'defineProperty::<D>'
|
||||
|
||||
customDelete = 'proxyhandler::delete'
|
||||
if self.descriptor.isMaybeCrossOriginObject() or self.descriptor.operations['NamedDeleter']:
|
||||
customDelete = 'delete'
|
||||
customDelete = 'delete::<D>'
|
||||
|
||||
customGetPrototypeIfOrdinary = 'Some(proxyhandler::get_prototype_if_ordinary)'
|
||||
customGetPrototype = 'None'
|
||||
|
@ -3728,17 +3730,17 @@ class CGDefineProxyHandler(CGAbstractMethod):
|
|||
assert not self.descriptor.operations['NamedGetter']
|
||||
customSet = 'Some(proxyhandler::maybe_cross_origin_set_rawcx)'
|
||||
|
||||
getOwnEnumerablePropertyKeys = "own_property_keys"
|
||||
getOwnEnumerablePropertyKeys = "own_property_keys::<D>"
|
||||
if self.descriptor.interface.getExtendedAttribute("LegacyUnenumerableNamedProperties") or \
|
||||
self.descriptor.isMaybeCrossOriginObject():
|
||||
getOwnEnumerablePropertyKeys = "getOwnEnumerablePropertyKeys"
|
||||
getOwnEnumerablePropertyKeys = "getOwnEnumerablePropertyKeys::<D>"
|
||||
|
||||
return CGGeneric(f"""
|
||||
let traps = ProxyTraps {{
|
||||
enter: None,
|
||||
getOwnPropertyDescriptor: Some(getOwnPropertyDescriptor),
|
||||
getOwnPropertyDescriptor: Some(getOwnPropertyDescriptor::<D>),
|
||||
defineProperty: Some({customDefineProperty}),
|
||||
ownPropertyKeys: Some(own_property_keys),
|
||||
ownPropertyKeys: Some(own_property_keys::<D>),
|
||||
delete_: Some({customDelete}),
|
||||
enumerate: None,
|
||||
getPrototypeIfOrdinary: {customGetPrototypeIfOrdinary},
|
||||
|
@ -3748,11 +3750,11 @@ let traps = ProxyTraps {{
|
|||
preventExtensions: Some(proxyhandler::prevent_extensions),
|
||||
isExtensible: Some(proxyhandler::is_extensible),
|
||||
has: None,
|
||||
get: Some(get),
|
||||
get: Some(get::<D>),
|
||||
set: {customSet},
|
||||
call: None,
|
||||
construct: None,
|
||||
hasOwn: Some(hasOwn),
|
||||
hasOwn: Some(hasOwn::<D>),
|
||||
getOwnEnumerablePropertyKeys: Some({getOwnEnumerablePropertyKeys}),
|
||||
nativeCall: None,
|
||||
objectClassIs: None,
|
||||
|
@ -3760,8 +3762,8 @@ let traps = ProxyTraps {{
|
|||
fun_toString: None,
|
||||
boxedValue_unbox: None,
|
||||
defaultValue: None,
|
||||
trace: Some({TRACE_HOOK_NAME}),
|
||||
finalize: Some({FINALIZE_HOOK_NAME}),
|
||||
trace: Some({TRACE_HOOK_NAME}::<D>),
|
||||
finalize: Some({FINALIZE_HOOK_NAME}::<D>),
|
||||
objectMoved: None,
|
||||
isCallable: None,
|
||||
isConstructor: None,
|
||||
|
@ -5708,7 +5710,7 @@ class CGProxyNamedOperation(CGProxySpecialOperation):
|
|||
# Our first argument is the id we're getting.
|
||||
argName = self.arguments[0].identifier.name
|
||||
return (f'let {argName} = jsid_to_string(*cx, Handle::from_raw(id)).expect("Not a string-convertible JSID?");\n'
|
||||
"let this = UnwrapProxy(proxy);\n"
|
||||
"let this = UnwrapProxy::<D>(proxy);\n"
|
||||
"let this = &*this;\n"
|
||||
f"{CGProxySpecialOperation.define(self)}")
|
||||
|
||||
|
@ -5758,7 +5760,7 @@ class CGProxyNamedDeleter(CGProxyNamedOperation):
|
|||
" return false;\n"
|
||||
" }\n"
|
||||
" };\n"
|
||||
" let this = UnwrapProxy(proxy);\n"
|
||||
" let this = UnwrapProxy::<D>(proxy);\n"
|
||||
" let this = &*this;\n"
|
||||
f" {CGProxySpecialOperation.define(self)}"
|
||||
"}\n")
|
||||
|
@ -5768,14 +5770,15 @@ class CGProxyUnwrap(CGAbstractMethod):
|
|||
def __init__(self, descriptor):
|
||||
args = [Argument('RawHandleObject', 'obj')]
|
||||
CGAbstractMethod.__init__(self, descriptor, "UnwrapProxy",
|
||||
f'*const {descriptor.concreteType}', args,
|
||||
alwaysInline=True, unsafe=True)
|
||||
f'*const D::{descriptor.concreteType}', args,
|
||||
alwaysInline=True, unsafe=True,
|
||||
templateArgs=['D: DomTypes'])
|
||||
|
||||
def definition_body(self):
|
||||
return CGGeneric(f"""
|
||||
let mut slot = UndefinedValue();
|
||||
GetProxyReservedSlot(obj.get(), 0, &mut slot);
|
||||
let box_ = slot.to_private() as *const {self.descriptor.concreteType};
|
||||
let box_ = slot.to_private() as *const D::{self.descriptor.concreteType};
|
||||
return box_;""")
|
||||
|
||||
|
||||
|
@ -5786,7 +5789,7 @@ class CGDOMJSProxyHandler_getOwnPropertyDescriptor(CGAbstractExternMethod):
|
|||
Argument('RawMutableHandle<PropertyDescriptor>', 'mut desc'),
|
||||
Argument('*mut bool', 'is_none')]
|
||||
CGAbstractExternMethod.__init__(self, descriptor, "getOwnPropertyDescriptor",
|
||||
"bool", args)
|
||||
"bool", args, templateArgs=['D: DomTypes'])
|
||||
self.descriptor = descriptor
|
||||
|
||||
# https://heycam.github.io/webidl/#LegacyPlatformObjectGetOwnProperty
|
||||
|
@ -5833,7 +5836,7 @@ class CGDOMJSProxyHandler_getOwnPropertyDescriptor(CGAbstractExternMethod):
|
|||
'pre': 'rooted!(in(*cx) let mut rval = UndefinedValue());'
|
||||
}
|
||||
get += ("if let Some(index) = index {\n"
|
||||
" let this = UnwrapProxy(proxy);\n"
|
||||
" let this = UnwrapProxy::<D>(proxy);\n"
|
||||
" let this = &*this;\n"
|
||||
f"{CGIndenter(CGProxyIndexedGetter(self.descriptor, templateValues)).define()}\n"
|
||||
"}\n")
|
||||
|
@ -5911,7 +5914,7 @@ class CGDOMJSProxyHandler_defineProperty(CGAbstractExternMethod):
|
|||
Argument('RawHandleId', 'id'),
|
||||
Argument('RawHandle<PropertyDescriptor>', 'desc'),
|
||||
Argument('*mut ObjectOpResult', 'opresult')]
|
||||
CGAbstractExternMethod.__init__(self, descriptor, "defineProperty", "bool", args)
|
||||
CGAbstractExternMethod.__init__(self, descriptor, "defineProperty", "bool", args, templateArgs=['D: DomTypes'])
|
||||
self.descriptor = descriptor
|
||||
|
||||
def getBody(self):
|
||||
|
@ -5932,7 +5935,7 @@ class CGDOMJSProxyHandler_defineProperty(CGAbstractExternMethod):
|
|||
if indexedSetter:
|
||||
set += ("let index = get_array_index_from_id(*cx, Handle::from_raw(id));\n"
|
||||
"if let Some(index) = index {\n"
|
||||
" let this = UnwrapProxy(proxy);\n"
|
||||
" let this = UnwrapProxy::<D>(proxy);\n"
|
||||
" let this = &*this;\n"
|
||||
f"{CGIndenter(CGProxyIndexedSetter(self.descriptor)).define()}"
|
||||
" return (*opresult).succeed();\n"
|
||||
|
@ -5970,7 +5973,7 @@ class CGDOMJSProxyHandler_delete(CGAbstractExternMethod):
|
|||
args = [Argument('*mut JSContext', 'cx'), Argument('RawHandleObject', 'proxy'),
|
||||
Argument('RawHandleId', 'id'),
|
||||
Argument('*mut ObjectOpResult', 'res')]
|
||||
CGAbstractExternMethod.__init__(self, descriptor, "delete", "bool", args)
|
||||
CGAbstractExternMethod.__init__(self, descriptor, "delete", "bool", args, templateArgs=['D: DomTypes'])
|
||||
self.descriptor = descriptor
|
||||
|
||||
def getBody(self):
|
||||
|
@ -6004,14 +6007,15 @@ class CGDOMJSProxyHandler_ownPropertyKeys(CGAbstractExternMethod):
|
|||
args = [Argument('*mut JSContext', 'cx'),
|
||||
Argument('RawHandleObject', 'proxy'),
|
||||
Argument('RawMutableHandleIdVector', 'props')]
|
||||
CGAbstractExternMethod.__init__(self, descriptor, "own_property_keys", "bool", args)
|
||||
CGAbstractExternMethod.__init__(self, descriptor, "own_property_keys", "bool", args,
|
||||
templateArgs=['D: DomTypes'])
|
||||
self.descriptor = descriptor
|
||||
|
||||
def getBody(self):
|
||||
body = dedent(
|
||||
"""
|
||||
let cx = SafeJSContext::from_ptr(cx);
|
||||
let unwrapped_proxy = UnwrapProxy(proxy);
|
||||
let unwrapped_proxy = UnwrapProxy::<D>(proxy);
|
||||
""")
|
||||
|
||||
if self.descriptor.isMaybeCrossOriginObject():
|
||||
|
@ -6077,14 +6081,14 @@ class CGDOMJSProxyHandler_getOwnEnumerablePropertyKeys(CGAbstractExternMethod):
|
|||
Argument('RawHandleObject', 'proxy'),
|
||||
Argument('RawMutableHandleIdVector', 'props')]
|
||||
CGAbstractExternMethod.__init__(self, descriptor,
|
||||
"getOwnEnumerablePropertyKeys", "bool", args)
|
||||
"getOwnEnumerablePropertyKeys", "bool", args, templateArgs=['D: DomTypes'])
|
||||
self.descriptor = descriptor
|
||||
|
||||
def getBody(self):
|
||||
body = dedent(
|
||||
"""
|
||||
let cx = SafeJSContext::from_ptr(cx);
|
||||
let unwrapped_proxy = UnwrapProxy(proxy);
|
||||
let unwrapped_proxy = UnwrapProxy::<D>(proxy);
|
||||
""")
|
||||
|
||||
if self.descriptor.isMaybeCrossOriginObject():
|
||||
|
@ -6131,7 +6135,7 @@ class CGDOMJSProxyHandler_hasOwn(CGAbstractExternMethod):
|
|||
def __init__(self, descriptor):
|
||||
args = [Argument('*mut JSContext', 'cx'), Argument('RawHandleObject', 'proxy'),
|
||||
Argument('RawHandleId', 'id'), Argument('*mut bool', 'bp')]
|
||||
CGAbstractExternMethod.__init__(self, descriptor, "hasOwn", "bool", args)
|
||||
CGAbstractExternMethod.__init__(self, descriptor, "hasOwn", "bool", args, templateArgs=['D: DomTypes'])
|
||||
self.descriptor = descriptor
|
||||
|
||||
def getBody(self):
|
||||
|
@ -6154,7 +6158,7 @@ class CGDOMJSProxyHandler_hasOwn(CGAbstractExternMethod):
|
|||
if indexedGetter:
|
||||
indexed += ("let index = get_array_index_from_id(*cx, Handle::from_raw(id));\n"
|
||||
"if let Some(index) = index {\n"
|
||||
" let this = UnwrapProxy(proxy);\n"
|
||||
" let this = UnwrapProxy::<D>(proxy);\n"
|
||||
" let this = &*this;\n"
|
||||
f"{CGIndenter(CGProxyIndexedGetter(self.descriptor)).define()}\n"
|
||||
" *bp = result.is_some();\n"
|
||||
|
@ -6206,7 +6210,7 @@ class CGDOMJSProxyHandler_get(CGAbstractExternMethod):
|
|||
args = [Argument('*mut JSContext', 'cx'), Argument('RawHandleObject', 'proxy'),
|
||||
Argument('RawHandleValue', 'receiver'), Argument('RawHandleId', 'id'),
|
||||
Argument('RawMutableHandleValue', 'vp')]
|
||||
CGAbstractExternMethod.__init__(self, descriptor, "get", "bool", args)
|
||||
CGAbstractExternMethod.__init__(self, descriptor, "get", "bool", args, templateArgs=['D: DomTypes'])
|
||||
self.descriptor = descriptor
|
||||
|
||||
# https://heycam.github.io/webidl/#LegacyPlatformObjectGetOwnProperty
|
||||
|
@ -6246,7 +6250,7 @@ if !expando.is_null() {
|
|||
if indexedGetter:
|
||||
getIndexedOrExpando = ("let index = get_array_index_from_id(*cx, id_lt);\n"
|
||||
"if let Some(index) = index {\n"
|
||||
" let this = UnwrapProxy(proxy);\n"
|
||||
" let this = UnwrapProxy::<D>(proxy);\n"
|
||||
" let this = &*this;\n"
|
||||
f"{CGIndenter(CGProxyIndexedGetter(self.descriptor, templateValues)).define()}")
|
||||
trimmedGetFromExpando = stripTrailingWhitespace(getFromExpando.replace('\n', '\n '))
|
||||
|
@ -6341,7 +6345,7 @@ class CGAbstractClassHook(CGAbstractExternMethod):
|
|||
"""
|
||||
def __init__(self, descriptor, name, returnType, args, doesNotPanic=False):
|
||||
CGAbstractExternMethod.__init__(self, descriptor, name, returnType,
|
||||
args)
|
||||
args, templateArgs=['D: DomTypes'])
|
||||
|
||||
def definition_body_prologue(self):
|
||||
return CGGeneric(f"""
|
||||
|
@ -6725,7 +6729,7 @@ class CGInitStatics(CGThing):
|
|||
else ""
|
||||
)
|
||||
nonproxy = (
|
||||
"init_domjs_class();"
|
||||
"init_domjs_class::<D>();"
|
||||
if not descriptor.proxy
|
||||
and descriptor.concrete
|
||||
else ""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue