//go:build puc package main /* #cgo LDFLAGS: -llua #include #include #include #include #include extern int luna_run_go_func(uintptr_t); static inline int luna_run(lua_State *l) { return luna_run_go_func(lua_tonumber(l, lua_upvalueindex(1))); } static inline void luna_push_function(lua_State *l, uintptr_t f) { lua_pushinteger(l, f); lua_pushcclosure(l, luna_run, 1); } */ import "C" import ( "errors" "runtime/cgo" "unsafe" ) type LuaRef C.int // Lua is a wrapper around C Lua state with several conveniences. type Lua struct { l *C.lua_State running **Lua yield func() resume func() bool deferred func() } //export luna_run_go_func // luna_run_go_func calls a Go function from C/Lua. func luna_run_go_func(f C.uintptr_t) C.int { fn := cgo.Handle(f).Value().(func() int) return C.int(fn()) } // Start opens the Lua context with all built-in libraries. func (l *Lua) Start() error { if l.l != nil { return errors.New("already started") } l.l = C.luaL_newstate() l.running = &l l.yield = nil l.resume = nil C.luaL_openlibs(l.l) // Put traceback function at the start of the stack so it's always // possible to refer to it via stack index 1. l.PushGoFunction(TracebackHandler) return nil } // Close closes the Lua context. func (l *Lua) Close() { C.lua_close(l.l) l.l = nil } // PCall calls a function with the stack index (-nargs-1) expecting nresults // number of results. func (l *Lua) PCall(nargs int, nresults int, errfunc int) error { *l.running = l res := C.lua_pcallk( l.l, C.int(nargs), C.int(nresults), C.int(errfunc), 0, nil, ) if res != C.LUA_OK { errMsg := l.ToString(-1) l.Pop(1) l.deferred = nil return errors.New(errMsg) } if l.deferred != nil { l.deferred() l.deferred = nil } return nil } // LoadFile loads the file code in the current Lua context. func (l *Lua) LoadFile(file string) error { cstr := C.CString(file) defer C.free(unsafe.Pointer(cstr)) res := C.luaL_loadfilex(l.l, cstr, nil) if res != C.LUA_OK { errMsg := l.ToString(-1) l.Pop(1) return errors.New(errMsg) } return nil } // StackLen returns the length of the Lua stack. func (l *Lua) StackLen() int { return int(C.lua_gettop(l.l)) } // ToString converts a stack value with stack index into a string. func (l *Lua) ToString(index int) string { return C.GoString(C.lua_tolstring(l.l, C.int(index), nil)) } // ToInt converts a stack value with stack index -1 into a number. func (l *Lua) ToInt(index int) int { return int(C.lua_tonumberx(l.l, C.int(index), nil)) } // ToBoolean converts a stack value with stack index -1 into a boolean. func (l *Lua) ToBoolean(index int) bool { return int(C.lua_toboolean(l.l, C.int(index))) == 1 } // PopToRef pops the item (stack offset -1) from the stack and then stores it // in the registry. func (l *Lua) PopToRef() LuaRef { return LuaRef(C.luaL_ref(l.l, C.LUA_REGISTRYINDEX)) } // Pop pops N items from the Lua stack. func (l *Lua) Pop(n int) { C.lua_settop(l.l, C.int(-n - 1)) } // SetTop sets head of the stack at top. func (l *Lua) SetTop(top int) { C.lua_settop(l.l, C.int(top)) } // PushNil pushes tnil onto the Lua stack. func (l *Lua) PushNil() { C.lua_pushnil(l.l) } // PushBoolean pushes a boolean onto the Lua stack. func (l *Lua) PushBoolean(b bool) { // For some reason this is needed here to not mess up threads. *l.running = l if b { C.lua_pushboolean(l.l, 1) } else { C.lua_pushboolean(l.l, 0) } } // PushNumber pushes a number onto the Lua stack. func (l *Lua) PushNumber(num int) { C.lua_pushnumber(l.l, C.double(num)) } // PushFloatNumber pushes a float number onto the Lua stack. func (l *Lua) PushFloatNumber(num float64) { C.lua_pushnumber(l.l, C.double(num)) } // PushString pushes the string onto the Lua stack. func (l *Lua) PushString(str string) { cstr := C.CString(str) defer C.free(unsafe.Pointer(cstr)) C.lua_pushstring(l.l, cstr) } // PushGoFunction pushes a Go function onto the Lua stack so it's possible to // call it from Lua. You may need to delete returned handle with .Delete() // method later. func (l *Lua) PushGoFunction(f func (l *Lua) int) cgo.Handle { h := cgo.NewHandle(func () int { return f(*l.running) }) C.luna_push_function(l.l, C.uintptr_t(h)) return h } // CreateTable pushes a new Lua table onto the stack. func (l *Lua) CreateTable(len int) { C.lua_createtable(l.l, 0, C.int(len)) } // SetTableItem adds a value (stack offset -1) to a table (offset -2) under // the specified key. func (l *Lua) SetTableItem(key string) { cstr := C.CString(key) defer C.free(unsafe.Pointer(cstr)) C.lua_setfield(l.l, -2, C.CString(key)) } // GetTableItem gets a value from the table (offset -1) under the specified key // and puts it onto the stack. func (l *Lua) GetTableItem(key string) { cstr := C.CString(key) defer C.free(unsafe.Pointer(cstr)) C.lua_getfield(l.l, -1, C.CString(key)) } // RawGetI pushes onto the stack value from table from tableOffset at index i. func (l *Lua) RawGetI(tableOffset, i int) { C.lua_rawgeti(l.l, C.int(tableOffset), C.longlong(i)) } // PushArray recursively pushes an array of Go values onto the stack. func (l *Lua) PushArray(array []any) error { l.CreateTable(len(array)) for k, v := range array { l.PushNumber(k + 1) err := l.PushAny(v) if err != nil { return err } C.lua_settable(l.l, C.int(-3)) } return nil } // PushFromRef pushes a value from registry ref onto the stack. func (l *Lua) PushFromRef(ref LuaRef) { C.lua_rawgeti(l.l, C.LUA_REGISTRYINDEX, C.longlong(ref)); } // Unref removes the ref from the registry. func (l *Lua) Unref(ref LuaRef) { C.luaL_unref(l.l, C.LUA_REGISTRYINDEX, C.int(ref)) } // Type returns type of the value sitting at n index on the stack. func (l *Lua) Type(n int) int { return int(C.lua_type(l.l, C.int(n))) } // IsNil checks if the stack contains nil under the given index. func (l *Lua) IsNil(index int) bool { return C.lua_type(l.l, C.int(index)) == C.LUA_TNIL } // IsString checks if the stack contains a string under the given index. func (l *Lua) IsString(index int) bool { return C.lua_isstring(l.l, C.int(index)) == 1 } // IsNumber checks if the stack contains a number (both int and float) under // the given index. func (l *Lua) IsNumber(index int) bool { return C.lua_isnumber(l.l, C.int(index)) == 1 } // IsBoolean checks if the stack contains a boolean under the given index. func (l *Lua) IsBoolean(index int) bool { return C.lua_type(l.l, C.int(index)) == C.LUA_TBOOLEAN } // IsFunction checks if the stack contains a function under the given index. func (l *Lua) IsFunction(index int) bool { return C.lua_type(l.l, C.int(index)) == C.LUA_TFUNCTION } // IsTable checks if the stack contains a table under the given index. func (l *Lua) IsTable(index int) bool { return C.lua_type(l.l, C.int(index)) == C.LUA_TTABLE } // Next advances a table iterator at the stack index -2. // IsTable checks if the stack contains a table under the given index. func (l *Lua) Next() bool { return C.lua_next(l.l, -2) != 0 } // LoadAndCall loads and calls the code in the current Lua context. Whatever // the code returns is stored in the stack. To drop the results use l.Pop. func (l *Lua) LoadAndCall(code string) error { cstr := C.CString(code) defer C.free(unsafe.Pointer(cstr)) if C.luaL_loadstring(l.l, cstr) != C.LUA_OK { errMsg := l.ToString(-1) l.Pop(1) return errors.New(errMsg) } return l.PCall(0, C.LUA_MULTRET, 1) } // SetGlobal sets a global value at the -1 stack index with the name. func (l *Lua) SetGlobal(name string) { cstr := C.CString(name) defer C.free(unsafe.Pointer(cstr)) C.lua_setglobal(l.l, cstr) } // GetGlobal gets a global value under the name. func (l *Lua) GetGlobal(name string) { cstr := C.CString(name) defer C.free(unsafe.Pointer(cstr)) C.lua_getglobal(l.l, cstr) } func (l *Lua) NewThread(yield func(), resume func() bool) *Lua { newl := &Lua{ l: C.lua_newthread(l.l), running: l.running, resume: resume, yield: yield, } newl.PushGoFunction(TracebackHandler) return newl } // TracebackHandler handles stack trace formatting on errors. func TracebackHandler(l *Lua) int { msg := l.ToString(-1) l.GetGlobal("debug") l.GetTableItem("traceback") l.PushString(msg) C.lua_callk(l.l, 1, 1, 0, nil) return 1 }