wasm-bindgen/tests/wasm/math.js
Alex Crichton 0160f6af45 Fix handling of u32 between Rust and JS
All numbers in WebAssembly are signed and then each operation on them
may optionally have an unsigned version. This means that when we pass
large signed numbers to JS they actually show up as large negative
numbers even though JS numbers can faithfully represent the type.

This is fixed by adding `>>>0` in a few locations in the generated
bindings to coerce the JS value into an unsigned value.

Closes #1388
2019-03-27 13:37:14 -07:00

38 lines
1.5 KiB
JavaScript

const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');
exports.js_auto_bind_math = () => {
wasm.math(1.0, 2.0);
};
exports.roundtrip = x => x;
exports.test_js_roundtrip = () => {
assert.strictEqual(wasm.rust_roundtrip_i8(0), 0);
assert.strictEqual(wasm.rust_roundtrip_i8(0x80), -128);
assert.strictEqual(wasm.rust_roundtrip_i8(0x7f), 127);
assert.strictEqual(wasm.rust_roundtrip_i16(0), 0);
assert.strictEqual(wasm.rust_roundtrip_i16(0x8000), -32768);
assert.strictEqual(wasm.rust_roundtrip_i16(0x7fff), 32767);
assert.strictEqual(wasm.rust_roundtrip_i32(0), 0);
assert.strictEqual(wasm.rust_roundtrip_i32(0x80000000), -2147483648);
assert.strictEqual(wasm.rust_roundtrip_i32(0x7fffffff), 2147483647);
assert.strictEqual(wasm.rust_roundtrip_u8(0), 0);
assert.strictEqual(wasm.rust_roundtrip_u8(0x80), 128);
assert.strictEqual(wasm.rust_roundtrip_u8(0x7f), 127);
assert.strictEqual(wasm.rust_roundtrip_u8(0xff), 255);
assert.strictEqual(wasm.rust_roundtrip_u16(0), 0);
assert.strictEqual(wasm.rust_roundtrip_u16(0x8000), 32768);
assert.strictEqual(wasm.rust_roundtrip_u16(0x7fff), 32767);
assert.strictEqual(wasm.rust_roundtrip_u16(0xffff), 65535);
assert.strictEqual(wasm.rust_roundtrip_u32(0), 0);
assert.strictEqual(wasm.rust_roundtrip_u32(0x80000000), 2147483648);
assert.strictEqual(wasm.rust_roundtrip_u32(0x7fffffff), 2147483647);
assert.strictEqual(wasm.rust_roundtrip_u32(0xffffffff), 4294967295);
};