本文整理汇总了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()
}
示例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()
}