]> Cypherpunks.ru repositories - gostls13.git/commitdiff
syscall/js: prepare IDs for the preset objects
authorHajime Hoshi <hajimehoshi@gmail.com>
Wed, 6 May 2020 14:00:58 +0000 (23:00 +0900)
committerHajime Hoshi <hajimehoshi@gmail.com>
Tue, 12 May 2020 15:01:56 +0000 (15:01 +0000)
Fixes #38899

Change-Id: Ib8131c3078c60dc3fe2cf0eaac45b25a4f6e4649
Reviewed-on: https://go-review.googlesource.com/c/go/+/232518
Run-TryBot: Hajime Hoshi <hajimehoshi@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Richard Musiol <neelance@gmail.com>
misc/wasm/wasm_exec.js
src/syscall/js/js_test.go

index a99aaeda071876154af07eb7265341faee6e03ea..8501ae7cd843ac443d46d3ff0f9fb864f5f11f1f 100644 (file)
                        const storeValue = (addr, v) => {
                                const nanHead = 0x7FF80000;
 
-                               if (typeof v === "number") {
+                               if (typeof v === "number" && v !== 0) {
                                        if (isNaN(v)) {
                                                this.mem.setUint32(addr + 4, nanHead, true);
                                                this.mem.setUint32(addr, 0, true);
                                                return;
                                        }
-                                       if (v === 0) {
-                                               this.mem.setUint32(addr + 4, nanHead, true);
-                                               this.mem.setUint32(addr, 1, true);
-                                               return;
-                                       }
                                        this.mem.setFloat64(addr, v, true);
                                        return;
                                }
 
-                               switch (v) {
-                                       case undefined:
-                                               this.mem.setFloat64(addr, 0, true);
-                                               return;
-                                       case null:
-                                               this.mem.setUint32(addr + 4, nanHead, true);
-                                               this.mem.setUint32(addr, 2, true);
-                                               return;
-                                       case true:
-                                               this.mem.setUint32(addr + 4, nanHead, true);
-                                               this.mem.setUint32(addr, 3, true);
-                                               return;
-                                       case false:
-                                               this.mem.setUint32(addr + 4, nanHead, true);
-                                               this.mem.setUint32(addr, 4, true);
-                                               return;
+                               if (v === undefined) {
+                                       this.mem.setFloat64(addr, 0, true);
+                                       return;
                                }
 
                                let id = this._ids.get(v);
                                        this._ids.set(v, id);
                                }
                                this._goRefCounts[id]++;
-                               let typeFlag = 1;
+                               let typeFlag = 0;
                                switch (typeof v) {
+                                       case "object":
+                                               if (v !== null) {
+                                                       typeFlag = 1;
+                                               }
+                                               break;
                                        case "string":
                                                typeFlag = 2;
                                                break;
                                global,
                                this,
                        ];
-                       this._goRefCounts = []; // number of references that Go has to a JS value, indexed by reference id
-                       this._ids = new Map();  // mapping from JS values to reference ids
-                       this._idPool = [];      // unused ids that have been garbage collected
-                       this.exited = false;    // whether the Go program has exited
+                       this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
+                       this._ids = new Map([ // mapping from JS values to reference ids
+                               [0, 1],
+                               [null, 2],
+                               [true, 3],
+                               [false, 4],
+                               [global, 5],
+                               [this, 6],
+                       ]);
+                       this._idPool = [];   // unused ids that have been garbage collected
+                       this.exited = false; // whether the Go program has exited
 
                        // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
                        let offset = 4096;
index fea4c135afc85abec0e2178efb821cfc076f3009..5fc9107d40250ce9da212e5db934a7c1c49da446 100644 (file)
@@ -591,3 +591,14 @@ func BenchmarkDOM(b *testing.B) {
                document.Get("body").Call("removeChild", div)
        }
 }
+
+func TestGlobal(t *testing.T) {
+       ident := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
+               return args[0]
+       })
+       defer ident.Release()
+
+       if got := ident.Invoke(js.Global()); !got.Equal(js.Global()) {
+               t.Errorf("got %#v, want %#v", got, js.Global())
+       }
+}