Skip to content

Commit e4ba33f

Browse files
committed
Implement #[wasm_bindgen(extends = ...)]
This commit implements the `extends` attribute for `#[wasm_bindgen]` to statically draw the inheritance hierarchy in the generated bindings, generating appropriate `AsRef`, `AsMut`, and `From` implementations.
1 parent 7c35c29 commit e4ba33f

File tree

8 files changed

+133
-4
lines changed

8 files changed

+133
-4
lines changed

crates/backend/src/ast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pub struct ImportType {
125125
pub attrs: Vec<syn::Attribute>,
126126
pub doc_comment: Option<String>,
127127
pub instanceof_shim: String,
128+
pub extends: Vec<Ident>,
128129
}
129130

130131
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]

crates/backend/src/codegen.rs

+25
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,31 @@ impl ToTokens for ast::ImportType {
656656
()
657657
};
658658
}).to_tokens(tokens);
659+
660+
for superclass in self.extends.iter() {
661+
(quote! {
662+
impl From<#name> for #superclass {
663+
fn from(obj: #name) -> #superclass {
664+
use wasm_bindgen::JsCast;
665+
#superclass::unchecked_from_js(obj.into())
666+
}
667+
}
668+
669+
impl AsRef<#superclass> for #name {
670+
fn as_ref(&self) -> &#superclass {
671+
use wasm_bindgen::JsCast;
672+
#superclass::unchecked_from_js_ref(self.as_ref())
673+
}
674+
}
675+
676+
impl AsMut<#superclass> for #name {
677+
fn as_mut(&mut self) -> &mut #superclass {
678+
use wasm_bindgen::JsCast;
679+
#superclass::unchecked_from_js_mut(self.as_mut())
680+
}
681+
}
682+
}).to_tokens(tokens);
683+
}
659684
}
660685
}
661686

crates/macro-support/src/parser.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ impl BindgenAttrs {
158158
})
159159
.next()
160160
}
161+
162+
/// Return the list of classes that a type extends
163+
fn extends(&self) -> impl Iterator<Item = &Ident> {
164+
self.attrs
165+
.iter()
166+
.filter_map(|a| match a {
167+
BindgenAttr::Extends(s) => Some(s),
168+
_ => None,
169+
})
170+
}
161171
}
162172

163173
impl syn::synom::Synom for BindgenAttrs {
@@ -190,6 +200,7 @@ pub enum BindgenAttr {
190200
Readonly,
191201
JsName(String),
192202
JsClass(String),
203+
Extends(Ident),
193204
}
194205

195206
impl syn::synom::Synom for BindgenAttr {
@@ -262,6 +273,13 @@ impl syn::synom::Synom for BindgenAttr {
262273
s: syn!(syn::LitStr) >>
263274
(s.value())
264275
)=> { BindgenAttr::JsClass }
276+
|
277+
do_parse!(
278+
call!(term, "extends") >>
279+
punct!(=) >>
280+
ns: call!(term2ident) >>
281+
(ns)
282+
)=> { BindgenAttr::Extends }
265283
));
266284
}
267285

@@ -478,17 +496,18 @@ impl<'a> ConvertToAst<(BindgenAttrs, &'a Option<String>)> for syn::ForeignItemFn
478496
}
479497
}
480498

481-
impl ConvertToAst<()> for syn::ForeignItemType {
499+
impl ConvertToAst<BindgenAttrs> for syn::ForeignItemType {
482500
type Target = ast::ImportKind;
483501

484-
fn convert(self, (): ()) -> Result<Self::Target, Diagnostic> {
502+
fn convert(self, attrs: BindgenAttrs) -> Result<Self::Target, Diagnostic> {
485503
let shim = format!("__wbg_instanceof_{}_{}", self.ident, ShortHash(&self.ident));
486504
Ok(ast::ImportKind::Type(ast::ImportType {
487505
vis: self.vis,
488506
attrs: self.attrs,
489507
doc_comment: None,
490508
instanceof_shim: shim,
491509
name: self.ident,
510+
extends: attrs.extends().cloned().collect(),
492511
}))
493512
}
494513
}
@@ -895,7 +914,7 @@ impl<'a> MacroParse<&'a BindgenAttrs> for syn::ForeignItem {
895914
let js_namespace = item_opts.js_namespace().or(opts.js_namespace()).cloned();
896915
let kind = match self {
897916
syn::ForeignItem::Fn(f) => f.convert((item_opts, &module))?,
898-
syn::ForeignItem::Type(t) => t.convert(())?,
917+
syn::ForeignItem::Type(t) => t.convert(item_opts)?,
899918
syn::ForeignItem::Static(s) => s.convert(item_opts)?,
900919
_ => panic!("only foreign functions/types allowed for now"),
901920
};

crates/webidl/src/first_pass.rs

+28
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use weedle;
1717

1818
use super::Result;
1919
use util;
20+
use util::camel_case_ident;
2021

2122
/// Collection of constructs that may use partial.
2223
#[derive(Default)]
@@ -36,6 +37,7 @@ pub(crate) struct InterfaceData<'src> {
3637
pub(crate) partial: bool,
3738
pub(crate) global: bool,
3839
pub(crate) operations: BTreeMap<OperationId<'src>, OperationData<'src>>,
40+
pub(crate) superclass: Option<&'src str>,
3941
}
4042

4143
#[derive(PartialEq, Eq, PartialOrd, Ord)]
@@ -143,6 +145,7 @@ impl<'src> FirstPass<'src, ()> for weedle::InterfaceDefinition<'src> {
143145
.entry(self.identifier.0)
144146
.or_insert_with(Default::default);
145147
interface.partial = false;
148+
interface.superclass = self.inheritance.map(|s| s.identifier.0);
146149
}
147150

148151
if util::is_chrome_only(&self.attributes) {
@@ -173,6 +176,7 @@ impl<'src> FirstPass<'src, ()> for weedle::PartialInterfaceDefinition<'src> {
173176
partial: true,
174177
operations: Default::default(),
175178
global: false,
179+
superclass: None,
176180
},
177181
);
178182

