Skip to content
This repository
Browse code

add rb_vm_check_call() and use its function to check/call the method

  • Loading branch information...
commit eefae4a348945a9340bf5a70f9c0b15ab69fa9e1 1 parent 860cb40
Watson Watson1978 authored
11 error.c
@@ -581,12 +581,15 @@ exc_equal(VALUE exc, SEL sel, VALUE obj)
581 581 if (rb_obj_class(exc) != rb_obj_class(obj)) {
582 582 SEL sel_message = sel_registerName("message");
583 583 SEL sel_backtrace = sel_registerName("backtrace");
584   - if (!rb_vm_respond_to(obj, sel_message, false)
585   - || !rb_vm_respond_to(obj, sel_backtrace, false)) {
  584 +
  585 + mesg = rb_vm_check_call(obj, sel_message, 0, NULL);
  586 + if (mesg == Qundef) {
  587 + return Qfalse;
  588 + }
  589 + backtrace = rb_vm_check_call(obj, sel_backtrace, 0, NULL);
  590 + if (backtrace == Qundef) {
586 591 return Qfalse;
587 592 }
588   - mesg = rb_vm_call(obj, sel_message, 0, NULL);
589   - backtrace = rb_vm_call(obj, sel_backtrace, 0, NULL);
590 593 }
591 594 else {
592 595 mesg = rb_attr_get(obj, id_mesg);
8 eval.c
@@ -357,7 +357,7 @@ VALUE
357 357 rb_make_exception(int argc, VALUE *argv)
358 358 {
359 359 VALUE mesg;
360   - ID exception;
  360 + SEL sel_exception;
361 361 int n;
362 362
363 363 mesg = Qnil;
@@ -382,11 +382,11 @@ rb_make_exception(int argc, VALUE *argv)
382 382 case 3:
383 383 n = 1;
384 384 exception_call:
385   - exception = rb_intern("exception");
386   - if (!rb_respond_to(argv[0], exception)) {
  385 + sel_exception = sel_registerName("exception");
  386 + mesg = rb_vm_check_call(argv[0], sel_exception, n, argv+1);
  387 + if (mesg == Qundef) {
387 388 rb_raise(rb_eTypeError, "exception class/object expected");
388 389 }
389   - mesg = rb_funcall(argv[0], exception, n, argv[1]);
390 390 break;
391 391 default:
392 392 rb_raise(rb_eArgError, "wrong number of arguments");
11 file.c
@@ -108,14 +108,9 @@ rb_get_path_check(VALUE obj, int level)
108 108 rb_insecure_operation();
109 109 }
110 110
111   - if (rb_vm_respond_to(obj, selToPath, true)) {
112   - tmp = rb_vm_call(obj, selToPath, 0, NULL);
113   - }
114   - else {
115   - tmp = rb_check_string_type(obj);
116   - if (NIL_P(tmp)) {
117   - tmp = obj;
118   - }
  111 + tmp = rb_vm_check_call(obj, selToPath, 0, NULL);
  112 + if (tmp == Qundef) {
  113 + tmp = obj;
119 114 }
120 115 StringValue(tmp);
121 116
9 object.c
@@ -2500,7 +2500,8 @@ convert_type(VALUE val, const char *tname, const char *method, int raise)
2500 2500 // if (!rb_obj_respond_to(val, m, Qtrue)) {
2501 2501
2502 2502 SEL sel = sel_registerName(method);
2503   - if (!rb_vm_respond_to(val, sel, true)) {
  2503 + VALUE result = rb_vm_check_call(val, sel, 0, NULL);
  2504 + if (result == Qundef) {
2504 2505 if (raise) {
2505 2506 rb_raise(rb_eTypeError, "can't convert %s into %s",
2506 2507 NIL_P(val) ? "nil" :
@@ -2509,11 +2510,9 @@ convert_type(VALUE val, const char *tname, const char *method, int raise)
2509 2510 rb_obj_classname(val),
2510 2511 tname);
2511 2512 }
2512   - else {
2513   - return Qnil;
2514   - }
  2513 + return Qnil;
2515 2514 }
2516   - return rb_vm_call(val, sel, 0, NULL);
  2515 + return result;
2517 2516 }
2518 2517
2519 2518 VALUE
12 time.c
@@ -678,10 +678,7 @@ num_exact(VALUE v)
678 678 goto typeerror;
679 679
680 680 default:
681   - tmp = Qundef;
682   - if (rb_vm_respond_to(v, sel_to_r, true)) {
683   - tmp = rb_vm_call(v, sel_to_r, 0, NULL);
684   - }
  681 + tmp = rb_vm_check_call(v, sel_to_r, 0, NULL);
685 682 if (tmp != Qundef) {
686 683 if (rb_respond_to(v, rb_intern("to_str"))) goto typeerror;
687 684 v = tmp;
@@ -1502,7 +1499,7 @@ calc_wday(int year, int month, int day)
1502 1499 static VALUE
1503 1500 guess_local_offset(struct vtm *vtm_utc, int *isdst_ret, const char **zone_ret)
1504 1501 {
1505   - struct tm tm;
  1502 + struct tm tm = {0}; // avoid warning for gcc
1506 1503 long gmtoff;
1507 1504 const char *zone;
1508 1505 time_t t;
@@ -2381,10 +2378,7 @@ time_timespec(VALUE num, int interval)
2381 2378
2382 2379 default:
2383 2380 i = INT2FIX(1);
2384   - ary = Qundef;
2385   - if (rb_vm_respond_to(num, sel_divmod, true)) {
2386   - ary = rb_vm_call(num, sel_divmod, 1, &i);
2387   - }
  2381 + ary = rb_vm_check_call(num, sel_divmod, 1, &i);
2388 2382 if (ary != Qundef && !NIL_P(ary = rb_check_array_type(ary))) {
2389 2383 i = rb_ary_entry(ary, 0);
2390 2384 f = rb_ary_entry(ary, 1);
9 vm.h
@@ -692,6 +692,15 @@ rb_vm_method_call(rb_vm_method_t *m, rb_vm_block_t *block, int argc,
692 692 argc, argv);
693 693 }
694 694
  695 +static inline VALUE
  696 +rb_vm_check_call(VALUE self, SEL sel, int argc, const VALUE *argv)
  697 +{
  698 + if (!rb_vm_respond_to(self, sel, true)) {
  699 + return Qundef;
  700 + }
  701 + return rb_vm_call(self, sel, argc, argv);
  702 +}
  703 +
695 704 VALUE rb_vm_yield_args(void *vm, int argc, const VALUE *argv);
696 705
697 706 static inline VALUE

0 comments on commit eefae4a

Please sign in to comment.
Something went wrong with that request. Please try again.