#pragma once
#include "lua.h"
#include "lcommon.h"
typedef union GCObject GCObject;
#define CommonHeader \
uint8_t tt; uint8_t marked; uint8_t memcat
typedef struct GCheader
{
CommonHeader;
} GCheader;
typedef union
{
GCObject* gc;
void* p;
double n;
int b;
int64_t l;
float v[2];
} Value;
typedef struct lua_TValue
{
Value value;
int extra[LUA_EXTRA_SIZE];
int tt;
} TValue;
#define ttisnil(o) (ttype(o) == LUA_TNIL)
#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
#define ttisinteger(o) (ttype(o) == LUA_TINTEGER)
#define ttisstring(o) (ttype(o) == LUA_TSTRING)
#define ttistable(o) (ttype(o) == LUA_TTABLE)
#define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
#define ttisthread(o) (ttype(o) == LUA_TTHREAD)
#define ttisbuffer(o) (ttype(o) == LUA_TBUFFER)
#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
#define ttisvector(o) (ttype(o) == LUA_TVECTOR)
#define ttisupval(o) (ttype(o) == LUA_TUPVAL)
#define ttype(o) ((o)->tt)
#define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
#define lvalue(o) check_exp(ttisinteger(o), (o)->value.l)
#define vvalue(o) check_exp(ttisvector(o), (o)->value.v)
#define tsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
#define uvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
#define bufvalue(o) check_exp(ttisbuffer(o), &(o)->value.gc->buf)
#define upvalue(o) check_exp(ttisupval(o), &(o)->value.gc->uv)
#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
#define lightuserdatatag(o) check_exp(ttislightuserdata(o), (o)->extra[0])
#define LU_TAG_ITERATOR LUA_UTAG_LIMIT
#define checkconsistency(obj) LUAU_ASSERT(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
#define checkliveness(g, obj) LUAU_ASSERT(!iscollectable(obj) || ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
#define setnilvalue(obj) ((obj)->tt = LUA_TNIL)
#define setnvalue(obj, x) \
{ \
TValue* i_o = (obj); \
i_o->value.n = (x); \
i_o->tt = LUA_TNUMBER; \
}
#define setlvalue(obj, x) \
{ \
TValue* i_o = (obj); \
i_o->value.l = (x); \
i_o->tt = LUA_TINTEGER; \
}
#if LUA_VECTOR_SIZE == 4
#define setvvalue(obj, x, y, z, w) \
{ \
TValue* i_o = (obj); \
float* i_v = i_o->value.v; \
i_v[0] = (x); \
i_v[1] = (y); \
i_v[2] = (z); \
i_v[3] = (w); \
i_o->tt = LUA_TVECTOR; \
}
#else
#define setvvalue(obj, x, y, z, w) \
{ \
TValue* i_o = (obj); \
float* i_v = i_o->value.v; \
i_v[0] = (x); \
i_v[1] = (y); \
i_v[2] = (z); \
i_o->tt = LUA_TVECTOR; \
}
#endif
#define setpvalue(obj, x, tag) \
{ \
TValue* i_o = (obj); \
i_o->value.p = (x); \
i_o->extra[0] = (tag); \
i_o->tt = LUA_TLIGHTUSERDATA; \
}
#define setbvalue(obj, x) \
{ \
TValue* i_o = (obj); \
i_o->value.b = (x); \
i_o->tt = LUA_TBOOLEAN; \
}
#define setsvalue(L, obj, x) \
{ \
TValue* i_o = (obj); \
i_o->value.gc = cast_to(GCObject*, (x)); \
i_o->tt = LUA_TSTRING; \
checkliveness(L->global, i_o); \
}
#define setuvalue(L, obj, x) \
{ \
TValue* i_o = (obj); \
i_o->value.gc = cast_to(GCObject*, (x)); \
i_o->tt = LUA_TUSERDATA; \
checkliveness(L->global, i_o); \
}
#define setthvalue(L, obj, x) \
{ \
TValue* i_o = (obj); \
i_o->value.gc = cast_to(GCObject*, (x)); \
i_o->tt = LUA_TTHREAD; \
checkliveness(L->global, i_o); \
}
#define setbufvalue(L, obj, x) \
{ \
TValue* i_o = (obj); \
i_o->value.gc = cast_to(GCObject*, (x)); \
i_o->tt = LUA_TBUFFER; \
checkliveness(L->global, i_o); \
}
#define setclvalue(L, obj, x) \
{ \
TValue* i_o = (obj); \
i_o->value.gc = cast_to(GCObject*, (x)); \
i_o->tt = LUA_TFUNCTION; \
checkliveness(L->global, i_o); \
}
#define sethvalue(L, obj, x) \
{ \
TValue* i_o = (obj); \
i_o->value.gc = cast_to(GCObject*, (x)); \
i_o->tt = LUA_TTABLE; \
checkliveness(L->global, i_o); \
}
#define setptvalue(L, obj, x) \
{ \
TValue* i_o = (obj); \
i_o->value.gc = cast_to(GCObject*, (x)); \
i_o->tt = LUA_TPROTO; \
checkliveness(L->global, i_o); \
}
#define setupvalue(L, obj, x) \
{ \
TValue* i_o = (obj); \
i_o->value.gc = cast_to(GCObject*, (x)); \
i_o->tt = LUA_TUPVAL; \
checkliveness(L->global, i_o); \
}
#define setobj(L, obj1, obj2) \
{ \
const TValue* o2 = (obj2); \
TValue* o1 = (obj1); \
*o1 = *o2; \
checkliveness(L->global, o1); \
}
#define setobj2s setobj
#define setobjt2t setobj
#define setobj2t setobj
#define setobj2n setobj
#define setttype(obj, tt) (ttype(obj) = (tt))
#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
typedef TValue* StkId;
typedef struct TString
{
CommonHeader;
int16_t atom;
TString* next;
unsigned int hash;
unsigned int len;
char data[1];
} TString;
#define getstr(ts) (ts)->data
#define svalue(o) getstr(tsvalue(o))
typedef struct Udata
{
CommonHeader;
uint8_t tag;
int len;
struct LuaTable* metatable;
alignas(8) char data[1];
} Udata;
typedef struct LuauBuffer
{
CommonHeader;
unsigned int len;
alignas(8) char data[1];
} Buffer;
typedef struct Proto
{
CommonHeader;
uint8_t nups;
uint8_t numparams;
uint8_t is_vararg;
uint8_t maxstacksize;
uint8_t flags;
TValue* k;
Instruction* code;
struct Proto** p;
const Instruction* codeentry;
void* execdata;
uintptr_t exectarget;
uint8_t* lineinfo;
int* abslineinfo;
struct LocVar* locvars;
TString** upvalues;
TString* source;
TString* debugname;
uint8_t* debuginsn;
uint8_t* typeinfo;
void* userdata;
GCObject* gclist;
int sizecode;
int sizep;
int sizelocvars;
int sizeupvalues;
int sizek;
int sizelineinfo;
int linegaplog2;
int linedefined;
int bytecodeid;
int sizetypeinfo;
} Proto;
typedef struct LocVar
{
TString* varname;
int startpc;
int endpc;
uint8_t reg;
} LocVar;
typedef struct UpVal
{
CommonHeader;
uint8_t markedopen;
TValue* v;
union
{
TValue value;
struct
{
struct UpVal* prev;
struct UpVal* next;
struct UpVal* threadnext;
} open;
} u;
} UpVal;
#define upisopen(up) ((up)->v != &(up)->u.value)
typedef struct Closure
{
CommonHeader;
uint8_t isC;
uint8_t nupvalues;
uint8_t stacksize;
uint8_t preload;
GCObject* gclist;
struct LuaTable* env;
union
{
struct
{
lua_CFunction f;
lua_Continuation cont;
const char* debugname;
TValue upvals[1];
} c;
struct
{
struct Proto* p;
TValue uprefs[1];
} l;
};
} Closure;
#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->isC)
typedef struct TKey
{
::Value value;
int extra[LUA_EXTRA_SIZE];
unsigned tt : 4;
int next : 28;
} TKey;
typedef struct LuaNode
{
TValue val;
TKey key;
} LuaNode;
#define setnodekey(L, node, obj) \
{ \
LuaNode* n_ = (node); \
const TValue* i_o = (obj); \
n_->key.value = i_o->value; \
memcpy(n_->key.extra, i_o->extra, sizeof(n_->key.extra)); \
n_->key.tt = i_o->tt; \
checkliveness(L->global, i_o); \
}
#define getnodekey(L, obj, node) \
{ \
TValue* i_o = (obj); \
const LuaNode* n_ = (node); \
i_o->value = n_->key.value; \
memcpy(i_o->extra, n_->key.extra, sizeof(i_o->extra)); \
i_o->tt = n_->key.tt; \
checkliveness(L->global, i_o); \
}
typedef struct LuaTable
{
CommonHeader;
uint8_t tmcache;
uint8_t readonly;
uint8_t safeenv;
uint8_t lsizenode;
uint8_t nodemask8;
int sizearray;
union
{
int lastfree;
int aboundary;
};
struct LuaTable* metatable;
TValue* array;
LuaNode* node;
GCObject* gclist;
} LuaTable;
#define lmod(s, size) (check_exp((size & (size - 1)) == 0, (cast_to(int, (s) & ((size)-1)))))
#define twoto(x) ((int)(1 << (x)))
#define sizenode(t) (twoto((t)->lsizenode))
#define luaO_nilobject (&luaO_nilobject_)
LUAI_DATA const TValue luaO_nilobject_;
#define ceillog2(x) (luaO_log2((x)-1) + 1)
LUAI_FUNC int luaO_log2(unsigned int x);
LUAI_FUNC int luaO_rawequalObj(const TValue* t1, const TValue* t2);
LUAI_FUNC int luaO_rawequalKey(const TKey* t1, const TValue* t2);
LUAI_FUNC int luaO_str2d(const char* s, double* result);
LUAI_FUNC int luaO_str2l(const char* s, int64_t* result, int base = 10);
LUAI_FUNC const char* luaO_pushvfstring(lua_State* L, const char* fmt, va_list argp);
LUAI_FUNC const char* luaO_pushfstring(lua_State* L, const char* fmt, ...);
LUAI_FUNC const char* luaO_chunkid(char* buf, size_t buflen, const char* source, size_t srclen);