From e8e9a3e3c3ada0bdda4f2eb99797d3ad9f862b19 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Wed, 11 May 2022 22:13:18 -0700 Subject: [PATCH] Scripting: Bring up lists --- include/mgba/script/types.h | 4 ++++ src/script/types.c | 29 ++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/include/mgba/script/types.h b/include/mgba/script/types.h index 08925f514..bc81d3105 100644 --- a/include/mgba/script/types.h +++ b/include/mgba/script/types.h @@ -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; diff --git a/src/script/types.c b/src/script/types.c index f2c5efc60..83e7fbf29 100644 --- a/src/script/types.c +++ b/src/script/types.c @@ -9,7 +9,8 @@ #include #include -#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 = {