ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.56 by root, Thu Jul 26 11:33:35 2007 UTC vs.
Revision 1.60 by root, Mon Aug 13 16:19:13 2007 UTC

57# define inline static 57# define inline static
58#endif 58#endif
59 59
60#define expect_false(expr) expect ((expr) != 0, 0) 60#define expect_false(expr) expect ((expr) != 0, 0)
61#define expect_true(expr) expect ((expr) != 0, 1) 61#define expect_true(expr) expect ((expr) != 0, 1)
62
63#ifdef USE_ITHREADS
64# define JSON_SLOW 1
65# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
66#else
67# define JSON_SLOW 0
68# define JSON_STASH json_stash
69#endif
62 70
63static HV *json_stash, *json_boolean_stash; // JSON::XS:: 71static HV *json_stash, *json_boolean_stash; // JSON::XS::
64static SV *json_true, *json_false; 72static SV *json_true, *json_false;
65 73
66typedef struct { 74typedef struct {
478 SvGETMAGIC (sv); 486 SvGETMAGIC (sv);
479 svt = SvTYPE (sv); 487 svt = SvTYPE (sv);
480 488
481 if (expect_false (SvOBJECT (sv))) 489 if (expect_false (SvOBJECT (sv)))
482 { 490 {
491 HV *stash = !JSON_SLOW || json_boolean_stash
492 ? json_boolean_stash
493 : gv_stashpv ("JSON::XS::Boolean", 1);
494
483 if (SvSTASH (sv) == json_boolean_stash) 495 if (SvSTASH (sv) == stash)
484 { 496 {
485 if (SvIV (sv)) 497 if (SvIV (sv))
486 encode_str (enc, "true", 4, 0); 498 encode_str (enc, "true", 4, 0);
487 else 499 else
488 encode_str (enc, "false", 5, 0); 500 encode_str (enc, "false", 5, 0);
500 // we re-bless the reference to get overload and other niceties right 512 // we re-bless the reference to get overload and other niceties right
501 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); 513 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
502 514
503 if (to_json) 515 if (to_json)
504 { 516 {
505 int count;
506 dSP; 517 dSP;
507 518
508 ENTER; SAVETMPS; PUSHMARK (SP); 519 ENTER; SAVETMPS; PUSHMARK (SP);
509 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 520 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
510 521
1214 1225
1215 case 't': 1226 case 't':
1216 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1227 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1217 { 1228 {
1218 dec->cur += 4; 1229 dec->cur += 4;
1230#if JSON_SLOW
1231 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1232#endif
1219 return SvREFCNT_inc (json_true); 1233 return SvREFCNT_inc (json_true);
1220 } 1234 }
1221 else 1235 else
1222 ERR ("'true' expected"); 1236 ERR ("'true' expected");
1223 1237
1225 1239
1226 case 'f': 1240 case 'f':
1227 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1241 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1228 { 1242 {
1229 dec->cur += 5; 1243 dec->cur += 5;
1244#if JSON_SLOW
1245 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1246#endif
1230 return SvREFCNT_inc (json_false); 1247 return SvREFCNT_inc (json_false);
1231 } 1248 }
1232 else 1249 else
1233 ERR ("'false' expected"); 1250 ERR ("'false' expected");
1234 1251
1361 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1378 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1362} 1379}
1363 1380
1364PROTOTYPES: DISABLE 1381PROTOTYPES: DISABLE
1365 1382
1383void CLONE (...)
1384 CODE:
1385 json_stash = 0;
1386 json_boolean_stash = 0;
1387
1366void new (char *klass) 1388void new (char *klass)
1367 PPCODE: 1389 PPCODE:
1368{ 1390{
1369 SV *pv = NEWSV (0, sizeof (JSON)); 1391 SV *pv = NEWSV (0, sizeof (JSON));
1370 SvPOK_only (pv); 1392 SvPOK_only (pv);
1371 Zero (SvPVX (pv), 1, JSON); 1393 Zero (SvPVX (pv), 1, JSON);
1372 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1394 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1373 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1395 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH)));
1374} 1396}
1375 1397
1376void ascii (JSON *self, int enable = 1) 1398void ascii (JSON *self, int enable = 1)
1377 ALIAS: 1399 ALIAS:
1378 ascii = F_ASCII 1400 ascii = F_ASCII

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines