lobject.h 5.3 KB

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