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. #define ttisanyfunction(o) (ttisfunction(o) || ttislightfunction(o))
  98. /* Macros to access values */
  99. #define ttype(o) ((void) (o)->value, (o)->tt)
  100. #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
  101. #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
  102. #define rvalue(o) check_exp(ttisrotable(o), (o)->value.p)
  103. #define fvalue(o) check_exp(ttislightfunction(o), (o)->value.p)
  104. #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
  105. #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
  106. #define tsvalue(o) (&rawtsvalue(o)->tsv)
  107. #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
  108. #define uvalue(o) (&rawuvalue(o)->uv)
  109. #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
  110. #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
  111. #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
  112. #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
  113. #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
  114. /*
  115. ** for internal debug only
  116. */
  117. #define checkconsistency(obj) \
  118. lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
  119. #define checkliveness(g,obj) \
  120. lua_assert(!iscollectable(obj) || \
  121. ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
  122. /* Macros to set values */
  123. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  124. #define setnvalue(obj,x) \
  125. { lua_Number i_x = (x); TValue *i_o=(obj); i_o->value.n=i_x; i_o->tt=LUA_TNUMBER; }
  126. #define setpvalue(obj,x) \
  127. { void *i_x = (x); TValue *i_o=(obj); i_o->value.p=i_x; i_o->tt=LUA_TLIGHTUSERDATA; }
  128. #define setrvalue(obj,x) \
  129. { void *i_x = (x); TValue *i_o=(obj); i_o->value.p=i_x; i_o->tt=LUA_TROTABLE; }
  130. #define setfvalue(obj,x) \
  131. { void *i_x = (x); TValue *i_o=(obj); i_o->value.p=i_x; i_o->tt=LUA_TLIGHTFUNCTION; }
  132. #define setbvalue(obj,x) \
  133. { int i_x = (x); TValue *i_o=(obj); i_o->value.b=i_x; i_o->tt=LUA_TBOOLEAN; }
  134. #define setsvalue(L,obj,x) \
  135. { GCObject *i_x = cast(GCObject *, (x)); \
  136. TValue *i_o=(obj); \
  137. i_o->value.gc=i_x; i_o->tt=LUA_TSTRING; \
  138. checkliveness(G(L),i_o); }
  139. #define setuvalue(L,obj,x) \
  140. { GCObject *i_x = cast(GCObject *, (x)); \
  141. TValue *i_o=(obj); \
  142. i_o->value.gc=i_x; i_o->tt=LUA_TUSERDATA; \
  143. checkliveness(G(L),i_o); }
  144. #define setthvalue(L,obj,x) \
  145. { GCObject *i_x = cast(GCObject *, (x)); \
  146. TValue *i_o=(obj); \
  147. i_o->value.gc=i_x; i_o->tt=LUA_TTHREAD; \
  148. checkliveness(G(L),i_o); }
  149. #define setclvalue(L,obj,x) \
  150. { GCObject *i_x = cast(GCObject *, (x)); \
  151. TValue *i_o=(obj); \
  152. i_o->value.gc=i_x; i_o->tt=LUA_TFUNCTION; \
  153. checkliveness(G(L),i_o); }
  154. #define sethvalue(L,obj,x) \
  155. { GCObject *i_x = cast(GCObject *, (x)); \
  156. TValue *i_o=(obj); \
  157. i_o->value.gc=i_x; i_o->tt=LUA_TTABLE; \
  158. checkliveness(G(L),i_o); }
  159. #define setptvalue(L,obj,x) \
  160. { GCObject *i_x = cast(GCObject *, (x)); \
  161. TValue *i_o=(obj); \
  162. i_o->value.gc=i_x; i_o->tt=LUA_TPROTO; \
  163. checkliveness(G(L),i_o); }
  164. #define setobj(L,obj1,obj2) \
  165. { const TValue *o2=(obj2); TValue *o1=(obj1); \
  166. o1->value = o2->value; o1->tt=o2->tt; \
  167. checkliveness(G(L),o1); }
  168. /*
  169. ** different types of sets, according to destination
  170. */
  171. /* from stack to (same) stack */
  172. #define setobjs2s setobj
  173. /* to stack (not from same stack) */
  174. #define setobj2s setobj
  175. #define setsvalue2s setsvalue
  176. #define sethvalue2s sethvalue
  177. #define setptvalue2s setptvalue
  178. /* from table to same table */
  179. #define setobjt2t setobj
  180. /* to table */
  181. #define setobj2t setobj
  182. /* to new object */
  183. #define setobj2n setobj
  184. #define setsvalue2n setsvalue
  185. #define setttype(obj, stt) ((void) (obj)->value, (obj)->tt = (stt))
  186. #define iscollectable(o) (ttype(o) >= LUA_TSTRING)
  187. typedef TValue *StkId; /* index to stack elements */
  188. /*
  189. ** String headers for string table
  190. */
  191. typedef union TString {
  192. L_Umaxalign dummy; /* ensures maximum alignment for strings */
  193. struct {
  194. CommonHeader;
  195. unsigned int hash;
  196. size_t len;
  197. } tsv;
  198. } TString;
  199. #ifdef LUA_CROSS_COMPILER
  200. #define isreadonly(o) (0)
  201. #else
  202. #define isreadonly(o) ((o).marked & READONLYMASK)
  203. #endif
  204. #define ts_isreadonly(ts) isreadonly((ts)->tsv)
  205. #define getstr(ts) (ts_isreadonly(ts) ? \
  206. cast(const char *, *(const char**)((ts) + 1)) : \
  207. cast(const char *, (ts) + 1))
  208. #define svalue(o) getstr(rawtsvalue(o))
  209. typedef union Udata {
  210. L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  211. struct {
  212. CommonHeader;
  213. struct Table *metatable;
  214. struct Table *env;
  215. size_t len;
  216. } uv;
  217. } Udata;
  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. #define proto_isreadonly(p) isreadonly(*(p))
  251. /* masks for new-style vararg */
  252. #define VARARG_HASARG 1
  253. #define VARARG_ISVARARG 2
  254. #define VARARG_NEEDSARG 4
  255. typedef struct LocVar {
  256. TString *varname;
  257. int startpc; /* first point where variable is active */
  258. int endpc; /* first point where variable is dead */
  259. } LocVar;
  260. /*
  261. ** Upvalues
  262. */
  263. typedef struct UpVal {
  264. CommonHeader;
  265. TValue *v; /* points to stack or to its own value */
  266. union {
  267. TValue value; /* the value (when closed) */
  268. struct { /* double linked list (when open) */
  269. struct UpVal *prev;
  270. struct UpVal *next;
  271. } l;
  272. } u;
  273. } UpVal;
  274. /*
  275. ** Closures
  276. */
  277. #define ClosureHeader \
  278. CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
  279. struct Table *env
  280. typedef struct CClosure {
  281. ClosureHeader;
  282. lua_CFunction f;
  283. TValue upvalue[1];
  284. } CClosure;
  285. typedef struct LClosure {
  286. ClosureHeader;
  287. struct Proto *p;
  288. UpVal *upvals[1];
  289. } LClosure;
  290. typedef union Closure {
  291. CClosure c;
  292. LClosure l;
  293. } Closure;
  294. #define iscfunction(o) ((ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)||(ttype(o)==LUA_TLIGHTFUNCTION))
  295. #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
  296. /*
  297. ** Tables
  298. */
  299. typedef union TKey {
  300. struct {
  301. TValuefields;
  302. struct Node *next; /* for chaining */
  303. } nk;
  304. TValue tvk;
  305. } TKey;
  306. #define LUA_TKEY_NIL {LUA_TVALUE_NIL, NULL}
  307. typedef struct Node {
  308. TValue i_val;
  309. TKey i_key;
  310. } Node;
  311. typedef struct Table {
  312. CommonHeader;
  313. lu_byte flags; /* 1<<p means tagmethod(p) is not present */
  314. lu_byte lsizenode; /* log2 of size of `node' array */
  315. struct Table *metatable;
  316. TValue *array; /* array part */
  317. Node *node;
  318. Node *lastfree; /* any free position is before this position */
  319. GCObject *gclist;
  320. int sizearray; /* size of `array' array */
  321. } Table;
  322. typedef const struct luaR_entry ROTable;
  323. /*
  324. ** `module' operation for hashing (size is always a power of 2)
  325. */
  326. #define lmod(s,size) \
  327. (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
  328. #define twoto(x) (1<<(x))
  329. #define sizenode(t) (twoto((t)->lsizenode))
  330. #define luaO_nilobject (&luaO_nilobject_)
  331. LUAI_DATA const TValue luaO_nilobject_;
  332. #define ceillog2(x) (luaO_log2((x)-1) + 1)
  333. LUAI_FUNC int luaO_log2 (unsigned int x);
  334. LUAI_FUNC int luaO_int2fb (unsigned int x);
  335. LUAI_FUNC int luaO_fb2int (int x);
  336. LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
  337. LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
  338. LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
  339. va_list argp);
  340. LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  341. LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
  342. #endif