本文整理匯總了Golang中C.lua_type函數的典型用法代碼示例。如果您正苦於以下問題:Golang lua_type函數的具體用法?Golang lua_type怎麽用?Golang lua_type使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了lua_type函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GO_newindexObject
//export GO_newindexObject
func GO_newindexObject(_L unsafe.Pointer, ref unsafe.Pointer, lkey C.int, lvalue C.int) (ret int) {
L := (*C.lua_State)(_L)
node := (*refGo)(ref)
vm := node.vm
state := State{vm, L}
v := reflect.ValueOf(node.obj)
t := v.Type()
k := v.Kind()
defer func() {
if r := recover(); r != nil {
pushStringToLua(L, fmt.Sprintf("%v", r))
ret = -1
}
}()
ltype := C.lua_type(L, lkey)
switch k {
case reflect.Slice:
if ltype == C.LUA_TNUMBER {
tElem := t.Elem()
value, err := state.luaToGoValue(int(lvalue), &tElem)
if err != nil {
panic(fmt.Sprintf("error when assign to slice member, %s", err.Error()))
}
idx := int(C.lua_tointeger(L, lkey))
v.Index(idx).Set(value)
return 0
}
panic(fmt.Sprintf("index of slice must be a number type, got `%v'", luaTypeName(ltype)))
case reflect.Map:
keyType := t.Key()
key, err := state.luaToGoValue(int(lkey), &keyType)
if err != nil {
panic(fmt.Sprintf("index type of map must be type `%v', %s", keyType.Kind(), err.Error()))
}
if lvtype := C.lua_type(L, lvalue); lvtype == C.LUA_TNIL {
v.SetMapIndex(key, reflect.Value{})
} else {
tElem := t.Elem()
value, err := state.luaToGoValue(int(lvalue), &tElem)
if err != nil {
panic(fmt.Sprintf("error when assign to map member, %s", err.Error()))
}
v.SetMapIndex(key, value)
}
return 0
case reflect.Ptr:
if t.Elem().Kind() == reflect.Struct {
_, err := state.setStructField(v, lkey, lvalue)
if err != nil {
panic(fmt.Sprintf("error when set field of struct, %s", err.Error()))
}
return 0
}
}
panic(fmt.Sprintf("try to assign a non-indexable go object, type `%v'", k))
return -1
}
示例2: 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)
}
示例3: packLuaObject
func packLuaObject(out io.Writer, state State, object int, depth int) (n int, err error) {
L := state.L
ltype := C.lua_type(L, C.int(object))
switch ltype {
case C.LUA_TNUMBER:
n, err = packLuaNumber(out, state, object)
case C.LUA_TBOOLEAN:
n, err = packLuaBoolean(out, state, object)
case C.LUA_TSTRING:
n, err = packLuaString(out, state, object)
case C.LUA_TNIL:
n, err = packLuaNil(out, state, object)
case C.LUA_TTABLE:
n, err = packLuaTable(out, state, object, depth)
case C.LUA_TUSERDATA:
fallthrough
case C.LUA_TTHREAD:
fallthrough
case C.LUA_TLIGHTUSERDATA:
fallthrough
default:
typeName := luaTypeName(ltype)
return n, fmt.Errorf("cannot pack lua type `%v'", typeName)
}
return n, err
}
示例4: setStructField
func (state State) setStructField(structPtr reflect.Value, lkey C.int, lvalue C.int) (ret int, err error) {
L := state.L
vm := state.VM
structValue := structPtr.Elem()
t := structValue.Type()
info := vm.findStruct(t)
if info == nil {
return -1, fmt.Errorf("can not assign field of a solid struct")
}
ltype := int(C.lua_type(L, lkey))
if ltype != C.LUA_TSTRING {
return -1, fmt.Errorf("field key of struct must be a string")
}
key := stringFromLua(L, lkey)
fld, ok := info.fields[key]
if !ok {
return -1, fmt.Errorf("not such field `%v'", key)
}
if fld.typ != DATA_FIELD {
return -1, fmt.Errorf("only data field is assignble, but `%v' is not !", key)
}
sf := t.FieldByIndex(fld.dataIndex) // StructField
value, err := state.luaToGoValue(int(lvalue), &sf.Type)
if err != nil {
return -1, err
}
structValue.FieldByIndex(fld.dataIndex).Set(value)
return 0, nil
}
示例5: getStructField
func (state State) getStructField(structPtr reflect.Value, lkey C.int) (ret int, err error) {
L := state.L
vm := state.VM
structValue := structPtr.Elem()
t := structValue.Type()
info := vm.findStruct(t)
if info == nil {
return -1, fmt.Errorf("can not index a solid struct")
}
ltype := int(C.lua_type(L, lkey))
if ltype != C.LUA_TSTRING {
return -1, fmt.Errorf("field key of struct must be a string")
}
//
//key := stringFromLua(L, lkey)
//fld, ok := info.fields[key]
//
// <hack> using string pstr cache
pstr := C.lua_tolstring(L, lkey, nil)
// </hack>
fld, ok := info.cache[pstr]
if !ok {
key := stringFromLua(L, lkey)
return -1, fmt.Errorf("not such field `%v'", key)
}
value := getStructFieldValue(structValue, fld)
state.goToLuaValue(value)
return 1, nil
}
示例6: Pcall
// 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
}
示例7: 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
}
示例8: GO_indexObject
//export GO_indexObject
func GO_indexObject(_L unsafe.Pointer, ref unsafe.Pointer, lkey C.int) (ret int) {
L := (*C.lua_State)(_L)
node := (*refGo)(ref)
vm := node.vm
state := State{vm, L}
v := reflect.ValueOf(node.obj)
t := v.Type()
k := v.Kind()
defer func() {
if r := recover(); r != nil {
pushStringToLua(L, fmt.Sprintf("%v", r))
ret = -1
}
}()
ltype := C.lua_type(L, lkey)
switch k {
case reflect.Slice:
if ltype == C.LUA_TNUMBER {
idx := int(C.lua_tointeger(L, lkey))
value := v.Index(idx)
state.goToLuaValue(value)
return 1
}
panic(fmt.Sprintf("index of slice must be a number type, here got `%v'", luaTypeName(ltype)))
case reflect.Map:
keyType := t.Key()
key, err := state.luaToGoValue(int(lkey), &keyType)
if err != nil {
panic(fmt.Sprintf("index type of map must be type `%v', %s", keyType.Kind(), err.Error()))
}
value := v.MapIndex(key)
if !value.IsValid() {
C.lua_pushnil(L)
return 1
}
state.goToLuaValue(value)
return 1
case reflect.Ptr:
if t.Elem().Kind() == reflect.Struct {
ret, err := state.getStructField(v, lkey)
if err != nil {
panic(fmt.Sprintf("error when get field of struct, %s", err.Error()))
}
return ret
}
}
panic(fmt.Sprintf("try to index a non-indexable go object, type `%v'", k))
return -1
}
示例9: 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
}
示例10: mustBeMap
func mustBeMap(state State, lvalue int) *reflect.Value {
var vmap *reflect.Value
L := state.L
ltype := C.lua_type(L, C.int(lvalue))
if ltype == C.LUA_TUSERDATA {
ref := C.clua_getGoRef(L, C.int(lvalue))
if ref != nil {
obj := (*refGo)(ref).obj
objValue := reflect.ValueOf(obj)
if objValue.Kind() == reflect.Map {
vmap = &objValue
}
}
}
return vmap
}
示例11: getValueByLuaType
func (L *State) getValueByLuaType(i int) (v *rf.Value) {
switch C.lua_type(L.s, C.int(i+1)) {
case C.LUA_TBOOLEAN:
v = L.getValueByType(rf.Bool, i)
case C.LUA_TNUMBER:
v = L.getValueByType(rf.Float64, i)
case C.LUA_TSTRING:
v = L.getValueByType(rf.String, i)
case C.LUA_TUSERDATA:
v = L.getValueByType(rf.Interface, i)
default:
L.pushString("Wrong parameters.")
C.lua_error(L.s)
v = nil
}
return
}
示例12: IsThread
// lua_isthread
func (L *State) IsThread(index int) bool {
return LuaValType(C.lua_type(L.s, C.int(index))) == LUA_TTHREAD
}
示例13: IsTable
// lua_istable
func (L *State) IsTable(index int) bool {
return LuaValType(C.lua_type(L.s, C.int(index))) == LUA_TTABLE
}
示例14: IsNoneOrNil
// lua_isnoneornil
func (L *State) IsNoneOrNil(index int) bool { return int(C.lua_type(L.s, C.int(index))) <= 0 }
示例15: IsNone
// lua_isnone
func (L *State) IsNone(index int) bool { return LuaValType(C.lua_type(L.s, C.int(index))) == LUA_TNONE }