lobject.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. ** $Id: lobject.h,v 1.109 2001/06/28 14:56:25 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. int marked;
  63. union TString *nexthash; /* chain for hash table */
  64. } tsv;
  65. } TString;
  66. #define getstr(ts) ((l_char *)((ts) + 1))
  67. #define svalue(o) getstr(tsvalue(o))
  68. typedef union Udata {
  69. union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  70. struct {
  71. int tag; /* negative means `marked' (only during GC) */
  72. void *value;
  73. size_t len;
  74. union Udata *next; /* chain for list of all udata */
  75. } uv;
  76. } Udata;
  77. #define switchudatamark(u) ((u)->uv.tag = (-((u)->uv.tag+1)))
  78. #define ismarkedudata(u) ((u)->uv.tag < 0)
  79. /*
  80. ** Function Prototypes
  81. */
  82. typedef struct Proto {
  83. TObject *k; /* constants used by the function */
  84. int sizek; /* size of `k' */
  85. struct Proto **p; /* functions defined inside the function */
  86. int sizep; /* size of `p' */
  87. Instruction *code;
  88. int sizecode;
  89. short nupvalues;
  90. short numparams;
  91. short is_vararg;
  92. short maxstacksize;
  93. short marked;
  94. struct Proto *next;
  95. /* debug information */
  96. int *lineinfo; /* map from opcodes to source lines */
  97. int sizelineinfo; /* size of `lineinfo' */
  98. struct LocVar *locvars; /* information about local variables */
  99. int sizelocvars;
  100. int lineDefined;
  101. TString *source;
  102. } Proto;
  103. typedef struct LocVar {
  104. TString *varname;
  105. int startpc; /* first point where variable is active */
  106. int endpc; /* first point where variable is dead */
  107. } LocVar;
  108. /*
  109. ** Closures
  110. */
  111. typedef struct Closure {
  112. int isC; /* 0 for Lua functions, 1 for C functions */
  113. int nupvalues;
  114. union {
  115. lua_CFunction c; /* C functions */
  116. struct Proto *l; /* Lua functions */
  117. } f;
  118. struct Closure *next;
  119. struct Closure *mark; /* marked closures (point to itself when not marked) */
  120. TObject upvalue[1];
  121. } Closure;
  122. #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
  123. typedef struct Node {
  124. struct Node *next; /* for chaining */
  125. TObject key;
  126. TObject val;
  127. } Node;
  128. typedef struct Hash {
  129. Node *node;
  130. int htag;
  131. int size;
  132. Node *firstfree; /* this position is free; all positions after it are full */
  133. struct Hash *next;
  134. struct Hash *mark; /* marked tables (point to itself when not marked) */
  135. int weakmode;
  136. } Hash;
  137. /* unmarked tables and closures are represented by pointing `mark' to
  138. ** themselves
  139. */
  140. #define ismarked(x) ((x)->mark != (x))
  141. /*
  142. ** `module' operation for hashing (size is always a power of 2)
  143. */
  144. #define lmod(s,size) ((int)((s) & ((size)-1)))
  145. /*
  146. ** informations about a call (for debugging)
  147. */
  148. typedef struct CallInfo {
  149. struct CallInfo *prev; /* linked list */
  150. StkId base; /* base for called function */
  151. const Instruction **pc; /* current pc of called function */
  152. int lastpc; /* last pc traced */
  153. int line; /* current line */
  154. int refi; /* current index in `lineinfo' */
  155. } CallInfo;
  156. #define ci_func(ci) (clvalue((ci)->base - 1))
  157. extern const TObject luaO_nilobject;
  158. #define luaO_openspace(L,n,t) ((t *)luaO_openspaceaux(L,(n)*sizeof(t)))
  159. void *luaO_openspaceaux (lua_State *L, size_t n);
  160. int luaO_equalObj (const TObject *t1, const TObject *t2);
  161. int luaO_str2d (const l_char *s, lua_Number *result);
  162. void luaO_verror (lua_State *L, const l_char *fmt, ...);
  163. void luaO_chunkid (l_char *out, const l_char *source, int len);
  164. #endif