lobject.h 10 KB

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