Lua 5.1.4: lobject.h
This files provides the types an operations on for handling
values (TValue objects) in Lua.
L0001 /*
L0002 ** $Id: lobject.h,v 2.20.1.2 2008年08月06日 13:29:48 roberto Exp $
L0003 ** Type definitions for Lua objects
L0004 ** See Copyright Notice in lua.h
L0005 */
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 /* tags for values visible from Lua */
L0020 #define LAST_TAG LUA_TTHREAD
L0021
L0022 #define NUM_TAGS (LAST_TAG+1)
L0023
L0024
L0025 /*
L0026 ** Extra tags for non-values
L0027 */
L0028 #define LUA_TPROTO (LAST_TAG+1)
L0029 #define LUA_TUPVAL (LAST_TAG+2)
L0030 #define LUA_TDEADKEY (LAST_TAG+3)
L0031
L0032
L0033 /*
L0034 ** Union of all collectable objects
L0035 */
L0036 typedef union GCObject GCObject;
L0037
L0038
L0039 /*
L0040 ** Common Header for all collectable objects (in macro form, to be
L0041 ** included in other objects)
L0042 */
L0043 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
L0044
L0045
L0046 /*
L0047 ** Common header in struct form
L0048 */
L0049 typedef struct GCheader {
L0050 CommonHeader;
L0051 } GCheader;
L0052
L0053
L0054
L0055
L0056 /*
L0057 ** Union of all Lua values
L0058 */
L0059 typedef union {
L0060 GCObject *gc;
L0061 void *p;
L0062 lua_Number n;
L0063 int b;
L0064 } Value;
L0065
L0066
L0067 /*
L0068 ** Tagged Values
L0069 */
L0070
L0071 #define TValuefields Value value; int tt
L0072
L0073 typedef struct lua_TValue {
L0074 TValuefields;
L0075 } TValue;
L0076
L0077
L0078 /* Macros to test type */
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 /* Macros to access values */
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 /*
L0106 ** for internal debug only
L0107 */
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 /* Macros to set values */
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 /*
L0168 ** different types of sets, according to destination
L0169 */
L0170
L0171 /* from stack to (same) stack */
L0172 #define setobjs2s setobj
L0173 /* to stack (not from same stack) */
L0174 #define setobj2s setobj
L0175 #define setsvalue2s setsvalue
L0176 #define sethvalue2s sethvalue
L0177 #define setptvalue2s setptvalue
L0178 /* from table to same table */
L0179 #define setobjt2t setobj
L0180 /* to table */
L0181 #define setobj2t setobj
L0182 /* to new object */
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; /* index to stack elements */
L0194
L0195
L0196 /*
L0197 ** String headers for string table
L0198 */
L0199 typedef union TString {
L0200 L_Umaxalign dummy; /* ensures maximum alignment for strings */
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; /* ensures maximum alignment for `local' udata */
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 /*
L0229 ** Function Prototypes
L0230 */
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; /* constants used by the function */
L0234 Instruction *code;
L0235 struct Proto **p; /* functions defined inside the function */
L0236 int *lineinfo; /* map from opcodes to source lines */
L0237 struct LocVar *locvars; /* information about local variables */
L0238 TString **upvalues; /* upvalue names */
L0239 TString *source;
L0240 int sizeupvalues;
L0241 int sizek; /* size of `k' */
L0242 int sizecode;
L0243 int sizelineinfo;
L0244 int sizep; /* size of `p' */
L0245 int sizelocvars;
L0246 int linedefined;
L0247 int lastlinedefined;
L0248 GCObject *gclist;
L0249 lu_byte nups; /* number of upvalues */
L0250 lu_byte numparams;
L0251 lu_byte is_vararg;
L0252 lu_byte maxstacksize;
L0253 } Proto;
L0254
L0255
L0256 /* masks for new-style vararg */
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; /* first point where variable is active */
L0265 int endpc; /* first point where variable is dead */
L0266 } LocVar;
L0267
L0268
L0269
L0270 /*
L0271 ** Upvalues
L0272 */
L0273
L0274 typedef struct UpVal {
This represents upvalues. They are stored in a closure value (LClosure).
Compare to Upvaldesc.
L0275 CommonHeader;
L0276 TValue *v; /* points to stack or to its own value */
L0277 union {
L0278 TValue value; /* the value (when closed) */
L0279 struct { /* double linked list (when open) */
L0280 struct UpVal *prev;
L0281 struct UpVal *next;
L0282 } l;
L0283 } u;
L0284 } UpVal;
L0285
L0286
L0287 /*
L0288 ** Closures
L0289 */
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 /*
L0320 ** Tables
L0321 */
L0322
L0323 typedef union TKey {
L0324 struct {
L0325 TValuefields;
L0326 struct Node *next; /* for chaining */
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; /* 1<<p means tagmethod(p) is not present */
L0341 lu_byte lsizenode; /* log2 of size of `node' array */
L0342 struct Table *metatable;
L0343 TValue *array; /* array part */
L0344 Node *node;
L0345 Node *lastfree; /* any free position is before this position */
L0346 GCObject *gclist;
L0347 int sizearray; /* size of `array' array */
L0348 } Table;
L0349
L0350
L0351
L0352 /*
L0353 ** `module' operation for hashing (size is always a power of 2)
L0354 */
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