[macruby-changes] [2338] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Sun Aug 16 12:37:19 PDT 2009


Revision: 2338
          http://trac.macosforge.org/projects/ruby/changeset/2338
Author:   lsansonetti at apple.com
Date:     2009-08-16 12:37:16 -0700 (Sun, 16 Aug 2009)
Log Message:
-----------
some ByteString improvements

Modified Paths:
--------------
    MacRuby/trunk/include/ruby/ruby.h
    MacRuby/trunk/object.c
    MacRuby/trunk/re.c
    MacRuby/trunk/string.c

Modified: MacRuby/trunk/include/ruby/ruby.h
===================================================================
--- MacRuby/trunk/include/ruby/ruby.h	2009-08-16 08:14:08 UTC (rev 2337)
+++ MacRuby/trunk/include/ruby/ruby.h	2009-08-16 19:37:16 UTC (rev 2338)
@@ -404,6 +404,7 @@
 VALUE rb_bytestring_new();
 VALUE rb_bytestring_new_with_data(const UInt8 *buf, long size);
 VALUE rb_bytestring_new_with_cfdata(CFMutableDataRef data);
+VALUE rb_bytestring_copy(VALUE str);
 CFMutableDataRef rb_bytestring_wrapped_data(VALUE);
 CFStringRef rb_bytestring_resolve_cfstring(VALUE str);
 UInt8 *rb_bytestring_byte_pointer(VALUE);

Modified: MacRuby/trunk/object.c
===================================================================
--- MacRuby/trunk/object.c	2009-08-16 08:14:08 UTC (rev 2337)
+++ MacRuby/trunk/object.c	2009-08-16 19:37:16 UTC (rev 2338)
@@ -779,8 +779,7 @@
 		// fall through
 	    case T_STRING:
 		if (*(VALUE *)obj == rb_cByteString) {
-		    return rb_objc_flag_check((const void *)obj, FL_TAINT)
-			? Qtrue : Qfalse;
+		    return RBASIC(obj)->flags & FL_TAINT ? Qtrue : Qfalse;
 		}
 		// fall through
 	    case T_HASH:
@@ -826,7 +825,7 @@
 		// fall through
 	    case T_STRING:
 		if (*(VALUE *)obj == rb_cByteString) {
-		    rb_objc_flag_set((const void *)obj, FL_TAINT, true);
+		    RBASIC(obj)->flags |= FL_TAINT;
 		    break;
 		}
 		// fall through
