lobject.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. ** $Id: lobject.h,v 2.117.1.1 2017/04/19 17:39:34 roberto Exp $
  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 /* function prototypes */
  15. #define LUA_TDEADKEY (LUA_NUMTAGS+1) /* removed keys in tables */
  16. /*
  17. ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
  18. */
  19. #define LUA_TOTALTAGS (LUA_TPROTO + 2)
  20. /*
  21. ** tags for Tagged Values have the following use of bits:
  22. ** bits 0-3: actual tag (a LUA_T* value)
  23. ** bits 4-5: variant bits
  24. ** bit 6: whether value is collectable
  25. */
  26. /*
  27. ** LUA_TFUNCTION variants:
  28. ** 0 - Lua function
  29. ** 1 - light C function
  30. ** 2 - regular C function (closure)
  31. */
  32. /* Variant tags for functions */
  33. #define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */
  34. #define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */
  35. #define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
  36. /* Variant tags for strings */
  37. #define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */
  38. #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
  39. /* Variant tags for numbers */
  40. #define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */
  41. #define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */
  42. /* Bit mark for collectable types */
  43. #define LUA_TTBLRAM (LUA_TTABLE | (0 << 4)) /* RAM based Table */
  44. #define LUA_TTBLROF (LUA_TTABLE | (1 << 4)) /* RO Flash based ROTable */
  45. /* Bit mark for collectable types */
  46. #define BIT_ISCOLLECTABLE (1 << 6)
  47. /* mark a tag as collectable */
  48. #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
  49. /*
  50. ** Byte field access macro. On ESP targets this causes the compiler to emit
  51. ** a l32i + extui instruction pair instead of a single l8ui avoiding a call
  52. ** the S/W unaligned exception handler. This is used to force aligned access
  53. ** to commonly accessed fields in Flash-based record structures. It is not
  54. ** needed for RAM-only structures.
  55. **
  56. ** wo is the offset of aligned word in bytes 0,4,8,..
  57. ** bo is the field within the word in bits 0..31
  58. */
  59. #ifdef LUA_USE_ESP
  60. #define GET_BYTE_FN(name,t,wo,bo) \
  61. static inline lu_int32 get ## name(const void *o) { \
  62. lu_int32 res; /* extract named field */ \
  63. asm ("l32i %0, %1, " #wo "; extui %0, %0, " #bo ", 8;" : "=r"(res) : "r"(o) : );\
  64. return res; }
  65. #else
  66. #define GET_BYTE_FN(name,t,wo,bo) \
  67. static inline lu_byte get ## name(const void *o) { return (cast(const t *,o))->name; }
  68. #endif
  69. /*
  70. ** Common type for all collectable objects
  71. */
  72. typedef struct GCObject GCObject;
  73. /*
  74. ** Common Header for all collectable objects (in macro form, to be
  75. ** included in other objects)
  76. */
  77. #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
  78. /*
  79. ** Common type has only the common header
  80. */
  81. struct GCObject {
  82. CommonHeader;
  83. };
  84. GET_BYTE_FN(tt,GCObject,4,0)
  85. GET_BYTE_FN(marked,GCObject,4,8)
  86. /*
  87. ** Tagged Values. This is the basic representation of values in Lua,
  88. ** an actual value plus a tag with its type.
  89. */
  90. /*
  91. ** Union of all Lua values
  92. */
  93. typedef union Value {
  94. GCObject *gc; /* collectable objects */
  95. void *p; /* light userdata */
  96. int b; /* booleans */
  97. lua_CFunction f; /* light C functions */
  98. lua_Integer i; /* integer numbers */
  99. lua_Number n; /* float numbers */
  100. } Value;
  101. #define TValuefields Value value_; int tt_
  102. typedef struct lua_TValue {
  103. TValuefields;
  104. } TValue;
  105. /* macro defining a nil value */
  106. #define NILCONSTANT {NULL}, LUA_TNIL
  107. #define val_(o) ((o)->value_)
  108. /* raw type tag of a TValue */
  109. #define rttype(o) ((o)->tt_)
  110. /* tag with no variants (bits 0-3) */
  111. #define novariant(x) ((x) & 0x0F)
  112. /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
  113. #define ttype(o) (rttype(o) & 0x3F)
  114. /* type tag of a TValue with no variants (bits 0-3) */
  115. #define ttnov(o) (novariant(rttype(o)))
  116. /* Macros to test type */
  117. #define checktag(o,t) (rttype(o) == (t))
  118. #define checktype(o,t) (ttnov(o) == (t))
  119. #define ttisnumber(o) checktype((o), LUA_TNUMBER)
  120. #define ttisfloat(o) checktag((o), LUA_TNUMFLT)
  121. #define ttisinteger(o) checktag((o), LUA_TNUMINT)
  122. #define ttisnil(o) checktag((o), LUA_TNIL)
  123. #define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
  124. #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
  125. #define ttisstring(o) checktype((o), LUA_TSTRING)
  126. #define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
  127. #define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
  128. #define ttistable(o) checktype((o), LUA_TTABLE)
  129. #define ttisrwtable(o) checktag((o), ctb(LUA_TTBLRAM))
  130. #define ttisrotable(o) checktag((o), ctb(LUA_TTBLROF))
  131. #define ttisfunction(o) checktype(o, LUA_TFUNCTION)
  132. #define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
  133. #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
  134. #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
  135. #define ttislcf(o) checktag((o), LUA_TLCF)
  136. #define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA))
  137. #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
  138. #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)
  139. /* Macros to access values */
  140. #define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
  141. #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
  142. #define nvalue(o) check_exp(ttisnumber(o), \
  143. (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
  144. #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
  145. #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
  146. #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
  147. #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
  148. #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
  149. #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
  150. #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
  151. #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
  152. #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
  153. #define rwhvalue(o) check_exp(ttisrwtable(o), gco2rot(val_(o).gc))
  154. #define rohvalue(o) check_exp(ttisrotable(o), gco2rwt(val_(o).gc))
  155. #define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
  156. #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
  157. /* a dead value may get the 'gc' field, but cannot access its contents */
  158. #define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
  159. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  160. #define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
  161. /* Macros for internal tests */
  162. #define righttt(obj) (ttype(obj) == gettt(gcvalue(obj)))
  163. #define checkliveness(L,obj) \
  164. lua_longassert(!iscollectable(obj) || \
  165. (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))
  166. /* Macros to set values */
  167. #define settt_(o,t) ((o)->tt_=(t))
  168. #define setfltvalue(obj,x) \
  169. { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
  170. #define chgfltvalue(obj,x) \
  171. { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
  172. #define setivalue(obj,x) \
  173. { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
  174. #define chgivalue(obj,x) \
  175. { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
  176. #define setnilvalue(obj) settt_(obj, LUA_TNIL)
  177. #define setfvalue(obj,x) \
  178. { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
  179. #define setpvalue(obj,x) \
  180. { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
  181. #define setbvalue(obj,x) \
  182. { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
  183. #define setgcovalue(L,obj,x) \
  184. { TValue *io = (obj); GCObject *i_g=(x); \
  185. val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
  186. #define setsvalue(L,obj,x) \
  187. { TValue *io = (obj); TString *x_ = (x); \
  188. val_(io).gc = obj2gco(x_); settt_(io, ctb(gettt(x_))); \
  189. checkliveness(L,io); }
  190. #define setuvalue(L,obj,x) \
  191. { TValue *io = (obj); Udata *x_ = (x); \
  192. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
  193. checkliveness(L,io); }
  194. #define setthvalue(L,obj,x) \
  195. { TValue *io = (obj); lua_State *x_ = (x); \
  196. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
  197. checkliveness(L,io); }
  198. #define setclLvalue(L,obj,x) \
  199. { TValue *io = (obj); LClosure *x_ = (x); \
  200. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
  201. checkliveness(L,io); }
  202. #define setclCvalue(L,obj,x) \
  203. { TValue *io = (obj); CClosure *x_ = (x); \
  204. val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
  205. checkliveness(L,io); }
  206. #define sethvalue(L,obj,x) \
  207. { TValue *io = (obj); Table *x_ = (x); \
  208. val_(io).gc = obj2gco(x_); settt_(io, ctb(gettt(x_))); \
  209. checkliveness(L,io); }
  210. #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
  211. #define setobj(L,obj1,obj2) \
  212. { TValue *io1=(obj1); *io1 = *(obj2); \
  213. (void)L; checkliveness(L,io1); }
  214. /*
  215. ** different types of assignments, according to destination
  216. */
  217. /* from stack to (same) stack */
  218. #define setobjs2s setobj
  219. /* to stack (not from same stack) */
  220. #define setobj2s setobj
  221. #define setsvalue2s setsvalue
  222. #define sethvalue2s sethvalue
  223. #define setptvalue2s setptvalue
  224. /* from table to same table */
  225. #define setobjt2t setobj
  226. /* to new object */
  227. #define setobj2n setobj
  228. #define setsvalue2n setsvalue
  229. /* to table (define it as an expression to be used in macros) */
  230. #define setobj2t(L,o1,o2) ((void)L, *(o1)=*(o2), checkliveness(L,(o1)))
  231. /*
  232. ** {======================================================
  233. ** types and prototypes
  234. ** =======================================================
  235. */
  236. typedef TValue *StkId; /* index to stack elements */
  237. /*
  238. ** Header for string value; string bytes follow the end of this structure
  239. ** (aligned according to 'UTString'; see next).
  240. */
  241. typedef struct TString {
  242. CommonHeader;
  243. lu_byte extra; /* reserved words for short strings; "has hash" for longs */
  244. lu_byte shrlen; /* length for short strings */
  245. unsigned int hash;
  246. union {
  247. size_t lnglen; /* length for long strings */
  248. struct TString *hnext; /* linked list for hash table */
  249. } u;
  250. } TString;
  251. GET_BYTE_FN(extra,TString,4,16)
  252. GET_BYTE_FN(shrlen,TString,4,24)
  253. /*
  254. ** Ensures that address after this type is always fully aligned.
  255. */
  256. typedef union UTString {
  257. L_Umaxalign dummy; /* ensures maximum alignment for strings */
  258. TString tsv;
  259. } UTString;
  260. /*
  261. ** Get the actual string (array of bytes) from a 'TString'.
  262. ** (Access to 'extra' ensures that value is really a 'TString'.)
  263. */
  264. #define getstr(ts) \
  265. check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString))
  266. /* get the actual string (array of bytes) from a Lua value */
  267. #define svalue(o) getstr(tsvalue(o))
  268. /* get string length from 'TString *s' */
  269. #define tsslen(s) (gettt(s) == LUA_TSHRSTR ? getshrlen(s) : (s)->u.lnglen)
  270. /* get string length from 'TValue *o' */
  271. #define vslen(o) tsslen(tsvalue(o))
  272. /*
  273. ** Header for userdata; memory area follows the end of this structure
  274. ** (aligned according to 'UUdata'; see next).
  275. */
  276. typedef struct Udata {
  277. CommonHeader;
  278. lu_byte ttuv_; /* user value's tag */
  279. struct Table *metatable;
  280. size_t len; /* number of bytes */
  281. union Value user_; /* user value */
  282. } Udata;
  283. /*
  284. ** Ensures that address after this type is always fully aligned.
  285. */
  286. typedef union UUdata {
  287. L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */
  288. Udata uv;
  289. } UUdata;
  290. /*
  291. ** Get the address of memory block inside 'Udata'.
  292. ** (Access to 'ttuv_' ensures that value is really a 'Udata'.)
  293. */
  294. #define getudatamem(u) \
  295. check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata)))
  296. #define setuservalue(L,u,o) \
  297. { const TValue *io=(o); Udata *iu = (u); \
  298. iu->user_ = io->value_; iu->ttuv_ = rttype(io); \
  299. checkliveness(L,io); }
  300. #define getuservalue(L,u,o) \
  301. { TValue *io=(o); const Udata *iu = (u); \
  302. io->value_ = iu->user_; settt_(io, iu->ttuv_); \
  303. checkliveness(L,io); }
  304. /*
  305. ** Description of an upvalue for function prototypes
  306. */
  307. typedef struct Upvaldesc {
  308. TString *name; /* upvalue name (for debug information) */
  309. lu_byte instack; /* whether it is in stack (register) */
  310. lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
  311. } Upvaldesc;
  312. /*
  313. ** Description of a local variable for function prototypes
  314. ** (used for debug information)
  315. */
  316. typedef struct LocVar {
  317. TString *varname;
  318. int startpc; /* first point where variable is active */
  319. int endpc; /* first point where variable is dead */
  320. } LocVar;
  321. /*
  322. ** Function Prototypes
  323. */
  324. typedef struct Proto {
  325. CommonHeader;
  326. lu_byte numparams; /* number of fixed parameters */
  327. lu_byte is_vararg;
  328. lu_byte maxstacksize; /* number of registers needed by this function */
  329. int sizeupvalues; /* size of 'upvalues' */
  330. int sizek; /* size of 'k' */
  331. int sizecode;
  332. int sizelineinfo;
  333. int sizep; /* size of 'p' */
  334. int sizelocvars;
  335. int linedefined; /* debug information */
  336. int lastlinedefined; /* debug information */
  337. TValue *k; /* constants used by the function */
  338. Instruction *code; /* opcodes */
  339. struct Proto **p; /* functions defined inside the function */
  340. lu_byte *lineinfo; /* packedmap from opcodes to source lines (debug inf) */
  341. LocVar *locvars; /* information about local variables (debug information) */
  342. Upvaldesc *upvalues; /* upvalue information */
  343. TString *source; /* used for debug information */
  344. GCObject *gclist;
  345. } Proto;
  346. GET_BYTE_FN(numparams,Proto,4,16)
  347. GET_BYTE_FN(is_vararg,Proto,4,24)
  348. GET_BYTE_FN(maxstacksize,Proto,8,0)
  349. /*
  350. ** Lua Upvalues
  351. */
  352. typedef struct UpVal UpVal;
  353. /*
  354. ** Closures
  355. */
  356. #define ClosureHeader \
  357. CommonHeader; lu_byte nupvalues; GCObject *gclist
  358. typedef struct CClosure {
  359. ClosureHeader;
  360. lua_CFunction f;
  361. TValue upvalue[1]; /* list of upvalues */
  362. } CClosure;
  363. typedef struct LClosure {
  364. ClosureHeader;
  365. struct Proto *p;
  366. UpVal *upvals[1]; /* list of upvalues */
  367. } LClosure;
  368. typedef union Closure {
  369. CClosure c;
  370. LClosure l;
  371. } Closure;
  372. #define isLfunction(o) ttisLclosure(o)
  373. #define getproto(o) (clLvalue(o)->p)
  374. /*
  375. ** Common Table fields for both table versions (like CommonHeader in
  376. ** macro form, to be included in table structure definitions).
  377. **
  378. ** Note that the sethvalue() macro works much like the setsvalue()
  379. ** macro and handles the abstracted type. the hvalue(o) macro can be
  380. ** used to access CommonTable fields, but the rwhvalue(o) and
  381. ** rohvalue(o) value variants must be used if accessing variant-specfic
  382. ** fields
  383. */
  384. #define CommonTable CommonHeader; \
  385. lu_byte flags; lu_byte lsizenode; struct Table *metatable;
  386. /*
  387. ** Tables
  388. */
  389. typedef union TKey {
  390. struct {
  391. TValuefields;
  392. int next; /* for chaining (offset for next node) */
  393. } nk;
  394. TValue tvk;
  395. } TKey;
  396. /* copy a value into a key without messing up field 'next' */
  397. #define setnodekey(L,key,obj) \
  398. { TKey *k_=(key); const TValue *io_=(obj); \
  399. k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \
  400. (void)L; checkliveness(L,io_); }
  401. typedef struct Node {
  402. TValue i_val;
  403. TKey i_key;
  404. } Node;
  405. typedef struct Table {
  406. /* flags & 1<<p means tagmethod(p) is not present */
  407. /* lsizenode = log2 of size of 'node' array */
  408. CommonTable;
  409. unsigned int sizearray; /* size of 'array' array */
  410. TValue *array; /* array part */
  411. Node *node;
  412. Node *lastfree; /* any free position is before this position */
  413. GCObject *gclist;
  414. } Table;
  415. GET_BYTE_FN(flags,Table,4,16)
  416. GET_BYTE_FN(lsizenode,Table,4,24)
  417. typedef const struct ROTable_entry {
  418. const char *key;
  419. const TValue value;
  420. } ROTable_entry;
  421. typedef struct ROTable {
  422. /* next always has the value (GCObject *)((size_t) 1); */
  423. /* flags & 1<<p means tagmethod(p) is not present */
  424. /* lsizenode is the number of ROTable entries */
  425. /* Like TStrings, the ROTable_entry vector follows the ROTable */
  426. CommonTable;
  427. ROTable_entry *entry;
  428. } ROTable;
  429. /*
  430. ** 'module' operation for hashing (size is always a power of 2)
  431. */
  432. #define lmod(s,size) \
  433. (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
  434. #define twoto(x) (1<<(x))
  435. #define sizenode(t) (twoto((t)->lsizenode))
  436. /*
  437. ** (address of) a fixed nil value
  438. */
  439. #define luaO_nilobject (&luaO_nilobject_)
  440. LUAI_DDEC const TValue luaO_nilobject_;
  441. /* size of buffer for 'luaO_utf8esc' function */
  442. #define UTF8BUFFSZ 8
  443. LUAI_FUNC int luaO_int2fb (unsigned int x);
  444. LUAI_FUNC int luaO_fb2int (int x);
  445. LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
  446. LUAI_FUNC int luaO_ceillog2 (unsigned int x);
  447. LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
  448. const TValue *p2, TValue *res);
  449. LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
  450. LUAI_FUNC int luaO_hexavalue (int c);
  451. LUAI_FUNC void luaO_tostring (lua_State *L, StkId obj);
  452. LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
  453. va_list argp);
  454. LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  455. LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
  456. #endif