1
0
mirror of https://github.com/golang/go synced 2024-11-26 18:16:48 -07:00

syscall.js: add Value.InstanceOf

Change-Id: Icf56188fdb2b8ce6789830a35608203fdb9a3df6
Reviewed-on: https://go-review.googlesource.com/120560
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Richard Musiol 2018-06-15 10:45:04 +02:00 committed by Brad Fitzpatrick
parent 6fdbed0543
commit 9c35c1a503
5 changed files with 27 additions and 0 deletions

View File

@ -286,6 +286,11 @@
loadSlice(sp + 16).set(str);
},
// func valueInstanceOf(v ref, t ref) bool
"syscall/js.valueInstanceOf": (sp) => {
mem().setUint8(sp + 16, loadValue(sp + 8) instanceof loadValue(sp + 12));
},
"debug": (value) => {
console.log(value);
},

View File

@ -31,3 +31,4 @@ syscall/js/js_js.s: [wasm] valueInt: RET without writing to 8-byte ret+8(FP)
syscall/js/js_js.s: [wasm] valueBool: RET without writing to 1-byte ret+8(FP)
syscall/js/js_js.s: [wasm] valueLength: RET without writing to 8-byte ret+8(FP)
syscall/js/js_js.s: [wasm] valuePrepareString: RET without writing to 4-byte ret+8(FP)
syscall/js/js_js.s: [wasm] valueInstanceOf: RET without writing to 1-byte ret+8(FP)

View File

@ -227,3 +227,10 @@ func (v Value) String() string {
func valuePrepareString(v ref) (ref, int)
func valueLoadString(v ref, b []byte)
// InstanceOf reports whether v is an instance of type t according to JavaScript's instanceof operator.
func (v Value) InstanceOf(t Value) bool {
return valueInstanceOf(v.ref, t.ref)
}
func valueInstanceOf(v ref, t ref) bool

View File

@ -71,3 +71,7 @@ TEXT ·valuePrepareString(SB), NOSPLIT, $0
TEXT ·valueLoadString(SB), NOSPLIT, $0
CallImport
RET
TEXT ·valueInstanceOf(SB), NOSPLIT, $0
CallImport
RET

View File

@ -146,6 +146,16 @@ func TestNew(t *testing.T) {
}
}
func TestInstanceOf(t *testing.T) {
someArray := js.Global.Get("Array").New()
if got, want := someArray.InstanceOf(js.Global.Get("Array")), true; got != want {
t.Errorf("got %#v, want %#v", got, want)
}
if got, want := someArray.InstanceOf(js.Global.Get("Function")), false; got != want {
t.Errorf("got %#v, want %#v", got, want)
}
}
func TestCallback(t *testing.T) {
c := make(chan struct{})
cb := js.NewCallback(func(args []js.Value) {