- Timestamp:
- Jan 21, 2018, 12:10:09 AM (6 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-1.3.0
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-eval/src/eval.c
r321 r331 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" 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 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); 7 11 8 12 static struct mrb_irep * … … 27 31 28 32 if (!e) return NULL; 33 if (!MRB_ENV_STACK_SHARED_P(e)) return NULL; 34 c = e->cxt.c; 29 35 proc = c->cibase[e->cioff].proc; 30 36 … … 127 133 } 128 134 break; 129 } 130 } 131 } 135 136 case OP_STOP: 137 if (mrb->c->ci->acc >= 0) { 138 irep->iseq[i] = MKOP_AB(OP_RETURN, irep->nlocals, OP_R_NORMAL); 139 } 140 break; 141 } 142 } 143 } 144 145 void mrb_codedump_all(mrb_state*, struct RProc*); 132 146 133 147 static struct RProc* 134 create_proc_from_string(mrb_state *mrb, char *s, int len, mrb_value binding, c har *file, mrb_int line)148 create_proc_from_string(mrb_state *mrb, char *s, int len, mrb_value binding, const char *file, mrb_int line) 135 149 { 136 150 mrbc_context *cxt; … … 146 160 cxt = mrbc_context_new(mrb); 147 161 cxt->lineno = line; 148 if (file) { 149 mrbc_filename(mrb, cxt, file); 150 } 162 163 mrbc_filename(mrb, cxt, file ? file : "(eval)"); 151 164 cxt->capture_errors = TRUE; 152 165 cxt->no_optimize = TRUE; … … 161 174 if (0 < p->nerr) { 162 175 /* parse error */ 163 char buf[256]; 164 int n; 165 n = snprintf(buf, sizeof(buf), "line %d: %s\n", p->error_buffer[0].lineno, p->error_buffer[0].message); 176 mrb_value str; 177 178 if (file) { 179 str = mrb_format(mrb, " file %S line %S: %S", 180 mrb_str_new_cstr(mrb, file), 181 mrb_fixnum_value(p->error_buffer[0].lineno), 182 mrb_str_new_cstr(mrb, p->error_buffer[0].message)); 183 } 184 else { 185 str = mrb_format(mrb, " line %S: %S", 186 mrb_fixnum_value(p->error_buffer[0].lineno), 187 mrb_str_new_cstr(mrb, p->error_buffer[0].message)); 188 } 166 189 mrb_parser_free(p); 167 190 mrbc_context_free(mrb, cxt); 168 mrb_exc_raise(mrb, mrb_exc_new (mrb, E_SYNTAX_ERROR, buf, n));191 mrb_exc_raise(mrb, mrb_exc_new_str(mrb, E_SYNTAX_ERROR, str)); 169 192 } 170 193 … … 182 205 if (!e) e = c->ci[-1].env; 183 206 e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, (struct RClass*)e); 184 e-> mid = c->ci[-1].mid;185 e->cioff = c->ci - c->cibase - 1;207 e->cxt.c = c; 208 e->cioff = c->ci - c->cibase; 186 209 e->stack = c->ci->stackent; 187 MRB_SET_ENV_STACK_LEN(e, c->ci[-1].proc->body.irep->nlocals); 188 c->ci->env = e; 210 MRB_SET_ENV_STACK_LEN(e, c->ci->proc->body.irep->nlocals); 211 c->ci->target_class = proc->target_class; 212 c->ci->env = 0; 189 213 proc->env = e; 190 214 patch_irep(mrb, proc->body.irep, 0); … … 194 218 195 219 return proc; 220 } 221 222 static mrb_value 223 exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc) 224 { 225 if (mrb->c->ci->acc < 0) { 226 mrb_value ret = mrb_top_run(mrb, proc, mrb->c->stack[0], 0); 227 if (mrb->exc) { 228 mrb_exc_raise(mrb, mrb_obj_value(mrb->exc)); 229 } 230 return ret; 231 } 232 return mrb_exec_irep(mrb, self, proc); 196 233 } 197 234 … … 204 241 char *file = NULL; 205 242 mrb_int line = 1; 206 mrb_value ret;207 243 struct RProc *proc; 208 244 … … 210 246 211 247 proc = create_proc_from_string(mrb, s, len, binding, file, line); 212 ret = mrb_toplevel_run(mrb, proc); 213 if (mrb->exc) { 214 mrb_exc_raise(mrb, mrb_obj_value(mrb->exc)); 215 } 216 217 return ret; 218 } 219 220 mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self); 221 222 #define CI_ACC_SKIP -1 248 mrb_assert(!MRB_PROC_CFUNC_P(proc)); 249 return exec_irep(mrb, self, proc); 250 } 223 251 224 252 static mrb_value 225 253 f_instance_eval(mrb_state *mrb, mrb_value self) 226 254 { 227 struct mrb_context *c = mrb->c;228 255 mrb_value b; 229 256 mrb_int argc; mrb_value *argv; … … 237 264 mrb_int line = 1; 238 265 mrb_value cv; 266 struct RProc *proc; 239 267 240 268 mrb_get_args(mrb, "s|zi", &s, &len, &file, &line); 241 c->ci->acc = CI_ACC_SKIP;242 269 cv = mrb_singleton_class(mrb, self); 243 c->ci->target_class = mrb_class_ptr(cv); 244 return mrb_run(mrb, create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line), self); 270 proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line); 271 proc->target_class = mrb_class_ptr(cv); 272 mrb->c->ci->env = NULL; 273 mrb_assert(!MRB_PROC_CFUNC_P(proc)); 274 return exec_irep(mrb, self, proc); 245 275 } 246 276 else {
Note:
See TracChangeset
for help on using the changeset viewer.