--- JSON-XS/XS.xs 2007/07/01 14:08:03 1.47 +++ JSON-XS/XS.xs 2007/07/01 22:20:00 1.48 @@ -61,23 +61,14 @@ static HV *json_stash, *json_boolean_stash; // JSON::XS:: static SV *json_true, *json_false; -typedef struct json { +typedef struct { U32 flags; -} JSON__XS; +} JSON; ///////////////////////////////////////////////////////////////////////////// // utility functions -static UV * -SvJSON (SV *sv) -{ - if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) - croak ("object is not of type JSON::XS"); - - return &SvUVX (SvRV (sv)); -} - -static void +inline void shrink (SV *sv) { sv_utf8_downgrade (sv, 1); @@ -122,7 +113,7 @@ char *cur; // SvPVX (sv) + current output position char *end; // SvEND (sv) SV *sv; // result scalar - struct json json; + JSON json; U32 indent; // indentation level U32 maxdepth; // max. indentation/recursion level } enc_t; @@ -620,7 +611,7 @@ } static SV * -encode_json (SV *scalar, struct json *json) +encode_json (SV *scalar, JSON *json) { enc_t enc; @@ -658,7 +649,7 @@ char *cur; // current parser pointer char *end; // end of input string const char *err; // parse error, if != 0 - struct json json; + JSON json; U32 depth; // recursion depth U32 maxdepth; // recursion depth limit } dec_t; @@ -1171,7 +1162,7 @@ } static SV * -decode_json (SV *string, struct json *json, UV *offset_return) +decode_json (SV *string, JSON *json, UV *offset_return) { dec_t dec; UV offset; @@ -1276,13 +1267,17 @@ PROTOTYPES: DISABLE -SV *new (char *dummy) - CODE: - RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); - OUTPUT: - RETVAL +void new (char *klass) + PPCODE: +{ + SV *pv = NEWSV (0, sizeof (JSON)); + SvPOK_only (pv); + Zero (SvPVX (pv), 1, sizeof (JSON)); + ((JSON *)SvPVX (pv))->flags = F_DEFAULT; + XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); +} -SV *ascii (SV *self, int enable = 1) +void ascii (JSON *self, int enable = 1) ALIAS: ascii = F_ASCII latin1 = F_LATIN1 @@ -1296,23 +1291,19 @@ shrink = F_SHRINK allow_blessed = F_ALLOW_BLESSED convert_blessed = F_CONV_BLESSED - CODE: + PPCODE: { - UV *uv = SvJSON (self); if (enable) - *uv |= ix; + self->flags |= ix; else - *uv &= ~ix; + self->flags &= ~ix; - RETVAL = newSVsv (self); + XPUSHs (ST (0)); } - OUTPUT: - RETVAL -SV *max_depth (SV *self, UV max_depth = 0x80000000UL) - CODE: +void max_depth (JSON *self, UV max_depth = 0x80000000UL) + PPCODE: { - UV *uv = SvJSON (self); UV log2 = 0; if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; @@ -1320,17 +1311,14 @@ while ((1UL << log2) < max_depth) ++log2; - *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); + self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); - RETVAL = newSVsv (self); + XPUSHs (ST (0)); } - OUTPUT: - RETVAL -SV *max_size (SV *self, UV max_size = 0) - CODE: +void max_size (JSON *self, UV max_size = 0) + PPCODE: { - UV *uv = SvJSON (self); UV log2 = 0; if (max_size > 0x80000000UL) max_size = 0x80000000UL; @@ -1339,34 +1327,25 @@ while ((1UL << log2) < max_size) ++log2; - *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); + self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); - RETVAL = newSVsv (self); + XPUSHs (ST (0)); } - OUTPUT: - RETVAL -void encode (SV *self, SV *scalar) +void encode (JSON *self, SV *scalar) PPCODE: -{ - struct json json = { *SvJSON (self) }; - XPUSHs (encode_json (scalar, &json)); -} + XPUSHs (encode_json (scalar, self)); -void decode (SV *self, SV *jsonstr) +void decode (JSON *self, SV *jsonstr) PPCODE: -{ - struct json json = { *SvJSON (self) }; - XPUSHs (decode_json (jsonstr, &json, 0)); -} + XPUSHs (decode_json (jsonstr, self, 0)); -void decode_prefix (SV *self, SV *jsonstr) +void decode_prefix (JSON *self, SV *jsonstr) PPCODE: { UV offset; - struct json json = { *SvJSON (self) }; EXTEND (SP, 2); - PUSHs (decode_json (jsonstr, &json, &offset)); + PUSHs (decode_json (jsonstr, self, &offset)); PUSHs (sv_2mortal (newSVuv (offset))); } @@ -1375,14 +1354,14 @@ void to_json (SV *scalar) PPCODE: { - struct json json = { F_DEFAULT | F_UTF8 }; + JSON json = { F_DEFAULT | F_UTF8 }; XPUSHs (encode_json (scalar, &json)); } void from_json (SV *jsonstr) PPCODE: { - struct json json = { F_DEFAULT | F_UTF8 }; + JSON json = { F_DEFAULT | F_UTF8 }; XPUSHs (decode_json (jsonstr, &json, 0)); }