Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

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

553
  lu_byte lsizenode;  /* log2 of size of `node' array */
554
  struct Table *metatable;
555
  TValue *array;  /* array part */
556
  Node *node;
557
  Node *lastfree;  /* any free position is before this position */
558
  GCObject *gclist;
559
  int sizearray;  /* size of `array' array */
560
} Table;
561
 
562
 
563
 
564
/*
565
** `module' operation for hashing (size is always a power of 2)
566
*/
567
#define lmod(s,size) \
568
	(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
569
 
570
 
571
#define twoto(x)	(1<<(x))
572
#define sizenode(t)	(twoto((t)->lsizenode))
573
 
574
 
575
/*
576
** (address of) a fixed nil value
577
*/
578
#define luaO_nilobject		(&luaO_nilobject_)
579
 
580
 
581
LUAI_DDEC const TValue luaO_nilobject_;
582
 
583
 
584
LUAI_FUNC int luaO_int2fb (unsigned int x);
585
LUAI_FUNC int luaO_fb2int (int x);
586
LUAI_FUNC int luaO_ceillog2 (unsigned int x);
587
LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
588
LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
589
LUAI_FUNC int luaO_hexavalue (int c);
590
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
591
                                                       va_list argp);
592
LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
593
LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
594
 
595
 
596
#endif
597