Advertisement
Guest User

Untitled

a guest
May 16th, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. #include <cstdio>
  2. #include <lua.hpp>
  3. #include <cerrno>
  4. #include <cstring>
  5. #include <stdlib.h>
  6.  
  7. #include <new>
  8.  
  9. void lua_stack_dump(lua_State *L);
  10.  
  11. #define _strify(str) #str
  12. #define _concat(str1, str2) str1 ## str2
  13.  
  14. #define register_class(state, class_name, meta_table, class_table) do { \
  15.     luaL_newmetatable(state, #class_name); \
  16.     lua_pushvalue(state, -1); \
  17.     lua_setfield(state, -2, "__index"); \
  18.     luaL_setfuncs(state, class_table, 0); \
  19.     luaL_newlib(state, meta_table); \
  20.     /* lua_pushvalue(state, -1);*/ \
  21.     lua_setglobal(state, #class_name); \
  22.     lua_stack_dump(state); \
  23. } while(0)
  24.  
  25. #define create_lua_object(state, class_name, obj) do { \
  26.     void *udata__ = lua_newuserdata(state, sizeof(_concat(Lua,class_name))); \
  27.     _concat(Lua,class_name) *obj__ = ::new (udata__) _concat(Lua,class_name)(state, obj); \
  28.     \
  29.     luaL_getmetatable(state, #class_name); \
  30.     lua_setmetatable(state, -2); \
  31.     lua_stack_dump(state); \
  32. } while(0)
  33.  
  34. #define get_lua_object(state, class_name) *(_concat(Lua,class_name) **)luaL_checkudata(state, 1, #class_name)
  35.  
  36. #define destroy_lua_object(state, class_name) do { \
  37.     _concat(Lua, class_name) *udata__ = *(_concat(Lua, class_name) **)luaL_checkudata(state, 1, #class_name); \
  38.     udata__->~_concat(Lua, class_name)(); \
  39. } while(0)
  40.  
  41. class Test
  42. {
  43.     public:
  44.         Test() { printf("new Test!\n"); }
  45.         ~Test() { printf("delete Test!\n"); }
  46.        
  47.         void it() { printf("TEST IT!\n"); }
  48. };
  49.  
  50. class LuaTest
  51. {
  52.     public:
  53.         LuaTest(lua_State *, Test *test) : test(test) { printf("new LuaTest!\n"); }
  54.         ~LuaTest() { printf("delete LuaTest!\n"); }
  55.         void it() { test->it(); }
  56.     private:
  57.         Test *test;
  58. };
  59.  
  60. static int Test_destroy(lua_State *state)
  61. {
  62.     printf("Test_destroy\n");
  63.     destroy_lua_object(state, Test);
  64.     return 0;
  65. }
  66.  
  67. static int Test_it(lua_State *state)
  68. {
  69.     printf("Test_it\n");
  70.     LuaTest *test = get_lua_object(state, Test);
  71.     test->it();
  72.     return 0;
  73. }
  74.  
  75. static int Test_new(lua_State *state)
  76. {
  77.     printf("Test_new begin\n");
  78.     Test *newTest = new Test();
  79.     create_lua_object(state, Test, newTest);
  80.     printf("Test_new end\n");
  81.     return 1;
  82. }
  83.  
  84. static const luaL_Reg test_meta_methods[] = {
  85.     { "new", &Test_new },
  86.     { "__gc", &Test_destroy },
  87.     { 0, 0 }
  88. };
  89.  
  90. static const luaL_Reg test_methods[] = {
  91.     { "it", &Test_it },
  92.     { 0, 0 }
  93. };
  94.  
  95. int main(int argc, char **argv)
  96. {
  97.     printf("start\n");
  98.     lua_State *state = luaL_newstate();
  99.     luaL_openlibs(state);
  100.    
  101.     printf("call register_class\n");
  102.     register_class(state, Test, test_meta_methods, test_methods);
  103.    
  104.     printf("open lua file\n");
  105.     FILE *fh = fopen("luaobjtest.lua", "r");
  106.     if(!fh)
  107.     {
  108.         printf("failed to open luaobjtest.lua: %s\n", strerror(errno));
  109.         return 0;
  110.     }
  111.    
  112.     fseek(fh, 0, SEEK_END);
  113.     long len = ftell(fh);
  114.     fseek(fh, 0, SEEK_SET);
  115.    
  116.     char *script = (char *)malloc(len+1);
  117.     if(!script)
  118.     {
  119.         printf("failed to allocate enough memory to load luaobjtest.lua\n");
  120.         return 0;
  121.     }
  122.    
  123.     script[len] = 0;
  124.     size_t ret = fread(script, 1, len, fh);
  125.     if(ret != (size_t)len)
  126.     {
  127.         printf("failed to read entire luaobjtest.lua file, read %li bytes, expected %li bytes.\n", ret, len);
  128.         return 0;
  129.     }
  130.    
  131.     fclose(fh);
  132.  
  133.     printf("call luaL_loadstring\n");
  134.     if(luaL_loadstring(state, script) != LUA_OK)
  135.     {
  136.         const char *errstr = luaL_checklstring(state, 1, 0);
  137.         printf("failed to load luaobjtest.lua: %s\n", errstr);
  138.         free(script);
  139.         return 0;
  140.     }
  141.  
  142.     printf("call lua_pcall\n");
  143.     if(lua_pcall(state, 0, LUA_MULTRET, 0) != LUA_OK)
  144.     {
  145.         const char *errstr = luaL_checklstring(state, 1, 0);
  146.         printf("failed to run luaobjtest.lua: %s\n", errstr);
  147.         free(script);
  148.         return 0;
  149.     }
  150.    
  151.     printf("done!\n");
  152.    
  153.     return 0;
  154. }
  155.  
  156. void lua_stack_dump(lua_State *L)
  157. {
  158.     int i;
  159.     int top = lua_gettop(L);
  160.     for (i = 1; i <= top; i++) {  /* repeat for each level */
  161.         int t = lua_type(L, i);
  162.         switch (t) {
  163.  
  164.             case LUA_TSTRING:  /* strings */
  165.             printf("`%s'", lua_tostring(L, i));
  166.             break;
  167.  
  168.             case LUA_TBOOLEAN:  /* booleans */
  169.             printf(lua_toboolean(L, i) ? "true" : "false");
  170.             break;
  171.  
  172.             case LUA_TNUMBER:  /* numbers */
  173.             printf("%g", lua_tonumber(L, i));
  174.             break;
  175.  
  176.             default:  /* other values */
  177.             printf("%s", lua_typename(L, t));
  178.             break;
  179.  
  180.         }
  181.         printf("  ");  /* put a separator */
  182.     }
  183.     printf("\n");  /* end the listing */
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement