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

SCRIPT: Clear the _ENV var and set it to the constructed vivy table to have a...

SCRIPT: Clear the _ENV var and set it to the constructed vivy table to have a clean env for the user

The things are loaded in order
- The libVivy (rsc/lua/lib.lua)
- The C++ Objects
- Env swap
- Any user script
parent 74b12bc7
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!9Lua cpp expose
...@@ -17,18 +17,14 @@ LuaContext::LuaContext() noexcept ...@@ -17,18 +17,14 @@ LuaContext::LuaContext() noexcept
if (!libVivy.open(QIODevice::ReadOnly | QIODevice::Text)) if (!libVivy.open(QIODevice::ReadOnly | QIODevice::Text))
qFatal("Failed to find packaged libVivy (:lia/lib.lua)"); qFatal("Failed to find packaged libVivy (:lia/lib.lua)");
SampleObject::Register(L); loadString(libVivy.readAll().toStdString().c_str()); // Load the lib
loadString(libVivy.readAll().toStdString().c_str()); SampleObject::Register(L); // Load the C++ objects
swapEnv(); // Swap the _ENV
} }
LuaContext::~LuaContext() noexcept LuaContext::~LuaContext() noexcept
{ {
} lua_close(L);
lua_State *
LuaContext::getState() const noexcept
{
return L;
} }
int int
......
...@@ -13,7 +13,6 @@ public: ...@@ -13,7 +13,6 @@ public:
LuaContext() noexcept; LuaContext() noexcept;
~LuaContext() noexcept; ~LuaContext() noexcept;
lua_State *getState() const noexcept;
int loadFile(const char *) noexcept; int loadFile(const char *) noexcept;
int loadString(const char *) noexcept; int loadString(const char *) noexcept;
...@@ -21,11 +20,18 @@ public: ...@@ -21,11 +20,18 @@ public:
operator lua_State *() noexcept operator lua_State *() noexcept
{ {
return getState(); return L;
} }
private: private:
// Exec all loaded strings and files
int exec() noexcept; int exec() noexcept;
// Swap the env, needed before executing a user script!
void swapEnv() noexcept
{
loadString("_ENV = vivy\n");
}
}; };
} }
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
or duration, etc. or duration, etc.
--]] --]]
local global = vivy:global() local global = Vivy:global()
--[[ --[[
Get other script, here the util script. You can pass an author in case of Get other script, here the util script. You can pass an author in case of
...@@ -19,10 +19,10 @@ local global = vivy:global() ...@@ -19,10 +19,10 @@ local global = vivy:global()
not unique, you will get an error on register time. not unique, you will get an error on register time.
--]] --]]
local utils = vivy:getScript{script = "utils", author = "std"} local utils = Vivy:getScript{script = "utils", author = "std"}
-- Create a new script. -- Create a new script.
local script = vivy:newScript{ local script = Vivy:newScript{
name = "sample_script", name = "sample_script",
author = "Vivy", author = "Vivy",
revision = "rev-1254", revision = "rev-1254",
...@@ -30,7 +30,7 @@ local script = vivy:newScript{ ...@@ -30,7 +30,7 @@ local script = vivy:newScript{
option1 = false, option1 = false,
preTime = -900, preTime = -900,
postTime = 300, postTime = 300,
color1 = vivy:newColor("#314159") color1 = Vivy:newColor("#314159")
}, },
description = [[ description = [[
Sample script use for the specifications. Sample script use for the specifications.
...@@ -48,9 +48,9 @@ end ...@@ -48,9 +48,9 @@ end
document defined options. document defined options.
--]] --]]
local setRetime = vivy:newJob{ local setRetime = Vivy:newJob {
name = "retime_lines", name = "retime_lines",
iterate = Iterable.LINE, iterate = Vivy.Iterable.LINE,
callback = function (opt, line) callback = function (opt, line)
line.start += opt.preTime line.start += opt.preTime
line.finish += opt.postTime line.finish += opt.postTime
...@@ -58,9 +58,9 @@ local setRetime = vivy:newJob{ ...@@ -58,9 +58,9 @@ local setRetime = vivy:newJob{
end end
} }
local demultiply_syllabes = vivy:newJob{ local demultiply_syllabes = Vivy:newJob {
name = "create_syllabes", name = "create_syllabes",
iterate = Iterable.SYLLABE, iterate = Vivy.Iterable.SYLLABE,
callback = function (opt, syl) callback = function (opt, syl)
local newSyls = tripleCopySyl(syl) local newSyls = tripleCopySyl(syl)
...@@ -77,11 +77,11 @@ local demultiply_syllabes = vivy:newJob{ ...@@ -77,11 +77,11 @@ local demultiply_syllabes = vivy:newJob{
} }
-- Register the callbacks. Will be called in that order -- Register the callbacks. Will be called in that order
script:addJobs({ script:addJobs {
set_retime, -- Retime lines set_retime, -- Retime lines
demultiply_syllabes, -- Aegisub's syllabe demultiply_syllabes, -- Aegisub's syllabe
utils:getJob("color_after_read_3") -- 3-patern syls, apply color{1,2} utils:getJob("color_after_read_3") -- 3-patern syls, apply color{1,2}
}) }
-- Register the script. -- Register the script.
vivy:registerScript(script) Vivy:registerScript script
...@@ -30,6 +30,37 @@ Iterable = enum { ...@@ -30,6 +30,37 @@ Iterable = enum {
"SYLLABE" "SYLLABE"
} }
-- Table: reservedKeywords["keyword"] = "type"
local reservedKeywords = {}
-- Vivy global object construction
vivy = {}
vivy.math = math
vivy.Iterable = Iterable
vivy.Vivy = vivy
vivy.Version = setmetatable({
["MAJOR"] = 0,
["MINOR"] = 1,
["PATCH"] = 0,
["LABEL"] = "alpha",
}, {
__tostring = function ()
return string.format("%d.%d.%d-%s",
vivy.Version.MAJOR,
vivy.Version.MINOR,
vivy.Version.PATCH,
vivy.Version.LABEL)
end,
__newindex = function (t, k, v)
if reservedKeywords[k] ~= nil then
error(k .. " is a reserved word")
else
rawset(t, k, v)
end
end
})
-- Hide the io, os and debug libraries -- Hide the io, os and debug libraries
io = nil io = nil
os = nil os = nil
......
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