File indexing completed on 2026-11-07 10:37:56
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef lobject_h
0009 #define lobject_h
0010
0011
0012 #include <stdarg.h>
0013
0014
0015 #include "llimits.h"
0016 #include "lua.h"
0017
0018
0019
0020
0021
0022 #define LUA_TUPVAL LUA_NUMTYPES
0023 #define LUA_TPROTO (LUA_NUMTYPES+1)
0024 #define LUA_TDEADKEY (LUA_NUMTYPES+2)
0025
0026
0027
0028
0029
0030
0031 #define LUA_TOTALTYPES (LUA_TPROTO + 2)
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 #define makevariant(t,v) ((t) | ((v) << 4))
0043
0044
0045
0046
0047
0048
0049 typedef union Value {
0050 struct GCObject *gc;
0051 void *p;
0052 lua_CFunction f;
0053 lua_Integer i;
0054 lua_Number n;
0055
0056 lu_byte ub;
0057 } Value;
0058
0059
0060
0061
0062
0063
0064
0065 #define TValuefields Value value_; lu_byte tt_
0066
0067 typedef struct TValue {
0068 TValuefields;
0069 } TValue;
0070
0071
0072 #define val_(o) ((o)->value_)
0073 #define valraw(o) (val_(o))
0074
0075
0076
0077 #define rawtt(o) ((o)->tt_)
0078
0079
0080 #define novariant(t) ((t) & 0x0F)
0081
0082
0083 #define withvariant(t) ((t) & 0x3F)
0084 #define ttypetag(o) withvariant(rawtt(o))
0085
0086
0087 #define ttype(o) (novariant(rawtt(o)))
0088
0089
0090
0091 #define checktag(o,t) (rawtt(o) == (t))
0092 #define checktype(o,t) (ttype(o) == (t))
0093
0094
0095
0096
0097
0098 #define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt)
0099
0100
0101
0102
0103
0104
0105
0106 #define checkliveness(L,obj) \
0107 ((void)L, lua_longassert(!iscollectable(obj) || \
0108 (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))))
0109
0110
0111
0112
0113
0114 #define settt_(o,t) ((o)->tt_=(t))
0115
0116
0117
0118 #define setobj(L,obj1,obj2) \
0119 { TValue *io1=(obj1); const TValue *io2=(obj2); \
0120 io1->value_ = io2->value_; settt_(io1, io2->tt_); \
0121 checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); }
0122
0123
0124
0125
0126
0127
0128
0129 #define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2))
0130
0131 #define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2)
0132
0133 #define setobjt2t setobj
0134
0135 #define setobj2n setobj
0136
0137 #define setobj2t setobj
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148 typedef union StackValue {
0149 TValue val;
0150 struct {
0151 TValuefields;
0152 unsigned short delta;
0153 } tbclist;
0154 } StackValue;
0155
0156
0157
0158 typedef StackValue *StkId;
0159
0160
0161
0162
0163
0164
0165 typedef union {
0166 StkId p;
0167 ptrdiff_t offset;
0168 } StkIdRel;
0169
0170
0171
0172 #define s2v(o) (&(o)->val)
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183 #define LUA_VNIL makevariant(LUA_TNIL, 0)
0184
0185
0186 #define LUA_VEMPTY makevariant(LUA_TNIL, 1)
0187
0188
0189 #define LUA_VABSTKEY makevariant(LUA_TNIL, 2)
0190
0191
0192
0193 #define ttisnil(v) checktype((v), LUA_TNIL)
0194
0195
0196
0197 #define ttisstrictnil(o) checktag((o), LUA_VNIL)
0198
0199
0200 #define setnilvalue(obj) settt_(obj, LUA_VNIL)
0201
0202
0203 #define isabstkey(v) checktag((v), LUA_VABSTKEY)
0204
0205
0206
0207
0208
0209 #define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v))
0210
0211
0212
0213
0214
0215
0216
0217 #define isempty(v) ttisnil(v)
0218
0219
0220
0221 #define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY
0222
0223
0224
0225 #define setempty(v) settt_(v, LUA_VEMPTY)
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239 #define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0)
0240 #define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1)
0241
0242 #define ttisboolean(o) checktype((o), LUA_TBOOLEAN)
0243 #define ttisfalse(o) checktag((o), LUA_VFALSE)
0244 #define ttistrue(o) checktag((o), LUA_VTRUE)
0245
0246
0247 #define l_isfalse(o) (ttisfalse(o) || ttisnil(o))
0248
0249
0250 #define setbfvalue(obj) settt_(obj, LUA_VFALSE)
0251 #define setbtvalue(obj) settt_(obj, LUA_VTRUE)
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262 #define LUA_VTHREAD makevariant(LUA_TTHREAD, 0)
0263
0264 #define ttisthread(o) checktag((o), ctb(LUA_VTHREAD))
0265
0266 #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
0267
0268 #define setthvalue(L,obj,x) \
0269 { TValue *io = (obj); lua_State *x_ = (x); \
0270 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \
0271 checkliveness(L,io); }
0272
0273 #define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t)
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288 #define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked
0289
0290
0291
0292 typedef struct GCObject {
0293 CommonHeader;
0294 } GCObject;
0295
0296
0297
0298 #define BIT_ISCOLLECTABLE (1 << 6)
0299
0300 #define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE)
0301
0302
0303 #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
0304
0305 #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
0306
0307 #define gcvalueraw(v) ((v).gc)
0308
0309 #define setgcovalue(L,obj,x) \
0310 { TValue *io = (obj); GCObject *i_g=(x); \
0311 val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323 #define LUA_VNUMINT makevariant(LUA_TNUMBER, 0)
0324 #define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1)
0325
0326 #define ttisnumber(o) checktype((o), LUA_TNUMBER)
0327 #define ttisfloat(o) checktag((o), LUA_VNUMFLT)
0328 #define ttisinteger(o) checktag((o), LUA_VNUMINT)
0329
0330 #define nvalue(o) check_exp(ttisnumber(o), \
0331 (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
0332 #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
0333 #define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
0334
0335 #define fltvalueraw(v) ((v).n)
0336 #define ivalueraw(v) ((v).i)
0337
0338 #define setfltvalue(obj,x) \
0339 { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); }
0340
0341 #define chgfltvalue(obj,x) \
0342 { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
0343
0344 #define setivalue(obj,x) \
0345 { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); }
0346
0347 #define chgivalue(obj,x) \
0348 { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360 #define LUA_VSHRSTR makevariant(LUA_TSTRING, 0)
0361 #define LUA_VLNGSTR makevariant(LUA_TSTRING, 1)
0362
0363 #define ttisstring(o) checktype((o), LUA_TSTRING)
0364 #define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR))
0365 #define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR))
0366
0367 #define tsvalueraw(v) (gco2ts((v).gc))
0368
0369 #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
0370
0371 #define setsvalue(L,obj,x) \
0372 { TValue *io = (obj); TString *x_ = (x); \
0373 val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
0374 checkliveness(L,io); }
0375
0376
0377 #define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s)
0378
0379
0380 #define setsvalue2n setsvalue
0381
0382
0383
0384
0385
0386 typedef struct TString {
0387 CommonHeader;
0388 lu_byte extra;
0389 lu_byte shrlen;
0390 unsigned int hash;
0391 union {
0392 size_t lnglen;
0393 struct TString *hnext;
0394 } u;
0395 char contents[1];
0396 } TString;
0397
0398
0399
0400
0401
0402
0403
0404 #define getstr(ts) ((ts)->contents)
0405 #define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents)
0406 #define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents)
0407
0408
0409
0410 #define tsslen(s) \
0411 ((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen)
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427 #define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0)
0428
0429 #define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0)
0430
0431 #define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA)
0432 #define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA))
0433
0434 #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
0435 #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
0436
0437 #define pvalueraw(v) ((v).p)
0438
0439 #define setpvalue(obj,x) \
0440 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); }
0441
0442 #define setuvalue(L,obj,x) \
0443 { TValue *io = (obj); Udata *x_ = (x); \
0444 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \
0445 checkliveness(L,io); }
0446
0447
0448
0449 typedef union UValue {
0450 TValue uv;
0451 LUAI_MAXALIGN;
0452 } UValue;
0453
0454
0455
0456
0457
0458
0459 typedef struct Udata {
0460 CommonHeader;
0461 unsigned short nuvalue;
0462 size_t len;
0463 struct Table *metatable;
0464 GCObject *gclist;
0465 UValue uv[1];
0466 } Udata;
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478 typedef struct Udata0 {
0479 CommonHeader;
0480 unsigned short nuvalue;
0481 size_t len;
0482 struct Table *metatable;
0483 union {LUAI_MAXALIGN;} bindata;
0484 } Udata0;
0485
0486
0487
0488 #define udatamemoffset(nuv) \
0489 ((nuv) == 0 ? offsetof(Udata0, bindata) \
0490 : offsetof(Udata, uv) + (sizeof(UValue) * (nuv)))
0491
0492
0493 #define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue))
0494
0495
0496 #define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb))
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507 #define LUA_VPROTO makevariant(LUA_TPROTO, 0)
0508
0509
0510
0511
0512
0513 typedef struct Upvaldesc {
0514 TString *name;
0515 lu_byte instack;
0516 lu_byte idx;
0517 lu_byte kind;
0518 } Upvaldesc;
0519
0520
0521
0522
0523
0524
0525 typedef struct LocVar {
0526 TString *varname;
0527 int startpc;
0528 int endpc;
0529 } LocVar;
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542 typedef struct AbsLineInfo {
0543 int pc;
0544 int line;
0545 } AbsLineInfo;
0546
0547
0548
0549
0550 typedef struct Proto {
0551 CommonHeader;
0552 lu_byte numparams;
0553 lu_byte is_vararg;
0554 lu_byte maxstacksize;
0555 int sizeupvalues;
0556 int sizek;
0557 int sizecode;
0558 int sizelineinfo;
0559 int sizep;
0560 int sizelocvars;
0561 int sizeabslineinfo;
0562 int linedefined;
0563 int lastlinedefined;
0564 TValue *k;
0565 Instruction *code;
0566 struct Proto **p;
0567 Upvaldesc *upvalues;
0568 ls_byte *lineinfo;
0569 AbsLineInfo *abslineinfo;
0570 LocVar *locvars;
0571 TString *source;
0572 GCObject *gclist;
0573 } Proto;
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583
0584 #define LUA_VUPVAL makevariant(LUA_TUPVAL, 0)
0585
0586
0587
0588 #define LUA_VLCL makevariant(LUA_TFUNCTION, 0)
0589 #define LUA_VLCF makevariant(LUA_TFUNCTION, 1)
0590 #define LUA_VCCL makevariant(LUA_TFUNCTION, 2)
0591
0592 #define ttisfunction(o) checktype(o, LUA_TFUNCTION)
0593 #define ttisLclosure(o) checktag((o), ctb(LUA_VLCL))
0594 #define ttislcf(o) checktag((o), LUA_VLCF)
0595 #define ttisCclosure(o) checktag((o), ctb(LUA_VCCL))
0596 #define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o))
0597
0598
0599 #define isLfunction(o) ttisLclosure(o)
0600
0601 #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
0602 #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
0603 #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
0604 #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
0605
0606 #define fvalueraw(v) ((v).f)
0607
0608 #define setclLvalue(L,obj,x) \
0609 { TValue *io = (obj); LClosure *x_ = (x); \
0610 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \
0611 checkliveness(L,io); }
0612
0613 #define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl)
0614
0615 #define setfvalue(obj,x) \
0616 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); }
0617
0618 #define setclCvalue(L,obj,x) \
0619 { TValue *io = (obj); CClosure *x_ = (x); \
0620 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \
0621 checkliveness(L,io); }
0622
0623
0624
0625
0626
0627 typedef struct UpVal {
0628 CommonHeader;
0629 union {
0630 TValue *p;
0631 ptrdiff_t offset;
0632 } v;
0633 union {
0634 struct {
0635 struct UpVal *next;
0636 struct UpVal **previous;
0637 } open;
0638 TValue value;
0639 } u;
0640 } UpVal;
0641
0642
0643
0644 #define ClosureHeader \
0645 CommonHeader; lu_byte nupvalues; GCObject *gclist
0646
0647 typedef struct CClosure {
0648 ClosureHeader;
0649 lua_CFunction f;
0650 TValue upvalue[1];
0651 } CClosure;
0652
0653
0654 typedef struct LClosure {
0655 ClosureHeader;
0656 struct Proto *p;
0657 UpVal *upvals[1];
0658 } LClosure;
0659
0660
0661 typedef union Closure {
0662 CClosure c;
0663 LClosure l;
0664 } Closure;
0665
0666
0667 #define getproto(o) (clLvalue(o)->p)
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678 #define LUA_VTABLE makevariant(LUA_TTABLE, 0)
0679
0680 #define ttistable(o) checktag((o), ctb(LUA_VTABLE))
0681
0682 #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
0683
0684 #define sethvalue(L,obj,x) \
0685 { TValue *io = (obj); Table *x_ = (x); \
0686 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \
0687 checkliveness(L,io); }
0688
0689 #define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h)
0690
0691
0692
0693
0694
0695
0696
0697
0698
0699 typedef union Node {
0700 struct NodeKey {
0701 TValuefields;
0702 lu_byte key_tt;
0703 int next;
0704 Value key_val;
0705 } u;
0706 TValue i_val;
0707 } Node;
0708
0709
0710
0711 #define setnodekey(L,node,obj) \
0712 { Node *n_=(node); const TValue *io_=(obj); \
0713 n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \
0714 checkliveness(L,io_); }
0715
0716
0717
0718 #define getnodekey(L,obj,node) \
0719 { TValue *io_=(obj); const Node *n_=(node); \
0720 io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \
0721 checkliveness(L,io_); }
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731 #define BITRAS (1 << 7)
0732 #define isrealasize(t) (!((t)->flags & BITRAS))
0733 #define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS))
0734 #define setnorealasize(t) ((t)->flags |= BITRAS)
0735
0736
0737 typedef struct Table {
0738 CommonHeader;
0739 lu_byte flags;
0740 lu_byte lsizenode;
0741 unsigned int alimit;
0742 TValue *array;
0743 Node *node;
0744 Node *lastfree;
0745 struct Table *metatable;
0746 GCObject *gclist;
0747 } Table;
0748
0749
0750
0751
0752
0753 #define keytt(node) ((node)->u.key_tt)
0754 #define keyval(node) ((node)->u.key_val)
0755
0756 #define keyisnil(node) (keytt(node) == LUA_TNIL)
0757 #define keyisinteger(node) (keytt(node) == LUA_VNUMINT)
0758 #define keyival(node) (keyval(node).i)
0759 #define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR))
0760 #define keystrval(node) (gco2ts(keyval(node).gc))
0761
0762 #define setnilkey(node) (keytt(node) = LUA_TNIL)
0763
0764 #define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE)
0765
0766 #define gckey(n) (keyval(n).gc)
0767 #define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL)
0768
0769
0770
0771
0772
0773
0774
0775
0776 #define setdeadkey(node) (keytt(node) = LUA_TDEADKEY)
0777 #define keyisdead(node) (keytt(node) == LUA_TDEADKEY)
0778
0779
0780
0781
0782
0783
0784
0785
0786 #define lmod(s,size) \
0787 (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1)))))
0788
0789
0790 #define twoto(x) (1<<(x))
0791 #define sizenode(t) (twoto((t)->lsizenode))
0792
0793
0794
0795 #define UTF8BUFFSZ 8
0796
0797 LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
0798 LUAI_FUNC int luaO_ceillog2 (unsigned int x);
0799 LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
0800 const TValue *p2, TValue *res);
0801 LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
0802 const TValue *p2, StkId res);
0803 LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
0804 LUAI_FUNC int luaO_hexavalue (int c);
0805 LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj);
0806 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
0807 va_list argp);
0808 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
0809 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen);
0810
0811
0812 #endif
0813