Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 8342cd07 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

[WIP] SCRIPT: Add methods to load and export modules and functions

parent b671c073
Branches
Aucune étiquette associée trouvée
1 requête de fusion!16Add a way to execute a Lua file with Vivy in a click-click way
...@@ -57,6 +57,52 @@ ModuleDeclaration::resolvModules(LuaContext *const context) noexcept ...@@ -57,6 +57,52 @@ ModuleDeclaration::resolvModules(LuaContext *const context) noexcept
return true; return true;
} }
int
ModuleDeclaration::importMethod(lua_State *const L) noexcept
{
auto *const self = CHECK(L, 1);
auto *const context = LuaContext::getContext(L);
luaL_checktype(L, 2, LUA_TTABLE);
// Get the module name
lua_pushinteger(L, 1); // Stack is { top: int, table, self }
lua_gettable(L, 2); // Stack is { top: str, table, self }
const std::string_view mod = CHECK_STRING_VIEW(L, 3);
// Import a module
err(context) << "Importing a module...";
const ModuleDeclaration *const module = context->getModule(mod);
if (module) {
err(context) << "Loaded module " << module->moduleName << "\n";
} else {
err(context) << "Failed to load module " << mod << "\n";
return 1;
}
// Stack is { top: module, str, table, self }
// Import a function
if (lua_rawlen(L, 2) == 2) {
lua_pushinteger(L, 2); // Stack is { top: int, module, str, table, self }
lua_gettable(L, 2); // Stack is { top: str, module, str, table, self }
const std::string_view func = CHECK_STRING_VIEW(L, 5);
err(context) << "Load function " << func << " but not implemented, pushing nil\n";
lua_pushnil(L);
// Stack is { top: nil | function, str, module, str, table, self }
}
return 1;
}
int
ModuleDeclaration::exportMethod(lua_State *const L) noexcept
{
auto *const self = CHECK(L, 1);
auto *const context = LuaContext::getContext(L);
const std::string_view object = CHECK_STRING_VIEW(L, 2);
err(context) << "Vivy:export: Not implemented\n";
return 0;
}
int int
ModuleDeclaration::setName(lua_State *const L) noexcept ModuleDeclaration::setName(lua_State *const L) noexcept
{ {
......
...@@ -11,6 +11,7 @@ namespace Vivy::Script ...@@ -11,6 +11,7 @@ namespace Vivy::Script
{ {
// clang-format off // clang-format off
#define LUA_DECL_METHOD(class, name) luaL_Reg{ #name, class ::name } #define LUA_DECL_METHOD(class, name) luaL_Reg{ #name, class ::name }
#define LUA_DECL_NAMED_METHOD(class, method, name) luaL_Reg{ method, class ::name }
#define LUA_DECL_META_METHOD(class, meta, name) luaL_Reg{ meta, class ::name } #define LUA_DECL_META_METHOD(class, meta, name) luaL_Reg{ meta, class ::name }
#define LUA_DECL_CREATE(class) luaL_Reg{ "new", class ::CREATE } #define LUA_DECL_CREATE(class) luaL_Reg{ "new", class ::CREATE }
#define LUA_RETURN_NOTHING(L) { lua_settop(L, 0); return 0; } #define LUA_RETURN_NOTHING(L) { lua_settop(L, 0); return 0; }
...@@ -233,6 +234,9 @@ script_class (ModuleDeclaration) { ...@@ -233,6 +234,9 @@ script_class (ModuleDeclaration) {
static int pushToRuntime(lua_State *const) noexcept; static int pushToRuntime(lua_State *const) noexcept;
static int importMethod(lua_State *const) noexcept;
static int exportMethod(lua_State *const) noexcept;
method_list metaMethods = { luaRegDefaultGC }; method_list metaMethods = { luaRegDefaultGC };
method_list methods = { method_list methods = {
LUA_DECL_METHOD(ModuleDeclaration, setName), LUA_DECL_METHOD(ModuleDeclaration, setName),
...@@ -245,6 +249,8 @@ script_class (ModuleDeclaration) { ...@@ -245,6 +249,8 @@ script_class (ModuleDeclaration) {
LUA_DECL_METHOD(ModuleDeclaration, setFunctions), LUA_DECL_METHOD(ModuleDeclaration, setFunctions),
LUA_DECL_METHOD(ModuleDeclaration, setJobs), LUA_DECL_METHOD(ModuleDeclaration, setJobs),
LUA_DECL_METHOD(ModuleDeclaration, pushToRuntime), LUA_DECL_METHOD(ModuleDeclaration, pushToRuntime),
LUA_DECL_NAMED_METHOD(ModuleDeclaration, "import", importMethod),
LUA_DECL_NAMED_METHOD(ModuleDeclaration, "export", exportMethod),
LUA_DECL_CREATE(ModuleDeclaration), LUA_DECL_CREATE(ModuleDeclaration),
}; };
......
...@@ -168,6 +168,7 @@ LuaContext::getModule(const std::string_view name) const noexcept ...@@ -168,6 +168,7 @@ LuaContext::getModule(const std::string_view name) const noexcept
lua_rawgeti(L, LUA_REGISTRYINDEX, r); lua_rawgeti(L, LUA_REGISTRYINDEX, r);
return ModuleDeclaration::CHECK(L, -1); return ModuleDeclaration::CHECK(L, -1);
} else { } else {
lua_pushnil(L);
return nullptr; return nullptr;
} }
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter