lobject.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. ** $Id: lobject.h,v 2.41 2010/06/04 13:25:10 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 setsvalue(L,obj,x) \
  110. { TValue *i_o=(obj); \
  111. i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TSTRING; \
  112. checkliveness(G(L),i_o); }
  113. #define setuvalue(L,obj,x) \
  114. { TValue *i_o=(obj); \
  115. i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TUSERDATA; \
  116. checkliveness(G(L),i_o); }
  117. #define setthvalue(L,obj,x) \
  118. { TValue *i_o=(obj); \
  119. i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TTHREAD; \
  120. checkliveness(G(L),i_o); }
  121. #define setclvalue(L,obj,x) \
  122. { TValue *i_o=(obj); \
  123. i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TFUNCTION; \
  124. checkliveness(G(L),i_o); }
  125. #define sethvalue(L,obj,x) \
  126. { TValue *i_o=(obj); \
  127. i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TTABLE; \
  128. checkliveness(G(L),i_o); }
  129. #define setptvalue(L,obj,x) \
  130. { TValue *i_o=(obj); \
  131. i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TPROTO; \
  132. checkliveness(G(L),i_o); }
  133. #define setdeadvalue(obj) ((obj)->tt_=LUA_TDEADKEY)
  134. #define setobj(L,obj1,obj2) \
  135. { const TValue *o2=(obj2); TValue *o1=(obj1); \
  136. o1->value_ = o2->value_; o1->tt_=o2->tt_; \
  137. checkliveness(G(L),o1); }
  138. /*
  139. ** different types of assignments, according to destination
  140. */
  141. /* from stack to (same) stack */
  142. #define setobjs2s setobj
  143. /* to stack (not from same stack) */
  144. #define setobj2s setobj
  145. #define setsvalue2s setsvalue
  146. #define sethvalue2s sethvalue
  147. #define setptvalue2s setptvalue
  148. /* from table to same table */
  149. #define setobjt2t setobj
  150. /* to table */
  151. #define setobj2t setobj
  152. /* to new object */
  153. #define setobj2n setobj
  154. #define setsvalue2n setsvalue
  155. typedef TValue *StkId; /* index to stack elements */
  156. /*
  157. ** Header for string value; string bytes follow the end of this structure
  158. */
  159. typedef union TString {
  160. L_Umaxalign dummy; /* ensures maximum alignment for strings */
  161. struct {
  162. CommonHeader;
  163. lu_byte reserved;
  164. unsigned int hash;
  165. size_t len;
  166. } tsv;
  167. } TString;
  168. /* get the actual string (array of bytes) from a TString */
  169. #define getstr(ts) cast(const char *, (ts) + 1)
  170. /* get the actual string (array of bytes) from a Lua value */
  171. #define svalue(o) getstr(rawtsvalue(o))
  172. /*
  173. ** Header for userdata; memory area follows the end of this structure
  174. */
  175. typedef union Udata {
  176. L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  177. struct {
  178. CommonHeader;
  179. struct Table *metatable;
  180. struct Table *env;
  181. size_t len;
  182. } uv;
  183. } Udata;
  184. /*
  185. ** Description of an upvalue for function prototypes
  186. */
  187. typedef struct Upvaldesc {
  188. TString *name; /* upvalue name (for debug information) */
  189. lu_byte instack; /* whether it is in stack */
  190. lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
  191. } Upvaldesc;
  192. /*
  193. ** Description of a local variable for function prototypes
  194. ** (used for debug information)
  195. */
  196. typedef struct LocVar {
  197. TString *varname;
  198. int startpc; /* first point where variable is active */
  199. int endpc; /* first point where variable is dead */
  200. } LocVar;
  201. /*
  202. ** Function Prototypes
  203. */
  204. typedef struct Proto {
  205. CommonHeader;
  206. TValue *k; /* constants used by the function */
  207. Instruction *code;
  208. struct Proto **p; /* functions defined inside the function */
  209. int *lineinfo; /* map from opcodes to source lines */
  210. LocVar *locvars; /* information about local variables */
  211. Upvaldesc *upvalues; /* upvalue information */
  212. union Closure *cache; /* last created closure with this prototype */
  213. TString *source;
  214. int sizeupvalues; /* size of 'upvalues' */
  215. int sizek; /* size of `k' */
  216. int sizecode;
  217. int sizelineinfo;
  218. int sizep; /* size of `p' */
  219. int sizelocvars;
  220. int linedefined;
  221. int lastlinedefined;
  222. GCObject *gclist;
  223. lu_byte numparams; /* number of fixed parameters */
  224. lu_byte is_vararg;
  225. lu_byte maxstacksize; /* maximum stack used by this function */
  226. } Proto;
  227. /*
  228. ** Lua Upvalues
  229. */
  230. typedef struct UpVal {
  231. CommonHeader;
  232. TValue *v; /* points to stack or to its own value */
  233. union {
  234. TValue value; /* the value (when closed) */
  235. struct { /* double linked list (when open) */
  236. struct UpVal *prev;
  237. struct UpVal *next;
  238. } l;
  239. } u;
  240. } UpVal;
  241. /*
  242. ** Closures
  243. */
  244. #define ClosureHeader \
  245. CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist
  246. typedef struct CClosure {
  247. ClosureHeader;
  248. lua_CFunction f;
  249. TValue upvalue[1]; /* list of upvalues */
  250. } CClosure;
  251. typedef struct LClosure {
  252. ClosureHeader;
  253. struct Proto *p;
  254. UpVal *upvals[1]; /* list of upvalues */
  255. } LClosure;
  256. typedef union Closure {
  257. CClosure c;
  258. LClosure l;
  259. } Closure;
  260. #define isLfunction(o) (ttisclosure(o) && !clvalue(o)->c.isC)
  261. #define getproto(o) (clvalue(o)->l.p)
  262. /*
  263. ** Tables
  264. */
  265. typedef union TKey {
  266. struct {
  267. TValuefields;
  268. struct Node *next; /* for chaining */
  269. } nk;
  270. TValue tvk;
  271. } TKey;
  272. typedef struct Node {
  273. TValue i_val;
  274. TKey i_key;
  275. } Node;
  276. typedef struct Table {
  277. CommonHeader;
  278. lu_byte flags; /* 1<<p means tagmethod(p) is not present */
  279. lu_byte lsizenode; /* log2 of size of `node' array */
  280. struct Table *metatable;
  281. TValue *array; /* array part */
  282. Node *node;
  283. Node *lastfree; /* any free position is before this position */
  284. GCObject *gclist;
  285. int sizearray; /* size of `array' array */
  286. } Table;
  287. /*
  288. ** `module' operation for hashing (size is always a power of 2)
  289. */
  290. #define lmod(s,size) \
  291. (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
  292. #define twoto(x) (1<<(x))
  293. #define sizenode(t) (twoto((t)->lsizenode))
  294. /*
  295. ** (address of) a fixed nil value
  296. */
  297. #define luaO_nilobject (&luaO_nilobject_)
  298. LUAI_DDEC const TValue luaO_nilobject_;
  299. LUAI_FUNC int luaO_int2fb (unsigned int x);
  300. LUAI_FUNC int luaO_fb2int (int x);
  301. LUAI_FUNC int luaO_ceillog2 (lu_int32 x);
  302. LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
  303. LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
  304. LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
  305. LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
  306. va_list argp);
  307. LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  308. LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
  309. #endif