lua-users home
lua-l archive

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


Strip in Lua:

-- Strips debug information from a dumped chunk
function strip(dump)
  local version, format, endian, int, size, ins, num = dump:byte(5, 11)
  local subint
  if endian == 1 then
    subint = function(dump, i, l)
      local val = 0
      for n = l, 1, -1 do
          val = val * 256 + dump:byte(i + n - 1)
      end
      return val, i + l
    end
  else
    subint = function(dump, i, l)
            local val = 0
            for n = 1, l, 1 do
                val = val * 256 + dump:byte(i + n - 1)
            end
            return val, i + l
        end
    end
	local strip_function
  strip_function = function(dump)
    local count, offset = subint(dump, 1, size)
    local stripped, dirty = string.rep("\0", size), offset + count
    offset = offset + count + int * 2 + 4
    offset = offset + int + subint(dump, offset, int) * ins
    count, offset = subint(dump, offset, int)
    for n = 1, count do
      local t
      t, offset = subint(dump, offset, 1)
      if t == 1 then
        offset = offset + 1
      elseif t == 4 then
        offset = offset + size + subint(dump, offset, size)
      elseif t == 3 then
        offset = offset + num
      end
    end
    count, offset = subint(dump, offset, int)
    stripped = stripped .. dump:sub(dirty, offset - 1)
    for n = 1, count do
      local proto, off = strip_function(dump:sub(offset, -1))
      stripped, offset = stripped .. proto, offset + off - 1
    end
    offset = offset + subint(dump, offset, int) * int + int
    count, offset = subint(dump, offset, int)
    for n = 1, count do
      offset = offset + subint(dump, offset, size) + size + int * 2
    end
    count, offset = subint(dump, offset, int)
    for n = 1, count do
      offset = offset + subint(dump, offset, size) + size
    end
    stripped = stripped .. string.rep("\0", int * 3)
    return stripped, offset
  end
	return dump:sub(1,12) .. strip_function(dump:sub(13,-1))
end

-- Test function
local function test()
  local alpha, beta = "world", "hello"
  alpha, beta = beta, alpha
  local table, mod = {1,2,3,4,5,6,7}, 3
  _G.table.sort(table, function(a,b) return (a % mod) > (b % mod) end)
  return alpha .. " " .. beta .. " " .. _G.table.concat(table,",")
end

print("Normal: ", test())

local dumped = string.dump(test)
test = loadstring(dumped)

print("Dumped #: ", #dumped)
print("After dump: ", test())

dumped = strip(dumped)
test = loadstring(dumped)

print("Stripped #: ", #dumped)
print("After strip: ", test())

-- Strip the strip function
dumped = string.dump(strip)
print("Dumped strip #: ", #dumped)
dumped = strip(dumped)
print("Stripped strip #: ", #dumped)
strip = loadstring(dumped)

dumped = strip(string.dump(function() print"Strip still works!" end))
print(assert(loadstring(dumped))())

On 26/02/2008, David Burgess <dburgess@gmail.com> wrote:
> Can we levaluate that  again? I think it is a valuable feature.
>
>  Davud B
>
>
>  On Wed, Feb 27, 2008 at 4:40 AM, Luiz Henrique de Figueiredo
>  <lhf@tecgraf.puc-rio.br> wrote:
>  > > This was requested on 2006. Don't know if the discussion arrived to any
>  >  > conclusion.
>  >  >
>  >  > http://lua-users.org/lists/lua-l/2006-10/msg00013.html
>  >
>  >  I think the conclusion was that it would complicate the API.
>  >
>