1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* proc.c ({proc,method}_data_type): typed.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23990 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2009-07-08 08:13:41 +00:00
parent ca93e00559
commit ebd13e3eed
2 changed files with 64 additions and 44 deletions

View file

@ -1,3 +1,7 @@
Wed Jul 8 17:13:36 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* proc.c ({proc,method}_data_type): typed.
Wed Jul 8 16:47:03 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> Wed Jul 8 16:47:03 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* file.c (rb_file_s_basename): returns new string instead of * file.c (rb_file_s_basename): returns new string instead of

104
proc.c
View file

@ -29,7 +29,7 @@ VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc);
static VALUE bmcall(VALUE, VALUE); static VALUE bmcall(VALUE, VALUE);
static int method_arity(VALUE); static int method_arity(VALUE);
static VALUE rb_obj_is_method(VALUE m); static int rb_obj_is_method(VALUE m);
static rb_iseq_t *get_method_iseq(VALUE method); static rb_iseq_t *get_method_iseq(VALUE method);
/* Proc */ /* Proc */
@ -62,21 +62,30 @@ proc_mark(void *ptr)
RUBY_MARK_LEAVE("proc"); RUBY_MARK_LEAVE("proc");
} }
static size_t
proc_memsize(void *ptr)
{
return ptr ? sizeof(rb_proc_t) : 0;
}
static const rb_data_type_t proc_data_type = {
"proc",
proc_mark,
proc_free,
proc_memsize,
};
VALUE VALUE
rb_proc_alloc(VALUE klass) rb_proc_alloc(VALUE klass)
{ {
VALUE obj;
rb_proc_t *proc; rb_proc_t *proc;
obj = Data_Make_Struct(klass, rb_proc_t, proc_mark, proc_free, proc); return TypedData_Make_Struct(klass, rb_proc_t, &proc_data_type, proc);
MEMZERO(proc, rb_proc_t, 1);
return obj;
} }
VALUE VALUE
rb_obj_is_proc(VALUE proc) rb_obj_is_proc(VALUE proc)
{ {
if (TYPE(proc) == T_DATA && if (rb_typeddata_is_kind_of(proc, &proc_data_type)) {
RDATA(proc)->dfree == (RUBY_DATA_FUNC) proc_free) {
return Qtrue; return Qtrue;
} }
else { else {
@ -724,8 +733,7 @@ proc_eq(VALUE self, VALUE other)
return Qtrue; return Qtrue;
} }
else { else {
if (TYPE(other) == T_DATA && if (rb_obj_is_proc(other)) {
RDATA(other)->dmark == proc_mark) {
rb_proc_t *p1, *p2; rb_proc_t *p1, *p2;
GetProcPtr(self, p1); GetProcPtr(self, p1);
GetProcPtr(other, p2); GetProcPtr(other, p2);
@ -818,22 +826,39 @@ proc_to_proc(VALUE self)
} }
static void static void
bm_mark(struct METHOD *data) bm_mark(void *ptr)
{ {
struct METHOD *data = ptr;
rb_gc_mark(data->rclass); rb_gc_mark(data->rclass);
rb_gc_mark(data->oclass); rb_gc_mark(data->oclass);
rb_gc_mark(data->recv); rb_gc_mark(data->recv);
rb_gc_mark((VALUE)data->body); rb_gc_mark((VALUE)data->body);
} }
static size_t
bm_memsize(void *ptr)
{
return ptr ? sizeof(struct METHOD) : 0;
}
static const rb_data_type_t method_data_type = {
"method",
bm_mark,
RUBY_TYPED_DEFAULT_FREE,
bm_memsize,
};
static inline int
rb_obj_is_method(VALUE m)
{
return rb_typeddata_is_kind_of(m, &method_data_type);
}
NODE * NODE *
rb_method_body(VALUE method) rb_method_body(VALUE method)
{ {
struct METHOD *data; if (rb_obj_is_method(method)) {
struct METHOD *data = DATA_PTR(method);
if (TYPE(method) == T_DATA &&
RDATA(method)->dmark == (RUBY_DATA_FUNC) bm_mark) {
Data_Get_Struct(method, struct METHOD, data);
return data->body; return data->body;
} }
else { else {
@ -874,7 +899,7 @@ mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
} }
if (TYPE(klass) == T_ICLASS) if (TYPE(klass) == T_ICLASS)
klass = RBASIC(klass)->klass; klass = RBASIC(klass)->klass;
method = Data_Make_Struct(mclass, struct METHOD, bm_mark, -1, data); method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
data->oclass = klass; data->oclass = klass;
data->recv = obj; data->recv = obj;
@ -926,14 +951,14 @@ method_eq(VALUE method, VALUE other)
{ {
struct METHOD *m1, *m2; struct METHOD *m1, *m2;
if (TYPE(other) != T_DATA if (!rb_obj_is_method(other))
|| RDATA(other)->dmark != (RUBY_DATA_FUNC) bm_mark)
return Qfalse; return Qfalse;
if (CLASS_OF(method) != CLASS_OF(other)) if (CLASS_OF(method) != CLASS_OF(other))
return Qfalse; return Qfalse;
Data_Get_Struct(method, struct METHOD, m1); Check_TypedStruct(method, &method_data_type);
Data_Get_Struct(other, struct METHOD, m2); m1 = (struct METHOD *)DATA_PTR(method);
m2 = (struct METHOD *)DATA_PTR(other);
if (m1->oclass != m2->oclass || m1->rclass != m2->rclass || if (m1->oclass != m2->oclass || m1->rclass != m2->rclass ||
m1->recv != m2->recv || m1->body != m2->body) m1->recv != m2->recv || m1->body != m2->body)
@ -955,7 +980,7 @@ method_hash(VALUE method)
struct METHOD *m; struct METHOD *m;
long hash; long hash;
Data_Get_Struct(method, struct METHOD, m); TypedData_Get_Struct(method, struct METHOD, &method_data_type, m);
hash = (long)m->oclass; hash = (long)m->oclass;
hash ^= (long)m->rclass; hash ^= (long)m->rclass;
hash ^= (long)m->recv; hash ^= (long)m->recv;
@ -979,9 +1004,9 @@ method_unbind(VALUE obj)
VALUE method; VALUE method;
struct METHOD *orig, *data; struct METHOD *orig, *data;
Data_Get_Struct(obj, struct METHOD, orig); TypedData_Get_Struct(obj, struct METHOD, &method_data_type, orig);
method = method =
Data_Make_Struct(rb_cUnboundMethod, struct METHOD, bm_mark, -1, data); TypedData_Make_Struct(rb_cUnboundMethod, struct METHOD, &method_data_type, data);
data->oclass = orig->oclass; data->oclass = orig->oclass;
data->recv = Qundef; data->recv = Qundef;
data->id = orig->id; data->id = orig->id;
@ -1005,7 +1030,7 @@ method_receiver(VALUE obj)
{ {
struct METHOD *data; struct METHOD *data;
Data_Get_Struct(obj, struct METHOD, data); TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
return data->recv; return data->recv;
} }
@ -1021,7 +1046,7 @@ method_name(VALUE obj)
{ {
struct METHOD *data; struct METHOD *data;
Data_Get_Struct(obj, struct METHOD, data); TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
return ID2SYM(data->id); return ID2SYM(data->id);
} }
@ -1037,7 +1062,7 @@ method_owner(VALUE obj)
{ {
struct METHOD *data; struct METHOD *data;
Data_Get_Struct(obj, struct METHOD, data); TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
return data->oclass; return data->oclass;
} }
@ -1187,7 +1212,7 @@ rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc); rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
} }
if (RDATA(body)->dmark == (RUBY_DATA_FUNC) bm_mark) { if (rb_obj_is_method(body)) {
struct METHOD *method = (struct METHOD *)DATA_PTR(body); struct METHOD *method = (struct METHOD *)DATA_PTR(body);
VALUE rclass = method->rclass; VALUE rclass = method->rclass;
if (rclass != mod) { if (rclass != mod) {
@ -1245,8 +1270,8 @@ method_clone(VALUE self)
VALUE clone; VALUE clone;
struct METHOD *orig, *data; struct METHOD *orig, *data;
Data_Get_Struct(self, struct METHOD, orig); TypedData_Get_Struct(self, struct METHOD, &method_data_type, orig);
clone = Data_Make_Struct(CLASS_OF(self), struct METHOD, bm_mark, -1, data); clone = TypedData_Make_Struct(CLASS_OF(self), struct METHOD, &method_data_type, data);
CLONESETUP(clone, self); CLONESETUP(clone, self);
*data = *orig; *data = *orig;
@ -1274,7 +1299,7 @@ rb_method_call(int argc, VALUE *argv, VALUE method)
int state; int state;
volatile int safe = -1; volatile int safe = -1;
Data_Get_Struct(method, struct METHOD, data); TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
if (data->recv == Qundef) { if (data->recv == Qundef) {
rb_raise(rb_eTypeError, "can't call unbound method; bind first"); rb_raise(rb_eTypeError, "can't call unbound method; bind first");
} }
@ -1398,7 +1423,7 @@ umethod_bind(VALUE method, VALUE recv)
{ {
struct METHOD *data, *bound; struct METHOD *data, *bound;
Data_Get_Struct(method, struct METHOD, data); TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
if (data->rclass != CLASS_OF(recv)) { if (data->rclass != CLASS_OF(recv)) {
if (FL_TEST(data->rclass, FL_SINGLETON)) { if (FL_TEST(data->rclass, FL_SINGLETON)) {
rb_raise(rb_eTypeError, rb_raise(rb_eTypeError,
@ -1410,7 +1435,7 @@ umethod_bind(VALUE method, VALUE recv)
} }
} }
method = Data_Make_Struct(rb_cMethod, struct METHOD, bm_mark, -1, bound); method = TypedData_Make_Struct(rb_cMethod, struct METHOD, &method_data_type, bound);
*bound = *data; *bound = *data;
bound->recv = recv; bound->recv = recv;
bound->rclass = CLASS_OF(recv); bound->rclass = CLASS_OF(recv);
@ -1495,7 +1520,7 @@ method_arity(VALUE method)
{ {
struct METHOD *data; struct METHOD *data;
Data_Get_Struct(method, struct METHOD, data); TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
return rb_node_arity(data->body); return rb_node_arity(data->body);
} }
@ -1519,7 +1544,7 @@ get_method_iseq(VALUE method)
NODE *body; NODE *body;
rb_iseq_t *iseq; rb_iseq_t *iseq;
Data_Get_Struct(method, struct METHOD, data); TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
body = data->body; body = data->body;
switch (nd_type(body)) { switch (nd_type(body)) {
case NODE_BMETHOD: case NODE_BMETHOD:
@ -1582,7 +1607,7 @@ method_inspect(VALUE method)
const char *s; const char *s;
const char *sharp = "#"; const char *sharp = "#";
Data_Get_Struct(method, struct METHOD, data); TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
str = rb_str_buf_new2("#<"); str = rb_str_buf_new2("#<");
s = rb_obj_classname(method); s = rb_obj_classname(method);
rb_str_buf_cat2(str, s); rb_str_buf_cat2(str, s);
@ -1691,15 +1716,6 @@ method_proc(VALUE method)
return procval; return procval;
} }
static VALUE
rb_obj_is_method(VALUE m)
{
if (TYPE(m) == T_DATA && RDATA(m)->dmark == (RUBY_DATA_FUNC) bm_mark) {
return Qtrue;
}
return Qfalse;
}
/* /*
* call_seq: * call_seq:
* local_jump_error.exit_value => obj * local_jump_error.exit_value => obj