[macruby-changes] [5169] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Sat Jan 15 00:46:23 PST 2011


Revision: 5169
          http://trac.macosforge.org/projects/ruby/changeset/5169
Author:   watson1978 at gmail.com
Date:     2011-01-15 00:46:14 -0800 (Sat, 15 Jan 2011)
Log Message:
-----------
Array#pack with format "fFeEdDgG" will raise a TypeError when does not convert into float.

Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

assert_raise(TypeError){ ["a"].pack("f") }
assert_raise(TypeError){ ["a"].pack("F") }
assert_raise(TypeError){ ["a"].pack("e") }
assert_raise(TypeError){ ["a"].pack("E") }
assert_raise(TypeError){ ["a"].pack("d") }
assert_raise(TypeError){ ["a"].pack("D") }
assert_raise(TypeError){ ["a"].pack("g") }
assert_raise(TypeError){ ["a"].pack("G") }

class String
  def utf8
    self.force_encoding('UTF-8')
  end
end
assert_equal("\x00\x00\x80?".utf8, [1.0].pack("f").utf8)
assert_equal("\x00\x00\x80?".utf8, [1.0].pack("e").utf8)
assert_equal("\x00\x00\x00\x00\x00\x00\xF0?".utf8, [1.0].pack("d").utf8)
assert_equal("?\x80\x00\x00".utf8, [1.0].pack("g").utf8)

puts :ok
}}}

Modified Paths:
--------------
    MacRuby/trunk/include/ruby/intern.h
    MacRuby/trunk/object.c
    MacRuby/trunk/pack.c

Modified: MacRuby/trunk/include/ruby/intern.h
===================================================================
--- MacRuby/trunk/include/ruby/intern.h	2011-01-15 04:35:28 UTC (rev 5168)
+++ MacRuby/trunk/include/ruby/intern.h	2011-01-15 08:46:14 UTC (rev 5169)
@@ -365,6 +365,7 @@
 VALUE rb_check_to_integer(VALUE, const char *);
 VALUE rb_to_int(VALUE);
 VALUE rb_Integer(VALUE);
+VALUE rb_to_float(VALUE);
 VALUE rb_Float(VALUE);
 VALUE rb_String(VALUE);
 VALUE rb_Array(VALUE);

Modified: MacRuby/trunk/object.c
===================================================================
--- MacRuby/trunk/object.c	2011-01-15 04:35:28 UTC (rev 5168)
+++ MacRuby/trunk/object.c	2011-01-15 08:46:14 UTC (rev 5169)
@@ -2755,6 +2755,20 @@
 }
 
 VALUE
+rb_to_float(VALUE val)
+{
+    if (TYPE(val) ==  T_FLOAT) return val;
+    if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
+	rb_raise(rb_eTypeError, "can't convert %s into Float",
+		 NIL_P(val) ? "nil" :
+		 val ==  Qtrue ? "true" :
+		 val ==  Qfalse ? "false" :
+		 rb_obj_classname(val));
+    }
+    return rb_convert_type(val, T_FLOAT, "Float", "to_f");
+}
+
+VALUE
 rb_check_to_float(VALUE val)
 {
     if (TYPE(val) == T_FLOAT) {

Modified: MacRuby/trunk/pack.c
===================================================================
--- MacRuby/trunk/pack.c	2011-01-15 04:35:28 UTC (rev 5168)
+++ MacRuby/trunk/pack.c	2011-01-15 08:46:14 UTC (rev 5169)
@@ -792,7 +792,7 @@
 		float f;
 
 		from = NEXTFROM;
-		f = RFLOAT_VALUE(rb_Float(from));
+		f = RFLOAT_VALUE(rb_to_float(from));
 		rb_bstr_concat(data, (const UInt8 *)&f, sizeof(float));
 	    }
 	    break;
@@ -803,7 +803,7 @@
 		FLOAT_CONVWITH(ftmp);
 
 		from = NEXTFROM;
-		f = RFLOAT_VALUE(rb_Float(from));
+		f = RFLOAT_VALUE(rb_to_float(from));
 		f = HTOVF(f,ftmp);
 		rb_bstr_concat(data, (const UInt8 *)&f, sizeof(float));
 	    }
@@ -815,7 +815,7 @@
 		DOUBLE_CONVWITH(dtmp);
 
 		from = NEXTFROM;
-		d = RFLOAT_VALUE(rb_Float(from));
+		d = RFLOAT_VALUE(rb_to_float(from));
 		d = HTOVD(d,dtmp);
 		rb_bstr_concat(data, (const UInt8 *)&d, sizeof(double));
 	    }
@@ -827,7 +827,7 @@
 		double d;
 
 		from = NEXTFROM;
-		d = RFLOAT_VALUE(rb_Float(from));
+		d = RFLOAT_VALUE(rb_to_float(from));
 		rb_bstr_concat(data, (const UInt8 *)&d, sizeof(double));
 	    }
 	    break;
@@ -838,7 +838,7 @@
 		FLOAT_CONVWITH(ftmp);
 
 		from = NEXTFROM;
-		f = RFLOAT_VALUE(rb_Float(from));
+		f = RFLOAT_VALUE(rb_to_float(from));
 		f = HTONF(f,ftmp);
 		rb_bstr_concat(data, (const UInt8 *)&f, sizeof(float));
 	    }
@@ -850,7 +850,7 @@
 		DOUBLE_CONVWITH(dtmp);
 
 		from = NEXTFROM;
-		d = RFLOAT_VALUE(rb_Float(from));
+		d = RFLOAT_VALUE(rb_to_float(from));
 		d = HTOND(d,dtmp);
 		rb_bstr_concat(data, (const UInt8 *)&d, sizeof(double));
 	    }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20110115/5fdc58bf/attachment.html>


More information about the macruby-changes mailing list