Skip to content
This repository has been archived by the owner on Apr 30, 2019. It is now read-only.

Add (*js.Value).InstanceOf() #2

Merged
merged 2 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion js/js_notwasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ type Value struct {
}

var (
id *js.Object
id *js.Object
instanceOf *js.Object
)

func init() {
if js.Global != nil {
id = js.Global.Call("eval", "(function(x) { return x; })")
instanceOf = js.Global.Call("eval", "(function(x, y) { return x instanceof y; })")
}
}

Expand Down Expand Up @@ -171,6 +173,10 @@ func (v Value) String() string {
return v.v.String()
}

func (v Value) InstanceOf(t Value) bool {
return instanceOf.Invoke(v, t).Bool()
}

func GetInternalObject(v Value) interface{} {
return v.v
}
9 changes: 9 additions & 0 deletions js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,12 @@ func TestInt64(t *testing.T) {
t.Errorf("got %#v, want %#v", got, want)
}
}

func TestInstanceOf(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A second test for the negative case would be good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

arr := js.Global.Call("eval", "[]")
got := arr.InstanceOf(js.Global.Call("eval", "Array"))
want := true
if got != want {
t.Errorf("got %#v, want %#v", got, want)
}
}