// Pcall calls a lua function. no panic func (l *Lua) Pcall(fullname string, args ...interface{}) (returns []interface{}, err error) { C.push_errfunc(l.State) curTop := C.lua_gettop(l.State) // get function path := strings.Split(fullname, ".") for i, name := range path { if i == 0 { C.lua_getfield(l.State, C.LUA_GLOBALSINDEX, cstr(name)) } else { if C.lua_type(l.State, -1) != C.LUA_TTABLE { return nil, fmt.Errorf("%s is not a function", fullname) } C.lua_pushstring(l.State, cstr(name)) C.lua_gettable(l.State, -2) C.lua_remove(l.State, -2) // remove table } } if C.lua_type(l.State, -1) != C.LUA_TFUNCTION { return nil, fmt.Errorf("%s is not a function", fullname) } // args for _, arg := range args { l.pushGoValue(arg, "") } // call l.err = nil if ret := C.lua_pcall(l.State, C.int(len(args)), C.LUA_MULTRET, C.int(-(len(args))-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 } returns[int(nReturn-1-i)] = value.Interface() } } return }
// Encodes a Go object into Msgpack and adds it to the function arguments. func (e *ExecutionEngine) encodeArgument(value interface{}) error { // Encode Go object into msgpack. buffer := new(bytes.Buffer) encoder := msgpack.NewEncoder(buffer) err := encoder.Encode(value) if err != nil { return err } // Push the msgpack data onto the Lua stack. data := buffer.String() cdata := C.CString(data) defer C.free(unsafe.Pointer(cdata)) C.lua_pushlstring(e.state, cdata, (C.size_t)(len(data))) // Convert the argument from msgpack into Lua. rc := C.mp_unpack(e.state) if rc != 1 { return errors.New("skyd.ExecutionEngine: Unable to msgpack encode Lua argument") } C.lua_remove(e.state, -2) return nil }
// lua_remove func (L *State) Remove(index int) { C.lua_remove(L.s, C.int(index)) }
// Removes the element at the given valid index, shifting down the elements // above this index to fill the gap. Cannot be called with a pseudo-index, // because a pseudo-index is not an actual stack position. func (this *State) Remove(index int) { C.lua_remove(this.luastate, C.int(index)) }
// Removes the element at the given valid index, shifting down the elements // above this index to fill the gap. Cannot be called with a pseudo-index, // because a pseudo-index is not an actual stack position. func (s *State) Remove(index int) { C.lua_remove(s.l, C.int(index)) }