lobject.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ** $Id: lobject.h,v 1.79 2000/10/05 12:14:08 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. /* 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. typedef struct TString {
  54. union {
  55. union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
  56. struct { /* for strings */
  57. unsigned long hash;
  58. int constindex; /* hint to reuse constants */
  59. } s;
  60. struct { /* for userdata */
  61. int tag;
  62. void *value;
  63. } d;
  64. } u;
  65. size_t len;
  66. struct TString *nexthash; /* chain for hash table */
  67. unsigned char marked;
  68. char str[1]; /* variable length string!! must be the last field! */
  69. } TString;
  70. /*
  71. ** Function Prototypes
  72. */
  73. typedef struct Proto {
  74. Number *knum; /* Number numbers used by the function */
  75. int nknum; /* size of `knum' */
  76. struct TString **kstr; /* strings used by the function */
  77. int nkstr; /* size of `kstr' */
  78. struct Proto **kproto; /* functions defined inside the function */
  79. int nkproto; /* size of `kproto' */
  80. Instruction *code;
  81. int ncode; /* size of `code'; when 0 means an incomplete `Proto' */
  82. short numparams;
  83. short is_vararg;
  84. short maxstacksize;
  85. short marked;
  86. struct Proto *next;
  87. /* debug information */
  88. int *lineinfo; /* map from opcodes to source lines */
  89. int nlineinfo; /* size of `lineinfo' */
  90. int nlocvars;
  91. struct LocVar *locvars; /* information about local variables */
  92. int lineDefined;
  93. TString *source;
  94. } Proto;
  95. typedef struct LocVar {
  96. TString *varname;
  97. int startpc; /* first point where variable is active */
  98. int endpc; /* first point where variable is dead */
  99. } LocVar;
  100. /*
  101. ** Closures
  102. */
  103. typedef struct Closure {
  104. union {
  105. lua_CFunction c; /* C functions */
  106. struct Proto *l; /* Lua functions */
  107. } f;
  108. struct Closure *next;
  109. struct Closure *mark; /* marked closures (point to itself when not marked) */
  110. short isC; /* 0 for Lua functions, 1 for C functions */
  111. short nupvalues;
  112. TObject upvalue[1];
  113. } Closure;
  114. #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
  115. typedef struct Node {
  116. TObject key;
  117. TObject val;
  118. struct Node *next; /* for chaining */
  119. } Node;
  120. typedef struct Hash {
  121. Node *node;
  122. int htag;
  123. int size;
  124. Node *firstfree; /* this position is free; all positions after it are full */
  125. struct Hash *next;
  126. struct Hash *mark; /* marked tables (point to itself when not marked) */
  127. } Hash;
  128. /* unmarked tables and closures are represented by pointing `mark' to
  129. ** themselves
  130. */
  131. #define ismarked(x) ((x)->mark != (x))
  132. /*
  133. ** informations about a call (for debugging)
  134. */
  135. typedef struct CallInfo {
  136. struct Closure *func; /* function being called */
  137. const Instruction **pc; /* current pc of called function */
  138. int lastpc; /* last pc traced */
  139. int line; /* current line */
  140. int refi; /* current index in `lineinfo' */
  141. } CallInfo;
  142. extern const TObject luaO_nilobject;
  143. extern const char *const luaO_typenames[];
  144. #define luaO_typename(o) (luaO_typenames[ttype(o)])
  145. lint32 luaO_power2 (lint32 n);
  146. char *luaO_openspace (lua_State *L, size_t n);
  147. int luaO_equalObj (const TObject *t1, const TObject *t2);
  148. int luaO_str2d (const char *s, Number *result);
  149. void luaO_verror (lua_State *L, const char *fmt, ...);
  150. void luaO_chunkid (char *out, const char *source, int len);
  151. #endif