diff options
| author | Paul Eggert | 2016-03-02 10:47:27 -0800 |
|---|---|---|
| committer | Paul Eggert | 2016-03-02 10:48:06 -0800 |
| commit | 08e239a21936dcb0a79778ed591dfea50016f6a0 (patch) | |
| tree | e401aad07bed69eb7e62e13452edeb2b14566e81 /src | |
| parent | d20d02fef7fd68f03db5abe48548068191dadedf (diff) | |
| download | emacs-08e239a21936dcb0a79778ed591dfea50016f6a0.tar.gz emacs-08e239a21936dcb0a79778ed591dfea50016f6a0.zip | |
emacs-module.c simplification and tuneup
* src/emacs-module.c (CHECK_USER_PTR): New function.
(module_get_user_ptr, module_set_user_ptr)
(module_get_user_finalizer, module_set_user_finalizer): Use it.
(module_make_global_ref, module_copy_string_contents)
(module_make_string, module_vec_set, module_vec_get): Omit
unnecessary runtime tests. For example, vector sizes are always
fixnums, so we don’t need to test that they are in fixnum range.
Diffstat (limited to 'src')
| -rw-r--r-- | src/emacs-module.c | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/src/emacs-module.c b/src/emacs-module.c index d8f2c1da14e..1176ca309f9 100644 --- a/src/emacs-module.c +++ b/src/emacs-module.c | |||
| @@ -241,6 +241,12 @@ struct module_fun_env | |||
| 241 | return error_retval; \ | 241 | return error_retval; \ |
| 242 | MODULE_HANDLE_NONLOCAL_EXIT (error_retval) | 242 | MODULE_HANDLE_NONLOCAL_EXIT (error_retval) |
| 243 | 243 | ||
| 244 | static void | ||
| 245 | CHECK_USER_PTR (Lisp_Object obj) | ||
| 246 | { | ||
| 247 | CHECK_TYPE (USER_PTRP (obj), Quser_ptrp, lisp); | ||
| 248 | } | ||
| 249 | |||
| 244 | /* Catch signals and throws only if the code can actually signal or | 250 | /* Catch signals and throws only if the code can actually signal or |
| 245 | throw. If checking is enabled, abort if the current thread is not | 251 | throw. If checking is enabled, abort if the current thread is not |
| 246 | the Emacs main thread. */ | 252 | the Emacs main thread. */ |
| @@ -267,9 +273,9 @@ module_make_global_ref (emacs_env *env, emacs_value ref) | |||
| 267 | if (i >= 0) | 273 | if (i >= 0) |
| 268 | { | 274 | { |
| 269 | Lisp_Object value = HASH_VALUE (h, i); | 275 | Lisp_Object value = HASH_VALUE (h, i); |
| 270 | verify (EMACS_INT_MAX > MOST_POSITIVE_FIXNUM); | ||
| 271 | EMACS_INT refcount = XFASTINT (value) + 1; | 276 | EMACS_INT refcount = XFASTINT (value) + 1; |
| 272 | if (FIXNUM_OVERFLOW_P (refcount)) xsignal0 (Qoverflow_error); | 277 | if (MOST_POSITIVE_FIXNUM < refcount) |
| 278 | xsignal0 (Qoverflow_error); | ||
| 273 | value = make_natnum (refcount); | 279 | value = make_natnum (refcount); |
| 274 | set_hash_value_slot (h, i, value); | 280 | set_hash_value_slot (h, i, value); |
| 275 | } | 281 | } |
| @@ -409,7 +415,8 @@ module_funcall (emacs_env *env, emacs_value fun, ptrdiff_t nargs, | |||
| 409 | first arg, because that's what Ffuncall takes. */ | 415 | first arg, because that's what Ffuncall takes. */ |
| 410 | Lisp_Object *newargs; | 416 | Lisp_Object *newargs; |
| 411 | USE_SAFE_ALLOCA; | 417 | USE_SAFE_ALLOCA; |
| 412 | if (nargs == PTRDIFF_MAX) xsignal0 (Qoverflow_error); | 418 | if (nargs == PTRDIFF_MAX) |
| 419 | xsignal0 (Qoverflow_error); | ||
| 413 | SAFE_ALLOCA_LISP (newargs, nargs + 1); | 420 | SAFE_ALLOCA_LISP (newargs, nargs + 1); |
| 414 | newargs[0] = value_to_lisp (fun); | 421 | newargs[0] = value_to_lisp (fun); |
| 415 | for (ptrdiff_t i = 0; i < nargs; i++) | 422 | for (ptrdiff_t i = 0; i < nargs; i++) |
| @@ -464,7 +471,8 @@ static emacs_value | |||
| 464 | module_make_integer (emacs_env *env, intmax_t n) | 471 | module_make_integer (emacs_env *env, intmax_t n) |
| 465 | { | 472 | { |
| 466 | MODULE_FUNCTION_BEGIN (module_nil); | 473 | MODULE_FUNCTION_BEGIN (module_nil); |
| 467 | if (FIXNUM_OVERFLOW_P (n)) xsignal0 (Qoverflow_error); | 474 | if (FIXNUM_OVERFLOW_P (n)) |
| 475 | xsignal0 (Qoverflow_error); | ||
| 468 | return lisp_to_value (make_number (n)); | 476 | return lisp_to_value (make_number (n)); |
| 469 | } | 477 | } |
| 470 | 478 | ||
| @@ -494,7 +502,6 @@ module_copy_string_contents (emacs_env *env, emacs_value value, char *buffer, | |||
| 494 | 502 | ||
| 495 | Lisp_Object lisp_str_utf8 = ENCODE_UTF_8 (lisp_str); | 503 | Lisp_Object lisp_str_utf8 = ENCODE_UTF_8 (lisp_str); |
| 496 | ptrdiff_t raw_size = SBYTES (lisp_str_utf8); | 504 | ptrdiff_t raw_size = SBYTES (lisp_str_utf8); |
| 497 | if (raw_size == PTRDIFF_MAX) xsignal0 (Qoverflow_error); | ||
| 498 | ptrdiff_t required_buf_size = raw_size + 1; | 505 | ptrdiff_t required_buf_size = raw_size + 1; |
| 499 | 506 | ||
| 500 | eassert (length != NULL); | 507 | eassert (length != NULL); |
| @@ -523,7 +530,6 @@ static emacs_value | |||
| 523 | module_make_string (emacs_env *env, const char *str, ptrdiff_t length) | 530 | module_make_string (emacs_env *env, const char *str, ptrdiff_t length) |
| 524 | { | 531 | { |
| 525 | MODULE_FUNCTION_BEGIN (module_nil); | 532 | MODULE_FUNCTION_BEGIN (module_nil); |
| 526 | if (length > STRING_BYTES_BOUND) xsignal0 (Qoverflow_error); | ||
| 527 | Lisp_Object lstr = make_unibyte_string (str, length); | 533 | Lisp_Object lstr = make_unibyte_string (str, length); |
| 528 | return lisp_to_value (code_convert_string_norecord (lstr, Qutf_8, false)); | 534 | return lisp_to_value (code_convert_string_norecord (lstr, Qutf_8, false)); |
| 529 | } | 535 | } |
| @@ -540,7 +546,7 @@ module_get_user_ptr (emacs_env *env, emacs_value uptr) | |||
| 540 | { | 546 | { |
| 541 | MODULE_FUNCTION_BEGIN (NULL); | 547 | MODULE_FUNCTION_BEGIN (NULL); |
| 542 | Lisp_Object lisp = value_to_lisp (uptr); | 548 | Lisp_Object lisp = value_to_lisp (uptr); |
| 543 | CHECK_TYPE (USER_PTRP (lisp), Quser_ptrp, lisp); | 549 | CHECK_USER_PTR (lisp); |
| 544 | return XUSER_PTR (lisp)->p; | 550 | return XUSER_PTR (lisp)->p; |
| 545 | } | 551 | } |
| 546 | 552 | ||
| @@ -550,7 +556,7 @@ module_set_user_ptr (emacs_env *env, emacs_value uptr, void *ptr) | |||
| 550 | /* FIXME: This function should return bool because it can fail. */ | 556 | /* FIXME: This function should return bool because it can fail. */ |
| 551 | MODULE_FUNCTION_BEGIN (); | 557 | MODULE_FUNCTION_BEGIN (); |
| 552 | Lisp_Object lisp = value_to_lisp (uptr); | 558 | Lisp_Object lisp = value_to_lisp (uptr); |
| 553 | CHECK_TYPE (USER_PTRP (lisp), Quser_ptrp, lisp); | 559 | CHECK_USER_PTR (lisp); |
| 554 | XUSER_PTR (lisp)->p = ptr; | 560 | XUSER_PTR (lisp)->p = ptr; |
| 555 | } | 561 | } |
| 556 | 562 | ||
| @@ -559,7 +565,7 @@ module_get_user_finalizer (emacs_env *env, emacs_value uptr) | |||
| 559 | { | 565 | { |
| 560 | MODULE_FUNCTION_BEGIN (NULL); | 566 | MODULE_FUNCTION_BEGIN (NULL); |
| 561 | Lisp_Object lisp = value_to_lisp (uptr); | 567 | Lisp_Object lisp = value_to_lisp (uptr); |
| 562 | CHECK_TYPE (USER_PTRP (lisp), Quser_ptrp, lisp); | 568 | CHECK_USER_PTR (lisp); |
| 563 | return XUSER_PTR (lisp)->finalizer; | 569 | return XUSER_PTR (lisp)->finalizer; |
| 564 | } | 570 | } |
| 565 | 571 | ||
| @@ -570,7 +576,7 @@ module_set_user_finalizer (emacs_env *env, emacs_value uptr, | |||
| 570 | /* FIXME: This function should return bool because it can fail. */ | 576 | /* FIXME: This function should return bool because it can fail. */ |
| 571 | MODULE_FUNCTION_BEGIN (); | 577 | MODULE_FUNCTION_BEGIN (); |
| 572 | Lisp_Object lisp = value_to_lisp (uptr); | 578 | Lisp_Object lisp = value_to_lisp (uptr); |
| 573 | CHECK_TYPE (USER_PTRP (lisp), Quser_ptrp, lisp); | 579 | CHECK_USER_PTR (lisp); |
| 574 | XUSER_PTR (lisp)->finalizer = fin; | 580 | XUSER_PTR (lisp)->finalizer = fin; |
| 575 | } | 581 | } |
| 576 | 582 | ||
| @@ -581,7 +587,6 @@ module_vec_set (emacs_env *env, emacs_value vec, ptrdiff_t i, emacs_value val) | |||
| 581 | MODULE_FUNCTION_BEGIN (); | 587 | MODULE_FUNCTION_BEGIN (); |
| 582 | Lisp_Object lvec = value_to_lisp (vec); | 588 | Lisp_Object lvec = value_to_lisp (vec); |
| 583 | CHECK_VECTOR (lvec); | 589 | CHECK_VECTOR (lvec); |
| 584 | if (FIXNUM_OVERFLOW_P (i)) xsignal0 (Qoverflow_error); | ||
| 585 | CHECK_RANGED_INTEGER (make_number (i), 0, ASIZE (lvec) - 1); | 590 | CHECK_RANGED_INTEGER (make_number (i), 0, ASIZE (lvec) - 1); |
| 586 | ASET (lvec, i, value_to_lisp (val)); | 591 | ASET (lvec, i, value_to_lisp (val)); |
| 587 | } | 592 | } |
| @@ -592,7 +597,6 @@ module_vec_get (emacs_env *env, emacs_value vec, ptrdiff_t i) | |||
| 592 | MODULE_FUNCTION_BEGIN (module_nil); | 597 | MODULE_FUNCTION_BEGIN (module_nil); |
| 593 | Lisp_Object lvec = value_to_lisp (vec); | 598 | Lisp_Object lvec = value_to_lisp (vec); |
| 594 | CHECK_VECTOR (lvec); | 599 | CHECK_VECTOR (lvec); |
| 595 | if (FIXNUM_OVERFLOW_P (i)) xsignal0 (Qoverflow_error); | ||
| 596 | CHECK_RANGED_INTEGER (make_number (i), 0, ASIZE (lvec) - 1); | 600 | CHECK_RANGED_INTEGER (make_number (i), 0, ASIZE (lvec) - 1); |
| 597 | return lisp_to_value (AREF (lvec, i)); | 601 | return lisp_to_value (AREF (lvec, i)); |
| 598 | } | 602 | } |