bakpakin / Fennel

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

Failing to compile certain Luakit config #443

Closed fosskers closed 1 year ago

fosskers commented 1 year ago

Hi again, forgive me if this is the wrong place to report. I figured I'd bring this up before adding anything to the Wiki regarding Luakit, since I'd like to get to the bottom of it.

I have the following extremely simple Luakit config in userconf.lua:

require("fennel").install().dofile("/home/colin/.config/luakit/config.fnl")

with the following within config.fnl:

(local modes (require :modes))
(local adblock (require :adblock))
(local select (require :select))

(modes.remap_binds "normal" [["n" "j" false]
                             ["e" "k" false]
                             ["N" "J" false]
                             ["E" "K" false]])

(adblock.load false "easylist.txt")
(adblock.load false "easyprivacy.txt")
(adblock.load false "fanboy-annoyance.txt")

(tset select "label_maker" (fn [] (interleave "arst" "neio")))

Everything works except the last line containing interleave. If I attempt to open Luakit with this uncommented, I'm told:

[    0.999346] E [core/common/lualib]: Lua error: /home/colin/.config/luakit/config.fnl:24:35 Compile error: unknown identifier: interleave

                 (tset select "label_maker" (fn [] (interleave "arst" "neio")))

Note that I'm following their documentation to the letter, and oddly, if I instead add the equivalent Lua directly to my userconf.lua, there are no problems:

local select = require("select")

select.label_maker = function()
  return interleave("arst", "neio")
end

The Lua produced by Fennel is mostly the same:

local select = require("select")

local function _1_()
  return interleave("arst", "neio")
end
select["label_maker"] = _1_

Notice that interleave isn't prefixed by a table name, for instance select.interleave. It's just bare, as if part of the global namespace. Being new to both Fennel and Lua, it's not clear to me why this would work, since that's not a vanilla Lua function.

Does anything come to mind as to why this might be happening? If no, I'll go pound on Luakit's door. Thank you kindly.

technomancy commented 1 year ago

The error you're seeing is coming from the Fennel compiler; it's saying that you're trying to reference a global that doesn't exist. In fact, interleave is not a global; the only place it's defined in the Luakit source is inside the label_styles table here: https://github.com/luakit/luakit/blob/develop/lib/select_wm.lua#L67

But when you run the Lua, it turns out the Lua is able to resolve it somehow. They are probably using a metatable on the _G globals table to magically resolve it at runtime. This is a weird technique that ends up causing a lot of problems and is best avoided, but in this case it's outside your control to fix.

The simplest way to work around this is to just turn off global checking altogether, but that would make it so the compiler couldn't catch typos, so I don't recommend this. If you try replacing interleave with _G.interleave you can bypass the strict global checks just for this one call but still benefit from its checks in the rest of your code; I think that's probably the best workaround for you.

But the best fix is for Luakit to stop doing this provide the function in a module for you to use normally.

Unrelatedly, the last line would be better as this instead: (fn select.label_maker [] (_G.interleave "arst" "neio"))

fosskers commented 1 year ago

Thank you! Yes I suspected they were up to some internal funny business. By prefixing interleave by _G the browser actually starts now, but the resetting of label_maker fails (without crashing the browser). I'm told:

Lua error: attempt to index global '_G' (a nil value)

Nevertheless:

But the best fix is for Luakit to stop doing this provide the function in a module for you to use normally.

I agree.

Unrelatedly, the last line would be better as this instead

Ah so instead of explicitly using tset, the above form is equivalent to overwriting the function defined with that name in that table?

technomancy commented 1 year ago

Oh dear, if they've removed _G altogether then that's probably a lost cause and you'll need to turn off strict global checks altogether. You can do that with: require("fennel").install({allowedGlobals=false}).dofile("/home/colin/.config/luakit/config.fnl")

That goof might be enough to report to them as a bug; they really shouldn't be leaving the globals table out altogether.

fosskers commented 1 year ago

It shall be done, that's for the feedback.

Update: Oops... even with allowsGlobals=false it's still failing, claiming interleave doesn't exist. Either way I'll still report this upstream.

fosskers commented 1 year ago

Mysteriously fixed by following this example: https://github.com/luakit/luakit/issues/782#issuecomment-487839783

It looks like the lambda label_maker has multi-arity, and can accept an arg. The arg is a table containing the missing symbols, like interleave. This is otherwise undocumented but is hinted at in the code.

EDIT: It is technically documented, just very vaguely:

This function is executed on the web process, with a custom environment that provides access to the label composer functions.