-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.lua
391 lines (318 loc) · 14.4 KB
/
api.lua
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
--[[
Name: api.lua
Author: ByteXenon [Luna Gilbert]
Date: 2024-05-21
Description:
A simple API for the entire project.
This is the only file that should be exposed to the user.
Read the license file in the root of the project directory.
--]]
--* Dependencies *--
local AnsiFormatter = require("AnsiFormatter/AnsiFormatter")
local Helpers = require("Helpers/Helpers")
local Assembler = require("Assembler/Assembler")
local Lexer = require("Interpreter/LuaInterpreter/Lexer/Lexer")
local Parser = require("Interpreter/LuaInterpreter/Parser/Parser")
local InstructionGenerator = require("Interpreter/LuaInterpreter/InstructionGenerator/InstructionGenerator")
local ASTToTokensConverter = require("Interpreter/LuaInterpreter/ASTToTokensConverter/ASTToTokensConverter")
local VirtualMachine = require("VirtualMachine/VirtualMachine")
local Minifier = require("Minifier/Minifier")
local Packer = require("Packer/Packer")
local LuaState = require("Structures/LuaState")
local ASTExecutor = require("ASTExecutor/ASTExecutor")
local Printer = require("Printer/Printer")
local ASTPrinter = require("Printer/ASTPrinter/ASTPrinter")
local SyntaxHighlighter = require("Interpreter/LuaInterpreter/SyntaxHighlighter/SyntaxHighlighter")
local ASTObfuscator = require("Obfuscator/AST/ASTObfuscator")
local IronBrikked = require("Obfuscator/IronBrikked/IronBrikked")
--* Imports *--
local unpack = (unpack or table.unpack)
--* API *--
local API = {
VirtualMachine = {},
Interpreter = {},
InstructionGenerator = {},
Assembler = {},
ASTExecutor = {},
Minifier = {},
ASTToTokensConverter = {},
Printer = {
TokenPrinter = {},
ASTPrinter = {}
},
Packer = {},
LuaState = {},
Obfuscator = {
ASTObfuscator = {},
IronBrikked = {}
},
-- Expose modules for easier access in the future
Modules = {
AnsiFormatter = AnsiFormatter,
Helpers = Helpers,
Assembler = Assembler,
Lexer = Lexer,
Parser = Parser,
InstructionGenerator = InstructionGenerator,
ASTToTokensConverter = ASTToTokensConverter,
VirtualMachine = VirtualMachine,
Minifier = Minifier,
Packer = Packer,
ASTExecutor = ASTExecutor,
Printer = Printer,
ASTPrinter = ASTPrinter,
SyntaxHighlighter = SyntaxHighlighter,
ASTObfuscator = ASTObfuscator,
IronBrikked = IronBrikked
}
}
--* API.VirtualMachine *--
--- Executes a provided proto in a virtual machine.
-- @param <LuaProto> proto The proto of a Lua script.
-- @param <boolean> debug Whether to run in debug mode or not.
function API.VirtualMachine.ExecuteProto(proto, debug)
assert(type(proto) == "table", "Expected table for argument 'proto', but got " .. type(proto))
local newVirtualMachine = VirtualMachine:new(proto, debug)
newVirtualMachine:run()
end
--- Executes a provided script in a virtual machine.
-- @param <string> script A Lua script.
-- @param <boolean> debug Whether to run in debug mode or not.
function API.VirtualMachine.ExecuteScript(script, debug)
assert(type(script) == "string", "Expected string for argument 'script', but got " .. type(script))
local luaProto = API.Interpreter.ConvertToInstructions(script)
API.VirtualMachine.ExecuteProto(luaProto, debug)
end
--* API.Interpreter *--
--- Tokenizes a Lua script and returns its tokens.
-- @param <string> script A Lua script.
-- @param <boolean> includeHighlightTokens Whether to include additional highlight tokens or not.
-- @return <table> tokens The tokens of the Lua script.
function API.Interpreter.ConvertToTokens(script, includeHighlightTokens)
assert(type(script) == "string", "Expected string for argument 'script', but got " .. type(script))
local tokens = Lexer:new(script, includeHighlightTokens):tokenize()
return tokens
end
--- Tokenizes and parses Lua script and returns its Abstract Syntax Tree.
-- @param <string> script A Lua script.
-- @return <table> AST The Abstract Syntax Tree of the Lua script.
function API.Interpreter.ConvertToAST(script)
assert(type(script) == "string", "Expected string for argument 'script', but got " .. type(script))
local tokens = API.Interpreter.ConvertToTokens(script)
local AST = Parser:new(tokens):parse()
return AST
end
--- Tokenizes, parses, and converts Lua script to instructions and returns its proto.
-- @param <string> script A Lua script.
-- @return <table> proto The proto of a Lua script.
function API.Interpreter.ConvertToInstructions(script)
assert(type(script) == "string", "Expected string for argument 'script', but got " .. type(script))
local AST = API.Interpreter.ConvertToAST(script)
local proto = InstructionGenerator:new(AST):run()
return proto
end
--* API.InstructionGenerator *--
--- Converts AST to instructions and returns its proto.
-- @param <table> AST The Abstract Syntax Tree of a Lua script.
-- @return <table> proto The proto of a Lua script.
function API.InstructionGenerator.ConvertASTToInstructions(AST)
assert(type(AST) == "table", "Expected table for argument 'AST', but got " .. type(AST))
local proto = InstructionGenerator:new(AST):run()
return proto
end
--- Converts Lua script to instructions and returns its proto.
-- @param <string> script A Lua script.
-- @return <table> proto The proto of a Lua script.
function API.InstructionGenerator.ConvertScriptToInstructions(script)
assert(type(script) == "string", "Expected string for argument 'script', but got " .. type(script))
local AST = API.Interpreter.ConvertToAST(script)
local proto = API.InstructionGenerator.ConvertASTToInstructions(AST)
return proto
end
--* API.Assembler *--
--- Tokenizes code and returns its tokens.
-- @param <string> code Assembly code.
-- @return <table> tokens The tokens of an assembly code.
function API.Assembler.Tokenize(code)
assert(type(code) == "string", "Expected string for argument 'code', but got " .. type(code))
local tokens = Assembler:tokenize(code)
return tokens
end
--- Tokenizes and Parses code and returns its proto.
-- @param <string> code Assembly code.
-- @return <table> proto The proto of the code.
function API.Assembler.Parse(code)
assert(type(code) == "string", "Expected string for argument 'code', but got " .. type(code))
local tokens = Assembler:tokenize(code)
local proto = Assembler:parse(tokens)
return proto
end
--- Executes assembly code
-- @param <string> code Assembly code.
-- @return <any> returnValue The return value of the assembly code.
function API.Assembler.Execute(code)
assert(type(code) == "string", "Expected string for argument 'code', but got " .. type(code))
local proto = API.Assembler.Parse(code)
return API.VirtualMachine.ExecuteProto(proto)
end
--* API.ASTExecutor *--
--- Execute an Abstract Syntax Tree and return its returned values.
-- @param <table> AST An Abstract Syntax Tree of a Lua script.
-- @param <table?> state The state of a Lua script.
-- @param <boolean?> debug Whether to run in debug mode or not.
-- @param <string?> scriptName The name of the script.
-- @return <...any> returnValue The return value of the AST.
function API.ASTExecutor.Execute(AST, state, debug, scriptName)
assert(type(AST) == "table", "Expected table for argument 'AST', but got " .. type(AST))
local returnValues = { ASTExecutor:new(AST, state, debug, scriptName):execute() }
return unpack(returnValues)
end
--- Execute a script using Abstarct Syntax Tree executor and return its returned values.
-- @param <string> script A script to execute.
-- @param <table?> state The state of a Lua script.
-- @param <boolean?> debug Whether to run in debug mode or not.
-- @param <string?> scriptName The name of the script.
-- @return <...any> returnValue The return value of the Lua script.
function API.ASTExecutor.ExecuteScript(script, state, debug, scriptName)
assert(type(script) == "string", "Expected string for argument 'script', but got " .. type(script))
local AST = API.Interpreter.ConvertToAST(script)
local returnValues = { ASTExecutor:new(AST, state, debug, scriptName):execute() }
return unpack(returnValues)
end
--* API.Minifier *--
--- Minify an Abstract Syntax Tree.
-- @param <table> AST The Abstract Syntax Tree of a Lua script.
-- @return <table> minifiedAST The minified Abstract Syntax Tree.
function API.Minifier.MinifyAST(AST)
assert(type(AST) == "table", "Expected table for argument 'AST', but got " .. type(AST))
local minifiedAST = Minifier:new(AST):run()
return minifiedAST
end
--- Minify a Lua script.
-- @param <string> script A Lua script.
-- @return <table> minifiedAST The minified Abstract Syntax Tree.
function API.Minifier.MinifyScript(script)
assert(type(script) == "string", "Expected string for argument 'script', but got " .. type(script))
local AST = API.Interpreter.ConvertToAST(script)
local minifiedAST = API.Minifier.MinifyAST(AST)
return minifiedAST
end
--* API.Packer *--
--- Pack all `require` calls in an Abstract Syntax Tree.
-- @param <table> AST The Abstract Syntax Tree of a Lua script.
-- @param <string?> localizedPath The localized path of require calls.
-- @return <table> packedAST The packed Abstract Syntax Tree.
function API.Packer.PackAST(AST, localizedPath)
assert(type(AST) == "table", "Expected table for argument 'AST', but got " .. type(AST))
local packedAST = Packer:new(AST, localizedPath):pack()
return packedAST
end
--- Pack all `require` calls in one Lua script.
-- @param <string> script A Lua script.
-- @param <string?> localizedPath The localized path of require calls.
-- @return <table> packedAST The packed Abstract Syntax Tree.
function API.Packer.PackScript(script, localizedPath)
assert(type(script) == "string", "Expected string for argument 'script', but got " .. type(script))
local AST = API.Interpreter.ConvertToAST(script)
local packedAST = API.Packer.PackAST(AST, localizedPath)
return packedAST
end
--* API.ASTToTokensConverter *--
--- Convert the given Abstract Syntax Tree to tokens and return it
-- @param <table> AST The Abstract Syntax Tree of a Lua script.
-- @return <table> tokens The tokens of the given Abstract Syntax Tree.
function API.ASTToTokensConverter.ConvertToTokens(AST)
assert(type(AST) == "table", "Expected table for argument 'AST', but got " .. type(AST))
local tokens = ASTToTokensConverter:new(AST):convert()
return tokens
end
--* API.Printer *--
--* API.Printer.TokenPrinter *--
--- Print tokens of a Lua script.
-- @param <table> tokens The tokens of a Lua script.
-- @return <string> script The Lua script.
function API.Printer.TokenPrinter.PrintTokens(tokens)
assert(type(tokens) == "table", "Expected table for argument 'tokens', but got " .. type(tokens))
local script = Printer:new(tokens):run()
return script
end
--- Convert a Lua script to tokens and print them.
-- @param <string> script A Lua script.
-- @return <string> script The Lua script.
function API.Printer.TokenPrinter.PrintScriptTokens(script)
assert(type(script) == "string", "Expected string for argument 'script', but got " .. type(script))
local tokens = API.Interpreter.ConvertToTokens(script)
local script = API.Printer.TokenPrinter.PrintTokens(tokens)
return script
end
--* API.Printer.ASTPrinter *--
--- Print an Abstract Syntax Tree.
-- @param <table> AST The Abstract Syntax Tree of a Lua script.
-- @return <string> script The Lua script.
function API.Printer.ASTPrinter.PrintAST(AST)
assert(type(AST) == "table", "Expected table for argument 'AST', but got " .. type(AST))
local script = ASTPrinter:new(AST):print()
return script
end
--- Convert a Lua script to an Abstract Syntax Tree and print it.
-- @param <string> script A Lua script.
-- @return <string> script The Lua script.
function API.Printer.ASTPrinter.PrintScriptAST(script)
assert(type(script) == "string", "Expected string for argument 'script', but got " .. type(script))
local AST = API.Interpreter.ConvertToAST(script)
local script = API.Printer.ASTPrinter.PrintAST(AST)
return script
end
--* API.Obfuscator *--
--* API.Obfuscator.ASTObfuscator *--
--- Obfuscate an Abstract Syntax Tree.
-- @param <table> ast The Abstract Syntax Tree of a Lua script.
-- @return <table> obfuscatedAST The obfuscated Abstract Syntax Tree.
function API.Obfuscator.ASTObfuscator.ObfuscateAST(ast)
assert(type(ast) == "table", "Expected table for argument 'ast', but got " .. type(ast))
local obfuscatedAST = ASTObfuscator:new(ast):obfuscate()
-- Convert that ast to tokens -> code -> ast
-- because we need to update the ast with new metadata
-- and it's currently the only way to do that
local obfuscatedASTTokens = ASTToTokensConverter:new(obfuscatedAST):convert()
local obfuscatedCode = Printer:new(obfuscatedASTTokens):run()
local obfuscatedAST = API.Interpreter.ConvertToAST(obfuscatedCode)
return obfuscatedAST
end
--- Obfuscate a Lua script.
-- @param <string> script A Lua script.
-- @return <string> obfuscatedScript The obfuscated version of the given Lua script.
function API.Obfuscator.ASTObfuscator.ObfuscateScript(script)
assert(type(script) == "string", "Expected string for argument 'script', but got " .. type(script))
local AST = API.Interpreter.ConvertToAST(script)
local obfuscatedAST = API.Obfuscator.ASTObfuscator.ObfuscateAST(AST)
local obfuscatedASTTokens = ASTToTokensConverter:new(obfuscatedAST):convert()
local obfuscatedScript = Printer:new(obfuscatedASTTokens):run()
return obfuscatedScript
end
--* API.Obfuscator.IronBrikked *--
--- Obfuscate a Lua prototype.
-- @param <table> proto A Lua prototype.
-- @return <string> obfuscatedCode The obfuscated version of the given Lua prototype.
function API.Obfuscator.IronBrikked.ObfuscateProto(proto)
assert(type(proto) == "table", "Expected table for argument 'proto', but got " .. type(proto))
local obfuscatedCode = IronBrikked:new(proto):obfuscate()
return obfuscatedCode
end
--- Obfuscate a Lua script.
-- @param <string> script A Lua script.
-- @return <string> obfuscatedScript The obfuscated version of the given Lua script.
function API.Obfuscator.IronBrikked.ObfuscateScript(script)
assert(type(script) == "string", "Expected string for argument 'script', but got " .. type(script))
local proto = API.InstructionGenerator.ConvertScriptToInstructions(script)
local obfuscatedScript = API.Obfuscator.IronBrikked.ObfuscateProto(proto)
return obfuscatedScript
end
--* API.LuaState *--
--- Create a new LuaState object.
-- @return <table> luaState The LuaState object.
function API.LuaState.NewLuaState()
local luaState = LuaState:new()
return luaState
end
return API