本文整理匯總了Golang中github.com/aarzilli/golua/lua.State.PushGoFunction方法的典型用法代碼示例。如果您正苦於以下問題:Golang State.PushGoFunction方法的具體用法?Golang State.PushGoFunction怎麽用?Golang State.PushGoFunction使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/aarzilli/golua/lua.State
的用法示例。
在下文中一共展示了State.PushGoFunction方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: register
func register(L *lua.State, table string, values Map, convertFun bool) {
pop := true
if table == "*" {
pop = false
} else if len(table) > 0 {
L.GetGlobal(table)
if L.IsNil(-1) {
L.NewTable()
L.SetGlobal(table)
L.GetGlobal(table)
}
} else {
L.GetGlobal("_G")
}
for name, val := range values {
t := reflect.TypeOf(val)
if t.Kind() == reflect.Func {
if convertFun {
L.PushGoFunction(GoLuaFunc(L, val))
} else {
lf := val.(func(*lua.State) int)
L.PushGoFunction(lf)
}
} else {
GoToLua(L, t, valueOf(val), false)
}
L.SetField(-2, name)
}
if pop {
L.Pop(1)
}
}
示例2: MessThingTellallMethod
func MessThingTellallMethod(state *lua.State, thing *Thing) int {
state.PushGoFunction(func(state *lua.State) int {
place := checkThing(state, 1)
text := state.CheckString(2)
// If arg 3 is present, it should be a table of Things to exclude.
excludes := make(map[ThingId]bool)
if 2 < state.GetTop() {
if !state.IsTable(3) {
state.ArgError(3, "expected `table` for exclude argument if present")
}
numExcludes := int(state.ObjLen(3))
for i := 0; i < numExcludes; i++ {
state.RawGeti(3, i+1)
exclude := checkThing(state, -1)
excludes[exclude.Id] = true
}
}
for _, content := range place.GetContents() {
if excludes[content.Id] {
continue
}
if content.Client != nil {
content.Client.Send(text)
}
}
return 0
})
return 1
}
示例3: 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))
}
示例4: goToLua
// goToLua copies Go values to Lua and sets the result to global 'name'.
// Compound types are deep-copied.
// Functions are automatically converted to 'func (L *lua.State) int'.
func goToLua(L *lua.State, name string, val interface{}) {
t := reflect.TypeOf(val)
if t.Kind() == reflect.Func {
L.PushGoFunction(luar.GoLuaFunc(L, val))
} else {
luar.GoToLua(L, t, reflect.ValueOf(val), true)
}
L.SetGlobal(name)
}
示例5: MessThingMovetoMethod
func MessThingMovetoMethod(state *lua.State, thing *Thing) int {
state.PushGoFunction(func(state *lua.State) int {
source := checkThing(state, 1)
target := checkThing(state, 2)
ok := source.MoveTo(target)
state.PushBoolean(ok)
return 1
})
return 1
}
示例6: MessThingTellMethod
func MessThingTellMethod(state *lua.State, thing *Thing) int {
state.PushGoFunction(func(state *lua.State) int {
thing := checkThing(state, 1)
text := state.CheckString(2)
if thing.Client != nil {
thing.Client.Send(text)
}
state.Pop(2) // ( udataThing strText -- )
return 0
})
return 1
}
示例7: ProxyPairs
// ProxyPairs implements Lua 5.2 'pairs' functions.
// It respects the __pairs metamethod.
//
// It is only useful for compatibility with Lua 5.1.
func ProxyPairs(L *lua.State) int {
// See Lua >=5.2 source code.
if L.GetMetaField(1, "__pairs") {
L.PushValue(1)
L.Call(1, 3)
return 3
}
L.CheckType(1, lua.LUA_TTABLE)
L.PushGoFunction(pairsAux)
L.PushValue(1)
L.PushNil()
return 3
}
示例8: 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
}
示例9: MessThingFindinsideMethod
func MessThingFindinsideMethod(state *lua.State, thing *Thing) int {
state.PushGoFunction(func(state *lua.State) int {
thing := checkThing(state, 1)
text := state.CheckString(2)
if text == "" {
state.ArgError(2, "cannot find empty string")
}
otherThing := thing.FindInside(text)
if otherThing == nil {
return 0
}
pushValue(state, otherThing.Id)
return 1
})
return 1
}
示例10: slice__ipairs
func slice__ipairs(L *lua.State) int {
s, _ := valueOfProxy(L, 1)
n := s.Len()
idx := -1
iter := func(L *lua.State) int {
idx++
if idx == n {
L.PushNil()
return 1
}
GoToLua(L, nil, valueOf(idx+1), false) // report as 1-based index
val := s.Index(idx)
GoToLua(L, nil, val, false)
return 2
}
L.PushGoFunction(iter)
return 1
}
示例11: callGoMethod
func callGoMethod(L *lua.State, name string, st reflect.Value) {
ret := st.MethodByName(name)
if !ret.IsValid() {
T := st.Type()
// Could not resolve this method. Perhaps it's defined on the pointer?
if T.Kind() != reflect.Ptr {
if st.CanAddr() { // easy if we can get a pointer directly
st = st.Addr()
} else { // otherwise have to create and initialize one...
VP := reflect.New(T)
VP.Elem().Set(st)
st = VP
}
}
ret = st.MethodByName(name)
assertValid(L, ret, st, name, "method")
}
L.PushGoFunction(GoLuaFunc(L, ret))
}
示例12: map__pairs
func map__pairs(L *lua.State) int {
m, _ := valueOfProxy(L, 1)
keys := m.MapKeys()
idx := -1
n := m.Len()
iter := func(L *lua.State) int {
idx++
if idx == n {
L.PushNil()
return 1
}
GoToLua(L, nil, keys[idx], false)
val := m.MapIndex(keys[idx])
GoToLua(L, nil, val, false)
return 2
}
L.PushGoFunction(iter)
return 1
}
示例13: initializeProxies
func initializeProxies(L *lua.State) {
flagValue := func() {
L.SetMetaMethod("__tostring", proxy__tostring)
L.SetMetaMethod("__gc", proxy__gc)
L.SetMetaMethod("__eq", proxy__eq)
L.PushBoolean(true)
L.SetField(-2, "luago.value")
L.Pop(1)
}
L.NewMetaTable(cSLICE_META)
L.SetMetaMethod("__index", slice__index)
L.SetMetaMethod("__newindex", slice__newindex)
L.SetMetaMethod("__len", slicemap__len)
L.SetMetaMethod("__ipairs", slice__ipairs)
flagValue()
L.NewMetaTable(cMAP_META)
L.SetMetaMethod("__index", map__index)
L.SetMetaMethod("__newindex", map__newindex)
L.SetMetaMethod("__len", slicemap__len)
L.SetMetaMethod("__pairs", map__pairs)
flagValue()
L.NewMetaTable(cSTRUCT_META)
L.SetMetaMethod("__index", struct__index)
L.SetMetaMethod("__newindex", struct__newindex)
flagValue()
L.NewMetaTable(cINTERFACE_META)
L.SetMetaMethod("__index", interface__index)
flagValue()
L.NewMetaTable(cCHANNEL_META)
//~ RegisterFunctions(L,"*",FMap {
//~ "Send":channel_send,
//~ "Recv":channel_recv,
//~ })
L.NewTable()
L.PushGoFunction(channel_send)
L.SetField(-2, "Send")
L.PushGoFunction(channel_recv)
L.SetField(-2, "Recv")
L.SetField(-2, "__index")
flagValue()
}
示例14: MessThingPronounsubMethod
func MessThingPronounsubMethod(state *lua.State, thing *Thing) int {
state.PushGoFunction(func(state *lua.State) int {
thing := checkThing(state, 1)
text := state.CheckString(2)
for code, pronoun := range thing.Pronouns() {
lowerCode := fmt.Sprintf(`%%%s`, code)
upperCode := fmt.Sprintf(`%%%s`, strings.ToUpper(code))
text = strings.Replace(text, lowerCode, pronoun, -1)
text = strings.Replace(text, upperCode, strings.ToTitle(pronoun), -1)
}
text = strings.Replace(text, `%n`, thing.Name, -1)
text = strings.Replace(text, `%N`, thing.Name, -1)
state.Pop(2) // ( udataThing str -- )
state.PushString(text) // ( -- str' )
return 1
})
return 1
}
示例15: installWorld
func installWorld(state *lua.State) {
log.Println("Installing world")
printStackTypes(state)
state.NewMetaTable(ThingMetaTableName) // ( -- mtbl )
state.SetMetaMethod("__index", MessThingIndex) // ( mtbl -- mtbl )
state.Pop(1) // ( mtbl -- )
worldTable := map[string]interface{}{
/*
"Root":
"Create": func...
*/
}
pushValue(state, worldTable)
// Install Thing types as singleton sentinel values. As userdata, these will only compare if the values are exactly equal.
state.NewUserdata(uintptr(0))
state.SetField(-2, "Player")
state.NewUserdata(uintptr(0))
state.SetField(-2, "Place")
state.NewUserdata(uintptr(0))
state.SetField(-2, "Program")
state.NewUserdata(uintptr(0))
state.SetField(-2, "Action")
state.NewUserdata(uintptr(0))
state.SetField(-2, "Thing")
state.SetGlobal("world")
state.GetGlobal("table") // ( -- tblTable )
state.PushGoFunction(TableFormat)
state.SetField(-2, "format")
state.Pop(1) // ( tblTable -- )
log.Println("Finished installing world")
printStackTypes(state)
}