Skip to content

Commit

Permalink
Fix weird repl scoping issue.
Browse files Browse the repository at this point in the history
This would cause locals-saving in the repl to not work on certain
things because the second reference would be considered a clash and
get mangled to have a zero on the end in the compiled output.

See https://todo.sr.ht/~technomancy/fennel/85
  • Loading branch information
technomancy committed Sep 24, 2021
1 parent 9337fb3 commit 20e6b50
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
32 changes: 14 additions & 18 deletions src/fennel/repl.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,14 @@ For more information about the language, see https://fennel-lang.org/reference")
(compiler.metadata:set commands.apropos-show-docs :fnl/docstring
"Print all documentations matching a pattern in function name")

(fn load-plugin-commands []
(when (and utils.root utils.root.options utils.root.options.plugins)
(each [_ plugin (ipairs utils.root.options.plugins)]
(each [name f (pairs plugin)]
;; first function to provide a command should win
(match (name:match "^repl%-command%-(.*)")
cmd-name (tset commands cmd-name (or (. commands cmd-name) f)))))))

(fn run-command-loop [input read loop env on-values on-error scope chars]
(load-plugin-commands)
(fn load-plugin-commands [plugins]
(each [_ plugin (ipairs (or plugins []))]
(each [name f (pairs plugin)]
;; first function to provide a command should win
(match (name:match "^repl%-command%-(.*)")
cmd-name (tset commands cmd-name (or (. commands cmd-name) f))))))

(fn run-command-loop [input read loop env on-values on-error scope chars plugins]
(let [command-name (input:match ",([^%s/]+)")]
(match (. commands command-name)
command (command env read on-values on-error scope chars)
Expand Down Expand Up @@ -286,15 +284,15 @@ For more information about the language, see https://fennel-lang.org/reference")
(read reset) (parser.parser (fn [parser-state]
(let [c (byte-stream parser-state)]
(table.insert chars c)
c)))
scope (compiler.make-scope)]
c)))]
(set (opts.env opts.scope) (values env (compiler.make-scope)))
;; use metadata unless we've specifically disabled it
(set opts.useMetadata (not= options.useMetadata false))
(when (= opts.allowedGlobals nil)
(set opts.allowedGlobals (specials.current-global-names opts.env)))
(when opts.registerCompleter
(opts.registerCompleter (partial completer env scope)))
(set (utils.root.options utils.root.scope) (values opts scope))
(opts.registerCompleter (partial completer env opts.scope)))
(load-plugin-commands opts.plugins)

(fn print-values [...]
(let [vals [...]
Expand All @@ -318,12 +316,10 @@ For more information about the language, see https://fennel-lang.org/reference")
(loop))
(command? src-string)
(run-command-loop src-string read loop env on-values on-error
scope chars)
opts.scope chars)
(when parse-ok? ; if this is false, we got eof
(match (pcall compiler.compile x (doto opts
(tset :env env)
(tset :source src-string)
(tset :scope scope)))
(tset :source src-string)))
(false msg) (do
(clear-stream)
(on-error :Compile msg))
Expand Down
5 changes: 2 additions & 3 deletions test/repl.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,9 @@
(fn test-code []
(let [(send comp) (wrap-repl)]
(send "(local {: foo} (require :test.mod.foo7))")
(l.assertEquals (comp "fo") [:for :foo])
;; repro case for https://todo.sr.ht/~technomancy/fennel/85
;; (l.assertEquals (send "(foo)") [:foo])
))
(l.assertEquals (send "(foo)") [:foo])
(l.assertEquals (comp "fo") [:for :foo])))

;; Skip REPL tests in non-JIT Lua 5.1 only to avoid engine coroutine
;; limitation. Normally we want all tests to run on all versions, but in
Expand Down

0 comments on commit 20e6b50

Please sign in to comment.