Skip to content

Commit 7ebd216

Browse files
committed
js: replaced IsNull() and IsUndefined() by " == nil" and " == js.Undefined" (fixes #145)
1 parent 2561fc0 commit 7ebd216

File tree

10 files changed

+32
-33
lines changed

10 files changed

+32
-33
lines changed

compiler/compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var sizes32 = &types.StdSizes{WordSize: 4, MaxAlign: 8}
1919
var reservedKeywords = make(map[string]bool)
2020

2121
func init() {
22-
for _, keyword := range []string{"abstract", "arguments", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "else", "enum", "eval", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "let", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with", "yield"} {
22+
for _, keyword := range []string{"abstract", "arguments", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "else", "enum", "eval", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "let", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof", "undefined", "var", "void", "volatile", "while", "with", "yield"} {
2323
reservedKeywords[keyword] = true
2424
}
2525
}

compiler/expressions.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
104104
return c.formatExpr(`new ($sliceType(%s.Object))($global.Array.prototype.slice.call(%s, []))`, c.p.pkgVars["github.com/gopherjs/gopherjs/js"], args)
105105
case "Module":
106106
return c.formatExpr("$module")
107+
case "Undefined":
108+
return c.formatExpr("undefined")
107109
}
108110
}
109111

@@ -683,10 +685,6 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
683685
return c.internalize(recv, types.NewInterface(nil, nil))
684686
case "Unsafe":
685687
return recv
686-
case "IsUndefined":
687-
return c.formatParenExpr("%s === undefined", recv)
688-
case "IsNull":
689-
return c.formatParenExpr("%s === null", recv)
690688
default:
691689
panic("Invalid js package object: " + sel.Obj().Name())
692690
}

