Support long-lived closures

Docs coming soon!
This commit is contained in:
Alex Crichton
2018-04-05 18:25:22 -07:00
parent 28d6c1bc12
commit f7f0d578e7
6 changed files with 541 additions and 15 deletions

View File

@ -220,6 +220,37 @@ impl<'a> Context<'a> {
return ptr;
}")
});
for i in 0..8 {
let name = format!("__wbindgen_cb_arity{}", i);
bind(&name, &|me| {
me.expose_add_heap_object();
me.function_table_needed = true;
let args = (0..i)
.map(|x| format!("arg{}", x))
.collect::<Vec<_>>()
.join(", ");
format!("function(a, b, c) {{
const cb = function({0}) {{
return this.f(this.a, this.b {1} {0});
}};
cb.a = b;
cb.b = c;
cb.f = wasm.__wbg_function_table.get(a);
let real = cb.bind(cb);
real.original = cb;
return addHeapObject(real);
}}", args, if i == 0 {""} else {","})
});
}
bind("__wbindgen_cb_drop", &|me| {
me.expose_drop_ref();
String::from("function(i) {
let obj = getObject(i).original;
obj.a = obj.b = 0;
dropRef(i);
}")
});
}
self.rewrite_imports(module_name);
@ -1438,6 +1469,10 @@ impl<'a, 'b> SubContext<'a, 'b> {
self.cx.expose_get_object();
format!("getObject(arg{})", i)
}
shared::TYPE_FUNC => {
self.cx.expose_get_object();
format!("getObject(arg{})", i)
}
shared::TYPE_STACK_FUNC0 |
shared::TYPE_STACK_FUNC1 |
shared::TYPE_STACK_FUNC2 |