diff --git a/type_go_map.go b/type_go_map.go index 3e204a0..41b9ac0 100644 --- a/type_go_map.go +++ b/type_go_map.go @@ -53,6 +53,14 @@ func goMapGetOwnProperty(self *_object, name string) *_property { return &_property{self.runtime.toValue(value.Interface()), 0111} } + // Other methods + if method := self.value.(*_goMapObject).value.MethodByName(name); (method != reflect.Value{}) { + return &_property{ + value: self.runtime.toValue(method.Interface()), + mode: 0110, + } + } + return nil } diff --git a/type_go_map_test.go b/type_go_map_test.go new file mode 100644 index 0000000..a89892b --- /dev/null +++ b/type_go_map_test.go @@ -0,0 +1,40 @@ +package otto + +import ( + "fmt" + "sort" + "testing" +) + +type GoMapTest map[string]int + +func (s GoMapTest) Join() string { + joinedStr := "" + + // Ordering the map takes some effort + // because map iterators in golang are unordered by definition. + // So we need to extract keys, sort them, and then generate K/V pairs + // All of this is meant to ensure that the test is predictable. + keys := make([]string, len(s)) + i := 0 + for key, _ := range s { + keys[i] = key + i++ + } + + sort.Strings(keys) + + for _, key := range keys { + joinedStr += key + ": " + fmt.Sprintf("%d", s[key]) + " " + } + return joinedStr +} + +func TestGoMap(t *testing.T) { + tt(t, func() { + test, vm := test() + vm.Set("TestMap", GoMapTest{"one": 1, "two": 2, "three": 3}) + is(test(`TestMap["one"]`).export(), 1) + is(test(`TestMap.Join()`).export(), "one: 1 three: 3 two: 2 ") + }) +}