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

SCRIPT: Setup a custom _ENV for scripts to be executed inside + protect the Vivy table

parent 9c0e7ba6
Branches
Aucune étiquette associée trouvée
1 requête de fusion!9Lua cpp expose
...@@ -19,7 +19,6 @@ LuaContext::LuaContext() noexcept ...@@ -19,7 +19,6 @@ LuaContext::LuaContext() noexcept
loadString(libVivy.readAll().toStdString().c_str()); // Load the lib loadString(libVivy.readAll().toStdString().c_str()); // Load the lib
SampleObject::Register(L); // Load the C++ objects SampleObject::Register(L); // Load the C++ objects
swapEnv(); // Swap the _ENV
} }
LuaContext::~LuaContext() noexcept LuaContext::~LuaContext() noexcept
...@@ -27,6 +26,12 @@ LuaContext::~LuaContext() noexcept ...@@ -27,6 +26,12 @@ LuaContext::~LuaContext() noexcept
lua_close(L); lua_close(L);
} }
void
LuaContext::swapEnv() noexcept
{
lua_setglobal(L, "vivy");
}
int int
LuaContext::exec() noexcept LuaContext::exec() noexcept
{ {
...@@ -40,16 +45,20 @@ LuaContext::exec() noexcept ...@@ -40,16 +45,20 @@ LuaContext::exec() noexcept
int int
LuaContext::loadString(const char *str) noexcept LuaContext::loadString(const char *str) noexcept
{ {
if (luaL_loadstring(L, str) != LUA_OK) if (luaL_loadstring(L, str) != LUA_OK) {
puts("Error loading string");
return LUA_ERRERR; return LUA_ERRERR;
}
return exec(); return exec();
} }
int int
LuaContext::loadFile(const char *file) noexcept LuaContext::loadFile(const char *file) noexcept
{ {
if (luaL_loadfile(L, file) != LUA_OK) if (luaL_loadfile(L, file) != LUA_OK) {
puts("Error loading file");
return LUA_ERRERR; return LUA_ERRERR;
}
return exec(); return exec();
} }
......
...@@ -28,10 +28,7 @@ private: ...@@ -28,10 +28,7 @@ private:
int exec() noexcept; int exec() noexcept;
// Swap the env, needed before executing a user script! // Swap the env, needed before executing a user script!
void swapEnv() noexcept void swapEnv() noexcept;
{
loadString("_ENV = vivy\n");
}
}; };
} }
...@@ -30,34 +30,46 @@ Iterable = enum { ...@@ -30,34 +30,46 @@ Iterable = enum {
"SYLLABE" "SYLLABE"
} }
-- Table: reservedKeywords["keyword"] = "type" -- Table: ReservedKeywords["keyword"] = "type"
local reservedKeywords = {} local ReservedKeywords = {
["Vivy"] = "table"
}
-- Vivy global object construction -- Vivy global object construction
vivy = {} Vivy = {
-- Standard libraries
["math"] = math,
["string"] = string,
["utf8"] = utf8,
["table"] = table,
-- Custom things
["Iterable"] = Iterable,
}
vivy.math = math version = {
vivy.Iterable = Iterable
vivy.Vivy = vivy
vivy.Version = setmetatable({
["MAJOR"] = 0, ["MAJOR"] = 0,
["MINOR"] = 1, ["MINOR"] = 1,
["PATCH"] = 0, ["PATCH"] = 0,
["LABEL"] = "alpha", ["LABEL"] = "alpha",
}, { }
Vivy["Version"] = setmetatable(version, {
__tostring = function () __tostring = function ()
return string.format("%d.%d.%d-%s", return string.format("%d.%d.%d-%s",
vivy.Version.MAJOR, Vivy.Version.MAJOR,
vivy.Version.MINOR, Vivy.Version.MINOR,
vivy.Version.PATCH, Vivy.Version.PATCH,
vivy.Version.LABEL) 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
})
-- In case we are able to do the _ENV <- Vivy one day...
Vivy["Vivy"] = Vivy
Vivy = setmetatable(Vivy, {
__newindex = function (t, k, v)
error("you can't set new keys in the Vivy table")
end end
}) })
...@@ -65,3 +77,14 @@ vivy.Version = setmetatable({ ...@@ -65,3 +77,14 @@ vivy.Version = setmetatable({
io = nil io = nil
os = nil os = nil
debug = nil debug = nil
local _ENV = setmetatable(Vivy, {
__newindex = function (t, k, v)
-- Use the ReservedKeywords table here
if (k ~= "Vivy") then
rawset(t, k, v)
else
error("Try to assign something to the protected global symbol 'Vivy'")
end
end
})
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