summaryrefslogtreecommitdiff
path: root/script/brave/work.lua
blob: a6a7a41e0ec2cb49b7455afcd96dad79b529495f (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
local brave   = require 'brave.brave'

brave.on('loadProtoByStdio', function ()
    local jsonrpc = require 'jsonrpc'
    while true do
        local proto, err = jsonrpc.decode(io.read)
        --log.debug('loaded proto', proto.method)
        if not proto then
            brave.push('protoerror', err)
            return
        end
        brave.push('proto', proto)
    end
end)

brave.on('loadProtoBySocket', function (param)
    local jsonrpc = require 'jsonrpc'
    local net     = require 'service.net'
    local buf = ''

    ---@async
    local parser = coroutine.create(function ()
        while true do
            ---@async
            local proto, err = jsonrpc.decode(function (len)
                while true do
                    if #buf >= len then
                        local res = buf:sub(1, len)
                        buf = buf:sub(len + 1)
                        return res
                    end
                    coroutine.yield()
                end
            end)
            --log.debug('loaded proto', proto.method)
            if not proto then
                brave.push('protoerror', err)
                return
            end
            brave.push('proto', proto)
        end
    end)

    local lsclient = net.connect('tcp', '127.0.0.1', param.port)
    local lsmaster = net.connect('unix', param.unixPath)

    assert(lsclient)
    assert(lsmaster)

    function lsclient:on_data(data)
        buf = buf .. data
        coroutine.resume(parser)
    end

    function lsmaster:on_data(data)
        lsclient:write(data)
        net.update()
    end

    while true do
        net.update(10)
    end
end)

brave.on('timer', function (time)
    local thread = require 'bee.thread'
    while true do
        thread.sleep(time)
        brave.push('wakeup')
    end
end)

brave.on('loadFile', function (path)
    local util    = require 'utility'
    return util.loadFile(path)
end)

brave.on('removeCaches', function (path)
    local fs  = require 'bee.filesystem'
    local fsu = require 'fs-utility'
    for dir in fs.pairs(fs.path(path)) do
        local lockFile = dir / '.lock'
        local f = io.open(lockFile:string(), 'wb')
        if f then
            f:close()
            fsu.fileRemove(dir)
        end
    end
end)

---@class brave.param.compile
---@field uri uri
---@field text string
---@field mode string
---@field version string
---@field options brave.param.compile.options

---@class brave.param.compile.options
---@field special table<string, string>
---@field unicodeName boolean
---@field nonstandardSymbol table<string, true>

---@param param brave.param.compile
brave.on('compile', function (param)
    local parser = require 'parser'
    local clock = os.clock()
    local state, err = parser.compile(param.text
        , param.mode
        , param.version
        , param.options
    )
    log.debug('Async compile', param.uri, 'takes:', os.clock() - clock)
    return {
        state = state,
        err   = err,
    }
end)