syscall/js: make zero js.Value represent "undefined"

This commit changes the encoding of js.Value so that the zero js.Value
represents the JavaScript value "undefined". This is what users
intuitively expect.

Specifically, the encodings of "undefined" and the number zero have
been swapped.

Fixes #27592.

Change-Id: Icfc832c8cdf7a8a78bd69d20e00a04dbed0ccd10
Reviewed-on: https://go-review.googlesource.com/c/143137
Run-TryBot: Richard Musiol <neelance@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Richard Musiol 2018-10-18 15:53:38 +02:00 committed by Richard Musiol
parent 8ccafb1ac7
commit 138bfc2809
3 changed files with 42 additions and 10 deletions

View file

@ -22,6 +22,7 @@ var dummys = js.Global().Call("eval", `({
add: function(a, b) {
return a + b;
},
zero: 0,
NaN: NaN,
})`)
@ -74,6 +75,9 @@ func TestInt(t *testing.T) {
if dummys.Get("someInt") != dummys.Get("someInt") {
t.Errorf("same value not equal")
}
if got := dummys.Get("zero").Int(); got != 0 {
t.Errorf("got %#v, want %#v", got, 0)
}
}
func TestIntConversion(t *testing.T) {
@ -237,6 +241,9 @@ func TestType(t *testing.T) {
if got, want := js.ValueOf(true).Type(), js.TypeBoolean; got != want {
t.Errorf("got %s, want %s", got, want)
}
if got, want := js.ValueOf(0).Type(), js.TypeNumber; got != want {
t.Errorf("got %s, want %s", got, want)
}
if got, want := js.ValueOf(42).Type(), js.TypeNumber; got != want {
t.Errorf("got %s, want %s", got, want)
}
@ -269,6 +276,13 @@ func TestValueOf(t *testing.T) {
}
}
func TestZeroValue(t *testing.T) {
var v js.Value
if v != js.Undefined() {
t.Error("zero js.Value is not js.Undefined()")
}
}
func TestCallback(t *testing.T) {
c := make(chan struct{})
cb := js.NewCallback(func(args []js.Value) {