Skip to content
This repository
Browse code

Convert Range instances to NS/CFRange structures when passed as argum…

…ent.

Closes #35.
  • Loading branch information...
commit 7b1f3619faea4e42104c3e6832266ebb016ab88d 1 parent f0633b3
Eloy Durán authored July 07, 2011
21  bridgesupport.cpp
@@ -797,6 +797,27 @@ rb_vm_get_struct_fields(VALUE rval, VALUE *buf, rb_vm_bs_boxed_t *bs_boxed)
797 797
 	    buf[i] = RARRAY_AT(rval, i);
798 798
 	}
799 799
     }
  800
+    else if (CLASS_OF(rval) == rb_cRange &&
  801
+		(strcmp(bs_boxed->as.s->name, "NSRange") == 0 ||
  802
+		    strcmp(bs_boxed->as.s->name, "CFRange") == 0)) {
  803
+	VALUE b, e;
  804
+	int exclusive;
  805
+	rb_range_values(rval, &b, &e, &exclusive);
  806
+	int begin = NUM2INT(b);
  807
+	int end = NUM2INT(e);
  808
+	if (begin < 0 || end < 0) {
  809
+	    // We don't know what the max length of the range will be, so we
  810
+	    // can't count backwards.
  811
+	    rb_raise(rb_eArgError,
  812
+		    "negative values are not allowed in ranges " \
  813
+		    "that are converted to %s structures `%s'",
  814
+		    bs_boxed->as.s->name,
  815
+		    RSTRING_PTR(rb_inspect(rval)));
  816
+	}
  817
+	int length = exclusive ? end-1-begin : end-begin;
  818
+	buf[0] = INT2NUM(begin);
  819
+	buf[1] = INT2NUM(length);
  820
+    }
800 821
     else {
801 822
 	if (!rb_obj_is_kind_of(rval, bs_boxed->klass)) {
802 823
 	    rb_raise(rb_eTypeError, 
4  range.c
@@ -691,10 +691,10 @@ rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
691 691
     }
692 692
     else {
693 693
 	if (!rb_vm_respond_to(range, selBeg, false)) {
694  
-	    return Qfalse;
  694
+	    return 0;
695 695
 	}
696 696
 	if (!rb_vm_respond_to(range, selEnd, false)) {
697  
-	    return Qfalse;
  697
+	    return 0;
698 698
 	}
699 699
 	b = rb_vm_call(range, selBeg, 0, NULL);
700 700
 	e = rb_vm_call(range, selEnd, 0, NULL);
16  spec/macruby/language/objc_method_spec.rb
@@ -367,6 +367,22 @@ def o2.to_str; 'foo' end
367 367
     lambda { @o.methodAcceptingNSRect([[1, 2], [3, 4, 5]]) }.should raise_error(ArgumentError)
368 368
   end
369 369
 
  370
+  it "accepting a Range as a structure type should receive a NSRange C structure" do
  371
+    @o.methodAcceptingNSRange(0..41).should == 0
  372
+    @o.methodAcceptingNSRange(1..42).should == 0
  373
+    @o.methodAcceptingNSRange(0..42).should == 1
  374
+    @o.methodAcceptingNSRange(0...42).should == 0
  375
+    @o.methodAcceptingNSRange(1...42).should == 0
  376
+    @o.methodAcceptingNSRange(0...43).should == 1
  377
+
  378
+    lambda { @o.methodAcceptingNSRange(-1..1) }.should raise_error(ArgumentError)
  379
+    lambda { @o.methodAcceptingNSRange(1..-1) }.should raise_error(ArgumentError)
  380
+    lambda { @o.methodAcceptingNSRange(-1..-1) }.should raise_error(ArgumentError)
  381
+    lambda { @o.methodAcceptingNSRange(-1...1) }.should raise_error(ArgumentError)
  382
+    lambda { @o.methodAcceptingNSRange(1...-1) }.should raise_error(ArgumentError)
  383
+    lambda { @o.methodAcceptingNSRange(-1...-1) }.should raise_error(ArgumentError)
  384
+  end
  385
+
370 386
   it "accepting various C types should receive these types as expected" do
371 387
     @o.methodAcceptingInt(42, float:42, double:42, short:42, NSPoint:[42, 42],
372 388
                           NSRect:[42, 42, 42, 42], char:42).should == 1

0 notes on commit 7b1f361

Please sign in to comment.
Something went wrong with that request. Please try again.