Revision: 3923 http://trac.macosforge.org/projects/ruby/changeset/3923 Author: eloy.de.enige@gmail.com Date: 2010-04-13 14:13:55 -0700 (Tue, 13 Apr 2010) Log Message: ----------- Use the same message format for printed exceptions everywhere. Modified Paths: -------------- MacRuby/trunk/error.c MacRuby/trunk/include/ruby/intern.h MacRuby/trunk/objc.m MacRuby/trunk/vm.cpp Modified: MacRuby/trunk/error.c =================================================================== --- MacRuby/trunk/error.c 2010-04-13 05:26:55 UTC (rev 3922) +++ MacRuby/trunk/error.c 2010-04-13 21:13:55 UTC (rev 3923) @@ -1215,6 +1215,38 @@ if (OBJ_FROZEN(obj)) rb_error_frozen(rb_obj_classname(obj)); } +VALUE +rb_format_exception_message(VALUE exc) +{ + VALUE message = rb_vm_call(exc, sel_registerName("message"), 0, NULL, + false); + VALUE bt = rb_vm_call(exc, sel_registerName("backtrace"), 0, NULL, + false); + + CFMutableStringRef result = CFStringCreateMutable(NULL, 0); + + const long count = (bt != Qnil ? RARRAY_LEN(bt) : 0); + if (count > 0) { + for (long i = 0; i < count; i++) { + const char *bte = RSTRING_PTR(RARRAY_AT(bt, i)); + if (i == 0) { + CFStringAppendFormat(result, NULL, CFSTR("%s: %s (%s)\n"), + bte, RSTRING_PTR(message), rb_class2name(*(VALUE *)exc)); + } + else { + CFStringAppendFormat(result, NULL, CFSTR("\tfrom %s\n"), bte); + } + } + } + else { + CFStringAppendFormat(result, NULL, CFSTR("%s (%s)\n"), + RSTRING_PTR(message), rb_class2name(*(VALUE *)exc)); + } + + CFMakeCollectable(result); + return (VALUE)result; +} + void Init_syserr(void) { Modified: MacRuby/trunk/include/ruby/intern.h =================================================================== --- MacRuby/trunk/include/ruby/intern.h 2010-04-13 05:26:55 UTC (rev 3922) +++ MacRuby/trunk/include/ruby/intern.h 2010-04-13 21:13:55 UTC (rev 3923) @@ -205,6 +205,7 @@ VALUE rb_exc_new(VALUE, const char*, long); VALUE rb_exc_new2(VALUE, const char*); VALUE rb_exc_new3(VALUE, VALUE); +VALUE rb_format_exception_message(VALUE exc); PRINTF_ARGS(NORETURN(void rb_loaderror(const char*, ...)), 1, 2); PRINTF_ARGS(NORETURN(void rb_name_error(ID, const char*, ...)), 2, 3); NORETURN(void rb_invalid_str(const char*, const char*)); Modified: MacRuby/trunk/objc.m =================================================================== --- MacRuby/trunk/objc.m 2010-04-13 05:26:55 UTC (rev 3922) +++ MacRuby/trunk/objc.m 2010-04-13 21:13:55 UTC (rev 3923) @@ -606,7 +606,7 @@ rb_rb2oc_exception(VALUE exc) { NSString *name = [NSString stringWithUTF8String:rb_obj_classname(exc)]; - NSString *reason = [(id)exc performSelector:@selector(message)]; + NSString *reason = (NSString *)rb_format_exception_message(exc); #if 0 // This is technically not required, and it seems that some exceptions // don't like to be treated like NSDictionary values... Modified: MacRuby/trunk/vm.cpp =================================================================== --- MacRuby/trunk/vm.cpp 2010-04-13 05:26:55 UTC (rev 3922) +++ MacRuby/trunk/vm.cpp 2010-04-13 21:13:55 UTC (rev 3923) @@ -3837,29 +3837,7 @@ printf("uncaught Objective-C/C++ exception...\n"); std::terminate(); } - - VALUE message = rb_vm_call(exc, sel_registerName("message"), 0, NULL, - false); - VALUE bt = rb_vm_call(exc, sel_registerName("backtrace"), 0, NULL, - false); - - const long count = (bt != Qnil ? RARRAY_LEN(bt) : 0); - if (count > 0) { - for (long i = 0; i < count; i++) { - const char *bte = RSTRING_PTR(RARRAY_AT(bt, i)); - if (i == 0) { - printf("%s: %s (%s)\n", bte, RSTRING_PTR(message), - rb_class2name(*(VALUE *)exc)); - } - else { - printf("\tfrom %s\n", bte); - } - } - } - else { - printf("%s (%s)\n", RSTRING_PTR(message), - rb_class2name(*(VALUE *)exc)); - } + printf("%s", rb_str_cstr(rb_format_exception_message(exc))); } extern "C"