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


Golang Otto.Eval方法代碼示例

本文整理匯總了Golang中github.com/robertkrimen/otto.Otto.Eval方法的典型用法代碼示例。如果您正苦於以下問題:Golang Otto.Eval方法的具體用法?Golang Otto.Eval怎麽用?Golang Otto.Eval使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/robertkrimen/otto.Otto的用法示例。


在下文中一共展示了Otto.Eval方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: RunWithOptions

// RunWithOptions runs a REPL with the given options.
func RunWithOptions(vm *otto.Otto, options Options) error {
	prompt := options.Prompt
	if prompt == "" {
		prompt = "> "
	}

	c := &readline.Config{
		Prompt: prompt,
	}

	if options.Autocomplete {
		c.AutoComplete = &autoCompleter{vm}
	}

	rl, err := readline.NewEx(c)
	if err != nil {
		return err
	}

	prelude := options.Prelude
	if prelude != "" {
		if _, err := io.Copy(rl.Stderr(), strings.NewReader(prelude+"\n")); err != nil {
			return err
		}

		rl.Refresh()
	}

	var d []string

	for {
		l, err := rl.Readline()
		if err != nil {
			if err == readline.ErrInterrupt {
				if d != nil {
					d = nil

					rl.SetPrompt(prompt)
					rl.Refresh()

					continue
				}

				break
			}

			return err
		}

		if l == "" {
			continue
		}

		d = append(d, l)

		s, err := vm.Compile("repl", strings.Join(d, "\n"))
		if err != nil {
			rl.SetPrompt(strings.Repeat(" ", len(prompt)))
		} else {
			rl.SetPrompt(prompt)

			d = nil

			v, err := vm.Eval(s)
			if err != nil {
				if oerr, ok := err.(*otto.Error); ok {
					io.Copy(rl.Stdout(), strings.NewReader(oerr.String()))
				} else {
					io.Copy(rl.Stdout(), strings.NewReader(err.Error()))
				}
			} else {
				rl.Stdout().Write([]byte(v.String() + "\n"))
			}
		}

		rl.Refresh()
	}

	return rl.Close()
}
開發者ID:vozhyk-,項目名稱:gohan,代碼行數:81,代碼來源:repl.go

示例2: RunWithPromptAndPrelude

// RunWithPromptAndPrelude runs a REPL with the given prompt and prelude.
func RunWithPromptAndPrelude(vm *otto.Otto, prompt, prelude string) error {
	if prompt == "" {
		prompt = ">"
	}

	prompt = strings.Trim(prompt, " ")
	prompt += " "

	rl, err := readline.New(prompt)
	if err != nil {
		return err
	}

	if prelude != "" {
		if _, err := io.Copy(rl.Stderr(), strings.NewReader(prelude+"\n")); err != nil {
			return err
		}

		rl.Refresh()
	}

	var d []string

	for {
		l, err := rl.Readline()
		if err != nil {
			if err == readline.ErrInterrupt {
				if d != nil {
					d = nil

					rl.SetPrompt(prompt)
					rl.Refresh()

					continue
				}

				break
			}

			return err
		}

		if l == "" {
			continue
		}

		d = append(d, l)

		s, err := vm.Compile("repl", strings.Join(d, "\n"))
		if err != nil {
			rl.SetPrompt(strings.Repeat(" ", len(prompt)))
		} else {
			rl.SetPrompt(prompt)

			d = nil

			v, err := vm.Eval(s)
			if err != nil {
				if oerr, ok := err.(*otto.Error); ok {
					io.Copy(rl.Stdout(), strings.NewReader(oerr.String()))
				} else {
					io.Copy(rl.Stdout(), strings.NewReader(err.Error()))
				}
			} else {
				rl.Stdout().Write([]byte(v.String() + "\n"))
			}
		}

		rl.Refresh()
	}

	return rl.Close()
}
開發者ID:Raskal8,項目名稱:go-ethereum,代碼行數:74,代碼來源:repl.go


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