lobject.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. ** $Id: lobject.h,v 1.162 2003/11/18 14:55:11 roberto Exp roberto $
  3. ** Type definitions for Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef lobject_h
  7. #define lobject_h
  8. #include "llimits.h"
  9. #include "lua.h"
  10. /* tags for values visible from Lua */
  11. #define NUM_TAGS LUA_TTHREAD
  12. /*
  13. ** Extra tags for non-values
  14. */
  15. #define LUA_TPROTO (NUM_TAGS+1)
  16. #define LUA_TUPVAL (NUM_TAGS+2)
  17. /*
  18. ** Union of all collectable objects
  19. */
  20. typedef union GCObject GCObject;
  21. /*
  22. ** Common Header for all collectable objects (in macro form, to be
  23. ** included in other objects)
  24. */
  25. #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
  26. /*
  27. ** Common header in struct form
  28. */
  29. typedef struct GCheader {
  30. CommonHeader;
  31. } GCheader;
  32. /*
  33. ** Union of all Lua values
  34. */
  35. typedef union {
  36. GCObject *gc;
  37. void *p;
  38. lua_Number n;
  39. int b;
  40. } Value;
  41. /*
  42. ** Tagged Values
  43. */
  44. typedef struct lua_TValue {
  45. int tt;
  46. Value value;
  47. } TValue;
  48. /* Macros to test type */
  49. #define ttisnil(o) (ttype(o) == LUA_TNIL)
  50. #define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
  51. #define ttisstring(o) (ttype(o) == LUA_TSTRING)
  52. #define ttistable(o) (ttype(o) == LUA_TTABLE)
  53. #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
  54. #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
  55. #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
  56. #define ttisthread(o) (ttype(o) == LUA_TTHREAD)
  57. #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
  58. /* Macros to access values */
  59. #define ttype(o) ((o)->tt)
  60. #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
  61. #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
  62. #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
  63. #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
  64. #define tsvalue(o) (&rawtsvalue(o)->tsv)
  65. #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
  66. #define uvalue(o) (&rawuvalue(o)->uv)
  67. #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
  68. #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
  69. #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
  70. #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
  71. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  72. /*
  73. ** for internal debug only
  74. */
  75. #define checkconsistency(obj) \
  76. lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
  77. #define checkliveness(L,obj) \
  78. lua_assert(!iscollectable(obj) || \
  79. ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(G(L), (obj)->value.gc)))
  80. /* Macros to set values */
  81. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  82. #define setnvalue(obj,x) \
  83. { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
  84. #define chgnvalue(obj,x) \
  85. check_exp(ttype(obj)==LUA_TNUMBER, (obj)->value.n=(x))
  86. #define setpvalue(obj,x) \
  87. { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
  88. #define setbvalue(obj,x) \
  89. { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
  90. #define setsvalue(L,obj,x) \
  91. { TValue *i_o=(obj); \
  92. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
  93. checkliveness(L,i_o); }
  94. #define setuvalue(L,obj,x) \
  95. { TValue *i_o=(obj); \
  96. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
  97. checkliveness(L,i_o); }
  98. #define setthvalue(L,obj,x) \
  99. { TValue *i_o=(obj); \
  100. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
  101. checkliveness(L,i_o); }
  102. #define setclvalue(L,obj,x) \
  103. { TValue *i_o=(obj); \
  104. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
  105. checkliveness(L,i_o); }
  106. #define sethvalue(L,obj,x) \
  107. { TValue *i_o=(obj); \
  108. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
  109. checkliveness(L,i_o); }
  110. #define setptvalue(L,obj,x) \
  111. { TValue *i_o=(obj); \
  112. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
  113. checkliveness(L,i_o); }
  114. #define setobj(L,obj1,obj2) \
  115. { const TValue *o2=(obj2); TValue *o1=(obj1); \
  116. o1->tt=o2->tt; o1->value = o2->value; \
  117. checkliveness(L,o1); }
  118. /*
  119. ** different types of sets, according to destination
  120. */
  121. /* from stack to (same) stack */
  122. #define setobjs2s setobj
  123. /* to stack (not from same stack) */
  124. #define setobj2s setobj
  125. #define setsvalue2s setsvalue
  126. #define sethvalue2s sethvalue
  127. #define setptvalue2s setptvalue
  128. /* from table to same table */
  129. #define setobjt2t setobj
  130. /* to table */
  131. #define setobj2t setobj
  132. /* to new object */
  133. #define setobj2n setobj
  134. #define setsvalue2n setsvalue
  135. #define setttype(obj, tt) (ttype(obj) = (tt))
  136. #define iscollectable(o) (ttype(o) >= LUA_TSTRING)
  137. typedef TValue *StkId; /* index to stack elements */
  138. /*
  139. ** String headers for string table
  140. */
  141. typedef union TString {
  142. L_Umaxalign dummy; /* ensures maximum alignment for strings */
  143. struct {
  144. CommonHeader;
  145. lu_byte reserved;
  146. unsigned int hash;
  147. size_t len;
  148. } tsv;
  149. } TString;
  150. #define getstr(ts) cast(const char *, (ts) + 1)
  151. #define svalue(o) getstr(tsvalue(o))
  152. typedef union Udata {
  153. L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  154. struct {
  155. CommonHeader;
  156. struct Table *metatable;
  157. size_t len;
  158. } uv;
  159. } Udata;
  160. /*
  161. ** Function Prototypes
  162. */
  163. typedef struct Proto {
  164. CommonHeader;
  165. TValue *k; /* constants used by the function */
  166. Instruction *code;
  167. struct Proto **p; /* functions defined inside the function */
  168. int *lineinfo; /* map from opcodes to source lines */
  169. struct LocVar *locvars; /* information about local variables */
  170. TString **upvalues; /* upvalue names */
  171. TString *source;
  172. int sizeupvalues;
  173. int sizek; /* size of `k' */
  174. int sizecode;
  175. int sizelineinfo;
  176. int sizep; /* size of `p' */
  177. int sizelocvars;
  178. int lineDefined;
  179. GCObject *gclist;
  180. lu_byte nups; /* number of upvalues */
  181. lu_byte numparams;
  182. lu_byte is_vararg;
  183. lu_byte maxstacksize;
  184. } Proto;
  185. typedef struct LocVar {
  186. TString *varname;
  187. int startpc; /* first point where variable is active */
  188. int endpc; /* first point where variable is dead */
  189. } LocVar;
  190. /*
  191. ** Upvalues
  192. */
  193. typedef struct UpVal {
  194. CommonHeader;
  195. GCObject *gclist;
  196. TValue *v; /* points to stack or to its own value */
  197. TValue value; /* the value (when closed) */
  198. } UpVal;
  199. /*
  200. ** Closures
  201. */
  202. #define ClosureHeader \
  203. CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist
  204. typedef struct CClosure {
  205. ClosureHeader;
  206. lua_CFunction f;
  207. TValue upvalue[1];
  208. } CClosure;
  209. typedef struct LClosure {
  210. ClosureHeader;
  211. struct Proto *p;
  212. TValue g; /* global table for this closure */
  213. UpVal *upvals[1];
  214. } LClosure;
  215. typedef union Closure {
  216. CClosure c;
  217. LClosure l;
  218. } Closure;
  219. #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
  220. #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
  221. /*
  222. ** Tables
  223. */
  224. typedef struct Node {
  225. TValue i_key;
  226. TValue i_val;
  227. struct Node *next; /* for chaining */
  228. } Node;
  229. typedef struct Table {
  230. CommonHeader;
  231. lu_byte flags; /* 1<<p means tagmethod(p) is not present */
  232. lu_byte lsizenode; /* log2 of size of `node' array */
  233. struct Table *metatable;
  234. TValue *array; /* array part */
  235. Node *node;
  236. Node *firstfree; /* this position is free; all positions after it are full */
  237. GCObject *gclist;
  238. int sizearray; /* size of `array' array */
  239. } Table;
  240. /*
  241. ** `module' operation for hashing (size is always a power of 2)
  242. */
  243. #define lmod(s,size) \
  244. check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1))))
  245. #define twoto(x) (1<<(x))
  246. #define sizenode(t) (twoto((t)->lsizenode))
  247. extern const TValue luaO_nilobject;
  248. int luaO_log2 (unsigned int x);
  249. int luaO_int2fb (unsigned int x);
  250. #define fb2int(x) (((x) & 7) << ((x) >> 3))
  251. int luaO_rawequalObj (const TValue *t1, const TValue *t2);
  252. int luaO_str2d (const char *s, lua_Number *result);
  253. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp);
  254. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  255. void luaO_chunkid (char *out, const char *source, int len);
  256. #endif