lobject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 __XTENSA__
  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. #define GET_BYTE_FN(name,t,wo,bo) \
  29. static inline lu_byte get ## name(void *o) { \
  30. lu_byte res; /* extract named field */ \
  31. asm ("l32i %0, %1, " #wo "; extui %0, %0, " #bo ", 8;" : "=r"(res) : "r"(o) : );\
  32. return res; }
  33. #else
  34. #define GET_BYTE_FN(name,t,wo,bo) \
  35. static inline lu_byte get ## name(void *o) { return ((t *)o)->name; }
  36. #endif
  37. /*
  38. ** Union of all collectable objects
  39. */
  40. typedef union GCObject GCObject;
  41. /*
  42. ** Common Header for all collectable objects (in macro form, to be
  43. ** included in other objects)
  44. */
  45. #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
  46. /*
  47. ** Common header in struct form
  48. */
  49. typedef struct GCheader {
  50. CommonHeader;
  51. } GCheader;
  52. /*
  53. ** Word aligned inline access functions for the CommonHeader tt and marked fields.
  54. ** Note that these MUST be consistent with the CommonHeader definition above. Arg
  55. ** 3 is a word offset (4 bytes in this case) and arg 4 the bit offset in the word.
  56. */
  57. GET_BYTE_FN(tt,GCheader,4,0)
  58. GET_BYTE_FN(marked,GCheader,4,8)
  59. #if defined(LUA_PACK_VALUE) || defined(ELUA_ENDIAN_BIG) || defined(ELUA_ENDIAN_SMALL)
  60. # error "NodeMCU does not support the eLua LUA_PACK_VALUE and ELUA_ENDIAN defines"
  61. #endif
  62. /*
  63. ** Union of all Lua values
  64. */
  65. typedef union {
  66. GCObject *gc;
  67. void *p;
  68. lua_Number n;
  69. int b;
  70. } Value;
  71. /*
  72. ** Tagged Values
  73. */
  74. #define TValuefields Value value; int tt
  75. #define LUA_TVALUE_NIL {NULL}, LUA_TNIL
  76. #if defined(LUA_PACK_TVALUES) && !defined(LUA_CROSS_COMPILER)
  77. #pragma pack(4)
  78. #endif
  79. typedef struct lua_TValue {
  80. TValuefields;
  81. } TValue;
  82. #if defined(LUA_PACK_TVALUES) && !defined(LUA_CROSS_COMPILER)
  83. #pragma pack()
  84. #endif
  85. /* Macros to test type */
  86. #define ttisnil(o) (ttype(o) == LUA_TNIL)
  87. #define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
  88. #define ttisstring(o) (ttype(o) == LUA_TSTRING)
  89. #define ttistable(o) (ttype(o) == LUA_TTABLE)
  90. #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
  91. #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
  92. #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
  93. #define ttisthread(o) (ttype(o) == LUA_TTHREAD)
  94. #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
  95. #define ttisrotable(o) (ttype(o) == LUA_TROTABLE)
  96. #define ttislightfunction(o) (ttype(o) == LUA_TLIGHTFUNCTION)
  97. /* Macros to access values */
  98. #define ttype(o) ((void) (o)->value, (o)->tt)
  99. #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
  100. #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
  101. #define rvalue(o) check_exp(ttisrotable(o), (o)->value.p)
  102. #define fvalue(o) check_exp(ttislightfunction(o), (o)->value.p)
  103. #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
  104. #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
  105. #define tsvalue(o) (&rawtsvalue(o)->tsv)
  106. #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
  107. #define uvalue(o) (&rawuvalue(o)->uv)
  108. #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
  109. #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
  110. #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
  111. #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
  112. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  113. /*
  114. ** for internal debug only
  115. */
  116. #define checkconsistency(obj) \
  117. lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
  118. #define checkliveness(g,obj) \
  119. lua_assert(!iscollectable(obj) || \
  120. ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
  121. /* Macros to set values */
  122. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  123. #define setnvalue(obj,x) \
  124. { lua_Number i_x = (x); TValue *i_o=(obj); i_o->value.n=i_x; i_o->tt=LUA_TNUMBER; }
  125. #define setpvalue(obj,x) \
  126. { void *i_x = (x); TValue *i_o=(obj); i_o->value.p=i_x; i_o->tt=LUA_TLIGHTUSERDATA; }
  127. #define setrvalue(obj,x) \
  128. { void *i_x = (x); TValue *i_o=(obj); i_o->value.p=i_x; i_o->tt=LUA_TROTABLE; }
  129. #define setfvalue(obj,x) \
  130. { void *i_x = (x); TValue *i_o=(obj); i_o->value.p=i_x; i_o->tt=LUA_TLIGHTFUNCTION; }
  131. #define setbvalue(obj,x) \
  132. { int i_x = (x); TValue *i_o=(obj); i_o->value.b=i_x; i_o->tt=LUA_TBOOLEAN; }
  133. #define setsvalue(L,obj,x) \
  134. { GCObject *i_x = cast(GCObject *, (x)); \
  135. TValue *i_o=(obj); \
  136. i_o->value.gc=i_x; i_o->tt=LUA_TSTRING; \
  137. checkliveness(G(L),i_o); }
  138. #define setuvalue(L,obj,x) \
  139. { GCObject *i_x = cast(GCObject *, (x)); \
  140. TValue *i_o=(obj); \
  141. i_o->value.gc=i_x; i_o->tt=LUA_TUSERDATA; \
  142. checkliveness(G(L),i_o); }
  143. #define setthvalue(L,obj,x) \
  144. { GCObject *i_x = cast(GCObject *, (x)); \
  145. TValue *i_o=(obj); \
  146. i_o->value.gc=i_x; i_o->tt=LUA_TTHREAD; \
  147. checkliveness(G(L),i_o); }
  148. #define setclvalue(L,obj,x) \
  149. { GCObject *i_x = cast(GCObject *, (x)); \
  150. TValue *i_o=(obj); \
  151. i_o->value.gc=i_x; i_o->tt=LUA_TFUNCTION; \
  152. checkliveness(G(L),i_o); }
  153. #define sethvalue(L,obj,x) \
  154. { GCObject *i_x = cast(GCObject *, (x)); \
  155. TValue *i_o=(obj); \
  156. i_o->value.gc=i_x; i_o->tt=LUA_TTABLE; \
  157. checkliveness(G(L),i_o); }
  158. #define setptvalue(L,obj,x) \
  159. { GCObject *i_x = cast(GCObject *, (x)); \
  160. TValue *i_o=(obj); \
  161. i_o->value.gc=i_x; i_o->tt=LUA_TPROTO; \
  162. checkliveness(G(L),i_o); }
  163. #define setobj(L,obj1,obj2) \
  164. { const TValue *o2=(obj2); TValue *o1=(obj1); \
  165. o1->value = o2->value; o1->tt=o2->tt; \
  166. checkliveness(G(L),o1); }
  167. /*
  168. ** different types of sets, according to destination
  169. */
  170. /* from stack to (same) stack */
  171. #define setobjs2s setobj
  172. /* to stack (not from same stack) */
  173. #define setobj2s setobj
  174. #define setsvalue2s setsvalue
  175. #define sethvalue2s sethvalue
  176. #define setptvalue2s setptvalue
  177. /* from table to same table */
  178. #define setobjt2t setobj
  179. /* to table */
  180. #define setobj2t setobj
  181. /* to new object */
  182. #define setobj2n setobj
  183. #define setsvalue2n setsvalue
  184. #define setttype(obj, stt) ((void) (obj)->value, (obj)->tt = (stt))
  185. #define iscollectable(o) (ttype(o) >= LUA_TSTRING)
  186. typedef TValue *StkId; /* index to stack elements */
  187. /*
  188. ** String headers for string table
  189. */
  190. typedef union TString {
  191. L_Umaxalign dummy; /* ensures maximum alignment for strings */
  192. struct {
  193. CommonHeader;
  194. unsigned int hash;
  195. size_t len;
  196. } tsv;
  197. } TString;
  198. #ifdef LUA_CROSS_COMPILER
  199. #define isreadonly(o) (0)
  200. #else
  201. #define isreadonly(o) ((o).marked & READONLYMASK)
  202. #endif
  203. #define ts_isreadonly(ts) isreadonly((ts)->tsv)
  204. #define getstr(ts) (ts_isreadonly(ts) ? \
  205. cast(const char *, *(const char**)((ts) + 1)) : \
  206. cast(const char *, (ts) + 1))
  207. #define svalue(o) getstr(rawtsvalue(o))
  208. typedef union Udata {
  209. L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  210. struct {
  211. CommonHeader;
  212. struct Table *metatable;
  213. struct Table *env;
  214. size_t len;
  215. } uv;
  216. } Udata;
  217. /*
  218. ** Function Prototypes
  219. */
  220. typedef struct Proto {
  221. CommonHeader;
  222. TValue *k; /* constants used by the function */
  223. Instruction *code;
  224. struct Proto **p; /* functions defined inside the function */
  225. #ifdef LUA_OPTIMIZE_DEBUG
  226. unsigned char *packedlineinfo;
  227. #else
  228. int *lineinfo; /* map from opcodes to source lines */
  229. #endif
  230. struct LocVar *locvars; /* information about local variables */
  231. TString **upvalues; /* upvalue names */
  232. TString *source;
  233. int sizeupvalues;
  234. int sizek; /* size of `k' */
  235. int sizecode;
  236. #ifndef LUA_OPTIMIZE_DEBUG
  237. int sizelineinfo;
  238. #endif
  239. int sizep; /* size of `p' */
  240. int sizelocvars;
  241. int linedefined;
  242. int lastlinedefined;
  243. GCObject *gclist;
  244. lu_byte nups; /* number of upvalues */
  245. lu_byte numparams;
  246. lu_byte is_vararg;
  247. lu_byte maxstacksize;
  248. } Proto;
  249. #define proto_isreadonly(p) isreadonly(*(p))
  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. typedef union TKey {
  299. struct {
  300. TValuefields;
  301. struct Node *next; /* for chaining */
  302. } nk;
  303. TValue tvk;
  304. } TKey;
  305. #define LUA_TKEY_NIL {LUA_TVALUE_NIL, NULL}
  306. typedef struct Node {
  307. TValue i_val;
  308. TKey i_key;
  309. } Node;
  310. typedef struct Table {
  311. CommonHeader;
  312. lu_byte flags; /* 1<<p means tagmethod(p) is not present */
  313. lu_byte lsizenode; /* log2 of size of `node' array */
  314. struct Table *metatable;
  315. TValue *array; /* array part */
  316. Node *node;
  317. Node *lastfree; /* any free position is before this position */
  318. GCObject *gclist;
  319. int sizearray; /* size of `array' array */
  320. } Table;
  321. typedef const struct luaR_entry ROTable;
  322. /*
  323. ** `module' operation for hashing (size is always a power of 2)
  324. */
  325. #define lmod(s,size) \
  326. (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
  327. #define twoto(x) (1<<(x))
  328. #define sizenode(t) (twoto((t)->lsizenode))
  329. #define luaO_nilobject (&luaO_nilobject_)
  330. LUAI_DATA const TValue luaO_nilobject_;
  331. #define ceillog2(x) (luaO_log2((x)-1) + 1)
  332. LUAI_FUNC int luaO_log2 (unsigned int x);
  333. LUAI_FUNC int luaO_int2fb (unsigned int x);
  334. LUAI_FUNC int luaO_fb2int (int x);
  335. LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
  336. LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
  337. LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
  338. va_list argp);
  339. LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  340. LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
  341. #endif