]> git.zerfleddert.de Git - proxmark3-svn/blob - liblua/lobject.h
cmdhf14b.c: fixing a crash in the crc computation when missing data (e.g. with 'hf...
[proxmark3-svn] / liblua / lobject.h
1 /*
2 ** $Id: lobject.h,v 2.71 2012/09/11 18:21:44 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_TUPVAL (LUA_NUMTAGS+1)
24 #define LUA_TDEADKEY (LUA_NUMTAGS+2)
25
26 /*
27 ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
28 */
29 #define LUA_TOTALTAGS (LUA_TUPVAL+2)
30
31
32 /*
33 ** tags for Tagged Values have the following use of bits:
34 ** bits 0-3: actual tag (a LUA_T* value)
35 ** bits 4-5: variant bits
36 ** bit 6: whether value is collectable
37 */
38
39 #define VARBITS (3 << 4)
40
41
42 /*
43 ** LUA_TFUNCTION variants:
44 ** 0 - Lua function
45 ** 1 - light C function
46 ** 2 - regular C function (closure)
47 */
48
49 /* Variant tags for functions */
50 #define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */
51 #define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */
52 #define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
53
54
55 /* Variant tags for strings */
56 #define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */
57 #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
58
59
60 /* Bit mark for collectable types */
61 #define BIT_ISCOLLECTABLE (1 << 6)
62
63 /* mark a tag as collectable */
64 #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
65
66
67 /*
68 ** Union of all collectable objects
69 */
70 typedef union GCObject GCObject;
71
72
73 /*
74 ** Common Header for all collectable objects (in macro form, to be
75 ** included in other objects)
76 */
77 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
78
79
80 /*
81 ** Common header in struct form
82 */
83 typedef struct GCheader {
84 CommonHeader;
85 } GCheader;
86
87
88
89 /*
90 ** Union of all Lua values
91 */
92 typedef union Value Value;
93
94
95 #define numfield lua_Number n; /* numbers */
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 #define num_(o) (val_(o).n)
115
116
117 /* raw type tag of a TValue */
118 #define rttype(o) ((o)->tt_)
119
120 /* tag with no variants (bits 0-3) */
121 #define novariant(x) ((x) & 0x0F)
122
123 /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
124 #define ttype(o) (rttype(o) & 0x3F)
125
126 /* type tag of a TValue with no variants (bits 0-3) */
127 #define ttypenv(o) (novariant(rttype(o)))
128
129
130 /* Macros to test type */
131 #define checktag(o,t) (rttype(o) == (t))
132 #define checktype(o,t) (ttypenv(o) == (t))
133 #define ttisnumber(o) checktag((o), LUA_TNUMBER)
134 #define ttisnil(o) checktag((o), LUA_TNIL)
135 #define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
136 #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
137 #define ttisstring(o) checktype((o), LUA_TSTRING)
138 #define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
139 #define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
140 #define ttistable(o) checktag((o), ctb(LUA_TTABLE))
141 #define ttisfunction(o) checktype(o, LUA_TFUNCTION)
142 #define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
143 #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
144 #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
145 #define ttislcf(o) checktag((o), LUA_TLCF)
146 #define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA))
147 #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
148 #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)
149
150 #define ttisequal(o1,o2) (rttype(o1) == rttype(o2))
151
152 /* Macros to access values */
153 #define nvalue(o) check_exp(ttisnumber(o), num_(o))
154 #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
155 #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
156 #define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts)
157 #define tsvalue(o) (&rawtsvalue(o)->tsv)
158 #define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u)
159 #define uvalue(o) (&rawuvalue(o)->uv)
160 #define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl)
161 #define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
162 #define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
163 #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
164 #define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h)
165 #define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
166 #define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th)
167 /* a dead value may get the 'gc' field, but cannot access its contents */
168 #define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
169
170 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
171
172
173 #define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
174
175
176 /* Macros for internal tests */
177 #define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt)
178
179 #define checkliveness(g,obj) \
180 lua_longassert(!iscollectable(obj) || \
181 (righttt(obj) && !isdead(g,gcvalue(obj))))
182
183
184 /* Macros to set values */
185 #define settt_(o,t) ((o)->tt_=(t))
186
187 #define setnvalue(obj,x) \
188 { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
189
190 #define setnilvalue(obj) settt_(obj, LUA_TNIL)
191
192 #define setfvalue(obj,x) \
193 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
194
195 #define setpvalue(obj,x) \
196 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
197
198 #define setbvalue(obj,x) \
199 { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
200
201 #define setgcovalue(L,obj,x) \
202 { TValue *io=(obj); GCObject *i_g=(x); \
203 val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
204
205 #define setsvalue(L,obj,x) \
206 { TValue *io=(obj); \
207 TString *x_ = (x); \
208 val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \
209 checkliveness(G(L),io); }
210
211 #define setuvalue(L,obj,x) \
212 { TValue *io=(obj); \
213 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
214 checkliveness(G(L),io); }
215
216 #define setthvalue(L,obj,x) \
217 { TValue *io=(obj); \
218 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
219 checkliveness(G(L),io); }
220
221 #define setclLvalue(L,obj,x) \
222 { TValue *io=(obj); \
223 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
224 checkliveness(G(L),io); }
225
226 #define setclCvalue(L,obj,x) \
227 { TValue *io=(obj); \
228 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
229 checkliveness(G(L),io); }
230
231 #define sethvalue(L,obj,x) \
232 { TValue *io=(obj); \
233 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
234 checkliveness(G(L),io); }
235
236 #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
237
238
239
240 #define setobj(L,obj1,obj2) \
241 { const TValue *io2=(obj2); TValue *io1=(obj1); \
242 io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
243 checkliveness(G(L),io1); }
244
245
246 /*
247 ** different types of assignments, according to destination
248 */
249
250 /* from stack to (same) stack */
251 #define setobjs2s setobj
252 /* to stack (not from same stack) */
253 #define setobj2s setobj
254 #define setsvalue2s setsvalue
255 #define sethvalue2s sethvalue
256 #define setptvalue2s setptvalue
257 /* from table to same table */
258 #define setobjt2t setobj
259 /* to table */
260 #define setobj2t setobj
261 /* to new object */
262 #define setobj2n setobj
263 #define setsvalue2n setsvalue
264
265
266 /* check whether a number is valid (useful only for NaN trick) */
267 #define luai_checknum(L,o,c) { /* empty */ }
268
269
270 /*
271 ** {======================================================
272 ** NaN Trick
273 ** =======================================================
274 */
275 #if defined(LUA_NANTRICK)
276
277 /*
278 ** numbers are represented in the 'd_' field. All other values have the
279 ** value (NNMARK | tag) in 'tt__'. A number with such pattern would be
280 ** a "signaled NaN", which is never generated by regular operations by
281 ** the CPU (nor by 'strtod')
282 */
283
284 /* allows for external implementation for part of the trick */
285 #if !defined(NNMARK) /* { */
286
287
288 #if !defined(LUA_IEEEENDIAN)
289 #error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN'
290 #endif
291
292
293 #define NNMARK 0x7FF7A500
294 #define NNMASK 0x7FFFFF00
295
296 #undef TValuefields
297 #undef NILCONSTANT
298
299 #if (LUA_IEEEENDIAN == 0) /* { */
300
301 /* little endian */
302 #define TValuefields \
303 union { struct { Value v__; int tt__; } i; double d__; } u
304 #define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}}
305 /* field-access macros */
306 #define v_(o) ((o)->u.i.v__)
307 #define d_(o) ((o)->u.d__)
308 #define tt_(o) ((o)->u.i.tt__)
309
310 #else /* }{ */
311
312 /* big endian */
313 #define TValuefields \
314 union { struct { int tt__; Value v__; } i; double d__; } u
315 #define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}}
316 /* field-access macros */
317 #define v_(o) ((o)->u.i.v__)
318 #define d_(o) ((o)->u.d__)
319 #define tt_(o) ((o)->u.i.tt__)
320
321 #endif /* } */
322
323 #endif /* } */
324
325
326 /* correspondence with standard representation */
327 #undef val_
328 #define val_(o) v_(o)
329 #undef num_
330 #define num_(o) d_(o)
331
332
333 #undef numfield
334 #define numfield /* no such field; numbers are the entire struct */
335
336 /* basic check to distinguish numbers from non-numbers */
337 #undef ttisnumber
338 #define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK)
339
340 #define tag2tt(t) (NNMARK | (t))
341
342 #undef rttype
343 #define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff)
344
345 #undef settt_
346 #define settt_(o,t) (tt_(o) = tag2tt(t))
347
348 #undef setnvalue
349 #define setnvalue(obj,x) \
350 { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }
351
352 #undef setobj
353 #define setobj(L,obj1,obj2) \
354 { const TValue *o2_=(obj2); TValue *o1_=(obj1); \
355 o1_->u = o2_->u; \
356 checkliveness(G(L),o1_); }
357
358
359 /*
360 ** these redefinitions are not mandatory, but these forms are more efficient
361 */
362
363 #undef checktag
364 #undef checktype
365 #define checktag(o,t) (tt_(o) == tag2tt(t))
366 #define checktype(o,t) (ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS))
367
368 #undef ttisequal
369 #define ttisequal(o1,o2) \
370 (ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))
371
372
373 #undef luai_checknum
374 #define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; }
375
376 #endif
377 /* }====================================================== */
378
379
380
381 /*
382 ** {======================================================
383 ** types and prototypes
384 ** =======================================================
385 */
386
387
388 union Value {
389 GCObject *gc; /* collectable objects */
390 void *p; /* light userdata */
391 int b; /* booleans */
392 lua_CFunction f; /* light C functions */
393 numfield /* numbers */
394 };
395
396
397 struct lua_TValue {
398 TValuefields;
399 };
400
401
402 typedef TValue *StkId; /* index to stack elements */
403
404
405
406
407 /*
408 ** Header for string value; string bytes follow the end of this structure
409 */
410 typedef union TString {
411 L_Umaxalign dummy; /* ensures maximum alignment for strings */
412 struct {
413 CommonHeader;
414 lu_byte extra; /* reserved words for short strings; "has hash" for longs */
415 unsigned int hash;
416 size_t len; /* number of characters in string */
417 } tsv;
418 } TString;
419
420
421 /* get the actual string (array of bytes) from a TString */
422 #define getstr(ts) cast(const char *, (ts) + 1)
423
424 /* get the actual string (array of bytes) from a Lua value */
425 #define svalue(o) getstr(rawtsvalue(o))
426
427
428 /*
429 ** Header for userdata; memory area follows the end of this structure
430 */
431 typedef union Udata {
432 L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
433 struct {
434 CommonHeader;
435 struct Table *metatable;
436 struct Table *env;
437 size_t len; /* number of bytes */
438 } uv;
439 } Udata;
440
441
442
443 /*
444 ** Description of an upvalue for function prototypes
445 */
446 typedef struct Upvaldesc {
447 TString *name; /* upvalue name (for debug information) */
448 lu_byte instack; /* whether it is in stack */
449 lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
450 } Upvaldesc;
451
452
453 /*
454 ** Description of a local variable for function prototypes
455 ** (used for debug information)
456 */
457 typedef struct LocVar {
458 TString *varname;
459 int startpc; /* first point where variable is active */
460 int endpc; /* first point where variable is dead */
461 } LocVar;
462
463
464 /*
465 ** Function Prototypes
466 */
467 typedef struct Proto {
468 CommonHeader;
469 TValue *k; /* constants used by the function */
470 Instruction *code;
471 struct Proto **p; /* functions defined inside the function */
472 int *lineinfo; /* map from opcodes to source lines (debug information) */
473 LocVar *locvars; /* information about local variables (debug information) */
474 Upvaldesc *upvalues; /* upvalue information */
475 union Closure *cache; /* last created closure with this prototype */
476 TString *source; /* used for debug information */
477 int sizeupvalues; /* size of 'upvalues' */
478 int sizek; /* size of `k' */
479 int sizecode;
480 int sizelineinfo;
481 int sizep; /* size of `p' */
482 int sizelocvars;
483 int linedefined;
484 int lastlinedefined;
485 GCObject *gclist;
486 lu_byte numparams; /* number of fixed parameters */
487 lu_byte is_vararg;
488 lu_byte maxstacksize; /* maximum stack used by this function */
489 } Proto;
490
491
492
493 /*
494 ** Lua Upvalues
495 */
496 typedef struct UpVal {
497 CommonHeader;
498 TValue *v; /* points to stack or to its own value */
499 union {
500 TValue value; /* the value (when closed) */
501 struct { /* double linked list (when open) */
502 struct UpVal *prev;
503 struct UpVal *next;
504 } l;
505 } u;
506 } UpVal;
507
508
509 /*
510 ** Closures
511 */
512
513 #define ClosureHeader \
514 CommonHeader; lu_byte nupvalues; GCObject *gclist
515
516 typedef struct CClosure {
517 ClosureHeader;
518 lua_CFunction f;
519 TValue upvalue[1]; /* list of upvalues */
520 } CClosure;
521
522
523 typedef struct LClosure {
524 ClosureHeader;
525 struct Proto *p;
526 UpVal *upvals[1]; /* list of upvalues */
527 } LClosure;
528
529
530 typedef union Closure {
531 CClosure c;
532 LClosure l;
533 } Closure;
534
535
536 #define isLfunction(o) ttisLclosure(o)
537
538 #define getproto(o) (clLvalue(o)->p)
539
540
541 /*
542 ** Tables
543 */
544
545 typedef union TKey {
546 struct {
547 TValuefields;
548 struct Node *next; /* for chaining */
549 } nk;
550 TValue tvk;
551 } TKey;
552
553
554 typedef struct Node {
555 TValue i_val;
556 TKey i_key;
557 } Node;
558
559
560 typedef struct Table {
561 CommonHeader;
562 lu_byte flags; /* 1<<p means tagmethod(p) is not present */
563 lu_byte lsizenode; /* log2 of size of `node' array */
564 struct Table *metatable;
565 TValue *array; /* array part */
566 Node *node;
567 Node *lastfree; /* any free position is before this position */
568 GCObject *gclist;
569 int sizearray; /* size of `array' array */
570 } Table;
571
572
573
574 /*
575 ** `module' operation for hashing (size is always a power of 2)
576 */
577 #define lmod(s,size) \
578 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
579
580
581 #define twoto(x) (1<<(x))
582 #define sizenode(t) (twoto((t)->lsizenode))
583
584
585 /*
586 ** (address of) a fixed nil value
587 */
588 #define luaO_nilobject (&luaO_nilobject_)
589
590
591 LUAI_DDEC const TValue luaO_nilobject_;
592
593
594 LUAI_FUNC int luaO_int2fb (unsigned int x);
595 LUAI_FUNC int luaO_fb2int (int x);
596 LUAI_FUNC int luaO_ceillog2 (unsigned int x);
597 LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
598 LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
599 LUAI_FUNC int luaO_hexavalue (int c);
600 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
601 va_list argp);
602 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
603 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
604
605
606 #endif
607
Impressum, Datenschutz