From 66ca9722b6264a24a8b458744d1db8a57c64d773 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 21 Mar 2023 16:12:53 +0900 Subject: [PATCH] mruby-binding-core (mrb_binding_new): new function Use this function instead of `mrb_binding_alloc` (removed). --- mrbgems/mruby-binding-core/src/binding-core.c | 30 +++++++++---------- mrbgems/mruby-proc-binding/src/proc-binding.c | 9 ++---- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/mrbgems/mruby-binding-core/src/binding-core.c b/mrbgems/mruby-binding-core/src/binding-core.c index 7c3d81adc..a15cf1254 100644 --- a/mrbgems/mruby-binding-core/src/binding-core.c +++ b/mrbgems/mruby-binding-core/src/binding-core.c @@ -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 diff --git a/mrbgems/mruby-proc-binding/src/proc-binding.c b/mrbgems/mruby-proc-binding/src/proc-binding.c index b95edbc1b..0641617ac 100644 --- a/mrbgems/mruby-proc-binding/src/proc-binding.c +++ b/mrbgems/mruby-proc-binding/src/proc-binding.c @@ -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; }