From 73b5951391a9c206414b2f460a043374b1383b94 Mon Sep 17 00:00:00 2001 From: Richard Musiol Date: Tue, 15 May 2018 00:52:18 +0200 Subject: [PATCH] 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/113076 Reviewed-by: Brad Fitzpatrick --- misc/wasm/wasm_exec.js | 6 +----- src/syscall/js/js_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/misc/wasm/wasm_exec.js b/misc/wasm/wasm_exec.js index f616b5a1a6..d61bbcc95a 100755 --- a/misc/wasm/wasm_exec.js +++ b/misc/wasm/wasm_exec.js @@ -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) => { diff --git a/src/syscall/js/js_test.go b/src/syscall/js/js_test.go index 39e3744a99..ca065e321d 100644 --- a/src/syscall/js/js_test.go +++ b/src/syscall/js/js_test.go @@ -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") -- 2.48.1