summaryrefslogtreecommitdiff
path: root/script/vm/module.lua
blob: 60191bf39ff47b5bd642a8c8b2474ee7cf887d71 (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
52
53
54
55
56
local mt = require 'vm.manager'
local createMulti = require 'vm.multi'

--[[
function module(name, ...)
    local env = {}
    for _, opt in ipairs {...} do
        opt(env)
    end
    @ENV = env
end
--]]
function mt:callModuel(func, values)
    local envLoc = self:loadLocal('@ENV')
    if not envLoc then
        return
    end
    local source = self:getDefaultSource()
    local newEnvValue = self:createValue('table', source)
    local args = createMulti()

    args:push(newEnvValue)

    for i = 2, #values do
        local value = values[i]
        -- opt(env)
        self:call(value, args, source)
    end

    -- @ENV = env
    envLoc:setValue(newEnvValue)
end

--[[
function package.seeall(env)
    setmetatable(env, { __index = @ENV })
end
--]]
function mt:callSeeAll(func, values)
    local newEnv = values[1]
    if not newEnv then
        return
    end
    local envLoc = self:loadLocal('@ENV')
    if not envLoc then
        return
    end
    local oldEnv = envLoc:getValue()
    if not oldEnv then
        return
    end
    local source = self:getDefaultSource()
    local meta = self:createValue('table', source)
    meta:setChild('__index', oldEnv, source)
    newEnv:setMetaTable(meta)
end