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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines