Revision: 1499 http://trac.macosforge.org/projects/ruby/changeset/1499 Author: lsansonetti@apple.com Date: 2009-04-27 22:13:10 -0700 (Mon, 27 Apr 2009) Log Message: ----------- guess what, more Pointer work Modified Paths: -------------- MacRuby/branches/experimental/include/ruby/ruby.h MacRuby/branches/experimental/objc.m MacRuby/branches/experimental/roxor.cpp MacRuby/branches/experimental/spec/macruby/fixtures/method.m MacRuby/branches/experimental/spec/macruby/objc_method_spec.rb Modified: MacRuby/branches/experimental/include/ruby/ruby.h =================================================================== --- MacRuby/branches/experimental/include/ruby/ruby.h 2009-04-28 04:39:25 UTC (rev 1498) +++ MacRuby/branches/experimental/include/ruby/ruby.h 2009-04-28 05:13:10 UTC (rev 1499) @@ -1065,6 +1065,7 @@ RUBY_EXTERN VALUE rb_cNSMutableSet; RUBY_EXTERN VALUE rb_cCFNumber; RUBY_EXTERN VALUE rb_cBoxed; +RUBY_EXTERN VALUE rb_cPointer; bool _CFArrayIsMutable(void *); #define RARRAY_IMMUTABLE(o) (*(VALUE *)o == rb_cCFArray && !_CFArrayIsMutable((void *)o)) Modified: MacRuby/branches/experimental/objc.m =================================================================== --- MacRuby/branches/experimental/objc.m 2009-04-28 04:39:25 UTC (rev 1498) +++ MacRuby/branches/experimental/objc.m 2009-04-28 05:13:10 UTC (rev 1499) @@ -75,7 +75,7 @@ static struct st_table *bs_inf_prot_imethods; static struct st_table *bs_cftypes; -static VALUE rb_cPointer; +//static VALUE rb_cPointer; struct RPointer { @@ -503,6 +503,7 @@ return n; } +#if 0 static VALUE rb_pointer_create(void *ptr, const char *type) { @@ -514,6 +515,7 @@ return Data_Wrap_Struct(rb_cPointer, NULL, NULL, data); } +#endif static void rb_objc_rval_to_ocval(VALUE, const char *, void **); @@ -720,12 +722,14 @@ if (SPECIAL_CONST_P(rval)) { ok = false; } +#if 0 else if (*(VALUE *)rval == rb_cPointer) { struct RPointer *data; Data_Get_Struct(rval, struct RPointer, data); *(void **)ocval = data->ptr; } +#endif else if (strcmp(octype, "^v") == 0) { *(void **)ocval = (void *)rval; } @@ -972,7 +976,7 @@ *rbval = Qnil; } else { - *rbval = rb_pointer_create(*(void **)ocval, octype + 1); + //*rbval = rb_pointer_create(*(void **)ocval, octype + 1); } break; Modified: MacRuby/branches/experimental/roxor.cpp =================================================================== --- MacRuby/branches/experimental/roxor.cpp 2009-04-28 04:39:25 UTC (rev 1498) +++ MacRuby/branches/experimental/roxor.cpp 2009-04-28 05:13:10 UTC (rev 1499) @@ -5070,6 +5070,26 @@ *ocval = (float)rval_to_double(rval); } +static void *rb_pointer_get_data(VALUE rcv); + +extern "C" +void +rb_vm_rval_to_cptr(VALUE rval, void **cptr) +{ + if (NIL_P(rval)) { + *cptr = NULL; + } + else { + if (!rb_obj_is_kind_of(rval, rb_cPointer)) { + rb_raise(rb_eTypeError, + "expected instance of Pointer, got `%s' (%s)", + RSTRING_PTR(rb_inspect(rval)), + rb_obj_classname(rval)); + } + *cptr = rb_pointer_get_data(rval); + } +} + static inline long rebuild_new_struct_ary(const StructType *type, VALUE orig, VALUE new_ary) { @@ -5336,13 +5356,14 @@ if (bs_boxed != NULL) { return compile_get_opaque_data(val, bs_boxed, slot); } + + func_name = "rb_vm_rval_to_cptr"; } break; } if (func_name == NULL) { - printf("unrecognized compile type `%s' to C - aborting\n", type); - abort(); + rb_raise(rb_eTypeError, "unrecognized compile type `%s' to C", type); } std::vector<Value *> params; @@ -5579,7 +5600,7 @@ } if (func_name == NULL) { - printf("unrecognized compile type `%s' to Ruby - aborting\n", type); + rb_raise(rb_eTypeError, "unrecognized compile type `%s' to Ruby", type); abort(); } @@ -5660,7 +5681,7 @@ break; } - rb_raise(rb_eTypeError, "unrecognized runtime type `%s'\n", type); + rb_raise(rb_eTypeError, "unrecognized runtime type `%s'", type); } Function * @@ -9115,6 +9136,15 @@ return Data_Wrap_Struct(rb_cPointer, NULL, NULL, ptr); } +static void * +rb_pointer_get_data(VALUE rcv) +{ + rb_vm_pointer_t *ptr; + Data_Get_Struct(rcv, rb_vm_pointer_t, ptr); + + return ptr->val; +} + #define POINTER_VAL(ptr, idx) \ (void *)((char *)ptr->val + (FIX2INT(idx) * ptr->type_size)) @@ -9258,14 +9288,14 @@ RoxorVM::find_bs_struct(std::string type) { rb_vm_bs_boxed_t *boxed = find_bs_boxed(type); - return boxed->is_struct() ? boxed : NULL; + return boxed == NULL ? NULL : boxed->is_struct() ? boxed : NULL; } inline rb_vm_bs_boxed_t * RoxorVM::find_bs_opaque(std::string type) { rb_vm_bs_boxed_t *boxed = find_bs_boxed(type); - return boxed->is_struct() ? NULL : boxed; + return boxed == NULL ? NULL : boxed->is_struct() ? NULL : boxed; } static inline void Modified: MacRuby/branches/experimental/spec/macruby/fixtures/method.m =================================================================== --- MacRuby/branches/experimental/spec/macruby/fixtures/method.m 2009-04-28 04:39:25 UTC (rev 1498) +++ MacRuby/branches/experimental/spec/macruby/fixtures/method.m 2009-04-28 05:13:10 UTC (rev 1499) @@ -345,6 +345,43 @@ && a7 == 42; } +- (BOOL)methodAcceptingIntPtr:(int *)ptr +{ + BOOL ok = ptr[0] == 42; + ptr[0] = 43; + return ok; +} + +- (BOOL)methodAcceptingIntPtr2:(int *)ptr +{ + return ptr == NULL; +} + +- (BOOL)methodAcceptingObjectPtr:(id *)ptr +{ + BOOL ok = ptr[0] == self; + ptr[0] = (id)[NSObject class]; + return ok; +} + +- (BOOL)methodAcceptingObjectPtr2:(id *)ptr +{ + return ptr == NULL; +} + +- (BOOL)methodAcceptingNSRectPtr:(NSRect *)ptr +{ + BOOL ok = ptr->origin.x == 1 && ptr->origin.y == 2 + && ptr->size.width == 3 && ptr->size.height == 4; + *ptr = NSMakeRect(42, 43, 44, 45); + return ok; +} + +- (BOOL)methodAcceptingNSRectPtr2:(NSRect *)ptr +{ + return ptr == NULL; +} + @end void Modified: MacRuby/branches/experimental/spec/macruby/objc_method_spec.rb =================================================================== --- MacRuby/branches/experimental/spec/macruby/objc_method_spec.rb 2009-04-28 04:39:25 UTC (rev 1498) +++ MacRuby/branches/experimental/spec/macruby/objc_method_spec.rb 2009-04-28 05:13:10 UTC (rev 1499) @@ -372,4 +372,40 @@ @o.methodAcceptingInt(42, float:42, double:42, short:42, NSPoint:[42, 42], NSRect:[42, 42, 42, 42], char:42).should == 1 end + + it "accepting an 'int *' should be given a Pointer object and receive the pointer as expected" do + p = Pointer.new(:int) + p[0] = 42 + @o.methodAcceptingIntPtr(p).should == 1 + p[0].should == 43 + + lambda { @o.methodAcceptingIntPtr(123) }.should raise_error(TypeError) + + @o.methodAcceptingIntPtr2(nil).should == 1 + end + + it "accepting an 'id *' should be given a Pointer object and receive the pointer as expected" do + p = Pointer.new(:object) + p[0] = @o + @o.methodAcceptingObjectPtr(p).should == 1 + p[0].should == NSObject + + lambda { @o.methodAcceptingObjectPtr(123) }.should raise_error(TypeError) + + @o.methodAcceptingObjectPtr2(nil).should == 1 + end + + it "accepting an 'NSRect *' should be given a Pointer object and receive the pointer as expected" do + p = Pointer.new(NSRect.type) + p[0] = [1, 2, 3, 4] + @o.methodAcceptingNSRectPtr(p).should == 1 + p[0].origin.x.should == 42 + p[0].origin.y.should == 43 + p[0].size.width.should == 44 + p[0].size.height.should == 45 + + lambda { @o.methodAcceptingNSRectPtr(123) }.should raise_error(TypeError) + + @o.methodAcceptingNSRectPtr2(nil).should == 1 + end end
participants (1)
-
source_changes@macosforge.org