Skip to content

Commit a44838e

Browse files
committed
Untag rb_define_alloc_func's specs and define custom allocation function for Object, Array and String
1 parent d265f17 commit a44838e

File tree

6 files changed

+35
-11
lines changed

6 files changed

+35
-11
lines changed

lib/truffle/truffle/cext.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,14 @@ def rb_undef_alloc_func(ruby_class)
15001500
Primitive.object_hidden_var_set(ruby_class.singleton_class, ALLOCATOR_FUNC, nil)
15011501
end
15021502

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+
15031511
def rb_alias(mod, new_name, old_name)
15041512
mod.send(:alias_method, new_name, old_name)
15051513
end

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)