lobject.h 4.1 KB

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