Unified `target_class` and `env` of `mrb_callinfo`
If there is `env`, `env->c` means `target_class`.
MODIFY
include/mruby.h
include/mruby.h
@@ -152,12 +152,14 @@ typedef struct {
mrb_sym mid;
const struct RProc *proc;
mrb_value *stackent;
- struct REnv *env;
const mrb_code *pc; /* return address */
const mrb_code *err; /* error position */
int16_t argc;
int16_t acc;
+ union {
+ struct REnv *env;
struct RClass *target_class;
+ } u;
} mrb_callinfo;
enum mrb_fiber_state {

MODIFY
include/mruby/proc.h
include/mruby/proc.h
@@ -136,6 +136,67 @@ MRB_API mrb_value mrb_proc_cfunc_env_get(mrb_state *mrb, mrb_int idx);
MRB_API mrb_value mrb_load_proc(mrb_state *mrb, const struct RProc *proc);
+static inline struct RClass *
+mrb_vm_ci_target_class(const mrb_callinfo *ci)
+{
+ if (ci->u.env && ci->u.env->tt == MRB_TT_ENV) {
+ return ci->u.env->c;
+ }
+ else {
+ return ci->u.target_class;
+ }
+}
+
+static inline void
+mrb_vm_ci_target_class_set(mrb_callinfo *ci, struct RClass *tc)
+{
+ struct REnv *e = ci->u.env;
+ if (e) {
+ if (e->tt == MRB_TT_ENV) {
+ e->c = tc;
+ }
+ else {
+ ci->u.target_class = tc;
+ }
+ }
+}
+
+static inline struct REnv *
+mrb_vm_ci_env(const mrb_callinfo *ci)
+{
+ if (ci->u.env && ci->u.env->tt == MRB_TT_ENV) {
+ return ci->u.env;
+ }
+ else {
+ return NULL;
+ }
+}
+
+static inline void
+mrb_vm_ci_env_set(mrb_callinfo *ci, struct REnv *e)
+{
+ if (ci->u.env) {
+ if (ci->u.env->tt == MRB_TT_ENV) {
+ if (e) {
+ e->c = ci->u.env->c;
+ ci->u.env = e;
+ }
+ else {
+ ci->u.target_class = ci->u.env->c;
+ }
+ }
+ else {
+ if (e) {
+ e->c = ci->u.target_class;
+ ci->u.env = e;
+ }
+ }
+ }
+ else {
+ ci->u.env = e;
+ }
+}
+
MRB_END_DECL
#endif /* MRUBY_PROC_H */

MODIFY
lib/mruby/gem.rb
lib/mruby/gem.rb
@@ -218,8 +218,8 @@ module MRuby
f.puts %Q[ mrb_close(mrb);]
f.puts %Q[ exit(EXIT_FAILURE);]
f.puts %Q[ }]
- f.puts %Q[ struct REnv *e = mrb->c->cibase->env;]
- f.puts %Q[ mrb->c->cibase->env = NULL;]
+ f.puts %Q[ struct REnv *e = mrb_vm_ci_env(mrb->c->cibase);]
+ f.puts %Q[ mrb_vm_ci_env_set(mrb->c->cibase, NULL);]
f.puts %Q[ mrb_env_unshare(mrb, e);]
end
f.puts %Q[ mrb_gc_arena_restore(mrb, ai);]

