1
0
mirror of https://github.com/golang/go synced 2024-11-22 01:14:40 -07:00

exp/template: use MethodByName, add to build.

R=rsc, adg, r
CC=golang-dev
https://golang.org/cl/4641081
This commit is contained in:
Rob Pike 2011-06-29 13:44:13 +10:00
parent 22484e2262
commit a733f0bcd1
2 changed files with 5 additions and 12 deletions

View File

@ -82,6 +82,7 @@ DIRS=\
exp/gui\
exp/gui/x11\
exp/regexp/syntax\
exp/template\
expvar\
flag\
fmt\

View File

@ -134,10 +134,7 @@ func (s *state) evalFieldNode(data reflect.Value, field *fieldNode, args []node,
}
func (s *state) evalField(data reflect.Value, fieldName string) reflect.Value {
for {
if data.Kind() != reflect.Ptr {
break
}
for data.Kind() == reflect.Ptr {
data = reflect.Indirect(data)
}
switch data.Kind() {
@ -162,12 +159,8 @@ func (s *state) evalMethodOrField(data reflect.Value, fieldName string, args []n
ptr, data = data, reflect.Indirect(data)
}
// Is it a method? We use the pointer because it has value methods too.
// TODO: reflect.Type could use a MethodByName.
for i := 0; i < ptr.Type().NumMethod(); i++ {
method := ptr.Type().Method(i)
if method.Name == fieldName {
return s.evalMethod(ptr, i, args, final)
}
if method, ok := ptr.Type().MethodByName(fieldName); ok {
return s.evalMethod(ptr, method, args, final)
}
if len(args) > 1 || final.IsValid() {
s.errorf("%s is not a method but has arguments", fieldName)
@ -185,8 +178,7 @@ var (
osErrorType = reflect.TypeOf(new(os.Error)).Elem()
)
func (s *state) evalMethod(v reflect.Value, i int, args []node, final reflect.Value) reflect.Value {
method := v.Type().Method(i)
func (s *state) evalMethod(v reflect.Value, method reflect.Method, args []node, final reflect.Value) reflect.Value {
typ := method.Type
fun := method.Func
numIn := len(args)