@@ -871,13 +870,13 @@
 	switch (TYPE(obj)) {
 	    case T_ARRAY:
 		if (*(VALUE *)obj != rb_cCFArray) {
-		   RBASIC(obj)->flags &= ~FL_TAINT;
-		   break;
+		    RBASIC(obj)->flags &= ~FL_TAINT;
+		    break;
 		}	
 		// fall through
 	    case T_STRING:
 		if (*(VALUE *)obj == rb_cByteString) {
-		    rb_objc_flag_set((const void *)obj, FL_TAINT, false);
+		    RBASIC(obj)->flags &= ~FL_TAINT;
 		    break;
 		}
 		// fall through
@@ -918,8 +917,7 @@
 		// fall through
 	    case T_STRING:
 		if (*(VALUE *)obj == rb_cByteString) {
-		    return rb_objc_flag_check((const void *)obj, FL_UNTRUSTED)
-			? Qtrue : Qfalse;
+		    return RBASIC(obj)->flags & FL_UNTRUSTED ? Qtrue : Qfalse;
 		}
 		// fall through
 	    case T_HASH:
@@ -956,7 +954,7 @@
 		// fall through
 	    case T_STRING:
 		if (*(VALUE *)obj == rb_cByteString) {
-		    rb_objc_flag_set((const void *)obj, FL_UNTRUSTED, false);
+		    RBASIC(obj)->flags &= ~FL_UNTRUSTED;
 		    break;
 		}
 		// fall through
@@ -999,7 +997,7 @@
 		// fall through
 	    case T_STRING:
 		if (*(VALUE *)obj == rb_cByteString) {
-		    rb_objc_flag_set((const void *)obj, FL_UNTRUSTED, true);
+		    RBASIC(obj)->flags |= FL_UNTRUSTED;
 		    break;
 		}
 		// fall through
@@ -1080,7 +1078,7 @@
 		    // fall through
 		case T_STRING:
 		    if (*(VALUE *)obj == rb_cByteString) {
-			rb_objc_flag_set((const void *)obj, FL_FREEZE, true);
+			RBASIC(obj)->flags |= FL_FREEZE;
 			break;
 		    }
 		    // fall through
@@ -1106,7 +1104,7 @@
 VALUE
 rb_obj_freeze(VALUE obj)
 {
-	return rb_obj_freeze_m(obj, 0);
+    return rb_obj_freeze_m(obj, 0);
 }
 
 /*
@@ -1124,8 +1122,12 @@
 rb_obj_frozen(VALUE obj, SEL sel)
 {
     if (SPECIAL_CONST_P(obj)) {
-	if (!immediate_frozen_tbl) return Qfalse;
-	if (st_lookup(immediate_frozen_tbl, obj, 0)) return Qtrue;
+	if (!immediate_frozen_tbl) {
+	    return Qfalse;
+	}
+	if (st_lookup(immediate_frozen_tbl, obj, 0)) {
+	    return Qtrue;
+	}
 	return Qfalse;
     }
     switch (TYPE(obj)) {
@@ -1136,8 +1138,7 @@
 	    // fall through
 	case T_STRING:
 	    if (*(VALUE *)obj == rb_cByteString) {
-		return rb_objc_flag_check((const void *)obj, FL_FREEZE)
-		    ? Qtrue : Qfalse;
+		return RBASIC(obj)->flags & FL_FREEZE ? Qtrue : Qfalse;
 	    }
 	    // fall through
 	case T_HASH:
@@ -2913,9 +2914,9 @@
     rb_objc_define_method(rb_mKernel, "untaint", rb_obj_untaint_m, 0);
     rb_objc_define_method(rb_mKernel, "freeze", rb_obj_freeze_m, 0);
     rb_objc_define_method(rb_mKernel, "frozen?", rb_obj_frozen, 0);
-	rb_objc_define_method(rb_mKernel, "trust", rb_obj_trust_imp, 0);
-	rb_objc_define_method(rb_mKernel, "untrust", rb_obj_untrust_imp, 0);
-	rb_objc_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted_imp, 0);
+    rb_objc_define_method(rb_mKernel, "trust", rb_obj_trust_imp, 0);
+    rb_objc_define_method(rb_mKernel, "untrust", rb_obj_untrust_imp, 0);
+    rb_objc_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted_imp, 0);
 
     rb_objc_define_method(rb_mKernel, "to_s", rb_any_to_string, 0);
     rb_objc_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);

Modified: MacRuby/trunk/re.c
===================================================================
--- MacRuby/trunk/re.c	2009-08-16 08:14:08 UTC (rev 2337)
+++ MacRuby/trunk/re.c	2009-08-16 19:37:16 UTC (rev 2338)
@@ -1291,6 +1291,12 @@
 rb_reg_prepare_enc(VALUE re, VALUE str, char **pcstr, size_t *pcharsize,
 	bool *should_free)
 {
+    if (*(VALUE *)str == rb_cByteString) {
+	*pcstr = (char *)rb_bytestring_byte_pointer(str);
+	*pcharsize = 1;
+	*should_free = false;
+	return (rb_encoding *)ONIG_ENCODING_ASCII;
+    }
     CFStringEncoding enc = CFStringGetFastestEncoding((CFStringRef)str);
     switch (enc) {
 	case kCFStringEncodingMacRoman:

Modified: MacRuby/trunk/string.c
===================================================================
--- MacRuby/trunk/string.c	2009-08-16 08:14:08 UTC (rev 2337)
+++ MacRuby/trunk/string.c	2009-08-16 19:37:16 UTC (rev 2338)
@@ -34,10 +34,6 @@
 VALUE rb_cSymbol;
 VALUE rb_cByteString;
 
-static ptrdiff_t wrappedDataOffset;
-#define WRAPPED_DATA_IV_NAME "wrappedData"
-#define BYTESTRING_ENCODING_IV_NAME "encoding"
-
 VALUE
 rb_str_freeze(VALUE str)
 {
@@ -320,8 +316,13 @@
     VALUE dup;
 
 #if 1
-    dup = (VALUE)CFStringCreateMutableCopy(NULL, 0, (CFStringRef)str);
-    CFMakeCollectable((CFTypeRef)dup);
+    if (*(VALUE *)str == rb_cByteString) {
+	dup = rb_bytestring_copy(str);
+    }
+    else {
+	dup = (VALUE)CFStringCreateMutableCopy(NULL, 0, (CFStringRef)str);
+	CFMakeCollectable((CFTypeRef)dup);
+    }
 #else
     dup = (VALUE)objc_msgSend((id)str, selMutableCopy);
 #endif
@@ -739,8 +740,7 @@
     return str;
 }
 
-__attribute__((always_inline))
-static void
+static force_inline void
 rb_objc_str_cat(VALUE str, const char *ptr, long len, int cfstring_encoding)
 {
     if (*(VALUE *)str == rb_cByteString) {
@@ -5315,24 +5315,21 @@
 
 #undef INSTALL_METHOD
 
-static inline void **
-rb_bytestring_ivar_addr(VALUE bstr)
-{
-    return (void **)((char *)bstr + wrappedDataOffset);
-}
+typedef struct {
+    struct RBasic basic;
+    CFMutableDataRef data;
+} rb_bstr_t;
 
 CFMutableDataRef 
 rb_bytestring_wrapped_data(VALUE bstr)
 {
-    void **addr = rb_bytestring_ivar_addr(bstr);
-    return (CFMutableDataRef)(*addr); 
+    return ((rb_bstr_t *)bstr)->data;
 }
 
 inline void
 rb_bytestring_set_wrapped_data(VALUE bstr, CFMutableDataRef data)
 {
-    void **addr = rb_bytestring_ivar_addr(bstr);
-    GC_WB(addr, data);
+    GC_WB(&((rb_bstr_t *)bstr)->data, data);
 }
 
 UInt8 *
@@ -5344,7 +5341,11 @@
 static inline VALUE
 bytestring_alloc(void)
 {
-    return (VALUE)class_createInstance((Class)rb_cByteString, sizeof(void *));
+    NEWOBJ(bstr, rb_bstr_t);
+    bstr->basic.flags = 0;
+    bstr->basic.klass = rb_cByteString;
+    bstr->data = NULL;
+    return (VALUE)bstr;
 }
 
 static VALUE
@@ -5362,9 +5363,7 @@
 VALUE 
 rb_bytestring_new() 
 {
-    VALUE bs = rb_bytestring_alloc(0, 0);
-    bs = (VALUE)objc_msgSend((id)bs, selInit); // [recv init];
-    return bs;
+    return rb_bytestring_alloc(0, 0);
 }
 
 VALUE
@@ -5402,8 +5401,6 @@
 
     rb_scan_args(argc, argv, "01", &orig);
 
-    recv = (VALUE)objc_msgSend((id)recv, selInit); // [recv init];
-
     if (!NIL_P(orig)) {
 	StringValue(orig);
 	rb_bytestring_copy_cfstring_content(recv, (CFStringRef)orig);
@@ -5535,17 +5532,23 @@
     CFDataReplaceBytes(data, range, bytes, length);
 }
 
-static void *
-imp_rb_bytestring_mutableCopy(void *rcv, SEL sel)
+VALUE
+rb_bytestring_copy(VALUE bstr)
 {
     VALUE new_bstr = rb_bytestring_new();
-    CFMutableDataRef rcv_data = rb_bytestring_wrapped_data((VALUE)rcv);
+    CFMutableDataRef rcv_data = rb_bytestring_wrapped_data(bstr);
     CFMutableDataRef new_data = rb_bytestring_wrapped_data(new_bstr);
     CFDataAppendBytes(new_data, (const UInt8 *)CFDataGetMutableBytePtr(rcv_data),
 	    CFDataGetLength(rcv_data));
-    return (void *)new_bstr;
+    return new_bstr;
 }
 
+static void *
+imp_rb_bytestring_mutableCopy(void *rcv, SEL sel)
+{
+    return (void *)rb_bytestring_copy((VALUE)rcv);
+}
+
 static void
 imp_rb_bytestring_cfAppendCString_length(void *rcv, SEL sel, const UInt8 *cstr,
 					 long len)
@@ -5743,12 +5746,8 @@
 
     install_symbol_primitives();
 
-    rb_cByteString = (VALUE)objc_allocateClassPair((Class)rb_cNSMutableString,
-	    "ByteString", sizeof(void *));
+    rb_cByteString = rb_define_class("ByteString", rb_cNSMutableString);
     RCLASS_SET_VERSION_FLAG(rb_cByteString, RCLASS_IS_STRING_SUBCLASS);
-    class_addIvar((Class)rb_cByteString, WRAPPED_DATA_IV_NAME, sizeof(id), 
-	    0, "@");
-    objc_registerClassPair((Class)rb_cByteString);
 
     rb_objc_install_method2((Class)rb_cByteString, "isEqual:",
 	    (IMP)imp_rb_bytestring_isEqual);
@@ -5773,7 +5772,4 @@
 	    rb_bytestring_bytesize, 0);
     rb_objc_define_method(rb_cByteString, "getbyte", rb_bytestring_getbyte, 1);
     rb_objc_define_method(rb_cByteString, "setbyte", rb_bytestring_setbyte, 2);
-    wrappedDataOffset = ivar_getOffset(
-	    class_getInstanceVariable((Class)rb_cByteString,
-		WRAPPED_DATA_IV_NAME));
 }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090816/7dfa70f5/attachment-0001.html>


More information about the macruby-changes mailing list