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
# Example
loads a raw lua bytecode dump
```python
import luac
lc = luac.LuaCompiler()
chunk = lc.compileC("test.lua")
lc = luac.LuaUndump()
chunk = lc.loadFile("test.luac")
print("\n===== [[Disassembly]] =====\n")
lc.print_dissassembly()
```
or just parse lua bytecode
or just parse lua bytecode from an array
```python
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 = list(map(int, bytecode))
lc = luac.LuaCompiler()
lc = luac.LuaUndump()
chunk = lc.decode_bytecode(bytecode)
lc.print_dissassembly()

16
luac.py
View File

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