MODIFY
mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
@@ -516,8 +516,8 @@ main(int argc, char **argv)
}
mrb_load_file_cxt(mrb, lfp, cxt);
fclose(lfp);
- e = mrb->c->cibase->env;
- mrb->c->cibase->env = NULL;
+ e = mrb_vm_ci_env(mrb->c->cibase);
+ mrb_vm_ci_env_set(mrb->c->cibase, NULL);
mrb_env_unshare(mrb, e);
mrbc_cleanup_local_variables(mrb, cxt);
}
@@ -658,8 +658,8 @@ main(int argc, char **argv)
mrb_codedump_all(mrb, proc);
}
/* adjust stack length of toplevel environment */
- if (mrb->c->cibase->env) {
- struct REnv *e = mrb->c->cibase->env;
+ if (mrb->c->cibase->u.env) {
+ struct REnv *e = mrb_vm_ci_env(mrb->c->cibase);
if (e && MRB_ENV_LEN(e) < proc->body.irep->nlocals) {
MRB_ENV_SET_LEN(e, proc->body.irep->nlocals);
}

MODIFY
mrbgems/mruby-bin-mruby/tools/mruby/mruby.c
mrbgems/mruby-bin-mruby/tools/mruby/mruby.c
@@ -333,8 +333,8 @@ main(int argc, char **argv)
v = mrb_load_detect_file_cxt(mrb, lfp, c);
}
fclose(lfp);
- e = mrb->c->cibase->env;
- mrb->c->cibase->env = NULL;
+ e = mrb_vm_ci_env(mrb->c->cibase);
+ mrb_vm_ci_env_set(mrb->c->cibase, NULL);
mrb_env_unshare(mrb, e);
mrbc_cleanup_local_variables(mrb, c);
}

MODIFY
mrbgems/mruby-class-ext/src/class.c
mrbgems/mruby-class-ext/src/class.c
#include "mruby.h"
#include "mruby/class.h"
#include "mruby/string.h"
+#include "mruby/proc.h"
static mrb_value
mrb_mod_name(mrb_state *mrb, mrb_value self)
@@ -51,7 +52,7 @@ mrb_mod_module_exec(mrb_state *mrb, mrb_value self)
if (mrb->c->ci->acc < 0) {
return mrb_yield_with_class(mrb, blk, argc, argv, self, c);
}
- mrb->c->ci->target_class = c;
+ mrb_vm_ci_target_class_set(mrb->c->ci, c);
return mrb_yield_cont(mrb, blk, self, argc, argv);
}

MODIFY
mrbgems/mruby-compiler/core/parse.y
mrbgems/mruby-compiler/core/parse.y
@@ -6783,7 +6783,7 @@ mrb_load_exec(mrb_state *mrb, struct mrb_parser_state *p, mrbc_context *c)
}
MRB_PROC_SET_TARGET_CLASS(proc, target);
if (mrb->c->ci) {
- mrb->c->ci->target_class = target;
+ mrb_vm_ci_target_class_set(mrb->c->ci, target);
}
v = mrb_top_run(mrb, proc, mrb_top_self(mrb), keep);
if (mrb->exc) return mrb_nil_value();

MODIFY
mrbgems/mruby-compiler/core/y.tab.c
mrbgems/mruby-compiler/core/y.tab.c
@@ -12784,7 +12784,7 @@ mrb_load_exec(mrb_state *mrb, struct mrb_parser_state *p, mrbc_context *c)
}
MRB_PROC_SET_TARGET_CLASS(proc, target);
if (mrb->c->ci) {
- mrb->c->ci->target_class = target;
+ mrb_vm_ci_target_class_set(mrb->c->ci, target);
}
v = mrb_top_run(mrb, proc, mrb_top_self(mrb), keep);
if (mrb->exc) return mrb_nil_value();

