Skip to content

Commit aa8b3f1

Browse files
committed
[GR-14806] Follow up for "[GR-14806] Update specs"
PullRequest: truffleruby/4235
2 parents 401e061 + a44838e commit aa8b3f1

File tree

9 files changed

+54
-12
lines changed

9 files changed

+54
-12
lines changed

lib/cext/include/truffleruby/truffleruby-abi-version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
// $RUBY_VERSION must be the same as TruffleRuby.LANGUAGE_VERSION.
2121
// $ABI_NUMBER starts at 1 and is incremented for every ABI-incompatible change.
2222

23-
#define TRUFFLERUBY_ABI_VERSION "3.2.2.11"
23+
#define TRUFFLERUBY_ABI_VERSION "3.2.2.12"
2424

2525
#endif

lib/truffle/truffle/cext.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ def rb_class_real(ruby_class)
604604
end
605605

606606
def rb_class_get_superclass(ruby_class)
607+
return false unless Primitive.is_a?(ruby_class, Class)
607608
ruby_class.superclass || false
608609
end
609610

@@ -1499,6 +1500,14 @@ def rb_undef_alloc_func(ruby_class)
14991500
Primitive.object_hidden_var_set(ruby_class.singleton_class, ALLOCATOR_FUNC, nil)
15001501
end
15011502

1503+
def rb_tr_set_default_alloc_func(ruby_class, alloc_function)
1504+
Primitive.object_hidden_var_set(ruby_class.singleton_class, ALLOCATOR_FUNC, alloc_function)
1505+
end
1506+
1507+
def rb_tr_default_alloc_func(ruby_class)
1508+
ruby_class.__send__(:__layout_allocate__)
1509+
end
1510+
15021511
def rb_alias(mod, new_name, old_name)
15031512
mod.send(:alias_method, new_name, old_name)
15041513
end

spec/ruby/optional/capi/class_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,16 @@ def obj.some_method() end
487487
@s.rb_class_real(0).should == 0
488488
end
489489
end
490+
491+
describe "rb_class_get_superclass" do
492+
it "returns parent class for a provided class" do
493+
a = Class.new
494+
@s.rb_class_get_superclass(Class.new(a)).should == a
495+
end
496+
497+
it "returns false when there is no parent class" do
498+
@s.rb_class_get_superclass(BasicObject).should == false
499+
@s.rb_class_get_superclass(Module.new).should == false
500+
end
501+
end
490502
end

spec/ruby/optional/capi/ext/class_spec.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ static VALUE class_spec_rb_class_real(VALUE self, VALUE object) {
7979
}
8080
}
8181

