lobject.h 7.5 KB

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