Skip to content

Commit abaf53f

Browse files
committed
misc/wasm: use single map for string, symbol and object id mapping.
Currently we use a globally unique symbol property on objects that get passed from JavaScript to Go to store a unique ID that Go then uses when referring back to the JavaScript object (via js.Value.ref). This approach fails however when a JavaScript object cannot be modified, i.e. cannot have new properties added or is frozen. The test that is added as part of this commit currently fails with: Cannot add property Symbol(), object is not extensible Instead we consolidate the string, symbol and object unique ID mapping into a single map. Map key equality is determined via strict equality, which is the semantic we want in this situation. Change-Id: Ieb2b50fc36d3c30e148aa7a41557f3c59cd33766 Reviewed-on: https://go-review.googlesource.com/121799 Run-TryBot: Paul Jolly <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Richard Musiol <[email protected]>
1 parent 5d4f047 commit abaf53f

File tree

2 files changed

+12
-28
lines changed

2 files changed

+12
-28
lines changed

misc/wasm/wasm_exec.js

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -118,33 +118,11 @@
118118
return;
119119
}
120120

121-
if (typeof v === "string") {
122-
let ref = this._stringRefs.get(v);
123-
if (ref === undefined) {
124-
ref = this._values.length;
125-
this._values.push(v);
126-
this._stringRefs.set(v, ref);
127-
}
128-
mem().setUint32(addr, ref, true);
129-
return;
130-
}
131-
132-
if (typeof v === "symbol") {
133-
let ref = this._symbolRefs.get(v);
134-
if (ref === undefined) {
135-
ref = this._values.length;
136-
this._values.push(v);
137-
this._symbolRefs.set(v, ref);
138-
}
139-
mem().setUint32(addr, ref, true);
140-
return;
141-
}
142-
143-
let ref = v[this._refProp];
144-
if (ref === undefined || this._values[ref] !== v) {
121+
let ref = this._refs.get(v);
122+
if (ref === undefined) {
145123
ref = this._values.length;
146124
this._values.push(v);
147-
v[this._refProp] = ref;
125+
this._refs.set(v, ref);
148126
}
149127
mem().setUint32(addr, ref, true);
150128
}
@@ -335,9 +313,7 @@
335313
setTimeout(this._resolveCallbackPromise, 0); // make sure it is asynchronous
336314
},
337315
];
338-
this._stringRefs = new Map();
339-
this._symbolRefs = new Map();
340-
this._refProp = Symbol();
316+
this._refs = new Map();
341317
this.exited = false;
342318

343319
const mem = new DataView(this._inst.exports.mem.buffer)

src/syscall/js/js_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ func TestObject(t *testing.T) {
116116
}
117117
}
118118

119+
func TestFrozenObject(t *testing.T) {
120+
o := js.Global().Call("eval", "(function () { let o = new Object(); o.field = 5; Object.freeze(o); return o; })()")
121+
want := 5
122+
if got := o.Get("field").Int(); want != got {
123+
t.Errorf("got %#v, want %#v", got, want)
124+
}
125+
}
126+
119127
func TestTypedArrayOf(t *testing.T) {
120128
testTypedArrayOf(t, "[]int8", []int8{0, -42, 0}, -42)
121129
testTypedArrayOf(t, "[]int16", []int16{0, -42, 0}, -42)

0 commit comments

Comments
 (0)