| 1 |
const wasm = require('wasm-bindgen-test.js'); |
| 2 |
const assert = require('assert'); |
| 3 |
|
| 4 |
// NB: `wasm-pack` uses the presence of checks for moved values as a way to test |
| 5 |
// whether it is correctly enabling `--debug` when configured to do so, so don't |
| 6 |
// change this expected debug output without also updating `wasm-pack`'s tests. |
| 7 |
const assertMovedPtrThrows = process.env.WASM_BINDGEN_NO_DEBUG == null |
| 8 |
? f => assert.throws(f, /Attempt to use a moved value/) |
| 9 |
: f => assert.throws(f, /null pointer passed to rust/); |
| 10 |
|
| 11 |
const useMoved = () => { |
| 12 |
const apple = new wasm.Fruit('apple'); |
| 13 |
apple.name(); |
| 14 |
wasm.eat(apple); |
| 15 |
assertMovedPtrThrows(() => apple.name()); |
| 16 |
}; |
| 17 |
|
| 18 |
const moveMoved = () => { |
| 19 |
const pear = new wasm.Fruit('pear'); |
| 20 |
pear.name(); |
| 21 |
wasm.eat(pear); |
| 22 |
assertMovedPtrThrows(() => wasm.eat(pear)); |
| 23 |
}; |
| 24 |
|
| 25 |
const methodMoved = () => { |
| 26 |
const quince = new wasm.Fruit('quince'); |
| 27 |
quince.name(); |
| 28 |
quince.rot(); |
| 29 |
assertMovedPtrThrows(() => quince.rot()); |
| 30 |
}; |
| 31 |
|
| 32 |
exports.js_works = () => { |
| 33 |
useMoved(); |
| 34 |
moveMoved(); |
| 35 |
methodMoved(); |
| 36 |
|
| 37 |
const a = new wasm.Fruit('a'); |
| 38 |
a.prop; |
| 39 |
assertMovedPtrThrows(() => a.prop); |
| 40 |
const b = new wasm.Fruit('a'); |
| 41 |
b.prop = 3; |
| 42 |
assertMovedPtrThrows(() => { b.prop = 4; }); |
| 43 |
}; |