]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/lua-5.3.1/src/lobject.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / civetweb / src / third_party / lua-5.3.1 / src / lobject.h
1 /*
2 ** $Id: lobject.h,v 2.111 2015/06/09 14:21:42 roberto Exp $
3 ** Type definitions for Lua objects
4 ** See Copyright Notice in lua.h
5 */
6
7
8 #ifndef lobject_h
9 #define lobject_h
10
11
12 #include <stdarg.h>
13
14
15 #include "llimits.h"
16 #include "lua.h"
17
18
19 /*
20 ** Extra tags for non-values
21 */
22 #define LUA_TPROTO LUA_NUMTAGS
23 #define LUA_TDEADKEY (LUA_NUMTAGS+1)
24
25 /*
26 ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
27 */
28 #define LUA_TOTALTAGS (LUA_TPROTO + 2)
29
30
31 /*
32 ** tags for Tagged Values have the following use of bits:
33 ** bits 0-3: actual tag (a LUA_T* value)
34 ** bits 4-5: variant bits
35 ** bit 6: whether value is collectable
36 */
37
38
39 /*
40 ** LUA_TFUNCTION variants:
41 ** 0 - Lua function
42 ** 1 - light C function
43 ** 2 - regular C function (closure)
44 */
45
46 /* Variant tags for functions */
47 #define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */
48 #define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */
49 #define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
50
51
52 /* Variant tags for strings */
53 #define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */
54 #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
55
56
57 /* Variant tags for numbers */
58 #define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */
59 #define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */
60
61
62 /* Bit mark for collectable types */
63 #define BIT_ISCOLLECTABLE (1 << 6)
64
65 /* mark a tag as collectable */
66 #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
67
68
69 /*
70 ** Common type for all collectable objects
71 */
72 typedef struct GCObject GCObject;
73
74
75 /*
76 ** Common Header for all collectable objects (in macro form, to be
77 ** included in other objects)
78 */
79 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
80
81
82 /*
83 ** Common type has only the common header
84 */
85 struct GCObject {
86 CommonHeader;
87 };
88
89
90
91 /*
92 ** Union of all Lua values
93 */
94 typedef union Value Value;
95
96
97
98
99 /*
100 ** Tagged Values. This is the basic representation of values in Lua,
101 ** an actual value plus a tag with its type.
102 */
103
104 #define TValuefields Value value_; int tt_
105
106 typedef struct lua_TValue TValue;
107
108
109 /* macro defining a nil value */
110 #define NILCONSTANT {NULL}, LUA_TNIL
111
112
113 #define val_(o) ((o)->value_)
114
115
116 /* raw type tag of a TValue */
117 #define rttype(o) ((o)->tt_)
118
119 /* tag with no variants (bits 0-3) */
120 #define novariant(x) ((x) & 0x0F)
121
122 /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
123 #define ttype(o) (rttype(o) & 0x3F)
124
125 /* type tag of a TValue with no variants (bits 0-3) */
126 #define ttnov(o) (novariant(rttype(o)))
127
128
129 /* Macros to test type */
130 #define checktag(o,t) (rttype(o) == (t))
131 #define checktype(o,t) (ttnov(o) == (t))
132 #define ttisnumber(o) checktype((o), LUA_TNUMBER)
133 #define ttisfloat(o) checktag((o), LUA_TNUMFLT)
134 #define ttisinteger(o) checktag((o), LUA_TNUMINT)
135 #define ttisnil(o) checktag((o), LUA_TNIL)
136 #define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
137 #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
138 #define ttisstring(o) checktype((o), LUA_TSTRING)
139 #define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
140 #define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
141 #define ttistable(o) checktag((o), ctb(LUA_TTABLE))
142 #define ttisfunction(o) checktype(o, LUA_TFUNCTION)
143 #define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
144 #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
145 #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
146 #define ttislcf(o) checktag((o), LUA_TLCF)
147 #define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA))
148 #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
149 #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)
150
151
152 /* Macros to access values */
153 #define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
154 #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
155 #define nvalue(o) check_exp(ttisnumber(o), \
156 (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
157 #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
158 #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
159 #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
160 #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
161 #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
162 #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
163 #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
164 #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
165 #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
166 #define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
167 #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
168 /* a dead value may get the 'gc' field, but cannot access its contents */
169 #define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
170
171 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
172
173
174 #define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
175
176
177 /* Macros for internal tests */
178 #define righttt(obj) (ttype(obj) == gcvalue(obj)->tt)
179
180 #define checkliveness(g,obj) \
181 lua_longassert(!iscollectable(obj) || \
182 (righttt(obj) && !isdead(g,gcvalue(obj))))
183
184
185 /* Macros to set values */
186 #define settt_(o,t) ((o)->tt_=(t))
187
188 #define setfltvalue(obj,x) \
189 { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
190
191 #define chgfltvalue(obj,x) \
192 { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
193
194 #define setivalue(obj,x) \
195 { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
196
197 #define chgivalue(obj,x) \
198 { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
199
200 #define setnilvalue(obj) settt_(obj, LUA_TNIL)
201
202 #define setfvalue(obj,x) \
203 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
204
205 #define setpvalue(obj,x) \
206 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
207
208 #define setbvalue(obj,x) \
209 { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
210
211 #define setgcovalue(L,obj,x) \
212 { TValue *io = (obj); GCObject *i_g=(x); \
213 val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
214
215 #define setsvalue(L,obj,x) \
216 { TValue *io = (obj); TString *x_ = (x); \
217 val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
218 checkliveness(G(L),io); }
219
220 #define setuvalue(L,obj,x) \
221 { TValue *io = (obj); Udata *x_ = (x); \
222 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
223 checkliveness(G(L),io); }
224
225 #define setthvalue(L,obj,x) \
226 { TValue *io = (obj); lua_State *x_ = (x); \
227 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
228 checkliveness(G(L),io); }
229
230 #define setclLvalue(L,obj,x) \
231 { TValue *io = (obj); LClosure *x_ = (x); \
232 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
233 checkliveness(G(L),io); }
234
235 #define setclCvalue(L,obj,x) \
236 { TValue *io = (obj); CClosure *x_ = (x); \
237 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
238 checkliveness(G(L),io); }
239
240 #define sethvalue(L,obj,x) \
241 { TValue *io = (obj); Table *x_ = (x); \
242 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \
243 checkliveness(G(L),io); }
244
245 #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
246
247
248
249 #define setobj(L,obj1,obj2) \
250 { TValue *io1=(obj1); *io1 = *(obj2); \
251 (void)L; checkliveness(G(L),io1); }
252
253
254 /*
255 ** different types of assignments, according to destination
256 */
257
258 /* from stack to (same) stack */
259 #define setobjs2s setobj
260 /* to stack (not from same stack) */
261 #define setobj2s setobj
262 #define setsvalue2s setsvalue
263 #define sethvalue2s sethvalue
264 #define setptvalue2s setptvalue
265 /* from table to same table */
266 #define setobjt2t setobj
267 /* to table */
268 #define setobj2t setobj
269 /* to new object */
270 #define setobj2n setobj
271 #define setsvalue2n setsvalue
272
273
274
275
276 /*
277 ** {======================================================
278 ** types and prototypes
279 ** =======================================================
280 */
281
282
283 union Value {
284 GCObject *gc; /* collectable objects */
285 void *p; /* light userdata */
286 int b; /* booleans */
287 lua_CFunction f; /* light C functions */
288 lua_Integer i; /* integer numbers */
289 lua_Number n; /* float numbers */
290 };
291
292
293 struct lua_TValue {
294 TValuefields;
295 };
296
297
298 typedef TValue *StkId; /* index to stack elements */
299
300
301
302
303 /*
304 ** Header for string value; string bytes follow the end of this structure
305 ** (aligned according to 'UTString'; see next).
306 */
307 typedef struct TString {
308 CommonHeader;
309 lu_byte extra; /* reserved words for short strings; "has hash" for longs */
310 lu_byte shrlen; /* length for short strings */
311 unsigned int hash;
312 union {
313 size_t lnglen; /* length for long strings */
314 struct TString *hnext; /* linked list for hash table */
315 } u;
316 } TString;
317
318
319 /*
320 ** Ensures that address after this type is always fully aligned.
321 */
322 typedef union UTString {
323 L_Umaxalign dummy; /* ensures maximum alignment for strings */
324 TString tsv;
325 } UTString;
326
327
328 /*
329 ** Get the actual string (array of bytes) from a 'TString'.
330 ** (Access to 'extra' ensures that value is really a 'TString'.)
331 */
332 #define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString))
333 #define getstr(ts) \
334 check_exp(sizeof((ts)->extra), cast(const char*, getaddrstr(ts)))
335
336 /* get the actual string (array of bytes) from a Lua value */
337 #define svalue(o) getstr(tsvalue(o))
338
339 /* get string length from 'TString *s' */
340 #define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen)
341
342 /* get string length from 'TValue *o' */
343 #define vslen(o) tsslen(tsvalue(o))
344
345
346 /*
347 ** Header for userdata; memory area follows the end of this structure
348 ** (aligned according to 'UUdata'; see next).
349 */
350 typedef struct Udata {
351 CommonHeader;
352 lu_byte ttuv_; /* user value's tag */
353 struct Table *metatable;
354 size_t len; /* number of bytes */
355 union Value user_; /* user value */
356 } Udata;
357
358
359 /*
360 ** Ensures that address after this type is always fully aligned.
361 */
362 typedef union UUdata {
363 L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */
364 Udata uv;
365 } UUdata;
366
367
368 /*
369 ** Get the address of memory block inside 'Udata'.
370 ** (Access to 'ttuv_' ensures that value is really a 'Udata'.)
371 */
372 #define getudatamem(u) \
373 check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata)))
374
375 #define setuservalue(L,u,o) \
376 { const TValue *io=(o); Udata *iu = (u); \
377 iu->user_ = io->value_; iu->ttuv_ = rttype(io); \
378 checkliveness(G(L),io); }
379
380
381 #define getuservalue(L,u,o) \
382 { TValue *io=(o); const Udata *iu = (u); \
383 io->value_ = iu->user_; settt_(io, iu->ttuv_); \
384 checkliveness(G(L),io); }
385
386
387 /*
388 ** Description of an upvalue for function prototypes
389 */
390 typedef struct Upvaldesc {
391 TString *name; /* upvalue name (for debug information) */
392 lu_byte instack; /* whether it is in stack (register) */
393 lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
394 } Upvaldesc;
395
396
397 /*
398 ** Description of a local variable for function prototypes
399 ** (used for debug information)
400 */
401 typedef struct LocVar {
402 TString *varname;
403 int startpc; /* first point where variable is active */
404 int endpc; /* first point where variable is dead */
405 } LocVar;
406
407
408 /*
409 ** Function Prototypes
410 */
411 typedef struct Proto {
412 CommonHeader;
413 lu_byte numparams; /* number of fixed parameters */
414 lu_byte is_vararg;
415 lu_byte maxstacksize; /* number of registers needed by this function */
416 int sizeupvalues; /* size of 'upvalues' */
417 int sizek; /* size of 'k' */
418 int sizecode;
419 int sizelineinfo;
420 int sizep; /* size of 'p' */
421 int sizelocvars;
422 int linedefined;
423 int lastlinedefined;
424 TValue *k; /* constants used by the function */
425 Instruction *code; /* opcodes */
426 struct Proto **p; /* functions defined inside the function */
427 int *lineinfo; /* map from opcodes to source lines (debug information) */
428 LocVar *locvars; /* information about local variables (debug information) */
429 Upvaldesc *upvalues; /* upvalue information */
430 struct LClosure *cache; /* last-created closure with this prototype */
431 TString *source; /* used for debug information */
432 GCObject *gclist;
433 } Proto;
434
435
436
437 /*
438 ** Lua Upvalues
439 */
440 typedef struct UpVal UpVal;
441
442
443 /*
444 ** Closures
445 */
446
447 #define ClosureHeader \
448 CommonHeader; lu_byte nupvalues; GCObject *gclist
449
450 typedef struct CClosure {
451 ClosureHeader;
452 lua_CFunction f;
453 TValue upvalue[1]; /* list of upvalues */
454 } CClosure;
455
456
457 typedef struct LClosure {
458 ClosureHeader;
459 struct Proto *p;
460 UpVal *upvals[1]; /* list of upvalues */
461 } LClosure;
462
463
464 typedef union Closure {
465 CClosure c;
466 LClosure l;
467 } Closure;
468
469
470 #define isLfunction(o) ttisLclosure(o)
471
472 #define getproto(o) (clLvalue(o)->p)
473
474
475 /*
476 ** Tables
477 */
478
479 typedef union TKey {
480 struct {
481 TValuefields;
482 int next; /* for chaining (offset for next node) */
483 } nk;
484 TValue tvk;
485 } TKey;
486
487
488 /* copy a value into a key without messing up field 'next' */
489 #define setnodekey(L,key,obj) \
490 { TKey *k_=(key); const TValue *io_=(obj); \
491 k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \
492 (void)L; checkliveness(G(L),io_); }
493
494
495 typedef struct Node {
496 TValue i_val;
497 TKey i_key;
498 } Node;
499
500
501 typedef struct Table {
502 CommonHeader;
503 lu_byte flags; /* 1<<p means tagmethod(p) is not present */
504 lu_byte lsizenode; /* log2 of size of 'node' array */
505 unsigned int sizearray; /* size of 'array' array */
506 TValue *array; /* array part */
507 Node *node;
508 Node *lastfree; /* any free position is before this position */
509 struct Table *metatable;
510 GCObject *gclist;
511 } Table;
512
513
514
515 /*
516 ** 'module' operation for hashing (size is always a power of 2)
517 */
518 #define lmod(s,size) \
519 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
520
521
522 #define twoto(x) (1<<(x))
523 #define sizenode(t) (twoto((t)->lsizenode))
524
525
526 /*
527 ** (address of) a fixed nil value
528 */
529 #define luaO_nilobject (&luaO_nilobject_)
530
531
532 LUAI_DDEC const TValue luaO_nilobject_;
533
534 /* size of buffer for 'luaO_utf8esc' function */
535 #define UTF8BUFFSZ 8
536
537 LUAI_FUNC int luaO_int2fb (unsigned int x);
538 LUAI_FUNC int luaO_fb2int (int x);
539 LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
540 LUAI_FUNC int luaO_ceillog2 (unsigned int x);
541 LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
542 const TValue *p2, TValue *res);
543 LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
544 LUAI_FUNC int luaO_hexavalue (int c);
545 LUAI_FUNC void luaO_tostring (lua_State *L, StkId obj);
546 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
547 va_list argp);
548 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
549 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
550
551
552 #endif
553