lobject.h 5.1 KB

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