Revision: 1518 http://trac.macosforge.org/projects/ruby/changeset/1518 Author: lsansonetti@apple.com Date: 2009-05-02 20:05:49 -0700 (Sat, 02 May 2009) Log Message: ----------- added support for CF types bridging Modified Paths: -------------- MacRuby/branches/experimental/roxor.cpp MacRuby/branches/experimental/sample-macruby/Scripts/circle.rb Added Paths: ----------- MacRuby/branches/experimental/spec/macruby/cftype_spec.rb Modified: MacRuby/branches/experimental/roxor.cpp =================================================================== --- MacRuby/branches/experimental/roxor.cpp 2009-05-03 01:02:45 UTC (rev 1517) +++ MacRuby/branches/experimental/roxor.cpp 2009-05-03 03:05:49 UTC (rev 1518) @@ -527,6 +527,7 @@ std::map<ID, bs_element_constant_t *> bs_consts; std::map<std::string, std::map<SEL, bs_element_method_t *> *> bs_classes_class_methods, bs_classes_instance_methods; + std::map<std::string, bs_element_cftype_t *> bs_cftypes; bs_element_method_t *find_bs_method(Class klass, SEL sel); rb_vm_bs_boxed_t *find_bs_boxed(std::string type); @@ -5340,6 +5341,11 @@ type = SkipTypeModifiers(type); + if (*type == _C_PTR + && GET_VM()->bs_cftypes.find(type) != GET_VM()->bs_cftypes.end()) { + type = "@"; + } + switch (*type) { case _C_ID: case _C_CLASS: @@ -5596,6 +5602,11 @@ type = SkipTypeModifiers(type); + if (*type == _C_PTR + && GET_VM()->bs_cftypes.find(type) != GET_VM()->bs_cftypes.end()) { + type = "@"; + } + switch (*type) { case _C_VOID: return nilVal; @@ -5723,7 +5734,7 @@ case _C_ID: case _C_CLASS: - return RubyObjTy; + return PtrTy; case _C_SEL: case _C_CHARPTR: @@ -9766,12 +9777,18 @@ case BS_ELEMENT_CFTYPE: { -#if 0 + bs_element_cftype_t *bs_cftype = (bs_element_cftype_t *)value; - st_insert(bs_cftypes, (st_data_t)bs_cftype->type, - (st_data_t)bs_cftype); - do_not_free = true; -#endif + std::map<std::string, bs_element_cftype_t *>::iterator + iter = GET_VM()->bs_cftypes.find(bs_cftype->type); + if (iter == GET_VM()->bs_cftypes.end()) { + GET_VM()->bs_cftypes[bs_cftype->type] = bs_cftype; + do_not_free = true; + } + else { + rb_warning("bs: CF type `%s' already defined", + bs_cftype->type); + } break; } } Modified: MacRuby/branches/experimental/sample-macruby/Scripts/circle.rb =================================================================== --- MacRuby/branches/experimental/sample-macruby/Scripts/circle.rb 2009-05-03 01:02:45 UTC (rev 1517) +++ MacRuby/branches/experimental/sample-macruby/Scripts/circle.rb 2009-05-03 03:05:49 UTC (rev 1518) @@ -2,7 +2,9 @@ framework 'Cocoa' url = NSURL.fileURLWithPath('circle.pdf') -pdf = CGPDFContextCreateWithURL(url, [[0, 0], [617, 792]], nil) +prect = Pointer.new(CGRect.type) +prect[0] = [[0, 0], [617, 792]] +pdf = CGPDFContextCreateWithURL(url, prect, nil) CGPDFContextBeginPage(pdf, nil) CGContextSetRGBFillColor(pdf, 1.0, 0.0, 0.0, 1.0) Added: MacRuby/branches/experimental/spec/macruby/cftype_spec.rb =================================================================== --- MacRuby/branches/experimental/spec/macruby/cftype_spec.rb (rev 0) +++ MacRuby/branches/experimental/spec/macruby/cftype_spec.rb 2009-05-03 03:05:49 UTC (rev 1518) @@ -0,0 +1,29 @@ +require File.dirname(__FILE__) + "/spec_helper" +FixtureCompiler.require! "method" + +describe "A CoreFoundation type" do + it "behaves like a regular Objective-C/Ruby object" do + o = CFBundleGetMainBundle() + o.class.should == NSCFType + o.inspect.class.should == String + end + + it "can be passed to a C/Objective-C API" do + o = CFBundleGetMainBundle() + CFBundleIsExecutableLoaded(o).should == true + end + + it "toll-free bridged to an Objective-C type behaves like the Objective-C version" do + s = CFStringCreateWithCString(nil, "foo", KCFStringEncodingUTF8) + s.class.should == NSString + s.should == 'foo' + CFRelease(s) + end + + it "can be substitued with the toll-free bridged equivalent" do + s = CFStringCreateMutableCopy(nil, 0, "foo") + s.class.should == NSMutableString + s.should == 'foo' + CFRelease(s) + end +end