MODIFY
mrbgems/mruby-eval/src/eval.c
mrbgems/mruby-eval/src/eval.c
@@ -79,19 +79,19 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi
target_class = MRB_PROC_TARGET_CLASS(ci->proc);
}
if (ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) {
- if (ci->env) {
- e = ci->env;
+ if ((e = mrb_vm_ci_env(ci)) != NULL) {
+ /* do nothing, because e is assigned already */
}
else {
e = mrb_env_new(mrb, mrb->c, ci, ci->proc->body.irep->nlocals, ci[1].stackent, target_class);
- ci->env = e;
+ ci->u.env = e;
}
proc->e.env = e;
proc->flags |= MRB_PROC_ENVSET;
mrb_field_write_barrier(mrb, (struct RBasic*)proc, (struct RBasic*)e);
}
proc->upper = ci->proc;
- mrb->c->ci->target_class = target_class;
+ mrb_vm_ci_target_class_set(mrb->c->ci, target_class);
/* mrb_codedump_all(mrb, proc); */
mrb_parser_free(p);
@@ -157,7 +157,7 @@ f_instance_eval(mrb_state *mrb, mrb_value self)
proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line);
MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(cv));
mrb_assert(!MRB_PROC_CFUNC_P(proc));
- mrb->c->ci->target_class = mrb_class_ptr(cv);
+ mrb_vm_ci_target_class_set(mrb->c->ci, mrb_class_ptr(cv));
return exec_irep(mrb, self, proc);
}
else {

MODIFY
mrbgems/mruby-fiber/src/fiber.c
mrbgems/mruby-fiber/src/fiber.c
@@ -120,7 +120,7 @@ fiber_init(mrb_state *mrb, mrb_value self)
/* adjust return callinfo */
ci = c->ci;
- ci->target_class = MRB_PROC_TARGET_CLASS(p);
+ mrb_vm_ci_target_class_set(ci, MRB_PROC_TARGET_CLASS(p));
ci->proc = p;
mrb_field_write_barrier(mrb, (struct RBasic*)mrb_obj_ptr(self), (struct RBasic*)p);
ci->pc = p->body.irep->iseq;
@@ -154,7 +154,7 @@ fiber_result(mrb_state *mrb, const mrb_value *a, mrb_int len)
}
/* mark return from context modifying method */
-#define MARK_CONTEXT_MODIFY(c) (c)->ci->target_class = NULL
+#define MARK_CONTEXT_MODIFY(c) (c)->ci->u.target_class = NULL
static void
fiber_check_cfunc(mrb_state *mrb, struct mrb_context *c)

MODIFY
mrbgems/mruby-object-ext/src/object.c
mrbgems/mruby-object-ext/src/object.c
@@ -105,7 +105,7 @@ mrb_obj_instance_exec(mrb_state *mrb, mrb_value self)
if (mrb->c->ci->acc < 0) {
return mrb_yield_with_class(mrb, blk, argc, argv, self, c);
}
- mrb->c->ci->target_class = c;
+ mrb_vm_ci_target_class_set(mrb->c->ci, c);
return mrb_yield_cont(mrb, blk, self, argc, argv);
}

