Scripting: Add flag for freeing the value buffer

This commit is contained in:
Vicki Pfau 2022-05-10 20:33:39 -07:00
parent 0c28e34a7e
commit c296ea79ff
2 changed files with 9 additions and 0 deletions

View File

@ -135,6 +135,7 @@ CXX_GUARD_START
struct mScriptValue* _val = mScriptListAppend(STACK); \
_val->type = mSCRIPT_TYPE_MS_ ## TYPE; \
_val->refs = mSCRIPT_VALUE_UNREF; \
_val->flags = 0; \
_val->value.mSCRIPT_TYPE_FIELD_ ## TYPE = NAME; \
} while (0)
@ -444,6 +445,10 @@ enum mScriptClassInitType {
mSCRIPT_CLASS_INIT_INHERIT,
};
enum {
mSCRIPT_VALUE_FLAG_FREE_BUFFER = 1
};
struct mScriptType;
extern const struct mScriptType mSTVoid;
extern const struct mScriptType mSTSInt8;
@ -522,6 +527,7 @@ struct mScriptType {
struct mScriptValue {
const struct mScriptType* type;
int refs;
uint32_t flags;
union {
int32_t s32;
uint32_t u32;

View File

@ -636,6 +636,7 @@ struct mScriptValue* mScriptValueAlloc(const struct mScriptType* type) {
struct mScriptValue* val = malloc(sizeof(*val));
val->refs = 1;
val->type = type;
val->flags = 0;
if (type->alloc) {
type->alloc(val);
} else {
@ -662,6 +663,8 @@ void mScriptValueDeref(struct mScriptValue* val) {
}
if (val->type->free) {
val->type->free(val);
} else if (val->flags & mSCRIPT_VALUE_FLAG_FREE_BUFFER) {
free(val->value.opaque);
}
free(val);
}