From e5ff4cef59805d5b060b2e53d2d5d2ab0c68b71f Mon Sep 17 00:00:00 2001 From: Robert Krimen Date: Mon, 4 Feb 2013 07:30:53 -0800 Subject: [PATCH] Add delete to type_array --- otto_test.go | 12 +++++++++--- type_array.go | 20 ++++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/otto_test.go b/otto_test.go index 323fc66..4a64c81 100644 --- a/otto_test.go +++ b/otto_test.go @@ -1262,9 +1262,9 @@ func TestArray(t *testing.T) { test := runTest() test(` - abc = ""+[0, 1, 2, 3] - def = [].toString() - ghi = [null, 4, "null"].toString() + abc = ""+[0, 1, 2, 3]; + def = [].toString(); + ghi = [null, 4, "null"].toString(); `) test("abc", "0,1,2,3") test("def", "") @@ -1272,6 +1272,12 @@ func TestArray(t *testing.T) { test(`new Array(0).length`, "0") test(`new Array(11).length`, "11") test(`new Array(11, 1).length`, "2") + + test(` + abc = [0, 1, 2, 3]; + delete abc[1]; + abc; + `, "0,,2,3") } func TestArray_toString(t *testing.T) { diff --git a/type_array.go b/type_array.go index 3dd9c16..652a3ba 100644 --- a/type_array.go +++ b/type_array.go @@ -117,7 +117,7 @@ func (self *_arrayStash) property(name string) *_property { if name == "length" { return &_property{ value: toValue(len(self.valueArray)), - mode: 0100, // +Write -Enumerate -Configure + mode: 0100, // +w-ec } } @@ -130,7 +130,7 @@ func (self *_arrayStash) property(name string) *_property { } return &_property{ value: value, - mode: 0111, // +Write +Enumerate +Configure + mode: 0111, // +wec } } @@ -148,3 +148,19 @@ func (self *_arrayStash) enumerate(each func(string)) { } self._stash.enumerate(each) } + +func (self *_arrayStash) delete(name string) { + // length + if name == "length" { + return + } + + // .0, .1, .2, ... + index := stringToArrayIndex(name) + if index >= 0 { + if index < int64(len(self.valueArray)) { + self.valueArray[index] = emptyValue() + } + } + +}