Reorganize `env_new()` as `mrb_env_new()`
The `mrb_env_new()` function is a global function, but it is still treated as an internal function.
MODIFY
mrbgems/mruby-eval/src/eval.c
mrbgems/mruby-eval/src/eval.c
#include <mruby/opcode.h>
#include <mruby/error.h>
+struct REnv *mrb_env_new(mrb_state *mrb, struct mrb_context *c, mrb_callinfo *ci, int nstacks, mrb_value *stack, struct RClass *tc);
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);
@@ -20,7 +21,6 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi
struct REnv *e;
mrb_callinfo *ci; /* callinfo of eval caller */
struct RClass *target_class = NULL;
- mrb_int bidx;
if (!mrb_nil_p(binding)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "Binding of eval must be nil.");
@@ -83,16 +83,7 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi
e = ci->env;
}
else {
- e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV,
- (struct RClass*)target_class);
- e->mid = ci->mid;
- e->stack = ci[1].stackent;
- e->cxt = mrb->c;
- MRB_ENV_SET_LEN(e, ci->proc->body.irep->nlocals);
- bidx = ci->argc;
- if (ci->argc < 0) bidx = 2;
- else bidx += 1;
- MRB_ENV_SET_BIDX(e, bidx);
+ e = mrb_env_new(mrb, mrb->c, ci, ci->proc->body.irep->nlocals, ci[1].stackent, target_class);
ci->env = e;
}
proc->e.env = e;

MODIFY
src/proc.c
src/proc.c
@@ -57,22 +57,21 @@ mrb_proc_new(mrb_state *mrb, const mrb_irep *irep)
return p;
}
-static struct REnv*
-env_new(mrb_state *mrb, mrb_int nlocals)
+struct REnv*
+mrb_env_new(mrb_state *mrb, struct mrb_context *c, mrb_callinfo *ci, int nstacks, mrb_value *stack, struct RClass *tc)
{
struct REnv *e;
- mrb_callinfo *ci = mrb->c->ci;
mrb_int bidx;
- e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, NULL);
- MRB_ENV_SET_LEN(e, nlocals);
+ e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, tc);
+ MRB_ENV_SET_LEN(e, nstacks);
bidx = ci->argc;
- if (ci->argc < 0) bidx = 2;
+ if (bidx < 0) bidx = 2;
else bidx += 1;
MRB_ENV_SET_BIDX(e, bidx);
e->mid = ci->mid;
- e->stack = mrb->c->stack;
- e->cxt = mrb->c;
+ e->stack = stack;
+ e->cxt = c;
return e;
}
@@ -90,12 +89,8 @@ closure_setup(mrb_state *mrb, struct RProc *p)
else if (up) {
struct RClass *tc = MRB_PROC_TARGET_CLASS(p);
- e = env_new(mrb, up->body.irep->nlocals);
+ e = mrb_env_new(mrb, mrb->c, ci, up->body.irep->nlocals, mrb->c->stack, tc);
ci->env = e;
- if (tc) {
- e->c = tc;
- mrb_field_write_barrier(mrb, (struct RBasic*)e, (struct RBasic*)tc);
- }
if (MRB_PROC_ENV_P(up) && MRB_PROC_ENV(up)->cxt == NULL) {
e->mid = MRB_PROC_ENV(up)->mid;
}
@@ -137,15 +132,11 @@ mrb_proc_new_cfunc_with_env(mrb_state *mrb, mrb_func_t func, mrb_int argc, const
struct REnv *e;
int i;
- p->e.env = e = env_new(mrb, argc);
+ p->e.env = e = mrb_env_new(mrb, mrb->c, mrb->c->ci, 0, NULL, NULL);
p->flags |= MRB_PROC_ENVSET;
mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)e);
MRB_ENV_CLOSE(e);
- /* NOTE: Prevents keeping invalid addresses when NoMemoryError is raised from `mrb_malloc()`. */
- e->stack = NULL;
- MRB_ENV_SET_LEN(e, 0);
-
e->stack = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * argc);
MRB_ENV_SET_LEN(e, argc);

Reorganize `env_new()` as `mrb_env_new()`

The `mrb_env_new()` function is a global function, but it is still treated as an internal function.