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.66 by root, Sun Nov 25 19:02:42 2007 UTC

27#define F_SPACE_AFTER 0x00000040UL 27#define F_SPACE_AFTER 0x00000040UL
28#define F_ALLOW_NONREF 0x00000100UL 28#define F_ALLOW_NONREF 0x00000100UL
29#define F_SHRINK 0x00000200UL 29#define F_SHRINK 0x00000200UL
30#define F_ALLOW_BLESSED 0x00000400UL 30#define F_ALLOW_BLESSED 0x00000400UL
31#define F_CONV_BLESSED 0x00000800UL 31#define F_CONV_BLESSED 0x00000800UL
32#define F_RELAXED 0x00001000UL
33
32#define F_MAXDEPTH 0xf8000000UL 34#define F_MAXDEPTH 0xf8000000UL
33#define S_MAXDEPTH 27 35#define S_MAXDEPTH 27
34#define F_MAXSIZE 0x01f00000UL 36#define F_MAXSIZE 0x01f00000UL
35#define S_MAXSIZE 20 37#define S_MAXSIZE 20
36#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 38#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
57# define inline static 59# define inline static
58#endif 60#endif
59 61
60#define expect_false(expr) expect ((expr) != 0, 0) 62#define expect_false(expr) expect ((expr) != 0, 0)
61#define expect_true(expr) expect ((expr) != 0, 1) 63#define expect_true(expr) expect ((expr) != 0, 1)
64
65#ifdef USE_ITHREADS
66# define JSON_SLOW 1
67# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
68#else
69# define JSON_SLOW 0
70# define JSON_STASH json_stash
71#endif
62 72
63static HV *json_stash, *json_boolean_stash; // JSON::XS:: 73static HV *json_stash, *json_boolean_stash; // JSON::XS::
64static SV *json_true, *json_false; 74static SV *json_true, *json_false;
65 75
66typedef struct { 76typedef struct {
302 int i, len = av_len (av); 312 int i, len = av_len (av);
303 313
304 if (enc->indent >= enc->maxdepth) 314 if (enc->indent >= enc->maxdepth)
305 croak ("data structure too deep (hit recursion limit)"); 315 croak ("data structure too deep (hit recursion limit)");
306 316
307 encode_ch (enc, '['); encode_nl (enc); 317 encode_ch (enc, '[');
308 ++enc->indent; 318
319 if (len >= 0)
320 {
321 encode_nl (enc); ++enc->indent;
309 322
310 for (i = 0; i <= len; ++i) 323 for (i = 0; i <= len; ++i)
311 { 324 {
312 SV **svp = av_fetch (av, i, 0); 325 SV **svp = av_fetch (av, i, 0);
313 326
314 encode_indent (enc); 327 encode_indent (enc);
315 328
316 if (svp) 329 if (svp)
317 encode_sv (enc, *svp); 330 encode_sv (enc, *svp);
318 else 331 else
319 encode_str (enc, "null", 4, 0); 332 encode_str (enc, "null", 4, 0);
320 333
321 if (i < len) 334 if (i < len)
322 encode_comma (enc); 335 encode_comma (enc);
323 } 336 }
324 337
338 encode_nl (enc); --enc->indent; encode_indent (enc);
339 }
340
325 encode_nl (enc); 341 encode_ch (enc, ']');
326
327 --enc->indent;
328 encode_indent (enc); encode_ch (enc, ']');
329} 342}
330 343
331static void 344static void
332encode_he (enc_t *enc, HE *he) 345encode_hk (enc_t *enc, HE *he)
333{ 346{
334 encode_ch (enc, '"'); 347 encode_ch (enc, '"');
335 348
336 if (HeKLEN (he) == HEf_SVKEY) 349 if (HeKLEN (he) == HEf_SVKEY)
337 { 350 {
350 encode_ch (enc, '"'); 363 encode_ch (enc, '"');
351 364
352 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 365 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
353 encode_ch (enc, ':'); 366 encode_ch (enc, ':');
354 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 367 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
355 encode_sv (enc, HeVAL (he));
356} 368}
357 369
358// compare hash entries, used when all keys are bytestrings 370// compare hash entries, used when all keys are bytestrings
359static int 371static int
360he_cmp_fast (const void *a_, const void *b_) 372he_cmp_fast (const void *a_, const void *b_)
365 HE *b = *(HE **)b_; 377 HE *b = *(HE **)b_;
366 378
367 STRLEN la = HeKLEN (a); 379 STRLEN la = HeKLEN (a);
368 STRLEN lb = HeKLEN (b); 380 STRLEN lb = HeKLEN (b);
369 381
370 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 382 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
371 cmp = la - lb; 383 cmp = lb - la;
372 384
373 return cmp; 385 return cmp;
374} 386}
375 387
376// compare hash entries, used when some keys are sv's or utf-x 388// compare hash entries, used when some keys are sv's or utf-x
377static int 389static int
378he_cmp_slow (const void *a, const void *b) 390he_cmp_slow (const void *a, const void *b)
379{ 391{
380 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 392 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
381} 393}
382 394
383static void 395static void
384encode_hv (enc_t *enc, HV *hv) 396encode_hv (enc_t *enc, HV *hv)
385{ 397{
398 HE *he;
386 int count, i; 399 int count;
387 400
388 if (enc->indent >= enc->maxdepth) 401 if (enc->indent >= enc->maxdepth)
389 croak ("data structure too deep (hit recursion limit)"); 402 croak ("data structure too deep (hit recursion limit)");
390 403
391 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 404 encode_ch (enc, '{');
392 405
393 if ((count = hv_iterinit (hv)))
394 {
395 // for canonical output we have to sort by keys first 406 // for canonical output we have to sort by keys first
396 // actually, this is mostly due to the stupid so-called 407 // actually, this is mostly due to the stupid so-called
397 // security workaround added somewhere in 5.8.x. 408 // security workaround added somewhere in 5.8.x.
398 // that randomises hash orderings 409 // that randomises hash orderings
399 if (enc->json.flags & F_CANONICAL) 410 if (enc->json.flags & F_CANONICAL)
411 {
412 int count = hv_iterinit (hv);
413
414 if (SvMAGICAL (hv))
400 { 415 {
416 // need to count by iterating. could improve by dynamically building the vector below
417 // but I don't care for the speed of this special case.
418 // note also that we will run into undefined behaviour when the two iterations
419 // do not result in the same count, something I might care for in some later release.
420
421 count = 0;
422 while (hv_iternext (hv))
423 ++count;
424
425 hv_iterinit (hv);
426 }
427
428 if (count)
429 {
401 int fast = 1; 430 int i, fast = 1;
402 HE *he;
403#if defined(__BORLANDC__) || defined(_MSC_VER) 431#if defined(__BORLANDC__) || defined(_MSC_VER)
404 HE **hes = _alloca (count * sizeof (HE)); 432 HE **hes = _alloca (count * sizeof (HE));
405#else 433#else
406 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 434 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
407#endif 435#endif
434 462
435 FREETMPS; 463 FREETMPS;
436 LEAVE; 464 LEAVE;
437 } 465 }
438 466
439 for (i = 0; i < count; ++i) 467 encode_nl (enc); ++enc->indent;
468
469 while (count--)
440 { 470 {
441 encode_indent (enc); 471 encode_indent (enc);
472 he = hes [count];
442 encode_he (enc, hes [i]); 473 encode_hk (enc, he);
474 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
443 475
444 if (i < count - 1) 476 if (count)
445 encode_comma (enc); 477 encode_comma (enc);
446 } 478 }
447 479
448 encode_nl (enc); 480 encode_nl (enc); --enc->indent; encode_indent (enc);
449 } 481 }
482 }
450 else 483 else
484 {
485 if (hv_iterinit (hv) || SvMAGICAL (hv))
486 if ((he = hv_iternext (hv)))
451 { 487 {
452 HE *he = hv_iternext (hv); 488 encode_nl (enc); ++enc->indent;
453 489
454 for (;;) 490 for (;;)
455 { 491 {
456 encode_indent (enc); 492 encode_indent (enc);
457 encode_he (enc, he); 493 encode_hk (enc, he);
494 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
458 495
459 if (!(he = hv_iternext (hv))) 496 if (!(he = hv_iternext (hv)))
460 break; 497 break;
461 498
462 encode_comma (enc); 499 encode_comma (enc);
463 } 500 }
464 501
465 encode_nl (enc); 502 encode_nl (enc); --enc->indent; encode_indent (enc);
466 } 503 }
467 } 504 }
468 505
469 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 506 encode_ch (enc, '}');
470} 507}
471 508
472// encode objects, arrays and special \0=false and \1=true values. 509// encode objects, arrays and special \0=false and \1=true values.
473static void 510static void
474encode_rv (enc_t *enc, SV *sv) 511encode_rv (enc_t *enc, SV *sv)
478 SvGETMAGIC (sv); 515 SvGETMAGIC (sv);
479 svt = SvTYPE (sv); 516 svt = SvTYPE (sv);
480 517
481 if (expect_false (SvOBJECT (sv))) 518 if (expect_false (SvOBJECT (sv)))
482 { 519 {
520 HV *stash = !JSON_SLOW || json_boolean_stash
521 ? json_boolean_stash
522 : gv_stashpv ("JSON::XS::Boolean", 1);
523
483 if (SvSTASH (sv) == json_boolean_stash) 524 if (SvSTASH (sv) == stash)
484 { 525 {
485 if (SvIV (sv)) 526 if (SvIV (sv))
486 encode_str (enc, "true", 4, 0); 527 encode_str (enc, "true", 4, 0);
487 else 528 else
488 encode_str (enc, "false", 5, 0); 529 encode_str (enc, "false", 5, 0);
500 // we re-bless the reference to get overload and other niceties right 541 // we re-bless the reference to get overload and other niceties right
501 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); 542 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
502 543
503 if (to_json) 544 if (to_json)
504 { 545 {
505 int count;
506 dSP; 546 dSP;
507 547
508 ENTER; SAVETMPS; PUSHMARK (SP); 548 ENTER; SAVETMPS; PUSHMARK (SP);
509 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 549 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
510 550
671 U32 depth; // recursion depth 711 U32 depth; // recursion depth
672 U32 maxdepth; // recursion depth limit 712 U32 maxdepth; // recursion depth limit
673} dec_t; 713} dec_t;
674 714
675inline void 715inline void
716decode_comment (dec_t *dec)
717{
718 // only '#'-style comments allowed a.t.m.
719
720 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
721 ++dec->cur;
722}
723
724inline void
676decode_ws (dec_t *dec) 725decode_ws (dec_t *dec)
677{ 726{
678 for (;;) 727 for (;;)
679 { 728 {
680 char ch = *dec->cur; 729 char ch = *dec->cur;
681 730
682 if (ch > 0x20 731 if (ch > 0x20)
732 {
733 if (expect_false (ch == '#'))
734 {
735 if (dec->json.flags & F_RELAXED)
736 decode_comment (dec);
737 else
738 break;
739 }
740 else
741 break;
742 }
683 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 743 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
684 break; 744 break; // parse error, but let higher level handle it, gives better error messages
685 745
686 ++dec->cur; 746 ++dec->cur;
687 } 747 }
688} 748}
689 749
1026 1086
1027 if (*dec->cur != ',') 1087 if (*dec->cur != ',')
1028 ERR (", or ] expected while parsing array"); 1088 ERR (", or ] expected while parsing array");
1029 1089
1030 ++dec->cur; 1090 ++dec->cur;
1091
1092 decode_ws (dec);
1093
1094 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1095 {
1096 ++dec->cur;
1097 break;
1098 }
1031 } 1099 }
1032 1100
1033 DEC_DEC_DEPTH; 1101 DEC_DEC_DEPTH;
1034 return newRV_noinc ((SV *)av); 1102 return newRV_noinc ((SV *)av);
1035 1103
1051 if (*dec->cur == '}') 1119 if (*dec->cur == '}')
1052 ++dec->cur; 1120 ++dec->cur;
1053 else 1121 else
1054 for (;;) 1122 for (;;)
1055 { 1123 {
1056 decode_ws (dec); EXPECT_CH ('"'); 1124 EXPECT_CH ('"');
1057 1125
1058 // heuristic: assume that 1126 // heuristic: assume that
1059 // a) decode_str + hv_store_ent are abysmally slow. 1127 // a) decode_str + hv_store_ent are abysmally slow.
1060 // b) most hash keys are short, simple ascii text. 1128 // b) most hash keys are short, simple ascii text.
1061 // => try to "fast-match" such strings to avoid 1129 // => try to "fast-match" such strings to avoid
1075 if (!key) 1143 if (!key)
1076 goto fail; 1144 goto fail;
1077 1145
1078 decode_ws (dec); EXPECT_CH (':'); 1146 decode_ws (dec); EXPECT_CH (':');
1079 1147
1148 decode_ws (dec);
1080 value = decode_sv (dec); 1149 value = decode_sv (dec);
1081 if (!value) 1150 if (!value)
1082 { 1151 {
1083 SvREFCNT_dec (key); 1152 SvREFCNT_dec (key);
1084 goto fail; 1153 goto fail;
1096 int len = p - key; 1165 int len = p - key;
1097 dec->cur = p + 1; 1166 dec->cur = p + 1;
1098 1167
1099 decode_ws (dec); EXPECT_CH (':'); 1168 decode_ws (dec); EXPECT_CH (':');
1100 1169
1170 decode_ws (dec);
1101 value = decode_sv (dec); 1171 value = decode_sv (dec);
1102 if (!value) 1172 if (!value)
1103 goto fail; 1173 goto fail;
1104 1174
1105 hv_store (hv, key, len, value, 0); 1175 hv_store (hv, key, len, value, 0);
1121 1191
1122 if (*dec->cur != ',') 1192 if (*dec->cur != ',')
1123 ERR (", or } expected while parsing object/hash"); 1193 ERR (", or } expected while parsing object/hash");
1124 1194
1125 ++dec->cur; 1195 ++dec->cur;
1196
1197 decode_ws (dec);
1198
1199 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1200 {
1201 ++dec->cur;
1202 break;
1203 }
1126 } 1204 }
1127 1205
1128 DEC_DEC_DEPTH; 1206 DEC_DEC_DEPTH;
1129 sv = newRV_noinc ((SV *)hv); 1207 sv = newRV_noinc ((SV *)hv);
1130 1208
1195} 1273}
1196 1274
1197static SV * 1275static SV *
1198decode_sv (dec_t *dec) 1276decode_sv (dec_t *dec)
1199{ 1277{
1200 decode_ws (dec);
1201
1202 // the beauty of JSON: you need exactly one character lookahead 1278 // the beauty of JSON: you need exactly one character lookahead
1203 // to parse anything. 1279 // to parse anything.
1204 switch (*dec->cur) 1280 switch (*dec->cur)
1205 { 1281 {
1206 case '"': ++dec->cur; return decode_str (dec); 1282 case '"': ++dec->cur; return decode_str (dec);
1214 1290
1215 case 't': 1291 case 't':
1216 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1292 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1217 { 1293 {
1218 dec->cur += 4; 1294 dec->cur += 4;
1295#if JSON_SLOW
1296 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1297#endif
1219 return SvREFCNT_inc (json_true); 1298 return SvREFCNT_inc (json_true);
1220 } 1299 }
1221 else 1300 else
1222 ERR ("'true' expected"); 1301 ERR ("'true' expected");
1223 1302
1225 1304
1226 case 'f': 1305 case 'f':
1227 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1306 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1228 { 1307 {
1229 dec->cur += 5; 1308 dec->cur += 5;
1309#if JSON_SLOW
1310 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1311#endif
1230 return SvREFCNT_inc (json_false); 1312 return SvREFCNT_inc (json_false);
1231 } 1313 }
1232 else 1314 else
1233 ERR ("'false' expected"); 1315 ERR ("'false' expected");
1234 1316
1284 1366
1285 if (dec.json.cb_object || dec.json.cb_sk_object) 1367 if (dec.json.cb_object || dec.json.cb_sk_object)
1286 dec.json.flags |= F_HOOK; 1368 dec.json.flags |= F_HOOK;
1287 1369
1288 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1370 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1371
1372 decode_ws (&dec);
1289 sv = decode_sv (&dec); 1373 sv = decode_sv (&dec);
1290 1374
1291 if (!(offset_return || !sv)) 1375 if (!(offset_return || !sv))
1292 { 1376 {
1293 // check for trailing garbage 1377 // check for trailing garbage
1361 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1445 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1362} 1446}
1363 1447
1364PROTOTYPES: DISABLE 1448PROTOTYPES: DISABLE
1365 1449
1450void CLONE (...)
1451 CODE:
1452 json_stash = 0;
1453 json_boolean_stash = 0;
1454
1366void new (char *klass) 1455void new (char *klass)
1367 PPCODE: 1456 PPCODE:
1368{ 1457{
1369 SV *pv = NEWSV (0, sizeof (JSON)); 1458 SV *pv = NEWSV (0, sizeof (JSON));
1370 SvPOK_only (pv); 1459 SvPOK_only (pv);
1371 Zero (SvPVX (pv), 1, JSON); 1460 Zero (SvPVX (pv), 1, JSON);
1372 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1461 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1373 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1462 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH)));
1374} 1463}
1375 1464
1376void ascii (JSON *self, int enable = 1) 1465void ascii (JSON *self, int enable = 1)
1377 ALIAS: 1466 ALIAS:
1378 ascii = F_ASCII 1467 ascii = F_ASCII
1385 pretty = F_PRETTY 1474 pretty = F_PRETTY
1386 allow_nonref = F_ALLOW_NONREF 1475 allow_nonref = F_ALLOW_NONREF
1387 shrink = F_SHRINK 1476 shrink = F_SHRINK
1388 allow_blessed = F_ALLOW_BLESSED 1477 allow_blessed = F_ALLOW_BLESSED
1389 convert_blessed = F_CONV_BLESSED 1478 convert_blessed = F_CONV_BLESSED
1479 relaxed = F_RELAXED
1390 PPCODE: 1480 PPCODE:
1391{ 1481{
1392 if (enable) 1482 if (enable)
1393 self->flags |= ix; 1483 self->flags |= ix;
1394 else 1484 else
1395 self->flags &= ~ix; 1485 self->flags &= ~ix;
1396 1486
1397 XPUSHs (ST (0)); 1487 XPUSHs (ST (0));
1398} 1488}
1399 1489
1490void get_ascii (JSON *self)
1491 ALIAS:
1492 get_ascii = F_ASCII
1493 get_latin1 = F_LATIN1
1494 get_utf8 = F_UTF8
1495 get_indent = F_INDENT
1496 get_canonical = F_CANONICAL
1497 get_space_before = F_SPACE_BEFORE
1498 get_space_after = F_SPACE_AFTER
1499 get_pretty = F_PRETTY
1500 get_allow_nonref = F_ALLOW_NONREF
1501 get_shrink = F_SHRINK
1502 get_allow_blessed = F_ALLOW_BLESSED
1503 get_convert_blessed = F_CONV_BLESSED
1504 get_relaxed = F_RELAXED
1505 PPCODE:
1506 XPUSHs (boolSV (self->flags & ix));
1507
1400void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1508void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1401 PPCODE: 1509 PPCODE:
1402{ 1510{
1403 UV log2 = 0; 1511 UV log2 = 0;
1404 1512
1410 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1518 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1411 1519
1412 XPUSHs (ST (0)); 1520 XPUSHs (ST (0));
1413} 1521}
1414 1522
1523int get_max_depth (JSON *self)
1524 CODE:
1525 RETVAL = DEC_DEPTH (self->flags);
1526 OUTPUT:
1527 RETVAL
1528
1415void max_size (JSON *self, UV max_size = 0) 1529void max_size (JSON *self, UV max_size = 0)
1416 PPCODE: 1530 PPCODE:
1417{ 1531{
1418 UV log2 = 0; 1532 UV log2 = 0;
1419 1533
1425 1539
1426 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1540 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1427 1541
1428 XPUSHs (ST (0)); 1542 XPUSHs (ST (0));
1429} 1543}
1544
1545int get_max_size (JSON *self)
1546 CODE:
1547 RETVAL = DEC_SIZE (self->flags);
1548 OUTPUT:
1549 RETVAL
1430 1550
1431void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1551void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1432 PPCODE: 1552 PPCODE:
1433{ 1553{
1434 SvREFCNT_dec (self->cb_object); 1554 SvREFCNT_dec (self->cb_object);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines