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

SCRIPT: Add free functions and an example with a custom "print"

parent 03dba5b4
Aucune branche associée trouvée
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
......@@ -24,6 +24,11 @@ function enum(tbl)
return tbl
end
-- Check if a table contains a key
function tableHasKey(tbl, key)
return tbl[key] ~= nil
end
-- Things used to iterate over with a job
Iterable = enum {
"LINE",
......@@ -42,6 +47,7 @@ Vivy = {
["string"] = string,
["utf8"] = utf8,
["table"] = table,
["print"] = print,
-- Custom things
["Iterable"] = Iterable,
......@@ -73,18 +79,14 @@ Vivy = setmetatable(Vivy, {
end
})
-- Hide the io, os and debug libraries
io = nil
os = nil
debug = nil
-- Hide everything from the user, appart some specific things he can do.
local _ENV = setmetatable(Vivy, {
__newindex = function (t, k, v)
-- Use the ReservedKeywords table here
if (k ~= "Vivy") then
rawset(t, k, v)
if (tableHasKey(ReservedKeywords, k)) then
error("The keyword " .. k .. " is protected!")
else
error("Try to assign something to the protected global symbol 'Vivy'")
rawset(t, k, v)
end
end
})
......@@ -76,7 +76,7 @@ public:
lua_settable(L, metatable);
for (const luaL_Reg &meta : Object::metaMethods) {
fprintf(stderr, "REGISTER %s\n", meta.name);
fprintf(stderr, "LUA: REGISTER %s\n", meta.name);
lua_pushstring(L, meta.name);
lua_pushcfunction(L, meta.func);
lua_settable(L, metatable);
......@@ -87,7 +87,7 @@ public:
// Fill the method table with specified methods
for (const luaL_Reg &method : Object::methods) {
fprintf(stderr, "REGISTER %s\n", method.name);
fprintf(stderr, "LUA: REGISTER %s\n", method.name);
lua_pushstring(L, method.name);
lua_pushcfunction(L, method.func);
lua_settable(L, methodtable);
......
#include "CRTPLuaScriptObject.hh"
#include "ScriptDocument.hh"
#include "LuaContext.hh"
#include "LuaFreeFunctions.hh"
#include "lua.hpp"
#include <cstdio>
#include <QtGlobal>
......@@ -18,8 +19,9 @@ LuaContext::LuaContext() noexcept
if (!libVivy.open(QIODevice::ReadOnly | QIODevice::Text))
qFatal("Failed to find packaged libVivy (:lia/lib.lua)");
loadString(libVivy.readAll().toStdString().c_str()); // Load the lib
FreeFunctions::registerFunctions(L); // Register the free functions
SampleObject::Register(L); // Load the C++ objects
loadString(libVivy.readAll().toStdString().c_str()); // Load the lib
}
LuaContext::~LuaContext() noexcept { lua_close(L); }
......
#include "LuaFreeFunctions.hh"
#include "LuaContext.hh"
#include <initializer_list>
#include <QtGlobal>
using namespace Vivy::Script;
static int
vivyPrint(lua_State *L) noexcept
{
luaL_checktype(L, 1, LUA_TSTRING);
qDebug() << "LUA - MSG:" << lua_tostring(L, 1);
return 0;
}
static inline constexpr std::initializer_list<std::tuple<const char *, int (*)(lua_State *)>>
functionsToRegister = {
{ "print", vivyPrint },
};
void
FreeFunctions::registerFunctions(lua_State *L) noexcept
{
for (const auto &[name, functionPtr] : functionsToRegister) {
lua_register(L, name, functionPtr);
qDebug() << "LUA: Register function" << name;
}
}
#pragma once
#include "../Utils.hh"
struct lua_State;
namespace Vivy::Script::FreeFunctions
{
void registerFunctions(lua_State *) noexcept;
}
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