mruby-binding-core (mrb_binding_new): new function

Use this function instead of `mrb_binding_alloc` (removed).
This commit is contained in:
Yukihiro "Matz" Matsumoto
2023-03-21 16:12:53 +09:00
parent 88ae5e8cf8
commit 66ca9722b6
2 changed files with 17 additions and 22 deletions
+15 -15
View File
@@ -359,35 +359,35 @@ binding_source_location(mrb_state *mrb, mrb_value self)
}
mrb_value
mrb_binding_alloc(mrb_state *mrb)
mrb_binding_new(mrb_state *mrb, const struct RProc *proc, mrb_value recv, struct REnv *env)
{
struct RObject *obj = MRB_OBJ_ALLOC(mrb, MRB_TT_OBJECT, mrb_class_get_id(mrb, MRB_SYM(Binding)));
return mrb_obj_value(obj);
struct RObject *binding = MRB_OBJ_ALLOC(mrb, MRB_TT_OBJECT, mrb_class_get_id(mrb, MRB_SYM(Binding)));
if (proc && !MRB_PROC_CFUNC_P(proc)) {
const mrb_irep *irep = proc->body.irep;
mrb_obj_iv_set(mrb, binding, MRB_SYM(pc), mrb_fixnum_value(mrb->c->ci[-1].pc - irep->iseq - 1 /* step back */));
}
proc = mrb_binding_wrap_lvspace(mrb, proc, &env);
mrb_obj_iv_set(mrb, binding, MRB_SYM(proc), mrb_obj_value((void*)proc));
mrb_obj_iv_set(mrb, binding, MRB_SYM(recv), recv);
mrb_obj_iv_set(mrb, binding, MRB_SYM(env), mrb_obj_value(env));
return mrb_obj_value(binding);
}
static mrb_value
mrb_f_binding(mrb_state *mrb, mrb_value self)
{
mrb_value binding;
struct RProc *proc;
struct REnv *env;
binding = mrb_binding_alloc(mrb);
proc = (struct RProc*)mrb_proc_get_caller(mrb, &env);
if (!env || MRB_PROC_CFUNC_P(proc)) {
proc = NULL;
env = NULL;
}
if (proc && !MRB_PROC_CFUNC_P(proc)) {
const mrb_irep *irep = proc->body.irep;
mrb_iv_set(mrb, binding, MRB_SYM(pc), mrb_fixnum_value(mrb->c->ci[-1].pc - irep->iseq - 1 /* step back */));
}
proc = mrb_binding_wrap_lvspace(mrb, proc, &env);
mrb_iv_set(mrb, binding, MRB_SYM(proc), mrb_obj_value(proc));
mrb_iv_set(mrb, binding, MRB_SYM(recv), self);
mrb_iv_set(mrb, binding, MRB_SYM(env), mrb_obj_value(env));
return binding;
return mrb_binding_new(mrb, proc, self, env);
}
void
@@ -7,13 +7,11 @@
mrb_value mrb_proc_source_location(mrb_state *mrb, struct RProc *p);
/* provided by mruby-binding-core */
mrb_value mrb_binding_alloc(mrb_state *mrb);
struct RProc *mrb_binding_wrap_lvspace(mrb_state *mrb, const struct RProc *proc, struct REnv **envp);
mrb_value mrb_binding_new(mrb_state *mrb, const struct RProc *proc, mrb_value recv, struct REnv *env);
static mrb_value
mrb_proc_binding(mrb_state *mrb, mrb_value procval)
{
mrb_value binding = mrb_binding_alloc(mrb);
const struct RProc *proc = mrb_proc_ptr(procval);
struct REnv *env;
@@ -30,10 +28,7 @@ mrb_proc_binding(mrb_state *mrb, mrb_value procval)
receiver = MRB_ENV_LEN(env) > 0 ? env->stack[0] : mrb_nil_value();
}
proc = mrb_binding_wrap_lvspace(mrb, proc, &env);
mrb_iv_set(mrb, binding, MRB_SYM(proc), mrb_obj_value((void*)proc));
mrb_iv_set(mrb, binding, MRB_SYM(recv), receiver);
mrb_iv_set(mrb, binding, MRB_SYM(env), mrb_obj_value(env));
mrb_value binding = mrb_binding_new(mrb, proc, receiver, env);
mrb_iv_set(mrb, binding, MRB_SYM(source_location), mrb_proc_source_location(mrb, mrb_proc_ptr(procval)));
return binding;
}