aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-12-31 15:58:23 +1030
committerMark Pulford <mark@kyne.com.au>2011-12-31 15:58:23 +1030
commit5f95673ab4ea2a45d906a999ca03a5af50184473 (patch)
tree2d45bcdc80e7143ee4c3bf41e2d048b32da6e08a
parent85a1d3276cbb25c40eadc96ec4aaba1977118154 (diff)
downloadlua-cjson-5f95673ab4ea2a45d906a999ca03a5af50184473.tar.gz
lua-cjson-5f95673ab4ea2a45d906a999ca03a5af50184473.tar.bz2
lua-cjson-5f95673ab4ea2a45d906a999ca03a5af50184473.zip
Remove shared current_depth var from cfg struct
-rw-r--r--lua_cjson.c42
1 files changed, 17 insertions, 25 deletions
diff --git a/lua_cjson.c b/lua_cjson.c
index be5cdc2..f1c54ec 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -110,7 +110,6 @@ typedef struct {
110 char *char2escape[256]; /* Encoding */ 110 char *char2escape[256]; /* Encoding */
111#endif 111#endif
112 strbuf_t encode_buf; 112 strbuf_t encode_buf;
113 int current_depth;
114 113
115 int encode_sparse_convert; 114 int encode_sparse_convert;
116 int encode_sparse_ratio; 115 int encode_sparse_ratio;
@@ -514,31 +513,28 @@ static int lua_array_length(lua_State *l, json_config_t *cfg)
514 return max; 513 return max;
515} 514}
516 515
517static void json_encode_descend(lua_State *l, json_config_t *cfg) 516static void json_check_encode_depth(lua_State *l, json_config_t *cfg, int current_depth)
518{ 517{
519 cfg->current_depth++; 518 if (current_depth > cfg->encode_max_depth) {
520
521 if (cfg->current_depth > cfg->encode_max_depth) {
522 if (!cfg->encode_keep_buffer) 519 if (!cfg->encode_keep_buffer)
523 strbuf_free(&cfg->encode_buf); 520 strbuf_free(&cfg->encode_buf);
524 luaL_error(l, "Cannot serialise, excessive nesting (%d)", 521 luaL_error(l, "Cannot serialise, excessive nesting (%d)",
525 cfg->current_depth); 522 current_depth);
526 } 523 }
527} 524}
528 525
529static void json_append_data(lua_State *l, json_config_t *cfg, strbuf_t *json); 526static void json_append_data(lua_State *l, json_config_t *cfg,
527 int current_depth, strbuf_t *json);
530 528
531/* json_append_array args: 529/* json_append_array args:
532 * - lua_State 530 * - lua_State
533 * - JSON strbuf 531 * - JSON strbuf
534 * - Size of passwd Lua array (top of stack) */ 532 * - Size of passwd Lua array (top of stack) */
535static void json_append_array(lua_State *l, json_config_t *cfg, strbuf_t *json, 533static void json_append_array(lua_State *l, json_config_t *cfg, int current_depth,
536 int array_length) 534 strbuf_t *json, int array_length)
537{ 535{
538 int comma, i; 536 int comma, i;
539 537
540 json_encode_descend(l, cfg);
541
542 strbuf_append_char(json, '['); 538 strbuf_append_char(json, '[');
543 539
544 comma = 0; 540 comma = 0;
@@ -549,13 +545,11 @@ static void json_append_array(lua_State *l, json_config_t *cfg, strbuf_t *json,
549 comma = 1; 545 comma = 1;
550 546
551 lua_rawgeti(l, -1, i); 547 lua_rawgeti(l, -1, i);
552 json_append_data(l, cfg, json); 548 json_append_data(l, cfg, current_depth, json);
553 lua_pop(l, 1); 549 lua_pop(l, 1);
554 } 550 }
555 551
556 strbuf_append_char(json, ']'); 552 strbuf_append_char(json, ']');
557
558 cfg->current_depth--;
559} 553}
560 554
561static void json_append_number(lua_State *l, strbuf_t *json, int index, 555static void json_append_number(lua_State *l, strbuf_t *json, int index,
@@ -580,12 +574,10 @@ static void json_append_number(lua_State *l, strbuf_t *json, int index,
580} 574}
581 575
582static void json_append_object(lua_State *l, json_config_t *cfg, 576static void json_append_object(lua_State *l, json_config_t *cfg,
583 strbuf_t *json) 577 int current_depth, strbuf_t *json)
584{ 578{
585 int comma, keytype; 579 int comma, keytype;
586 580
587 json_encode_descend(l, cfg);
588
589 /* Object */ 581 /* Object */
590 strbuf_append_char(json, '{'); 582 strbuf_append_char(json, '{');
591 583
@@ -614,18 +606,17 @@ static void json_append_object(lua_State *l, json_config_t *cfg,
614 } 606 }
615 607
616 /* table, key, value */ 608 /* table, key, value */
617 json_append_data(l, cfg, json); 609 json_append_data(l, cfg, current_depth, json);
618 lua_pop(l, 1); 610 lua_pop(l, 1);
619 /* table, key */ 611 /* table, key */
620 } 612 }
621 613
622 strbuf_append_char(json, '}'); 614 strbuf_append_char(json, '}');
623
624 cfg->current_depth--;
625} 615}
626 616
627/* Serialise Lua data into JSON string. */ 617/* Serialise Lua data into JSON string. */
628static void json_append_data(lua_State *l, json_config_t *cfg, strbuf_t *json) 618static void json_append_data(lua_State *l, json_config_t *cfg,
619 int current_depth, strbuf_t *json)
629{ 620{
630 int len; 621 int len;
631 622
@@ -644,10 +635,12 @@ static void json_append_data(lua_State *l, json_config_t *cfg, strbuf_t *json)
644 break; 635 break;
645 case LUA_TTABLE: 636 case LUA_TTABLE:
646 len = lua_array_length(l, cfg); 637 len = lua_array_length(l, cfg);
638 current_depth++;
639 json_check_encode_depth(l, cfg, current_depth);
647 if (len > 0) 640 if (len > 0)
648 json_append_array(l, cfg, json, len); 641 json_append_array(l, cfg, current_depth, json, len);
649 else 642 else
650 json_append_object(l, cfg, json); 643 json_append_object(l, cfg, current_depth, json);
651 break; 644 break;
652 case LUA_TNIL: 645 case LUA_TNIL:
653 strbuf_append_mem(json, "null", 4); 646 strbuf_append_mem(json, "null", 4);
@@ -676,7 +669,6 @@ static int json_encode(lua_State *l)
676 luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); 669 luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
677 670
678 cfg = json_fetch_config(l); 671 cfg = json_fetch_config(l);
679 cfg->current_depth = 0;
680 672
681 /* Reset the persistent buffer if it exists. 673 /* Reset the persistent buffer if it exists.
682 * Otherwise allocate a new buffer. */ 674 * Otherwise allocate a new buffer. */
@@ -685,7 +677,7 @@ static int json_encode(lua_State *l)
685 else 677 else
686 strbuf_init(&cfg->encode_buf, 0); 678 strbuf_init(&cfg->encode_buf, 0);
687 679
688 json_append_data(l, cfg, &cfg->encode_buf); 680 json_append_data(l, cfg, 0, &cfg->encode_buf);
689 json = strbuf_string(&cfg->encode_buf, &len); 681 json = strbuf_string(&cfg->encode_buf, &len);
690 682
691 lua_pushlstring(l, json, len); 683 lua_pushlstring(l, json, len);