summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/capi/ext/object_spec.c
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2020-01-28 20:47:48 +0100
committerBenoit Daloze <eregontp@gmail.com>2020-01-28 20:47:48 +0100
commit809f0b8a1357f14f9645210d4812f4400c8d397e (patch)
tree7ce6192f94b1dc4b004798aa5d0c4d6bac02577f /spec/ruby/optional/capi/ext/object_spec.c
parented377cc9aaf1ccbede19ddc6c464f5fbf3cabc34 (diff)
Update to ruby/spec@f8a2d54
Diffstat (limited to 'spec/ruby/optional/capi/ext/object_spec.c')
-rw-r--r--spec/ruby/optional/capi/ext/object_spec.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/spec/ruby/optional/capi/ext/object_spec.c b/spec/ruby/optional/capi/ext/object_spec.c
index cd32050f14..2151e37e5c 100644
--- a/spec/ruby/optional/capi/ext/object_spec.c
+++ b/spec/ruby/optional/capi/ext/object_spec.c
@@ -345,6 +345,40 @@ static VALUE object_spec_rb_class_inherited_p(VALUE self, VALUE mod, VALUE arg)
return rb_class_inherited_p(mod, arg);
}
+static VALUE speced_allocator(VALUE klass) {
+ VALUE flags = 0;
+ VALUE instance;
+ if (rb_class_inherited_p(klass, rb_cString)) {
+ flags = T_STRING;
+ } else if (rb_class_inherited_p(klass, rb_cArray)) {
+ flags = T_ARRAY;
+ } else {
+ flags = T_OBJECT;
+ }
+ instance = rb_newobj_of(klass, flags);
+ rb_iv_set(instance, "@from_custom_allocator", Qtrue);
+ return instance;
+}
+
+static VALUE define_alloc_func(VALUE self, VALUE klass) {
+ rb_define_alloc_func(klass, speced_allocator);
+ return Qnil;
+}
+
+static VALUE undef_alloc_func(VALUE self, VALUE klass) {
+ rb_undef_alloc_func(klass);
+ return Qnil;
+}
+
+static VALUE speced_allocator_p(VALUE self, VALUE klass) {
+ rb_alloc_func_t allocator = rb_get_alloc_func(klass);
+ return (allocator == speced_allocator) ? Qtrue : Qfalse;
+}
+
+static VALUE custom_alloc_func_p(VALUE self, VALUE klass) {
+ rb_alloc_func_t allocator = rb_get_alloc_func(klass);
+ return allocator ? Qtrue : Qfalse;
+}
void Init_object_spec(void) {
VALUE cls = rb_define_class("CApiObjectSpecs", rb_cObject);
@@ -412,6 +446,10 @@ void Init_object_spec(void) {
rb_define_method(cls, "rb_ivar_defined", object_spec_rb_ivar_defined, 2);
rb_define_method(cls, "rb_copy_generic_ivar", object_spec_rb_copy_generic_ivar, 2);
rb_define_method(cls, "rb_free_generic_ivar", object_spec_rb_free_generic_ivar, 1);
+ rb_define_method(cls, "rb_define_alloc_func", define_alloc_func, 1);
+ rb_define_method(cls, "rb_undef_alloc_func", undef_alloc_func, 1);
+ rb_define_method(cls, "speced_allocator?", speced_allocator_p, 1);
+ rb_define_method(cls, "custom_alloc_func?", custom_alloc_func_p, 1);
}
#ifdef __cplusplus