Advertisement
denisbaic

создание новой метатаблицы

Jun 19th, 2021 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. int l_getsize(lua_State* L)
  2. {
  3.     auto const grid_ptr = checkgrid(L);
  4.     if (!grid_ptr)
  5.         return 0;
  6.     lua_newtable(L);//size
  7.     lua_pushinteger(L, grid_ptr->Size.x);
  8.     lua_setfield(L, -2, "x");
  9.     lua_pushinteger(L, grid_ptr->Size.y);
  10.     lua_setfield(L, -2, "y");
  11.     return 1;
  12. }
  13. /*эту функцию просто взял с интернета, в 5.1 такой функции нет*/
  14. static void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup) {
  15.     luaL_checkstack(L, nup + 1, "too many upvalues");
  16.     for (; l->name != NULL; l++) {  /* fill the table with given functions */
  17.         int i;
  18.         lua_pushstring(L, l->name);
  19.         for (i = 0; i < nup; i++)  /* copy upvalues to the top */
  20.             lua_pushvalue(L, -(nup + 1));
  21.         lua_pushcclosure(L, l->func, nup);  /* closure with those upvalues */
  22.         lua_settable(L, -(nup + 3));
  23.     }
  24.     lua_pop(L, nup);  /* remove upvalues */
  25. }
  26.  
  27. CORONA_EXPORT int luaopen_plugin_helloplug (lua_State* L)
  28. {
  29.     using namespace Corona;
  30.  
  31.     luaL_newmetatable(L, GRID_META);
  32.     lua_pushvalue(L, -1);
  33.     lua_setfield(L, -2, "__index");
  34.     luaL_Reg arraylib_m[] = {
  35.         {
  36.             "size", l_getsize
  37.         },
  38.         { nullptr, nullptr }
  39.     };
  40.  
  41.     luaL_setfuncs(L, arraylib_m, 0);
  42.    
  43.     lua_newtable(L); // helloplug
  44.    
  45.     luaL_Reg arraylib_f[] = {
  46.         {
  47.             "sin_from_c", l_sin
  48.         },
  49.         {
  50.             "GenerateAndCalculateCorridors", l_GenerateAndCalculateCorridors
  51.         },
  52.         { nullptr, nullptr }
  53.     };
  54.    
  55.     luaL_register(L, nullptr, arraylib_f);
  56.  
  57.     return 1;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement