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.52 by root, Mon Jul 2 02:57:11 2007 UTC vs.
Revision 1.58 by root, Mon Aug 13 16:06:25 2007 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include "assert.h" 5#include <assert.h>
6#include "string.h" 6#include <string.h>
7#include "stdlib.h" 7#include <stdlib.h>
8#include "stdio.h" 8#include <stdio.h>
9#include <float.h>
9 10
10#if defined(__BORLANDC__) || defined(_MSC_VER) 11#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h 12# define snprintf _snprintf // C compilers have this in stdio.h
12#endif 13#endif
13 14
56# define inline static 57# define inline static
57#endif 58#endif
58 59
59#define expect_false(expr) expect ((expr) != 0, 0) 60#define expect_false(expr) expect ((expr) != 0, 0)
60#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#else
66# define JSON_SLOW 0
67#endif
61 68
62static HV *json_stash, *json_boolean_stash; // JSON::XS:: 69static HV *json_stash, *json_boolean_stash; // JSON::XS::
63static SV *json_true, *json_false; 70static SV *json_true, *json_false;
64 71
65typedef struct { 72typedef struct {
306 encode_ch (enc, '['); encode_nl (enc); 313 encode_ch (enc, '['); encode_nl (enc);
307 ++enc->indent; 314 ++enc->indent;
308 315
309 for (i = 0; i <= len; ++i) 316 for (i = 0; i <= len; ++i)
310 { 317 {
318 SV **svp = av_fetch (av, i, 0);
319
311 encode_indent (enc); 320 encode_indent (enc);
312 encode_sv (enc, *av_fetch (av, i, 0)); 321
322 if (svp)
323 encode_sv (enc, *svp);
324 else
325 encode_str (enc, "null", 4, 0);
313 326
314 if (i < len) 327 if (i < len)
315 encode_comma (enc); 328 encode_comma (enc);
316 } 329 }
317 330
471 SvGETMAGIC (sv); 484 SvGETMAGIC (sv);
472 svt = SvTYPE (sv); 485 svt = SvTYPE (sv);
473 486
474 if (expect_false (SvOBJECT (sv))) 487 if (expect_false (SvOBJECT (sv)))
475 { 488 {
489 HV *stash = !JSON_SLOW || json_boolean_stash
490 ? json_boolean_stash
491 : gv_stashpv ("JSON::XS::Boolean", 1);
492
476 if (SvSTASH (sv) == json_boolean_stash) 493 if (SvSTASH (sv) == stash)
477 { 494 {
478 if (SvIV (sv)) 495 if (SvIV (sv))
479 encode_str (enc, "true", 4, 0); 496 encode_str (enc, "true", 4, 0);
480 else 497 else
481 encode_str (enc, "false", 5, 0); 498 encode_str (enc, "false", 5, 0);
489 } 506 }
490#endif 507#endif
491 if (enc->json.flags & F_CONV_BLESSED) 508 if (enc->json.flags & F_CONV_BLESSED)
492 { 509 {
493 // we re-bless the reference to get overload and other niceties right 510 // we re-bless the reference to get overload and other niceties right
494 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 511 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
495 512
496 if (to_json) 513 if (to_json)
497 { 514 {
515 dSP;
516
498 dSP; ENTER; SAVETMPS; PUSHMARK (SP); 517 ENTER; SAVETMPS; PUSHMARK (SP);
499 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 518 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
500 519
501 // calling with G_SCALAR ensures that we always get a 1 reutrn value 520 // calling with G_SCALAR ensures that we always get a 1 return value
502 // check anyways.
503 PUTBACK; 521 PUTBACK;
504 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 522 call_sv ((SV *)GvCV (to_json), G_SCALAR);
505 SPAGAIN; 523 SPAGAIN;
506 524
525 // catch this surprisingly common error
526 if (SvROK (TOPs) && SvRV (TOPs) == sv)
527 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
528
529 sv = POPs;
530 PUTBACK;
531
507 encode_sv (enc, POPs); 532 encode_sv (enc, sv);
508 533
509 FREETMPS; LEAVE; 534 FREETMPS; LEAVE;
510 } 535 }
511 else if (enc->json.flags & F_ALLOW_BLESSED) 536 else if (enc->json.flags & F_ALLOW_BLESSED)
512 encode_str (enc, "null", 4, 0); 537 encode_str (enc, "null", 4, 0);
922 is_nv = 1; 947 is_nv = 1;
923 } 948 }
924 949
925 if (!is_nv) 950 if (!is_nv)
926 { 951 {
952 int len = dec->cur - start;
953
927 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 954 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
928 if (*start == '-') 955 if (*start == '-')
929 switch (dec->cur - start) 956 switch (len)
930 { 957 {
931 case 2: return newSViv (-( start [1] - '0' * 1)); 958 case 2: return newSViv (-( start [1] - '0' * 1));
932 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 959 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
933 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 960 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
934 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 961 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
935 } 962 }
936 else 963 else
937 switch (dec->cur - start) 964 switch (len)
938 { 965 {
939 case 1: return newSViv ( start [0] - '0' * 1); 966 case 1: return newSViv ( start [0] - '0' * 1);
940 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 967 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
941 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 968 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
942 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 969 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
943 } 970 }
944 971
945 { 972 {
946 UV uv; 973 UV uv;
947 int numtype = grok_number (start, dec->cur - start, &uv); 974 int numtype = grok_number (start, len, &uv);
948 if (numtype & IS_NUMBER_IN_UV) 975 if (numtype & IS_NUMBER_IN_UV)
949 if (numtype & IS_NUMBER_NEG) 976 if (numtype & IS_NUMBER_NEG)
950 { 977 {
951 if (uv < (UV)IV_MIN) 978 if (uv < (UV)IV_MIN)
952 return newSViv (-(IV)uv); 979 return newSViv (-(IV)uv);
953 } 980 }
954 else 981 else
955 return newSVuv (uv); 982 return newSVuv (uv);
956
957 // here would likely be the place for bigint support
958 } 983 }
959 }
960 984
961 // if we ever support bigint or bigfloat, this is the place for bigfloat 985 len -= *start == '-' ? 1 : 0;
986
987 // does not fit into IV or UV, try NV
988 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
989 #if defined (LDBL_DIG)
990 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
991 #endif
992 )
993 // fits into NV without loss of precision
994 return newSVnv (Atof (start));
995
996 // everything else fails, convert it to a string
997 return newSVpvn (start, dec->cur - start);
998 }
999
1000 // loss of precision here
962 return newSVnv (Atof (start)); 1001 return newSVnv (Atof (start));
963 1002
964fail: 1003fail:
965 return 0; 1004 return 0;
966} 1005}
1113 // might want to optimise this for common cases. 1152 // might want to optimise this for common cases.
1114 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); 1153 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1115 1154
1116 if (cb) 1155 if (cb)
1117 { 1156 {
1157 dSP;
1118 int count; 1158 int count;
1119 ENTER; SAVETMPS;
1120 1159
1121 dSP; PUSHMARK (SP); 1160 ENTER; SAVETMPS; PUSHMARK (SP);
1122 XPUSHs (HeVAL (he)); 1161 XPUSHs (HeVAL (he));
1123 1162
1124 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1163 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1125 1164
1126 if (count == 1) 1165 if (count == 1)
1134 } 1173 }
1135 } 1174 }
1136 1175
1137 if (dec->json.cb_object) 1176 if (dec->json.cb_object)
1138 { 1177 {
1178 dSP;
1139 int count; 1179 int count;
1140 ENTER; SAVETMPS;
1141 1180
1142 dSP; ENTER; SAVETMPS; PUSHMARK (SP); 1181 ENTER; SAVETMPS; PUSHMARK (SP);
1143 XPUSHs (sv_2mortal (sv)); 1182 XPUSHs (sv_2mortal (sv));
1144 1183
1145 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1184 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1146 1185
1147 if (count == 1) 1186 if (count == 1)
1331 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1370 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1332} 1371}
1333 1372
1334PROTOTYPES: DISABLE 1373PROTOTYPES: DISABLE
1335 1374
1375void CLONE (...)
1376 CODE:
1377 json_stash = 0;
1378 json_boolean_stash = 0;
1379
1336void new (char *klass) 1380void new (char *klass)
1337 PPCODE: 1381 PPCODE:
1338{ 1382{
1383 HV *stash = !JSON_SLOW || json_stash
1384 ? json_stash
1385 : gv_stashpv ("JSON::XS", 1);
1339 SV *pv = NEWSV (0, sizeof (JSON)); 1386 SV *pv = NEWSV (0, sizeof (JSON));
1340 SvPOK_only (pv); 1387 SvPOK_only (pv);
1341 Zero (SvPVX (pv), 1, JSON); 1388 Zero (SvPVX (pv), 1, JSON);
1342 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1389 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1343 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1390 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash)));
1344} 1391}
1345 1392
1346void ascii (JSON *self, int enable = 1) 1393void ascii (JSON *self, int enable = 1)
1347 ALIAS: 1394 ALIAS:
1348 ascii = F_ASCII 1395 ascii = F_ASCII

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines