This repository has been archived by the owner on Jun 7, 2021. It is now read-only.
forked from afitz/golua
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlua.go
477 lines (375 loc) · 11.4 KB
/
lua.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package golua
/*
#cgo windows LDFLAGS: -llua51
#cgo LDFLAGS: -lm
#include <stdlib.h>
#include "lua.h"
#include "golua.h"
*/
import "C"
import "unsafe"
//like lua_Writer, but as p will contain capacity, not needed as separate param
type Writer func(L *State, p []byte, ud interface{})
//like lua reader, but the return slice has the size, so does
// we do not need it to be an out param
type Reader func(L *State, data interface{}) []byte
func newState(L *C.lua_State) *State {
var newstatei interface{}
newstate := &State{L, make([]interface{}, 0, 8), make([]uint, 0, 8)}
newstatei = newstate
ns1 := unsafe.Pointer(&newstatei)
ns2 := (*C.GoInterface)(ns1)
C.clua_setgostate(L, *ns2) //hacky....
C.clua_initstate(L)
return newstate
}
func (L *State) addFreeIndex(i uint) {
freelen := len(L.freeIndices)
//reallocate if necessary
if freelen+1 > cap(L.freeIndices) {
newSlice := make([]uint, freelen, cap(L.freeIndices)*2)
copy(newSlice, L.freeIndices)
L.freeIndices = newSlice
}
//reslice
L.freeIndices = L.freeIndices[0 : freelen+1]
L.freeIndices[freelen] = i
}
func (L *State) getFreeIndex() (index uint, ok bool) {
freelen := len(L.freeIndices)
//if there exist entries in the freelist
if freelen > 0 {
i := L.freeIndices[freelen-1] //get index
L.freeIndices = L.freeIndices[0:i] //'pop' index from list
return i, true
}
return 0, false
}
//returns the registered function id
func (L *State) register(f interface{}) uint {
index, ok := L.getFreeIndex()
//if not ok, then we need to add new index by extending the slice
if !ok {
index = uint(len(L.registry))
//reallocate backing array if necessary
if index+1 > uint(cap(L.registry)) {
newSlice := make([]interface{}, index, cap(L.registry)*2)
copy(newSlice, L.registry)
L.registry = newSlice
}
//reslice
L.registry = L.registry[0 : index+1]
}
L.registry[index] = f
return index
}
func (L *State) unregister(fid uint) {
if (fid < uint(len(L.registry))) && (L.registry[fid] != nil) {
L.registry[fid] = nil
L.addFreeIndex(fid)
}
}
func (L *State) PushGoFunction(f GoFunction) {
fid := L.register(f)
C.clua_pushgofunction(L.s, C.uint(fid))
}
func (L *State) PushLightInteger(n int) {
C.clua_pushlightinteger(L.s, C.int(n))
}
//push pointer by value, as a value - we don't impact lifetime
func (L *State) PushLightUserdata(ud *interface{}) {
//push
C.lua_pushlightuserdata(L.s, unsafe.Pointer(ud))
}
/*
//TODO:
//push pointer as full userdata - mem is go owned, but we
//make a guarantee that lifetime will outlast lua lifetime
func PushUserdata(L *State, ud interface{}) {
}
*/
//old style
func (L *State) NewUserdata(size uintptr) unsafe.Pointer {
return unsafe.Pointer(C.lua_newuserdata(L.s, C.size_t(size)))
}
func (L *State) AtPanic(panicf GoFunction) (oldpanicf GoFunction) {
fid := uint(0)
if panicf != nil {
fid = L.register(panicf)
}
oldres := interface{}(C.clua_atpanic(L.s, C.uint(fid)))
switch i := oldres.(type) {
case C.uint:
f := L.registry[uint(i)].(GoFunction)
//free registry entry
L.unregister(uint(i))
return f
case C.lua_CFunction:
return func(L1 *State) int {
return int(C.clua_callluacfunc(L1.s, i))
}
}
//generally we only get here if the panicf got set to something like nil
//potentially dangerous because we may silently fail
return nil
}
func (L *State) Call(nargs int, nresults int) {
C.lua_call(L.s, C.int(nargs), C.int(nresults))
}
func (L *State) CheckStack(extra int) bool {
return C.lua_checkstack(L.s, C.int(extra)) != 0
}
func (L *State) Close() {
C.lua_close(L.s)
}
func (L *State) Concat(n int) {
C.lua_concat(L.s, C.int(n))
}
func (L *State) CreateTable(narr int, nrec int) {
C.lua_createtable(L.s, C.int(narr), C.int(nrec))
}
//CPcall replacement
func (L *State) GoPCall(fun GoFunction, ud interface{}) int {
//TODO: need to emulate by pushing a c closure as in pushgofunction
return 0
}
//TODO: data be a slice?
func (L *State) Dump(writer Writer, data interface{}) int {
//TODO:
return 0
}
func (L *State) Equal(index1, index2 int) bool {
return C.lua_equal(L.s, C.int(index1), C.int(index2)) == 1
}
func (L *State) Error() int { return int(C.lua_error(L.s)) }
func (L *State) GC(what, data int) int { return int(C.lua_gc(L.s, C.int(what), C.int(data))) }
func (L *State) GetfEnv(index int) { C.lua_getfenv(L.s, C.int(index)) }
func (L *State) GetField(index int, k string) {
Ck := C.CString(k)
defer C.free(unsafe.Pointer(Ck))
C.lua_getfield(L.s, C.int(index), Ck)
}
func (L *State) GetGlobal(name string) { L.GetField(LUA_GLOBALSINDEX, name) }
func (L *State) GetMetaTable(index int) bool {
return C.lua_getmetatable(L.s, C.int(index)) != 0
}
func (L *State) GetTable(index int) { C.lua_gettable(L.s, C.int(index)) }
func (L *State) GetTop() int { return int(C.lua_gettop(L.s)) }
func (L *State) Insert(index int) { C.lua_insert(L.s, C.int(index)) }
func (L *State) IsBoolean(index int) bool {
return int(C.lua_type(L.s, C.int(index))) == LUA_TBOOLEAN
}
func (L *State) IsGoFunction(index int) bool {
//TODO:go function is now a userdatum, not a c function, so this will not work
return C.lua_iscfunction(L.s, C.int(index)) == 1
}
//TODO: add iscfunction
func (L *State) IsFunction(index int) bool {
return int(C.lua_type(L.s, C.int(index))) == LUA_TFUNCTION
}
func (L *State) IsLightUserdata(index int) bool {
return int(C.lua_type(L.s, C.int(index))) == LUA_TLIGHTUSERDATA
}
func (L *State) IsNil(index int) bool { return int(C.lua_type(L.s, C.int(index))) == LUA_TNIL }
func (L *State) IsNone(index int) bool { return int(C.lua_type(L.s, C.int(index))) == LUA_TNONE }
func (L *State) IsNoneOrNil(index int) bool { return int(C.lua_type(L.s, C.int(index))) <= 0 }
func (L *State) IsNumber(index int) bool { return C.lua_isnumber(L.s, C.int(index)) == 1 }
func (L *State) IsString(index int) bool { return C.lua_isstring(L.s, C.int(index)) == 1 }
func (L *State) IsTable(index int) bool { return int(C.lua_type(L.s, C.int(index))) == LUA_TTABLE }
func (L *State) IsThread(index int) bool {
return int(C.lua_type(L.s, C.int(index))) == LUA_TTHREAD
}
func (L *State) IsUserdata(index int) bool { return C.lua_isuserdata(L.s, C.int(index)) == 1 }
func (L *State) LessThan(index1, index2 int) bool {
return C.lua_lessthan(L.s, C.int(index1), C.int(index2)) == 1
}
func (L *State) Load(reader Reader, data interface{}, chunkname string) int {
//TODO:
return 0
}
//NOTE: lua_newstate becomes NewStateAlloc whereas
// luaL_newstate becomes NewState
func NewStateAlloc(f Alloc) *State {
//TODO: implement a newState function which will initialize a State
// call with result from C.lua_newstate for the s initializer
//ls := lua_newstate(
ls := C.clua_newstate(unsafe.Pointer(&f))
//ls := clua_newstate(
return newState(ls)
}
func (L *State) NewTable() {
C.lua_createtable(L.s, 0, 0)
}
func (L *State) NewThread() *State {
//TODO: call newState with result from C.lua_newthread and return it
//TODO: should have same lists as parent
// but may complicate gc
s := C.lua_newthread(L.s)
return &State{s, nil, nil}
}
func (L *State) Next(index int) int {
return int(C.lua_next(L.s, C.int(index)))
}
func (L *State) ObjLen(index int) uint {
return uint(C.lua_objlen(L.s, C.int(index)))
}
func (L *State) PCall(nargs int, nresults int, errfunc int) int {
return int(C.lua_pcall(L.s, C.int(nargs), C.int(nresults), C.int(errfunc)))
}
func (L *State) Pop(n int) {
//C.lua_pop(L.s, C.int(n));
C.lua_settop(L.s, C.int(-n-1))
}
func (L *State) PushBoolean(b bool) {
var bint int
if b {
bint = 1
} else {
bint = 0
}
C.lua_pushboolean(L.s, C.int(bint))
}
func (L *State) PushString(str string) {
Cstr := C.CString(str)
defer C.free(unsafe.Pointer(Cstr))
C.lua_pushstring(L.s, Cstr)
}
func (L *State) PushInteger(n int) {
C.lua_pushinteger(L.s, C.lua_Integer(n))
}
func (L *State) PushNil() {
C.lua_pushnil(L.s)
}
func (L *State) PushNumber(n float64) {
C.lua_pushnumber(L.s, C.lua_Number(n))
}
func (L *State) PushThread() (isMain bool) {
return C.lua_pushthread(L.s) != 0
}
func (L *State) PushValue(index int) {
C.lua_pushvalue(L.s, C.int(index))
}
func (L *State) RawEqual(index1 int, index2 int) bool {
return C.lua_rawequal(L.s, C.int(index1), C.int(index2)) != 0
}
func (L *State) RawGet(index int) {
C.lua_rawget(L.s, C.int(index))
}
func (L *State) RawGeti(index int, n int) {
C.lua_rawgeti(L.s, C.int(index), C.int(n))
}
func (L *State) RawSet(index int) {
C.lua_rawset(L.s, C.int(index))
}
func (L *State) RawSeti(index int, n int) {
C.lua_rawseti(L.s, C.int(index), C.int(n))
}
func (L *State) Register(name string, f GoFunction) {
L.PushGoFunction(f)
L.SetGlobal(name)
}
func (L *State) Remove(index int) {
C.lua_remove(L.s, C.int(index))
}
func (L *State) Replace(index int) {
C.lua_replace(L.s, C.int(index))
}
func (L *State) Resume(narg int) int {
return int(C.lua_resume(L.s, C.int(narg)))
}
func (L *State) SetAllocf(f Alloc) {
C.clua_setallocf(L.s, unsafe.Pointer(&f))
}
func (L *State) SetfEnv(index int) {
C.lua_setfenv(L.s, C.int(index))
}
func (L *State) SetField(index int, k string) {
Ck := C.CString(k)
defer C.free(unsafe.Pointer(Ck))
C.lua_setfield(L.s, C.int(index), Ck)
}
func (L *State) SetGlobal(name string) {
Cname := C.CString(name)
defer C.free(unsafe.Pointer(Cname))
C.lua_setfield(L.s, C.int(LUA_GLOBALSINDEX), Cname)
}
func (L *State) SetMetaTable(index int) {
C.lua_setmetatable(L.s, C.int(index))
}
func (L *State) SetTable(index int) {
C.lua_settable(L.s, C.int(index))
}
func (L *State) SetTop(index int) {
C.lua_settop(L.s, C.int(index))
}
func (L *State) Status() int {
return int(C.lua_status(L.s))
}
func (L *State) ToBoolean(index int) bool {
return C.lua_toboolean(L.s, C.int(index)) != 0
}
func (L *State) ToGoFunction(index int) (f GoFunction) {
fid := C.clua_togofunction(L.s, C.int(index))
return L.registry[fid].(GoFunction)
}
func (L *State) ToString(index int) string {
var size C.size_t
//C.GoString(C.lua_tolstring(L.s, C.int(index), &size));
return C.GoString(C.lua_tolstring(L.s, C.int(index), &size))
}
func (L *State) ToInteger(index int) int {
return int(C.lua_tointeger(L.s, C.int(index)))
}
func (L *State) ToNumber(index int) float64 {
return float64(C.lua_tonumber(L.s, C.int(index)))
}
func (L *State) ToPointer(index int) uintptr {
return uintptr(C.lua_topointer(L.s, C.int(index)))
}
func (L *State) ToThread(index int) *State {
//TODO: find a way to link lua_State* to existing *State, return that
return &State{}
}
func (L *State) ToUserdata(index int) unsafe.Pointer {
return unsafe.Pointer(C.lua_touserdata(L.s, C.int(index)))
}
func (L *State) ToLightInteger(index int) int {
return int(C.clua_tolightinteger(L.s, C.int(index)))
}
func (L *State) Type(index int) int {
return int(C.lua_type(L.s, C.int(index)))
}
func (L *State) Typename(tp int) string {
return C.GoString(C.lua_typename(L.s, C.int(tp)))
}
func XMove(from *State, to *State, n int) {
C.lua_xmove(from.s, to.s, C.int(n))
}
func (L *State) Yield(nresults int) int {
return int(C.lua_yield(L.s, C.int(nresults)))
}
// Restricted library opens
func (L *State) OpenBase() {
C.clua_openbase(L.s)
}
func (L *State) OpenIO() {
C.clua_openio(L.s)
}
func (L *State) OpenMath() {
C.clua_openmath(L.s)
}
func (L *State) OpenPackage() {
C.clua_openpackage(L.s)
}
func (L *State) OpenString() {
C.clua_openstring(L.s)
}
func (L *State) OpenTable() {
C.clua_opentable(L.s)
}
func (L *State) OpenOS() {
C.clua_openos(L.s)
}
func (L *State) SetExecutionLimit(instrNumber int) {
C.clua_setexecutionlimit(L.s, C.int(instrNumber))
}