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.53 by root, Tue Jul 10 15:45:34 2007 UTC vs.
Revision 1.62 by root, Sun Aug 26 22:27:32 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
320 --enc->indent; 335 --enc->indent;
321 encode_indent (enc); encode_ch (enc, ']'); 336 encode_indent (enc); encode_ch (enc, ']');
322} 337}
323 338
324static void 339static void
325encode_he (enc_t *enc, HE *he) 340encode_hk (enc_t *enc, HE *he)
326{ 341{
327 encode_ch (enc, '"'); 342 encode_ch (enc, '"');
328 343
329 if (HeKLEN (he) == HEf_SVKEY) 344 if (HeKLEN (he) == HEf_SVKEY)
330 { 345 {
343 encode_ch (enc, '"'); 358 encode_ch (enc, '"');
344 359
345 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 360 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
346 encode_ch (enc, ':'); 361 encode_ch (enc, ':');
347 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 362 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
348 encode_sv (enc, HeVAL (he));
349} 363}
350 364
351// compare hash entries, used when all keys are bytestrings 365// compare hash entries, used when all keys are bytestrings
352static int 366static int
353he_cmp_fast (const void *a_, const void *b_) 367he_cmp_fast (const void *a_, const void *b_)
358 HE *b = *(HE **)b_; 372 HE *b = *(HE **)b_;
359 373
360 STRLEN la = HeKLEN (a); 374 STRLEN la = HeKLEN (a);
361 STRLEN lb = HeKLEN (b); 375 STRLEN lb = HeKLEN (b);
362 376
363 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 377 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
364 cmp = la - lb; 378 cmp = lb - la;
365 379
366 return cmp; 380 return cmp;
367} 381}
368 382
369// compare hash entries, used when some keys are sv's or utf-x 383// compare hash entries, used when some keys are sv's or utf-x
370static int 384static int
371he_cmp_slow (const void *a, const void *b) 385he_cmp_slow (const void *a, const void *b)
372{ 386{
373 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 387 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
374} 388}
375 389
376static void 390static void
377encode_hv (enc_t *enc, HV *hv) 391encode_hv (enc_t *enc, HV *hv)
378{ 392{
393 HE *he;
379 int count, i; 394 int count;
380 395
381 if (enc->indent >= enc->maxdepth) 396 if (enc->indent >= enc->maxdepth)
382 croak ("data structure too deep (hit recursion limit)"); 397 croak ("data structure too deep (hit recursion limit)");
383 398
384 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 399 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent;
385 400
386 if ((count = hv_iterinit (hv)))
387 {
388 // for canonical output we have to sort by keys first 401 // for canonical output we have to sort by keys first
389 // actually, this is mostly due to the stupid so-called 402 // actually, this is mostly due to the stupid so-called
390 // security workaround added somewhere in 5.8.x. 403 // security workaround added somewhere in 5.8.x.
391 // that randomises hash orderings 404 // that randomises hash orderings
392 if (enc->json.flags & F_CANONICAL) 405 if (enc->json.flags & F_CANONICAL)
406 {
407 int count = hv_iterinit (hv);
408
409 if (SvMAGICAL (hv))
393 { 410 {
411 // need to count by iterating. could improve by dynamically building the vector below
412 // but I don't care for the speed of this special case.
413 // note also that we will run into undefined behaviour when the two iterations
414 // do not result in the same count, something I might care for in some later release.
415
416 count = 0;
417 while (hv_iternext (hv))
418 ++count;
419
420 hv_iterinit (hv);
421 }
422
423 if (count)
424 {
394 int fast = 1; 425 int i, fast = 1;
395 HE *he;
396#if defined(__BORLANDC__) || defined(_MSC_VER) 426#if defined(__BORLANDC__) || defined(_MSC_VER)
397 HE **hes = _alloca (count * sizeof (HE)); 427 HE **hes = _alloca (count * sizeof (HE));
398#else 428#else
399 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 429 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
400#endif 430#endif
427 457
428 FREETMPS; 458 FREETMPS;
429 LEAVE; 459 LEAVE;
430 } 460 }
431 461
432 for (i = 0; i < count; ++i) 462 while (count--)
433 { 463 {
434 encode_indent (enc); 464 encode_indent (enc);
465 he = hes [count];
435 encode_he (enc, hes [i]); 466 encode_hk (enc, he);
467 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
436 468
437 if (i < count - 1) 469 if (count)
438 encode_comma (enc); 470 encode_comma (enc);
439 } 471 }
440
441 encode_nl (enc);
442 } 472 }
473 }
443 else 474 else
444 { 475 {
476 if (hv_iterinit (hv) || SvMAGICAL (hv))
445 HE *he = hv_iternext (hv); 477 if ((he = hv_iternext (hv)))
446
447 for (;;) 478 for (;;)
448 { 479 {
449 encode_indent (enc); 480 encode_indent (enc);
450 encode_he (enc, he); 481 encode_hk (enc, he);
482 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
451 483
452 if (!(he = hv_iternext (hv))) 484 if (!(he = hv_iternext (hv)))
453 break; 485 break;
454 486
455 encode_comma (enc); 487 encode_comma (enc);
456 } 488 }
489 }
457 490
458 encode_nl (enc); 491 encode_nl (enc);
459 }
460 }
461 492
462 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 493 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
463} 494}
464 495
465// encode objects, arrays and special \0=false and \1=true values. 496// encode objects, arrays and special \0=false and \1=true values.
471 SvGETMAGIC (sv); 502 SvGETMAGIC (sv);
472 svt = SvTYPE (sv); 503 svt = SvTYPE (sv);
473 504
474 if (expect_false (SvOBJECT (sv))) 505 if (expect_false (SvOBJECT (sv)))
475 { 506 {
507 HV *stash = !JSON_SLOW || json_boolean_stash
508 ? json_boolean_stash
509 : gv_stashpv ("JSON::XS::Boolean", 1);
510
476 if (SvSTASH (sv) == json_boolean_stash) 511 if (SvSTASH (sv) == stash)
477 { 512 {
478 if (SvIV (sv)) 513 if (SvIV (sv))
479 encode_str (enc, "true", 4, 0); 514 encode_str (enc, "true", 4, 0);
480 else 515 else
481 encode_str (enc, "false", 5, 0); 516 encode_str (enc, "false", 5, 0);
499 534
500 ENTER; SAVETMPS; PUSHMARK (SP); 535 ENTER; SAVETMPS; PUSHMARK (SP);
501 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 536 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
502 537
503 // calling with G_SCALAR ensures that we always get a 1 return value 538 // calling with G_SCALAR ensures that we always get a 1 return value
504 // check anyways.
505 PUTBACK; 539 PUTBACK;
506 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 540 call_sv ((SV *)GvCV (to_json), G_SCALAR);
507 SPAGAIN; 541 SPAGAIN;
508 542
509 // catch this surprisingly common error 543 // catch this surprisingly common error
510 if (SvROK (TOPs) && SvRV (TOPs) == sv) 544 if (SvROK (TOPs) && SvRV (TOPs) == sv)
511 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 545 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
931 is_nv = 1; 965 is_nv = 1;
932 } 966 }
933 967
934 if (!is_nv) 968 if (!is_nv)
935 { 969 {
970 int len = dec->cur - start;
971
936 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 972 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
937 if (*start == '-') 973 if (*start == '-')
938 switch (dec->cur - start) 974 switch (len)
939 { 975 {
940 case 2: return newSViv (-( start [1] - '0' * 1)); 976 case 2: return newSViv (-( start [1] - '0' * 1));
941 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 977 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
942 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 978 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
943 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 979 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
944 } 980 }
945 else 981 else
946 switch (dec->cur - start) 982 switch (len)
947 { 983 {
948 case 1: return newSViv ( start [0] - '0' * 1); 984 case 1: return newSViv ( start [0] - '0' * 1);
949 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 985 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
950 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 986 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
951 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 987 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
952 } 988 }
953 989
954 { 990 {
955 UV uv; 991 UV uv;
956 int numtype = grok_number (start, dec->cur - start, &uv); 992 int numtype = grok_number (start, len, &uv);
957 if (numtype & IS_NUMBER_IN_UV) 993 if (numtype & IS_NUMBER_IN_UV)
958 if (numtype & IS_NUMBER_NEG) 994 if (numtype & IS_NUMBER_NEG)
959 { 995 {
960 if (uv < (UV)IV_MIN) 996 if (uv < (UV)IV_MIN)
961 return newSViv (-(IV)uv); 997 return newSViv (-(IV)uv);
962 } 998 }
963 else 999 else
964 return newSVuv (uv); 1000 return newSVuv (uv);
965
966 // here would likely be the place for bigint support
967 } 1001 }
968 }
969 1002
970 // if we ever support bigint or bigfloat, this is the place for bigfloat 1003 len -= *start == '-' ? 1 : 0;
1004
1005 // does not fit into IV or UV, try NV
1006 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
1007 #if defined (LDBL_DIG)
1008 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1009 #endif
1010 )
1011 // fits into NV without loss of precision
1012 return newSVnv (Atof (start));
1013
1014 // everything else fails, convert it to a string
1015 return newSVpvn (start, dec->cur - start);
1016 }
1017
1018 // loss of precision here
971 return newSVnv (Atof (start)); 1019 return newSVnv (Atof (start));
972 1020
973fail: 1021fail:
974 return 0; 1022 return 0;
975} 1023}
1193 1241
1194 case 't': 1242 case 't':
1195 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1243 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1196 { 1244 {
1197 dec->cur += 4; 1245 dec->cur += 4;
1246#if JSON_SLOW
1247 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1248#endif
1198 return SvREFCNT_inc (json_true); 1249 return SvREFCNT_inc (json_true);
1199 } 1250 }
1200 else 1251 else
1201 ERR ("'true' expected"); 1252 ERR ("'true' expected");
1202 1253
1204 1255
1205 case 'f': 1256 case 'f':
1206 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1257 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1207 { 1258 {
1208 dec->cur += 5; 1259 dec->cur += 5;
1260#if JSON_SLOW
1261 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1262#endif
1209 return SvREFCNT_inc (json_false); 1263 return SvREFCNT_inc (json_false);
1210 } 1264 }
1211 else 1265 else
1212 ERR ("'false' expected"); 1266 ERR ("'false' expected");
1213 1267
1340 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1394 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1341} 1395}
1342 1396
1343PROTOTYPES: DISABLE 1397PROTOTYPES: DISABLE
1344 1398
1399void CLONE (...)
1400 CODE:
1401 json_stash = 0;
1402 json_boolean_stash = 0;
1403
1345void new (char *klass) 1404void new (char *klass)
1346 PPCODE: 1405 PPCODE:
1347{ 1406{
1348 SV *pv = NEWSV (0, sizeof (JSON)); 1407 SV *pv = NEWSV (0, sizeof (JSON));
1349 SvPOK_only (pv); 1408 SvPOK_only (pv);
1350 Zero (SvPVX (pv), 1, JSON); 1409 Zero (SvPVX (pv), 1, JSON);
1351 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1410 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1352 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1411 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH)));
1353} 1412}
1354 1413
1355void ascii (JSON *self, int enable = 1) 1414void ascii (JSON *self, int enable = 1)
1356 ALIAS: 1415 ALIAS:
1357 ascii = F_ASCII 1416 ascii = F_ASCII

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines