sjson.c 27 KB

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