Permalink
Browse files

Support to handle exception

  • Loading branch information...
1 parent 2d11d9e commit e5a2817a5ffd621eba7331ce290d9c76320d9dcf @masuidrive masuidrive committed Apr 20, 2013
Showing with 24 additions and 3 deletions.
  1. +1 −1 README.md
  2. +3 −0 mrblib/object.rb
  3. +1 −0 run_test.rb
  4. +7 −2 src/cocoa_object.m
  5. +12 −0 test/exception.rb
View
@@ -26,7 +26,7 @@ it's based on [Objective-C Runtime](https://developer.apple.com/library/mac/#doc
- [x] define protocol - instance method
- [x] define protocol - class method
- [x] adopt protocol
-- [ ] handle exception
+- [x] handle exception
- [ ] research NSCopying
- pending
View
@@ -31,6 +31,9 @@ def self.const_missing(name)
end
end
+class ObjCException < ::Exception
+end
+
class CFunc::Void
def self.objc_type_encode; 'v'; end
end
View
@@ -19,5 +19,6 @@
g.download_libffi
end
conf.gem "#{root}/mrbgems/mruby-print"
+ conf.gem "#{root}/mrbgems/mruby-enum-ext"
conf.gem File.expand_path(File.dirname(__FILE__))
end
View
@@ -416,8 +416,13 @@
}
ffi_cif cif;
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, cocoa_argc, result_ffi_type, arg_types) == FFI_OK) {
- // todo: should handling objective-c exception
- ffi_call(&cif, method, result_ptr, values);
+ @try {
+ ffi_call(&cif, method, result_ptr, values);
+ }
+ @catch (NSException *exception) {
+ cfunc_mrb_raise_without_jump(mrb, mrb_class_obj_get(mrb, "ObjCException"), "%s:%s", [[exception name] UTF8String], [[exception reason] UTF8String]);
+ goto error_exit;
+ }
if(result_ptr) {
mrb_value mresult_p = cfunc_pointer_new_with_pointer(mrb, result_ptr, 1);
if(strcmp("alloc", method_name) == 0) {
View
@@ -0,0 +1,12 @@
+mobiruby_test "Cocoa::catch exception" do
+ class Cocoa::MobiCocoaExceptionTest < Cocoa::NSObject
+ define_class CFunc::Void, :raise_exception do |i|
+ Cocoa::NSException._raise Cocoa::NSString._stringWithUTF8String("TestException"), :format, Cocoa::NSString._stringWithUTF8String("Test")
+ end
+ end
+ Cocoa::MobiCocoaExceptionTest.register
+
+ assert_raise(ObjCException) do
+ Cocoa::MobiCocoaExceptionTest._raise_exception
+ end
+end

0 comments on commit e5a2817

Please sign in to comment.