Skip to content

Commit

Permalink
Move JS function into .inc.js
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Sep 15, 2015
1 parent ac7b7eb commit 687a38a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
16 changes: 1 addition & 15 deletions jsbuiltin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,7 @@ func IsNaN(value interface{}) bool {
return js.Global.Call("isNaN", value).Bool()
}

var typeOfFunc *js.Object

func initTypeOf() {
typeOfFunc = js.Global.Call("eval", `(function() {
return function (x) {
return typeof x
}
})()
`)
}

// TypeOf returns the JavaScript type of the passed value
func TypeOf(value interface{}) string {
if typeOfFunc == nil {
initTypeOf()
}
return typeOfFunc.Invoke(value).String()
return js.Global.Get("$jsbuiltin$").Call("typeoffunc", value).String()
}
3 changes: 3 additions & 0 deletions jsbuiltin.inc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$global.$jsbuiltin$ = {
typeoffunc: function(x) { return typeof x },
}
11 changes: 10 additions & 1 deletion jsbuiltin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ func TestIsNaN(t *testing.T) {
}
}

// This is a hack to initialize the jsbuiltin object, until issue 306 is resolved
// See https://github.com/gopherjs/gopherjs/issues/306
func init() {
js.Global.Call("eval", `
GLOBAL.$jsbuiltin$ = {
typeoffunc: function(x) { return typeof x },
}`)
}


func TestTypeOf(t *testing.T) {
data := map[interface{}]string{
// Standard JS types
Expand Down Expand Up @@ -169,5 +179,4 @@ func TestTypeOf(t *testing.T) {
if to := TypeOf(js.Object{}); to != "object" {
t.Fatal("Invalid/empty JS object not recognized as object")
}

}

0 comments on commit 687a38a

Please sign in to comment.