Pushok icon

Lua stack v2

Pushok | PRO | 06/06/11 08:37:30 AM UTC | 0 ⭐ | 628 👁️ | Never ⏰ | []
C++ |

1.88 KB

|

None

|

0 👍

/

0 👎

#include "windows.h"
 
extern "C" {
#include "math.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "lua/lua.hpp"
}
 
extern "C" {
    int __declspec(dllexport) luaopen_luamodule (lua_State *L);
}
 
enum STYPE{LUADOUBLE, LUASTRING};
 
#include "lstack.h"
 
static int Stack_Create(lua_State* Lua)
{
    int SType = lua_tonumber(Lua, 1);
    lua_newuserdata(Lua, sizeof(CStack<void*>));
    reinterpret_cast<CStack<void*>*>(lua_touserdata(Lua, 2))->Init(SType);
    return 1;
}
 
static int Stack_Push(lua_State* Lua)
{
    CStack<void*> *LuaStack = reinterpret_cast<CStack<void*>*>(lua_touserdata(Lua, 1));
 
    switch(LuaStack->GetStackType())
    {
    case STYPE::LUADOUBLE:
        {
            reinterpret_cast<CStack<double>*>(LuaStack)->push(lua_tonumber(Lua, 2));
            break;
        }
    case STYPE::LUASTRING:
        {
            reinterpret_cast<CStack<const char*>*>(LuaStack)->push(lua_tostring(Lua, 2));
            break;
        }
    }
    return 0;
}
 
static int Stack_Pop(lua_State* Lua)
{
    CStack<void*> *LuaStack = reinterpret_cast<CStack<void*>*>(lua_touserdata(Lua, 1));
    switch(LuaStack->GetStackType())
    {
    case STYPE::LUADOUBLE:
        {
            lua_pushnumber(Lua, reinterpret_cast<CStack<LUA_NUMBER>*>(LuaStack)->pop());
            break;
        }
    case STYPE::LUASTRING:
        {
            lua_pushstring(Lua, reinterpret_cast<CStack<const char*>*>(LuaStack)->pop());
            break;
        }
    }
    return 1;
}
 
static int Stack_SwitchType(lua_State* Lua)
{
    lua_pushnumber(Lua, !strcmp(lua_tostring(Lua, -1), "string") ? STYPE::LUASTRING : STYPE::LUADOUBLE);
    return 1;
}
 
static const struct luaL_reg arraylib [] = {
    {"Push", Stack_Push},
    {"Pop", Stack_Pop},
    {"New", Stack_Create},
    {"GetType", Stack_SwitchType},
    {NULL, NULL}
};
 
int luaopen_array (lua_State *L) {
    luaL_openlib(L, "LuaStack", arraylib, 0);
    return 1;
}
 
int __declspec(dllexport) luaopen_luamodule (lua_State *L) {
    luaopen_array(L);
    return 1;
};

Comments