diff options
-rw-r--r-- | script/brave/brave.lua | 9 | ||||
-rw-r--r-- | script/files.lua | 88 | ||||
-rw-r--r-- | script/pub/pub.lua | 19 | ||||
-rw-r--r-- | script/service/service.lua | 5 | ||||
-rw-r--r-- | script/workspace/loading.lua | 9 |
5 files changed, 87 insertions, 43 deletions
diff --git a/script/brave/brave.lua b/script/brave/brave.lua index 36b56b54..f07fb00f 100644 --- a/script/brave/brave.lua +++ b/script/brave/brave.lua @@ -10,7 +10,7 @@ m.ability = {} m.queue = {} --- 注册成为勇者 -function m.register(id) +function m.register(id, privatePad) m.id = id if #m.queue > 0 then @@ -20,7 +20,7 @@ function m.register(id) end m.queue = nil - m.start() + m.start(privatePad) end --- 注册能力 @@ -41,10 +41,11 @@ function m.push(name, params) end --- 开始找工作 -function m.start() +function m.start(privatePad) + local myPad = privatePad and thread.channel('private:' .. privatePad) or taskPad m.push('mem', collectgarbage 'count') while true do - local name, id, params = taskPad:bpop() + local name, id, params = myPad:bpop() local ability = m.ability[name] -- TODO if not ability then diff --git a/script/files.lua b/script/files.lua index b01f02e6..353030a4 100644 --- a/script/files.lua +++ b/script/files.lua @@ -555,15 +555,11 @@ function m.compileStateThen(state, file) end ---@param uri uri ----@param async boolean? ----@return parser.state? -function m.compileState(uri, async) +---@return boolean +function m.checkPreload(uri) local file = m.fileMap[uri] if not file then - return - end - if file.state then - return file.state + return false end local ws = require 'workspace' local client = require 'client' @@ -588,7 +584,22 @@ function m.compileState(uri, async) client.logMessage('Info', message) end end - return nil + return false + end + return true +end + +---@param uri uri +---@param callback fun(state: parser.state?) +function m.compileStateAsync(uri, callback) + local file = m.fileMap[uri] + if not file then + callback(nil) + return + end + if file.state then + callback(file.state) + return end ---@type brave.param.compile.options @@ -598,27 +609,50 @@ function m.compileState(uri, async) nonstandardSymbol = util.arrayToHash(config.get(uri, 'Lua.runtime.nonstandardSymbol')), } - if async then - ---@type brave.param.compile - local params = { - uri = uri, - text = file.text, - mode = 'Lua', - version = config.get(uri, 'Lua.runtime.version'), - options = options - } - pub.task('compile', params, function (result) - if file.text ~= params.text then - return - end - if not result.state then - log.error('Compile failed:', uri, result.err) - return - end - m.compileStateThen(result.state, file) - end) + ---@type brave.param.compile + local params = { + uri = uri, + text = file.text, + mode = 'Lua', + version = config.get(uri, 'Lua.runtime.version'), + options = options + } + pub.task('compile', params, function (result) + if file.text ~= params.text then + return + end + if not result.state then + log.error('Compile failed:', uri, result.err) + callback(nil) + return + end + m.compileStateThen(result.state, file) + callback(result.state) + end) +end + +---@param uri uri +---@return parser.state? +function m.compileState(uri) + local file = m.fileMap[uri] + if not file then + return + end + if file.state then + return file.state + end + if not m.checkPreload(uri) then + return end + ---@type brave.param.compile.options + local options = { + special = config.get(uri, 'Lua.runtime.special'), + unicodeName = config.get(uri, 'Lua.runtime.unicodeName'), + nonstandardSymbol = util.arrayToHash(config.get(uri, 'Lua.runtime.nonstandardSymbol')), + } + + local ws = require 'workspace' local prog <close> = progress.create(uri, lang.script.WINDOW_COMPILING, 0.5) prog:setMessage(ws.getRelativePath(uri)) local clock = os.clock() diff --git a/script/pub/pub.lua b/script/pub/pub.lua index 1e9b6c8f..9733846a 100644 --- a/script/pub/pub.lua +++ b/script/pub/pub.lua @@ -24,7 +24,7 @@ log = require 'brave.log' xpcall(dofile, log.error, %q) local brave = require 'brave' -brave.register(%d) +brave.register(%d, %q) ]] ---@class pub @@ -34,6 +34,7 @@ m.braves = {} m.ability = {} m.taskQueue = {} m.taskMap = {} +m.prvtPad = {} --- 注册酒馆的功能 function m.on(name, callback) @@ -42,7 +43,8 @@ end --- 招募勇者,勇者会从公告板上领取任务,完成任务后到看板娘处交付任务 ---@param num integer -function m.recruitBraves(num) +---@param privatePad string? +function m.recruitBraves(num, privatePad) for _ = 1, num do local id = #m.braves + 1 log.debug('Create brave:', id) @@ -55,13 +57,18 @@ function m.recruitBraves(num) DBGPORT or 11412, DBGWAIT or 'nil', (ROOT / 'debugger.lua'):string(), - id + id, + privatePad )), taskMap = {}, currentTask = nil, memory = 0, } end + if privatePad and not m.prvtPad[privatePad] then + thread.newchannel('private:' .. privatePad) + m.prvtPad[privatePad] = thread.channel('private:' .. privatePad) + end end --- 给勇者推送任务 @@ -69,7 +76,11 @@ function m.pushTask(info) if info.removed then return false end - taskPad:push(info.name, info.id, info.params) + if m.prvtPad[info.name] then + m.prvtPad[info.name]:push(info.name, info.id, info.params) + else + taskPad:push(info.name, info.id, info.params) + end m.taskMap[info.id] = info return true end diff --git a/script/service/service.lua b/script/service/service.lua index 7e0c48c3..e15dfbd1 100644 --- a/script/service/service.lua +++ b/script/service/service.lua @@ -260,7 +260,10 @@ end function m.start() util.enableCloseFunction() await.setErrorHandle(log.error) - pub.recruitBraves(4 + (COMPILECORES or 0)) + pub.recruitBraves(4) + if COMPILECORES and COMPILECORES > 0 then + pub.recruitBraves(COMPILECORES, 'compile') + end proto.listen() m.report() m.testVersion() diff --git a/script/workspace/loading.lua b/script/workspace/loading.lua index 43909357..f28665cd 100644 --- a/script/workspace/loading.lua +++ b/script/workspace/loading.lua @@ -88,12 +88,7 @@ function mt:loadFile(uri, libraryUri) self._cache[uri] = true log.debug(('Skip loaded file: %s'):format(uri)) else - local content - if COMPILECORES then - content = util.loadFile(furi.decode(uri)) - else - content = pub.awaitTask('loadFile', furi.decode(uri)) - end + local content = pub.awaitTask('loadFile', furi.decode(uri)) self.read = self.read + 1 self:update() if not content then @@ -105,7 +100,7 @@ function mt:loadFile(uri, libraryUri) --end) files.setText(uri, content, false) if COMPILECORES then - files.compileState(uri, true) + files.compileStateAsync(uri, function (state) end) else files.compileState(uri) end |