Nano-scale chemically powered sphere-dimer motor https://colberg.org/nano-dimer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strict.lua 992B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. -- strict.lua
  2. -- checks uses of undeclared global variables
  3. -- All global variables must be 'declared' through a regular assignment
  4. -- (even assigning nil will do) in a main chunk before being used
  5. -- anywhere or assigned to inside a function.
  6. -- distributed under the Lua license: http://www.lua.org/license.html
  7. local getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget
  8. local mt = getmetatable(_G)
  9. if mt == nil then
  10. mt = {}
  11. setmetatable(_G, mt)
  12. end
  13. mt.__declared = {}
  14. local function what ()
  15. local d = getinfo(3, "S")
  16. return d and d.what or "C"
  17. end
  18. mt.__newindex = function (t, n, v)
  19. if not mt.__declared[n] then
  20. local w = what()
  21. if w ~= "main" and w ~= "C" then
  22. error("assign to undeclared variable '"..n.."'", 2)
  23. end
  24. mt.__declared[n] = true
  25. end
  26. rawset(t, n, v)
  27. end
  28. mt.__index = function (t, n)
  29. if not mt.__declared[n] and what() ~= "C" then
  30. error("variable '"..n.."' is not declared", 2)
  31. end
  32. return rawget(t, n)
  33. end