diff --git a/extension/script/backend/worker/eval.lua b/extension/script/backend/worker/eval.lua index 043ddd34..b507da99 100644 --- a/extension/script/backend/worker/eval.lua +++ b/extension/script/backend/worker/eval.lua @@ -49,6 +49,13 @@ generate("dump", function() end end) +generate("lineinfo", function() + local eval_lineinfo = assert(rdebug.load(readfile 'backend.worker.eval.lineinfo')) + return function(content) + return rdebug.eval(eval_lineinfo, content) + end +end) + generate("ffi_reflect", function () if not luaver.isjit then return diff --git a/extension/script/backend/worker/eval/lineinfo.lua b/extension/script/backend/worker/eval/lineinfo.lua new file mode 100644 index 00000000..8467c1ed --- /dev/null +++ b/extension/script/backend/worker/eval/lineinfo.lua @@ -0,0 +1,28 @@ +local jutil = require 'jit.util' + +-- Describes a prototype as JSON, using the same field names as undump. +local function dump(fn) + local info = jutil.funcinfo(fn) + local lines = {} + -- pc 0 is the function header, which is not an active line. + for pc = 1, info.bytecodes - 1 do + lines[pc] = jutil.funcinfo(fn, pc).currentline + end + local protos = {} + for i = -1, -info.gcconsts, -1 do + local k = jutil.funck(fn, i) + if type(k) == "proto" then + protos[#protos + 1] = dump(k) + end + end + return string.format('{"linedefined":%d,"lastlinedefined":%d,"lineinfo":[%s],"sizep":%d,"p":[%s]}', + info.linedefined, + -- rdebug.getinfo reports 0 for the main chunk, see rdebug_visitor.cpp. + info.linedefined == 0 and 0 or info.lastlinedefined, + table.concat(lines, ","), + #protos, + table.concat(protos, ",") + ) +end + +return dump(assert(loadstring(...))) diff --git a/extension/script/backend/worker/parser.lua b/extension/script/backend/worker/parser.lua index cd874dad..894e86ae 100644 --- a/extension/script/backend/worker/parser.lua +++ b/extension/script/backend/worker/parser.lua @@ -93,11 +93,28 @@ local function dumpTarget(content) return nil, type(bin) == "string" and bin or "can not dump function." end +-- undump can not read LuaJIT bytecode, so LuaJIT reports the line info itself. +local function dumpJit(content) + local eval = require 'backend.worker.eval' + local ok, res = eval.lineinfo(content) + if not ok then + local log = require 'common.log' + log.error("ERROR:"..tostring(res)) + return + end + local json = require 'common.json' + return json.decode(res) +end + return function (content, syntaxCompatibility) + local luaver = require 'backend.worker.luaver' local err local bin local cl, v - if syntaxCompatibility then + local proto = luaver.isjit and dumpJit(content) + if proto then + cl, v = { f = proto }, 0x51 + elseif syntaxCompatibility then bin, err = dumpTarget(content) if not bin then local log = require 'common.log'