2018-07-11 11:07:03 -07:00
|
|
|
use std::iter::{self, FromIterator};
|
2018-06-14 02:03:52 -07:00
|
|
|
|
|
|
|
use backend;
|
2018-07-11 11:07:03 -07:00
|
|
|
use backend::util::{ident_ty, leading_colon_path_ty, raw_ident, rust_ident, simple_path_ty};
|
2018-07-10 22:59:59 -07:00
|
|
|
use heck::{CamelCase, SnakeCase};
|
2018-06-25 10:41:33 -07:00
|
|
|
use proc_macro2::Ident;
|
2018-06-14 02:03:52 -07:00
|
|
|
use syn;
|
|
|
|
use webidl;
|
2018-07-02 20:35:05 -07:00
|
|
|
use webidl::ast::ExtendedAttribute;
|
2018-06-14 02:03:52 -07:00
|
|
|
|
2018-07-13 21:46:36 -07:00
|
|
|
use first_pass::FirstPassRecord;
|
|
|
|
|
2018-06-14 02:03:52 -07:00
|
|
|
fn shared_ref(ty: syn::Type) -> syn::Type {
|
|
|
|
syn::TypeReference {
|
|
|
|
and_token: Default::default(),
|
|
|
|
lifetime: None,
|
|
|
|
mutability: None,
|
|
|
|
elem: Box::new(ty),
|
|
|
|
}.into()
|
|
|
|
}
|
|
|
|
|
2018-07-13 06:04:40 +02:00
|
|
|
pub fn webidl_const_ty_to_syn_ty(ty: &webidl::ast::ConstType) -> syn::Type {
|
|
|
|
use webidl::ast::ConstType::*;
|
|
|
|
|
|
|
|
// similar to webidl_ty_to_syn_ty
|
|
|
|
match ty {
|
|
|
|
Boolean => ident_ty(raw_ident("bool")),
|
|
|
|
Byte => ident_ty(raw_ident("i8")),
|
|
|
|
Octet => ident_ty(raw_ident("u8")),
|
|
|
|
RestrictedDouble | UnrestrictedDouble => ident_ty(raw_ident("f64")),
|
|
|
|
RestrictedFloat | UnrestrictedFloat => ident_ty(raw_ident("f32")),
|
|
|
|
SignedLong => ident_ty(raw_ident("i32")),
|
|
|
|
SignedLongLong => ident_ty(raw_ident("i64")),
|
|
|
|
SignedShort => ident_ty(raw_ident("i16")),
|
|
|
|
UnsignedLong => ident_ty(raw_ident("u32")),
|
|
|
|
UnsignedLongLong => ident_ty(raw_ident("u64")),
|
|
|
|
UnsignedShort => ident_ty(raw_ident("u16")),
|
|
|
|
Identifier(ref id) => ident_ty(rust_ident(id)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn webidl_const_v_to_backend_const_v(v: &webidl::ast::ConstValue) -> backend::ast::ConstValue {
|
|
|
|
match *v {
|
|
|
|
webidl::ast::ConstValue::BooleanLiteral(b) => backend::ast::ConstValue::BooleanLiteral(b),
|
|
|
|
webidl::ast::ConstValue::FloatLiteral(f) => backend::ast::ConstValue::FloatLiteral(f),
|
2018-07-14 14:48:04 +02:00
|
|
|
webidl::ast::ConstValue::SignedIntegerLiteral(i) => backend::ast::ConstValue::SignedIntegerLiteral(i),
|
|
|
|
webidl::ast::ConstValue::UnsignedIntegerLiteral(u) => backend::ast::ConstValue::UnsignedIntegerLiteral(u),
|
2018-07-13 06:04:40 +02:00
|
|
|
webidl::ast::ConstValue::Null => backend::ast::ConstValue::Null,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 02:03:52 -07:00
|
|
|
fn simple_fn_arg(ident: Ident, ty: syn::Type) -> syn::ArgCaptured {
|
|
|
|
syn::ArgCaptured {
|
|
|
|
pat: syn::Pat::Ident(syn::PatIdent {
|
|
|
|
by_ref: None,
|
|
|
|
mutability: None,
|
|
|
|
ident,
|
|
|
|
subpat: None,
|
|
|
|
}),
|
|
|
|
colon_token: Default::default(),
|
|
|
|
ty,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 11:07:03 -07:00
|
|
|
fn unit_ty() -> syn::Type {
|
|
|
|
syn::Type::Tuple(syn::TypeTuple {
|
|
|
|
paren_token: Default::default(),
|
|
|
|
elems: syn::punctuated::Punctuated::new(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn result_ty(t: syn::Type) -> syn::Type {
|
|
|
|
let js_value = leading_colon_path_ty(vec![rust_ident("wasm_bindgen"), rust_ident("JsValue")]);
|
|
|
|
|
|
|
|
let arguments = syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
|
|
|
|
colon2_token: None,
|
|
|
|
lt_token: Default::default(),
|
|
|
|
args: FromIterator::from_iter(vec![
|
|
|
|
syn::GenericArgument::Type(t),
|
|
|
|
syn::GenericArgument::Type(js_value),
|
|
|
|
]),
|
|
|
|
gt_token: Default::default(),
|
|
|
|
});
|
|
|
|
|
|
|
|
let ident = raw_ident("Result");
|
|
|
|
let seg = syn::PathSegment { ident, arguments };
|
|
|
|
let path: syn::Path = seg.into();
|
|
|
|
let ty = syn::TypePath { qself: None, path };
|
|
|
|
ty.into()
|
|
|
|
}
|
|
|
|
|
2018-07-10 22:59:59 -07:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum TypePosition {
|
|
|
|
Argument,
|
|
|
|
Return,
|
|
|
|
}
|
2018-06-14 02:03:52 -07:00
|
|
|
|
2018-07-13 21:46:36 -07:00
|
|
|
impl<'a> FirstPassRecord<'a> {
|
2018-07-10 22:59:59 -07:00
|
|
|
pub fn webidl_ty_to_syn_ty(
|
|
|
|
&self,
|
|
|
|
ty: &webidl::ast::Type,
|
|
|
|
pos: TypePosition,
|
|
|
|
) -> Option<syn::Type> {
|
|
|
|
// nullable types are not yet supported (see issue #14)
|
|
|
|
if ty.nullable {
|
2018-06-14 19:21:33 -07:00
|
|
|
return None;
|
|
|
|
}
|
2018-07-10 22:59:59 -07:00
|
|
|
Some(match ty.kind {
|
|
|
|
// `any` becomes `::wasm_bindgen::JsValue`.
|
|
|
|
webidl::ast::TypeKind::Any => {
|
|
|
|
simple_path_ty(vec![rust_ident("wasm_bindgen"), rust_ident("JsValue")])
|
|
|
|
}
|
|
|
|
|
|
|
|
// A reference to a type by name becomes the same thing in the
|
|
|
|
// bindings.
|
|
|
|
webidl::ast::TypeKind::Identifier(ref id) => {
|
|
|
|
let ty = ident_ty(rust_ident(id.to_camel_case().as_str()));
|
|
|
|
if self.interfaces.contains(id) {
|
|
|
|
if pos == TypePosition::Argument {
|
|
|
|
shared_ref(ty)
|
|
|
|
} else {
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
} else if self.dictionaries.contains(id) {
|
|
|
|
ty
|
|
|
|
} else if self.enums.contains(id) {
|
|
|
|
ty
|
|
|
|
} else {
|
|
|
|
warn!("unrecognized type {}", id);
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scalars.
|
|
|
|
webidl::ast::TypeKind::Boolean => ident_ty(raw_ident("bool")),
|
|
|
|
webidl::ast::TypeKind::Byte => ident_ty(raw_ident("i8")),
|
|
|
|
webidl::ast::TypeKind::Octet => ident_ty(raw_ident("u8")),
|
|
|
|
webidl::ast::TypeKind::RestrictedDouble | webidl::ast::TypeKind::UnrestrictedDouble => {
|
|
|
|
ident_ty(raw_ident("f64"))
|
|
|
|
}
|
|
|
|
webidl::ast::TypeKind::RestrictedFloat | webidl::ast::TypeKind::UnrestrictedFloat => {
|
|
|
|
ident_ty(raw_ident("f32"))
|
|
|
|
}
|
|
|
|
webidl::ast::TypeKind::SignedLong => ident_ty(raw_ident("i32")),
|
|
|
|
webidl::ast::TypeKind::SignedLongLong => ident_ty(raw_ident("i64")),
|
|
|
|
webidl::ast::TypeKind::SignedShort => ident_ty(raw_ident("i16")),
|
|
|
|
webidl::ast::TypeKind::UnsignedLong => ident_ty(raw_ident("u32")),
|
|
|
|
webidl::ast::TypeKind::UnsignedLongLong => ident_ty(raw_ident("u64")),
|
|
|
|
webidl::ast::TypeKind::UnsignedShort => ident_ty(raw_ident("u16")),
|
|
|
|
|
|
|
|
// `DOMString -> `&str` for arguments
|
|
|
|
webidl::ast::TypeKind::DOMString if pos == TypePosition::Argument => {
|
|
|
|
shared_ref(ident_ty(raw_ident("str")))
|
|
|
|
}
|
|
|
|
// `DOMString` is not supported yet in other positions.
|
|
|
|
webidl::ast::TypeKind::DOMString => return None,
|
|
|
|
|
|
|
|
// 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::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;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-06-14 19:21:33 -07:00
|
|
|
|
2018-07-10 22:59:59 -07:00
|
|
|
fn webidl_arguments_to_syn_arg_captured<'b, I>(
|
|
|
|
&self,
|
|
|
|
arguments: I,
|
|
|
|
kind: &backend::ast::ImportFunctionKind,
|
|
|
|
) -> Option<Vec<syn::ArgCaptured>>
|
|
|
|
where
|
|
|
|
I: Iterator<Item = (&'b str, &'b webidl::ast::Type, bool)>,
|
|
|
|
{
|
|
|
|
let estimate = arguments.size_hint();
|
|
|
|
let len = estimate.1.unwrap_or(estimate.0);
|
|
|
|
let mut res = if let backend::ast::ImportFunctionKind::Method {
|
|
|
|
ty,
|
|
|
|
kind:
|
|
|
|
backend::ast::MethodKind::Operation(backend::ast::Operation {
|
|
|
|
is_static: false, ..
|
|
|
|
}),
|
|
|
|
..
|
|
|
|
} = kind
|
2018-06-14 19:21:33 -07:00
|
|
|
{
|
2018-07-10 22:59:59 -07:00
|
|
|
let mut res = Vec::with_capacity(len + 1);
|
|
|
|
res.push(simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone())));
|
|
|
|
res
|
|
|
|
} else {
|
|
|
|
Vec::with_capacity(len)
|
|
|
|
};
|
|
|
|
|
|
|
|
for (name, ty, variadic) in arguments {
|
|
|
|
if variadic {
|
|
|
|
warn!("Variadic arguments are not supported yet",);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.webidl_ty_to_syn_ty(ty, TypePosition::Argument) {
|
|
|
|
None => {
|
|
|
|
warn!("Argument's type is not yet supported: {:?}", ty);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(ty) => res.push(simple_fn_arg(rust_ident(&name.to_snake_case()), ty)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_function<'b, I>(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
arguments: I,
|
|
|
|
mut ret: Option<syn::Type>,
|
|
|
|
kind: backend::ast::ImportFunctionKind,
|
|
|
|
structural: bool,
|
|
|
|
catch: bool,
|
|
|
|
) -> Option<backend::ast::ImportFunction>
|
|
|
|
where
|
|
|
|
I: Iterator<Item = (&'b str, &'b webidl::ast::Type, bool)>,
|
|
|
|
{
|
|
|
|
let rust_name = rust_ident(&name.to_snake_case());
|
|
|
|
let name = raw_ident(name);
|
|
|
|
|
|
|
|
let arguments = self.webidl_arguments_to_syn_arg_captured(arguments, &kind)?;
|
|
|
|
|
|
|
|
let js_ret = ret.clone();
|
|
|
|
|
|
|
|
if catch {
|
2018-07-15 12:43:55 -04:00
|
|
|
ret = Some(ret.map_or_else(|| result_ty(unit_ty()), result_ty))
|
2018-07-10 22:59:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let shim = {
|
|
|
|
let ns = match kind {
|
|
|
|
backend::ast::ImportFunctionKind::Normal => "",
|
|
|
|
backend::ast::ImportFunctionKind::Method { ref class, .. } => class,
|
|
|
|
};
|
|
|
|
|
|
|
|
raw_ident(&format!("__widl_f_{}_{}", rust_name, ns))
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(backend::ast::ImportFunction {
|
|
|
|
function: backend::ast::Function {
|
|
|
|
name,
|
|
|
|
arguments,
|
|
|
|
ret,
|
|
|
|
rust_attrs: vec![],
|
|
|
|
rust_vis: public(),
|
|
|
|
},
|
|
|
|
rust_name,
|
|
|
|
js_ret,
|
|
|
|
catch,
|
|
|
|
structural,
|
|
|
|
kind,
|
|
|
|
shim,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_basic_method(
|
|
|
|
&self,
|
|
|
|
arguments: &[webidl::ast::Argument],
|
|
|
|
name: Option<&String>,
|
|
|
|
return_type: &webidl::ast::ReturnType,
|
|
|
|
self_name: &str,
|
|
|
|
is_static: bool,
|
|
|
|
catch: bool,
|
|
|
|
) -> Option<backend::ast::ImportFunction> {
|
|
|
|
let name = match name {
|
2018-06-14 19:21:33 -07:00
|
|
|
None => {
|
2018-07-10 22:59:59 -07:00
|
|
|
warn!("Operations without a name are unsupported");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(ref name) => name,
|
|
|
|
};
|
|
|
|
|
|
|
|
let kind = backend::ast::ImportFunctionKind::Method {
|
|
|
|
class: self_name.to_string(),
|
|
|
|
ty: ident_ty(rust_ident(self_name.to_camel_case().as_str())),
|
|
|
|
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
|
|
|
|
is_static,
|
|
|
|
kind: backend::ast::OperationKind::Regular,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
let ret = match return_type {
|
|
|
|
webidl::ast::ReturnType::Void => None,
|
|
|
|
webidl::ast::ReturnType::NonVoid(ty) => {
|
|
|
|
match self.webidl_ty_to_syn_ty(ty, TypePosition::Return) {
|
|
|
|
None => {
|
|
|
|
warn!("Operation's return type is not yet supported: {:?}", ty);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(ty) => Some(ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.create_function(
|
|
|
|
&name,
|
|
|
|
arguments
|
|
|
|
.iter()
|
|
|
|
.map(|arg| (&*arg.name, &*arg.type_, arg.variadic)),
|
|
|
|
ret,
|
|
|
|
kind,
|
|
|
|
false,
|
|
|
|
catch,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_getter(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
ty: &webidl::ast::Type,
|
|
|
|
self_name: &str,
|
|
|
|
is_static: bool,
|
|
|
|
is_structural: bool,
|
|
|
|
catch: bool,
|
|
|
|
) -> Option<backend::ast::ImportFunction> {
|
|
|
|
let ret = match self.webidl_ty_to_syn_ty(ty, TypePosition::Return) {
|
|
|
|
None => {
|
|
|
|
warn!("Attribute's type does not yet support reading: {:?}", ty);
|
2018-06-14 19:21:33 -07:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(ty) => Some(ty),
|
2018-07-10 22:59:59 -07:00
|
|
|
};
|
2018-06-14 19:21:33 -07:00
|
|
|
|
2018-07-10 22:59:59 -07:00
|
|
|
let kind = backend::ast::ImportFunctionKind::Method {
|
|
|
|
class: self_name.to_string(),
|
|
|
|
ty: ident_ty(rust_ident(self_name.to_camel_case().as_str())),
|
|
|
|
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
|
|
|
|
is_static,
|
|
|
|
kind: backend::ast::OperationKind::Getter(Some(raw_ident(name))),
|
|
|
|
}),
|
|
|
|
};
|
2018-06-14 22:48:32 -07:00
|
|
|
|
2018-07-10 22:59:59 -07:00
|
|
|
self.create_function(name, iter::empty(), ret, kind, is_structural, catch)
|
|
|
|
}
|
2018-06-14 22:48:32 -07:00
|
|
|
|
2018-07-10 22:59:59 -07:00
|
|
|
pub fn create_setter(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
ty: &webidl::ast::Type,
|
|
|
|
self_name: &str,
|
|
|
|
is_static: bool,
|
|
|
|
is_structural: bool,
|
|
|
|
catch: bool,
|
|
|
|
) -> Option<backend::ast::ImportFunction> {
|
|
|
|
let kind = backend::ast::ImportFunctionKind::Method {
|
|
|
|
class: self_name.to_string(),
|
|
|
|
ty: ident_ty(rust_ident(self_name.to_camel_case().as_str())),
|
|
|
|
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
|
|
|
|
is_static,
|
|
|
|
kind: backend::ast::OperationKind::Setter(Some(raw_ident(name))),
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.create_function(
|
|
|
|
&format!("set_{}", name),
|
|
|
|
iter::once((name, ty, false)),
|
|
|
|
None,
|
|
|
|
kind,
|
|
|
|
is_structural,
|
|
|
|
catch,
|
|
|
|
)
|
|
|
|
}
|
2018-06-14 22:48:32 -07:00
|
|
|
}
|
2018-07-02 20:35:05 -07:00
|
|
|
|
2018-07-17 22:23:17 -07:00
|
|
|
fn has_named_attribute(ext_attrs: &[Box<ExtendedAttribute>], attribute: &str) -> bool {
|
2018-07-10 22:59:59 -07:00
|
|
|
ext_attrs.iter().any(|attr| match &**attr {
|
|
|
|
ExtendedAttribute::NoArguments(webidl::ast::Other::Identifier(name)) => {
|
2018-07-17 22:23:17 -07:00
|
|
|
name == attribute
|
2018-07-10 22:59:59 -07:00
|
|
|
}
|
|
|
|
_ => false,
|
2018-07-02 20:35:05 -07:00
|
|
|
})
|
2018-07-07 10:20:31 -07:00
|
|
|
}
|
2018-07-09 16:35:25 -07:00
|
|
|
|
2018-07-17 22:23:17 -07:00
|
|
|
/// ChromeOnly is for things that are only exposed to privileged code in Firefox.
|
|
|
|
pub fn is_chrome_only(ext_attrs: &[Box<ExtendedAttribute>]) -> bool {
|
|
|
|
has_named_attribute(ext_attrs, "ChromeOnly")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_no_interface_object(ext_attrs: &[Box<ExtendedAttribute>]) -> bool {
|
|
|
|
has_named_attribute(ext_attrs, "NoInterfaceObject")
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:35:25 -07:00
|
|
|
pub fn is_structural(attrs: &[Box<ExtendedAttribute>]) -> bool {
|
2018-07-10 22:59:59 -07:00
|
|
|
attrs.iter().any(|attr| match &**attr {
|
|
|
|
ExtendedAttribute::NoArguments(webidl::ast::Other::Identifier(name)) => {
|
2018-07-09 16:35:25 -07:00
|
|
|
name == "Unforgeable"
|
|
|
|
}
|
2018-07-10 22:59:59 -07:00
|
|
|
_ => false,
|
2018-07-09 16:35:25 -07:00
|
|
|
})
|
|
|
|
}
|
2018-07-11 11:07:03 -07:00
|
|
|
|
|
|
|
pub fn throws(attrs: &[Box<ExtendedAttribute>]) -> bool {
|
2018-07-10 22:59:59 -07:00
|
|
|
attrs.iter().any(|attr| match &**attr {
|
|
|
|
ExtendedAttribute::NoArguments(webidl::ast::Other::Identifier(name)) => name == "Throws",
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn public() -> syn::Visibility {
|
|
|
|
syn::Visibility::Public(syn::VisPublic {
|
|
|
|
pub_token: Default::default(),
|
2018-07-11 11:07:03 -07:00
|
|
|
})
|
|
|
|
}
|