~ubuntu-branches/ubuntu/trusty/luajit/trusty

« back to all changes in this revision

Viewing changes to etc/strict.lua

  • Committer: Package Import Robot
  • Author(s): Enrico Tassi
  • Date: 2012-11-03 14:07:56 UTC
  • mfrom: (1.2.1) (15.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20121103140756-z0zcnyrwqlvuc2m5
Tags: 2.0.0+dfsg-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
--
2
 
--
3
 
 
4
 
local getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget
5
 
 
6
 
local mt = getmetatable(_G)
7
 
if mt == nil then
8
 
  mt = {}
9
 
  setmetatable(_G, mt)
10
 
end
11
 
 
12
 
mt.__declared = {}
13
 
 
14
 
local function what ()
15
 
  local d = getinfo(3, "S")
16
 
  return d and d.what or "C"
17
 
end
18
 
 
19
 
mt.__newindex = function (t, n, v)
20
 
  if not mt.__declared[n] then
21
 
    local w = what()
22
 
    if w ~= "main" and w ~= "C" then
23
 
      error("assign to undeclared variable '"..n.."'", 2)
24
 
    end
25
 
    mt.__declared[n] = true
26
 
  end
27
 
  rawset(t, n, v)
28
 
end
29
 
 
30
 
mt.__index = function (t, n)
31
 
  if not mt.__declared[n] and what() ~= "C" then
32
 
    error("variable '"..n.."' is not declared", 2)
33
 
  end
34
 
  return rawget(t, n)
35
 
end
36