Support by-value self methods (#348)

Refactor slightly to use the same internal support that the other reference
conversions are using.

Closes #329
This commit is contained in:
Alex Crichton
2018-06-28 20:09:11 -05:00
committed by GitHub
parent 9a3ff77ea9
commit e55af85edc
6 changed files with 143 additions and 43 deletions

View File

@ -66,14 +66,22 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
/// Flag this shim as a method call into Rust, so the first Rust argument
/// passed should be `this.ptr`.
pub fn method(&mut self, method: bool) -> &mut Self {
pub fn method(&mut self, method: bool, consumed: bool) -> &mut Self {
if method {
self.prelude(
"if (this.ptr === 0) {
throw new Error('Attempt to use a moved value');
}",
throw new Error('Attempt to use a moved value');
}"
);
self.rust_arguments.insert(0, "this.ptr".to_string());
if consumed {
self.prelude("\
const ptr = this.ptr;\n\
this.ptr = 0;\n\
");
self.rust_arguments.insert(0, "ptr".to_string());
} else {
self.rust_arguments.insert(0, "this.ptr".to_string());
}
}
self
}

View File

@ -551,7 +551,7 @@ impl<'a> Context<'a> {
let set = {
let mut cx = Js2Rust::new(&field.name, self);
cx.method(true).argument(&descriptor)?.ret(&None)?;
cx.method(true, false).argument(&descriptor)?.ret(&None)?;
ts_dst.push_str(&format!(
"{}{}: {}\n",
if field.readonly { "readonly " } else { "" },
@ -561,7 +561,7 @@ impl<'a> Context<'a> {
cx.finish("", &format!("wasm.{}", wasm_setter)).0
};
let (get, _ts) = Js2Rust::new(&field.name, self)
.method(true)
.method(true, false)
.ret(&Some(descriptor))?
.finish("", &format!("wasm.{}", wasm_getter));
if !dst.ends_with("\n") {
@ -1657,7 +1657,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
};
let (js, ts) = Js2Rust::new(&export.function.name, self.cx)
.method(export.method)
.method(export.method, export.consumed)
.process(descriptor.unwrap_function())?
.finish("", &format!("wasm.{}", wasm_name));
let class = self