Skip to content
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

Implement dup on unbound method #519

Open
wants to merge 1 commit into
base: v3.2.1-pshopify4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions method.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ VALUE rb_method_entry_location(const rb_method_entry_t *me);
void rb_free_method_entry(const rb_method_entry_t *me);

const rb_method_entry_t *rb_method_entry_clone(const rb_method_entry_t *me);
const rb_method_entry_t *rb_method_entry_dup(const rb_method_entry_t *me);
const rb_callable_method_entry_t *rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID called_id, VALUE defined_class);
void rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src);

Expand Down
18 changes: 18 additions & 0 deletions proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2429,6 +2429,23 @@ method_clone(VALUE self)
return clone;
}

static VALUE
method_dup(VALUE self)
{
VALUE dup;
struct METHOD *orig, *data;

TypedData_Get_Struct(self, struct METHOD, &method_data_type, orig);
dup = TypedData_Make_Struct(CLASS_OF(self), struct METHOD, &method_data_type, data);
CLONESETUP(dup, self);
RB_OBJ_WRITE(dup, &data->recv, orig->recv);
RB_OBJ_WRITE(dup, &data->klass, orig->klass);
RB_OBJ_WRITE(dup, &data->iclass, orig->iclass);
RB_OBJ_WRITE(dup, &data->owner, orig->owner);
RB_OBJ_WRITE(dup, &data->me, rb_method_entry_dup(orig->me));
return dup;
}

/* Document-method: Method#===
*
* call-seq:
Expand Down Expand Up @@ -4354,6 +4371,7 @@ Init_Proc(void)
rb_define_method(rb_cUnboundMethod, "eql?", unbound_method_eq, 1);
rb_define_method(rb_cUnboundMethod, "hash", method_hash, 0);
rb_define_method(rb_cUnboundMethod, "clone", method_clone, 0);
rb_define_method(rb_cUnboundMethod, "dup", method_dup, 0);
rb_define_method(rb_cUnboundMethod, "arity", method_arity_m, 0);
rb_define_method(rb_cUnboundMethod, "inspect", method_inspect, 0);
rb_define_method(rb_cUnboundMethod, "to_s", method_inspect, 0);
Expand Down
26 changes: 26 additions & 0 deletions vm_method.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,32 @@ rb_method_entry_clone(const rb_method_entry_t *src_me)
return me;
}

const rb_method_entry_t *
rb_method_entry_dup(const rb_method_entry_t *src_me)
{
// Create an empty method definition.
rb_method_definition_t *def = rb_method_definition_create(src_me->def->type, src_me->def->original_id);

const rb_iseq_t *iseq = src_me->def->body.iseq.iseqptr;

// Duplicate the instruction sequence
VALUE str = rb_iseq_ibf_dump(iseq, Qnil);
const rb_iseq_t *new_iseq = rb_iseq_ibf_load(str);

// Assign the copied iseq to the new method definition
def->body.iseq.iseqptr = new_iseq;

// Create a new method entry
rb_method_entry_t *me = rb_method_entry_alloc(src_me->called_id, src_me->owner, src_me->defined_class, def);

if (METHOD_ENTRY_COMPLEMENTED(src_me)) {
method_definition_addref_complement(def);
}

METHOD_ENTRY_FLAGS_COPY(me, src_me);
return me;
}

MJIT_FUNC_EXPORTED const rb_callable_method_entry_t *
rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID called_id, VALUE defined_class)
{
Expand Down