Change flag names in preparation of `REnv` refactoring.
MODIFY
include/mruby/proc.h
include/mruby/proc.h
@@ -22,14 +22,19 @@ struct REnv {
mrb_sym mid;
};
-/* flags (21bits): 1(shared flag):10(cioff/bidx):10(stack_len) */
-#define MRB_ENV_SET_STACK_LEN(e,len) ((e)->flags = (((e)->flags & ~0x3ff)|((unsigned int)(len) & 0x3ff)))
-#define MRB_ENV_STACK_LEN(e) ((mrb_int)((e)->flags & 0x3ff))
-#define MRB_ENV_STACK_UNSHARED (1<<20)
-#define MRB_ENV_UNSHARE_STACK(e) ((e)->flags |= MRB_ENV_STACK_UNSHARED)
-#define MRB_ENV_STACK_SHARED_P(e) (((e)->flags & MRB_ENV_STACK_UNSHARED) == 0)
-#define MRB_ENV_BIDX(e) (((e)->flags >> 10) & 0x3ff)
-#define MRB_ENV_SET_BIDX(e,idx) ((e)->flags = (((e)->flags & ~(0x3ff<<10))|((unsigned int)(idx) & 0x3ff)<<10))
+/* flags (21bits): 1(close):1(touched):1(heap):8(cioff/bidx):8(stack_len) */
+#define MRB_ENV_SET_LEN(e,len) ((e)->flags = (((e)->flags & ~0xff)|((unsigned int)(len) & 0xff)))
+#define MRB_ENV_LEN(e) ((mrb_int)((e)->flags & 0xff))
+#define MRB_ENV_CLOSED (1<<20)
+#define MRB_ENV_TOUCHED (1<<19)
+#define MRB_ENV_HEAPED (1<<18)
+#define MRB_ENV_CLOSE(e) ((e)->flags |= MRB_ENV_CLOSED)
+#define MRB_ENV_TOUCH(e) ((e)->flags |= MRB_ENV_TOUCHED)
+#define MRB_ENV_HEAP(e) ((e)->flags |= MRB_ENV_HEAPED)
+#define MRB_ENV_HEAP_P(e) ((e)->flags & MRB_ENV_HEAPED)
+#define MRB_ENV_ONSTACK_P(e) (((e)->flags & MRB_ENV_CLOSED) == 0)
+#define MRB_ENV_BIDX(e) (((e)->flags >> 8) & 0xff)
+#define MRB_ENV_SET_BIDX(e,idx) ((e)->flags = (((e)->flags & ~(0xff<<8))|((unsigned int)(idx) & 0xff)<<10))
void mrb_env_unshare(mrb_state*, struct REnv*);

MODIFY
mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
@@ -649,8 +649,8 @@ main(int argc, char **argv)
/* adjust stack length of toplevel environment */
if (mrb->c->cibase->env) {
struct REnv *e = mrb->c->cibase->env;
- if (e && MRB_ENV_STACK_LEN(e) < proc->body.irep->nlocals) {
- MRB_ENV_SET_STACK_LEN(e, proc->body.irep->nlocals);
+ if (e && MRB_ENV_LEN(e) < proc->body.irep->nlocals) {
+ MRB_ENV_SET_LEN(e, proc->body.irep->nlocals);
}
}
/* pass a proc for evaluation */

MODIFY
mrbgems/mruby-eval/src/eval.c
mrbgems/mruby-eval/src/eval.c
@@ -88,7 +88,7 @@ create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding,
e->mid = ci->mid;
e->stack = ci[1].stackent;
e->cxt = mrb->c;
- MRB_ENV_SET_STACK_LEN(e, ci->proc->body.irep->nlocals);
+ MRB_ENV_SET_LEN(e, ci->proc->body.irep->nlocals);
bidx = ci->argc;
if (ci->argc < 0) bidx = 2;
else bidx += 1;

MODIFY
src/gc.c
src/gc.c
@@ -716,10 +716,10 @@ gc_mark_children(mrb_state *mrb, mrb_gc *gc, struct RBasic *obj)
struct REnv *e = (struct REnv*)obj;
mrb_int i, len;
- if (MRB_ENV_STACK_SHARED_P(e) && e->cxt && e->cxt->fib) {
+ if (MRB_ENV_ONSTACK_P(e) && e->cxt && e->cxt->fib) {
mrb_gc_mark(mrb, (struct RBasic*)e->cxt->fib);
}
- len = MRB_ENV_STACK_LEN(e);
+ len = MRB_ENV_LEN(e);
for (i=0; i<len; i++) {
mrb_gc_mark_value(mrb, e->stack[i]);
}
@@ -820,7 +820,7 @@ obj_free(mrb_state *mrb, struct RBasic *obj, int end)
{
struct REnv *e = (struct REnv*)obj;
- if (MRB_ENV_STACK_SHARED_P(e)) {
+ if (MRB_ENV_ONSTACK_P(e)) {
/* cannot be freed */
e->stack = NULL;
break;
@@ -842,7 +842,7 @@ obj_free(mrb_state *mrb, struct RBasic *obj, int end)
while (ce <= ci) {
struct REnv *e = ci->env;
if (e && !mrb_object_dead_p(mrb, (struct RBasic*)e) &&
- e->tt == MRB_TT_ENV && MRB_ENV_STACK_SHARED_P(e)) {
+ e->tt == MRB_TT_ENV && MRB_ENV_ONSTACK_P(e)) {
mrb_env_unshare(mrb, e);
}
ci--;
@@ -990,7 +990,7 @@ gc_gray_counts(mrb_state *mrb, mrb_gc *gc, struct RBasic *obj)
break;
case MRB_TT_ENV:
- children += MRB_ENV_STACK_LEN(obj);
+ children += MRB_ENV_LEN(obj);
break;
case MRB_TT_FIBER:

MODIFY
src/kernel.c
src/kernel.c
@@ -160,7 +160,7 @@ mrb_f_block_given_p_m(mrb_state *mrb, mrb_value self)
/* use saved block arg position */
bidx = MRB_ENV_BIDX(e);
/* bidx may be useless (e.g. define_method) */
- if (bidx >= MRB_ENV_STACK_LEN(e))
+ if (bidx >= MRB_ENV_LEN(e))
return mrb_false_value();
bp = &e->stack[bidx];
}

MODIFY
src/proc.c
src/proc.c
@@ -47,7 +47,7 @@ env_new(mrb_state *mrb, mrb_int nlocals)
int bidx;
e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, NULL);
- MRB_ENV_SET_STACK_LEN(e, nlocals);
+ MRB_ENV_SET_LEN(e, nlocals);
bidx = ci->argc;
if (ci->argc < 0) bidx = 2;
else bidx += 1;
@@ -122,14 +122,14 @@ mrb_proc_new_cfunc_with_env(mrb_state *mrb, mrb_func_t func, mrb_int argc, const
p->e.env = e = env_new(mrb, argc);
p->flags |= MRB_PROC_ENVSET;
mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)e);
- MRB_ENV_UNSHARE_STACK(e);
+ MRB_ENV_CLOSE(e);
/* NOTE: Prevents keeping invalid addresses when NoMemoryError is raised from `mrb_malloc()`. */
e->stack = NULL;
- MRB_ENV_SET_STACK_LEN(e, 0);
+ MRB_ENV_SET_LEN(e, 0);
e->stack = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * argc);
- MRB_ENV_SET_STACK_LEN(e, argc);
+ MRB_ENV_SET_LEN(e, argc);
if (argv) {
for (i = 0; i < argc; ++i) {
@@ -163,9 +163,9 @@ mrb_proc_cfunc_env_get(mrb_state *mrb, mrb_int idx)
if (!e) {
mrb_raise(mrb, E_TYPE_ERROR, "Can't get cfunc env from cfunc Proc without REnv.");
}
- if (idx < 0 || MRB_ENV_STACK_LEN(e) <= idx) {
+ if (idx < 0 || MRB_ENV_LEN(e) <= idx) {
mrb_raisef(mrb, E_INDEX_ERROR, "Env index out of range: %i (expected: 0 <= index < %i)",
- idx, MRB_ENV_STACK_LEN(e));
+ idx, MRB_ENV_LEN(e));
}
return e->stack[idx];

MODIFY
src/vm.c
src/vm.c
@@ -152,7 +152,7 @@ envadjust(mrb_state *mrb, mrb_value *oldbase, mrb_value *newbase, size_t oldsize
struct REnv *e = ci->env;
mrb_value *st;
- if (e && MRB_ENV_STACK_SHARED_P(e) &&
+ if (e && MRB_ENV_ONSTACK_P(e) &&
(st = e->stack) && oldbase <= st && st < oldbase+oldsize) {
ptrdiff_t off = e->stack - oldbase;
@@ -162,7 +162,7 @@ envadjust(mrb_state *mrb, mrb_value *oldbase, mrb_value *newbase, size_t oldsize
if (ci->proc && MRB_PROC_ENV_P(ci->proc) && ci->env != MRB_PROC_ENV(ci->proc)) {
e = MRB_PROC_ENV(ci->proc);
- if (e && MRB_ENV_STACK_SHARED_P(e) &&
+ if (e && MRB_ENV_ONSTACK_P(e) &&
(st = e->stack) && oldbase <= st && st < oldbase+oldsize) {
ptrdiff_t off = e->stack - oldbase;
@@ -297,10 +297,10 @@ mrb_env_unshare(mrb_state *mrb, struct REnv *e)
{
if (e == NULL) return;
else {
- size_t len = (size_t)MRB_ENV_STACK_LEN(e);
+ size_t len = (size_t)MRB_ENV_LEN(e);
mrb_value *p;
- if (!MRB_ENV_STACK_SHARED_P(e)) return;
+ if (!MRB_ENV_ONSTACK_P(e)) return;
if (e->cxt != mrb->c) return;
if (e == mrb->c->cibase->env) return; /* for mirb */
p = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value)*len);
@@ -308,7 +308,7 @@ mrb_env_unshare(mrb_state *mrb, struct REnv *e)
stack_copy(p, e->stack, len);
}
e->stack = p;
- MRB_ENV_UNSHARE_STACK(e);
+ MRB_ENV_CLOSE(e);
mrb_write_barrier(mrb, (struct RBasic *)e);
}
}
@@ -1160,7 +1160,7 @@ RETRY_TRY_BLOCK:
mrb_value *regs_a = regs + a;
struct REnv *e = uvenv(mrb, c);
- if (e && b < MRB_ENV_STACK_LEN(e)) {
+ if (e && b < MRB_ENV_LEN(e)) {
*regs_a = e->stack[b];
}
else {
@@ -1175,7 +1175,7 @@ RETRY_TRY_BLOCK:
if (e) {
mrb_value *regs_a = regs + a;
- if (b < MRB_ENV_STACK_LEN(e)) {
+ if (b < MRB_ENV_LEN(e)) {
e->stack[b] = *regs_a;
mrb_write_barrier(mrb, (struct RBasic*)e);
}
@@ -1689,7 +1689,7 @@ RETRY_TRY_BLOCK:
else {
struct REnv *e = uvenv(mrb, lv-1);
if (!e) goto L_NOSUPER;
- if (MRB_ENV_STACK_LEN(e) <= m1+r+m2+kd+1)
+ if (MRB_ENV_LEN(e) <= m1+r+m2+kd+1)
goto L_NOSUPER;
stack = e->stack + 1;
}
@@ -2015,7 +2015,7 @@ RETRY_TRY_BLOCK:
if (MRB_PROC_ENV_P(dst)) {
struct REnv *e = MRB_PROC_ENV(dst);
- if (!MRB_ENV_STACK_SHARED_P(e) || (e->cxt && e->cxt != mrb->c)) {
+ if (!MRB_ENV_ONSTACK_P(e) || (e->cxt && e->cxt != mrb->c)) {
localjump_error(mrb, LOCALJUMP_ERROR_RETURN);
goto L_RAISE;
}
@@ -2070,7 +2070,7 @@ RETRY_TRY_BLOCK:
mrb_exc_set(mrb, exc);
goto L_RAISE;
}
- if (!MRB_PROC_ENV_P(proc) || !MRB_ENV_STACK_SHARED_P(MRB_PROC_ENV(proc))) {
+ if (!MRB_PROC_ENV_P(proc) || !MRB_ENV_ONSTACK_P(MRB_PROC_ENV(proc))) {
goto L_BREAK_ERROR;
}
else {
@@ -2170,8 +2170,8 @@ RETRY_TRY_BLOCK:
if (lv == 0) stack = regs + 1;
else {
struct REnv *e = uvenv(mrb, lv-1);
- if (!e || (!MRB_ENV_STACK_SHARED_P(e) && e->mid == 0) ||
- MRB_ENV_STACK_LEN(e) <= m1+r+m2+1) {
+ if (!e || (!MRB_ENV_ONSTACK_P(e) && e->mid == 0) ||
+ MRB_ENV_LEN(e) <= m1+r+m2+1) {
localjump_error(mrb, LOCALJUMP_ERROR_YIELD);
goto L_RAISE;
}

Change flag names in preparation of `REnv` refactoring.