mirror of https://github.com/mgba-emu/mgba.git
Scripting: Start exporting some constants
This commit is contained in:
parent
303fc17e77
commit
5c84278667
|
@ -14,6 +14,9 @@ CXX_GUARD_START
|
|||
#include <mgba-util/table.h>
|
||||
#include <mgba-util/vfs.h>
|
||||
|
||||
#define mSCRIPT_CONSTANT_PAIR(NS, CONST) { #CONST, mScriptValueCreateFromSInt(NS ## _ ## CONST) }
|
||||
#define mSCRIPT_CONSTANT_SENTINEL { NULL, NULL }
|
||||
|
||||
struct mScriptFrame;
|
||||
struct mScriptFunction;
|
||||
struct mScriptEngineContext;
|
||||
|
@ -25,6 +28,7 @@ struct mScriptContext {
|
|||
struct Table weakrefs;
|
||||
uint32_t nextWeakref;
|
||||
struct Table callbacks;
|
||||
struct mScriptValue* constants;
|
||||
};
|
||||
|
||||
struct mScriptEngine2 {
|
||||
|
@ -50,6 +54,11 @@ struct mScriptEngineContext {
|
|||
const char* (*getError)(struct mScriptEngineContext*);
|
||||
};
|
||||
|
||||
struct mScriptKVPair {
|
||||
const char* key;
|
||||
struct mScriptValue* value;
|
||||
};
|
||||
|
||||
void mScriptContextInit(struct mScriptContext*);
|
||||
void mScriptContextDeinit(struct mScriptContext*);
|
||||
|
||||
|
@ -70,6 +79,7 @@ struct mScriptValue* mScriptContextAccessWeakref(struct mScriptContext*, struct
|
|||
void mScriptContextClearWeakref(struct mScriptContext*, uint32_t weakref);
|
||||
|
||||
void mScriptContextAttachStdlib(struct mScriptContext* context);
|
||||
void mScriptContextExportConstants(struct mScriptContext* context, const char* nspace, struct mScriptKVPair* constants);
|
||||
|
||||
void mScriptContextTriggerCallback(struct mScriptContext*, const char* callback);
|
||||
void mScriptContextAddCallback(struct mScriptContext*, const char* callback, struct mScriptValue* value);
|
||||
|
|
|
@ -8,11 +8,6 @@
|
|||
#include <mgba/internal/script/lua.h>
|
||||
#endif
|
||||
|
||||
struct mScriptKVPair {
|
||||
const char* key;
|
||||
struct mScriptValue* value;
|
||||
};
|
||||
|
||||
struct mScriptFileInfo {
|
||||
const char* name;
|
||||
struct VFile* vf;
|
||||
|
@ -61,6 +56,7 @@ void mScriptContextInit(struct mScriptContext* context) {
|
|||
TableInit(&context->weakrefs, 0, (void (*)(void*)) mScriptValueDeref);
|
||||
context->nextWeakref = 1;
|
||||
HashTableInit(&context->callbacks, 0, (void (*)(void*)) mScriptValueDeref);
|
||||
context->constants = NULL;
|
||||
}
|
||||
|
||||
void mScriptContextDeinit(struct mScriptContext* context) {
|
||||
|
@ -223,6 +219,24 @@ void mScriptContextAddCallback(struct mScriptContext* context, const char* callb
|
|||
mScriptValueWrap(fn, mScriptListAppend(list->value.opaque));
|
||||
}
|
||||
|
||||
void mScriptContextExportConstants(struct mScriptContext* context, const char* nspace, struct mScriptKVPair* constants) {
|
||||
if (!context->constants) {
|
||||
context->constants = mScriptValueAlloc(mSCRIPT_TYPE_MS_TABLE);
|
||||
}
|
||||
struct mScriptValue* table = mScriptValueAlloc(mSCRIPT_TYPE_MS_TABLE);
|
||||
size_t i;
|
||||
for (i = 0; constants[i].key; ++i) {
|
||||
struct mScriptValue* key = mScriptStringCreateFromUTF8(constants[i].key);
|
||||
mScriptTableInsert(table, key, constants[i].value);
|
||||
mScriptValueDeref(key);
|
||||
mScriptValueDeref(constants[i].value);
|
||||
}
|
||||
struct mScriptValue* key = mScriptStringCreateFromUTF8(nspace);
|
||||
mScriptTableInsert(context->constants, key, table);
|
||||
mScriptValueDeref(key);
|
||||
mScriptValueDeref(table);
|
||||
}
|
||||
|
||||
bool mScriptContextLoadVF(struct mScriptContext* context, const char* name, struct VFile* vf) {
|
||||
struct mScriptFileInfo info = {
|
||||
.name = name,
|
||||
|
|
|
@ -5,6 +5,9 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
#include <mgba/script/context.h>
|
||||
|
||||
#include <mgba/core/core.h>
|
||||
#include <mgba/core/serialize.h>
|
||||
|
||||
struct mScriptCallbackAdapter {
|
||||
struct mScriptContext* context;
|
||||
};
|
||||
|
@ -34,4 +37,21 @@ void mScriptContextAttachStdlib(struct mScriptContext* context) {
|
|||
};
|
||||
lib->flags = mSCRIPT_VALUE_FLAG_FREE_BUFFER;
|
||||
mScriptContextSetGlobal(context, "callbacks", lib);
|
||||
|
||||
mScriptContextExportConstants(context, "SAVESTATE", (struct mScriptKVPair[]) {
|
||||
mSCRIPT_CONSTANT_PAIR(SAVESTATE, SCREENSHOT),
|
||||
mSCRIPT_CONSTANT_PAIR(SAVESTATE, SAVEDATA),
|
||||
mSCRIPT_CONSTANT_PAIR(SAVESTATE, CHEATS),
|
||||
mSCRIPT_CONSTANT_PAIR(SAVESTATE, RTC),
|
||||
mSCRIPT_CONSTANT_PAIR(SAVESTATE, METADATA),
|
||||
mSCRIPT_CONSTANT_PAIR(SAVESTATE, ALL),
|
||||
mSCRIPT_CONSTANT_SENTINEL
|
||||
});
|
||||
mScriptContextExportConstants(context, "PLATFORM", (struct mScriptKVPair[]) {
|
||||
mSCRIPT_CONSTANT_PAIR(mPLATFORM, NONE),
|
||||
mSCRIPT_CONSTANT_PAIR(mPLATFORM, GBA),
|
||||
mSCRIPT_CONSTANT_PAIR(mPLATFORM, GB),
|
||||
mSCRIPT_CONSTANT_SENTINEL
|
||||
});
|
||||
mScriptContextSetGlobal(context, "C", context->constants);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue