sjson.c 27 KB

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