-
Notifications
You must be signed in to change notification settings - Fork 1
/
interp.go
151 lines (127 loc) · 3.96 KB
/
interp.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
// Copyright 2023 The Yock Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package yockr
import (
"context"
"path/filepath"
yocki "github.com/ansurfen/yock/interface"
lua "github.com/yuin/gopher-lua"
)
// New initialize yock runtime and returns the its pointer
func New(opts ...YockrOption) yocki.YockRuntime {
var yockr yocki.YockRuntime = &YockInterp{state: NewYState()}
for _, opt := range opts {
if err := opt(yockr); err != nil {
panic(err)
}
}
if yockr.State() == nil {
s, cancel := yockr.NewState()
if cancel != nil {
defer cancel()
}
yockr.SetState(s)
}
return yockr
}
var _ yocki.YockRuntime = (*YockInterp)(nil)
type YockrOption func(yockr yocki.YockRuntime) error
// YockInterp abstracts lua interpreter
type YockInterp struct {
state yocki.YockState
}
// State returns LState
func (yockr *YockInterp) State() yocki.YockState {
return yockr.state
}
func (yockr *YockInterp) SetState(l yocki.YockState) {
yockr.state = l
}
func (yockr *YockInterp) NewState() (yocki.YockState, context.CancelFunc) {
ls, cancel := yockr.state.LState().NewThread()
return UpgradeLState(ls), cancel
}
// FastCall to call specify function without arguments and not return value
func (yockr *YockInterp) FastCall(fun string) error {
return yockr.state.LState().CallByParam(lua.P{
Fn: yockr.state.LState().GetGlobal(fun),
NRet: 0,
Protect: true,
})
}
// Call to call specify function without arguments
func (yockr *YockInterp) Call(fun string) ([]lua.LValue, error) {
ret := []lua.LValue{}
if err := yockr.state.LState().CallByParam(lua.P{
Fn: yockr.state.LState().GetGlobal(fun),
NRet: 0,
Protect: true,
}); err != nil {
return ret, err
}
for i := 1; i <= yockr.state.LState().GetTop(); i++ {
ret = append(ret, yockr.state.LState().CheckAny(i))
}
return ret, nil
}
// SetGlobalFn to set global function
func (yockr *YockInterp) SetGlobalFn(loaders map[string]lua.LGFunction) {
for name, loader := range loaders {
yockr.state.LState().SetGlobal(name, yockr.state.LState().NewFunction(loader))
}
}
// SafeSetGlobalFn to set global function when it isn't exist
func (yockr *YockInterp) SafeSetGlobalFn(loaders map[string]lua.LGFunction) {
for name, loader := range loaders {
if value := yockr.state.LState().GetGlobal(name); value.String() == "nil" {
yockr.state.LState().SetGlobal(name, yockr.state.LState().NewFunction(loader))
}
}
}
// Eval to execute string of script
func (yockr *YockInterp) Eval(script string) error {
return yockr.state.LState().DoString(script)
}
// EvalFile to execute file of script
func (yockr *YockInterp) EvalFile(fullpath string) error {
if filepath.Ext(fullpath) == ".lua" {
return yockr.state.LState().DoFile(fullpath)
}
return nil
}
// EvalFunc to execute function
func (yockr *YockInterp) EvalFunc(fn lua.LValue, args []lua.LValue) ([]lua.LValue, error) {
ret := []lua.LValue{}
if err := yockr.state.LState().CallByParam(lua.P{
Fn: fn,
Protect: true,
}, args...); err != nil {
return ret, err
}
for i := 1; i <= yockr.state.LState().GetTop(); i++ {
ret = append(ret, yockr.state.LState().CheckAny(i))
}
return ret, nil
}
// FastEvalFunc to execute function and not return value
func (yockr *YockInterp) FastEvalFunc(fn lua.LValue, args []lua.LValue) error {
return yockr.state.LState().CallByParam(lua.P{
Fn: fn,
Protect: true,
}, args...)
}
// GetGlobalVar returns global variable
func (yockr *YockInterp) GetGlobalVar(name string) lua.LValue {
return yockr.state.LState().GetGlobal(name)
}
// SetGlobalVar to set global variable
func (yockr *YockInterp) SetGlobalVar(name string, v lua.LValue) {
yockr.state.LState().SetGlobal(name, v)
}
// SafeSetGlobalVar to set global variable when variable isn't exist
func (yockr *YockInterp) SafeSetGlobalVar(name string, v lua.LValue) {
if yockr.state.LState().GetGlobal(name).Type().String() == "nil" {
yockr.state.LState().SetGlobal(name, v)
}
}