本文整理匯總了Golang中github.com/aarzilli/golua/lua.State.GetGlobal方法的典型用法代碼示例。如果您正苦於以下問題:Golang State.GetGlobal方法的具體用法?Golang State.GetGlobal怎麽用?Golang State.GetGlobal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/aarzilli/golua/lua.State
的用法示例。
在下文中一共展示了State.GetGlobal方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: outputNumbersToStrings
func outputNumbersToStrings(L *lua.State) {
L.GetGlobal("output")
if !L.IsTable(-1) {
L.NewTable()
L.SetGlobal("output")
}
L.GetField(-1, "tags")
if L.IsTable(-1) {
// First key.
L.PushNil()
for L.Next(-2) != 0 {
// Use 'key' at index -2 and 'value' at index -1.
if L.IsString(-2) && L.IsString(-1) {
// Convert numbers to strings.
L.ToString(-1)
L.SetField(-3, L.ToString(-2))
} else {
// Remove 'value' and keep 'key' for next iteration.
L.Pop(1)
}
}
}
L.Pop(1)
L.Pop(1)
}
示例2: GetTasklistFromLua
func GetTasklistFromLua(L *lua.State) *Tasklist {
L.CheckStack(1)
L.GetGlobal(TASKLIST)
rawptr := L.ToUserdata(-1)
var ptr **Tasklist = (**Tasklist)(rawptr)
L.Pop(1)
return *ptr
}
示例3: sandboxRegister
// Registers a Go function as a global variable and add it to the sandbox.
func sandboxRegister(L *lua.State, name string, f interface{}) {
goToLua(L, name, f)
L.PushString(registryWhitelist)
L.GetTable(lua.LUA_REGISTRYINDEX)
L.GetGlobal(name)
L.SetField(-2, name)
}
示例4: GetEntryFromLua
func GetEntryFromLua(L *lua.State, name string, fname string) *Entry {
L.CheckStack(1)
L.GetGlobal(name)
rawptr := L.ToUserdata(-1)
var ptr **Entry = (**Entry)(rawptr)
L.Pop(1)
if ptr == nil {
panic(errors.New("No cursor set, can not use " + fname))
}
return *ptr
}
示例5: 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
}
}
示例6: Run
func (p ProbeRunner) Run() {
var err error
var state *lua.State
var fun *luar.LuaObject
var res interface{}
r := p.Error
args := make(map[string]interface{})
if err = json.Unmarshal([]byte(p.Probe.Arguments), &args); err != nil {
goto out
}
state = mkstate()
defer state.Close()
err = state.DoString(fmt.Sprintf("fun = function(args) %s end", p.Script.Code))
if err != nil {
goto out
}
state.GetGlobal("fun")
fun = luar.NewLuaObject(state, -1)
if res, err = fun.Call(args); err != nil {
goto out
} else if res == nil {
// if nil, that means no error. just go to out.
goto out
} else if status, ok := res.(string); !ok {
// if it's not a string, that's bad. luar seems to convert go errors to strings..
err = fmt.Errorf("script resulted in non-string return value %q", res)
} else if status != "" {
// if the string is not empty that's an error.
err = fmt.Errorf("probe error: %s", status)
}
out:
r <- err
close(r)
}
示例7: 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)
}
}
示例8: 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)
}
示例9: Register
// Register makes a number of Go values available in Lua code.
// 'values' is a map of strings to Go values.
//
// - If table is non-nil, then create or reuse a global table of that name and
// put the values in it.
//
// - If table is '' then put the values in the global table (_G).
//
// - If table is '*' then assume that the table is already on the stack.
func Register(L *lua.State, table string, values Map) {
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 {
GoToLua(L, nil, reflect.ValueOf(val), false)
L.SetField(-2, name)
}
if pop {
L.Pop(1)
}
}
示例10: Global
// new LuaObject refering to the global environment
func Global(L *lua.State) *LuaObject {
L.GetGlobal("_G")
val := NewLuaObject(L, -1)
L.Pop(1)
return val
}
示例11: 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 t.Kind() == reflect.Func {
var lf func(*lua.State) int
if convertFun {
lf = GoLuaFunc(L, val)
} else {
lf = val.(func(*lua.State) int)
}
L.PushGoFunction(func(L *lua.State) (ret int) {
defer func() {
if err2 := recover(); err2 != nil {
GoToLua(L, typeof(err2), valueOf(err2), false)
ret = 1
return
}
}()
ret = lf(L)
pos := L.GetTop() - ret + 1
L.PushNil()
L.Insert(pos)
for i := 0; i < L.GetTop(); i++ {
fmt.Println(L.Typename(int(L.Type(i + 1))))
}
return ret + 1
})
L.SetField(-2, "safe_"+name)
}
}
if pop {
L.Pop(1)
}
}
示例12: pushValue
func pushValue(state *lua.State, value interface{}) error {
switch v := value.(type) {
default:
return fmt.Errorf("An item of unknown type was included in the environment or arguments of a Lua call (skipping it): %v",
value)
case nil:
log.Println("Pushing nil onto lua stack")
state.PushNil()
case string:
log.Println("Pushing string onto lua stack")
state.PushString(v)
case int:
log.Println("Pushing int onto lua stack")
state.PushInteger(int64(v))
case int64:
log.Println("Pushing int64 onto lua stack")
state.PushInteger(v)
case float64:
log.Println("Pushing float64 onto lua stack")
state.PushNumber(v)
case bool:
log.Println("Pushing bool onto lua stack")
state.PushBoolean(v)
case map[string]interface{}:
log.Println("Pushing map[string]interface{} onto lua stack")
state.CreateTable(0, len(v))
for name, value := range v {
err := pushValue(state, value)
if err != nil {
// error means nothing was added to stack. So pop our new table so *we* leave nothing added to the stack.
state.Pop(1)
return err
}
state.SetField(-2, name)
}
// then leave the table on the stack
case ThingType:
// These are singleton sentinel values, so load them from Lua-land.
state.GetGlobal("world")
state.GetField(-1, strings.Title(v.String()))
state.Remove(-2)
case *Thing:
log.Println("Pushing *Thing onto lua stack")
return pushValue(state, v.Id)
case ThingId:
log.Println("Pushing ThingId onto lua stack")
// We're pushing a ThingId, so make a new userdata for it, with the Thing metatable.
userdata := state.NewUserdata(uintptr(unsafe.Sizeof(int64(0))))
thingPtr := (*int64)(userdata)
*thingPtr = int64(v)
if !state.IsUserdata(-1) {
log.Println("!!! HOGAD JUST PUSHED NEW USERDATA BUT IT ISN'T OMG !!!")
}
log.Println("Pushed ThingId", *thingPtr, "onto lua stack")
// Now make it act like a Thing.
state.LGetMetaTable(ThingMetaTableName) // ( udata -- udata mtbl )
state.SetMetaTable(-2) // ( udata mtbl -- udata )
// Let's just check that it's that, for sures.
if !state.IsUserdata(-1) {
log.Println("!!! WOOP WOOP DID NOT SET METATABLE RIGHT :( !!!")
}
}
return nil
}
示例13: run
// 'exist' is optional.
func run(L *lua.State, registryIndex string, code string, input *inputInfo, output *outputInfo, exist *inputInfo) error {
// Restore the sandbox.
err := L.DoString(luaRestoreSandbox)
if err != nil {
log.Fatal("Cannot load function to restore sandbox", err)
}
L.PushString(registryWhitelist)
L.GetTable(lua.LUA_REGISTRYINDEX)
err = L.Call(1, 0)
if err != nil {
log.Fatal("Failed to restore sandbox", err)
}
goToLua(L, "input", *input)
goToLua(L, "output", *output)
if exist != nil {
goToLua(L, "existinfo", *exist)
}
// Shortcut (mostly for prescript and postscript).
L.GetGlobal("input")
L.GetField(-1, "tags")
L.SetGlobal("i")
L.Pop(1)
L.GetGlobal("output")
L.GetField(-1, "tags")
L.SetGlobal("o")
L.Pop(1)
// Call the compiled script.
L.PushString(registryIndex)
L.GetTable(lua.LUA_REGISTRYINDEX)
L.PushString(code)
if L.IsTable(-2) {
L.GetTable(-2)
if L.IsFunction(-1) {
err := L.Call(0, 0)
if err != nil {
L.SetTop(0)
return fmt.Errorf("%s", err)
}
} else {
L.Pop(1)
}
} else {
L.Pop(1)
}
L.Pop(1)
// Allow tags to be numbers for convenience.
outputNumbersToStrings(L)
L.GetGlobal("output")
r := luar.LuaToGo(L, reflect.TypeOf(*output), -1)
L.Pop(1)
*output = r.(outputInfo)
return nil
}