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 }
// Loads a string as a Lua chunk. // // This function only loads the chunk; it does not run it. func (this *State) Loadstring(str string) error { cs := C.CString(str) defer C.free(unsafe.Pointer(cs)) r := int(C.luaL_loadstring(this.luastate, cs)) return this.geterror(r) }
// DoString ... func (state *LuaState) DoString(script string) (string, error) { code := C.CString(script) defer C.free(unsafe.Pointer(code)) C.luaL_loadstring(state.lua, code) C.lua_pcallk(state.lua, 0, LuaMultRet, 0, 0, nil) return "", nil }
// 执行lua脚本字符串 func (L *State) Dostring(str string) (ok bool) { cs := C.CString(str) defer C.free(unsafe.Pointer(cs)) if C.luaL_loadstring(L.s, cs) == 0 { return C.lua_pcall(L.s, 0, C.LUA_MULTRET, 0) == 0 } return false }
// Peval evaluates a piece of lua code. no panic when error occur. func (l *Lua) Peval(code string, envs ...interface{}) (returns []interface{}, err error) { defer C.lua_settop(l.State, 0) C.push_errfunc(l.State) curTop := C.lua_gettop(l.State) // parse cCode := C.CString(code) defer C.free(unsafe.Pointer(cCode)) if ret := C.luaL_loadstring(l.State, cCode); ret != 0 { // load error return nil, fmt.Errorf("LOAD ERROR: %s", C.GoString(C.lua_tolstring(l.State, -1, nil))) } // env if len(envs) > 0 { if len(envs)%2 != 0 { return nil, fmt.Errorf("number of arguments not match") } C.lua_createtable(l.State, 0, 0) for i := 0; i < len(envs); i += 2 { name, ok := envs[i].(string) if !ok { return nil, fmt.Errorf("name must be string, not %v", envs[i]) } C.lua_pushstring(l.State, cstr(name)) err := l.pushGoValue(envs[i+1], name) if err != nil { return nil, err } C.lua_rawset(l.State, -3) } // set env C.set_eval_env(l.State) } // call l.err = nil if ret := C.lua_pcall(l.State, 0, C.LUA_MULTRET, -2); ret != 0 { // error occured return nil, fmt.Errorf("CALL ERROR: %s", C.GoString(C.lua_tolstring(l.State, -1, nil))) } else if l.err != nil { // error raise by invokeGoFunc return nil, l.err } else { // return values nReturn := C.lua_gettop(l.State) - curTop returns = make([]interface{}, int(nReturn)) for i := C.int(0); i < nReturn; i++ { value, err := l.toGoValue(-1-i, interfaceType) if err != nil { return nil, err } if value != nil { returns[int(nReturn-1-i)] = value.Interface() } else { returns[int(nReturn-1-i)] = nil } } } 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 }
func (self *Lua) RunString(code string) { defer func() { if r := recover(); r != nil { if self.PrintTraceback { //NOCOVER print("============ start lua traceback ============\n") self.RunString(`print(debug.traceback())`) print("============ end lua traceback ==============\n") } panic(r) } }() cCode := cstr(code) C.setup_message_handler(self.State) if ret := C.luaL_loadstring(self.State, cCode); ret != C.int(0) { self.Panic("%s", C.GoString(C.lua_tolstring(self.State, -1, nil))) } ret := C.lua_pcallk(self.State, 0, 0, C.lua_gettop(self.State)-C.int(1), 0, nil) if ret != C.int(0) { self.Panic("%s", C.GoString(C.lua_tolstring(self.State, -1, nil))) } C.lua_settop(self.State, 0) }
// luaL_loadstring func (L *State) LoadString(s string) int { Cs := C.CString(s) defer C.free(unsafe.Pointer(Cs)) return int(C.luaL_loadstring(L.s, Cs)) }
// Loads a string as a Lua chunk. // // This function only loads the chunk; it does not run it. func (s *State) Loadstring(str string) error { cs := C.CString(str) defer C.free(unsafe.Pointer(cs)) r := int(C.luaL_loadstring(s.l, cs)) return numtoerror(r) }
func (L *State) LoadString(s string) int { return int(C.luaL_loadstring(L.s, C.CString(s))) }
func Loadstring(s *State, str string) int { // Create a c string cstr := C.CString(str) defer C.free(unsafe.Pointer(cstr)) return int(C.luaL_loadstring((*C.lua_State)(s), cstr)) }