mirror of https://github.com/mgba-emu/mgba.git
Scripting: More table scaffolding
This commit is contained in:
parent
c6e18b2a59
commit
bbf6d94fe2
|
@ -631,6 +631,7 @@ struct mScriptValue* mScriptStringCreateFromUTF8(const char* string);
|
||||||
bool mScriptTableInsert(struct mScriptValue* table, struct mScriptValue* key, struct mScriptValue* value);
|
bool mScriptTableInsert(struct mScriptValue* table, struct mScriptValue* key, struct mScriptValue* value);
|
||||||
bool mScriptTableRemove(struct mScriptValue* table, struct mScriptValue* key);
|
bool mScriptTableRemove(struct mScriptValue* table, struct mScriptValue* key);
|
||||||
struct mScriptValue* mScriptTableLookup(struct mScriptValue* table, struct mScriptValue* key);
|
struct mScriptValue* mScriptTableLookup(struct mScriptValue* table, struct mScriptValue* key);
|
||||||
|
bool mScriptTableClear(struct mScriptValue* table);
|
||||||
|
|
||||||
void mScriptFrameInit(struct mScriptFrame* frame);
|
void mScriptFrameInit(struct mScriptFrame* frame);
|
||||||
void mScriptFrameDeinit(struct mScriptFrame* frame);
|
void mScriptFrameDeinit(struct mScriptFrame* frame);
|
||||||
|
|
|
@ -38,6 +38,7 @@ static int _luaThunk(lua_State* lua);
|
||||||
static int _luaGetObject(lua_State* lua);
|
static int _luaGetObject(lua_State* lua);
|
||||||
static int _luaSetObject(lua_State* lua);
|
static int _luaSetObject(lua_State* lua);
|
||||||
static int _luaGcObject(lua_State* lua);
|
static int _luaGcObject(lua_State* lua);
|
||||||
|
static int _luaGetTable(lua_State* lua);
|
||||||
|
|
||||||
#if LUA_VERSION_NUM < 503
|
#if LUA_VERSION_NUM < 503
|
||||||
#define lua_pushinteger lua_pushnumber
|
#define lua_pushinteger lua_pushnumber
|
||||||
|
@ -96,6 +97,11 @@ static const luaL_Reg _mSTStruct[] = {
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static const luaL_Reg _mSTTable[] = {
|
||||||
|
{ "__index", _luaGetTable },
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
struct mScriptEngineContext* _luaCreate(struct mScriptEngine2* engine, struct mScriptContext* context) {
|
struct mScriptEngineContext* _luaCreate(struct mScriptEngine2* engine, struct mScriptContext* context) {
|
||||||
UNUSED(engine);
|
UNUSED(engine);
|
||||||
struct mScriptEngineContextLua* luaContext = calloc(1, sizeof(*luaContext));
|
struct mScriptEngineContextLua* luaContext = calloc(1, sizeof(*luaContext));
|
||||||
|
@ -122,6 +128,14 @@ struct mScriptEngineContext* _luaCreate(struct mScriptEngine2* engine, struct mS
|
||||||
#endif
|
#endif
|
||||||
lua_pop(luaContext->lua, 1);
|
lua_pop(luaContext->lua, 1);
|
||||||
|
|
||||||
|
luaL_newmetatable(luaContext->lua, "mSTTable");
|
||||||
|
#if LUA_VERSION_NUM < 502
|
||||||
|
luaL_register(luaContext->lua, NULL, _mSTTable);
|
||||||
|
#else
|
||||||
|
luaL_setfuncs(luaContext->lua, _mSTTable, 0);
|
||||||
|
#endif
|
||||||
|
lua_pop(luaContext->lua, 1);
|
||||||
|
|
||||||
return &luaContext->d;
|
return &luaContext->d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -268,6 +282,15 @@ bool _luaWrap(struct mScriptEngineContextLua* luaContext, struct mScriptValue* v
|
||||||
lua_pushstring(luaContext->lua, ((struct mScriptString*) value->value.opaque)->buffer);
|
lua_pushstring(luaContext->lua, ((struct mScriptString*) value->value.opaque)->buffer);
|
||||||
mScriptValueDeref(value);
|
mScriptValueDeref(value);
|
||||||
break;
|
break;
|
||||||
|
case mSCRIPT_TYPE_TABLE:
|
||||||
|
newValue = lua_newuserdata(luaContext->lua, sizeof(*newValue));
|
||||||
|
if (needsWeakref) {
|
||||||
|
*newValue = mSCRIPT_MAKE(WEAKREF, weakref);
|
||||||
|
} else {
|
||||||
|
mScriptValueWrap(value, newValue);
|
||||||
|
}
|
||||||
|
luaL_setmetatable(luaContext->lua, "mSTTable");
|
||||||
|
break;
|
||||||
case mSCRIPT_TYPE_FUNCTION:
|
case mSCRIPT_TYPE_FUNCTION:
|
||||||
newValue = lua_newuserdata(luaContext->lua, sizeof(*newValue));
|
newValue = lua_newuserdata(luaContext->lua, sizeof(*newValue));
|
||||||
newValue->type = value->type;
|
newValue->type = value->type;
|
||||||
|
@ -537,7 +560,6 @@ int _luaGetObject(lua_State* lua) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int _luaSetObject(lua_State* lua) {
|
int _luaSetObject(lua_State* lua) {
|
||||||
struct mScriptEngineContextLua* luaContext = _luaGetContext(lua);
|
struct mScriptEngineContextLua* luaContext = _luaGetContext(lua);
|
||||||
char key[MAX_KEY_SIZE];
|
char key[MAX_KEY_SIZE];
|
||||||
|
@ -583,3 +605,35 @@ static int _luaGcObject(lua_State* lua) {
|
||||||
mScriptValueDeref(val);
|
mScriptValueDeref(val);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int _luaGetTable(lua_State* lua) {
|
||||||
|
struct mScriptEngineContextLua* luaContext = _luaGetContext(lua);
|
||||||
|
char key[MAX_KEY_SIZE];
|
||||||
|
const char* keyPtr = lua_tostring(lua, -1);
|
||||||
|
struct mScriptValue* obj = lua_touserdata(lua, -2);
|
||||||
|
|
||||||
|
if (!keyPtr) {
|
||||||
|
lua_pop(lua, 2);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
strlcpy(key, keyPtr, sizeof(key));
|
||||||
|
lua_pop(lua, 2);
|
||||||
|
|
||||||
|
obj = mScriptContextAccessWeakref(luaContext->d.context, obj);
|
||||||
|
if (!obj) {
|
||||||
|
lua_pushliteral(lua, "Invalid object");
|
||||||
|
lua_error(lua);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mScriptValue keyVal = mSCRIPT_MAKE_CHARP(key);
|
||||||
|
struct mScriptValue* val = mScriptTableLookup(obj, &keyVal);
|
||||||
|
if (!val) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_luaWrap(luaContext, val)) {
|
||||||
|
lua_pushliteral(lua, "Invalid value");
|
||||||
|
lua_error(lua);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
|
@ -751,6 +751,15 @@ struct mScriptValue* mScriptTableLookup(struct mScriptValue* table, struct mScri
|
||||||
return HashTableLookupCustom(t, key);
|
return HashTableLookupCustom(t, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool mScriptTableClear(struct mScriptValue* table) {
|
||||||
|
if (table->type != mSCRIPT_TYPE_MS_TABLE) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
struct Table* t = table->value.opaque;
|
||||||
|
HashTableClear(t);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void mScriptFrameInit(struct mScriptFrame* frame) {
|
void mScriptFrameInit(struct mScriptFrame* frame) {
|
||||||
mScriptListInit(&frame->arguments, 4);
|
mScriptListInit(&frame->arguments, 4);
|
||||||
mScriptListInit(&frame->returnValues, 1);
|
mScriptListInit(&frame->returnValues, 1);
|
||||||
|
@ -874,6 +883,11 @@ static bool _accessRawMember(struct mScriptClassMember* member, void* raw, bool
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case mSCRIPT_TYPE_TABLE:
|
||||||
|
val->refs = mSCRIPT_VALUE_UNREF;
|
||||||
|
val->type = mSCRIPT_TYPE_MS_WRAPPER;
|
||||||
|
val->value.opaque = raw;
|
||||||
|
break;
|
||||||
case mSCRIPT_TYPE_FUNCTION:
|
case mSCRIPT_TYPE_FUNCTION:
|
||||||
val->refs = mSCRIPT_VALUE_UNREF;
|
val->refs = mSCRIPT_VALUE_UNREF;
|
||||||
val->flags = 0;
|
val->flags = 0;
|
||||||
|
|
Loading…
Reference in New Issue