2
0

readonly.lua 260 B

123456789101112
  1. -- make global variables readonly
  2. local f=function (t,i) error("cannot redefine global variable `"..i.."'",2) end
  3. local g={}
  4. local G=getfenv()
  5. setmetatable(g,{__index=G,__newindex=f})
  6. setfenv(1,g)
  7. -- an example
  8. rawset(g,"x",3)
  9. x=2
  10. y=1 -- cannot redefine `y'