Merge pull request #879 from alexcrichton/closure-zst

Improve codegen for `Closure<T>`
This commit is contained in:
Alex Crichton
2018-10-01 14:41:13 -07:00
committed by GitHub
7 changed files with 156 additions and 45 deletions

View File

@ -226,26 +226,32 @@ impl ClosureDescriptors {
let (js, _ts, _js_doc) = {
let mut builder = Js2Rust::new("", input);
builder.prelude("this.cnt++;");
if closure.mutable {
builder
.prelude("let a = this.a;\n")
.prelude("this.a = 0;\n")
.rust_argument("a")
.rust_argument("b")
.finally("this.a = a;\n");
} else {
builder.rust_argument("this.a");
builder.rust_argument("this.a")
.rust_argument("b");
}
builder.finally("if (this.cnt-- == 1) d(this.a, b);");
builder
.process(&closure.function)?
.finish("function", "this.f")
.finish("function", "f")
};
input.expose_add_heap_object();
input.function_table_needed = true;
let body = format!(
"function(ptr, f, _ignored) {{
let cb = {};
cb.f = wasm.__wbg_function_table.get(f);
cb.a = ptr;
"function(a, b, fi, di, _ignored) {{
const f = wasm.__wbg_function_table.get(fi);
const d = wasm.__wbg_function_table.get(di);
const cb = {};
cb.a = a;
cb.cnt = 1;
let real = cb.bind(cb);
real.original = cb;
return addHeapObject(real);

View File

@ -327,9 +327,13 @@ impl<'a> Context<'a> {
Ok(String::from(
"
function(i) {
let obj = getObject(i).original;
obj.a = obj.b = 0;
const obj = getObject(i).original;
dropRef(i);
if (obj.cnt-- == 1) {
obj.a = 0;
return 1;
}
return 0;
}
",
))
@ -337,13 +341,7 @@ impl<'a> Context<'a> {
self.bind("__wbindgen_cb_forget", &|me| {
me.expose_drop_ref();
Ok(String::from(
"
function(i) {
dropRef(i);
}
",
))
Ok("dropRef".to_string())
})?;
self.bind("__wbindgen_json_parse", &|me| {

View File

@ -347,6 +347,8 @@ impl Interpreter {
self.descriptor_table_idx = Some(self.stack.pop().unwrap() as u32);
self.stack.pop();
self.stack.pop();
self.stack.pop();
self.stack.pop();
self.stack.push(0);
} else {
self.call(*idx, sections);