lobject.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. ** $Id: lobject.h,v 1.11 1997/12/15 16:17:20 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 "lua.h"
  9. #include <limits.h>
  10. /*
  11. ** "real" is the type "number" of Lua
  12. ** GREP LUA_NUMBER to change that
  13. */
  14. #ifndef real
  15. #define real float
  16. #endif
  17. #define Byte lua_Byte /* some systems have Byte as a predefined type */
  18. typedef unsigned char Byte; /* unsigned 8 bits */
  19. #define Word lua_Word /* some systems have Word as a predefined type */
  20. typedef unsigned short Word; /* unsigned 16 bits */
  21. #define MAX_WORD (USHRT_MAX-2) /* maximum value of a word (-2 for safety) */
  22. #define MAX_INT (INT_MAX-2) /* maximum value of a int (-2 for safety) */
  23. typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
  24. /*
  25. ** Lua TYPES
  26. ** WARNING: if you change the order of this enumeration,
  27. ** grep "ORDER LUA_T"
  28. */
  29. typedef enum {
  30. LUA_T_USERDATA = 0, /* tag default for userdata */
  31. LUA_T_NUMBER = -1, /* fixed tag for numbers */
  32. LUA_T_STRING = -2, /* fixed tag for strings */
  33. LUA_T_ARRAY = -3, /* tag default for tables (or arrays) */
  34. LUA_T_PROTO = -4, /* fixed tag for functions */
  35. LUA_T_CPROTO = -5, /* fixed tag for Cfunctions */
  36. LUA_T_NIL = -6, /* last "pre-defined" tag */
  37. LUA_T_CLOSURE = -7,
  38. LUA_T_CLMARK = -8, /* mark for closures */
  39. LUA_T_PMARK = -9, /* mark for Lua prototypes */
  40. LUA_T_CMARK = -10, /* mark for C prototypes */
  41. LUA_T_LINE = -11
  42. } lua_Type;
  43. #define NUM_TYPES 11
  44. #define NUM_TAGS 7
  45. typedef union {
  46. lua_CFunction f; /* LUA_T_CPROTO, LUA_T_CMARK */
  47. real n; /* LUA_T_NUMBER */
  48. struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */
  49. struct TProtoFunc *tf; /* LUA_T_PROTO, LUA_T_PMARK */
  50. struct Closure *cl; /* LUA_T_CLOSURE, LUA_T_CLMARK */
  51. struct Hash *a; /* LUA_T_ARRAY */
  52. int i; /* LUA_T_LINE */
  53. } Value;
  54. typedef struct TObject {
  55. lua_Type ttype;
  56. Value value;
  57. } TObject;
  58. /*
  59. ** generic header for garbage collector lists
  60. */
  61. typedef struct GCnode {
  62. struct GCnode *next;
  63. int marked;
  64. } GCnode;
  65. /*
  66. ** String headers for string table
  67. */
  68. typedef struct TaggedString {
  69. GCnode head;
  70. int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
  71. unsigned long hash;
  72. union {
  73. TObject globalval;
  74. struct {
  75. void *v; /* if this is a userdata, here is its value */
  76. int tag;
  77. } d;
  78. } u;
  79. char str[1]; /* \0 byte already reserved */
  80. } TaggedString;
  81. /*
  82. ** Function Prototypes
  83. */
  84. typedef struct TProtoFunc {
  85. GCnode head;
  86. Byte *code; /* ends with opcode ENDCODE */
  87. int lineDefined;
  88. TaggedString *fileName;
  89. struct TObject *consts;
  90. int nconsts;
  91. struct LocVar *locvars; /* ends with line = -1 */
  92. } TProtoFunc;
  93. typedef struct LocVar {
  94. TaggedString *varname; /* NULL signals end of scope */
  95. int line;
  96. } LocVar;
  97. /* Macros to access structure members */
  98. #define ttype(o) ((o)->ttype)
  99. #define nvalue(o) ((o)->value.n)
  100. #define svalue(o) ((o)->value.ts->str)
  101. #define tsvalue(o) ((o)->value.ts)
  102. #define clvalue(o) ((o)->value.cl)
  103. #define avalue(o) ((o)->value.a)
  104. #define fvalue(o) ((o)->value.f)
  105. #define tfvalue(o) ((o)->value.tf)
  106. #define protovalue(o) ((o)->value.cl->consts)
  107. /*
  108. ** Closures
  109. */
  110. typedef struct Closure {
  111. GCnode head;
  112. int nelems; /* not included the first one (always the prototype) */
  113. TObject consts[1]; /* at least one for prototype */
  114. } Closure;
  115. typedef struct node {
  116. TObject ref;
  117. TObject val;
  118. } Node;
  119. typedef struct Hash {
  120. GCnode head;
  121. Node *node;
  122. int nhash;
  123. int nuse;
  124. int htag;
  125. } Hash;
  126. /*
  127. ** a gross estimation of number of memory "blocks" allocated
  128. ** (a block is *roughly* 32 bytes)
  129. */
  130. extern char *luaO_typenames[];
  131. extern TObject luaO_nilobject;
  132. int luaO_equalObj (TObject *t1, TObject *t2);
  133. int luaO_redimension (int oldsize);
  134. int luaO_findstring (char *name, char *list[]);
  135. void luaO_insertlist (GCnode *root, GCnode *node);
  136. #endif