diff options
author | David Walter Seikel | 2012-01-25 02:29:35 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-25 02:29:35 +1000 |
commit | 3236320311f15640083c5bf420bcdeab725ff242 (patch) | |
tree | 7f8abdafad10884fbdedf919e49c8b79c1f2287b /LuaSL/src | |
parent | Quick and dirty testing of luaJIT+luaproc from LuaSL. (diff) | |
download | SledjHamr-3236320311f15640083c5bf420bcdeab725ff242.zip SledjHamr-3236320311f15640083c5bf420bcdeab725ff242.tar.gz SledjHamr-3236320311f15640083c5bf420bcdeab725ff242.tar.bz2 SledjHamr-3236320311f15640083c5bf420bcdeab725ff242.tar.xz |
Hack together LuaSL, LuaJIT, and luaproc. It's frankenstein right now. lol
Diffstat (limited to 'LuaSL/src')
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.h | 7 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_main.c | 185 |
2 files changed, 186 insertions, 6 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 687c56c..20277b9 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h | |||
@@ -20,6 +20,13 @@ | |||
20 | #include <stdio.h> | 20 | #include <stdio.h> |
21 | #include <limits.h> // For PATH_MAX. | 21 | #include <limits.h> // For PATH_MAX. |
22 | 22 | ||
23 | #include <lua.h> | ||
24 | #include <luajit.h> | ||
25 | #include <lualib.h> | ||
26 | #include <lauxlib.h> | ||
27 | |||
28 | #include <luaproc/sched.h> | ||
29 | |||
23 | #include "LuaSL_lemon_yaccer.h" | 30 | #include "LuaSL_lemon_yaccer.h" |
24 | 31 | ||
25 | #define YYERRCODE 256 | 32 | #define YYERRCODE 256 |
diff --git a/LuaSL/src/LuaSL_main.c b/LuaSL/src/LuaSL_main.c index 3f20460..f2f4f17 100644 --- a/LuaSL/src/LuaSL_main.c +++ b/LuaSL/src/LuaSL_main.c | |||
@@ -106,6 +106,163 @@ static void dirListLua_cb(const char *name, const char *path, void *data) | |||
106 | } | 106 | } |
107 | } | 107 | } |
108 | } | 108 | } |
109 | |||
110 | #ifdef _WIN32 | ||
111 | # define FMT_SIZE_T "%Iu" | ||
112 | #else | ||
113 | # define FMT_SIZE_T "%zu" | ||
114 | #endif | ||
115 | |||
116 | #define MAX_LUA_MEM (4 * (1024 * 1024)) | ||
117 | |||
118 | #define _edje_lua2_error(L, err_code) _edje_lua2_error_full(__FILE__, __FUNCTION__, __LINE__, L, err_code) | ||
119 | |||
120 | /* | ||
121 | typedef struct _Edje_Lua_Alloc Edje_Lua_Alloc; | ||
122 | |||
123 | struct _Edje_Lua_Alloc | ||
124 | { | ||
125 | size_t max, cur; | ||
126 | }; | ||
127 | |||
128 | static void * | ||
129 | _elua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) | ||
130 | { | ||
131 | Edje_Lua_Alloc *ela = ud; | ||
132 | void *ptr2 = NULL; | ||
133 | |||
134 | if (ela) | ||
135 | { | ||
136 | ela->cur += nsize - osize; | ||
137 | if (ela->cur > ela->max) | ||
138 | { | ||
139 | printf("Lua memory limit of " FMT_SIZE_T " bytes reached (" FMT_SIZE_T " allocated)", ela->max, ela->cur); | ||
140 | } | ||
141 | else if (nsize == 0) | ||
142 | { | ||
143 | free(ptr); | ||
144 | } | ||
145 | else | ||
146 | { | ||
147 | ptr2 = realloc(ptr, nsize); | ||
148 | if (NULL == ptr2) | ||
149 | printf("Lua cannot re-allocate " FMT_SIZE_T " bytes", nsize); | ||
150 | } | ||
151 | } | ||
152 | else | ||
153 | printf("Lua cannoct allocate memory, no Edje_Lua_Alloc"); | ||
154 | |||
155 | return ptr2; | ||
156 | } | ||
157 | |||
158 | static int panics = 0; | ||
159 | static int | ||
160 | _elua_custom_panic(lua_State *L) // Stack usage [-0, +0, m] | ||
161 | { | ||
162 | // If we somehow manage to have multiple panics, it's likely due to being out | ||
163 | // of memory in the following lua_tostring() call. | ||
164 | panics++; | ||
165 | if (panics) | ||
166 | { | ||
167 | printf("Lua PANICS!!!!!"); | ||
168 | } | ||
169 | else | ||
170 | { | ||
171 | printf("Lua PANIC!!!!!: %s", lua_tostring(L, -1)); // Stack usage [-0, +0, m] | ||
172 | } | ||
173 | // The docs say that this will cause an exit(EXIT_FAILURE) if we return, | ||
174 | // and that we we should long jump some where to avoid that. This is only | ||
175 | // called for things not called from a protected environment. We always | ||
176 | // use pcalls though, except for the library load calls. If we can't load | ||
177 | // the standard libraries, then perhaps a crash is the right thing. | ||
178 | return 0; | ||
179 | } | ||
180 | |||
181 | static void | ||
182 | _edje_lua2_error_full(const char *file, const char *fnc, int line, | ||
183 | lua_State *L, int err_code) // Stack usage [-0, +0, m] | ||
184 | { | ||
185 | const char *err_type; | ||
186 | |||
187 | switch (err_code) | ||
188 | { | ||
189 | case LUA_ERRRUN: | ||
190 | err_type = "runtime"; | ||
191 | break; | ||
192 | case LUA_ERRSYNTAX: | ||
193 | err_type = "syntax"; | ||
194 | break; | ||
195 | case LUA_ERRMEM: | ||
196 | err_type = "memory allocation"; | ||
197 | break; | ||
198 | case LUA_ERRERR: | ||
199 | err_type = "error handler"; | ||
200 | break; | ||
201 | default: | ||
202 | err_type = "unknown"; | ||
203 | break; | ||
204 | } | ||
205 | printf("Lua %s error: %s\n", err_type, lua_tostring(L, -1)); // Stack usage [-0, +0, m] | ||
206 | } | ||
207 | |||
208 | static int errFunc(lua_State *L) | ||
209 | { | ||
210 | int i = 0; | ||
211 | lua_Debug ar; | ||
212 | |||
213 | while (lua_getstack(L, i++, &ar)) | ||
214 | { | ||
215 | if (lua_getinfo(L, "nSlu", &ar)) | ||
216 | { | ||
217 | if (NULL == ar.name) | ||
218 | ar.name = "DUNNO"; | ||
219 | 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); | ||
220 | } | ||
221 | } | ||
222 | return 0; | ||
223 | } | ||
224 | */ | ||
225 | |||
226 | static void runnerSetup(gameGlobals *game) | ||
227 | { | ||
228 | luaprocInit(); | ||
229 | |||
230 | if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) | ||
231 | PE("Error creating luaproc worker thread."); | ||
232 | if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) | ||
233 | PE("Error creating luaproc worker thread."); | ||
234 | if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) | ||
235 | PE("Error creating luaproc worker thread."); | ||
236 | if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) | ||
237 | PE("Error creating luaproc worker thread."); | ||
238 | } | ||
239 | |||
240 | static void runnerTearDown(gameGlobals *game) | ||
241 | { | ||
242 | // TODO - this is what hangs the system. | ||
243 | sched_join_workerthreads(); | ||
244 | } | ||
245 | |||
246 | static void runLuaFile(gameGlobals *game, const char *filename) | ||
247 | { | ||
248 | newProc(filename, TRUE); | ||
249 | |||
250 | // TODO, should set up our panic and errfunc as below. Plus the other TODO stuff. | ||
251 | |||
252 | /* | ||
253 | // TODO - hack up LuaJIT so that we can limit memory per state. | ||
254 | // lua_setallocf(L, _elua_alloc, &ela); // LuaJIT uses a heavily hacked up dlmalloc. Seems that standard realloc is not so thread safe? | ||
255 | lua_atpanic(L, _elua_custom_panic); | ||
256 | // TODO - Sandbox out what this opens. See lib_init.c from LuaJIT. | ||
257 | luaL_openlibs(L); | ||
258 | |||
259 | lua_pushcfunction(L, errFunc); | ||
260 | ... | ||
261 | if ((err = lua_pcall(L, 0, 0, -2))) | ||
262 | _edje_lua2_error(L, err); | ||
263 | */ | ||
264 | } | ||
265 | |||
109 | #endif | 266 | #endif |
110 | 267 | ||
111 | int | 268 | int |
@@ -248,6 +405,7 @@ main(int argc, char **argv) | |||
248 | scriptCount = 0; | 405 | scriptCount = 0; |
249 | gettimeofday(&lastTime2, 0); | 406 | gettimeofday(&lastTime2, 0); |
250 | compilerSetup(&game); | 407 | compilerSetup(&game); |
408 | runnerSetup(&game); | ||
251 | snprintf(buf, sizeof(buf), "%s/Test sim/objects", PACKAGE_DATA_DIR); | 409 | snprintf(buf, sizeof(buf), "%s/Test sim/objects", PACKAGE_DATA_DIR); |
252 | eina_file_dir_list(buf, EINA_TRUE, dirList_cb, &game); | 410 | eina_file_dir_list(buf, EINA_TRUE, dirList_cb, &game); |
253 | diff = timeDiff(&thisTime2, &lastTime2); | 411 | diff = timeDiff(&thisTime2, &lastTime2); |
@@ -266,35 +424,46 @@ main(int argc, char **argv) | |||
266 | printf("Combined estimate of compiling speed is %f scripts per second.\n", 1 / ((diff0 / lslCount) + (diff / scriptCount))); | 424 | printf("Combined estimate of compiling speed is %f scripts per second.\n", 1 / ((diff0 / lslCount) + (diff / scriptCount))); |
267 | 425 | ||
268 | gettimeofday(&lastTime2, 0); | 426 | gettimeofday(&lastTime2, 0); |
269 | snprintf(buf, sizeof(buf), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' lua luaprocTest0.lua", PACKAGE_DATA_DIR); | 427 | snprintf(buf, sizeof(buf), "lua luaprocTest0.lua"); |
270 | system(buf); | 428 | system(buf); |
271 | diff = timeDiff(&thisTime2, &lastTime2); | 429 | diff = timeDiff(&thisTime2, &lastTime2); |
272 | printf("%s TOOK %f seconds.\n", buf, diff); | 430 | printf("%s TOOK %f seconds......................................................................................................\n", buf, diff); |
273 | 431 | ||
274 | gettimeofday(&lastTime2, 0); | 432 | gettimeofday(&lastTime2, 0); |
275 | snprintf(buf, sizeof(buf), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' lua luaprocTest1.lua", PACKAGE_DATA_DIR); | 433 | snprintf(buf, sizeof(buf), "%s/testLua/luaprocTest0_C.lua", PACKAGE_DATA_DIR); |
434 | runLuaFile(&game, buf); | ||
435 | diff = timeDiff(&thisTime2, &lastTime2); | ||
436 | printf("Running that last one locally TOOK %f seconds.\n",diff); | ||
437 | |||
438 | gettimeofday(&lastTime2, 0); | ||
439 | snprintf(buf, sizeof(buf), "lua luaprocTest1.lua"); | ||
276 | system(buf); | 440 | system(buf); |
277 | diff = timeDiff(&thisTime2, &lastTime2); | 441 | diff = timeDiff(&thisTime2, &lastTime2); |
278 | printf("%s TOOK %f seconds.\n", buf, diff); | 442 | printf("%s TOOK %f seconds.\n", buf, diff); |
279 | 443 | ||
280 | gettimeofday(&lastTime2, 0); | 444 | gettimeofday(&lastTime2, 0); |
281 | snprintf(buf, sizeof(buf), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' luajit luaprocTest1.lua", PACKAGE_DATA_DIR); | 445 | snprintf(buf, sizeof(buf), "../../libraries/luajit-2.0/src/luajit luaprocTest1.lua"); |
282 | system(buf); | 446 | system(buf); |
283 | diff = timeDiff(&thisTime2, &lastTime2); | 447 | diff = timeDiff(&thisTime2, &lastTime2); |
284 | printf("%s TOOK %f seconds.\n", buf, diff); | 448 | printf("%s TOOK %f seconds.\n", buf, diff); |
285 | 449 | ||
286 | gettimeofday(&lastTime2, 0); | 450 | gettimeofday(&lastTime2, 0); |
287 | snprintf(buf, sizeof(buf), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' lua luaprocTest2.lua", PACKAGE_DATA_DIR); | 451 | snprintf(buf, sizeof(buf), "lua luaprocTest2.lua"); |
288 | system(buf); | 452 | system(buf); |
289 | diff = timeDiff(&thisTime2, &lastTime2); | 453 | diff = timeDiff(&thisTime2, &lastTime2); |
290 | printf("%s TOOK %f seconds.\n", buf, diff); | 454 | printf("%s TOOK %f seconds.\n", buf, diff); |
291 | 455 | ||
292 | gettimeofday(&lastTime2, 0); | 456 | gettimeofday(&lastTime2, 0); |
293 | snprintf(buf, sizeof(buf), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' luajit luaprocTest2.lua", PACKAGE_DATA_DIR); | 457 | snprintf(buf, sizeof(buf), "../../libraries/luajit-2.0/src/luajit luaprocTest2.lua"); |
294 | system(buf); | 458 | system(buf); |
295 | diff = timeDiff(&thisTime2, &lastTime2); | 459 | diff = timeDiff(&thisTime2, &lastTime2); |
296 | printf("%s TOOK %f seconds.\n", buf, diff); | 460 | printf("%s TOOK %f seconds.\n", buf, diff); |
297 | 461 | ||
462 | gettimeofday(&lastTime2, 0); | ||
463 | snprintf(buf, sizeof(buf), "%s/testLua/luaprocTest2_C.lua", PACKAGE_DATA_DIR); | ||
464 | runLuaFile(&game, buf); | ||
465 | diff = timeDiff(&thisTime2, &lastTime2); | ||
466 | printf("Running that last one locally TOOK %f seconds.\n",diff); | ||
298 | #endif | 467 | #endif |
299 | 468 | ||
300 | if (game.ui) | 469 | if (game.ui) |
@@ -303,6 +472,10 @@ main(int argc, char **argv) | |||
303 | ecore_animator_del(ani); | 472 | ecore_animator_del(ani); |
304 | ecore_evas_free(game.ee); | 473 | ecore_evas_free(game.ee); |
305 | } | 474 | } |
475 | |||
476 | runnerTearDown(&game); | ||
477 | diff = timeDiff(&thisTime2, &lastTime2); | ||
478 | printf("Running that last one locally TOOK %f seconds.\n",diff); | ||
306 | edje_shutdown(); | 479 | edje_shutdown(); |
307 | ecore_evas_shutdown(); | 480 | ecore_evas_shutdown(); |
308 | } | 481 | } |