OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1025 lines
35 KiB

  1. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  2. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. #
  4. # Filename: package/.../lua-yajl/lua-yajl-2.0-upstream-fixes.patch
  5. # Copyright (C) 2013 The OpenSDE Project
  6. #
  7. # More information can be found in the files COPYING and README.
  8. #
  9. # This patch file is dual-licensed. It is available under the license the
  10. # patched project is licensed under, as long as it is an OpenSource license
  11. # as defined at http://www.opensource.org/ (e.g. BSD, X11) or under the terms
  12. # of the GNU General Public License as published by the Free Software
  13. # Foundation; either version 2 of the License, or (at your option) any later
  14. # version.
  15. # --- SDE-COPYRIGHT-NOTE-END ---
  16. From 3f0f323d16d31934e5b98e8d1440c0e05edf0c61 Mon Sep 17 00:00:00 2001
  17. From: Brian Maher <brian@brimworks.com>
  18. Date: Tue, 18 Oct 2011 06:43:17 -0700
  19. Subject: [PATCH] Clean-up warnings: Remove C++ comments, declare variables at
  20. the beginning of the block, return a value from
  21. got_array_value(), remove unused variable declarations.
  22. ---
  23. lua_yajl.c | 231 +++++++++++++++++++++++++++---------------------------------
  24. 1 file changed, 104 insertions(+), 127 deletions(-)
  25. diff --git a/lua_yajl.c b/lua_yajl.c
  26. index 5cb7bea..fc2e7a0 100644
  27. --- a/lua_yajl.c
  28. +++ b/lua_yajl.c
  29. @@ -26,13 +26,13 @@ static int got_map_key(lua_State* L);
  30. static int got_map_value(lua_State* L);
  31. -//////////////////////////////////////////////////////////////////////
  32. static double todouble(lua_State* L, const char* val, size_t len) {
  33. - // Convert into number using a temporary:
  34. + /* Convert into number using a temporary */
  35. char* tmp = (char*)lua_newuserdata(L, len+1);
  36. + double num;
  37. memcpy(tmp, val, len);
  38. tmp[len] = '\0';
  39. - double num = strtod(tmp, NULL);
  40. + num = strtod(tmp, NULL);
  41. /*
  42. if ((num == HUGE_VAL || num == -HUGE_VAL) &&
  43. errno == ERANGE)
  44. @@ -48,36 +48,37 @@ static double todouble(lua_State* L, const char* val, size_t len) {
  45. }
  46. -//////////////////////////////////////////////////////////////////////
  47. static int js_to_string(lua_State *L) {
  48. + yajl_gen* gen;
  49. + const unsigned char *buf;
  50. + size_t len;
  51. +
  52. lua_pushcfunction(L, js_generator);
  53. - // convert_me, {extra}, ?, js_gen
  54. + /* convert_me, {extra}, ?, js_gen */
  55. if ( lua_istable(L, 2) ) {
  56. - // Be sure printer is not defined:
  57. + /* Be sure printer is not defined: */
  58. lua_pushliteral(L, "printer");
  59. lua_pushnil(L);
  60. lua_rawset(L, 2);
  61. lua_pushvalue(L, 2);
  62. - // convert_me, {extra}, ?, js_gen, {extra}
  63. + /* convert_me, {extra}, ?, js_gen, {extra} */
  64. } else {
  65. lua_newtable(L);
  66. - // convert_me, {extra}, ?, js_gen, {}
  67. + /* convert_me, {extra}, ?, js_gen, {} */
  68. }
  69. lua_call(L, 1, 1);
  70. - // convert_me, {extra}, ?, gen_ud
  71. + /* convert_me, {extra}, ?, gen_ud */
  72. lua_pushcfunction(L, js_generator_value);
  73. - // convert_me, {extra}, ?, gen_ud, js_gen_val
  74. + /* convert_me, {extra}, ?, gen_ud, js_gen_val */
  75. lua_pushvalue(L, -2);
  76. - // convert_me, {extra}, ?, gen_ud, js_gen_val, gen_ud
  77. + /* convert_me, {extra}, ?, gen_ud, js_gen_val, gen_ud */
  78. lua_pushvalue(L, 1);
  79. - // convert_me, {extra}, ?, gen_ud, js_gen_val, gen_ud, convert_me
  80. + /* convert_me, {extra}, ?, gen_ud, js_gen_val, gen_ud, convert_me */
  81. lua_call(L, 2, 0);
  82. - // convert_me, {extra}, ?, gen_ud
  83. - yajl_gen* gen = js_check_generator(L, -1);
  84. - const unsigned char *buf;
  85. - size_t len;
  86. + /* convert_me, {extra}, ?, gen_ud */
  87. + gen = js_check_generator(L, -1);
  88. yajl_gen_get_buf(*gen, &buf, &len);
  89. - // Copy into results:
  90. + /* Copy into results: */
  91. lua_pushlstring(L, (char*)buf, len);
  92. yajl_gen_clear(*gen);
  93. return 1;
  94. @@ -124,10 +125,10 @@ static int got_map_value(lua_State* L) {
  95. lua_insert(L, -2);
  96. lua_pop(L, 1);
  97. lua_rawset(L, -3);
  98. - lua_pushnil(L); // Store future key here.
  99. + lua_pushnil(L); /* Store future key here. */
  100. lua_pushcfunction(L, got_map_key);
  101. - return 0; // Ignored.
  102. + return 0; /* Ignored. */
  103. }
  104. static int got_map_key(lua_State* L) {
  105. @@ -135,7 +136,7 @@ static int got_map_key(lua_State* L) {
  106. lua_pop(L, 1);
  107. lua_pushcfunction(L, got_map_value);
  108. - return 0; // Ignored.
  109. + return 0; /* Ignored. */
  110. }
  111. static int got_array_value(lua_State* L) {
  112. @@ -143,6 +144,8 @@ static int got_array_value(lua_State* L) {
  113. lua_rawseti(L, -4, lua_tointeger(L, -3));
  114. lua_pushinteger(L, lua_tointeger(L, -2)+1);
  115. lua_replace(L, -3);
  116. +
  117. + return 0; /* Ignored. */
  118. }
  119. static int to_value_start_map(void* ctx) {
  120. @@ -155,13 +158,13 @@ static int to_value_start_map(void* ctx) {
  121. - Value pushed on the stack by to_value_* functions.
  122. */
  123. if ( ! lua_checkstack(L, 4) ) {
  124. - // TODO: So far, YAJL seems to be fine with the longjmp, but
  125. - // perhaps we should do errors a different way?
  126. + /* TODO: So far, YAJL seems to be fine with the longjmp, but
  127. + perhaps we should do errors a different way? */
  128. return luaL_error(L, "lua stack overflow");
  129. }
  130. lua_newtable(L);
  131. - lua_pushnil(L); // Store future key here.
  132. + lua_pushnil(L); /* Store future key here. */
  133. lua_pushcfunction(L, got_map_key);
  134. return 1;
  135. @@ -177,8 +180,8 @@ static int to_value_start_array(void* ctx) {
  136. - Value pushed on the stack by to_value_* functions.
  137. */
  138. if ( ! lua_checkstack(L, 4) ) {
  139. - // TODO: So far, YAJL seems to be fine with the longjmp, but
  140. - // perhaps we should do errors a different way?
  141. + /* TODO: So far, YAJL seems to be fine with the longjmp, but
  142. + perhaps we should do errors a different way? */
  143. return luaL_error(L, "lua stack overflow");
  144. }
  145. @@ -192,7 +195,7 @@ static int to_value_start_array(void* ctx) {
  146. static int to_value_end(void* ctx) {
  147. lua_State* L = (lua_State*)ctx;
  148. - // Simply pop the stack and call the cfunction:
  149. + /* Simply pop the stack and call the cfunction: */
  150. lua_pop(L, 2);
  151. (lua_tocfunction(L, -2))(L);
  152. @@ -217,7 +220,7 @@ static yajl_callbacks js_to_value_callbacks = {
  153. to_value_end,
  154. };
  155. -//////////////////////////////////////////////////////////////////////
  156. +
  157. static int js_to_value(lua_State *L) {
  158. yajl_handle handle;
  159. size_t len;
  160. @@ -257,7 +260,6 @@ static int js_to_value(lua_State *L) {
  161. return 1;
  162. }
  163. -//////////////////////////////////////////////////////////////////////
  164. static int js_parser_null(void *ctx) {
  165. lua_State *L=(lua_State*)ctx;
  166. lua_getfield(L, lua_upvalueindex(2), "value");
  167. @@ -273,7 +275,6 @@ static int js_parser_null(void *ctx) {
  168. return 1;
  169. }
  170. -//////////////////////////////////////////////////////////////////////
  171. static int js_parser_boolean(void *ctx, int val) {
  172. lua_State *L=(lua_State*)ctx;
  173. @@ -290,7 +291,6 @@ static int js_parser_boolean(void *ctx, int val) {
  174. return 1;
  175. }
  176. -//////////////////////////////////////////////////////////////////////
  177. static int js_parser_number(void *ctx, const char* buffer, size_t buffer_len) {
  178. lua_State *L=(lua_State*)ctx;
  179. @@ -307,7 +307,6 @@ static int js_parser_number(void *ctx, const char* buffer, size_t buffer_len) {
  180. return 1;
  181. }
  182. -//////////////////////////////////////////////////////////////////////
  183. static int js_parser_string(void *ctx, const unsigned char *val, size_t len) {
  184. lua_State *L=(lua_State*)ctx;
  185. @@ -324,7 +323,6 @@ static int js_parser_string(void *ctx, const unsigned char *val, size_t len) {
  186. return 1;
  187. }
  188. -//////////////////////////////////////////////////////////////////////
  189. static int js_parser_start_map(void *ctx) {
  190. lua_State *L=(lua_State*)ctx;
  191. @@ -339,11 +337,10 @@ static int js_parser_start_map(void *ctx) {
  192. return 1;
  193. }
  194. -//////////////////////////////////////////////////////////////////////
  195. static int js_parser_map_key(void *ctx, const unsigned char *val, size_t len) {
  196. lua_State *L=(lua_State*)ctx;
  197. - // TODO: Do we want to fall-back to calling "value"?
  198. + /* TODO: Do we want to fall-back to calling "value"? */
  199. lua_getfield(L, lua_upvalueindex(2), "object_key");
  200. if ( ! lua_isnil(L, -1) ) {
  201. lua_pushvalue(L, lua_upvalueindex(2));
  202. @@ -356,7 +353,6 @@ static int js_parser_map_key(void *ctx, const unsigned char *val, size_t len) {
  203. return 1;
  204. }
  205. -//////////////////////////////////////////////////////////////////////
  206. static int js_parser_end_map(void *ctx) {
  207. lua_State *L=(lua_State*)ctx;
  208. @@ -372,7 +368,6 @@ static int js_parser_end_map(void *ctx) {
  209. return 1;
  210. }
  211. -//////////////////////////////////////////////////////////////////////
  212. static int js_parser_start_array(void *ctx) {
  213. lua_State *L=(lua_State*)ctx;
  214. @@ -387,7 +382,6 @@ static int js_parser_start_array(void *ctx) {
  215. return 1;
  216. }
  217. -//////////////////////////////////////////////////////////////////////
  218. static int js_parser_end_array(void *ctx) {
  219. lua_State *L=(lua_State*)ctx;
  220. @@ -417,7 +411,6 @@ static yajl_callbacks js_parser_callbacks = {
  221. js_parser_end_array
  222. };
  223. -//////////////////////////////////////////////////////////////////////
  224. static void js_parser_assert(lua_State* L,
  225. yajl_status status,
  226. yajl_handle* handle,
  227. @@ -446,7 +439,6 @@ static void js_parser_assert(lua_State* L,
  228. lua_error(L);
  229. }
  230. -//////////////////////////////////////////////////////////////////////
  231. static int js_parser_parse(lua_State *L) {
  232. yajl_handle* handle = (yajl_handle*)
  233. lua_touserdata(L, lua_upvalueindex(1));
  234. @@ -477,19 +469,18 @@ static int js_parser_parse(lua_State *L) {
  235. return 0;
  236. }
  237. -//////////////////////////////////////////////////////////////////////
  238. static int js_parser_delete(lua_State *L) {
  239. luaL_checktype(L, 1, LUA_TUSERDATA);
  240. yajl_free(*(yajl_handle*)lua_touserdata(L, 1));
  241. return 0;
  242. }
  243. -//////////////////////////////////////////////////////////////////////
  244. static int js_parser(lua_State *L) {
  245. + yajl_handle* handle;
  246. luaL_checktype(L, 1, LUA_TTABLE);
  247. - yajl_handle* handle = (yajl_handle*)lua_newuserdata(L, sizeof(yajl_handle));
  248. + handle = (yajl_handle*)lua_newuserdata(L, sizeof(yajl_handle));
  249. *handle = yajl_alloc(&js_parser_callbacks, NULL, (void*)L);
  250. luaL_getmetatable(L, "yajl.parser.meta");
  251. @@ -509,23 +500,21 @@ static int js_parser(lua_State *L) {
  252. lua_getfield(L, 1, "events");
  253. - // Create callback function that calls yajl_parse[_complete]()
  254. - //
  255. - // upvalue(1) = yajl_handle*
  256. - // upvalue(2) = events table
  257. + /* Create callback function that calls yajl_parse[_complete]()
  258. +
  259. + upvalue(1) = yajl_handle*
  260. + upvalue(2) = events table */
  261. lua_pushcclosure(L, &js_parser_parse, 2);
  262. return 1;
  263. }
  264. -//////////////////////////////////////////////////////////////////////
  265. static int js_generator_delete(lua_State *L) {
  266. yajl_gen* handle = js_check_generator(L, 1);
  267. yajl_gen_free(*handle);
  268. return 0;
  269. }
  270. -//////////////////////////////////////////////////////////////////////
  271. static void js_generator_assert(lua_State *L,
  272. yajl_gen_status status,
  273. const char* file,
  274. @@ -551,7 +540,6 @@ static void js_generator_assert(lua_State *L,
  275. lua_error(L);
  276. }
  277. -//////////////////////////////////////////////////////////////////////
  278. static int js_generator_integer(lua_State *L) {
  279. js_generator_assert(L,
  280. yajl_gen_integer(*js_check_generator(L, 1),
  281. @@ -560,7 +548,6 @@ static int js_generator_integer(lua_State *L) {
  282. return 0;
  283. }
  284. -//////////////////////////////////////////////////////////////////////
  285. static int js_generator_double(lua_State *L) {
  286. js_generator_assert(L,
  287. yajl_gen_double(*js_check_generator(L, 1),
  288. @@ -569,22 +556,21 @@ static int js_generator_double(lua_State *L) {
  289. return 0;
  290. }
  291. -//////////////////////////////////////////////////////////////////////
  292. static int js_generator_number(lua_State *L) {
  293. - // It would be better to make it so an arbitrary string can be
  294. - // used here, however we would then need to validate that the
  295. - // generated string is a JSON number which is a bit beyond scope
  296. - // at this point. Perhaps in the future we will loosen this
  297. - // restriction, it is always easier to loosen restrictions than it
  298. - // is to make new restrictions that break other people's code.
  299. + /* It would be better to make it so an arbitrary string can be
  300. + used here, however we would then need to validate that the
  301. + generated string is a JSON number which is a bit beyond scope
  302. + at this point. Perhaps in the future we will loosen this
  303. + restriction, it is always easier to loosen restrictions than it
  304. + is to make new restrictions that break other people's code. */
  305. double num = luaL_checknumber(L, 2);
  306. size_t len;
  307. const char* str;
  308. - // These are special cases, not sure how better to represent them
  309. - // :-(.
  310. + /* These are special cases, not sure how better to represent them
  311. + :-(. */
  312. if ( num == HUGE_VAL ) {
  313. str = "1e+666";
  314. len = 6;
  315. @@ -604,7 +590,6 @@ static int js_generator_number(lua_State *L) {
  316. return 0;
  317. }
  318. -//////////////////////////////////////////////////////////////////////
  319. static int js_generator_string(lua_State *L) {
  320. size_t len;
  321. const unsigned char* str = (const unsigned char*)luaL_checklstring(L, 2, &len);
  322. @@ -615,7 +600,6 @@ static int js_generator_string(lua_State *L) {
  323. return 0;
  324. }
  325. -//////////////////////////////////////////////////////////////////////
  326. static int js_generator_null(lua_State *L) {
  327. js_generator_assert(L,
  328. yajl_gen_null(*js_check_generator(L, 1)),
  329. @@ -623,7 +607,6 @@ static int js_generator_null(lua_State *L) {
  330. return 0;
  331. }
  332. -//////////////////////////////////////////////////////////////////////
  333. static int js_generator_boolean(lua_State *L) {
  334. luaL_checktype(L, 2, LUA_TBOOLEAN);
  335. js_generator_assert(L,
  336. @@ -636,12 +619,11 @@ static int js_generator_boolean(lua_State *L) {
  337. #define JS_OPEN_OBJECT 1
  338. #define JS_OPEN_ARRAY 2
  339. -//////////////////////////////////////////////////////////////////////
  340. static int js_generator_open_object(lua_State *L) {
  341. js_generator_assert(L,
  342. yajl_gen_map_open(*js_check_generator(L, 1)),
  343. __FILE__, __LINE__);
  344. - // Why doesn't yajl_gen keep track of this!?
  345. + /* Why doesn't yajl_gen keep track of this!? */
  346. lua_getfenv(L, 1);
  347. lua_getfield(L, -1, "stack");
  348. lua_pushinteger(L, JS_OPEN_OBJECT);
  349. @@ -649,12 +631,11 @@ static int js_generator_open_object(lua_State *L) {
  350. return 0;
  351. }
  352. -//////////////////////////////////////////////////////////////////////
  353. static int js_generator_open_array(lua_State *L) {
  354. js_generator_assert(L,
  355. yajl_gen_array_open(*js_check_generator(L, 1)),
  356. __FILE__, __LINE__);
  357. - // Why doesn't yajl_gen keep track of this!?
  358. + /* Why doesn't yajl_gen keep track of this!? */
  359. lua_getfenv(L, 1);
  360. lua_getfield(L, -1, "stack");
  361. lua_pushinteger(L, JS_OPEN_ARRAY);
  362. @@ -662,9 +643,10 @@ static int js_generator_open_array(lua_State *L) {
  363. return 0;
  364. }
  365. -//////////////////////////////////////////////////////////////////////
  366. static int js_generator_close(lua_State *L) {
  367. - // Why doesn't yajl_gen keep track of this!?
  368. + lua_Integer type;
  369. +
  370. + /* Why doesn't yajl_gen keep track of this!? */
  371. lua_getfenv(L, 1);
  372. lua_getfield(L, -1, "stack");
  373. lua_rawgeti(L, -1, lua_objlen(L, -1));
  374. @@ -672,7 +654,7 @@ static int js_generator_close(lua_State *L) {
  375. lua_pushfstring(L, "StackUnderflow: Attempt to call close() when no array or object has been opened at %s line %d", __FILE__, __LINE__);
  376. lua_error(L);
  377. }
  378. - lua_Integer type = lua_tointeger(L, -1);
  379. + type = lua_tointeger(L, -1);
  380. switch ( type ) {
  381. case JS_OPEN_OBJECT:
  382. js_generator_assert(L,
  383. @@ -688,7 +670,7 @@ static int js_generator_close(lua_State *L) {
  384. lua_pushfstring(L, "Unreachable: internal 'stack' contained invalid integer (%d) at %s line %d", type, __FILE__, __LINE__);
  385. lua_error(L);
  386. }
  387. - // delete the top of the "stack":
  388. + /* delete the top of the "stack": */
  389. lua_pop(L, 1);
  390. lua_pushnil(L);
  391. lua_rawseti(L, -2, lua_objlen(L, -2));
  392. @@ -696,8 +678,9 @@ static int js_generator_close(lua_State *L) {
  393. return 0;
  394. }
  395. -//////////////////////////////////////////////////////////////////////
  396. static int js_generator_value(lua_State *L) {
  397. + int max;
  398. + int is_array;
  399. int type = lua_type(L, 2);
  400. switch ( type ) {
  401. @@ -719,22 +702,22 @@ static int js_generator_value(lua_State *L) {
  402. case LUA_TTHREAD:
  403. if ( luaL_getmetafield(L, 2, "__gen_json") ) {
  404. if ( lua_isfunction(L, -1) ) {
  405. - lua_settop(L, 3); // gen, obj, func
  406. - lua_insert(L, 1); // func, gen, obj
  407. - lua_insert(L, 2); // func, obj, gen
  408. + lua_settop(L, 3); /* gen, obj, func */
  409. + lua_insert(L, 1); /* func, gen, obj */
  410. + lua_insert(L, 2); /* func, obj, gen */
  411. lua_call(L, 2, 0);
  412. return 0;
  413. }
  414. lua_pop(L, 1);
  415. }
  416. - // Simply ignore it, perhaps we should warn?
  417. + /* Simply ignore it, perhaps we should warn? */
  418. if ( type != LUA_TTABLE ) return 0;
  419. - int max = 0;
  420. - int is_array = 1;
  421. + max = 0;
  422. + is_array = 1;
  423. - // First iterate over the table to see if it is an array:
  424. + /* First iterate over the table to see if it is an array: */
  425. lua_pushnil(L);
  426. while ( lua_next(L, 2) != 0 ) {
  427. if ( lua_type(L, -2) == LUA_TNUMBER ) {
  428. @@ -761,8 +744,8 @@ static int js_generator_value(lua_State *L) {
  429. lua_pushinteger(L, i);
  430. lua_gettable(L, 2);
  431. - // RECURSIVE CALL:
  432. - // gen, obj, ?, val, func, gen, val
  433. + /* RECURSIVE CALL:
  434. + gen, obj, ?, val, func, gen, val */
  435. lua_pushcfunction(L, js_generator_value);
  436. lua_pushvalue(L, 1);
  437. lua_pushvalue(L, -3);
  438. @@ -775,16 +758,13 @@ static int js_generator_value(lua_State *L) {
  439. lua_pushnil(L);
  440. while ( lua_next(L, 2) != 0 ) {
  441. - size_t len;
  442. - const char* str;
  443. -
  444. - // gen, obj, ?, key, val, func, gen, key
  445. + /* gen, obj, ?, key, val, func, gen, key */
  446. lua_pushcfunction(L, js_generator_string);
  447. lua_pushvalue(L, 1);
  448. if ( lua_isstring(L, -4) ) {
  449. lua_pushvalue(L, -4);
  450. } else {
  451. - // Must coerce into a string:
  452. + /* Must coerce into a string: */
  453. lua_getglobal(L, "tostring");
  454. lua_pushvalue(L, -5);
  455. lua_call(L, 1, 1);
  456. @@ -792,8 +772,8 @@ static int js_generator_value(lua_State *L) {
  457. lua_call(L, 2, 0);
  458. - // RECURSIVE CALL:
  459. - // gen, obj, ?, key, val, func, gen, val
  460. + /* RECURSIVE CALL:
  461. + gen, obj, ?, key, val, func, gen, val */
  462. lua_pushcfunction(L, js_generator_value);
  463. lua_pushvalue(L, 1);
  464. lua_pushvalue(L, -3);
  465. @@ -809,7 +789,7 @@ static int js_generator_value(lua_State *L) {
  466. default:
  467. lua_pushfstring(L, "Unreachable: js_generator_value passed lua type (%d) not recognized at %s line %d", type, __FILE__, __LINE__);
  468. }
  469. - // Shouldn't get here:
  470. + /* Shouldn't get here: */
  471. lua_error(L);
  472. return 0;
  473. }
  474. @@ -819,18 +799,17 @@ typedef struct {
  475. int printer_ref;
  476. } js_printer_ctx;
  477. -//////////////////////////////////////////////////////////////////////
  478. static void js_printer(void* void_ctx, const char* str, size_t len) {
  479. js_printer_ctx* ctx = (js_printer_ctx*)void_ctx;
  480. lua_State* L = ctx->L;
  481. - // refs
  482. + /* refs */
  483. lua_getfield(L, LUA_REGISTRYINDEX, "yajl.refs");
  484. - // refs, printer
  485. + /* refs, printer */
  486. lua_rawgeti(L, -1, ctx->printer_ref);
  487. if ( lua_isfunction(L, -1) ) {
  488. lua_pushlstring(L, str, len);
  489. - // Not sure if yajl can handle longjmp's if this errors...
  490. + /* Not sure if yajl can handle longjmp's if this errors... */
  491. lua_call(L, 1, 0);
  492. lua_pop(L, 1);
  493. } else {
  494. @@ -838,60 +817,62 @@ static void js_printer(void* void_ctx, const char* str, size_t len) {
  495. }
  496. }
  497. -//////////////////////////////////////////////////////////////////////
  498. static int js_generator(lua_State *L) {
  499. yajl_print_t print = NULL;
  500. - void * ctx = NULL;
  501. + void * ctx = NULL;
  502. + yajl_gen* handle;
  503. luaL_checktype(L, 1, LUA_TTABLE);
  504. - // {args}, ?, tbl
  505. + /* {args}, ?, tbl */
  506. lua_newtable(L);
  507. - // Validate and save in fenv so it isn't gc'ed:
  508. + /* Validate and save in fenv so it isn't gc'ed: */
  509. lua_getfield(L, 1, "printer");
  510. if ( ! lua_isnil(L, -1) ) {
  511. + js_printer_ctx* print_ctx;
  512. +
  513. luaL_checktype(L, -1, LUA_TFUNCTION);
  514. lua_pushvalue(L, -1);
  515. - // {args}, ?, tbl, printer, printer
  516. + /* {args}, ?, tbl, printer, printer */
  517. lua_setfield(L, -3, "printer");
  518. - js_printer_ctx* print_ctx = (js_printer_ctx*)
  519. + print_ctx = (js_printer_ctx*)
  520. lua_newuserdata(L, sizeof(js_printer_ctx));
  521. - // {args}, ?, tbl, printer, printer_ctx
  522. + /* {args}, ?, tbl, printer, printer_ctx */
  523. lua_setfield(L, -3, "printer_ctx");
  524. - // {args}, ?, tbl, printer
  525. + /* {args}, ?, tbl, printer */
  526. lua_getfield(L, LUA_REGISTRYINDEX, "yajl.refs");
  527. - // {args}, ?, tbl, printer, refs
  528. + /* {args}, ?, tbl, printer, refs */
  529. lua_insert(L, -2);
  530. - // {args}, ?, tbl, refs, printer
  531. + /* {args}, ?, tbl, refs, printer */
  532. print_ctx->printer_ref = luaL_ref(L, -2);
  533. print_ctx->L = L;
  534. print = &js_printer;
  535. ctx = print_ctx;
  536. }
  537. lua_pop(L, 1);
  538. - // {args}, ?, tbl
  539. + /* {args}, ?, tbl */
  540. - // Sucks that yajl's generator doesn't keep track of this for me
  541. - // (this is a stack of strings "array" and "object" so I can keep
  542. - // track of what to "close"):
  543. + /* Sucks that yajl's generator doesn't keep track of this for me
  544. + (this is a stack of strings "array" and "object" so I can keep
  545. + track of what to "close"): */
  546. lua_newtable(L);
  547. lua_setfield(L, -2, "stack");
  548. - // {args}, ?, tbl
  549. - yajl_gen* handle = (yajl_gen*)lua_newuserdata(L, sizeof(yajl_gen));
  550. + /* {args}, ?, tbl */
  551. + handle = (yajl_gen*)lua_newuserdata(L, sizeof(yajl_gen));
  552. *handle = yajl_gen_alloc(NULL);
  553. if ( print ) {
  554. yajl_gen_config(*handle, yajl_gen_print_callback, print, ctx);
  555. }
  556. - // Get the indent and save so it isn't gc'ed:
  557. + /* Get the indent and save so it isn't gc'ed: */
  558. lua_getfield(L, 1, "indent");
  559. if ( ! lua_isnil(L, -1) ) {
  560. yajl_gen_config(*handle, yajl_gen_beautify, 1);
  561. @@ -900,33 +881,31 @@ static int js_generator(lua_State *L) {
  562. } else {
  563. lua_pop(L, 1);
  564. }
  565. - // {args}, ?, tbl
  566. + /* {args}, ?, tbl */
  567. - // {args}, ?, tbl, ud, meta
  568. + /* {args}, ?, tbl, ud, meta */
  569. luaL_getmetatable(L, "yajl.generator.meta");
  570. lua_setmetatable(L, -2);
  571. - // {args}, ?, tbl, ud
  572. + /* {args}, ?, tbl, ud */
  573. lua_insert(L, -2);
  574. - // {args}, ?, ud, tbl
  575. + /* {args}, ?, ud, tbl */
  576. lua_setfenv(L, -2);
  577. return 1;
  578. }
  579. -//////////////////////////////////////////////////////////////////////
  580. static void js_create_parser_mt(lua_State *L) {
  581. - luaL_newmetatable(L, "yajl.parser.meta"); // {}
  582. + luaL_newmetatable(L, "yajl.parser.meta");
  583. lua_pushcfunction(L, js_parser_delete);
  584. lua_setfield(L, -2, "__gc");
  585. - lua_pop(L, 1); // <empty>
  586. + lua_pop(L, 1);
  587. }
  588. -//////////////////////////////////////////////////////////////////////
  589. static void js_create_generator_mt(lua_State *L) {
  590. - luaL_newmetatable(L, "yajl.generator.meta"); // {}
  591. + luaL_newmetatable(L, "yajl.generator.meta");
  592. lua_pushvalue(L, -1);
  593. lua_setfield(L, -2, "__index");
  594. @@ -964,7 +943,7 @@ static void js_create_generator_mt(lua_State *L) {
  595. lua_pushcfunction(L, js_generator_close);
  596. lua_setfield(L, -2, "close");
  597. - lua_pop(L, 1); // <empty>
  598. + lua_pop(L, 1);
  599. }
  600. static int js_null_tostring(lua_State* L) {
  601. @@ -972,28 +951,26 @@ static int js_null_tostring(lua_State* L) {
  602. return 1;
  603. }
  604. -//////////////////////////////////////////////////////////////////////
  605. static void js_create_null_mt(lua_State *L) {
  606. - luaL_newmetatable(L, "yajl.null.meta"); // {}
  607. + luaL_newmetatable(L, "yajl.null.meta");
  608. lua_pushcfunction(L, js_null_tostring);
  609. lua_setfield(L, -2, "__tostring");
  610. - lua_pop(L, 1); // <empty>
  611. + lua_pop(L, 1);
  612. }
  613. -//////////////////////////////////////////////////////////////////////
  614. LUALIB_API int luaopen_yajl(lua_State *L) {
  615. js_create_parser_mt(L);
  616. js_create_generator_mt(L);
  617. js_create_null_mt(L);
  618. - // Create the yajl.refs weak table:
  619. + /* Create the yajl.refs weak table: */
  620. lua_createtable(L, 0, 2);
  621. - lua_pushliteral(L, "v"); // tbl, "v"
  622. + lua_pushliteral(L, "v"); /* tbl, "v" */
  623. lua_setfield(L, -2, "__mode");
  624. - lua_pushvalue(L, -1); // tbl, tbl
  625. - lua_setmetatable(L, -2); // tbl
  626. + lua_pushvalue(L, -1); /* tbl, tbl */
  627. + lua_setmetatable(L, -2); /* tbl */
  628. lua_setfield(L, LUA_REGISTRYINDEX, "yajl.refs");
  629. lua_createtable(L, 0, 4);
  630. --
  631. 1.7.10.2
  632. From a71a284833611c681e3613f3269dfc323a2d0c8c Mon Sep 17 00:00:00 2001
  633. From: Brian Maher <bmaher@brian-mahers-computer.local>
  634. Date: Tue, 18 Oct 2011 07:23:49 -0700
  635. Subject: [PATCH] Add a comment about the js_to_value() STRATEGY.
  636. ---
  637. lua_yajl.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  638. 1 file changed, 58 insertions(+)
  639. diff --git a/lua_yajl.c b/lua_yajl.c
  640. index fc2e7a0..45db965 100644
  641. --- a/lua_yajl.c
  642. +++ b/lua_yajl.c
  643. @@ -84,6 +84,7 @@ static int js_to_string(lua_State *L) {
  644. return 1;
  645. }
  646. +/* See STRATEGY section below */
  647. static int to_value_null(void* ctx) {
  648. lua_State* L = (lua_State*)ctx;
  649. @@ -93,6 +94,7 @@ static int to_value_null(void* ctx) {
  650. return 1;
  651. }
  652. +/* See STRATEGY section below */
  653. static int to_value_boolean(void* ctx, int val) {
  654. lua_State* L = (lua_State*)ctx;
  655. @@ -102,6 +104,7 @@ static int to_value_boolean(void* ctx, int val) {
  656. return 1;
  657. }
  658. +/* See STRATEGY section below */
  659. static int to_value_number(void* ctx, const char* val, size_t len) {
  660. lua_State* L = (lua_State*)ctx;
  661. @@ -111,6 +114,7 @@ static int to_value_number(void* ctx, const char* val, size_t len) {
  662. return 1;
  663. }
  664. +/* See STRATEGY section below */
  665. static int to_value_string(void* ctx, const unsigned char *val, size_t len) {
  666. lua_State* L = (lua_State*)ctx;
  667. @@ -120,6 +124,7 @@ static int to_value_string(void* ctx, const unsigned char *val, size_t len) {
  668. return 1;
  669. }
  670. +/* See STRATEGY section below */
  671. static int got_map_value(lua_State* L) {
  672. /* ..., Table, Key, Func, Value */
  673. lua_insert(L, -2);
  674. @@ -131,6 +136,7 @@ static int got_map_value(lua_State* L) {
  675. return 0; /* Ignored. */
  676. }
  677. +/* See STRATEGY section below */
  678. static int got_map_key(lua_State* L) {
  679. lua_replace(L, -3);
  680. lua_pop(L, 1);
  681. @@ -139,6 +145,7 @@ static int got_map_key(lua_State* L) {
  682. return 0; /* Ignored. */
  683. }
  684. +/* See STRATEGY section below */
  685. static int got_array_value(lua_State* L) {
  686. /* ..., Table, Integer, Func, Value */
  687. lua_rawseti(L, -4, lua_tointeger(L, -3));
  688. @@ -148,6 +155,7 @@ static int got_array_value(lua_State* L) {
  689. return 0; /* Ignored. */
  690. }
  691. +/* See STRATEGY section below */
  692. static int to_value_start_map(void* ctx) {
  693. lua_State* L = (lua_State*)ctx;
  694. @@ -170,6 +178,7 @@ static int to_value_start_map(void* ctx) {
  695. return 1;
  696. }
  697. +/* See STRATEGY section below */
  698. static int to_value_start_array(void* ctx) {
  699. lua_State* L = (lua_State*)ctx;
  700. @@ -192,6 +201,7 @@ static int to_value_start_array(void* ctx) {
  701. return 1;
  702. }
  703. +/* See STRATEGY section below */
  704. static int to_value_end(void* ctx) {
  705. lua_State* L = (lua_State*)ctx;
  706. @@ -202,10 +212,12 @@ static int to_value_end(void* ctx) {
  707. return 1;
  708. }
  709. +/* See STRATEGY section below */
  710. static int noop(lua_State* L) {
  711. return 0;
  712. }
  713. +/* See STRATEGY section below */
  714. static yajl_callbacks js_to_value_callbacks = {
  715. to_value_null,
  716. to_value_boolean,
  717. @@ -221,6 +233,52 @@ static yajl_callbacks js_to_value_callbacks = {
  718. };
  719. +/* STRATEGY:
  720. + *
  721. + * Each of the js_to_value_callbacks perform these actions:
  722. + *
  723. + * [1] Push a new value onto the top of the Lua stack.
  724. + *
  725. + * [2] Call the function that was at the top of the Lua stack before
  726. + * step [1] occurred.
  727. + *
  728. + * The purpose of the function call in [2] is to take the value at the
  729. + * top of the stack and store it in the appropriate location.
  730. + * Initially, the function is the noop (no operation) function which
  731. + * does nothing. Therefore we know that the final result is on the
  732. + * top of the Lua stack.
  733. + *
  734. + * The to_value_start_map and to_value_start_array callbacks are
  735. + * different since they need to use a bit of the Lua stack to store
  736. + * some state information. When these callbacks are ran, they perform
  737. + * these actions:
  738. + *
  739. + * [a] Push a new table which will represent the final "array" or
  740. + * "object" onto the top of the Lua stack.
  741. + *
  742. + * [b] Allocate space for the "key" (in the case of arrays, this is
  743. + * the index into the array to use as part of the next insertion)
  744. + *
  745. + * [c] Push the got_array_value or got_map_key function.
  746. + *
  747. + * The got_array_value function will take the value at the top of the
  748. + * stack and insert it into the table created in step [a]. It will
  749. + * then increment the index created in step [b]. As a final step, it
  750. + * removes the value at the top of the stack.
  751. + *
  752. + * The got_map_key function simply takes the value at the top of the
  753. + * stack and stores it in the space allocated by step [b] above. It
  754. + * then replaces the function pushed onto the stack by step [c] with
  755. + * the got_map_value function. As a final step, it removes the value
  756. + * at the top of the stack.
  757. + *
  758. + * The got_map_value function takes the value at the top of the stack
  759. + * and inserts it into the table created in step [a] with the key
  760. + * whose space was allocated in step [b]. The function pushed onto
  761. + * the stack by step [c] is then restored back to the got_map_key
  762. + * function. As a final step, it removes the value at the top of the
  763. + * stack.
  764. + */
  765. static int js_to_value(lua_State *L) {
  766. yajl_handle handle;
  767. size_t len;
  768. --
  769. 1.7.10.2
  770. From cc408efb08198efb2786be80c3dd2eb81f12d959 Mon Sep 17 00:00:00 2001
  771. From: Brian Maher <brian@brimworks.com>
  772. Date: Mon, 7 Jan 2013 21:17:33 -0800
  773. Subject: [PATCH] Fix bug with not calling yajl_complete_parse() in
  774. js_to_value(), and get rid of the expect_complete useless
  775. parameter to js_parser_assert().
  776. See https://github.com/brimworks/lua-yajl/issues/8
  777. ---
  778. lua_yajl.c | 17 ++++++++---------
  779. test.lua | 18 +++++++++++-------
  780. 2 files changed, 19 insertions(+), 16 deletions(-)
  781. diff --git a/lua_yajl.c b/lua_yajl.c
  782. index 45db965..c8410d7 100644
  783. --- a/lua_yajl.c
  784. +++ b/lua_yajl.c
  785. @@ -19,7 +19,6 @@ static void js_parser_assert(lua_State* L,
  786. yajl_handle* handle,
  787. const unsigned char* json_text,
  788. size_t json_text_len,
  789. - int expect_complete,
  790. const char* file,
  791. int line);
  792. static int got_map_key(lua_State* L);
  793. @@ -107,7 +106,6 @@ static int to_value_boolean(void* ctx, int val) {
  794. /* See STRATEGY section below */
  795. static int to_value_number(void* ctx, const char* val, size_t len) {
  796. lua_State* L = (lua_State*)ctx;
  797. -
  798. lua_pushnumber(L, todouble(L, val, len));
  799. (lua_tocfunction(L, -2))(L);
  800. @@ -283,7 +281,6 @@ static int js_to_value(lua_State *L) {
  801. yajl_handle handle;
  802. size_t len;
  803. const unsigned char* buff = (const unsigned char*) luaL_checklstring(L, 1, &len);
  804. - int expect_complete = 1;
  805. if ( NULL == buff ) return 0;
  806. @@ -309,7 +306,14 @@ static int js_to_value(lua_State *L) {
  807. &handle,
  808. buff,
  809. len,
  810. - expect_complete,
  811. + __FILE__,
  812. + __LINE__);
  813. +
  814. + js_parser_assert(L,
  815. + yajl_complete_parse(handle),
  816. + &handle,
  817. + buff,
  818. + len,
  819. __FILE__,
  820. __LINE__);
  821. @@ -474,7 +478,6 @@ static void js_parser_assert(lua_State* L,
  822. yajl_handle* handle,
  823. const unsigned char* json_text,
  824. size_t json_text_len,
  825. - int expect_complete,
  826. const char* file,
  827. int line)
  828. {
  829. @@ -501,17 +504,14 @@ static int js_parser_parse(lua_State *L) {
  830. yajl_handle* handle = (yajl_handle*)
  831. lua_touserdata(L, lua_upvalueindex(1));
  832. if ( lua_isnil(L, 1) ) {
  833. - int expect_complete = 1;
  834. js_parser_assert(L,
  835. yajl_complete_parse(*handle),
  836. handle,
  837. NULL,
  838. 0,
  839. - expect_complete,
  840. __FILE__,
  841. __LINE__);
  842. } else {
  843. - int expect_complete = 0;
  844. size_t len;
  845. const unsigned char* buff = (const unsigned char*) luaL_checklstring(L, 1, &len);
  846. if ( NULL == buff ) return 0;
  847. @@ -520,7 +520,6 @@ static int js_parser_parse(lua_State *L) {
  848. handle,
  849. buff,
  850. len,
  851. - expect_complete,
  852. __FILE__,
  853. __LINE__);
  854. }
  855. diff --git a/test.lua b/test.lua
  856. index 70a9337..2abe36a 100644
  857. --- a/test.lua
  858. +++ b/test.lua
  859. @@ -9,13 +9,13 @@ local yajl = require("yajl")
  860. local ok = tap.ok
  861. function main()
  862. - test_simple()
  863. - null_at_end_of_array()
  864. - null_object_value()
  865. - weird_numbers()
  866. - number_in_string()
  867. - test_generator()
  868. - test_to_value()
  869. + test_simple()
  870. + null_at_end_of_array()
  871. + null_object_value()
  872. + weird_numbers()
  873. + number_in_string()
  874. + test_generator()
  875. + test_to_value()
  876. end
  877. function to_value(string)
  878. @@ -107,6 +107,10 @@ function test_generator()
  879. end
  880. function test_simple()
  881. + -- Thanks to fab13n@github for this bug report:
  882. + -- https://github.com/brimworks/lua-yajl/issues/8
  883. + assert(yajl.to_value(yajl.to_string(0) == 0))
  884. +
  885. local expect =
  886. '['..
  887. '"float",1.5,'..
  888. --
  889. 1.7.10.2