Scripting: Bring up lists

This commit is contained in:
Vicki Pfau 2022-05-11 22:13:18 -07:00
parent 5eb25876b4
commit e8e9a3e3c3
2 changed files with 32 additions and 1 deletions

View File

@ -31,6 +31,7 @@ CXX_GUARD_START
#define mSCRIPT_TYPE_C_CHARP const char*
#define mSCRIPT_TYPE_C_PTR void*
#define mSCRIPT_TYPE_C_CPTR const void*
#define mSCRIPT_TYPE_C_LIST struct mScriptList*
#define mSCRIPT_TYPE_C_TABLE Table*
#define mSCRIPT_TYPE_C_WRAPPER struct mScriptValue*
#define mSCRIPT_TYPE_C_WEAKREF uint32_t
@ -53,6 +54,7 @@ CXX_GUARD_START
#define mSCRIPT_TYPE_FIELD_STR opaque
#define mSCRIPT_TYPE_FIELD_CHARP copaque
#define mSCRIPT_TYPE_FIELD_PTR opaque
#define mSCRIPT_TYPE_FIELD_LIST opaque
#define mSCRIPT_TYPE_FIELD_TABLE opaque
#define mSCRIPT_TYPE_FIELD_WRAPPER opaque
#define mSCRIPT_TYPE_FIELD_WEAKREF u32
@ -74,6 +76,7 @@ CXX_GUARD_START
#define mSCRIPT_TYPE_MS_F64 (&mSTFloat64)
#define mSCRIPT_TYPE_MS_STR (&mSTString)
#define mSCRIPT_TYPE_MS_CHARP (&mSTCharPtr)
#define mSCRIPT_TYPE_MS_LIST (&mSTList)
#define mSCRIPT_TYPE_MS_TABLE (&mSTTable)
#define mSCRIPT_TYPE_MS_WRAPPER (&mSTWrapper)
#define mSCRIPT_TYPE_MS_WEAKREF (&mSTWeakref)
@ -524,6 +527,7 @@ extern const struct mScriptType mSTUInt64;
extern const struct mScriptType mSTFloat64;
extern const struct mScriptType mSTString;
extern const struct mScriptType mSTCharPtr;
extern const struct mScriptType mSTList;
extern const struct mScriptType mSTTable;
extern const struct mScriptType mSTWrapper;
extern const struct mScriptType mSTWeakref;

View File

@ -9,7 +9,8 @@
#include <mgba-util/hash.h>
#include <mgba-util/table.h>
#define MAX_ALIGNMENT 8
static void _allocList(struct mScriptValue*);
static void _freeList(struct mScriptValue*);
static void _allocTable(struct mScriptValue*);
static void _freeTable(struct mScriptValue*);
@ -178,6 +179,15 @@ const struct mScriptType mSTCharPtr = {
.equal = _charpEqual,
};
const struct mScriptType mSTList = {
.base = mSCRIPT_TYPE_LIST,
.size = sizeof(struct mScriptList),
.name = "list",
.alloc = _allocList,
.free = _freeList,
.hash = NULL,
};
const struct mScriptType mSTTable = {
.base = mSCRIPT_TYPE_TABLE,
.size = sizeof(struct Table),
@ -207,6 +217,23 @@ const struct mScriptType mSTWeakref = {
DEFINE_VECTOR(mScriptList, struct mScriptValue)
void _allocList(struct mScriptValue* val) {
val->value.opaque = malloc(sizeof(struct mScriptList));
mScriptListInit(val->value.opaque, 0);
}
void _freeList(struct mScriptValue* val) {
size_t i;
for (i = 0; i < mScriptListSize(val->value.opaque); ++i) {
struct mScriptValue* unwrapped = mScriptValueUnwrap(mScriptListGetPointer(val->value.opaque, i));
if (unwrapped) {
mScriptValueDeref(unwrapped);
}
}
mScriptListDeinit(val->value.opaque);
free(val->value.opaque);
}
void _allocTable(struct mScriptValue* val) {
val->value.opaque = malloc(sizeof(struct Table));
struct TableFunctions funcs = {