-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.go
182 lines (154 loc) · 4.91 KB
/
interpreter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright © 2023 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"fmt"
lua "github.com/yuin/gopher-lua"
)
type Interpreter struct {
debug bool
script string
}
// LuaInterpreter is the lua interpreter, instantiated in main()
var LuaInterpreter *lua.LState
// holds a user provided lua function
type LuaFunction struct {
name string
help string
numargs int
}
// LuaFuncs must be global since init() is being called from lua which
// doesn't have access to the interpreter instance
var LuaFuncs map[string]LuaFunction
func NewInterpreter(script string, debug bool) *Interpreter {
return &Interpreter{debug: debug, script: script}
}
// initialize the lua environment properly
func (i *Interpreter) InitLua() {
// we only load a subset of lua Open modules and don't allow
// net, system or io stuff
for _, pair := range []struct {
n string
f lua.LGFunction
}{
{lua.LoadLibName, lua.OpenPackage},
{lua.BaseLibName, lua.OpenBase},
{lua.TabLibName, lua.OpenTable},
{lua.DebugLibName, lua.OpenDebug},
{lua.MathLibName, lua.OpenMath},
} {
if err := LuaInterpreter.CallByParam(lua.P{
Fn: LuaInterpreter.NewFunction(pair.f),
NRet: 0,
Protect: true,
}, lua.LString(pair.n)); err != nil {
panic(err)
}
}
// load the lua config (which we expect to contain init() and math functions)
if err := LuaInterpreter.DoFile(i.script); err != nil {
panic(err)
}
// instantiate
LuaFuncs = map[string]LuaFunction{}
// that way the user can call register(...) from lua inside init()
LuaInterpreter.SetGlobal("register", LuaInterpreter.NewFunction(register))
// actually call init()
if err := LuaInterpreter.CallByParam(lua.P{
Fn: LuaInterpreter.GetGlobal("init"),
NRet: 0,
Protect: true,
}); err != nil {
panic(err)
}
}
func (i *Interpreter) Debug(msg string) {
if i.debug {
fmt.Printf("DEBUG(lua): %s\n", msg)
}
}
func (i *Interpreter) FuncNumArgs(name string) int {
return LuaFuncs[name].numargs
}
// Call a user provided math function registered with register().
//
// Each function has to tell us how many args it expects, the actual
// function call from here is different depending on the number of
// arguments. 1 uses the last item of the stack, 2 the last two and -1
// all items (which translates to batch mode)
//
// The items array will be provided by calc.Eval(), these are
// non-popped stack items. So the items will only removed from the
// stack when the lua function execution is successful.
func (i *Interpreter) CallLuaFunc(funcname string, items []float64) (float64, error) {
i.Debug(fmt.Sprintf("calling lua func %s() with %d args",
funcname, LuaFuncs[funcname].numargs))
switch LuaFuncs[funcname].numargs {
case 0:
fallthrough
case 1:
// 1 arg variant
if err := LuaInterpreter.CallByParam(lua.P{
Fn: LuaInterpreter.GetGlobal(funcname),
NRet: 1,
Protect: true,
}, lua.LNumber(items[0])); err != nil {
return 0, fmt.Errorf("failed to exec lua func %s: %w", funcname, err)
}
case 2:
// 2 arg variant
if err := LuaInterpreter.CallByParam(lua.P{
Fn: LuaInterpreter.GetGlobal(funcname),
NRet: 1,
Protect: true,
}, lua.LNumber(items[0]), lua.LNumber(items[1])); err != nil {
return 0, fmt.Errorf("failed to exec lua func %s: %w", funcname, err)
}
case -1:
// batch variant, use lua table as array
table := LuaInterpreter.NewTable()
// put the whole stack into it
for _, item := range items {
table.Append(lua.LNumber(item))
}
if err := LuaInterpreter.CallByParam(lua.P{
Fn: LuaInterpreter.GetGlobal(funcname),
NRet: 1,
Protect: true,
}, table); err != nil {
return 0, fmt.Errorf("failed to exec lua func %s: %w", funcname, err)
}
}
// get result and cast to float64
if res, ok := LuaInterpreter.Get(-1).(lua.LNumber); ok {
LuaInterpreter.Pop(1)
return float64(res), nil
}
return 0, errors.New("function did not return a float64")
}
// called from lua to register a math function numargs may be 1, 2 or
// -1, it denotes the number of items from the stack requested by the
// lua function. -1 means batch mode, that is all items
func register(lstate *lua.LState) int {
function := lstate.ToString(1)
numargs := lstate.ToInt(2)
help := lstate.ToString(3)
LuaFuncs[function] = LuaFunction{
name: function,
numargs: numargs,
help: help,
}
return 1
}