00001
00002
00003
00004
00005
00006
00007
00008 #ifndef lobject_h
00009 #define lobject_h
00010
00011
00012 #include <stdarg.h>
00013
00014
00015 #include "llimits.h"
00016 #include "lua.h"
00017
00018
00019
00020
00021
00022 #define LUA_TPROTO LUA_NUMTAGS
00023 #define LUA_TUPVAL (LUA_NUMTAGS+1)
00024 #define LUA_TDEADKEY (LUA_NUMTAGS+2)
00025
00026
00027
00028
00029 #define LUA_TOTALTAGS (LUA_TUPVAL+2)
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #define LUA_TLCL (LUA_TFUNCTION | (0 << 4))
00048 #define LUA_TLCF (LUA_TFUNCTION | (1 << 4))
00049 #define LUA_TCCL (LUA_TFUNCTION | (2 << 4))
00050
00051
00052
00053 #define BIT_ISCOLLECTABLE (1 << 6)
00054
00055
00056 #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
00057
00058
00059
00060
00061
00062 typedef union GCObject GCObject;
00063
00064
00065
00066
00067
00068
00069 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
00070
00071
00072
00073
00074
00075 typedef struct GCheader {
00076 CommonHeader;
00077 } GCheader;
00078
00079
00080
00081
00082
00083
00084 typedef union Value Value;
00085
00086
00087 #define numfield lua_Number n;
00088
00089
00090
00091
00092
00093
00094
00095
00096 #define TValuefields Value value_; int tt_
00097
00098 typedef struct lua_TValue TValue;
00099
00100
00101
00102 #define NILCONSTANT {NULL}, LUA_TNIL
00103
00104
00105 #define val_(o) ((o)->value_)
00106 #define num_(o) (val_(o).n)
00107
00108
00109
00110 #define rttype(o) ((o)->tt_)
00111
00112
00113 #define ttype(o) (rttype(o) & 0x3F)
00114
00115
00116
00117 #define ttypenv(o) (rttype(o) & 0x0F)
00118
00119
00120
00121 #define checktag(o,t) (rttype(o) == (t))
00122 #define ttisnumber(o) checktag((o), LUA_TNUMBER)
00123 #define ttisnil(o) checktag((o), LUA_TNIL)
00124 #define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
00125 #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
00126 #define ttisstring(o) checktag((o), ctb(LUA_TSTRING))
00127 #define ttistable(o) checktag((o), ctb(LUA_TTABLE))
00128 #define ttisfunction(o) (ttypenv(o) == LUA_TFUNCTION)
00129 #define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
00130 #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
00131 #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
00132 #define ttislcf(o) checktag((o), LUA_TLCF)
00133 #define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA))
00134 #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
00135 #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)
00136
00137 #define ttisequal(o1,o2) (rttype(o1) == rttype(o2))
00138
00139
00140 #define nvalue(o) check_exp(ttisnumber(o), num_(o))
00141 #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
00142 #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
00143 #define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts)
00144 #define tsvalue(o) (&rawtsvalue(o)->tsv)
00145 #define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u)
00146 #define uvalue(o) (&rawuvalue(o)->uv)
00147 #define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl)
00148 #define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
00149 #define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
00150 #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
00151 #define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h)
00152 #define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
00153 #define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th)
00154
00155 #define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
00156
00157 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
00158
00159
00160 #define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
00161
00162
00163
00164 #define righttt(obj) (ttypenv(obj) == gcvalue(obj)->gch.tt)
00165
00166 #define checkliveness(g,obj) \
00167 lua_longassert(!iscollectable(obj) || \
00168 (righttt(obj) && !isdead(g,gcvalue(obj))))
00169
00170
00171
00172 #define settt_(o,t) ((o)->tt_=(t))
00173
00174 #define setnvalue(obj,x) \
00175 { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
00176
00177 #define changenvalue(o,x) check_exp(ttisnumber(o), num_(o)=(x))
00178
00179 #define setnilvalue(obj) settt_(obj, LUA_TNIL)
00180
00181 #define setfvalue(obj,x) \
00182 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
00183
00184 #define setpvalue(obj,x) \
00185 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
00186
00187 #define setbvalue(obj,x) \
00188 { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
00189
00190 #define setgcovalue(L,obj,x) \
00191 { TValue *io=(obj); GCObject *i_g=(x); \
00192 val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
00193
00194 #define setsvalue(L,obj,x) \
00195 { TValue *io=(obj); \
00196 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TSTRING)); \
00197 checkliveness(G(L),io); }
00198
00199 #define setuvalue(L,obj,x) \
00200 { TValue *io=(obj); \
00201 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
00202 checkliveness(G(L),io); }
00203
00204 #define setthvalue(L,obj,x) \
00205 { TValue *io=(obj); \
00206 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
00207 checkliveness(G(L),io); }
00208
00209 #define setclLvalue(L,obj,x) \
00210 { TValue *io=(obj); \
00211 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
00212 checkliveness(G(L),io); }
00213
00214 #define setclCvalue(L,obj,x) \
00215 { TValue *io=(obj); \
00216 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
00217 checkliveness(G(L),io); }
00218
00219 #define sethvalue(L,obj,x) \
00220 { TValue *io=(obj); \
00221 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
00222 checkliveness(G(L),io); }
00223
00224 #define setptvalue(L,obj,x) \
00225 { TValue *io=(obj); \
00226 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TPROTO)); \
00227 checkliveness(G(L),io); }
00228
00229 #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
00230
00231
00232
00233 #define setobj(L,obj1,obj2) \
00234 { const TValue *io2=(obj2); TValue *io1=(obj1); \
00235 io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
00236 checkliveness(G(L),io1); }
00237
00238
00239
00240
00241
00242
00243
00244 #define setobjs2s setobj
00245
00246 #define setobj2s setobj
00247 #define setsvalue2s setsvalue
00248 #define sethvalue2s sethvalue
00249 #define setptvalue2s setptvalue
00250
00251 #define setobjt2t setobj
00252
00253 #define setobj2t setobj
00254
00255 #define setobj2n setobj
00256 #define setsvalue2n setsvalue
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 #if defined(LUA_NANTRICK) \
00268 || defined(LUA_NANTRICK_LE) \
00269 || defined(LUA_NANTRICK_BE)
00270
00271
00272
00273
00274
00275
00276
00277 #if !defined(NNMARK)
00278 #define NNMARK 0x7FF7A500
00279 #define NNMASK 0x7FFFFF00
00280 #endif
00281
00282 #undef TValuefields
00283 #undef NILCONSTANT
00284
00285 #if defined(LUA_NANTRICK_LE)
00286
00287
00288 #define TValuefields \
00289 union { struct { Value v__; int tt__; } i; double d__; } u
00290 #define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}}
00291
00292 #define v_(o) ((o)->u.i.v__)
00293 #define d_(o) ((o)->u.d__)
00294 #define tt_(o) ((o)->u.i.tt__)
00295
00296 #elif defined(LUA_NANTRICK_BE)
00297
00298
00299 #define TValuefields \
00300 union { struct { int tt__; Value v__; } i; double d__; } u
00301 #define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}}
00302
00303 #define v_(o) ((o)->u.i.v__)
00304 #define d_(o) ((o)->u.d__)
00305 #define tt_(o) ((o)->u.i.tt__)
00306
00307 #elif !defined(TValuefields)
00308 #error option 'LUA_NANTRICK' needs declaration for 'TValuefields'
00309
00310 #endif
00311
00312
00313
00314 #undef val_
00315 #define val_(o) v_(o)
00316 #undef num_
00317 #define num_(o) d_(o)
00318
00319
00320 #undef numfield
00321 #define numfield
00322
00323
00324 #undef ttisnumber
00325 #define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK)
00326
00327 #define tag2tt(t) (NNMARK | (t))
00328
00329 #undef rttype
00330 #define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff)
00331
00332 #undef settt_
00333 #define settt_(o,t) (tt_(o) = tag2tt(t))
00334
00335 #undef setnvalue
00336 #define setnvalue(obj,x) \
00337 { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }
00338
00339 #undef setobj
00340 #define setobj(L,obj1,obj2) \
00341 { const TValue *o2_=(obj2); TValue *o1_=(obj1); \
00342 o1_->u = o2_->u; \
00343 checkliveness(G(L),o1_); }
00344
00345
00346
00347
00348
00349
00350 #undef checktag
00351 #define checktag(o,t) (tt_(o) == tag2tt(t))
00352
00353 #undef ttisequal
00354 #define ttisequal(o1,o2) \
00355 (ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))
00356
00357
00358
00359 #define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; }
00360
00361
00362 #else
00363
00364 #define luai_checknum(L,o,c) { }
00365
00366 #endif
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 union Value {
00379 GCObject *gc;
00380 void *p;
00381 int b;
00382 lua_CFunction f;
00383 numfield
00384 };
00385
00386
00387 struct lua_TValue {
00388 TValuefields;
00389 };
00390
00391
00392 typedef TValue *StkId;
00393
00394
00395
00396
00397
00398
00399
00400 typedef union TString {
00401 L_Umaxalign dummy;
00402 struct {
00403 CommonHeader;
00404 lu_byte reserved;
00405 unsigned int hash;
00406 size_t len;
00407 } tsv;
00408 } TString;
00409
00410
00411
00412 #define getstr(ts) cast(const char *, (ts) + 1)
00413
00414
00415 #define svalue(o) getstr(rawtsvalue(o))
00416
00417
00418
00419
00420
00421 typedef union Udata {
00422 L_Umaxalign dummy;
00423 struct {
00424 CommonHeader;
00425 struct Table *metatable;
00426 struct Table *env;
00427 size_t len;
00428 } uv;
00429 } Udata;
00430
00431
00432
00433
00434
00435
00436 typedef struct Upvaldesc {
00437 TString *name;
00438 lu_byte instack;
00439 lu_byte idx;
00440 } Upvaldesc;
00441
00442
00443
00444
00445
00446
00447 typedef struct LocVar {
00448 TString *varname;
00449 int startpc;
00450 int endpc;
00451 } LocVar;
00452
00453
00454
00455
00456
00457 typedef struct Proto {
00458 CommonHeader;
00459 TValue *k;
00460 Instruction *code;
00461 struct Proto **p;
00462 int *lineinfo;
00463 LocVar *locvars;
00464 Upvaldesc *upvalues;
00465 union Closure *cache;
00466 TString *source;
00467 int sizeupvalues;
00468 int sizek;
00469 int sizecode;
00470 int sizelineinfo;
00471 int sizep;
00472 int sizelocvars;
00473 int linedefined;
00474 int lastlinedefined;
00475 GCObject *gclist;
00476 lu_byte numparams;
00477 lu_byte is_vararg;
00478 lu_byte maxstacksize;
00479 } Proto;
00480
00481
00482
00483
00484
00485
00486 typedef struct UpVal {
00487 CommonHeader;
00488 TValue *v;
00489 union {
00490 TValue value;
00491 struct {
00492 struct UpVal *prev;
00493 struct UpVal *next;
00494 } l;
00495 } u;
00496 } UpVal;
00497
00498
00499
00500
00501
00502
00503 #define ClosureHeader \
00504 CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist
00505
00506 typedef struct CClosure {
00507 ClosureHeader;
00508 lua_CFunction f;
00509 TValue upvalue[1];
00510 } CClosure;
00511
00512
00513 typedef struct LClosure {
00514 ClosureHeader;
00515 struct Proto *p;
00516 UpVal *upvals[1];
00517 } LClosure;
00518
00519
00520 typedef union Closure {
00521 CClosure c;
00522 LClosure l;
00523 } Closure;
00524
00525
00526 #define isLfunction(o) ttisLclosure(o)
00527
00528 #define getproto(o) (clLvalue(o)->p)
00529
00530
00531
00532
00533
00534
00535 typedef union TKey {
00536 struct {
00537 TValuefields;
00538 struct Node *next;
00539 } nk;
00540 TValue tvk;
00541 } TKey;
00542
00543
00544 typedef struct Node {
00545 TValue i_val;
00546 TKey i_key;
00547 } Node;
00548
00549
00550 typedef struct Table {
00551 CommonHeader;
00552 lu_byte flags;
00553 lu_byte lsizenode;
00554 struct Table *metatable;
00555 TValue *array;
00556 Node *node;
00557 Node *lastfree;
00558 GCObject *gclist;
00559 int sizearray;
00560 } Table;
00561
00562
00563
00564
00565
00566
00567 #define lmod(s,size) \
00568 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
00569
00570
00571 #define twoto(x) (1<<(x))
00572 #define sizenode(t) (twoto((t)->lsizenode))
00573
00574
00575
00576
00577
00578 #define luaO_nilobject (&luaO_nilobject_)
00579
00580
00581 LUAI_DDEC const TValue luaO_nilobject_;
00582
00583
00584 LUAI_FUNC int luaO_int2fb (unsigned int x);
00585 LUAI_FUNC int luaO_fb2int (int x);
00586 LUAI_FUNC int luaO_ceillog2 (unsigned int x);
00587 LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
00588 LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
00589 LUAI_FUNC int luaO_hexavalue (int c);
00590 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
00591 va_list argp);
00592 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
00593 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
00594
00595
00596 #endif
00597