summaryrefslogtreecommitdiff
path: root/proc.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-11-28 04:19:37 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-11-28 04:19:37 +0000
commit46e3b0fe7b1d39bd783d1bab539f5624e80d73d6 (patch)
tree35e2e4ad9d9d9965b6642e264779787213490b93 /proc.c
parent4d3f3af83689790f21b344abbfbe6bbdd483813e (diff)
* iseq.c (simple_default_value): extracts simplest default
argument value. * iseq.c (rb_iseq_parameters): returns parameter list. * proc.c (get_proc_iseq, get_method_iseq): handles ifunc and bmethod. * proc.c (rb_proc_parameters, rb_method_parameters): added Proc#parameters and Method#parameters. [ruby-core:19759] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20384 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'proc.c')
-rw-r--r--proc.c71
1 files changed, 69 insertions, 2 deletions
diff --git a/proc.c b/proc.c
index b383d85d8e..4d13f92c6e 100644
--- a/proc.c
+++ b/proc.c
@@ -25,9 +25,12 @@ VALUE rb_cMethod;
VALUE rb_cBinding;
VALUE rb_cProc;
+VALUE rb_iseq_parameters(const rb_iseq_t *iseq);
+
static VALUE bmcall(VALUE, VALUE);
static int method_arity(VALUE);
static VALUE rb_obj_is_method(VALUE m);
+static rb_iseq_t *get_method_iseq(VALUE method);
/* Proc */
@@ -615,8 +618,14 @@ get_proc_iseq(VALUE self)
GetProcPtr(self, proc);
iseq = proc->block.iseq;
- if (!RUBY_VM_NORMAL_ISEQ_P(iseq))
- return 0;
+ if (!RUBY_VM_NORMAL_ISEQ_P(iseq)) {
+ NODE *node = (NODE *)iseq;
+ iseq = 0;
+ if (nd_type(node) == NODE_IFUNC && node->nd_cfnc == bmcall) {
+ /* method(:foo).to_proc */
+ iseq = get_method_iseq(node->nd_tval);
+ }
+ }
return iseq;
}
@@ -650,6 +659,42 @@ rb_proc_location(VALUE self)
return iseq_location(get_proc_iseq(self));
}
+static VALUE
+unnamed_parameters(int arity)
+{
+ VALUE a, param = rb_ary_new2((arity < 0) ? -arity : arity);
+ int n = (arity < 0) ? ~arity : arity;
+ ID req, rest;
+ CONST_ID(req, "req");
+ a = rb_ary_new3(1, ID2SYM(req));
+ OBJ_FREEZE(a);
+ for (; n; --n) {
+ rb_ary_push(param, a);
+ }
+ if (arity < 0) {
+ CONST_ID(rest, "rest");
+ rb_ary_store(param, ~arity, rb_ary_new3(1, ID2SYM(rest)));
+ }
+ return param;
+}
+
+/*
+ * call-seq:
+ * proc.parameters => array
+ *
+ * returns the parameter information of this proc
+ */
+
+static VALUE
+rb_proc_parameters(VALUE self)
+{
+ rb_iseq_t *iseq = get_proc_iseq(self);
+ if (!iseq) {
+ return unnamed_parameters(proc_arity(self));
+ }
+ return rb_iseq_parameters(iseq);
+}
+
/*
* call-seq:
* prc == other_proc => true or false
@@ -1463,6 +1508,8 @@ get_method_iseq(VALUE method)
Data_Get_Struct(method, struct METHOD, data);
body = data->body;
switch (nd_type(body)) {
+ case NODE_BMETHOD:
+ return get_proc_iseq(body->nd_cval);
case RUBY_VM_METHOD_NODE:
GetISeqPtr((VALUE)body->nd_body, iseq);
if (RUBY_VM_NORMAL_ISEQ_P(iseq)) break;
@@ -1487,6 +1534,23 @@ rb_method_location(VALUE method)
}
/*
+ * call-seq:
+ * meth.parameters => array
+ *
+ * returns the parameter information of this method
+ */
+
+static VALUE
+rb_method_parameters(VALUE method)
+{
+ rb_iseq_t *iseq = get_method_iseq(method);
+ if (!iseq) {
+ return unnamed_parameters(method_arity(method));
+ }
+ return rb_iseq_parameters(iseq);
+}
+
+/*
* call-seq:
* meth.to_s => string
* meth.inspect => string
@@ -1814,6 +1878,7 @@ Init_Proc(void)
rb_define_method(rb_cProc, "binding", proc_binding, 0);
rb_define_method(rb_cProc, "curry", proc_curry, -1);
rb_define_method(rb_cProc, "source_location", rb_proc_location, 0);
+ rb_define_method(rb_cProc, "parameters", rb_proc_parameters, 0);
/* Exceptions */
rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError);
@@ -1849,6 +1914,7 @@ Init_Proc(void)
rb_define_method(rb_cMethod, "owner", method_owner, 0);
rb_define_method(rb_cMethod, "unbind", method_unbind, 0);
rb_define_method(rb_cMethod, "source_location", rb_method_location, 0);
+ rb_define_method(rb_cMethod, "parameters", rb_method_parameters, 0);
rb_define_method(rb_mKernel, "method", rb_obj_method, 1);
rb_define_method(rb_mKernel, "public_method", rb_obj_public_method, 1);
@@ -1867,6 +1933,7 @@ Init_Proc(void)
rb_define_method(rb_cUnboundMethod, "owner", method_owner, 0);
rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1);
rb_define_method(rb_cUnboundMethod, "source_location", rb_method_location, 0);
+ rb_define_method(rb_cUnboundMethod, "parameters", rb_method_parameters, 0);
/* Module#*_method */
rb_define_method(rb_cModule, "instance_method", rb_mod_instance_method, 1);