From 50441f0d2ab415474e668aaae8a368e0eea3f8ed Mon Sep 17 00:00:00 2001 From: Jonathan Hall Date: Sun, 26 Feb 2017 17:44:29 +0100 Subject: [PATCH 1/5] Add a failing test --- js/js_test.go | 14 ++++++++++++++ js/js_test.inc.js | 1 + 2 files changed, 15 insertions(+) create mode 100644 js/js_test.inc.js diff --git a/js/js_test.go b/js/js_test.go index 798d73e50..20e20ad45 100644 --- a/js/js_test.go +++ b/js/js_test.go @@ -558,3 +558,17 @@ func TestSurrogatePairs(t *testing.T) { t.Fail() } } + +func instanceof(x interface{}, y *js.Object) bool { + return js.Global.Call("$instanceof", x, y).Bool() +} + +func TestUint8Array(t *testing.T) { + uint8Array := js.Global.Get("Uint8Array") + if !instanceof([]byte{}, uint8Array) { + t.Errorf("Empty byte array is not externalized as a Uint8Array") + } + if !instanceof([]byte{0x01}, uint8Array) { + t.Errorf("Non-empty byte array is not externalized as a Uint8Array") + } +} diff --git a/js/js_test.inc.js b/js/js_test.inc.js new file mode 100644 index 000000000..3ce471838 --- /dev/null +++ b/js/js_test.inc.js @@ -0,0 +1 @@ +$global.$instanceof = function(x,y) { return x instanceof y }; From 48573b0dc88c1bece13ec7942b25ddd993ecc351 Mon Sep 17 00:00:00 2001 From: Jonathan Hall Date: Sun, 26 Feb 2017 17:13:51 +0100 Subject: [PATCH 2/5] Don't ignore array type for empty arrays of special JS array types --- compiler/prelude/prelude.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/prelude/prelude.go b/compiler/prelude/prelude.go index 9b1c4a6e2..f0e814fed 100644 --- a/compiler/prelude/prelude.go +++ b/compiler/prelude/prelude.go @@ -120,7 +120,7 @@ var $substring = function(str, low, high) { var $sliceToArray = function(slice) { if (slice.$length === 0) { - return []; + return new slice.$array.constructor; } if (slice.$array.constructor !== Array) { return slice.$array.subarray(slice.$offset, slice.$offset + slice.$length); From 7f7a768023f7153fda62c413a0912b97ef6ce7b4 Mon Sep 17 00:00:00 2001 From: Jonathan Hall Date: Sun, 26 Feb 2017 17:27:19 +0100 Subject: [PATCH 3/5] Fix unrelated lint error --- tests/deferblock_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/deferblock_test.go b/tests/deferblock_test.go index 8aa3f72d9..89fe0c9c6 100644 --- a/tests/deferblock_test.go +++ b/tests/deferblock_test.go @@ -26,7 +26,7 @@ func outer(ch chan struct{}, b bool) ([]byte, error) { func TestBlockingInDefer(t *testing.T) { defer func() { if x := recover(); x != nil { - t.Error("run time panic: %v", x) + t.Errorf("run time panic: %v", x) } }() From cb342d9a3bb175d3fc887d5cac97705ba9946463 Mon Sep 17 00:00:00 2001 From: Jonathan Hall Date: Sun, 26 Feb 2017 19:27:50 +0100 Subject: [PATCH 4/5] Remove 0-length array special case --- compiler/prelude/prelude.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/compiler/prelude/prelude.go b/compiler/prelude/prelude.go index f0e814fed..8ee4ea5bf 100644 --- a/compiler/prelude/prelude.go +++ b/compiler/prelude/prelude.go @@ -119,9 +119,6 @@ var $substring = function(str, low, high) { }; var $sliceToArray = function(slice) { - if (slice.$length === 0) { - return new slice.$array.constructor; - } if (slice.$array.constructor !== Array) { return slice.$array.subarray(slice.$offset, slice.$offset + slice.$length); } From 9aeaa8bdcffcd0b665237ee30fe7d8bb96e1b61d Mon Sep 17 00:00:00 2001 From: Jonathan Hall Date: Sun, 26 Feb 2017 19:38:18 +0100 Subject: [PATCH 5/5] Simpler test code --- js/js_test.go | 11 +++++------ js/js_test.inc.js | 1 - 2 files changed, 5 insertions(+), 7 deletions(-) delete mode 100644 js/js_test.inc.js diff --git a/js/js_test.go b/js/js_test.go index 20e20ad45..f96cac143 100644 --- a/js/js_test.go +++ b/js/js_test.go @@ -43,6 +43,9 @@ var dummys = js.Global.Call("eval", `({ call: function(f, a) { f(a); }, + return: function(x) { + return x; + }, })`) func TestBool(t *testing.T) { @@ -559,16 +562,12 @@ func TestSurrogatePairs(t *testing.T) { } } -func instanceof(x interface{}, y *js.Object) bool { - return js.Global.Call("$instanceof", x, y).Bool() -} - func TestUint8Array(t *testing.T) { uint8Array := js.Global.Get("Uint8Array") - if !instanceof([]byte{}, uint8Array) { + if dummys.Call("return", []byte{}).Get("constructor") != uint8Array { t.Errorf("Empty byte array is not externalized as a Uint8Array") } - if !instanceof([]byte{0x01}, uint8Array) { + if dummys.Call("return", []byte{0x01}).Get("constructor") != uint8Array { t.Errorf("Non-empty byte array is not externalized as a Uint8Array") } } diff --git a/js/js_test.inc.js b/js/js_test.inc.js deleted file mode 100644 index 3ce471838..000000000 --- a/js/js_test.inc.js +++ /dev/null @@ -1 +0,0 @@ -$global.$instanceof = function(x,y) { return x instanceof y };