Support Namespace const in webidl (#30492)

* Add TestNS with const

* Implement namespace const in codegen
This commit is contained in:
Samson 2023-10-04 13:29:54 +02:00 committed by GitHub
parent a31e2ea576
commit 8436002383
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 74 additions and 8 deletions

View file

@ -3281,19 +3281,24 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
methods = self.properties.static_methods.variableName()
else:
methods = "&[]"
return CGGeneric("""\
rooted!(in(*cx) let proto = %(proto)s);
if self.descriptor.interface.hasConstants():
constants = "sConstants"
else:
constants = "&[]"
id = MakeNativeName(name)
return CGGeneric(f"""\
rooted!(in(*cx) let proto = {proto});
assert!(!proto.is_null());
rooted!(in(*cx) let mut namespace = ptr::null_mut::<JSObject>());
create_namespace_object(cx, global, proto.handle(), &NAMESPACE_OBJECT_CLASS,
%(methods)s, %(name)s, namespace.handle_mut());
{methods}, {constants}, {str_to_const_array(name)}, namespace.handle_mut());
assert!(!namespace.is_null());
assert!((*cache)[PrototypeList::Constructor::%(id)s as usize].is_null());
(*cache)[PrototypeList::Constructor::%(id)s as usize] = namespace.get();
<*mut JSObject>::post_barrier((*cache).as_mut_ptr().offset(PrototypeList::Constructor::%(id)s as isize),
assert!((*cache)[PrototypeList::Constructor::{id} as usize].is_null());
(*cache)[PrototypeList::Constructor::{id} as usize] = namespace.get();
<*mut JSObject>::post_barrier((*cache).as_mut_ptr().offset(PrototypeList::Constructor::{id} as isize),
ptr::null_mut(),
namespace.get());
""" % {"id": MakeNativeName(name), "methods": methods, "name": str_to_const_array(name), "proto": proto})
""")
if self.descriptor.interface.isCallback():
assert not self.descriptor.interface.ctor() and self.descriptor.interface.hasConstants()
return CGGeneric("""\

View file

@ -9,6 +9,7 @@ use std::ptr;
use js::jsapi::{JSClass, JSFunctionSpec};
use js::rust::{HandleObject, MutableHandleObject};
use super::constant::ConstantSpec;
use crate::dom::bindings::guard::Guard;
use crate::dom::bindings::interface::{create_object, define_on_global_object};
use crate::script_runtime::JSContext;
@ -40,9 +41,10 @@ pub fn create_namespace_object(
proto: HandleObject,
class: &'static NamespaceObjectClass,
methods: &[Guard<&'static [JSFunctionSpec]>],
constants: &[Guard<&'static [ConstantSpec]>],
name: &[u8],
rval: MutableHandleObject,
) {
create_object(cx, global, proto, &class.0, methods, &[], &[], rval);
create_object(cx, global, proto, &class.0, methods, &[], constants, rval);
define_on_global_object(cx, global, name, rval.handle());
}

View file

@ -537,6 +537,7 @@ pub mod testbindingmaplike;
pub mod testbindingpairiterable;
pub mod testbindingproxy;
pub mod testbindingsetlike;
pub mod testns;
pub mod testrunner;
pub mod testworklet;
pub mod testworkletglobalscope;

View file

@ -0,0 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
// check-tidy: no specs after this line
pub struct TestNS(());

View file

@ -599,3 +599,9 @@ partial interface TestBinding {
[Pref="dom.testable_crash.enabled"]
undefined crashHard();
};
[Exposed=(Window,Worker), Pref="dom.testbinding.enabled"]
namespace TestNS {
const unsigned long ONE = 1;
const unsigned long TWO = 0x2;
};

View file

@ -0,0 +1,7 @@
[ns.any.html]
type: testharness
prefs: [dom.testbinding.enabled:true]
[ns.any.worker.html]
type: testharness
prefs: [dom.testbinding.enabled:true]

View file

@ -13639,6 +13639,31 @@
{}
]
],
"ns.any.js": [
"05087872b02abdeeb48c21ee9ec037b3a1483c03",
[
"mozilla/ns.any.html",
{
"script_metadata": [
[
"title",
"Namespace bindings"
]
]
}
],
[
"mozilla/ns.any.worker.html",
{
"script_metadata": [
[
"title",
"Namespace bindings"
]
]
}
]
],
"out-of-order-stylesheet-loads-and-imports.html": [
"d22ae59c689daf77ccda9fa38979413658778dcb",
[

View file

@ -0,0 +1,7 @@
[ns.any.html]
type: testharness
prefs: [dom.testbinding.enabled:true]
[ns.any.worker.html]
type: testharness
prefs: [dom.testbinding.enabled:true]

View file

@ -0,0 +1,6 @@
// META: title=Namespace bindings
test(function () {
assert_equals(TestNS.ONE, 1);
assert_equals(TestNS.TWO, 2);
}, "Namespace constants");