[macruby-changes] [3863] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 24 23:26:21 PDT 2010


Revision: 3863
          http://trac.macosforge.org/projects/ruby/changeset/3863
Author:   martinlagardette at apple.com
Date:     2010-03-24 23:26:21 -0700 (Wed, 24 Mar 2010)
Log Message:
-----------
Make Symbol NSCoding compliant

 - Added `-classForKeyedArchiver`, `-encodeWithCoder:` and `-initWithCoder:` so that symbols can be archived in Cocoa
 - Added `-copy` returning `self` (copying a symbol does not make sense) so that copied symbols return symbols, not strings
 - Fixes 7716974

Modified Paths:
--------------
    MacRuby/trunk/NSString.m
    MacRuby/trunk/encoding.h
    MacRuby/trunk/symbol.c

Added Paths:
-----------
    MacRuby/trunk/spec/macruby/core/symbol_spec.rb

Modified: MacRuby/trunk/NSString.m
===================================================================
--- MacRuby/trunk/NSString.m	2010-03-25 06:12:31 UTC (rev 3862)
+++ MacRuby/trunk/NSString.m	2010-03-25 06:26:21 UTC (rev 3863)
@@ -180,6 +180,20 @@
 }
 
 void
+rb_str_NSCoder_encode(void *coder, VALUE str, const char *key)
+{
+    NSString *nskey = [NSString stringWithUTF8String:key];
+    [(NSCoder *)coder encodeObject:(NSString *)str forKey:nskey];
+}
+
+VALUE
+rb_str_NSCoder_decode(void *coder, const char *key)
+{
+    NSString *nskey = [NSString stringWithUTF8String:key];
+    return OC2RB([(NSCoder *)coder decodeObjectForKey:nskey]);
+}
+
+void
 Init_NSString(void)
 {
     rb_cNSString = (VALUE)objc_getClass("NSString");

Modified: MacRuby/trunk/encoding.h
===================================================================
--- MacRuby/trunk/encoding.h	2010-03-25 06:12:31 UTC (rev 3862)
+++ MacRuby/trunk/encoding.h	2010-03-25 06:26:21 UTC (rev 3863)
@@ -289,6 +289,9 @@
 	    STRING_VALID_ENCODING);
 }
 
+void rb_str_NSCoder_encode(void *coder, VALUE str, const char *key);
+VALUE rb_str_NSCoder_decode(void *coder, const char *key);
+
 VALUE mr_enc_s_is_compatible(VALUE klass, SEL sel, VALUE str1, VALUE str2);
 VALUE rb_str_intern_fast(VALUE str);
 VALUE rstr_aref(VALUE str, SEL sel, int argc, VALUE *argv);

Added: MacRuby/trunk/spec/macruby/core/symbol_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/core/symbol_spec.rb	                        (rev 0)
+++ MacRuby/trunk/spec/macruby/core/symbol_spec.rb	2010-03-25 06:26:21 UTC (rev 3863)
@@ -0,0 +1,16 @@
+require File.dirname(__FILE__) + "/../spec_helper"
+
+describe "A Symbol" do
+  it "should conform to the NSCoding protocol" do
+    # framework 'Foundation'
+    NSKeyedUnarchiver.unarchiveObjectWithData(NSKeyedArchiver.archivedDataWithRootObject(:test)).should == :test
+  end
+
+  it "should return 'Symbol' on -classForKeyedArchiver" do
+    :sym.classForKeyedArchiver.class.should == Symbol.class
+  end
+
+  it "should return self on -copy" do
+    :sym.copy.__id__.should == :sym.__id__
+  end
+end

Modified: MacRuby/trunk/symbol.c
===================================================================
--- MacRuby/trunk/symbol.c	2010-03-25 06:12:31 UTC (rev 3862)
+++ MacRuby/trunk/symbol.c	2010-03-25 06:26:21 UTC (rev 3863)
@@ -673,6 +673,14 @@
     return ID2SYM(rb_intern_str(rstr_swapcase(RSYM(sym)->str, sel)));
 }
 
+// Cocoa primitives
+
+static void *
+rsym_imp_copy(void *rcv, SEL sel)
+{
+    return rcv;
+}
+
 static CFIndex
 rsym_imp_length(void *rcv, SEL sel)
 {
@@ -685,6 +693,26 @@
     return CFStringGetCharacterAtIndex((CFStringRef)RSYM(rcv)->str, idx);
 }
 
+#define RSYM_NSCODER_KEY "MRSymbolStr"
+
+static void
+rsym_imp_encodeWithCoder(void *rcv, SEL sel, void *coder)
+{
+    rb_str_NSCoder_encode(coder, RSYM(rcv)->str, RSYM_NSCODER_KEY);
+}
+
+static VALUE
+rsym_imp_initWithCoder(void *rcv, SEL sel, void *coder)
+{
+    return ID2SYM(rb_intern_str(rb_str_NSCoder_decode(coder, RSYM_NSCODER_KEY)));
+}
+
+static Class
+rsym_imp_classForKeyedArchiver(void *rcv, SEL sel)
+{
+    return (Class)rb_cSymbol;
+}
+
 void
 Init_Symbol(void)
 {
@@ -720,8 +748,16 @@
     rb_objc_define_method(rb_cSymbol, "capitalize", rsym_capitalize, 0);
 
     // Cocoa primitives.
+    rb_objc_install_method2((Class)rb_cSymbol, "copy",
+	    (IMP)rsym_imp_copy);
     rb_objc_install_method2((Class)rb_cSymbol, "length",
 	    (IMP)rsym_imp_length);
     rb_objc_install_method2((Class)rb_cSymbol, "characterAtIndex:",
 	    (IMP)rsym_imp_characterAtIndex);
+    rb_objc_install_method2((Class)rb_cSymbol, "encodeWithCoder:",
+	    (IMP)rsym_imp_encodeWithCoder);
+    rb_objc_install_method2((Class)rb_cSymbol, "initWithCoder:",
+	    (IMP)rsym_imp_initWithCoder);
+    rb_objc_install_method2((Class)rb_cSymbol, "classForKeyedArchiver",
+	    (IMP)rsym_imp_classForKeyedArchiver);
 }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100324/1f10db6b/attachment.html>


More information about the macruby-changes mailing list