lobject.h 8.9 KB

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