lua-sharedata.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. #define LUA_LIB
  2. #include <lua.h>
  3. #include <lauxlib.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include "atomic.h"
  9. #define KEYTYPE_INTEGER 0
  10. #define KEYTYPE_STRING 1
  11. #define VALUETYPE_NIL 0
  12. #define VALUETYPE_REAL 1
  13. #define VALUETYPE_STRING 2
  14. #define VALUETYPE_BOOLEAN 3
  15. #define VALUETYPE_TABLE 4
  16. #define VALUETYPE_INTEGER 5
  17. struct table;
  18. union value {
  19. lua_Number n;
  20. lua_Integer d;
  21. struct table * tbl;
  22. int string;
  23. int boolean;
  24. };
  25. struct node {
  26. union value v;
  27. int key; // integer key or index of string table
  28. int next; // next slot index
  29. uint32_t keyhash;
  30. uint8_t keytype; // key type must be integer or string
  31. uint8_t valuetype; // value type can be number/string/boolean/table
  32. uint8_t nocolliding; // 0 means colliding slot
  33. };
  34. struct state {
  35. int dirty;
  36. ATOM_INT ref;
  37. struct table * root;
  38. };
  39. struct table {
  40. int sizearray;
  41. int sizehash;
  42. uint8_t *arraytype;
  43. union value * array;
  44. struct node * hash;
  45. lua_State * L;
  46. };
  47. struct context {
  48. lua_State * L;
  49. struct table * tbl;
  50. int string_index;
  51. };
  52. struct ctrl {
  53. struct table * root;
  54. struct table * update;
  55. };
  56. static int
  57. countsize(lua_State *L, int sizearray) {
  58. int n = 0;
  59. lua_pushnil(L);
  60. while (lua_next(L, 1) != 0) {
  61. int type = lua_type(L, -2);
  62. ++n;
  63. if (type == LUA_TNUMBER) {
  64. if (!lua_isinteger(L, -2)) {
  65. luaL_error(L, "Invalid key %f", lua_tonumber(L, -2));
  66. }
  67. lua_Integer nkey = lua_tointeger(L, -2);
  68. if (nkey > 0 && nkey <= sizearray) {
  69. --n;
  70. }
  71. } else if (type != LUA_TSTRING && type != LUA_TTABLE) {
  72. luaL_error(L, "Invalid key type %s", lua_typename(L, type));
  73. }
  74. lua_pop(L, 1);
  75. }
  76. return n;
  77. }
  78. static uint32_t
  79. calchash(const char * str, size_t l) {
  80. uint32_t h = (uint32_t)l;
  81. size_t l1;
  82. size_t step = (l >> 5) + 1;
  83. for (l1 = l; l1 >= step; l1 -= step) {
  84. h = h ^ ((h<<5) + (h>>2) + (uint8_t)(str[l1 - 1]));
  85. }
  86. return h;
  87. }
  88. static int
  89. stringindex(struct context *ctx, const char * str, size_t sz) {
  90. lua_State *L = ctx->L;
  91. lua_pushlstring(L, str, sz);
  92. lua_pushvalue(L, -1);
  93. lua_rawget(L, 1);
  94. int index;
  95. // stringmap(1) str index
  96. if (lua_isnil(L, -1)) {
  97. index = ++ctx->string_index;
  98. lua_pop(L, 1);
  99. lua_pushinteger(L, index);
  100. lua_rawset(L, 1);
  101. } else {
  102. index = lua_tointeger(L, -1);
  103. lua_pop(L, 2);
  104. }
  105. return index;
  106. }
  107. static int convtable(lua_State *L);
  108. static void
  109. setvalue(struct context * ctx, lua_State *L, int index, struct node *n) {
  110. int vt = lua_type(L, index);
  111. switch(vt) {
  112. case LUA_TNIL:
  113. n->valuetype = VALUETYPE_NIL;
  114. break;
  115. case LUA_TNUMBER:
  116. if (lua_isinteger(L, index)) {
  117. n->v.d = lua_tointeger(L, index);
  118. n->valuetype = VALUETYPE_INTEGER;
  119. } else {
  120. n->v.n = lua_tonumber(L, index);
  121. n->valuetype = VALUETYPE_REAL;
  122. }
  123. break;
  124. case LUA_TSTRING: {
  125. size_t sz = 0;
  126. const char * str = lua_tolstring(L, index, &sz);
  127. n->v.string = stringindex(ctx, str, sz);
  128. n->valuetype = VALUETYPE_STRING;
  129. break;
  130. }
  131. case LUA_TBOOLEAN:
  132. n->v.boolean = lua_toboolean(L, index);
  133. n->valuetype = VALUETYPE_BOOLEAN;
  134. break;
  135. case LUA_TTABLE: {
  136. struct table *tbl = ctx->tbl;
  137. ctx->tbl = (struct table *)malloc(sizeof(struct table));
  138. if (ctx->tbl == NULL) {
  139. ctx->tbl = tbl;
  140. luaL_error(L, "memory error");
  141. // never get here
  142. }
  143. memset(ctx->tbl, 0, sizeof(struct table));
  144. int absidx = lua_absindex(L, index);
  145. lua_pushcfunction(L, convtable);
  146. lua_pushvalue(L, absidx);
  147. lua_pushlightuserdata(L, ctx);
  148. lua_call(L, 2, 0);
  149. n->v.tbl = ctx->tbl;
  150. n->valuetype = VALUETYPE_TABLE;
  151. ctx->tbl = tbl;
  152. break;
  153. }
  154. default:
  155. luaL_error(L, "Unsupport value type %s", lua_typename(L, vt));
  156. break;
  157. }
  158. }
  159. static void
  160. setarray(struct context *ctx, lua_State *L, int index, int key) {
  161. struct node n;
  162. setvalue(ctx, L, index, &n);
  163. struct table *tbl = ctx->tbl;
  164. --key; // base 0
  165. tbl->arraytype[key] = n.valuetype;
  166. tbl->array[key] = n.v;
  167. }
  168. static int
  169. ishashkey(struct context * ctx, lua_State *L, int index, int *key, uint32_t *keyhash, int *keytype) {
  170. int sizearray = ctx->tbl->sizearray;
  171. int kt = lua_type(L, index);
  172. if (kt == LUA_TNUMBER) {
  173. *key = lua_tointeger(L, index);
  174. if (*key > 0 && *key <= sizearray) {
  175. return 0;
  176. }
  177. *keyhash = (uint32_t)*key;
  178. *keytype = KEYTYPE_INTEGER;
  179. } else {
  180. size_t sz = 0;
  181. const char * s = lua_tolstring(L, index, &sz);
  182. *keyhash = calchash(s, sz);
  183. *key = stringindex(ctx, s, sz);
  184. *keytype = KEYTYPE_STRING;
  185. }
  186. return 1;
  187. }
  188. static void
  189. fillnocolliding(lua_State *L, struct context *ctx) {
  190. struct table * tbl = ctx->tbl;
  191. lua_pushnil(L);
  192. while (lua_next(L, 1) != 0) {
  193. int key;
  194. int keytype;
  195. uint32_t keyhash;
  196. if (!ishashkey(ctx, L, -2, &key, &keyhash, &keytype)) {
  197. setarray(ctx, L, -1, key);
  198. } else {
  199. struct node * n = &tbl->hash[keyhash % tbl->sizehash];
  200. if (n->valuetype == VALUETYPE_NIL) {
  201. n->key = key;
  202. n->keytype = keytype;
  203. n->keyhash = keyhash;
  204. n->next = -1;
  205. n->nocolliding = 1;
  206. setvalue(ctx, L, -1, n); // set n->v , n->valuetype
  207. }
  208. }
  209. lua_pop(L,1);
  210. }
  211. }
  212. static void
  213. fillcolliding(lua_State *L, struct context *ctx) {
  214. struct table * tbl = ctx->tbl;
  215. int sizehash = tbl->sizehash;
  216. int emptyslot = 0;
  217. int i;
  218. lua_pushnil(L);
  219. while (lua_next(L, 1) != 0) {
  220. int key;
  221. int keytype;
  222. uint32_t keyhash;
  223. if (ishashkey(ctx, L, -2, &key, &keyhash, &keytype)) {
  224. struct node * mainpos = &tbl->hash[keyhash % tbl->sizehash];
  225. if (!(mainpos->keytype == keytype && mainpos->key == key)) {
  226. // the key has not insert
  227. struct node * n = NULL;
  228. for (i=emptyslot;i<sizehash;i++) {
  229. if (tbl->hash[i].valuetype == VALUETYPE_NIL) {
  230. n = &tbl->hash[i];
  231. emptyslot = i + 1;
  232. break;
  233. }
  234. }
  235. assert(n);
  236. n->next = mainpos->next;
  237. mainpos->next = n - tbl->hash;
  238. mainpos->nocolliding = 0;
  239. n->key = key;
  240. n->keytype = keytype;
  241. n->keyhash = keyhash;
  242. n->nocolliding = 0;
  243. setvalue(ctx, L, -1, n); // set n->v , n->valuetype
  244. }
  245. }
  246. lua_pop(L,1);
  247. }
  248. }
  249. // table need convert
  250. // struct context * ctx
  251. static int
  252. convtable(lua_State *L) {
  253. int i;
  254. struct context *ctx = lua_touserdata(L,2);
  255. struct table *tbl = ctx->tbl;
  256. tbl->L = ctx->L;
  257. int sizearray = lua_rawlen(L, 1);
  258. if (sizearray) {
  259. tbl->arraytype = (uint8_t *)malloc(sizearray * sizeof(uint8_t));
  260. if (tbl->arraytype == NULL) {
  261. goto memerror;
  262. }
  263. for (i=0;i<sizearray;i++) {
  264. tbl->arraytype[i] = VALUETYPE_NIL;
  265. }
  266. tbl->array = (union value *)malloc(sizearray * sizeof(union value));
  267. if (tbl->array == NULL) {
  268. goto memerror;
  269. }
  270. tbl->sizearray = sizearray;
  271. }
  272. int sizehash = countsize(L, sizearray);
  273. if (sizehash) {
  274. tbl->hash = (struct node *)malloc(sizehash * sizeof(struct node));
  275. if (tbl->hash == NULL) {
  276. goto memerror;
  277. }
  278. for (i=0;i<sizehash;i++) {
  279. tbl->hash[i].valuetype = VALUETYPE_NIL;
  280. tbl->hash[i].nocolliding = 0;
  281. tbl->hash[i].next = -1;
  282. }
  283. tbl->sizehash = sizehash;
  284. fillnocolliding(L, ctx);
  285. fillcolliding(L, ctx);
  286. } else {
  287. int i;
  288. for (i=1;i<=sizearray;i++) {
  289. lua_rawgeti(L, 1, i);
  290. setarray(ctx, L, -1, i);
  291. lua_pop(L,1);
  292. }
  293. }
  294. return 0;
  295. memerror:
  296. return luaL_error(L, "memory error");
  297. }
  298. static void
  299. delete_tbl(struct table *tbl) {
  300. int i;
  301. for (i=0;i<tbl->sizearray;i++) {
  302. if (tbl->arraytype[i] == VALUETYPE_TABLE) {
  303. delete_tbl(tbl->array[i].tbl);
  304. }
  305. }
  306. for (i=0;i<tbl->sizehash;i++) {
  307. if (tbl->hash[i].valuetype == VALUETYPE_TABLE) {
  308. delete_tbl(tbl->hash[i].v.tbl);
  309. }
  310. }
  311. free(tbl->arraytype);
  312. free(tbl->array);
  313. free(tbl->hash);
  314. free(tbl);
  315. }
  316. static int
  317. pconv(lua_State *L) {
  318. struct context *ctx = lua_touserdata(L,1);
  319. lua_State * pL = lua_touserdata(L, 2);
  320. int ret;
  321. lua_settop(L, 0);
  322. // init L (may throw memory error)
  323. // create a table for string map
  324. lua_newtable(L);
  325. lua_pushcfunction(pL, convtable);
  326. lua_pushvalue(pL,1);
  327. lua_pushlightuserdata(pL, ctx);
  328. ret = lua_pcall(pL, 2, 0, 0);
  329. if (ret != LUA_OK) {
  330. size_t sz = 0;
  331. const char * error = lua_tolstring(pL, -1, &sz);
  332. lua_pushlstring(L, error, sz);
  333. lua_error(L);
  334. // never get here
  335. }
  336. luaL_checkstack(L, ctx->string_index + 3, NULL);
  337. lua_settop(L,1);
  338. return 1;
  339. }
  340. static void
  341. convert_stringmap(struct context *ctx, struct table *tbl) {
  342. lua_State *L = ctx->L;
  343. lua_checkstack(L, ctx->string_index + LUA_MINSTACK);
  344. lua_settop(L, ctx->string_index + 1);
  345. lua_pushvalue(L, 1);
  346. struct state * s = lua_newuserdatauv(L, sizeof(*s), 1);
  347. s->dirty = 0;
  348. ATOM_INIT(&s->ref , 0);
  349. s->root = tbl;
  350. lua_replace(L, 1);
  351. lua_replace(L, -2);
  352. lua_pushnil(L);
  353. // ... stringmap nil
  354. while (lua_next(L, -2) != 0) {
  355. int idx = lua_tointeger(L, -1);
  356. lua_pop(L, 1);
  357. lua_pushvalue(L, -1);
  358. lua_replace(L, idx);
  359. }
  360. lua_pop(L, 1);
  361. lua_gc(L, LUA_GCCOLLECT, 0);
  362. }
  363. static int
  364. lnewconf(lua_State *L) {
  365. int ret;
  366. struct context ctx;
  367. struct table * tbl = NULL;
  368. luaL_checktype(L,1,LUA_TTABLE);
  369. ctx.L = luaL_newstate();
  370. ctx.tbl = NULL;
  371. ctx.string_index = 1; // 1 reserved for dirty flag
  372. if (ctx.L == NULL) {
  373. lua_pushliteral(L, "memory error");
  374. goto error;
  375. }
  376. tbl = (struct table *)malloc(sizeof(struct table));
  377. if (tbl == NULL) {
  378. // lua_pushliteral may fail because of memory error, close first.
  379. lua_close(ctx.L);
  380. ctx.L = NULL;
  381. lua_pushliteral(L, "memory error");
  382. goto error;
  383. }
  384. memset(tbl, 0, sizeof(struct table));
  385. ctx.tbl = tbl;
  386. lua_pushcfunction(ctx.L, pconv);
  387. lua_pushlightuserdata(ctx.L , &ctx);
  388. lua_pushlightuserdata(ctx.L , L);
  389. ret = lua_pcall(ctx.L, 2, 1, 0);
  390. if (ret != LUA_OK) {
  391. size_t sz = 0;
  392. const char * error = lua_tolstring(ctx.L, -1, &sz);
  393. lua_pushlstring(L, error, sz);
  394. goto error;
  395. }
  396. convert_stringmap(&ctx, tbl);
  397. lua_pushlightuserdata(L, tbl);
  398. return 1;
  399. error:
  400. if (ctx.L) {
  401. lua_close(ctx.L);
  402. }
  403. if (tbl) {
  404. delete_tbl(tbl);
  405. }
  406. lua_error(L);
  407. return -1;
  408. }
  409. static struct table *
  410. get_table(lua_State *L, int index) {
  411. struct table *tbl = lua_touserdata(L,index);
  412. if (tbl == NULL) {
  413. luaL_error(L, "Need a conf object");
  414. }
  415. return tbl;
  416. }
  417. static int
  418. ldeleteconf(lua_State *L) {
  419. struct table *tbl = get_table(L,1);
  420. lua_close(tbl->L);
  421. delete_tbl(tbl);
  422. return 0;
  423. }
  424. static void
  425. pushvalue(lua_State *L, lua_State *sL, uint8_t vt, union value *v) {
  426. switch(vt) {
  427. case VALUETYPE_REAL:
  428. lua_pushnumber(L, v->n);
  429. break;
  430. case VALUETYPE_INTEGER:
  431. lua_pushinteger(L, v->d);
  432. break;
  433. case VALUETYPE_STRING: {
  434. size_t sz = 0;
  435. const char *str = lua_tolstring(sL, v->string, &sz);
  436. lua_pushlstring(L, str, sz);
  437. break;
  438. }
  439. case VALUETYPE_BOOLEAN:
  440. lua_pushboolean(L, v->boolean);
  441. break;
  442. case VALUETYPE_TABLE:
  443. lua_pushlightuserdata(L, v->tbl);
  444. break;
  445. default:
  446. lua_pushnil(L);
  447. break;
  448. }
  449. }
  450. static struct node *
  451. lookup_key(struct table *tbl, uint32_t keyhash, int key, int keytype, const char *str, size_t sz) {
  452. if (tbl->sizehash == 0)
  453. return NULL;
  454. struct node *n = &tbl->hash[keyhash % tbl->sizehash];
  455. if (keyhash != n->keyhash && n->nocolliding)
  456. return NULL;
  457. for (;;) {
  458. if (keyhash == n->keyhash) {
  459. if (n->keytype == KEYTYPE_INTEGER) {
  460. if (keytype == KEYTYPE_INTEGER && n->key == key) {
  461. return n;
  462. }
  463. } else {
  464. // n->keytype == KEYTYPE_STRING
  465. if (keytype == KEYTYPE_STRING) {
  466. size_t sz2 = 0;
  467. const char * str2 = lua_tolstring(tbl->L, n->key, &sz2);
  468. if (sz == sz2 && memcmp(str,str2,sz) == 0) {
  469. return n;
  470. }
  471. }
  472. }
  473. }
  474. if (n->next < 0) {
  475. return NULL;
  476. }
  477. n = &tbl->hash[n->next];
  478. }
  479. }
  480. static int
  481. lindexconf(lua_State *L) {
  482. struct table *tbl = get_table(L,1);
  483. int kt = lua_type(L,2);
  484. uint32_t keyhash;
  485. int key = 0;
  486. int keytype;
  487. size_t sz = 0;
  488. const char * str = NULL;
  489. if (kt == LUA_TNUMBER) {
  490. if (!lua_isinteger(L, 2)) {
  491. return luaL_error(L, "Invalid key %f", lua_tonumber(L, 2));
  492. }
  493. key = (int)lua_tointeger(L, 2);
  494. if (key > 0 && key <= tbl->sizearray) {
  495. --key;
  496. pushvalue(L, tbl->L, tbl->arraytype[key], &tbl->array[key]);
  497. return 1;
  498. }
  499. keytype = KEYTYPE_INTEGER;
  500. keyhash = (uint32_t)key;
  501. } else {
  502. str = luaL_checklstring(L, 2, &sz);
  503. keyhash = calchash(str, sz);
  504. keytype = KEYTYPE_STRING;
  505. }
  506. struct node *n = lookup_key(tbl, keyhash, key, keytype, str, sz);
  507. if (n) {
  508. pushvalue(L, tbl->L, n->valuetype, &n->v);
  509. return 1;
  510. } else {
  511. return 0;
  512. }
  513. }
  514. static void
  515. pushkey(lua_State *L, lua_State *sL, struct node *n) {
  516. if (n->keytype == KEYTYPE_INTEGER) {
  517. lua_pushinteger(L, n->key);
  518. } else {
  519. size_t sz = 0;
  520. const char * str = lua_tolstring(sL, n->key, &sz);
  521. lua_pushlstring(L, str, sz);
  522. }
  523. }
  524. static int
  525. pushfirsthash(lua_State *L, struct table * tbl) {
  526. if (tbl->sizehash) {
  527. pushkey(L, tbl->L, &tbl->hash[0]);
  528. return 1;
  529. } else {
  530. return 0;
  531. }
  532. }
  533. static int
  534. lnextkey(lua_State *L) {
  535. struct table *tbl = get_table(L,1);
  536. if (lua_isnoneornil(L,2)) {
  537. if (tbl->sizearray > 0) {
  538. int i;
  539. for (i=0;i<tbl->sizearray;i++) {
  540. if (tbl->arraytype[i] != VALUETYPE_NIL) {
  541. lua_pushinteger(L, i+1);
  542. return 1;
  543. }
  544. }
  545. }
  546. return pushfirsthash(L, tbl);
  547. }
  548. int kt = lua_type(L,2);
  549. uint32_t keyhash;
  550. int key = 0;
  551. int keytype;
  552. size_t sz=0;
  553. const char *str = NULL;
  554. int sizearray = tbl->sizearray;
  555. if (kt == LUA_TNUMBER) {
  556. if (!lua_isinteger(L, 2)) {
  557. return 0;
  558. }
  559. key = (int)lua_tointeger(L, 2);
  560. if (key > 0 && key <= sizearray) {
  561. lua_Integer i;
  562. for (i=key;i<sizearray;i++) {
  563. if (tbl->arraytype[i] != VALUETYPE_NIL) {
  564. lua_pushinteger(L, i+1);
  565. return 1;
  566. }
  567. }
  568. return pushfirsthash(L, tbl);
  569. }
  570. keyhash = (uint32_t)key;
  571. keytype = KEYTYPE_INTEGER;
  572. } else {
  573. str = luaL_checklstring(L, 2, &sz);
  574. keyhash = calchash(str, sz);
  575. keytype = KEYTYPE_STRING;
  576. }
  577. struct node *n = lookup_key(tbl, keyhash, key, keytype, str, sz);
  578. if (n) {
  579. ++n;
  580. int index = n-tbl->hash;
  581. if (index == tbl->sizehash) {
  582. return 0;
  583. }
  584. pushkey(L, tbl->L, n);
  585. return 1;
  586. } else {
  587. return 0;
  588. }
  589. }
  590. static int
  591. llen(lua_State *L) {
  592. struct table *tbl = get_table(L,1);
  593. lua_pushinteger(L, tbl->sizearray);
  594. return 1;
  595. }
  596. static int
  597. lhashlen(lua_State *L) {
  598. struct table *tbl = get_table(L,1);
  599. lua_pushinteger(L, tbl->sizehash);
  600. return 1;
  601. }
  602. static int
  603. releaseobj(lua_State *L) {
  604. struct ctrl *c = lua_touserdata(L, 1);
  605. struct table *tbl = c->root;
  606. struct state *s = lua_touserdata(tbl->L, 1);
  607. ATOM_FDEC(&s->ref);
  608. c->root = NULL;
  609. c->update = NULL;
  610. return 0;
  611. }
  612. static int
  613. lboxconf(lua_State *L) {
  614. struct table * tbl = get_table(L,1);
  615. struct state * s = lua_touserdata(tbl->L, 1);
  616. ATOM_FINC(&s->ref);
  617. struct ctrl * c = lua_newuserdatauv(L, sizeof(*c), 1);
  618. c->root = tbl;
  619. c->update = NULL;
  620. if (luaL_newmetatable(L, "confctrl")) {
  621. lua_pushcfunction(L, releaseobj);
  622. lua_setfield(L, -2, "__gc");
  623. }
  624. lua_setmetatable(L, -2);
  625. return 1;
  626. }
  627. static int
  628. lmarkdirty(lua_State *L) {
  629. struct table *tbl = get_table(L,1);
  630. struct state * s = lua_touserdata(tbl->L, 1);
  631. s->dirty = 1;
  632. return 0;
  633. }
  634. static int
  635. lisdirty(lua_State *L) {
  636. struct table *tbl = get_table(L,1);
  637. struct state * s = lua_touserdata(tbl->L, 1);
  638. int d = s->dirty;
  639. lua_pushboolean(L, d);
  640. return 1;
  641. }
  642. static int
  643. lgetref(lua_State *L) {
  644. struct table *tbl = get_table(L,1);
  645. struct state * s = lua_touserdata(tbl->L, 1);
  646. lua_pushinteger(L , ATOM_LOAD(&s->ref));
  647. return 1;
  648. }
  649. static int
  650. lincref(lua_State *L) {
  651. struct table *tbl = get_table(L,1);
  652. struct state * s = lua_touserdata(tbl->L, 1);
  653. int ref = ATOM_FINC(&s->ref)+1;
  654. lua_pushinteger(L , ref);
  655. return 1;
  656. }
  657. static int
  658. ldecref(lua_State *L) {
  659. struct table *tbl = get_table(L,1);
  660. struct state * s = lua_touserdata(tbl->L, 1);
  661. int ref = ATOM_FDEC(&s->ref)-1;
  662. lua_pushinteger(L , ref);
  663. return 1;
  664. }
  665. static int
  666. lneedupdate(lua_State *L) {
  667. struct ctrl * c = lua_touserdata(L, 1);
  668. if (c->update) {
  669. lua_pushlightuserdata(L, c->update);
  670. lua_getiuservalue(L, 1, 1);
  671. return 2;
  672. }
  673. return 0;
  674. }
  675. static int
  676. lupdate(lua_State *L) {
  677. luaL_checktype(L, 1, LUA_TUSERDATA);
  678. luaL_checktype(L, 2, LUA_TLIGHTUSERDATA);
  679. luaL_checktype(L, 3, LUA_TTABLE);
  680. struct ctrl * c= lua_touserdata(L, 1);
  681. struct table *n = lua_touserdata(L, 2);
  682. if (c->root == n) {
  683. return luaL_error(L, "You should update a new object");
  684. }
  685. lua_settop(L, 3);
  686. lua_setiuservalue(L, 1, 1);
  687. c->update = n;
  688. return 0;
  689. }
  690. LUAMOD_API int
  691. luaopen_skynet_sharedata_core(lua_State *L) {
  692. luaL_Reg l[] = {
  693. // used by host
  694. { "new", lnewconf },
  695. { "delete", ldeleteconf },
  696. { "markdirty", lmarkdirty },
  697. { "getref", lgetref },
  698. { "incref", lincref },
  699. { "decref", ldecref },
  700. // used by client
  701. { "box", lboxconf },
  702. { "index", lindexconf },
  703. { "nextkey", lnextkey },
  704. { "len", llen },
  705. { "hashlen", lhashlen },
  706. { "isdirty", lisdirty },
  707. { "needupdate", lneedupdate },
  708. { "update", lupdate },
  709. { NULL, NULL },
  710. };
  711. luaL_checkversion(L);
  712. luaL_newlib(L, l);
  713. return 1;
  714. }