lobject.h 10 KB

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