lobject.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. ** $Id: lobject.h,v 1.85 2000/12/28 12:55:41 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. #ifdef LUA_DEBUG
  11. #undef NDEBUG
  12. #include <assert.h>
  13. #define LUA_INTERNALERROR(s) assert(((void)s,0))
  14. #define LUA_ASSERT(c,s) assert(((void)s,(c)))
  15. #else
  16. #define LUA_INTERNALERROR(s) /* empty */
  17. #define LUA_ASSERT(c,s) /* empty */
  18. #endif
  19. #ifdef LUA_DEBUG
  20. /* to avoid warnings, and make sure value is really unused */
  21. #define UNUSED(x) (x=0, (void)(x))
  22. #else
  23. #define UNUSED(x) ((void)(x)) /* to avoid warnings */
  24. #endif
  25. /* mark for closures active in the stack */
  26. #define LUA_TMARK 6
  27. /* tags for values visible from Lua == first user-created tag */
  28. #define NUM_TAGS 6
  29. /* check whether `t' is a mark */
  30. #define is_T_MARK(t) (ttype(t) == LUA_TMARK)
  31. typedef union {
  32. void *v;
  33. lua_Number n; /* LUA_TNUMBER */
  34. } Value;
  35. typedef struct lua_TObject {
  36. int tt;
  37. Value value;
  38. } TObject;
  39. /* Macros to access values */
  40. #define ttype(o) ((o)->tt)
  41. #define nvalue(o) ((o)->value.n)
  42. #define tsvalue(o) ((struct TString *)(o)->value.v)
  43. #define clvalue(o) ((struct Closure *)(o)->value.v)
  44. #define hvalue(o) ((struct Hash *)(o)->value.v)
  45. #define infovalue(o) ((struct CallInfo *)(o)->value.v)
  46. #define svalue(o) (tsvalue(o)->str)
  47. /* Macros to set values */
  48. #define setnvalue(obj,x) \
  49. { TObject *o=(obj); o->tt=LUA_TNUMBER; o->value.n=(x); }
  50. #define setsvalue(obj,x) \
  51. { TObject *o=(obj); struct TString *v=(x); \
  52. o->tt=LUA_TSTRING; o->value.v=v; }
  53. #define setuvalue(obj,x) \
  54. { TObject *o=(obj); struct TString *v=(x); \
  55. o->tt=LUA_TUSERDATA; o->value.v=v; }
  56. #define setclvalue(obj,x) \
  57. { TObject *o=(obj); struct Closure *v=(x); \
  58. o->tt=LUA_TFUNCTION; o->value.v=v; }
  59. #define sethvalue(obj,x) \
  60. { TObject *o=(obj); struct Hash *v=(x); \
  61. o->tt=LUA_TTABLE; o->value.v=v; }
  62. #define setivalue(obj,x) \
  63. { TObject *o=(obj); struct CallInfo *v=(x); \
  64. o->tt=LUA_TMARK; o->value.v=v; }
  65. #define setnilvalue(obj) { (obj)->tt=LUA_TNIL; }
  66. #define setobj(obj1,obj2) \
  67. { TObject *o1=(obj1); const TObject *o2=(obj2); \
  68. o1->tt=o2->tt; o1->value = o2->value; }
  69. /*
  70. ** String headers for string table
  71. */
  72. /*
  73. ** most `malloc' libraries allocate memory in blocks of 8 bytes. TSPACK
  74. ** tries to make sizeof(TString) a multiple of this granularity, to reduce
  75. ** waste of space.
  76. */
  77. #define TSPACK ((int)sizeof(int))
  78. typedef struct TString {
  79. union {
  80. struct { /* for strings */
  81. luint32 hash;
  82. int constindex; /* hint to reuse constants */
  83. } s;
  84. struct { /* for userdata */
  85. int tag;
  86. void *value;
  87. } d;
  88. } u;
  89. size_t len;
  90. struct TString *nexthash; /* chain for hash table */
  91. int marked;
  92. char str[TSPACK]; /* variable length string!! must be the last field! */
  93. } TString;
  94. /*
  95. ** Function Prototypes
  96. */
  97. typedef struct Proto {
  98. lua_Number *knum; /* numbers used by the function */
  99. int sizeknum; /* size of `knum' */
  100. struct TString **kstr; /* strings used by the function */
  101. int sizekstr; /* size of `kstr' */
  102. struct Proto **kproto; /* functions defined inside the function */
  103. int sizekproto; /* size of `kproto' */
  104. Instruction *code;
  105. int sizecode;
  106. short numparams;
  107. short is_vararg;
  108. short maxstacksize;
  109. short marked;
  110. struct Proto *next;
  111. /* debug information */
  112. int *lineinfo; /* map from opcodes to source lines */
  113. int sizelineinfo; /* size of `lineinfo' */
  114. struct LocVar *locvars; /* information about local variables */
  115. int sizelocvars;
  116. int lineDefined;
  117. TString *source;
  118. } Proto;
  119. typedef struct LocVar {
  120. TString *varname;
  121. int startpc; /* first point where variable is active */
  122. int endpc; /* first point where variable is dead */
  123. } LocVar;
  124. /*
  125. ** Closures
  126. */
  127. typedef struct Closure {
  128. union {
  129. lua_CFunction c; /* C functions */
  130. struct Proto *l; /* Lua functions */
  131. } f;
  132. struct Closure *next;
  133. struct Closure *mark; /* marked closures (point to itself when not marked) */
  134. short isC; /* 0 for Lua functions, 1 for C functions */
  135. short nupvalues;
  136. TObject upvalue[1];
  137. } Closure;
  138. #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
  139. typedef struct Node {
  140. TObject key;
  141. TObject val;
  142. struct Node *next; /* for chaining */
  143. } Node;
  144. typedef struct Hash {
  145. Node *node;
  146. int htag;
  147. int size;
  148. Node *firstfree; /* this position is free; all positions after it are full */
  149. struct Hash *next;
  150. struct Hash *mark; /* marked tables (point to itself when not marked) */
  151. } Hash;
  152. /* unmarked tables and closures are represented by pointing `mark' to
  153. ** themselves
  154. */
  155. #define ismarked(x) ((x)->mark != (x))
  156. /*
  157. ** informations about a call (for debugging)
  158. */
  159. typedef struct CallInfo {
  160. struct Closure *func; /* function being called */
  161. const Instruction **pc; /* current pc of called function */
  162. int lastpc; /* last pc traced */
  163. int line; /* current line */
  164. int refi; /* current index in `lineinfo' */
  165. } CallInfo;
  166. extern const TObject luaO_nilobject;
  167. extern const char *const luaO_typenames[];
  168. #define luaO_typename(o) (luaO_typenames[ttype(o)])
  169. luint32 luaO_power2 (luint32 n);
  170. char *luaO_openspace (lua_State *L, size_t n);
  171. int luaO_equalObj (const TObject *t1, const TObject *t2);
  172. int luaO_str2d (const char *s, lua_Number *result);
  173. void luaO_verror (lua_State *L, const char *fmt, ...);
  174. void luaO_chunkid (char *out, const char *source, int len);
  175. #endif