]> Cypherpunks repositories - gostls13.git/commitdiff
misc/wasm: fix passing large negative integers from JS to Go
authorRichard Musiol <mail@richard-musiol.de>
Mon, 14 May 2018 22:52:18 +0000 (00:52 +0200)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 15 May 2018 14:15:44 +0000 (14:15 +0000)
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 <bradfitz@golang.org>
misc/wasm/wasm_exec.js
src/syscall/js/js_test.go

index f616b5a1a6faa47a98fa39a854ae7fbd70c9c6ba..d61bbcc95a45adb61f04a4f1566009f4f9b0f499 100755 (executable)
 
        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) => {
index 39e3744a995ae03a4e1f714dc85b0ffa956f1b11..ca065e321d33c2d6ef0f9a681c373fdc4660b3b3 100644 (file)
@@ -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")