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