lobject.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. ** $Id: lobject.h,v 1.77 2000/09/29 12:42:13 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 DEBUG
  11. #undef NDEBUG
  12. #include <assert.h>
  13. #define LUA_INTERNALERROR(s) assert(((void)s,0))
  14. #define LUA_ASSERT(c,s) assert(((void)s,(c)))
  15. #else
  16. #define LUA_INTERNALERROR(s) /* empty */
  17. #define LUA_ASSERT(c,s) /* empty */
  18. #endif
  19. #ifdef DEBUG
  20. /* to avoid warnings, and make sure value is really unused */
  21. #define UNUSED(x) (x=0, (void)(x))
  22. #else
  23. #define UNUSED(x) ((void)(x)) /* to avoid warnings */
  24. #endif
  25. /*
  26. ** Lua TYPES
  27. ** WARNING: if you change the order of this enumeration,
  28. ** grep "ORDER LUA_T"
  29. */
  30. typedef enum {
  31. TAG_USERDATA = 0, /* default tag for userdata */
  32. TAG_NUMBER, /* fixed tag for numbers */
  33. TAG_STRING, /* fixed tag for strings */
  34. TAG_TABLE, /* default tag for tables */
  35. TAG_LCLOSURE, /* fixed tag for Lua closures */
  36. TAG_CCLOSURE, /* fixed tag for C closures */
  37. TAG_NIL, /* last "pre-defined" tag */
  38. TAG_LMARK, /* mark for Lua closures */
  39. TAG_CMARK /* mark for C closures */
  40. } lua_Tag;
  41. /* tags for values visible from Lua == first user-created tag */
  42. #define NUM_TAGS 7
  43. /*
  44. ** check whether `t' is a mark
  45. */
  46. #define is_T_MARK(t) ((t) == TAG_LMARK || (t) == TAG_CMARK)
  47. typedef union {
  48. struct TString *ts; /* TAG_STRING, TAG_USERDATA */
  49. struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_CMARK */
  50. struct Hash *a; /* TAG_TABLE */
  51. struct CallInfo *i; /* TAG_LMARK */
  52. Number n; /* TAG_NUMBER */
  53. } Value;
  54. /* Macros to access values */
  55. #define ttype(o) ((o)->ttype)
  56. #define nvalue(o) ((o)->value.n)
  57. #define tsvalue(o) ((o)->value.ts)
  58. #define clvalue(o) ((o)->value.cl)
  59. #define hvalue(o) ((o)->value.a)
  60. #define infovalue(o) ((o)->value.i)
  61. #define svalue(o) (tsvalue(o)->str)
  62. typedef struct lua_TObject {
  63. lua_Tag ttype;
  64. Value value;
  65. } TObject;
  66. /*
  67. ** String headers for string table
  68. */
  69. typedef struct TString {
  70. union {
  71. struct { /* for strings */
  72. unsigned long hash;
  73. size_t len;
  74. int constindex; /* hint to reuse constants */
  75. } s;
  76. struct { /* for userdata */
  77. int tag;
  78. void *value;
  79. } d;
  80. } u;
  81. struct TString *nexthash; /* chain for hash table */
  82. unsigned char marked;
  83. char str[1]; /* variable length string!! must be the last field! */
  84. } TString;
  85. /*
  86. ** Function Prototypes
  87. */
  88. typedef struct Proto {
  89. Number *knum; /* Number numbers used by the function */
  90. int nknum; /* size of `knum' */
  91. struct TString **kstr; /* strings used by the function */
  92. int nkstr; /* size of `kstr' */
  93. struct Proto **kproto; /* functions defined inside the function */
  94. int nkproto; /* size of `kproto' */
  95. Instruction *code;
  96. int ncode; /* size of `code'; when 0 means an incomplete `Proto' */
  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 nlineinfo; /* size of `lineinfo' */
  105. int nlocvars;
  106. struct LocVar *locvars; /* information about local variables */
  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. 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. typedef struct Node {
  129. TObject key;
  130. TObject val;
  131. struct Node *next; /* for chaining */
  132. } Node;
  133. typedef struct Hash {
  134. Node *node;
  135. int htag;
  136. int size;
  137. Node *firstfree; /* this position is free; all positions after it are full */
  138. struct Hash *next;
  139. struct Hash *mark; /* marked tables (point to itself when not marked) */
  140. } Hash;
  141. /* unmarked tables and closures are represented by pointing `mark' to
  142. ** themselves
  143. */
  144. #define ismarked(x) ((x)->mark != (x))
  145. /*
  146. ** informations about a call (for debugging)
  147. */
  148. typedef struct CallInfo {
  149. struct Closure *func; /* function being called */
  150. const Instruction **pc; /* current pc of called function */
  151. int lastpc; /* last pc traced */
  152. int line; /* current line */
  153. int refi; /* current index in `lineinfo' */
  154. } CallInfo;
  155. extern const lua_Type luaO_typearr[];
  156. extern const TObject luaO_nilobject;
  157. #define luaO_tag2type(t) (luaO_typearr[(int)(t)])
  158. #define luaO_type(o) (luaO_tag2type(ttype(o)))
  159. #define luaO_typename(L, o) (lua_typename(L, luaO_type(o)))
  160. lint32 luaO_power2 (lint32 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, 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