lobject.h 3.7 KB

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