source: EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-eval/src/eval.c@ 439

Last change on this file since 439 was 439, checked in by coas-nagasima, 4 years ago

mrubyを2.1.1に更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 9.7 KB
Line 
1#include <mruby.h>
2#include <mruby/class.h>
3#include <mruby/compile.h>
4#include <mruby/irep.h>
5#include <mruby/proc.h>
6#include <mruby/opcode.h>
7#include <mruby/error.h>
8
9mrb_value mrb_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p);
10mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self);
11
12static struct mrb_irep *
13get_closure_irep(mrb_state *mrb, int level)
14{
15 struct RProc *proc = mrb->c->ci[-1].proc;
16
17 while (level--) {
18 if (!proc) return NULL;
19 proc = proc->upper;
20 }
21 if (!proc) return NULL;
22 if (MRB_PROC_CFUNC_P(proc)) {
23 return NULL;
24 }
25 return proc->body.irep;
26}
27
28/* search for irep lev above the bottom */
29static mrb_irep*
30search_irep(mrb_irep *top, int bnest, int lev, mrb_irep *bottom)
31{
32 int i;
33
34 for (i=0; i<top->rlen; i++) {
35 mrb_irep* tmp = top->reps[i];
36
37 if (tmp == bottom) return top;
38 tmp = search_irep(tmp, bnest-1, lev, bottom);
39 if (tmp) {
40 if (bnest == lev) return top;
41 return tmp;
42 }
43 }
44 return NULL;
45}
46
47static uint16_t
48search_variable(mrb_state *mrb, mrb_sym vsym, int bnest)
49{
50 mrb_irep *virep;
51 int level;
52 int pos;
53
54 for (level = 0; (virep = get_closure_irep(mrb, level)); level++) {
55 if (virep->lv == NULL) {
56 continue;
57 }
58 for (pos = 0; pos < virep->nlocals - 1; pos++) {
59 if (vsym == virep->lv[pos].name) {
60 return (pos+1)<<8 | (level+bnest);
61 }
62 }
63 }
64
65 return 0;
66}
67
68static int
69irep_argc(mrb_irep *irep)
70{
71 mrb_code c;
72
73 c = irep->iseq[0];
74 if (c == OP_ENTER) {
75 mrb_aspec ax = PEEK_W(irep->iseq+1);
76 /* extra 1 means a slot for block */
77 return MRB_ASPEC_REQ(ax)+MRB_ASPEC_OPT(ax)+MRB_ASPEC_REST(ax)+MRB_ASPEC_POST(ax)+1;
78 }
79 return 0;
80}
81
82static mrb_bool
83potential_upvar_p(struct mrb_locals *lv, uint16_t v, int argc, uint16_t nlocals)
84{
85 if (v >= nlocals) return FALSE;
86 /* skip arguments */
87 if (v < argc+1) return FALSE;
88 return TRUE;
89}
90
91extern uint8_t mrb_insn_size[];
92extern uint8_t mrb_insn_size1[];
93extern uint8_t mrb_insn_size2[];
94extern uint8_t mrb_insn_size3[];
95
96static void
97patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest, mrb_irep *top)
98{
99 int i;
100 uint32_t a;
101 uint16_t b;
102 uint8_t c;
103 mrb_code insn;
104 int argc = irep_argc(irep);
105 mrb_code *iseq = (mrb_code *)irep->iseq;
106
107 mrb_assert((irep->flags & MRB_ISEQ_NO_FREE) == 0);
108
109 for (i = 0; i < irep->ilen; ) {
110 insn = iseq[i];
111 switch(insn){
112 case OP_EPUSH:
113 a = PEEK_B(iseq+i+1);
114 patch_irep(mrb, irep->reps[a], bnest + 1, top);
115 break;
116
117 case OP_LAMBDA:
118 case OP_BLOCK:
119 a = PEEK_B(iseq+i+1);
120 b = PEEK_B(iseq+i+2);
121 patch_irep(mrb, irep->reps[b], bnest + 1, top);
122 break;
123
124 case OP_SEND:
125 b = PEEK_B(iseq+i+2);
126 c = PEEK_B(iseq+i+3);
127 if (c != 0) {
128 break;
129 }
130 else {
131 uint16_t arg = search_variable(mrb, irep->syms[b], bnest);
132 if (arg != 0) {
133 /* must replace */
134 iseq[i] = OP_GETUPVAR;
135 iseq[i+2] = arg >> 8;
136 iseq[i+3] = arg & 0xff;
137 }
138 }
139 break;
140
141 case OP_MOVE:
142 a = PEEK_B(iseq+i+1);
143 b = PEEK_B(iseq+i+2);
144 /* src part */
145 if (potential_upvar_p(irep->lv, b, argc, irep->nlocals)) {
146 uint16_t arg = search_variable(mrb, irep->lv[b - 1].name, bnest);
147 if (arg != 0) {
148 /* must replace */
149 iseq[i] = insn = OP_GETUPVAR;
150 iseq[i+2] = arg >> 8;
151 iseq[i+3] = arg & 0xff;
152 }
153 }
154 /* dst part */
155 if (potential_upvar_p(irep->lv, a, argc, irep->nlocals)) {
156 uint16_t arg = search_variable(mrb, irep->lv[a - 1].name, bnest);
157 if (arg != 0) {
158 /* must replace */
159 iseq[i] = insn = OP_SETUPVAR;
160 iseq[i+1] = (mrb_code)b;
161 iseq[i+2] = arg >> 8;
162 iseq[i+3] = arg & 0xff;
163 }
164 }
165 break;
166
167 case OP_GETUPVAR:
168 a = PEEK_B(iseq+i+1);
169 b = PEEK_B(iseq+i+2);
170 c = PEEK_B(iseq+i+3);
171 {
172 int lev = c+1;
173 mrb_irep *tmp = search_irep(top, bnest, lev, irep);
174 if (potential_upvar_p(tmp->lv, b, irep_argc(tmp), tmp->nlocals)) {
175 uint16_t arg = search_variable(mrb, tmp->lv[b-1].name, bnest);
176 if (arg != 0) {
177 /* must replace */
178 iseq[i] = OP_GETUPVAR;
179 iseq[i+2] = arg >> 8;
180 iseq[i+3] = arg & 0xff;
181 }
182 }
183 }
184 break;
185
186 case OP_SETUPVAR:
187 a = PEEK_B(iseq+i+1);
188 b = PEEK_B(iseq+i+2);
189 c = PEEK_B(iseq+i+3);
190 {
191 int lev = c+1;
192 mrb_irep *tmp = search_irep(top, bnest, lev, irep);
193 if (potential_upvar_p(tmp->lv, b, irep_argc(tmp), tmp->nlocals)) {
194 uint16_t arg = search_variable(mrb, tmp->lv[b-1].name, bnest);
195 if (arg != 0) {
196 /* must replace */
197 iseq[i] = OP_SETUPVAR;
198 iseq[i+1] = a;
199 iseq[i+2] = arg >> 8;
200 iseq[i+3] = arg & 0xff;
201 }
202 }
203 }
204 break;
205
206 case OP_EXT1:
207 insn = PEEK_B(iseq+i+1);
208 i += mrb_insn_size1[insn]+1;
209 continue;
210 case OP_EXT2:
211 insn = PEEK_B(iseq+i+1);
212 i += mrb_insn_size2[insn]+1;
213 continue;
214 case OP_EXT3:
215 insn = PEEK_B(iseq+i+1);
216 i += mrb_insn_size3[insn]+1;
217 continue;
218 }
219 i+=mrb_insn_size[insn];
220 }
221}
222
223void mrb_codedump_all(mrb_state*, struct RProc*);
224
225static struct RProc*
226create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding, const char *file, mrb_int line)
227{
228 mrbc_context *cxt;
229 struct mrb_parser_state *p;
230 struct RProc *proc;
231 struct REnv *e;
232 mrb_callinfo *ci; /* callinfo of eval caller */
233 struct RClass *target_class = NULL;
234 int bidx;
235
236 if (!mrb_nil_p(binding)) {
237 mrb_raise(mrb, E_ARGUMENT_ERROR, "Binding of eval must be nil.");
238 }
239
240 cxt = mrbc_context_new(mrb);
241 cxt->lineno = (uint16_t)line;
242
243 mrbc_filename(mrb, cxt, file ? file : "(eval)");
244 cxt->capture_errors = TRUE;
245 cxt->no_optimize = TRUE;
246 cxt->on_eval = TRUE;
247
248 p = mrb_parse_nstring(mrb, s, len, cxt);
249
250 /* only occur when memory ran out */
251 if (!p) {
252 mrb_raise(mrb, E_RUNTIME_ERROR, "Failed to create parser state.");
253 }
254
255 if (0 < p->nerr) {
256 /* parse error */
257 mrb_value str;
258
259 if (file) {
260 str = mrb_format(mrb, "file %s line %d: %s",
261 file,
262 p->error_buffer[0].lineno,
263 p->error_buffer[0].message);
264 }
265 else {
266 str = mrb_format(mrb, "line %d: %s",
267 p->error_buffer[0].lineno,
268 p->error_buffer[0].message);
269 }
270 mrb_parser_free(p);
271 mrbc_context_free(mrb, cxt);
272 mrb_exc_raise(mrb, mrb_exc_new_str(mrb, E_SYNTAX_ERROR, str));
273 }
274
275 proc = mrb_generate_code(mrb, p);
276 if (proc == NULL) {
277 /* codegen error */
278 mrb_parser_free(p);
279 mrbc_context_free(mrb, cxt);
280 mrb_raise(mrb, E_SCRIPT_ERROR, "codegen error");
281 }
282 if (mrb->c->ci > mrb->c->cibase) {
283 ci = &mrb->c->ci[-1];
284 }
285 else {
286 ci = mrb->c->cibase;
287 }
288 if (ci->proc) {
289 target_class = MRB_PROC_TARGET_CLASS(ci->proc);
290 }
291 if (ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) {
292 if (ci->env) {
293 e = ci->env;
294 }
295 else {
296 e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV,
297 (struct RClass*)target_class);
298 e->mid = ci->mid;
299 e->stack = ci[1].stackent;
300 e->cxt = mrb->c;
301 MRB_ENV_SET_STACK_LEN(e, ci->proc->body.irep->nlocals);
302 bidx = ci->argc;
303 if (ci->argc < 0) bidx = 2;
304 else bidx += 1;
305 MRB_ENV_SET_BIDX(e, bidx);
306 ci->env = e;
307 }
308 proc->e.env = e;
309 proc->flags |= MRB_PROC_ENVSET;
310 mrb_field_write_barrier(mrb, (struct RBasic*)proc, (struct RBasic*)e);
311 }
312 proc->upper = ci->proc;
313 mrb->c->ci->target_class = target_class;
314 patch_irep(mrb, proc->body.irep, 0, proc->body.irep);
315 /* mrb_codedump_all(mrb, proc); */
316
317 mrb_parser_free(p);
318 mrbc_context_free(mrb, cxt);
319
320 return proc;
321}
322
323static mrb_value
324exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc)
325{
326 /* no argument passed from eval() */
327 mrb->c->ci->argc = 0;
328 if (mrb->c->ci->acc < 0) {
329 ptrdiff_t cioff = mrb->c->ci - mrb->c->cibase;
330 mrb_value ret = mrb_top_run(mrb, proc, self, 0);
331 if (mrb->exc) {
332 mrb_exc_raise(mrb, mrb_obj_value(mrb->exc));
333 }
334 mrb->c->ci = mrb->c->cibase + cioff;
335 return ret;
336 }
337 /* clear block */
338 mrb->c->stack[1] = mrb_nil_value();
339 return mrb_exec_irep(mrb, self, proc);
340}
341
342static mrb_value
343f_eval(mrb_state *mrb, mrb_value self)
344{
345 char *s;
346 mrb_int len;
347 mrb_value binding = mrb_nil_value();
348 char *file = NULL;
349 mrb_int line = 1;
350 struct RProc *proc;
351
352 mrb_get_args(mrb, "s|ozi", &s, &len, &binding, &file, &line);
353
354 proc = create_proc_from_string(mrb, s, len, binding, file, line);
355 mrb_assert(!MRB_PROC_CFUNC_P(proc));
356 return exec_irep(mrb, self, proc);
357}
358
359static mrb_value
360f_instance_eval(mrb_state *mrb, mrb_value self)
361{
362 mrb_value b;
363 mrb_int argc; mrb_value *argv;
364
365 mrb_get_args(mrb, "*!&", &argv, &argc, &b);
366
367 if (mrb_nil_p(b)) {
368 char *s;
369 mrb_int len;
370 char *file = NULL;
371 mrb_int line = 1;
372 mrb_value cv;
373 struct RProc *proc;
374
375 mrb_get_args(mrb, "s|zi", &s, &len, &file, &line);
376 cv = mrb_singleton_class(mrb, self);
377 proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line);
378 MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(cv));
379 mrb_assert(!MRB_PROC_CFUNC_P(proc));
380 mrb->c->ci->target_class = mrb_class_ptr(cv);
381 return exec_irep(mrb, self, proc);
382 }
383 else {
384 mrb_get_args(mrb, "&", &b);
385 return mrb_obj_instance_eval(mrb, self);
386 }
387}
388
389void
390mrb_mruby_eval_gem_init(mrb_state* mrb)
391{
392 mrb_define_module_function(mrb, mrb->kernel_module, "eval", f_eval, MRB_ARGS_ARG(1, 3));
393 mrb_define_method(mrb, mrb_class_get(mrb, "BasicObject"), "instance_eval", f_instance_eval, MRB_ARGS_OPT(3)|MRB_ARGS_BLOCK());
394}
395
396void
397mrb_mruby_eval_gem_final(mrb_state* mrb)
398{
399}
Note: See TracBrowser for help on using the repository browser.