-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·362 lines (310 loc) · 8.97 KB
/
main.cpp
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
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
#include "common.hpp"
void memUsage(double& vm_usage, double& resident_set)
{
vm_usage = 0.0;
resident_set = 0.0;
ifstream stat_stream("/proc/self/stat", ios_base::in);
string pid, comm, state, ppid, pgrp, session, tty_nr;
string tpgid, flags, minflt, cminflt, majflt, cmajflt;
string utime, stime, cutime, cstime, priority, nice;
string O, itrealvalue, starttime;
unsigned long vsize;
long rss;
stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
>> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
>> utime >> stime >> cutime >> cstime >> priority >> nice
>> O >> itrealvalue >> starttime >> vsize >> rss;
stat_stream.close();
long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024;
vm_usage = vsize / 1024.0;
resident_set = rss * page_size_kb;
}
bool replace(string& str, const string& from, const string& to)
{
size_t start_pos = str.find(from);
if(start_pos == string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
void replaceForPkgPath(string &str)
{
string identifier = "?.lua";
replace(str, "main.lua", identifier);
replace(str, "Main.lua", identifier);
replace(str, "init.lua", identifier);
replace(str, "Init.lua", identifier);
replace(str, "index.lua", identifier);
replace(str, "Index.lua", identifier);
replace(str, "program.lua", identifier);
replace(str, "Program.lua", identifier);
}
int process_exit()
{
exit(1);
return 0;
}
// int process_memoryUsage(lua_State *L)
// {
// double vm, rss;
// memUsage(vm, rss);
// lua_pushnumber(L, vm);
// return 1;
// }
// int process_rss(lua_State *L)
// {
// double vm, rss;
// memUsage(vm, rss);
// lua_pushnumber(L, rss);
// return 1;
// }
static void stackDump (lua_State *L)
{
printf("{");
int top = lua_gettop(L);
for (int i = 1; i <= top; i++)
{
int t = lua_type(L, i);
switch (t)
{
case LUA_TSTRING: /* strings */
printf("`%s'", lua_tostring(L, i));
break;
case LUA_TBOOLEAN: /* booleans */
printf(lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER: /* numbers */
printf("%g", lua_tonumber(L, i));
break;
default: /* other values */
printf("%s", lua_typename(L, t));
break;
}
printf(", ");
}
printf("}\n");
}
int luay_cwd(lua_State *L)
{
char buff[FILENAME_MAX]; //create string buffer to hold path
GetCurrentDir(buff, FILENAME_MAX);
string cwd(buff);
lua_pushstring(L, cwd.c_str());
return 1;
}
int luay_wait(lua_State *L)
{
lua_Number secs = luaL_checknumber(L, 1);
int time = round(secs * 1000000);
this_thread::sleep_for(chrono::microseconds(time));
lua_pushnumber(L, secs);
return 1;
}
class Luay
{
public:
lua_State *L;
int argc;
char** argv;
Luay(int argc, char** argv)
{
this->L = luaL_newstate();
this->argc = argc;
this->argv = argv;
}
string cwd()
{
char buff[FILENAME_MAX]; //create string buffer to hold path
GetCurrentDir(buff, FILENAME_MAX);
string cwd(buff);
return cwd;
}
void peekStack()
{
stackDump(this->L);
}
void executeMainFn()
{
lua_getglobal(this->L, "main");
this->pushArgs();
if (lua_isnil(this->L, this->top() - 2)) // if main is nil
{
lua_getglobal(this->L, "Program");
if (lua_isnil(this->L, this->top())) // if program is nil
return this->error("Your program lacks a 'main' function or 'Program' class with 'Main' method, therefore it can not run.");
lua_pushliteral(this->L, "Main");
lua_gettable(this->L, this->top() - 1); // Program["Main"]
lua_getglobal(this->L, "Program"); // self
this->pushArgs();
if (lua_pcall(this->L, 3, 0, 0) == LUA_OK)
this->popTop();
else
this->error();
}
else
{
if (lua_pcall(this->L, 2, 0, 0) != LUA_OK)
this->error();
}
}
void close()
{
lua_close(this->L);
}
void openLibs(string fileName)
{
// const struct luaL_Reg ProcessLib[] = {
// {"MemoryUsage", process_memoryUsage},
// {"RSS", process_rss}
// };
this->table();
this->pushProcessEnv();
this->pushProcessArgs();
// luaL_setfuncs(this->L, ProcessLib, 0);
lua_setglobal(this->L, "Process");
lua_pushcfunction(this->L, luay_cwd);
lua_setglobal(this->L, "cwd");
luay_cwd(this->L);
lua_setglobal(this->L, "__dirname");
string filePath = this->joinPath(this->cwd(), fileName);
string home(getenv("HOME"));
ifstream luayPathFile(home + "/Desktop/Dev/Luay/luaypath");
string luayPath(
(istreambuf_iterator<char>(luayPathFile)),
(istreambuf_iterator<char>()));
replace(luayPath, "~", home);
lua_pushstring(this->L, luayPath.c_str());
lua_setglobal(this->L, "__luaypath");
lua_pushcfunction(this->L, luay_wait);
lua_setglobal(this->L, "wait");
this->doLuaFile("lib/main.lua");
}
void openDefaultLibs()
{
luaL_openlibs(this->L);
}
string joinPath(string left, string right)
{
return left + "/" + right;
}
void doLuaFile(string fileName)
{
string filePath = this->joinPath(this->cwd(), fileName);
string home(getenv("HOME"));
ifstream luayPathFile(home + "/Luay/luaypath");
string luayPath(
(istreambuf_iterator<char>(luayPathFile)),
(istreambuf_iterator<char>()));
cout<<luayPath<<endl;
replace(luayPath, "~", home);
string fullFilePath = this->joinPath(luayPath, fileName);
string pathStr = fileName == "lib/main.lua" ? fullFilePath : filePath;
string fileStr = pathStr;
replaceForPkgPath(pathStr);
this->doString("package.path = package.path .. ';" + pathStr + "'");
if (luaL_dofile(this->L, fileStr.c_str()) != LUA_OK)
this->error();
}
void doString(string str)
{
if (luaL_dostring(this->L, str.c_str()) != LUA_OK)
this->error();
}
void doFile(string fileName)
{
this->doLuaFile(fileName);
this->executeMainFn();
}
int top()
{
return lua_gettop(this->L);
}
void popTop()
{
lua_pop(this->L, this->top());
}
void error(string msg = "")
{
if (msg.empty())
puts(lua_tostring(this->L, this->top()));
else
puts(("[Luay] " + msg).c_str());
this->popTop();
process_exit();
}
void table()
{
lua_newtable(this->L);
}
void setTable(int top = -3)
{
lua_settable(this->L, top);
}
void pushArgs()
{
lua_pushinteger(this->L, this->argc);
this->table();
int top = this->top();
int offset = 1;
for (int i = offset; i <= this->argc - offset; i++)
{
string arg = this->argv[i];
lua_pushnumber(this->L, i);
lua_pushstring(this->L, arg.c_str());
this->setTable(top);
}
}
void pushProcessArgs()
{
// {<process>}
lua_pushliteral(this->L, "argc"); // {<process>}, "argc"
lua_pushinteger(this->L, this->argc);
this->setTable();
lua_pushliteral(this->L, "argv");
this->table();
int top = this->top();
int offset = 1;
for (int i = offset; i <= this->argc - offset; i++)
{
string arg = this->argv[i];
lua_pushnumber(this->L, i);
lua_pushstring(this->L, arg.c_str());
this->setTable(top);
}
this->setTable();
}
void pushProcessEnv()
{
lua_pushliteral(this->L, "env"); // {...}, "env"
lua_newtable(this->L); // {...}, "env", {}
lua_pushliteral(this->L, "LUAY_ENV"); // {...}, "env", {}, "LUAY_ENV"
lua_pushstring(this->L, "production"); // {...}, "env", {}, "LUAY_ENV", "production"
this->setTable(); // {...}, "env", {["LUAY_ENV"] = "production"}
this->setTable(); // {<process>}
}
};
int main(int argc, char** argv)
{
Luay luay(argc, argv);
if (luay.argc <= 1)
{
puts("Usage: luay <file>\n");
return 0;
}
else
{
char* fileDir = luay.argv[1];
luay.openDefaultLibs();
luay.openLibs(fileDir);
luay.doFile(fileDir);
}
luay.close();
return 0;
}