Debian Codegraph @

luaez.go

raw
1 package goluaez
2
3 import (
4 "fmt"
5 "log"
6 "reflect"
7 "sync"
8
9 "github.com/yuin/gluare"
10 "github.com/yuin/gopher-lua"
11 )
12
13 type State struct {
14 mu sync.Mutex
15 *lua.LState
16 }
17
18 // dont modify this directly
19 // modify code in data/prelude.lua and then put it here.
20 const prelude = `
21 -- remove whitespace at the ends of a string.
22 function string:strip()
23 return self:match'^%s*(.*%S)' or ''
24 end
25
26 -- split a string by a separator
27 function string:split(sep)
28 local sep, fields = sep or "\t", {}
29 local pattern = string.format("[^%s]+", sep)
30 for tok in self:gmatch(pattern) do fields[#fields+1] = tok end
31 return fields
32 end
33 `
34
35 // TODO: see https://github.com/yuin/gluamapper
36
37 // NewState creates a new State object optionally initialized with some code.
38 func NewState(code ...string) (*State, error) {
39 s := &State{}
40 options := lua.Options{IncludeGoStackTrace: true}
41 s.LState = lua.NewState(options)
42 s.PreloadModule("re", gluare.Loader)
43 var err error
44 err = s.LState.DoString(prelude)
45 if err != nil {
46 return nil, err
47 }
48 if len(code) != 0 && len(code[0]) != 0 {
49 err = s.LState.DoString(code[0])
50 if err != nil {
51 return s, err
52 }
53 }
54 return s, err
55 }
56
57 func (s *State) SetGlobal(name string, val interface{}) error {
58 l, err := Go2LValue(val)
59 s.LState.SetGlobal(name, l)
60 return err
61 }
62
63 func Go2LValue(v interface{}) (lua.LValue, error) {
64 switch cast := v.(type) {
65 case float32:
66 return lua.LNumber(cast), nil
67 case float64:
68 return lua.LNumber(cast), nil
69 case int, int32, int64:
70 return lua.LNumber(float64(reflect.ValueOf(v).Int())), nil
71 case uint, uint32, uint64:
72 return lua.LNumber(float64(reflect.ValueOf(v).Uint())), nil
73 case string:
74 return lua.LString(cast), nil
75 case bool:
76 return lua.LBool(cast), nil
77 case nil:
78 return lua.LNil, nil
79 case []string:
80 tbl := &lua.LTable{}
81 for _, val := range cast {
82 tbl.Append(lua.LString(val))
83 }
84 return tbl, nil
85 case []int:
86 tbl := &lua.LTable{}
87 for _, val := range cast {
88 tbl.Append(lua.LNumber(val))
89 }
90 return tbl, nil
91 case []int64:
92 tbl := &lua.LTable{}
93 for _, val := range cast {
94 tbl.Append(lua.LNumber(val))
95 }
96 return tbl, nil
97 case []int32:
98 tbl := &lua.LTable{}
99 for _, val := range cast {
100 tbl.Append(lua.LNumber(val))
101 }
102 return tbl, nil
103 case []float32:
104 tbl := &lua.LTable{}
105 for _, val := range cast {
106 tbl.Append(lua.LNumber(val))
107 }
108 return tbl, nil
109 case []float64:
110 tbl := &lua.LTable{}
111 for _, val := range cast {
112 tbl.Append(lua.LNumber(val))
113 }
114 return tbl, nil
115 case []bool:
116 tbl := &lua.LTable{}
117 for _, val := range cast {
118 tbl.Append(lua.LBool(val))
119 }
120 return tbl, nil
121 case []interface{}:
122 if len(cast) == 0 {
123 return lua.LNil, nil
124 }
125 tbl := &lua.LTable{}
126 for _, val := range cast {
127 v, err := Go2LValue(val)
128 if err != nil {
129 return nil, err
130 }
131 tbl.Append(v)
132 }
133 return tbl, nil
134 default:
135 return nil, fmt.Errorf("cant convert %+v to LValue %+v", v, cast)
136 }
137 }
138
139 func LValue2Go(v lua.LValue) (interface{}, error) {
140 switch v.Type() {
141 case lua.LTString:
142 return string(v.(lua.LString)), nil
143 case lua.LTNumber:
144 return float64(v.(lua.LNumber)), nil
145 case lua.LTBool:
146 return bool(v.(lua.LBool)), nil
147 case lua.LTNil:
148 return nil, nil
149 case lua.LTTable:
150 tbl := v.(*lua.LTable)
151 varr := make([]interface{}, 0)
152 vmap := make(map[string]interface{})
153 all_ints := true
154 k := lua.LNil
155 i := 0
156 for {
157 i += 1
158 key, val := tbl.Next(k)
159 if key == lua.LNil {
160 break
161 }
162 gokey, err := LValue2Go(key)
163 if err != nil {
164 return nil, err
165 }
166 goval, err := LValue2Go(val)
167 if err != nil {
168 return nil, err
169 }
170
171 // see if we have all int keys
172 if gofloat, ok := gokey.(float64); ok && all_ints {
173 if int(gofloat) == i {
174 varr = append(varr, goval)
175 } else {
176 all_ints = false
177 }
178 } else {
179 all_ints = false
180 }
181 vmap[key.String()] = goval
182 k = key
183 }
184 if all_ints {
185 return varr, nil
186 }
187 return vmap, nil
188 case lua.LTUserData:
189 goval := v.(*lua.LUserData).Value
190 return goval, nil
191 default:
192 switch t := v.(type) {
193 default:
194 log.Println("IN luaez ...", t)
195 log.Printf("type:%+v\n", v)
196 log.Println(v.(*lua.LUserData).Value)
197 return reflect.ValueOf(t), nil
198 }
199
200 }
201 }
202
203 // Run code given some values. This is thread-safe.
204 func (s *State) Run(code string, values ...map[string]interface{}) (interface{}, error) {
205 var err error
206 if len(values) != 0 {
207 lvals := make([]lua.LValue, len(values[0]))
208 keys := make([]string, len(values[0]))
209 j := 0
210 for k, v := range values[0] {
211 s.mu.Lock()
212 lvals[j], err = Go2LValue(v)
213 s.mu.Unlock()
214 if err != nil {
215 return nil, err
216 }
217 keys[j] = k
218 j++
219 }
220
221 s.mu.Lock()
222 for i, lv := range lvals {
223 s.LState.SetGlobal(keys[i], lv)
224 }
225 } else {
226 s.mu.Lock()
227 }
228 if err := s.DoString("return " + code); err != nil {
229 s.mu.Unlock()
230 return nil, err
231 }
232 if s.GetTop() == 0 {
233 s.mu.Unlock()
234 return nil, nil
235 }
236 v := s.Get(-1)
237 s.Pop(1)
238 s.mu.Unlock()
239 return LValue2Go(v)
240 }