本文整理匯總了Golang中C.lua_settop函數的典型用法代碼示例。如果您正苦於以下問題:Golang lua_settop函數的具體用法?Golang lua_settop怎麽用?Golang lua_settop使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了lua_settop函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: RegisterFunction
func (self *Lua) RegisterFunction(name string, fun interface{}) {
path := strings.Split(name, ".")
name = path[len(path)-1]
path = path[0 : len(path)-1]
if len(path) == 0 {
path = append(path, "_G")
}
// ensure namespaces
for i, namespace := range path {
cNamespace := cstr(namespace)
if i == 0 { // top namespace
what := C.lua_getglobal(self.State, cNamespace)
if what == C.LUA_TNIL { // not exists
C.lua_settop(self.State, -2)
C.lua_createtable(self.State, 0, 0)
C.lua_setglobal(self.State, cNamespace)
C.lua_getglobal(self.State, cNamespace)
}
if C.lua_type(self.State, -1) != C.LUA_TTABLE {
self.Panic("global %s is not a table", namespace)
}
} else { // sub namespace
C.lua_pushstring(self.State, cNamespace)
C.lua_rawget(self.State, -2)
if C.lua_type(self.State, -1) == C.LUA_TNIL {
C.lua_settop(self.State, -2)
C.lua_pushstring(self.State, cNamespace)
C.lua_createtable(self.State, 0, 0)
C.lua_rawset(self.State, -3)
C.lua_pushstring(self.State, cNamespace)
C.lua_rawget(self.State, -2)
}
if C.lua_type(self.State, -1) != C.LUA_TTABLE {
self.Panic("namespace %s is not a table", namespace)
}
}
}
// register function
funcType := reflect.TypeOf(fun)
if funcType.IsVariadic() {
self.Panic("cannot register variadic function: %v", fun)
}
argc := funcType.NumIn()
cName := cstr(name)
function := &Function{
fun: fun,
lua: self,
name: name,
funcType: funcType,
funcValue: reflect.ValueOf(fun),
argc: argc,
}
funcId := rand.Int63()
functionRegister.Set(funcId, function)
C.register_function(self.State, cName, (C.int64_t)(funcId))
self.Functions[name] = function
C.lua_settop(self.State, -2)
}
示例2: Foreach
func (tbl *Table) Foreach(fn func(key interface{}, value interface{}) bool) {
if tbl.Ref == 0 {
return
}
L := tbl.VM.globalL
state := State{tbl.VM, L}
bottom := C.lua_gettop(L)
defer C.lua_settop(L, bottom)
tbl.PushValue(state)
ltable := C.lua_gettop(L)
C.lua_pushnil(L)
for {
if 0 == C.lua_next(L, ltable) {
return
}
vkey, err := state.luaToGoValue(-2, nil)
if err != nil {
return
}
vvalue, err := state.luaToGoValue(-1, nil)
if err != nil {
return
}
cont := fn(vkey.Interface(), vvalue.Interface())
if !cont {
return
}
C.lua_settop(L, -2)
}
}
示例3: packLuaTable
func packLuaTable(out io.Writer, state State, object int, depth int) (n int, err error) {
depth++
L := state.L
if depth > MAX_PACK_DEPTH {
return 0, fmt.Errorf("pack too depth, depth=%v", depth)
}
n = 0
err = nil
var mapSize int = 0
C.lua_pushnil(L)
for {
if 0 == C.lua_next(L, C.int(object)) {
break
}
mapSize++
C.lua_settop(L, -2) // pop 1
}
var ni int
ni, err = P.PackMapHead(out, uint32(mapSize))
n += ni
if err != nil {
return
}
C.lua_pushnil(L)
for {
if 0 == C.lua_next(L, C.int(object)) {
break
}
top := int(C.lua_gettop(L))
// key
ni, err = packLuaObject(out, state, top-1, depth)
n += ni
if err != nil {
C.lua_settop(L, -3) // pop 2
return
}
// value
ni, err = packLuaObject(out, state, top, depth)
n += ni
if err != nil {
C.lua_settop(L, -3) // pop 2
return
}
C.lua_settop(L, -2) // removes value, keeps key for next iteration
}
return
}
示例4: luaTableToKeyValues
func (state *State) luaTableToKeyValues(ltable int) (value reflect.Value, err error) {
var vvalue reflect.Value
L := state.L
size := sizeOfLuaTable(L, ltable)
result := make([]base.KeyValue, 0, size)
C.lua_pushnil(L)
for {
if 0 == C.lua_next(L, C.int(ltable)) {
break
}
lvalue := int(C.lua_gettop(L))
lkey := lvalue - 1
vkey, err := state.luaToGoValue(lkey, nil)
if err != nil {
C.lua_settop(L, -3) // pop 2
break
}
if C.LUA_TTABLE == C.lua_type(L, C.int(lvalue)) {
vvalue, err = state.luaTableToKeyValues(lvalue)
} else {
vvalue, err = state.luaToGoValue(lvalue, nil)
}
if err != nil {
C.lua_settop(L, -3) // pop 2
break
}
key := vkey.Interface()
var skey string
if s, ok := key.(string); ok {
skey = s
} else {
skey = fmt.Sprint(key)
}
value := vvalue.Interface()
result = append(result, base.KeyValue{skey, value})
C.lua_settop(L, -2) // pop 1
}
return reflect.ValueOf(result), err
}
示例5: Register
// 注冊Go庫到lua
func (L *State) Register(lib *Libfuncs) (bool, error) {
libn := C.CString(lib.Libname)
defer C.free(unsafe.Pointer(libn))
// 獲取GlibTable
fsize := len(lib.Funcs)
if C.GetGlibTable(L.s, libn, C.int(fsize)) != 0 {
return false, errors.New(fmt.Sprintf("Lib name(%s) is wrong.\n", lib.Libname))
}
// 設置函數
for k, v := range lib.Funcs {
// 檢查函數列表
if ok, err := checkFuncInOutArgs(v); !ok {
log.Println(err)
continue
}
// 保存到State
L.lf = append(L.lf, v)
idx := len(L.lf)
kn := C.CString(k)
// 設置index到GlibTtable
C.SetGfunc(L.s, kn, C.int(idx-1))
C.free(unsafe.Pointer(kn))
}
C.lua_settop(L.s, 0)
return true, nil
}
示例6: Call
// 調用lua中的函數,但是隻能返回bool, float, string,以及其他go特殊類型,int型被轉換為float返回。
func (L *State) Call(fname string, args ...interface{}) (out []interface{}, ok bool) {
fn := C.CString(fname)
defer C.free(unsafe.Pointer(fn))
top := int(C.lua_gettop(L.s))
if C.int(1) != C.FindFuncs(L.s, fn) {
ok = false
out = append(out, errors.New(fmt.Sprintf("not find the function(%s).\n", fname)))
return
}
num := len(args)
for _, arg := range args {
argt := rf.TypeOf(arg)
argv := rf.ValueOf(arg)
L.pushValueByType(argt.Kind(), &argv)
}
C.lua_call(L.s, C.int(num), C.LUA_MULTRET)
for i := top; i < int(C.lua_gettop(L.s)); i++ {
ret := L.getValueByLuaType(i)
if ret.IsValid() {
out = append(out, ret.Interface())
} else {
out = append(out, nil)
}
}
C.lua_settop(L.s, C.int(top))
ok = true
return
}
示例7: GetWithError
func (tbl *Table) GetWithError(key interface{}) (interface{}, error) {
if tbl.Ref == 0 {
return nil, fmt.Errorf("cannot get a released lua table")
}
L := tbl.VM.globalL
state := State{tbl.VM, L}
bottom := C.lua_gettop(L)
defer C.lua_settop(L, bottom)
tbl.PushValue(state)
vkey := reflect.ValueOf(key)
ok := state.goToLuaValue(vkey)
if !ok {
return nil, fmt.Errorf("invalid key type for lua type: %v", vkey.Kind())
}
C.lua_gettable(L, C.int(-2))
vvalue, err := state.luaToGoValue(-1, nil)
if err != nil {
return nil, err
}
if vvalue.IsValid() {
return vvalue.Interface(), nil
}
return nil, nil
}
示例8: Peval
// 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
}
示例9: callLuaFuncUtil
func callLuaFuncUtil(state State, inv []reflect.Value, nout int) ([]interface{}, error) {
L := state.L
bottom := int(C.lua_gettop(L))
var result []interface{}
var nluaout C.int
var nin C.int
if nout >= 0 {
nluaout = C.int(nout)
result = make([]interface{}, 0, nout)
} else {
nluaout = C.LUA_MULTRET
result = make([]interface{}, 0, 1)
}
if inv != nil {
for _, iarg := range inv {
state.goToLuaValue(iarg)
}
nin = C.int(len(inv))
} else {
nin = 0
}
ret := int(C.lua_pcall(L, nin, nluaout, 0))
if ret != 0 {
err := stringFromLua(L, -1)
C.lua_settop(L, -2)
return result, errors.New(err)
}
top := int(C.lua_gettop(L))
for i := bottom; i <= top; i++ {
value, _ := state.luaToGoValue(i, nil)
if value.IsValid() {
result = append(result, value.Interface())
} else {
result = append(result, nil)
}
}
rnout := C.int(top + 1 - bottom)
C.lua_settop(L, -rnout-1)
return result, nil
}
示例10: sizeOfLuaTable
func sizeOfLuaTable(L *C.lua_State, ltable int) int {
var size int = 0
C.lua_pushnil(L)
for {
if 0 == C.lua_next(L, C.int(ltable)) {
break
}
size++
C.lua_settop(L, -2) // pop 1
}
return size
}
示例11: GetnWithError
func (tbl *Table) GetnWithError() (int, error) {
if tbl.Ref == 0 {
return 0, fmt.Errorf("cannot get lenght a released lua table")
}
L := tbl.VM.globalL
state := State{tbl.VM, L}
bottom := int(C.lua_gettop(L))
defer C.lua_settop(L, C.int(bottom))
tbl.PushValue(state)
n := int(C.lua_objlen(L, C.int(-1)))
return n, nil
}
示例12: PackLuaObjects
func PackLuaObjects(out io.Writer, state State, from int, to int) (ok bool, err error) {
L := state.L
top := C.lua_gettop(L)
ok = true
err = nil
for object := from; object <= to; object++ {
_, err = PackLuaObject(out, state, object)
if err != nil {
ok = false
break
}
}
C.lua_settop(L, top)
return ok, err
}
示例13: luaGetSubTable
func luaGetSubTable(L *C.lua_State, table C.int, key string) (bool, error) {
pushStringToLua(L, key)
C.lua_gettable(L, table)
ltype := C.lua_type(L, -1)
if ltype == C.LUA_TNIL {
C.lua_createtable(L, 0, 0)
// table[key] = {}
pushStringToLua(L, key)
C.lua_pushvalue(L, -2)
C.lua_settable(L, table)
}
ltype = C.lua_type(L, -1)
if ltype != C.LUA_TTABLE {
C.lua_settop(L, -2)
return false, fmt.Errorf("field `%v` exist, and it is not a table", key)
}
return true, nil
}
示例14: set
func (l *Lua) set(fullname string, v interface{}) error {
// ensure name
errMsg := C.ensure_name(l.State, C.CString(fullname))
if errMsg != nil {
return fmt.Errorf("%s: %s", errMsg, fullname)
}
// push value
err := l.pushGoValue(v, fullname)
if err != nil {
return err
}
// set
C.lua_rawset(l.State, -3)
// clear stack
C.lua_settop(l.State, 0)
return nil
}
示例15: EvalStringWithError
func (vm *VM) EvalStringWithError(str string, arg ...interface{}) ([]interface{}, error) {
L := vm.globalL
state := State{vm, L}
s, n := stringToC(str)
bottom := C.lua_gettop(L)
defer C.lua_settop(L, bottom)
ret := int(C.luaL_loadbuffer(L, s, n, nil))
if ret != 0 {
err := stringFromLua(L, -1)
return make([]interface{}, 0), errors.New(err)
}
nout := -1
if len(arg) > 0 {
if x, ok := arg[0].(int); ok {
nout = x
}
}
return callLuaFuncUtil(state, nil, nout)
}