本文整理汇总了Golang中github.com/aarzilli/golua/lua.State.DoString方法的典型用法代码示例。如果您正苦于以下问题:Golang State.DoString方法的具体用法?Golang State.DoString怎么用?Golang State.DoString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/aarzilli/golua/lua.State
的用法示例。
在下文中一共展示了State.DoString方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
示例2: runUnsafeLua
func runUnsafeLua(vm *lua.State, unsafe string) error {
start := time.Now()
defer func() {
duration := time.Since(start)
if err := recover(); err != nil {
if err == halt {
fmt.Println("Some code took too long! Stopping after: ", duration)
}
panic(err)
}
}()
vm.SetExecutionLimit(maxExecutionTime * (1 << 26))
err := vm.DoString(unsafe)
if err.Error() == "Lua execution quantum exceeded" {
panic(halt)
}
return err
}
示例3: enableDebug
func (p *LuaPlugin) enableDebug(L *lua.State) {
L.DoString(inspectLua)
L.SetGlobal("inspect")
}
示例4: 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
}