diff options
Diffstat (limited to 'src/lua/lstate.h')
| -rw-r--r-- | src/lua/lstate.h | 364 |
1 files changed, 364 insertions, 0 deletions
diff --git a/src/lua/lstate.h b/src/lua/lstate.h new file mode 100644 index 0000000..2e8bd6c --- /dev/null +++ b/src/lua/lstate.h | |||
| @@ -0,0 +1,364 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: lstate.h $ | ||
| 3 | ** Global State | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | #ifndef lstate_h | ||
| 8 | #define lstate_h | ||
| 9 | |||
| 10 | #include "lua.h" | ||
| 11 | |||
| 12 | #include "lobject.h" | ||
| 13 | #include "ltm.h" | ||
| 14 | #include "lzio.h" | ||
| 15 | |||
| 16 | |||
| 17 | /* | ||
| 18 | ** Some notes about garbage-collected objects: All objects in Lua must | ||
| 19 | ** be kept somehow accessible until being freed, so all objects always | ||
| 20 | ** belong to one (and only one) of these lists, using field 'next' of | ||
| 21 | ** the 'CommonHeader' for the link: | ||
| 22 | ** | ||
| 23 | ** 'allgc': all objects not marked for finalization; | ||
| 24 | ** 'finobj': all objects marked for finalization; | ||
| 25 | ** 'tobefnz': all objects ready to be finalized; | ||
| 26 | ** 'fixedgc': all objects that are not to be collected (currently | ||
| 27 | ** only small strings, such as reserved words). | ||
| 28 | ** | ||
| 29 | ** For the generational collector, some of these lists have marks for | ||
| 30 | ** generations. Each mark points to the first element in the list for | ||
| 31 | ** that particular generation; that generation goes until the next mark. | ||
| 32 | ** | ||
| 33 | ** 'allgc' -> 'survival': new objects; | ||
| 34 | ** 'survival' -> 'old': objects that survived one collection; | ||
| 35 | ** 'old' -> 'reallyold': objects that became old in last collection; | ||
| 36 | ** 'reallyold' -> NULL: objects old for more than one cycle. | ||
| 37 | ** | ||
| 38 | ** 'finobj' -> 'finobjsur': new objects marked for finalization; | ||
| 39 | ** 'finobjsur' -> 'finobjold': survived """"; | ||
| 40 | ** 'finobjold' -> 'finobjrold': just old """"; | ||
| 41 | ** 'finobjrold' -> NULL: really old """". | ||
| 42 | */ | ||
| 43 | |||
| 44 | /* | ||
| 45 | ** Moreover, there is another set of lists that control gray objects. | ||
| 46 | ** These lists are linked by fields 'gclist'. (All objects that | ||
| 47 | ** can become gray have such a field. The field is not the same | ||
| 48 | ** in all objects, but it always has this name.) Any gray object | ||
| 49 | ** must belong to one of these lists, and all objects in these lists | ||
| 50 | ** must be gray: | ||
| 51 | ** | ||
| 52 | ** 'gray': regular gray objects, still waiting to be visited. | ||
| 53 | ** 'grayagain': objects that must be revisited at the atomic phase. | ||
| 54 | ** That includes | ||
| 55 | ** - black objects got in a write barrier; | ||
| 56 | ** - all kinds of weak tables during propagation phase; | ||
| 57 | ** - all threads. | ||
| 58 | ** 'weak': tables with weak values to be cleared; | ||
| 59 | ** 'ephemeron': ephemeron tables with white->white entries; | ||
| 60 | ** 'allweak': tables with weak keys and/or weak values to be cleared. | ||
| 61 | */ | ||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | /* | ||
| 66 | ** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of | ||
| 67 | ** how many "C calls" it still can do in the C stack, to avoid C-stack | ||
| 68 | ** overflow. This count is very rough approximation; it considers only | ||
| 69 | ** recursive functions inside the interpreter, as non-recursive calls | ||
| 70 | ** can be considered using a fixed (although unknown) amount of stack | ||
| 71 | ** space. | ||
| 72 | ** | ||
| 73 | ** The count has two parts: the lower part is the count itself; the | ||
| 74 | ** higher part counts the number of non-yieldable calls in the stack. | ||
| 75 | ** (They are together so that we can change both with one instruction.) | ||
| 76 | ** | ||
| 77 | ** Because calls to external C functions can use an unknown amount | ||
| 78 | ** of space (e.g., functions using an auxiliary buffer), calls | ||
| 79 | ** to these functions add more than one to the count (see CSTACKCF). | ||
| 80 | ** | ||
| 81 | ** The proper count excludes the number of CallInfo structures allocated | ||
| 82 | ** by Lua, as a kind of "potential" calls. So, when Lua calls a function | ||
| 83 | ** (and "consumes" one CallInfo), it needs neither to decrement nor to | ||
| 84 | ** check 'nCcalls', as its use of C stack is already accounted for. | ||
| 85 | */ | ||
| 86 | |||
| 87 | /* number of "C stack slots" used by an external C function */ | ||
| 88 | #define CSTACKCF 10 | ||
| 89 | |||
| 90 | |||
| 91 | /* | ||
| 92 | ** The C-stack size is sliced in the following zones: | ||
| 93 | ** - larger than CSTACKERR: normal stack; | ||
| 94 | ** - [CSTACKMARK, CSTACKERR]: buffer zone to signal a stack overflow; | ||
| 95 | ** - [CSTACKCF, CSTACKERRMARK]: error-handling zone; | ||
| 96 | ** - below CSTACKERRMARK: buffer zone to signal overflow during overflow; | ||
| 97 | ** (Because the counter can be decremented CSTACKCF at once, we need | ||
| 98 | ** the so called "buffer zones", with at least that size, to properly | ||
| 99 | ** detect a change from one zone to the next.) | ||
| 100 | */ | ||
| 101 | #define CSTACKERR (8 * CSTACKCF) | ||
| 102 | #define CSTACKMARK (CSTACKERR - (CSTACKCF + 2)) | ||
| 103 | #define CSTACKERRMARK (CSTACKCF + 2) | ||
| 104 | |||
| 105 | |||
| 106 | /* initial limit for the C-stack of threads */ | ||
| 107 | #define CSTACKTHREAD (2 * CSTACKERR) | ||
| 108 | |||
| 109 | |||
| 110 | /* true if this thread does not have non-yieldable calls in the stack */ | ||
| 111 | #define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0) | ||
| 112 | |||
| 113 | /* real number of C calls */ | ||
| 114 | #define getCcalls(L) ((L)->nCcalls & 0xffff) | ||
| 115 | |||
| 116 | |||
| 117 | /* Increment the number of non-yieldable calls */ | ||
| 118 | #define incnny(L) ((L)->nCcalls += 0x10000) | ||
| 119 | |||
| 120 | /* Decrement the number of non-yieldable calls */ | ||
| 121 | #define decnny(L) ((L)->nCcalls -= 0x10000) | ||
| 122 | |||
| 123 | /* Increment the number of non-yieldable calls and decrement nCcalls */ | ||
| 124 | #define incXCcalls(L) ((L)->nCcalls += 0x10000 - CSTACKCF) | ||
| 125 | |||
| 126 | /* Decrement the number of non-yieldable calls and increment nCcalls */ | ||
| 127 | #define decXCcalls(L) ((L)->nCcalls -= 0x10000 - CSTACKCF) | ||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | struct lua_longjmp; /* defined in ldo.c */ | ||
| 135 | |||
| 136 | |||
| 137 | /* | ||
| 138 | ** Atomic type (relative to signals) to better ensure that 'lua_sethook' | ||
| 139 | ** is thread safe | ||
| 140 | */ | ||
| 141 | #if !defined(l_signalT) | ||
| 142 | #include <signal.h> | ||
| 143 | #define l_signalT sig_atomic_t | ||
| 144 | #endif | ||
| 145 | |||
| 146 | |||
| 147 | /* extra stack space to handle TM calls and some other extras */ | ||
| 148 | #define EXTRA_STACK 5 | ||
| 149 | |||
| 150 | |||
| 151 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) | ||
| 152 | |||
| 153 | |||
| 154 | /* kinds of Garbage Collection */ | ||
| 155 | #define KGC_INC 0 /* incremental gc */ | ||
| 156 | #define KGC_GEN 1 /* generational gc */ | ||
| 157 | |||
| 158 | |||
| 159 | typedef struct stringtable { | ||
| 160 | TString **hash; | ||
| 161 | int nuse; /* number of elements */ | ||
| 162 | int size; | ||
| 163 | } stringtable; | ||
| 164 | |||
| 165 | |||
| 166 | /* | ||
| 167 | ** Information about a call. | ||
| 168 | */ | ||
| 169 | typedef struct CallInfo { | ||
| 170 | StkId func; /* function index in the stack */ | ||
| 171 | StkId top; /* top for this function */ | ||
| 172 | struct CallInfo *previous, *next; /* dynamic call link */ | ||
| 173 | union { | ||
| 174 | struct { /* only for Lua functions */ | ||
| 175 | const Instruction *savedpc; | ||
| 176 | volatile l_signalT trap; | ||
| 177 | int nextraargs; /* # of extra arguments in vararg functions */ | ||
| 178 | } l; | ||
| 179 | struct { /* only for C functions */ | ||
| 180 | lua_KFunction k; /* continuation in case of yields */ | ||
| 181 | ptrdiff_t old_errfunc; | ||
| 182 | lua_KContext ctx; /* context info. in case of yields */ | ||
| 183 | } c; | ||
| 184 | } u; | ||
| 185 | union { | ||
| 186 | int funcidx; /* called-function index */ | ||
| 187 | int nyield; /* number of values yielded */ | ||
| 188 | struct { /* info about transferred values (for call/return hooks) */ | ||
| 189 | unsigned short ftransfer; /* offset of first value transferred */ | ||
| 190 | unsigned short ntransfer; /* number of values transferred */ | ||
| 191 | } transferinfo; | ||
| 192 | } u2; | ||
| 193 | short nresults; /* expected number of results from this function */ | ||
| 194 | unsigned short callstatus; | ||
| 195 | } CallInfo; | ||
| 196 | |||
| 197 | |||
| 198 | /* | ||
| 199 | ** Bits in CallInfo status | ||
| 200 | */ | ||
| 201 | #define CIST_OAH (1<<0) /* original value of 'allowhook' */ | ||
| 202 | #define CIST_C (1<<1) /* call is running a C function */ | ||
| 203 | #define CIST_HOOKED (1<<2) /* call is running a debug hook */ | ||
| 204 | #define CIST_YPCALL (1<<3) /* call is a yieldable protected call */ | ||
| 205 | #define CIST_TAIL (1<<4) /* call was tail called */ | ||
| 206 | #define CIST_HOOKYIELD (1<<5) /* last hook called yielded */ | ||
| 207 | #define CIST_FIN (1<<6) /* call is running a finalizer */ | ||
| 208 | #define CIST_TRAN (1<<7) /* 'ci' has transfer information */ | ||
| 209 | #if defined(LUA_COMPAT_LT_LE) | ||
| 210 | #define CIST_LEQ (1<<8) /* using __lt for __le */ | ||
| 211 | #endif | ||
| 212 | |||
| 213 | /* active function is a Lua function */ | ||
| 214 | #define isLua(ci) (!((ci)->callstatus & CIST_C)) | ||
| 215 | |||
| 216 | /* call is running Lua code (not a hook) */ | ||
| 217 | #define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED))) | ||
| 218 | |||
| 219 | /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ | ||
| 220 | #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) | ||
| 221 | #define getoah(st) ((st) & CIST_OAH) | ||
| 222 | |||
| 223 | |||
| 224 | /* | ||
| 225 | ** 'global state', shared by all threads of this state | ||
| 226 | */ | ||
| 227 | typedef struct global_State { | ||
| 228 | lua_Alloc frealloc; /* function to reallocate memory */ | ||
| 229 | void *ud; /* auxiliary data to 'frealloc' */ | ||
| 230 | l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ | ||
| 231 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ | ||
| 232 | lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ | ||
| 233 | lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */ | ||
| 234 | stringtable strt; /* hash table for strings */ | ||
| 235 | TValue l_registry; | ||
| 236 | TValue nilvalue; /* a nil value */ | ||
| 237 | unsigned int seed; /* randomized seed for hashes */ | ||
| 238 | lu_byte currentwhite; | ||
| 239 | lu_byte gcstate; /* state of garbage collector */ | ||
| 240 | lu_byte gckind; /* kind of GC running */ | ||
| 241 | lu_byte genminormul; /* control for minor generational collections */ | ||
| 242 | lu_byte genmajormul; /* control for major generational collections */ | ||
| 243 | lu_byte gcrunning; /* true if GC is running */ | ||
| 244 | lu_byte gcemergency; /* true if this is an emergency collection */ | ||
| 245 | lu_byte gcpause; /* size of pause between successive GCs */ | ||
| 246 | lu_byte gcstepmul; /* GC "speed" */ | ||
| 247 | lu_byte gcstepsize; /* (log2 of) GC granularity */ | ||
| 248 | GCObject *allgc; /* list of all collectable objects */ | ||
| 249 | GCObject **sweepgc; /* current position of sweep in list */ | ||
| 250 | GCObject *finobj; /* list of collectable objects with finalizers */ | ||
| 251 | GCObject *gray; /* list of gray objects */ | ||
| 252 | GCObject *grayagain; /* list of objects to be traversed atomically */ | ||
| 253 | GCObject *weak; /* list of tables with weak values */ | ||
| 254 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ | ||
| 255 | GCObject *allweak; /* list of all-weak tables */ | ||
| 256 | GCObject *tobefnz; /* list of userdata to be GC */ | ||
| 257 | GCObject *fixedgc; /* list of objects not to be collected */ | ||
| 258 | /* fields for generational collector */ | ||
| 259 | GCObject *survival; /* start of objects that survived one GC cycle */ | ||
| 260 | GCObject *old; /* start of old objects */ | ||
| 261 | GCObject *reallyold; /* old objects with more than one cycle */ | ||
| 262 | GCObject *finobjsur; /* list of survival objects with finalizers */ | ||
| 263 | GCObject *finobjold; /* list of old objects with finalizers */ | ||
| 264 | GCObject *finobjrold; /* list of really old objects with finalizers */ | ||
| 265 | struct lua_State *twups; /* list of threads with open upvalues */ | ||
| 266 | lua_CFunction panic; /* to be called in unprotected errors */ | ||
| 267 | struct lua_State *mainthread; | ||
| 268 | TString *memerrmsg; /* message for memory-allocation errors */ | ||
| 269 | TString *tmname[TM_N]; /* array with tag-method names */ | ||
| 270 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ | ||
| 271 | TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ | ||
| 272 | lua_WarnFunction warnf; /* warning function */ | ||
| 273 | void *ud_warn; /* auxiliary data to 'warnf' */ | ||
| 274 | unsigned int Cstacklimit; /* current limit for the C stack */ | ||
| 275 | } global_State; | ||
| 276 | |||
| 277 | |||
| 278 | /* | ||
| 279 | ** 'per thread' state | ||
| 280 | */ | ||
| 281 | struct lua_State { | ||
| 282 | CommonHeader; | ||
| 283 | lu_byte status; | ||
| 284 | lu_byte allowhook; | ||
| 285 | unsigned short nci; /* number of items in 'ci' list */ | ||
| 286 | StkId top; /* first free slot in the stack */ | ||
| 287 | global_State *l_G; | ||
| 288 | CallInfo *ci; /* call info for current function */ | ||
| 289 | const Instruction *oldpc; /* last pc traced */ | ||
| 290 | StkId stack_last; /* last free slot in the stack */ | ||
| 291 | StkId stack; /* stack base */ | ||
| 292 | UpVal *openupval; /* list of open upvalues in this stack */ | ||
| 293 | GCObject *gclist; | ||
| 294 | struct lua_State *twups; /* list of threads with open upvalues */ | ||
| 295 | struct lua_longjmp *errorJmp; /* current error recover point */ | ||
| 296 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ | ||
| 297 | volatile lua_Hook hook; | ||
| 298 | ptrdiff_t errfunc; /* current error handling function (stack index) */ | ||
| 299 | l_uint32 nCcalls; /* number of allowed nested C calls - 'nci' */ | ||
| 300 | int stacksize; | ||
| 301 | int basehookcount; | ||
| 302 | int hookcount; | ||
| 303 | volatile l_signalT hookmask; | ||
| 304 | }; | ||
| 305 | |||
| 306 | |||
| 307 | #define G(L) (L->l_G) | ||
| 308 | |||
| 309 | |||
| 310 | /* | ||
| 311 | ** Union of all collectable objects (only for conversions) | ||
| 312 | */ | ||
| 313 | union GCUnion { | ||
| 314 | GCObject gc; /* common header */ | ||
| 315 | struct TString ts; | ||
| 316 | struct Udata u; | ||
| 317 | union Closure cl; | ||
| 318 | struct Table h; | ||
| 319 | struct Proto p; | ||
| 320 | struct lua_State th; /* thread */ | ||
| 321 | struct UpVal upv; | ||
| 322 | }; | ||
| 323 | |||
| 324 | |||
| 325 | #define cast_u(o) cast(union GCUnion *, (o)) | ||
| 326 | |||
| 327 | /* macros to convert a GCObject into a specific value */ | ||
| 328 | #define gco2ts(o) \ | ||
| 329 | check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) | ||
| 330 | #define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u)) | ||
| 331 | #define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l)) | ||
| 332 | #define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c)) | ||
| 333 | #define gco2cl(o) \ | ||
| 334 | check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) | ||
| 335 | #define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h)) | ||
| 336 | #define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p)) | ||
| 337 | #define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th)) | ||
| 338 | #define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv)) | ||
| 339 | |||
| 340 | |||
| 341 | /* | ||
| 342 | ** macro to convert a Lua object into a GCObject | ||
| 343 | ** (The access to 'tt' tries to ensure that 'v' is actually a Lua object.) | ||
| 344 | */ | ||
| 345 | #define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc)) | ||
| 346 | |||
| 347 | |||
| 348 | /* actual number of total bytes allocated */ | ||
| 349 | #define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt) | ||
| 350 | |||
| 351 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); | ||
| 352 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); | ||
| 353 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); | ||
| 354 | LUAI_FUNC void luaE_freeCI (lua_State *L); | ||
| 355 | LUAI_FUNC void luaE_shrinkCI (lua_State *L); | ||
| 356 | LUAI_FUNC void luaE_enterCcall (lua_State *L); | ||
| 357 | LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); | ||
| 358 | LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); | ||
| 359 | |||
| 360 | |||
| 361 | #define luaE_exitCcall(L) ((L)->nCcalls++) | ||
| 362 | |||
| 363 | #endif | ||
| 364 | |||
