-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathLFunctionType.cs
176 lines (136 loc) · 5.9 KB
/
LFunctionType.cs
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnluacNET.IO;
namespace UnluacNET
{
public class LFunctionType : BObjectType<LFunction>
{
public static readonly LFunctionType TYPE51 = new LFunctionType();
public static readonly LFunctionType TYPE52 = new LFunctionType52();
protected sealed class LFunctionParseState
{
public LString Name { get; set; }
public int LineBegin { get; set; }
public int LineEnd { get; set; }
public int LenUpvalues { get; set; }
public int LenParameter { get; set; }
public int VarArg { get; set; }
public int MaximumStackSize { get; set; }
public int Length { get; set; }
public int[] Code { get; set; }
public BList<LObject> Constants { get; set; }
public BList<LFunction> Functions { get; set; }
public BList<BInteger> Lines { get; set; }
public BList<LLocal> Locals { get; set; }
public LUpvalue[] Upvalues { get; set; }
}
public override LFunction Parse(Stream stream, BHeader header)
{
if (header.Debug)
{
Console.WriteLine("-- beginning to parse function");
Console.WriteLine("-- parsing name...start...end...upvalues...params...varargs...stack");
}
var s = new LFunctionParseState();
ParseMain(stream, header, s);
return new LFunction(header,
s.Code,
s.Locals.AsArray(),
s.Constants.AsArray(),
s.Upvalues,
s.Functions.AsArray(),
s.MaximumStackSize,
s.LenUpvalues,
s.LenParameter,
s.VarArg);
}
protected virtual void ParseCode(Stream stream, BHeader header, LFunctionParseState s)
{
if (header.Debug)
Console.WriteLine("-- beginning to parse bytecode list");
// HACK HACK HACK
var bigEndian = header.BigEndian;
s.Length = header.Integer.Parse(stream, header).AsInteger();
s.Code = new int[s.Length];
for (int i = 0; i < s.Length; i++)
{
s.Code[i] = stream.ReadInt32(bigEndian);
if (header.Debug)
Console.WriteLine("-- parsed codepoint 0x{0:X}" + s.Code[i]);
}
}
protected virtual void ParseConstants(Stream stream, BHeader header, LFunctionParseState s)
{
if (header.Debug)
Console.WriteLine("-- beginning to parse constants list");
s.Constants = header.Constant.ParseList(stream, header);
if (header.Debug)
Console.WriteLine("-- beginning to parse functions list");
s.Functions = header.Function.ParseList(stream, header);
}
protected virtual void ParseDebug(Stream stream, BHeader header, LFunctionParseState s)
{
if (header.Debug)
Console.WriteLine("-- beginning to parse source lines list");
s.Lines = header.Integer.ParseList(stream, header);
if (header.Debug)
Console.WriteLine("-- beginning to parse locals list");
s.Locals = header.Local.ParseList(stream, header);
if (header.Debug)
Console.WriteLine("-- beginning to parse upvalues list");
var upvalNames = header.String.ParseList(stream, header);
var count = upvalNames.Length.AsInteger();
for (int i = 0; i < count; i++)
s.Upvalues[i].Name = upvalNames[i].DeRef();
}
protected virtual void ParseMain(Stream stream, BHeader header, LFunctionParseState s)
{
s.Name = header.String.Parse(stream, header);
s.LineBegin = header.Integer.Parse(stream, header).AsInteger();
s.LineEnd = header.Integer.Parse(stream, header).AsInteger();
s.LenUpvalues = stream.ReadByte();
s.LenParameter = stream.ReadByte();
s.VarArg = stream.ReadByte();
s.MaximumStackSize = stream.ReadByte();
ParseCode(stream, header, s);
ParseConstants(stream, header, s);
ParseUpvalues(stream, header, s);
ParseDebug(stream, header, s);
}
protected virtual void ParseUpvalues(Stream stream, BHeader header, LFunctionParseState s)
{
s.Upvalues = new LUpvalue[s.LenUpvalues];
for (int i = 0; i < s.LenUpvalues; i++)
s.Upvalues[i] = new LUpvalue();
}
}
public class LFunctionType52 : LFunctionType
{
protected override void ParseDebug(Stream stream, BHeader header, LFunctionParseState s)
{
s.Name = header.String.Parse(stream, header);
base.ParseDebug(stream, header, s);
}
protected override void ParseMain(Stream stream, BHeader header, LFunctionParseState s)
{
s.LineBegin = header.Integer.Parse(stream, header).AsInteger();
s.LineEnd = header.Integer.Parse(stream, header).AsInteger();
s.LenParameter = stream.ReadByte();
s.VarArg = stream.ReadByte();
s.MaximumStackSize = stream.ReadByte();
ParseCode(stream, header, s);
ParseConstants(stream, header, s);
ParseUpvalues(stream, header, s);
ParseDebug(stream, header, s);
}
protected override void ParseUpvalues(Stream stream, BHeader header, LFunctionParseState s)
{
var upvalues = header.UpValue.ParseList(stream, header);
s.LenUpvalues = upvalues.Length.AsInteger();
s.Upvalues = upvalues.AsArray();
}
}
}