annotate src/lua/lobject.h @ 359:8c474d1d6956

script: got leomnades and TM13, shopping is complete.
author Robert McIntyre <rlm@mit.edu>
date Mon, 09 Apr 2012 08:27:45 -0500
parents 27763b933818
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $
rlm@1 3 ** Type definitions for Lua objects
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7
rlm@1 8 #ifndef lobject_h
rlm@1 9 #define lobject_h
rlm@1 10
rlm@1 11
rlm@1 12 #include <stdarg.h>
rlm@1 13
rlm@1 14
rlm@1 15 #include "llimits.h"
rlm@1 16 #include "lua.h"
rlm@1 17
rlm@1 18
rlm@1 19 /* tags for values visible from Lua */
rlm@1 20 #define LAST_TAG LUA_TTHREAD
rlm@1 21
rlm@1 22 #define NUM_TAGS (LAST_TAG+1)
rlm@1 23
rlm@1 24
rlm@1 25 /*
rlm@1 26 ** Extra tags for non-values
rlm@1 27 */
rlm@1 28 #define LUA_TPROTO (LAST_TAG+1)
rlm@1 29 #define LUA_TUPVAL (LAST_TAG+2)
rlm@1 30 #define LUA_TDEADKEY (LAST_TAG+3)
rlm@1 31
rlm@1 32
rlm@1 33 /*
rlm@1 34 ** Union of all collectable objects
rlm@1 35 */
rlm@1 36 typedef union GCObject GCObject;
rlm@1 37
rlm@1 38
rlm@1 39 /*
rlm@1 40 ** Common Header for all collectable objects (in macro form, to be
rlm@1 41 ** included in other objects)
rlm@1 42 */
rlm@1 43 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
rlm@1 44
rlm@1 45
rlm@1 46 /*
rlm@1 47 ** Common header in struct form
rlm@1 48 */
rlm@1 49 typedef struct GCheader {
rlm@1 50 CommonHeader;
rlm@1 51 } GCheader;
rlm@1 52
rlm@1 53
rlm@1 54
rlm@1 55
rlm@1 56 /*
rlm@1 57 ** Union of all Lua values
rlm@1 58 */
rlm@1 59 typedef union {
rlm@1 60 GCObject *gc;
rlm@1 61 void *p;
rlm@1 62 lua_Number n;
rlm@1 63 int b;
rlm@1 64 } Value;
rlm@1 65
rlm@1 66
rlm@1 67 /*
rlm@1 68 ** Tagged Values
rlm@1 69 */
rlm@1 70
rlm@1 71 #define TValuefields Value value; int tt
rlm@1 72
rlm@1 73 typedef struct lua_TValue {
rlm@1 74 TValuefields;
rlm@1 75 } TValue;
rlm@1 76
rlm@1 77
rlm@1 78 /* Macros to test type */
rlm@1 79 #define ttisnil(o) (ttype(o) == LUA_TNIL)
rlm@1 80 #define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
rlm@1 81 #define ttisstring(o) (ttype(o) == LUA_TSTRING)
rlm@1 82 #define ttistable(o) (ttype(o) == LUA_TTABLE)
rlm@1 83 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
rlm@1 84 #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
rlm@1 85 #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
rlm@1 86 #define ttisthread(o) (ttype(o) == LUA_TTHREAD)
rlm@1 87 #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
rlm@1 88
rlm@1 89 /* Macros to access values */
rlm@1 90 #define ttype(o) ((o)->tt)
rlm@1 91 #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
rlm@1 92 #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
rlm@1 93 #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
rlm@1 94 #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
rlm@1 95 #define tsvalue(o) (&rawtsvalue(o)->tsv)
rlm@1 96 #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
rlm@1 97 #define uvalue(o) (&rawuvalue(o)->uv)
rlm@1 98 #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
rlm@1 99 #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
rlm@1 100 #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
rlm@1 101 #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
rlm@1 102
rlm@1 103 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
rlm@1 104
rlm@1 105 /*
rlm@1 106 ** for internal debug only
rlm@1 107 */
rlm@1 108 #define checkconsistency(obj) \
rlm@1 109 lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
rlm@1 110
rlm@1 111 #define checkliveness(g,obj) \
rlm@1 112 lua_assert(!iscollectable(obj) || \
rlm@1 113 ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
rlm@1 114
rlm@1 115
rlm@1 116 /* Macros to set values */
rlm@1 117 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
rlm@1 118
rlm@1 119 #define setnvalue(obj,x) \
rlm@1 120 { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
rlm@1 121
rlm@1 122 #define setpvalue(obj,x) \
rlm@1 123 { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
rlm@1 124
rlm@1 125 #define setbvalue(obj,x) \
rlm@1 126 { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
rlm@1 127
rlm@1 128 #define setsvalue(L,obj,x) \
rlm@1 129 { TValue *i_o=(obj); \
rlm@1 130 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
rlm@1 131 checkliveness(G(L),i_o); }
rlm@1 132
rlm@1 133 #define setuvalue(L,obj,x) \
rlm@1 134 { TValue *i_o=(obj); \
rlm@1 135 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
rlm@1 136 checkliveness(G(L),i_o); }
rlm@1 137
rlm@1 138 #define setthvalue(L,obj,x) \
rlm@1 139 { TValue *i_o=(obj); \
rlm@1 140 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
rlm@1 141 checkliveness(G(L),i_o); }
rlm@1 142
rlm@1 143 #define setclvalue(L,obj,x) \
rlm@1 144 { TValue *i_o=(obj); \
rlm@1 145 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
rlm@1 146 checkliveness(G(L),i_o); }
rlm@1 147
rlm@1 148 #define sethvalue(L,obj,x) \
rlm@1 149 { TValue *i_o=(obj); \
rlm@1 150 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
rlm@1 151 checkliveness(G(L),i_o); }
rlm@1 152
rlm@1 153 #define setptvalue(L,obj,x) \
rlm@1 154 { TValue *i_o=(obj); \
rlm@1 155 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
rlm@1 156 checkliveness(G(L),i_o); }
rlm@1 157
rlm@1 158
rlm@1 159
rlm@1 160
rlm@1 161 #define setobj(L,obj1,obj2) \
rlm@1 162 { const TValue *o2=(obj2); TValue *o1=(obj1); \
rlm@1 163 o1->value = o2->value; o1->tt=o2->tt; \
rlm@1 164 checkliveness(G(L),o1); }
rlm@1 165
rlm@1 166
rlm@1 167 /*
rlm@1 168 ** different types of sets, according to destination
rlm@1 169 */
rlm@1 170
rlm@1 171 /* from stack to (same) stack */
rlm@1 172 #define setobjs2s setobj
rlm@1 173 /* to stack (not from same stack) */
rlm@1 174 #define setobj2s setobj
rlm@1 175 #define setsvalue2s setsvalue
rlm@1 176 #define sethvalue2s sethvalue
rlm@1 177 #define setptvalue2s setptvalue
rlm@1 178 /* from table to same table */
rlm@1 179 #define setobjt2t setobj
rlm@1 180 /* to table */
rlm@1 181 #define setobj2t setobj
rlm@1 182 /* to new object */
rlm@1 183 #define setobj2n setobj
rlm@1 184 #define setsvalue2n setsvalue
rlm@1 185
rlm@1 186 #define setttype(obj, tt) (ttype(obj) = (tt))
rlm@1 187
rlm@1 188
rlm@1 189 #define iscollectable(o) (ttype(o) >= LUA_TSTRING)
rlm@1 190
rlm@1 191
rlm@1 192
rlm@1 193 typedef TValue *StkId; /* index to stack elements */
rlm@1 194
rlm@1 195
rlm@1 196 /*
rlm@1 197 ** String headers for string table
rlm@1 198 */
rlm@1 199 typedef union TString {
rlm@1 200 L_Umaxalign dummy; /* ensures maximum alignment for strings */
rlm@1 201 struct {
rlm@1 202 CommonHeader;
rlm@1 203 lu_byte reserved;
rlm@1 204 unsigned int hash;
rlm@1 205 size_t len;
rlm@1 206 } tsv;
rlm@1 207 } TString;
rlm@1 208
rlm@1 209
rlm@1 210 #define getstr(ts) cast(const char *, (ts) + 1)
rlm@1 211 #define svalue(o) getstr(rawtsvalue(o))
rlm@1 212
rlm@1 213
rlm@1 214
rlm@1 215 typedef union Udata {
rlm@1 216 L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
rlm@1 217 struct {
rlm@1 218 CommonHeader;
rlm@1 219 struct Table *metatable;
rlm@1 220 struct Table *env;
rlm@1 221 size_t len;
rlm@1 222 } uv;
rlm@1 223 } Udata;
rlm@1 224
rlm@1 225
rlm@1 226
rlm@1 227
rlm@1 228 /*
rlm@1 229 ** Function Prototypes
rlm@1 230 */
rlm@1 231 typedef struct Proto {
rlm@1 232 CommonHeader;
rlm@1 233 TValue *k; /* constants used by the function */
rlm@1 234 Instruction *code;
rlm@1 235 struct Proto **p; /* functions defined inside the function */
rlm@1 236 int *lineinfo; /* map from opcodes to source lines */
rlm@1 237 struct LocVar *locvars; /* information about local variables */
rlm@1 238 TString **upvalues; /* upvalue names */
rlm@1 239 TString *source;
rlm@1 240 int sizeupvalues;
rlm@1 241 int sizek; /* size of `k' */
rlm@1 242 int sizecode;
rlm@1 243 int sizelineinfo;
rlm@1 244 int sizep; /* size of `p' */
rlm@1 245 int sizelocvars;
rlm@1 246 int linedefined;
rlm@1 247 int lastlinedefined;
rlm@1 248 GCObject *gclist;
rlm@1 249 lu_byte nups; /* number of upvalues */
rlm@1 250 lu_byte numparams;
rlm@1 251 lu_byte is_vararg;
rlm@1 252 lu_byte maxstacksize;
rlm@1 253 } Proto;
rlm@1 254
rlm@1 255
rlm@1 256 /* masks for new-style vararg */
rlm@1 257 #define VARARG_HASARG 1
rlm@1 258 #define VARARG_ISVARARG 2
rlm@1 259 #define VARARG_NEEDSARG 4
rlm@1 260
rlm@1 261
rlm@1 262 typedef struct LocVar {
rlm@1 263 TString *varname;
rlm@1 264 int startpc; /* first point where variable is active */
rlm@1 265 int endpc; /* first point where variable is dead */
rlm@1 266 } LocVar;
rlm@1 267
rlm@1 268
rlm@1 269
rlm@1 270 /*
rlm@1 271 ** Upvalues
rlm@1 272 */
rlm@1 273
rlm@1 274 typedef struct UpVal {
rlm@1 275 CommonHeader;
rlm@1 276 TValue *v; /* points to stack or to its own value */
rlm@1 277 union {
rlm@1 278 TValue value; /* the value (when closed) */
rlm@1 279 struct { /* double linked list (when open) */
rlm@1 280 struct UpVal *prev;
rlm@1 281 struct UpVal *next;
rlm@1 282 } l;
rlm@1 283 } u;
rlm@1 284 } UpVal;
rlm@1 285
rlm@1 286
rlm@1 287 /*
rlm@1 288 ** Closures
rlm@1 289 */
rlm@1 290
rlm@1 291 #define ClosureHeader \
rlm@1 292 CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
rlm@1 293 struct Table *env
rlm@1 294
rlm@1 295 typedef struct CClosure {
rlm@1 296 ClosureHeader;
rlm@1 297 lua_CFunction f;
rlm@1 298 TValue upvalue[1];
rlm@1 299 } CClosure;
rlm@1 300
rlm@1 301
rlm@1 302 typedef struct LClosure {
rlm@1 303 ClosureHeader;
rlm@1 304 struct Proto *p;
rlm@1 305 UpVal *upvals[1];
rlm@1 306 } LClosure;
rlm@1 307
rlm@1 308
rlm@1 309 typedef union Closure {
rlm@1 310 CClosure c;
rlm@1 311 LClosure l;
rlm@1 312 } Closure;
rlm@1 313
rlm@1 314
rlm@1 315 #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
rlm@1 316 #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
rlm@1 317
rlm@1 318
rlm@1 319 /*
rlm@1 320 ** Tables
rlm@1 321 */
rlm@1 322
rlm@1 323 typedef union TKey {
rlm@1 324 struct {
rlm@1 325 TValuefields;
rlm@1 326 struct Node *next; /* for chaining */
rlm@1 327 } nk;
rlm@1 328 TValue tvk;
rlm@1 329 } TKey;
rlm@1 330
rlm@1 331
rlm@1 332 typedef struct Node {
rlm@1 333 TValue i_val;
rlm@1 334 TKey i_key;
rlm@1 335 } Node;
rlm@1 336
rlm@1 337
rlm@1 338 typedef struct Table {
rlm@1 339 CommonHeader;
rlm@1 340 lu_byte flags; /* 1<<p means tagmethod(p) is not present */
rlm@1 341 lu_byte lsizenode; /* log2 of size of `node' array */
rlm@1 342 struct Table *metatable;
rlm@1 343 TValue *array; /* array part */
rlm@1 344 Node *node;
rlm@1 345 Node *lastfree; /* any free position is before this position */
rlm@1 346 GCObject *gclist;
rlm@1 347 int sizearray; /* size of `array' array */
rlm@1 348 } Table;
rlm@1 349
rlm@1 350
rlm@1 351
rlm@1 352 /*
rlm@1 353 ** `module' operation for hashing (size is always a power of 2)
rlm@1 354 */
rlm@1 355 #define lmod(s,size) \
rlm@1 356 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
rlm@1 357
rlm@1 358
rlm@1 359 #define twoto(x) (1<<(x))
rlm@1 360 #define sizenode(t) (twoto((t)->lsizenode))
rlm@1 361
rlm@1 362
rlm@1 363 #define luaO_nilobject (&luaO_nilobject_)
rlm@1 364
rlm@1 365 LUAI_DATA const TValue luaO_nilobject_;
rlm@1 366
rlm@1 367 #define ceillog2(x) (luaO_log2((x)-1) + 1)
rlm@1 368
rlm@1 369 LUAI_FUNC int luaO_log2 (unsigned int x);
rlm@1 370 LUAI_FUNC int luaO_int2fb (unsigned int x);
rlm@1 371 LUAI_FUNC int luaO_fb2int (int x);
rlm@1 372 LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
rlm@1 373 LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
rlm@1 374 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
rlm@1 375 va_list argp);
rlm@1 376 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
rlm@1 377 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
rlm@1 378
rlm@1 379
rlm@1 380 #endif
rlm@1 381