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>
9 mrb_value mrb_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p);
10 mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self);
12 void mrb_codedump_all(mrb_state*, struct RProc*);
15 create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding, const char *file, mrb_int line)
18 struct mrb_parser_state *p;
21 mrb_callinfo *ci; /* callinfo of eval caller */
22 struct RClass *target_class = NULL;
25 if (!mrb_nil_p(binding)) {
26 mrb_raise(mrb, E_ARGUMENT_ERROR, "Binding of eval must be nil.");
29 cxt = mrbc_context_new(mrb);
30 cxt->lineno = (uint16_t)line;
32 mrbc_filename(mrb, cxt, file ? file : "(eval)");
33 cxt->capture_errors = TRUE;
34 cxt->no_optimize = TRUE;
35 ci = (mrb->c->ci > mrb->c->cibase) ? mrb->c->ci - 1 : mrb->c->cibase;
36 cxt->upper = ci->proc && MRB_PROC_CFUNC_P(ci->proc) ? NULL : ci->proc;
38 p = mrb_parse_nstring(mrb, s, len, cxt);
40 /* only occur when memory ran out */
42 mrb_raise(mrb, E_RUNTIME_ERROR, "Failed to create parser state.");
50 str = mrb_format(mrb, "file %s line %d: %s",
52 p->error_buffer[0].lineno,
53 p->error_buffer[0].message);
56 str = mrb_format(mrb, "line %d: %s",
57 p->error_buffer[0].lineno,
58 p->error_buffer[0].message);
61 mrbc_context_free(mrb, cxt);
62 mrb_exc_raise(mrb, mrb_exc_new_str(mrb, E_SYNTAX_ERROR, str));
65 proc = mrb_generate_code(mrb, p);
69 mrbc_context_free(mrb, cxt);
70 mrb_raise(mrb, E_SCRIPT_ERROR, "codegen error");
72 if (mrb->c->ci > mrb->c->cibase) {
79 target_class = MRB_PROC_TARGET_CLASS(ci->proc);
81 if (ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) {
86 e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV,
87 (struct RClass*)target_class);
89 e->stack = ci[1].stackent;
91 MRB_ENV_SET_LEN(e, ci->proc->body.irep->nlocals);
93 if (ci->argc < 0) bidx = 2;
95 MRB_ENV_SET_BIDX(e, bidx);
99 proc->flags |= MRB_PROC_ENVSET;
100 mrb_field_write_barrier(mrb, (struct RBasic*)proc, (struct RBasic*)e);
102 proc->upper = ci->proc;
103 mrb->c->ci->target_class = target_class;
104 /* mrb_codedump_all(mrb, proc); */
107 mrbc_context_free(mrb, cxt);
113 exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc)
115 /* no argument passed from eval() */
116 mrb->c->ci->argc = 0;
117 if (mrb->c->ci->acc < 0) {
118 ptrdiff_t cioff = mrb->c->ci - mrb->c->cibase;
119 mrb_value ret = mrb_top_run(mrb, proc, self, 0);
121 mrb_exc_raise(mrb, mrb_obj_value(mrb->exc));
123 mrb->c->ci = mrb->c->cibase + cioff;
127 mrb->c->stack[1] = mrb_nil_value();
128 return mrb_exec_irep(mrb, self, proc);
132 f_eval(mrb_state *mrb, mrb_value self)
136 mrb_value binding = mrb_nil_value();
141 mrb_get_args(mrb, "s|ozi", &s, &len, &binding, &file, &line);
143 proc = create_proc_from_string(mrb, s, len, binding, file, line);
144 mrb_assert(!MRB_PROC_CFUNC_P(proc));
145 return exec_irep(mrb, self, proc);
149 f_instance_eval(mrb_state *mrb, mrb_value self)
152 mrb_int argc; mrb_value *argv;
154 mrb_get_args(mrb, "*!&", &argv, &argc, &b);
164 mrb_get_args(mrb, "s|zi", &s, &len, &file, &line);
165 cv = mrb_singleton_class(mrb, self);
166 proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line);
167 MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(cv));
168 mrb_assert(!MRB_PROC_CFUNC_P(proc));
169 mrb->c->ci->target_class = mrb_class_ptr(cv);
170 return exec_irep(mrb, self, proc);
173 mrb_get_args(mrb, "&", &b);
174 return mrb_obj_instance_eval(mrb, self);
179 mrb_mruby_eval_gem_init(mrb_state* mrb)
181 mrb_define_module_function(mrb, mrb->kernel_module, "eval", f_eval, MRB_ARGS_ARG(1, 3));
182 mrb_define_method(mrb, mrb_class_get(mrb, "BasicObject"), "instance_eval", f_instance_eval, MRB_ARGS_OPT(3)|MRB_ARGS_BLOCK());
186 mrb_mruby_eval_gem_final(mrb_state* mrb)