Lua 5.1.4: lobject.h
This files provides the types an operations on for handling
values (TValue objects) in Lua.
L0001
L0006
L0007
L0008 #ifndef lobject_h
L0009 #define lobject_h
L0010
L0011
L0012 #include <stdarg.h>
L0013
L0014
L0015 #include "llimits.h"
L0016 #include "lua.h"
L0017
L0018
L0019
L0020 #define LAST_TAG LUA_TTHREAD
L0021
L0022 #define NUM_TAGS (LAST_TAG+1)
L0023
L0024
L0025
L0028 #define LUA_TPROTO (LAST_TAG+1)
L0029 #define LUA_TUPVAL (LAST_TAG+2)
L0030 #define LUA_TDEADKEY (LAST_TAG+3)
L0031
L0032
L0033
L0036 typedef union GCObject GCObject;
L0037
L0038
L0039
L0043 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
L0044
L0045
L0046
L0049 typedef struct GCheader {
L0050 CommonHeader;
L0051 } GCheader;
L0052
L0053
L0054
L0055
L0056
L0059 typedef union {
L0060 GCObject *gc;
L0061 void *p;
L0062 lua_Number n;
L0063 int b;
L0064 } Value;
L0065
L0066
L0067
L0070
L0071 #define TValuefields Value value; int tt
L0072
L0073 typedef struct lua_TValue {
L0074 TValuefields;
L0075 } TValue;
L0076
L0077
L0078
L0079 #define ttisnil(o) (ttype(o) == LUA_TNIL)
Checks whether TValue o is nil.
L0080 #define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
Checks whether TValue o is of type number.
L0081 #define ttisstring(o) (ttype(o) == LUA_TSTRING)
L0082 #define ttistable(o) (ttype(o) == LUA_TTABLE)
L0083 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
L0084 #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
L0085 #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
L0086 #define ttisthread(o) (ttype(o) == LUA_TTHREAD)
L0087 #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
L0088
L0089
L0090 #define ttype(o) ((o)->tt)
L0091 #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
L0092 #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
Gets void pointer in TValue o.
L0093 #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
Gets number (lua_Number) in TValue o (or asserts if not number).
L0094 #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
Gets string (TString) in TValue o.
L0095 #define tsvalue(o) (&rawtsvalue(o)->tsv)
Gets string header (TString.tsv) in TValue o.
L0096 #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
L0097 #define uvalue(o) (&rawuvalue(o)->uv)
Gets userdata header (Udata.uv) in TValue o.
L0098 #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
Gets closure (Closure - GCObject.cl) in TValue o.
L0099 #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
Gets table (Table - GCobject.h) in TValue o.
L0100 #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
Gets boolean (int) value in TValue o.
L0101 #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
Gets thread lua_State (GCObject.th) value in Tvalue o.
L0102
L0103 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
Gets whether TValue o evaluates to false (i.e. is nil or false).
L0104
L0105
L0108 #define checkconsistency(obj) \
L0109 lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
L0110
L0111 #define checkliveness(g,obj) \
L0112 lua_assert(!iscollectable(obj) || \
L0113 ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
L0114
L0115
L0116
L0117 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
L0118
L0119 #define setnvalue(obj,x) \
L0120 { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
Sets value of TValue obj to number (lua_Number) x.
L0121
L0122 #define setpvalue(obj,x) \
L0123 { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
L0124
L0125 #define setbvalue(obj,x) \
L0126 { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
Sets value of TValue obj to boolean (int 0..1) x.
L0127
L0128 #define setsvalue(L,obj,x) \
L0129 { TValue *i_o=(obj); \
L0130 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
L0131 checkliveness(G(L),i_o); }
L0132
L0133 #define setuvalue(L,obj,x) \
L0134 { TValue *i_o=(obj); \
L0135 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
L0136 checkliveness(G(L),i_o); }
L0137
L0138 #define setthvalue(L,obj,x) \
L0139 { TValue *i_o=(obj); \
L0140 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
L0141 checkliveness(G(L),i_o); }
L0142
L0143 #define setclvalue(L,obj,x) \
L0144 { TValue *i_o=(obj); \
L0145 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
L0146 checkliveness(G(L),i_o); }
L0147
L0148 #define sethvalue(L,obj,x) \
L0149 { TValue *i_o=(obj); \
L0150 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
L0151 checkliveness(G(L),i_o); }
L0152
L0153 #define setptvalue(L,obj,x) \
L0154 { TValue *i_o=(obj); \
L0155 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
L0156 checkliveness(G(L),i_o); }
L0157
L0158
L0159
L0160
L0161 #define setobj(L,obj1,obj2) \
L0162 { const TValue *o2=(obj2); TValue *o1=(obj1); \
L0163 o1->value = o2->value; o1->tt=o2->tt; \
L0164 checkliveness(G(L),o1); }
L0165
L0166
L0167
L0170
L0171
L0172 #define setobjs2s setobj
L0173
L0174 #define setobj2s setobj
L0175 #define setsvalue2s setsvalue
L0176 #define sethvalue2s sethvalue
L0177 #define setptvalue2s setptvalue
L0178
L0179 #define setobjt2t setobj
L0180
L0181 #define setobj2t setobj
L0182
L0183 #define setobj2n setobj
L0184 #define setsvalue2n setsvalue
L0185
L0186 #define setttype(obj, tt) (ttype(obj) = (tt))
L0187
L0188
L0189 #define iscollectable(o) (ttype(o) >= LUA_TSTRING)
L0190
L0191
L0192
L0193 typedef TValue *StkId;
L0194
L0195
L0196
L0199 typedef union TString {
L0200 L_Umaxalign dummy;
L0201 struct {
L0202 CommonHeader;
L0203 lu_byte reserved;
L0204 unsigned int hash;
L0205 size_t len;
L0206 } tsv;
L0207 } TString;
L0208
L0209
L0210 #define getstr(ts) cast(const char *, (ts) + 1)
L0211 #define svalue(o) getstr(rawtsvalue(o))
Note: TValue o -> const char *.
L0212
L0213
L0214
L0215 typedef union Udata {
Note: each (heavyweight) userdata can be associated with a metatable
and a user value (previously called a "userdata environment table") set
via lua_setuservalue.
A userdata is garbage collectable CommonHeader) and is allocated
as a series of `len` bytes in Lua's own memory.
L0216 L_Umaxalign dummy;
L0217 struct {
L0218 CommonHeader;
L0219 struct Table *metatable;
L0220 struct Table *env;
L0221 size_t len;
L0222 } uv;
L0223 } Udata;
L0224
L0225
L0226
L0227
L0228
L0231 typedef struct Proto {
Every definition of a Lua function (or C function exposed as a Lua function)
is represented with a function prototype object.
This contains things like a list of bytecode instructions (code),
links to other constant data used by the functions
(constants, nested functions, and upvalue descriptions), and other
metadata (e.g. debugging info).
For each function prototype, any number of values may be
instantiated (allocated) based on that prototype. For example, this:
local t = {}
for i=1,10 do
t[i] = function() return i end
end
creates 10 function objects that all link to the same prototype of
"function() return i end".
L0232 CommonHeader;
L0233 TValue *k;
L0234 Instruction *code;
L0235 struct Proto **p;
L0236 int *lineinfo;
L0237 struct LocVar *locvars;
L0238 TString **upvalues;
L0239 TString *source;
L0240 int sizeupvalues;
L0241 int sizek;
L0242 int sizecode;
L0243 int sizelineinfo;
L0244 int sizep;
L0245 int sizelocvars;
L0246 int linedefined;
L0247 int lastlinedefined;
L0248 GCObject *gclist;
L0249 lu_byte nups;
L0250 lu_byte numparams;
L0251 lu_byte is_vararg;
L0252 lu_byte maxstacksize;
L0253 } Proto;
L0254
L0255
L0256
L0257 #define VARARG_HASARG 1
L0258 #define VARARG_ISVARARG 2
L0259 #define VARARG_NEEDSARG 4
L0260
L0261
L0262 typedef struct LocVar {
L0263 TString *varname;
L0264 int startpc;
L0265 int endpc;
L0266 } LocVar;
L0267
L0268
L0269
L0270
L0273
L0274 typedef struct UpVal {
This represents upvalues. They are stored in a closure value (LClosure).
Compare to Upvaldesc.
L0275 CommonHeader;
L0276 TValue *v;
L0277 union {
L0278 TValue value;
L0279 struct {
L0280 struct UpVal *prev;
L0281 struct UpVal *next;
L0282 } l;
L0283 } u;
L0284 } UpVal;
L0285
L0286
L0287
L0290
L0291 #define ClosureHeader \
L0292 CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
L0293 struct Table *env
L0294
L0295 typedef struct CClosure {
L0296 ClosureHeader;
L0297 lua_CFunction f;
L0298 TValue upvalue[1];
L0299 } CClosure;
L0300
L0301
L0302 typedef struct LClosure {
L0303 ClosureHeader;
L0304 struct Proto *p;
L0305 UpVal *upvals[1];
L0306 } LClosure;
L0307
L0308
L0309 typedef union Closure {
Represents a closure (implemented via C Function or Lua function).
These are linked to TValue's, among other places.
L0310 CClosure c;
L0311 LClosure l;
L0312 } Closure;
L0313
L0314
L0315 #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
L0316 #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
L0317
L0318
L0319
L0322
L0323 typedef union TKey {
L0324 struct {
L0325 TValuefields;
L0326 struct Node *next;
L0327 } nk;
L0328 TValue tvk;
L0329 } TKey;
L0330
L0331
L0332 typedef struct Node {
L0333 TValue i_val;
L0334 TKey i_key;
L0335 } Node;
L0336
L0337
L0338 typedef struct Table {
Represents a Lua table ({}).
These are linked to TValues.
Note: each table can have an optional metatable and can have
an array part (stored in array `array` of length `sizearray` and
a hash part (stored in array `node` of length encoded in `lsizenode`).
You can quickly check for certain metamethods via the `flags` field
rather than looking in the metatable.
L0339 CommonHeader;
L0340 lu_byte flags;
L0341 lu_byte lsizenode;
L0342 struct Table *metatable;
L0343 TValue *array;
L0344 Node *node;
L0345 Node *lastfree;
L0346 GCObject *gclist;
L0347 int sizearray;
L0348 } Table;
L0349
L0350
L0351
L0352
L0355 #define lmod(s,size) \
L0356 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
L0357
L0358
L0359 #define twoto(x) (1<<(x))
Efficiently raises 2 to the integer x power.
L0360 #define sizenode(t) (twoto((t)->lsizenode))
L0361
L0362
L0363 #define luaO_nilobject (&luaO_nilobject_)
L0364
L0365 LUAI_DATA const TValue luaO_nilobject_;
L0366
L0367 #define ceillog2(x) (luaO_log2((x)-1) + 1)
L0368
L0369 LUAI_FUNC int luaO_log2 (unsigned int x);
L0370 LUAI_FUNC int luaO_int2fb (unsigned int x);
L0371 LUAI_FUNC int luaO_fb2int (int x);
L0372 LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
L0373 LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
L0374 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
L0375 va_list argp);
L0376 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
L0377 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
L0378
L0379
L0380 #endif
L0381
Generated by pretty.lua