Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit eefae4a348945a9340bf5a70f9c0b15ab69fa9e1 1 parent 860cb40
@Watson1978 Watson1978 authored
Showing with 30 additions and 30 deletions.
  1. +7 −4 error.c
  2. +4 −4 eval.c
  3. +3 −8 file.c
  4. +4 −5 object.c
  5. +3 −9 time.c
  6. +9 −0 vm.h
View
11 error.c
@@ -581,12 +581,15 @@ exc_equal(VALUE exc, SEL sel, VALUE obj)
if (rb_obj_class(exc) != rb_obj_class(obj)) {
SEL sel_message = sel_registerName("message");
SEL sel_backtrace = sel_registerName("backtrace");
- if (!rb_vm_respond_to(obj, sel_message, false)
- || !rb_vm_respond_to(obj, sel_backtrace, false)) {
+
+ mesg = rb_vm_check_call(obj, sel_message, 0, NULL);
+ if (mesg == Qundef) {
+ return Qfalse;
+ }
+ backtrace = rb_vm_check_call(obj, sel_backtrace, 0, NULL);
+ if (backtrace == Qundef) {
return Qfalse;
}
- mesg = rb_vm_call(obj, sel_message, 0, NULL);
- backtrace = rb_vm_call(obj, sel_backtrace, 0, NULL);
}
else {
mesg = rb_attr_get(obj, id_mesg);
View
8 eval.c
@@ -357,7 +357,7 @@ VALUE
rb_make_exception(int argc, VALUE *argv)
{
VALUE mesg;
- ID exception;
+ SEL sel_exception;
int n;
mesg = Qnil;
@@ -382,11 +382,11 @@ rb_make_exception(int argc, VALUE *argv)
case 3:
n = 1;
exception_call:
- exception = rb_intern("exception");
- if (!rb_respond_to(argv[0], exception)) {
+ sel_exception = sel_registerName("exception");
+ mesg = rb_vm_check_call(argv[0], sel_exception, n, argv+1);
+ if (mesg == Qundef) {
rb_raise(rb_eTypeError, "exception class/object expected");
}
- mesg = rb_funcall(argv[0], exception, n, argv[1]);
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments");
View
11 file.c
@@ -108,14 +108,9 @@ rb_get_path_check(VALUE obj, int level)
rb_insecure_operation();
}
- if (rb_vm_respond_to(obj, selToPath, true)) {
- tmp = rb_vm_call(obj, selToPath, 0, NULL);
- }
- else {
- tmp = rb_check_string_type(obj);
- if (NIL_P(tmp)) {
- tmp = obj;
- }
+ tmp = rb_vm_check_call(obj, selToPath, 0, NULL);
+ if (tmp == Qundef) {
+ tmp = obj;
}
StringValue(tmp);
View
9 object.c
@@ -2500,7 +2500,8 @@ convert_type(VALUE val, const char *tname, const char *method, int raise)
// if (!rb_obj_respond_to(val, m, Qtrue)) {
SEL sel = sel_registerName(method);
- if (!rb_vm_respond_to(val, sel, true)) {
+ VALUE result = rb_vm_check_call(val, sel, 0, NULL);
+ if (result == Qundef) {
if (raise) {
rb_raise(rb_eTypeError, "can't convert %s into %s",
NIL_P(val) ? "nil" :
@@ -2509,11 +2510,9 @@ convert_type(VALUE val, const char *tname, const char *method, int raise)
rb_obj_classname(val),
tname);
}
- else {
- return Qnil;
- }
+ return Qnil;
}
- return rb_vm_call(val, sel, 0, NULL);
+ return result;
}
VALUE
View
12 time.c
@@ -678,10 +678,7 @@ num_exact(VALUE v)
goto typeerror;
default:
- tmp = Qundef;
- if (rb_vm_respond_to(v, sel_to_r, true)) {
- tmp = rb_vm_call(v, sel_to_r, 0, NULL);
- }
+ tmp = rb_vm_check_call(v, sel_to_r, 0, NULL);
if (tmp != Qundef) {
if (rb_respond_to(v, rb_intern("to_str"))) goto typeerror;
v = tmp;
@@ -1502,7 +1499,7 @@ calc_wday(int year, int month, int day)
static VALUE
guess_local_offset(struct vtm *vtm_utc, int *isdst_ret, const char **zone_ret)
{
- struct tm tm;
+ struct tm tm = {0}; // avoid warning for gcc
long gmtoff;
const char *zone;
time_t t;
@@ -2381,10 +2378,7 @@ time_timespec(VALUE num, int interval)
default:
i = INT2FIX(1);
- ary = Qundef;
- if (rb_vm_respond_to(num, sel_divmod, true)) {
- ary = rb_vm_call(num, sel_divmod, 1, &i);
- }
+ ary = rb_vm_check_call(num, sel_divmod, 1, &i);
if (ary != Qundef && !NIL_P(ary = rb_check_array_type(ary))) {
i = rb_ary_entry(ary, 0);
f = rb_ary_entry(ary, 1);
View
9 vm.h
@@ -692,6 +692,15 @@ rb_vm_method_call(rb_vm_method_t *m, rb_vm_block_t *block, int argc,
argc, argv);
}
+static inline VALUE
+rb_vm_check_call(VALUE self, SEL sel, int argc, const VALUE *argv)
+{
+ if (!rb_vm_respond_to(self, sel, true)) {
+ return Qundef;
+ }
+ return rb_vm_call(self, sel, argc, argv);
+}
+
VALUE rb_vm_yield_args(void *vm, int argc, const VALUE *argv);
static inline VALUE
Please sign in to comment.
Something went wrong with that request. Please try again.