lobject.h 11 KB

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