[前][次][番号順一覧][スレッド一覧]

ruby-changes:35637

From: nobu <ko1@a...>
Date: Sat, 27 Sep 2014 10:30:24 +0900 (JST)
Subject: [ruby-changes:35637] nobu:r47719 (trunk): digest.c: typed data

nobu	2014-09-27 10:30:16 +0900 (Sat, 27 Sep 2014)

  New Revision: 47719

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47719

  Log:
    digest.c: typed data
    
    * ext/digest/digest.c (rb_digest_base_alloc): use typed data.
    
    * ext/digest/digest.c (rb_digest_base_copy): fail unless original
      object has same algorithm.

  Modified files:
    trunk/ext/digest/digest.c
    trunk/test/digest/test_digest.rb
Index: ext/digest/digest.c
===================================================================
--- ext/digest/digest.c	(revision 47718)
+++ ext/digest/digest.c	(revision 47719)
@@ -535,6 +535,13 @@ get_digest_base_metadata(VALUE klass) https://github.com/ruby/ruby/blob/trunk/ext/digest/digest.c#L535
     return algo;
 }
 
+static const rb_data_type_t digest_type = {
+    "digest",
+    {0, RUBY_TYPED_DEFAULT_FREE, 0,},
+    NULL, NULL,
+    (RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED),
+};
+
 static inline void
 algo_init(const rb_digest_metadata_t *algo, void *pctx)
 {
@@ -559,7 +566,7 @@ rb_digest_base_alloc(VALUE klass) https://github.com/ruby/ruby/blob/trunk/ext/digest/digest.c#L566
     pctx = xmalloc(algo->ctx_size);
     algo_init(algo, pctx);
 
-    obj = Data_Wrap_Struct(klass, 0, xfree, pctx);
+    obj = TypedData_Wrap_Struct(klass, &digest_type, pctx);
 
     return obj;
 }
@@ -576,9 +583,11 @@ rb_digest_base_copy(VALUE copy, VALUE ob https://github.com/ruby/ruby/blob/trunk/ext/digest/digest.c#L583
     rb_check_frozen(copy);
 
     algo = get_digest_base_metadata(rb_obj_class(copy));
+    if (algo != get_digest_base_metadata(rb_obj_class(obj)))
+	rb_raise(rb_eTypeError, "different algorithms");
 
-    Data_Get_Struct(obj, void, pctx1);
-    Data_Get_Struct(copy, void, pctx2);
+    TypedData_Get_Struct(obj, void, &digest_type, pctx1);
+    TypedData_Get_Struct(copy, void, &digest_type, pctx2);
     memcpy(pctx2, pctx1, algo->ctx_size);
 
     return copy;
@@ -593,7 +602,7 @@ rb_digest_base_reset(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/digest/digest.c#L602
 
     algo = get_digest_base_metadata(rb_obj_class(self));
 
-    Data_Get_Struct(self, void, pctx);
+    TypedData_Get_Struct(self, void, &digest_type, pctx);
 
     algo_init(algo, pctx);
 
@@ -609,7 +618,7 @@ rb_digest_base_update(VALUE self, VALUE https://github.com/ruby/ruby/blob/trunk/ext/digest/digest.c#L618
 
     algo = get_digest_base_metadata(rb_obj_class(self));
 
-    Data_Get_Struct(self, void, pctx);
+    TypedData_Get_Struct(self, void, &digest_type, pctx);
 
     StringValue(str);
     algo->update_func(pctx, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str));
@@ -627,7 +636,7 @@ rb_digest_base_finish(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/digest/digest.c#L636
 
     algo = get_digest_base_metadata(rb_obj_class(self));
 
-    Data_Get_Struct(self, void, pctx);
+    TypedData_Get_Struct(self, void, &digest_type, pctx);
 
     str = rb_str_new(0, algo->digest_len);
     algo->finish_func(pctx, (unsigned char *)RSTRING_PTR(str));
Index: test/digest/test_digest.rb
===================================================================
--- test/digest/test_digest.rb	(revision 47718)
+++ test/digest/test_digest.rb	(revision 47719)
@@ -198,4 +198,16 @@ module TestDigest https://github.com/ruby/ruby/blob/trunk/test/digest/test_digest.rb#L198
       assert_raise(NotImplementedError, bug3810) {Digest::Base.new}
     end
   end
+
+  class TestInitCopy < Test::Unit::TestCase
+    if defined?(Digest::MD5) and defined?(Digest::RMD160)
+      def test_initialize_copy_md5_rmd160
+        assert_separately(%w[-rdigest], <<-'end;')
+          md5 = Digest::MD5.allocate
+          rmd160 = Digest::RMD160.allocate
+          assert_raise(TypeError) {md5.__send__(:initialize_copy, rmd160)}
+        end;
+      end
+    end
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]