Skip to content

Instantly share code, notes, and snippets.

@dmacvicar
Created August 28, 2010 00:39
Show Gist options
  • Save dmacvicar/554450 to your computer and use it in GitHub Desktop.
Save dmacvicar/554450 to your computer and use it in GitHub Desktop.
From a76eba0bf3be09ac92ff949e695ed95c915bd33a Mon Sep 17 00:00:00 2001
From: Duncan Mac-Vicar P <dmacvicar@suse.de>
Date: Mon, 23 Aug 2010 23:49:46 +0200
Subject: [PATCH 1/3] implement rb_hash in the C API
---
vm/capi/hash.cpp | 4 ++++
1 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/vm/capi/hash.cpp b/vm/capi/hash.cpp
index f599720..248a494 100644
--- a/vm/capi/hash.cpp
+++ b/vm/capi/hash.cpp
@@ -6,6 +6,10 @@ using namespace rubinius::capi;
extern "C" {
+ VALUE rb_hash(VALUE obj) {
+ return rb_funcall(obj, rb_intern("hash"), 0);
+ }
+
VALUE rb_hash_new() {
return rb_funcall(rb_cHash, rb_intern("new"), 0);
}
--
1.7.1
From 180559cfe5f8b19d845fac54663b2abcef77ebcd Mon Sep 17 00:00:00 2001
From: Duncan Mac-Vicar P <dmacvicar@suse.de>
Date: Mon, 23 Aug 2010 23:49:50 +0200
Subject: [PATCH 2/3] spec for rb_hash implementation in the C API
---
spec/ruby/optional/capi/ext/hash_spec.c | 10 ++++++++++
spec/ruby/optional/capi/ext/rubyspec.h | 1 +
spec/ruby/optional/capi/hash_spec.rb | 7 +++++++
3 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/spec/ruby/optional/capi/ext/hash_spec.c b/spec/ruby/optional/capi/ext/hash_spec.c
index f0b0238..ee536f5 100644
--- a/spec/ruby/optional/capi/ext/hash_spec.c
+++ b/spec/ruby/optional/capi/ext/hash_spec.c
@@ -5,6 +5,12 @@
extern "C" {
#endif
+#ifdef HAVE_RB_HASH
+VALUE hash_spec_rb_hash(VALUE self, VALUE hash) {
+ return rb_hash(hash);
+}
+#endif
+
#ifdef HAVE_RB_HASH_AREF
VALUE hash_spec_rb_hash_aref(VALUE self, VALUE hash, VALUE key) {
return rb_hash_aref(hash, key);
@@ -69,6 +75,10 @@ void Init_hash_spec() {
VALUE cls;
cls = rb_define_class("CApiHashSpecs", rb_cObject);
+#ifdef HAVE_RB_HASH
+ rb_define_method(cls, "rb_hash", hash_spec_rb_hash, 1);
+#endif
+
#ifdef HAVE_RB_HASH_AREF
rb_define_method(cls, "rb_hash_aref", hash_spec_rb_hash_aref, 2);
rb_define_method(cls, "rb_hash_aref_nil", hash_spec_rb_hash_aref_nil, 2);
diff --git a/spec/ruby/optional/capi/ext/rubyspec.h b/spec/ruby/optional/capi/ext/rubyspec.h
index 65a31f1..ae3c985 100644
--- a/spec/ruby/optional/capi/ext/rubyspec.h
+++ b/spec/ruby/optional/capi/ext/rubyspec.h
@@ -131,6 +131,7 @@
#define HAVE_RB_SET_KCODE 1
// Hash
+#define HAVE_RB_HASH 1
#define HAVE_RB_HASH_AREF 1
#define HAVE_RB_HASH_ASET 1
#define HAVE_RB_HASH_DELETE 1
diff --git a/spec/ruby/optional/capi/hash_spec.rb b/spec/ruby/optional/capi/hash_spec.rb
index 5a6fe58..5c042f3 100644
--- a/spec/ruby/optional/capi/hash_spec.rb
+++ b/spec/ruby/optional/capi/hash_spec.rb
@@ -7,6 +7,13 @@ describe "C-API Hash function" do
@s = CApiHashSpecs.new
end
+ describe "rb_hash" do
+ it "returns the hash is of an object" do
+ obj = Object.new
+ @s.rb_hash(obj).should == obj.hash
+ end
+ end
+
describe "rb_hash_new" do
it "returns a new hash" do
@s.rb_hash_new.should == {}
--
1.7.1
From 35709c2c3989822e3d74b749a7341b7e972cb87b Mon Sep 17 00:00:00 2001
From: Duncan Mac-Vicar P <dmacvicar@suse.de>
Date: Tue, 24 Aug 2010 00:18:40 +0200
Subject: [PATCH 3/3] expose rb_hash in ruby C api
---
vm/capi/include/ruby.h | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/vm/capi/include/ruby.h b/vm/capi/include/ruby.h
index 93f5114..2b29102 100644
--- a/vm/capi/include/ruby.h
+++ b/vm/capi/include/ruby.h
@@ -1109,6 +1109,9 @@ VALUE rb_uint2big(unsigned long number);
*/
#define rb_funcall3 rb_funcall2
+ /** Return the hash id of the object **/
+ VALUE rb_hash(VALUE self);
+
/** Create a new Hash object */
VALUE rb_hash_new();
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment