aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/src/LuaSL_main.c
diff options
context:
space:
mode:
authorDavid Walter Seikel2012-02-05 13:09:13 +1000
committerDavid Walter Seikel2012-02-05 13:09:13 +1000
commitb8ac69f1c3e21822b1d4e0713b2d77331f29c9aa (patch)
treebc9c7396edfadc3b65312865749239839ef032d7 /LuaSL/src/LuaSL_main.c
parentMerge doneParsing(), it's only used once. (diff)
downloadSledjHamr-b8ac69f1c3e21822b1d4e0713b2d77331f29c9aa.zip
SledjHamr-b8ac69f1c3e21822b1d4e0713b2d77331f29c9aa.tar.gz
SledjHamr-b8ac69f1c3e21822b1d4e0713b2d77331f29c9aa.tar.bz2
SledjHamr-b8ac69f1c3e21822b1d4e0713b2d77331f29c9aa.tar.xz
Turn on the script running stuff, clean out the now excess tests, then move the runner to it's own file.
Diffstat (limited to 'LuaSL/src/LuaSL_main.c')
-rw-r--r--LuaSL/src/LuaSL_main.c245
1 files changed, 2 insertions, 243 deletions
diff --git a/LuaSL/src/LuaSL_main.c b/LuaSL/src/LuaSL_main.c
index 5601dde..247b360 100644
--- a/LuaSL/src/LuaSL_main.c
+++ b/LuaSL/src/LuaSL_main.c
@@ -1,7 +1,6 @@
1 1
2#include "LuaSL.h" 2#include "LuaSL.h"
3 3
4#define LUA_TEST 0
5 4
6static int scriptCount; 5static int scriptCount;
7 6
@@ -84,188 +83,13 @@ static void dirList_cb(const char *name, const char *path, void *data)
84 scriptCount++; 83 scriptCount++;
85 snprintf(buf, sizeof(buf), "%s/%s", path, name); 84 snprintf(buf, sizeof(buf), "%s/%s", path, name);
86 if (compileLSL(game, buf, FALSE)) 85 if (compileLSL(game, buf, FALSE))
87 PD("Against all odds, the compile of %s worked! lol", buf); 86 PD("The compile of %s worked,", buf);
88 else 87 else
89 PE("The compile of %s failed!", buf); 88 PE("The compile of %s failed!", buf);
90 } 89 }
91 } 90 }
92} 91}
93 92
94#if LUA_TEST
95static void dirListLua_cb(const char *name, const char *path, void *data)
96{
97 char buf[PATH_MAX];
98 char *ext = rindex(name, '.');
99
100 if (ext)
101 {
102 if (0 == strcmp(ext, ".lua"))
103 {
104 scriptCount++;
105 snprintf(buf, sizeof(buf), "luac %s/%s 2>/dev/null", path, name);
106 system(buf);
107 }
108 }
109}
110
111#ifdef _WIN32
112# define FMT_SIZE_T "%Iu"
113#else
114# define FMT_SIZE_T "%zu"
115#endif
116
117#define MAX_LUA_MEM (4 * (1024 * 1024))
118
119#define _edje_lua2_error(L, err_code) _edje_lua2_error_full(__FILE__, __FUNCTION__, __LINE__, L, err_code)
120
121/*
122typedef struct _Edje_Lua_Alloc Edje_Lua_Alloc;
123
124struct _Edje_Lua_Alloc
125{
126 size_t max, cur;
127};
128
129static void *
130_elua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
131{
132 Edje_Lua_Alloc *ela = ud;
133 void *ptr2 = NULL;
134
135 if (ela)
136 {
137 ela->cur += nsize - osize;
138 if (ela->cur > ela->max)
139 {
140 printf("Lua memory limit of " FMT_SIZE_T " bytes reached (" FMT_SIZE_T " allocated)", ela->max, ela->cur);
141 }
142 else if (nsize == 0)
143 {
144 free(ptr);
145 }
146 else
147 {
148 ptr2 = realloc(ptr, nsize);
149 if (NULL == ptr2)
150 printf("Lua cannot re-allocate " FMT_SIZE_T " bytes", nsize);
151 }
152 }
153 else
154 printf("Lua cannoct allocate memory, no Edje_Lua_Alloc");
155
156 return ptr2;
157}
158
159static int panics = 0;
160static int
161_elua_custom_panic(lua_State *L) // Stack usage [-0, +0, m]
162{
163 // If we somehow manage to have multiple panics, it's likely due to being out
164 // of memory in the following lua_tostring() call.
165 panics++;
166 if (panics)
167 {
168 printf("Lua PANICS!!!!!");
169 }
170 else
171 {
172 printf("Lua PANIC!!!!!: %s", lua_tostring(L, -1)); // Stack usage [-0, +0, m]
173 }
174 // The docs say that this will cause an exit(EXIT_FAILURE) if we return,
175 // and that we we should long jump some where to avoid that. This is only
176 // called for things not called from a protected environment. We always
177 // use pcalls though, except for the library load calls. If we can't load
178 // the standard libraries, then perhaps a crash is the right thing.
179 return 0;
180}
181
182static void
183_edje_lua2_error_full(const char *file, const char *fnc, int line,
184 lua_State *L, int err_code) // Stack usage [-0, +0, m]
185{
186 const char *err_type;
187
188 switch (err_code)
189 {
190 case LUA_ERRRUN:
191 err_type = "runtime";
192 break;
193 case LUA_ERRSYNTAX:
194 err_type = "syntax";
195 break;
196 case LUA_ERRMEM:
197 err_type = "memory allocation";
198 break;
199 case LUA_ERRERR:
200 err_type = "error handler";
201 break;
202 default:
203 err_type = "unknown";
204 break;
205 }
206 printf("Lua %s error: %s\n", err_type, lua_tostring(L, -1)); // Stack usage [-0, +0, m]
207}
208
209static int errFunc(lua_State *L)
210{
211 int i = 0;
212 lua_Debug ar;
213
214 while (lua_getstack(L, i++, &ar))
215 {
216 if (lua_getinfo(L, "nSlu", &ar))
217 {
218 if (NULL == ar.name)
219 ar.name = "DUNNO";
220 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);
221 }
222 }
223 return 0;
224}
225*/
226
227static void runnerSetup(gameGlobals *game)
228{
229 luaprocInit();
230
231 if ( sched_create_worker( ) != LUAPROC_SCHED_OK )
232 PE("Error creating luaproc worker thread.");
233 if ( sched_create_worker( ) != LUAPROC_SCHED_OK )
234 PE("Error creating luaproc worker thread.");
235 if ( sched_create_worker( ) != LUAPROC_SCHED_OK )
236 PE("Error creating luaproc worker thread.");
237 if ( sched_create_worker( ) != LUAPROC_SCHED_OK )
238 PE("Error creating luaproc worker thread.");
239}
240
241static void runnerTearDown(gameGlobals *game)
242{
243// TODO - this is what hangs the system.
244 sched_join_workerthreads();
245}
246
247static void runLuaFile(gameGlobals *game, const char *filename)
248{
249 newProc(filename, TRUE);
250
251// TODO, should set up our panic and errfunc as below. Plus the other TODO stuff.
252
253/*
254// TODO - hack up LuaJIT so that we can limit memory per state.
255// lua_setallocf(L, _elua_alloc, &ela); // LuaJIT uses a heavily hacked up dlmalloc. Seems that standard realloc is not so thread safe?
256 lua_atpanic(L, _elua_custom_panic);
257// TODO - Sandbox out what this opens. See lib_init.c from LuaJIT.
258// 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."
259 luaL_openlibs(L);
260
261 lua_pushcfunction(L, errFunc);
262 ...
263 if ((err = lua_pcall(L, 0, 0, -2)))
264 _edje_lua2_error(L, err);
265*/
266}
267
268#endif
269 93
270int 94int
271main(int argc, char **argv) 95main(int argc, char **argv)
@@ -329,10 +153,6 @@ main(int argc, char **argv)
329 struct timeval lastTime2; 153 struct timeval lastTime2;
330 struct timeval thisTime2; 154 struct timeval thisTime2;
331 float diff; 155 float diff;
332#if LUA_TEST
333 unsigned int lslCount;
334 float diff0;
335#endif
336 156
337 if (game.ui) 157 if (game.ui)
338 { 158 {
@@ -405,71 +225,14 @@ main(int argc, char **argv)
405 225
406 // Do the compiles. 226 // Do the compiles.
407 scriptCount = 0; 227 scriptCount = 0;
408 gettimeofday(&lastTime2, 0);
409 compilerSetup(&game); 228 compilerSetup(&game);
410#if LUA_TEST
411 runnerSetup(&game); 229 runnerSetup(&game);
412#endif 230 gettimeofday(&lastTime2, 0);
413 snprintf(buf, sizeof(buf), "%s/Test sim/objects", PACKAGE_DATA_DIR); 231 snprintf(buf, sizeof(buf), "%s/Test sim/objects", PACKAGE_DATA_DIR);
414 eina_file_dir_list(buf, EINA_TRUE, dirList_cb, &game); 232 eina_file_dir_list(buf, EINA_TRUE, dirList_cb, &game);
415 diff = timeDiff(&thisTime2, &lastTime2); 233 diff = timeDiff(&thisTime2, &lastTime2);
416 printf("Compiling %d LSL scripts took %f seconds, that's %f scripts per second.\n", scriptCount, diff, scriptCount / diff); 234 printf("Compiling %d LSL scripts took %f seconds, that's %f scripts per second.\n", scriptCount, diff, scriptCount / diff);
417 235
418#if LUA_TEST
419 lslCount = scriptCount;
420 diff0 = diff;
421 scriptCount = 0;
422 gettimeofday(&lastTime2, 0);
423 snprintf(buf, sizeof(buf), "%s/testLua", PACKAGE_DATA_DIR);
424 eina_file_dir_list(buf, EINA_TRUE, dirListLua_cb, &game);
425 diff = timeDiff(&thisTime2, &lastTime2);
426 printf("Compiling %d Lua scripts took %f seconds, that's %f scripts per second.\n\n", scriptCount, diff, scriptCount / diff);
427
428 printf("Combined estimate of compiling speed is %f scripts per second.\n", 1 / ((diff0 / lslCount) + (diff / scriptCount)));
429
430 gettimeofday(&lastTime2, 0);
431 snprintf(buf, sizeof(buf), "lua luaprocTest0.lua");
432 system(buf);
433 diff = timeDiff(&thisTime2, &lastTime2);
434 printf("%s TOOK %f seconds......................................................................................................\n", buf, diff);
435
436 gettimeofday(&lastTime2, 0);
437 snprintf(buf, sizeof(buf), "%s/testLua/luaprocTest0_C.lua", PACKAGE_DATA_DIR);
438 runLuaFile(&game, buf);
439 diff = timeDiff(&thisTime2, &lastTime2);
440 printf("Running that last one locally TOOK %f seconds.\n",diff);
441
442 gettimeofday(&lastTime2, 0);
443 snprintf(buf, sizeof(buf), "lua luaprocTest1.lua");
444 system(buf);
445 diff = timeDiff(&thisTime2, &lastTime2);
446 printf("%s TOOK %f seconds.\n", buf, diff);
447
448 gettimeofday(&lastTime2, 0);
449 snprintf(buf, sizeof(buf), "../../libraries/luajit-2.0/src/luajit luaprocTest1.lua");
450 system(buf);
451 diff = timeDiff(&thisTime2, &lastTime2);
452 printf("%s TOOK %f seconds.\n", buf, diff);
453
454 gettimeofday(&lastTime2, 0);
455 snprintf(buf, sizeof(buf), "lua luaprocTest2.lua");
456 system(buf);
457 diff = timeDiff(&thisTime2, &lastTime2);
458 printf("%s TOOK %f seconds.\n", buf, diff);
459
460 gettimeofday(&lastTime2, 0);
461 snprintf(buf, sizeof(buf), "../../libraries/luajit-2.0/src/luajit luaprocTest2.lua");
462 system(buf);
463 diff = timeDiff(&thisTime2, &lastTime2);
464 printf("%s TOOK %f seconds.\n", buf, diff);
465
466 gettimeofday(&lastTime2, 0);
467 snprintf(buf, sizeof(buf), "%s/testLua/luaprocTest2_C.lua", PACKAGE_DATA_DIR);
468 runLuaFile(&game, buf);
469 diff = timeDiff(&thisTime2, &lastTime2);
470 printf("Running that last one locally TOOK %f seconds.\n",diff);
471#endif
472
473 if (game.ui) 236 if (game.ui)
474 { 237 {
475 ecore_main_loop_begin(); 238 ecore_main_loop_begin();
@@ -477,11 +240,7 @@ main(int argc, char **argv)
477 ecore_evas_free(game.ee); 240 ecore_evas_free(game.ee);
478 } 241 }
479 242
480#if LUA_TEST
481 runnerTearDown(&game); 243 runnerTearDown(&game);
482 diff = timeDiff(&thisTime2, &lastTime2);
483 printf("Running that last one locally TOOK %f seconds.\n",diff);
484#endif
485 edje_shutdown(); 244 edje_shutdown();
486 ecore_evas_shutdown(); 245 ecore_evas_shutdown();
487 } 246 }