From 28f9b880f6e9f95c61b31b6e0a7ac458cb6a0e0c Mon Sep 17 00:00:00 2001
From: Cherry Zhang <cherryyz@google.com>
Date: Fri, 29 Jun 2018 23:09:34 -0400
Subject: [PATCH] misc/wasm: make sure value ref id is unique

For each Javascript object that returns to Go as a js.Value, we
associate the ref id to it. But if this ref id is copied or
inherited to other object, it would mess up the ref-object
mapping.

In storeValue, make sure the object is indeed the one we are
storing. Otherwise allocate a new ref id.

Fixes #26143.

Change-Id: Ie60bb2f8d1533da1bbe6f46045866515ec2af5a9
Reviewed-on: https://go-review.googlesource.com/121835
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Richard Musiol <neelance@gmail.com>
---
 misc/wasm/wasm_exec.js    | 2 +-
 src/syscall/js/js_test.go | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/misc/wasm/wasm_exec.js b/misc/wasm/wasm_exec.js
index ecb096509fb..3617c498664 100644
--- a/misc/wasm/wasm_exec.js
+++ b/misc/wasm/wasm_exec.js
@@ -141,7 +141,7 @@
 				}
 
 				let ref = v[this._refProp];
-				if (ref === undefined) {
+				if (ref === undefined || this._values[ref] !== v) {
 					ref = this._values.length;
 					this._values.push(v);
 					v[this._refProp] = ref;
diff --git a/src/syscall/js/js_test.go b/src/syscall/js/js_test.go
index 497b9467bbe..c4141c2196e 100644
--- a/src/syscall/js/js_test.go
+++ b/src/syscall/js/js_test.go
@@ -107,6 +107,13 @@ func TestObject(t *testing.T) {
 	if dummys.Get("someArray") != dummys.Get("someArray") {
 		t.Errorf("same value not equal")
 	}
+
+	// An object and its prototype should not be equal.
+	proto := js.Global().Get("Object").Get("prototype")
+	o := js.Global().Call("eval", "new Object()")
+	if proto == o {
+		t.Errorf("object equals to its prototype")
+	}
 }
 
 func TestTypedArrayOf(t *testing.T) {
-- 
GitLab