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.38 by root, Mon Jun 11 03:18:07 2007 UTC vs.
Revision 1.44 by root, Mon Jun 25 04:08:17 2007 UTC

9 9
10#if defined(__BORLANDC__) || defined(_MSC_VER) 10#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h 11# define snprintf _snprintf // C compilers have this in stdio.h
12#endif 12#endif
13 13
14// some old perls do not have this, try to make it work, no
15// guarentees, though. if it breaks, you get to keep the pieces.
16#ifndef UTF8_MAXBYTES
17# define UTF8_MAXBYTES 13
18#endif
19
14#define F_ASCII 0x00000001UL 20#define F_ASCII 0x00000001UL
15#define F_LATIN1 0x00000002UL 21#define F_LATIN1 0x00000002UL
16#define F_UTF8 0x00000004UL 22#define F_UTF8 0x00000004UL
17#define F_INDENT 0x00000008UL 23#define F_INDENT 0x00000008UL
18#define F_CANONICAL 0x00000010UL 24#define F_CANONICAL 0x00000010UL
19#define F_SPACE_BEFORE 0x00000020UL 25#define F_SPACE_BEFORE 0x00000020UL
20#define F_SPACE_AFTER 0x00000040UL 26#define F_SPACE_AFTER 0x00000040UL
21#define F_ALLOW_NONREF 0x00000100UL 27#define F_ALLOW_NONREF 0x00000100UL
22#define F_SHRINK 0x00000200UL 28#define F_SHRINK 0x00000200UL
29#define F_ALLOW_BLESSED 0x00000400UL
30#define F_CONV_BLESSED 0x00000800UL // NYI
23#define F_MAXDEPTH 0xf8000000UL 31#define F_MAXDEPTH 0xf8000000UL
24#define S_MAXDEPTH 27 32#define S_MAXDEPTH 27
25 33
26#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 34#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
27
28// F_SELFCONVERT? <=> to_json/toJson
29// F_BLESSED? <=> { $__class__$ => }
30 35
31#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 36#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
32#define F_DEFAULT (9UL << S_MAXDEPTH) 37#define F_DEFAULT (9UL << S_MAXDEPTH)
33 38
34#define INIT_SIZE 32 // initial scalar size to be allocated 39#define INIT_SIZE 32 // initial scalar size to be allocated
48#endif 53#endif
49 54
50#define expect_false(expr) expect ((expr) != 0, 0) 55#define expect_false(expr) expect ((expr) != 0, 0)
51#define expect_true(expr) expect ((expr) != 0, 1) 56#define expect_true(expr) expect ((expr) != 0, 1)
52 57
53static HV *json_stash; // JSON::XS:: 58static HV *json_stash, *json_boolean_stash; // JSON::XS::
59static SV *json_true, *json_false;
54 60
55///////////////////////////////////////////////////////////////////////////// 61/////////////////////////////////////////////////////////////////////////////
56// utility functions 62// utility functions
57 63
58static UV * 64static UV *
178 STRLEN clen; 184 STRLEN clen;
179 UV uch; 185 UV uch;
180 186
181 if (is_utf8) 187 if (is_utf8)
182 { 188 {
183 //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
184 uch = decode_utf8 (str, end - str, &clen); 189 uch = decode_utf8 (str, end - str, &clen);
185 if (clen == (STRLEN)-1) 190 if (clen == (STRLEN)-1)
186 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); 191 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
187 } 192 }
188 else 193 else
463 svtype svt; 468 svtype svt;
464 469
465 SvGETMAGIC (sv); 470 SvGETMAGIC (sv);
466 svt = SvTYPE (sv); 471 svt = SvTYPE (sv);
467 472
473 if (expect_false (SvOBJECT (sv)))
474 {
475 if (SvSTASH (sv) == json_boolean_stash)
476 {
477 if (SvIV (sv) == 0)
478 encode_str (enc, "false", 5, 0);
479 else
480 encode_str (enc, "true", 4, 0);
481 }
482 else
483 {
484#if 0
485 if (0 && sv_derived_from (rv, "JSON::Literal"))
486 {
487 // not yet
488 }
489#endif
490 if (enc->flags & F_CONV_BLESSED)
491 {
492 // we re-bless the reference to get overload and other niceties right
493 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1);
494
495 if (to_json)
496 {
497 dSP;
498 ENTER;
499 SAVETMPS;
500 PUSHMARK (SP);
501 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
502
503 // calling with G_SCALAR ensures that we always get a 1 reutrn value
504 // check anyways.
505 PUTBACK;
506 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR));
507 SPAGAIN;
508
509 encode_sv (enc, POPs);
510
511 FREETMPS;
512 LEAVE;
513 }
514 else if (enc->flags & F_ALLOW_BLESSED)
515 encode_str (enc, "null", 4, 0);
516 else
517 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
518 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
519 }
520 else if (enc->flags & F_ALLOW_BLESSED)
521 encode_str (enc, "null", 4, 0);
522 else
523 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled",
524 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
525 }
526 }
468 if (svt == SVt_PVHV) 527 else if (svt == SVt_PVHV)
469 encode_hv (enc, (HV *)sv); 528 encode_hv (enc, (HV *)sv);
470 else if (svt == SVt_PVAV) 529 else if (svt == SVt_PVAV)
471 encode_av (enc, (AV *)sv); 530 encode_av (enc, (AV *)sv);
472 else if (svt < SVt_PVAV) 531 else if (svt < SVt_PVAV)
473 { 532 {
497 encode_str (enc, str, len, SvUTF8 (sv)); 556 encode_str (enc, str, len, SvUTF8 (sv));
498 encode_ch (enc, '"'); 557 encode_ch (enc, '"');
499 } 558 }
500 else if (SvNOKp (sv)) 559 else if (SvNOKp (sv))
501 { 560 {
561 // trust that perl will do the right thing w.r.t. JSON syntax.
502 need (enc, NV_DIG + 32); 562 need (enc, NV_DIG + 32);
503 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 563 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
504 enc->cur += strlen (enc->cur); 564 enc->cur += strlen (enc->cur);
505 } 565 }
506 else if (SvIOKp (sv)) 566 else if (SvIOKp (sv))
507 { 567 {
508 // we assume we can always read an IV as a UV 568 // we assume we can always read an IV as a UV
509 if (SvUV (sv) & ~(UV)0x7fff) 569 if (SvUV (sv) & ~(UV)0x7fff)
510 { 570 {
571 // large integer, use the (rather slow) snprintf way.
511 need (enc, sizeof (UV) * 3); 572 need (enc, sizeof (UV) * 3);
512 enc->cur += 573 enc->cur +=
513 SvIsUV(sv) 574 SvIsUV(sv)
514 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) 575 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
515 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); 576 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
518 { 579 {
519 // optimise the "small number case" 580 // optimise the "small number case"
520 // code will likely be branchless and use only a single multiplication 581 // code will likely be branchless and use only a single multiplication
521 I32 i = SvIV (sv); 582 I32 i = SvIV (sv);
522 U32 u; 583 U32 u;
584 char digit, nz = 0;
523 585
524 need (enc, 6); 586 need (enc, 6);
525 587
526 *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; 588 *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0;
527 u = i < 0 ? -i : i; 589 u = i < 0 ? -i : i;
528 590
529 // convert to 4.28 fixed-point representation 591 // convert to 4.28 fixed-point representation
530 u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits 592 u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits
531 593
532 char digit, nz = 0; 594 // now output digit by digit, each time masking out the integer part
533 595 // and multiplying by 5 while moving the decimal point one to the right,
596 // resulting in a net multiplication by 10.
597 // we always write the digit to memory but conditionally increment
598 // the pointer, to ease the usage of conditional move instructions.
534 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; 599 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5;
535 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; 600 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5;
536 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; 601 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5;
537 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; 602 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5;
538 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; 603 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0'
539 } 604 }
540 } 605 }
541 else if (SvROK (sv)) 606 else if (SvROK (sv))
542 encode_rv (enc, SvRV (sv)); 607 encode_rv (enc, SvRV (sv));
543 else if (!SvOK (sv)) 608 else if (!SvOK (sv))
625decode_4hex (dec_t *dec) 690decode_4hex (dec_t *dec)
626{ 691{
627 signed char d1, d2, d3, d4; 692 signed char d1, d2, d3, d4;
628 unsigned char *cur = (unsigned char *)dec->cur; 693 unsigned char *cur = (unsigned char *)dec->cur;
629 694
630 d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); 695 d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected");
631 d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); 696 d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected");
632 d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); 697 d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected");
633 d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); 698 d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected");
634 699
635 dec->cur += 4; 700 dec->cur += 4;
636 701
637 return ((UV)d1) << 12 702 return ((UV)d1) << 12
638 | ((UV)d2) << 8 703 | ((UV)d2) << 8
861 { 926 {
862 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 927 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
863 if (*start == '-') 928 if (*start == '-')
864 switch (dec->cur - start) 929 switch (dec->cur - start)
865 { 930 {
866 case 2: return newSViv (-( start [1] - '0' )); 931 case 2: return newSViv (-( start [1] - '0' * 1));
867 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 932 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
868 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 933 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
869 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 934 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
870 } 935 }
871 else 936 else
872 switch (dec->cur - start) 937 switch (dec->cur - start)
873 { 938 {
874 case 1: return newSViv ( start [0] - '0' ); 939 case 1: return newSViv ( start [0] - '0' * 1);
875 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 940 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
876 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 941 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
877 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 942 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
878 } 943 }
879 944
886 if (uv < (UV)IV_MIN) 951 if (uv < (UV)IV_MIN)
887 return newSViv (-(IV)uv); 952 return newSViv (-(IV)uv);
888 } 953 }
889 else 954 else
890 return newSVuv (uv); 955 return newSVuv (uv);
956
957 // here would likely be the place for bigint support
891 } 958 }
892 } 959 }
893 960
961 // if we ever support bigint or bigfloat, this is the place for bigfloat
894 return newSVnv (Atof (start)); 962 return newSVnv (Atof (start));
895 963
896fail: 964fail:
897 return 0; 965 return 0;
898} 966}
999 1067
1000static SV * 1068static SV *
1001decode_sv (dec_t *dec) 1069decode_sv (dec_t *dec)
1002{ 1070{
1003 decode_ws (dec); 1071 decode_ws (dec);
1072
1073 // the beauty of JSON: you need exactly one character lookahead
1074 // to parse anything.
1004 switch (*dec->cur) 1075 switch (*dec->cur)
1005 { 1076 {
1006 case '"': ++dec->cur; return decode_str (dec); 1077 case '"': ++dec->cur; return decode_str (dec);
1007 case '[': ++dec->cur; return decode_av (dec); 1078 case '[': ++dec->cur; return decode_av (dec);
1008 case '{': ++dec->cur; return decode_hv (dec); 1079 case '{': ++dec->cur; return decode_hv (dec);
1014 1085
1015 case 't': 1086 case 't':
1016 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1087 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1017 { 1088 {
1018 dec->cur += 4; 1089 dec->cur += 4;
1019 return newSViv (1); 1090 return SvREFCNT_inc (json_true);
1020 } 1091 }
1021 else 1092 else
1022 ERR ("'true' expected"); 1093 ERR ("'true' expected");
1023 1094
1024 break; 1095 break;
1025 1096
1026 case 'f': 1097 case 'f':
1027 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1098 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1028 { 1099 {
1029 dec->cur += 5; 1100 dec->cur += 5;
1030 return newSViv (0); 1101 return SvREFCNT_inc (json_false);
1031 } 1102 }
1032 else 1103 else
1033 ERR ("'false' expected"); 1104 ERR ("'false' expected");
1034 1105
1035 break; 1106 break;
1138 1209
1139BOOT: 1210BOOT:
1140{ 1211{
1141 int i; 1212 int i;
1142 1213
1143 memset (decode_hexdigit, 0xff, 256);
1144
1145 for (i = 0; i < 256; ++i) 1214 for (i = 0; i < 256; ++i)
1146 decode_hexdigit [i] = 1215 decode_hexdigit [i] =
1147 i >= '0' && i <= '9' ? i - '0' 1216 i >= '0' && i <= '9' ? i - '0'
1148 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1217 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1149 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1218 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1150 : -1; 1219 : -1;
1151 1220
1152 json_stash = gv_stashpv ("JSON::XS", 1); 1221 json_stash = gv_stashpv ("JSON::XS" , 1);
1222 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1223
1224 json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true );
1225 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1153} 1226}
1154 1227
1155PROTOTYPES: DISABLE 1228PROTOTYPES: DISABLE
1156 1229
1157SV *new (char *dummy) 1230SV *new (char *dummy)
1160 OUTPUT: 1233 OUTPUT:
1161 RETVAL 1234 RETVAL
1162 1235
1163SV *ascii (SV *self, int enable = 1) 1236SV *ascii (SV *self, int enable = 1)
1164 ALIAS: 1237 ALIAS:
1165 ascii = F_ASCII 1238 ascii = F_ASCII
1166 latin1 = F_LATIN1 1239 latin1 = F_LATIN1
1167 utf8 = F_UTF8 1240 utf8 = F_UTF8
1168 indent = F_INDENT 1241 indent = F_INDENT
1169 canonical = F_CANONICAL 1242 canonical = F_CANONICAL
1170 space_before = F_SPACE_BEFORE 1243 space_before = F_SPACE_BEFORE
1171 space_after = F_SPACE_AFTER 1244 space_after = F_SPACE_AFTER
1172 pretty = F_PRETTY 1245 pretty = F_PRETTY
1173 allow_nonref = F_ALLOW_NONREF 1246 allow_nonref = F_ALLOW_NONREF
1174 shrink = F_SHRINK 1247 shrink = F_SHRINK
1248 allow_blessed = F_ALLOW_BLESSED
1249 convert_blessed = F_CONV_BLESSED
1175 CODE: 1250 CODE:
1176{ 1251{
1177 UV *uv = SvJSON (self); 1252 UV *uv = SvJSON (self);
1178 if (enable) 1253 if (enable)
1179 *uv |= ix; 1254 *uv |= ix;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines