ext/rubype/rubype.c in rubype-0.3.0 vs ext/rubype/rubype.c in rubype-0.3.1

- old
+ new

@@ -38,32 +38,43 @@ default: return rb_str_new2(""); break; } } +#define assing_ivars VALUE meth_caller, meth, arg_types, rtn_type;\ + meth_caller = rb_ivar_get(self, id_owner);\ + meth = rb_ivar_get(self, id_meth);\ + arg_types = rb_ivar_get(self, id_arg_types);\ + rtn_type = rb_ivar_get(self, id_rtn_type); + static VALUE -rb_rubype_assert_type(VALUE self, VALUE args, VALUE rtn) +rb_rubype_assert_args_type(VALUE self, VALUE args) { int i; VALUE target; - VALUE meth_caller, meth, arg_types, arg_type, rtn_type, arg; + VALUE arg, arg_type; - meth_caller = rb_ivar_get(self, id_owner); - meth = rb_ivar_get(self, id_meth); - arg_types = rb_ivar_get(self, id_arg_types); - rtn_type = rb_ivar_get(self, id_rtn_type); + assing_ivars for (i=0; i<RARRAY_LEN(args); i++) { arg = rb_ary_entry(args, i); arg_type = rb_ary_entry(arg_types, i); if (unmatch_type_p(arg, arg_type)){ target = rb_sprintf("%"PRIsVALUE"#%"PRIsVALUE"'s %d argument", meth_caller, meth, i+1); rb_raise(rb_eRubypeArgumentTypeError, error_fmt, target, expected_mes(arg_type), arg); } } + return Qnil; +} +static VALUE +rb_rubype_assert_rtn_type(VALUE self, VALUE rtn) +{ + VALUE target; + assing_ivars + if (unmatch_type_p(rtn, rtn_type)){ target = rb_sprintf("%"PRIsVALUE"#%"PRIsVALUE"'s return", meth_caller, meth); rb_raise(rb_eRubypeReturnTypeError, error_fmt, target, expected_mes(rtn_type), rtn); } return rtn; @@ -87,10 +98,11 @@ rb_eRubypeReturnTypeError = rb_define_class_under(rb_mRubype, "ReturnTypeError", rb_eTypeError); rb_eInvalidTypesigError = rb_define_class_under(rb_mRubype, "InvalidTypesigError", rb_eTypeError); rb_cContract = rb_define_class_under(rb_mRubype, "Contract", rb_cObject); rb_define_method(rb_cContract, "initialize", rb_rubype_initialize, 4); - rb_define_method(rb_cContract, "assert_type", rb_rubype_assert_type, 2); + rb_define_method(rb_cContract, "assert_args_type", rb_rubype_assert_args_type, 1); + rb_define_method(rb_cContract, "assert_rtn_type", rb_rubype_assert_rtn_type, 1); id_meth = rb_intern("@meth"); id_owner = rb_intern("@owner"); id_arg_types = rb_intern("@arg_types"); id_rtn_type = rb_intern("@rtn_type");