[331] | 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>
|
---|
[270] | 8 |
|
---|
[331] | 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);
|
---|
| 11 |
|
---|
[270] | 12 | static struct mrb_irep *
|
---|
| 13 | get_closure_irep(mrb_state *mrb, int level)
|
---|
| 14 | {
|
---|
[439] | 15 | struct RProc *proc = mrb->c->ci[-1].proc;
|
---|
[270] | 16 |
|
---|
[439] | 17 | while (level--) {
|
---|
| 18 | if (!proc) return NULL;
|
---|
| 19 | proc = proc->upper;
|
---|
[270] | 20 | }
|
---|
[439] | 21 | if (!proc) return NULL;
|
---|
| 22 | if (MRB_PROC_CFUNC_P(proc)) {
|
---|
| 23 | return NULL;
|
---|
[270] | 24 | }
|
---|
[439] | 25 | return proc->body.irep;
|
---|
| 26 | }
|
---|
[270] | 27 |
|
---|
[439] | 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;
|
---|
[270] | 33 |
|
---|
[439] | 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 | }
|
---|
[270] | 43 | }
|
---|
[439] | 44 | return NULL;
|
---|
[270] | 45 | }
|
---|
| 46 |
|
---|
[439] | 47 | static uint16_t
|
---|
[270] | 48 | search_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++) {
|
---|
[439] | 55 | if (virep->lv == NULL) {
|
---|
[270] | 56 | continue;
|
---|
| 57 | }
|
---|
| 58 | for (pos = 0; pos < virep->nlocals - 1; pos++) {
|
---|
| 59 | if (vsym == virep->lv[pos].name) {
|
---|
[439] | 60 | return (pos+1)<<8 | (level+bnest);
|
---|
[270] | 61 | }
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | return 0;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[439] | 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 | }
|
---|
| 79 | return 0;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[270] | 82 | static mrb_bool
|
---|
| 83 | potential_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 |
|
---|
[439] | 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 |
|
---|
[270] | 96 | static void
|
---|
[439] | 97 | patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest, mrb_irep *top)
|
---|
[270] | 98 | {
|
---|
[439] | 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;
|
---|
[270] | 106 |
|
---|
[439] | 107 | mrb_assert((irep->flags & MRB_ISEQ_NO_FREE) == 0);
|
---|
[270] | 108 |
|
---|
[439] | 109 | for (i = 0; i < irep->ilen; ) {
|
---|
| 110 | insn = iseq[i];
|
---|
| 111 | switch(insn){
|
---|
[270] | 112 | case OP_EPUSH:
|
---|
[439] | 113 | a = PEEK_B(iseq+i+1);
|
---|
| 114 | patch_irep(mrb, irep->reps[a], bnest + 1, top);
|
---|
[270] | 115 | break;
|
---|
| 116 |
|
---|
| 117 | case OP_LAMBDA:
|
---|
[439] | 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);
|
---|
[270] | 122 | break;
|
---|
| 123 |
|
---|
| 124 | case OP_SEND:
|
---|
[439] | 125 | b = PEEK_B(iseq+i+2);
|
---|
| 126 | c = PEEK_B(iseq+i+3);
|
---|
| 127 | if (c != 0) {
|
---|
[270] | 128 | break;
|
---|
| 129 | }
|
---|
[439] | 130 | else {
|
---|
| 131 | uint16_t arg = search_variable(mrb, irep->syms[b], bnest);
|
---|
[270] | 132 | if (arg != 0) {
|
---|
| 133 | /* must replace */
|
---|
[439] | 134 | iseq[i] = OP_GETUPVAR;
|
---|
| 135 | iseq[i+2] = arg >> 8;
|
---|
| 136 | iseq[i+3] = arg & 0xff;
|
---|
[270] | 137 | }
|
---|
| 138 | }
|
---|
| 139 | break;
|
---|
| 140 |
|
---|
| 141 | case OP_MOVE:
|
---|
[439] | 142 | a = PEEK_B(iseq+i+1);
|
---|
| 143 | b = PEEK_B(iseq+i+2);
|
---|
[270] | 144 | /* src part */
|
---|
[439] | 145 | if (potential_upvar_p(irep->lv, b, argc, irep->nlocals)) {
|
---|
| 146 | uint16_t arg = search_variable(mrb, irep->lv[b - 1].name, bnest);
|
---|
[270] | 147 | if (arg != 0) {
|
---|
| 148 | /* must replace */
|
---|
[439] | 149 | iseq[i] = insn = OP_GETUPVAR;
|
---|
| 150 | iseq[i+2] = arg >> 8;
|
---|
| 151 | iseq[i+3] = arg & 0xff;
|
---|
[270] | 152 | }
|
---|
| 153 | }
|
---|
| 154 | /* dst part */
|
---|
[439] | 155 | if (potential_upvar_p(irep->lv, a, argc, irep->nlocals)) {
|
---|
| 156 | uint16_t arg = search_variable(mrb, irep->lv[a - 1].name, bnest);
|
---|
[270] | 157 | if (arg != 0) {
|
---|
| 158 | /* must replace */
|
---|
[439] | 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;
|
---|
[270] | 163 | }
|
---|
| 164 | }
|
---|
| 165 | break;
|
---|
[331] | 166 |
|
---|
[439] | 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 | }
|
---|
[331] | 183 | }
|
---|
| 184 | break;
|
---|
[439] | 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;
|
---|
[270] | 218 | }
|
---|
[439] | 219 | i+=mrb_insn_size[insn];
|
---|
[270] | 220 | }
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[331] | 223 | void mrb_codedump_all(mrb_state*, struct RProc*);
|
---|
| 224 |
|
---|
[270] | 225 | static struct RProc*
|
---|
[439] | 226 | create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding, const char *file, mrb_int line)
|
---|
[270] | 227 | {
|
---|
| 228 | mrbc_context *cxt;
|
---|
| 229 | struct mrb_parser_state *p;
|
---|
| 230 | struct RProc *proc;
|
---|
| 231 | struct REnv *e;
|
---|
[439] | 232 | mrb_callinfo *ci; /* callinfo of eval caller */
|
---|
| 233 | struct RClass *target_class = NULL;
|
---|
| 234 | int bidx;
|
---|
[270] | 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);
|
---|
[439] | 241 | cxt->lineno = (uint16_t)line;
|
---|
[331] | 242 |
|
---|
| 243 | mrbc_filename(mrb, cxt, file ? file : "(eval)");
|
---|
[270] | 244 | cxt->capture_errors = TRUE;
|
---|
| 245 | cxt->no_optimize = TRUE;
|
---|
[439] | 246 | cxt->on_eval = TRUE;
|
---|
[270] | 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 */
|
---|
[331] | 257 | mrb_value str;
|
---|
| 258 |
|
---|
| 259 | if (file) {
|
---|
[439] | 260 | str = mrb_format(mrb, "file %s line %d: %s",
|
---|
| 261 | file,
|
---|
| 262 | p->error_buffer[0].lineno,
|
---|
| 263 | p->error_buffer[0].message);
|
---|
[331] | 264 | }
|
---|
| 265 | else {
|
---|
[439] | 266 | str = mrb_format(mrb, "line %d: %s",
|
---|
| 267 | p->error_buffer[0].lineno,
|
---|
| 268 | p->error_buffer[0].message);
|
---|
[331] | 269 | }
|
---|
[270] | 270 | mrb_parser_free(p);
|
---|
| 271 | mrbc_context_free(mrb, cxt);
|
---|
[331] | 272 | mrb_exc_raise(mrb, mrb_exc_new_str(mrb, E_SYNTAX_ERROR, str));
|
---|
[270] | 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 | }
|
---|
[439] | 282 | if (mrb->c->ci > mrb->c->cibase) {
|
---|
| 283 | ci = &mrb->c->ci[-1];
|
---|
[270] | 284 | }
|
---|
[439] | 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); */
|
---|
[270] | 316 |
|
---|
| 317 | mrb_parser_free(p);
|
---|
| 318 | mrbc_context_free(mrb, cxt);
|
---|
| 319 |
|
---|
| 320 | return proc;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | static mrb_value
|
---|
[331] | 324 | exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc)
|
---|
| 325 | {
|
---|
[439] | 326 | /* no argument passed from eval() */
|
---|
| 327 | mrb->c->ci->argc = 0;
|
---|
[331] | 328 | if (mrb->c->ci->acc < 0) {
|
---|
[439] | 329 | ptrdiff_t cioff = mrb->c->ci - mrb->c->cibase;
|
---|
| 330 | mrb_value ret = mrb_top_run(mrb, proc, self, 0);
|
---|
[331] | 331 | if (mrb->exc) {
|
---|
| 332 | mrb_exc_raise(mrb, mrb_obj_value(mrb->exc));
|
---|
| 333 | }
|
---|
[439] | 334 | mrb->c->ci = mrb->c->cibase + cioff;
|
---|
[331] | 335 | return ret;
|
---|
| 336 | }
|
---|
[439] | 337 | /* clear block */
|
---|
| 338 | mrb->c->stack[1] = mrb_nil_value();
|
---|
[331] | 339 | return mrb_exec_irep(mrb, self, proc);
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | static mrb_value
|
---|
[270] | 343 | f_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);
|
---|
[331] | 355 | mrb_assert(!MRB_PROC_CFUNC_P(proc));
|
---|
| 356 | return exec_irep(mrb, self, proc);
|
---|
[270] | 357 | }
|
---|
| 358 |
|
---|
| 359 | static mrb_value
|
---|
| 360 | f_instance_eval(mrb_state *mrb, mrb_value self)
|
---|
| 361 | {
|
---|
| 362 | mrb_value b;
|
---|
| 363 | mrb_int argc; mrb_value *argv;
|
---|
| 364 |
|
---|
[439] | 365 | mrb_get_args(mrb, "*!&", &argv, &argc, &b);
|
---|
[270] | 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;
|
---|
[331] | 373 | struct RProc *proc;
|
---|
[270] | 374 |
|
---|
| 375 | mrb_get_args(mrb, "s|zi", &s, &len, &file, &line);
|
---|
| 376 | cv = mrb_singleton_class(mrb, self);
|
---|
[331] | 377 | proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line);
|
---|
[439] | 378 | MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(cv));
|
---|
[331] | 379 | mrb_assert(!MRB_PROC_CFUNC_P(proc));
|
---|
[439] | 380 | mrb->c->ci->target_class = mrb_class_ptr(cv);
|
---|
[331] | 381 | return exec_irep(mrb, self, proc);
|
---|
[270] | 382 | }
|
---|
| 383 | else {
|
---|
| 384 | mrb_get_args(mrb, "&", &b);
|
---|
| 385 | return mrb_obj_instance_eval(mrb, self);
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | void
|
---|
| 390 | mrb_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));
|
---|
[439] | 393 | mrb_define_method(mrb, mrb_class_get(mrb, "BasicObject"), "instance_eval", f_instance_eval, MRB_ARGS_OPT(3)|MRB_ARGS_BLOCK());
|
---|
[270] | 394 | }
|
---|
| 395 |
|
---|
| 396 | void
|
---|
| 397 | mrb_mruby_eval_gem_final(mrb_state* mrb)
|
---|
| 398 | {
|
---|
| 399 | }
|
---|