Commit 60f27135 authored by Richard Musiol's avatar Richard Musiol Committed by Richard Musiol

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: default avatarCherry Zhang <cherryyz@google.com>
parent 843fec1c
...@@ -308,6 +308,11 @@ ...@@ -308,6 +308,11 @@
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32)); 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 // func valueIndex(v ref, i int) ref
"syscall/js.valueIndex": (sp) => { "syscall/js.valueIndex": (sp) => {
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16))); storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
......
...@@ -267,6 +267,17 @@ func (v Value) Set(p string, x interface{}) { ...@@ -267,6 +267,17 @@ func (v Value) Set(p string, x interface{}) {
func valueSet(v ref, p string, x ref) 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. // Index returns JavaScript index i of value v.
// It panics if v is not a JavaScript object. // It panics if v is not a JavaScript object.
func (v Value) Index(i int) Value { func (v Value) Index(i int) Value {
......
...@@ -16,6 +16,10 @@ TEXT ·valueSet(SB), NOSPLIT, $0 ...@@ -16,6 +16,10 @@ TEXT ·valueSet(SB), NOSPLIT, $0
CallImport CallImport
RET RET
TEXT ·valueDelete(SB), NOSPLIT, $0
CallImport
RET
TEXT ·valueIndex(SB), NOSPLIT, $0 TEXT ·valueIndex(SB), NOSPLIT, $0
CallImport CallImport
RET RET
......
...@@ -212,6 +212,18 @@ func TestSet(t *testing.T) { ...@@ -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) { func TestIndex(t *testing.T) {
if got := dummys.Get("someArray").Index(1).Int(); got != 42 { if got := dummys.Get("someArray").Index(1).Int(); got != 42 {
t.Errorf("got %#v, want %#v", got, 42) t.Errorf("got %#v, want %#v", got, 42)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment