mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge branch 'dearblue-eval-patchfree'; close #5016
This commit is contained in:
@@ -33,7 +33,7 @@ typedef struct mrbc_context {
|
||||
mrb_bool no_exec:1;
|
||||
mrb_bool keep_lv:1;
|
||||
mrb_bool no_optimize:1;
|
||||
mrb_bool on_eval:1;
|
||||
struct RProc *upper;
|
||||
|
||||
size_t parser_nerr;
|
||||
} mrbc_context;
|
||||
@@ -151,8 +151,8 @@ struct mrb_parser_state {
|
||||
mrb_ast_node *tree;
|
||||
|
||||
mrb_bool no_optimize:1;
|
||||
mrb_bool on_eval:1;
|
||||
mrb_bool capture_errors:1;
|
||||
struct RProc *upper;
|
||||
struct mrb_parser_message error_buffer[10];
|
||||
struct mrb_parser_message warn_buffer[10];
|
||||
|
||||
|
||||
@@ -281,15 +281,6 @@ no_optimize(codegen_scope *s)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static
|
||||
mrb_bool
|
||||
on_eval(codegen_scope *s)
|
||||
{
|
||||
if (s && s->parser && s->parser->on_eval)
|
||||
return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
struct mrb_insn_data
|
||||
mrb_decode_insn(const mrb_code *pc)
|
||||
{
|
||||
@@ -407,9 +398,6 @@ gen_move(codegen_scope *s, uint16_t dst, uint16_t src, int nopeep)
|
||||
if (no_peephole(s)) {
|
||||
normal:
|
||||
genop_2(s, OP_MOVE, dst, src);
|
||||
if (on_eval(s)) {
|
||||
genop_0(s, OP_NOP);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else {
|
||||
@@ -674,6 +662,43 @@ lv_idx(codegen_scope *s, mrb_sym id)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
search_upvar(codegen_scope *s, mrb_sym id, int *idx)
|
||||
{
|
||||
struct RProc *u;
|
||||
int lv = 0;
|
||||
codegen_scope *up = s->prev;
|
||||
|
||||
while (up) {
|
||||
*idx = lv_idx(up, id);
|
||||
if (*idx > 0) {
|
||||
return lv;
|
||||
}
|
||||
lv ++;
|
||||
up = up->prev;
|
||||
}
|
||||
|
||||
if (lv < 1) lv = 1;
|
||||
u = s->parser->upper;
|
||||
while (u && !MRB_PROC_CFUNC_P(u)) {
|
||||
struct mrb_irep *ir = u->body.irep;
|
||||
uint_fast16_t n = ir->nlocals;
|
||||
const struct mrb_locals *v = ir->lv;
|
||||
for (; n > 1; n --, v ++) {
|
||||
if (v->name == id) {
|
||||
*idx = v->r;
|
||||
return lv - 1;
|
||||
}
|
||||
}
|
||||
if (MRB_PROC_SCOPE_P(u)) break;
|
||||
u = u->upper;
|
||||
lv ++;
|
||||
}
|
||||
|
||||
codegen_error(s, "Can't found local variables");
|
||||
return -1; /* not reached */
|
||||
}
|
||||
|
||||
static void
|
||||
for_body(codegen_scope *s, node *tree)
|
||||
{
|
||||
@@ -786,12 +811,19 @@ lambda_body(codegen_scope *s, node *tree, int blk)
|
||||
i = 0;
|
||||
while (opt) {
|
||||
int idx;
|
||||
mrb_sym id = nsym(opt->car->car);
|
||||
|
||||
dispatch(s, pos+i*3+1);
|
||||
codegen(s, opt->car->cdr, VAL);
|
||||
pop();
|
||||
idx = lv_idx(s, nsym(opt->car->car));
|
||||
gen_move(s, idx, cursp(), 0);
|
||||
idx = lv_idx(s, id);
|
||||
if (idx > 0) {
|
||||
gen_move(s, idx, cursp(), 0);
|
||||
}
|
||||
else {
|
||||
int lv = search_upvar(s, id, &idx);
|
||||
genop_3(s, OP_GETUPVAR, cursp(), idx, lv);
|
||||
}
|
||||
i++;
|
||||
opt = opt->cdr;
|
||||
}
|
||||
@@ -818,11 +850,19 @@ lambda_body(codegen_scope *s, node *tree, int blk)
|
||||
mrb_assert(nint(kwd->car) == NODE_KW_ARG);
|
||||
|
||||
if (def_arg) {
|
||||
int idx;
|
||||
genop_2(s, OP_KEY_P, lv_idx(s, kwd_sym), new_sym(s, kwd_sym));
|
||||
jmpif_key_p = genjmp2(s, OP_JMPIF, lv_idx(s, kwd_sym), 0, 0);
|
||||
codegen(s, def_arg, VAL);
|
||||
pop();
|
||||
gen_move(s, lv_idx(s, kwd_sym), cursp(), 0);
|
||||
idx = lv_idx(s, kwd_sym);
|
||||
if (idx > 0) {
|
||||
gen_move(s, idx, cursp(), 0);
|
||||
}
|
||||
else {
|
||||
int lv = search_upvar(s, kwd_sym, &idx);
|
||||
genop_3(s, OP_GETUPVAR, cursp(), idx, lv);
|
||||
}
|
||||
jmp_def_set = genjmp(s, OP_JMP, 0);
|
||||
dispatch(s, jmpif_key_p);
|
||||
}
|
||||
@@ -1103,23 +1143,12 @@ gen_assignment(codegen_scope *s, node *tree, int sp, int val)
|
||||
if (idx > 0) {
|
||||
if (idx != sp) {
|
||||
gen_move(s, idx, sp, val);
|
||||
if (val && on_eval(s)) genop_0(s, OP_NOP);
|
||||
}
|
||||
break;
|
||||
}
|
||||
else { /* upvar */
|
||||
int lv = 0;
|
||||
codegen_scope *up = s->prev;
|
||||
|
||||
while (up) {
|
||||
idx = lv_idx(up, nsym(tree));
|
||||
if (idx > 0) {
|
||||
genop_3(s, OP_SETUPVAR, sp, idx, lv);
|
||||
break;
|
||||
}
|
||||
lv++;
|
||||
up = up->prev;
|
||||
}
|
||||
int lv = search_upvar(s, nsym(tree), &idx);
|
||||
genop_3(s, OP_SETUPVAR, sp, idx, lv);
|
||||
}
|
||||
break;
|
||||
case NODE_NVAR:
|
||||
@@ -2324,21 +2353,10 @@ codegen(codegen_scope *s, node *tree, int val)
|
||||
|
||||
if (idx > 0) {
|
||||
gen_move(s, cursp(), idx, val);
|
||||
if (val && on_eval(s)) genop_0(s, OP_NOP);
|
||||
}
|
||||
else {
|
||||
int lv = 0;
|
||||
codegen_scope *up = s->prev;
|
||||
|
||||
while (up) {
|
||||
idx = lv_idx(up, nsym(tree));
|
||||
if (idx > 0) {
|
||||
genop_3(s, OP_GETUPVAR, cursp(), idx, lv);
|
||||
break;
|
||||
}
|
||||
lv++;
|
||||
up = up->prev;
|
||||
}
|
||||
int lv = search_upvar(s, nsym(tree), &idx);
|
||||
genop_3(s, OP_GETUPVAR, cursp(), idx, lv);
|
||||
}
|
||||
push();
|
||||
}
|
||||
@@ -2349,7 +2367,6 @@ codegen(codegen_scope *s, node *tree, int val)
|
||||
int idx = nint(tree);
|
||||
|
||||
gen_move(s, cursp(), idx, val);
|
||||
if (val && on_eval(s)) genop_0(s, OP_NOP);
|
||||
|
||||
push();
|
||||
}
|
||||
|
||||
@@ -265,6 +265,7 @@ local_unnest(parser_state *p)
|
||||
static mrb_bool
|
||||
local_var_p(parser_state *p, mrb_sym sym)
|
||||
{
|
||||
struct RProc *u;
|
||||
node *l = p->locals;
|
||||
|
||||
while (l) {
|
||||
@@ -275,6 +276,18 @@ local_var_p(parser_state *p, mrb_sym sym)
|
||||
}
|
||||
l = l->cdr;
|
||||
}
|
||||
|
||||
u = p->upper;
|
||||
while (u && !MRB_PROC_CFUNC_P(u)) {
|
||||
struct mrb_irep *ir = u->body.irep;
|
||||
uint_fast16_t n = ir->nlocals;
|
||||
const struct mrb_locals *v = ir->lv;
|
||||
for (; n > 1; n --, v ++) {
|
||||
if (v->name == sym) return TRUE;
|
||||
}
|
||||
if (MRB_PROC_SCOPE_P(u)) break;
|
||||
u = u->upper;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
@@ -6192,7 +6205,7 @@ parser_init_cxt(parser_state *p, mrbc_context *cxt)
|
||||
}
|
||||
p->capture_errors = cxt->capture_errors;
|
||||
p->no_optimize = cxt->no_optimize;
|
||||
p->on_eval = cxt->on_eval;
|
||||
p->upper = cxt->upper;
|
||||
if (cxt->partial_hook) {
|
||||
p->cxt = cxt;
|
||||
}
|
||||
|
||||
+1312
-1693
File diff suppressed because it is too large
Load Diff
@@ -9,217 +9,6 @@
|
||||
mrb_value mrb_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p);
|
||||
mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self);
|
||||
|
||||
static struct mrb_irep *
|
||||
get_closure_irep(mrb_state *mrb, int level)
|
||||
{
|
||||
struct RProc *proc = mrb->c->ci[-1].proc;
|
||||
|
||||
while (level--) {
|
||||
if (!proc) return NULL;
|
||||
proc = proc->upper;
|
||||
}
|
||||
if (!proc) return NULL;
|
||||
if (MRB_PROC_CFUNC_P(proc)) {
|
||||
return NULL;
|
||||
}
|
||||
return proc->body.irep;
|
||||
}
|
||||
|
||||
/* search for irep lev above the bottom */
|
||||
static mrb_irep*
|
||||
search_irep(mrb_irep *top, int bnest, int lev, mrb_irep *bottom)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i=0; i<top->rlen; i++) {
|
||||
mrb_irep* tmp = top->reps[i];
|
||||
|
||||
if (tmp == bottom) return top;
|
||||
tmp = search_irep(tmp, bnest-1, lev, bottom);
|
||||
if (tmp) {
|
||||
if (bnest == lev) return top;
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static uint16_t
|
||||
search_variable(mrb_state *mrb, mrb_sym vsym, int bnest)
|
||||
{
|
||||
mrb_irep *virep;
|
||||
int level;
|
||||
int pos;
|
||||
|
||||
for (level = 0; (virep = get_closure_irep(mrb, level)); level++) {
|
||||
if (virep->lv == NULL) {
|
||||
continue;
|
||||
}
|
||||
for (pos = 0; pos < virep->nlocals - 1; pos++) {
|
||||
if (vsym == virep->lv[pos].name) {
|
||||
return (pos+1)<<8 | (level+bnest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
irep_argc(mrb_irep *irep)
|
||||
{
|
||||
mrb_code c;
|
||||
|
||||
c = irep->iseq[0];
|
||||
if (c == OP_ENTER) {
|
||||
mrb_aspec ax = PEEK_W(irep->iseq+1);
|
||||
/* extra 1 means a slot for block */
|
||||
return MRB_ASPEC_REQ(ax)+MRB_ASPEC_OPT(ax)+MRB_ASPEC_REST(ax)+MRB_ASPEC_POST(ax)+1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static mrb_bool
|
||||
potential_upvar_p(struct mrb_locals *lv, uint16_t v, int argc, uint16_t nlocals)
|
||||
{
|
||||
if (v >= nlocals) return FALSE;
|
||||
/* skip arguments */
|
||||
if (v < argc+1) return FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
extern uint8_t mrb_insn_size[];
|
||||
extern uint8_t mrb_insn_size1[];
|
||||
extern uint8_t mrb_insn_size2[];
|
||||
extern uint8_t mrb_insn_size3[];
|
||||
|
||||
static void
|
||||
patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest, mrb_irep *top)
|
||||
{
|
||||
int i;
|
||||
uint32_t a;
|
||||
uint16_t b;
|
||||
uint8_t c;
|
||||
mrb_code insn;
|
||||
int argc = irep_argc(irep);
|
||||
mrb_code *iseq = (mrb_code *)irep->iseq;
|
||||
|
||||
mrb_assert((irep->flags & MRB_ISEQ_NO_FREE) == 0);
|
||||
|
||||
for (i = 0; i < irep->ilen; ) {
|
||||
insn = iseq[i];
|
||||
switch(insn){
|
||||
case OP_EPUSH:
|
||||
a = PEEK_B(iseq+i+1);
|
||||
patch_irep(mrb, irep->reps[a], bnest + 1, top);
|
||||
break;
|
||||
|
||||
case OP_LAMBDA:
|
||||
case OP_BLOCK:
|
||||
a = PEEK_B(iseq+i+1);
|
||||
b = PEEK_B(iseq+i+2);
|
||||
patch_irep(mrb, irep->reps[b], bnest + 1, top);
|
||||
break;
|
||||
|
||||
case OP_SEND:
|
||||
b = PEEK_B(iseq+i+2);
|
||||
c = PEEK_B(iseq+i+3);
|
||||
if (c != 0) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
uint16_t arg = search_variable(mrb, irep->syms[b], bnest);
|
||||
if (arg != 0) {
|
||||
/* must replace */
|
||||
iseq[i] = OP_GETUPVAR;
|
||||
iseq[i+2] = arg >> 8;
|
||||
iseq[i+3] = arg & 0xff;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_MOVE:
|
||||
a = PEEK_B(iseq+i+1);
|
||||
b = PEEK_B(iseq+i+2);
|
||||
/* src part */
|
||||
if (potential_upvar_p(irep->lv, b, argc, irep->nlocals)) {
|
||||
uint16_t arg = search_variable(mrb, irep->lv[b - 1].name, bnest);
|
||||
if (arg != 0) {
|
||||
/* must replace */
|
||||
iseq[i] = insn = OP_GETUPVAR;
|
||||
iseq[i+2] = arg >> 8;
|
||||
iseq[i+3] = arg & 0xff;
|
||||
}
|
||||
}
|
||||
/* dst part */
|
||||
if (potential_upvar_p(irep->lv, a, argc, irep->nlocals)) {
|
||||
uint16_t arg = search_variable(mrb, irep->lv[a - 1].name, bnest);
|
||||
if (arg != 0) {
|
||||
/* must replace */
|
||||
iseq[i] = insn = OP_SETUPVAR;
|
||||
iseq[i+1] = (mrb_code)b;
|
||||
iseq[i+2] = arg >> 8;
|
||||
iseq[i+3] = arg & 0xff;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_GETUPVAR:
|
||||
a = PEEK_B(iseq+i+1);
|
||||
b = PEEK_B(iseq+i+2);
|
||||
c = PEEK_B(iseq+i+3);
|
||||
{
|
||||
int lev = c+1;
|
||||
mrb_irep *tmp = search_irep(top, bnest, lev, irep);
|
||||
if (potential_upvar_p(tmp->lv, b, irep_argc(tmp), tmp->nlocals)) {
|
||||
uint16_t arg = search_variable(mrb, tmp->lv[b-1].name, bnest);
|
||||
if (arg != 0) {
|
||||
/* must replace */
|
||||
iseq[i] = OP_GETUPVAR;
|
||||
iseq[i+2] = arg >> 8;
|
||||
iseq[i+3] = arg & 0xff;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_SETUPVAR:
|
||||
a = PEEK_B(iseq+i+1);
|
||||
b = PEEK_B(iseq+i+2);
|
||||
c = PEEK_B(iseq+i+3);
|
||||
{
|
||||
int lev = c+1;
|
||||
mrb_irep *tmp = search_irep(top, bnest, lev, irep);
|
||||
if (potential_upvar_p(tmp->lv, b, irep_argc(tmp), tmp->nlocals)) {
|
||||
uint16_t arg = search_variable(mrb, tmp->lv[b-1].name, bnest);
|
||||
if (arg != 0) {
|
||||
/* must replace */
|
||||
iseq[i] = OP_SETUPVAR;
|
||||
iseq[i+1] = a;
|
||||
iseq[i+2] = arg >> 8;
|
||||
iseq[i+3] = arg & 0xff;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_EXT1:
|
||||
insn = PEEK_B(iseq+i+1);
|
||||
i += mrb_insn_size1[insn]+1;
|
||||
continue;
|
||||
case OP_EXT2:
|
||||
insn = PEEK_B(iseq+i+1);
|
||||
i += mrb_insn_size2[insn]+1;
|
||||
continue;
|
||||
case OP_EXT3:
|
||||
insn = PEEK_B(iseq+i+1);
|
||||
i += mrb_insn_size3[insn]+1;
|
||||
continue;
|
||||
}
|
||||
i+=mrb_insn_size[insn];
|
||||
}
|
||||
}
|
||||
|
||||
void mrb_codedump_all(mrb_state*, struct RProc*);
|
||||
|
||||
static struct RProc*
|
||||
@@ -243,7 +32,8 @@ create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding,
|
||||
mrbc_filename(mrb, cxt, file ? file : "(eval)");
|
||||
cxt->capture_errors = TRUE;
|
||||
cxt->no_optimize = TRUE;
|
||||
cxt->on_eval = TRUE;
|
||||
ci = (mrb->c->ci > mrb->c->cibase) ? mrb->c->ci - 1 : mrb->c->cibase;
|
||||
cxt->upper = ci->proc && MRB_PROC_CFUNC_P(ci->proc) ? NULL : ci->proc;
|
||||
|
||||
p = mrb_parse_nstring(mrb, s, len, cxt);
|
||||
|
||||
@@ -311,7 +101,6 @@ create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding,
|
||||
}
|
||||
proc->upper = ci->proc;
|
||||
mrb->c->ci->target_class = target_class;
|
||||
patch_irep(mrb, proc->body.irep, 0, proc->body.irep);
|
||||
/* mrb_codedump_all(mrb, proc); */
|
||||
|
||||
mrb_parser_free(p);
|
||||
|
||||
@@ -130,3 +130,24 @@ Proc.new { foo }
|
||||
EOS
|
||||
}
|
||||
end
|
||||
|
||||
assert('Calling the same method as the variable name') do
|
||||
hoge = Object.new
|
||||
def hoge.fuga
|
||||
"Hit!"
|
||||
end
|
||||
assert_equal("Hit!") { fuga = "Miss!"; eval "hoge.fuga" }
|
||||
assert_equal("Hit!") { fuga = "Miss!"; -> { eval "hoge.fuga" }.call }
|
||||
assert_equal("Hit!") { -> { fuga = "Miss!"; eval "hoge.fuga" }.call }
|
||||
assert_equal("Hit!") { fuga = "Miss!"; eval("-> { hoge.fuga }").call }
|
||||
end
|
||||
|
||||
assert('Access numbered parameter from eval') do
|
||||
hoge = Object.new
|
||||
def hoge.fuga(a, &b)
|
||||
b.call(a)
|
||||
end
|
||||
assert_equal(6) {
|
||||
hoge.fuga(3) { _1 + eval("_1") }
|
||||
}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user