MODIFY
src/gc.c
src/gc.c
@@ -660,9 +660,8 @@ mark_context(mrb_state *mrb, struct mrb_context *c)
/* mark call stack */
if (c->cibase) {
for (ci = c->cibase; ci <= c->ci; ci++) {
- mrb_gc_mark(mrb, (struct RBasic*)ci->env);
mrb_gc_mark(mrb, (struct RBasic*)ci->proc);
- mrb_gc_mark(mrb, (struct RBasic*)ci->target_class);
+ mrb_gc_mark(mrb, (struct RBasic*)ci->u.target_class);
}
}
/* mark fibers */
@@ -840,7 +839,7 @@ obj_free(mrb_state *mrb, struct RBasic *obj, int end)
mrb_callinfo *ce = c->cibase;
while (ce <= ci) {
- struct REnv *e = ci->env;
+ struct REnv *e = ci->u.env;
if (e && !mrb_object_dead_p(mrb, (struct RBasic*)e) &&
e->tt == MRB_TT_ENV && MRB_ENV_ONSTACK_P(e)) {
mrb_env_unshare(mrb, e);

MODIFY
src/kernel.c
src/kernel.c
@@ -176,8 +176,7 @@ mrb_f_block_given_p_m(mrb_state *mrb, mrb_value self)
if (bidx < 0) return mrb_false_value();
bp = &e->stack[bidx];
}
- else if (ci->env) {
- e = ci->env;
+ else if ((e = mrb_vm_ci_env(ci)) != NULL) {
/* top-level does not have block slot (always false) */
if (e->stack == mrb->c->stbase) return mrb_false_value();
bidx = env_bidx(e);

MODIFY
src/proc.c
src/proc.c
@@ -46,7 +46,7 @@ mrb_proc_new(mrb_state *mrb, const mrb_irep *irep)
tc = MRB_PROC_TARGET_CLASS(ci->proc);
}
if (tc == NULL) {
- tc = ci->target_class;
+ tc = mrb_vm_ci_target_class(ci);
}
p->upper = ci->proc;
p->e.target_class = tc;
@@ -83,14 +83,14 @@ closure_setup(mrb_state *mrb, struct RProc *p)
const struct RProc *up = p->upper;
struct REnv *e = NULL;
- if (ci && ci->env) {
- e = ci->env;
+ if (ci && (e = mrb_vm_ci_env(ci)) != NULL) {
+ /* do nothing, because e is assigned already */
}
else if (up) {
struct RClass *tc = MRB_PROC_TARGET_CLASS(p);
e = mrb_env_new(mrb, mrb->c, ci, up->body.irep->nlocals, mrb->c->stack, tc);
- ci->env = e;
+ ci->u.env = e;
if (MRB_PROC_ENV_P(up) && MRB_PROC_ENV(up)->cxt == NULL) {
e->mid = MRB_PROC_ENV(up)->mid;
}
@@ -211,7 +211,7 @@ mrb_proc_s_new(mrb_state *mrb, mrb_value proc_class)
proc = mrb_obj_value(p);
mrb_funcall_with_block(mrb, proc, MRB_SYM(initialize), 0, NULL, proc);
if (!MRB_PROC_STRICT_P(p) &&
- mrb->c->ci > mrb->c->cibase && MRB_PROC_ENV(p) == mrb->c->ci[-1].env) {
+ mrb->c->ci > mrb->c->cibase && MRB_PROC_ENV(p) == mrb->c->ci[-1].u.env) {
p->flags |= MRB_PROC_ORPHAN;
}
return proc;

MODIFY
src/vm.c
src/vm.c
@@ -133,7 +133,7 @@ stack_init(mrb_state *mrb)
c->cibase = (mrb_callinfo *)mrb_calloc(mrb, CALLINFO_INIT_SIZE, sizeof(mrb_callinfo));
c->ciend = c->cibase + CALLINFO_INIT_SIZE;
c->ci = c->cibase;
- c->ci->target_class = mrb->object_class;
+ c->ci->u.target_class = mrb->object_class;
c->ci->stackent = c->stack;
}
@@ -144,7 +144,7 @@ envadjust(mrb_state *mrb, mrb_value *oldbase, mrb_value *newbase, size_t oldsize
if (newbase == oldbase) return;
while (ci <= mrb->c->ci) {
- struct REnv *e = ci->env;
+ struct REnv *e = mrb_vm_ci_env(ci);
mrb_value *st;
if (e && MRB_ENV_ONSTACK_P(e) &&
@@ -154,7 +154,7 @@ envadjust(mrb_state *mrb, mrb_value *oldbase, mrb_value *newbase, size_t oldsize
e->stack = newbase + off;
}
- if (ci->proc && MRB_PROC_ENV_P(ci->proc) && ci->env != MRB_PROC_ENV(ci->proc)) {
+ if (ci->proc && MRB_PROC_ENV_P(ci->proc) && e != MRB_PROC_ENV(ci->proc)) {
e = MRB_PROC_ENV(ci->proc);
if (e && MRB_ENV_ONSTACK_P(e) &&
@@ -240,7 +240,7 @@ uvenv(mrb_state *mrb, mrb_int up)
while (cb <= ci) {
if (ci->proc == proc) {
- return ci->env;
+ return mrb_vm_ci_env(ci);
}
ci--;
}
@@ -284,9 +284,8 @@ cipush(mrb_state *mrb, const mrb_code *pc, mrb_int push_stacks, mrb_int acc,
ci->pc = pc;
ci->argc = argc;
ci->acc = acc;
- ci->target_class = target_class;
+ ci->u.target_class = target_class;
ci->err = 0;
- ci->env = 0;
c->stack += push_stacks;
return ci;
@@ -302,7 +301,7 @@ mrb_env_unshare(mrb_state *mrb, struct REnv *e)
if (!MRB_ENV_ONSTACK_P(e)) return;
if (e->cxt != mrb->c) return;
- if (e == mrb->c->cibase->env) return; /* for mirb */
+ if (e == mrb_vm_ci_env(mrb->c->cibase)) return; /* for mirb */
p = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value)*len);
if (len > 0) {
stack_copy(p, e->stack, len);
@@ -317,7 +316,7 @@ static inline mrb_callinfo*
cipop(mrb_state *mrb)
{
struct mrb_context *c = mrb->c;
- struct REnv *env = c->ci->env;
+ struct REnv *env = mrb_vm_ci_env(c->ci);
mrb->c->stack = c->ci->stackent;
c->ci--;
@@ -573,7 +572,7 @@ mrb_f_send(mrb_state *mrb, mrb_value self)
}
ci->mid = name;
- ci->target_class = c;
+ ci->u.target_class = c;
regs = mrb->c->stack+1;
/* remove first symbol from arguments */
if (ci->argc >= 0) {
@@ -609,7 +608,7 @@ eval_under(mrb_state *mrb, mrb_value self, mrb_value blk, struct RClass *c)
if (ci->acc == CI_ACC_DIRECT) {
return mrb_yield_with_class(mrb, blk, 1, &self, self, c);
}
- ci->target_class = c;
+ ci->u.target_class = c;
p = mrb_proc_ptr(blk);
ci->proc = p;
ci->argc = 1;
@@ -1005,7 +1004,7 @@ mrb_vm_run(mrb_state *mrb, const struct RProc *proc, mrb_value self, mrb_int sta
static mrb_bool
check_target_class(mrb_state *mrb)
{
- if (!mrb->c->ci->target_class) {
+ if (!mrb_vm_ci_target_class(mrb->c->ci)) {
mrb_value exc = mrb_exc_new_lit(mrb, E_TYPE_ERROR, "no target class or module");
mrb_exc_set(mrb, exc);
return FALSE;
@@ -1476,11 +1475,11 @@ RETRY_TRY_BLOCK:
ci = mrb->c->ci;
if (mrb_proc_p(blk)) {
struct RProc *p = mrb_proc_ptr(blk);
- if (p && !MRB_PROC_STRICT_P(p) && MRB_PROC_ENV(p) == ci[-1].env) {
+ if (p && !MRB_PROC_STRICT_P(p) && MRB_PROC_ENV(p) == mrb_vm_ci_env(&ci[-1])) {
p->flags |= MRB_PROC_ORPHAN;
}
}
- if (!ci->target_class) { /* return from context modifying method (resume/yield) */
+ if (!ci->u.target_class) { /* return from context modifying method (resume/yield) */
if (ci->acc == CI_ACC_RESUMED) {
mrb->jmp = prev_jmp;
return recv;
@@ -1518,7 +1517,7 @@ RETRY_TRY_BLOCK:
/* replace callinfo */
ci = mrb->c->ci;
- ci->target_class = MRB_PROC_TARGET_CLASS(m);
+ ci->u.target_class = MRB_PROC_TARGET_CLASS(m);
ci->proc = m;
if (MRB_PROC_ENV_P(m)) {
ci->mid = MRB_PROC_ENV(m)->mid;
@@ -1591,10 +1590,10 @@ RETRY_TRY_BLOCK:
goto L_RAISE;
}
if (target_class->flags & MRB_FL_CLASS_IS_PREPENDED) {
- target_class = ci->target_class;
+ target_class = mrb_vm_ci_target_class(ci);
}
else if (target_class->tt == MRB_TT_MODULE) {
- target_class = ci->target_class;
+ target_class = mrb_vm_ci_target_class(ci);
if (target_class->tt != MRB_TT_ICLASS) {
mrb_value exc = mrb_exc_new_lit(mrb, E_RUNTIME_ERROR, "superclass info lost [mruby limitations]");
mrb_exc_set(mrb, exc);
@@ -1659,7 +1658,7 @@ RETRY_TRY_BLOCK:
if (mrb->exc) goto L_RAISE;
ci = mrb->c->ci;
mrb_assert(!mrb_break_p(v));
- if (!ci->target_class) { /* return from context modifying method (resume/yield) */
+ if (!mrb_vm_ci_target_class(ci)) { /* return from context modifying method (resume/yield) */
if (ci->acc == CI_ACC_RESUMED) {
mrb->jmp = prev_jmp;
return v;
@@ -1700,7 +1699,7 @@ RETRY_TRY_BLOCK:
mrb_int lv = (b>>0)&0xf;
mrb_value *stack;
- if (mrb->c->ci->mid == 0 || mrb->c->ci->target_class == NULL) {
+ if (mrb->c->ci->mid == 0 || mrb_vm_ci_target_class(mrb->c->ci) == NULL) {
mrb_value exc;
L_NOSUPER:
@@ -1951,7 +1950,7 @@ RETRY_TRY_BLOCK:
struct RProc *p = mrb_proc_ptr(blk);
if (!MRB_PROC_STRICT_P(p) &&
- ci > mrb->c->cibase && MRB_PROC_ENV(p) == ci[-1].env) {
+ ci > mrb->c->cibase && MRB_PROC_ENV(p) == mrb_vm_ci_env(&ci[-1])) {
p->flags |= MRB_PROC_ORPHAN;
}
}
@@ -2203,7 +2202,7 @@ RETRY_TRY_BLOCK:
mrb_assert(ci == mrb->c->ci);
mrb_assert(mrb->exc == NULL);
- if (mrb->c->vmexec && !ci->target_class) {
+ if (mrb->c->vmexec && !mrb_vm_ci_target_class(ci)) {
mrb_gc_arena_restore(mrb, ai);
mrb->c->vmexec = FALSE;
mrb->jmp = prev_jmp;
@@ -2780,7 +2779,7 @@ RETRY_TRY_BLOCK:
CASE(OP_TCLASS, B) {
if (!check_target_class(mrb)) goto L_RAISE;
- regs[a] = mrb_obj_value(mrb->c->ci->target_class);
+ regs[a] = mrb_obj_value(mrb_vm_ci_target_class(mrb->c->ci));
NEXT;
}
@@ -2788,7 +2787,7 @@ RETRY_TRY_BLOCK:
struct RClass *target;
if (!check_target_class(mrb)) goto L_RAISE;
- target = mrb->c->ci->target_class;
+ target = mrb_vm_ci_target_class(mrb->c->ci);
mrb_alias_method(mrb, target, syms[a], syms[b]);
NEXT;
}
@@ -2796,7 +2795,7 @@ RETRY_TRY_BLOCK:
struct RClass *target;
if (!check_target_class(mrb)) goto L_RAISE;
- target = mrb->c->ci->target_class;
+ target = mrb_vm_ci_target_class(mrb->c->ci);
mrb_undef_method_id(mrb, target, syms[a]);
NEXT;
}
@@ -2884,7 +2883,7 @@ mrb_top_run(mrb_state *mrb, const struct RProc *proc, mrb_value self, mrb_int st
return mrb_vm_run(mrb, proc, self, stack_keep);
}
if (mrb->c->ci == mrb->c->cibase) {
- mrb->c->ci->env = NULL;
+ mrb_vm_ci_env_set(mrb->c->ci, NULL);
return mrb_vm_run(mrb, proc, self, stack_keep);
}
cipush(mrb, NULL, 0, CI_ACC_SKIP, mrb->object_class, NULL, 0, 0);

Unified `target_class` and `env` of `mrb_callinfo`

If there is `env`, `env->c` means `target_class`.