lobject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. ** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 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. /* tags for values visible from Lua */
  12. #define LAST_TAG LUA_TTHREAD
  13. #define NUM_TAGS (LAST_TAG+1)
  14. #define READONLYMASK (1<<7) /* denormalised bitmask for READONLYBIT and */
  15. #define LFSMASK (1<<6) /* LFSBIT to avoid include proliferation */
  16. /*
  17. ** Extra tags for non-values
  18. */
  19. #define LUA_TPROTO (LAST_TAG+1)
  20. #define LUA_TUPVAL (LAST_TAG+2)
  21. #define LUA_TDEADKEY (LAST_TAG+3)
  22. #ifdef LUA_USE_ESP
  23. /*
  24. ** force aligned access to critical fields in Flash-based structures
  25. ** wo is the offset of aligned word in bytes 0,4,8,..
  26. ** bo is the field within the word in bits 0..31
  27. **
  28. ** Note that this returns a lu_int32 as returning a byte can cause the
  29. ** gcc code generator to emit an extra extui instruction.
  30. */
  31. #define GET_BYTE_FN(name,t,wo,bo) \
  32. static inline lu_int32 get ## name(void *o) { \
  33. lu_int32 res; /* extract named field */ \
  34. asm ("l32i %0, %1, " #wo "; extui %0, %0, " #bo ", 8;" : "=r"(res) : "r"(o) : );\
  35. return res; }
  36. #else
  37. #define GET_BYTE_FN(name,t,wo,bo) \
  38. static inline lu_byte get ## name(void *o) { return ((t *)o)->name; }
  39. #endif
  40. /*
  41. ** Union of all collectable objects
  42. */
  43. typedef union GCObject GCObject;
  44. /*
  45. ** Common Header for all collectable objects (in macro form, to be
  46. ** included in other objects)
  47. */
  48. #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
  49. /*
  50. ** Common header in struct form
  51. */
  52. typedef struct GCheader {
  53. CommonHeader;
  54. } GCheader;
  55. /*
  56. ** Word aligned inline access functions for the CommonHeader tt and marked fields.
  57. ** Note that these MUST be consistent with the CommonHeader definition above. Arg
  58. ** 3 is a word offset (4 bytes in this case) and arg 4 the bit offset in the word.
  59. */
  60. GET_BYTE_FN(tt,GCheader,4,0)
  61. GET_BYTE_FN(marked,GCheader,4,8)
  62. #if defined(LUA_PACK_VALUE) || defined(ELUA_ENDIAN_BIG) || defined(ELUA_ENDIAN_SMALL)
  63. # error "NodeMCU does not support the eLua LUA_PACK_VALUE and ELUA_ENDIAN defines"
  64. #endif
  65. /*
  66. ** Union of all Lua values
  67. */
  68. typedef union {
  69. GCObject *gc;
  70. void *p;
  71. lua_Number n;
  72. int b;
  73. } Value;
  74. /*
  75. ** Tagged Values
  76. */
  77. #define TValuefields Value value; int tt
  78. #define LUA_TVALUE_NIL {NULL}, LUA_TNIL
  79. #ifdef LUA_USE_ESP
  80. # pragma pack(4)
  81. #endif
  82. typedef struct lua_TValue {
  83. TValuefields;
  84. } TValue;
  85. #ifdef LUA_USE_ESP
  86. # pragma pack()
  87. #endif
  88. /* Macros to test type */
  89. #define ttisnil(o) (ttype(o) == LUA_TNIL)
  90. #define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
  91. #define ttisstring(o) (ttype(o) == LUA_TSTRING)
  92. #define ttistable(o) (basettype(o) == LUA_TTABLE)
  93. #define ttisrwtable(o) (type(o) == LUA_TTABLE)
  94. #define ttisrotable(o) (ttype(o) & LUA_TISROTABLE)
  95. #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
  96. #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
  97. #define ttisthread(o) (ttype(o) == LUA_TTHREAD)
  98. #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
  99. #define ttislightfunction(o) (ttype(o) == LUA_TLIGHTFUNCTION)
  100. #define ttisclfunction(o) (ttype(o) == LUA_TFUNCTION)
  101. #define ttisfunction(o) (basettype(o) == LUA_TFUNCTION)
  102. /* Macros to access values */
  103. #define ttype(o) ((void) (o)->value, (o)->tt)
  104. #define basettype(o) ((void) (o)->value, ((o)->tt & LUA_TMASK))
  105. #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
  106. #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
  107. #define fvalue(o) check_exp(ttislightfunction(o), (o)->value.p)
  108. #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
  109. #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
  110. #define tsvalue(o) (&rawtsvalue(o)->tsv)
  111. #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
  112. #define uvalue(o) (&rawuvalue(o)->uv)
  113. #define clvalue(o) check_exp(ttisclfunction(o), &(o)->value.gc->cl)
  114. #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
  115. #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
  116. #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
  117. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  118. /*
  119. ** for internal debug only
  120. */
  121. #define checkconsistency(obj) \
  122. lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
  123. #define checkliveness(g,obj) \
  124. lua_assert(!iscollectable(obj) || \
  125. ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
  126. /* Macros to set values */
  127. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  128. #define setnvalue(obj,x) \
  129. { lua_Number i_x = (x); TValue *i_o=(obj); i_o->value.n=i_x; i_o->tt=LUA_TNUMBER; }
  130. #define setpvalue(obj,x) \
  131. { void *i_x = (x); TValue *i_o=(obj); i_o->value.p=i_x; i_o->tt=LUA_TLIGHTUSERDATA; }
  132. #define setfvalue(obj,x) \
  133. { void *i_x = (x); TValue *i_o=(obj); i_o->value.p=i_x; i_o->tt=LUA_TLIGHTFUNCTION; }
  134. #define setbvalue(obj,x) \
  135. { int i_x = (x); TValue *i_o=(obj); i_o->value.b=i_x; i_o->tt=LUA_TBOOLEAN; }
  136. #define setsvalue(L,obj,x) \
  137. { GCObject *i_x = cast(GCObject *, (x)); \
  138. TValue *i_o=(obj); \
  139. i_o->value.gc=i_x; i_o->tt=LUA_TSTRING; \
  140. checkliveness(G(L),i_o); }
  141. #define setuvalue(L,obj,x) \
  142. { GCObject *i_x = cast(GCObject *, (x)); \
  143. TValue *i_o=(obj); \
  144. i_o->value.gc=i_x; i_o->tt=LUA_TUSERDATA; \
  145. checkliveness(G(L),i_o); }
  146. #define setthvalue(L,obj,x) \
  147. { GCObject *i_x = cast(GCObject *, (x)); \
  148. TValue *i_o=(obj); \
  149. i_o->value.gc=i_x; i_o->tt=LUA_TTHREAD; \
  150. checkliveness(G(L),i_o); }
  151. #define setclvalue(L,obj,x) \
  152. { GCObject *i_x = cast(GCObject *, (x)); \
  153. TValue *i_o=(obj); \
  154. i_o->value.gc=i_x; i_o->tt=LUA_TFUNCTION; \
  155. checkliveness(G(L),i_o); }
  156. #define sethvalue(L,obj,x) \
  157. { GCObject *i_x = cast(GCObject *, (x)); \
  158. TValue *i_o=(obj); \
  159. i_o->value.gc=i_x; i_o->tt=gettt(x); \
  160. checkliveness(G(L),i_o); }
  161. #define setptvalue(L,obj,x) \
  162. { GCObject *i_x = cast(GCObject *, (x)); \
  163. TValue *i_o=(obj); \
  164. i_o->value.gc=i_x; i_o->tt=LUA_TPROTO; \
  165. checkliveness(G(L),i_o); }
  166. #define setobj(L,obj1,obj2) \
  167. { const TValue *o2=(obj2); TValue *o1=(obj1); \
  168. o1->value = o2->value; o1->tt=o2->tt; \
  169. checkliveness(G(L),o1); }
  170. /*
  171. ** different types of sets, according to destination
  172. */
  173. /* from stack to (same) stack */
  174. #define setobjs2s setobj
  175. /* to stack (not from same stack) */
  176. #define setobj2s setobj
  177. #define setsvalue2s setsvalue
  178. #define sethvalue2s sethvalue
  179. #define setptvalue2s setptvalue
  180. /* from table to same table */
  181. #define setobjt2t setobj
  182. /* to table */
  183. #define setobj2t setobj
  184. /* to new object */
  185. #define setobj2n setobj
  186. #define setsvalue2n setsvalue
  187. #define setttype(obj, stt) ((void) (obj)->value, (obj)->tt = (stt))
  188. #define iscollectable(o) (ttype(o) >= LUA_TSTRING && ttype(o) <= LUA_TMASK)
  189. typedef TValue *StkId; /* index to stack elements */
  190. /*
  191. ** String headers for string table
  192. */
  193. typedef union TString {
  194. L_Umaxalign dummy; /* ensures maximum alignment for strings */
  195. struct {
  196. CommonHeader;
  197. unsigned int hash;
  198. size_t len;
  199. } tsv;
  200. } TString;
  201. #define getstr(ts) cast(const char *, (ts) + 1)
  202. #define svalue(o) getstr(rawtsvalue(o))
  203. typedef union Udata {
  204. L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  205. struct {
  206. CommonHeader;
  207. struct Table *metatable;
  208. struct Table *env;
  209. size_t len;
  210. } uv;
  211. } Udata;
  212. #ifdef LUA_CROSS_COMPILER
  213. #define isreadonly(o) (0)
  214. #else
  215. #define isreadonly(o) (getmarked(o) & READONLYMASK)
  216. #endif
  217. #define islfs(o) (getmarked(o) & LFSMASK)
  218. /*
  219. ** Function Prototypes
  220. */
  221. typedef struct Proto {
  222. CommonHeader;
  223. TValue *k; /* constants used by the function */
  224. Instruction *code;
  225. struct Proto **p; /* functions defined inside the function */
  226. #ifdef LUA_OPTIMIZE_DEBUG
  227. unsigned char *packedlineinfo;
  228. #else
  229. int *lineinfo; /* map from opcodes to source lines */
  230. #endif
  231. struct LocVar *locvars; /* information about local variables */
  232. TString **upvalues; /* upvalue names */
  233. TString *source;
  234. int sizeupvalues;
  235. int sizek; /* size of `k' */
  236. int sizecode;
  237. #ifndef LUA_OPTIMIZE_DEBUG
  238. int sizelineinfo;
  239. #endif
  240. int sizep; /* size of `p' */
  241. int sizelocvars;
  242. int linedefined;
  243. int lastlinedefined;
  244. GCObject *gclist;
  245. lu_byte nups; /* number of upvalues */
  246. lu_byte numparams;
  247. lu_byte is_vararg;
  248. lu_byte maxstacksize;
  249. } Proto;
  250. /* masks for new-style vararg */
  251. #define VARARG_HASARG 1
  252. #define VARARG_ISVARARG 2
  253. #define VARARG_NEEDSARG 4
  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. ** Upvalues
  261. */
  262. typedef struct UpVal {
  263. CommonHeader;
  264. TValue *v; /* points to stack or to its own value */
  265. union {
  266. TValue value; /* the value (when closed) */
  267. struct { /* double linked list (when open) */
  268. struct UpVal *prev;
  269. struct UpVal *next;
  270. } l;
  271. } u;
  272. } UpVal;
  273. /*
  274. ** Closures
  275. */
  276. #define ClosureHeader \
  277. CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
  278. struct Table *env
  279. typedef struct CClosure {
  280. ClosureHeader;
  281. lua_CFunction f;
  282. TValue upvalue[1];
  283. } CClosure;
  284. typedef struct LClosure {
  285. ClosureHeader;
  286. struct Proto *p;
  287. UpVal *upvals[1];
  288. } LClosure;
  289. typedef union Closure {
  290. CClosure c;
  291. LClosure l;
  292. } Closure;
  293. #define iscfunction(o) ((ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)||(ttype(o)==LUA_TLIGHTFUNCTION))
  294. #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
  295. /*
  296. ** Tables
  297. */
  298. /*
  299. ** Common Table fields for both table versions (like CommonHeader in
  300. ** macro form, to be included in table structure definitions).
  301. **
  302. ** Note that the sethvalue() macro works much like the setsvalue()
  303. ** macro and handles the abstracted type. the hvalue(o) macro can be
  304. ** used to access CommonTable fields and rw Table fields
  305. */
  306. #define CommonTable CommonHeader; \
  307. lu_byte flags; lu_byte lsizenode; struct Table *metatable
  308. typedef union TKey {
  309. struct {
  310. TValuefields;
  311. struct Node *next; /* for chaining */
  312. } nk;
  313. TValue tvk;
  314. } TKey;
  315. #define LUA_TKEY_NIL {LUA_TVALUE_NIL, NULL}
  316. typedef struct Node {
  317. TValue i_val;
  318. TKey i_key;
  319. } Node;
  320. typedef struct Table {
  321. CommonTable;
  322. TValue *array; /* array part */
  323. Node *node;
  324. Node *lastfree; /* any free position is before this position */
  325. GCObject *gclist;
  326. int sizearray; /* size of `array' array */
  327. } Table;
  328. GET_BYTE_FN(flags,Table,4,16)
  329. GET_BYTE_FN(lsizenode,Table,4,24)
  330. typedef const struct ROTable_entry {
  331. const char *key;
  332. const TValue value;
  333. } ROTable_entry;
  334. typedef struct ROTable {
  335. /* next always has the value (GCObject *)((size_t) 1); */
  336. /* flags & 1<<p means tagmethod(p) is not present */
  337. /* lsizenode is unused */
  338. /* Like TStrings, the ROTable_entry vector follows the ROTable */
  339. CommonTable;
  340. ROTable_entry *entry;
  341. } ROTable;
  342. /*
  343. ** `module' operation for hashing (size is always a power of 2)
  344. */
  345. #define lmod(s,size) \
  346. (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
  347. #define twoto(x) (1<<(x))
  348. #define sizenode(t) (twoto((t)->lsizenode))
  349. #define luaO_nilobject (&luaO_nilobject_)
  350. LUAI_DATA const TValue luaO_nilobject_;
  351. #define ceillog2(x) (luaO_log2((x)-1) + 1)
  352. LUAI_FUNC int luaO_log2 (unsigned int x);
  353. LUAI_FUNC int luaO_int2fb (unsigned int x);
  354. LUAI_FUNC int luaO_fb2int (int x);
  355. LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
  356. LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
  357. LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
  358. va_list argp);
  359. LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  360. LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
  361. #endif