summaryrefslogtreecommitdiff
path: root/globals.lua
diff options
context:
space:
mode:
Diffstat (limited to 'globals.lua')
-rw-r--r--globals.lua23
1 files changed, 23 insertions, 0 deletions
diff --git a/globals.lua b/globals.lua
new file mode 100644
index 0000000..6ee62a9
--- /dev/null
+++ b/globals.lua
@@ -0,0 +1,23 @@
+local gbl_mt = {}
+local decl = {}
+
+function gbl_mt:__index(k)
+ if not decl[k] then
+ error("unknown global: "..k, 2)
+ end
+ return rawget(self, k)
+end
+
+function gbl_mt:__newindex(k, v)
+ if not decl[k] then
+ error("unknown global: "..k, 2)
+ end
+ rawset(self, k, v)
+end
+
+function global(k, v)
+ decl[k] = true
+ rawset(_G, k, v)
+end
+
+setmetatable(_G, gbl_mt)