Skip to content

Commit

Permalink
Report 'length' as own property for Go slices. Fixes #328.
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Sep 12, 2021
1 parent e67f3e7 commit ac5354e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion object_goslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (o *objectGoSlice) hasOwnPropertyStr(name unistring.String) bool {
if idx := strToIdx64(name); idx >= 0 {
return idx < int64(len(*o.data))
}
return false
return name == "length"
}

func (o *objectGoSlice) defineOwnPropertyIdx(idx valueInt, descr PropertyDescriptor, throw bool) bool {
Expand Down
4 changes: 2 additions & 2 deletions object_goslice_reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ func (o *objectGoSliceReflect) setForeignIdx(idx valueInt, val, receiver Value,
}

func (o *objectGoSliceReflect) setForeignStr(name unistring.String, val, receiver Value, throw bool) (bool, bool) {
return o._setForeignStr(name, trueValIfPresent(o._hasStr(name)), val, receiver, throw)
return o._setForeignStr(name, trueValIfPresent(o.hasOwnPropertyStr(name)), val, receiver, throw)
}

func (o *objectGoSliceReflect) hasOwnPropertyIdx(idx valueInt) bool {
return o._hasIdx(idx)
}

func (o *objectGoSliceReflect) hasOwnPropertyStr(name unistring.String) bool {
if o._hasStr(name) {
if o._hasStr(name) || name == "length" {
return true
}
return o.objectGoReflect._has(name.String())
Expand Down
40 changes: 40 additions & 0 deletions object_goslice_reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,43 @@ func TestGoSliceReflectPopNoPtr(t *testing.T) {
t.Fatal(v)
}
}

func TestGoSliceReflectLengthProperty(t *testing.T) {
vm := New()
vm.Set("s", []int{2, 3, 4})
_, err := vm.RunString(`
if (!s.hasOwnProperty("length")) {
throw new Error("hasOwnProperty() returned false");
}
let desc = Object.getOwnPropertyDescriptor(s, "length");
if (desc.value !== 3 || !desc.writable || desc.enumerable || desc.configurable) {
throw new Error("incorrect property descriptor: " + JSON.stringify(desc));
}
`)
if err != nil {
t.Fatal(err)
}
}

type testCustomSliceWithMethods []int

func (a testCustomSliceWithMethods) Method() bool {
return true
}

func TestGoSliceReflectMethods(t *testing.T) {
vm := New()
vm.Set("s", testCustomSliceWithMethods{1, 2, 3})
_, err := vm.RunString(`
if (!s.hasOwnProperty("Method")) {
throw new Error("hasOwnProperty() returned false");
}
let desc = Object.getOwnPropertyDescriptor(s, "Method");
if (desc.value() !== true || desc.writable || !desc.enumerable || desc.configurable) {
throw new Error("incorrect property descriptor: " + JSON.stringify(desc));
}
`)
if err != nil {
t.Fatal(err)
}
}
17 changes: 17 additions & 0 deletions object_goslice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,20 @@ func TestGoSliceShift(t *testing.T) {
t.Fatal(v)
}
}

func TestGoSliceLengthProperty(t *testing.T) {
vm := New()
vm.Set("s", []interface{}{2, 3, 4})
_, err := vm.RunString(`
if (!s.hasOwnProperty("length")) {
throw new Error("hasOwnProperty() returned false");
}
let desc = Object.getOwnPropertyDescriptor(s, "length");
if (desc.value !== 3 || !desc.writable || desc.enumerable || desc.configurable) {
throw new Error("incorrect property descriptor: " + JSON.stringify(desc));
}
`)
if err != nil {
t.Fatal(err)
}
}

0 comments on commit ac5354e

Please sign in to comment.