[macruby-changes] [1908] MacRuby/branches/experimental

source_changes at macosforge.org source_changes at macosforge.org
Sat Jun 20 01:50:06 PDT 2009


Revision: 1908
          http://trac.macosforge.org/projects/ruby/changeset/1908
Author:   lsansonetti at apple.com
Date:     2009-06-20 01:50:04 -0700 (Sat, 20 Jun 2009)
Log Message:
-----------
Boxed#[], Boxed#[]=: implemented

Modified Paths:
--------------
    MacRuby/branches/experimental/bridgesupport.cpp
    MacRuby/branches/experimental/spec/macruby/core/struct_spec.rb

Modified: MacRuby/branches/experimental/bridgesupport.cpp
===================================================================
--- MacRuby/branches/experimental/bridgesupport.cpp	2009-06-20 05:35:41 UTC (rev 1907)
+++ MacRuby/branches/experimental/bridgesupport.cpp	2009-06-20 08:50:04 UTC (rev 1908)
@@ -378,6 +378,38 @@
 }
 
 static VALUE
+rb_vm_struct_aref(VALUE rcv, SEL sel, VALUE index)
+{
+    const long idx = NUM2LONG(index);
+
+    rb_vm_bs_boxed_t *bs_boxed = locate_bs_boxed(CLASS_OF(rcv), true);
+    if (idx < 0 || (unsigned long)idx >= bs_boxed->as.s->fields_count) {
+	rb_raise(rb_eArgError, "given index %ld out of bounds", idx);
+    }
+
+    VALUE *rcv_data;
+    Data_Get_Struct(rcv, VALUE, rcv_data);
+
+    return rcv_data[idx];
+}
+
+static VALUE
+rb_vm_struct_aset(VALUE rcv, SEL sel, VALUE index, VALUE val)
+{
+    const long idx = NUM2LONG(index);
+
+    rb_vm_bs_boxed_t *bs_boxed = locate_bs_boxed(CLASS_OF(rcv), true);
+    if (idx < 0 || (unsigned long)idx >= bs_boxed->as.s->fields_count) {
+	rb_raise(rb_eArgError, "given index %ld out of bounds", idx);
+    }
+
+    char buf[100];
+    snprintf(buf, sizeof buf, "%s=:", bs_boxed->as.s->fields[idx].name);
+
+    return rb_vm_call(rcv, sel_registerName(buf), 1, &val, false);
+}
+
+static VALUE
 rb_vm_struct_dup(VALUE rcv, SEL sel)
 {
     VALUE klass = CLASS_OF(rcv);
@@ -479,6 +511,10 @@
 		(void *)rb_vm_struct_inspect, 0);
 	rb_objc_define_method(boxed->klass, "to_a",
 		(void *)rb_vm_struct_to_a, 0);
+	rb_objc_define_method(boxed->klass, "[]",
+		(void *)rb_vm_struct_aref, 1);
+	rb_objc_define_method(boxed->klass, "[]=",
+		(void *)rb_vm_struct_aset, 2);
     }
     else {
 	// Opaque methods.

Modified: MacRuby/branches/experimental/spec/macruby/core/struct_spec.rb
===================================================================
--- MacRuby/branches/experimental/spec/macruby/core/struct_spec.rb	2009-06-20 05:35:41 UTC (rev 1907)
+++ MacRuby/branches/experimental/spec/macruby/core/struct_spec.rb	2009-06-20 08:50:04 UTC (rev 1908)
@@ -225,4 +225,34 @@
     o.should == [NSPoint.new(1, 2), NSSize.new(3, 4)]
     o.map { |x| x.to_a }.flatten.should == [1, 2, 3, 4]
   end
+
+  it "allows its elements to be retrieved using #[]" do
+    p = NSPoint.new(1, 2)
+    p[0].should == 1
+    p[1].should == 2
+    p.x = 42
+    p.y = 4242
+    p[0].should == 42
+    p[1].should == 4242
+
+    lambda { p[-1] }.should raise_error(ArgumentError)
+    lambda { p[2] }.should raise_error(ArgumentError)
+  end
+
+  it "allows its elements to be set using #[]=" do
+    p = NSPoint.new
+    p[0] = 1
+    p[1] = 2
+    p.x.should == 1
+    p.y.should == 2
+
+    lambda { p[-1] = 42 }.should raise_error(ArgumentError)
+    lambda { p[2] = 42 }.should raise_error(ArgumentError)
+
+    r = NSRect.new
+    r[0] = p
+    r.origin.should == p
+    r[1] = [3, 4]
+    r.size.should == NSSize.new(3, 4)
+  end
 end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090620/d0add754/attachment-0001.html>


More information about the macruby-changes mailing list