本文整理汇总了Golang中github.com/aarzilli/golua/lua.State.ToNumber方法的典型用法代码示例。如果您正苦于以下问题:Golang State.ToNumber方法的具体用法?Golang State.ToNumber怎么用?Golang State.ToNumber使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/aarzilli/golua/lua.State
的用法示例。
在下文中一共展示了State.ToNumber方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: popMap
func popMap(L *lua.State) map[string]interface{} {
m := make(map[string]interface{})
L.PushNil()
for L.Next(-2) != 0 {
if L.IsString(-2) {
var v interface{}
if L.IsBoolean(-1) {
v = L.ToBoolean(-1)
} else if L.IsNumber(-1) {
var t float64
t = L.ToNumber(-1)
if t == float64(int64(t)) {
v = int(t)
} else {
v = t
}
} else if L.IsString(-1) {
v = L.ToString(-1)
} else if L.IsNil(-1) {
v = nil
} else if L.IsTable(-1) {
v = popMap(L)
}
m[L.ToString(-2)] = v
}
L.Pop(1)
}
return m
}
示例2: GoLuaFunc
// GoLuaFunc converts an arbitrary Go function into a Lua-compatible GoFunction.
// There are special optimized cases for functions that go from strings to strings,
// and doubles to doubles, but otherwise Go
// reflection is used to provide a generic wrapper function
func GoLuaFunc(L *lua.State, fun interface{}) lua.LuaGoFunction {
switch f := fun.(type) {
case func(*lua.State) int:
return f
case func(string) string:
return func(L *lua.State) int {
L.PushString(f(L.ToString(1)))
return 1
}
case func(float64) float64:
return func(L *lua.State) int {
L.PushNumber(f(L.ToNumber(1)))
return 1
}
default:
}
var funv reflect.Value
switch ff := fun.(type) {
case reflect.Value:
funv = ff
default:
funv = valueOf(fun)
}
funt := funv.Type()
targs, tout := functionArgRetTypes(funt)
return func(L *lua.State) int {
var lastT reflect.Type
orig_targs := targs
isVariadic := funt.IsVariadic()
if isVariadic {
n := len(targs)
lastT = targs[n-1].Elem()
targs = targs[0 : n-1]
}
args := make([]reflect.Value, len(targs))
for i, t := range targs {
val := LuaToGo(L, t, i+1)
args[i] = valueOfNil(val)
//println(i,args[i].String())
}
if isVariadic {
n := L.GetTop()
for i := len(targs) + 1; i <= n; i++ {
ival := LuaToGo(L, lastT, i)
args = append(args, valueOfNil(ival))
}
targs = orig_targs
}
resv := callGo(L, funv, args)
for i, val := range resv {
GoToLua(L, tout[i], val, false)
}
return len(resv)
}
}
示例3: LuaToGo
//.........这里部分代码省略.........
case reflect.Uint8:
{
ptr := new(uint8)
*ptr = uint8(L.ToNumber(idx))
value = *ptr
}
case reflect.Uint16:
{
ptr := new(uint16)
*ptr = uint16(L.ToNumber(idx))
value = *ptr
}
case reflect.Uint32:
{
ptr := new(uint32)
*ptr = uint32(L.ToNumber(idx))
value = *ptr
}
case reflect.Uint64:
{
ptr := new(uint64)
*ptr = uint64(L.ToNumber(idx))
value = *ptr
}
default:
cannotConvert(L, idx, "number", kind, t)
}
case lua.LUA_TTABLE:
if t == nil {
kind = reflect.Interface
}
fallthrough
default:
istable := L.IsTable(idx)
// if we don't know the type and the Lua object is userdata,
// then it might be a proxy for a Go object. Otherwise wrap
// it up as a LuaObject.
if t == nil && !istable {
if isValueProxy(L, idx) {
return unwrapProxy(L, idx)
} else {
return NewLuaObject(L, idx)
}
}
switch kind {
case reflect.Slice:
{
// if we get a table, then copy its values to a new slice
if istable {
value = CopyTableToSlice(L, t, idx)
} else {
value = unwrapProxyOrComplain(L, idx)
}
}
case reflect.Map:
{
if istable {
value = CopyTableToMap(L, t, idx)
} else {
value = unwrapProxyOrComplain(L, idx)
}
}
case reflect.Struct:
{
if istable {
value = CopyTableToStruct(L, t, idx)
} else {
value = unwrapProxyOrComplain(L, idx)
}
}
case reflect.Interface:
{
if istable {
// have to make an executive decision here: tables with non-zero
// length are assumed to be slices!
if L.ObjLen(idx) > 0 {
value = CopyTableToSlice(L, nil, idx)
} else {
value = CopyTableToMap(L, nil, idx)
}
} else if L.IsNumber(idx) {
value = L.ToNumber(idx)
} else if L.IsString(idx) {
value = L.ToString(idx)
} else if L.IsBoolean(idx) {
value = L.ToBoolean(idx)
} else if L.IsNil(idx) {
return nil
} else {
value = unwrapProxyOrComplain(L, idx)
}
}
default:
value = unwrapProxyOrComplain(L, idx)
//cannotConvert(L,idx,"unknown",kind,t)
}
}
return value
}
示例4: luaToGo
//.........这里部分代码省略.........
ptr := new(uint32)
*ptr = uint32(L.ToNumber(idx))
value = *ptr
case reflect.Uint64:
ptr := new(uint64)
*ptr = uint64(L.ToNumber(idx))
value = *ptr
default:
value = reflect.Zero(t).Interface()
}
case lua.LUA_TTABLE:
if t == nil {
kind = reflect.Interface
}
fallthrough
default:
istable := L.IsTable(idx)
// If we don't know the type and the Lua object is userdata,
// then it might be a proxy for a Go object. Otherwise wrap
// it up as a LuaObject.
if t == nil && !istable {
if isValueProxy(L, idx) {
v, _ := valueOfProxy(L, idx)
return v.Interface()
}
return NewLuaObject(L, idx)
}
switch kind {
case reflect.Array:
if istable {
ptr := L.ToPointer(idx)
if val, ok := visited[ptr]; ok {
return val
}
value = copyTableToSlice(L, t, idx, visited)
} else {
value = mustUnwrapProxy(L, idx)
}
case reflect.Slice:
// if we get a table, then copy its values to a new slice
if istable {
ptr := L.ToPointer(idx)
if val, ok := visited[ptr]; ok {
return val
}
value = copyTableToSlice(L, t, idx, visited)
} else {
value = mustUnwrapProxy(L, idx)
}
case reflect.Map:
if istable {
ptr := L.ToPointer(idx)
if val, ok := visited[ptr]; ok {
return val
}
value = copyTableToMap(L, t, idx, visited)
} else {
value = mustUnwrapProxy(L, idx)
}
case reflect.Struct:
if istable {
ptr := L.ToPointer(idx)
if val, ok := visited[ptr]; ok {
return val
}
value = copyTableToStruct(L, t, idx, visited)
} else {
value = mustUnwrapProxy(L, idx)
}
case reflect.Interface:
if istable {
ptr := L.ToPointer(idx)
if val, ok := visited[ptr]; ok {
return val
}
// We have to make an executive decision here: tables with non-zero
// length are assumed to be slices!
if L.ObjLen(idx) > 0 {
value = copyTableToSlice(L, nil, idx, visited)
} else {
value = copyTableToMap(L, nil, idx, visited)
}
} else if L.IsNumber(idx) {
value = L.ToNumber(idx)
} else if L.IsString(idx) {
value = L.ToString(idx)
} else if L.IsBoolean(idx) {
value = L.ToBoolean(idx)
} else if L.IsNil(idx) {
return nil
} else {
value = mustUnwrapProxy(L, idx)
}
default:
value = mustUnwrapProxy(L, idx)
}
}
return value
}
示例5: apiAudioSetVolume
func (p *Plugin) apiAudioSetVolume(l *lua.State) int {
volume := l.ToNumber(1)
p.instance.Audio.Volume = float32(volume)
return 0
}
示例6: LuaToGo
//.........这里部分代码省略.........
}
} else if t.Kind() == reflect.Ptr {
kind = t.Elem().Kind()
} else {
kind = t.Kind()
}
switch kind {
// various numerical types are tedious but straightforward
case reflect.Float64:
{
ptr := new(float64)
*ptr = L.ToNumber(idx)
value = *ptr
}
case reflect.Float32:
{
ptr := new(float32)
*ptr = float32(L.ToNumber(idx))
value = *ptr
}
case reflect.Int:
{
ptr := new(int)
*ptr = int(L.ToNumber(idx))
value = *ptr
}
case reflect.Int8:
{
ptr := new(byte)
*ptr = byte(L.ToNumber(idx))
value = *ptr
}
case reflect.String:
{
tos := L.ToString(idx)
ptr := new(string)
*ptr = tos
value = *ptr
}
case reflect.Bool:
{
ptr := new(bool)
*ptr = bool(L.ToBoolean(idx))
value = *ptr
}
case reflect.Slice:
{
// if we get a table, then copy its values to a new slice
if L.IsTable(idx) {
value = CopyTableToSlice(L, t, idx)
} else {
value = unwrapProxy(L, idx)
}
}
case reflect.Map:
{
if L.IsTable(idx) {
value = CopyTableToMap(L, t, idx)
} else {
value = unwrapProxy(L, idx)
}
}
case reflect.Struct:
{
if L.IsTable(idx) {
value = CopyTableToStruct(L, t, idx)
} else {
value = unwrapProxy(L, idx)
}
}
case reflect.Interface:
{
if L.IsTable(idx) {
// have to make an executive decision here: tables with non-zero
// length are assumed to be slices!
if L.ObjLen(idx) > 0 {
value = CopyTableToSlice(L, nil, idx)
} else {
value = CopyTableToMap(L, nil, idx)
}
} else if L.IsNumber(idx) {
value = L.ToNumber(idx)
} else if L.IsString(idx) {
value = L.ToString(idx)
} else if L.IsBoolean(idx) {
value = L.ToBoolean(idx)
} else {
value = unwrapProxy(L, idx)
}
}
default:
{
fmt.Println("unhandled type", t)
value = 20
}
}
return value
}