82+
static VALUE class_spec_rb_class_get_superclass(VALUE self, VALUE klass) {
83+
return rb_class_get_superclass(klass);
84+
}
85+
8286
static VALUE class_spec_rb_class_superclass(VALUE self, VALUE klass) {
8387
return rb_class_superclass(klass);
8488
}
@@ -160,6 +164,7 @@ void Init_class_spec(void) {
160164
rb_define_method(cls, "rb_class_new_instance_kw", class_spec_rb_class_new_instance_kw, 2);
161165
#endif
162166
rb_define_method(cls, "rb_class_real", class_spec_rb_class_real, 1);
167+
rb_define_method(cls, "rb_class_get_superclass", class_spec_rb_class_get_superclass, 1);
163168
rb_define_method(cls, "rb_class_superclass", class_spec_rb_class_superclass, 1);
164169
rb_define_method(cls, "rb_cvar_defined", class_spec_cvar_defined, 2);
165170
rb_define_method(cls, "rb_cvar_get", class_spec_cvar_get, 2);

spec/ruby/optional/capi/ext/object_spec.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -390,22 +390,22 @@ static VALUE speced_allocator(VALUE klass) {
390390
return instance;
391391
}
392392

393-
static VALUE define_alloc_func(VALUE self, VALUE klass) {
393+
static VALUE object_spec_rb_define_alloc_func(VALUE self, VALUE klass) {
394394
rb_define_alloc_func(klass, speced_allocator);
395395
return Qnil;
396396
}
397397

398-
static VALUE undef_alloc_func(VALUE self, VALUE klass) {
398+
static VALUE object_spec_rb_undef_alloc_func(VALUE self, VALUE klass) {
399399
rb_undef_alloc_func(klass);
400400
return Qnil;
401401
}
402402

403-
static VALUE speced_allocator_p(VALUE self, VALUE klass) {
403+
static VALUE object_spec_speced_allocator_p(VALUE self, VALUE klass) {
404404
rb_alloc_func_t allocator = rb_get_alloc_func(klass);
405405
return (allocator == speced_allocator) ? Qtrue : Qfalse;
406406
}
407407

408-
static VALUE custom_alloc_func_p(VALUE self, VALUE klass) {
408+
static VALUE object_spec_custom_alloc_func_p(VALUE self, VALUE klass) {
409409
rb_alloc_func_t allocator = rb_get_alloc_func(klass);
410410
return allocator ? Qtrue : Qfalse;
411411
}
@@ -485,10 +485,10 @@ void Init_object_spec(void) {
485485
rb_define_method(cls, "rb_ivar_defined", object_spec_rb_ivar_defined, 2);
486486
rb_define_method(cls, "rb_copy_generic_ivar", object_spec_rb_copy_generic_ivar, 2);
487487
rb_define_method(cls, "rb_free_generic_ivar", object_spec_rb_free_generic_ivar, 1);
488-
rb_define_method(cls, "rb_define_alloc_func", define_alloc_func, 1);
489-
rb_define_method(cls, "rb_undef_alloc_func", undef_alloc_func, 1);
490-
rb_define_method(cls, "speced_allocator?", speced_allocator_p, 1);
491-
rb_define_method(cls, "custom_alloc_func?", custom_alloc_func_p, 1);
488+
rb_define_method(cls, "rb_define_alloc_func", object_spec_rb_define_alloc_func, 1);
489+
rb_define_method(cls, "rb_undef_alloc_func", object_spec_rb_undef_alloc_func, 1);
490+
rb_define_method(cls, "speced_allocator?", object_spec_speced_allocator_p, 1);
491+
rb_define_method(cls, "custom_alloc_func?", object_spec_custom_alloc_func_p, 1);
492492
rb_define_method(cls, "not_implemented_method", rb_f_notimplement, -1);
493493
rb_define_method(cls, "rb_ivar_foreach", object_spec_rb_ivar_foreach, 1);
494494
}

spec/tags/optional/capi/object_tags.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/main/c/cext/define.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ rb_alloc_func_t rb_get_alloc_func(VALUE klass) {
109109
return RUBY_CEXT_INVOKE_NO_WRAP("rb_get_alloc_func", klass);
110110
}
111111

112+
void rb_tr_set_default_alloc_func(VALUE ruby_class, rb_alloc_func_t alloc_function) {
113+
polyglot_invoke(RUBY_CEXT, "rb_tr_set_default_alloc_func", rb_tr_unwrap(ruby_class), alloc_function);
114+
}
115+
116+
VALUE rb_tr_default_alloc_func(VALUE klass) {
117+
return RUBY_CEXT_INVOKE("rb_tr_default_alloc_func", klass);
118+
}
119+
112120
VALUE rb_define_class_id(ID id, VALUE super) {
113121
// id is deliberately ignored - see MRI
114122
if (!super) {

src/main/c/cext/ruby.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,13 @@ void rb_tr_init(void *ruby_cext) {
5454
polyglot_invoke(rb_tr_cext, "cext_start_new_handle_block");
5555
rb_tr_init_exception();
5656
rb_tr_init_global_constants(sulong_get_constant);
57+
58+
// In CRuby some core classes have custom allocation function.
59+
// So mimic this CRuby implementation detail to satisfy rb_define_alloc_func's specs
60+
// for classes that are used in these specs only.
61+
rb_tr_set_default_alloc_func(rb_cBasicObject, rb_tr_default_alloc_func);
62+
rb_tr_set_default_alloc_func(rb_cArray, rb_tr_default_alloc_func);
63+
rb_tr_set_default_alloc_func(rb_cString, rb_tr_default_alloc_func);
64+
5765
polyglot_invoke(rb_tr_cext, "cext_start_new_handle_block");
5866
}

src/main/c/cext/truffleruby-impl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ extern ID (*rb_tr_sym2id)(VALUE sym);
4848
extern void* (*rb_tr_force_native)(VALUE obj);
4949
extern bool (*rb_tr_is_native_object)(VALUE value);
5050
extern VALUE (*rb_tr_rb_f_notimplement)(int argc, const VALUE *argv, VALUE obj, VALUE marker);
51+
extern void rb_tr_set_default_alloc_func(VALUE klass, rb_alloc_func_t func);
52+
extern VALUE rb_tr_default_alloc_func(VALUE klass);
53+
5154

5255
// Create a native MutableTruffleString from ptr and len without copying.
5356
// The returned RubyString is only valid as long as ptr is valid (typically only as long as the caller is on the stack),

0 commit comments

Comments
 (0)