Imported from ../lua-4.0.tar.gz.
[lua.git] / src / lobject.h
blobcb232c770b2e21ed12dbbbdd83af9c9dfc0ad073
1 /*
2 ** $Id: lobject.h,v 1.82 2000/10/30 17:49:19 roberto Exp $
3 ** Type definitions for Lua objects
4 ** See Copyright Notice in lua.h
5 */
7 #ifndef lobject_h
8 #define lobject_h
11 #include "llimits.h"
12 #include "lua.h"
15 #ifdef LUA_DEBUG
16 #undef NDEBUG
17 #include <assert.h>
18 #define LUA_INTERNALERROR(s) assert(((void)s,0))
19 #define LUA_ASSERT(c,s) assert(((void)s,(c)))
20 #else
21 #define LUA_INTERNALERROR(s) /* empty */
22 #define LUA_ASSERT(c,s) /* empty */
23 #endif
26 #ifdef LUA_DEBUG
27 /* to avoid warnings, and make sure value is really unused */
28 #define UNUSED(x) (x=0, (void)(x))
29 #else
30 #define UNUSED(x) ((void)(x)) /* to avoid warnings */
31 #endif
34 /* mark for closures active in the stack */
35 #define LUA_TMARK 6
38 /* tags for values visible from Lua == first user-created tag */
39 #define NUM_TAGS 6
42 /* check whether `t' is a mark */
43 #define is_T_MARK(t) ((t) == LUA_TMARK)
46 typedef union {
47 struct TString *ts; /* LUA_TSTRING, LUA_TUSERDATA */
48 struct Closure *cl; /* LUA_TFUNCTION */
49 struct Hash *a; /* LUA_TTABLE */
50 struct CallInfo *i; /* LUA_TLMARK */
51 Number n; /* LUA_TNUMBER */
52 } Value;
55 /* Macros to access values */
56 #define ttype(o) ((o)->ttype)
57 #define nvalue(o) ((o)->value.n)
58 #define tsvalue(o) ((o)->value.ts)
59 #define clvalue(o) ((o)->value.cl)
60 #define hvalue(o) ((o)->value.a)
61 #define infovalue(o) ((o)->value.i)
62 #define svalue(o) (tsvalue(o)->str)
65 typedef struct lua_TObject {
66 int ttype;
67 Value value;
68 } TObject;
72 ** String headers for string table
76 ** most `malloc' libraries allocate memory in blocks of 8 bytes. TSPACK
77 ** tries to make sizeof(TString) a multiple of this granularity, to reduce
78 ** waste of space.
80 #define TSPACK ((int)sizeof(int))
82 typedef struct TString {
83 union {
84 struct { /* for strings */
85 unsigned long hash;
86 int constindex; /* hint to reuse constants */
87 } s;
88 struct { /* for userdata */
89 int tag;
90 void *value;
91 } d;
92 } u;
93 size_t len;
94 struct TString *nexthash; /* chain for hash table */
95 int marked;
96 char str[TSPACK]; /* variable length string!! must be the last field! */
97 } TString;
101 ** Function Prototypes
103 typedef struct Proto {
104 Number *knum; /* Number numbers used by the function */
105 int nknum; /* size of `knum' */
106 struct TString **kstr; /* strings used by the function */
107 int nkstr; /* size of `kstr' */
108 struct Proto **kproto; /* functions defined inside the function */
109 int nkproto; /* size of `kproto' */
110 Instruction *code;
111 int ncode; /* size of `code'; when 0 means an incomplete `Proto' */
112 short numparams;
113 short is_vararg;
114 short maxstacksize;
115 short marked;
116 struct Proto *next;
117 /* debug information */
118 int *lineinfo; /* map from opcodes to source lines */
119 int nlineinfo; /* size of `lineinfo' */
120 int nlocvars;
121 struct LocVar *locvars; /* information about local variables */
122 int lineDefined;
123 TString *source;
124 } Proto;
127 typedef struct LocVar {
128 TString *varname;
129 int startpc; /* first point where variable is active */
130 int endpc; /* first point where variable is dead */
131 } LocVar;
135 ** Closures
137 typedef struct Closure {
138 union {
139 lua_CFunction c; /* C functions */
140 struct Proto *l; /* Lua functions */
141 } f;
142 struct Closure *next;
143 struct Closure *mark; /* marked closures (point to itself when not marked) */
144 short isC; /* 0 for Lua functions, 1 for C functions */
145 short nupvalues;
146 TObject upvalue[1];
147 } Closure;
150 #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
153 typedef struct Node {
154 TObject key;
155 TObject val;
156 struct Node *next; /* for chaining */
157 } Node;
159 typedef struct Hash {
160 Node *node;
161 int htag;
162 int size;
163 Node *firstfree; /* this position is free; all positions after it are full */
164 struct Hash *next;
165 struct Hash *mark; /* marked tables (point to itself when not marked) */
166 } Hash;
169 /* unmarked tables and closures are represented by pointing `mark' to
170 ** themselves
172 #define ismarked(x) ((x)->mark != (x))
176 ** informations about a call (for debugging)
178 typedef struct CallInfo {
179 struct Closure *func; /* function being called */
180 const Instruction **pc; /* current pc of called function */
181 int lastpc; /* last pc traced */
182 int line; /* current line */
183 int refi; /* current index in `lineinfo' */
184 } CallInfo;
187 extern const TObject luaO_nilobject;
188 extern const char *const luaO_typenames[];
191 #define luaO_typename(o) (luaO_typenames[ttype(o)])
194 lint32 luaO_power2 (lint32 n);
195 char *luaO_openspace (lua_State *L, size_t n);
197 int luaO_equalObj (const TObject *t1, const TObject *t2);
198 int luaO_str2d (const char *s, Number *result);
200 void luaO_verror (lua_State *L, const char *fmt, ...);
201 void luaO_chunkid (char *out, const char *source, int len);
204 #endif