wasm-bindgen/tests/wasm/closures.js
Alex Crichton 8ba41cce6e Improve codegen for Closure<T>
This commit improves the codegen for `Closure<T>`, primarily for ZST
where the closure doesn't actually capture anything. Previously
`wasm-bindgen` would unconditionally allocate an `Rc` for a fat pointer,
meaning that it would always hit the allocator even when the `Box<T>`
didn't actually contain an allocation. Now the reference count for the
closure is stored on the JS object rather than in Rust.

Some more advanced tests were added along the way to ensure that
functionality didn't regress, and otherwise the calling convention for
`Closure` changed a good deal but should still be the same user-facing.
The primary change was that the reference count reaching zero may cause
JS to need to run the destructor. It simply returns this information in
`Drop for Closure` and otherwise when calling it now also retains a
function pointer that runs the destructor.

Closes #874
2018-09-29 07:00:53 -07:00

100 lines
1.7 KiB
JavaScript

const assert = require('assert');
exports.works_call = a => {
a();
};
exports.works_thread = a => a(2);
let CANNOT_REUSE_CACHE = null;
exports.cannot_reuse_call = a => {
CANNOT_REUSE_CACHE = a;
};
exports.cannot_reuse_call_again = () => {
CANNOT_REUSE_CACHE();
};
exports.long_lived_call1 = a => {
a();
};
exports.long_lived_call2 = a => a(2);
exports.many_arity_call1 = a => {
a();
};
exports.many_arity_call2 = a => {
a(1);
};
exports.many_arity_call3 = a => {
a(1, 2);
};
exports.many_arity_call4 = a => {
a(1, 2, 3);
};
exports.many_arity_call5 = a => {
a(1, 2, 3, 4);
};
exports.many_arity_call6 = a => {
a(1, 2, 3, 4, 5);
};
exports.many_arity_call7 = a => {
a(1, 2, 3, 4, 5, 6);
};
exports.many_arity_call8 = a => {
a(1, 2, 3, 4, 5, 6, 7);
};
let LONG_LIVED_DROPPING_CACHE = null;
exports.long_lived_dropping_cache = a => {
LONG_LIVED_DROPPING_CACHE = a;
};
exports.long_lived_dropping_call = () => {
LONG_LIVED_DROPPING_CACHE();
};
let LONG_FNMUT_RECURSIVE_CACHE = null;
exports.long_fnmut_recursive_cache = a => {
LONG_FNMUT_RECURSIVE_CACHE = a;
};
exports.long_fnmut_recursive_call = () => {
LONG_FNMUT_RECURSIVE_CACHE();
};
exports.fnmut_call = a => {
a();
};
exports.fnmut_thread = a => a(2);
let FNMUT_BAD_F = null;
exports.fnmut_bad_call = a => {
FNMUT_BAD_F = a;
a();
};
exports.fnmut_bad_again = x => {
if (x) {
FNMUT_BAD_F();
}
};
exports.string_arguments_call = a => {
a('foo');
};
exports.string_ret_call = a => {
assert.strictEqual(a('foo'), 'foobar');
};
let DROP_DURING_CALL = null;
exports.drop_during_call_save = f => {
DROP_DURING_CALL = f;
};
exports.drop_during_call_call = () => DROP_DURING_CALL();