[macruby-changes] [657] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Wed Oct 8 13:37:09 PDT 2008


Revision: 657
          http://trac.macosforge.org/projects/ruby/changeset/657
Author:   lsansonetti at apple.com
Date:     2008-10-08 13:37:08 -0700 (Wed, 08 Oct 2008)
Log Message:
-----------
catching exceptions

Modified Paths:
--------------
    MacRuby/trunk/objc.m
    MacRuby/trunk/sample-macruby/EmbeddedMacRuby/MyController.m

Modified: MacRuby/trunk/objc.m
===================================================================
--- MacRuby/trunk/objc.m	2008-10-08 02:09:49 UTC (rev 656)
+++ MacRuby/trunk/objc.m	2008-10-08 20:37:08 UTC (rev 657)
@@ -3338,9 +3338,54 @@
     return nil;
 }
 
+static VALUE
+evaluateString_safe(VALUE expression)
+{
+    return rb_eval_string([(NSString *)expression UTF8String]);
+}
+
+static VALUE
+evaluateString_rescue(VALUE expression)
+{
+    VALUE ex, ex_name, ex_message, ex_backtrace;
+    NSException *ocex;
+    static ID name_id = 0;
+    static ID message_id = 0;
+    static ID backtrace_id = 0;
+
+    if (name_id == 0) {
+	name_id = rb_intern("name");
+    }
+    if (message_id == 0) {
+	message_id = rb_intern("message");
+    }
+    if (backtrace_id == 0) {
+	backtrace_id = rb_intern("backtrace");
+    }
+
+    ex = rb_errinfo();
+    ex_name = rb_funcall(CLASS_OF(ex), name_id, 0);
+    ex_message = rb_funcall(ex, message_id, 0);
+    ex_backtrace = rb_funcall(ex, backtrace_id, 0);
+
+    ocex = [NSException exceptionWithName:(id)ex_name reason:(id)ex_message userInfo:
+	[NSDictionary dictionaryWithObjectsAndKeys:(id)ex, @"object", 
+						   (id)ex_backtrace, @"backtrace",
+						   NULL]];
+
+    [ocex raise];
+
+    return Qnil;
+}
+
 - (id)evaluateString:(NSString *)expression
 {
-    return RB2OC(rb_eval_string([expression UTF8String]));
+    VALUE ret;
+
+    ret = rb_rescue2(evaluateString_safe, (VALUE)expression,
+	    	     evaluateString_rescue, (VALUE)expression,
+		     rb_eException, (VALUE)0);
+    return RB2OC(ret);
 }
 
 - (id)evaluateFileAtPath:(NSString *)path

Modified: MacRuby/trunk/sample-macruby/EmbeddedMacRuby/MyController.m
===================================================================
--- MacRuby/trunk/sample-macruby/EmbeddedMacRuby/MyController.m	2008-10-08 02:09:49 UTC (rev 656)
+++ MacRuby/trunk/sample-macruby/EmbeddedMacRuby/MyController.m	2008-10-08 20:37:08 UTC (rev 657)
@@ -24,10 +24,17 @@
 
 - (IBAction)evaluate:(id)sender
 {
-    id object;
+    @try {
+        id object;
     
-    object = [[MacRuby sharedRuntime] evaluateString:[expressionTextView string]];
-    [resultTextView setString:[object description]];
+        object = [[MacRuby sharedRuntime] evaluateString:[expressionTextView string]];
+        [resultTextView setString:[object description]];
+    }
+    @catch (NSException *exception) {
+        NSString *string = [NSString stringWithFormat:@"%@: %@\n%@", [exception name], [exception reason], 
+            [[[exception userInfo] objectForKey:@"backtrace"] description]];
+        [resultTextView setString:string];
+    }
 }
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macruby-changes/attachments/20081008/6db8e6fe/attachment.html 


More information about the macruby-changes mailing list