lobject.h 4.7 KB

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