--- JSON-XS/XS.xs 2007/06/25 22:11:39 1.46 +++ JSON-XS/XS.xs 2007/07/01 14:08:03 1.47 @@ -61,6 +61,10 @@ static HV *json_stash, *json_boolean_stash; // JSON::XS:: static SV *json_true, *json_false; +typedef struct json { + U32 flags; +} JSON__XS; + ///////////////////////////////////////////////////////////////////////////// // utility functions @@ -118,7 +122,7 @@ char *cur; // SvPVX (sv) + current output position char *end; // SvEND (sv) SV *sv; // result scalar - U32 flags; // F_* + struct json json; U32 indent; // indentation level U32 maxdepth; // max. indentation/recursion level } enc_t; @@ -202,7 +206,7 @@ if (uch > 0x10FFFFUL) croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); - if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) + if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) { if (uch > 0xFFFFUL) { @@ -226,7 +230,7 @@ str += clen; } - else if (enc->flags & F_LATIN1) + else if (enc->json.flags & F_LATIN1) { *enc->cur++ = uch; str += clen; @@ -257,7 +261,7 @@ inline void encode_indent (enc_t *enc) { - if (enc->flags & F_INDENT) + if (enc->json.flags & F_INDENT) { int spaces = enc->indent * INDENT_STEP; @@ -277,7 +281,7 @@ inline void encode_nl (enc_t *enc) { - if (enc->flags & F_INDENT) + if (enc->json.flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); @@ -289,9 +293,9 @@ { encode_ch (enc, ','); - if (enc->flags & F_INDENT) + if (enc->json.flags & F_INDENT) encode_nl (enc); - else if (enc->flags & F_SPACE_AFTER) + else if (enc->json.flags & F_SPACE_AFTER) encode_space (enc); } @@ -344,9 +348,9 @@ encode_ch (enc, '"'); - if (enc->flags & F_SPACE_BEFORE) encode_space (enc); + if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); encode_ch (enc, ':'); - if (enc->flags & F_SPACE_AFTER ) encode_space (enc); + if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); encode_sv (enc, HeVAL (he)); } @@ -391,7 +395,7 @@ // actually, this is mostly due to the stupid so-called // security workaround added somewhere in 5.8.x. // that randomises hash orderings - if (enc->flags & F_CANONICAL) + if (enc->json.flags & F_CANONICAL) { int fast = 1; HE *he; @@ -490,7 +494,7 @@ // not yet } #endif - if (enc->flags & F_CONV_BLESSED) + if (enc->json.flags & F_CONV_BLESSED) { // we re-bless the reference to get overload and other niceties right GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); @@ -514,13 +518,13 @@ FREETMPS; LEAVE; } - else if (enc->flags & F_ALLOW_BLESSED) + else if (enc->json.flags & F_ALLOW_BLESSED) encode_str (enc, "null", 4, 0); else croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", SvPV_nolen (sv_2mortal (newRV_inc (sv)))); } - else if (enc->flags & F_ALLOW_BLESSED) + else if (enc->json.flags & F_ALLOW_BLESSED) encode_str (enc, "null", 4, 0); else croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", @@ -616,19 +620,19 @@ } static SV * -encode_json (SV *scalar, U32 flags) +encode_json (SV *scalar, struct json *json) { enc_t enc; - if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) + if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); - enc.flags = flags; + enc.json = *json; enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); enc.cur = SvPVX (enc.sv); enc.end = SvEND (enc.sv); enc.indent = 0; - enc.maxdepth = DEC_DEPTH (flags); + enc.maxdepth = DEC_DEPTH (enc.json.flags); SvPOK_only (enc.sv); encode_sv (&enc, scalar); @@ -636,10 +640,10 @@ SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings - if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) + if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) SvUTF8_on (enc.sv); - if (enc.flags & F_SHRINK) + if (enc.json.flags & F_SHRINK) shrink (enc.sv); return enc.sv; @@ -654,7 +658,7 @@ char *cur; // current parser pointer char *end; // end of input string const char *err; // parse error, if != 0 - U32 flags; // F_* + struct json json; U32 depth; // recursion depth U32 maxdepth; // recursion depth limit } dec_t; @@ -1028,10 +1032,10 @@ decode_ws (dec); EXPECT_CH ('"'); // heuristic: assume that - // a) decode_str + hv_store_ent are abysmally slow - // b) most hash keys are short, simple ascii text - // so try to "fast-match" such strings to avoid - // the overhead of hv_store_ent. + // a) decode_str + hv_store_ent are abysmally slow. + // b) most hash keys are short, simple ascii text. + // => try to "fast-match" such strings to avoid + // the overhead of decode_str + hv_store_ent. { SV *value; char *p = dec->cur; @@ -1039,6 +1043,7 @@ for (;;) { + // the >= 0x80 is true on most architectures if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') { // slow path, back up and use decode_str @@ -1166,7 +1171,7 @@ } static SV * -decode_json (SV *string, U32 flags, UV *offset_return) +decode_json (SV *string, struct json *json, UV *offset_return) { dec_t dec; UV offset; @@ -1175,23 +1180,23 @@ SvGETMAGIC (string); SvUPGRADE (string, SVt_PV); - if (flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (flags)) + if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", - (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (flags)); + (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); - if (flags & F_UTF8) + if (json->flags & F_UTF8) sv_utf8_downgrade (string, 0); else sv_utf8_upgrade (string); SvGROW (string, SvCUR (string) + 1); // should basically be a NOP - dec.flags = flags; + dec.json = *json; dec.cur = SvPVX (string); dec.end = SvEND (string); dec.err = 0; dec.depth = 0; - dec.maxdepth = DEC_DEPTH (dec.flags); + dec.maxdepth = DEC_DEPTH (dec.json.flags); *dec.end = 0; // this should basically be a nop, too, but make sure it's there sv = decode_sv (&dec); @@ -1211,7 +1216,7 @@ if (offset_return || !sv) { - offset = dec.flags & F_UTF8 + offset = dec.json.flags & F_UTF8 ? dec.cur - SvPVX (string) : utf8_distance (dec.cur, SvPVX (string)); @@ -1240,7 +1245,7 @@ sv = sv_2mortal (sv); - if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) + if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); return sv; @@ -1343,32 +1348,41 @@ void encode (SV *self, SV *scalar) PPCODE: - XPUSHs (encode_json (scalar, *SvJSON (self))); +{ + struct json json = { *SvJSON (self) }; + XPUSHs (encode_json (scalar, &json)); +} void decode (SV *self, SV *jsonstr) PPCODE: - XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); +{ + struct json json = { *SvJSON (self) }; + XPUSHs (decode_json (jsonstr, &json, 0)); +} void decode_prefix (SV *self, SV *jsonstr) PPCODE: { UV offset; + struct json json = { *SvJSON (self) }; EXTEND (SP, 2); - PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); + PUSHs (decode_json (jsonstr, &json, &offset)); PUSHs (sv_2mortal (newSVuv (offset))); } PROTOTYPES: ENABLE void to_json (SV *scalar) - ALIAS: - objToJson = 0 PPCODE: - XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); +{ + struct json json = { F_DEFAULT | F_UTF8 }; + XPUSHs (encode_json (scalar, &json)); +} void from_json (SV *jsonstr) - ALIAS: - jsonToObj = 0 PPCODE: - XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); +{ + struct json json = { F_DEFAULT | F_UTF8 }; + XPUSHs (decode_json (jsonstr, &json, 0)); +}