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 | |
62 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
69 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
63 | static SV *json_true, *json_false; |
70 | static SV *json_true, *json_false; |
64 | |
71 | |
65 | typedef struct { |
72 | typedef struct { |
… | |
… | |
477 | SvGETMAGIC (sv); |
484 | SvGETMAGIC (sv); |
478 | svt = SvTYPE (sv); |
485 | svt = SvTYPE (sv); |
479 | |
486 | |
480 | if (expect_false (SvOBJECT (sv))) |
487 | if (expect_false (SvOBJECT (sv))) |
481 | { |
488 | { |
|
|
489 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
490 | ? json_boolean_stash |
|
|
491 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
492 | |
482 | if (SvSTASH (sv) == json_boolean_stash) |
493 | if (SvSTASH (sv) == stash) |
483 | { |
494 | { |
484 | if (SvIV (sv)) |
495 | if (SvIV (sv)) |
485 | encode_str (enc, "true", 4, 0); |
496 | encode_str (enc, "true", 4, 0); |
486 | else |
497 | else |
487 | encode_str (enc, "false", 5, 0); |
498 | encode_str (enc, "false", 5, 0); |
… | |
… | |
499 | // 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 |
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
511 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
501 | |
512 | |
502 | if (to_json) |
513 | if (to_json) |
503 | { |
514 | { |
504 | int count; |
|
|
505 | dSP; |
515 | dSP; |
506 | |
516 | |
507 | ENTER; SAVETMPS; PUSHMARK (SP); |
517 | ENTER; SAVETMPS; PUSHMARK (SP); |
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
518 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
509 | |
519 | |
… | |
… | |
937 | is_nv = 1; |
947 | is_nv = 1; |
938 | } |
948 | } |
939 | |
949 | |
940 | if (!is_nv) |
950 | if (!is_nv) |
941 | { |
951 | { |
|
|
952 | int len = dec->cur - start; |
|
|
953 | |
942 | // 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 |
943 | if (*start == '-') |
955 | if (*start == '-') |
944 | switch (dec->cur - start) |
956 | switch (len) |
945 | { |
957 | { |
946 | case 2: return newSViv (-( start [1] - '0' * 1)); |
958 | case 2: return newSViv (-( start [1] - '0' * 1)); |
947 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
959 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
948 | 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)); |
949 | 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)); |
950 | } |
962 | } |
951 | else |
963 | else |
952 | switch (dec->cur - start) |
964 | switch (len) |
953 | { |
965 | { |
954 | case 1: return newSViv ( start [0] - '0' * 1); |
966 | case 1: return newSViv ( start [0] - '0' * 1); |
955 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
967 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
956 | 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); |
957 | 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); |
958 | } |
970 | } |
959 | |
971 | |
960 | { |
972 | { |
961 | UV uv; |
973 | UV uv; |
962 | int numtype = grok_number (start, dec->cur - start, &uv); |
974 | int numtype = grok_number (start, len, &uv); |
963 | if (numtype & IS_NUMBER_IN_UV) |
975 | if (numtype & IS_NUMBER_IN_UV) |
964 | if (numtype & IS_NUMBER_NEG) |
976 | if (numtype & IS_NUMBER_NEG) |
965 | { |
977 | { |
966 | if (uv < (UV)IV_MIN) |
978 | if (uv < (UV)IV_MIN) |
967 | return newSViv (-(IV)uv); |
979 | return newSViv (-(IV)uv); |
968 | } |
980 | } |
969 | else |
981 | else |
970 | return newSVuv (uv); |
982 | return newSVuv (uv); |
971 | |
|
|
972 | // here would likely be the place for bigint support |
|
|
973 | } |
983 | } |
974 | } |
|
|
975 | |
984 | |
976 | // 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 |
977 | return newSVnv (Atof (start)); |
1001 | return newSVnv (Atof (start)); |
978 | |
1002 | |
979 | fail: |
1003 | fail: |
980 | return 0; |
1004 | return 0; |
981 | } |
1005 | } |
… | |
… | |
1346 | 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); |
1347 | } |
1371 | } |
1348 | |
1372 | |
1349 | PROTOTYPES: DISABLE |
1373 | PROTOTYPES: DISABLE |
1350 | |
1374 | |
|
|
1375 | void CLONE (...) |
|
|
1376 | CODE: |
|
|
1377 | json_stash = 0; |
|
|
1378 | json_boolean_stash = 0; |
|
|
1379 | |
1351 | void new (char *klass) |
1380 | void new (char *klass) |
1352 | PPCODE: |
1381 | PPCODE: |
1353 | { |
1382 | { |
|
|
1383 | HV *stash = !JSON_SLOW || json_stash |
|
|
1384 | ? json_stash |
|
|
1385 | : gv_stashpv ("JSON::XS", 1); |
1354 | SV *pv = NEWSV (0, sizeof (JSON)); |
1386 | SV *pv = NEWSV (0, sizeof (JSON)); |
1355 | SvPOK_only (pv); |
1387 | SvPOK_only (pv); |
1356 | Zero (SvPVX (pv), 1, JSON); |
1388 | Zero (SvPVX (pv), 1, JSON); |
1357 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1389 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1358 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
1390 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash))); |
1359 | } |
1391 | } |
1360 | |
1392 | |
1361 | void ascii (JSON *self, int enable = 1) |
1393 | void ascii (JSON *self, int enable = 1) |
1362 | ALIAS: |
1394 | ALIAS: |
1363 | ascii = F_ASCII |
1395 | ascii = F_ASCII |