-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.c
122 lines (94 loc) · 2.66 KB
/
bootstrap.c
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
#include <limits.h> // bug in gcc config/build, needs to be included before stdlib.h, maybe others, too
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "bootstrap.h"
#include "build/squished.lua.dump"
void startupLog(const char* msg)
{
printXY(0, 0, msg);
}
void printErrorAndDie(const char *message)
{
if (strlen(message) == 0)
message = "printErrorAndDie called with zero length message";
displayMultilineText(message);
exit(1);
}
void printLuaErrorAndDie(lua_State *L, const char *errorContext)
{
// TODO errorContext?
printErrorAndDie(lua_tostring(L, -1));
}
void outOfMemory(void)
{
printErrorAndDie("out of memory");
}
static int luaPanicHook(lua_State *L)
{
// TODO use paging
printXY(0, 3, "Lua panic:");
printXY(0, 4, (char*)lua_tostring(L, -1));
getKey();
return 0; // return to Lua to abort
}
struct luaL_Reg *
copyLuaFunctionTable(const luaL_Reg *sourceEntries, const char *prefixToDrop)
{
int count = 0;
luaL_Reg *entry = (luaL_Reg *)sourceEntries;
for (; entry -> name; ++entry, ++count);
size_t rawSize = (count + 1) * sizeof(luaL_Reg);
char* rawStorage = malloc(rawSize);
memset(rawStorage, 0, rawSize);
luaL_Reg* copy = (luaL_Reg*)rawStorage;
const luaL_Reg *original = sourceEntries;
while (original -> name)
{
const char* name = original -> name;
if (prefixToDrop)
{
size_t prefixLength = strlen(prefixToDrop);
if (strncmp(name, prefixToDrop, prefixLength) == 0)
name = name + prefixLength;
}
copy -> name = name;
copy -> func = original -> func;
++original;
++copy;
}
return (struct luaL_Reg *)rawStorage;
}
int genericMain(void)
{
int status;
lua_State *L = NULL;
clearScreen();
startupLog("Initializing...");
L = luaL_newstate();
lua_atpanic(L, &luaPanicHook);
luaL_openlibs(L);
// register the lua modules provided from the C side
platformLuaBindingSetupHook(L);
{
// set lua load path
lua_getglobal(L, "package");
lua_pushstring(L, "./lua/?.lua;?/init.lua");
lua_setfield(L, -2, "path");
}
startupLog("Running script...");
squished_lua[squished_lua_len - 1] = 0;
status = luaL_dostring(L, squished_lua);
if (status)
printLuaErrorAndDie(L, "running main lua script");
clearScreen();
startupLog("Cleaning up...");
lua_pop(L, 1); // Take the returned value out of the stack
lua_close(L); // Cya, Lua
startupLog("Exiting...");
return 0;
}