1 | /* |
2 | ** $Id: lobject.h $ |
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 types for collectable non-values |
21 | */ |
22 | #define LUA_TUPVAL LUA_NUMTYPES /* upvalues */ |
23 | #define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */ |
24 | #define LUA_TDEADKEY (LUA_NUMTYPES+2) /* removed keys in tables */ |
25 | |
26 | |
27 | |
28 | /* |
29 | ** number of all possible types (including LUA_TNONE but excluding DEADKEY) |
30 | */ |
31 | #define LUA_TOTALTYPES (LUA_TPROTO + 2) |
32 | |
33 | |
34 | /* |
35 | ** tags for Tagged Values have the following use of bits: |
36 | ** bits 0-3: actual tag (a LUA_T* constant) |
37 | ** bits 4-5: variant bits |
38 | ** bit 6: whether value is collectable |
39 | */ |
40 | |
41 | /* add variant bits to a type */ |
42 | #define makevariant(t,v) ((t) | ((v) << 4)) |
43 | |
44 | |
45 | |
46 | /* |
47 | ** Union of all Lua values |
48 | */ |
49 | typedef union Value { |
50 | struct GCObject *gc; /* collectable objects */ |
51 | void *p; /* light userdata */ |
52 | lua_CFunction f; /* light C functions */ |
53 | lua_Integer i; /* integer numbers */ |
54 | lua_Number n; /* float numbers */ |
55 | } Value; |
56 | |
57 | |
58 | /* |
59 | ** Tagged Values. This is the basic representation of values in Lua: |
60 | ** an actual value plus a tag with its type. |
61 | */ |
62 | |
63 | #define TValuefields Value value_; lu_byte tt_ |
64 | |
65 | typedef struct TValue { |
66 | TValuefields; |
67 | } TValue; |
68 | |
69 | |
70 | #define val_(o) ((o)->value_) |
71 | #define valraw(o) (&val_(o)) |
72 | |
73 | |
74 | /* raw type tag of a TValue */ |
75 | #define rawtt(o) ((o)->tt_) |
76 | |
77 | /* tag with no variants (bits 0-3) */ |
78 | #define novariant(t) ((t) & 0x0F) |
79 | |
80 | /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ |
81 | #define withvariant(t) ((t) & 0x3F) |
82 | #define ttypetag(o) withvariant(rawtt(o)) |
83 | |
84 | /* type of a TValue */ |
85 | #define ttype(o) (novariant(rawtt(o))) |
86 | |
87 | |
88 | /* Macros to test type */ |
89 | #define checktag(o,t) (rawtt(o) == (t)) |
90 | #define checktype(o,t) (ttype(o) == (t)) |
91 | |
92 | |
93 | /* Macros for internal tests */ |
94 | |
95 | /* collectable object has the same tag as the original value */ |
96 | #define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt) |
97 | |
98 | /* |
99 | ** Any value being manipulated by the program either is non |
100 | ** collectable, or the collectable object has the right tag |
101 | ** and it is not dead. The option 'L == NULL' allows other |
102 | ** macros using this one to be used where L is not available. |
103 | */ |
104 | #define checkliveness(L,obj) \ |
105 | ((void)L, lua_longassert(!iscollectable(obj) || \ |
106 | (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))) |
107 | |
108 | |
109 | /* Macros to set values */ |
110 | |
111 | /* set a value's tag */ |
112 | #define settt_(o,t) ((o)->tt_=(t)) |
113 | |
114 | |
115 | /* main macro to copy values (from 'obj1' to 'obj2') */ |
116 | #define setobj(L,obj1,obj2) \ |
117 | { TValue *io1=(obj1); const TValue *io2=(obj2); \ |
118 | io1->value_ = io2->value_; settt_(io1, io2->tt_); \ |
119 | checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } |
120 | |
121 | /* |
122 | ** Different types of assignments, according to source and destination. |
123 | ** (They are mostly equal now, but may be different in the future.) |
124 | */ |
125 | |
126 | /* from stack to stack */ |
127 | #define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2)) |
128 | /* to stack (not from same stack) */ |
129 | #define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2) |
130 | /* from table to same table */ |
131 | #define setobjt2t setobj |
132 | /* to new object */ |
133 | #define setobj2n setobj |
134 | /* to table */ |
135 | #define setobj2t setobj |
136 | |
137 | |
138 | /* |
139 | ** Entries in a Lua stack. Field 'tbclist' forms a list of all |
140 | ** to-be-closed variables active in this stack. Dummy entries are |
141 | ** used when the distance between two tbc variables does not fit |
142 | ** in an unsigned short. They are represented by delta==0, and |
143 | ** their real delta is always the maximum value that fits in |
144 | ** that field. |
145 | */ |
146 | typedef union StackValue { |
147 | TValue val; |
148 | struct { |
149 | TValuefields; |
150 | unsigned short delta; |
151 | } tbclist; |
152 | } StackValue; |
153 | |
154 | |
155 | /* index to stack elements */ |
156 | typedef StackValue *StkId; |
157 | |
158 | /* convert a 'StackValue' to a 'TValue' */ |
159 | #define s2v(o) (&(o)->val) |
160 | |
161 | |
162 | |
163 | /* |
164 | ** {================================================================== |
165 | ** Nil |
166 | ** =================================================================== |
167 | */ |
168 | |
169 | /* Standard nil */ |
170 | #define LUA_VNIL makevariant(LUA_TNIL, 0) |
171 | |
172 | /* Empty slot (which might be different from a slot containing nil) */ |
173 | #define LUA_VEMPTY makevariant(LUA_TNIL, 1) |
174 | |
175 | /* Value returned for a key not found in a table (absent key) */ |
176 | #define LUA_VABSTKEY makevariant(LUA_TNIL, 2) |
177 | |
178 | |
179 | /* macro to test for (any kind of) nil */ |
180 | #define ttisnil(v) checktype((v), LUA_TNIL) |
181 | |
182 | |
183 | /* macro to test for a standard nil */ |
184 | #define ttisstrictnil(o) checktag((o), LUA_VNIL) |
185 | |
186 | |
187 | #define setnilvalue(obj) settt_(obj, LUA_VNIL) |
188 | |
189 | |
190 | #define isabstkey(v) checktag((v), LUA_VABSTKEY) |
191 | |
192 | |
193 | /* |
194 | ** macro to detect non-standard nils (used only in assertions) |
195 | */ |
196 | #define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v)) |
197 | |
198 | |
199 | /* |
200 | ** By default, entries with any kind of nil are considered empty. |
201 | ** (In any definition, values associated with absent keys must also |
202 | ** be accepted as empty.) |
203 | */ |
204 | #define isempty(v) ttisnil(v) |
205 | |
206 | |
207 | /* macro defining a value corresponding to an absent key */ |
208 | #define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY |
209 | |
210 | |
211 | /* mark an entry as empty */ |
212 | #define setempty(v) settt_(v, LUA_VEMPTY) |
213 | |
214 | |
215 | |
216 | /* }================================================================== */ |
217 | |
218 | |
219 | /* |
220 | ** {================================================================== |
221 | ** Booleans |
222 | ** =================================================================== |
223 | */ |
224 | |
225 | |
226 | #define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0) |
227 | #define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1) |
228 | |
229 | #define ttisboolean(o) checktype((o), LUA_TBOOLEAN) |
230 | #define ttisfalse(o) checktag((o), LUA_VFALSE) |
231 | #define ttistrue(o) checktag((o), LUA_VTRUE) |
232 | |
233 | |
234 | #define l_isfalse(o) (ttisfalse(o) || ttisnil(o)) |
235 | |
236 | |
237 | #define setbfvalue(obj) settt_(obj, LUA_VFALSE) |
238 | #define setbtvalue(obj) settt_(obj, LUA_VTRUE) |
239 | |
240 | /* }================================================================== */ |
241 | |
242 | |
243 | /* |
244 | ** {================================================================== |
245 | ** Threads |
246 | ** =================================================================== |
247 | */ |
248 | |
249 | #define LUA_VTHREAD makevariant(LUA_TTHREAD, 0) |
250 | |
251 | #define ttisthread(o) checktag((o), ctb(LUA_VTHREAD)) |
252 | |
253 | #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) |
254 | |
255 | #define setthvalue(L,obj,x) \ |
256 | { TValue *io = (obj); lua_State *x_ = (x); \ |
257 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \ |
258 | checkliveness(L,io); } |
259 | |
260 | #define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t) |
261 | |
262 | /* }================================================================== */ |
263 | |
264 | |
265 | /* |
266 | ** {================================================================== |
267 | ** Collectable Objects |
268 | ** =================================================================== |
269 | */ |
270 | |
271 | /* |
272 | ** Common Header for all collectable objects (in macro form, to be |
273 | ** included in other objects) |
274 | */ |
275 | #define struct GCObject *next; lu_byte tt; lu_byte marked |
276 | |
277 | |
278 | /* Common type for all collectable objects */ |
279 | typedef struct GCObject { |
280 | CommonHeader; |
281 | } GCObject; |
282 | |
283 | |
284 | /* Bit mark for collectable types */ |
285 | #define BIT_ISCOLLECTABLE (1 << 6) |
286 | |
287 | #define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE) |
288 | |
289 | /* mark a tag as collectable */ |
290 | #define ctb(t) ((t) | BIT_ISCOLLECTABLE) |
291 | |
292 | #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) |
293 | |
294 | #define gcvalueraw(v) ((v).gc) |
295 | |
296 | #define setgcovalue(L,obj,x) \ |
297 | { TValue *io = (obj); GCObject *i_g=(x); \ |
298 | val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } |
299 | |
300 | /* }================================================================== */ |
301 | |
302 | |
303 | /* |
304 | ** {================================================================== |
305 | ** Numbers |
306 | ** =================================================================== |
307 | */ |
308 | |
309 | /* Variant tags for numbers */ |
310 | #define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */ |
311 | #define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */ |
312 | |
313 | #define ttisnumber(o) checktype((o), LUA_TNUMBER) |
314 | #define ttisfloat(o) checktag((o), LUA_VNUMFLT) |
315 | #define ttisinteger(o) checktag((o), LUA_VNUMINT) |
316 | |
317 | #define nvalue(o) check_exp(ttisnumber(o), \ |
318 | (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) |
319 | #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) |
320 | #define ivalue(o) check_exp(ttisinteger(o), val_(o).i) |
321 | |
322 | #define fltvalueraw(v) ((v).n) |
323 | #define ivalueraw(v) ((v).i) |
324 | |
325 | #define setfltvalue(obj,x) \ |
326 | { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); } |
327 | |
328 | #define chgfltvalue(obj,x) \ |
329 | { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } |
330 | |
331 | #define setivalue(obj,x) \ |
332 | { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); } |
333 | |
334 | #define chgivalue(obj,x) \ |
335 | { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } |
336 | |
337 | /* }================================================================== */ |
338 | |
339 | |
340 | /* |
341 | ** {================================================================== |
342 | ** Strings |
343 | ** =================================================================== |
344 | */ |
345 | |
346 | /* Variant tags for strings */ |
347 | #define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */ |
348 | #define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */ |
349 | |
350 | #define ttisstring(o) checktype((o), LUA_TSTRING) |
351 | #define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR)) |
352 | #define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR)) |
353 | |
354 | #define tsvalueraw(v) (gco2ts((v).gc)) |
355 | |
356 | #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) |
357 | |
358 | #define setsvalue(L,obj,x) \ |
359 | { TValue *io = (obj); TString *x_ = (x); \ |
360 | val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ |
361 | checkliveness(L,io); } |
362 | |
363 | /* set a string to the stack */ |
364 | #define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s) |
365 | |
366 | /* set a string to a new object */ |
367 | #define setsvalue2n setsvalue |
368 | |
369 | |
370 | /* |
371 | ** Header for a string value. |
372 | */ |
373 | typedef struct TString { |
374 | CommonHeader; |
375 | lu_byte ; /* reserved words for short strings; "has hash" for longs */ |
376 | lu_byte shrlen; /* length for short strings */ |
377 | unsigned int hash; |
378 | union { |
379 | size_t lnglen; /* length for long strings */ |
380 | struct TString *hnext; /* linked list for hash table */ |
381 | } u; |
382 | char contents[1]; |
383 | } TString; |
384 | |
385 | |
386 | |
387 | /* |
388 | ** Get the actual string (array of bytes) from a 'TString'. |
389 | */ |
390 | #define getstr(ts) ((ts)->contents) |
391 | |
392 | |
393 | /* get the actual string (array of bytes) from a Lua value */ |
394 | #define svalue(o) getstr(tsvalue(o)) |
395 | |
396 | /* get string length from 'TString *s' */ |
397 | #define tsslen(s) ((s)->tt == LUA_VSHRSTR ? (s)->shrlen : (s)->u.lnglen) |
398 | |
399 | /* get string length from 'TValue *o' */ |
400 | #define vslen(o) tsslen(tsvalue(o)) |
401 | |
402 | /* }================================================================== */ |
403 | |
404 | |
405 | /* |
406 | ** {================================================================== |
407 | ** Userdata |
408 | ** =================================================================== |
409 | */ |
410 | |
411 | |
412 | /* |
413 | ** Light userdata should be a variant of userdata, but for compatibility |
414 | ** reasons they are also different types. |
415 | */ |
416 | #define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0) |
417 | |
418 | #define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0) |
419 | |
420 | #define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA) |
421 | #define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA)) |
422 | |
423 | #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) |
424 | #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) |
425 | |
426 | #define pvalueraw(v) ((v).p) |
427 | |
428 | #define setpvalue(obj,x) \ |
429 | { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); } |
430 | |
431 | #define setuvalue(L,obj,x) \ |
432 | { TValue *io = (obj); Udata *x_ = (x); \ |
433 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \ |
434 | checkliveness(L,io); } |
435 | |
436 | |
437 | /* Ensures that addresses after this type are always fully aligned. */ |
438 | typedef union UValue { |
439 | TValue uv; |
440 | LUAI_MAXALIGN; /* ensures maximum alignment for udata bytes */ |
441 | } UValue; |
442 | |
443 | |
444 | /* |
445 | ** Header for userdata with user values; |
446 | ** memory area follows the end of this structure. |
447 | */ |
448 | typedef struct Udata { |
449 | CommonHeader; |
450 | unsigned short nuvalue; /* number of user values */ |
451 | size_t len; /* number of bytes */ |
452 | struct Table *metatable; |
453 | GCObject *gclist; |
454 | UValue uv[1]; /* user values */ |
455 | } Udata; |
456 | |
457 | |
458 | /* |
459 | ** Header for userdata with no user values. These userdata do not need |
460 | ** to be gray during GC, and therefore do not need a 'gclist' field. |
461 | ** To simplify, the code always use 'Udata' for both kinds of userdata, |
462 | ** making sure it never accesses 'gclist' on userdata with no user values. |
463 | ** This structure here is used only to compute the correct size for |
464 | ** this representation. (The 'bindata' field in its end ensures correct |
465 | ** alignment for binary data following this header.) |
466 | */ |
467 | typedef struct Udata0 { |
468 | CommonHeader; |
469 | unsigned short nuvalue; /* number of user values */ |
470 | size_t len; /* number of bytes */ |
471 | struct Table *metatable; |
472 | union {LUAI_MAXALIGN;} bindata; |
473 | } Udata0; |
474 | |
475 | |
476 | /* compute the offset of the memory area of a userdata */ |
477 | #define udatamemoffset(nuv) \ |
478 | ((nuv) == 0 ? offsetof(Udata0, bindata) \ |
479 | : offsetof(Udata, uv) + (sizeof(UValue) * (nuv))) |
480 | |
481 | /* get the address of the memory block inside 'Udata' */ |
482 | #define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue)) |
483 | |
484 | /* compute the size of a userdata */ |
485 | #define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb)) |
486 | |
487 | /* }================================================================== */ |
488 | |
489 | |
490 | /* |
491 | ** {================================================================== |
492 | ** Prototypes |
493 | ** =================================================================== |
494 | */ |
495 | |
496 | #define LUA_VPROTO makevariant(LUA_TPROTO, 0) |
497 | |
498 | |
499 | /* |
500 | ** Description of an upvalue for function prototypes |
501 | */ |
502 | typedef struct Upvaldesc { |
503 | TString *name; /* upvalue name (for debug information) */ |
504 | lu_byte instack; /* whether it is in stack (register) */ |
505 | lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ |
506 | lu_byte kind; /* kind of corresponding variable */ |
507 | } Upvaldesc; |
508 | |
509 | |
510 | /* |
511 | ** Description of a local variable for function prototypes |
512 | ** (used for debug information) |
513 | */ |
514 | typedef struct LocVar { |
515 | TString *varname; |
516 | int startpc; /* first point where variable is active */ |
517 | int endpc; /* first point where variable is dead */ |
518 | } LocVar; |
519 | |
520 | |
521 | /* |
522 | ** Associates the absolute line source for a given instruction ('pc'). |
523 | ** The array 'lineinfo' gives, for each instruction, the difference in |
524 | ** lines from the previous instruction. When that difference does not |
525 | ** fit into a byte, Lua saves the absolute line for that instruction. |
526 | ** (Lua also saves the absolute line periodically, to speed up the |
527 | ** computation of a line number: we can use binary search in the |
528 | ** absolute-line array, but we must traverse the 'lineinfo' array |
529 | ** linearly to compute a line.) |
530 | */ |
531 | typedef struct AbsLineInfo { |
532 | int pc; |
533 | int line; |
534 | } AbsLineInfo; |
535 | |
536 | /* |
537 | ** Function Prototypes |
538 | */ |
539 | typedef struct Proto { |
540 | CommonHeader; |
541 | lu_byte numparams; /* number of fixed (named) parameters */ |
542 | lu_byte is_vararg; |
543 | lu_byte maxstacksize; /* number of registers needed by this function */ |
544 | int sizeupvalues; /* size of 'upvalues' */ |
545 | int sizek; /* size of 'k' */ |
546 | int sizecode; |
547 | int sizelineinfo; |
548 | int sizep; /* size of 'p' */ |
549 | int sizelocvars; |
550 | int sizeabslineinfo; /* size of 'abslineinfo' */ |
551 | int linedefined; /* debug information */ |
552 | int lastlinedefined; /* debug information */ |
553 | TValue *k; /* constants used by the function */ |
554 | Instruction *code; /* opcodes */ |
555 | struct Proto **p; /* functions defined inside the function */ |
556 | Upvaldesc *upvalues; /* upvalue information */ |
557 | ls_byte *lineinfo; /* information about source lines (debug information) */ |
558 | AbsLineInfo *abslineinfo; /* idem */ |
559 | LocVar *locvars; /* information about local variables (debug information) */ |
560 | TString *source; /* used for debug information */ |
561 | GCObject *gclist; |
562 | } Proto; |
563 | |
564 | /* }================================================================== */ |
565 | |
566 | |
567 | /* |
568 | ** {================================================================== |
569 | ** Functions |
570 | ** =================================================================== |
571 | */ |
572 | |
573 | #define LUA_VUPVAL makevariant(LUA_TUPVAL, 0) |
574 | |
575 | |
576 | /* Variant tags for functions */ |
577 | #define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */ |
578 | #define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */ |
579 | #define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */ |
580 | |
581 | #define ttisfunction(o) checktype(o, LUA_TFUNCTION) |
582 | #define ttisLclosure(o) checktag((o), ctb(LUA_VLCL)) |
583 | #define ttislcf(o) checktag((o), LUA_VLCF) |
584 | #define ttisCclosure(o) checktag((o), ctb(LUA_VCCL)) |
585 | #define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o)) |
586 | |
587 | |
588 | #define isLfunction(o) ttisLclosure(o) |
589 | |
590 | #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) |
591 | #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) |
592 | #define fvalue(o) check_exp(ttislcf(o), val_(o).f) |
593 | #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) |
594 | |
595 | #define fvalueraw(v) ((v).f) |
596 | |
597 | #define setclLvalue(L,obj,x) \ |
598 | { TValue *io = (obj); LClosure *x_ = (x); \ |
599 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \ |
600 | checkliveness(L,io); } |
601 | |
602 | #define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl) |
603 | |
604 | #define setfvalue(obj,x) \ |
605 | { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); } |
606 | |
607 | #define setclCvalue(L,obj,x) \ |
608 | { TValue *io = (obj); CClosure *x_ = (x); \ |
609 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \ |
610 | checkliveness(L,io); } |
611 | |
612 | |
613 | /* |
614 | ** Upvalues for Lua closures |
615 | */ |
616 | typedef struct UpVal { |
617 | CommonHeader; |
618 | lu_byte tbc; /* true if it represents a to-be-closed variable */ |
619 | TValue *v; /* points to stack or to its own value */ |
620 | union { |
621 | struct { /* (when open) */ |
622 | struct UpVal *next; /* linked list */ |
623 | struct UpVal **previous; |
624 | } open; |
625 | TValue value; /* the value (when closed) */ |
626 | } u; |
627 | } UpVal; |
628 | |
629 | |
630 | |
631 | #define \ |
632 | CommonHeader; lu_byte nupvalues; GCObject *gclist |
633 | |
634 | typedef struct CClosure { |
635 | ClosureHeader; |
636 | lua_CFunction f; |
637 | TValue upvalue[1]; /* list of upvalues */ |
638 | } CClosure; |
639 | |
640 | |
641 | typedef struct LClosure { |
642 | ClosureHeader; |
643 | struct Proto *p; |
644 | UpVal *upvals[1]; /* list of upvalues */ |
645 | } LClosure; |
646 | |
647 | |
648 | typedef union Closure { |
649 | CClosure c; |
650 | LClosure l; |
651 | } Closure; |
652 | |
653 | |
654 | #define getproto(o) (clLvalue(o)->p) |
655 | |
656 | /* }================================================================== */ |
657 | |
658 | |
659 | /* |
660 | ** {================================================================== |
661 | ** Tables |
662 | ** =================================================================== |
663 | */ |
664 | |
665 | #define LUA_VTABLE makevariant(LUA_TTABLE, 0) |
666 | |
667 | #define ttistable(o) checktag((o), ctb(LUA_VTABLE)) |
668 | |
669 | #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) |
670 | |
671 | #define sethvalue(L,obj,x) \ |
672 | { TValue *io = (obj); Table *x_ = (x); \ |
673 | val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \ |
674 | checkliveness(L,io); } |
675 | |
676 | #define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h) |
677 | |
678 | |
679 | /* |
680 | ** Nodes for Hash tables: A pack of two TValue's (key-value pairs) |
681 | ** plus a 'next' field to link colliding entries. The distribution |
682 | ** of the key's fields ('key_tt' and 'key_val') not forming a proper |
683 | ** 'TValue' allows for a smaller size for 'Node' both in 4-byte |
684 | ** and 8-byte alignments. |
685 | */ |
686 | typedef union Node { |
687 | struct NodeKey { |
688 | TValuefields; /* fields for value */ |
689 | lu_byte key_tt; /* key type */ |
690 | int next; /* for chaining */ |
691 | Value key_val; /* key value */ |
692 | } u; |
693 | TValue i_val; /* direct access to node's value as a proper 'TValue' */ |
694 | } Node; |
695 | |
696 | |
697 | /* copy a value into a key */ |
698 | #define setnodekey(L,node,obj) \ |
699 | { Node *n_=(node); const TValue *io_=(obj); \ |
700 | n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \ |
701 | checkliveness(L,io_); } |
702 | |
703 | |
704 | /* copy a value from a key */ |
705 | #define getnodekey(L,obj,node) \ |
706 | { TValue *io_=(obj); const Node *n_=(node); \ |
707 | io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \ |
708 | checkliveness(L,io_); } |
709 | |
710 | |
711 | /* |
712 | ** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the |
713 | ** real size of 'array'. Otherwise, the real size of 'array' is the |
714 | ** smallest power of two not smaller than 'alimit' (or zero iff 'alimit' |
715 | ** is zero); 'alimit' is then used as a hint for #t. |
716 | */ |
717 | |
718 | #define BITRAS (1 << 7) |
719 | #define isrealasize(t) (!((t)->flags & BITRAS)) |
720 | #define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS)) |
721 | #define setnorealasize(t) ((t)->flags |= BITRAS) |
722 | |
723 | |
724 | typedef struct Table { |
725 | CommonHeader; |
726 | lu_byte flags; /* 1<<p means tagmethod(p) is not present */ |
727 | lu_byte lsizenode; /* log2 of size of 'node' array */ |
728 | unsigned int alimit; /* "limit" of 'array' array */ |
729 | TValue *array; /* array part */ |
730 | Node *node; |
731 | Node *lastfree; /* any free position is before this position */ |
732 | struct Table *metatable; |
733 | GCObject *gclist; |
734 | } Table; |
735 | |
736 | |
737 | /* |
738 | ** Macros to manipulate keys inserted in nodes |
739 | */ |
740 | #define keytt(node) ((node)->u.key_tt) |
741 | #define keyval(node) ((node)->u.key_val) |
742 | |
743 | #define keyisnil(node) (keytt(node) == LUA_TNIL) |
744 | #define keyisinteger(node) (keytt(node) == LUA_VNUMINT) |
745 | #define keyival(node) (keyval(node).i) |
746 | #define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR)) |
747 | #define keystrval(node) (gco2ts(keyval(node).gc)) |
748 | |
749 | #define setnilkey(node) (keytt(node) = LUA_TNIL) |
750 | |
751 | #define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE) |
752 | |
753 | #define gckey(n) (keyval(n).gc) |
754 | #define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL) |
755 | |
756 | |
757 | /* |
758 | ** Dead keys in tables have the tag DEADKEY but keep their original |
759 | ** gcvalue. This distinguishes them from regular keys but allows them to |
760 | ** be found when searched in a special way. ('next' needs that to find |
761 | ** keys removed from a table during a traversal.) |
762 | */ |
763 | #define setdeadkey(node) (keytt(node) = LUA_TDEADKEY) |
764 | #define keyisdead(node) (keytt(node) == LUA_TDEADKEY) |
765 | |
766 | /* }================================================================== */ |
767 | |
768 | |
769 | |
770 | /* |
771 | ** 'module' operation for hashing (size is always a power of 2) |
772 | */ |
773 | #define lmod(s,size) \ |
774 | (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1))))) |
775 | |
776 | |
777 | #define twoto(x) (1<<(x)) |
778 | #define sizenode(t) (twoto((t)->lsizenode)) |
779 | |
780 | |
781 | /* size of buffer for 'luaO_utf8esc' function */ |
782 | #define UTF8BUFFSZ 8 |
783 | |
784 | LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); |
785 | LUAI_FUNC int luaO_ceillog2 (unsigned int x); |
786 | LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, |
787 | const TValue *p2, TValue *res); |
788 | LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, |
789 | const TValue *p2, StkId res); |
790 | LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); |
791 | LUAI_FUNC int luaO_hexavalue (int c); |
792 | LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj); |
793 | LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, |
794 | va_list argp); |
795 | LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); |
796 | LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen); |
797 | |
798 | |
799 | #endif |
800 | |
801 | |