mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-07-21 15:11:58 +00:00
webidl: add support for static methods
This commit is contained in:
@@ -24,12 +24,14 @@ use std::iter;
|
||||
use std::path::Path;
|
||||
|
||||
use failure::ResultExt;
|
||||
use heck::CamelCase;
|
||||
use quote::ToTokens;
|
||||
|
||||
mod util;
|
||||
|
||||
use util::{create_function, ident_ty, raw_ident, rust_ident, webidl_ty_to_syn_ty, TypePosition};
|
||||
use util::{
|
||||
create_basic_method, create_function, ident_ty, raw_ident, rust_ident, webidl_ty_to_syn_ty,
|
||||
wrap_import_function, TypePosition,
|
||||
};
|
||||
|
||||
/// Either `Ok(t)` or `Err(failure::Error)`.
|
||||
pub type Result<T> = ::std::result::Result<T, failure::Error>;
|
||||
@@ -184,9 +186,10 @@ impl<'a> WebidlParse<&'a webidl::ast::NonPartialInterface> for webidl::ast::Exte
|
||||
) -> Result<()> {
|
||||
let mut add_constructor = |arguments: &[webidl::ast::Argument], class: &str| {
|
||||
let self_ty = ident_ty(rust_ident(&interface.name));
|
||||
let kind = backend::ast::ImportFunctionKind::JsConstructor {
|
||||
let kind = backend::ast::ImportFunctionKind::Method {
|
||||
class: class.to_string(),
|
||||
ty: self_ty.clone(),
|
||||
kind: backend::ast::MethodKind::Constructor,
|
||||
};
|
||||
create_function(
|
||||
"new",
|
||||
@@ -216,7 +219,7 @@ impl<'a> WebidlParse<&'a webidl::ast::NonPartialInterface> for webidl::ast::Exte
|
||||
webidl::ast::ExtendedAttribute::NoArguments(webidl::ast::Other::Identifier(name))
|
||||
if name == "Constructor" =>
|
||||
{
|
||||
add_constructor(&[] as &[_], &interface.name);
|
||||
add_constructor(&[], &interface.name);
|
||||
}
|
||||
webidl::ast::ExtendedAttribute::NamedArgumentList(
|
||||
webidl::ast::NamedArgumentListExtendedAttribute {
|
||||
@@ -275,12 +278,11 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::Attribute {
|
||||
|
||||
impl<'a> WebidlParse<&'a str> for webidl::ast::Operation {
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
|
||||
match *self {
|
||||
webidl::ast::Operation::Regular(ref op) => op.webidl_parse(program, self_name),
|
||||
match self {
|
||||
webidl::ast::Operation::Regular(op) => op.webidl_parse(program, self_name),
|
||||
webidl::ast::Operation::Static(op) => op.webidl_parse(program, self_name),
|
||||
// TODO
|
||||
webidl::ast::Operation::Special(_)
|
||||
| webidl::ast::Operation::Static(_)
|
||||
| webidl::ast::Operation::Stringifier(_) => {
|
||||
webidl::ast::Operation::Special(_) | webidl::ast::Operation::Stringifier(_) => {
|
||||
warn!("Unsupported WebIDL operation: {:?}", self);
|
||||
Ok(())
|
||||
}
|
||||
@@ -306,6 +308,7 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::RegularAttribute {
|
||||
let kind = backend::ast::ImportFunctionKind::Method {
|
||||
class: self_name.to_string(),
|
||||
ty: ident_ty(rust_ident(self_name)),
|
||||
kind: backend::ast::MethodKind::Normal,
|
||||
};
|
||||
|
||||
create_function(
|
||||
@@ -316,12 +319,7 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::RegularAttribute {
|
||||
vec![backend::ast::BindgenAttr::Getter(Some(raw_ident(
|
||||
&this.name,
|
||||
)))],
|
||||
).map(|function| backend::ast::Import {
|
||||
module: None,
|
||||
version: None,
|
||||
js_namespace: None,
|
||||
kind: backend::ast::ImportKind::Function(function),
|
||||
})
|
||||
).map(wrap_import_function)
|
||||
}
|
||||
|
||||
fn create_setter(
|
||||
@@ -331,22 +329,18 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::RegularAttribute {
|
||||
let kind = backend::ast::ImportFunctionKind::Method {
|
||||
class: self_name.to_string(),
|
||||
ty: ident_ty(rust_ident(self_name)),
|
||||
kind: backend::ast::MethodKind::Normal,
|
||||
};
|
||||
|
||||
create_function(
|
||||
&format!("set_{}", this.name.to_camel_case()),
|
||||
&format!("set_{}", this.name),
|
||||
iter::once((&*this.name, &*this.type_, false)),
|
||||
kind,
|
||||
None,
|
||||
vec![backend::ast::BindgenAttr::Setter(Some(raw_ident(
|
||||
&this.name,
|
||||
)))],
|
||||
).map(|function| backend::ast::Import {
|
||||
module: None,
|
||||
version: None,
|
||||
js_namespace: None,
|
||||
kind: backend::ast::ImportKind::Function(function),
|
||||
})
|
||||
).map(wrap_import_function)
|
||||
}
|
||||
|
||||
create_getter(self, self_name).map(|import| program.imports.push(import));
|
||||
@@ -361,54 +355,29 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::RegularAttribute {
|
||||
|
||||
impl<'a> WebidlParse<&'a str> for webidl::ast::RegularOperation {
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
|
||||
let name = match self.name {
|
||||
None => {
|
||||
warn!(
|
||||
"Operations without a name are unsupported. Skipping {:?}",
|
||||
self
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
Some(ref name) => name,
|
||||
};
|
||||
|
||||
let kind = backend::ast::ImportFunctionKind::Method {
|
||||
class: self_name.to_string(),
|
||||
ty: ident_ty(rust_ident(self_name)),
|
||||
};
|
||||
|
||||
let ret = match self.return_type {
|
||||
webidl::ast::ReturnType::Void => None,
|
||||
webidl::ast::ReturnType::NonVoid(ref ty) => {
|
||||
match webidl_ty_to_syn_ty(ty, TypePosition::Return) {
|
||||
None => {
|
||||
warn!(
|
||||
"Operation's return type is not yet supported: {:?}. Skipping bindings for {:?}",
|
||||
ty, self
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
Some(ty) => Some(ty),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
create_function(
|
||||
&name,
|
||||
self.arguments
|
||||
.iter()
|
||||
.map(|arg| (&*arg.name, &*arg.type_, arg.variadic)),
|
||||
kind,
|
||||
ret,
|
||||
Vec::new(),
|
||||
).map(|function| {
|
||||
program.imports.push(backend::ast::Import {
|
||||
module: None,
|
||||
version: None,
|
||||
js_namespace: None,
|
||||
kind: backend::ast::ImportKind::Function(function),
|
||||
})
|
||||
});
|
||||
create_basic_method(
|
||||
&self.arguments,
|
||||
self.name.as_ref(),
|
||||
&self.return_type,
|
||||
self_name,
|
||||
backend::ast::MethodKind::Normal,
|
||||
).map(wrap_import_function)
|
||||
.map(|import| program.imports.push(import));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> WebidlParse<&'a str> for webidl::ast::StaticOperation {
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
|
||||
create_basic_method(
|
||||
&self.arguments,
|
||||
self.name.as_ref(),
|
||||
&self.return_type,
|
||||
self_name,
|
||||
backend::ast::MethodKind::Static,
|
||||
).map(wrap_import_function)
|
||||
.map(|import| program.imports.push(import));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user