blob: a9d45785ec7c33e95f9d0e2f21132bdcb2e1f01e [file] [log] [blame]
Lukas Zilka6cdd4002018-12-05 23:30:46 +01001/*
Elliott Hughes3d755922020-08-05 12:30:16 -07002** $Id: lobject.h $
Lukas Zilka6cdd4002018-12-05 23:30:46 +01003** 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/*
Elliott Hughes3d755922020-08-05 12:30:16 -070020** Extra types for collectable non-values
Lukas Zilka6cdd4002018-12-05 23:30:46 +010021*/
Elliott Hughes3d755922020-08-05 12:30:16 -070022#define LUA_TUPVAL LUA_NUMTYPES /* upvalues */
23#define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */
24
Lukas Zilka6cdd4002018-12-05 23:30:46 +010025
26/*
Elliott Hughes3d755922020-08-05 12:30:16 -070027** number of all possible types (including LUA_TNONE)
Lukas Zilka6cdd4002018-12-05 23:30:46 +010028*/
Elliott Hughes3d755922020-08-05 12:30:16 -070029#define LUA_TOTALTYPES (LUA_TPROTO + 2)
Lukas Zilka6cdd4002018-12-05 23:30:46 +010030
31
32/*
33** tags for Tagged Values have the following use of bits:
Elliott Hughes3d755922020-08-05 12:30:16 -070034** bits 0-3: actual tag (a LUA_T* constant)
Lukas Zilka6cdd4002018-12-05 23:30:46 +010035** bits 4-5: variant bits
36** bit 6: whether value is collectable
37*/
38
Elliott Hughes3d755922020-08-05 12:30:16 -070039/* add variant bits to a type */
40#define makevariant(t,v) ((t) | ((v) << 4))
Lukas Zilka6cdd4002018-12-05 23:30:46 +010041
42
Lukas Zilka6cdd4002018-12-05 23:30:46 +010043
44/*
45** Union of all Lua values
46*/
47typedef union Value {
Elliott Hughes3d755922020-08-05 12:30:16 -070048 struct GCObject *gc; /* collectable objects */
Lukas Zilka6cdd4002018-12-05 23:30:46 +010049 void *p; /* light userdata */
Lukas Zilka6cdd4002018-12-05 23:30:46 +010050 lua_CFunction f; /* light C functions */
51 lua_Integer i; /* integer numbers */
52 lua_Number n; /* float numbers */
53} Value;
54
55
Elliott Hughes3d755922020-08-05 12:30:16 -070056/*
57** Tagged Values. This is the basic representation of values in Lua:
58** an actual value plus a tag with its type.
59*/
Lukas Zilka6cdd4002018-12-05 23:30:46 +010060
Elliott Hughes3d755922020-08-05 12:30:16 -070061#define TValuefields Value value_; lu_byte tt_
Lukas Zilka6cdd4002018-12-05 23:30:46 +010062
Elliott Hughes3d755922020-08-05 12:30:16 -070063typedef struct TValue {
Lukas Zilka6cdd4002018-12-05 23:30:46 +010064 TValuefields;
65} TValue;
66
67
Lukas Zilka6cdd4002018-12-05 23:30:46 +010068#define val_(o) ((o)->value_)
Elliott Hughes3d755922020-08-05 12:30:16 -070069#define valraw(o) (&val_(o))
Lukas Zilka6cdd4002018-12-05 23:30:46 +010070
71
72/* raw type tag of a TValue */
Elliott Hughes3d755922020-08-05 12:30:16 -070073#define rawtt(o) ((o)->tt_)
Lukas Zilka6cdd4002018-12-05 23:30:46 +010074
75/* tag with no variants (bits 0-3) */
Elliott Hughes3d755922020-08-05 12:30:16 -070076#define novariant(t) ((t) & 0x0F)
Lukas Zilka6cdd4002018-12-05 23:30:46 +010077
78/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
Elliott Hughes3d755922020-08-05 12:30:16 -070079#define withvariant(t) ((t) & 0x3F)
80#define ttypetag(o) withvariant(rawtt(o))
Lukas Zilka6cdd4002018-12-05 23:30:46 +010081
Elliott Hughes3d755922020-08-05 12:30:16 -070082/* type of a TValue */
83#define ttype(o) (novariant(rawtt(o)))
Lukas Zilka6cdd4002018-12-05 23:30:46 +010084
85
86/* Macros to test type */
Elliott Hughes3d755922020-08-05 12:30:16 -070087#define checktag(o,t) (rawtt(o) == (t))
88#define checktype(o,t) (ttype(o) == (t))
Lukas Zilka6cdd4002018-12-05 23:30:46 +010089
90
91/* Macros for internal tests */
Lukas Zilka6cdd4002018-12-05 23:30:46 +010092
Elliott Hughes3d755922020-08-05 12:30:16 -070093/* 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
Tony Mak86713e62020-10-12 14:27:10 +010099** and it is not dead. The option 'L == NULL' allows other
100** macros using this one to be used where L is not available.
Elliott Hughes3d755922020-08-05 12:30:16 -0700101*/
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100102#define checkliveness(L,obj) \
Elliott Hughes3d755922020-08-05 12:30:16 -0700103 ((void)L, lua_longassert(!iscollectable(obj) || \
104 (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))))
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100105
106
107/* Macros to set values */
Elliott Hughes3d755922020-08-05 12:30:16 -0700108
109/* set a value's tag */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100110#define settt_(o,t) ((o)->tt_=(t))
111
Elliott Hughes3d755922020-08-05 12:30:16 -0700112
113/* main macro to copy values (from 'obj1' to 'obj2') */
114#define setobj(L,obj1,obj2) \
115 { TValue *io1=(obj1); const TValue *io2=(obj2); \
116 io1->value_ = io2->value_; settt_(io1, io2->tt_); \
117 checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); }
118
119/*
120** Different types of assignments, according to source and destination.
121** (They are mostly equal now, but may be different in the future.)
122*/
123
124/* from stack to stack */
125#define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2))
126/* to stack (not from same stack) */
127#define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2)
128/* from table to same table */
129#define setobjt2t setobj
130/* to new object */
131#define setobj2n setobj
132/* to table */
133#define setobj2t setobj
134
135
136/*
137** Entries in the Lua stack
138*/
139typedef union StackValue {
140 TValue val;
141} StackValue;
142
143
144/* index to stack elements */
145typedef StackValue *StkId;
146
147/* convert a 'StackValue' to a 'TValue' */
148#define s2v(o) (&(o)->val)
149
150
151
152/*
153** {==================================================================
154** Nil
155** ===================================================================
156*/
157
158/* Standard nil */
159#define LUA_VNIL makevariant(LUA_TNIL, 0)
160
161/* Empty slot (which might be different from a slot containing nil) */
162#define LUA_VEMPTY makevariant(LUA_TNIL, 1)
163
164/* Value returned for a key not found in a table (absent key) */
165#define LUA_VABSTKEY makevariant(LUA_TNIL, 2)
166
167
168/* macro to test for (any kind of) nil */
169#define ttisnil(v) checktype((v), LUA_TNIL)
170
171
172/* macro to test for a standard nil */
173#define ttisstrictnil(o) checktag((o), LUA_VNIL)
174
175
176#define setnilvalue(obj) settt_(obj, LUA_VNIL)
177
178
179#define isabstkey(v) checktag((v), LUA_VABSTKEY)
180
181
182/*
183** macro to detect non-standard nils (used only in assertions)
184*/
185#define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v))
186
187
188/*
189** By default, entries with any kind of nil are considered empty.
190** (In any definition, values associated with absent keys must also
191** be accepted as empty.)
192*/
193#define isempty(v) ttisnil(v)
194
195
196/* macro defining a value corresponding to an absent key */
197#define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY
198
199
200/* mark an entry as empty */
201#define setempty(v) settt_(v, LUA_VEMPTY)
202
203
204
205/* }================================================================== */
206
207
208/*
209** {==================================================================
210** Booleans
211** ===================================================================
212*/
213
214
215#define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0)
216#define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1)
217
218#define ttisboolean(o) checktype((o), LUA_TBOOLEAN)
219#define ttisfalse(o) checktag((o), LUA_VFALSE)
220#define ttistrue(o) checktag((o), LUA_VTRUE)
221
222
223#define l_isfalse(o) (ttisfalse(o) || ttisnil(o))
224
225
226#define setbfvalue(obj) settt_(obj, LUA_VFALSE)
227#define setbtvalue(obj) settt_(obj, LUA_VTRUE)
228
229/* }================================================================== */
230
231
232/*
233** {==================================================================
234** Threads
235** ===================================================================
236*/
237
238#define LUA_VTHREAD makevariant(LUA_TTHREAD, 0)
239
240#define ttisthread(o) checktag((o), ctb(LUA_VTHREAD))
241
242#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
243
244#define setthvalue(L,obj,x) \
245 { TValue *io = (obj); lua_State *x_ = (x); \
246 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \
247 checkliveness(L,io); }
248
249#define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t)
250
251/* }================================================================== */
252
253
254/*
255** {==================================================================
256** Collectable Objects
257** ===================================================================
258*/
259
260/*
261** Common Header for all collectable objects (in macro form, to be
262** included in other objects)
263*/
264#define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked
265
266
267/* Common type for all collectable objects */
268typedef struct GCObject {
269 CommonHeader;
270} GCObject;
271
272
273/* Bit mark for collectable types */
274#define BIT_ISCOLLECTABLE (1 << 6)
275
276#define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE)
277
278/* mark a tag as collectable */
279#define ctb(t) ((t) | BIT_ISCOLLECTABLE)
280
281#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
282
283#define gcvalueraw(v) ((v).gc)
284
285#define setgcovalue(L,obj,x) \
286 { TValue *io = (obj); GCObject *i_g=(x); \
287 val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
288
289/* }================================================================== */
290
291
292/*
293** {==================================================================
294** Numbers
295** ===================================================================
296*/
297
298/* Variant tags for numbers */
299#define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */
300#define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */
301
302#define ttisnumber(o) checktype((o), LUA_TNUMBER)
303#define ttisfloat(o) checktag((o), LUA_VNUMFLT)
304#define ttisinteger(o) checktag((o), LUA_VNUMINT)
305
306#define nvalue(o) check_exp(ttisnumber(o), \
307 (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
308#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
309#define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
310
311#define fltvalueraw(v) ((v).n)
312#define ivalueraw(v) ((v).i)
313
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100314#define setfltvalue(obj,x) \
Elliott Hughes3d755922020-08-05 12:30:16 -0700315 { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); }
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100316
317#define chgfltvalue(obj,x) \
318 { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
319
320#define setivalue(obj,x) \
Elliott Hughes3d755922020-08-05 12:30:16 -0700321 { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); }
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100322
323#define chgivalue(obj,x) \
324 { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
325
Elliott Hughes3d755922020-08-05 12:30:16 -0700326/* }================================================================== */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100327
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100328
Elliott Hughes3d755922020-08-05 12:30:16 -0700329/*
330** {==================================================================
331** Strings
332** ===================================================================
333*/
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100334
Elliott Hughes3d755922020-08-05 12:30:16 -0700335/* Variant tags for strings */
336#define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */
337#define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100338
Elliott Hughes3d755922020-08-05 12:30:16 -0700339#define ttisstring(o) checktype((o), LUA_TSTRING)
340#define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR))
341#define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR))
342
343#define tsvalueraw(v) (gco2ts((v).gc))
344
345#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100346
347#define setsvalue(L,obj,x) \
348 { TValue *io = (obj); TString *x_ = (x); \
349 val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
350 checkliveness(L,io); }
351
Elliott Hughes3d755922020-08-05 12:30:16 -0700352/* set a string to the stack */
353#define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s)
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100354
Elliott Hughes3d755922020-08-05 12:30:16 -0700355/* set a string to a new object */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100356#define setsvalue2n setsvalue
357
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100358
359/*
Elliott Hughes3d755922020-08-05 12:30:16 -0700360** Header for a string value.
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100361*/
362typedef struct TString {
363 CommonHeader;
364 lu_byte extra; /* reserved words for short strings; "has hash" for longs */
365 lu_byte shrlen; /* length for short strings */
366 unsigned int hash;
367 union {
368 size_t lnglen; /* length for long strings */
369 struct TString *hnext; /* linked list for hash table */
370 } u;
Elliott Hughes3d755922020-08-05 12:30:16 -0700371 char contents[1];
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100372} TString;
373
374
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100375
376/*
377** Get the actual string (array of bytes) from a 'TString'.
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100378*/
Elliott Hughes3d755922020-08-05 12:30:16 -0700379#define getstr(ts) ((ts)->contents)
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100380
381
382/* get the actual string (array of bytes) from a Lua value */
383#define svalue(o) getstr(tsvalue(o))
384
385/* get string length from 'TString *s' */
Elliott Hughes3d755922020-08-05 12:30:16 -0700386#define tsslen(s) ((s)->tt == LUA_VSHRSTR ? (s)->shrlen : (s)->u.lnglen)
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100387
388/* get string length from 'TValue *o' */
389#define vslen(o) tsslen(tsvalue(o))
390
Elliott Hughes3d755922020-08-05 12:30:16 -0700391/* }================================================================== */
392
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100393
394/*
Elliott Hughes3d755922020-08-05 12:30:16 -0700395** {==================================================================
396** Userdata
397** ===================================================================
398*/
399
400
401/*
402** Light userdata should be a variant of userdata, but for compatibility
403** reasons they are also different types.
404*/
405#define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0)
406
407#define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0)
408
409#define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA)
410#define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA))
411
412#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
413#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
414
415#define pvalueraw(v) ((v).p)
416
417#define setpvalue(obj,x) \
418 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); }
419
420#define setuvalue(L,obj,x) \
421 { TValue *io = (obj); Udata *x_ = (x); \
422 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \
423 checkliveness(L,io); }
424
425
426/* Ensures that addresses after this type are always fully aligned. */
427typedef union UValue {
428 TValue uv;
429 LUAI_MAXALIGN; /* ensures maximum alignment for udata bytes */
430} UValue;
431
432
433/*
434** Header for userdata with user values;
435** memory area follows the end of this structure.
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100436*/
437typedef struct Udata {
438 CommonHeader;
Elliott Hughes3d755922020-08-05 12:30:16 -0700439 unsigned short nuvalue; /* number of user values */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100440 size_t len; /* number of bytes */
Elliott Hughes3d755922020-08-05 12:30:16 -0700441 struct Table *metatable;
442 GCObject *gclist;
443 UValue uv[1]; /* user values */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100444} Udata;
445
446
447/*
Elliott Hughes3d755922020-08-05 12:30:16 -0700448** Header for userdata with no user values. These userdata do not need
449** to be gray during GC, and therefore do not need a 'gclist' field.
450** To simplify, the code always use 'Udata' for both kinds of userdata,
451** making sure it never accesses 'gclist' on userdata with no user values.
452** This structure here is used only to compute the correct size for
453** this representation. (The 'bindata' field in its end ensures correct
454** alignment for binary data following this header.)
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100455*/
Elliott Hughes3d755922020-08-05 12:30:16 -0700456typedef struct Udata0 {
457 CommonHeader;
458 unsigned short nuvalue; /* number of user values */
459 size_t len; /* number of bytes */
460 struct Table *metatable;
461 union {LUAI_MAXALIGN;} bindata;
462} Udata0;
463
464
465/* compute the offset of the memory area of a userdata */
466#define udatamemoffset(nuv) \
467 ((nuv) == 0 ? offsetof(Udata0, bindata) \
468 : offsetof(Udata, uv) + (sizeof(UValue) * (nuv)))
469
470/* get the address of the memory block inside 'Udata' */
471#define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue))
472
473/* compute the size of a userdata */
474#define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb))
475
476/* }================================================================== */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100477
478
479/*
Elliott Hughes3d755922020-08-05 12:30:16 -0700480** {==================================================================
481** Prototypes
482** ===================================================================
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100483*/
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100484
Elliott Hughes3d755922020-08-05 12:30:16 -0700485#define LUA_VPROTO makevariant(LUA_TPROTO, 0)
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100486
487
488/*
489** Description of an upvalue for function prototypes
490*/
491typedef struct Upvaldesc {
492 TString *name; /* upvalue name (for debug information) */
493 lu_byte instack; /* whether it is in stack (register) */
494 lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
Elliott Hughes3d755922020-08-05 12:30:16 -0700495 lu_byte kind; /* kind of corresponding variable */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100496} Upvaldesc;
497
498
499/*
500** Description of a local variable for function prototypes
501** (used for debug information)
502*/
503typedef struct LocVar {
504 TString *varname;
505 int startpc; /* first point where variable is active */
506 int endpc; /* first point where variable is dead */
507} LocVar;
508
509
510/*
Elliott Hughes3d755922020-08-05 12:30:16 -0700511** Associates the absolute line source for a given instruction ('pc').
512** The array 'lineinfo' gives, for each instruction, the difference in
513** lines from the previous instruction. When that difference does not
514** fit into a byte, Lua saves the absolute line for that instruction.
515** (Lua also saves the absolute line periodically, to speed up the
516** computation of a line number: we can use binary search in the
517** absolute-line array, but we must traverse the 'lineinfo' array
518** linearly to compute a line.)
519*/
520typedef struct AbsLineInfo {
521 int pc;
522 int line;
523} AbsLineInfo;
524
525/*
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100526** Function Prototypes
527*/
528typedef struct Proto {
529 CommonHeader;
Elliott Hughes3d755922020-08-05 12:30:16 -0700530 lu_byte numparams; /* number of fixed (named) parameters */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100531 lu_byte is_vararg;
532 lu_byte maxstacksize; /* number of registers needed by this function */
533 int sizeupvalues; /* size of 'upvalues' */
534 int sizek; /* size of 'k' */
535 int sizecode;
536 int sizelineinfo;
537 int sizep; /* size of 'p' */
538 int sizelocvars;
Elliott Hughes3d755922020-08-05 12:30:16 -0700539 int sizeabslineinfo; /* size of 'abslineinfo' */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100540 int linedefined; /* debug information */
541 int lastlinedefined; /* debug information */
542 TValue *k; /* constants used by the function */
543 Instruction *code; /* opcodes */
544 struct Proto **p; /* functions defined inside the function */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100545 Upvaldesc *upvalues; /* upvalue information */
Elliott Hughes3d755922020-08-05 12:30:16 -0700546 ls_byte *lineinfo; /* information about source lines (debug information) */
547 AbsLineInfo *abslineinfo; /* idem */
548 LocVar *locvars; /* information about local variables (debug information) */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100549 TString *source; /* used for debug information */
550 GCObject *gclist;
551} Proto;
552
Elliott Hughes3d755922020-08-05 12:30:16 -0700553/* }================================================================== */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100554
555
556/*
Elliott Hughes3d755922020-08-05 12:30:16 -0700557** {==================================================================
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100558** Closures
Elliott Hughes3d755922020-08-05 12:30:16 -0700559** ===================================================================
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100560*/
561
Elliott Hughes3d755922020-08-05 12:30:16 -0700562#define LUA_VUPVAL makevariant(LUA_TUPVAL, 0)
563
564
565/* Variant tags for functions */
566#define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */
567#define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */
568#define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */
569
570#define ttisfunction(o) checktype(o, LUA_TFUNCTION)
571#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_VLCL)
572#define ttisLclosure(o) checktag((o), ctb(LUA_VLCL))
573#define ttislcf(o) checktag((o), LUA_VLCF)
574#define ttisCclosure(o) checktag((o), ctb(LUA_VCCL))
575
576#define isLfunction(o) ttisLclosure(o)
577
578#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
579#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
580#define fvalue(o) check_exp(ttislcf(o), val_(o).f)
581#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
582
583#define fvalueraw(v) ((v).f)
584
585#define setclLvalue(L,obj,x) \
586 { TValue *io = (obj); LClosure *x_ = (x); \
587 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \
588 checkliveness(L,io); }
589
590#define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl)
591
592#define setfvalue(obj,x) \
593 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); }
594
595#define setclCvalue(L,obj,x) \
596 { TValue *io = (obj); CClosure *x_ = (x); \
597 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \
598 checkliveness(L,io); }
599
600
601/*
602** Upvalues for Lua closures
603*/
604typedef struct UpVal {
605 CommonHeader;
606 lu_byte tbc; /* true if it represents a to-be-closed variable */
607 TValue *v; /* points to stack or to its own value */
608 union {
609 struct { /* (when open) */
610 struct UpVal *next; /* linked list */
611 struct UpVal **previous;
612 } open;
613 TValue value; /* the value (when closed) */
614 } u;
615} UpVal;
616
617
618
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100619#define ClosureHeader \
620 CommonHeader; lu_byte nupvalues; GCObject *gclist
621
622typedef struct CClosure {
623 ClosureHeader;
624 lua_CFunction f;
625 TValue upvalue[1]; /* list of upvalues */
626} CClosure;
627
628
629typedef struct LClosure {
630 ClosureHeader;
631 struct Proto *p;
632 UpVal *upvals[1]; /* list of upvalues */
633} LClosure;
634
635
636typedef union Closure {
637 CClosure c;
638 LClosure l;
639} Closure;
640
641
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100642#define getproto(o) (clLvalue(o)->p)
643
Elliott Hughes3d755922020-08-05 12:30:16 -0700644/* }================================================================== */
645
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100646
647/*
Elliott Hughes3d755922020-08-05 12:30:16 -0700648** {==================================================================
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100649** Tables
Elliott Hughes3d755922020-08-05 12:30:16 -0700650** ===================================================================
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100651*/
652
Elliott Hughes3d755922020-08-05 12:30:16 -0700653#define LUA_VTABLE makevariant(LUA_TTABLE, 0)
654
655#define ttistable(o) checktag((o), ctb(LUA_VTABLE))
656
657#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
658
659#define sethvalue(L,obj,x) \
660 { TValue *io = (obj); Table *x_ = (x); \
661 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \
662 checkliveness(L,io); }
663
664#define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h)
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100665
666
Elliott Hughes3d755922020-08-05 12:30:16 -0700667/*
668** Nodes for Hash tables: A pack of two TValue's (key-value pairs)
669** plus a 'next' field to link colliding entries. The distribution
670** of the key's fields ('key_tt' and 'key_val') not forming a proper
671** 'TValue' allows for a smaller size for 'Node' both in 4-byte
672** and 8-byte alignments.
673*/
674typedef union Node {
675 struct NodeKey {
676 TValuefields; /* fields for value */
677 lu_byte key_tt; /* key type */
678 int next; /* for chaining */
679 Value key_val; /* key value */
680 } u;
681 TValue i_val; /* direct access to node's value as a proper 'TValue' */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100682} Node;
683
684
Elliott Hughes3d755922020-08-05 12:30:16 -0700685/* copy a value into a key */
686#define setnodekey(L,node,obj) \
687 { Node *n_=(node); const TValue *io_=(obj); \
688 n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \
689 checkliveness(L,io_); }
690
691
692/* copy a value from a key */
693#define getnodekey(L,obj,node) \
694 { TValue *io_=(obj); const Node *n_=(node); \
695 io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \
696 checkliveness(L,io_); }
697
698
699/*
700** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the
701** real size of 'array'. Otherwise, the real size of 'array' is the
702** smallest power of two not smaller than 'alimit' (or zero iff 'alimit'
703** is zero); 'alimit' is then used as a hint for #t.
704*/
705
706#define BITRAS (1 << 7)
Tony Mak86713e62020-10-12 14:27:10 +0100707#define isrealasize(t) (!((t)->flags & BITRAS))
708#define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS))
709#define setnorealasize(t) ((t)->flags |= BITRAS)
Elliott Hughes3d755922020-08-05 12:30:16 -0700710
711
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100712typedef struct Table {
713 CommonHeader;
714 lu_byte flags; /* 1<<p means tagmethod(p) is not present */
715 lu_byte lsizenode; /* log2 of size of 'node' array */
Elliott Hughes3d755922020-08-05 12:30:16 -0700716 unsigned int alimit; /* "limit" of 'array' array */
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100717 TValue *array; /* array part */
718 Node *node;
719 Node *lastfree; /* any free position is before this position */
720 struct Table *metatable;
721 GCObject *gclist;
722} Table;
723
724
Elliott Hughes3d755922020-08-05 12:30:16 -0700725/*
726** Macros to manipulate keys inserted in nodes
727*/
728#define keytt(node) ((node)->u.key_tt)
729#define keyval(node) ((node)->u.key_val)
730
731#define keyisnil(node) (keytt(node) == LUA_TNIL)
732#define keyisinteger(node) (keytt(node) == LUA_VNUMINT)
733#define keyival(node) (keyval(node).i)
734#define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR))
735#define keystrval(node) (gco2ts(keyval(node).gc))
736
737#define setnilkey(node) (keytt(node) = LUA_TNIL)
738
739#define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE)
740
741#define gckey(n) (keyval(n).gc)
742#define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL)
743
744
745/*
746** Use a "nil table" to mark dead keys in a table. Those keys serve
747** to keep space for removed entries, which may still be part of
748** chains. Note that the 'keytt' does not have the BIT_ISCOLLECTABLE
749** set, so these values are considered not collectable and are different
750** from any valid value.
751*/
752#define setdeadkey(n) (keytt(n) = LUA_TTABLE, gckey(n) = NULL)
753
754/* }================================================================== */
755
756
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100757
758/*
759** 'module' operation for hashing (size is always a power of 2)
760*/
761#define lmod(s,size) \
Elliott Hughes3d755922020-08-05 12:30:16 -0700762 (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1)))))
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100763
764
765#define twoto(x) (1<<(x))
766#define sizenode(t) (twoto((t)->lsizenode))
767
768
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100769/* size of buffer for 'luaO_utf8esc' function */
770#define UTF8BUFFSZ 8
771
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100772LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
773LUAI_FUNC int luaO_ceillog2 (unsigned int x);
Elliott Hughes3d755922020-08-05 12:30:16 -0700774LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
775 const TValue *p2, TValue *res);
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100776LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
Elliott Hughes3d755922020-08-05 12:30:16 -0700777 const TValue *p2, StkId res);
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100778LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
779LUAI_FUNC int luaO_hexavalue (int c);
Elliott Hughes3d755922020-08-05 12:30:16 -0700780LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj);
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100781LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
782 va_list argp);
783LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
Elliott Hughes3d755922020-08-05 12:30:16 -0700784LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen);
Lukas Zilka6cdd4002018-12-05 23:30:46 +0100785
786
787#endif
788