本文整理汇总了Golang中github.com/kdar/morphgen/golua/lua.State类的典型用法代码示例。如果您正苦于以下问题:Golang State类的具体用法?Golang State怎么用?Golang State使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了State类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: callGoMethod
func callGoMethod(L *lua.State, name string, st reflect.Value) {
ret := st.MethodByName(name)
if !ret.IsValid() {
fmt.Println("whoops")
}
L.PushGoFunction(GoLuaFunc(L, ret))
}
示例2: GoLua
func GoLua(L *lua.State) int {
go func() {
LT := L.NewThread()
L.PushValue(1)
lua.XMove(L, LT, 1)
res := LT.Resume(0)
for res != 0 {
if res == 2 {
emsg := LT.ToString(-1)
fmt.Println("error", emsg)
}
ch, t := valueOfProxy(LT, -2)
if LT.ToBoolean(-1) { // send on a channel
val := valueOf(LuaToGo(LT, t.Elem(), -3))
ch.Send(val)
res = LT.Resume(0)
} else { // receive on a channel
val, ok := ch.Recv()
GoToLua(LT, t.Elem(), val)
LT.PushBoolean(ok)
res = LT.Resume(2)
}
}
}()
return 0
}
示例3: slice__newindex
func slice__newindex(L *lua.State) int {
slice, t := valueOfProxy(L, 1)
idx := L.ToInteger(2)
val := LuaToGo(L, t.Elem(), 3)
slice.Index(idx - 1).Set(valueOf(val))
return 0
}
示例4: slice_slice
func slice_slice(L *lua.State) int {
slice, _ := valueOfProxy(L, 1)
i1, i2 := L.ToInteger(2), L.ToInteger(3)
newslice := slice.Slice(i1, i2)
makeValueProxy(L, newslice, SLICE_META)
return 1
}
示例5: struct__newindex
func struct__newindex(L *lua.State) int {
st, t := valueOfProxy(L, 1)
name := L.ToString(2)
if t.Kind() == reflect.Ptr {
st = st.Elem()
}
field := st.FieldByName(name)
val := LuaToGo(L, field.Type(), 3)
field.Set(valueOf(val))
return 0
}
示例6: 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
isVariadic := funt.IsVariadic()
if isVariadic {
lastT = targs[len(targs)-1].Elem()
targs = targs[0 : len(targs)-1]
}
args := make([]reflect.Value, len(targs))
for i, t := range targs {
val := LuaToGo(L, t, i+1)
args[i] = valueOf(val)
}
if isVariadic {
n := L.GetTop()
for i := len(targs) + 1; i <= n; i++ {
val := valueOf(LuaToGo(L, lastT, i))
args = append(args, val)
}
}
resv := funv.Call(args)
for i, val := range resv {
GoToLua(L, tout[i], val)
}
return len(resv)
}
}
示例7: struct__index
func struct__index(L *lua.State) int {
st, t := valueOfProxy(L, 1)
name := L.ToString(2)
est := st
if t.Kind() == reflect.Ptr {
est = st.Elem()
}
ret := est.FieldByName(name)
if !ret.IsValid() { // no such field, try for method?
callGoMethod(L, name, st)
} else {
GoToLua(L, ret.Type(), ret)
}
return 1
}
示例8: CopySliceToTable
// copy a Go slice to a Lua table
func CopySliceToTable(L *lua.State, vslice reflect.Value) int {
if vslice.IsValid() && vslice.Type().Kind() == reflect.Slice {
n := vslice.Len()
L.CreateTable(n, 0)
for i := 0; i < n; i++ {
L.PushInteger(int64(i + 1))
GoToLua(L, nil, vslice.Index(i))
L.SetTable(-3)
}
return 1
} else {
L.PushNil()
L.PushString("not a slice!")
}
return 2
}
示例9: Lookup
// look up a Lua value by its full name. If idx is 0, then this name
// is assumed to start in the global table, e.g. "string.gsub".
// With non-zero idx, can be used to look up subfields of a table
func Lookup(L *lua.State, path string, idx int) {
parts := strings.Split(path, ".")
if idx != 0 {
L.PushValue(idx)
} else {
L.GetGlobal("_G")
}
for _, field := range parts {
L.GetField(-1, field)
L.Remove(-2) // remove table
}
}
示例10: makeValueProxy
func makeValueProxy(L *lua.State, val reflect.Value, proxyMT string) {
rawptr := L.NewUserdata(uintptr(unsafe.Sizeof(ValueProxy{})))
ptr := (*ValueProxy)(rawptr)
ptr.value = val
ptr.t = val.Type()
L.LGetMetaTable(proxyMT)
L.SetMetaTable(-2)
}
示例11: CopyMapToTable
// copy a Go map to a Lua table
func CopyMapToTable(L *lua.State, vmap reflect.Value) int {
if vmap.IsValid() && vmap.Type().Kind() == reflect.Map {
n := vmap.Len()
L.CreateTable(0, n)
for _, key := range vmap.MapKeys() {
val := vmap.MapIndex(key)
GoToLua(L, nil, key)
GoToLua(L, nil, val)
L.SetTable(-3)
}
return 1
} else {
L.PushNil()
L.PushString("not a map!")
}
return 2
}
示例12: slice__index
func slice__index(L *lua.State) int {
slice, _ := valueOfProxy(L, 1)
if L.IsNumber(2) {
idx := L.ToInteger(2)
ret := slice.Index(idx - 1)
GoToLua(L, ret.Type(), ret)
} else {
name := L.ToString(2)
switch name {
case "Slice":
L.PushGoFunction(slice_slice)
default:
fmt.Println("unknown slice method")
}
}
return 1
}
示例13: channel_send
func channel_send(L *lua.State) int {
fmt.Println("yield send")
L.PushValue(2)
L.PushValue(1)
L.PushBoolean(true)
return L.Yield(3)
//~ ch,t := valueOfProxy(L,1)
//~ val := valueOf(LuaToGo(L, t.Elem(),2))
//~ ch.Send(val)
//~ return 0
}
示例14: CopyTableToStruct
func CopyTableToStruct(L *lua.State, t reflect.Type, idx int) interface{} {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
s := reflect.New(t) // T -> *T
ref := s.Elem()
L.PushNil()
if idx < 0 {
idx--
}
for L.Next(idx) != 0 {
key := L.ToString(-2)
f := ref.FieldByName(key)
if f.IsValid() {
val := valueOf(LuaToGo(L, f.Type(), -1))
f.Set(val)
}
L.Pop(1)
}
return s.Interface()
}
示例15: channel_recv
func channel_recv(L *lua.State) int {
fmt.Println("yield recv")
L.PushValue(1)
L.PushBoolean(false)
return L.Yield(2)
//~ ch,t := valueOfProxy(L,1)
//~ L.Yield(0)
//~ val,ok := ch.Recv()
//~ GoToLua(L,t.Elem(),val)
//~ L.PushBoolean(ok)
//~ L.Resume(0)
//~ return 2
}