lobject.h 7.7 KB

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