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.51 by root, Mon Jul 2 00:48:18 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
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)
61 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
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 {
66 U32 flags; 75 U32 flags;
67 SV *cb_object, *cb_sk_object; 76 SV *cb_object;
77 HV *cb_sk_object;
68} JSON; 78} JSON;
69 79
70///////////////////////////////////////////////////////////////////////////// 80/////////////////////////////////////////////////////////////////////////////
71// utility functions 81// utility functions
72 82
305 encode_ch (enc, '['); encode_nl (enc); 315 encode_ch (enc, '['); encode_nl (enc);
306 ++enc->indent; 316 ++enc->indent;
307 317
308 for (i = 0; i <= len; ++i) 318 for (i = 0; i <= len; ++i)
309 { 319 {
320 SV **svp = av_fetch (av, i, 0);
321
310 encode_indent (enc); 322 encode_indent (enc);
311 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);
312 328
313 if (i < len) 329 if (i < len)
314 encode_comma (enc); 330 encode_comma (enc);
315 } 331 }
316 332
470 SvGETMAGIC (sv); 486 SvGETMAGIC (sv);
471 svt = SvTYPE (sv); 487 svt = SvTYPE (sv);
472 488
473 if (expect_false (SvOBJECT (sv))) 489 if (expect_false (SvOBJECT (sv)))
474 { 490 {
491 HV *stash = !JSON_SLOW || json_boolean_stash
492 ? json_boolean_stash
493 : gv_stashpv ("JSON::XS::Boolean", 1);
494
475 if (SvSTASH (sv) == json_boolean_stash) 495 if (SvSTASH (sv) == stash)
476 { 496 {
477 if (SvIV (sv)) 497 if (SvIV (sv))
478 encode_str (enc, "true", 4, 0); 498 encode_str (enc, "true", 4, 0);
479 else 499 else
480 encode_str (enc, "false", 5, 0); 500 encode_str (enc, "false", 5, 0);
488 } 508 }
489#endif 509#endif
490 if (enc->json.flags & F_CONV_BLESSED) 510 if (enc->json.flags & F_CONV_BLESSED)
491 { 511 {
492 // 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
493 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 513 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
494 514
495 if (to_json) 515 if (to_json)
496 { 516 {
517 dSP;
518
497 dSP; ENTER; SAVETMPS; PUSHMARK (SP); 519 ENTER; SAVETMPS; PUSHMARK (SP);
498 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 520 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
499 521
500 // 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
501 // check anyways.
502 PUTBACK; 523 PUTBACK;
503 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 524 call_sv ((SV *)GvCV (to_json), G_SCALAR);
504 SPAGAIN; 525 SPAGAIN;
505 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
506 encode_sv (enc, POPs); 534 encode_sv (enc, sv);
507 535
508 FREETMPS; LEAVE; 536 FREETMPS; LEAVE;
509 } 537 }
510 else if (enc->json.flags & F_ALLOW_BLESSED) 538 else if (enc->json.flags & F_ALLOW_BLESSED)
511 encode_str (enc, "null", 4, 0); 539 encode_str (enc, "null", 4, 0);
921 is_nv = 1; 949 is_nv = 1;
922 } 950 }
923 951
924 if (!is_nv) 952 if (!is_nv)
925 { 953 {
954 int len = dec->cur - start;
955
926 // 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
927 if (*start == '-') 957 if (*start == '-')
928 switch (dec->cur - start) 958 switch (len)
929 { 959 {
930 case 2: return newSViv (-( start [1] - '0' * 1)); 960 case 2: return newSViv (-( start [1] - '0' * 1));
931 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 961 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
932 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));
933 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));
934 } 964 }
935 else 965 else
936 switch (dec->cur - start) 966 switch (len)
937 { 967 {
938 case 1: return newSViv ( start [0] - '0' * 1); 968 case 1: return newSViv ( start [0] - '0' * 1);
939 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 969 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
940 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);
941 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);
942 } 972 }
943 973
944 { 974 {
945 UV uv; 975 UV uv;
946 int numtype = grok_number (start, dec->cur - start, &uv); 976 int numtype = grok_number (start, len, &uv);
947 if (numtype & IS_NUMBER_IN_UV) 977 if (numtype & IS_NUMBER_IN_UV)
948 if (numtype & IS_NUMBER_NEG) 978 if (numtype & IS_NUMBER_NEG)
949 { 979 {
950 if (uv < (UV)IV_MIN) 980 if (uv < (UV)IV_MIN)
951 return newSViv (-(IV)uv); 981 return newSViv (-(IV)uv);
952 } 982 }
953 else 983 else
954 return newSVuv (uv); 984 return newSVuv (uv);
955
956 // here would likely be the place for bigint support
957 } 985 }
958 }
959 986
960 // 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
961 return newSVnv (Atof (start)); 1003 return newSVnv (Atof (start));
962 1004
963fail: 1005fail:
964 return 0; 1006 return 0;
965} 1007}
1098 sv = newRV_noinc ((SV *)hv); 1140 sv = newRV_noinc ((SV *)hv);
1099 1141
1100 // check filter callbacks 1142 // check filter callbacks
1101 if (dec->json.flags & F_HOOK) 1143 if (dec->json.flags & F_HOOK)
1102 { 1144 {
1103 ENTER; SAVETMPS;
1104
1105 if (dec->json.cb_sk_object && HvKEYS (hv) == 1) 1145 if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
1106 { 1146 {
1147 HE *cb, *he;
1148
1149 hv_iterinit (hv);
1150 he = hv_iternext (hv);
1151 hv_iterinit (hv);
1152
1153 // the next line creates a mortal sv each time its called.
1154 // might want to optimise this for common cases.
1155 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1156
1157 if (cb)
1158 {
1159 dSP;
1107 int count; 1160 int count;
1108 1161
1109 dSP; PUSHMARK (SP); 1162 ENTER; SAVETMPS; PUSHMARK (SP);
1110 XPUSHs (sv_2mortal (sv)); 1163 XPUSHs (HeVAL (he));
1111 1164
1112 PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN; 1165 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1113 1166
1114 if (count == 1) 1167 if (count == 1)
1115 { 1168 {
1116 sv = newSVsv (POPs); 1169 sv = newSVsv (POPs);
1117 goto filter_ok; 1170 FREETMPS; LEAVE;
1171 return sv;
1118 } 1172 }
1119 1173
1120 SvREFCNT_inc (sv); 1174 FREETMPS; LEAVE;
1175 }
1121 } 1176 }
1122 1177
1123 if (dec->json.cb_object) 1178 if (dec->json.cb_object)
1124 { 1179 {
1180 dSP;
1125 int count; 1181 int count;
1126 1182
1127 dSP; ENTER; SAVETMPS; PUSHMARK (SP); 1183 ENTER; SAVETMPS; PUSHMARK (SP);
1128 XPUSHs (sv_2mortal (sv)); 1184 XPUSHs (sv_2mortal (sv));
1129 1185
1130 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1186 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1131 1187
1132 if (count == 1) 1188 if (count == 1)
1133 { 1189 {
1134 sv = newSVsv (POPs); 1190 sv = newSVsv (POPs);
1135 goto filter_ok; 1191 FREETMPS; LEAVE;
1192 return sv;
1136 } 1193 }
1137 1194
1138 SvREFCNT_inc (sv); 1195 SvREFCNT_inc (sv);
1196 FREETMPS; LEAVE;
1139 } 1197 }
1140
1141filter_ok:
1142 FREETMPS; LEAVE;
1143 } 1198 }
1144 1199
1145 return sv; 1200 return sv;
1146 1201
1147fail: 1202fail:
1170 1225
1171 case 't': 1226 case 't':
1172 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1227 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1173 { 1228 {
1174 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
1175 return SvREFCNT_inc (json_true); 1233 return SvREFCNT_inc (json_true);
1176 } 1234 }
1177 else 1235 else
1178 ERR ("'true' expected"); 1236 ERR ("'true' expected");
1179 1237
1181 1239
1182 case 'f': 1240 case 'f':
1183 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1241 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1184 { 1242 {
1185 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
1186 return SvREFCNT_inc (json_false); 1247 return SvREFCNT_inc (json_false);
1187 } 1248 }
1188 else 1249 else
1189 ERR ("'false' expected"); 1250 ERR ("'false' expected");
1190 1251
1317 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);
1318} 1379}
1319 1380
1320PROTOTYPES: DISABLE 1381PROTOTYPES: DISABLE
1321 1382
1383void CLONE (...)
1384 CODE:
1385 json_stash = 0;
1386 json_boolean_stash = 0;
1387
1322void new (char *klass) 1388void new (char *klass)
1323 PPCODE: 1389 PPCODE:
1324{ 1390{
1325 SV *pv = NEWSV (0, sizeof (JSON)); 1391 SV *pv = NEWSV (0, sizeof (JSON));
1326 SvPOK_only (pv); 1392 SvPOK_only (pv);
1327 Zero (SvPVX (pv), 1, JSON); 1393 Zero (SvPVX (pv), 1, JSON);
1328 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1394 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1329 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1395 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH)));
1330} 1396}
1331 1397
1332void ascii (JSON *self, int enable = 1) 1398void ascii (JSON *self, int enable = 1)
1333 ALIAS: 1399 ALIAS:
1334 ascii = F_ASCII 1400 ascii = F_ASCII
1383 1449
1384 XPUSHs (ST (0)); 1450 XPUSHs (ST (0));
1385} 1451}
1386 1452
1387void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1453void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1388 ALIAS:
1389 filter_json_single_key_object = 1
1390 PPCODE: 1454 PPCODE:
1391{ 1455{
1392 SV **svp; 1456 SvREFCNT_dec (self->cb_object);
1457 self->cb_object = SvOK (cb) ? newSVsv (cb) : 0;
1393 1458
1459 XPUSHs (ST (0));
1460}
1461
1462void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
1463 PPCODE:
1464{
1465 if (!self->cb_sk_object)
1466 self->cb_sk_object = newHV ();
1467
1394 if (!SvOK (cb)) 1468 if (SvOK (cb))
1395 cb = 0; 1469 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1396 else 1470 else
1397 cb = newSVsv (cb);
1398
1399 switch (ix)
1400 { 1471 {
1401 case 0: svp = &self->cb_object ; break; 1472 hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0);
1402 case 1: svp = &self->cb_sk_object; break; 1473
1474 if (!HvKEYS (self->cb_sk_object))
1475 {
1476 SvREFCNT_dec (self->cb_sk_object);
1477 self->cb_sk_object = 0;
1478 }
1403 } 1479 }
1404
1405 if (*svp)
1406 SvREFCNT_dec (*svp);
1407
1408 *svp = cb;
1409 1480
1410 XPUSHs (ST (0)); 1481 XPUSHs (ST (0));
1411} 1482}
1412 1483
1413void encode (JSON *self, SV *scalar) 1484void encode (JSON *self, SV *scalar)
1425 EXTEND (SP, 2); 1496 EXTEND (SP, 2);
1426 PUSHs (decode_json (jsonstr, self, &offset)); 1497 PUSHs (decode_json (jsonstr, self, &offset));
1427 PUSHs (sv_2mortal (newSVuv (offset))); 1498 PUSHs (sv_2mortal (newSVuv (offset)));
1428} 1499}
1429 1500
1501void DESTROY (JSON *self)
1502 CODE:
1503 SvREFCNT_dec (self->cb_sk_object);
1504 SvREFCNT_dec (self->cb_object);
1505
1430PROTOTYPES: ENABLE 1506PROTOTYPES: ENABLE
1431 1507
1432void to_json (SV *scalar) 1508void to_json (SV *scalar)
1433 PPCODE: 1509 PPCODE:
1434{ 1510{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines