2 #include <mruby/proc.h>
3 #include <mruby/opcode.h>
4 #include <mruby/array.h>
5 #include <mruby/string.h>
6 #include <mruby/debug.h>
9 mrb_proc_lambda(mrb_state *mrb, mrb_value self)
11 struct RProc *p = mrb_proc_ptr(self);
12 return mrb_bool_value(MRB_PROC_STRICT_P(p));
16 mrb_proc_source_location(mrb_state *mrb, mrb_value self)
18 struct RProc *p = mrb_proc_ptr(self);
20 if (MRB_PROC_CFUNC_P(p)) {
21 return mrb_nil_value();
24 mrb_irep *irep = p->body.irep;
28 filename = mrb_debug_get_filename(mrb, irep, 0);
29 line = mrb_debug_get_line(mrb, irep, 0);
31 return (!filename && line == -1)? mrb_nil_value()
32 : mrb_assoc_new(mrb, mrb_str_new_cstr(mrb, filename), mrb_fixnum_value(line));
37 mrb_proc_inspect(mrb_state *mrb, mrb_value self)
39 struct RProc *p = mrb_proc_ptr(self);
40 mrb_value str = mrb_str_new_lit(mrb, "#<Proc:");
41 mrb_str_concat(mrb, str, mrb_ptr_to_str(mrb, mrb_cptr(self)));
43 if (!MRB_PROC_CFUNC_P(p)) {
44 mrb_irep *irep = p->body.irep;
47 mrb_str_cat_lit(mrb, str, "@");
49 filename = mrb_debug_get_filename(mrb, irep, 0);
50 mrb_str_cat_cstr(mrb, str, filename ? filename : "-");
51 mrb_str_cat_lit(mrb, str, ":");
53 line = mrb_debug_get_line(mrb, irep, 0);
55 str = mrb_format(mrb, "%S:%S", str, mrb_fixnum_value(line));
58 mrb_str_cat_lit(mrb, str, "-");
62 if (MRB_PROC_STRICT_P(p)) {
63 mrb_str_cat_lit(mrb, str, " (lambda)");
66 mrb_str_cat_lit(mrb, str, ">");
71 mrb_kernel_proc(mrb_state *mrb, mrb_value self)
75 mrb_get_args(mrb, "&", &blk);
77 mrb_raise(mrb, E_ARGUMENT_ERROR, "tried to create Proc object without a block");
85 * prc.parameters -> array
87 * Returns the parameter information of this proc.
89 * prc = lambda{|x, y=42, *other|}
90 * prc.parameters #=> [[:req, :x], [:opt, :y], [:rest, :other]]
94 mrb_proc_parameters(mrb_state *mrb, mrb_value self)
96 struct parameters_type {
100 } *p, parameters_list [] = {
101 {sizeof("req") - 1, "req", 0},
102 {sizeof("opt") - 1, "opt", 0},
103 {sizeof("rest") - 1, "rest", 0},
104 {sizeof("req") - 1, "req", 0},
105 {sizeof("block") - 1, "block", 0},
108 const struct RProc *proc = mrb_proc_ptr(self);
109 const struct mrb_irep *irep = proc->body.irep;
111 mrb_value parameters;
115 if (MRB_PROC_CFUNC_P(proc)) {
116 // TODO cfunc aspec is not implemented yet
117 return mrb_ary_new(mrb);
120 return mrb_ary_new(mrb);
123 return mrb_ary_new(mrb);
125 if (*irep->iseq != OP_ENTER) {
126 return mrb_ary_new(mrb);
129 if (!MRB_PROC_STRICT_P(proc)) {
130 parameters_list[0].len = sizeof("opt") - 1;
131 parameters_list[0].name = "opt";
132 parameters_list[3].len = sizeof("opt") - 1;
133 parameters_list[3].name = "opt";
136 aspec = PEEK_W(irep->iseq+1);
137 parameters_list[0].size = MRB_ASPEC_REQ(aspec);
138 parameters_list[1].size = MRB_ASPEC_OPT(aspec);
139 parameters_list[2].size = MRB_ASPEC_REST(aspec);
140 parameters_list[3].size = MRB_ASPEC_POST(aspec);
141 parameters_list[4].size = MRB_ASPEC_BLOCK(aspec);
143 parameters = mrb_ary_new_capa(mrb, irep->nlocals-1);
145 max = irep->nlocals-1;
146 for (i = 0, p = parameters_list; p->name; p++) {
147 mrb_value sname = mrb_symbol_value(mrb_intern_static(mrb, p->name, p->len));
149 for (j = 0; j < p->size; i++, j++) {
152 a = mrb_ary_new(mrb);
153 mrb_ary_push(mrb, a, sname);
154 if (i < max && irep->lv[i].name) {
155 mrb_sym sym = irep->lv[i].name;
156 const char *name = mrb_sym2name(mrb, sym);
161 mrb_ary_push(mrb, a, mrb_symbol_value(sym));
165 mrb_ary_push(mrb, parameters, a);
172 mrb_mruby_proc_ext_gem_init(mrb_state* mrb)
174 struct RClass *p = mrb->proc_class;
175 mrb_define_method(mrb, p, "lambda?", mrb_proc_lambda, MRB_ARGS_NONE());
176 mrb_define_method(mrb, p, "source_location", mrb_proc_source_location, MRB_ARGS_NONE());
177 mrb_define_method(mrb, p, "to_s", mrb_proc_inspect, MRB_ARGS_NONE());
178 mrb_define_method(mrb, p, "inspect", mrb_proc_inspect, MRB_ARGS_NONE());
179 mrb_define_method(mrb, p, "parameters", mrb_proc_parameters, MRB_ARGS_NONE());
181 mrb_define_class_method(mrb, mrb->kernel_module, "proc", mrb_kernel_proc, MRB_ARGS_NONE());
182 mrb_define_method(mrb, mrb->kernel_module, "proc", mrb_kernel_proc, MRB_ARGS_NONE());
186 mrb_mruby_proc_ext_gem_final(mrb_state* mrb)