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.57 by root, Mon Aug 13 16:05:42 2007 UTC vs.
Revision 1.65 by root, Sat Oct 13 01:55:31 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 1 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))
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);
406 { 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)
510 // 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
511 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); 542 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
512 543
513 if (to_json) 544 if (to_json)
514 { 545 {
515 int count;
516 dSP; 546 dSP;
517 547
518 ENTER; SAVETMPS; PUSHMARK (SP); 548 ENTER; SAVETMPS; PUSHMARK (SP);
519 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 549 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
520 550
681 U32 depth; // recursion depth 711 U32 depth; // recursion depth
682 U32 maxdepth; // recursion depth limit 712 U32 maxdepth; // recursion depth limit
683} dec_t; 713} dec_t;
684 714
685inline 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
686decode_ws (dec_t *dec) 725decode_ws (dec_t *dec)
687{ 726{
688 for (;;) 727 for (;;)
689 { 728 {
690 char ch = *dec->cur; 729 char ch = *dec->cur;
691 730
692 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 }
693 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 743 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
694 break; 744 break; // parse error, but let higher level handle it, gives better error messages
695 745
696 ++dec->cur; 746 ++dec->cur;
697 } 747 }
698} 748}
699 749
1036 1086
1037 if (*dec->cur != ',') 1087 if (*dec->cur != ',')
1038 ERR (", or ] expected while parsing array"); 1088 ERR (", or ] expected while parsing array");
1039 1089
1040 ++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 }
1041 } 1099 }
1042 1100
1043 DEC_DEC_DEPTH; 1101 DEC_DEC_DEPTH;
1044 return newRV_noinc ((SV *)av); 1102 return newRV_noinc ((SV *)av);
1045 1103
1061 if (*dec->cur == '}') 1119 if (*dec->cur == '}')
1062 ++dec->cur; 1120 ++dec->cur;
1063 else 1121 else
1064 for (;;) 1122 for (;;)
1065 { 1123 {
1066 decode_ws (dec); EXPECT_CH ('"'); 1124 EXPECT_CH ('"');
1067 1125
1068 // heuristic: assume that 1126 // heuristic: assume that
1069 // a) decode_str + hv_store_ent are abysmally slow. 1127 // a) decode_str + hv_store_ent are abysmally slow.
1070 // b) most hash keys are short, simple ascii text. 1128 // b) most hash keys are short, simple ascii text.
1071 // => try to "fast-match" such strings to avoid 1129 // => try to "fast-match" such strings to avoid
1085 if (!key) 1143 if (!key)
1086 goto fail; 1144 goto fail;
1087 1145
1088 decode_ws (dec); EXPECT_CH (':'); 1146 decode_ws (dec); EXPECT_CH (':');
1089 1147
1148 decode_ws (dec);
1090 value = decode_sv (dec); 1149 value = decode_sv (dec);
1091 if (!value) 1150 if (!value)
1092 { 1151 {
1093 SvREFCNT_dec (key); 1152 SvREFCNT_dec (key);
1094 goto fail; 1153 goto fail;
1106 int len = p - key; 1165 int len = p - key;
1107 dec->cur = p + 1; 1166 dec->cur = p + 1;
1108 1167
1109 decode_ws (dec); EXPECT_CH (':'); 1168 decode_ws (dec); EXPECT_CH (':');
1110 1169
1170 decode_ws (dec);
1111 value = decode_sv (dec); 1171 value = decode_sv (dec);
1112 if (!value) 1172 if (!value)
1113 goto fail; 1173 goto fail;
1114 1174
1115 hv_store (hv, key, len, value, 0); 1175 hv_store (hv, key, len, value, 0);
1131 1191
1132 if (*dec->cur != ',') 1192 if (*dec->cur != ',')
1133 ERR (", or } expected while parsing object/hash"); 1193 ERR (", or } expected while parsing object/hash");
1134 1194
1135 ++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 }
1136 } 1204 }
1137 1205
1138 DEC_DEC_DEPTH; 1206 DEC_DEC_DEPTH;
1139 sv = newRV_noinc ((SV *)hv); 1207 sv = newRV_noinc ((SV *)hv);
1140 1208
1205} 1273}
1206 1274
1207static SV * 1275static SV *
1208decode_sv (dec_t *dec) 1276decode_sv (dec_t *dec)
1209{ 1277{
1210 decode_ws (dec);
1211
1212 // the beauty of JSON: you need exactly one character lookahead 1278 // the beauty of JSON: you need exactly one character lookahead
1213 // to parse anything. 1279 // to parse anything.
1214 switch (*dec->cur) 1280 switch (*dec->cur)
1215 { 1281 {
1216 case '"': ++dec->cur; return decode_str (dec); 1282 case '"': ++dec->cur; return decode_str (dec);
1224 1290
1225 case 't': 1291 case 't':
1226 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1292 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1227 { 1293 {
1228 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
1229 return SvREFCNT_inc (json_true); 1298 return SvREFCNT_inc (json_true);
1230 } 1299 }
1231 else 1300 else
1232 ERR ("'true' expected"); 1301 ERR ("'true' expected");
1233 1302
1235 1304
1236 case 'f': 1305 case 'f':
1237 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1306 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1238 { 1307 {
1239 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
1240 return SvREFCNT_inc (json_false); 1312 return SvREFCNT_inc (json_false);
1241 } 1313 }
1242 else 1314 else
1243 ERR ("'false' expected"); 1315 ERR ("'false' expected");
1244 1316
1294 1366
1295 if (dec.json.cb_object || dec.json.cb_sk_object) 1367 if (dec.json.cb_object || dec.json.cb_sk_object)
1296 dec.json.flags |= F_HOOK; 1368 dec.json.flags |= F_HOOK;
1297 1369
1298 *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);
1299 sv = decode_sv (&dec); 1373 sv = decode_sv (&dec);
1300 1374
1301 if (!(offset_return || !sv)) 1375 if (!(offset_return || !sv))
1302 { 1376 {
1303 // check for trailing garbage 1377 // check for trailing garbage
1379 json_boolean_stash = 0; 1453 json_boolean_stash = 0;
1380 1454
1381void new (char *klass) 1455void new (char *klass)
1382 PPCODE: 1456 PPCODE:
1383{ 1457{
1384 HV *stash = !JSON_SLOW || json_stash
1385 ? json_stash
1386 : gv_stashpv ("JSON::XS", 1);
1387 SV *pv = NEWSV (0, sizeof (JSON)); 1458 SV *pv = NEWSV (0, sizeof (JSON));
1388 SvPOK_only (pv); 1459 SvPOK_only (pv);
1389 Zero (SvPVX (pv), 1, JSON); 1460 Zero (SvPVX (pv), 1, JSON);
1390 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1461 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1391 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash))); 1462 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH)));
1392} 1463}
1393 1464
1394void ascii (JSON *self, int enable = 1) 1465void ascii (JSON *self, int enable = 1)
1395 ALIAS: 1466 ALIAS:
1396 ascii = F_ASCII 1467 ascii = F_ASCII
1403 pretty = F_PRETTY 1474 pretty = F_PRETTY
1404 allow_nonref = F_ALLOW_NONREF 1475 allow_nonref = F_ALLOW_NONREF
1405 shrink = F_SHRINK 1476 shrink = F_SHRINK
1406 allow_blessed = F_ALLOW_BLESSED 1477 allow_blessed = F_ALLOW_BLESSED
1407 convert_blessed = F_CONV_BLESSED 1478 convert_blessed = F_CONV_BLESSED
1479 relaxed = F_RELAXED
1408 PPCODE: 1480 PPCODE:
1409{ 1481{
1410 if (enable) 1482 if (enable)
1411 self->flags |= ix; 1483 self->flags |= ix;
1412 else 1484 else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines