| 1 |
local function parse_exports (export_defs) |
| 2 |
local exports = {} |
| 3 |
local i = 1 |
| 4 |
while true do |
| 5 |
local key |
| 6 |
local begin |
| 7 |
|
| 8 |
local external_name, internal_name |
| 9 |
local i2 |
| 10 |
|
| 11 |
external_name, i2 = string.match(export_defs, "^%s*([%a_][%w_]*)()", i) |
| 12 |
if i2 ~= nil then i = i2 else break end |
| 13 |
|
| 14 |
|
| 15 |
internal_name, i2 = string.match(export_defs, "^%s*=%s*([%a_][%w_]*)()", i) |
| 16 |
if i2 ~= nil then i = i2 end |
| 17 |
|
| 18 |
|
| 19 |
assert(exports[external_name] == nil, |
| 20 |
"two module exports with the same name!") |
| 21 |
exports[external_name] = internal_name or external_name |
| 22 |
|
| 23 |
|
| 24 |
i2 = string.match(export_defs, "^%s*,()", i) |
| 25 |
if i2 ~= nil then i = i2 else break end |
| 26 |
end |
| 27 |
i = string.match(export_defs, "^%s*()", i) |
| 28 |
assert(i == string.len(export_defs) + 1, |
| 29 |
"invalid exports starting at " .. i) |
| 30 |
|
| 31 |
return exports |
| 32 |
end |
| 33 |
|
| 34 |
|
| 35 |
local function make_private_table () |
| 36 |
local getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget |
| 37 |
|
| 38 |
local mt = {} |
| 39 |
|
| 40 |
mt.__declared = {} |
| 41 |
|
| 42 |
local function what () |
| 43 |
local d = getinfo(3, "S") |
| 44 |
return d and d.what or "C" |
| 45 |
end |
| 46 |
|
| 47 |
mt.__newindex = function (t, n, v) |
| 48 |
if not mt.__declared[n] then |
| 49 |
local w = what() |
| 50 |
if w ~= "main" and w ~= "C" then |
| 51 |
error("assign to undeclared variable '"..n.."'", 2) |
| 52 |
end |
| 53 |
mt.__declared[n] = true |
| 54 |
end |
| 55 |
rawset(t, n, v) |
| 56 |
end |
| 57 |
|
| 58 |
mt.__index = function (t, n) |
| 59 |
if _G[n] then |
| 60 |
return _G[n] |
| 61 |
elseif not mt.__declared[n] and what() ~= "C" then |
| 62 |
error("variable '"..n.."' is not declared", 2) |
| 63 |
else |
| 64 |
return rawget(t, n) |
| 65 |
end |
| 66 |
end |
| 67 |
|
| 68 |
return setmetatable({}, mt) |
| 69 |
end |
| 70 |
|
| 71 |
if dokidoki_disable_debug then |
| 72 |
function make_private_table () |
| 73 |
return setmetatable({}, {__index = _G}) |
| 74 |
end |
| 75 |
end |
| 76 |
|
| 77 |
return function (export_defs) |
| 78 |
local private |
| 79 |
local exports = parse_exports(export_defs) |
| 80 |
|
| 81 |
local function get_module_exports () |
| 82 |
local exported_values = {} |
| 83 |
for external, internal in pairs(exports) do |
| 84 |
exported_values[external] = private[internal] |
| 85 |
end |
| 86 |
return exported_values |
| 87 |
end |
| 88 |
|
| 89 |
local function import (t) |
| 90 |
if type(t) == 'string' then t = require(t) end |
| 91 |
for k, v in pairs(t) do |
| 92 |
assert(rawget(private, k) == nil and rawget(_G, k) == nil, |
| 93 |
"import collision for variable " .. k) |
| 94 |
rawset(private, k, v) |
| 95 |
end |
| 96 |
end |
| 97 |
|
| 98 |
private = make_private_table() |
| 99 |
rawset(private, "_P", private) |
| 100 |
rawset(private, "get_module_exports", get_module_exports) |
| 101 |
rawset(private, "import", import) |
| 102 |
|
| 103 |
setfenv(2, private) |
| 104 |
end |