PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/elua/elua-svn-r844-patches/lua-remove-hooks.patch

https://code.google.com/p/mizar32/
Patch | 392 lines | 347 code | 45 blank | 0 comment | 0 complexity | 4f36583faf829afe9c9b98aeb3200da2 MD5 | raw file
  1. Make the inclusion of the Lua hooks mechanism, only useful when Lua is
  2. embedded in a larger C program, conditional on the definition of LUA_HOOKS.
  3. Saving in code: 512 bytes
  4. Note that the hook mechanism is needed by the new eLua interrupt mechanism.
  5. Index: elua-svn/src/lua/ldebug.c
  6. ===================================================================
  7. --- elua-svn.orig/src/lua/ldebug.c 2010-12-02 11:28:05.633727001 +0100
  8. +++ elua-svn/src/lua/ldebug.c 2010-12-02 11:30:17.733727000 +0100
  9. @@ -50,6 +50,7 @@
  10. }
  11. +#ifdef LUA_HOOKS
  12. /*
  13. ** this function can be called asynchronous (e.g. during a signal)
  14. */
  15. @@ -79,6 +80,7 @@
  16. LUA_API int lua_gethookcount (lua_State *L) {
  17. return L->basehookcount;
  18. }
  19. +#endif
  20. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  21. Index: elua-svn/src/lua/ldebug.h
  22. ===================================================================
  23. --- elua-svn.orig/src/lua/ldebug.h 2010-12-02 11:28:05.777727001 +0100
  24. +++ elua-svn/src/lua/ldebug.h 2010-12-02 11:30:17.733727000 +0100
  25. @@ -15,7 +15,9 @@
  26. #define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0)
  27. +#ifdef LUA_HOOKS
  28. #define resethookcount(L) (L->hookcount = L->basehookcount)
  29. +#endif
  30. LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o,
  31. Index: elua-svn/src/lua/lua.h
  32. ===================================================================
  33. --- elua-svn.orig/src/lua/lua.h 2010-12-02 11:28:05.713727001 +0100
  34. +++ elua-svn/src/lua/lua.h 2010-12-02 11:30:17.733727000 +0100
  35. @@ -335,8 +335,10 @@
  36. typedef struct lua_Debug lua_Debug; /* activation record */
  37. +#ifdef LUA_HOOKS
  38. /* Functions to be called by the debuger in specific events */
  39. typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
  40. +#endif
  41. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar);
  42. @@ -346,10 +348,14 @@
  43. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n);
  44. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n);
  45. +#ifdef LUA_HOOKS
  46. LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count);
  47. LUA_API lua_Hook lua_gethook (lua_State *L);
  48. LUA_API int lua_gethookmask (lua_State *L);
  49. LUA_API int lua_gethookcount (lua_State *L);
  50. +#else
  51. +// #define lua_sethook(a,b,c,d) do { } while(0)
  52. +#endif
  53. struct lua_Debug {
  54. Index: elua-svn/src/lua/luaconf.h
  55. ===================================================================
  56. --- elua-svn.orig/src/lua/luaconf.h 2010-12-02 11:28:27.269727000 +0100
  57. +++ elua-svn/src/lua/luaconf.h 2010-12-02 11:31:04.669727000 +0100
  58. @@ -397,6 +397,14 @@
  59. /*
  60. +@@ LUA_HOOKS enables the debug hook interface
  61. +** CHANGE it to undefined if you don't want that
  62. +*/
  63. +#undef LUA_HOOKS
  64. +
  65. +
  66. +
  67. +/*
  68. @@ luai_apicheck is the assert macro used by the Lua-C API.
  69. ** CHANGE luai_apicheck if you want Lua to perform some checks in the
  70. ** parameters it gets from API calls. This may slow down the interpreter
  71. Index: elua-svn/src/lua/ldblib.c
  72. ===================================================================
  73. --- elua-svn.orig/src/lua/ldblib.c 2010-12-02 11:28:05.805727001 +0100
  74. +++ elua-svn/src/lua/ldblib.c 2010-12-02 11:30:17.737727000 +0100
  75. @@ -204,6 +204,7 @@
  76. static const char KEY_HOOK = 'h';
  77. +#ifdef LUA_HOOKS
  78. static void hookf (lua_State *L, lua_Debug *ar) {
  79. static const char *const hooknames[] =
  80. {"call", "return", "line", "count", "tail return"};
  81. @@ -220,6 +221,7 @@
  82. lua_call(L, 2, 0);
  83. }
  84. }
  85. +#endif
  86. static int makemask (const char *smask, int count) {
  87. @@ -242,6 +244,7 @@
  88. }
  89. +#ifdef LUA_HOOKS
  90. static void gethooktable (lua_State *L) {
  91. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  92. lua_rawget(L, LUA_REGISTRYINDEX);
  93. @@ -297,6 +300,7 @@
  94. lua_pushinteger(L, lua_gethookcount(L1));
  95. return 3;
  96. }
  97. +#endif
  98. static int db_debug (lua_State *L) {
  99. @@ -376,14 +380,18 @@
  100. const LUA_REG_TYPE dblib[] = {
  101. {LSTRKEY("debug"), LFUNCVAL(db_debug)},
  102. {LSTRKEY("getfenv"), LFUNCVAL(db_getfenv)},
  103. +#ifdef LUA_HOOKS
  104. {LSTRKEY("gethook"), LFUNCVAL(db_gethook)},
  105. +#endif
  106. {LSTRKEY("getinfo"), LFUNCVAL(db_getinfo)},
  107. {LSTRKEY("getlocal"), LFUNCVAL(db_getlocal)},
  108. {LSTRKEY("getregistry"), LFUNCVAL(db_getregistry)},
  109. {LSTRKEY("getmetatable"), LFUNCVAL(db_getmetatable)},
  110. {LSTRKEY("getupvalue"), LFUNCVAL(db_getupvalue)},
  111. {LSTRKEY("setfenv"), LFUNCVAL(db_setfenv)},
  112. +#ifdef LUA_HOOKS
  113. {LSTRKEY("sethook"), LFUNCVAL(db_sethook)},
  114. +#endif
  115. {LSTRKEY("setlocal"), LFUNCVAL(db_setlocal)},
  116. {LSTRKEY("setmetatable"), LFUNCVAL(db_setmetatable)},
  117. {LSTRKEY("setupvalue"), LFUNCVAL(db_setupvalue)},
  118. Index: elua-svn/src/lua/ldo.c
  119. ===================================================================
  120. --- elua-svn.orig/src/lua/ldo.c 2010-12-02 11:28:15.321727001 +0100
  121. +++ elua-svn/src/lua/ldo.c 2010-12-02 11:31:04.625727000 +0100
  122. @@ -86,7 +86,9 @@
  123. luaF_close(L, L->base); /* close eventual pending closures */
  124. luaD_seterrorobj(L, status, L->base);
  125. L->nCcalls = L->baseCcalls;
  126. +#ifdef LUA_HOOKS
  127. L->allowhook = 1;
  128. +#endif
  129. restore_stack_limit(L);
  130. L->errfunc = 0;
  131. L->errorJmp = NULL;
  132. @@ -182,6 +184,7 @@
  133. }
  134. +#ifdef LUA_HOOKS
  135. void luaD_callhook (lua_State *L, int event, int line) {
  136. lua_Hook hook = L->hook;
  137. if (hook && L->allowhook) {
  138. @@ -207,6 +210,7 @@
  139. L->top = restorestack(L, top);
  140. }
  141. }
  142. +#endif
  143. static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
  144. @@ -310,11 +314,13 @@
  145. for (st = L->top; st < ci->top; st++)
  146. setnilvalue(st);
  147. L->top = ci->top;
  148. +#ifdef LUA_HOOKS
  149. if (L->hookmask & LUA_MASKCALL) {
  150. L->savedpc++; /* hooks assume 'pc' is already incremented */
  151. luaD_callhook(L, LUA_HOOKCALL, -1);
  152. L->savedpc--; /* correct 'pc' */
  153. }
  154. +#endif
  155. return PCRLUA;
  156. }
  157. else { /* if is a C function, call it */
  158. @@ -327,8 +333,10 @@
  159. ci->top = L->top + LUA_MINSTACK;
  160. lua_assert(ci->top <= L->stack_last);
  161. ci->nresults = nresults;
  162. +#ifdef LUA_HOOKS
  163. if (L->hookmask & LUA_MASKCALL)
  164. luaD_callhook(L, LUA_HOOKCALL, -1);
  165. +#endif
  166. lua_unlock(L);
  167. if (ttisfunction(func))
  168. n = (*curr_func(L)->c.f)(L); /* do the actual call */
  169. @@ -345,6 +353,7 @@
  170. }
  171. +#ifdef LUA_HOOKS
  172. static StkId callrethooks (lua_State *L, StkId firstResult) {
  173. ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
  174. luaD_callhook(L, LUA_HOOKRET, -1);
  175. @@ -354,14 +363,17 @@
  176. }
  177. return restorestack(L, fr);
  178. }
  179. +#endif
  180. int luaD_poscall (lua_State *L, StkId firstResult) {
  181. StkId res;
  182. int wanted, i;
  183. CallInfo *ci;
  184. +#ifdef LUA_HOOKS
  185. if (L->hookmask & LUA_MASKRET)
  186. firstResult = callrethooks(L, firstResult);
  187. +#endif
  188. ci = L->ci--;
  189. res = ci->func; /* res == final position of 1st result */
  190. wanted = ci->nresults;
  191. @@ -474,7 +486,9 @@
  192. int status;
  193. unsigned short oldnCcalls = L->nCcalls;
  194. ptrdiff_t old_ci = saveci(L, L->ci);
  195. +#ifdef LUA_HOOKS
  196. lu_byte old_allowhooks = L->allowhook;
  197. +#endif
  198. ptrdiff_t old_errfunc = L->errfunc;
  199. L->errfunc = ef;
  200. status = luaD_rawrunprotected(L, func, u);
  201. @@ -486,7 +500,9 @@
  202. L->ci = restoreci(L, old_ci);
  203. L->base = L->ci->base;
  204. L->savedpc = L->ci->savedpc;
  205. +#ifdef LUA_HOOKS
  206. L->allowhook = old_allowhooks;
  207. +#endif
  208. restore_stack_limit(L);
  209. }
  210. L->errfunc = old_errfunc;
  211. Index: elua-svn/src/lua/lgc.c
  212. ===================================================================
  213. --- elua-svn.orig/src/lua/lgc.c 2010-12-02 11:28:05.585727001 +0100
  214. +++ elua-svn/src/lua/lgc.c 2010-12-02 11:30:17.741727000 +0100
  215. @@ -461,15 +461,19 @@
  216. makewhite(g, o);
  217. tm = fasttm(L, udata->uv.metatable, TM_GC);
  218. if (tm != NULL) {
  219. - lu_byte oldah = L->allowhook;
  220. lu_mem oldt = g->GCthreshold;
  221. +#ifdef LUA_HOOKS
  222. + lu_byte oldah = L->allowhook;
  223. L->allowhook = 0; /* stop debug hooks during GC tag method */
  224. +#endif
  225. g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */
  226. setobj2s(L, L->top, tm);
  227. setuvalue(L, L->top+1, udata);
  228. L->top += 2;
  229. luaD_call(L, L->top - 2, 0);
  230. +#ifdef LUA_HOOKS
  231. L->allowhook = oldah; /* restore hooks */
  232. +#endif
  233. g->GCthreshold = oldt; /* restore threshold */
  234. }
  235. }
  236. Index: elua-svn/src/lua/lstate.c
  237. ===================================================================
  238. --- elua-svn.orig/src/lua/lstate.c 2010-12-02 11:28:05.757727001 +0100
  239. +++ elua-svn/src/lua/lstate.c 2010-12-02 11:30:17.741727000 +0100
  240. @@ -93,11 +93,13 @@
  241. L->stack = NULL;
  242. L->stacksize = 0;
  243. L->errorJmp = NULL;
  244. +#ifdef LUA_HOOKS
  245. L->hook = NULL;
  246. L->hookmask = 0;
  247. L->basehookcount = 0;
  248. L->allowhook = 1;
  249. resethookcount(L);
  250. +#endif
  251. L->openupval = NULL;
  252. L->size_ci = 0;
  253. L->nCcalls = L->baseCcalls = 0;
  254. @@ -131,10 +133,12 @@
  255. preinit_state(L1, G(L));
  256. stack_init(L1, L); /* init stack */
  257. setobj2n(L, gt(L1), gt(L)); /* share table of globals */
  258. +#ifdef LUA_HOOKS
  259. L1->hookmask = L->hookmask;
  260. L1->basehookcount = L->basehookcount;
  261. L1->hook = L->hook;
  262. resethookcount(L1);
  263. +#endif
  264. lua_assert(!isdead(G(L), obj2gco(L1)));
  265. L->top--; /* remove thread from stack */
  266. return L1;
  267. @@ -233,7 +237,9 @@
  268. LUA_API void lua_close (lua_State *L) {
  269. #ifndef LUA_CROSS_COMPILER
  270. int oldstate = platform_cpu_set_global_interrupts( PLATFORM_CPU_DISABLE );
  271. +#ifdef LUA_HOOKS
  272. lua_sethook( L, NULL, 0, 0 );
  273. +#endif
  274. lua_crtstate = NULL;
  275. lua_pushnil( L );
  276. lua_rawseti( L, LUA_REGISTRYINDEX, LUA_INT_HANDLER_KEY );
  277. Index: elua-svn/src/lua/lstate.h
  278. ===================================================================
  279. --- elua-svn.orig/src/lua/lstate.h 2010-12-02 11:28:05.609727001 +0100
  280. +++ elua-svn/src/lua/lstate.h 2010-12-02 11:30:17.741727000 +0100
  281. @@ -116,11 +116,13 @@
  282. int size_ci; /* size of array `base_ci' */
  283. unsigned short nCcalls; /* number of nested C calls */
  284. unsigned short baseCcalls; /* nested C calls when resuming coroutine */
  285. +#ifdef LUA_HOOKS
  286. lu_byte hookmask;
  287. lu_byte allowhook;
  288. int basehookcount;
  289. int hookcount;
  290. lua_Hook hook;
  291. +#endif
  292. TValue l_gt; /* table of globals */
  293. TValue env; /* temporary place for environments */
  294. GCObject *openupval; /* list of open upvalues in this stack */
  295. Index: elua-svn/src/lua/lvm.c
  296. ===================================================================
  297. --- elua-svn.orig/src/lua/lvm.c 2010-12-02 11:28:05.653727001 +0100
  298. +++ elua-svn/src/lua/lvm.c 2010-12-02 11:30:17.745727000 +0100
  299. @@ -76,7 +76,7 @@
  300. }
  301. }
  302. -
  303. +#ifdef LUA_HOOKS
  304. static void traceexec (lua_State *L, const Instruction *pc) {
  305. lu_byte mask = L->hookmask;
  306. const Instruction *oldpc = L->savedpc;
  307. @@ -95,6 +95,7 @@
  308. luaD_callhook(L, LUA_HOOKLINE, newline);
  309. }
  310. }
  311. +#endif
  312. static void callTMres (lua_State *L, StkId res, const TValue *f,
  313. @@ -435,6 +436,7 @@
  314. for (;;) {
  315. const Instruction i = *pc++;
  316. StkId ra;
  317. +#ifdef LUA_HOOKS
  318. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  319. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  320. traceexec(L, pc);
  321. @@ -444,6 +446,7 @@
  322. }
  323. base = L->base;
  324. }
  325. +#endif
  326. /* warning!! several calls may realloc the stack and invalidate `ra' */
  327. ra = RA(i);
  328. lua_assert(base == L->base && L->base == L->ci->base);
  329. Index: elua-svn/src/lua/lua.c
  330. ===================================================================
  331. --- elua-svn.orig/src/lua/lua.c 2010-12-02 11:28:15.325727001 +0100
  332. +++ elua-svn/src/lua/lua.c 2010-12-02 11:32:23.141727000 +0100
  333. @@ -25,17 +25,21 @@
  334. +#ifdef LUA_HOOKS
  335. static void lstop (lua_State *L, lua_Debug *ar) {
  336. (void)ar; /* unused arg. */
  337. lua_sethook(L, NULL, 0, 0);
  338. luaL_error(L, "interrupted!");
  339. }
  340. +#endif
  341. static void laction (int i) {
  342. signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
  343. terminate process (default action) */
  344. +#ifdef LUA_HOOKS
  345. lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
  346. +#endif
  347. }