Scripting: Enhance error reporting

This commit is contained in:
Vicki Pfau 2023-09-16 01:30:40 -07:00
parent 404e84b045
commit d2205d3475
2 changed files with 27 additions and 1 deletions

View File

@ -80,7 +80,6 @@ bool ScriptingController::load(VFileDevice& vf, const QString& name) {
} }
bool ok = true; bool ok = true;
if (!m_activeEngine->load(m_activeEngine, utf8.constData(), vf) || !m_activeEngine->run(m_activeEngine)) { if (!m_activeEngine->load(m_activeEngine, utf8.constData(), vf) || !m_activeEngine->run(m_activeEngine)) {
emit error(QString::fromUtf8(m_activeEngine->getError(m_activeEngine)));
ok = false; ok = false;
} }
if (m_controller) { if (m_controller) {

View File

@ -810,6 +810,30 @@ static const char* _reader(lua_State* lua, void* context, size_t* size) {
return reader->block; return reader->block;
} }
void _luaError(struct mScriptEngineContextLua* luaContext) {
struct mScriptValue* console = mScriptContextGetGlobal(luaContext->d.context, "console");
struct mScriptValue error = {0};
bool ok = false;
if (console) {
ok = mScriptObjectGet(console, "error", &error);
}
if (ok) {
struct mScriptFrame frame;
mScriptFrameInit(&frame);
struct mScriptValue* this = mScriptListAppend(&frame.arguments);
this->type = console->type;
this->refs = mSCRIPT_VALUE_UNREF;
this->flags = 0;
this->value.opaque = console->value.opaque;
mSCRIPT_PUSH(&frame.arguments, CHARP, luaContext->lastError);
ok = mScriptInvoke(&error, &frame);
mScriptFrameDeinit(&frame);
}
if (!ok) {
mLOG(SCRIPT, ERROR, "%s", luaContext->lastError);
}
}
bool _luaLoad(struct mScriptEngineContext* ctx, const char* filename, struct VFile* vf) { bool _luaLoad(struct mScriptEngineContext* ctx, const char* filename, struct VFile* vf) {
struct mScriptEngineContextLua* luaContext = (struct mScriptEngineContextLua*) ctx; struct mScriptEngineContextLua* luaContext = (struct mScriptEngineContextLua*) ctx;
struct mScriptEngineLuaReader data = { struct mScriptEngineLuaReader data = {
@ -861,6 +885,7 @@ bool _luaLoad(struct mScriptEngineContext* ctx, const char* filename, struct VFi
case LUA_ERRSYNTAX: case LUA_ERRSYNTAX:
luaContext->lastError = strdup(lua_tostring(luaContext->lua, -1)); luaContext->lastError = strdup(lua_tostring(luaContext->lua, -1));
lua_pop(luaContext->lua, 1); lua_pop(luaContext->lua, 1);
_luaError(luaContext);
break; break;
default: default:
break; break;
@ -967,6 +992,8 @@ bool _luaInvoke(struct mScriptEngineContextLua* luaContext, struct mScriptFrame*
if (ret == LUA_ERRRUN) { if (ret == LUA_ERRRUN) {
luaContext->lastError = strdup(lua_tostring(luaContext->lua, -1)); luaContext->lastError = strdup(lua_tostring(luaContext->lua, -1));
lua_pop(luaContext->lua, 1); lua_pop(luaContext->lua, 1);
_luaError(luaContext);
} }
if (ret) { if (ret) {
return false; return false;