Author Topic: List of Global Variables - Can you see where they come from ?  (Read 209 times)

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2339
  • Karma: +32/-45
  • Life Moves Pretty Fast....
    • Node Central
List of Global Variables - Can you see where they come from ?
« on: September 19, 2017, 10:25:50 am »
Hi

Looking at the LUA webpage, they provide code to list all of your global variables - https://www.lua.org/cgi-bin/demo?globals

Code: [Select]
-- globals.lua
-- show all global variables

local seen={}

function dump(t,i)
seen[t]=true
local s={}
local n=0
for k in pairs(t) do
n=n+1 s[n]=k
end
table.sort(s)
for k,v in ipairs(s) do
print(i,v)
v=t[v]
if type(v)=="table" and not seen[v] then
dump(v,i.."\t")
end
end
end

dump(_G,"")

My assumption is that these are all loaded on start up, but does anyone know if there is a way to see which ones were system generated and which are user generated (e.g via me in a scene or in lua start up).

I'm guessing not, but thought i would ask the question.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5181
  • Karma: +223/-67
  • "Less is more"
Re: List of Global Variables - Can you see where they come from ?
« Reply #1 on: September 19, 2017, 12:11:19 pm »
No, absolutely not.  A global is a global.

Although... you could generate a list of all the globals which exist when Lua Startup is run, and compare that to the list made after your code has run.

However... you shouldn't be creating random globals all over the place.  Best practice would be to place these in a single global table:

Code: [Select]
MyGlobals = {
  a = 1,
  b = 2,
  version = "4.2",
}

and then access them in your code later:

Code: [Select]
local v = MyGlobals.version

3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline Matsohl

  • Sr. Newbie
  • *
  • Posts: 27
  • Karma: +4/-1
Re: List of Global Variables - Can you see where they come from ?
« Reply #2 on: September 19, 2017, 02:12:27 pm »
Yes, of course. Thank you, solved a old problem for me  :)

Offline rigpapa

  • Full Member
  • ***
  • Posts: 122
  • Karma: +18/-0
Re: List of Global Variables - Can you see where they come from ?
« Reply #3 on: September 19, 2017, 03:37:07 pm »
I insert this code into my plugin modules during development. It alerts me to any creation of global variables or use of an undeclared global (which often implies an undeclared local) during the execution of the module in which it's placed.

local mt = getmetatable(_G)
if mt == nil then
  mt = {}
  setmetatable(_G, mt)
end

__STRICT = true
mt.__declared = {}

mt.__newindex = function (t, n, v)
  if __STRICT and not mt.__declared[n] then
    local w = debug.getinfo(2, "S").what
    if w ~= "main" and w ~= "C" then
      luup.log("ASSIGNMENT TO GLOBAL "..n)
      -- error("assign to undeclared variable '"..n.."'", 2)
    end
    mt.__declared[n] = true
  end
  rawset(t, n, v)
end

mt.__index = function (t, n)
  if not mt.__declared[n] and debug.getinfo(2, "S").what ~= "C" then
    luup.log("REFERENCE TO UNDECLARED GLOBAL " .. n)
    luup.log(debug.traceback())
    -- error("variable '"..n.."' is not declared", 2)
  end
  return rawget(t, n)
end


By the way, this was taken from Lua docs, so it's been around.
« Last Edit: September 19, 2017, 03:42:37 pm by rigpapa »
Author of Rachio, Deus Ex Machina II, SiteSensor, and Auto Virtual Thermostat plugins. Using Vera Plus (1.7.3015), Vera3 (1.7.855), 50 dimmers and 40 switches (mostly Leviton, some Linear and GE), a dozen sensors, a truckload of PLEG, and of course, my own plugins.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5181
  • Karma: +223/-67
  • "Less is more"
Re: List of Global Variables - Can you see where they come from ?
« Reply #4 on: September 19, 2017, 06:00:26 pm »
Yes, that's a classic use of a metatable.  Perhaps a bit strict for the OP's application, but it could be modified to log what/when a new global is created.  I suppose that, if added to some of the debug module functionality, it could exactly pinpoint which bit of code was creating the global, but that definitely is OTT.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline rigpapa

  • Full Member
  • ***
  • Posts: 122
  • Karma: +18/-0
Re: List of Global Variables - Can you see where they come from ?
« Reply #5 on: September 19, 2017, 09:11:25 pm »
Yes, that's a classic use of a metatable.  Perhaps a bit strict for the OP's application, but it could be modified to log what/when a new global is created.  I suppose that, if added to some of the debug module functionality, it could exactly pinpoint which bit of code was creating the global, but that definitely is OTT.

It is exactly that--for debugging purposes. I would not recommend it for production, but it's very handy for highlighting small mistakes that can lead to bugs, in the modules, and in other code as well.
Author of Rachio, Deus Ex Machina II, SiteSensor, and Auto Virtual Thermostat plugins. Using Vera Plus (1.7.3015), Vera3 (1.7.855), 50 dimmers and 40 switches (mostly Leviton, some Linear and GE), a dozen sensors, a truckload of PLEG, and of course, my own plugins.

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9611
  • Karma: +733/-136
    • RTS Services Plugins
Re: List of Global Variables - Can you see where they come from ?
« Reply #6 on: September 20, 2017, 10:48:57 am »
Good software practices and object oriented design ... even if the language does not provide a lot of support like lua ...

In general a global variable is a BAD thing. 
You should try to minimize the number of things you place in the global name space.
Your plugin should module should hold your variables and encapsulate them based on the conceptual objects in your plugin.