-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluapp.lua
122 lines (105 loc) · 2.93 KB
/
luapp.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
local luvit = require "luvit.init"
local native = require "uv_native"
local process = require "luvit.process"
local traceback = require "debug".traceback
local table = require "table"
local jit = require "jit"
local origionalP = p
local verbose = false
p = function() end
local function printUsage()
process.stderr:write(("Usage: %s [-i | -r | -d] [-j0 | -j1] [-o0] [-v] [-s] [--] <script file> [<Arguments>...]\n"):format(process.argv[0]))
process.exit(1)
end
if not process.argv[1] then
printUsage()
else
local flags = {}
local args = {}
local parsingArgs = false
for k, v in pairs(process.argv) do
if v ~= process.argv[0] then
parsingArgs = parsingArgs or (v:sub(1, 1) ~= '-')
if parsingArgs then
table.insert(args, v)
else
parsingArgs = v == '--'
if not parsingArgs then
flags[v] = v
end
end
end
end
if flags["-v"] then
verbose = true
p = origionalP
end
if flags["-j0"] then
jit.off()
if verbose then
print "JIT compilation disabled"
end
elseif flags["-j1"] then
jit.on()
if verbose then
print "JIT compilation enabled"
end
end
if flags["-o0"] then
jit.opt.start(0)
if verbose then
print "Disabled optimailisations"
end
end
local loadFunction = require
if flags["-i"] then
loadFunction = nil
elseif flags["-d"] then
loadFunction = dofile
end
if loadFunction ~= nil then
process.luappArgv = process.argv
local newArgs = {}
for k, v in ipairs(args) do
newArgs[k-1] = v
end
process.argv = newArgs
assert(xpcall(function()
loadFunction(newArgs[0])
end, traceback))
else
local function runString(str)
local func = loadstring("p("..str..")")
if not func then
func = assert(loadstring(str))
end
func()
end
local isSilent = flags["-s"] == "-s"
p = origionalP -- Always verbose
if #args > 0 then
runString(table.concat(args, " "))
else
if not isSilent then
process.stdout:write("lua>");
end
process.stdin:on("data", function(data)
local suc, err = pcall(function()
runString(data)
end)
if not suc then
process.stderr:write(err.."\n")
end
if not isSilent then
process.stdout:write("lua>");
end
end)
process.stdin:readStart()
end
end
end
-- Start the event loop
native.run()
if process.exitCode and process.exitCode ~= 0 then
process.exit(process.exitCode or 0)
end