Pushok

Lua stack v2

Jun 6th, 2011
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include "windows.h"
  2.  
  3. extern "C" {
  4. #include "math.h"
  5. #include "string.h"
  6. #include "stdio.h"
  7. #include "stdlib.h"
  8. #include "lua/lua.hpp"
  9. }
  10.  
  11. extern "C" {
  12.     int __declspec(dllexport) luaopen_luamodule (lua_State *L);
  13. }
  14.  
  15. enum STYPE{LUADOUBLE, LUASTRING};
  16.  
  17. #include "lstack.h"
  18.  
  19. static int Stack_Create(lua_State* Lua)
  20. {
  21.     int SType = lua_tonumber(Lua, 1);
  22.     lua_newuserdata(Lua, sizeof(CStack<void*>));
  23.     reinterpret_cast<CStack<void*>*>(lua_touserdata(Lua, 2))->Init(SType);
  24.     return 1;
  25. }
  26.  
  27. static int Stack_Push(lua_State* Lua)
  28. {
  29.     CStack<void*> *LuaStack = reinterpret_cast<CStack<void*>*>(lua_touserdata(Lua, 1));
  30.  
  31.     switch(LuaStack->GetStackType())
  32.     {
  33.     case STYPE::LUADOUBLE:
  34.         {
  35.             reinterpret_cast<CStack<double>*>(LuaStack)->push(lua_tonumber(Lua, 2));
  36.             break;
  37.         }
  38.     case STYPE::LUASTRING:
  39.         {
  40.             reinterpret_cast<CStack<const char*>*>(LuaStack)->push(lua_tostring(Lua, 2));
  41.             break;
  42.         }
  43.     }
  44.     return 0;
  45. }
  46.  
  47. static int Stack_Pop(lua_State* Lua)
  48. {
  49.     CStack<void*> *LuaStack = reinterpret_cast<CStack<void*>*>(lua_touserdata(Lua, 1));
  50.     switch(LuaStack->GetStackType())
  51.     {
  52.     case STYPE::LUADOUBLE:
  53.         {
  54.             lua_pushnumber(Lua, reinterpret_cast<CStack<LUA_NUMBER>*>(LuaStack)->pop());
  55.             break;
  56.         }
  57.     case STYPE::LUASTRING:
  58.         {
  59.             lua_pushstring(Lua, reinterpret_cast<CStack<const char*>*>(LuaStack)->pop());
  60.             break;
  61.         }
  62.     }
  63.     return 1;
  64. }
  65.  
  66. static int Stack_SwitchType(lua_State* Lua)
  67. {
  68.     lua_pushnumber(Lua, !strcmp(lua_tostring(Lua, -1), "string") ? STYPE::LUASTRING : STYPE::LUADOUBLE);
  69.     return 1;
  70. }
  71.  
  72. static const struct luaL_reg arraylib [] = {
  73.     {"Push", Stack_Push},
  74.     {"Pop", Stack_Pop},
  75.     {"New", Stack_Create},
  76.     {"GetType", Stack_SwitchType},
  77.     {NULL, NULL}
  78. };
  79.  
  80. int luaopen_array (lua_State *L) {
  81.     luaL_openlib(L, "LuaStack", arraylib, 0);
  82.     return 1;
  83. }
  84.  
  85. int __declspec(dllexport) luaopen_luamodule (lua_State *L) {
  86.     luaopen_array(L);
  87.     return 1;
  88. };
Advertisement
Add Comment
Please, Sign In to add comment