lunajit/readbc.lua

52 lines
1.1 KiB
Lua

-- module for reading serialized lua bytecode
local bit=require('bit')
local HEADER_MAGIC = {27, string.byte('L'), string.byte('J')}
local HEADER_FLAG_STRIP = 0x02
local function read_uleb128(fp)
local last_byte = false
local res = 0
local shift = 0
-- the bytes are read from the LSB to the MSB
while not last_byte do
local uleb_byte = string.byte(fp:read(1))
res = bit.bor(res, bit.lshift(bit.band(uleb_byte, 0x7f), shift))
if bit.band(uleb_byte, 0x80) == 0 then
last_byte = true
end
end
return res
end
local function read_header(fp)
local magic = { string.byte(fp:read(3), 1, 3) }
local version = string.byte(fp:read(1))
local flags = read_uleb128(fp)
local name
if bit.band(flags, HEADER_FLAG_STRIP) ~= 0 then
name = ''
else
local nameLen = read_uleb128(fp)
name = fp:read(nameLen)
end
return {
magic = magic,
version = version,
flags = flags,
name = name,
}
end
local function read_proto(fp)
end
return {
read_header = read_header,
read_proto = read_proto,
}