-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
290 lines (280 loc) · 13.6 KB
/
init.lua
File metadata and controls
290 lines (280 loc) · 13.6 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
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
local package, insert, getfenv, require, setmetatable, assert, setfenv, unpack, loadstring, create, resume = package, table.insert, getfenv, require, setmetatable, assert, setfenv, unpack, loadstring, coroutine.create, coroutine.resume;
local supported_files = {'lua'};
local preload, loaded, split, getSplitter = package.preload, package.loaded, function(s, spl)
local result = {};
for str in (s..spl):gmatch('(.-)'..spl) do
insert(result, str);
end;
return result;
end, function(s)
return ((s:find('/') ~= nil) and '/') or ((s:find('\\') ~= nil) and '\\') or '/';
end;
local getName, getExtension = function(path)
return path:match('^.+'..getSplitter(path)..'(.+)$');
end, function(path)
return path:match'^.+(%..+)$';
end;
local c_env, c_require, c_loadstring = getfenv(), require, loadstring;
local bundle = require'luvi'.bundle;
local local_path = args[0];
local name, extension = getName(local_path), getExtension(local_path);
local compiler = ((name == 'luvit.exe') and 0) or ((name == 'luvi.exe') and 1) or ((extension == '.exe') and 2) or 3;
local read = ((compiler == 0) and require'fs'.readFileSync) or ((compiler >= 1) and bundle.readfile);
local stat = ((compiler == 0) and require'fs'.statSync) or ((compiler >= 1) and bundle.stat);
local new_env = function()
local env = {};
for i, v in next, _G do
if (type(v) == 'function') then
local success, result = pcall(setfenv, v, env);
if (success == true) then
v = result;
end;
end;
env[i] = v;
end;
return env;
end;
--environments created via loadstring do not have metatables but ones created from a compiler do
--the loadstring environments created are exactly the same in the same environment
local internal_generator_source = [=[
local generator, loadstring, direct, splitter, read, stat, extend_env, c_require, loaded, preload = ...;
local customRequire = function(libName)
if (loaded[libName] ~= nil) then
return loaded[libName];
elseif (preload[libName] ~= nil) then
return preload[libName]();
end;
local c_env = getfenv();
local limport_data = c_env.limport_data;
local directors = {limport_data.cd; direct..splitter..'libs'; direct;};
for i = 1, #directors do
local pathFile = directors[i]..splitter..libName;
local pathExtension = pathFile..'.lua';
local stats = stat(pathExtension);
if (stats ~= nil) then
local body = assert(read(pathExtension));
local convert = assert(loadstring(body));
local ls_env = getfenv(convert);
local ex_ls_env = extend_env(ls_env, limport_data.global_env, c_env);
ex_ls_env.limport_data = {
ls_env = ls_env;
global_env = limport_data.global_env;
parent_env = c_env;
cd = pathFile;
};
local new_require = generator(direct, splitter, read, stat, extend_env, c_require, loaded, preload);
ex_ls_env.require = setfenv(new_require, ex_ls_env);
local result = {setfenv(convert, ex_ls_env)()};
loaded[libName] = unpack(result);
return unpack(result);
end;
end;
return c_require(libName);
end;
return customRequire;]=];
local loadstring = function(...)
return setfenv(c_loadstring(...), new_env());
end;
local extend_env = function(loadstring_env, global_env, parent_env)
local index = function(self, index)
local global_result = global_env[index];
if (global_result == nil) then
return parent_env[index];
end;
return global_result;
end;
local meta = {__index = index; __newindex = function(self, index, value)
global_env[index] = value;
end};
return setmetatable(loadstring_env, meta);
end;
local generator; generator = function(direct, splitter, read, stat, extend_env, c_require, loaded, preload)
return loadstring(internal_generator_source)(generator, loadstring, direct, splitter, read, stat, extend_env, c_require, loaded, preload);
end;
local singleImport = function(libraryName)
--We want to be able to import entire libraries and single files
local splitter = getSplitter(libraryName);
local all = split(libraryName, splitter);
local isMulti = #all > 1;
if (isMulti == true) then --stuff like
--'ekidona/libs/class' needs to inherit the environment of 'ekidona' in one way or another
--'ekidona/libs/class.lua' needs to be compatiable
--'ekidona/package' and 'ekidona/package.lua' also needs to be compatiable with inheriting 'ekidona's environment
--import names should only accept true pathing
--because import affects the main thread that required the module we need to adjust preload still
local direct = all[#all];
local hasExtension = direct:find('%.') ~= nil;
if (hasExtension == true) then
--Library Names must direct to the actual directory, no fake directories or short hand notations
local stats = stat(libraryName);
if (stats ~= nil and stats.type == 'file') then
--I decided to just say fuck inheriting environments of their respective libraries if you want the library just require it
local extension = getExtension(direct);
local paths = {libraryName, libraryName:gsub(extension, ''), direct, direct:gsub(extension, '')};
local loader = function(...)
local body = assert(read(libraryName));
local convert = assert(loadstring(body));
local result = {setfenv(convert, c_env)(...)};
for i = 1, #paths do
loaded[paths[i]] = unpack(result);
end;
return unpack(result);
end;
for i = 1, #paths do
preload[paths[i]] = loader;
end;
--preload[libraryName], preload[libraryName:gsub(extension, '')], preload[direct], preload[direct:gsub(extension, '')] = loader, loader, loader, loader;
end;
elseif (hasExtension == false) then
--Probably a standard directory search to a file maybe or another folder
--ekidona/libs/package, deps/ekidona, deps/ekidona/libs/class, deps/ekidona/libs/package
--package, ekidona, class, package
local stats = stat(libraryName);
if (stats ~= nil and stats.type == 'directory') then
local full_direct = libraryName..splitter..'init.lua';
local stats_i = stat(full_direct);
local paths = {libraryName, direct};
if (stats_i ~= nil and stats_i.type == 'file') then
local customRequire = generator(libraryName, splitter, read, stat, extend_env, c_require, loaded, preload);
local loader = function(...)
local init_body = assert(read(full_direct));
local convert = assert(loadstring(init_body));
local ls_env = getfenv(convert);
local global_env = new_env();
local ex_ls_env = extend_env(ls_env, global_env, c_env);
ex_ls_env.limport_data = {
ls_env = ls_env;
global_env = global_env;
parent_env = c_env;
cd = libraryName;
};
ex_ls_env.require = setfenv(customRequire, ex_ls_env);
local result = {setfenv(convert, ex_ls_env)(...)};
for i = 1, #paths do
loaded[paths[i]] = unpack(result);
end;
return unpack(result);
end;
for i = 1, #paths do
preload[paths[i]] = loader;
end;
else --Init does not exist
local loader = function(...)
for i = 1, #paths do
loaded[paths[i]] = true;
end;
return true;
end;
for i = 1, #paths do
preload[paths[i]] = loader;
end;
end;
else --Probably a file
for i = 1, #supported_files do
local ext = supported_files[i];
local full_direct = libraryName..'.'..ext;
local stats_s = stat(full_direct);
--assert(success_s and stat_type_s == 'directory', 'Impossible path for second check');
if (stats_s ~= nil and stats_s.type == 'file') then
local paths = {libraryName, full_direct, direct, direct..'.'..ext};
local loader = function(...)
local body = assert(read(full_direct));
local convert = assert(loadstring(body));
local result = {setfenv(convert, c_env)(...)};
for e = 1, #paths do
loaded[paths[e]] = unpack(result);
end;
return unpack(result);
end;
for e = 1, #paths do
preload[paths[e]] = loader;
end;
break;
end;
end;
end;
end;
elseif (isMulti == false) then --stuff like 'ekidona'
local direct = all[1];
local hasExtension = direct:find('%.') ~= nil;
if (hasExtension == true) then
--'ekidona.lua', must be a file
local extension = getExtension(direct);
local no_extension = direct:gsub(extension, '');
local paths = {direct, no_extension};
local loader = function(...)
local body = assert(read(direct));
local convert = assert(loadstring(body));
local result = {setfenv(convert, c_env)(...)};
for i = 1, #paths do
loaded[paths[i]] = unpack(result);
end;
return unpack(result);
end;
for i = 1, #paths do
preload[paths[i]] = loader;
end;
elseif (hasExtension == false) then
--'ekidona', could be a directory or a file
local stats = stat(direct);
--stat requires file extension for file types
--assert(success and stat_type == 'file', 'Impossible path');
if (stats ~= nil and stats.type == 'directory') then
local full_direct = direct..splitter..'init.lua';
local stats_i = stat(full_direct);
if (stats_i ~= nil and stats_i.type == 'file') then --Init exists
local customRequire = generator(direct, splitter, read, stat, extend_env, c_require, loaded, preload);
preload[direct] = function(...)
local init_body = assert(read(full_direct));
local convert = assert(loadstring(init_body));
local ls_env = getfenv(convert);
local global_env = new_env();
local ex_ls_env = extend_env(ls_env, global_env, c_env);
ex_ls_env.limport_data = {
ls_env = ls_env;
global_env = global_env;
parent_env = c_env;
cd = direct;
};
ex_ls_env.require = setfenv(customRequire, ex_ls_env);
return setfenv(convert, ex_ls_env)(...);
end;
else --Init does not exist
preload[direct] = function(...)
return true;
end;
end;
else --We have to check for supported file extensions, must check if is a file
for i = 1, #supported_files do
local ext = supported_files[i];
local full_direct = direct..'.'..ext;
local stats_s = stat(full_direct);
--assert(success_s and stat_type_s == 'directory', 'Impossible path for second check');
if (stats_s ~= nil and stats_s.type == 'file') then
local paths = {direct, full_direct};
local loader = function(...)
local body = assert(read(full_direct));
local convert = assert(loadstring(body));
local result = {setfenv(convert, c_env)(...)};
for e = 1, #paths do
loaded[paths[e]] = unpack(result);
end;
return unpack(result);
end;
for e = 1, #paths do
preload[paths[e]] = loader;
end;
break;
end;
end;
end;
end;
end;
end;
return function(...)
local list = {...};
for i = 1, #list do
local thread = create(singleImport);
assert(resume(thread, list[i]));
end;
end;