Debian Codegraph @

repl/repl.go

raw symbols SCIP
1 // Package repl implements a REPL (read-eval-print loop) for otto.
2 package repl
3
4 import (
5 "errors"
6 "fmt"
7 "io"
8 "strings"
9 "sync/atomic"
10
11 "github.com/robertkrimen/otto"
12 "gopkg.in/readline.v1"
13 )
14
15 var counter uint32
16
17 // DebuggerHandler implements otto's debugger handler signature, providing a
18 // simple drop-in debugger implementation.
19 func DebuggerHandler(vm *otto.Otto) {
20 i := atomic.AddUint32(&counter, 1)
21
22 // purposefully ignoring the error here - we can't do anything useful with
23 // it except panicking, and that'd be pretty rude. it'd be easy enough for a
24 // consumer to define an equivalent function that _does_ panic if desired.
25 _ = RunWithPrompt(vm, fmt.Sprintf("DEBUGGER[%d]> ", i))
26 }
27
28 // Run creates a REPL with the default prompt and no prelude.
29 func Run(vm *otto.Otto) error {
30 return RunWithOptions(vm, Options{})
31 }
32
33 // RunWithPrompt runs a REPL with the given prompt and no prelude.
34 func RunWithPrompt(vm *otto.Otto, prompt string) error {
35 return RunWithOptions(vm, Options{
36 Prompt: prompt,
37 })
38 }
39
40 // RunWithPrelude runs a REPL with the default prompt and the given prelude.
41 func RunWithPrelude(vm *otto.Otto, prelude string) error {
42 return RunWithOptions(vm, Options{
43 Prelude: prelude,
44 })
45 }
46
47 // RunWithPromptAndPrelude runs a REPL with the given prompt and prelude.
48 func RunWithPromptAndPrelude(vm *otto.Otto, prompt, prelude string) error {
49 return RunWithOptions(vm, Options{
50 Prompt: prompt,
51 Prelude: prelude,
52 })
53 }
54
55 // Options contains parameters for configuring a REPL session.
56 type Options struct {
57 // Prompt controls what's shown at the beginning of the line. If not
58 // specified, this defaults to "> "
59 Prompt string
60 // Prelude is some text that's printed before control is given to the user.
61 // By default this is empty. If specified, this will be printed with a
62 // newline following it.
63 Prelude string
64 // Autocomplete controls whether this REPL session has autocompletion
65 // enabled. The way autocomplete is implemented can incur a performance
66 // penalty, so it's turned off by default.
67 Autocomplete bool
68 }
69
70 // RunWithOptions runs a REPL with the given options.
71 func RunWithOptions(vm *otto.Otto, options Options) error {
72 prompt := options.Prompt
73 if prompt == "" {
74 prompt = "> "
75 }
76
77 c := &readline.Config{
78 Prompt: prompt,
79 }
80
81 if options.Autocomplete {
82 c.AutoComplete = &autoCompleter{vm}
83 }
84
85 rl, err := readline.NewEx(c)
86 if err != nil {
87 return err
88 }
89
90 prelude := options.Prelude
91 if prelude != "" {
92 if _, err = io.Copy(rl.Stderr(), strings.NewReader(prelude+"\n")); err != nil {
93 return err
94 }
95
96 rl.Refresh()
97 }
98
99 var d []string
100
101 for {
102 l, errRL := rl.Readline()
103 if errRL != nil {
104 if errors.Is(errRL, readline.ErrInterrupt) {
105 if d != nil {
106 d = nil
107
108 rl.SetPrompt(prompt)
109 rl.Refresh()
110
111 continue
112 }
113
114 break
115 }
116
117 return errRL
118 }
119
120 if l == "" {
121 continue
122 }
123
124 d = append(d, l)
125
126 s, errRL := vm.Compile("repl", strings.Join(d, "\n"))
127 if errRL != nil {
128 rl.SetPrompt(strings.Repeat(" ", len(prompt)))
129 } else {
130 rl.SetPrompt(prompt)
131
132 d = nil
133
134 v, errEval := vm.Eval(s)
135 if errEval != nil {
136 var oerr *otto.Error
137 if errors.As(errEval, &oerr) {
138 if _, err = io.Copy(rl.Stdout(), strings.NewReader(oerr.String())); err != nil {
139 return fmt.Errorf("write out: %w", err)
140 }
141 } else {
142 if _, err = io.Copy(rl.Stdout(), strings.NewReader(errEval.Error())); err != nil {
143 return fmt.Errorf("write out: %w", err)
144 }
145 }
146 } else {
147 if _, err = rl.Stdout().Write([]byte(v.String() + "\n")); err != nil {
148 return fmt.Errorf("write out: %w", err)
149 }
150 }
151 }
152
153 rl.Refresh()
154 }
155
156 return rl.Close()
157 }