diff src/lua/src/lobject.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lua/src/lobject.h	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,381 @@
     1.4 +/*
     1.5 +** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $
     1.6 +** Type definitions for Lua objects
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +
    1.11 +#ifndef lobject_h
    1.12 +#define lobject_h
    1.13 +
    1.14 +
    1.15 +#include <stdarg.h>
    1.16 +
    1.17 +
    1.18 +#include "llimits.h"
    1.19 +#include "lua.h"
    1.20 +
    1.21 +
    1.22 +/* tags for values visible from Lua */
    1.23 +#define LAST_TAG	LUA_TTHREAD
    1.24 +
    1.25 +#define NUM_TAGS	(LAST_TAG+1)
    1.26 +
    1.27 +
    1.28 +/*
    1.29 +** Extra tags for non-values
    1.30 +*/
    1.31 +#define LUA_TPROTO	(LAST_TAG+1)
    1.32 +#define LUA_TUPVAL	(LAST_TAG+2)
    1.33 +#define LUA_TDEADKEY	(LAST_TAG+3)
    1.34 +
    1.35 +
    1.36 +/*
    1.37 +** Union of all collectable objects
    1.38 +*/
    1.39 +typedef union GCObject GCObject;
    1.40 +
    1.41 +
    1.42 +/*
    1.43 +** Common Header for all collectable objects (in macro form, to be
    1.44 +** included in other objects)
    1.45 +*/
    1.46 +#define CommonHeader	GCObject *next; lu_byte tt; lu_byte marked
    1.47 +
    1.48 +
    1.49 +/*
    1.50 +** Common header in struct form
    1.51 +*/
    1.52 +typedef struct GCheader {
    1.53 +  CommonHeader;
    1.54 +} GCheader;
    1.55 +
    1.56 +
    1.57 +
    1.58 +
    1.59 +/*
    1.60 +** Union of all Lua values
    1.61 +*/
    1.62 +typedef union {
    1.63 +  GCObject *gc;
    1.64 +  void *p;
    1.65 +  lua_Number n;
    1.66 +  int b;
    1.67 +} Value;
    1.68 +
    1.69 +
    1.70 +/*
    1.71 +** Tagged Values
    1.72 +*/
    1.73 +
    1.74 +#define TValuefields	Value value; int tt
    1.75 +
    1.76 +typedef struct lua_TValue {
    1.77 +  TValuefields;
    1.78 +} TValue;
    1.79 +
    1.80 +
    1.81 +/* Macros to test type */
    1.82 +#define ttisnil(o)	(ttype(o) == LUA_TNIL)
    1.83 +#define ttisnumber(o)	(ttype(o) == LUA_TNUMBER)
    1.84 +#define ttisstring(o)	(ttype(o) == LUA_TSTRING)
    1.85 +#define ttistable(o)	(ttype(o) == LUA_TTABLE)
    1.86 +#define ttisfunction(o)	(ttype(o) == LUA_TFUNCTION)
    1.87 +#define ttisboolean(o)	(ttype(o) == LUA_TBOOLEAN)
    1.88 +#define ttisuserdata(o)	(ttype(o) == LUA_TUSERDATA)
    1.89 +#define ttisthread(o)	(ttype(o) == LUA_TTHREAD)
    1.90 +#define ttislightuserdata(o)	(ttype(o) == LUA_TLIGHTUSERDATA)
    1.91 +
    1.92 +/* Macros to access values */
    1.93 +#define ttype(o)	((o)->tt)
    1.94 +#define gcvalue(o)	check_exp(iscollectable(o), (o)->value.gc)
    1.95 +#define pvalue(o)	check_exp(ttislightuserdata(o), (o)->value.p)
    1.96 +#define nvalue(o)	check_exp(ttisnumber(o), (o)->value.n)
    1.97 +#define rawtsvalue(o)	check_exp(ttisstring(o), &(o)->value.gc->ts)
    1.98 +#define tsvalue(o)	(&rawtsvalue(o)->tsv)
    1.99 +#define rawuvalue(o)	check_exp(ttisuserdata(o), &(o)->value.gc->u)
   1.100 +#define uvalue(o)	(&rawuvalue(o)->uv)
   1.101 +#define clvalue(o)	check_exp(ttisfunction(o), &(o)->value.gc->cl)
   1.102 +#define hvalue(o)	check_exp(ttistable(o), &(o)->value.gc->h)
   1.103 +#define bvalue(o)	check_exp(ttisboolean(o), (o)->value.b)
   1.104 +#define thvalue(o)	check_exp(ttisthread(o), &(o)->value.gc->th)
   1.105 +
   1.106 +#define l_isfalse(o)	(ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
   1.107 +
   1.108 +/*
   1.109 +** for internal debug only
   1.110 +*/
   1.111 +#define checkconsistency(obj) \
   1.112 +  lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
   1.113 +
   1.114 +#define checkliveness(g,obj) \
   1.115 +  lua_assert(!iscollectable(obj) || \
   1.116 +  ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
   1.117 +
   1.118 +
   1.119 +/* Macros to set values */
   1.120 +#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
   1.121 +
   1.122 +#define setnvalue(obj,x) \
   1.123 +  { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
   1.124 +
   1.125 +#define setpvalue(obj,x) \
   1.126 +  { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
   1.127 +
   1.128 +#define setbvalue(obj,x) \
   1.129 +  { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
   1.130 +
   1.131 +#define setsvalue(L,obj,x) \
   1.132 +  { TValue *i_o=(obj); \
   1.133 +    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
   1.134 +    checkliveness(G(L),i_o); }
   1.135 +
   1.136 +#define setuvalue(L,obj,x) \
   1.137 +  { TValue *i_o=(obj); \
   1.138 +    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
   1.139 +    checkliveness(G(L),i_o); }
   1.140 +
   1.141 +#define setthvalue(L,obj,x) \
   1.142 +  { TValue *i_o=(obj); \
   1.143 +    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
   1.144 +    checkliveness(G(L),i_o); }
   1.145 +
   1.146 +#define setclvalue(L,obj,x) \
   1.147 +  { TValue *i_o=(obj); \
   1.148 +    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
   1.149 +    checkliveness(G(L),i_o); }
   1.150 +
   1.151 +#define sethvalue(L,obj,x) \
   1.152 +  { TValue *i_o=(obj); \
   1.153 +    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
   1.154 +    checkliveness(G(L),i_o); }
   1.155 +
   1.156 +#define setptvalue(L,obj,x) \
   1.157 +  { TValue *i_o=(obj); \
   1.158 +    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
   1.159 +    checkliveness(G(L),i_o); }
   1.160 +
   1.161 +
   1.162 +
   1.163 +
   1.164 +#define setobj(L,obj1,obj2) \
   1.165 +  { const TValue *o2=(obj2); TValue *o1=(obj1); \
   1.166 +    o1->value = o2->value; o1->tt=o2->tt; \
   1.167 +    checkliveness(G(L),o1); }
   1.168 +
   1.169 +
   1.170 +/*
   1.171 +** different types of sets, according to destination
   1.172 +*/
   1.173 +
   1.174 +/* from stack to (same) stack */
   1.175 +#define setobjs2s	setobj
   1.176 +/* to stack (not from same stack) */
   1.177 +#define setobj2s	setobj
   1.178 +#define setsvalue2s	setsvalue
   1.179 +#define sethvalue2s	sethvalue
   1.180 +#define setptvalue2s	setptvalue
   1.181 +/* from table to same table */
   1.182 +#define setobjt2t	setobj
   1.183 +/* to table */
   1.184 +#define setobj2t	setobj
   1.185 +/* to new object */
   1.186 +#define setobj2n	setobj
   1.187 +#define setsvalue2n	setsvalue
   1.188 +
   1.189 +#define setttype(obj, tt) (ttype(obj) = (tt))
   1.190 +
   1.191 +
   1.192 +#define iscollectable(o)	(ttype(o) >= LUA_TSTRING)
   1.193 +
   1.194 +
   1.195 +
   1.196 +typedef TValue *StkId;  /* index to stack elements */
   1.197 +
   1.198 +
   1.199 +/*
   1.200 +** String headers for string table
   1.201 +*/
   1.202 +typedef union TString {
   1.203 +  L_Umaxalign dummy;  /* ensures maximum alignment for strings */
   1.204 +  struct {
   1.205 +    CommonHeader;
   1.206 +    lu_byte reserved;
   1.207 +    unsigned int hash;
   1.208 +    size_t len;
   1.209 +  } tsv;
   1.210 +} TString;
   1.211 +
   1.212 +
   1.213 +#define getstr(ts)	cast(const char *, (ts) + 1)
   1.214 +#define svalue(o)       getstr(rawtsvalue(o))
   1.215 +
   1.216 +
   1.217 +
   1.218 +typedef union Udata {
   1.219 +  L_Umaxalign dummy;  /* ensures maximum alignment for `local' udata */
   1.220 +  struct {
   1.221 +    CommonHeader;
   1.222 +    struct Table *metatable;
   1.223 +    struct Table *env;
   1.224 +    size_t len;
   1.225 +  } uv;
   1.226 +} Udata;
   1.227 +
   1.228 +
   1.229 +
   1.230 +
   1.231 +/*
   1.232 +** Function Prototypes
   1.233 +*/
   1.234 +typedef struct Proto {
   1.235 +  CommonHeader;
   1.236 +  TValue *k;  /* constants used by the function */
   1.237 +  Instruction *code;
   1.238 +  struct Proto **p;  /* functions defined inside the function */
   1.239 +  int *lineinfo;  /* map from opcodes to source lines */
   1.240 +  struct LocVar *locvars;  /* information about local variables */
   1.241 +  TString **upvalues;  /* upvalue names */
   1.242 +  TString  *source;
   1.243 +  int sizeupvalues;
   1.244 +  int sizek;  /* size of `k' */
   1.245 +  int sizecode;
   1.246 +  int sizelineinfo;
   1.247 +  int sizep;  /* size of `p' */
   1.248 +  int sizelocvars;
   1.249 +  int linedefined;
   1.250 +  int lastlinedefined;
   1.251 +  GCObject *gclist;
   1.252 +  lu_byte nups;  /* number of upvalues */
   1.253 +  lu_byte numparams;
   1.254 +  lu_byte is_vararg;
   1.255 +  lu_byte maxstacksize;
   1.256 +} Proto;
   1.257 +
   1.258 +
   1.259 +/* masks for new-style vararg */
   1.260 +#define VARARG_HASARG		1
   1.261 +#define VARARG_ISVARARG		2
   1.262 +#define VARARG_NEEDSARG		4
   1.263 +
   1.264 +
   1.265 +typedef struct LocVar {
   1.266 +  TString *varname;
   1.267 +  int startpc;  /* first point where variable is active */
   1.268 +  int endpc;    /* first point where variable is dead */
   1.269 +} LocVar;
   1.270 +
   1.271 +
   1.272 +
   1.273 +/*
   1.274 +** Upvalues
   1.275 +*/
   1.276 +
   1.277 +typedef struct UpVal {
   1.278 +  CommonHeader;
   1.279 +  TValue *v;  /* points to stack or to its own value */
   1.280 +  union {
   1.281 +    TValue value;  /* the value (when closed) */
   1.282 +    struct {  /* double linked list (when open) */
   1.283 +      struct UpVal *prev;
   1.284 +      struct UpVal *next;
   1.285 +    } l;
   1.286 +  } u;
   1.287 +} UpVal;
   1.288 +
   1.289 +
   1.290 +/*
   1.291 +** Closures
   1.292 +*/
   1.293 +
   1.294 +#define ClosureHeader \
   1.295 +	CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
   1.296 +	struct Table *env
   1.297 +
   1.298 +typedef struct CClosure {
   1.299 +  ClosureHeader;
   1.300 +  lua_CFunction f;
   1.301 +  TValue upvalue[1];
   1.302 +} CClosure;
   1.303 +
   1.304 +
   1.305 +typedef struct LClosure {
   1.306 +  ClosureHeader;
   1.307 +  struct Proto *p;
   1.308 +  UpVal *upvals[1];
   1.309 +} LClosure;
   1.310 +
   1.311 +
   1.312 +typedef union Closure {
   1.313 +  CClosure c;
   1.314 +  LClosure l;
   1.315 +} Closure;
   1.316 +
   1.317 +
   1.318 +#define iscfunction(o)	(ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
   1.319 +#define isLfunction(o)	(ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
   1.320 +
   1.321 +
   1.322 +/*
   1.323 +** Tables
   1.324 +*/
   1.325 +
   1.326 +typedef union TKey {
   1.327 +  struct {
   1.328 +    TValuefields;
   1.329 +    struct Node *next;  /* for chaining */
   1.330 +  } nk;
   1.331 +  TValue tvk;
   1.332 +} TKey;
   1.333 +
   1.334 +
   1.335 +typedef struct Node {
   1.336 +  TValue i_val;
   1.337 +  TKey i_key;
   1.338 +} Node;
   1.339 +
   1.340 +
   1.341 +typedef struct Table {
   1.342 +  CommonHeader;
   1.343 +  lu_byte flags;  /* 1<<p means tagmethod(p) is not present */ 
   1.344 +  lu_byte lsizenode;  /* log2 of size of `node' array */
   1.345 +  struct Table *metatable;
   1.346 +  TValue *array;  /* array part */
   1.347 +  Node *node;
   1.348 +  Node *lastfree;  /* any free position is before this position */
   1.349 +  GCObject *gclist;
   1.350 +  int sizearray;  /* size of `array' array */
   1.351 +} Table;
   1.352 +
   1.353 +
   1.354 +
   1.355 +/*
   1.356 +** `module' operation for hashing (size is always a power of 2)
   1.357 +*/
   1.358 +#define lmod(s,size) \
   1.359 +	(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
   1.360 +
   1.361 +
   1.362 +#define twoto(x)	(1<<(x))
   1.363 +#define sizenode(t)	(twoto((t)->lsizenode))
   1.364 +
   1.365 +
   1.366 +#define luaO_nilobject		(&luaO_nilobject_)
   1.367 +
   1.368 +LUAI_DATA const TValue luaO_nilobject_;
   1.369 +
   1.370 +#define ceillog2(x)	(luaO_log2((x)-1) + 1)
   1.371 +
   1.372 +LUAI_FUNC int luaO_log2 (unsigned int x);
   1.373 +LUAI_FUNC int luaO_int2fb (unsigned int x);
   1.374 +LUAI_FUNC int luaO_fb2int (int x);
   1.375 +LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
   1.376 +LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
   1.377 +LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
   1.378 +                                                       va_list argp);
   1.379 +LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
   1.380 +LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
   1.381 +
   1.382 +
   1.383 +#endif
   1.384 +