lobject.h 10 KB

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