webidl: add support for static methods

This commit is contained in:
R. Andrew Ohana
2018-06-14 19:21:33 -07:00
parent 639ccd53ce
commit fe5cde8636
5 changed files with 186 additions and 88 deletions

View File

@ -49,11 +49,21 @@ pub struct ImportFunction {
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
pub enum ImportFunctionKind {
Method { class: String, ty: syn::Type },
JsConstructor { class: String, ty: syn::Type },
Method {
class: String,
ty: syn::Type,
kind: MethodKind,
},
Normal,
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
pub enum MethodKind {
Normal,
Constructor,
Static,
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
pub struct ImportStatic {
pub vis: syn::Visibility,
@ -398,6 +408,7 @@ impl Program {
ImportFunctionKind::Method {
class: class_name.to_string(),
ty: class.clone(),
kind: MethodKind::Normal,
}
} else if wasm.opts.constructor() {
let class = match wasm.ret {
@ -414,9 +425,10 @@ impl Program {
let class_name = extract_path_ident(class_name)
.expect("first argument of method must be a bare type");
ImportFunctionKind::JsConstructor {
ImportFunctionKind::Method {
class: class_name.to_string(),
ty: class.clone(),
kind: MethodKind::Constructor,
}
} else {
ImportFunctionKind::Normal
@ -426,7 +438,6 @@ impl Program {
let ns = match kind {
ImportFunctionKind::Normal => "n",
ImportFunctionKind::Method { ref class, .. } => class,
ImportFunctionKind::JsConstructor { ref class, .. } => class,
};
format!("__wbg_f_{}_{}_{}", js_name, f.ident, ns)
};
@ -694,12 +705,16 @@ impl ImportFunction {
let mut js_new = false;
let mut class_name = None;
match self.kind {
ImportFunctionKind::Method { ref class, .. } => {
method = true;
class_name = Some(class);
}
ImportFunctionKind::JsConstructor { ref class, .. } => {
js_new = true;
ImportFunctionKind::Method {
ref class,
ref kind,
..
} => {
match kind {
MethodKind::Normal => method = true,
MethodKind::Constructor => js_new = true,
MethodKind::Static => {}
}
class_name = Some(class);
}
ImportFunctionKind::Normal => {}

View File

@ -552,11 +552,12 @@ impl ToTokens for ast::ImportFunction {
let mut class_ty = None;
let mut is_method = false;
match self.kind {
ast::ImportFunctionKind::Method { ref ty, .. } => {
is_method = true;
class_ty = Some(ty);
}
ast::ImportFunctionKind::JsConstructor { ref ty, .. } => {
ast::ImportFunctionKind::Method {
ref ty, ref kind, ..
} => {
if let ast::MethodKind::Normal = kind {
is_method = true;
}
class_ty = Some(ty);
}
ast::ImportFunctionKind::Normal => {}