@@ -295,3 +299,27 @@ impl<'src> FirstPass<'src, ()> for weedle::TypedefDefinition<'src> {
295299
Ok(())
296300
}
297301
}
302+
303+
impl<'a> FirstPassRecord<'a> {
304+
pub fn all_superclasses<'me>(&'me self, interface: &str)
305+
-> impl Iterator<Item = String> + 'me
306+
{
307+
let mut set = BTreeSet::new();
308+
self.fill_superclasses(interface, &mut set);
309+
set.into_iter()
310+
}
311+
312+
fn fill_superclasses(&self, interface: &str, set: &mut BTreeSet<String>) {
313+
let data = match self.interfaces.get(interface) {
314+
Some(data) => data,
315+
None => return,
316+
};
317+
let superclass = match &data.superclass {
318+
Some(class) => class,
319+
None => return,
320+
};
321+
if set.insert(camel_case_ident(superclass)) {
322+
self.fill_superclasses(superclass, set);
323+
}
324+
}
325+
}

crates/webidl/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use backend::defined::{ImportedTypeDefinitions, RemoveUndefinedImports};
3939
use backend::util::{ident_ty, rust_ident, wrap_import_function};
4040
use failure::ResultExt;
4141
use heck::{ShoutySnakeCase};
42+
use proc_macro2::{Ident, Span};
4243
use weedle::argument::Argument;
4344
use weedle::attribute::{ExtendedAttribute, ExtendedAttributeList};
4445

@@ -246,7 +247,10 @@ impl<'src> WebidlParse<'src, ()> for weedle::InterfaceDefinition<'src> {
246247
name: rust_ident(camel_case_ident(self.identifier.0).as_str()),
247248
attrs: Vec::new(),
248249
doc_comment,
249-
instanceof_shim: format!("__widl_instanceof_{}", self.name),
250+
instanceof_shim: format!("__widl_instanceof_{}", self.identifier.0),
251+
extends: first_pass.all_superclasses(self.identifier.0)
252+
.map(|name| Ident::new(&name, Span::call_site()))
253+
.collect(),
250254
}),
251255
});
252256

guide/src/design/import-customization.md

+22
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,25 @@ possibilities!
168168

169169
All of these functions will call `console.log` in JS, but each identifier
170170
will have only one signature in Rust.
171+
172+
* `extends = Bar` - this can be used to say that an imported type extends
173+
another type. This will generate `AsRef`, `AsMut`, and `From` impls for
174+
converting a type into another given that we statically know the inheritance
175+
hierarchy:
176+
177+
```rust
178+
#[wasm_bindgen]
179+
extern {
180+
type Foo;
181+
182+
#[wasm_bindgen(extends = Foo)]
183+
type Bar;
184+
}
185+
186+
let x: &Bar = ...;
187+
let y: &Foo = x.as_ref(); // zero cost cast
188+
```
189+
190+
The `extends = ...` attribute can be specified multiple times for longer
191+
inheritance chains, and `AsRef` and such impls will be generated for each of
192+
the types.

tests/wasm/jscast.js

+10
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@ class JsCast1 {
44
}
55
myval() { return this.val; }
66
}
7+
78
class JsCast2 {
89
}
10+
911
class JsCast3 extends JsCast1 {
1012
constructor() {
1113
super();
1214
this.val = 3;
1315
}
1416
}
1517

18+
class JsCast4 extends JsCast3 {
19+
constructor() {
20+
super();
21+
this.val = 4;
22+
}
23+
}
24+
1625
exports.JsCast1 = JsCast1;
1726
exports.JsCast2 = JsCast2;
1827
exports.JsCast3 = JsCast3;
28+
exports.JsCast4 = JsCast4;

tests/wasm/jscast.rs

+20
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ extern {
1414
#[wasm_bindgen(constructor)]
1515
fn new() -> JsCast2;
1616

17+
#[wasm_bindgen(extends = JsCast1)]
1718
type JsCast3;
1819
#[wasm_bindgen(constructor)]
1920
fn new() -> JsCast3;
21+
22+
#[wasm_bindgen(extends = JsCast1, extends = JsCast3)]
23+
type JsCast4;
24+
#[wasm_bindgen(constructor)]
25+
fn new() -> JsCast4;
2026
}
2127

2228
#[wasm_bindgen_test]
@@ -65,4 +71,18 @@ fn method_calling() {
6571
assert_eq!(a.myval(), 1);
6672
assert_eq!(b.dyn_ref::<JsCast1>().unwrap().myval(), 3);
6773
assert_eq!(b.unchecked_ref::<JsCast1>().myval(), 3);
74+
let c: &JsCast1 = b.as_ref();
75+
assert_eq!(c.myval(), 3);
76+
}
77+
78+
#[wasm_bindgen_test]
79+
fn multiple_layers_of_inheritance() {
80+
let a = JsCast4::new();
81+
assert!(a.is_instance_of::<JsCast4>());
82+
assert!(a.is_instance_of::<JsCast3>());
83+
assert!(a.is_instance_of::<JsCast1>());
84+
85+
let _: &JsCast3 = a.as_ref();
86+
let b: &JsCast1 = a.as_ref();
87+
assert_eq!(b.myval(), 4);
6888
}

0 commit comments

Comments
 (0)