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

SCRIPT: Get the methods working, meta methods not working for now...

parent 519ddcd9
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!9Lua cpp expose
......@@ -26,11 +26,11 @@ SampleObject::foo(lua_State *L) noexcept
LuaMethodTable
SampleObject::getMetaMethods() noexcept
{
return { LUA_DECL_METHOD(SampleObject, foo) };
return { luaRegDefaultGC, LUA_DECL_CREATE(SampleObject) };
}
LuaMethodTable
SampleObject::getMethods() noexcept
{
return { luaRegDefaultGC };
return { LUA_DECL_METHOD(SampleObject, foo) };
}
......@@ -4,11 +4,13 @@
#include "lua.hpp"
#include <vector>
#include <cstdio>
namespace Vivy::Script
{
// clang-format off
#define LUA_DECL_METHOD(class, name) luaL_Reg{ #name, class ::name }
#define LUA_DECL_CREATE(class) luaL_Reg{ "new", class ::CREATE }
#define LUA_DECL_SCRIPTABLE_CLASS(class) public CRTPLuaScriptObject<class>
#define LUA_SCRIPTABLE_CLASS(class) \
......@@ -27,6 +29,7 @@ protected:
static int GC(lua_State *L) noexcept
{
const Object *obj = reinterpret_cast<const Object *>(lua_topointer(L, 1));
puts("CALL DESTRUCTOR");
obj->~Object();
return 0;
}
......@@ -34,6 +37,7 @@ protected:
static int CREATE(lua_State *L) noexcept
{
Object *obj = reinterpret_cast<Object *>(lua_newuserdata(L, sizeof(Object)));
puts("CALL CONSTRUCTOR");
new (obj) Object;
luaL_getmetatable(L, Object::className);
lua_setmetatable(L, -2);
......@@ -44,8 +48,10 @@ protected:
{
luaL_checktype(L, index, LUA_TUSERDATA);
Object *obj = reinterpret_cast<Object *>(luaL_checkudata(L, index, Object::className));
if (obj == nullptr)
if (obj == nullptr) {
puts("TYPE ERROR");
luaL_typeerror(L, index, Object::className);
}
return obj;
}
......@@ -72,6 +78,7 @@ public:
lua_settable(L, metatable);
for (const luaL_Reg meta : Object::getMetaMethods()) {
fprintf(stderr, "REGISTER %s\n", meta.name);
lua_pushstring(L, meta.name);
lua_pushcfunction(L, meta.func);
lua_settable(L, metatable);
......@@ -81,7 +88,8 @@ public:
}
// Fill the method table with specified methods
for (const luaL_Reg method : Object::getMetaMethods()) {
for (const luaL_Reg method : Object::getMethods()) {
fprintf(stderr, "REGISTER %s\n", method.name);
lua_pushstring(L, method.name);
lua_pushcfunction(L, method.func);
lua_settable(L, methodtable);
......
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