Skip to content

Commit

Permalink
catch catchable panics, eg 1/0, the rest (infinite recursion) is caug…
Browse files Browse the repository at this point in the history
…ht JS side for wasm
  • Loading branch information
ldemailly committed Jul 19, 2024
1 parent 04c2333 commit a643b31
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,20 @@ func EvalAll(s, macroState *eval.State, in io.Reader, out io.Writer, options Opt

// EvalString can be used from playground etc for single eval.
// returns the eval errors and an array of errors if any.
func EvalString(what string) (string, []string) {
func EvalString(what string) (res string, errs []string) {
defer func() {
if r := recover(); r != nil {
errs = append(errs, fmt.Sprintf("panic: %v", r))
}
}()
s := eval.NewState()
macroState := eval.NewState()
out := &strings.Builder{}
s.Out = out
s.NoLog = true
_, errs := EvalOne(s, macroState, what, out, Options{All: true, ShowEval: true, NoColor: true})
return out.String(), errs
_, errs = EvalOne(s, macroState, what, out, Options{All: true, ShowEval: true, NoColor: true})
res = out.String()
return
}

func Interactive(in io.Reader, out io.Writer, options Options) {
Expand Down

0 comments on commit a643b31

Please sign in to comment.