sjson.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. #define LUA_LIB
  2. #include "lua.h"
  3. #include "lauxlib.h"
  4. #include "lstring.h"
  5. #ifndef LOCAL_LUA
  6. #include "module.h"
  7. #include "c_string.h"
  8. #include "c_math.h"
  9. #include "c_limits.h"
  10. #endif
  11. #define JSONSL_STATE_USER_FIELDS int lua_object_ref; int used_count;
  12. #define JSONSL_NO_JPR
  13. #include "jsonsl.c"
  14. #define LUA_SJSONLIBNAME "sjson"
  15. #define DEFAULT_DEPTH 20
  16. #define DBG_PRINTF(...)
  17. typedef struct {
  18. jsonsl_t jsn;
  19. int result_ref;
  20. int hkey_ref;
  21. int null_ref;
  22. int metatable;
  23. int pos_ref;
  24. uint8_t complete;
  25. const char *error;
  26. lua_State *L;
  27. size_t min_needed;
  28. size_t min_available;
  29. size_t buffer_len;
  30. const char *buffer; // Points into buffer_ref
  31. int buffer_ref;
  32. } JSN_DATA;
  33. #define get_parent_object_ref() ((state->level == 1) ? data->result_ref : state[-1].lua_object_ref)
  34. #define get_parent_object_used_count_pre_inc() ((state->level == 1) ? 1 : ++state[-1].used_count)
  35. static const char* get_state_buffer(JSN_DATA *ctx, struct jsonsl_state_st *state)
  36. {
  37. size_t offset = state->pos_begin - ctx->min_available;
  38. return ctx->buffer + offset;
  39. }
  40. // The elem data is a ref
  41. static int error_callback(jsonsl_t jsn,
  42. jsonsl_error_t err,
  43. struct jsonsl_state_st *state,
  44. char *at)
  45. {
  46. JSN_DATA *data = (JSN_DATA *) jsn->data;
  47. data->error = jsonsl_strerror(err);
  48. //fprintf(stderr, "Got error at pos %lu: %s\n", jsn->pos, jsonsl_strerror(err));
  49. return 0;
  50. }
  51. static void
  52. create_table(JSN_DATA *data) {
  53. lua_newtable(data->L);
  54. if (data->metatable != LUA_NOREF) {
  55. lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->metatable);
  56. lua_setmetatable(data->L, -2);
  57. }
  58. }
  59. static void
  60. create_new_element(jsonsl_t jsn,
  61. jsonsl_action_t action,
  62. struct jsonsl_state_st *state,
  63. const char *buf)
  64. {
  65. JSN_DATA *data = jsn->data;
  66. DBG_PRINTF("L%d: new action %d @ %d state->type %s\n", state->level, action, state->pos_begin, jsonsl_strtype(state->type));
  67. DBG_PRINTF("buf: '%s' ('%.10s')\n", buf, get_state_buffer(data, state));
  68. state->lua_object_ref = LUA_NOREF;
  69. switch(state->type) {
  70. case JSONSL_T_SPECIAL:
  71. case JSONSL_T_STRING:
  72. case JSONSL_T_HKEY:
  73. break;
  74. case JSONSL_T_LIST:
  75. case JSONSL_T_OBJECT:
  76. create_table(data);
  77. state->lua_object_ref = lua_ref(data->L, 1);
  78. state->used_count = 0;
  79. lua_rawgeti(data->L, LUA_REGISTRYINDEX, get_parent_object_ref());
  80. if (data->hkey_ref == LUA_NOREF) {
  81. // list, so append
  82. lua_pushnumber(data->L, get_parent_object_used_count_pre_inc());
  83. DBG_PRINTF("Adding array element\n");
  84. } else {
  85. // object, so
  86. lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->hkey_ref);
  87. lua_unref(data->L, data->hkey_ref);
  88. data->hkey_ref = LUA_NOREF;
  89. DBG_PRINTF("Adding hash element\n");
  90. }
  91. if (data->pos_ref != LUA_NOREF && state->level > 1) {
  92. lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->pos_ref);
  93. lua_pushnumber(data->L, state->level - 1);
  94. lua_pushvalue(data->L, -3); // get the key
  95. lua_settable(data->L, -3);
  96. lua_pop(data->L, 1);
  97. }
  98. // At this point, the stack:
  99. // top: index/hash key
  100. // : table
  101. int want_value = 1;
  102. // Invoke the checkpath method if possible
  103. if (data->pos_ref != LUA_NOREF) {
  104. lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->metatable);
  105. lua_getfield(data->L, -1, "checkpath");
  106. if (lua_type(data->L, -1) != LUA_TNIL) {
  107. // Call with the new table and the path as arguments
  108. lua_rawgeti(data->L, LUA_REGISTRYINDEX, state->lua_object_ref);
  109. lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->pos_ref);
  110. lua_call(data->L, 2, 1);
  111. want_value = lua_toboolean(data->L, -1);
  112. }
  113. lua_pop(data->L, 2); // Discard the metatable and either the getfield result or retval
  114. }
  115. if (want_value) {
  116. lua_rawgeti(data->L, LUA_REGISTRYINDEX, state->lua_object_ref);
  117. lua_settable(data->L, -3);
  118. lua_pop(data->L, 1); // the table
  119. } else {
  120. lua_pop(data->L, 2); // the index and table
  121. }
  122. break;
  123. default:
  124. DBG_PRINTF("Unhandled type %c\n", state->type);
  125. luaL_error(data->L, "Unhandled type");
  126. break;
  127. }
  128. data->min_needed = state->pos_begin;
  129. }
  130. static void push_number(JSN_DATA *data, struct jsonsl_state_st *state) {
  131. lua_pushlstring(data->L, get_state_buffer(data, state), state->pos_cur - state->pos_begin);
  132. LUA_NUMBER r = lua_tonumber(data->L, -1);
  133. lua_pop(data->L, 1);
  134. lua_pushnumber(data->L, r);
  135. }
  136. static int fromhex(char c) {
  137. if (c <= '9') {
  138. return c & 0xf;
  139. }
  140. return ((c - 'A' + 10) & 0xf);
  141. }
  142. static void output_utf8(luaL_Buffer *buf, int c) {
  143. char space[4];
  144. char *b = space;
  145. if (c<0x80) *b++=c;
  146. else if (c<0x800) *b++=192+c/64, *b++=128+c%64;
  147. else if (c-0xd800u<0x800) *b++ = '?';
  148. else if (c<0x10000) *b++=224+c/4096, *b++=128+c/64%64, *b++=128+c%64;
  149. else if (c<0x110000) *b++=240+c/262144, *b++=128+c/4096%64, *b++=128+c/64%64, *b++=128+c%64;
  150. else *b++ = '?';
  151. luaL_addlstring(buf, space, b - space);
  152. }
  153. static void push_string(JSN_DATA *data, struct jsonsl_state_st *state) {
  154. luaL_Buffer b;
  155. luaL_buffinit(data->L, &b);
  156. int i;
  157. const char *c = get_state_buffer(data, state) + 1;
  158. for (i = 0; i < state->pos_cur - state->pos_begin - 1; i++) {
  159. int nc = c[i];
  160. if (nc == '\\') {
  161. i++;
  162. nc = c[i] & 255;
  163. switch (c[i]) {
  164. case 'b':
  165. nc = '\b';
  166. break;
  167. case 'f':
  168. nc = '\f';
  169. break;
  170. case 'n':
  171. nc = '\n';
  172. break;
  173. case 'r':
  174. nc = '\r';
  175. break;
  176. case 't':
  177. nc = '\t';
  178. break;
  179. case 'u':
  180. nc = fromhex(c[++i]) << 12;
  181. nc += fromhex(c[++i]) << 8;
  182. nc += fromhex(c[++i]) << 4;
  183. nc += fromhex(c[++i]) ;
  184. output_utf8(&b, nc);
  185. continue;
  186. }
  187. }
  188. luaL_putchar(&b, nc);
  189. }
  190. luaL_pushresult(&b);
  191. }
  192. static void
  193. cleanup_closing_element(jsonsl_t jsn,
  194. jsonsl_action_t action,
  195. struct jsonsl_state_st *state,
  196. const char *at)
  197. {
  198. JSN_DATA *data = (JSN_DATA *) jsn->data;
  199. DBG_PRINTF( "L%d: cc action %d state->type %s\n", state->level, action, jsonsl_strtype(state->type));
  200. DBG_PRINTF( "buf (%d - %d): '%.*s'\n", state->pos_begin, state->pos_cur, state->pos_cur - state->pos_begin, get_state_buffer(data, state));
  201. DBG_PRINTF( "at: '%s'\n", at);
  202. switch (state->type) {
  203. case JSONSL_T_HKEY:
  204. push_string(data, state);
  205. data->hkey_ref = lua_ref(data->L, 1);
  206. break;
  207. case JSONSL_T_STRING:
  208. lua_rawgeti(data->L, LUA_REGISTRYINDEX, get_parent_object_ref());
  209. if (data->hkey_ref == LUA_NOREF) {
  210. // list, so append
  211. lua_pushnumber(data->L, get_parent_object_used_count_pre_inc());
  212. } else {
  213. // object, so
  214. lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->hkey_ref);
  215. lua_unref(data->L, data->hkey_ref);
  216. data->hkey_ref = LUA_NOREF;
  217. }
  218. push_string(data, state);
  219. lua_settable(data->L, -3);
  220. lua_pop(data->L, 1);
  221. break;
  222. case JSONSL_T_SPECIAL:
  223. DBG_PRINTF("Special flags = 0x%x\n", state->special_flags);
  224. // need to deal with true/false/null
  225. if (state->special_flags & (JSONSL_SPECIALf_TRUE|JSONSL_SPECIALf_FALSE|JSONSL_SPECIALf_NUMERIC|JSONSL_SPECIALf_NULL)) {
  226. if (state->special_flags & JSONSL_SPECIALf_TRUE) {
  227. lua_pushboolean(data->L, 1);
  228. } else if (state->special_flags & JSONSL_SPECIALf_FALSE) {
  229. lua_pushboolean(data->L, 0);
  230. } else if (state->special_flags & JSONSL_SPECIALf_NULL) {
  231. DBG_PRINTF("Outputting null\n");
  232. lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->null_ref);
  233. } else if (state->special_flags & JSONSL_SPECIALf_NUMERIC) {
  234. push_number(data, state);
  235. }
  236. lua_rawgeti(data->L, LUA_REGISTRYINDEX, get_parent_object_ref());
  237. if (data->hkey_ref == LUA_NOREF) {
  238. // list, so append
  239. lua_pushnumber(data->L, get_parent_object_used_count_pre_inc());
  240. } else {
  241. // object, so
  242. lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->hkey_ref);
  243. lua_unref(data->L, data->hkey_ref);
  244. data->hkey_ref = LUA_NOREF;
  245. }
  246. lua_pushvalue(data->L, -3);
  247. lua_remove(data->L, -4);
  248. lua_settable(data->L, -3);
  249. lua_pop(data->L, 1);
  250. }
  251. break;
  252. case JSONSL_T_OBJECT:
  253. case JSONSL_T_LIST:
  254. lua_unref(data->L, state->lua_object_ref);
  255. state->lua_object_ref = LUA_NOREF;
  256. if (data->pos_ref != LUA_NOREF) {
  257. lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->pos_ref);
  258. lua_pushnumber(data->L, state->level);
  259. lua_pushnil(data->L);
  260. lua_settable(data->L, -3);
  261. lua_pop(data->L, 1);
  262. }
  263. if (state->level == 1) {
  264. data->complete = 1;
  265. }
  266. break;
  267. }
  268. }
  269. static int sjson_decoder_int(lua_State *L, int argno) {
  270. int nlevels = DEFAULT_DEPTH;
  271. if (lua_type(L, argno) == LUA_TTABLE) {
  272. lua_getfield(L, argno, "depth");
  273. nlevels = lua_tointeger(L, argno);
  274. if (nlevels == 0) {
  275. nlevels = DEFAULT_DEPTH;
  276. }
  277. if (nlevels < 4) {
  278. nlevels = 4;
  279. }
  280. if (nlevels > 1000) {
  281. nlevels = 1000;
  282. }
  283. lua_pop(L, 1);
  284. }
  285. JSN_DATA *data = (JSN_DATA *) lua_newuserdata(L, sizeof(JSN_DATA) + jsonsl_get_size(nlevels));
  286. //
  287. // Associate its metatable
  288. luaL_getmetatable(L, "sjson.decoder");
  289. lua_setmetatable(L, -2);
  290. jsonsl_t jsn = jsonsl_init((jsonsl_t) (data + 1), nlevels);
  291. int i;
  292. for (i = 0; i < jsn->levels_max; i++) {
  293. jsn->stack[i].lua_object_ref = LUA_NOREF;
  294. }
  295. data->jsn = jsn;
  296. data->result_ref = LUA_NOREF;
  297. data->null_ref = LUA_REFNIL;
  298. data->metatable = LUA_NOREF;
  299. data->hkey_ref = LUA_NOREF;
  300. data->pos_ref = LUA_NOREF;
  301. data->buffer_ref = LUA_NOREF;
  302. data->complete = 0;
  303. data->error = NULL;
  304. data->L = L;
  305. data->buffer_len = 0;
  306. data->min_needed = data->min_available = jsn->pos;
  307. lua_pushlightuserdata(L, 0);
  308. data->null_ref = lua_ref(L, 1);
  309. // This may throw...
  310. lua_newtable(L);
  311. data->result_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  312. if (lua_type(L, argno) == LUA_TTABLE) {
  313. luaL_unref(L, LUA_REGISTRYINDEX, data->null_ref);
  314. data->null_ref = LUA_NOREF;
  315. lua_getfield(L, argno, "null");
  316. data->null_ref = lua_ref(L, 1);
  317. lua_getfield(L, argno, "metatable");
  318. lua_pushvalue(L, -1);
  319. data->metatable = lua_ref(L, 1);
  320. if (lua_type(L, -1) != LUA_TNIL) {
  321. lua_getfield(L, -1, "checkpath");
  322. if (lua_type(L, -1) != LUA_TNIL) {
  323. lua_newtable(L);
  324. data->pos_ref = lua_ref(L, 1);
  325. }
  326. lua_pop(L, 1); // Throw away the checkpath value
  327. }
  328. lua_pop(L, 1); // Throw away the metatable
  329. }
  330. jsonsl_enable_all_callbacks(data->jsn);
  331. jsn->action_callback = NULL;
  332. jsn->action_callback_PUSH = create_new_element;
  333. jsn->action_callback_POP = cleanup_closing_element;
  334. jsn->error_callback = error_callback;
  335. jsn->data = data;
  336. jsn->max_callback_level = nlevels;
  337. return 1;
  338. }
  339. static int sjson_decoder(lua_State *L) {
  340. return sjson_decoder_int(L, 1);
  341. }
  342. static int sjson_decoder_result_int(lua_State *L, JSN_DATA *data) {
  343. if (!data->complete) {
  344. luaL_error(L, "decode not complete");
  345. }
  346. lua_rawgeti(L, LUA_REGISTRYINDEX, data->result_ref);
  347. lua_rawgeti(L, -1, 1);
  348. lua_remove(L, -2);
  349. return 1;
  350. }
  351. static int sjson_decoder_result(lua_State *L) {
  352. JSN_DATA *data = (JSN_DATA *)luaL_checkudata(L, 1, "sjson.decoder");
  353. return sjson_decoder_result_int(L, data);
  354. }
  355. static void sjson_free_working_data(lua_State *L, JSN_DATA *data) {
  356. jsonsl_t jsn = data->jsn;
  357. int i;
  358. for (i = 0; i < jsn->levels_max; i++) {
  359. luaL_unref(L, LUA_REGISTRYINDEX, jsn->stack[i].lua_object_ref);
  360. jsn->stack[i].lua_object_ref = LUA_NOREF;
  361. }
  362. luaL_unref(L, LUA_REGISTRYINDEX, data->metatable);
  363. data->metatable = LUA_NOREF;
  364. luaL_unref(L, LUA_REGISTRYINDEX, data->hkey_ref);
  365. data->hkey_ref = LUA_NOREF;
  366. luaL_unref(L, LUA_REGISTRYINDEX, data->null_ref);
  367. data->null_ref = LUA_NOREF;
  368. luaL_unref(L, LUA_REGISTRYINDEX, data->pos_ref);
  369. data->pos_ref = LUA_NOREF;
  370. luaL_unref(L, LUA_REGISTRYINDEX, data->buffer_ref);
  371. data->buffer_ref = LUA_NOREF;
  372. }
  373. static int sjson_decoder_write_int(lua_State *L, int udata_pos, int string_pos) {
  374. JSN_DATA *data = (JSN_DATA *)luaL_checkudata(L, udata_pos, "sjson.decoder");
  375. size_t len;
  376. const char *str = luaL_checklstring(L, string_pos, &len);
  377. if (data->error) {
  378. luaL_error(L, "JSON parse error: previous call");
  379. }
  380. if (!data->complete) {
  381. data->L = L;
  382. // Merge into any existing buffer and deal with discard
  383. if (data->buffer_ref != LUA_NOREF) {
  384. luaL_Buffer b;
  385. luaL_buffinit(L, &b);
  386. lua_rawgeti(L, LUA_REGISTRYINDEX, data->buffer_ref);
  387. size_t prev_len;
  388. const char *prev_buffer = luaL_checklstring(L, -1, &prev_len);
  389. lua_pop(L, 1); // But string still referenced so it cannot move
  390. int discard = data->min_needed - data->min_available;
  391. prev_buffer += discard;
  392. prev_len -= discard;
  393. if (prev_len > 0) {
  394. luaL_addlstring(&b, prev_buffer, prev_len);
  395. }
  396. data->min_available += discard;
  397. luaL_unref(L, LUA_REGISTRYINDEX, data->buffer_ref);
  398. data->buffer_ref = LUA_NOREF;
  399. lua_pushvalue(L, string_pos);
  400. luaL_addvalue(&b);
  401. luaL_pushresult(&b);
  402. } else {
  403. lua_pushvalue(L, string_pos);
  404. }
  405. size_t blen;
  406. data->buffer = luaL_checklstring(L, -1, &blen);
  407. data->buffer_len = blen;
  408. data->buffer_ref = lua_ref(L, 1);
  409. jsonsl_feed(data->jsn, str, len);
  410. if (data->error) {
  411. luaL_error(L, "JSON parse error: %s", data->error);
  412. }
  413. }
  414. if (data->complete) {
  415. // We no longer need the buffer
  416. sjson_free_working_data(L, data);
  417. return sjson_decoder_result_int(L, data);
  418. }
  419. return 0;
  420. }
  421. static int sjson_decoder_write(lua_State *L) {
  422. return sjson_decoder_write_int(L, 1, 2);
  423. }
  424. static int sjson_decode(lua_State *L) {
  425. int push_count = sjson_decoder_int(L, 2);
  426. if (push_count != 1) {
  427. luaL_error(L, "Internal error in sjson.deocder");
  428. }
  429. luaL_checkudata(L, -1, "sjson.decoder");
  430. push_count = sjson_decoder_write_int(L, -1, 1);
  431. if (push_count != 1) {
  432. luaL_error(L, "Incomplete JSON object passed to sjson.decode");
  433. }
  434. // Now we have two items on the stack -- the udata and the result
  435. lua_remove(L, -2);
  436. return 1;
  437. }
  438. static int sjson_decoder_destructor(lua_State *L) {
  439. JSN_DATA *data = (JSN_DATA *)luaL_checkudata(L, 1, "sjson.decoder");
  440. sjson_free_working_data(L, data);
  441. data->jsn = NULL;
  442. luaL_unref(L, LUA_REGISTRYINDEX, data->result_ref);
  443. data->result_ref = LUA_NOREF;
  444. DBG_PRINTF("Destructor called\n");
  445. return 0;
  446. }
  447. //
  448. //--------------------------------- ENCODER BELOW
  449. //
  450. //
  451. //
  452. //#undef DBG_PRINTF
  453. //#define DBG_PRINTF printf
  454. typedef struct {
  455. int lua_object_ref;
  456. // for arrays
  457. // 0 -> [
  458. // 1 -> first element
  459. // 2 -> ,
  460. // 3 -> second element
  461. // 4 -> ]
  462. // for objects
  463. // 0 -> { firstkey :
  464. // 1 -> first value
  465. // 2 -> , secondkey :
  466. // 3 -> second value
  467. // 4 -> }
  468. short offset;
  469. // -1 for objects
  470. // 0 -> n maximum integer key = n
  471. short size;
  472. int lua_key_ref;
  473. } ENC_DATA_STATE;
  474. typedef struct {
  475. ENC_DATA_STATE *stack;
  476. int nlevels;
  477. int level;
  478. int current_str_ref;
  479. int null_ref;
  480. int offset;
  481. } ENC_DATA;
  482. static int sjson_encoder_get_table_size(lua_State *L, int argno) {
  483. // Returns -1 for object, otherwise the maximum integer key value found.
  484. lua_pushvalue(L, argno);
  485. // stack now contains: -1 => table
  486. lua_pushnil(L);
  487. // stack now contains: -1 => nil; -2 => table
  488. //
  489. int maxkey = 0;
  490. while (lua_next(L, -2)) {
  491. lua_pop(L, 1);
  492. // stack now contains: -1 => key; -2 => table
  493. if (lua_type(L, -1) == LUA_TNUMBER) {
  494. int val = lua_tointeger(L, -1);
  495. if (val > maxkey) {
  496. maxkey = val;
  497. } else if (val <= 0) {
  498. maxkey = -1;
  499. lua_pop(L, 1);
  500. break;
  501. }
  502. } else {
  503. maxkey = -1;
  504. lua_pop(L, 1);
  505. break;
  506. }
  507. }
  508. lua_pop(L, 1);
  509. return maxkey;
  510. }
  511. static void enc_pop_stack(lua_State *L, ENC_DATA *data) {
  512. if (data->level < 0) {
  513. luaL_error(L, "encoder stack underflow");
  514. }
  515. ENC_DATA_STATE *state = &data->stack[data->level];
  516. lua_unref(L, state->lua_object_ref);
  517. state->lua_object_ref = LUA_NOREF;
  518. lua_unref(L, state->lua_key_ref);
  519. state->lua_key_ref = LUA_REFNIL;
  520. data->level--;
  521. }
  522. static void enc_push_stack(lua_State *L, ENC_DATA *data, int argno) {
  523. if (++data->level >= data->nlevels) {
  524. luaL_error(L, "encoder stack overflow");
  525. }
  526. lua_pushvalue(L, argno);
  527. ENC_DATA_STATE *state = &data->stack[data->level];
  528. state->lua_object_ref = lua_ref(L, 1);
  529. state->size = sjson_encoder_get_table_size(L, argno);
  530. state->offset = 0; // We haven't started on this one yet
  531. }
  532. static int sjson_encoder(lua_State *L) {
  533. int nlevels = DEFAULT_DEPTH;
  534. int argno = 1;
  535. // Validate first arg is a table
  536. luaL_checktype(L, argno++, LUA_TTABLE);
  537. if (lua_type(L, argno) == LUA_TTABLE) {
  538. lua_getfield(L, argno, "depth");
  539. nlevels = lua_tointeger(L, argno);
  540. if (nlevels == 0) {
  541. nlevels = DEFAULT_DEPTH;
  542. }
  543. if (nlevels < 4) {
  544. nlevels = 4;
  545. }
  546. if (nlevels > 1000) {
  547. nlevels = 1000;
  548. }
  549. lua_pop(L, 1);
  550. }
  551. ENC_DATA *data = (ENC_DATA *) lua_newuserdata(L, sizeof(ENC_DATA) + nlevels * sizeof(ENC_DATA_STATE));
  552. // Associate its metatable
  553. luaL_getmetatable(L, "sjson.encoder");
  554. lua_setmetatable(L, -2);
  555. data->nlevels = nlevels;
  556. data->level = -1;
  557. data->stack = (ENC_DATA_STATE *) (data + 1);
  558. data->current_str_ref = LUA_NOREF;
  559. int i;
  560. for (i = 0; i < nlevels; i++) {
  561. data->stack[i].lua_object_ref = LUA_NOREF;
  562. data->stack[i].lua_key_ref = LUA_REFNIL;
  563. }
  564. enc_push_stack(L, data, 1);
  565. data->null_ref = LUA_REFNIL;
  566. if (lua_type(L, argno) == LUA_TTABLE) {
  567. luaL_unref(L, LUA_REGISTRYINDEX, data->null_ref);
  568. data->null_ref = LUA_NOREF;
  569. lua_getfield(L, argno, "null");
  570. data->null_ref = lua_ref(L, 1);
  571. }
  572. return 1;
  573. }
  574. static void encode_lua_object(lua_State *L, ENC_DATA *data, int argno, const char *prefix, const char *suffix) {
  575. luaL_Buffer b;
  576. luaL_buffinit(L, &b);
  577. luaL_addstring(&b, prefix);
  578. int type = lua_type(L, argno);
  579. if (type == LUA_TSTRING) {
  580. // Check to see if it is the NULL value
  581. if (data->null_ref != LUA_REFNIL) {
  582. lua_rawgeti(L, LUA_REGISTRYINDEX, data->null_ref);
  583. if (lua_equal(L, -1, -2)) {
  584. type = LUA_TNIL;
  585. }
  586. lua_pop(L, 1);
  587. }
  588. }
  589. switch (type) {
  590. default:
  591. luaL_error(L, "Cannot encode type %d", type);
  592. break;
  593. case LUA_TLIGHTUSERDATA:
  594. case LUA_TNIL:
  595. luaL_addstring(&b, "null");
  596. break;
  597. case LUA_TBOOLEAN:
  598. luaL_addstring(&b, lua_toboolean(L, argno) ? "true" : "false");
  599. break;
  600. case LUA_TNUMBER:
  601. {
  602. lua_pushvalue(L, argno);
  603. size_t len;
  604. const char *str = lua_tolstring(L, -1, &len);
  605. char value[len + 1];
  606. strcpy(value, str);
  607. lua_pop(L, 1);
  608. luaL_addstring(&b, value);
  609. break;
  610. }
  611. case LUA_TSTRING:
  612. {
  613. luaL_addchar(&b, '"');
  614. size_t len;
  615. const char *str = lua_tolstring(L, argno, &len);
  616. while (len > 0) {
  617. if ((*str & 0xff) < 0x20) {
  618. char value[8];
  619. value[0] = '\\';
  620. char *d = value + 1;
  621. switch(*str) {
  622. case '\f':
  623. *d++ = 'f';
  624. break;
  625. case '\n':
  626. *d++ = 'n';
  627. break;
  628. case '\t':
  629. *d++ = 't';
  630. break;
  631. case '\r':
  632. *d++ = 'r';
  633. break;
  634. case '\b':
  635. *d++ = 'b';
  636. break;
  637. default:
  638. *d++ = 'u';
  639. *d++ = '0';
  640. *d++ = '0';
  641. *d++ = "0123456789abcdef"[(*str >> 4) & 0xf];
  642. *d++ = "0123456789abcdef"[(*str ) & 0xf];
  643. break;
  644. }
  645. *d = '\0';
  646. luaL_addstring(&b, value);
  647. } else {
  648. luaL_addchar(&b, *str);
  649. }
  650. str++;
  651. len--;
  652. }
  653. luaL_addchar(&b, '"');
  654. break;
  655. }
  656. }
  657. luaL_addstring(&b, suffix);
  658. luaL_pushresult(&b);
  659. }
  660. static int sjson_encoder_next_value_is_table(lua_State *L) {
  661. int count = 10;
  662. while ((lua_type(L, -1) == LUA_TFUNCTION
  663. #ifdef LUA_TLIGHTFUNCTION
  664. || lua_type(L, -1) == LUA_TLIGHTFUNCTION
  665. #endif
  666. ) && count-- > 0) {
  667. // call it and use the return value
  668. lua_call(L, 0, 1); // Expecting replacement value
  669. }
  670. return (lua_type(L, -1) == LUA_TTABLE);
  671. }
  672. static void sjson_encoder_make_next_chunk(lua_State *L, ENC_DATA *data) {
  673. if (data->level < 0) {
  674. return;
  675. }
  676. luaL_Buffer b;
  677. luaL_buffinit(L, &b);
  678. // Ending condition
  679. while (data->level >= 0 && !b.lvl) {
  680. ENC_DATA_STATE *state = &data->stack[data->level];
  681. int finished = 0;
  682. if (state->size >= 0) {
  683. if (state->offset == 0) {
  684. // start of object or whatever
  685. luaL_addchar(&b, '[');
  686. }
  687. if (state->offset == state->size << 1) {
  688. luaL_addchar(&b, ']');
  689. finished = 1;
  690. } else if ((state->offset & 1) == 0) {
  691. if (state->offset > 0) {
  692. luaL_addchar(&b, ',');
  693. }
  694. } else {
  695. // output the value
  696. lua_rawgeti(L, LUA_REGISTRYINDEX, state->lua_object_ref);
  697. lua_rawgeti(L, -1, (state->offset >> 1) + 1);
  698. if (sjson_encoder_next_value_is_table(L)) {
  699. enc_push_stack(L, data, -1);
  700. lua_pop(L, 2);
  701. state->offset++;
  702. continue;
  703. }
  704. encode_lua_object(L, data, -1, "", "");
  705. lua_remove(L, -2);
  706. lua_remove(L, -2);
  707. luaL_addvalue(&b);
  708. }
  709. state->offset++;
  710. } else {
  711. lua_rawgeti(L, LUA_REGISTRYINDEX, state->lua_object_ref);
  712. // stack now contains: -1 => table
  713. lua_rawgeti(L, LUA_REGISTRYINDEX, state->lua_key_ref);
  714. // stack now contains: -1 => nil or key; -2 => table
  715. if (lua_next(L, -2)) {
  716. // save the key
  717. if (state->offset & 1) {
  718. lua_unref(L, state->lua_key_ref);
  719. state->lua_key_ref = LUA_NOREF;
  720. // Duplicate the key
  721. lua_pushvalue(L, -2);
  722. state->lua_key_ref = lua_ref(L, 1);
  723. }
  724. if ((state->offset & 1) == 0) {
  725. // copy the key so that lua_tostring does not modify the original
  726. lua_pushvalue(L, -2);
  727. // stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
  728. // key
  729. lua_tostring(L, -1);
  730. encode_lua_object(L, data, -1, state->offset ? "," : "{", ":");
  731. lua_remove(L, -2);
  732. lua_remove(L, -2);
  733. lua_remove(L, -2);
  734. lua_remove(L, -2);
  735. } else {
  736. if (sjson_encoder_next_value_is_table(L)) {
  737. enc_push_stack(L, data, -1);
  738. lua_pop(L, 3);
  739. state->offset++;
  740. continue;
  741. }
  742. encode_lua_object(L, data, -1, "", "");
  743. lua_remove(L, -2);
  744. lua_remove(L, -2);
  745. lua_remove(L, -2);
  746. }
  747. luaL_addvalue(&b);
  748. } else {
  749. lua_pop(L, 1);
  750. // We have got to the end
  751. luaL_addchar(&b, '}');
  752. finished = 1;
  753. }
  754. state->offset++;
  755. }
  756. if (finished) {
  757. enc_pop_stack(L, data);
  758. }
  759. }
  760. luaL_pushresult(&b);
  761. data->current_str_ref = lua_ref(L, 1);
  762. data->offset = 0;
  763. }
  764. static int sjson_encoder_read_int(lua_State *L, ENC_DATA *data, int readsize) {
  765. luaL_Buffer b;
  766. luaL_buffinit(L, &b);
  767. size_t len;
  768. do {
  769. // Fill the buffer with (up to) readsize characters
  770. if (data->current_str_ref != LUA_NOREF) {
  771. // this is not allowed
  772. lua_rawgeti(L, LUA_REGISTRYINDEX, data->current_str_ref);
  773. const char *str = lua_tolstring(L, -1, &len);
  774. lua_pop(L, 1); // Note that we still have the string referenced so it can't go away
  775. int amnt = len - data->offset;;
  776. if (amnt > readsize) {
  777. amnt = readsize;
  778. }
  779. luaL_addlstring(&b, str + data->offset, amnt);
  780. data->offset += amnt;
  781. readsize -= amnt;
  782. if (data->offset == len) {
  783. lua_unref(L, data->current_str_ref);
  784. data->current_str_ref = LUA_NOREF;
  785. }
  786. }
  787. if (readsize > 0) {
  788. // Make the next chunk
  789. sjson_encoder_make_next_chunk(L, data);
  790. }
  791. } while (readsize > 0 && data->current_str_ref != LUA_NOREF);
  792. luaL_pushresult(&b);
  793. lua_tolstring(L, -1, &len);
  794. if (len == 0) {
  795. // we have got to the end
  796. lua_pop(L, 1);
  797. return 0;
  798. }
  799. return 1;
  800. }
  801. static int sjson_encoder_read(lua_State *L) {
  802. ENC_DATA *data = (ENC_DATA *)luaL_checkudata(L, 1, "sjson.encoder");
  803. int readsize = 1024;
  804. if (lua_type(L, 2) == LUA_TNUMBER) {
  805. readsize = lua_tointeger(L, 2);
  806. if (readsize < 1) {
  807. readsize = 1;
  808. }
  809. }
  810. return sjson_encoder_read_int(L, data, readsize);
  811. }
  812. static int sjson_encode(lua_State *L) {
  813. sjson_encoder(L);
  814. ENC_DATA *data = (ENC_DATA *)luaL_checkudata(L, -1, "sjson.encoder");
  815. int rc = sjson_encoder_read_int(L, data, 1000000);
  816. lua_remove(L, -(rc + 1));
  817. return rc;
  818. }
  819. static int sjson_encoder_destructor(lua_State *L) {
  820. ENC_DATA *data = (ENC_DATA *)luaL_checkudata(L, 1, "sjson.encoder");
  821. int i;
  822. for (i = 0; i < data->nlevels; i++) {
  823. luaL_unref(L, LUA_REGISTRYINDEX, data->stack[i].lua_object_ref);
  824. luaL_unref(L, LUA_REGISTRYINDEX, data->stack[i].lua_key_ref);
  825. }
  826. luaL_unref(L, LUA_REGISTRYINDEX, data->null_ref);
  827. luaL_unref(L, LUA_REGISTRYINDEX, data->current_str_ref);
  828. DBG_PRINTF("Destructor called\n");
  829. return 0;
  830. }
  831. #ifdef LOCAL_LUA
  832. static const luaL_Reg sjson_encoder_map[] = {
  833. { "read", sjson_encoder_read },
  834. { "__gc", sjson_encoder_destructor },
  835. { NULL, NULL }
  836. };
  837. static const luaL_Reg sjson_decoder_map[] = {
  838. { "write", sjson_decoder_write },
  839. { "result", sjson_decoder_result },
  840. { "__gc", sjson_decoder_destructor },
  841. { NULL, NULL }
  842. };
  843. static const luaL_Reg sjsonlib[] = {
  844. { "decode", sjson_decode },
  845. { "decoder", sjson_decoder },
  846. { "encode", sjson_encode },
  847. { "encoder", sjson_encoder },
  848. {NULL, NULL}
  849. };
  850. #else
  851. static const LUA_REG_TYPE sjson_encoder_map[] = {
  852. { LSTRKEY( "read" ), LFUNCVAL( sjson_encoder_read ) },
  853. { LSTRKEY( "__gc" ), LFUNCVAL( sjson_encoder_destructor ) },
  854. { LSTRKEY( "__index" ), LROVAL( sjson_encoder_map ) },
  855. { LNILKEY, LNILVAL }
  856. };
  857. static const LUA_REG_TYPE sjson_decoder_map[] = {
  858. { LSTRKEY( "write" ), LFUNCVAL( sjson_decoder_write ) },
  859. { LSTRKEY( "result" ), LFUNCVAL( sjson_decoder_result ) },
  860. { LSTRKEY( "__gc" ), LFUNCVAL( sjson_decoder_destructor ) },
  861. { LSTRKEY( "__index" ), LROVAL( sjson_decoder_map ) },
  862. { LNILKEY, LNILVAL }
  863. };
  864. static const LUA_REG_TYPE sjson_map[] = {
  865. { LSTRKEY( "encode" ), LFUNCVAL( sjson_encode ) },
  866. { LSTRKEY( "decode" ), LFUNCVAL( sjson_decode ) },
  867. { LSTRKEY( "encoder" ), LFUNCVAL( sjson_encoder ) },
  868. { LSTRKEY( "decoder" ), LFUNCVAL( sjson_decoder ) },
  869. { LSTRKEY( "NULL" ), LUDATA( 0 ) },
  870. { LNILKEY, LNILVAL }
  871. };
  872. #endif
  873. LUALIB_API int luaopen_sjson (lua_State *L) {
  874. #ifdef LOCAL_LUA
  875. luaL_register(L, LUA_SJSONLIBNAME, sjsonlib);
  876. lua_getglobal(L, LUA_SJSONLIBNAME);
  877. lua_pushstring(L, "NULL");
  878. lua_pushlightuserdata(L, 0);
  879. lua_settable(L, -3);
  880. lua_pop(L, 1);
  881. luaL_newmetatable(L, "sjson.encoder");
  882. luaL_register(L, NULL, sjson_encoder_map);
  883. lua_setfield(L, -1, "__index");
  884. luaL_newmetatable(L, "sjson.decoder");
  885. luaL_register(L, NULL, sjson_decoder_map);
  886. lua_setfield(L, -1, "__index");
  887. #else
  888. luaL_rometatable(L, "sjson.decoder", (void *)sjson_decoder_map);
  889. luaL_rometatable(L, "sjson.encoder", (void *)sjson_encoder_map);
  890. #endif
  891. return 1;
  892. }
  893. #ifndef LOCAL_LUA
  894. NODEMCU_MODULE(SJSON, "sjson", sjson_map, luaopen_sjson);
  895. #endif