Beispiel #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
}
Beispiel #2
0
Datei: lgo.go Projekt: 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
}
Beispiel #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
}
Beispiel #4
0
// luaL_openlibs
func (L *State) OpenLibs() {
	C.luaL_openlibs(L.s)
	C.clua_hide_pcall(L.s)
}
Beispiel #5
0
// 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)
}
Beispiel #6
0
// Opens all standard Lua libraries into the given state.
func (s *State) Openlibs() {
	C.luaL_openlibs(s.l)
}
Beispiel #7
0
// NewLuaState ...
func NewLuaState() *LuaState {
	lua := C.luaL_newstate()
	C.luaL_openlibs(lua)
	return &LuaState{lua}
}
Beispiel #8
0
func (L *State) OpenLibs() {
	C.luaL_openlibs(L.s)
}
Beispiel #9
0
func (vm *VM) Openlibs() {
	C.luaL_openlibs(vm.globalL)
	vm.initLuaLib()
}
Beispiel #10
0
Datei: core.go Projekt: vron/lua
func Openlibs(s *State) {
	C.luaL_openlibs((*C.lua_State)(s))
}