本文整理汇总了Golang中github.com/robertkrimen/otto.Otto.Compile方法的典型用法代码示例。如果您正苦于以下问题:Golang Otto.Compile方法的具体用法?Golang Otto.Compile怎么用?Golang Otto.Compile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/robertkrimen/otto.Otto
的用法示例。
在下文中一共展示了Otto.Compile方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: compileAndRun
func compileAndRun(vm *otto.Otto, filename string, src interface{}) (otto.Value, error) {
script, err := vm.Compile(filename, src)
if err != nil {
return otto.Value{}, err
}
return vm.Run(script)
}
示例2: run
func run(vm *otto.Otto, filename string, code string) (otto.Value, error) {
// Compile before running so that stacktraces have filenames.
script, err := vm.Compile(filename, code)
if err != nil {
return otto.Value{}, err
}
return vm.Run(script)
}
示例3: Define
func Define(vm *otto.Otto, l *loop.Loop) error {
if v, err := vm.Get("Promise"); err != nil {
return err
} else if !v.IsUndefined() {
return nil
}
if err := timers.Define(vm, l); err != nil {
return err
}
s, err := vm.Compile("promise-bundle.js", src)
if err != nil {
return err
}
if _, err := vm.Run(s); err != nil {
return err
}
return nil
}
示例4: Define
func Define(vm *otto.Otto, l *loop.Loop) error {
if v, err := vm.Get("Promise"); err != nil {
return err
} else if !v.IsUndefined() {
return nil
}
if err := timers.Define(vm, l); err != nil {
return err
}
s, err := vm.Compile("bundle.js", rice.MustFindBox("dist-promise").MustString("bundle.js"))
if err != nil {
return err
}
if _, err := vm.Run(s); err != nil {
return err
}
return nil
}
示例5: 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()
}
示例6: DefineWithHandler
func DefineWithHandler(vm *otto.Otto, l *loop.Loop, h http.Handler) error {
if err := promise.Define(vm, l); err != nil {
return err
}
s, err := vm.Compile("fetch.js", rice.MustFindBox("dist-fetch").MustString("bundle.js"))
if err != nil {
return err
}
if _, err := vm.Run(s); err != nil {
return err
}
vm.Set("__private__fetch_execute", func(c otto.FunctionCall) otto.Value {
jsReq := c.Argument(0).Object()
jsRes := c.Argument(1).Object()
cb := c.Argument(2)
method := mustValue(jsReq.Get("method")).String()
urlStr := mustValue(jsReq.Get("url")).String()
jsBody := mustValue(jsReq.Get("body"))
var body io.Reader
if jsBody.IsString() {
body = strings.NewReader(jsBody.String())
}
t := &fetchTask{
jsReq: jsReq,
jsRes: jsRes,
cb: cb,
}
l.Add(t)
go func() {
defer l.Ready(t)
req, err := http.NewRequest(method, urlStr, body)
if err != nil {
t.err = err
return
}
if h != nil && urlStr[0] == '/' {
res := httptest.NewRecorder()
h.ServeHTTP(res, req)
jsRes.Set("status", res.Code)
jsRes.Set("statusText", http.StatusText(res.Code))
h := mustValue(jsRes.Get("headers")).Object()
for k, vs := range res.Header() {
for _, v := range vs {
if _, err := h.Call("append", k, v); err != nil {
t.err = err
return
}
}
}
jsRes.Set("_body", string(res.Body.Bytes()))
} else {
res, err := http.DefaultClient.Do(req)
if err != nil {
t.err = err
return
}
jsRes.Set("status", res.StatusCode)
jsRes.Set("statusText", res.Status)
h := mustValue(jsRes.Get("headers")).Object()
for k, vs := range res.Header {
for _, v := range vs {
if _, err := h.Call("append", k, v); err != nil {
t.err = err
return
}
}
}
d, err := ioutil.ReadAll(res.Body)
if err != nil {
t.err = err
return
}
jsRes.Set("_body", string(d))
}
}()
return otto.UndefinedValue()
})
return nil
}
示例7: 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()
}