summaryrefslogtreecommitdiff
path: root/script/vm/local.lua
blob: 7e8af0f1b1d3c0858bd5120eb86e722799d88574 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
local listMgr = require 'vm.list'

local Sort = 0
local Watch = setmetatable({}, {__mode = 'kv'})

---@class Local
local mt = {}
mt.__index = mt
mt.type = 'local'
mt._close = math.maxinteger

function mt:setValue(value)
    if not value then
        return
    end
    if self.value then
        --self.value:mergeValue(value)
        self.value:mergeType(value)
        self.value = value
    else
        self.value = value
    end
    if self._emmy then
        self.value:setEmmy(self._emmy)
    end
    return value
end

function mt:getValue()
    return self.value
end

function mt:addInfo(tp, source)
    if not source then
        error('No source')
    end
    local id = source.id
    if not id then
        error('Not instanted source')
    end
    if self._info[id] then
        return
    end
    Sort = Sort + 1
    local info = {
        type = tp,
        source = id,
        _sort = Sort,
    }

    self._info[id] = info
end

function mt:eachInfo(callback)
    local list = {}
    for srcId, info in pairs(self._info) do
        local src = listMgr.get(srcId)
        if src then
            list[#list+1] = info
        else
            self._info[srcId] = nil
        end
    end
    table.sort(list, function (a, b)
        return a._sort < b._sort
    end)
    for i = 1, #list do
        local info = list[i]
        local res = callback(info, listMgr.get(info.source))
        if res ~= nil then
            return res
        end
    end
    return nil
end

function mt:set(name, v)
    if not self._flag then
        self._flag = {}
    end
    self._flag[name] = v
end

function mt:get(name)
    if not self._flag then
        return nil
    end
    return self._flag[name]
end

function mt:getName()
    return self.name
end

function mt:shadow(old)
    if not old then
        if not self._shadow then
            return nil
        end
        for i = #self._shadow, 1, -1 do
            local loc = self._shadow[i]
            if not loc:getSource() then
                table.remove(self._shadow, i)
            end
        end
        return self._shadow
    end
    local group = old._shadow
    if not group then
        group = {}
        group[#group+1] = old
    end
    group[#group+1] = self
    self._shadow = group

    if not self:getSource() then
        log.error('local no source')
        return
    end

    old:close(self:getSource().start - 1)
end

function mt:close(pos)
    if pos then
        if pos <= 0 then
            pos = math.maxinteger
        end
        self._close = pos
    else
        return self._close
    end
end

function mt:getSource()
    return listMgr.get(self.source)
end

local EMMY_TYPE = {
    ['emmy.class']        = true,
    ['emmy.type']         = true,
    ['emmy.arrayType']    = true,
    ['emmy.tableType']    = true,
    ['emmy.functionType'] = true,
}

function mt:setEmmy(emmy)
    if not emmy then
        return
    end
    if self.value and EMMY_TYPE[emmy.type] then
        self.value:setEmmy(emmy)
    end
end

---@param comment string
function mt:setComment(comment)
    self._comment = comment
end

---@return string
function mt:getComment()
    return self._comment
end

local function create(name, source, value, tags)
    if not value then
        error('Local must has a value')
    end
    if not source then
        error('No source')
    end
    local id = source.id
    if not id then
        error('Not instanted source')
    end
    local self = setmetatable({
        name = name,
        source = id,
        value = value,
        tags = tags,
        _info = {},
    }, mt)
    Watch[self] = true
    return self
end

return {
    create = create,
    watch = Watch,
}