summaryrefslogtreecommitdiff
path: root/loaders/lua5.1/resources/peas-lua-strict.lua
blob: d3458b9abd25aa57010b8c9210bc17fb44aad849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
--
--  Copyright (C) 2015 - Garrett Regier
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU Library General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Library General Public License for more details.
--
-- You should have received a copy of the GNU Library General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin Street, Fifth Floor,
-- Boston, MA 02110-1301, USA.
--
-- Modified version of: http://metalua.luaforge.net/src/lib/strict.lua.html

__STRICT = true

local mt = getmetatable(_G)
if mt == nil then
    mt = {}
    setmetatable(_G, mt)
end

function mt:__newindex(name, value)
    if __STRICT then
        local what = debug.getinfo(2, 'S').what

        if what ~= 'C' then
            error("Attempted to create global variable '" ..
                  tostring(name) .. "'", 2)
        end
    end

    rawset(self, name, value)
end

function mt:__index(name)
    if not __STRICT or debug.getinfo(2, 'S').what == 'C' then
        return rawget(self, name)
    end

    error("Attempted to access nonexistent " ..
          "global variable '" .. tostring(name) .. "'", 2)
end

-- ex:ts=4:et: