Refactoring, remove compiler support

- LuaCompiler has been renamed to LuaUndump
- LuaUndump has been striped of the connection to luac, focusing on strictly parsing Lua 5.1 bytecode, not compiling.
- Updated README.md to reflect changes
This commit is contained in:
CPunch 2021-04-09 09:21:47 -05:00
parent 005567fee1
commit 88d669f3c8
2 changed files with 11 additions and 16 deletions

View File

@ -2,25 +2,26 @@
Parses Lua 5.1 bytecode Parses Lua 5.1 bytecode
# Example # Example
loads a raw lua bytecode dump
```python ```python
import luac import luac
lc = luac.LuaCompiler() lc = luac.LuaUndump()
chunk = lc.compileC("test.lua") chunk = lc.loadFile("test.luac")
print("\n===== [[Disassembly]] =====\n") print("\n===== [[Disassembly]] =====\n")
lc.print_dissassembly() lc.print_dissassembly()
``` ```
or just parse lua bytecode or just parse lua bytecode from an array
```python ```python
import luac import luac
bytecode = "27\\76\\117\\97\\81\\0\\1\\4\\8\\4\\8\\0\\21\\0\\0\\0\\0\\0\\0\\0\\112\\114\\105\\110\\116\\40\\39\\104\\101\\108\\108\\111\\32\\119\\111\\114\\108\\100\\39\\41\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\2\\2\\4\\0\\0\\0\\5\\0\\0\\0\\65\\64\\0\\0\\28\\64\\0\\1\\30\\0\\128\\0\\2\\0\\0\\0\\4\\6\\0\\0\\0\\0\\0\\0\\0\\112\\114\\105\\110\\116\\0\\4\\12\\0\\0\\0\\0\\0\\0\\0\\104\\101\\108\\108\\111\\32\\119\\111\\114\\108\\100\\0\\0\\0\\0\\0\\4\\0\\0\\0\\1\\0\\0\\0\\1\\0\\0\\0\\1\\0\\0\\0\\1\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0".split('\\') bytecode = "27\\76\\117\\97\\81\\0\\1\\4\\8\\4\\8\\0\\21\\0\\0\\0\\0\\0\\0\\0\\112\\114\\105\\110\\116\\40\\39\\104\\101\\108\\108\\111\\32\\119\\111\\114\\108\\100\\39\\41\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\2\\2\\4\\0\\0\\0\\5\\0\\0\\0\\65\\64\\0\\0\\28\\64\\0\\1\\30\\0\\128\\0\\2\\0\\0\\0\\4\\6\\0\\0\\0\\0\\0\\0\\0\\112\\114\\105\\110\\116\\0\\4\\12\\0\\0\\0\\0\\0\\0\\0\\104\\101\\108\\108\\111\\32\\119\\111\\114\\108\\100\\0\\0\\0\\0\\0\\4\\0\\0\\0\\1\\0\\0\\0\\1\\0\\0\\0\\1\\0\\0\\0\\1\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0".split('\\')
bytecode = list(map(int, bytecode)) bytecode = list(map(int, bytecode))
lc = luac.LuaCompiler() lc = luac.LuaUndump()
chunk = lc.decode_bytecode(bytecode) chunk = lc.decode_bytecode(bytecode)
lc.print_dissassembly() lc.print_dissassembly()

16
luac.py
View File

@ -1,4 +1,3 @@
import os
import struct import struct
import array import array
@ -49,11 +48,8 @@ def get_bits(num, p, k):
# convert extracted sub-string into decimal again # convert extracted sub-string into decimal again
return (int(kBitSubStr,2)) return (int(kBitSubStr,2))
class LuaCompiler: class LuaUndump:
def __init__(self): def __init__(self):
self.luac = "luac5.1"
self.o_flag = "-o"
self.temp_out = "out.luac"
self.chunks = [] self.chunks = []
self.chunk = {} self.chunk = {}
self.index = 0 self.index = 0
@ -63,7 +59,7 @@ class LuaCompiler:
print("==== [[" + str(chunk['NAME']) + "]] ====\n") print("==== [[" + str(chunk['NAME']) + "]] ====\n")
for z in chunk['PROTOTYPES']: for z in chunk['PROTOTYPES']:
print("** decoding proto\n") print("** decoding proto\n")
LuaCompiler.dis_chunk(chunk['PROTOTYPES'][z]) LuaUndump.dis_chunk(chunk['PROTOTYPES'][z])
print("\n==== [[" + str(chunk['NAME']) + "'s constants]] ====\n") print("\n==== [[" + str(chunk['NAME']) + "'s constants]] ====\n")
for z in chunk['CONSTANTS']: for z in chunk['CONSTANTS']:
@ -261,13 +257,11 @@ class LuaCompiler:
self.chunk = self.decode_chunk() self.chunk = self.decode_chunk()
return self.chunk return self.chunk
def compileC(self, luafile): def loadFile(self, luaCFile):
os.system(self.luac + " " + self.o_flag + " " + self.temp_out + " " + luafile) with open(luaCFile, 'rb') as luac_file:
with open(self.temp_out, 'rb') as luac_file:
bytecode = luac_file.read() bytecode = luac_file.read()
return self.decode_rawbytecode(bytecode) return self.decode_rawbytecode(bytecode)
def print_dissassembly(self): def print_dissassembly(self):
LuaCompiler.dis_chunk(self.chunk) LuaUndump.dis_chunk(self.chunk)