mirror of https://github.com/mgba-emu/mgba.git
Scripting: Memory fixes
This commit is contained in:
parent
304d8d1a4d
commit
7bc7656988
|
@ -60,12 +60,12 @@ void mScriptContextInit(struct mScriptContext* context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void mScriptContextDeinit(struct mScriptContext* context) {
|
void mScriptContextDeinit(struct mScriptContext* context) {
|
||||||
HashTableDeinit(&context->engines);
|
|
||||||
HashTableDeinit(&context->rootScope);
|
HashTableDeinit(&context->rootScope);
|
||||||
HashTableDeinit(&context->weakrefs);
|
HashTableDeinit(&context->weakrefs);
|
||||||
mScriptContextDrainPool(context);
|
mScriptContextDrainPool(context);
|
||||||
mScriptListDeinit(&context->refPool);
|
mScriptListDeinit(&context->refPool);
|
||||||
HashTableDeinit(&context->callbacks);
|
HashTableDeinit(&context->callbacks);
|
||||||
|
HashTableDeinit(&context->engines);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mScriptContextFillPool(struct mScriptContext* context, struct mScriptValue* value) {
|
void mScriptContextFillPool(struct mScriptContext* context, struct mScriptValue* value) {
|
||||||
|
|
|
@ -26,7 +26,7 @@ static const char* _luaGetError(struct mScriptEngineContext*);
|
||||||
static bool _luaCall(struct mScriptFrame*, void* context);
|
static bool _luaCall(struct mScriptFrame*, void* context);
|
||||||
|
|
||||||
struct mScriptEngineContextLua;
|
struct mScriptEngineContextLua;
|
||||||
static bool _luaPushFrame(struct mScriptEngineContextLua*, struct mScriptList*);
|
static bool _luaPushFrame(struct mScriptEngineContextLua*, struct mScriptList*, bool internal);
|
||||||
static bool _luaPopFrame(struct mScriptEngineContextLua*, struct mScriptList*);
|
static bool _luaPopFrame(struct mScriptEngineContextLua*, struct mScriptList*);
|
||||||
static bool _luaInvoke(struct mScriptEngineContextLua*, struct mScriptFrame*);
|
static bool _luaInvoke(struct mScriptEngineContextLua*, struct mScriptFrame*);
|
||||||
|
|
||||||
|
@ -238,10 +238,10 @@ struct mScriptValue* _luaCoerce(struct mScriptEngineContextLua* luaContext) {
|
||||||
case LUA_TSTRING:
|
case LUA_TSTRING:
|
||||||
buffer = lua_tolstring(luaContext->lua, -1, &size);
|
buffer = lua_tolstring(luaContext->lua, -1, &size);
|
||||||
value = mScriptStringCreateFromBytes(buffer, size);
|
value = mScriptStringCreateFromBytes(buffer, size);
|
||||||
mScriptValueWrap(value, mScriptListAppend(&luaContext->d.context->refPool));
|
mScriptContextFillPool(luaContext->d.context, value);
|
||||||
mScriptValueDeref(value);
|
|
||||||
break;
|
break;
|
||||||
case LUA_TFUNCTION:
|
case LUA_TFUNCTION:
|
||||||
|
// This function pops the value internally via luaL_ref
|
||||||
return _luaCoerceFunction(luaContext);
|
return _luaCoerceFunction(luaContext);
|
||||||
case LUA_TUSERDATA:
|
case LUA_TUSERDATA:
|
||||||
if (!lua_getmetatable(luaContext->lua, -1)) {
|
if (!lua_getmetatable(luaContext->lua, -1)) {
|
||||||
|
@ -255,6 +255,7 @@ struct mScriptValue* _luaCoerce(struct mScriptEngineContextLua* luaContext) {
|
||||||
lua_pop(luaContext->lua, 2);
|
lua_pop(luaContext->lua, 2);
|
||||||
value = lua_touserdata(luaContext->lua, -1);
|
value = lua_touserdata(luaContext->lua, -1);
|
||||||
value = mScriptContextAccessWeakref(luaContext->d.context, value);
|
value = mScriptContextAccessWeakref(luaContext->d.context, value);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
lua_pop(luaContext->lua, 1);
|
lua_pop(luaContext->lua, 1);
|
||||||
return value;
|
return value;
|
||||||
|
@ -315,7 +316,6 @@ bool _luaWrap(struct mScriptEngineContextLua* luaContext, struct mScriptValue* v
|
||||||
break;
|
break;
|
||||||
case mSCRIPT_TYPE_STRING:
|
case mSCRIPT_TYPE_STRING:
|
||||||
lua_pushlstring(luaContext->lua, value->value.string->buffer, value->value.string->size);
|
lua_pushlstring(luaContext->lua, value->value.string->buffer, value->value.string->size);
|
||||||
mScriptValueDeref(value);
|
|
||||||
break;
|
break;
|
||||||
case mSCRIPT_TYPE_LIST:
|
case mSCRIPT_TYPE_LIST:
|
||||||
newValue = lua_newuserdata(luaContext->lua, sizeof(*newValue));
|
newValue = lua_newuserdata(luaContext->lua, sizeof(*newValue));
|
||||||
|
@ -333,6 +333,7 @@ bool _luaWrap(struct mScriptEngineContextLua* luaContext, struct mScriptValue* v
|
||||||
*newValue = mSCRIPT_MAKE(WEAKREF, weakref);
|
*newValue = mSCRIPT_MAKE(WEAKREF, weakref);
|
||||||
} else {
|
} else {
|
||||||
mScriptValueWrap(value, newValue);
|
mScriptValueWrap(value, newValue);
|
||||||
|
mScriptValueDeref(value);
|
||||||
}
|
}
|
||||||
luaL_setmetatable(luaContext->lua, "mSTTable");
|
luaL_setmetatable(luaContext->lua, "mSTTable");
|
||||||
break;
|
break;
|
||||||
|
@ -350,6 +351,7 @@ bool _luaWrap(struct mScriptEngineContextLua* luaContext, struct mScriptValue* v
|
||||||
*newValue = mSCRIPT_MAKE(WEAKREF, weakref);
|
*newValue = mSCRIPT_MAKE(WEAKREF, weakref);
|
||||||
} else {
|
} else {
|
||||||
mScriptValueWrap(value, newValue);
|
mScriptValueWrap(value, newValue);
|
||||||
|
mScriptValueDeref(value);
|
||||||
}
|
}
|
||||||
luaL_setmetatable(luaContext->lua, "mSTStruct");
|
luaL_setmetatable(luaContext->lua, "mSTStruct");
|
||||||
break;
|
break;
|
||||||
|
@ -413,12 +415,17 @@ const char* _luaGetError(struct mScriptEngineContext* context) {
|
||||||
return luaContext->lastError;
|
return luaContext->lastError;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _luaPushFrame(struct mScriptEngineContextLua* luaContext, struct mScriptList* frame) {
|
bool _luaPushFrame(struct mScriptEngineContextLua* luaContext, struct mScriptList* frame, bool internal) {
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
if (frame) {
|
if (frame) {
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < mScriptListSize(frame); ++i) {
|
for (i = 0; i < mScriptListSize(frame); ++i) {
|
||||||
if (!_luaWrap(luaContext, mScriptListGetPointer(frame, i))) {
|
struct mScriptValue* value = mScriptListGetPointer(frame, i);
|
||||||
|
if (internal && value->type->base == mSCRIPT_TYPE_WRAPPER) {
|
||||||
|
value = mScriptValueUnwrap(value);
|
||||||
|
mScriptContextFillPool(luaContext->d.context, value);
|
||||||
|
}
|
||||||
|
if (!_luaWrap(luaContext, value)) {
|
||||||
ok = false;
|
ok = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -480,7 +487,7 @@ bool _luaInvoke(struct mScriptEngineContextLua* luaContext, struct mScriptFrame*
|
||||||
luaContext->lastError = NULL;
|
luaContext->lastError = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (frame && !_luaPushFrame(luaContext, &frame->arguments)) {
|
if (frame && !_luaPushFrame(luaContext, &frame->arguments, false)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -557,13 +564,13 @@ int _luaThunk(lua_State* lua) {
|
||||||
luaL_traceback(lua, lua, "Error calling function (invoking failed)", 1);
|
luaL_traceback(lua, lua, "Error calling function (invoking failed)", 1);
|
||||||
lua_error(lua);
|
lua_error(lua);
|
||||||
}
|
}
|
||||||
mScriptContextDrainPool(luaContext->d.context);
|
|
||||||
|
|
||||||
if (!_luaPushFrame(luaContext, &frame.returnValues)) {
|
if (!_luaPushFrame(luaContext, &frame.returnValues, true)) {
|
||||||
mScriptFrameDeinit(&frame);
|
mScriptFrameDeinit(&frame);
|
||||||
luaL_traceback(lua, lua, "Error calling function (translating return values from runtime)", 1);
|
luaL_traceback(lua, lua, "Error calling function (translating return values from runtime)", 1);
|
||||||
lua_error(lua);
|
lua_error(lua);
|
||||||
}
|
}
|
||||||
|
mScriptContextDrainPool(luaContext->d.context);
|
||||||
mScriptFrameDeinit(&frame);
|
mScriptFrameDeinit(&frame);
|
||||||
|
|
||||||
return lua_gettop(luaContext->lua);
|
return lua_gettop(luaContext->lua);
|
||||||
|
|
|
@ -18,6 +18,7 @@ static void _mScriptCallbackAdd(struct mScriptCallbackAdapter* adapter, struct m
|
||||||
fn = mScriptValueUnwrap(fn);
|
fn = mScriptValueUnwrap(fn);
|
||||||
}
|
}
|
||||||
mScriptContextAddCallback(adapter->context, name->buffer, fn);
|
mScriptContextAddCallback(adapter->context, name->buffer, fn);
|
||||||
|
mScriptValueDeref(fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
mSCRIPT_DECLARE_STRUCT(mScriptCallbackAdapter);
|
mSCRIPT_DECLARE_STRUCT(mScriptCallbackAdapter);
|
||||||
|
|
Loading…
Reference in New Issue