Allow arbitratry constructor names

This commit is contained in:
konstin 2018-04-14 16:41:41 +02:00 committed by Alex Crichton
parent 32ab5a5644
commit e87b32fb22
4 changed files with 26 additions and 17 deletions

View File

@ -14,7 +14,7 @@ pub struct Export {
pub class: Option<syn::Ident>, pub class: Option<syn::Ident>,
pub method: bool, pub method: bool,
pub mutable: bool, pub mutable: bool,
pub constructor: bool, pub constructor: Option<String>,
pub function: Function, pub function: Function,
} }
@ -123,7 +123,7 @@ impl Program {
class: None, class: None,
method: false, method: false,
mutable: false, mutable: false,
constructor: false, constructor: None,
function: Function::from(f, opts), function: Function::from(f, opts),
}); });
} }
@ -204,12 +204,14 @@ impl Program {
} }
let mut opts = BindgenAttrs::find(&mut method.attrs); let mut opts = BindgenAttrs::find(&mut method.attrs);
let constructor = opts.constructor(); let is_constructor = opts.constructor();
if constructor { let constructor = if is_constructor {
if method.sig.ident != syn::Ident::from("new") { Some(method.sig.ident.to_string())
panic!("The constructor must be called 'new' for now") } else {
} None
};
if is_constructor {
let pos = opts.attrs let pos = opts.attrs
.iter() .iter()
.enumerate() .enumerate()
@ -566,7 +568,7 @@ impl Export {
shared::Export { shared::Export {
class: self.class.map(|s| s.as_ref().to_string()), class: self.class.map(|s| s.as_ref().to_string()),
method: self.method, method: self.method,
constructor: self.constructor, constructor: self.constructor.clone(),
function: self.function.shared(), function: self.function.shared(),
} }
} }

View File

@ -29,7 +29,7 @@ pub struct Context<'a> {
pub struct ExportedClass { pub struct ExportedClass {
pub contents: String, pub contents: String,
pub typescript: String, pub typescript: String,
pub constructor: bool, pub constructor: Option<String>,
} }
pub struct SubContext<'a, 'b: 'a> { pub struct SubContext<'a, 'b: 'a> {
@ -328,14 +328,14 @@ impl<'a> Context<'a> {
}} }}
")); "));
if exports.constructor { if let Some(constructor) = exports.constructor {
ts_dst.push_str(&format!("constructor(...args: [any] | [ConstructorToken]);\n")); ts_dst.push_str(&format!("constructor(...args: [any] | [ConstructorToken]);\n"));
dst.push_str(&format!(" dst.push_str(&format!("
// This invocation of new will call this constructor with a ConstructorToken // This invocation of new will call this constructor with a ConstructorToken
let instance = {class}.new(...args); let instance = {class}.{constructor}(...args);
this.ptr = instance.ptr; this.ptr = instance.ptr;
", class = class)); ", class = class, constructor = constructor));
} else { } else {
ts_dst.push_str(&format!("constructor(...args: [ConstructorToken]);\n")); ts_dst.push_str(&format!("constructor(...args: [ConstructorToken]);\n"));
@ -1342,10 +1342,17 @@ impl<'a, 'b> SubContext<'a, 'b> {
class.typescript.push_str("static "); class.typescript.push_str("static ");
} }
class.constructor = self.program.exports let constructors: Vec<String> = self.program.exports
.iter() .iter()
.filter(|x| x.class == Some(class_name.to_string())) .filter(|x| x.class == Some(class_name.to_string()))
.any(|x| x.constructor); .filter_map(|x| x.constructor.clone())
.collect();
class.constructor = match constructors.len() {
0 => None,
1 => Some(constructors[0].clone()),
x @ _ => panic!("There must be only one constructor, not {}", x),
};
class.contents.push_str(&export.function.name); class.contents.push_str(&export.function.name);
class.contents.push_str(&js); class.contents.push_str(&js);

View File

@ -54,7 +54,7 @@ pub struct ImportType {
pub struct Export { pub struct Export {
pub class: Option<String>, pub class: Option<String>,
pub method: bool, pub method: bool,
pub constructor: bool, pub constructor: Option<String>,
pub function: Function, pub function: Function,
} }

View File

@ -405,7 +405,7 @@ fn constructors() {
#[wasm_bindgen] #[wasm_bindgen]
impl Bar { impl Bar {
#[wasm_bindgen(constructor)] #[wasm_bindgen(constructor)]
pub fn new(number: u32, number2: u32) -> Bar { pub fn other_name(number: u32, number2: u32) -> Bar {
Bar { number, number2 } Bar { number, number2 }
} }
@ -431,7 +431,7 @@ fn constructors() {
assert.strictEqual(bar.get_sum(), 7); assert.strictEqual(bar.get_sum(), 7);
bar.free(); bar.free();
const bar2 = Bar.new(5, 6); const bar2 = Bar.other_name(5, 6);
assert.strictEqual(bar2.get_sum(), 11); assert.strictEqual(bar2.get_sum(), 11);
bar2.free(); bar2.free();
} }