Skip to content

Commit

Permalink
[GR-14806] Follow up for "[GR-14806] Update specs"
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4235
  • Loading branch information
andrykonchin committed Apr 19, 2024
2 parents 401e061 + a44838e commit aa8b3f1
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/cext/include/truffleruby/truffleruby-abi-version.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
// $RUBY_VERSION must be the same as TruffleRuby.LANGUAGE_VERSION.
// $ABI_NUMBER starts at 1 and is incremented for every ABI-incompatible change.

#define TRUFFLERUBY_ABI_VERSION "3.2.2.11"
#define TRUFFLERUBY_ABI_VERSION "3.2.2.12"

#endif
9 changes: 9 additions & 0 deletions lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ def rb_class_real(ruby_class)
end

def rb_class_get_superclass(ruby_class)
return false unless Primitive.is_a?(ruby_class, Class)
ruby_class.superclass || false
end

Expand Down Expand Up @@ -1499,6 +1500,14 @@ def rb_undef_alloc_func(ruby_class)
Primitive.object_hidden_var_set(ruby_class.singleton_class, ALLOCATOR_FUNC, nil)
end

def rb_tr_set_default_alloc_func(ruby_class, alloc_function)
Primitive.object_hidden_var_set(ruby_class.singleton_class, ALLOCATOR_FUNC, alloc_function)
end

def rb_tr_default_alloc_func(ruby_class)
ruby_class.__send__(:__layout_allocate__)
end

def rb_alias(mod, new_name, old_name)
mod.send(:alias_method, new_name, old_name)
end
Expand Down
12 changes: 12 additions & 0 deletions spec/ruby/optional/capi/class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,16 @@ def obj.some_method() end
@s.rb_class_real(0).should == 0
end
end

describe "rb_class_get_superclass" do
it "returns parent class for a provided class" do
a = Class.new
@s.rb_class_get_superclass(Class.new(a)).should == a
end

it "returns false when there is no parent class" do
@s.rb_class_get_superclass(BasicObject).should == false
@s.rb_class_get_superclass(Module.new).should == false
end
end
end
5 changes: 5 additions & 0 deletions spec/ruby/optional/capi/ext/class_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ static VALUE class_spec_rb_class_real(VALUE self, VALUE object) {
}
}

static VALUE class_spec_rb_class_get_superclass(VALUE self, VALUE klass) {
return rb_class_get_superclass(klass);
}

static VALUE class_spec_rb_class_superclass(VALUE self, VALUE klass) {
return rb_class_superclass(klass);
}
Expand Down Expand Up @@ -160,6 +164,7 @@ void Init_class_spec(void) {
rb_define_method(cls, "rb_class_new_instance_kw", class_spec_rb_class_new_instance_kw, 2);
#endif
rb_define_method(cls, "rb_class_real", class_spec_rb_class_real, 1);
rb_define_method(cls, "rb_class_get_superclass", class_spec_rb_class_get_superclass, 1);
rb_define_method(cls, "rb_class_superclass", class_spec_rb_class_superclass, 1);
rb_define_method(cls, "rb_cvar_defined", class_spec_cvar_defined, 2);
rb_define_method(cls, "rb_cvar_get", class_spec_cvar_get, 2);
Expand Down
16 changes: 8 additions & 8 deletions spec/ruby/optional/capi/ext/object_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,22 +390,22 @@ static VALUE speced_allocator(VALUE klass) {
return instance;
}

static VALUE define_alloc_func(VALUE self, VALUE klass) {
static VALUE object_spec_rb_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) {
static VALUE object_spec_rb_undef_alloc_func(VALUE self, VALUE klass) {
rb_undef_alloc_func(klass);
return Qnil;
}

static VALUE speced_allocator_p(VALUE self, VALUE klass) {
static VALUE object_spec_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) {
static VALUE object_spec_custom_alloc_func_p(VALUE self, VALUE klass) {
rb_alloc_func_t allocator = rb_get_alloc_func(klass);
return allocator ? Qtrue : Qfalse;
}
Expand Down Expand Up @@ -485,10 +485,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);
rb_define_method(cls, "rb_define_alloc_func", object_spec_rb_define_alloc_func, 1);
rb_define_method(cls, "rb_undef_alloc_func", object_spec_rb_undef_alloc_func, 1);
rb_define_method(cls, "speced_allocator?", object_spec_speced_allocator_p, 1);
rb_define_method(cls, "custom_alloc_func?", object_spec_custom_alloc_func_p, 1);
rb_define_method(cls, "not_implemented_method", rb_f_notimplement, -1);
rb_define_method(cls, "rb_ivar_foreach", object_spec_rb_ivar_foreach, 1);
}
Expand Down
3 changes: 0 additions & 3 deletions spec/tags/optional/capi/object_tags.txt

This file was deleted.

8 changes: 8 additions & 0 deletions src/main/c/cext/define.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ rb_alloc_func_t rb_get_alloc_func(VALUE klass) {
return RUBY_CEXT_INVOKE_NO_WRAP("rb_get_alloc_func", klass);
}

void rb_tr_set_default_alloc_func(VALUE ruby_class, rb_alloc_func_t alloc_function) {
polyglot_invoke(RUBY_CEXT, "rb_tr_set_default_alloc_func", rb_tr_unwrap(ruby_class), alloc_function);
}

VALUE rb_tr_default_alloc_func(VALUE klass) {
return RUBY_CEXT_INVOKE("rb_tr_default_alloc_func", klass);
}

VALUE rb_define_class_id(ID id, VALUE super) {
// id is deliberately ignored - see MRI
if (!super) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/c/cext/ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,13 @@ void rb_tr_init(void *ruby_cext) {
polyglot_invoke(rb_tr_cext, "cext_start_new_handle_block");
rb_tr_init_exception();
rb_tr_init_global_constants(sulong_get_constant);

// In CRuby some core classes have custom allocation function.
// So mimic this CRuby implementation detail to satisfy rb_define_alloc_func's specs
// for classes that are used in these specs only.
rb_tr_set_default_alloc_func(rb_cBasicObject, rb_tr_default_alloc_func);
rb_tr_set_default_alloc_func(rb_cArray, rb_tr_default_alloc_func);
rb_tr_set_default_alloc_func(rb_cString, rb_tr_default_alloc_func);

polyglot_invoke(rb_tr_cext, "cext_start_new_handle_block");
}
3 changes: 3 additions & 0 deletions src/main/c/cext/truffleruby-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ extern ID (*rb_tr_sym2id)(VALUE sym);
extern void* (*rb_tr_force_native)(VALUE obj);
extern bool (*rb_tr_is_native_object)(VALUE value);
extern VALUE (*rb_tr_rb_f_notimplement)(int argc, const VALUE *argv, VALUE obj, VALUE marker);
extern void rb_tr_set_default_alloc_func(VALUE klass, rb_alloc_func_t func);
extern VALUE rb_tr_default_alloc_func(VALUE klass);


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

0 comments on commit aa8b3f1

Please sign in to comment.