syscall/js: add Value.Delete for deleting JavaScript properties

This change adds the method Value.Delete, which implements
JavaScript's "delete" operator for deleting properties.

Fixes #33079.

Change-Id: Ia5b190240bd59daca48094fcbc32f8d0a06f19d5
Reviewed-on: https://go-review.googlesource.com/c/go/+/197840
Run-TryBot: Richard Musiol <neelance@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
Richard Musiol 2019-09-28 23:47:37 +02:00 committed by Richard Musiol
parent 843fec1c7d
commit 60f271358f
4 changed files with 32 additions and 0 deletions

View File

@ -308,6 +308,11 @@
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
},
// func valueDelete(v ref, p string)
"syscall/js.valueDelete": (sp) => {
Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
},
// func valueIndex(v ref, i int) ref
"syscall/js.valueIndex": (sp) => {
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));

View File

@ -267,6 +267,17 @@ func (v Value) Set(p string, x interface{}) {
func valueSet(v ref, p string, x ref)
// Delete deletes the JavaScript property p of value v.
// It panics if v is not a JavaScript object.
func (v Value) Delete(p string) {
if vType := v.Type(); !vType.isObject() {
panic(&ValueError{"Value.Delete", vType})
}
valueDelete(v.ref, p)
}
func valueDelete(v ref, p string)
// Index returns JavaScript index i of value v.
// It panics if v is not a JavaScript object.
func (v Value) Index(i int) Value {

View File

@ -16,6 +16,10 @@ TEXT ·valueSet(SB), NOSPLIT, $0
CallImport
RET
TEXT ·valueDelete(SB), NOSPLIT, $0
CallImport
RET
TEXT ·valueIndex(SB), NOSPLIT, $0
CallImport
RET

View File

@ -212,6 +212,18 @@ func TestSet(t *testing.T) {
})
}
func TestDelete(t *testing.T) {
dummys.Set("test", 42)
dummys.Delete("test")
if dummys.Call("hasOwnProperty", "test").Bool() {
t.Errorf("property still exists")
}
expectValueError(t, func() {
dummys.Get("zero").Delete("badField")
})
}
func TestIndex(t *testing.T) {
if got := dummys.Get("someArray").Index(1).Int(); got != 42 {
t.Errorf("got %#v, want %#v", got, 42)