]> Cypherpunks repositories - gostls13.git/commitdiff
syscall/js: add Value.Delete for deleting JavaScript properties
authorRichard Musiol <mail@richard-musiol.de>
Sat, 28 Sep 2019 21:47:37 +0000 (23:47 +0200)
committerRichard Musiol <neelance@gmail.com>
Mon, 30 Sep 2019 14:40:06 +0000 (14:40 +0000)
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>
misc/wasm/wasm_exec.js
src/syscall/js/js.go
src/syscall/js/js_js.s
src/syscall/js/js_test.go

index 96395856939050d7eb12b4bb376466227445cf61..9ffa9201e87052570c08590e0ae0a3d596b2cd76 100644 (file)
                                                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)));
index 7300d2c7695fc902b809f46bebb5c1122274333e..f42a16f0d0c2f23780154326fa556690f180ea7d 100644 (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 {
index 5f294682378a03e0396028de07577cac1f9ef993..ab56087c169713a6e959855a3f0a17fbf04d9f71 100644 (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
index 753c2c3a0da5e46aba605cd035e2a9210445b727..10d4364e4c4e9cec2d9f7c48af31e613f54f52cd 100644 (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)