bakpakin / Fennel

Lua Lisp Language
https://fennel-lang.org
MIT License
2.44k stars 126 forks source link

Fennel no longer supports reading globals from `_G`? #328

Closed russmatney closed 4 years ago

russmatney commented 4 years ago

I hit a surprise this morning when I updated from 4.2 to 5.0 - I'm unable to add to the global environment with fennel code. A repo reproducing the behavior is available here, but the gist is that something like the following logs nil, rather than the value of the supposedly created global.

(global myGlobal "hello")
(print _G.myGlobal)

I'm not sure if this is intentional or not - I may have misread or misunderstood the changelog notes, and I agree globals are, you know, bad, and that part of fennel's design goal is to push people away from writing code like this (eg, lexical scoping and such). At the moment I'm digging into writing games with Love, and following tutorials that take advantage of this kind of behavior in Lua. I don't expect to build a program with a ton of globals, but it seems reasonable to allow this behavior, or at least make it optional (via allowedGlobals = false, perhaps)?

Whether or not this should be supported is probably a separate conversation - as is, this was fairly surprising. I usually try to debug differences in behavior between fennel/lua by reading the compiled lua version, but in this case there appears to be some more magic going on under the hood.

russmatney commented 4 years ago

Apologies! Just found that I need to pass _G as the env:

fennel = require("fennel")
table.insert(package.searchers,
             fennel.make_searcher({
                 env=_G
                 }))

Working as expected now. Thanks for all the work on fennel! Sorry for the noise.