mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-16 14:31:22 +00:00
webidl: emit methods for regular operations
This commit is contained in:
@ -20,6 +20,7 @@ use proc_macro2::Ident;
|
||||
use quote::ToTokens;
|
||||
use std::fs;
|
||||
use std::io::{self, Read};
|
||||
use std::iter::FromIterator;
|
||||
use std::path::Path;
|
||||
|
||||
/// Either `Ok(t)` or `Err(failure::Error)`.
|
||||
@ -40,7 +41,7 @@ pub fn parse(webidl_source: &str) -> Result<backend::ast::Program> {
|
||||
let definitions = webidl::parse_string(webidl_source).context("parsing WebIDL source text")?;
|
||||
|
||||
let mut program = backend::ast::Program::default();
|
||||
definitions.webidl_parse(&mut program)?;
|
||||
definitions.webidl_parse(&mut program, ())?;
|
||||
|
||||
Ok(program)
|
||||
}
|
||||
@ -65,23 +66,31 @@ fn compile_ast(ast: &backend::ast::Program) -> String {
|
||||
tokens.to_string()
|
||||
}
|
||||
|
||||
trait WebidlParse {
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program) -> Result<()>;
|
||||
trait WebidlParse<'a> {
|
||||
type Extra;
|
||||
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program, extra: Self::Extra) -> Result<()>;
|
||||
}
|
||||
|
||||
impl WebidlParse for Vec<webidl::ast::Definition> {
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program) -> Result<()> {
|
||||
impl<'a> WebidlParse<'a> for Vec<webidl::ast::Definition> {
|
||||
type Extra = ();
|
||||
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
|
||||
for def in self {
|
||||
def.webidl_parse(program)?;
|
||||
def.webidl_parse(program, ())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl WebidlParse for webidl::ast::Definition {
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program) -> Result<()> {
|
||||
impl<'a> WebidlParse<'a> for webidl::ast::Definition {
|
||||
type Extra = ();
|
||||
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
|
||||
match *self {
|
||||
webidl::ast::Definition::Interface(ref interface) => interface.webidl_parse(program),
|
||||
webidl::ast::Definition::Interface(ref interface) => {
|
||||
interface.webidl_parse(program, ())
|
||||
}
|
||||
// TODO
|
||||
webidl::ast::Definition::Callback(..)
|
||||
| webidl::ast::Definition::Dictionary(..)
|
||||
@ -95,18 +104,24 @@ impl WebidlParse for webidl::ast::Definition {
|
||||
}
|
||||
}
|
||||
|
||||
impl WebidlParse for webidl::ast::Interface {
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program) -> Result<()> {
|
||||
impl<'a> WebidlParse<'a> for webidl::ast::Interface {
|
||||
type Extra = ();
|
||||
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
|
||||
match *self {
|
||||
webidl::ast::Interface::NonPartial(ref interface) => interface.webidl_parse(program),
|
||||
webidl::ast::Interface::NonPartial(ref interface) => {
|
||||
interface.webidl_parse(program, ())
|
||||
}
|
||||
// TODO
|
||||
webidl::ast::Interface::Callback(..) | webidl::ast::Interface::Partial(..) => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl WebidlParse for webidl::ast::NonPartialInterface {
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program) -> Result<()> {
|
||||
impl<'a> WebidlParse<'a> for webidl::ast::NonPartialInterface {
|
||||
type Extra = ();
|
||||
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
|
||||
program.imports.push(backend::ast::Import {
|
||||
module: None,
|
||||
version: None,
|
||||
@ -118,7 +133,234 @@ impl WebidlParse for webidl::ast::NonPartialInterface {
|
||||
name: Ident::new(&self.name, proc_macro2::Span::call_site()),
|
||||
}),
|
||||
});
|
||||
println!("{:#?}", self);
|
||||
|
||||
for member in &self.members {
|
||||
member.webidl_parse(program, &self.name)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> WebidlParse<'a> for webidl::ast::InterfaceMember {
|
||||
type Extra = &'a str;
|
||||
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
|
||||
match *self {
|
||||
webidl::ast::InterfaceMember::Operation(ref op) => op.webidl_parse(program, self_name),
|
||||
// TODO
|
||||
webidl::ast::InterfaceMember::Attribute(_)
|
||||
| webidl::ast::InterfaceMember::Const(_)
|
||||
| webidl::ast::InterfaceMember::Iterable(_)
|
||||
| webidl::ast::InterfaceMember::Maplike(_)
|
||||
| webidl::ast::InterfaceMember::Setlike(_) => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> WebidlParse<'a> for webidl::ast::Operation {
|
||||
type Extra = &'a str;
|
||||
|
||||
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),
|
||||
// TODO
|
||||
webidl::ast::Operation::Special(_)
|
||||
| webidl::ast::Operation::Static(_)
|
||||
| webidl::ast::Operation::Stringifier(_) => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn simple_path_ty<I>(segments: I) -> syn::Type
|
||||
where
|
||||
I: IntoIterator,
|
||||
I::Item: AsRef<str>,
|
||||
{
|
||||
let segments: Vec<_> = segments
|
||||
.into_iter()
|
||||
.map(|s| syn::PathSegment {
|
||||
ident: syn::Ident::new(s.as_ref(), proc_macro2::Span::call_site()),
|
||||
arguments: syn::PathArguments::None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
syn::TypePath {
|
||||
qself: None,
|
||||
path: syn::Path {
|
||||
leading_colon: None,
|
||||
segments: syn::punctuated::Punctuated::from_iter(segments),
|
||||
},
|
||||
}.into()
|
||||
}
|
||||
|
||||
fn shared_ref(ty: syn::Type) -> syn::Type {
|
||||
syn::TypeReference {
|
||||
and_token: Default::default(),
|
||||
lifetime: None,
|
||||
mutability: None,
|
||||
elem: Box::new(ty),
|
||||
}.into()
|
||||
}
|
||||
|
||||
fn webidl_ty_to_syn_ty(ty: &webidl::ast::Type) -> Option<syn::Type> {
|
||||
Some(match ty.kind {
|
||||
// `any` becomes `::wasm_bindgen::JsValue`.
|
||||
webidl::ast::TypeKind::Any => simple_path_ty(&["wasm_bindgen", "JsValue"]),
|
||||
|
||||
// A reference to a type by name becomes the same thing in the
|
||||
// bindings.
|
||||
webidl::ast::TypeKind::Identifier(ref id) => simple_path_ty(Some(id)),
|
||||
|
||||
// Scalars.
|
||||
webidl::ast::TypeKind::Boolean => simple_path_ty(Some("bool")),
|
||||
webidl::ast::TypeKind::Byte => simple_path_ty(Some("i8")),
|
||||
webidl::ast::TypeKind::Octet => simple_path_ty(Some("u8")),
|
||||
webidl::ast::TypeKind::RestrictedDouble | webidl::ast::TypeKind::UnrestrictedDouble => {
|
||||
simple_path_ty(Some("f64"))
|
||||
}
|
||||
webidl::ast::TypeKind::RestrictedFloat | webidl::ast::TypeKind::UnrestrictedFloat => {
|
||||
simple_path_ty(Some("f32"))
|
||||
}
|
||||
webidl::ast::TypeKind::SignedLong => simple_path_ty(Some("i32")),
|
||||
webidl::ast::TypeKind::SignedLongLong => simple_path_ty(Some("i64")),
|
||||
webidl::ast::TypeKind::SignedShort => simple_path_ty(Some("i16")),
|
||||
webidl::ast::TypeKind::UnsignedLong => simple_path_ty(Some("u32")),
|
||||
webidl::ast::TypeKind::UnsignedLongLong => simple_path_ty(Some("u64")),
|
||||
webidl::ast::TypeKind::UnsignedShort => simple_path_ty(Some("u16")),
|
||||
|
||||
// Support for these types is not yet implemented, so skip
|
||||
// generating any bindings for this function.
|
||||
webidl::ast::TypeKind::ArrayBuffer
|
||||
| webidl::ast::TypeKind::ByteString
|
||||
| webidl::ast::TypeKind::DOMString
|
||||
| webidl::ast::TypeKind::DataView
|
||||
| webidl::ast::TypeKind::Error
|
||||
| webidl::ast::TypeKind::Float32Array
|
||||
| webidl::ast::TypeKind::Float64Array
|
||||
| webidl::ast::TypeKind::FrozenArray(_)
|
||||
| webidl::ast::TypeKind::Int16Array
|
||||
| webidl::ast::TypeKind::Int32Array
|
||||
| webidl::ast::TypeKind::Int8Array
|
||||
| webidl::ast::TypeKind::Object
|
||||
| webidl::ast::TypeKind::Promise(_)
|
||||
| webidl::ast::TypeKind::Record(..)
|
||||
| webidl::ast::TypeKind::Sequence(_)
|
||||
| webidl::ast::TypeKind::Symbol
|
||||
| webidl::ast::TypeKind::USVString
|
||||
| webidl::ast::TypeKind::Uint16Array
|
||||
| webidl::ast::TypeKind::Uint32Array
|
||||
| webidl::ast::TypeKind::Uint8Array
|
||||
| webidl::ast::TypeKind::Uint8ClampedArray
|
||||
| webidl::ast::TypeKind::Union(_) => {
|
||||
return None;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn simple_fn_arg(ident: proc_macro2::Ident, ty: syn::Type) -> syn::FnArg {
|
||||
syn::FnArg::Captured(syn::ArgCaptured {
|
||||
pat: syn::Pat::Ident(syn::PatIdent {
|
||||
by_ref: None,
|
||||
mutability: None,
|
||||
ident,
|
||||
subpat: None,
|
||||
}),
|
||||
colon_token: Default::default(),
|
||||
ty,
|
||||
})
|
||||
}
|
||||
|
||||
impl<'a> WebidlParse<'a> for webidl::ast::RegularOperation {
|
||||
type Extra = &'a str;
|
||||
|
||||
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
|
||||
let fn_name = match self.name {
|
||||
None => return Ok(()),
|
||||
Some(ref name) => Ident::new(name, proc_macro2::Span::call_site()),
|
||||
};
|
||||
|
||||
let (output, ret) = match self.return_type {
|
||||
webidl::ast::ReturnType::Void => (syn::ReturnType::Default, None),
|
||||
webidl::ast::ReturnType::NonVoid(ref ty) => match webidl_ty_to_syn_ty(ty) {
|
||||
None => return Ok(()),
|
||||
Some(ty) => (
|
||||
syn::ReturnType::Type(Default::default(), Box::new(ty.clone())),
|
||||
Some(ty),
|
||||
),
|
||||
},
|
||||
};
|
||||
|
||||
let mut inputs = Vec::with_capacity(self.arguments.len() + 1);
|
||||
let mut arguments = Vec::with_capacity(self.arguments.len() + 1);
|
||||
|
||||
let self_ty = simple_path_ty(Some(self_name));
|
||||
let self_ref_ty = shared_ref(self_ty.clone());
|
||||
inputs.push(simple_fn_arg(
|
||||
proc_macro2::Ident::new("self_", proc_macro2::Span::call_site()),
|
||||
self_ref_ty.clone(),
|
||||
));
|
||||
arguments.push(self_ref_ty);
|
||||
|
||||
for arg in &self.arguments {
|
||||
if arg.optional || arg.variadic {
|
||||
// We don't support optional or variadic functions yet; skip
|
||||
// bindings for this this whole function.
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
match webidl_ty_to_syn_ty(&arg.type_) {
|
||||
None => return Ok(()),
|
||||
Some(ty) => {
|
||||
inputs.push(simple_fn_arg(
|
||||
proc_macro2::Ident::new(&arg.name, proc_macro2::Span::call_site()),
|
||||
ty.clone(),
|
||||
));
|
||||
arguments.push(ty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let rust_name = fn_name.clone();
|
||||
let shim = proc_macro2::Ident::new(
|
||||
&format!("__wbg_f_{}_{}_{}", fn_name, fn_name, self_name),
|
||||
proc_macro2::Span::call_site(),
|
||||
);
|
||||
|
||||
program.imports.push(backend::ast::Import {
|
||||
module: None,
|
||||
version: None,
|
||||
js_namespace: None,
|
||||
kind: backend::ast::ImportKind::Function(backend::ast::ImportFunction {
|
||||
function: backend::ast::Function {
|
||||
name: fn_name,
|
||||
arguments,
|
||||
ret,
|
||||
opts: backend::ast::BindgenAttrs {
|
||||
attrs: vec![backend::ast::BindgenAttr::Method],
|
||||
},
|
||||
rust_attrs: vec![],
|
||||
rust_decl: Box::new(syn::FnDecl {
|
||||
fn_token: Default::default(),
|
||||
generics: Default::default(),
|
||||
paren_token: Default::default(),
|
||||
inputs: syn::punctuated::Punctuated::from_iter(inputs),
|
||||
variadic: None,
|
||||
output,
|
||||
}),
|
||||
rust_vis: syn::Visibility::Public(syn::VisPublic {
|
||||
pub_token: Default::default(),
|
||||
}),
|
||||
},
|
||||
rust_name,
|
||||
kind: backend::ast::ImportFunctionKind::Method {
|
||||
class: self_name.to_string(),
|
||||
ty: self_ty,
|
||||
},
|
||||
shim,
|
||||
}),
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user