aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/robertkrimen/otto/repl/repl.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2016-02-11 22:16:52 +0800
committerPéter Szilágyi <peterke@gmail.com>2016-02-11 22:16:52 +0800
commitb019f3ee29ce55c3d515ee8bafe0f4bb14221c0a (patch)
tree26e023be6c99a10e82a5a0ebadd1e42cefe9bd3c /Godeps/_workspace/src/github.com/robertkrimen/otto/repl/repl.go
parentb05e472c076d30035233d6a8b5fb3360b236e3ff (diff)
downloadgo-tangerine-b019f3ee29ce55c3d515ee8bafe0f4bb14221c0a.tar
go-tangerine-b019f3ee29ce55c3d515ee8bafe0f4bb14221c0a.tar.gz
go-tangerine-b019f3ee29ce55c3d515ee8bafe0f4bb14221c0a.tar.bz2
go-tangerine-b019f3ee29ce55c3d515ee8bafe0f4bb14221c0a.tar.lz
go-tangerine-b019f3ee29ce55c3d515ee8bafe0f4bb14221c0a.tar.xz
go-tangerine-b019f3ee29ce55c3d515ee8bafe0f4bb14221c0a.tar.zst
go-tangerine-b019f3ee29ce55c3d515ee8bafe0f4bb14221c0a.zip
Godeps: update all dependencies to latest code
Diffstat (limited to 'Godeps/_workspace/src/github.com/robertkrimen/otto/repl/repl.go')
-rw-r--r--Godeps/_workspace/src/github.com/robertkrimen/otto/repl/repl.go115
1 files changed, 115 insertions, 0 deletions
diff --git a/Godeps/_workspace/src/github.com/robertkrimen/otto/repl/repl.go b/Godeps/_workspace/src/github.com/robertkrimen/otto/repl/repl.go
new file mode 100644
index 000000000..0d70cc051
--- /dev/null
+++ b/Godeps/_workspace/src/github.com/robertkrimen/otto/repl/repl.go
@@ -0,0 +1,115 @@
+// Package repl implements a REPL (read-eval-print loop) for otto.
+package repl
+
+import (
+ "fmt"
+ "io"
+ "strings"
+ "sync/atomic"
+
+ "github.com/robertkrimen/otto"
+ "gopkg.in/readline.v1"
+)
+
+var counter uint32
+
+// DebuggerHandler implements otto's debugger handler signature, providing a
+// simple drop-in debugger implementation.
+func DebuggerHandler(vm *otto.Otto) {
+ i := atomic.AddUint32(&counter, 1)
+
+ // purposefully ignoring the error here - we can't do anything useful with
+ // it except panicking, and that'd be pretty rude. it'd be easy enough for a
+ // consumer to define an equivalent function that _does_ panic if desired.
+ _ = RunWithPrompt(vm, fmt.Sprintf("DEBUGGER[%d]>", i))
+}
+
+// Run creates a REPL with the default prompt and no prelude.
+func Run(vm *otto.Otto) error {
+ return RunWithPromptAndPrelude(vm, "", "")
+}
+
+// RunWithPrompt runs a REPL with the given prompt and no prelude.
+func RunWithPrompt(vm *otto.Otto, prompt string) error {
+ return RunWithPromptAndPrelude(vm, prompt, "")
+}
+
+// RunWithPrelude runs a REPL with the default prompt and the given prelude.
+func RunWithPrelude(vm *otto.Otto, prelude string) error {
+ return RunWithPromptAndPrelude(vm, "", prelude)
+}
+
+// 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()
+}