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