File: readonly.lua

package info (click to toggle)
lua5.1 5.1.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,344 kB
  • sloc: ansic: 12,763; makefile: 426; sh: 55
file content (12 lines) | stat: -rw-r--r-- 260 bytes parent folder | download | duplicates (67)
1
2
3
4
5
6
7
8
9
10
11
12
-- make global variables readonly

local f=function (t,i) error("cannot redefine global variable `"..i.."'",2) end
local g={}
local G=getfenv()
setmetatable(g,{__index=G,__newindex=f})
setfenv(1,g)

-- an example
rawset(g,"x",3)
x=2
y=1	-- cannot redefine `y'