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/table.h>
|
||||||
#include <mgba-util/vfs.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 mScriptFrame;
|
||||||
struct mScriptFunction;
|
struct mScriptFunction;
|
||||||
struct mScriptEngineContext;
|
struct mScriptEngineContext;
|
||||||
|
@ -25,6 +28,7 @@ struct mScriptContext {
|
||||||
struct Table weakrefs;
|
struct Table weakrefs;
|
||||||
uint32_t nextWeakref;
|
uint32_t nextWeakref;
|
||||||
struct Table callbacks;
|
struct Table callbacks;
|
||||||
|
struct mScriptValue* constants;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct mScriptEngine2 {
|
struct mScriptEngine2 {
|
||||||
|
@ -50,6 +54,11 @@ struct mScriptEngineContext {
|
||||||
const char* (*getError)(struct mScriptEngineContext*);
|
const char* (*getError)(struct mScriptEngineContext*);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct mScriptKVPair {
|
||||||
|
const char* key;
|
||||||
|
struct mScriptValue* value;
|
||||||
|
};
|
||||||
|
|
||||||
void mScriptContextInit(struct mScriptContext*);
|
void mScriptContextInit(struct mScriptContext*);
|
||||||
void mScriptContextDeinit(struct mScriptContext*);
|
void mScriptContextDeinit(struct mScriptContext*);
|
||||||
|
|
||||||
|
@ -70,6 +79,7 @@ struct mScriptValue* mScriptContextAccessWeakref(struct mScriptContext*, struct
|
||||||
void mScriptContextClearWeakref(struct mScriptContext*, uint32_t weakref);
|
void mScriptContextClearWeakref(struct mScriptContext*, uint32_t weakref);
|
||||||
|
|
||||||
void mScriptContextAttachStdlib(struct mScriptContext* context);
|
void mScriptContextAttachStdlib(struct mScriptContext* context);
|
||||||
|
void mScriptContextExportConstants(struct mScriptContext* context, const char* nspace, struct mScriptKVPair* constants);
|
||||||
|
|
||||||
void mScriptContextTriggerCallback(struct mScriptContext*, const char* callback);
|
void mScriptContextTriggerCallback(struct mScriptContext*, const char* callback);
|
||||||
void mScriptContextAddCallback(struct mScriptContext*, const char* callback, struct mScriptValue* value);
|
void mScriptContextAddCallback(struct mScriptContext*, const char* callback, struct mScriptValue* value);
|
||||||
|
|
|
@ -8,11 +8,6 @@
|
||||||
#include <mgba/internal/script/lua.h>
|
#include <mgba/internal/script/lua.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct mScriptKVPair {
|
|
||||||
const char* key;
|
|
||||||
struct mScriptValue* value;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct mScriptFileInfo {
|
struct mScriptFileInfo {
|
||||||
const char* name;
|
const char* name;
|
||||||
struct VFile* vf;
|
struct VFile* vf;
|
||||||
|
@ -61,6 +56,7 @@ void mScriptContextInit(struct mScriptContext* context) {
|
||||||
TableInit(&context->weakrefs, 0, (void (*)(void*)) mScriptValueDeref);
|
TableInit(&context->weakrefs, 0, (void (*)(void*)) mScriptValueDeref);
|
||||||
context->nextWeakref = 1;
|
context->nextWeakref = 1;
|
||||||
HashTableInit(&context->callbacks, 0, (void (*)(void*)) mScriptValueDeref);
|
HashTableInit(&context->callbacks, 0, (void (*)(void*)) mScriptValueDeref);
|
||||||
|
context->constants = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mScriptContextDeinit(struct mScriptContext* context) {
|
void mScriptContextDeinit(struct mScriptContext* context) {
|
||||||
|
@ -223,6 +219,24 @@ void mScriptContextAddCallback(struct mScriptContext* context, const char* callb
|
||||||
mScriptValueWrap(fn, mScriptListAppend(list->value.opaque));
|
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) {
|
bool mScriptContextLoadVF(struct mScriptContext* context, const char* name, struct VFile* vf) {
|
||||||
struct mScriptFileInfo info = {
|
struct mScriptFileInfo info = {
|
||||||
.name = name,
|
.name = name,
|
||||||
|
|
|
@ -5,6 +5,9 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
#include <mgba/script/context.h>
|
#include <mgba/script/context.h>
|
||||||
|
|
||||||
|
#include <mgba/core/core.h>
|
||||||
|
#include <mgba/core/serialize.h>
|
||||||
|
|
||||||
struct mScriptCallbackAdapter {
|
struct mScriptCallbackAdapter {
|
||||||
struct mScriptContext* context;
|
struct mScriptContext* context;
|
||||||
};
|
};
|
||||||
|
@ -34,4 +37,21 @@ void mScriptContextAttachStdlib(struct mScriptContext* context) {
|
||||||
};
|
};
|
||||||
lib->flags = mSCRIPT_VALUE_FLAG_FREE_BUFFER;
|
lib->flags = mSCRIPT_VALUE_FLAG_FREE_BUFFER;
|
||||||
mScriptContextSetGlobal(context, "callbacks", lib);
|
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