diff options
| author | Philipp Janda <siffiejoe@gmx.net> | 2015-04-11 17:09:37 +0200 |
|---|---|---|
| committer | Philipp Janda <siffiejoe@gmx.net> | 2015-04-11 17:09:37 +0200 |
| commit | ddeb3f7a1e25667694eaf3c8bf639b66d6809cb4 (patch) | |
| tree | a02b62ce9ad9135dd8dc6aab13940e96ea87dcfe /compat53/module.lua | |
| parent | ba8686d6af119fc7ecf734a2d2b6e319c8d74df5 (diff) | |
| download | lua-compat-5.3-ddeb3f7a1e25667694eaf3c8bf639b66d6809cb4.tar.gz lua-compat-5.3-ddeb3f7a1e25667694eaf3c8bf639b66d6809cb4.tar.bz2 lua-compat-5.3-ddeb3f7a1e25667694eaf3c8bf639b66d6809cb4.zip | |
split compat53 Lua module into separate modules that do/don't modify the global env
Diffstat (limited to 'compat53/module.lua')
| -rw-r--r-- | compat53/module.lua | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/compat53/module.lua b/compat53/module.lua new file mode 100644 index 0000000..f6fd1f7 --- /dev/null +++ b/compat53/module.lua | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | local _G, _VERSION, debug, error, require, setfenv, setmetatable = | ||
| 2 | _G, _VERSION, debug, error, require, setfenv, setmetatable | ||
| 3 | local lua_version = _VERSION:sub(-3) | ||
| 4 | local M = require("compat53.base") | ||
| 5 | |||
| 6 | local function findmain() | ||
| 7 | local i = 3 | ||
| 8 | local info = debug.getinfo(i, "fS") | ||
| 9 | while info do | ||
| 10 | if info.what == "main" then | ||
| 11 | return info.func | ||
| 12 | end | ||
| 13 | i = i + 1 | ||
| 14 | info = debug.getinfo(i, "fS") | ||
| 15 | end | ||
| 16 | end | ||
| 17 | |||
| 18 | local main = findmain() | ||
| 19 | if not main then | ||
| 20 | error("must require 'compat53.module' from Lua") | ||
| 21 | end | ||
| 22 | local env = setmetatable({}, { | ||
| 23 | __index = M, | ||
| 24 | __newindex = _G, | ||
| 25 | }) | ||
| 26 | if lua_version == "5.1" then | ||
| 27 | setfenv(main, env) | ||
| 28 | elseif lua_version == "5.2" or lua_version == "5.3" then | ||
| 29 | debug.setupvalue(main, 1, env) | ||
| 30 | else | ||
| 31 | error("unsupported Lua version") | ||
| 32 | end | ||
| 33 | |||
| 34 | -- return false to force reevaluation on next require | ||
| 35 | return false | ||
| 36 | |||
| 37 | -- vi: set expandtab softtabstop=3 shiftwidth=3 : | ||
