lobject.h 5.1 KB

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