diff options
Diffstat (limited to 'src/lua/lstate.h')
-rw-r--r-- | src/lua/lstate.h | 102 |
1 files changed, 39 insertions, 63 deletions
diff --git a/src/lua/lstate.h b/src/lua/lstate.h index 1b6bcdf..cbcf07e 100644 --- a/src/lua/lstate.h +++ b/src/lua/lstate.h | |||
@@ -87,48 +87,12 @@ | |||
87 | 87 | ||
88 | 88 | ||
89 | /* | 89 | /* |
90 | ** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of | 90 | ** About 'nCcalls': This count has two parts: the lower 16 bits counts |
91 | ** how many "C calls" it still can do in the C stack, to avoid C-stack | 91 | ** the number of recursive invocations in the C stack; the higher |
92 | ** overflow. This count is very rough approximation; it considers only | 92 | ** 16 bits counts the number of non-yieldable calls in the stack. |
93 | ** recursive functions inside the interpreter, as non-recursive calls | 93 | ** (They are together so that we can change and save both with one |
94 | ** can be considered using a fixed (although unknown) amount of stack | 94 | ** instruction.) |
95 | ** space. | ||
96 | ** | ||
97 | ** The count has two parts: the lower part is the count itself; the | ||
98 | ** higher part counts the number of non-yieldable calls in the stack. | ||
99 | ** (They are together so that we can change both with one instruction.) | ||
100 | ** | ||
101 | ** Because calls to external C functions can use an unknown amount | ||
102 | ** of space (e.g., functions using an auxiliary buffer), calls | ||
103 | ** to these functions add more than one to the count (see CSTACKCF). | ||
104 | ** | ||
105 | ** The proper count excludes the number of CallInfo structures allocated | ||
106 | ** by Lua, as a kind of "potential" calls. So, when Lua calls a function | ||
107 | ** (and "consumes" one CallInfo), it needs neither to decrement nor to | ||
108 | ** check 'nCcalls', as its use of C stack is already accounted for. | ||
109 | */ | ||
110 | |||
111 | /* number of "C stack slots" used by an external C function */ | ||
112 | #define CSTACKCF 10 | ||
113 | |||
114 | |||
115 | /* | ||
116 | ** The C-stack size is sliced in the following zones: | ||
117 | ** - larger than CSTACKERR: normal stack; | ||
118 | ** - [CSTACKMARK, CSTACKERR]: buffer zone to signal a stack overflow; | ||
119 | ** - [CSTACKCF, CSTACKERRMARK]: error-handling zone; | ||
120 | ** - below CSTACKERRMARK: buffer zone to signal overflow during overflow; | ||
121 | ** (Because the counter can be decremented CSTACKCF at once, we need | ||
122 | ** the so called "buffer zones", with at least that size, to properly | ||
123 | ** detect a change from one zone to the next.) | ||
124 | */ | 95 | */ |
125 | #define CSTACKERR (8 * CSTACKCF) | ||
126 | #define CSTACKMARK (CSTACKERR - (CSTACKCF + 2)) | ||
127 | #define CSTACKERRMARK (CSTACKCF + 2) | ||
128 | |||
129 | |||
130 | /* initial limit for the C-stack of threads */ | ||
131 | #define CSTACKTHREAD (2 * CSTACKERR) | ||
132 | 96 | ||
133 | 97 | ||
134 | /* true if this thread does not have non-yieldable calls in the stack */ | 98 | /* true if this thread does not have non-yieldable calls in the stack */ |
@@ -144,13 +108,8 @@ | |||
144 | /* Decrement the number of non-yieldable calls */ | 108 | /* Decrement the number of non-yieldable calls */ |
145 | #define decnny(L) ((L)->nCcalls -= 0x10000) | 109 | #define decnny(L) ((L)->nCcalls -= 0x10000) |
146 | 110 | ||
147 | /* Increment the number of non-yieldable calls and decrement nCcalls */ | 111 | /* Non-yieldable call increment */ |
148 | #define incXCcalls(L) ((L)->nCcalls += 0x10000 - CSTACKCF) | 112 | #define nyci (0x10000 | 1) |
149 | |||
150 | /* Decrement the number of non-yieldable calls and increment nCcalls */ | ||
151 | #define decXCcalls(L) ((L)->nCcalls -= 0x10000 - CSTACKCF) | ||
152 | |||
153 | |||
154 | 113 | ||
155 | 114 | ||
156 | 115 | ||
@@ -168,12 +127,20 @@ struct lua_longjmp; /* defined in ldo.c */ | |||
168 | #endif | 127 | #endif |
169 | 128 | ||
170 | 129 | ||
171 | /* extra stack space to handle TM calls and some other extras */ | 130 | /* |
131 | ** Extra stack space to handle TM calls and some other extras. This | ||
132 | ** space is not included in 'stack_last'. It is used only to avoid stack | ||
133 | ** checks, either because the element will be promptly popped or because | ||
134 | ** there will be a stack check soon after the push. Function frames | ||
135 | ** never use this extra space, so it does not need to be kept clean. | ||
136 | */ | ||
172 | #define EXTRA_STACK 5 | 137 | #define EXTRA_STACK 5 |
173 | 138 | ||
174 | 139 | ||
175 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) | 140 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) |
176 | 141 | ||
142 | #define stacksize(th) cast_int((th)->stack_last - (th)->stack) | ||
143 | |||
177 | 144 | ||
178 | /* kinds of Garbage Collection */ | 145 | /* kinds of Garbage Collection */ |
179 | #define KGC_INC 0 /* incremental gc */ | 146 | #define KGC_INC 0 /* incremental gc */ |
@@ -224,14 +191,15 @@ typedef struct CallInfo { | |||
224 | */ | 191 | */ |
225 | #define CIST_OAH (1<<0) /* original value of 'allowhook' */ | 192 | #define CIST_OAH (1<<0) /* original value of 'allowhook' */ |
226 | #define CIST_C (1<<1) /* call is running a C function */ | 193 | #define CIST_C (1<<1) /* call is running a C function */ |
227 | #define CIST_HOOKED (1<<2) /* call is running a debug hook */ | 194 | #define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */ |
228 | #define CIST_YPCALL (1<<3) /* call is a yieldable protected call */ | 195 | #define CIST_HOOKED (1<<3) /* call is running a debug hook */ |
229 | #define CIST_TAIL (1<<4) /* call was tail called */ | 196 | #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ |
230 | #define CIST_HOOKYIELD (1<<5) /* last hook called yielded */ | 197 | #define CIST_TAIL (1<<5) /* call was tail called */ |
231 | #define CIST_FIN (1<<6) /* call is running a finalizer */ | 198 | #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ |
232 | #define CIST_TRAN (1<<7) /* 'ci' has transfer information */ | 199 | #define CIST_FIN (1<<7) /* call is running a finalizer */ |
200 | #define CIST_TRAN (1<<8) /* 'ci' has transfer information */ | ||
233 | #if defined(LUA_COMPAT_LT_LE) | 201 | #if defined(LUA_COMPAT_LT_LE) |
234 | #define CIST_LEQ (1<<8) /* using __lt for __le */ | 202 | #define CIST_LEQ (1<<9) /* using __lt for __le */ |
235 | #endif | 203 | #endif |
236 | 204 | ||
237 | /* active function is a Lua function */ | 205 | /* active function is a Lua function */ |
@@ -296,7 +264,6 @@ typedef struct global_State { | |||
296 | TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ | 264 | TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ |
297 | lua_WarnFunction warnf; /* warning function */ | 265 | lua_WarnFunction warnf; /* warning function */ |
298 | void *ud_warn; /* auxiliary data to 'warnf' */ | 266 | void *ud_warn; /* auxiliary data to 'warnf' */ |
299 | unsigned int Cstacklimit; /* current limit for the C stack */ | ||
300 | } global_State; | 267 | } global_State; |
301 | 268 | ||
302 | 269 | ||
@@ -311,7 +278,7 @@ struct lua_State { | |||
311 | StkId top; /* first free slot in the stack */ | 278 | StkId top; /* first free slot in the stack */ |
312 | global_State *l_G; | 279 | global_State *l_G; |
313 | CallInfo *ci; /* call info for current function */ | 280 | CallInfo *ci; /* call info for current function */ |
314 | StkId stack_last; /* last free slot in the stack */ | 281 | StkId stack_last; /* end of stack (last element + 1) */ |
315 | StkId stack; /* stack base */ | 282 | StkId stack; /* stack base */ |
316 | UpVal *openupval; /* list of open upvalues in this stack */ | 283 | UpVal *openupval; /* list of open upvalues in this stack */ |
317 | GCObject *gclist; | 284 | GCObject *gclist; |
@@ -320,9 +287,8 @@ struct lua_State { | |||
320 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ | 287 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ |
321 | volatile lua_Hook hook; | 288 | volatile lua_Hook hook; |
322 | ptrdiff_t errfunc; /* current error handling function (stack index) */ | 289 | ptrdiff_t errfunc; /* current error handling function (stack index) */ |
323 | l_uint32 nCcalls; /* number of allowed nested C calls - 'nci' */ | 290 | l_uint32 nCcalls; /* number of nested (non-yieldable | C) calls */ |
324 | int oldpc; /* last pc traced */ | 291 | int oldpc; /* last pc traced */ |
325 | int stacksize; | ||
326 | int basehookcount; | 292 | int basehookcount; |
327 | int hookcount; | 293 | int hookcount; |
328 | volatile l_signalT hookmask; | 294 | volatile l_signalT hookmask; |
@@ -334,6 +300,12 @@ struct lua_State { | |||
334 | 300 | ||
335 | /* | 301 | /* |
336 | ** Union of all collectable objects (only for conversions) | 302 | ** Union of all collectable objects (only for conversions) |
303 | ** ISO C99, 6.5.2.3 p.5: | ||
304 | ** "if a union contains several structures that share a common initial | ||
305 | ** sequence [...], and if the union object currently contains one | ||
306 | ** of these structures, it is permitted to inspect the common initial | ||
307 | ** part of any of them anywhere that a declaration of the complete type | ||
308 | ** of the union is visible." | ||
337 | */ | 309 | */ |
338 | union GCUnion { | 310 | union GCUnion { |
339 | GCObject gc; /* common header */ | 311 | GCObject gc; /* common header */ |
@@ -347,6 +319,11 @@ union GCUnion { | |||
347 | }; | 319 | }; |
348 | 320 | ||
349 | 321 | ||
322 | /* | ||
323 | ** ISO C99, 6.7.2.1 p.14: | ||
324 | ** "A pointer to a union object, suitably converted, points to each of | ||
325 | ** its members [...], and vice versa." | ||
326 | */ | ||
350 | #define cast_u(o) cast(union GCUnion *, (o)) | 327 | #define cast_u(o) cast(union GCUnion *, (o)) |
351 | 328 | ||
352 | /* macros to convert a GCObject into a specific value */ | 329 | /* macros to convert a GCObject into a specific value */ |
@@ -378,12 +355,11 @@ LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); | |||
378 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); | 355 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); |
379 | LUAI_FUNC void luaE_freeCI (lua_State *L); | 356 | LUAI_FUNC void luaE_freeCI (lua_State *L); |
380 | LUAI_FUNC void luaE_shrinkCI (lua_State *L); | 357 | LUAI_FUNC void luaE_shrinkCI (lua_State *L); |
381 | LUAI_FUNC void luaE_enterCcall (lua_State *L); | 358 | LUAI_FUNC void luaE_checkcstack (lua_State *L); |
359 | LUAI_FUNC void luaE_incCstack (lua_State *L); | ||
382 | LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); | 360 | LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); |
383 | LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); | 361 | LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); |
384 | 362 | ||
385 | 363 | ||
386 | #define luaE_exitCcall(L) ((L)->nCcalls++) | ||
387 | |||
388 | #endif | 364 | #endif |
389 | 365 | ||