From b671c07361d5d302e1abb8451c4eb9a0a255f1bf Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Mon, 16 Aug 2021 16:04:40 +0200 Subject: [PATCH] SCRIPT: Add methods for Vivy to get globals and modules --- rsc/lua/lib.lua | 26 ++++++++++++++++++-------- src/Lib/Script/CRTPLuaScriptObject.cc | 27 ++++++++++++++++++++++++++- src/Lib/Script/CRTPLuaScriptObject.hh | 10 ++++++++-- utils/lua/sample-spec.lua | 2 +- 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/rsc/lua/lib.lua b/rsc/lua/lib.lua index a73f1364..b8f400e7 100644 --- a/rsc/lua/lib.lua +++ b/rsc/lua/lib.lua @@ -43,15 +43,14 @@ StdType = enum { "NUMBER", "COLOR", "BOOLEAN", + "STRING", } -- Vivy global object construction -os = nil -io = nil -debug = nil -Vivy = { } -Version = { MAJOR = 0, MINOR = 1, PATCH = 0, LABEL = "alpha" } -Vivy["Fun"] = FreeFunctions() +os, io, debug = nil, nil, nil +Vivy = { } +Version = { MAJOR = 0, MINOR = 1, PATCH = 0, LABEL = "alpha" } +Vivy["___Fun"] = FreeFunctions() Vivy["Version"] = setmetatable(Version, { __tostring = function () @@ -68,7 +67,18 @@ Vivy["Version"] = setmetatable(Version, { end }) --- Standard color creation function +function Vivy:global () return Vivy.___Fun end + +function Vivy:module (tbl) + if type(tbl) == "table" and #tbl == 1 then + return Vivy.___Fun.getModule(tbl[1]) + elseif type(tbl) == "string" then + return Vivy.___Fun.getModule(tbl) + else + error("Invalid argument, you should pass a single string", 2) + end +end + function Vivy:newColor (colorString) -- Not implemented return false @@ -81,4 +91,4 @@ Vivy["Vivy"] = Vivy -- Vivy keyword... setrometatable(Vivy) generalize(Iterable) -print = Vivy.Fun.print +print = Vivy.___Fun.print diff --git a/src/Lib/Script/CRTPLuaScriptObject.cc b/src/Lib/Script/CRTPLuaScriptObject.cc index 87ae0a28..21cf5f0d 100644 --- a/src/Lib/Script/CRTPLuaScriptObject.cc +++ b/src/Lib/Script/CRTPLuaScriptObject.cc @@ -8,13 +8,38 @@ using namespace Vivy::Script; // FreeFunctions int -FreeFunctions::print(lua_State *L) noexcept +FreeFunctions::print(lua_State *const L) noexcept { const std::string_view arg = CHECK_STRING_VIEW(L, 1); out(LuaContext::getContext(L)) << "OUT: " << arg << "\n"; LUA_RETURN_NOTHING(L); } +int +FreeFunctions::getModule(lua_State *const L) noexcept +{ + const auto *const context = LuaContext::getContext(L); + const std::string_view modName = CHECK_STRING_VIEW(L, 1); + [[maybe_unused]] const ModuleDeclaration *const mod = context->getModule(modName); + return 1; +} + +int +FreeFunctions::start(lua_State *const L) noexcept +{ + lua_settop(L, 0); + lua_pushinteger(L, 0); + return 1; +} + +int +FreeFunctions::finish(lua_State *const L) noexcept +{ + lua_settop(L, 0); + lua_pushinteger(L, 0); + return 1; +} + // ModuleDeclaration bool diff --git a/src/Lib/Script/CRTPLuaScriptObject.hh b/src/Lib/Script/CRTPLuaScriptObject.hh index 1c09bc20..c51b8e6a 100644 --- a/src/Lib/Script/CRTPLuaScriptObject.hh +++ b/src/Lib/Script/CRTPLuaScriptObject.hh @@ -271,9 +271,15 @@ public: script_class (FreeFunctions) { LUA_SCRIPTABLE_CLASS(FreeFunctions) - static int print(lua_State *) noexcept; + static int print(lua_State *const) noexcept; + static int getModule(lua_State *const) noexcept; + static int start(lua_State *const) noexcept; + static int finish(lua_State *const) noexcept; method_list metaMethods = { luaRegDefaultGC }; - method_list methods = { LUA_DECL_METHOD(FreeFunctions, print), LUA_DECL_CREATE(FreeFunctions) }; + method_list methods = { LUA_DECL_METHOD(FreeFunctions, getModule), + LUA_DECL_METHOD(FreeFunctions, start), + LUA_DECL_METHOD(FreeFunctions, finish), + LUA_DECL_METHOD(FreeFunctions, print), LUA_DECL_CREATE(FreeFunctions) }; }; } diff --git a/utils/lua/sample-spec.lua b/utils/lua/sample-spec.lua index 824ffef7..21e7dea0 100644 --- a/utils/lua/sample-spec.lua +++ b/utils/lua/sample-spec.lua @@ -14,7 +14,7 @@ module:export { "printFoo", function () prettyPrint ("Bar") end } module:export { "retime_lines", LINE, function (opt, line) line.start = line.start + opt.preTime line.finish = line.finish + opt.postTime - if line.start <= 0 then line.start = 0 end + if line.start <= global:start() then line.start = global:start() end if line.finish >= global:finish() then line.finish = global:finish() end return line end } -- GitLab