-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
e3aabcb
commit f4f6061
Showing
5 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,37 @@ | ||
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters