lua-sharetable.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #define LUA_LIB
  2. #include <lua.h>
  3. #include <lauxlib.h>
  4. #include <lualib.h>
  5. #include "lgc.h"
  6. #ifdef makeshared
  7. static void
  8. mark_shared(lua_State *L) {
  9. if (lua_type(L, -1) != LUA_TTABLE) {
  10. luaL_error(L, "Not a table, it's a %s.", lua_typename(L, lua_type(L, -1)));
  11. }
  12. Table * t = (Table *)lua_topointer(L, -1);
  13. if (isshared(t))
  14. return;
  15. makeshared(t);
  16. luaL_checkstack(L, 4, NULL);
  17. if (lua_getmetatable(L, -1)) {
  18. luaL_error(L, "Can't share metatable");
  19. }
  20. lua_pushnil(L);
  21. while (lua_next(L, -2) != 0) {
  22. int i;
  23. for (i=0;i<2;i++) {
  24. int idx = -i-1;
  25. int t = lua_type(L, idx);
  26. switch (t) {
  27. case LUA_TTABLE:
  28. mark_shared(L);
  29. break;
  30. case LUA_TNUMBER:
  31. case LUA_TBOOLEAN:
  32. case LUA_TLIGHTUSERDATA:
  33. break;
  34. case LUA_TFUNCTION:
  35. if (lua_getupvalue(L, idx, 1) != NULL) {
  36. luaL_error(L, "Invalid function with upvalue");
  37. } else if (!lua_iscfunction(L, idx)) {
  38. LClosure *f = (LClosure *)lua_topointer(L, idx);
  39. makeshared(f);
  40. lua_sharefunction(L, idx);
  41. }
  42. break;
  43. case LUA_TSTRING:
  44. lua_sharestring(L, idx);
  45. break;
  46. default:
  47. luaL_error(L, "Invalid type [%s]", lua_typename(L, t));
  48. break;
  49. }
  50. }
  51. lua_pop(L, 1);
  52. }
  53. }
  54. static int
  55. lis_sharedtable(lua_State* L) {
  56. int b = 0;
  57. if(lua_type(L, 1) == LUA_TTABLE) {
  58. Table * t = (Table *)lua_topointer(L, 1);
  59. b = isshared(t);
  60. }
  61. lua_pushboolean(L, b);
  62. return 1;
  63. }
  64. static int
  65. make_matrix(lua_State *L) {
  66. // turn off gc , because marking shared will prevent gc mark.
  67. lua_gc(L, LUA_GCSTOP, 0);
  68. mark_shared(L);
  69. Table * t = (Table *)lua_topointer(L, -1);
  70. lua_pushlightuserdata(L, t);
  71. return 1;
  72. }
  73. static int
  74. clone_table(lua_State *L) {
  75. lua_clonetable(L, lua_touserdata(L, 1));
  76. return 1;
  77. }
  78. static int
  79. lco_stackvalues(lua_State* L) {
  80. lua_State *cL = lua_tothread(L, 1);
  81. luaL_argcheck(L, cL, 1, "thread expected");
  82. int n = 0;
  83. if(cL != L) {
  84. luaL_checktype(L, 2, LUA_TTABLE);
  85. n = lua_gettop(cL);
  86. if(n > 0) {
  87. luaL_checkstack(L, n+1, NULL);
  88. int top = lua_gettop(L);
  89. lua_xmove(cL, L, n);
  90. int i=0;
  91. for(i=1; i<=n; i++) {
  92. lua_pushvalue(L, top+i);
  93. lua_seti(L, 2, i);
  94. }
  95. lua_xmove(L, cL, n);
  96. }
  97. }
  98. lua_pushinteger(L, n);
  99. return 1;
  100. }
  101. struct state_ud {
  102. lua_State *L;
  103. };
  104. static int
  105. close_state(lua_State *L) {
  106. struct state_ud *ud = (struct state_ud *)luaL_checkudata(L, 1, "BOXMATRIXSTATE");
  107. if (ud->L) {
  108. lua_close(ud->L);
  109. ud->L = NULL;
  110. }
  111. return 0;
  112. }
  113. static int
  114. get_matrix(lua_State *L) {
  115. struct state_ud *ud = (struct state_ud *)luaL_checkudata(L, 1, "BOXMATRIXSTATE");
  116. if (ud->L) {
  117. const void * v = lua_topointer(ud->L, 1);
  118. lua_pushlightuserdata(L, (void *)v);
  119. return 1;
  120. }
  121. return 0;
  122. }
  123. static int
  124. get_size(lua_State *L) {
  125. struct state_ud *ud = (struct state_ud *)luaL_checkudata(L, 1, "BOXMATRIXSTATE");
  126. if (ud->L) {
  127. lua_Integer sz = lua_gc(ud->L, LUA_GCCOUNT, 0);
  128. sz *= 1024;
  129. sz += lua_gc(ud->L, LUA_GCCOUNTB, 0);
  130. lua_pushinteger(L, sz);
  131. } else {
  132. lua_pushinteger(L, 0);
  133. }
  134. return 1;
  135. }
  136. static int
  137. box_state(lua_State *L, lua_State *mL) {
  138. struct state_ud *ud = (struct state_ud *)lua_newuserdatauv(L, sizeof(*ud), 0);
  139. ud->L = mL;
  140. if (luaL_newmetatable(L, "BOXMATRIXSTATE")) {
  141. lua_pushvalue(L, -1);
  142. lua_setfield(L, -2, "__index");
  143. lua_pushcfunction(L, close_state);
  144. lua_setfield(L, -2, "close");
  145. lua_pushcfunction(L, get_matrix);
  146. lua_setfield(L, -2, "getptr");
  147. lua_pushcfunction(L, get_size);
  148. lua_setfield(L, -2, "size");
  149. }
  150. lua_setmetatable(L, -2);
  151. return 1;
  152. }
  153. static int
  154. load_matrixfile(lua_State *L) {
  155. luaL_openlibs(L);
  156. const char * source = (const char *)lua_touserdata(L, 1);
  157. if (source[0] == '@') {
  158. if (luaL_loadfilex_(L, source+1, NULL) != LUA_OK)
  159. lua_error(L);
  160. } else {
  161. if (luaL_loadstring(L, source) != LUA_OK)
  162. lua_error(L);
  163. }
  164. lua_replace(L, 1);
  165. if (lua_pcall(L, lua_gettop(L) - 1, 1, 0) != LUA_OK)
  166. lua_error(L);
  167. lua_gc(L, LUA_GCCOLLECT, 0);
  168. lua_pushcfunction(L, make_matrix);
  169. lua_insert(L, -2);
  170. lua_call(L, 1, 1);
  171. return 1;
  172. }
  173. static int
  174. matrix_from_file(lua_State *L) {
  175. lua_State *mL = luaL_newstate();
  176. if (mL == NULL) {
  177. return luaL_error(L, "luaL_newstate failed");
  178. }
  179. const char * source = luaL_checkstring(L, 1);
  180. int top = lua_gettop(L);
  181. lua_pushcfunction(mL, load_matrixfile);
  182. lua_pushlightuserdata(mL, (void *)source);
  183. if (top > 1) {
  184. if (!lua_checkstack(mL, top + 1)) {
  185. return luaL_error(L, "Too many argument %d", top);
  186. }
  187. int i;
  188. for (i=2;i<=top;i++) {
  189. switch(lua_type(L, i)) {
  190. case LUA_TBOOLEAN:
  191. lua_pushboolean(mL, lua_toboolean(L, i));
  192. break;
  193. case LUA_TNUMBER:
  194. if (lua_isinteger(L, i)) {
  195. lua_pushinteger(mL, lua_tointeger(L, i));
  196. } else {
  197. lua_pushnumber(mL, lua_tonumber(L, i));
  198. }
  199. break;
  200. case LUA_TLIGHTUSERDATA:
  201. lua_pushlightuserdata(mL, lua_touserdata(L, i));
  202. break;
  203. case LUA_TFUNCTION:
  204. if (lua_iscfunction(L, i) && lua_getupvalue(L, i, 1) == NULL) {
  205. lua_pushcfunction(mL, lua_tocfunction(L, i));
  206. break;
  207. }
  208. return luaL_argerror(L, i, "Only support light C function");
  209. default:
  210. return luaL_argerror(L, i, "Type invalid");
  211. }
  212. }
  213. }
  214. int ok = lua_pcall(mL, top, 1, 0);
  215. if (ok != LUA_OK) {
  216. lua_pushstring(L, lua_tostring(mL, -1));
  217. lua_close(mL);
  218. lua_error(L);
  219. }
  220. return box_state(L, mL);
  221. }
  222. LUAMOD_API int
  223. luaopen_skynet_sharetable_core(lua_State *L) {
  224. luaL_checkversion(L);
  225. luaL_Reg l[] = {
  226. { "clone", clone_table },
  227. { "stackvalues", lco_stackvalues },
  228. { "matrix", matrix_from_file },
  229. { "is_sharedtable", lis_sharedtable },
  230. { NULL, NULL },
  231. };
  232. luaL_newlib(L, l);
  233. return 1;
  234. }
  235. #else
  236. LUAMOD_API int
  237. luaopen_skynet_sharetable_core(lua_State *L) {
  238. return luaL_error(L, "No share string table support");
  239. }
  240. #endif