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.58 by root, Mon Aug 13 16:06:25 2007 UTC vs.
Revision 1.68 by root, Thu Nov 29 08:37:11 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
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)
62 64
63#ifdef USE_ITHREADS 65#ifdef USE_ITHREADS
64# define JSON_SLOW 1 66# define JSON_SLOW 1
67# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
65#else 68#else
66# define JSON_SLOW 0 69# define JSON_SLOW 0
70# define JSON_STASH json_stash
67#endif 71#endif
68 72
69static HV *json_stash, *json_boolean_stash; // JSON::XS:: 73static HV *json_stash, *json_boolean_stash; // JSON::XS::
70static SV *json_true, *json_false; 74static SV *json_true, *json_false;
71 75
308 int i, len = av_len (av); 312 int i, len = av_len (av);
309 313
310 if (enc->indent >= enc->maxdepth) 314 if (enc->indent >= enc->maxdepth)
311 croak ("data structure too deep (hit recursion limit)"); 315 croak ("data structure too deep (hit recursion limit)");
312 316
313 encode_ch (enc, '['); encode_nl (enc); 317 encode_ch (enc, '[');
314 ++enc->indent; 318
319 if (len >= 0)
320 {
321 encode_nl (enc); ++enc->indent;
315 322
316 for (i = 0; i <= len; ++i) 323 for (i = 0; i <= len; ++i)
317 { 324 {
318 SV **svp = av_fetch (av, i, 0); 325 SV **svp = av_fetch (av, i, 0);
319 326
320 encode_indent (enc); 327 encode_indent (enc);
321 328
322 if (svp) 329 if (svp)
323 encode_sv (enc, *svp); 330 encode_sv (enc, *svp);
324 else 331 else
325 encode_str (enc, "null", 4, 0); 332 encode_str (enc, "null", 4, 0);
326 333
327 if (i < len) 334 if (i < len)
328 encode_comma (enc); 335 encode_comma (enc);
329 } 336 }
330 337
338 encode_nl (enc); --enc->indent; encode_indent (enc);
339 }
340
331 encode_nl (enc); 341 encode_ch (enc, ']');
332
333 --enc->indent;
334 encode_indent (enc); encode_ch (enc, ']');
335} 342}
336 343
337static void 344static void
338encode_he (enc_t *enc, HE *he) 345encode_hk (enc_t *enc, HE *he)
339{ 346{
340 encode_ch (enc, '"'); 347 encode_ch (enc, '"');
341 348
342 if (HeKLEN (he) == HEf_SVKEY) 349 if (HeKLEN (he) == HEf_SVKEY)
343 { 350 {
356 encode_ch (enc, '"'); 363 encode_ch (enc, '"');
357 364
358 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 365 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
359 encode_ch (enc, ':'); 366 encode_ch (enc, ':');
360 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 367 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
361 encode_sv (enc, HeVAL (he));
362} 368}
363 369
364// compare hash entries, used when all keys are bytestrings 370// compare hash entries, used when all keys are bytestrings
365static int 371static int
366he_cmp_fast (const void *a_, const void *b_) 372he_cmp_fast (const void *a_, const void *b_)
371 HE *b = *(HE **)b_; 377 HE *b = *(HE **)b_;
372 378
373 STRLEN la = HeKLEN (a); 379 STRLEN la = HeKLEN (a);
374 STRLEN lb = HeKLEN (b); 380 STRLEN lb = HeKLEN (b);
375 381
376 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 382 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
377 cmp = la - lb; 383 cmp = lb - la;
378 384
379 return cmp; 385 return cmp;
380} 386}
381 387
382// 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
383static int 389static int
384he_cmp_slow (const void *a, const void *b) 390he_cmp_slow (const void *a, const void *b)
385{ 391{
386 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 392 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
387} 393}
388 394
389static void 395static void
390encode_hv (enc_t *enc, HV *hv) 396encode_hv (enc_t *enc, HV *hv)
391{ 397{
398 HE *he;
392 int count, i; 399 int count;
393 400
394 if (enc->indent >= enc->maxdepth) 401 if (enc->indent >= enc->maxdepth)
395 croak ("data structure too deep (hit recursion limit)"); 402 croak ("data structure too deep (hit recursion limit)");
396 403
397 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 404 encode_ch (enc, '{');
398 405
399 if ((count = hv_iterinit (hv)))
400 {
401 // for canonical output we have to sort by keys first 406 // for canonical output we have to sort by keys first
402 // actually, this is mostly due to the stupid so-called 407 // actually, this is mostly due to the stupid so-called
403 // security workaround added somewhere in 5.8.x. 408 // security workaround added somewhere in 5.8.x.
404 // that randomises hash orderings 409 // that randomises hash orderings
405 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))
406 { 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 {
407 int fast = 1; 430 int i, fast = 1;
408 HE *he;
409#if defined(__BORLANDC__) || defined(_MSC_VER) 431#if defined(__BORLANDC__) || defined(_MSC_VER)
410 HE **hes = _alloca (count * sizeof (HE)); 432 HE **hes = _alloca (count * sizeof (HE));
411#else 433#else
412 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
413#endif 435#endif
440 462
441 FREETMPS; 463 FREETMPS;
442 LEAVE; 464 LEAVE;
443 } 465 }
444 466
445 for (i = 0; i < count; ++i) 467 encode_nl (enc); ++enc->indent;
468
469 while (count--)
446 { 470 {
447 encode_indent (enc); 471 encode_indent (enc);
472 he = hes [count];
448 encode_he (enc, hes [i]); 473 encode_hk (enc, he);
474 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
449 475
450 if (i < count - 1) 476 if (count)
451 encode_comma (enc); 477 encode_comma (enc);
452 } 478 }
453 479
454 encode_nl (enc); 480 encode_nl (enc); --enc->indent; encode_indent (enc);
455 } 481 }
482 }
456 else 483 else
484 {
485 if (hv_iterinit (hv) || SvMAGICAL (hv))
486 if ((he = hv_iternext (hv)))
457 { 487 {
458 HE *he = hv_iternext (hv); 488 encode_nl (enc); ++enc->indent;
459 489
460 for (;;) 490 for (;;)
461 { 491 {
462 encode_indent (enc); 492 encode_indent (enc);
463 encode_he (enc, he); 493 encode_hk (enc, he);
494 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
464 495
465 if (!(he = hv_iternext (hv))) 496 if (!(he = hv_iternext (hv)))
466 break; 497 break;
467 498
468 encode_comma (enc); 499 encode_comma (enc);
469 } 500 }
470 501
471 encode_nl (enc); 502 encode_nl (enc); --enc->indent; encode_indent (enc);
472 } 503 }
473 } 504 }
474 505
475 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 506 encode_ch (enc, '}');
476} 507}
477 508
478// encode objects, arrays and special \0=false and \1=true values. 509// encode objects, arrays and special \0=false and \1=true values.
479static void 510static void
480encode_rv (enc_t *enc, SV *sv) 511encode_rv (enc_t *enc, SV *sv)
680 U32 depth; // recursion depth 711 U32 depth; // recursion depth
681 U32 maxdepth; // recursion depth limit 712 U32 maxdepth; // recursion depth limit
682} dec_t; 713} dec_t;
683 714
684inline 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
685decode_ws (dec_t *dec) 725decode_ws (dec_t *dec)
686{ 726{
687 for (;;) 727 for (;;)
688 { 728 {
689 char ch = *dec->cur; 729 char ch = *dec->cur;
690 730
691 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 }
692 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 743 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
693 break; 744 break; // parse error, but let higher level handle it, gives better error messages
694 745
695 ++dec->cur; 746 ++dec->cur;
696 } 747 }
697} 748}
698 749
1035 1086
1036 if (*dec->cur != ',') 1087 if (*dec->cur != ',')
1037 ERR (", or ] expected while parsing array"); 1088 ERR (", or ] expected while parsing array");
1038 1089
1039 ++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 }
1040 } 1099 }
1041 1100
1042 DEC_DEC_DEPTH; 1101 DEC_DEC_DEPTH;
1043 return newRV_noinc ((SV *)av); 1102 return newRV_noinc ((SV *)av);
1044 1103
1060 if (*dec->cur == '}') 1119 if (*dec->cur == '}')
1061 ++dec->cur; 1120 ++dec->cur;
1062 else 1121 else
1063 for (;;) 1122 for (;;)
1064 { 1123 {
1065 decode_ws (dec); EXPECT_CH ('"'); 1124 EXPECT_CH ('"');
1066 1125
1067 // heuristic: assume that 1126 // heuristic: assume that
1068 // a) decode_str + hv_store_ent are abysmally slow. 1127 // a) decode_str + hv_store_ent are abysmally slow.
1069 // b) most hash keys are short, simple ascii text. 1128 // b) most hash keys are short, simple ascii text.
1070 // => try to "fast-match" such strings to avoid 1129 // => try to "fast-match" such strings to avoid
1084 if (!key) 1143 if (!key)
1085 goto fail; 1144 goto fail;
1086 1145
1087 decode_ws (dec); EXPECT_CH (':'); 1146 decode_ws (dec); EXPECT_CH (':');
1088 1147
1148 decode_ws (dec);
1089 value = decode_sv (dec); 1149 value = decode_sv (dec);
1090 if (!value) 1150 if (!value)
1091 { 1151 {
1092 SvREFCNT_dec (key); 1152 SvREFCNT_dec (key);
1093 goto fail; 1153 goto fail;
1105 int len = p - key; 1165 int len = p - key;
1106 dec->cur = p + 1; 1166 dec->cur = p + 1;
1107 1167
1108 decode_ws (dec); EXPECT_CH (':'); 1168 decode_ws (dec); EXPECT_CH (':');
1109 1169
1170 decode_ws (dec);
1110 value = decode_sv (dec); 1171 value = decode_sv (dec);
1111 if (!value) 1172 if (!value)
1112 goto fail; 1173 goto fail;
1113 1174
1114 hv_store (hv, key, len, value, 0); 1175 hv_store (hv, key, len, value, 0);
1130 1191
1131 if (*dec->cur != ',') 1192 if (*dec->cur != ',')
1132 ERR (", or } expected while parsing object/hash"); 1193 ERR (", or } expected while parsing object/hash");
1133 1194
1134 ++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 }
1135 } 1204 }
1136 1205
1137 DEC_DEC_DEPTH; 1206 DEC_DEC_DEPTH;
1138 sv = newRV_noinc ((SV *)hv); 1207 sv = newRV_noinc ((SV *)hv);
1139 1208
1204} 1273}
1205 1274
1206static SV * 1275static SV *
1207decode_sv (dec_t *dec) 1276decode_sv (dec_t *dec)
1208{ 1277{
1209 decode_ws (dec);
1210
1211 // the beauty of JSON: you need exactly one character lookahead 1278 // the beauty of JSON: you need exactly one character lookahead
1212 // to parse anything. 1279 // to parse anything.
1213 switch (*dec->cur) 1280 switch (*dec->cur)
1214 { 1281 {
1215 case '"': ++dec->cur; return decode_str (dec); 1282 case '"': ++dec->cur; return decode_str (dec);
1223 1290
1224 case 't': 1291 case 't':
1225 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1292 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1226 { 1293 {
1227 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
1228 return SvREFCNT_inc (json_true); 1298 return SvREFCNT_inc (json_true);
1229 } 1299 }
1230 else 1300 else
1231 ERR ("'true' expected"); 1301 ERR ("'true' expected");
1232 1302
1234 1304
1235 case 'f': 1305 case 'f':
1236 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1306 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1237 { 1307 {
1238 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
1239 return SvREFCNT_inc (json_false); 1312 return SvREFCNT_inc (json_false);
1240 } 1313 }
1241 else 1314 else
1242 ERR ("'false' expected"); 1315 ERR ("'false' expected");
1243 1316
1293 1366
1294 if (dec.json.cb_object || dec.json.cb_sk_object) 1367 if (dec.json.cb_object || dec.json.cb_sk_object)
1295 dec.json.flags |= F_HOOK; 1368 dec.json.flags |= F_HOOK;
1296 1369
1297 *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);
1298 sv = decode_sv (&dec); 1373 sv = decode_sv (&dec);
1299 1374
1300 if (!(offset_return || !sv)) 1375 if (!(offset_return || !sv))
1301 { 1376 {
1302 // check for trailing garbage 1377 // check for trailing garbage
1378 json_boolean_stash = 0; 1453 json_boolean_stash = 0;
1379 1454
1380void new (char *klass) 1455void new (char *klass)
1381 PPCODE: 1456 PPCODE:
1382{ 1457{
1383 HV *stash = !JSON_SLOW || json_stash
1384 ? json_stash
1385 : gv_stashpv ("JSON::XS", 1);
1386 SV *pv = NEWSV (0, sizeof (JSON)); 1458 SV *pv = NEWSV (0, sizeof (JSON));
1387 SvPOK_only (pv); 1459 SvPOK_only (pv);
1388 Zero (SvPVX (pv), 1, JSON); 1460 Zero (SvPVX (pv), 1, JSON);
1389 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1461 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1390 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash))); 1462 XPUSHs (sv_2mortal (sv_bless (
1463 newRV_noinc (pv),
1464 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1465 )));
1391} 1466}
1392 1467
1393void ascii (JSON *self, int enable = 1) 1468void ascii (JSON *self, int enable = 1)
1394 ALIAS: 1469 ALIAS:
1395 ascii = F_ASCII 1470 ascii = F_ASCII
1402 pretty = F_PRETTY 1477 pretty = F_PRETTY
1403 allow_nonref = F_ALLOW_NONREF 1478 allow_nonref = F_ALLOW_NONREF
1404 shrink = F_SHRINK 1479 shrink = F_SHRINK
1405 allow_blessed = F_ALLOW_BLESSED 1480 allow_blessed = F_ALLOW_BLESSED
1406 convert_blessed = F_CONV_BLESSED 1481 convert_blessed = F_CONV_BLESSED
1482 relaxed = F_RELAXED
1407 PPCODE: 1483 PPCODE:
1408{ 1484{
1409 if (enable) 1485 if (enable)
1410 self->flags |= ix; 1486 self->flags |= ix;
1411 else 1487 else
1412 self->flags &= ~ix; 1488 self->flags &= ~ix;
1413 1489
1414 XPUSHs (ST (0)); 1490 XPUSHs (ST (0));
1415} 1491}
1416 1492
1493void get_ascii (JSON *self)
1494 ALIAS:
1495 get_ascii = F_ASCII
1496 get_latin1 = F_LATIN1
1497 get_utf8 = F_UTF8
1498 get_indent = F_INDENT
1499 get_canonical = F_CANONICAL
1500 get_space_before = F_SPACE_BEFORE
1501 get_space_after = F_SPACE_AFTER
1502 get_allow_nonref = F_ALLOW_NONREF
1503 get_shrink = F_SHRINK
1504 get_allow_blessed = F_ALLOW_BLESSED
1505 get_convert_blessed = F_CONV_BLESSED
1506 get_relaxed = F_RELAXED
1507 PPCODE:
1508 XPUSHs (boolSV (self->flags & ix));
1509
1417void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1510void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1418 PPCODE: 1511 PPCODE:
1419{ 1512{
1420 UV log2 = 0; 1513 UV log2 = 0;
1421 1514
1427 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1520 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1428 1521
1429 XPUSHs (ST (0)); 1522 XPUSHs (ST (0));
1430} 1523}
1431 1524
1525U32 get_max_depth (JSON *self)
1526 CODE:
1527 RETVAL = DEC_DEPTH (self->flags);
1528 OUTPUT:
1529 RETVAL
1530
1432void max_size (JSON *self, UV max_size = 0) 1531void max_size (JSON *self, UV max_size = 0)
1433 PPCODE: 1532 PPCODE:
1434{ 1533{
1435 UV log2 = 0; 1534 UV log2 = 0;
1436 1535
1442 1541
1443 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1542 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1444 1543
1445 XPUSHs (ST (0)); 1544 XPUSHs (ST (0));
1446} 1545}
1546
1547int get_max_size (JSON *self)
1548 CODE:
1549 RETVAL = DEC_SIZE (self->flags);
1550 OUTPUT:
1551 RETVAL
1447 1552
1448void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1553void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1449 PPCODE: 1554 PPCODE:
1450{ 1555{
1451 SvREFCNT_dec (self->cb_object); 1556 SvREFCNT_dec (self->cb_object);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines