lobject.h 3.9 KB

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