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