diff options
author | alandonovan <adonovan@google.com> | 2019-10-21 14:58:36 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-21 14:58:36 -0400 |
commit | 28350e60855593ffbf8f7402df3417331258ca8b (patch) | |
tree | 237e9a0267dcd34872966107c89693a7fe6f52c0 /repl | |
parent | 58de16fb0ee76530b35384f2854db6b4e0004ea0 (diff) | |
download | starlark-go-28350e60855593ffbf8f7402df3417331258ca8b.tar.gz |
resolve: retain globals across REPL chunks (#247)
Previously, in the REPL, the globals bound by one chunk would become
the "predeclared" bindings of the next chunk. However, this produced
the wrong result for [x = x + 1] because both occurrences of x would
resolve to the same global x, which was undefined when the right-hand
side expression was evaluated, leading to a dynamic error. It would
fail for [x += 1] for a similar reason.
This change extends the resolver to allow clients to provide it with a
non-empty set of module globals. Thus the globals of one chunk remain
the globals of the next chunk, and the predeclared set is always empty.
The new API functions is intended for use only by the REPL.
Fixes #233
Diffstat (limited to 'repl')
-rw-r--r-- | repl/repl.go | 23 |
1 files changed, 3 insertions, 20 deletions
diff --git a/repl/repl.go b/repl/repl.go index d766eff..5f2ee36 100644 --- a/repl/repl.go +++ b/repl/repl.go @@ -133,26 +133,9 @@ func rep(rl *readline.Instance, thread *starlark.Thread, globals starlark.String if v != starlark.None { fmt.Println(v) } - } else { - // compile - prog, err := starlark.FileProgram(f, globals.Has) - if err != nil { - PrintError(err) - return nil - } - - // execute (but do not freeze) - res, err := prog.Init(thread, globals) - if err != nil { - PrintError(err) - } - - // The global names from the previous call become - // the predeclared names of this call. - // If execution failed, some globals may be undefined. - for k, v := range res { - globals[k] = v - } + } else if err := starlark.ExecREPLChunk(f, thread, globals); err != nil { + PrintError(err) + return nil } return nil |