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