File: luaez.go

package info (click to toggle)
golang-github-brentp-goluaez 0.0~git20160116.dd35d08-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 92 kB
  • sloc: makefile: 4
file content (240 lines) | stat: -rw-r--r-- 4,981 bytes parent folder | download
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package goluaez

import (
	"fmt"
	"log"
	"reflect"
	"sync"

	"github.com/yuin/gluare"
	"github.com/yuin/gopher-lua"
)

type State struct {
	mu sync.Mutex
	*lua.LState
}

// dont modify this directly
// modify code in data/prelude.lua and then put it here.
const prelude = `
-- remove whitespace at the ends of a string.
function string:strip()
    return self:match'^%s*(.*%S)' or ''
end

-- split a string by a separator
function string:split(sep)
    local sep, fields = sep or "\t", {}
    local pattern = string.format("[^%s]+", sep)
    for tok in self:gmatch(pattern) do fields[#fields+1] = tok end
    return fields
end
`

// TODO: see https://github.com/yuin/gluamapper

// NewState creates a new State object optionally initialized with some code.
func NewState(code ...string) (*State, error) {
	s := &State{}
	options := lua.Options{IncludeGoStackTrace: true}
	s.LState = lua.NewState(options)
	s.PreloadModule("re", gluare.Loader)
	var err error
	err = s.LState.DoString(prelude)
	if err != nil {
		return nil, err
	}
	if len(code) != 0 && len(code[0]) != 0 {
		err = s.LState.DoString(code[0])
		if err != nil {
			return s, err
		}
	}
	return s, err
}

func (s *State) SetGlobal(name string, val interface{}) error {
	l, err := Go2LValue(val)
	s.LState.SetGlobal(name, l)
	return err
}

func Go2LValue(v interface{}) (lua.LValue, error) {
	switch cast := v.(type) {
	case float32:
		return lua.LNumber(cast), nil
	case float64:
		return lua.LNumber(cast), nil
	case int, int32, int64:
		return lua.LNumber(float64(reflect.ValueOf(v).Int())), nil
	case uint, uint32, uint64:
		return lua.LNumber(float64(reflect.ValueOf(v).Uint())), nil
	case string:
		return lua.LString(cast), nil
	case bool:
		return lua.LBool(cast), nil
	case nil:
		return lua.LNil, nil
	case []string:
		tbl := &lua.LTable{}
		for _, val := range cast {
			tbl.Append(lua.LString(val))
		}
		return tbl, nil
	case []int:
		tbl := &lua.LTable{}
		for _, val := range cast {
			tbl.Append(lua.LNumber(val))
		}
		return tbl, nil
	case []int64:
		tbl := &lua.LTable{}
		for _, val := range cast {
			tbl.Append(lua.LNumber(val))
		}
		return tbl, nil
	case []int32:
		tbl := &lua.LTable{}
		for _, val := range cast {
			tbl.Append(lua.LNumber(val))
		}
		return tbl, nil
	case []float32:
		tbl := &lua.LTable{}
		for _, val := range cast {
			tbl.Append(lua.LNumber(val))
		}
		return tbl, nil
	case []float64:
		tbl := &lua.LTable{}
		for _, val := range cast {
			tbl.Append(lua.LNumber(val))
		}
		return tbl, nil
	case []bool:
		tbl := &lua.LTable{}
		for _, val := range cast {
			tbl.Append(lua.LBool(val))
		}
		return tbl, nil
	case []interface{}:
		if len(cast) == 0 {
			return lua.LNil, nil
		}
		tbl := &lua.LTable{}
		for _, val := range cast {
			v, err := Go2LValue(val)
			if err != nil {
				return nil, err
			}
			tbl.Append(v)
		}
		return tbl, nil
	default:
		return nil, fmt.Errorf("cant convert %+v to LValue %+v", v, cast)
	}
}

func LValue2Go(v lua.LValue) (interface{}, error) {
	switch v.Type() {
	case lua.LTString:
		return string(v.(lua.LString)), nil
	case lua.LTNumber:
		return float64(v.(lua.LNumber)), nil
	case lua.LTBool:
		return bool(v.(lua.LBool)), nil
	case lua.LTNil:
		return nil, nil
	case lua.LTTable:
		tbl := v.(*lua.LTable)
		varr := make([]interface{}, 0)
		vmap := make(map[string]interface{})
		all_ints := true
		k := lua.LNil
		i := 0
		for {
			i += 1
			key, val := tbl.Next(k)
			if key == lua.LNil {
				break
			}
			gokey, err := LValue2Go(key)
			if err != nil {
				return nil, err
			}
			goval, err := LValue2Go(val)
			if err != nil {
				return nil, err
			}

			// see if we have all int keys
			if gofloat, ok := gokey.(float64); ok && all_ints {
				if int(gofloat) == i {
					varr = append(varr, goval)
				} else {
					all_ints = false
				}
			} else {
				all_ints = false
			}
			vmap[key.String()] = goval
			k = key
		}
		if all_ints {
			return varr, nil
		}
		return vmap, nil
	case lua.LTUserData:
		goval := v.(*lua.LUserData).Value
		return goval, nil
	default:
		switch t := v.(type) {
		default:
			log.Println("IN luaez ...", t)
			log.Printf("type:%+v\n", v)
			log.Println(v.(*lua.LUserData).Value)
			return reflect.ValueOf(t), nil
		}

	}
}

// Run code given some values. This is thread-safe.
func (s *State) Run(code string, values ...map[string]interface{}) (interface{}, error) {
	var err error
	if len(values) != 0 {
		lvals := make([]lua.LValue, len(values[0]))
		keys := make([]string, len(values[0]))
		j := 0
		for k, v := range values[0] {
			s.mu.Lock()
			lvals[j], err = Go2LValue(v)
			s.mu.Unlock()
			if err != nil {
				return nil, err
			}
			keys[j] = k
			j++
		}

		s.mu.Lock()
		for i, lv := range lvals {
			s.LState.SetGlobal(keys[i], lv)
		}
	} else {
		s.mu.Lock()
	}
	if err := s.DoString("return " + code); err != nil {
		s.mu.Unlock()
		return nil, err
	}
	if s.GetTop() == 0 {
		s.mu.Unlock()
		return nil, nil
	}
	v := s.Get(-1)
	s.Pop(1)
	s.mu.Unlock()
	return LValue2Go(v)
}