Skip to content

Commit

Permalink
support methods on go array values
Browse files Browse the repository at this point in the history
  • Loading branch information
deoxxa committed Dec 15, 2019
1 parent 3ef5863 commit b839e7b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
45 changes: 45 additions & 0 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,3 +857,48 @@ func Test_stringArray(t *testing.T) {
is(r1, "these are strings")
})
}

type goByteArrayWithMethodsTest [8]byte

func (g goByteArrayWithMethodsTest) S() string { return string(g[:]) }
func (g goByteArrayWithMethodsTest) F(i int) byte { return g[i] }

func Test_goByteArrayWithMethods_typeof_S(t *testing.T) {
a := goByteArrayWithMethodsTest{97, 98, 99, 100, 101, 102, 103, 104}

tt(t, func() {
test, vm := test()
vm.Set("a", a)
is(test("typeof a.S").export(), "function")
})
}

func Test_goByteArrayWithMethods_S(t *testing.T) {
a := goByteArrayWithMethodsTest{97, 98, 99, 100, 101, 102, 103, 104}

tt(t, func() {
test, vm := test()
vm.Set("a", a)
is(test("a.S()").export(), "abcdefgh")
})
}

func Test_goByteArrayWithMethods_F0(t *testing.T) {
a := goByteArrayWithMethodsTest{97, 98, 99, 100, 101, 102, 103, 104}

tt(t, func() {
test, vm := test()
vm.Set("a", a)
is(test("a.F(0)").export(), 97)
})
}

func Test_goByteArrayWithMethods_F1(t *testing.T) {
a := goByteArrayWithMethodsTest{97, 98, 99, 100, 101, 102, 103, 104}

tt(t, func() {
test, vm := test()
vm.Set("a", a)
is(test("a.F(1)").export(), 98)
})
}
34 changes: 28 additions & 6 deletions type_go_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,32 @@ func _newGoArrayObject(value reflect.Value) *_goArrayObject {
return self
}

func (self _goArrayObject) getValue(index int64) (reflect.Value, bool) {
func (self _goArrayObject) getValue(name string) (reflect.Value, bool) {
if index, err := strconv.ParseInt(name, 10, 64); err != nil {
v, ok := self.getValueIndex(index)
if ok {
return v, ok
}
}

if m := self.value.MethodByName(name); m != (reflect.Value{}) {
return m, true
}

return reflect.Value{}, false
}

func (self _goArrayObject) getValueIndex(index int64) (reflect.Value, bool) {
value := reflect.Indirect(self.value)
if index < int64(value.Len()) {
return value.Index(int(index)), true
}

return reflect.Value{}, false
}

func (self _goArrayObject) setValue(index int64, value Value) bool {
indexValue, exists := self.getValue(index)
indexValue, exists := self.getValueIndex(index)
if !exists {
return false
}
Expand All @@ -64,11 +80,10 @@ func goArrayGetOwnProperty(self *_object, name string) *_property {
}

// .0, .1, .2, ...
index := stringToArrayIndex(name)
if index >= 0 {
if index := stringToArrayIndex(name); index >= 0 {
object := self.value.(*_goArrayObject)
value := Value{}
reflectValue, exists := object.getValue(index)
reflectValue, exists := object.getValueIndex(index)
if exists {
value = self.runtime.toValue(reflectValue.Interface())
}
Expand All @@ -78,6 +93,13 @@ func goArrayGetOwnProperty(self *_object, name string) *_property {
}
}

if method := self.value.(*_goArrayObject).value.MethodByName(name); method != (reflect.Value{}) {
return &_property{
self.runtime.toValue(method.Interface()),
0110,
}
}

return objectGetOwnProperty(self, name)
}

Expand Down Expand Up @@ -121,7 +143,7 @@ func goArrayDelete(self *_object, name string, throw bool) bool {
if index >= 0 {
object := self.value.(*_goArrayObject)
if object.writable {
indexValue, exists := object.getValue(index)
indexValue, exists := object.getValueIndex(index)
if exists {
indexValue.Set(reflect.Zero(reflect.Indirect(object.value).Type().Elem()))
return true
Expand Down

0 comments on commit b839e7b

Please sign in to comment.