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 NewVM() *VM { L := C.luaL_newstate() C.clua_initState(L) vm := &VM{globalL: L} vm.structTbl = make(map[reflect.Type]*structInfo) return vm }
// NewState Creates a new Lua state. It calls luaL_newstate which calls lua_newstate with an allocator based // on the standard C realloc function and then sets a panic function (see lua_atpanic) // that prints an error message to the standard error output in case of fatal errors. // TODO: Handle NULL return of luaL_newstate and error appropriately // TODO: Set panic function for lua_atpanic func Newstate() *State { state := &State{ luastate: C.luaL_newstate(), } state.Init() return state }
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 }
// NewState Creates a new Lua state. It calls luaL_newstate which calls lua_newstate with an allocator based // on the standard C realloc function and then sets a panic function (see lua_atpanic) // that prints an error message to the standard error output in case of fatal errors. // TODO: Handle NULL return of luaL_newstate and error appropriately // TODO: Set panic function for lua_atpanic func Newstate() *State { grindex := lua.GlobalRegistry.ReserveValue() state := &State{ luastate: C.luaL_newstate(), grindex: grindex, } lua.GlobalRegistry.SetValue(grindex, state) C.lua51_luainit(state.luastate) return state }
// 创建新的状态 func NewState() (L *State) { L = new(State) L.s = C.luaL_newstate() //L.lf = make([]interface{}, 5) // 保存State到lua栈中 var li interface{} = L C.SetGoState(L.s, unsafe.Pointer(&li)) // 生成metatable,用于回调Go函数 C.InitMetaTable(L.s) // 注册回收 runtime.SetFinalizer(L, (*State).free) log.Printf("Create %v.\n", *L) return }
// 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_newstate func NewState() *State { ls := (C.luaL_newstate()) L := newState(ls) return L }
func NewState() *State { return &State{C.luaL_newstate()} }
// NewLuaState ... func NewLuaState() *LuaState { lua := C.luaL_newstate() C.luaL_openlibs(lua) return &LuaState{lua} }
func Newstate() *State { return (*State)(C.luaL_newstate()) }