Add arrays to Lua (#222)

You can now have O(1) length and append.
This commit is contained in:
Paul Kulchenko 2021-08-06 04:55:01 -07:00 committed by GitHub
parent b142ea7176
commit 55a15c204e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 557 additions and 20 deletions

View file

@ -722,6 +722,8 @@ typedef struct Table {
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
lu_byte lsizenode; /* log2 of size of 'node' array */
unsigned int alimit; /* "limit" of 'array' array */
lu_byte truearray; /* 1 if the table is a true array (no hash part) */
unsigned int sizeused; /* size reported by objlen for true arrays (unused for regular tables) */
TValue *array; /* array part */
Node *node;
Node *lastfree; /* any free position is before this position */
@ -791,6 +793,31 @@ LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen);
/*
** {==================================================================
** Arrays
** ===================================================================
*/
#define ttisarray(o) checktag((o), ctb(LUA_TARRAY))
#define avalue(o) check_exp(ttisarray(o), gco2a(val_(o).gc))
#define setavalue(L,obj,x) \
{ TValue *io = (obj); Array *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TARRAY)); \
checkliveness(L,io); }
#define setavalue2s(L,o,h) setavalue(L,s2v(o),h)
typedef struct Array {
CommonHeader;
unsigned int alimit; /* size of 'array' array */
TValue *array; /* array part */
GCObject *gclist;
} Array;
/* }================================================================== */
#endif