diff --git a/include/ReScript/luaengine.hpp b/include/ReScript/luaengine.hpp index cf7d485..be1a09a 100644 --- a/include/ReScript/luaengine.hpp +++ b/include/ReScript/luaengine.hpp @@ -1,4 +1,9 @@ #include +#include +#include +#include +#include +#include "jlog/jlog.hpp" #pragma once @@ -24,15 +29,113 @@ public: void Pull(lua_State *l, int index) override { value = luaL_checknumber(l, index); }; }; +//template +class LuaCFunction : public LuaValue { + const char *name; +public: + LuaCFunction() : Base() {}; + LuaCFunction(lua_CFunction lcf) : LuaValue (lcf) {}; + + // This is for programmers not the lua engine + // The lua engine can fuck off + // Also you need to make sure to set it + const char* Name() { return name; }; + void Name(const char* n) { this->name = n; }; + + void Push(lua_State *l) { lua_pushcfunction(l, value); lua_setglobal(l, name); }; + + // I don't know how to check if we're pulling a valid cfunction back in yet. + // I assume if you're using this you're either a dumb ass or have a damn good reason to. + void Pull(lua_State *l, int index) { value = lua_tocfunction(l, index); }; +}; + +class LuaThread : public LuaValue +{ +public: + LuaThread() : Base() {}; + LuaThread(lua_State* value) : Base(value) {}; + void Push(lua_State *l) override { /* IDK */ }; + void Pull(lua_State *l, int index) override { /* IDK */ }; +}; + +class LuaBoolean : public LuaValue +{ +public: + LuaBoolean() : Base() {}; + LuaBoolean(bool value) : Base(value) {}; + void Push(lua_State *l) override { lua_pushboolean(l, value); }; + void Pull(lua_State *l, int index) override { luaL_checktype(l, index, LUA_TBOOLEAN); } +}; + + + +template +class LuaReturnTuple : public std::tuple +{ +public: + typedef std::tuple Base; + +}; + + +//using LuaAny = std::variant; +using LuaAny = std::variant; + +/* +class LuaTable : public LuaValue> { +public: + using MapBase = std::map; + using ImplBase = LuaValue; + + LuaTable() : ImplBase () {}; + LuaTable(MapBase t) : ImplBase(t) {}; + + void Push(lua_State *l) { + for (auto const& [key, val] : this->value) { + DEBUG(std::format("K {} V {}", std::get(key), val)); + } + }; + void Pull(lua_State *l, int index) {}; + + template + T Get(K key) const; +}; + */ + +// I fucking hate this SHIT +class LuaTable : public LuaValue> { +public: + using MapBase = std::map; + using ImplBase = LuaValue; + + LuaTable() : ImplBase () {}; + LuaTable(MapBase t) : ImplBase(t) {}; + + void Push(lua_State *l) { + for (auto const& [key, val] : this->value) { + //DEBUG(std::format("K {} V {}", std::get(key), val)); + std::visit([](auto&& arg){ DEBUG(std::format("K {}", arg.Value())); }, key); + std::visit([](auto&& arg){ DEBUG(std::format("V {}", arg.Value())); }, val); + } + }; + void Pull(lua_State *l, int index) {}; + + template + T Get(K key) const; +}; + class LuaEngine { public: lua_State *L; LuaEngine() { L = luaL_newstate(); luaL_openlibs(L); }; + ~LuaEngine() { lua_close(L); }; // Don't assume this is totally correct. The idea is upon deallocation we close the lua state. template void Push(Val lv) { lv.Push(L); }; template Val Pull(int index) { Val v; v.Pull(L, index); return v; }; bool LoadString(const char *str) { return luaL_loadstring(L, str); }; - + int Pcall(int nargs, int nresults, int msgh) { return lua_pcall(L, nargs, nresults, msgh); }; + const char *ToString(int index) { return lua_tostring(L, index); }; + void Pop(int n) { return lua_pop(L, n); }; }; diff --git a/main.cpp b/main.cpp index 544a01c..344b2e1 100644 --- a/main.cpp +++ b/main.cpp @@ -18,6 +18,8 @@ static int l_sin (lua_State *L) { return 1; } + + int main() { char buff[256]; int error; @@ -26,17 +28,28 @@ int main() { LuaEngine LE = LuaEngine(); - lua_pushcfunction(LE.L, l_sin); - lua_setglobal(LE.L, "mysin"); + //lua_pushcfunction(LE.L, l_sin); + //lua_setglobal(LE.L, "mysin"); + LuaCFunction lcf = LuaCFunction(&l_sin); + lcf.Name("mysin"); + LE.Push(lcf); - while(fgets(buff, sizeof(buff), stdin) != NULL) { - error = LE.LoadString(buff) || lua_pcall(LE.L, 0, 0, 0); - if (error) { - fprintf(stderr, "%s\n", lua_tostring(LE.L, -1)); - lua_pop(LE.L, 1); - } + //std::map m; + //m.emplace(LuaNumber(1), LuaNumber(20)); + //if (LuaNumber(1) == LuaNumber(2)) { + // exit(1); + //} + //LuaTable lt = m; + //LE.Push(lt); + + while(fgets(buff, sizeof(buff), stdin) != NULL) { + error = LE.LoadString(buff) || LE.Pcall(0,0,0); + if (error) { + fprintf(stderr, "%s\n", LE.ToString(-1)); + LE.Pop(1); } + } - lua_close(LE.L); + //lua_close(LE.L); return 0; }