Skip to content

Commit

Permalink
webidl: Convert [Unforgeable] attributes into `#[wasm_bindgen(structu…
Browse files Browse the repository at this point in the history
…ral)]`

Fixes rustwasm#432
  • Loading branch information
fitzgen committed Jul 9, 2018
1 parent 3266ced commit 16ea94c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 26 deletions.
13 changes: 9 additions & 4 deletions crates/webidl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ impl<'a> WebidlParse<&'a webidl::ast::NonPartialInterface> for webidl::ast::Exte
.map(|arg| (&*arg.name, &*arg.type_, arg.variadic)),
Some(self_ty),
kind,
false,
).map(|function| {
program.imports.push(backend::ast::Import {
module: None,
Expand Down Expand Up @@ -320,12 +321,14 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::RegularAttribute {
return Ok(());
}

create_getter(&self.name, &self.type_, self_name, false)
let is_structural = util::is_structural(&self.extended_attributes);

create_getter(&self.name, &self.type_, self_name, false, is_structural)
.map(wrap_import_function)
.map(|import| program.imports.push(import));

if !self.read_only {
create_setter(&self.name, &self.type_, self_name, false)
create_setter(&self.name, &self.type_, self_name, false, is_structural)
.map(wrap_import_function)
.map(|import| program.imports.push(import));
}
Expand All @@ -340,12 +343,14 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::StaticAttribute {
return Ok(());
}

create_getter(&self.name, &self.type_, self_name, true)
let is_structural = util::is_structural(&self.extended_attributes);

create_getter(&self.name, &self.type_, self_name, true, is_structural)
.map(wrap_import_function)
.map(|import| program.imports.push(import));

if !self.read_only {
create_setter(&self.name, &self.type_, self_name, true)
create_setter(&self.name, &self.type_, self_name, true, is_structural)
.map(wrap_import_function)
.map(|import| program.imports.push(import));
}
Expand Down
44 changes: 22 additions & 22 deletions crates/webidl/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub fn create_function<'a, I>(
arguments: I,
ret: Option<syn::Type>,
kind: backend::ast::ImportFunctionKind,
structural: bool,
) -> Option<backend::ast::ImportFunction>
where
I: Iterator<Item = (&'a str, &'a webidl::ast::Type, bool)>,
Expand Down Expand Up @@ -184,7 +185,7 @@ where
rust_name,
js_ret,
catch: false,
structural: false,
structural,
kind,
shim,
})
Expand Down Expand Up @@ -233,6 +234,7 @@ pub fn create_basic_method(
.map(|arg| (&*arg.name, &*arg.type_, arg.variadic)),
ret,
kind,
false,
)
}

Expand All @@ -241,6 +243,7 @@ pub fn create_getter(
ty: &webidl::ast::Type,
self_name: &str,
is_static: bool,
is_structural: bool,
) -> Option<backend::ast::ImportFunction> {
let ret = match webidl_ty_to_syn_ty(ty, TypePosition::Return) {
None => {
Expand All @@ -259,14 +262,15 @@ pub fn create_getter(
}),
};

create_function(name, iter::empty(), ret, kind)
create_function(name, iter::empty(), ret, kind, is_structural)
}

pub fn create_setter(
name: &str,
ty: &webidl::ast::Type,
self_name: &str,
is_static: bool,
is_structural: bool,
) -> Option<backend::ast::ImportFunction> {
let kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
Expand All @@ -282,36 +286,32 @@ pub fn create_setter(
iter::once((name, ty, false)),
None,
kind,
is_structural,
)
}

/// ChromeOnly is for things that are only exposed to priveleged code in Firefox.
pub fn is_chrome_only(ext_attrs: &[Box<ExtendedAttribute>]) -> bool {
ext_attrs.iter().any(|external_attribute| {
return match &**external_attribute {
ExtendedAttribute::ArgumentList(al) => {
println!("ArgumentList");
al.name == "ChromeOnly"
}
ExtendedAttribute::Identifier(i) => {
println!("Identifier");
i.lhs == "ChromeOnly"
}
ExtendedAttribute::IdentifierList(il) => {
println!("IdentifierList");
il.lhs == "ChromeOnly"
}
ExtendedAttribute::NamedArgumentList(nal) => {
println!("NamedArgumentList");
nal.lhs_name == "ChromeOnly"
}
ExtendedAttribute::ArgumentList(al) => al.name == "ChromeOnly",
ExtendedAttribute::Identifier(i) => i.lhs == "ChromeOnly",
ExtendedAttribute::IdentifierList(il) => il.lhs == "ChromeOnly",
ExtendedAttribute::NamedArgumentList(nal) => nal.lhs_name == "ChromeOnly",
ExtendedAttribute::NoArguments(webidl::ast::Other::Identifier(name)) => {
name == "ChromeOnly"
}
ExtendedAttribute::NoArguments(_na) => {
println!("NoArguments");
false
}
ExtendedAttribute::NoArguments(_na) => false,
};
})
}

pub fn is_structural(attrs: &[Box<ExtendedAttribute>]) -> bool {
attrs.iter().any(|attr| {
if let ExtendedAttribute::NoArguments(webidl::ast::Other::Identifier(ref name)) = **attr {
name == "Unforgeable"
} else {
false
}
})
}
46 changes: 46 additions & 0 deletions tests/all/webidl/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,49 @@ fn one_method_using_an_undefined_import_doesnt_break_all_other_methods() {
)
.test();
}

#[test]
fn unforgeable_is_structural() {
project()
.file(
"foo.webidl",
r#"
[Constructor()]
interface Foo {
[Unforgeable] readonly attribute short uno;
readonly attribute short dos;
};
"#,
)
.file(
"foo.js",
r#"
export class Foo {
constructor() {
this.uno = 1;
}
get dos() {
return 2;
}
}
"#,
)
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod foo;
#[wasm_bindgen]
pub fn test() {
let f = foo::Foo::new();
assert_eq!(f.uno(), 1);
assert_eq!(f.dos(), 2);
}
"#,
)
.test();
}

0 comments on commit 16ea94c

Please sign in to comment.