diff options
Diffstat (limited to '')
-rw-r--r-- | LuaSL/src/LuaSL_runner.c | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/LuaSL/src/LuaSL_runner.c b/LuaSL/src/LuaSL_runner.c new file mode 100644 index 0000000..411c437 --- /dev/null +++ b/LuaSL/src/LuaSL_runner.c | |||
@@ -0,0 +1,160 @@ | |||
1 | |||
2 | #include "LuaSL.h" | ||
3 | |||
4 | |||
5 | #ifdef _WIN32 | ||
6 | # define FMT_SIZE_T "%Iu" | ||
7 | #else | ||
8 | # define FMT_SIZE_T "%zu" | ||
9 | #endif | ||
10 | |||
11 | #define MAX_LUA_MEM (4 * (1024 * 1024)) | ||
12 | |||
13 | #define _edje_lua2_error(L, err_code) _edje_lua2_error_full(__FILE__, __FUNCTION__, __LINE__, L, err_code) | ||
14 | |||
15 | /* | ||
16 | typedef struct _Edje_Lua_Alloc Edje_Lua_Alloc; | ||
17 | |||
18 | struct _Edje_Lua_Alloc | ||
19 | { | ||
20 | size_t max, cur; | ||
21 | }; | ||
22 | |||
23 | static void * | ||
24 | _elua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) | ||
25 | { | ||
26 | Edje_Lua_Alloc *ela = ud; | ||
27 | void *ptr2 = NULL; | ||
28 | |||
29 | if (ela) | ||
30 | { | ||
31 | ela->cur += nsize - osize; | ||
32 | if (ela->cur > ela->max) | ||
33 | { | ||
34 | printf("Lua memory limit of " FMT_SIZE_T " bytes reached (" FMT_SIZE_T " allocated)", ela->max, ela->cur); | ||
35 | } | ||
36 | else if (nsize == 0) | ||
37 | { | ||
38 | free(ptr); | ||
39 | } | ||
40 | else | ||
41 | { | ||
42 | ptr2 = realloc(ptr, nsize); | ||
43 | if (NULL == ptr2) | ||
44 | printf("Lua cannot re-allocate " FMT_SIZE_T " bytes", nsize); | ||
45 | } | ||
46 | } | ||
47 | else | ||
48 | printf("Lua cannoct allocate memory, no Edje_Lua_Alloc"); | ||
49 | |||
50 | return ptr2; | ||
51 | } | ||
52 | |||
53 | static int panics = 0; | ||
54 | static int | ||
55 | _elua_custom_panic(lua_State *L) // Stack usage [-0, +0, m] | ||
56 | { | ||
57 | // If we somehow manage to have multiple panics, it's likely due to being out | ||
58 | // of memory in the following lua_tostring() call. | ||
59 | panics++; | ||
60 | if (panics) | ||
61 | { | ||
62 | printf("Lua PANICS!!!!!"); | ||
63 | } | ||
64 | else | ||
65 | { | ||
66 | printf("Lua PANIC!!!!!: %s", lua_tostring(L, -1)); // Stack usage [-0, +0, m] | ||
67 | } | ||
68 | // The docs say that this will cause an exit(EXIT_FAILURE) if we return, | ||
69 | // and that we we should long jump some where to avoid that. This is only | ||
70 | // called for things not called from a protected environment. We always | ||
71 | // use pcalls though, except for the library load calls. If we can't load | ||
72 | // the standard libraries, then perhaps a crash is the right thing. | ||
73 | return 0; | ||
74 | } | ||
75 | |||
76 | static void | ||
77 | _edje_lua2_error_full(const char *file, const char *fnc, int line, | ||
78 | lua_State *L, int err_code) // Stack usage [-0, +0, m] | ||
79 | { | ||
80 | const char *err_type; | ||
81 | |||
82 | switch (err_code) | ||
83 | { | ||
84 | case LUA_ERRRUN: | ||
85 | err_type = "runtime"; | ||
86 | break; | ||
87 | case LUA_ERRSYNTAX: | ||
88 | err_type = "syntax"; | ||
89 | break; | ||
90 | case LUA_ERRMEM: | ||
91 | err_type = "memory allocation"; | ||
92 | break; | ||
93 | case LUA_ERRERR: | ||
94 | err_type = "error handler"; | ||
95 | break; | ||
96 | default: | ||
97 | err_type = "unknown"; | ||
98 | break; | ||
99 | } | ||
100 | printf("Lua %s error: %s\n", err_type, lua_tostring(L, -1)); // Stack usage [-0, +0, m] | ||
101 | } | ||
102 | |||
103 | static int errFunc(lua_State *L) | ||
104 | { | ||
105 | int i = 0; | ||
106 | lua_Debug ar; | ||
107 | |||
108 | while (lua_getstack(L, i++, &ar)) | ||
109 | { | ||
110 | if (lua_getinfo(L, "nSlu", &ar)) | ||
111 | { | ||
112 | if (NULL == ar.name) | ||
113 | ar.name = "DUNNO"; | ||
114 | printf("Lua error in the %s %s %s @ line %d in %s\n%s!", ar.what, ar.namewhat, ar.name, ar.currentline, ar.short_src, ar.source); | ||
115 | } | ||
116 | } | ||
117 | return 0; | ||
118 | } | ||
119 | */ | ||
120 | |||
121 | void runnerSetup(gameGlobals *game) | ||
122 | { | ||
123 | luaprocInit(); | ||
124 | |||
125 | if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) | ||
126 | PE("Error creating luaproc worker thread."); | ||
127 | if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) | ||
128 | PE("Error creating luaproc worker thread."); | ||
129 | if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) | ||
130 | PE("Error creating luaproc worker thread."); | ||
131 | if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) | ||
132 | PE("Error creating luaproc worker thread."); | ||
133 | } | ||
134 | |||
135 | void runnerTearDown(gameGlobals *game) | ||
136 | { | ||
137 | // TODO - this is what hangs the system. | ||
138 | sched_join_workerthreads(); | ||
139 | } | ||
140 | |||
141 | void runLuaFile(gameGlobals *game, const char *filename) | ||
142 | { | ||
143 | newProc(filename, TRUE); | ||
144 | |||
145 | // TODO, should set up our panic and errfunc as below. Plus the other TODO stuff. | ||
146 | |||
147 | /* | ||
148 | // TODO - hack up LuaJIT so that we can limit memory per state. | ||
149 | // lua_setallocf(L, _elua_alloc, &ela); // LuaJIT uses a heavily hacked up dlmalloc. Seems that standard realloc is not so thread safe? | ||
150 | lua_atpanic(L, _elua_custom_panic); | ||
151 | // TODO - Sandbox out what this opens. See lib_init.c from LuaJIT. | ||
152 | // Just noticed this in the LuaJIT docs - "To change or extend the list of standard libraries to load, copy src/lib_init.c to your project and modify it accordingly. Make sure the jit library is loaded or the JIT compiler will not be activated." | ||
153 | luaL_openlibs(L); | ||
154 | |||
155 | lua_pushcfunction(L, errFunc); | ||
156 | ... | ||
157 | if ((err = lua_pcall(L, 0, 0, -2))) | ||
158 | _edje_lua2_error(L, err); | ||
159 | */ | ||
160 | } | ||