lobject.h 5.0 KB

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