-
Notifications
You must be signed in to change notification settings - Fork 194
Implement rb_get_alloc_func and spec related functions #1874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ module Truffle::CExt | |
DATA_HOLDER = Object.new | ||
DATA_MEMSIZER = Object.new | ||
RB_TYPE = Object.new | ||
ALLOCATOR_FUNC = Object.new | ||
|
||
extend self | ||
|
||
|
@@ -1278,17 +1279,40 @@ def rb_eval_string(str) | |
eval(str) | ||
end | ||
|
||
BASIC_ALLOC = ::BasicObject.singleton_class.instance_method(:__allocate__) | ||
|
||
def rb_newobj_of(ruby_class) | ||
# we need to bypass __allocate__ on ruby_class | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to bypass? Could you add a spec failing without that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah right, because And I guess Writing some thoughts here, not sure what's best yet. What we need would be to ignore the Maybe a way to achieve that is to check for the Alternatively, we could introduce a separate method for the Maybe the simplest and clearest is simply defining core In MRI, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated the comment, I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll give a shot at that. |
||
BASIC_ALLOC.bind(ruby_class).call | ||
end | ||
|
||
def rb_define_alloc_func(ruby_class, function) | ||
ruby_class.singleton_class.define_method(:__allocate__) do | ||
TrufflePrimitive.cext_unwrap(TrufflePrimitive.call_with_c_mutex(function, [TrufflePrimitive.cext_wrap(self)])) | ||
end | ||
class << ruby_class | ||
private :__allocate__ | ||
end | ||
hidden_variable_set(ruby_class.singleton_class, ALLOCATOR_FUNC, function) | ||
end | ||
|
||
def rb_get_alloc_func(ruby_class) | ||
return nil unless Class === ruby_class | ||
begin | ||
allocate_method = ruby_class.method(:__allocate__).owner | ||
rescue NameError | ||
nil | ||
else | ||
hidden_variable_get(allocate_method, ALLOCATOR_FUNC) | ||
end | ||
end | ||
|
||
def rb_undef_alloc_func(ruby_class) | ||
ruby_class.singleton_class.send(:undef_method, :__allocate__) | ||
rescue NameError | ||
# it's fine to call this on a class that doesn't have an allocator | ||
else | ||
hidden_variable_set(ruby_class.singleton_class, ALLOCATOR_FUNC, nil) | ||
end | ||
|
||
def rb_alias(mod, new_name, old_name) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -345,6 +345,31 @@ 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 instance = rb_newobj_of(klass, 0); | ||
rb_funcall(instance, rb_intern("instance_variable_set"), 2, ID2SYM(rb_intern("@from_custom_allocator")), Qtrue); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small thing, there is |
||
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 allocator_nil_p(VALUE self, VALUE klass) { | ||
rb_alloc_func_t allocator = rb_get_alloc_func(klass); | ||
return allocator ? Qfalse : Qtrue; | ||
} | ||
|
||
void Init_object_spec(void) { | ||
VALUE cls = rb_define_class("CApiObjectSpecs", rb_cObject); | ||
|
@@ -412,6 +437,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, "allocator_nil?", allocator_nil_p, 1); | ||
} | ||
|
||
#ifdef __cplusplus | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already
BASIC_OBJECT_ALLOCATE
a bit below