Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua

Home Page:http://www.hammerspoon.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to set global varable in Hammerspoon Lua

Mikeno-Philippe opened this issue · comments

The following code runs well under Lua:

counter, offset = 0, 1

function reset()
	counter = 0
	print(counter)
end

function inc()
	counter = counter + offset
	print(counter)
end

reset() --> 0
inc()    --> 1
inc()    --> 2
reset() --> 0

... but running the same code under Hammerspoon Lua doesn't work as expected, the last print statement gives 2, meaning that the global variable counter has not been reseted.
Thx for your help

I found how to solve this issue using rawset

function declare (name, initval)
	rawset(_G, name, initval or false)
end

counter, offset = 0, 1
declare("counter", 0)
declare("offset", 1)

It works fine on my Hammerspoon. Do you have any code in your config (or loaded Spoons) that would be setting a metatable on the global environment?

What does this code print for you when pasted into the console?

local mt = getmetatable(_G)
if mt then
	print(hs.inspect(mt))
	for k,v in pairs(mt) do
		if type(v) == 'function' then
			local info = debug.getinfo(v)
			if what ~= 'C' then
				print(hs.inspect(info))
			end
		end
	end
else
	print('no metatable on _G')
end

Weird… When you ran into the issue, was it in the console or the config file? If it was the config, try putting the code I sent there instead of in the console and see if that prints anything different.

Actually, just in case _G isn't what's getting used for some reason, can you try using this version instead?

local function checkMT(name, tbl)
	local mt = getmetatable(tbl)
	if mt then
		print(hs.inspect(mt))
		for k,v in pairs(mt) do
			if type(v) == 'function' then
				local info = debug.getinfo(v)
				if what ~= 'C' then
					print(hs.inspect(info))
				end
			end
		end
	else
		print('no metatable on '..name)
	end
end
checkMT('_G', _G)
if rawequal(_ENV, _G) then
	print '_ENV is _G'
else
	print '_ENV is not _G'
	checkMT('_ENV', _ENV)
end

What about when you run it from within your reset and/or inc functions? Also, is there anywhere you're declaring counter and/or offset as locals (or function argument names)?

I don't know of anything else that could interfere with setting global variables. As far as I know, Hammerspoon should be using an unmodified Lua interpreter.

The only other thing I can think of to try right now is moving your entire init.lua file aside and making a new one with just the code from your initial post. If that works correctly, we can try to narrow down exactly where it starts breaking. If it still fails, I'm not sure what to do.

I see a couple of issues in that code that might explain why the counter isn't updating properly:

  • I don't see a definition for that setOffset function anywhere, and even if it exists, any change it makes to offset will get overwritten by the offset = part of the menu item's fn - changing it to whatever setOffset's return value was.
  • This version of reset doesn't seem to actually set counter at all - it just updates the menu's title and shows the alert. Also, it's setting offset to reset's return value, which is nil - meaning that any Increase/Decrease commands after that point will throw an attempt to perform arithmetic on a nil value error and not actually change the counter.

These next two aren't directly related to the counter problem, but:

  • $ and ^ aren't their own keys, at least on most keyboard layouts that I know of, so they don't have keycodes defined in hs.keycodes.map and can't be passed directly to hs.hotkey.bind like that. Assuming that they're Shift-4 and Shift-6 like on my keyboard, you'll need to do something like:
hyper = {"cmd","alt","ctrl"}
hyperShift = {"cmd","alt","ctrl","shift"}

hs.hotkey.bind(hyperShift, "4", function()
	updateCounter(offset)
end)

hs.hotkey.bind(hyperShift, "6", function()
	updateCounter(-offset)
end)
  • "Set offset (" .. offset .. ")": This title will get evaluated once when your config first runs, and won't update when the offset is changed. You can either include something like:
	menuMenu[4].title = "Set offset (" .. offset .. ")"
	menu:setMenu(menuMenu)

in your setOffset function, or change menuMenu to a function so it gets re-evaluated each time the menu opens:

function menuMenu()
	return {
		{ title = "Increase", fn = function() updateCounter(offset) end },
		{ title = "Decrease", fn = function() updateCounter(-offset) end },
		{ title = "-" },
		{ title = "Set offset (" .. offset .. ")" , fn = function() offset = setOffset(offset) end },
		{ title = "Reset counter", fn = function() offset = reset() end }
	}
end

Glad to hear it's working for you.

Concerning both keys (« $ » and « ^ »), they are direct own keys on a French keyboard!

Gotcha. For some reason I wasn't sure if Hammerspoon knew that, though, but I see now that it does properly update hs.keycodes.map to include $ and ^ when you're on that layout.