Skip to content

Commit

Permalink
Added rb_struct_s_members()
Browse files Browse the repository at this point in the history
This function simply returns the members of a Struct, although it
segfaults on MRI when given an instance of the return value of
Struct.new(...).
  • Loading branch information
Yorick Peterse committed May 13, 2015
1 parent a4673b9 commit 2642a30
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions spec/ruby/optional/capi/ext/rubyspec.h
Expand Up @@ -599,6 +599,7 @@
#define HAVE_RB_STRUCT_DEFINE 1
#define HAVE_RB_STRUCT_NEW 1
#define HAVE_RB_STRUCT_GETMEMBER 1
#define HAVE_RB_STRUCT_S_MEMBERS 1

/* Symbol */
#define HAVE_RB_ID2NAME 1
Expand Down
10 changes: 10 additions & 0 deletions spec/ruby/optional/capi/ext/struct_spec.c
Expand Up @@ -55,6 +55,12 @@ static VALUE struct_spec_rb_struct_new(VALUE self, VALUE klass,
}
#endif

#ifdef HAVE_RB_STRUCT_S_MEMBERS
static VALUE struct_spec_rb_struct_s_members(VALUE self, VALUE obj) {
return rb_struct_s_members(obj);
}
#endif

void Init_struct_spec() {
VALUE cls;
cls = rb_define_class("CApiStructSpecs", rb_cObject);
Expand All @@ -78,6 +84,10 @@ void Init_struct_spec() {
#ifdef HAVE_RB_STRUCT_NEW
rb_define_method(cls, "rb_struct_new", struct_spec_rb_struct_new, 4);
#endif

#ifdef HAVE_RB_STRUCT_S_MEMBERS
rb_define_method(cls, "rb_struct_s_members", struct_spec_rb_struct_s_members, 1);
#endif
}

#ifdef __cplusplus
Expand Down
14 changes: 14 additions & 0 deletions spec/ruby/optional/capi/struct_spec.rb
Expand Up @@ -126,4 +126,18 @@
i.c.should == 3
end
end

describe "rb_struct_s_members" do
it "returns the members of a Struct as an Array" do
struct = Struct.new(:foo, :bar)

@s.rb_struct_s_members(struct).should == [:foo, :bar]
end

it "returns the members of a Struct instance as an Array" do
struct = Struct.new(:foo, :bar).new

@s.rb_struct_s_members(struct).should == [:foo, :bar]
end
end
end
4 changes: 4 additions & 0 deletions vm/capi/struct.cpp
Expand Up @@ -61,4 +61,8 @@ extern "C" {
VALUE rb_struct_aset(VALUE struct_handle, VALUE key, VALUE value) {
return rb_funcall(struct_handle, rb_intern("[]="), 2, key, value);
}

VALUE rb_struct_s_members(VALUE obj) {
return rb_funcall(obj, rb_intern("members"), 0);
}
}
2 changes: 2 additions & 0 deletions vm/include/capi/ruby/ruby.h
Expand Up @@ -1849,6 +1849,8 @@ struct RTypedData {
/** Sets the value of the key. */
VALUE rb_struct_aset(VALUE s, VALUE key, VALUE value);

VALUE rb_struct_s_members(VALUE obj);

/** Returns a String in locale encoding. */
VALUE rb_locale_str_new_cstr(const char* string);

Expand Down

0 comments on commit 2642a30

Please sign in to comment.