Skip to content

Commit

Permalink
Pass block environments to pl_imp_implementationWithBlock() directly …
Browse files Browse the repository at this point in the history
…instead of copying to heap
  • Loading branch information
macrotis committed May 26, 2012
1 parent 3043e29 commit e80a075
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,20 @@ make_struct(VALUE name, VALUE members, VALUE klass)
ID id = SYM2ID(RARRAY_AT(members, i));
if (rb_is_local_id(id) || rb_is_const_id(id)) {
long j = i; /* Needed for block data reference. */
VALUE
(^struct_ref)(VALUE)
= ^(VALUE obj)
{
return RSTRUCT_PTR(obj)[j];
};
VALUE
(^struct_set)(VALUE, VALUE)
= ^(VALUE obj, VALUE val)
{
VALUE *ptr = RSTRUCT_PTR(obj);
rb_struct_modify(obj);
GC_WB(&ptr[j], val);
return val;
};
rb_objc_define_method(nstr, rb_id2name(id),
pl_imp_implementationWithBlock(Block_copy(struct_ref)), 0);
rb_objc_define_method(nstr, rb_id2name(rb_id_attrset(id)),
pl_imp_implementationWithBlock(Block_copy(struct_set)), 1);
}
/* Struct attribute reader */
rb_objc_define_method(nstr, rb_id2name(id),
pl_imp_implementationWithBlock(^(VALUE obj) {
return RSTRUCT_PTR(obj)[j];
}), 0);
/* Struct attribute writer */
rb_objc_define_method(nstr, rb_id2name(rb_id_attrset(id)),
pl_imp_implementationWithBlock(^(VALUE obj, VALUE val) {
VALUE *ptr = RSTRUCT_PTR(obj);
rb_struct_modify(obj);
GC_WB(&ptr[i], val);
return val;
}), 1);
}
}

return nstr;
Expand Down

0 comments on commit e80a075

Please sign in to comment.