func main() { L := C.luaL_newstate() defer C.lua_close(L) C.luaL_openlibs(L) /* Load Lua libraries */ C.luaopen_cmsgpack(L) sumS := C.CString(sum) defer C.free(unsafe.Pointer(sumS)) C.luaL_loadstring(L, sumS) msg, _ := msgpack.Marshal([]int{1, 2, 3, 4, 5, 6}) C.lua_pushstring(L, cptr(msg)) dstr := C.CString("mpdata") defer C.free(unsafe.Pointer(dstr)) C.luaSetglobal(L, dstr) C.luaExecute(L) sum := C.lua_tonumber(L, -1) fmt.Println(sum) // Output: 21 }
func NewLua() *Lua { state := C.luaL_newstate() if state == nil { //NOCOVER panic("lua state create error") } C.luaL_openlibs(state) lua := &Lua{ State: state, Functions: make(map[string]*Function), PrintTraceback: true, } return lua }
// Initializes the Lua context and compiles the source code. func (e *ExecutionEngine) init() error { if e.state != nil { return nil } // Initialize the state and open the libraries. e.state = C.luaL_newstate() if e.state == nil { return errors.New("Unable to initialize Lua context.") } C.luaL_openlibs(e.state) // Generate the header file. err := e.generateHeader() if err != nil { e.Destroy() return err } // Compile the script. e.fullSource = fmt.Sprintf("%v\n%v", e.header, e.source) source := C.CString(e.fullSource) defer C.free(unsafe.Pointer(source)) ret := C.luaL_loadstring(e.state, source) if ret != 0 { defer e.Destroy() errstring := C.GoString(C.lua_tolstring(e.state, -1, nil)) return fmt.Errorf("skyd.ExecutionEngine: Syntax Error: %v", errstring) } // Run script once to initialize. ret = C.lua_pcall(e.state, 0, 0, 0) if ret != 0 { defer e.Destroy() errstring := C.GoString(C.lua_tolstring(e.state, -1, nil)) return fmt.Errorf("skyd.ExecutionEngine: Init Error: %v", errstring) } // Setup cursor. err = e.initCursor() if err != nil { e.Destroy() return err } return nil }
// luaL_openlibs func (L *State) OpenLibs() { C.luaL_openlibs(L.s) C.clua_hide_pcall(L.s) }
// Openlibs Opens all standard Lua libraries into the given state. // http://www.lua.org/manual/5.1/manual.html#luaL_openlibs func (this *State) Openlibs() { C.luaL_openlibs(this.luastate) }
// Opens all standard Lua libraries into the given state. func (s *State) Openlibs() { C.luaL_openlibs(s.l) }
// NewLuaState ... func NewLuaState() *LuaState { lua := C.luaL_newstate() C.luaL_openlibs(lua) return &LuaState{lua} }
func (L *State) OpenLibs() { C.luaL_openlibs(L.s) }
func (vm *VM) Openlibs() { C.luaL_openlibs(vm.globalL) vm.initLuaLib() }
func Openlibs(s *State) { C.luaL_openlibs((*C.lua_State)(s)) }