lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi, Mike!

I have a tool, written in Lua, which I really want to move to LuaJIT 2
(because performance starts to matter there).

Unfortunately, this tool uses Lua bytecode inspector library
(http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lbci).

I use it to get a list of GET/SETGLOBAL instructions from a given
function and to get number of upvalues. (Note that in future I'll
probably want to traverse the list of upvalues and check if they're
allowed...)

Is there a way to implement my function (see below) in current LJ2?
I'll settle for non-stable API.

If not, is it a lot of work to add such API to the LJ2?

Thanks,
Alexander.

The code as it is (data_id is string):

local list_globals_in_handler = function(data_id, handler_fn)
 local globals = { }

 local info = bci.getheader(handler_fn)

 if info.upvalues > 0 then
   error(
       "bad " .. data_id .. ": handler has " .. info.upvalues
    .. " upvalue(s), must have none"
     )
 end

 for i = 1, info.instructions do
   local line, opcode, a, b, c = bci.getinstruction(handler_fn, i)
   if opcode == "SETGLOBAL" then
     error(
         "bad " .. data_id .. ": handler changes global"
      .. " `" .. bci.getconstant(handler_fn, -b) .. "'"
      .. (
         line and (" at " .. (info.source or "") .. ":" .. line) or "")
       )
   elseif opcode == "GETGLOBAL" then
     local name = bci.getconstant(handler_fn, -b)
     globals[name] = globals[name] or { }
     globals[name][#globals[name] + 1] =
     {
       line = line;
       source = info.source;
     }
   end
 end

 return globals
end