Scripting: Pass filenames down to scripting engines

This commit is contained in:
Vicki Pfau 2022-05-22 22:29:13 -07:00
parent 5d7e3bdf13
commit b3476a997a
7 changed files with 37 additions and 14 deletions

View File

@ -49,7 +49,7 @@ struct mScriptEngineContext {
bool (*setGlobal)(struct mScriptEngineContext*, const char* name, struct mScriptValue*);
struct mScriptValue* (*getGlobal)(struct mScriptEngineContext*, const char* name);
bool (*load)(struct mScriptEngineContext*, struct VFile*);
bool (*load)(struct mScriptEngineContext*, const char* filename, struct VFile*);
bool (*run)(struct mScriptEngineContext*);
const char* (*getError)(struct mScriptEngineContext*);
};

View File

@ -69,7 +69,7 @@ static const uint8_t _fakeGBROM[0x4000] = {
#define LOAD_PROGRAM(PROG) \
do { \
struct VFile* vf = VFileFromConstMemory(PROG, strlen(PROG)); \
assert_true(lua->load(lua, vf)); \
assert_true(lua->load(lua, NULL, vf)); \
vf->close(vf); \
} while(0)

View File

@ -71,15 +71,16 @@ void ScriptingController::setController(std::shared_ptr<CoreController> controll
bool ScriptingController::loadFile(const QString& path) {
VFileDevice vf(path, QIODevice::ReadOnly);
return load(vf);
return load(vf, path);
}
bool ScriptingController::load(VFileDevice& vf) {
bool ScriptingController::load(VFileDevice& vf, const QString& name) {
if (!m_activeEngine) {
return false;
}
QByteArray utf8 = name.toUtf8();
CoreController::Interrupter interrupter(m_controller);
if (!m_activeEngine->load(m_activeEngine, 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)));
return false;
}
@ -100,7 +101,7 @@ void ScriptingController::clearController() {
void ScriptingController::runCode(const QString& code) {
VFileDevice vf(code.toUtf8());
load(vf);
load(vf, "*prompt");
}
mScriptTextBuffer* ScriptingController::createTextBuffer(void* context) {

View File

@ -30,7 +30,7 @@ public:
void setController(std::shared_ptr<CoreController> controller);
bool loadFile(const QString& path);
bool load(VFileDevice& vf);
bool load(VFileDevice& vf, const QString& name);
mScriptContext* context() { return &m_scriptContext; }

View File

@ -247,7 +247,7 @@ bool mScriptContextLoadVF(struct mScriptContext* context, const char* name, stru
if (!info.context) {
return false;
}
return info.context->load(info.context, vf);
return info.context->load(info.context, name, vf);
}
bool mScriptContextLoadFile(struct mScriptContext* context, const char* path) {

View File

@ -19,7 +19,7 @@ static void _luaDestroy(struct mScriptEngineContext*);
static bool _luaIsScript(struct mScriptEngineContext*, const char*, struct VFile*);
static struct mScriptValue* _luaGetGlobal(struct mScriptEngineContext*, const char* name);
static bool _luaSetGlobal(struct mScriptEngineContext*, const char* name, struct mScriptValue*);
static bool _luaLoad(struct mScriptEngineContext*, struct VFile*);
static bool _luaLoad(struct mScriptEngineContext*, const char*, struct VFile*);
static bool _luaRun(struct mScriptEngineContext*);
static const char* _luaGetError(struct mScriptEngineContext*);
@ -382,7 +382,7 @@ static const char* _reader(lua_State* lua, void* context, size_t* size) {
return reader->block;
}
bool _luaLoad(struct mScriptEngineContext* ctx, struct VFile* vf) {
bool _luaLoad(struct mScriptEngineContext* ctx, const char* filename, struct VFile* vf) {
struct mScriptEngineContextLua* luaContext = (struct mScriptEngineContextLua*) ctx;
struct mScriptEngineLuaReader data = {
.vf = vf
@ -391,7 +391,29 @@ bool _luaLoad(struct mScriptEngineContext* ctx, struct VFile* vf) {
free(luaContext->lastError);
luaContext->lastError = NULL;
}
int ret = lua_load(luaContext->lua, _reader, &data, NULL, "t");
char name[80];
if (filename) {
if (*filename == '*') {
snprintf(name, sizeof(name), "=%s", filename + 1);
} else {
const char* lastSlash = strrchr(filename, '/');
const char* lastBackslash = strrchr(filename, '\\');
if (lastSlash && lastBackslash) {
if (lastSlash > lastBackslash) {
filename = lastSlash + 1;
} else {
filename = lastBackslash + 1;
}
} else if (lastSlash) {
filename = lastSlash + 1;
} else if (lastBackslash) {
filename = lastBackslash + 1;
}
snprintf(name, sizeof(name), "@%s", filename);
}
filename = name;
}
int ret = lua_load(luaContext->lua, _reader, &data, filename, "t");
switch (ret) {
case LUA_OK:
luaContext->func = luaL_ref(luaContext->lua, LUA_REGISTRYINDEX);

View File

@ -16,7 +16,7 @@
#define LOAD_PROGRAM(PROG) \
do { \
struct VFile* vf = VFileFromConstMemory(PROG, strlen(PROG)); \
assert_true(lua->load(lua, vf)); \
assert_true(lua->load(lua, NULL, vf)); \
vf->close(vf); \
} while(0)
@ -119,7 +119,7 @@ M_TEST_DEFINE(loadGood) {
const char* program = "-- test\n";
struct VFile* vf = VFileFromConstMemory(program, strlen(program));
assert_true(lua->load(lua, vf));
assert_true(lua->load(lua, NULL, vf));
lua->destroy(lua);
mScriptContextDeinit(&context);
@ -133,7 +133,7 @@ M_TEST_DEFINE(loadBadSyntax) {
const char* program = "Invalid syntax! )\n";
struct VFile* vf = VFileFromConstMemory(program, strlen(program));
assert_false(lua->load(lua, vf));
assert_false(lua->load(lua, NULL, vf));
lua->destroy(lua);
mScriptContextDeinit(&context);