Path: blob/main/sys/contrib/openzfs/module/lua/lobject.h
48383 views
// SPDX-License-Identifier: MIT1/*2** $Id: lobject.h,v 2.71.1.2 2014/05/07 14:14:58 roberto Exp $3** Type definitions for Lua objects4** See Copyright Notice in lua.h5*/678#ifndef lobject_h9#define lobject_h101112#include "llimits.h"13#include <sys/lua/lua.h>141516/*17** Extra tags for non-values18*/19#define LUA_TPROTO LUA_NUMTAGS20#define LUA_TUPVAL (LUA_NUMTAGS+1)21#define LUA_TDEADKEY (LUA_NUMTAGS+2)2223/*24** number of all possible tags (including LUA_TNONE but excluding DEADKEY)25*/26#define LUA_TOTALTAGS (LUA_TUPVAL+2)272829/*30** tags for Tagged Values have the following use of bits:31** bits 0-3: actual tag (a LUA_T* value)32** bits 4-5: variant bits33** bit 6: whether value is collectable34*/3536#define VARBITS (3 << 4)373839/*40** LUA_TFUNCTION variants:41** 0 - Lua function42** 1 - light C function43** 2 - regular C function (closure)44*/4546/* Variant tags for functions */47#define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */48#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */49#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */505152/* Variant tags for strings */53#define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */54#define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */555657/* Bit mark for collectable types */58#define BIT_ISCOLLECTABLE (1 << 6)5960/* mark a tag as collectable */61#define ctb(t) ((t) | BIT_ISCOLLECTABLE)626364/*65** Union of all collectable objects66*/67typedef union GCObject GCObject;686970/*71** Common Header for all collectable objects (in macro form, to be72** included in other objects)73*/74#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked757677/*78** Common header in struct form79*/80typedef struct GCheader {81CommonHeader;82} GCheader;83848586/*87** Union of all Lua values88*/89typedef union Value Value;909192#define numfield lua_Number n; /* numbers */93949596/*97** Tagged Values. This is the basic representation of values in Lua,98** an actual value plus a tag with its type.99*/100101#define TValuefields Value value_; int tt_102103typedef struct lua_TValue TValue;104105106/* macro defining a nil value */107#define NILCONSTANT {NULL}, LUA_TNIL108109110#define val_(o) ((o)->value_)111#define num_(o) (val_(o).n)112113114/* raw type tag of a TValue */115#define rttype(o) ((o)->tt_)116117/* tag with no variants (bits 0-3) */118#define novariant(x) ((x) & 0x0F)119120/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */121#define ttype(o) (rttype(o) & 0x3F)122123/* type tag of a TValue with no variants (bits 0-3) */124#define ttypenv(o) (novariant(rttype(o)))125126127/* Macros to test type */128#define checktag(o,t) (rttype(o) == (t))129#define checktype(o,t) (ttypenv(o) == (t))130#define ttisnumber(o) checktag((o), LUA_TNUMBER)131#define ttisnil(o) checktag((o), LUA_TNIL)132#define ttisboolean(o) checktag((o), LUA_TBOOLEAN)133#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)134#define ttisstring(o) checktype((o), LUA_TSTRING)135#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))136#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))137#define ttistable(o) checktag((o), ctb(LUA_TTABLE))138#define ttisfunction(o) checktype(o, LUA_TFUNCTION)139#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)140#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))141#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))142#define ttislcf(o) checktag((o), LUA_TLCF)143#define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA))144#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))145#define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)146147#define ttisequal(o1,o2) (rttype(o1) == rttype(o2))148149/* Macros to access values */150#define nvalue(o) check_exp(ttisnumber(o), num_(o))151#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)152#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)153#define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts)154#define tsvalue(o) (&rawtsvalue(o)->tsv)155#define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u)156#define uvalue(o) (&rawuvalue(o)->uv)157#define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl)158#define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)159#define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)160#define fvalue(o) check_exp(ttislcf(o), val_(o).f)161#define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h)162#define bvalue(o) check_exp(ttisboolean(o), val_(o).b)163#define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th)164/* a dead value may get the 'gc' field, but cannot access its contents */165#define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))166167#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))168169170#define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)171172173/* Macros for internal tests */174#define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt)175176#define checkliveness(g,obj) \177lua_longassert(!iscollectable(obj) || \178(righttt(obj) && !isdead(g,gcvalue(obj))))179180181/* Macros to set values */182#define settt_(o,t) ((o)->tt_=(t))183184#define setnvalue(obj,x) \185{ TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }186187#define setnilvalue(obj) settt_(obj, LUA_TNIL)188189#define setfvalue(obj,x) \190{ TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }191192#define setpvalue(obj,x) \193{ TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }194195#define setbvalue(obj,x) \196{ TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }197198#define setgcovalue(L,obj,x) \199{ TValue *io=(obj); GCObject *i_g=(x); \200val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }201202#define setsvalue(L,obj,x) \203{ TValue *io=(obj); \204TString *x_ = (x); \205val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \206checkliveness(G(L),io); }207208#define setuvalue(L,obj,x) \209{ TValue *io=(obj); \210val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \211checkliveness(G(L),io); }212213#define setthvalue(L,obj,x) \214{ TValue *io=(obj); \215val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \216checkliveness(G(L),io); }217218#define setclLvalue(L,obj,x) \219{ TValue *io=(obj); \220val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \221checkliveness(G(L),io); }222223#define setclCvalue(L,obj,x) \224{ TValue *io=(obj); \225val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \226checkliveness(G(L),io); }227228#define sethvalue(L,obj,x) \229{ TValue *io=(obj); \230val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \231checkliveness(G(L),io); }232233#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)234235236237#define setobj(L,obj1,obj2) \238{ const TValue *io2=(obj2); TValue *io1=(obj1); \239io1->value_ = io2->value_; io1->tt_ = io2->tt_; \240checkliveness(G(L),io1); }241242243/*244** different types of assignments, according to destination245*/246247/* from stack to (same) stack */248#define setobjs2s setobj249/* to stack (not from same stack) */250#define setobj2s setobj251#define setsvalue2s setsvalue252#define sethvalue2s sethvalue253#define setptvalue2s setptvalue254/* from table to same table */255#define setobjt2t setobj256/* to table */257#define setobj2t setobj258/* to new object */259#define setobj2n setobj260#define setsvalue2n setsvalue261262263/* check whether a number is valid (useful only for NaN trick) */264#define luai_checknum(L,o,c) { /* empty */ }265266267/*268** {======================================================269** NaN Trick270** =======================================================271*/272#if defined(LUA_NANTRICK)273274/*275** numbers are represented in the 'd_' field. All other values have the276** value (NNMARK | tag) in 'tt__'. A number with such pattern would be277** a "signaled NaN", which is never generated by regular operations by278** the CPU (nor by 'strtod')279*/280281/* allows for external implementation for part of the trick */282#if !defined(NNMARK) /* { */283284285#if !defined(LUA_IEEEENDIAN)286#error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN'287#endif288289290#define NNMARK 0x7FF7A500291#define NNMASK 0x7FFFFF00292293#undef TValuefields294#undef NILCONSTANT295296#if (LUA_IEEEENDIAN == 0) /* { */297298/* little endian */299#define TValuefields \300union { struct { Value v__; int tt__; } i; double d__; } u301#define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}}302/* field-access macros */303#define v_(o) ((o)->u.i.v__)304#define d_(o) ((o)->u.d__)305#define tt_(o) ((o)->u.i.tt__)306307#else /* }{ */308309/* big endian */310#define TValuefields \311union { struct { int tt__; Value v__; } i; double d__; } u312#define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}}313/* field-access macros */314#define v_(o) ((o)->u.i.v__)315#define d_(o) ((o)->u.d__)316#define tt_(o) ((o)->u.i.tt__)317318#endif /* } */319320#endif /* } */321322323/* correspondence with standard representation */324#undef val_325#define val_(o) v_(o)326#undef num_327#define num_(o) d_(o)328329330#undef numfield331#define numfield /* no such field; numbers are the entire struct */332333/* basic check to distinguish numbers from non-numbers */334#undef ttisnumber335#define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK)336337#define tag2tt(t) (NNMARK | (t))338339#undef rttype340#define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff)341342#undef settt_343#define settt_(o,t) (tt_(o) = tag2tt(t))344345#undef setnvalue346#define setnvalue(obj,x) \347{ TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }348349#undef setobj350#define setobj(L,obj1,obj2) \351{ const TValue *o2_=(obj2); TValue *o1_=(obj1); \352o1_->u = o2_->u; \353checkliveness(G(L),o1_); }354355356/*357** these redefinitions are not mandatory, but these forms are more efficient358*/359360#undef checktag361#undef checktype362#define checktag(o,t) (tt_(o) == tag2tt(t))363#define checktype(o,t) (ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS))364365#undef ttisequal366#define ttisequal(o1,o2) \367(ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))368369370#undef luai_checknum371#define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; }372373#endif374/* }====================================================== */375376377378/*379** {======================================================380** types and prototypes381** =======================================================382*/383384385union Value {386GCObject *gc; /* collectable objects */387void *p; /* light userdata */388int b; /* booleans */389lua_CFunction f; /* light C functions */390numfield /* numbers */391};392393394struct lua_TValue {395TValuefields;396};397398399typedef TValue *StkId; /* index to stack elements */400401402403404/*405** Header for string value; string bytes follow the end of this structure406*/407typedef struct TString {408union {409L_Umaxalign dummy; /* ensures maximum alignment for strings */410struct {411CommonHeader;412lu_byte extra; /* reserved words for short strings; "has hash" for longs */413unsigned int hash;414size_t len; /* number of characters in string */415} tsv;416};417char contents[];418} TString;419420421/* get the actual string (array of bytes) from a TString */422#define getstr(ts) ((ts)->contents)423424/* get the actual string (array of bytes) from a Lua value */425#define svalue(o) getstr(rawtsvalue(o))426427428/*429** Header for userdata; memory area follows the end of this structure430*/431typedef union Udata {432L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */433struct {434CommonHeader;435struct Table *metatable;436struct Table *env;437size_t len; /* number of bytes */438} uv;439} Udata;440441442443/*444** Description of an upvalue for function prototypes445*/446typedef struct Upvaldesc {447TString *name; /* upvalue name (for debug information) */448lu_byte instack; /* whether it is in stack */449lu_byte idx; /* index of upvalue (in stack or in outer function's list) */450} Upvaldesc;451452453/*454** Description of a local variable for function prototypes455** (used for debug information)456*/457typedef struct LocVar {458TString *varname;459int startpc; /* first point where variable is active */460int endpc; /* first point where variable is dead */461} LocVar;462463464/*465** Function Prototypes466*/467typedef struct Proto {468CommonHeader;469TValue *k; /* constants used by the function */470Instruction *code;471struct Proto **p; /* functions defined inside the function */472int *lineinfo; /* map from opcodes to source lines (debug information) */473LocVar *locvars; /* information about local variables (debug information) */474Upvaldesc *upvalues; /* upvalue information */475union Closure *cache; /* last created closure with this prototype */476TString *source; /* used for debug information */477int sizeupvalues; /* size of 'upvalues' */478int sizek; /* size of `k' */479int sizecode;480int sizelineinfo;481int sizep; /* size of `p' */482int sizelocvars;483int linedefined;484int lastlinedefined;485GCObject *gclist;486lu_byte numparams; /* number of fixed parameters */487lu_byte is_vararg;488lu_byte maxstacksize; /* maximum stack used by this function */489} Proto;490491492493/*494** Lua Upvalues495*/496typedef struct UpVal {497CommonHeader;498TValue *v; /* points to stack or to its own value */499union {500TValue value; /* the value (when closed) */501struct { /* double linked list (when open) */502struct UpVal *prev;503struct UpVal *next;504} l;505} u;506} UpVal;507508509/*510** Closures511*/512513#define ClosureHeader \514CommonHeader; lu_byte nupvalues; GCObject *gclist515516typedef struct CClosure {517ClosureHeader;518lua_CFunction f;519TValue upvalue[]; /* list of upvalues */520} CClosure;521522523typedef struct LClosure {524ClosureHeader;525struct Proto *p;526UpVal *upvals[]; /* list of upvalues */527} LClosure;528529530typedef union Closure {531CClosure c;532LClosure l;533} Closure;534535536#define isLfunction(o) ttisLclosure(o)537538#define getproto(o) (clLvalue(o)->p)539540541/*542** Tables543*/544545typedef union TKey {546struct {547TValuefields;548struct Node *next; /* for chaining */549} nk;550TValue tvk;551} TKey;552553554typedef struct Node {555TValue i_val;556TKey i_key;557} Node;558559560typedef struct Table {561CommonHeader;562lu_byte flags; /* 1<<p means tagmethod(p) is not present */563lu_byte lsizenode; /* log2 of size of `node' array */564int sizearray; /* size of `array' array */565TValue *array; /* array part */566Node *node;567Node *lastfree; /* any free position is before this position */568struct Table *metatable;569GCObject *gclist;570} Table;571572573574/*575** `module' operation for hashing (size is always a power of 2)576*/577#define lmod(s,size) \578(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))579580581#define twoto(x) (1<<(x))582#define sizenode(t) (twoto((t)->lsizenode))583584585/*586** (address of) a fixed nil value587*/588#define luaO_nilobject (&luaO_nilobject_)589590591LUAI_DDEC const TValue luaO_nilobject_;592593594LUAI_FUNC int luaO_int2fb (unsigned int x);595LUAI_FUNC int luaO_fb2int (int x);596LUAI_FUNC int luaO_ceillog2 (unsigned int x);597LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);598LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);599LUAI_FUNC int luaO_hexavalue (int c);600LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,601va_list argp);602LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);603LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);604605606#endif607608609