diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-10-26 01:13:57 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-10-28 00:29:30 +0900 |
| commit | 93f5f225772c398be6e409da3d3ef0f07ffbe1cf (patch) | |
| tree | 37198a8c50baaf1a5714a581e049167073249ddc /src/kernel.c | |
| parent | 3f9d00ded3ce987927d975f7ce70637a973de1fc (diff) | |
| download | mruby-93f5f225772c398be6e409da3d3ef0f07ffbe1cf.tar.gz mruby-93f5f225772c398be6e409da3d3ef0f07ffbe1cf.zip | |
Heavily refactored how lexical scope links are implemented; fix #3821
Instead of `irep` links, we added a `upper` link to `struct RProc`.
To make a space for the `upper` link, we moved `target_class` reference.
If a `Proc` does not have `env`, `target_class` is saved in an `union`
shared with `env` (if a `Proc` has env, you can tell it by `MRB_PROC_ENV_P()).
Otherwise `target_class` is referenced from `env->c`. We removed links
in `env` as well.
This change removes 2 members from `mrb_irep` struct, thus saving 2
words per method/proc/block. This also fixes potential memory leaks
due to the circular references caused by a link from `mrb_irep`.
Diffstat (limited to 'src/kernel.c')
| -rw-r--r-- | src/kernel.c | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/src/kernel.c b/src/kernel.c index 33d142184..1e5a74222 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -136,6 +136,7 @@ mrb_f_block_given_p_m(mrb_state *mrb, mrb_value self) { mrb_callinfo *ci = mrb->c->ci; mrb_value *bp; + struct RProc *p; bp = ci->stackent + 1; ci--; @@ -143,24 +144,20 @@ mrb_f_block_given_p_m(mrb_state *mrb, mrb_value self) return mrb_false_value(); } /* block_given? called within block; check upper scope */ - if (ci->proc->env) { - struct REnv *e = ci->proc->env; - - while (e->c) { - e = (struct REnv*)e->c; - } + p = ci->proc; + while (p) { + if (MRB_PROC_SCOPE_P(p)) break; + p = p->upper; + } + /* top-level does not have block slot (always false) */ + if (p == NULL) return mrb_false_value(); + if (MRB_PROC_ENV_P(p)) { + struct REnv *e = MRB_PROC_ENV(p); /* top-level does not have block slot (always false) */ if (e->stack == mrb->c->stbase) return mrb_false_value(); - if (e->stack && e->cioff < 0) { - /* use saved block arg position */ - bp = &e->stack[-e->cioff]; - ci = 0; /* no callinfo available */ - } - else { - ci = e->cxt.c->cibase + e->cioff; - bp = ci[1].stackent + 1; - } + /* use saved block arg position */ + bp = &e->stack[MRB_ENV_BIDX(e)]; } if (ci && ci->argc > 0) { bp += ci->argc; @@ -655,7 +652,7 @@ method_entry_loop(mrb_state *mrb, struct RClass* klass, khash_t(st)* set) khint_t i; khash_t(mt) *h = klass->mt; - if (!h) return; + if (!h || kh_size(h) == 0) return; for (i=0;i<kh_end(h);i++) { if (kh_exist(h, i) && kh_value(h, i)) { kh_put(st, mrb, set, kh_key(h, i)); @@ -690,7 +687,7 @@ mrb_class_instance_method_list(mrb_state *mrb, mrb_bool recur, struct RClass* kl klass = klass->super; } - ary = mrb_ary_new(mrb); + ary = mrb_ary_new_capa(mrb, kh_size(set)); for (i=0;i<kh_end(set);i++) { if (kh_exist(set, i)) { mrb_ary_push(mrb, ary, mrb_symbol_value(kh_key(set, i))); @@ -1161,16 +1158,19 @@ mrb_local_variables(mrb_state *mrb, mrb_value self) return mrb_ary_new(mrb); } vars = mrb_hash_new(mrb); - irep = proc->body.irep; - while (irep) { + while (proc) { + if (MRB_PROC_CFUNC_P(proc)) break; + irep = proc->body.irep; if (!irep->lv) break; for (i = 0; i + 1 < irep->nlocals; ++i) { if (irep->lv[i].name) { mrb_hash_set(mrb, vars, mrb_symbol_value(irep->lv[i].name), mrb_true_value()); } } - if (!proc->env) break; - irep = irep->outer; + if (!MRB_PROC_ENV_P(proc)) break; + proc = proc->upper; + // if (MRB_PROC_SCOPE_P(proc)) break; + if (!proc->c) break; } return mrb_hash_keys(mrb, vars); |
