本文整理汇总了Golang中github.com/stevedonovan/golua/lua51.State类的典型用法代码示例。如果您正苦于以下问题:Golang State类的具体用法?Golang State怎么用?Golang State使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了State类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: 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
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:7,代码来源:luar.go
示例2: 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))
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:7,代码来源:luar.go
示例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
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:7,代码来源:luar.go
示例4: 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
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:27,代码来源:luar.go
示例5: 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()
lua.LGetMetaTable(L, proxyMT)
L.SetMetaTable(-2)
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:8,代码来源:luar.go
示例6: 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
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:11,代码来源:luar.go
示例7: 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.GoFunction {
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)
}
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:55,代码来源:luar.go
示例8: 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
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:15,代码来源:luar.go
示例9: 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(i + 1)
GoToLua(L, nil, vslice.Index(i))
L.SetTable(-3)
}
return 1
} else {
L.PushNil()
L.PushString("not a slice!")
}
return 2
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:17,代码来源:luar.go
示例10: 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
}
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:15,代码来源:luar.go
示例11: copyMapToTable
func copyMapToTable(L *lua.State) int {
vmap := valueOf(unwrapProxy(L, 1))
if vmap.IsValid() && vmap.Type().Kind() == reflect.Map {
n := vmap.Len()
L.CreateTable(0, n)
for _, key := range vmap.MapKeys() {
GoToLua(L, nil, key)
GoToLua(L, nil, vmap.MapIndex(key))
L.SetTable(-3)
}
return 1
} else {
L.PushNil()
L.PushString("not a map!")
}
return 2
}
开发者ID:losinggeneration,项目名称:luar,代码行数:17,代码来源:luar.go
示例12: 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
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:18,代码来源:luar.go
示例13: 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
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:17,代码来源:luar.go
示例14: 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
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:12,代码来源:luar.go
示例15: 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()
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:21,代码来源:luar.go
示例16: 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
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:14,代码来源:luar.go
示例17: main
func main() {
var L *lua.State
L = lua.NewState()
L.OpenLibs()
rawptr := L.NewUserdata(uintptr(unsafe.Sizeof(Userdata{})))
var ptr *Userdata
ptr = (*Userdata)(rawptr)
ptr.a = 2
ptr.b = 3
fmt.Println(ptr)
rawptr2 := L.ToUserdata(-1)
ptr2 := (*Userdata)(rawptr2)
fmt.Println(ptr2)
}
开发者ID:sanyaade-embedded-systems,项目名称:golua,代码行数:18,代码来源:userdata.go
示例18: CopyTableToSlice
// return the Lua table at 'idx' as a copied Go slice. If 't' is nil then the slice
// type is []interface{}
func CopyTableToSlice(L *lua.State, t reflect.Type, idx int) interface{} {
if t == nil {
t = reflect.TypeOf(tslice)
}
te := t.Elem()
n := int(L.ObjLen(idx))
slice := reflect.MakeSlice(t, n, n)
for i := 1; i <= n; i++ {
L.RawGeti(idx, i)
val := LuaToGo(L, te, -1)
slice.Index(i - 1).Set(valueOf(val))
L.Pop(1)
}
return slice.Interface()
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:17,代码来源:luar.go
示例19: CopyTableToMap
// return the Lua table at 'idx' as a copied Go map. If 't' is nil then the map
// type is map[string]interface{}
func CopyTableToMap(L *lua.State, t reflect.Type, idx int) interface{} {
if t == nil {
t = reflect.TypeOf(tmap)
}
te, tk := t.Elem(), t.Key()
m := reflect.MakeMap(t)
L.PushNil()
if idx < 0 {
idx--
}
for L.Next(idx) != 0 {
// key at -2, value at -1
key := valueOf(LuaToGo(L, tk, -2))
val := valueOf(LuaToGo(L, te, -1))
m.SetMapIndex(key, val)
L.Pop(1)
}
return m.Interface()
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:21,代码来源:luar.go
示例20: LuaToGo
// Convert a Lua value 'idx' on the stack to the Go value of desired type 't'. Handles
// numerical and string types in a straightforward way, and will convert tables to
// either map or slice types.
func LuaToGo(L *lua.State, t reflect.Type, idx int) interface{} {
var value interface{}
switch t.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.Func:
{
if L.IsFunction(idx) {
value = LuaGoFunc(L, t, 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
}
开发者ID:losinggeneration,项目名称:luar,代码行数:98,代码来源:luar.go
注:本文中的github.com/stevedonovan/golua/lua51.State类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论