1 /*
2 ** proc.c - Proc class
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #include <mruby.h>
8 #include <mruby/class.h>
9 #include <mruby/proc.h>
10 #include <mruby/opcode.h>
11 #include <mruby/data.h>
12
13 static const mrb_code call_iseq[] = {
14 OP_CALL,
15 };
16
17 struct RProc*
mrb_proc_new(mrb_state * mrb,mrb_irep * irep)18 mrb_proc_new(mrb_state *mrb, mrb_irep *irep)
19 {
20 struct RProc *p;
21 mrb_callinfo *ci = mrb->c->ci;
22
23 p = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb->proc_class);
24 if (ci) {
25 struct RClass *tc = NULL;
26
27 if (ci->proc) {
28 tc = MRB_PROC_TARGET_CLASS(ci->proc);
29 }
30 if (tc == NULL) {
31 tc = ci->target_class;
32 }
33 p->upper = ci->proc;
34 p->e.target_class = tc;
35 }
36 p->body.irep = irep;
37 mrb_irep_incref(mrb, irep);
38
39 return p;
40 }
41
42 static struct REnv*
env_new(mrb_state * mrb,mrb_int nlocals)43 env_new(mrb_state *mrb, mrb_int nlocals)
44 {
45 struct REnv *e;
46 mrb_callinfo *ci = mrb->c->ci;
47 int bidx;
48
49 e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, NULL);
50 MRB_ENV_SET_LEN(e, nlocals);
51 bidx = ci->argc;
52 if (ci->argc < 0) bidx = 2;
53 else bidx += 1;
54 MRB_ENV_SET_BIDX(e, bidx);
55 e->mid = ci->mid;
56 e->stack = mrb->c->stack;
57 e->cxt = mrb->c;
58
59 return e;
60 }
61
62 static void
closure_setup(mrb_state * mrb,struct RProc * p)63 closure_setup(mrb_state *mrb, struct RProc *p)
64 {
65 mrb_callinfo *ci = mrb->c->ci;
66 struct RProc *up = p->upper;
67 struct REnv *e = NULL;
68
69 if (ci && ci->env) {
70 e = ci->env;
71 }
72 else if (up) {
73 struct RClass *tc = MRB_PROC_TARGET_CLASS(p);
74
75 e = env_new(mrb, up->body.irep->nlocals);
76 ci->env = e;
77 if (tc) {
78 e->c = tc;
79 mrb_field_write_barrier(mrb, (struct RBasic*)e, (struct RBasic*)tc);
80 }
81 if (MRB_PROC_ENV_P(up) && MRB_PROC_ENV(up)->cxt == NULL) {
82 e->mid = MRB_PROC_ENV(up)->mid;
83 }
84 }
85 if (e) {
86 p->e.env = e;
87 p->flags |= MRB_PROC_ENVSET;
88 mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)e);
89 }
90 }
91
92 struct RProc*
mrb_closure_new(mrb_state * mrb,mrb_irep * irep)93 mrb_closure_new(mrb_state *mrb, mrb_irep *irep)
94 {
95 struct RProc *p = mrb_proc_new(mrb, irep);
96
97 closure_setup(mrb, p);
98 return p;
99 }
100
101 MRB_API struct RProc*
mrb_proc_new_cfunc(mrb_state * mrb,mrb_func_t func)102 mrb_proc_new_cfunc(mrb_state *mrb, mrb_func_t func)
103 {
104 struct RProc *p;
105
106 p = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb->proc_class);
107 p->body.func = func;
108 p->flags |= MRB_PROC_CFUNC_FL;
109 p->upper = 0;
110 p->e.target_class = 0;
111
112 return p;
113 }
114
115 MRB_API struct RProc*
mrb_proc_new_cfunc_with_env(mrb_state * mrb,mrb_func_t func,mrb_int argc,const mrb_value * argv)116 mrb_proc_new_cfunc_with_env(mrb_state *mrb, mrb_func_t func, mrb_int argc, const mrb_value *argv)
117 {
118 struct RProc *p = mrb_proc_new_cfunc(mrb, func);
119 struct REnv *e;
120 int i;
121
122 p->e.env = e = env_new(mrb, argc);
123 p->flags |= MRB_PROC_ENVSET;
124 mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)e);
125 MRB_ENV_CLOSE(e);
126
127 /* NOTE: Prevents keeping invalid addresses when NoMemoryError is raised from `mrb_malloc()`. */
128 e->stack = NULL;
129 MRB_ENV_SET_LEN(e, 0);
130
131 e->stack = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * argc);
132 MRB_ENV_SET_LEN(e, argc);
133
134 if (argv) {
135 for (i = 0; i < argc; ++i) {
136 e->stack[i] = argv[i];
137 }
138 }
139 else {
140 for (i = 0; i < argc; ++i) {
141 SET_NIL_VALUE(e->stack[i]);
142 }
143 }
144 return p;
145 }
146
147 MRB_API struct RProc*
mrb_closure_new_cfunc(mrb_state * mrb,mrb_func_t func,int nlocals)148 mrb_closure_new_cfunc(mrb_state *mrb, mrb_func_t func, int nlocals)
149 {
150 return mrb_proc_new_cfunc_with_env(mrb, func, nlocals, NULL);
151 }
152
153 MRB_API mrb_value
mrb_proc_cfunc_env_get(mrb_state * mrb,mrb_int idx)154 mrb_proc_cfunc_env_get(mrb_state *mrb, mrb_int idx)
155 {
156 struct RProc *p = mrb->c->ci->proc;
157 struct REnv *e;
158
159 if (!p || !MRB_PROC_CFUNC_P(p)) {
160 mrb_raise(mrb, E_TYPE_ERROR, "Can't get cfunc env from non-cfunc proc.");
161 }
162 e = MRB_PROC_ENV(p);
163 if (!e) {
164 mrb_raise(mrb, E_TYPE_ERROR, "Can't get cfunc env from cfunc Proc without REnv.");
165 }
166 if (idx < 0 || MRB_ENV_LEN(e) <= idx) {
167 mrb_raisef(mrb, E_INDEX_ERROR, "Env index out of range: %i (expected: 0 <= index < %i)",
168 idx, MRB_ENV_LEN(e));
169 }
170
171 return e->stack[idx];
172 }
173
174 void
mrb_proc_copy(struct RProc * a,struct RProc * b)175 mrb_proc_copy(struct RProc *a, struct RProc *b)
176 {
177 if (a->body.irep) {
178 /* already initialized proc */
179 return;
180 }
181 a->flags = b->flags;
182 a->body = b->body;
183 if (!MRB_PROC_CFUNC_P(a) && a->body.irep) {
184 a->body.irep->refcnt++;
185 }
186 a->upper = b->upper;
187 a->e.env = b->e.env;
188 /* a->e.target_class = a->e.target_class; */
189 }
190
191 static mrb_value
mrb_proc_s_new(mrb_state * mrb,mrb_value proc_class)192 mrb_proc_s_new(mrb_state *mrb, mrb_value proc_class)
193 {
194 mrb_value blk;
195 mrb_value proc;
196 struct RProc *p;
197
198 /* Calling Proc.new without a block is not implemented yet */
199 mrb_get_args(mrb, "&!", &blk);
200 p = (struct RProc *)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb_class_ptr(proc_class));
201 mrb_proc_copy(p, mrb_proc_ptr(blk));
202 proc = mrb_obj_value(p);
203 mrb_funcall_with_block(mrb, proc, mrb_intern_lit(mrb, "initialize"), 0, NULL, proc);
204 if (!MRB_PROC_STRICT_P(p) &&
205 mrb->c->ci > mrb->c->cibase && MRB_PROC_ENV(p) == mrb->c->ci[-1].env) {
206 p->flags |= MRB_PROC_ORPHAN;
207 }
208 return proc;
209 }
210
211 static mrb_value
mrb_proc_init_copy(mrb_state * mrb,mrb_value self)212 mrb_proc_init_copy(mrb_state *mrb, mrb_value self)
213 {
214 mrb_value proc = mrb_get_arg1(mrb);
215
216 if (!mrb_proc_p(proc)) {
217 mrb_raise(mrb, E_ARGUMENT_ERROR, "not a proc");
218 }
219 mrb_proc_copy(mrb_proc_ptr(self), mrb_proc_ptr(proc));
220 return self;
221 }
222
223 /* 15.2.17.4.2 */
224 static mrb_value
proc_arity(mrb_state * mrb,mrb_value self)225 proc_arity(mrb_state *mrb, mrb_value self)
226 {
227 return mrb_fixnum_value(mrb_proc_arity(mrb_proc_ptr(self)));
228 }
229
230 /* 15.3.1.2.6 */
231 /* 15.3.1.3.27 */
232 /*
233 * call-seq:
234 * lambda { |...| block } -> a_proc
235 *
236 * Equivalent to <code>Proc.new</code>, except the resulting Proc objects
237 * check the number of parameters passed when called.
238 */
239 static mrb_value
proc_lambda(mrb_state * mrb,mrb_value self)240 proc_lambda(mrb_state *mrb, mrb_value self)
241 {
242 mrb_value blk;
243 struct RProc *p;
244
245 mrb_get_args(mrb, "&", &blk);
246 if (mrb_nil_p(blk)) {
247 mrb_raise(mrb, E_ARGUMENT_ERROR, "tried to create Proc object without a block");
248 }
249 if (!mrb_proc_p(blk)) {
250 mrb_raise(mrb, E_ARGUMENT_ERROR, "not a proc");
251 }
252 p = mrb_proc_ptr(blk);
253 if (!MRB_PROC_STRICT_P(p)) {
254 struct RProc *p2 = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, p->c);
255 mrb_proc_copy(p2, p);
256 p2->flags |= MRB_PROC_STRICT;
257 return mrb_obj_value(p2);
258 }
259 return blk;
260 }
261
262 mrb_int
mrb_proc_arity(const struct RProc * p)263 mrb_proc_arity(const struct RProc *p)
264 {
265 struct mrb_irep *irep;
266 const mrb_code *pc;
267 mrb_aspec aspec;
268 int ma, op, ra, pa, arity;
269
270 if (MRB_PROC_CFUNC_P(p)) {
271 /* TODO cfunc aspec not implemented yet */
272 return -1;
273 }
274
275 irep = p->body.irep;
276 if (!irep) {
277 return 0;
278 }
279
280 pc = irep->iseq;
281 /* arity is depend on OP_ENTER */
282 if (*pc != OP_ENTER) {
283 return 0;
284 }
285
286 aspec = PEEK_W(pc+1);
287 ma = MRB_ASPEC_REQ(aspec);
288 op = MRB_ASPEC_OPT(aspec);
289 ra = MRB_ASPEC_REST(aspec);
290 pa = MRB_ASPEC_POST(aspec);
291 arity = ra || (MRB_PROC_STRICT_P(p) && op) ? -(ma + pa + 1) : ma + pa;
292
293 return arity;
294 }
295
296 static void
tempirep_free(mrb_state * mrb,void * p)297 tempirep_free(mrb_state *mrb, void *p)
298 {
299 if (p) mrb_irep_free(mrb, (mrb_irep *)p);
300 }
301
302 static const mrb_data_type tempirep_type = { "temporary irep", tempirep_free };
303
304 void
mrb_init_proc(mrb_state * mrb)305 mrb_init_proc(mrb_state *mrb)
306 {
307 struct RProc *p;
308 mrb_method_t m;
309 struct RData *irep_obj = mrb_data_object_alloc(mrb, mrb->object_class, NULL, &tempirep_type);
310 mrb_irep *call_irep;
311 static const mrb_irep mrb_irep_zero = { 0 };
312
313 call_irep = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep));
314 irep_obj->data = call_irep;
315 *call_irep = mrb_irep_zero;
316 call_irep->flags = MRB_ISEQ_NO_FREE;
317 call_irep->iseq = call_iseq;
318 call_irep->ilen = 1;
319 call_irep->nregs = 2; /* receiver and block */
320
321 mrb_define_class_method(mrb, mrb->proc_class, "new", mrb_proc_s_new, MRB_ARGS_NONE()|MRB_ARGS_BLOCK());
322 mrb_define_method(mrb, mrb->proc_class, "initialize_copy", mrb_proc_init_copy, MRB_ARGS_REQ(1));
323 mrb_define_method(mrb, mrb->proc_class, "arity", proc_arity, MRB_ARGS_NONE());
324
325 p = mrb_proc_new(mrb, call_irep);
326 irep_obj->data = NULL;
327 MRB_METHOD_FROM_PROC(m, p);
328 mrb_define_method_raw(mrb, mrb->proc_class, mrb_intern_lit(mrb, "call"), m);
329 mrb_define_method_raw(mrb, mrb->proc_class, mrb_intern_lit(mrb, "[]"), m);
330
331 mrb_define_class_method(mrb, mrb->kernel_module, "lambda", proc_lambda, MRB_ARGS_NONE()|MRB_ARGS_BLOCK()); /* 15.3.1.2.6 */
332 mrb_define_method(mrb, mrb->kernel_module, "lambda", proc_lambda, MRB_ARGS_NONE()|MRB_ARGS_BLOCK()); /* 15.3.1.3.27 */
333 }
334