diff --git a/include/mgba/script/macros.h b/include/mgba/script/macros.h index 346dbf64f..b911e7251 100644 --- a/include/mgba/script/macros.h +++ b/include/mgba/script/macros.h @@ -16,7 +16,7 @@ CXX_GUARD_START struct mScriptValue* _val = mScriptListGetPointer(STACK, mScriptListSize(STACK) - 1); \ bool deref = true; \ if (!(mSCRIPT_TYPE_CMP(TYPE, _val->type))) { \ - if (_val->type == mSCRIPT_TYPE_MS_WRAPPER) { \ + if (_val->type->base == mSCRIPT_TYPE_WRAPPER) { \ _val = mScriptValueUnwrap(_val); \ deref = false; \ if (!(mSCRIPT_TYPE_CMP(TYPE, _val->type))) { \ @@ -73,6 +73,8 @@ CXX_GUARD_START #define mSCRIPT_PREFIX_8(PREFIX, T0, T1, T2, T3, T4, T5, T6, T7) PREFIX ## T0, PREFIX ## T1, PREFIX ## T2, PREFIX ## T3, PREFIX ## T4, PREFIX ## T5, PREFIX ## T6, PREFIX ## T7 #define mSCRIPT_PREFIX_N(N) mSCRIPT_PREFIX_ ## N +#define _mSCRIPT_FIELD_NAME(V) (V)->name + #define _mSCRIPT_CALL_VOID(FUNCTION, NPARAMS) FUNCTION(_mCAT(mSCRIPT_ARG_NAMES_, NPARAMS)) #define _mSCRIPT_CALL(RETURN, FUNCTION, NPARAMS) \ mSCRIPT_TYPE_C_ ## RETURN out = FUNCTION(_mCAT(mSCRIPT_ARG_NAMES_, NPARAMS)); \ @@ -154,6 +156,26 @@ CXX_GUARD_START .free = NULL, \ .cast = _mSTStructPtrCast_ ## STRUCT, \ }; \ + const struct mScriptType mSTWrapper_ ## STRUCT = { \ + .base = mSCRIPT_TYPE_WRAPPER, \ + .details = { \ + .type = &mSTStruct_ ## STRUCT \ + }, \ + .size = sizeof(struct mScriptValue), \ + .name = "wrapper struct::" #STRUCT, \ + .alloc = NULL, \ + .free = NULL, \ + }; \ + const struct mScriptType mSTWrapperConst_ ## STRUCT = { \ + .base = mSCRIPT_TYPE_WRAPPER, \ + .details = { \ + .type = &mSTStructConst_ ## STRUCT \ + }, \ + .size = sizeof(struct mScriptValue), \ + .name = "wrapper const struct::" #STRUCT, \ + .alloc = NULL, \ + .free = NULL, \ + }; \ static struct mScriptTypeClass _mSTStructDetails_ ## STRUCT = { \ .init = false, \ .details = (const struct mScriptClassInitDetails[]) { diff --git a/include/mgba/script/types.h b/include/mgba/script/types.h index 1914bd1fe..fd7dbe5d8 100644 --- a/include/mgba/script/types.h +++ b/include/mgba/script/types.h @@ -40,6 +40,9 @@ CXX_GUARD_START #define mSCRIPT_TYPE_C_S_METHOD(STRUCT, NAME) _mSTStructFunctionType_ ## STRUCT ## _ ## NAME #define mSCRIPT_TYPE_C_PS(X) void #define mSCRIPT_TYPE_C_PCS(X) void +#define mSCRIPT_TYPE_C_WSTR struct mScriptValue* +#define mSCRIPT_TYPE_C_W(X) struct mScriptValue* +#define mSCRIPT_TYPE_C_CW(X) const struct mScriptValue* #define mSCRIPT_TYPE_FIELD_S8 s32 #define mSCRIPT_TYPE_FIELD_U8 s32 @@ -63,6 +66,9 @@ CXX_GUARD_START #define mSCRIPT_TYPE_FIELD_S_METHOD(STRUCT, NAME) copaque #define mSCRIPT_TYPE_FIELD_PS(STRUCT) opaque #define mSCRIPT_TYPE_FIELD_PCS(STRUCT) copaque +#define mSCRIPT_TYPE_FIELD_WSTR opaque +#define mSCRIPT_TYPE_FIELD_W(TYPE) opaque +#define mSCRIPT_TYPE_FIELD_CW(TYPE) opaque #define mSCRIPT_TYPE_MS_S8 (&mSTSInt8) #define mSCRIPT_TYPE_MS_U8 (&mSTUInt8) @@ -85,8 +91,9 @@ CXX_GUARD_START #define mSCRIPT_TYPE_MS_S_METHOD(STRUCT, NAME) (&_mSTStructBindingType_ ## STRUCT ## _ ## NAME) #define mSCRIPT_TYPE_MS_PS(STRUCT) (&mSTStructPtr_ ## STRUCT) #define mSCRIPT_TYPE_MS_PCS(STRUCT) (&mSTStructConstPtr_ ## STRUCT) - -#define _mSCRIPT_FIELD_NAME(V) (V)->name +#define mSCRIPT_TYPE_MS_WSTR (&mSTStringWrapper) +#define mSCRIPT_TYPE_MS_W(TYPE) (&mSTWrapper_ ## TYPE) +#define mSCRIPT_TYPE_MS_CW(TYPE) (&mSTWrapperConst_ ## TYPE) #define mSCRIPT_TYPE_CMP_GENERIC(TYPE0, TYPE1) (TYPE0 == TYPE1) #define mSCRIPT_TYPE_CMP_U8(TYPE) mSCRIPT_TYPE_CMP_GENERIC(mSCRIPT_TYPE_MS_U8, TYPE) @@ -107,6 +114,7 @@ CXX_GUARD_START #define mSCRIPT_TYPE_CMP_CS(STRUCT) mSCRIPT_TYPE_MS_CS(STRUCT)->name == _mSCRIPT_FIELD_NAME #define mSCRIPT_TYPE_CMP_S_METHOD(STRUCT, NAME) mSCRIPT_TYPE_MS_S_METHOD(STRUCT, NAME)->name == _mSCRIPT_FIELD_NAME #define mSCRIPT_TYPE_CMP(TYPE0, TYPE1) mSCRIPT_TYPE_CMP_ ## TYPE0(TYPE1) +#define mSCRIPT_TYPE_CMP_WSTR(TYPE) (mSCRIPT_TYPE_CMP_GENERIC(mSCRIPT_TYPE_MS_STR, TYPE) || mSCRIPT_TYPE_CMP_GENERIC(mSCRIPT_TYPE_MS_CHARP, TYPE)) enum mScriptTypeBase { mSCRIPT_TYPE_VOID = 0, @@ -158,6 +166,7 @@ extern const struct mScriptType mSTList; extern const struct mScriptType mSTTable; extern const struct mScriptType mSTWrapper; extern const struct mScriptType mSTWeakref; +extern const struct mScriptType mSTStringWrapper; struct mScriptType; struct mScriptValue { diff --git a/src/core/scripting.c b/src/core/scripting.c index a74beaf08..98a5f6662 100644 --- a/src/core/scripting.c +++ b/src/core/scripting.c @@ -250,7 +250,7 @@ mSCRIPT_DECLARE_STRUCT(mScriptMemoryDomain); mSCRIPT_DECLARE_STRUCT_METHOD(mScriptMemoryDomain, U32, read8, mScriptMemoryDomainRead8, 1, U32, address); mSCRIPT_DECLARE_STRUCT_METHOD(mScriptMemoryDomain, U32, read16, mScriptMemoryDomainRead16, 1, U32, address); mSCRIPT_DECLARE_STRUCT_METHOD(mScriptMemoryDomain, U32, read32, mScriptMemoryDomainRead32, 1, U32, address); -mSCRIPT_DECLARE_STRUCT_METHOD(mScriptMemoryDomain, WRAPPER, readRange, mScriptMemoryDomainReadRange, 2, U32, address, U32, length); +mSCRIPT_DECLARE_STRUCT_METHOD(mScriptMemoryDomain, WSTR, readRange, mScriptMemoryDomainReadRange, 2, U32, address, U32, length); mSCRIPT_DECLARE_STRUCT_VOID_METHOD(mScriptMemoryDomain, write8, mScriptMemoryDomainWrite8, 2, U32, address, U8, value); mSCRIPT_DECLARE_STRUCT_VOID_METHOD(mScriptMemoryDomain, write16, mScriptMemoryDomainWrite16, 2, U32, address, U16, value); mSCRIPT_DECLARE_STRUCT_VOID_METHOD(mScriptMemoryDomain, write32, mScriptMemoryDomainWrite32, 2, U32, address, U32, value); @@ -258,7 +258,7 @@ mSCRIPT_DECLARE_STRUCT_VOID_METHOD(mScriptMemoryDomain, write32, mScriptMemoryDo mSCRIPT_DECLARE_STRUCT_METHOD(mScriptMemoryDomain, U32, base, mScriptMemoryDomainBase, 0); mSCRIPT_DECLARE_STRUCT_METHOD(mScriptMemoryDomain, U32, bound, mScriptMemoryDomainEnd, 0); mSCRIPT_DECLARE_STRUCT_METHOD(mScriptMemoryDomain, U32, size, mScriptMemoryDomainSize, 0); -mSCRIPT_DECLARE_STRUCT_METHOD(mScriptMemoryDomain, WRAPPER, name, mScriptMemoryDomainName, 0); +mSCRIPT_DECLARE_STRUCT_METHOD(mScriptMemoryDomain, WSTR, name, mScriptMemoryDomainName, 0); mSCRIPT_DEFINE_STRUCT(mScriptMemoryDomain) mSCRIPT_DEFINE_CLASS_DOCSTRING( @@ -373,8 +373,8 @@ static struct mScriptValue* _mScriptCoreSaveState(struct mCore* core, int32_t fl return value; } -static int32_t _mScriptCoreLoadState(struct mCore* core, struct mScriptValue* buffer, int32_t flags) { - struct VFile* vf = VFileFromConstMemory(buffer->value.string->buffer, buffer->value.string->size); +static int32_t _mScriptCoreLoadState(struct mCore* core, struct mScriptString* buffer, int32_t flags) { + struct VFile* vf = VFileFromConstMemory(buffer->buffer, buffer->size); int ret = mCoreLoadStateNamed(core, vf, flags); vf->close(vf); return ret; @@ -398,9 +398,9 @@ mSCRIPT_DECLARE_STRUCT_CD_METHOD(mCore, S32, platform, 0); mSCRIPT_DECLARE_STRUCT_CD_METHOD(mCore, U32, frameCounter, 0); mSCRIPT_DECLARE_STRUCT_CD_METHOD(mCore, S32, frameCycles, 0); mSCRIPT_DECLARE_STRUCT_CD_METHOD(mCore, S32, frequency, 0); -mSCRIPT_DECLARE_STRUCT_C_METHOD(mCore, WRAPPER, getGameTitle, _mScriptCoreGetGameTitle, 0); -mSCRIPT_DECLARE_STRUCT_C_METHOD(mCore, WRAPPER, getGameCode, _mScriptCoreGetGameCode, 0); -mSCRIPT_DECLARE_STRUCT_C_METHOD_WITH_DEFAULTS(mCore, WRAPPER, checksum, _mScriptCoreChecksum, 1, S32, type); +mSCRIPT_DECLARE_STRUCT_C_METHOD(mCore, WSTR, getGameTitle, _mScriptCoreGetGameTitle, 0); +mSCRIPT_DECLARE_STRUCT_C_METHOD(mCore, WSTR, getGameCode, _mScriptCoreGetGameCode, 0); +mSCRIPT_DECLARE_STRUCT_C_METHOD_WITH_DEFAULTS(mCore, WSTR, checksum, _mScriptCoreChecksum, 1, S32, type); // Run functions mSCRIPT_DECLARE_STRUCT_VOID_D_METHOD(mCore, reset, 0); @@ -420,20 +420,20 @@ mSCRIPT_DECLARE_STRUCT_D_METHOD(mCore, U32, getKeys, 0); mSCRIPT_DECLARE_STRUCT_D_METHOD(mCore, U32, busRead8, 1, U32, address); mSCRIPT_DECLARE_STRUCT_D_METHOD(mCore, U32, busRead16, 1, U32, address); mSCRIPT_DECLARE_STRUCT_D_METHOD(mCore, U32, busRead32, 1, U32, address); -mSCRIPT_DECLARE_STRUCT_METHOD(mCore, WRAPPER, readRange, _mScriptCoreReadRange, 2, U32, address, U32, length); +mSCRIPT_DECLARE_STRUCT_METHOD(mCore, WSTR, readRange, _mScriptCoreReadRange, 2, U32, address, U32, length); mSCRIPT_DECLARE_STRUCT_VOID_D_METHOD(mCore, busWrite8, 2, U32, address, U8, value); mSCRIPT_DECLARE_STRUCT_VOID_D_METHOD(mCore, busWrite16, 2, U32, address, U16, value); mSCRIPT_DECLARE_STRUCT_VOID_D_METHOD(mCore, busWrite32, 2, U32, address, U32, value); // Register functions -mSCRIPT_DECLARE_STRUCT_METHOD(mCore, WRAPPER, readRegister, _mScriptCoreReadRegister, 1, CHARP, regName); +mSCRIPT_DECLARE_STRUCT_METHOD(mCore, WSTR, readRegister, _mScriptCoreReadRegister, 1, CHARP, regName); mSCRIPT_DECLARE_STRUCT_VOID_METHOD(mCore, writeRegister, _mScriptCoreWriteRegister, 2, CHARP, regName, S32, value); // Savestate functions mSCRIPT_DECLARE_STRUCT_METHOD_WITH_DEFAULTS(mCore, S32, saveStateSlot, mCoreSaveState, 2, S32, slot, S32, flags); -mSCRIPT_DECLARE_STRUCT_METHOD_WITH_DEFAULTS(mCore, WRAPPER, saveStateBuffer, _mScriptCoreSaveState, 1, S32, flags); +mSCRIPT_DECLARE_STRUCT_METHOD_WITH_DEFAULTS(mCore, WSTR, saveStateBuffer, _mScriptCoreSaveState, 1, S32, flags); mSCRIPT_DECLARE_STRUCT_METHOD_WITH_DEFAULTS(mCore, S32, loadStateSlot, mCoreLoadState, 2, S32, slot, S32, flags); -mSCRIPT_DECLARE_STRUCT_METHOD_WITH_DEFAULTS(mCore, S32, loadStateBuffer, _mScriptCoreLoadState, 2, WRAPPER, buffer, S32, flags); +mSCRIPT_DECLARE_STRUCT_METHOD_WITH_DEFAULTS(mCore, S32, loadStateBuffer, _mScriptCoreLoadState, 2, STR, buffer, S32, flags); // Miscellaneous functions mSCRIPT_DECLARE_STRUCT_VOID_METHOD_WITH_DEFAULTS(mCore, screenshot, _mScriptCoreTakeScreenshot, 1, CHARP, filename); @@ -605,7 +605,7 @@ static void _mScriptCoreAdapterReset(struct mScriptCoreAdapter* adapter) { } mSCRIPT_DECLARE_STRUCT(mScriptCoreAdapter); -mSCRIPT_DECLARE_STRUCT_METHOD(mScriptCoreAdapter, WRAPPER, _get, _mScriptCoreAdapterGet, 1, CHARP, name); +mSCRIPT_DECLARE_STRUCT_METHOD(mScriptCoreAdapter, W(mCore), _get, _mScriptCoreAdapterGet, 1, CHARP, name); mSCRIPT_DECLARE_STRUCT_VOID_METHOD(mScriptCoreAdapter, _deinit, _mScriptCoreAdapterDeinit, 0); mSCRIPT_DECLARE_STRUCT_VOID_METHOD(mScriptCoreAdapter, reset, _mScriptCoreAdapterReset, 0); diff --git a/src/script/engines/lua.c b/src/script/engines/lua.c index ac26345b4..5c8906531 100644 --- a/src/script/engines/lua.c +++ b/src/script/engines/lua.c @@ -270,7 +270,7 @@ bool _luaWrap(struct mScriptEngineContextLua* luaContext, struct mScriptValue* v } uint32_t weakref; bool needsWeakref = false; - if (value->type == mSCRIPT_TYPE_MS_WRAPPER) { + if (value->type->base == mSCRIPT_TYPE_WRAPPER) { value = mScriptValueUnwrap(value); if (!value) { lua_pushnil(luaContext->lua); @@ -826,7 +826,7 @@ int _luaGetList(lua_State* lua) { lua_pop(lua, 2); obj = mScriptContextAccessWeakref(luaContext->d.context, obj); - if (obj->type == mSCRIPT_TYPE_MS_WRAPPER) { + if (obj->type->base == mSCRIPT_TYPE_WRAPPER) { obj = mScriptValueUnwrap(obj); } if (!obj || obj->type != mSCRIPT_TYPE_MS_LIST) { @@ -859,7 +859,7 @@ static int _luaLenList(lua_State* lua) { lua_pop(lua, 1); obj = mScriptContextAccessWeakref(luaContext->d.context, obj); - if (obj->type == mSCRIPT_TYPE_MS_WRAPPER) { + if (obj->type->base == mSCRIPT_TYPE_WRAPPER) { obj = mScriptValueUnwrap(obj); } if (!obj || obj->type != mSCRIPT_TYPE_MS_LIST) { diff --git a/src/script/types.c b/src/script/types.c index b22ee1ad8..4ab6653e1 100644 --- a/src/script/types.c +++ b/src/script/types.c @@ -212,6 +212,15 @@ const struct mScriptType mSTWrapper = { .hash = NULL, }; +const struct mScriptType mSTStringWrapper = { + .base = mSCRIPT_TYPE_WRAPPER, + .size = sizeof(struct mScriptValue), + .name = "wrapper string", + .alloc = NULL, + .free = NULL, + .hash = NULL, +}; + const struct mScriptType mSTWeakref = { .base = mSCRIPT_TYPE_WEAKREF, .size = sizeof(uint32_t), @@ -745,14 +754,14 @@ void mScriptValueWrap(struct mScriptValue* value, struct mScriptValue* out) { } struct mScriptValue* mScriptValueUnwrap(struct mScriptValue* value) { - if (value->type == mSCRIPT_TYPE_MS_WRAPPER) { + if (value->type->base == mSCRIPT_TYPE_WRAPPER) { return value->value.opaque; } return NULL; } const struct mScriptValue* mScriptValueUnwrapConst(const struct mScriptValue* value) { - if (value->type == mSCRIPT_TYPE_MS_WRAPPER) { + if (value->type->base == mSCRIPT_TYPE_WRAPPER) { return value->value.copaque; } return NULL; @@ -833,7 +842,7 @@ bool mScriptTableRemove(struct mScriptValue* table, struct mScriptValue* key) { } struct mScriptValue* mScriptTableLookup(struct mScriptValue* table, struct mScriptValue* key) { - if (table->type == mSCRIPT_TYPE_MS_WRAPPER) { + if (table->type->base == mSCRIPT_TYPE_WRAPPER) { table = mScriptValueUnwrap(table); } if (table->type != mSCRIPT_TYPE_MS_TABLE) { @@ -861,7 +870,7 @@ size_t mScriptTableSize(struct mScriptValue* table) { } bool mScriptTableIteratorStart(struct mScriptValue* table, struct TableIterator* iter) { - if (table->type == mSCRIPT_TYPE_MS_WRAPPER) { + if (table->type->base == mSCRIPT_TYPE_WRAPPER) { table = mScriptValueUnwrap(table); } if (table->type != mSCRIPT_TYPE_MS_TABLE) { @@ -871,7 +880,7 @@ bool mScriptTableIteratorStart(struct mScriptValue* table, struct TableIterator* } bool mScriptTableIteratorNext(struct mScriptValue* table, struct TableIterator* iter) { - if (table->type == mSCRIPT_TYPE_MS_WRAPPER) { + if (table->type->base == mSCRIPT_TYPE_WRAPPER) { table = mScriptValueUnwrap(table); } if (table->type != mSCRIPT_TYPE_MS_TABLE) { @@ -881,7 +890,7 @@ bool mScriptTableIteratorNext(struct mScriptValue* table, struct TableIterator* } struct mScriptValue* mScriptTableIteratorGetKey(struct mScriptValue* table, struct TableIterator* iter) { - if (table->type == mSCRIPT_TYPE_MS_WRAPPER) { + if (table->type->base == mSCRIPT_TYPE_WRAPPER) { table = mScriptValueUnwrap(table); } if (table->type != mSCRIPT_TYPE_MS_TABLE) { @@ -891,7 +900,7 @@ struct mScriptValue* mScriptTableIteratorGetKey(struct mScriptValue* table, stru } struct mScriptValue* mScriptTableIteratorGetValue(struct mScriptValue* table, struct TableIterator* iter) { - if (table->type == mSCRIPT_TYPE_MS_WRAPPER) { + if (table->type->base == mSCRIPT_TYPE_WRAPPER) { table = mScriptValueUnwrap(table); } if (table->type != mSCRIPT_TYPE_MS_TABLE) { @@ -901,7 +910,7 @@ struct mScriptValue* mScriptTableIteratorGetValue(struct mScriptValue* table, st } bool mScriptTableIteratorLookup(struct mScriptValue* table, struct TableIterator* iter, struct mScriptValue* key) { - if (table->type == mSCRIPT_TYPE_MS_WRAPPER) { + if (table->type->base == mSCRIPT_TYPE_WRAPPER) { table = mScriptValueUnwrap(table); } if (table->type != mSCRIPT_TYPE_MS_TABLE) { @@ -1368,7 +1377,7 @@ bool mScriptCoerceFrame(const struct mScriptTypeTuple* types, struct mScriptList continue; } struct mScriptValue* unwrapped = NULL; - if (mScriptListGetPointer(frame, i)->type == mSCRIPT_TYPE_MS_WRAPPER) { + if (mScriptListGetPointer(frame, i)->type->base == mSCRIPT_TYPE_WRAPPER) { unwrapped = mScriptValueUnwrap(mScriptListGetPointer(frame, i)); if (types->entries[i] == unwrapped->type) { continue;