示例#1
0
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
}
示例#2
0
文件: lgo.go 项目: reusee/lgo
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
}
示例#3
0
// 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
}
示例#4
0
文件: lauxlib.go 项目: 1lann/golua
// luaL_openlibs
func (L *State) OpenLibs() {
	C.luaL_openlibs(L.s)
	C.clua_hide_pcall(L.s)
}
示例#5
0
文件: State.go 项目: rdlaitila/leaf
// 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)
}
示例#6
0
文件: state.go 项目: halturin/luajit
// Opens all standard Lua libraries into the given state.
func (s *State) Openlibs() {
	C.luaL_openlibs(s.l)
}
示例#7
0
// NewLuaState ...
func NewLuaState() *LuaState {
	lua := C.luaL_newstate()
	C.luaL_openlibs(lua)
	return &LuaState{lua}
}
示例#8
0
func (L *State) OpenLibs() {
	C.luaL_openlibs(L.s)
}
示例#9
0
文件: lua.go 项目: hxyxj/goinfi
func (vm *VM) Openlibs() {
	C.luaL_openlibs(vm.globalL)
	vm.initLuaLib()
}
示例#10
0
文件: core.go 项目: vron/lua
func Openlibs(s *State) {
	C.luaL_openlibs((*C.lua_State)(s))
}