summaryrefslogtreecommitdiff
path: root/script/vm/function.lua
blob: be58bf7ce44be89ea194cb2cb3acaff0ad1ba98e (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
local createMulti = require 'vm.multi'
local valueMgr = require 'vm.value'
local localMgr = require 'vm.local'
local sourceMgr = require 'vm.source'
local listMgr = require 'vm.list'

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

---@class emmyFunction
local mt = {}
mt.__index = mt
mt.type = 'function'
mt._runed = 0
mt._top = 0

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

function mt:getUri()
    local source = self:getSource()
    return source and source.uri or ''
end

function mt:push(source, ischunk)
    if self._removed then
        return
    end
    self._top = self._top + 1
    self.locals[self._top] = {}
    self.finishs[self._top] = source and source.finish or math.maxinteger
end

function mt:markChunk()
    self.chunk[self._top] = true
end

function mt:pop()
    if self._removed then
        return
    end
    local closed = self.finishs[self._top]
    local closedLocals = self.locals[self._top]
    self.locals[self._top] = nil
    self.chunk[self._top] = nil
    for _, loc in pairs(closedLocals) do
        loc:close(closed)
    end
    self._top = self._top - 1
end

function mt:saveLocal(name, loc)
    if self._removed then
        return
    end
    if loc.type ~= 'local' then
        error('saveLocal必须是local')
    end
    if not loc:getSource() then
        return
    end
    local old = self:loadLocal(name)
    if old then
        loc:shadow(old)
    end
    self.locals[self._top][name] = loc
end

function mt:saveUpvalue(name, loc)
    if self._removed then
        return
    end
    if loc.type ~= 'local' then
        error('saveLocal必须是local')
    end
    self.upvalues[name] = loc
end

function mt:loadLocal(name)
    for i = self._top, 1, -1 do
        local locals = self.locals[i]
        local loc = locals[name]
        if loc then
            return loc
        end
        if self.chunk[i] then
            break
        end
    end
    local uv = self.upvalues[name]
    if uv then
        return uv
    end
    return nil
end

function mt:eachLocal(callback)
    local mark = {}
    for i = self._top, 1, -1 do
        local locals = self.locals[i]
        for name, loc in pairs(locals) do
            if not mark[name] then
                mark[name] = true
                local res = callback(name, loc)
                if res ~= nil then
                    return res
                end
            end
        end
        if self.chunk[i] then
            break
        end
    end
    for name, loc in pairs(self.upvalues) do
        if not mark[name] then
            mark[name] = true
            local res = callback(name, loc)
            if res ~= nil then
                return res
            end
        end
    end
    return nil
end

function mt:saveLabel(label)
    if self._removed then
        return
    end
    if not self._label then
        self._label = {}
    end
    self._label[#self._label+1] = label
end

function mt:loadLabel(name)
    if not self._label then
        return nil
    end
    for _, label in ipairs(self._label) do
        if label:getName() == name then
            return label
        end
    end
    return nil
end

function mt:rawSetReturn(index, value)
    if self._removed then
        return
    end
    self:set('hasReturn', true)
    if not self.returns then
        self.returns = createMulti()
    end
    if value then
        self.returns:set(index, value)
        if self._global then
            value:markGlobal()
        end
    end
end

function mt:setReturn(index, value)
    local emmy = self._emmyReturns and self._emmyReturns[index]
    if emmy then
        if emmy:bindType() or emmy:bindGeneric() then
            return
        end
    end
    return self:rawSetReturn(index, value)
end

function mt:mergeReturn(index, value)
    if self._removed then
        return
    end
    local emmy = self._emmyReturns and self._emmyReturns[index]
    if emmy then
        if emmy:bindType() or emmy:bindGeneric() then
            return
        end
    end
    self:set('hasReturn', true)
    if not self.returns then
        self.returns = createMulti()
    end
    if value then
        if self.returns[index] then
            self.returns[index]:mergeValue(value)
            self.returns[index] = value
        else
            self.returns:set(index, value)
        end
    end
    if self._global then
        value:markGlobal()
    end
end

function mt:getReturn(index)
    if self._removed then
        return nil
    end
    if self.maxReturns and index and self.maxReturns < index then
        return nil
    end
    if not self.returns then
        self.returns = createMulti()
    end
    if index then
        return self.returns:get(index)
    else
        return self.returns
    end
end

function mt:returnDots(index)
    if not self.returns then
        self.returns = createMulti()
    end
    --self.returns[index] = createMulti()
end

function mt:loadDots()
    if not self._dots then
        self._dots = createMulti()
    end
    self._dotsLoad = true
    return self._dots
end

function mt:setObject(value, source)
    self._objectValue = value
    self._objectSource = source
end

function mt:getObject()
    return self._objectSource, self._objectValue
end

function mt:hasRuned()
    return self._runed > 0
end

function mt:needSkip()
    return self._runed > 3
end

---@param vm VM
function mt:run(vm)
    if self._removed then
        return
    end
    if not self:getSource() then
        return
    end

    self._runed = self._runed + 1

    -- 第一次运行函数时,创建函数的参数
    if self._runed == 1 then
        -- 如果是面向对象形式的函数,创建隐藏的参数self
        if self._objectSource then
            local loc = localMgr.create('self', vm:instantSource(self._objectSource), self._objectValue)
            loc:set('hide', true)
            loc:set('start', self:getSource().start)
            loc:close(self:getSource().finish)
            self:saveUpvalue('self', loc)
            self.args[#self.args+1] = loc
        end

        -- 显性声明的参数
        self:createArgs(vm)
    end

    if self:needSkip() then
        return
    end

    -- 向局部变量中填充参数
    for i, loc in ipairs(self.args) do
        loc:setValue(self.argValues[i])
        local emmyParam = self:findEmmyParamByName(loc:getName())
        if emmyParam then
            local typeObj = emmyParam:bindType()
            if typeObj then
                loc:getValue():setEmmy(typeObj)
            end
            local genericObj = emmyParam:bindGeneric()
            if genericObj then
                genericObj:setValue(loc:getValue())
            end
        end
    end
    if self._dots then
        local emmyParam = self:findEmmyParamByName('...')
        self._dots = createMulti()
        for i = #self.args + 1, #self.argValues do
            local value = self.argValues[i]
            self._dots:push(value)
            if emmyParam then
                local typeObj = emmyParam:bindType()
                if typeObj then
                    value:setEmmy(typeObj)
                end
                local genericObj = emmyParam:bindGeneric()
                if genericObj then
                    genericObj:setValue(value)
                end
            end
        end
        if emmyParam then
            local typeObj = emmyParam:bindType()
            if typeObj then
                self._dots:setEmmy(typeObj)
            end
            local genericObj = emmyParam:bindGeneric()
            if genericObj then
                local value = self._dots:first()
                if value then
                    genericObj:setValue(value)
                end
            end
        end
    end

    -- 填充返回值
    if self._emmyReturns then
        for i, rtn in ipairs(self._emmyReturns) do
            local value = vm:createValue('nil', rtn:getSource())
            local typeObj = rtn:bindType()
            if typeObj then
                value:setEmmy(typeObj)
            end
            local genericObj = rtn:bindGeneric()
            if genericObj then
                local destValue = genericObj:getValue()
                if destValue then
                    value:mergeType(destValue)
                end
            end
            self:rawSetReturn(i, value)
        end
    end
end

function mt:eachEmmyReturn(callback)
    if not self._emmyReturns then
        return
    end
    for _, rtn in ipairs(self._emmyReturns) do
        callback(rtn)
    end
end

function mt:setArgs(values)
    for i = 1, #self.argValues do
        self.argValues[i] = nil
    end
    for i = 1, #values do
        self.argValues[i] = values[i]
    end
end

function mt:findEmmyParamByName(name)
    local params = self._emmyParams
    if not params then
        return nil
    end
    for i = #params, 1, -1 do
        local param = params[i]
        if param:getName() == name then
            return param
        end
    end
    return nil
end

function mt:findEmmyParamByIndex(index)
    local arg = self.args[index]
    if not arg then
        return nil
    end
    local name = arg:getName()
    return self:findEmmyParamByName(name)
end

function mt:addArg(name, source, value, close)
    local loc = localMgr.create(name, source, value)
    loc:close(close)
    self:saveUpvalue(name, loc)
    self.args[#self.args+1] = loc
    return loc
end

function mt:createArg(vm, arg, close)
    vm:instantSource(arg)
    arg:set('arg', self)
    if arg.type == 'name' then
        vm:instantSource(arg)
        local value = valueMgr.create('nil', arg)
        self:addArg(arg[1], arg, value, close)
    elseif arg.type == '...' then
        self._dots = createMulti()
        self._dotsSource = arg
    end
end

function mt:createLibArg(arg, source)
    if arg.type == '...' then
        self._dots = createMulti()
    else
        local name = arg.name or '_'
        local loc = localMgr.create(name, source, valueMgr.create('any', source))
        self:saveUpvalue(name, loc)
        self.args[#self.args+1] = loc
    end
end

function mt:hasDots()
    return self._dots ~= nil
end

function mt:createArgs(vm)
    if not self:getSource() then
        return
    end
    local args = self:getSource().arg
    if not args then
        return
    end
    local close = self:getSource().finish
    if args.type == 'list' then
        for _, arg in ipairs(args) do
            self:createArg(vm, arg, close)
        end
    else
        self:createArg(vm, args, close)
    end
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:getSource()
    if self._removed then
        return nil
    end
    return listMgr.get(self.source)
end

function mt:kill()
    if self._removed then
        return
    end
    self._removed = true
    listMgr.clear(self.id)
end

function mt:markGlobal()
    if self._global then
        return
    end
    self._global = true
    if self.returns then
        self.returns:eachValue(function (_, v)
            v:markGlobal()
        end)
    end
end

function mt:setEmmy(params, returns, overLoads)
    if params then
        self._emmyParams = params
        for _, param in ipairs(params) do
            param:getSource():set('emmy function', self)
            if param:getSource()[1] then
                param:getSource()[1]:set('emmy function', self)
            end
        end
    end
    if returns then
        self._emmyReturns = returns
        for _, rtn in ipairs(returns) do
            rtn:getSource():set('emmy function', self)
        end
    end
    if overLoads then
        self._emmyOverLoads = overLoads
        for _, ol in ipairs(overLoads) do
            ol:getSource():set('emmy function', self)
        end
    end
end

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

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

function mt:getEmmyParams()
    return self._emmyParams
end

function mt:getEmmyOverLoads()
    return self._emmyOverLoads
end

local function create(source)
    if not source then
        error('Function need source')
    end
    local id = source.id
    if not id then
        error('Not instanted source')
    end
    local self = setmetatable({
        source = id,
        locals = {},
        upvalues = {},
        chunk = {},
        finishs = {},
        args = {},
        argValues = {},
    }, mt)

    local id = listMgr.add(self)
    self.id = id
    Watch[self] = id
    return self
end

return {
    create = create,
    watch = Watch,
}