lobject.h 5.2 KB

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