summaryrefslogtreecommitdiff
path: root/script/src/service.lua
blob: 2d8a3e64ed3f4184157b4d7b3dd9928b162681fc (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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
local subprocess = require 'bee.subprocess'
local method     = require 'method'
local thread     = require 'bee.thread'
local async      = require 'async'
local rpc        = require 'rpc'
local parser     = require 'parser'
local core       = require 'core'
local lang       = require 'language'
local updateTimer= require 'timer'
local buildVM    = require 'vm'
local sourceMgr  = require 'vm.source'
local localMgr   = require 'vm.local'
local valueMgr   = require 'vm.value'
local chainMgr   = require 'vm.chain'
local functionMgr= require 'vm.function'
local listMgr    = require 'vm.list'
local emmyMgr    = require 'emmy.manager'
local config     = require 'config'
local task       = require 'task'
local files      = require 'files'
local uric       = require 'uri'
local capability = require 'capability'
local plugin     = require 'plugin'

local ErrorCodes = {
    -- Defined by JSON RPC
    ParseError           = -32700,
    InvalidRequest       = -32600,
    MethodNotFound       = -32601,
    InvalidParams        = -32602,
    InternalError        = -32603,
    serverErrorStart     = -32099,
    serverErrorEnd       = -32000,
    ServerNotInitialized = -32002,
    UnknownErrorCode     = -32001,

    -- Defined by the protocol.
    RequestCancelled     = -32800,
}

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

---@class LSP
local mt = {}
mt.__index = mt
---@type files
mt._files = nil

function mt:_callMethod(name, params)
    local optional
    if name:sub(1, 2) == '$/' then
        name = name:sub(3)
        optional = true
    end
    local f = method[name]
    if f then
        local clock = os.clock()
        local suc, res = xpcall(f, debug.traceback, self, params)
        local passed = os.clock() - clock
        if passed > 0.2 then
            log.debug(('Task [%s] takes [%.3f]sec.'):format(name, passed))
        end
        if suc then
            return res
        else
            local ok, r = pcall(table.dump, params)
            local dump = ok and r or '<Cyclic table>'
            log.debug(('Task [%s] failed, params: %s'):format(
                name, dump
            ))
            log.error(res)
            if res:find 'not enough memory' then
                self:restartDueToMemoryLeak()
            end
            return nil, {
                code = ErrorCodes.InternalError,
                message = r .. '\n' .. res,
            }
        end
    end
    if optional then
        return nil
    else
        return nil, {
            code = ErrorCodes.MethodNotFound,
            message = 'MethodNotFound',
        }
    end
end

function mt:responseProto(id, response, err)
    local container = table.container()
    if err then
        container.error = err
    else
        container.result = response
    end
    rpc:response(id, container)
end

function mt:_doProto(proto)
    local id     = proto.id
    local name   = proto.method
    local params = proto.params
    local response, err = self:_callMethod(name, params)
    if not id then
        return
    end
    if type(response) == 'function' then
        response(function (final)
            self:responseProto(id, final)
        end)
    else
        self:responseProto(id, response, err)
    end
end

function mt:clearDiagnostics(uri)
    rpc:notify('textDocument/publishDiagnostics', {
        uri = uri,
        diagnostics = {},
    })
    self._needDiagnostics[uri] = nil
end

---@param uri uri
---@param compiled table
---@param mode string
---@return boolean
function mt:needCompile(uri, compiled, mode)
    self._needDiagnostics[uri] = true
    if self._needCompile[uri] then
        return false
    end
    if not compiled then
        compiled = {}
    end
    if compiled[uri] then
        return false
    end
    self._needCompile[uri] = compiled
    if mode == 'child' then
        table.insert(self._needCompile, uri)
    else
        table.insert(self._needCompile, 1, uri)
    end
    return true
end

function mt:isNeedCompile(uri)
    return self._needCompile[uri]
end

function mt:isWaitingCompile()
    if self._needCompile[1] then
        return true
    else
        return false
    end
end

---@param uri uri
---@param version integer
---@param text string
function mt:saveText(uri, version, text)
    self._lastLoadedVM = uri
    self._files:save(uri, text, version)
    self:needCompile(uri)
end

---@param uri uri
function mt:isDeadText(uri)
    return self._files:isDead(uri)
end

---@param uri uri
---@return boolean
function mt:isLua(uri)
    if not self.workspace then
        return true
    end
    local path = self.workspace:absolutePathByUri(uri)
    if not path then
        return false
    end
    if self.workspace:isLuaFile(path) then
        return true
    end
    return false
end

function mt:isIgnored(uri)
    if not self.workspace then
        return true
    end
    if not self.workspace.gitignore then
        return true
    end
    local path = self.workspace:relativePathByUri(uri)
    if not path then
        return true
    end
    if self.workspace.gitignore(path:string()) then
        return true
    end
    return false
end

---@param uri uri
---@param version integer
---@param text string
function mt:open(uri, version, text)
    if not self:isLua(uri) then
        return
    end
    self:saveText(uri, version, text)
    self._files:open(uri, text)
end

---@param uri uri
function mt:close(uri)
    self._files:close(uri)
    if self._files:isLibrary(uri) then
        return
    end
    if not self:isLua(uri) or self:isIgnored(uri) then
        self:removeText(uri)
    end
end

---@param uri uri
---@return boolean
function mt:isOpen(uri)
    return self._files:isOpen(uri)
end

---@param uri uri
---@param path path
---@param text string
function mt:checkReadFile(uri, path, text)
    if not text then
        log.debug('No file: ', path)
        return false
    end
    local size = #text / 1000.0
    if size > config.config.workspace.preloadFileSize then
        log.info(('Skip large file, size: %.3f KB: %s'):format(size, uri))
        return false
    end
    if self:getCachedFileCount() >= config.config.workspace.maxPreload then
        if not self._hasShowHitMaxPreload then
            self._hasShowHitMaxPreload = true
            rpc:notify('window/showMessage', {
                type = 3,
                message = lang.script('MWS_MAX_PRELOAD', config.config.workspace.maxPreload),
            })
        end
        return false
    end
    return true
end

---@param uri uri
---@param path path
---@param buf string
---@param compiled table
function mt:readText(uri, path, buf, compiled)
    if self._files:get(uri) then
        log.debug('Read failed due to duplicate:', uri)
        return
    end
    if not self:isLua(uri) then
        log.debug('Read failed due to not lua:', uri)
        return
    end
    if not self._files:isOpen() and self:isIgnored(uri) then
        log.debug('Read failed due to ignored:', uri)
        return
    end
    local text = buf or io.load(path)
    if not self._files:isOpen() and not self:checkReadFile(uri, path, text) then
        log.debug('Read failed due to check failed:', uri)
        return
    end
    self._files:save(uri, text, 0)
    self:needCompile(uri, compiled)
end

---@param uri uri
---@param path path
---@param buf string
---@param compiled table
function mt:readLibrary(uri, path, buf, compiled)
    if not self:isLua(uri) then
        return
    end
    if not self:checkReadFile(uri, path, buf) then
        return
    end
    self._files:save(uri, buf, 0)
    self._files:setLibrary(uri)
    self:needCompile(uri, compiled)
    self:clearDiagnostics(uri)
end

---@param uri uri
function mt:removeText(uri)
    self._files:remove(uri)
    self:compileVM(uri)
end

function mt:getCachedFileCount()
    return self._files:count()
end

function mt:reCompile()
    if self.global then
        self.global:remove()
    end
    if self.chain then
        self.chain:remove()
    end
    if self.emmy then
        self.emmy:remove()
    end

    local compiled = {}
    self._files:clearVM()

    for _, obj in pairs(listMgr.list) do
        if obj.type == 'source' or obj.type == 'function' then
            obj:kill()
        end
    end

    self.global = core.global(self)
    self.chain  = chainMgr()
    self.emmy   = emmyMgr()
    self.globalValue = nil
    self._compileTask:remove()
    self._needCompile = {}
    local n = 0
    for uri in self._files:eachFile() do
        self:needCompile(uri, compiled)
        n = n + 1
    end
    log.debug('reCompile:', n, self._files:count())

    self:_testMemory('skip')
end

function mt:reDiagnostic()
    for uri in self._files:eachFile() do
        self:clearDiagnostics(uri)
        self._needDiagnostics[uri] = true
    end
end

function mt:clearAllFiles()
    for uri in self._files:eachFile() do
        self:clearDiagnostics(uri)
    end
    self._files:clear()
end

---@param uri uri
function mt:loadVM(uri)
    local file = self._files:get(uri)
    if not file then
        return nil
    end
    if uri ~= self._lastLoadedVM then
        self:needCompile(uri)
    end
    if self._compileTask
        and not self._compileTask:isRemoved()
        and self._compileTask:get 'uri' == uri
    then
        self._compileTask:fastForward()
    else
        self:compileVM(uri)
    end
    if file:getVM() then
        self._lastLoadedVM = uri
    end
    return file:getVM(), file:getLines()
end

function mt:_markCompiled(uri, compiled)
    local newCompiled = self._needCompile[uri]
    if newCompiled then
        newCompiled[uri] = true
        self._needCompile[uri] = nil
    end
    for i, u in ipairs(self._needCompile) do
        if u == uri then
            table.remove(self._needCompile, i)
            break
        end
    end
    if newCompiled == compiled then
        return compiled
    end
    if not compiled then
        compiled = {}
    end
    for k, v in pairs(newCompiled) do
        compiled[k] = v
    end
    return compiled
end

---@param file file
---@return table
function mt:compileAst(file)
    local ast, err, comments = parser:parse(file:getText(), 'lua', config.config.runtime.version)
    file.comments = comments
    if ast then
        file:setAstErr(err)
    else
        if type(err) == 'string' then
            local message = lang.script('PARSER_CRASH', err)
            log.debug(message)
            rpc:notify('window/showMessage', {
                type = 3,
                message = lang.script('PARSER_CRASH', err:match '%.lua%:%d+%:(.+)' or err),
            })
            if message:find 'not enough memory' then
                self:restartDueToMemoryLeak()
            end
        end
    end
    return ast
end

---@param file file
---@param uri uri
function mt:_clearChainNode(file, uri)
    for pUri in file:eachParent() do
        local parent = self._files:get(pUri)
        if parent then
            parent:removeChild(uri)
        end
    end
end

---@param file file
---@param compiled table
function mt:_compileChain(file, compiled)
    if not compiled then
        compiled = {}
    end
    for uri in file:eachChild() do
        self:needCompile(uri, compiled, 'child')
    end
    for uri in file:eachParent() do
        self:needCompile(uri, compiled, 'parent')
    end
end

function mt:_compileGlobal(compiled)
    local uris = self.global:getAllUris()
    for _, uri in ipairs(uris) do
        self:needCompile(uri, compiled, 'global')
    end
end

function mt:_clearGlobal(uri)
    self.global:clearGlobal(uri)
end

function mt:_hasSetGlobal(uri)
    return self.global:hasSetGlobal(uri)
end

---@param uri uri
function mt:compileVM(uri)
    local file = self._files:get(uri)
    if not file then
        self:_markCompiled(uri)
        return nil
    end
    local compiled = self._needCompile[uri]
    if not compiled then
        return nil
    end
    file:removeVM()

    local clock = os.clock()
    local ast = self:compileAst(file)
    local version = file:getVersion()
    local astCost = os.clock() - clock
    if astCost > 0.1 then
        log.warn(('Compile Ast[%s] takes [%.3f] sec, size [%.3f]kb'):format(uri, astCost, #file:getText() / 1000))
    end
    file:clearOldText()

    self:_clearChainNode(file, uri)
    self:_clearGlobal(uri)

    local clock = os.clock()
    local vm, err = buildVM(ast, self, uri, file:getText())
    if vm then
        CachedVM[vm] = true
    end
    if self:isDeadText(uri)
        or file:isRemoved()
        or version ~= file:getVersion()
    then
        if vm then
            vm:remove()
        end
        return nil
    end
    if self._needCompile[uri] then
        self:_markCompiled(uri, compiled)
        self._needDiagnostics[uri] = true
    else
        if vm then
            vm:remove()
        end
        return nil
    end
    file:saveVM(vm, version, os.clock() - clock)

    local clock = os.clock()
    local lines = parser:lines(file:getText(), 'utf8')
    local lineCost = os.clock() - clock
    file:saveLines(lines, lineCost)

    if file:getVMCost() > 0.2 then
        log.debug(('Compile VM[%s] takes: %.3f sec'):format(uri, file:getVMCost()))
    end
    if not vm then
        error(err)
    end

    self:_compileChain(file, compiled)
    if self:_hasSetGlobal(uri) then
        self:_compileGlobal(compiled)
    end

    return file
end

---@param uri uri
function mt:doDiagnostics(uri)
    if not config.config.diagnostics.enable then
        self._needDiagnostics[uri] = nil
        return
    end
    if not self._needDiagnostics[uri] then
        return
    end
    local name = 'textDocument/publishDiagnostics'
    local file = self._files:get(uri)
    if not file
        or file:isRemoved()
        or not file:getVM()
        or file:getVM():isRemoved()
        or self._files:isLibrary(uri)
    then
        self._needDiagnostics[uri] = nil
        self:clearDiagnostics(uri)
        return
    end
    local data = {
        uri   = uri,
        vm    = file:getVM(),
        lines = file:getLines(),
        version = file:getVM():getVersion(),
    }
    local res = self:_callMethod(name, data)
    if self:isDeadText(uri) then
        return
    end
    if file:getVM():getVersion() ~= data.version then
        return
    end
    if self._needDiagnostics[uri] then
        self._needDiagnostics[uri] = nil
    else
        return
    end
    if res then
        rpc:notify(name, {
            uri = uri,
            diagnostics = res,
        })
    else
        self:clearDiagnostics(uri)
    end
end

---@param uri uri
---@return file
function mt:getFile(uri)
    return self._files:get(uri)
end

---@param uri uri
---@return VM
---@return table
---@return string
function mt:getVM(uri)
    local file = self._files:get(uri)
    if not file then
        return nil
    end
    return file:getVM(), file:getLines(), file:getText()
end

---@param uri uri
---@return string
---@return string
function mt:getText(uri)
    local file = self._files:get(uri)
    if not file then
        return nil
    end
    return file:getText(), file:getOldText()
end

function mt:getComments(uri)
    local file = self._files:get(uri)
    if not file then
        return nil
    end
    return file:getComments()
end

---@param uri uri
---@return table
function mt:getAstErrors(uri)
    local file = self._files:get(uri)
    if not file then
        return nil
    end
    return file:getAstErr()
end

---@param child uri
---@param parent uri
function mt:compileChain(child, parent)
    local parentFile = self._files:get(parent)
    local childFile = self._files:get(child)

    if not parentFile or not childFile then
        return
    end
    if parentFile == childFile then
        return
    end

    parentFile:addChild(child)
    childFile:addParent(parent)
end

function mt:checkWorkSpaceComplete()
    if self._hasCheckedWorkSpaceComplete then
        return
    end
    self._hasCheckedWorkSpaceComplete = true
    if self.workspace:isComplete() then
        return
    end
    self._needShowComplete = true
    rpc:notify('window/showMessage', {
        type = 3,
        message = lang.script.MWS_NOT_COMPLETE,
    })
end

function mt:_createCompileTask()
    if not self:isWaitingCompile() and not next(self._needDiagnostics) then
        if self._needShowComplete then
            self._needShowComplete = nil
            rpc:notify('window/showMessage', {
                type = 3,
                message = lang.script.MWS_COMPLETE,
            })
        end
    end
    self._compileTask = task(function ()
        self:doDiagnostics(self._lastLoadedVM)
        local uri = self._needCompile[1]
        if uri then
            self._compileTask:set('uri', uri)
            pcall(function () self:compileVM(uri) end)
        else
            uri = next(self._needDiagnostics)
            if uri then
                self:doDiagnostics(uri)
            end
        end
    end)
end

function mt:_doCompileTask()
    if not self._compileTask or self._compileTask:isRemoved() then
        self:_createCompileTask()
    end
    while true do
        local res = self._compileTask:step()
        if res == 'stop' then
            self._compileTask:remove()
            break
        end
        if self._compileTask:isRemoved() then
            break
        end
    end
    self:_loadProto()
end

function mt:_loadProto()
    while true do
        local ok, proto = self._proto:pop()
        if not ok then
            break
        end
        if proto.method then
            self:_doProto(proto)
        else
            rpc:recieve(proto)
        end
    end
end

function mt:restartDueToMemoryLeak()
    rpc:requestWait('window/showMessageRequest', {
        type = 3,
        message = lang.script('DEBUG_MEMORY_LEAK', '[Lua]'),
        actions = {
            {
                title = lang.script.DEBUG_RESTART_NOW,
            }
        }
    }, function ()
        os.exit(true)
    end)
    ac.wait(5, function ()
        os.exit(true)
    end)
end

function mt:reScanFiles()
    if not self.workspace then
        return
    end
    log.debug('reScanFiles')
    self:clearAllFiles()
    self.workspace:scanFiles()
end

function mt:onUpdateConfig(updated, other)
    local oldConfig = table.deepCopy(config.config)
    local oldOther  = table.deepCopy(config.other)
    config:setConfig(updated, other)
    local newConfig = config.config
    local newOther  = config.other
    if not table.equal(oldConfig.runtime, newConfig.runtime) then
        local library = require 'core.library'
        library.reload()
        self:reCompile()
    end
    if not table.equal(oldConfig.diagnostics, newConfig.diagnostics) then
        log.debug('reDiagnostic')
        self:reDiagnostic()
    end
    if newConfig.completion.enable then
        capability.completion.enable()
    else
        capability.completion.disable()
    end
    if not table.equal(oldConfig.plugin, newConfig.plugin) then
        plugin.load(self.workspace)
    end
    if not table.equal(oldConfig.workspace, newConfig.workspace)
    or not table.equal(oldConfig.plugin, newConfig.plugin)
    or not table.equal(oldOther.associations, newOther.associations)
    or not table.equal(oldOther.exclude, newOther.exclude)
    then
        self:reScanFiles()
    end
end

function mt:_testMemory(skipDead)
    local clock = os.clock()
    collectgarbage()
    log.debug('collectgarbage: ', ('%.3f'):format(os.clock() - clock))

    local clock = os.clock()
    local cachedVM = 0
    local cachedSource = 0
    local cachedFunction = 0
    for _, file in self._files:eachFile() do
        local vm = file:getVM()
        if vm and not vm:isRemoved() then
            cachedVM = cachedVM + 1
            cachedSource = cachedSource + #vm.sources
            cachedFunction = cachedFunction + #vm.funcs
        end
    end
    local aliveVM = 0
    local deadVM = 0
    for vm in pairs(CachedVM) do
        if vm:isRemoved() then
            deadVM = deadVM + 1
        else
            aliveVM = aliveVM + 1
        end
    end

    local alivedSource = 0
    local deadSource = 0
    for _, id in pairs(sourceMgr.watch) do
        if listMgr.get(id) then
            alivedSource = alivedSource + 1
        else
            deadSource = deadSource + 1
        end
    end

    local alivedFunction = 0
    local deadFunction = 0
    for _, id in pairs(functionMgr.watch) do
        if listMgr.get(id) then
            alivedFunction = alivedFunction + 1
        else
            deadFunction = deadFunction + 1
        end
    end

    local totalLocal = 0
    for _ in pairs(localMgr.watch) do
        totalLocal = totalLocal + 1
    end

    local totalValue = 0
    local deadValue = 0
    for value in pairs(valueMgr.watch) do
        totalValue = totalValue + 1
        if not value:getSource() then
            deadValue = deadValue + 1
        end
    end

    local totalEmmy = self.emmy:count()

    local mem = collectgarbage 'count'
    local threadInfo = async.info
    local threadBuf = {}
    for i, count in ipairs(threadInfo) do
        if count then
            threadBuf[i] = ('#%03d Mem:  [%.3f]kb'):format(i, count)
        else
            threadBuf[i] = ('#%03d Mem:  <Unknown>'):format(i)
        end
    end

    log.debug(('\n\z
    State\n\z
    Main Mem:  [%.3f]kb\n\z
    %s\n\z
-------------------\n\z
    CachedVM:  [%d]\n\z
    AlivedVM:  [%d]\n\z
    DeadVM:    [%d]\n\z
-------------------\n\z
    CachedSrc: [%d]\n\z
    AlivedSrc: [%d]\n\z
    DeadSrc:   [%d]\n\z
-------------------\n\z
    CachedFunc:[%d]\n\z
    AlivedFunc:[%d]\n\z
    DeadFunc:  [%d]\n\z
-------------------\n\z
    TotalVal:  [%d]\n\z
    DeadVal:   [%d]\n\z
-------------------\n\z
    TotalLoc:  [%d]\n\z
    TotalEmmy: [%d]\n\z'):format(
        mem,
        table.concat(threadBuf, '\n'),

        cachedVM,
        aliveVM,
        deadVM,

        cachedSource,
        alivedSource,
        deadSource,

        cachedFunction,
        alivedFunction,
        deadFunction,

        totalValue,
        deadValue,
        totalLocal,
        totalEmmy
    ))
    log.debug('test memory: ', ('%.3f'):format(os.clock() - clock))

    if deadValue / totalValue >= 0.5 and not skipDead then
        self:_testFindDeadValues()
    end
end

function mt:_testFindDeadValues()
    if self._testHasFoundDeadValues then
        return
    end
    self._testHasFoundDeadValues = true

    log.debug('Start find dead values, may takes few seconds...')

    local mark = {}
    local stack = {}
    local count = 0
    local clock = os.clock()
    local function push(info)
        stack[#stack+1] = info
    end
    local function pop()
        stack[#stack] = nil
    end
    local function showStack(uri)
        count = count + 1
        log.debug(uri, table.concat(stack, '->'))
    end
    local function scan(name, tbl)
        if count > 100 or os.clock() - clock > 5.0 then
            return
        end
        if type(tbl) ~= 'table' then
            return
        end
        if mark[tbl] then
            return
        end
        mark[tbl] = true
        if tbl.type then
            push(('%s<%s>'):format(name, tbl.type))
        else
            push(name)
        end
        if tbl.type == 'value' then
            if not tbl:getSource() then
                showStack(tbl.uri)
            end
        elseif tbl.type == 'files' then
            for k, v in tbl:eachFile() do
                scan(k, v)
            end
        else
            for k, v in pairs(tbl) do
                scan(k, v)
            end
        end
        pop()
    end
    scan('root', self._files)
    log.debug('Finish...')
end

function mt:onTick()
    self:_loadProto()
    self:_doCompileTask()
    if (os.clock() - self._clock >= 60 and not self:isWaitingCompile())
    or (os.clock() - self._clock >= 300)
    then
        self._clock = os.clock()
        self:_testMemory()
    end
end

function mt:listen()
    subprocess.filemode(io.stdin, 'b')
    subprocess.filemode(io.stdout, 'b')
    io.stdin:setvbuf 'no'
    io.stdout:setvbuf 'no'

    local _, out = async.run 'proto'
    self._proto = out

    local timerClock = 0.0
    while true do
        local startClock = os.clock()
        async.onTick()
        self:onTick()

        local delta = os.clock() - timerClock
        local suc, err = xpcall(updateTimer, log.error, delta)
        if not suc then
            io.stderr:write(err)
            io.stderr:flush()
        end
        timerClock = os.clock()

        local passedClock = os.clock() - startClock
        if passedClock > 0.1 then
            thread.sleep(0.0)
        else
            thread.sleep(0.001)
        end
    end
end

return function ()
    local session = setmetatable({
        _needCompile = {},
        _needDiagnostics = {},
        _clock = -100,
        _version = 0,
        _files = files(),
    }, mt)
    session.global = core.global(session)
    session.chain  = chainMgr()
    session.emmy   = emmyMgr()
    return session
end