-
-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathlua40.hexpat
More file actions
133 lines (113 loc) · 2.62 KB
/
lua40.hexpat
File metadata and controls
133 lines (113 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#pragma description Lua 4.0 bytecode
#pragma magic [ 1B 4C 75 61 40 ] @ 0x00
// based off of https://www.lua.org/source/4.0/dump.c.html
import std.io;
namespace impl {
fn format_LuaString(auto string) {
if (string.size == 0) {
return "None";
}
return std::format("\"{}\"", string.data);
};
fn format_Version(auto ver) {
return std::format("Ver. {}.{}", ver.major, ver.minor);
};
}
using LuaFunction;
bitfield Version {
minor : 4;
major : 4;
} [[format("impl::format_Version")]];
struct LuaBinaryHeader {
u8 id_chunk;
char magic[3];
Version version;
u8 endianness;
u8 size_of_int;
u8 size_of_size_t;
u8 size_of_instruction; // ???
u8 size_INSTRUCTION; // SIZE_INSTRUCTION in Lua 4 source
u8 size_OP; // SIZE_OP
u8 size_B; // SIZE_B
u8 size_number; // sizeof(Number)
if (size_number == 4) {
u32 TEST_NUMBER;
} else {
u64 TEST_NUMBER;
}
};
LuaBinaryHeader header @ 0;
struct LuaString {
if (header.size_of_size_t == 4) {
u32 size;
} else {
u64 size;
}
if (size > 0) {
char data[size];
}
}[[format("impl::format_LuaString")]];
struct Vector<T> {
if (header.size_of_int == 4) {
u32 size;
} else {
u64 size;
}
if (size > 0) {
T values[size];
}
};
struct LocalVar {
LuaString varname;
if (header.size_of_int == 4) {
u32 startpc;
u32 endpc;
} else {
u64 startpc;
u64 endpc;
}
};
struct LuaDebugInfo {
Vector<LocalVar> localVar;
if (header.size_of_int == 4) {
Vector<u32> lineInfo; // i think this is correct
} else {
Vector<u64> lineInfo;
}
};
bitfield LuaNumber {
raw : header.size_number;
};
struct LuaConstants{
Vector<LuaString> stringConstants;
if (header.size_of_int == 4) {
Vector<u32> intConstants;
} else {
Vector<u64> intConstants;
}
Vector<LuaFunction> protos;
};
struct LuaFunction {
LuaString source;
if (header.size_of_int == 4) {
u32 linedefined;
u32 numparams;
} else {
u64 linedefined;
u64 numparams;
}
u8 is_vararg;
if (header.size_of_int == 4) {
u32 maxstacksize;
} else {
u64 maxstacksize;
}
LuaDebugInfo debugInfo;
LuaConstants luaConstants;
if (header.size_of_int == 4) {
Vector<u32> code;
} else {
Vector<u64> code;
}
};
LuaFunction toplevelFunction @ sizeof(header);; // Lua header size is not always the same