Commit 73b59513 authored by Richard Musiol's avatar Richard Musiol Committed by Brad Fitzpatrick

misc/wasm: fix passing large negative integers from JS to Go

This commit addresses a FIXME left in the code of wasm_exec.js to
properly get the upper 32 bit of a JS number to be stored as an
64-bit integer. A bitshift operation is not possible, because in
JavaScript bitshift operations only operate on the lower 32 bits.

Change-Id: I8f627fd604e592682d9d322942a4852db64a7f66
Reviewed-on: https://go-review.googlesource.com/113076Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent db91ee36
......@@ -77,11 +77,7 @@
const setInt64 = (addr, v) => {
mem().setUint32(addr + 0, v, true);
if (v >= 0) {
mem().setUint32(addr + 4, v / 4294967296, true);
} else {
mem().setUint32(addr + 4, -1, true); // FIXME
}
mem().setUint32(addr + 4, Math.floor(v / 4294967296), true);
}
const getInt64 = (addr) => {
......
......@@ -58,6 +58,24 @@ func TestInt(t *testing.T) {
}
}
func TestIntConversion(t *testing.T) {
testIntConversion(t, 0)
testIntConversion(t, 1)
testIntConversion(t, -1)
testIntConversion(t, 1<<20)
testIntConversion(t, -1<<20)
testIntConversion(t, 1<<40)
testIntConversion(t, -1<<40)
testIntConversion(t, 1<<60)
testIntConversion(t, -1<<60)
}
func testIntConversion(t *testing.T, want int) {
if got := js.ValueOf(want).Int(); got != want {
t.Errorf("got %#v, want %#v", got, want)
}
}
func TestFloat(t *testing.T) {
want := 42.123
o := dummys.Get("someFloat")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment