lobject.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. ** $Id: lobject.h,v 1.108 2001/06/28 14:48:44 roberto Exp roberto $
  3. ** Type definitions for Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef lobject_h
  7. #define lobject_h
  8. #include "llimits.h"
  9. #include "lua.h"
  10. #ifndef lua_assert
  11. #define lua_assert(c) /* empty */
  12. #endif
  13. #ifndef UNUSED
  14. #define UNUSED(x) ((void)(x)) /* to avoid warnings */
  15. #endif
  16. /* tags for values visible from Lua == first user-created tag */
  17. #define NUM_TAGS 6
  18. typedef union {
  19. union TString *ts;
  20. union Udata *u;
  21. struct Closure *cl;
  22. struct Hash *h;
  23. lua_Number n; /* LUA_TNUMBER */
  24. } Value;
  25. typedef struct lua_TObject {
  26. int tt;
  27. Value value;
  28. } TObject;
  29. /* Macros to access values */
  30. #define ttype(o) ((o)->tt)
  31. #define nvalue(o) ((o)->value.n)
  32. #define tsvalue(o) ((o)->value.ts)
  33. #define uvalue(o) ((o)->value.u)
  34. #define clvalue(o) ((o)->value.cl)
  35. #define hvalue(o) ((o)->value.h)
  36. /* Macros to set values */
  37. #define setnvalue(obj,x) \
  38. { TObject *_o=(obj); _o->tt=LUA_TNUMBER; _o->value.n=(x); }
  39. #define chgnvalue(obj,x) ((obj)->value.n=(x))
  40. #define setsvalue(obj,x) \
  41. { TObject *_o=(obj); _o->tt=LUA_TSTRING; _o->value.ts=(x); }
  42. #define setuvalue(obj,x) \
  43. { TObject *_o=(obj); _o->tt=LUA_TUSERDATA; _o->value.u=(x); }
  44. #define setclvalue(obj,x) \
  45. { TObject *_o=(obj); _o->tt=LUA_TFUNCTION; _o->value.cl=(x); }
  46. #define sethvalue(obj,x) \
  47. { TObject *_o=(obj); _o->tt=LUA_TTABLE; _o->value.h=(x); }
  48. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  49. #define setobj(obj1,obj2) \
  50. { TObject *o1=(obj1); const TObject *o2=(obj2); \
  51. o1->tt=o2->tt; o1->value = o2->value; }
  52. #define setttype(obj, tt) (ttype(obj) = (tt))
  53. typedef TObject *StkId; /* index to stack elements */
  54. /*
  55. ** String headers for string table
  56. */
  57. typedef union TString {
  58. union L_Umaxalign dummy; /* ensures maximum alignment for strings */
  59. struct {
  60. lu_hash hash;
  61. size_t len;
  62. unsigned short constindex; /* hint to reuse constants */
  63. short marked;
  64. union TString *nexthash; /* chain for hash table */
  65. } tsv;
  66. } TString;
  67. #define getstr(ts) ((l_char *)((ts) + 1))
  68. #define svalue(o) getstr(tsvalue(o))
  69. typedef union Udata {
  70. union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  71. struct {
  72. int tag; /* negative means `marked' (only during GC) */
  73. void *value;
  74. size_t len;
  75. union Udata *next; /* chain for list of all udata */
  76. } uv;
  77. } Udata;
  78. #define switchudatamark(u) ((u)->uv.tag = (-((u)->uv.tag+1)))
  79. #define ismarkedudata(u) ((u)->uv.tag < 0)
  80. /*
  81. ** Function Prototypes
  82. */
  83. typedef struct Proto {
  84. TObject *k; /* constants used by the function */
  85. int sizek; /* size of `k' */
  86. struct Proto **p; /* functions defined inside the function */
  87. int sizep; /* size of `p' */
  88. Instruction *code;
  89. int sizecode;
  90. short nupvalues;
  91. short numparams;
  92. short is_vararg;
  93. short maxstacksize;
  94. short marked;
  95. struct Proto *next;
  96. /* debug information */
  97. int *lineinfo; /* map from opcodes to source lines */
  98. int sizelineinfo; /* size of `lineinfo' */
  99. struct LocVar *locvars; /* information about local variables */
  100. int sizelocvars;
  101. int lineDefined;
  102. TString *source;
  103. } Proto;
  104. typedef struct LocVar {
  105. TString *varname;
  106. int startpc; /* first point where variable is active */
  107. int endpc; /* first point where variable is dead */
  108. } LocVar;
  109. /*
  110. ** Closures
  111. */
  112. typedef struct Closure {
  113. int isC; /* 0 for Lua functions, 1 for C functions */
  114. int nupvalues;
  115. union {
  116. lua_CFunction c; /* C functions */
  117. struct Proto *l; /* Lua functions */
  118. } f;
  119. struct Closure *next;
  120. struct Closure *mark; /* marked closures (point to itself when not marked) */
  121. TObject upvalue[1];
  122. } Closure;
  123. #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
  124. typedef struct Node {
  125. struct Node *next; /* for chaining */
  126. TObject key;
  127. TObject val;
  128. } Node;
  129. typedef struct Hash {
  130. Node *node;
  131. int htag;
  132. int size;
  133. Node *firstfree; /* this position is free; all positions after it are full */
  134. struct Hash *next;
  135. struct Hash *mark; /* marked tables (point to itself when not marked) */
  136. int weakmode;
  137. } Hash;
  138. /* unmarked tables and closures are represented by pointing `mark' to
  139. ** themselves
  140. */
  141. #define ismarked(x) ((x)->mark != (x))
  142. /*
  143. ** `module' operation for hashing (size is always a power of 2)
  144. */
  145. #define lmod(s,size) ((int)((s) & ((size)-1)))
  146. /*
  147. ** informations about a call (for debugging)
  148. */
  149. typedef struct CallInfo {
  150. struct CallInfo *prev; /* linked list */
  151. StkId base; /* base for called function */
  152. const Instruction **pc; /* current pc of called function */
  153. int lastpc; /* last pc traced */
  154. int line; /* current line */
  155. int refi; /* current index in `lineinfo' */
  156. } CallInfo;
  157. #define ci_func(ci) (clvalue((ci)->base - 1))
  158. extern const TObject luaO_nilobject;
  159. #define luaO_openspace(L,n,t) ((t *)luaO_openspaceaux(L,(n)*sizeof(t)))
  160. void *luaO_openspaceaux (lua_State *L, size_t n);
  161. int luaO_equalObj (const TObject *t1, const TObject *t2);
  162. int luaO_str2d (const l_char *s, lua_Number *result);
  163. void luaO_verror (lua_State *L, const l_char *fmt, ...);
  164. void luaO_chunkid (l_char *out, const l_char *source, int len);
  165. #endif