lobject.h 7.9 KB

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