- Timestamp:
- 11/27/11 01:16:15 (18 months ago)
- Location:
- trunk/core
- Files:
-
- 3 edited
-
luascript.c (modified) (6 diffs)
-
luascript.h (modified) (1 diff)
-
script.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/core/luascript.c
r1423 r1433 18 18 #include "core.h" 19 19 #include "gui_fselect.h" 20 #include "lang.h" 20 21 #include "gui_lang.h" 21 22 … … 26 27 27 28 static int lua_script_is_ptp; 28 29 static int run_first_resume; // 1 first 'resume', 0 = resuming from yield 30 static int run_start_tick; // tick count at start of this kbd_task iteration 31 static int run_hook_count; // number of calls to the count hook this kbd_task iteration 32 #define YIELD_CHECK_COUNT 100 // check for yield every N vm instructions 33 #define YIELD_MAX_COUNT_DEFAULT 25 // 25 checks = 2500 vm instructions 34 #define YIELD_MAX_MS_DEFAULT 10 35 static unsigned yield_max_count; 36 static unsigned yield_max_ms; 29 37 static int yield_hook_enabled; 30 38 … … 124 132 static void lua_count_hook(lua_State *L, lua_Debug *ar) 125 133 { 126 if( L->nCcalls <= L->baseCcalls && yield_hook_enabled ) 134 run_hook_count++; 135 if( L->nCcalls > L->baseCcalls || !yield_hook_enabled ) 136 return; 137 if(run_hook_count >= yield_max_count || get_tick_count() - run_start_tick >= yield_max_ms) 127 138 lua_yield( L, 0 ); 128 139 } … … 191 202 return 0; 192 203 } 193 lua_sethook(Lt, lua_count_hook, LUA_MASKCOUNT, 1000);204 lua_sethook(Lt, lua_count_hook, LUA_MASKCOUNT, YIELD_CHECK_COUNT ); 194 205 lua_script_enable_yield_hook(); 195 return 1; 206 run_first_resume = 1; 207 yield_max_count = YIELD_MAX_COUNT_DEFAULT; 208 yield_max_ms = YIELD_MAX_MS_DEFAULT; 209 return 1; 210 } 211 212 // run a timeslice of lua script 213 void lua_script_run(void) 214 { 215 int Lres; 216 int top; 217 if (run_first_resume) { 218 run_first_resume = 0; 219 top = 0; 220 } else { 221 top = lua_gettop(Lt); 222 } 223 run_start_tick = get_tick_count(); 224 run_hook_count = 0; 225 Lres = lua_resume( Lt, top ); 226 227 if (Lres == LUA_YIELD) { 228 // yielded 229 return; 230 } else if(Lres != 0) { 231 lua_script_error(Lt,1); 232 return; 233 } else { 234 // finished normally, add ptp result 235 lua_script_finish(Lt); 236 script_console_add_line(lang_str(LANG_CONSOLE_TEXT_FINISHED)); 237 action_pop(); 238 script_end(); 239 } 196 240 } 197 241 … … 1962 2006 } 1963 2007 2008 /* 2009 set scheduling parameters 2010 old_max_count,old_max_ms=set_yield(max_count,max_ms) 2011 */ 2012 static int luaCB_set_yield( lua_State* L ) 2013 { 2014 lua_pushnumber(L,yield_max_count); 2015 lua_pushnumber(L,yield_max_ms); 2016 yield_max_count = luaL_optnumber(L,1,YIELD_MAX_COUNT_DEFAULT); 2017 yield_max_ms = luaL_optnumber(L,2,YIELD_MAX_MS_DEFAULT); 2018 return 2; 2019 } 2020 2021 1964 2022 static void register_func( lua_State* L, const char *name, void *func) { 1965 2023 lua_pushcfunction( L, func ); … … 2130 2188 FUNC(file_browser) 2131 2189 2190 FUNC(set_yield) 2191 2132 2192 {NULL, NULL}, 2133 2193 }; -
trunk/core/luascript.h
r1101 r1433 5 5 6 6 void lua_script_reset(); 7 int lua_script_start( char const* script,int is_ptp ); 7 int lua_script_start( char const* script,int is_ptp ); // initialize and load script 8 void lua_script_run( void ); // run script timeslice 8 9 void lua_script_error( lua_State* L,int runtime ); 9 10 void lua_script_finish( lua_State* L ); -
trunk/core/script.c
r1396 r1433 82 82 static int script_loaded_params[SCRIPT_NUM_PARAMS]; 83 83 static long running_script_stack_name = -1; 84 85 #ifdef OPT_LUA86 static int state_lua_kbd_first_call_to_resume; // AUJ87 #endif88 84 89 85 //------------------------------------------------------------------- … … 476 472 static void process_script() 477 473 { // Note: This function is called from an action stack for AS_SCRIPT_RUN. 478 479 long t;480 int Lres;481 474 482 475 if (state_kbd_script_run != 3) { 483 476 #ifdef OPT_LUA 484 477 if( L ) { 485 int top; 486 if (state_lua_kbd_first_call_to_resume) { 487 state_lua_kbd_first_call_to_resume = 0; 488 top = 0; 489 } else { 490 top = lua_gettop(Lt); 491 } 492 Lres = lua_resume( Lt, top ); 493 494 if (Lres != LUA_YIELD && Lres != 0) { 495 lua_script_error(Lt,1); 496 return; 497 } 498 499 if (Lres != LUA_YIELD) { 500 // add ptp result 501 lua_script_finish(Lt); 502 script_console_add_line(lang_str(LANG_CONSOLE_TEXT_FINISHED)); 503 action_pop(); 504 script_end(); 505 } 478 lua_script_run(); 506 479 } else 507 480 #endif … … 682 655 } 683 656 } 684 state_lua_kbd_first_call_to_resume = 1;685 657 #else 686 658 char msg[64]; … … 716 688 { 717 689 if (!lua_script_start(script,1)) return -1; 718 state_lua_kbd_first_call_to_resume = 1;719 690 state_kbd_script_run = 1; 720 691 kbd_set_block(1);
Note: See TracChangeset
for help on using the changeset viewer.