diff options
| author | Philipp Stephani | 2019-04-22 17:29:06 +0200 |
|---|---|---|
| committer | Philipp Stephani | 2019-04-22 17:36:52 +0200 |
| commit | 4e2ea400cbd78fa791fb897938a6dcb099401a25 (patch) | |
| tree | bf386a4685abccf8ffc6ad6cf907e18671f0f405 | |
| parent | 3b4e312cfe1e0b185fea58bc35fa951a8389c144 (diff) | |
| download | emacs-4e2ea400cbd78fa791fb897938a6dcb099401a25.tar.gz emacs-4e2ea400cbd78fa791fb897938a6dcb099401a25.zip | |
Introduce a helper macro to convert a Lisp integer to a C integer.
This is similar to CONS_TO_INTEGER. The inverse (INT_TO_INTEGER)
already exists.
* src/lisp.h (INTEGER_TO_INT): New macro.
(ranged_integer_to_int, ranged_integer_to_uint): New
functions.
* src/json.c (lisp_to_json): Use helper macro.
| -rw-r--r-- | src/json.c | 9 | ||||
| -rw-r--r-- | src/lisp.h | 27 |
2 files changed, 28 insertions, 8 deletions
diff --git a/src/json.c b/src/json.c index 928825e034c..16500bce72d 100644 --- a/src/json.c +++ b/src/json.c | |||
| @@ -495,14 +495,7 @@ lisp_to_json (Lisp_Object lisp, struct json_configuration *conf) | |||
| 495 | else if (EQ (lisp, Qt)) | 495 | else if (EQ (lisp, Qt)) |
| 496 | return json_check (json_true ()); | 496 | return json_check (json_true ()); |
| 497 | else if (INTEGERP (lisp)) | 497 | else if (INTEGERP (lisp)) |
| 498 | { | 498 | return json_check (json_integer (INTEGER_TO_INT (lisp, json_int_t))); |
| 499 | intmax_t low = TYPE_MINIMUM (json_int_t); | ||
| 500 | intmax_t high = TYPE_MAXIMUM (json_int_t); | ||
| 501 | intmax_t value; | ||
| 502 | if (! integer_to_intmax (lisp, &value) || value < low || high < value) | ||
| 503 | args_out_of_range_3 (lisp, make_int (low), make_int (high)); | ||
| 504 | return json_check (json_integer (value)); | ||
| 505 | } | ||
| 506 | else if (FLOATP (lisp)) | 499 | else if (FLOATP (lisp)) |
| 507 | return json_check (json_real (XFLOAT_DATA (lisp))); | 500 | return json_check (json_real (XFLOAT_DATA (lisp))); |
| 508 | else if (STRINGP (lisp)) | 501 | else if (STRINGP (lisp)) |
diff --git a/src/lisp.h b/src/lisp.h index d803f160006..8510ad72564 100644 --- a/src/lisp.h +++ b/src/lisp.h | |||
| @@ -2640,6 +2640,13 @@ make_uint (uintmax_t n) | |||
| 2640 | #define INT_TO_INTEGER(expr) \ | 2640 | #define INT_TO_INTEGER(expr) \ |
| 2641 | (EXPR_SIGNED (expr) ? make_int (expr) : make_uint (expr)) | 2641 | (EXPR_SIGNED (expr) ? make_int (expr) : make_uint (expr)) |
| 2642 | 2642 | ||
| 2643 | /* Return the integral value of NUM. If NUM is too big for TYPE, | ||
| 2644 | signal an error. */ | ||
| 2645 | #define INTEGER_TO_INT(num, type) \ | ||
| 2646 | (TYPE_SIGNED (type) \ | ||
| 2647 | ? ranged_integer_to_int ((num), TYPE_MINIMUM (type), TYPE_MAXIMUM (type)) \ | ||
| 2648 | : ranged_integer_to_uint ((num), TYPE_MINIMUM (type))) | ||
| 2649 | |||
| 2643 | 2650 | ||
| 2644 | /* Forwarding pointer to an int variable. | 2651 | /* Forwarding pointer to an int variable. |
| 2645 | This is allowed only in the value cell of a symbol, | 2652 | This is allowed only in the value cell of a symbol, |
| @@ -5016,6 +5023,26 @@ maybe_gc (void) | |||
| 5016 | garbage_collect (); | 5023 | garbage_collect (); |
| 5017 | } | 5024 | } |
| 5018 | 5025 | ||
| 5026 | INLINE intmax_t | ||
| 5027 | ranged_integer_to_int (Lisp_Object num, intmax_t min, intmax_t max) | ||
| 5028 | { | ||
| 5029 | CHECK_INTEGER (num); | ||
| 5030 | intmax_t result; | ||
| 5031 | if (!(integer_to_intmax (num, &result) && min <= result && result <= max)) | ||
| 5032 | args_out_of_range_3 (num, make_int (min), make_int (max)); | ||
| 5033 | return result; | ||
| 5034 | } | ||
| 5035 | |||
| 5036 | INLINE uintmax_t | ||
| 5037 | ranged_integer_to_uint (Lisp_Object num, uintmax_t max) | ||
| 5038 | { | ||
| 5039 | CHECK_INTEGER (num); | ||
| 5040 | uintmax_t result; | ||
| 5041 | if (!(integer_to_uintmax (num, &result) && result <= max)) | ||
| 5042 | args_out_of_range_3 (num, make_fixed_natnum (0), make_uint (max)); | ||
| 5043 | return result; | ||
| 5044 | } | ||
| 5045 | |||
| 5019 | INLINE_HEADER_END | 5046 | INLINE_HEADER_END |
| 5020 | 5047 | ||
| 5021 | #endif /* EMACS_LISP_H */ | 5048 | #endif /* EMACS_LISP_H */ |