lobject.h 7.7 KB

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