lobject.h 13 KB

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