當前位置: 首頁>>代碼示例>>Golang>>正文


Golang State.DoString方法代碼示例

本文整理匯總了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)
}
開發者ID:hackerlist,項目名稱:monty,代碼行數:42,代碼來源:probe.go

示例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
}
開發者ID:tyler-sommer,項目名稱:squircy,代碼行數:21,代碼來源:handler.go

示例3: enableDebug

func (p *LuaPlugin) enableDebug(L *lua.State) {
	L.DoString(inspectLua)
	L.SetGlobal("inspect")
}
開發者ID:12foo,項目名稱:apiplexy,代碼行數:4,代碼來源:lua.go

示例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
}
開發者ID:Ambrevar,項目名稱:Demlo,代碼行數:62,代碼來源:luascript.go


注:本文中的github.com/aarzilli/golua/lua.State.DoString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。