compiler/natives/os/os.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func runtime_args() []string {
1010
process := js.Global.Get("process")
11-
if process.IsUndefined() {
11+
if process == js.Undefined {
1212
return []string{"browser"}
1313
}
1414
argv := process.Get("argv")

compiler/natives/reflect/reflect.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TypeOf(i interface{}) Type {
100100
return nil
101101
}
102102
c := js.InternalObject(i).Get("constructor")
103-
if c.Get("kind").IsUndefined() { // js.Object
103+
if c.Get("kind") == js.Undefined { // js.Object
104104
return jsObject()
105105
}
106106
return reflectType(c)
@@ -111,7 +111,7 @@ func ValueOf(i interface{}) Value {
111111
return Value{}
112112
}
113113
c := js.InternalObject(i).Get("constructor")
114-
if c.Get("kind").IsUndefined() { // js.Object
114+
if c.Get("kind") == js.Undefined { // js.Object
115115
return Value{jsObject(), unsafe.Pointer(js.InternalObject(i).Unsafe()), flag(Interface)}
116116
}
117117
return makeValue(reflectType(c), js.InternalObject(i).Get("$val"), 0)
@@ -231,11 +231,11 @@ func makemap(t *rtype) (m unsafe.Pointer) {
231231

232232
func mapaccess(t *rtype, m, key unsafe.Pointer) unsafe.Pointer {
233233
k := js.InternalObject(key).Call("$get")
234-
if !k.Get("$key").IsUndefined() {
234+
if k.Get("$key") != js.Undefined {
235235
k = k.Call("$key")
236236
}
237237
entry := js.InternalObject(m).Get(k.Str())
238-
if entry.IsUndefined() {
238+
if entry == js.Undefined {
239239
return nil
240240
}
241241
return unsafe.Pointer(js.Global.Call("$newDataPointer", entry.Get("v"), jsType(PtrTo(t.Elem()))).Unsafe())
@@ -244,7 +244,7 @@ func mapaccess(t *rtype, m, key unsafe.Pointer) unsafe.Pointer {
244244
func mapassign(t *rtype, m, key, val unsafe.Pointer) {
245245
kv := js.InternalObject(key).Call("$get")
246246
k := kv
247-
if !k.Get("$key").IsUndefined() {
247+
if k.Get("$key") != js.Undefined {
248248
k = k.Call("$key")
249249
}
250250
jsVal := js.InternalObject(val).Call("$get")
@@ -262,7 +262,7 @@ func mapassign(t *rtype, m, key, val unsafe.Pointer) {
262262

263263
func mapdelete(t *rtype, m unsafe.Pointer, key unsafe.Pointer) {
264264
k := js.InternalObject(key).Call("$get")
265-
if !k.Get("$key").IsUndefined() {
265+
if k.Get("$key") != js.Undefined {
266266
k = k.Call("$key")
267267
}
268268
js.InternalObject(m).Delete(k.Str())

compiler/natives/runtime/runtime.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ func init() {
3636

3737
func GOROOT() string {
3838
process := js.Global.Get("process")
39-
if process.IsUndefined() {
39+
if process == js.Undefined {
4040
return "/"
4141
}
4242
goroot := process.Get("env").Get("GOROOT")
43-
if !goroot.IsUndefined() {
43+
if goroot != js.Undefined {
4444
return goroot.Str()
4545
}
4646
return defaultGoroot
@@ -52,7 +52,7 @@ func Breakpoint() {
5252

5353
func Caller(skip int) (pc uintptr, file string, line int, ok bool) {
5454
info := js.Global.Get("Error").New().Get("stack").Call("split", "\n").Index(skip + 2)
55-
if info.IsUndefined() {
55+
if info == js.Undefined {
5656
return 0, "", 0, false
5757
}
5858
parts := info.Call("substring", info.Call("indexOf", "(").Int()+1, info.Call("indexOf", ")").Int()).Call("split", ":")
@@ -162,7 +162,7 @@ func SetBlockProfileRate(rate int) {
162162

163163
func Stack(buf []byte, all bool) int {
164164
s := js.Global.Get("Error").New().Get("stack")
165-
if s.IsUndefined() {
165+
if s == js.Undefined {
166166
return 0
167167
}
168168
return copy(buf, s.Call("substr", s.Call("indexOf", "\n").Int()+1).Str())

compiler/natives/syscall/syscall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func printWarning() {
3030

3131
func printToConsole(b []byte) {
3232
goPrintToConsole := js.Global.Get("goPrintToConsole")
33-
if !goPrintToConsole.IsUndefined() {
33+
if goPrintToConsole != js.Undefined {
3434
goPrintToConsole.Invoke(js.InternalObject(b))
3535
return
3636
}

compiler/natives/syscall/syscall_unix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func runtime_envs() []string {
1212
process := js.Global.Get("process")
13-
if process.IsUndefined() {
13+
if process == js.Undefined {
1414
return nil
1515
}
1616
jsEnv := process.Get("env")
@@ -25,7 +25,7 @@ func runtime_envs() []string {
2525

2626
func setenv_c(k, v string) {
2727
process := js.Global.Get("process")
28-
if !process.IsUndefined() {
28+
if process != js.Undefined {
2929
process.Get("env").Set(k, v)
3030
}
3131
}
@@ -45,7 +45,7 @@ func syscall(name string) js.Object {
4545
}
4646
alreadyTriedToLoad = true
4747
require := js.Global.Get("require")
48-
if require.IsUndefined() {
48+
if require == js.Undefined {
4949
panic("")
5050
}
5151
syscallModule = require.Invoke("syscall")

compiler/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ func (c *funcContext) zeroValue(ty types.Type) string {
155155
case *types.Map:
156156
return "false"
157157
case *types.Interface:
158+
if isJsObject(ty) {
159+
return "null"
160+
}
158161
return "$ifaceNil"
159162
}
160163
return fmt.Sprintf("%s.nil", c.typeName(ty))

js/js.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// The second column denotes the types that are used when converting to `interface{}`. An exception are DOM elements, those are kept with the js.Object type. Additionally, a pointer to a named type is passed to JavaScript as an object which has wrappers for the type's exported methods. This can be used to provide getter and setter methods for Go fields to JavaScript.
2424
package js
2525

26-
// Object is a container for a native JavaScript object. Calls to its methods are treated specially by GopherJS and translated directly to their JavaScript syntax.
26+
// Object is a container for a native JavaScript object. Calls to its methods are treated specially by GopherJS and translated directly to their JavaScript syntax. Nil is equal to JavaScript's "null".
2727
type Object interface {
2828

2929
// Get returns the object's property with the given key.
@@ -76,12 +76,6 @@ type Object interface {
7676

7777
// Unsafe returns the object as an uintptr, which can be converted via unsafe.Pointer. Not intended for public use.
7878
Unsafe() uintptr
79-
80-
// IsUndefined returns true if the object is the JavaScript value "undefined".
81-
IsUndefined() bool
82-
83-
// IsNull returns true if the object is the JavaScript value "null".
84-
IsNull() bool
8579
}
8680

8781
// Error encapsulates JavaScript errors. Those are turned into a Go panic and may be rescued, giving an *Error that holds the JavaScript error object.
@@ -111,6 +105,9 @@ var Arguments []Object
111105
// Module gives the value of the "module" variable set by Node.js. Hint: Set a module export with 'js.Module.Get("exports").Set("exportName", ...)'.
112106
var Module Object
113107

108+
// Undefined gives the JavaScript value "undefined".
109+
var Undefined Object
110+
114111
// Debugger gets compiled to JavaScript's "debugger;" statement.
115112
func Debugger() {}
116113

@@ -121,7 +118,7 @@ func InternalObject(i interface{}) Object {
121118

122119
// Keys returns the keys of the given JavaScript object.
123120
func Keys(o Object) []string {
124-
if o.IsUndefined() || o.IsNull() {
121+
if o == Undefined || o == nil {
125122
return nil
126123
}
127124
a := Global.Get("Object").Call("keys", o)

js/js_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,16 @@ func TestFloat(t *testing.T) {
9696
}
9797
}
9898

99-
func TestIsUndefined(t *testing.T) {
100-
if dummys.IsUndefined() || !dummys.Get("xyz").IsUndefined() {
99+
func TestUndefined(t *testing.T) {
100+
if dummys == js.Undefined || dummys.Get("xyz") != js.Undefined {
101101
t.Fail()
102102
}
103103
}
104104

105-
func TestIsNull(t *testing.T) {
105+
func TestNull(t *testing.T) {
106+
var null js.Object
106107
dummys.Set("test", nil)
107-
if dummys.IsNull() || !dummys.Get("test").IsNull() {
108+
if null != nil || dummys == nil || dummys.Get("test") != nil {
108109
t.Fail()
109110
}
110111
}
@@ -292,7 +293,7 @@ func TestError(t *testing.T) {
292293
t.Fail()
293294
}
294295
jsErr, ok := err.(*js.Error)
295-
if !ok || jsErr.Get("stack").IsUndefined() {
296+
if !ok || jsErr.Get("stack") == js.Undefined {
296297
t.Fail()
297298
}
298299
}()

0 commit comments

Comments
 (0)