Skip to content

Commit 32b3b25

Browse files
committed
Rebuilding function invocation
1 parent 1a59175 commit 32b3b25

3 files changed

Lines changed: 53 additions & 56 deletions

File tree

glide.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mapper/function.go

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77

88
func ToGFunction(gfn interface{}) (lua.LGFunction, error) {
99
var (
10-
v = reflect.IndirectValue(reflect.ValueOf(gfn))
11-
t = v.Type()
12-
numIn = t.NumIn()
10+
v = reflect.IndirectValue(reflect.ValueOf(gfn))
11+
t = v.Type()
12+
numIn = t.NumIn()
13+
variadic = t.IsVariadic()
1314
)
1415

1516
if k := t.Kind(); k != reflect.Func {
@@ -22,69 +23,61 @@ func ToGFunction(gfn interface{}) (lua.LGFunction, error) {
2223
return lua.LGFunction(
2324
func(l *lua.LState) int {
2425
var (
25-
n = l.GetTop()
26-
tt reflect.Type
27-
arg interface{}
28-
args = make(
29-
[]reflect.Value,
30-
n,
31-
n,
32-
)
33-
rt lua.LValue
34-
rts []reflect.Value
35-
err error
26+
n = l.GetTop()
27+
fixedNumIn = numIn
28+
buf interface{}
29+
args []reflect.Value
30+
tt reflect.Type
31+
rt lua.LValue
32+
rts []reflect.Value
33+
err error
3634
)
3735

38-
if numIn != n {
39-
if !t.IsVariadic() {
36+
if variadic {
37+
fixedNumIn--
38+
39+
if n < fixedNumIn {
40+
l.ArgError(
41+
n,
42+
reflect.NewErrWrongArgumentsQuantity(fixedNumIn, n).Error(),
43+
)
44+
}
45+
} else {
46+
if n != fixedNumIn {
4047
l.ArgError(
4148
n,
42-
reflect.NewErrTooFewArguments(numIn, n).Error(),
49+
reflect.NewErrWrongArgumentsQuantity(fixedNumIn, n).Error(),
4350
)
4451
return 0
4552
}
46-
47-
numIn--
4853
}
4954

50-
for k := 0; k < numIn; k++ {
51-
tt = t.In(k)
52-
arg, err = FromValue(l.Get(k + 1))
55+
args = make([]reflect.Value, n)
56+
57+
for k := n; k > 0; k-- {
58+
buf, err = FromValue(l.Get(k))
5359
if err != nil {
54-
l.ArgError(k+1, err.Error())
60+
l.ArgError(k, err.Error())
5561
return 0
5662
}
57-
arg, err = reflect.ConvertToType(
58-
arg,
63+
64+
switch {
65+
case k <= fixedNumIn:
66+
tt = t.In(k - 1)
67+
case variadic && k == n:
68+
tt = t.In(fixedNumIn).Elem()
69+
}
70+
71+
buf, err = reflect.ConvertToType(
72+
buf,
5973
tt,
6074
)
6175
if err != nil {
62-
l.ArgError(k+1, err.Error())
76+
l.ArgError(k, err.Error())
6377
return 0
6478
}
6579

66-
args[k] = reflect.ValueOf(arg)
67-
}
68-
69-
if t.IsVariadic() {
70-
tt = t.In(numIn).Elem()
71-
for k := numIn; k < n; k++ {
72-
arg, err = FromValue(l.Get(k + 1))
73-
if err != nil {
74-
l.ArgError(k+1, err.Error())
75-
return 0
76-
}
77-
arg, err = reflect.ConvertToType(
78-
arg,
79-
tt,
80-
)
81-
if err != nil {
82-
l.ArgError(k+1, err.Error())
83-
return 0
84-
}
85-
86-
args[k] = reflect.ValueOf(arg)
87-
}
80+
args[k-1] = reflect.ValueOf(buf)
8881
}
8982

9083
rts = v.Call(args)

mapper/function_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,17 @@ func TestToGFunction(t *testing.T) {
114114

115115
l.PreloadModule("module", loader)
116116

117-
err = l.DoString(sample.code)
118-
if err != nil {
119-
t.Error(err)
120-
return
121-
}
117+
for n := 0; n < 10; n++ {
118+
state.called = false
119+
120+
err = l.DoString(sample.code)
121+
if err != nil {
122+
t.Error(err)
123+
return
124+
}
122125

123-
assert.Equal(t, true, state.called)
126+
assert.Equal(t, true, state.called)
127+
}
124128
},
125129
)
126130
}

0 commit comments

Comments
 (0)