?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

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