diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-11-20 21:57:09 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-11-20 21:57:09 +0800 |
commit | 4ca61ec457822dd14966afa0752340ae8ce180a1 (patch) | |
tree | ae8adb1ad82c717868e551e699fd3cf3bb290089 /script/brave | |
parent | c63b2e404d8d2bb984afe3678a5ba2b2836380cc (diff) | |
download | lua-language-server-4ca61ec457822dd14966afa0752340ae8ce180a1.zip |
no longer beta
Diffstat (limited to 'script/brave')
-rw-r--r-- | script/brave/brave.lua | 70 | ||||
-rw-r--r-- | script/brave/init.lua | 4 | ||||
-rw-r--r-- | script/brave/log.lua | 54 | ||||
-rw-r--r-- | script/brave/work.lua | 60 |
4 files changed, 188 insertions, 0 deletions
diff --git a/script/brave/brave.lua b/script/brave/brave.lua new file mode 100644 index 00000000..08909074 --- /dev/null +++ b/script/brave/brave.lua @@ -0,0 +1,70 @@ +local thread = require 'bee.thread' + +---@class pub_brave +local m = {} +m.type = 'brave' +m.ability = {} +m.queue = {} + +--- 注册成为勇者 +function m.register(id) + m.taskpad = thread.channel('taskpad' .. id) + m.waiter = thread.channel('waiter' .. id) + m.id = id + + if #m.queue > 0 then + for _, info in ipairs(m.queue) do + m.waiter:push(info.name, info.params) + end + end + m.queue = nil + + m.start() +end + +--- 注册能力 +function m.on(name, callback) + m.ability[name] = callback +end + +--- 报告 +function m.push(name, params) + if m.waiter then + m.waiter:push(name, params) + else + m.queue[#m.queue+1] = { + name = name, + params = params, + } + end +end + +--- 开始找工作 +function m.start() + m.push('mem', collectgarbage 'count') + while true do + local suc, name, id, params = m.taskpad:pop() + if not suc then + -- 找不到工作的勇者,只好睡觉 + thread.sleep(0.001) + goto CONTINUE + end + local ability = m.ability[name] + -- TODO + if not ability then + m.waiter:push(id) + log.error('Brave can not handle this work: ' .. name) + goto CONTINUE + end + local ok, res = xpcall(ability, log.error, params) + if ok then + m.waiter:push(id, res) + else + m.waiter:push(id) + end + m.push('mem', collectgarbage 'count') + ::CONTINUE:: + end +end + +return m diff --git a/script/brave/init.lua b/script/brave/init.lua new file mode 100644 index 00000000..24c2e412 --- /dev/null +++ b/script/brave/init.lua @@ -0,0 +1,4 @@ +local brave = require 'brave.brave' +require 'brave.work' + +return brave diff --git a/script/brave/log.lua b/script/brave/log.lua new file mode 100644 index 00000000..18ea7853 --- /dev/null +++ b/script/brave/log.lua @@ -0,0 +1,54 @@ +local brave = require 'brave' + +local tablePack = table.pack +local tostring = tostring +local tableConcat = table.concat +local debugTraceBack = debug.traceback +local debugGetInfo = debug.getinfo +local osClock = os.clock + +_ENV = nil + +local function pushLog(level, ...) + local t = tablePack(...) + for i = 1, t.n do + t[i] = tostring(t[i]) + end + local str = tableConcat(t, '\t', 1, t.n) + if level == 'error' then + str = str .. '\n' .. debugTraceBack(nil, 3) + end + local info = debugGetInfo(3, 'Sl') + brave.push('log', { + level = level, + msg = str, + src = info.source, + line = info.currentline, + clock = osClock(), + }) + return str +end + +local m = {} + +function m.info(...) + pushLog('info', ...) +end + +function m.debug(...) + pushLog('debug', ...) +end + +function m.trace(...) + pushLog('trace', ...) +end + +function m.warn(...) + pushLog('warn', ...) +end + +function m.error(...) + pushLog('error', ...) +end + +return m diff --git a/script/brave/work.lua b/script/brave/work.lua new file mode 100644 index 00000000..5ec8178f --- /dev/null +++ b/script/brave/work.lua @@ -0,0 +1,60 @@ +local brave = require 'brave.brave' +local parser = require 'parser' +local fs = require 'bee.filesystem' +local furi = require 'file-uri' +local util = require 'utility' +local thread = require 'bee.thread' + +brave.on('loadProto', function () + local jsonrpc = require 'jsonrpc' + while true do + local proto, err = jsonrpc.decode(io.read, log.error) + --log.debug('loaded proto', proto.method) + if not proto then + brave.push('protoerror', err) + return + end + brave.push('proto', proto) + thread.sleep(0.001) + end +end) + +brave.on('compile', function (text) + local state, err = parser:compile(text, 'lua', 'Lua 5.4') + if not state then + log.error(err) + return + end + local lines = parser:lines(text) + return { + root = state.root, + value = state.value, + errs = state.errs, + lines = lines, + } +end) + +brave.on('listDirectory', function (uri) + local path = fs.path(furi.decode(uri)) + local uris = {} + for child in path:list_directory() do + local childUri = furi.encode(child:string()) + uris[#uris+1] = childUri + end + return uris +end) + +brave.on('isDirectory', function (uri) + local path = fs.path(furi.decode(uri)) + return fs.is_directory(path) +end) + +brave.on('loadFile', function (uri) + local filename = furi.decode(uri) + return util.loadFile(filename) +end) + +brave.on('saveFile', function (params) + local filename = furi.decode(params.uri) + return util.saveFile(filename, params.text) +end) |