sjson.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 if (*str == '"') {
  649. luaL_addstring(&b, "\\\"");
  650. } else {
  651. luaL_addchar(&b, *str);
  652. }
  653. str++;
  654. len--;
  655. }
  656. luaL_addchar(&b, '"');
  657. break;
  658. }
  659. }
  660. luaL_addstring(&b, suffix);
  661. luaL_pushresult(&b);
  662. }
  663. static int sjson_encoder_next_value_is_table(lua_State *L) {
  664. int count = 10;
  665. while ((lua_type(L, -1) == LUA_TFUNCTION
  666. #ifdef LUA_TLIGHTFUNCTION
  667. || lua_type(L, -1) == LUA_TLIGHTFUNCTION
  668. #endif
  669. ) && count-- > 0) {
  670. // call it and use the return value
  671. lua_call(L, 0, 1); // Expecting replacement value
  672. }
  673. return (lua_type(L, -1) == LUA_TTABLE);
  674. }
  675. static void sjson_encoder_make_next_chunk(lua_State *L, ENC_DATA *data) {
  676. if (data->level < 0) {
  677. return;
  678. }
  679. luaL_Buffer b;
  680. luaL_buffinit(L, &b);
  681. // Ending condition
  682. while (data->level >= 0 && !b.lvl) {
  683. ENC_DATA_STATE *state = &data->stack[data->level];
  684. int finished = 0;
  685. if (state->size >= 0) {
  686. if (state->offset == 0) {
  687. // start of object or whatever
  688. luaL_addchar(&b, '[');
  689. }
  690. if (state->offset == state->size << 1) {
  691. luaL_addchar(&b, ']');
  692. finished = 1;
  693. } else if ((state->offset & 1) == 0) {
  694. if (state->offset > 0) {
  695. luaL_addchar(&b, ',');
  696. }
  697. } else {
  698. // output the value
  699. lua_rawgeti(L, LUA_REGISTRYINDEX, state->lua_object_ref);
  700. lua_rawgeti(L, -1, (state->offset >> 1) + 1);
  701. if (sjson_encoder_next_value_is_table(L)) {
  702. enc_push_stack(L, data, -1);
  703. lua_pop(L, 2);
  704. state->offset++;
  705. continue;
  706. }
  707. encode_lua_object(L, data, -1, "", "");
  708. lua_remove(L, -2);
  709. lua_remove(L, -2);
  710. luaL_addvalue(&b);
  711. }
  712. state->offset++;
  713. } else {
  714. lua_rawgeti(L, LUA_REGISTRYINDEX, state->lua_object_ref);
  715. // stack now contains: -1 => table
  716. lua_rawgeti(L, LUA_REGISTRYINDEX, state->lua_key_ref);
  717. // stack now contains: -1 => nil or key; -2 => table
  718. if (lua_next(L, -2)) {
  719. // save the key
  720. if (state->offset & 1) {
  721. lua_unref(L, state->lua_key_ref);
  722. state->lua_key_ref = LUA_NOREF;
  723. // Duplicate the key
  724. lua_pushvalue(L, -2);
  725. state->lua_key_ref = lua_ref(L, 1);
  726. }
  727. if ((state->offset & 1) == 0) {
  728. // copy the key so that lua_tostring does not modify the original
  729. lua_pushvalue(L, -2);
  730. // stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
  731. // key
  732. lua_tostring(L, -1);
  733. encode_lua_object(L, data, -1, state->offset ? "," : "{", ":");
  734. lua_remove(L, -2);
  735. lua_remove(L, -2);
  736. lua_remove(L, -2);
  737. lua_remove(L, -2);
  738. } else {
  739. if (sjson_encoder_next_value_is_table(L)) {
  740. enc_push_stack(L, data, -1);
  741. lua_pop(L, 3);
  742. state->offset++;
  743. continue;
  744. }
  745. encode_lua_object(L, data, -1, "", "");
  746. lua_remove(L, -2);
  747. lua_remove(L, -2);
  748. lua_remove(L, -2);
  749. }
  750. luaL_addvalue(&b);
  751. } else {
  752. lua_pop(L, 1);
  753. // We have got to the end
  754. luaL_addchar(&b, '}');
  755. finished = 1;
  756. }
  757. state->offset++;
  758. }
  759. if (finished) {
  760. enc_pop_stack(L, data);
  761. }
  762. }
  763. luaL_pushresult(&b);
  764. data->current_str_ref = lua_ref(L, 1);
  765. data->offset = 0;
  766. }
  767. static int sjson_encoder_read_int(lua_State *L, ENC_DATA *data, int readsize) {
  768. luaL_Buffer b;
  769. luaL_buffinit(L, &b);
  770. size_t len;
  771. do {
  772. // Fill the buffer with (up to) readsize characters
  773. if (data->current_str_ref != LUA_NOREF) {
  774. // this is not allowed
  775. lua_rawgeti(L, LUA_REGISTRYINDEX, data->current_str_ref);
  776. const char *str = lua_tolstring(L, -1, &len);
  777. lua_pop(L, 1); // Note that we still have the string referenced so it can't go away
  778. int amnt = len - data->offset;;
  779. if (amnt > readsize) {
  780. amnt = readsize;
  781. }
  782. luaL_addlstring(&b, str + data->offset, amnt);
  783. data->offset += amnt;
  784. readsize -= amnt;
  785. if (data->offset == len) {
  786. lua_unref(L, data->current_str_ref);
  787. data->current_str_ref = LUA_NOREF;
  788. }
  789. }
  790. if (readsize > 0) {
  791. // Make the next chunk
  792. sjson_encoder_make_next_chunk(L, data);
  793. }
  794. } while (readsize > 0 && data->current_str_ref != LUA_NOREF);
  795. luaL_pushresult(&b);
  796. lua_tolstring(L, -1, &len);
  797. if (len == 0) {
  798. // we have got to the end
  799. lua_pop(L, 1);
  800. return 0;
  801. }
  802. return 1;
  803. }
  804. static int sjson_encoder_read(lua_State *L) {
  805. ENC_DATA *data = (ENC_DATA *)luaL_checkudata(L, 1, "sjson.encoder");
  806. int readsize = 1024;
  807. if (lua_type(L, 2) == LUA_TNUMBER) {
  808. readsize = lua_tointeger(L, 2);
  809. if (readsize < 1) {
  810. readsize = 1;
  811. }
  812. }
  813. return sjson_encoder_read_int(L, data, readsize);
  814. }
  815. static int sjson_encode(lua_State *L) {
  816. sjson_encoder(L);
  817. ENC_DATA *data = (ENC_DATA *)luaL_checkudata(L, -1, "sjson.encoder");
  818. int rc = sjson_encoder_read_int(L, data, 1000000);
  819. lua_remove(L, -(rc + 1));
  820. return rc;
  821. }
  822. static int sjson_encoder_destructor(lua_State *L) {
  823. ENC_DATA *data = (ENC_DATA *)luaL_checkudata(L, 1, "sjson.encoder");
  824. int i;
  825. for (i = 0; i < data->nlevels; i++) {
  826. luaL_unref(L, LUA_REGISTRYINDEX, data->stack[i].lua_object_ref);
  827. luaL_unref(L, LUA_REGISTRYINDEX, data->stack[i].lua_key_ref);
  828. }
  829. luaL_unref(L, LUA_REGISTRYINDEX, data->null_ref);
  830. luaL_unref(L, LUA_REGISTRYINDEX, data->current_str_ref);
  831. DBG_PRINTF("Destructor called\n");
  832. return 0;
  833. }
  834. #ifdef LOCAL_LUA
  835. static const luaL_Reg sjson_encoder_map[] = {
  836. { "read", sjson_encoder_read },
  837. { "__gc", sjson_encoder_destructor },
  838. { NULL, NULL }
  839. };
  840. static const luaL_Reg sjson_decoder_map[] = {
  841. { "write", sjson_decoder_write },
  842. { "result", sjson_decoder_result },
  843. { "__gc", sjson_decoder_destructor },
  844. { NULL, NULL }
  845. };
  846. static const luaL_Reg sjsonlib[] = {
  847. { "decode", sjson_decode },
  848. { "decoder", sjson_decoder },
  849. { "encode", sjson_encode },
  850. { "encoder", sjson_encoder },
  851. {NULL, NULL}
  852. };
  853. #else
  854. static const LUA_REG_TYPE sjson_encoder_map[] = {
  855. { LSTRKEY( "read" ), LFUNCVAL( sjson_encoder_read ) },
  856. { LSTRKEY( "__gc" ), LFUNCVAL( sjson_encoder_destructor ) },
  857. { LSTRKEY( "__index" ), LROVAL( sjson_encoder_map ) },
  858. { LNILKEY, LNILVAL }
  859. };
  860. static const LUA_REG_TYPE sjson_decoder_map[] = {
  861. { LSTRKEY( "write" ), LFUNCVAL( sjson_decoder_write ) },
  862. { LSTRKEY( "result" ), LFUNCVAL( sjson_decoder_result ) },
  863. { LSTRKEY( "__gc" ), LFUNCVAL( sjson_decoder_destructor ) },
  864. { LSTRKEY( "__index" ), LROVAL( sjson_decoder_map ) },
  865. { LNILKEY, LNILVAL }
  866. };
  867. static const LUA_REG_TYPE sjson_map[] = {
  868. { LSTRKEY( "encode" ), LFUNCVAL( sjson_encode ) },
  869. { LSTRKEY( "decode" ), LFUNCVAL( sjson_decode ) },
  870. { LSTRKEY( "encoder" ), LFUNCVAL( sjson_encoder ) },
  871. { LSTRKEY( "decoder" ), LFUNCVAL( sjson_decoder ) },
  872. { LSTRKEY( "NULL" ), LUDATA( 0 ) },
  873. { LNILKEY, LNILVAL }
  874. };
  875. #endif
  876. LUALIB_API int luaopen_sjson (lua_State *L) {
  877. #ifdef LOCAL_LUA
  878. luaL_register(L, LUA_SJSONLIBNAME, sjsonlib);
  879. lua_getglobal(L, LUA_SJSONLIBNAME);
  880. lua_pushstring(L, "NULL");
  881. lua_pushlightuserdata(L, 0);
  882. lua_settable(L, -3);
  883. lua_pop(L, 1);
  884. luaL_newmetatable(L, "sjson.encoder");
  885. luaL_register(L, NULL, sjson_encoder_map);
  886. lua_setfield(L, -1, "__index");
  887. luaL_newmetatable(L, "sjson.decoder");
  888. luaL_register(L, NULL, sjson_decoder_map);
  889. lua_setfield(L, -1, "__index");
  890. #else
  891. luaL_rometatable(L, "sjson.decoder", (void *)sjson_decoder_map);
  892. luaL_rometatable(L, "sjson.encoder", (void *)sjson_encoder_map);
  893. #endif
  894. return 1;
  895. }
  896. #ifndef LOCAL_LUA
  897. NODEMCU_MODULE(SJSON, "sjson", sjson_map, luaopen_sjson);
  898. #endif