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

source_changes at macosforge.org source_changes at macosforge.org
Thu Apr 23 16:20:16 PDT 2009


Revision: 1478
          http://trac.macosforge.org/projects/ruby/changeset/1478
Author:   lsansonetti at apple.com
Date:     2009-04-23 16:20:16 -0700 (Thu, 23 Apr 2009)
Log Message:
-----------
implemented struct #==

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

Modified: MacRuby/branches/experimental/roxor.cpp
===================================================================
--- MacRuby/branches/experimental/roxor.cpp	2009-04-23 22:50:17 UTC (rev 1477)
+++ MacRuby/branches/experimental/roxor.cpp	2009-04-23 23:20:16 UTC (rev 1478)
@@ -8541,6 +8541,32 @@
 // Readers are statically generated.
 #include "bs_struct_readers.c"
 
+static VALUE
+rb_vm_struct_equal(VALUE rcv, SEL sel, VALUE val)
+{
+    if (rcv == val) {
+	return Qtrue;
+    }
+    VALUE klass = CLASS_OF(rcv);
+    if (!rb_obj_is_kind_of(val, klass)) {
+	return Qfalse;
+    }
+
+    rb_vm_bs_boxed_t *bs_boxed = locate_bs_boxed(klass);
+
+    VALUE *rcv_data;
+    VALUE *val_data;
+    Data_Get_Struct(rcv, VALUE, rcv_data);
+    Data_Get_Struct(val, VALUE, val_data);
+
+    for (unsigned i = 0; i < bs_boxed->as.s->fields_count; i++) {
+	if (!rb_equal(rcv_data[i], val_data[i])) {
+	    return Qfalse;
+	}
+    }
+    return Qtrue;
+}
+
 static bool
 register_bs_boxed(bs_element_type_t type, void *value)
 {
@@ -8581,6 +8607,10 @@
 	    rb_objc_define_method(boxed->klass, buf,
 		    (void *)rb_vm_struct_fake_set, 1);
 	}
+
+	// Define other utility methods.
+	rb_objc_define_method(boxed->klass, "==",
+		(void *)rb_vm_struct_equal, 1);
     }
 
     GET_VM()->bs_boxed[octype] = boxed;

Modified: MacRuby/branches/experimental/spec/macruby/struct_spec.rb
===================================================================
--- MacRuby/branches/experimental/spec/macruby/struct_spec.rb	2009-04-23 22:50:17 UTC (rev 1477)
+++ MacRuby/branches/experimental/spec/macruby/struct_spec.rb	2009-04-23 23:20:16 UTC (rev 1478)
@@ -115,4 +115,27 @@
     r.size.width.should == 789.0
     r.size.height.should == 100.0
   end
+
+  it "can be compared to another similar object using #==" do
+    p1 = NSPoint.new(1, 2)
+    p2 = NSPoint.new(3, 4)
+    p1.should_not == p2
+
+    p2.x = 1
+    p2.y = 2
+    p1.should == p2
+
+    r1 = NSRect.new
+    r2 = NSRect.new
+    r1.should == r2
+
+    r1.origin = NSPoint.new(1, 2)
+    r1.size = NSSize.new(3, 4)
+    r2.origin = NSPoint.new(1, 2)
+    r2.size = NSSize.new(3, 42)
+    r1.should_not == r2
+
+    r2.size.height = 4
+    r1.should == r2
+  end
 end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090423/1f8c61d0/attachment-0001.html>


More information about the macruby-changes mailing list