Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-proc-ext / src / proc.c
1 #include <mruby.h>
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>
7
8 static mrb_value
9 mrb_proc_lambda(mrb_state *mrb, mrb_value self)
10 {
11   struct RProc *p = mrb_proc_ptr(self);
12   return mrb_bool_value(MRB_PROC_STRICT_P(p));
13 }
14
15 static mrb_value
16 mrb_proc_source_location(mrb_state *mrb, mrb_value self)
17 {
18   struct RProc *p = mrb_proc_ptr(self);
19
20   if (MRB_PROC_CFUNC_P(p)) {
21     return mrb_nil_value();
22   }
23   else {
24     mrb_irep *irep = p->body.irep;
25     int32_t line;
26     const char *filename;
27
28     filename = mrb_debug_get_filename(mrb, irep, 0);
29     line = mrb_debug_get_line(mrb, irep, 0);
30
31     return (!filename && line == -1)? mrb_nil_value()
32         : mrb_assoc_new(mrb, mrb_str_new_cstr(mrb, filename), mrb_fixnum_value(line));
33   }
34 }
35
36 static mrb_value
37 mrb_proc_inspect(mrb_state *mrb, mrb_value self)
38 {
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)));
42
43   if (!MRB_PROC_CFUNC_P(p)) {
44     mrb_irep *irep = p->body.irep;
45     const char *filename;
46     int32_t line;
47     mrb_str_cat_lit(mrb, str, "@");
48
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, ":");
52
53     line = mrb_debug_get_line(mrb, irep, 0);
54     if (line != -1) {
55       str = mrb_format(mrb, "%S:%S", str, mrb_fixnum_value(line));
56     }
57     else {
58       mrb_str_cat_lit(mrb, str, "-");
59     }
60   }
61
62   if (MRB_PROC_STRICT_P(p)) {
63     mrb_str_cat_lit(mrb, str, " (lambda)");
64   }
65
66   mrb_str_cat_lit(mrb, str, ">");
67   return str;
68 }
69
70 static mrb_value
71 mrb_kernel_proc(mrb_state *mrb, mrb_value self)
72 {
73   mrb_value blk;
74
75   mrb_get_args(mrb, "&", &blk);
76   if (mrb_nil_p(blk)) {
77     mrb_raise(mrb, E_ARGUMENT_ERROR, "tried to create Proc object without a block");
78   }
79
80   return blk;
81 }
82
83 /*
84  * call-seq:
85  *    prc.parameters  -> array
86  *
87  * Returns the parameter information of this proc.
88  *
89  *    prc = lambda{|x, y=42, *other|}
90  *    prc.parameters  #=> [[:req, :x], [:opt, :y], [:rest, :other]]
91  */
92
93 static mrb_value
94 mrb_proc_parameters(mrb_state *mrb, mrb_value self)
95 {
96   struct parameters_type {
97     size_t len;
98     const char *name;
99     int size;
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},
106     {0, NULL, 0}
107   };
108   const struct RProc *proc = mrb_proc_ptr(self);
109   const struct mrb_irep *irep = proc->body.irep;
110   mrb_aspec aspec;
111   mrb_value parameters;
112   int i, j;
113   int max = -1;
114
115   if (MRB_PROC_CFUNC_P(proc)) {
116     // TODO cfunc aspec is not implemented yet
117     return mrb_ary_new(mrb);
118   }
119   if (!irep) {
120     return mrb_ary_new(mrb);
121   }
122   if (!irep->lv) {
123     return mrb_ary_new(mrb);
124   }
125   if (*irep->iseq != OP_ENTER) {
126     return mrb_ary_new(mrb);
127   }
128
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";
134   }
135
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);
142
143   parameters = mrb_ary_new_capa(mrb, irep->nlocals-1);
144
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));
148
149     for (j = 0; j < p->size; i++, j++) {
150       mrb_value a;
151
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);
157         switch (name[0]) {
158         case '*': case '&':
159           break;
160         default:
161           mrb_ary_push(mrb, a, mrb_symbol_value(sym));
162           break;
163         }
164       }
165       mrb_ary_push(mrb, parameters, a);
166     }
167   }
168   return parameters;
169 }
170
171 void
172 mrb_mruby_proc_ext_gem_init(mrb_state* mrb)
173 {
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());
180
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());
183 }
184
185 void
186 mrb_mruby_proc_ext_gem_final(mrb_state* mrb)
187 {
188 }