Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-eval
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-eval/src/eval.c
r331 r439 13 13 get_closure_irep(mrb_state *mrb, int level) 14 14 { 15 struct mrb_context *c = mrb->c; 16 struct REnv *e = c->ci[-1].proc->env; 17 struct RProc *proc; 18 19 if (level == 0) { 20 proc = c->ci[-1].proc; 21 if (MRB_PROC_CFUNC_P(proc)) { 22 return NULL; 23 } 24 return proc->body.irep; 25 } 26 27 while (--level) { 28 e = (struct REnv*)e->c; 29 if (!e) return NULL; 30 } 31 32 if (!e) return NULL; 33 if (!MRB_ENV_STACK_SHARED_P(e)) return NULL; 34 c = e->cxt.c; 35 proc = c->cibase[e->cioff].proc; 36 37 if (!proc || MRB_PROC_CFUNC_P(proc)) { 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)) { 38 23 return NULL; 39 24 } … … 41 26 } 42 27 43 static inline mrb_code 28 /* search for irep lev above the bottom */ 29 static mrb_irep* 30 search_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 47 static uint16_t 44 48 search_variable(mrb_state *mrb, mrb_sym vsym, int bnest) 45 49 { … … 49 53 50 54 for (level = 0; (virep = get_closure_irep(mrb, level)); level++) { 51 if ( !virep ||virep->lv == NULL) {55 if (virep->lv == NULL) { 52 56 continue; 53 57 } 54 58 for (pos = 0; pos < virep->nlocals - 1; pos++) { 55 59 if (vsym == virep->lv[pos].name) { 56 return (MKARG_B(pos + 1) | MKARG_C(level + bnest)); 57 } 58 } 59 } 60 60 return (pos+1)<<8 | (level+bnest); 61 } 62 } 63 } 64 65 return 0; 66 } 67 68 static int 69 irep_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 } 61 79 return 0; 62 80 } … … 71 89 } 72 90 91 extern uint8_t mrb_insn_size[]; 92 extern uint8_t mrb_insn_size1[]; 93 extern uint8_t mrb_insn_size2[]; 94 extern uint8_t mrb_insn_size3[]; 95 73 96 static void 74 patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest) 75 { 76 size_t i; 77 mrb_code c; 78 int argc = 0; 79 80 for (i = 0; i < irep->ilen; i++) { 81 c = irep->iseq[i]; 82 switch(GET_OPCODE(c)){ 83 case OP_ENTER: 84 { 85 mrb_aspec ax = GETARG_Ax(c); 86 /* extra 1 means a slot for block */ 87 argc = MRB_ASPEC_REQ(ax)+MRB_ASPEC_OPT(ax)+MRB_ASPEC_REST(ax)+MRB_ASPEC_POST(ax)+1; 88 } 89 break; 90 97 patch_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){ 91 112 case OP_EPUSH: 92 patch_irep(mrb, irep->reps[GETARG_Bx(c)], bnest + 1); 113 a = PEEK_B(iseq+i+1); 114 patch_irep(mrb, irep->reps[a], bnest + 1, top); 93 115 break; 94 116 95 117 case OP_LAMBDA: 96 { 97 int arg_c = GETARG_c(c); 98 if (arg_c & OP_L_CAPTURE) { 99 patch_irep(mrb, irep->reps[GETARG_b(c)], bnest + 1); 100 } 101 } 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); 102 122 break; 103 123 104 124 case OP_SEND: 105 if (GETARG_C(c) != 0) { 125 b = PEEK_B(iseq+i+2); 126 c = PEEK_B(iseq+i+3); 127 if (c != 0) { 106 128 break; 107 129 } 108 {109 mrb_code arg = search_variable(mrb, irep->syms[GETARG_B(c)], bnest);130 else { 131 uint16_t arg = search_variable(mrb, irep->syms[b], bnest); 110 132 if (arg != 0) { 111 133 /* must replace */ 112 irep->iseq[i] = MKOPCODE(OP_GETUPVAR) | MKARG_A(GETARG_A(c)) | arg; 134 iseq[i] = OP_GETUPVAR; 135 iseq[i+2] = arg >> 8; 136 iseq[i+3] = arg & 0xff; 113 137 } 114 138 } … … 116 140 117 141 case OP_MOVE: 142 a = PEEK_B(iseq+i+1); 143 b = PEEK_B(iseq+i+2); 118 144 /* src part */ 119 if (potential_upvar_p(irep->lv, GETARG_B(c), argc, irep->nlocals)) {120 mrb_code arg = search_variable(mrb, irep->lv[GETARG_B(c)- 1].name, bnest);145 if (potential_upvar_p(irep->lv, b, argc, irep->nlocals)) { 146 uint16_t arg = search_variable(mrb, irep->lv[b - 1].name, bnest); 121 147 if (arg != 0) { 122 148 /* must replace */ 123 irep->iseq[i] = MKOPCODE(OP_GETUPVAR) | MKARG_A(GETARG_A(c)) | arg; 149 iseq[i] = insn = OP_GETUPVAR; 150 iseq[i+2] = arg >> 8; 151 iseq[i+3] = arg & 0xff; 124 152 } 125 153 } 126 154 /* dst part */ 127 if (potential_upvar_p(irep->lv, GETARG_A(c), argc, irep->nlocals)) {128 mrb_code arg = search_variable(mrb, irep->lv[GETARG_A(c)- 1].name, bnest);155 if (potential_upvar_p(irep->lv, a, argc, irep->nlocals)) { 156 uint16_t arg = search_variable(mrb, irep->lv[a - 1].name, bnest); 129 157 if (arg != 0) { 130 158 /* must replace */ 131 irep->iseq[i] = MKOPCODE(OP_SETUPVAR) | MKARG_A(GETARG_B(c)) | arg; 132 } 133 } 134 break; 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 } 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]; 142 220 } 143 221 } … … 146 224 147 225 static struct RProc* 148 create_proc_from_string(mrb_state *mrb, char *s, int len, mrb_value binding, const char *file, mrb_int line)226 create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding, const char *file, mrb_int line) 149 227 { 150 228 mrbc_context *cxt; … … 152 230 struct RProc *proc; 153 231 struct REnv *e; 154 struct mrb_context *c = mrb->c; 232 mrb_callinfo *ci; /* callinfo of eval caller */ 233 struct RClass *target_class = NULL; 234 int bidx; 155 235 156 236 if (!mrb_nil_p(binding)) { … … 159 239 160 240 cxt = mrbc_context_new(mrb); 161 cxt->lineno = line;241 cxt->lineno = (uint16_t)line; 162 242 163 243 mrbc_filename(mrb, cxt, file ? file : "(eval)"); 164 244 cxt->capture_errors = TRUE; 165 245 cxt->no_optimize = TRUE; 246 cxt->on_eval = TRUE; 166 247 167 248 p = mrb_parse_nstring(mrb, s, len, cxt); … … 177 258 178 259 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));260 str = mrb_format(mrb, "file %s line %d: %s", 261 file, 262 p->error_buffer[0].lineno, 263 p->error_buffer[0].message); 183 264 } 184 265 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));266 str = mrb_format(mrb, "line %d: %s", 267 p->error_buffer[0].lineno, 268 p->error_buffer[0].message); 188 269 } 189 270 mrb_parser_free(p); … … 199 280 mrb_raise(mrb, E_SCRIPT_ERROR, "codegen error"); 200 281 } 201 if (c->ci[-1].proc->target_class) { 202 proc->target_class = c->ci[-1].proc->target_class; 203 } 204 e = c->ci[-1].proc->env; 205 if (!e) e = c->ci[-1].env; 206 e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, (struct RClass*)e); 207 e->cxt.c = c; 208 e->cioff = c->ci - c->cibase; 209 e->stack = c->ci->stackent; 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; 213 proc->env = e; 214 patch_irep(mrb, proc->body.irep, 0); 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); */ 215 316 216 317 mrb_parser_free(p); … … 223 324 exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc) 224 325 { 326 /* no argument passed from eval() */ 327 mrb->c->ci->argc = 0; 225 328 if (mrb->c->ci->acc < 0) { 226 mrb_value ret = mrb_top_run(mrb, proc, mrb->c->stack[0], 0); 329 ptrdiff_t cioff = mrb->c->ci - mrb->c->cibase; 330 mrb_value ret = mrb_top_run(mrb, proc, self, 0); 227 331 if (mrb->exc) { 228 332 mrb_exc_raise(mrb, mrb_obj_value(mrb->exc)); 229 333 } 334 mrb->c->ci = mrb->c->cibase + cioff; 230 335 return ret; 231 336 } 337 /* clear block */ 338 mrb->c->stack[1] = mrb_nil_value(); 232 339 return mrb_exec_irep(mrb, self, proc); 233 340 } … … 256 363 mrb_int argc; mrb_value *argv; 257 364 258 mrb_get_args(mrb, "* &", &argv, &argc, &b);365 mrb_get_args(mrb, "*!&", &argv, &argc, &b); 259 366 260 367 if (mrb_nil_p(b)) { … … 269 376 cv = mrb_singleton_class(mrb, self); 270 377 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; 378 MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(cv)); 273 379 mrb_assert(!MRB_PROC_CFUNC_P(proc)); 380 mrb->c->ci->target_class = mrb_class_ptr(cv); 274 381 return exec_irep(mrb, self, proc); 275 382 } … … 284 391 { 285 392 mrb_define_module_function(mrb, mrb->kernel_module, "eval", f_eval, MRB_ARGS_ARG(1, 3)); 286 mrb_define_method(mrb, mrb ->kernel_module, "instance_eval", f_instance_eval, MRB_ARGS_ARG(1, 2));393 mrb_define_method(mrb, mrb_class_get(mrb, "BasicObject"), "instance_eval", f_instance_eval, MRB_ARGS_OPT(3)|MRB_ARGS_BLOCK()); 287 394 } 288 395 -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-eval/test/eval.rb
r331 r439 35 35 assert_equal(2) { 36 36 a = 10 37 Kernel.eval 'def f(a); b=a .send(:+, 1); end'37 Kernel.eval 'def f(a); b=a+1; end' 38 38 f(1) 39 39 } … … 59 59 assert('String instance_eval') do 60 60 obj = Object.new 61 obj.instance_ variable_set :@test, 'test'61 obj.instance_eval{ @test = 'test' } 62 62 assert_raise(ArgumentError) { obj.instance_eval(0) { } } 63 63 assert_raise(ArgumentError) { obj.instance_eval('0', 'test', 0, 'test') } … … 81 81 end 82 82 83 assert(' Object#instance_eval with begin-rescue-ensure execution order') do83 assert('BasicObject#instance_eval with begin-rescue-ensure execution order') do 84 84 class HellRaiser 85 85 def raise_hell … … 100 100 assert_equal([:enter_raise_hell, :begin, :rescue, :ensure], hell_raiser.raise_hell) 101 101 end 102 103 assert('BasicObject#instance_eval to define singleton methods Issue #3141') do 104 foo_class = Class.new do 105 def bar(x) 106 instance_eval "def baz; #{x}; end" 107 end 108 end 109 110 f1 = foo_class.new 111 f2 = foo_class.new 112 f1.bar 1 113 f2.bar 2 114 assert_equal(1){f1.baz} 115 assert_equal(2){f2.baz} 116 end 117 118 assert('Kernel.#eval(string) Issue #4021') do 119 assert_equal('FOO') { (eval <<'EOS').call } 120 foo = "FOO" 121 Proc.new { foo } 122 EOS 123 assert_equal('FOO') { 124 def do_eval(code) 125 eval(code) 126 end 127 do_eval(<<'EOS').call 128 foo = "FOO" 129 Proc.new { foo } 130 EOS 131 } 132 end
Note:
See TracChangeset
for help on using the changeset viewer.