| 1 |
#include <mruby.h> |
| 2 |
#include <mruby/presym.h> |
| 3 |
#include <mruby/proc.h> |
| 4 |
#include <mruby/variable.h> |
| 5 |
|
| 6 |
/* provided by mruby-proc-ext */ |
| 7 |
mrb_value mrb_proc_source_location(mrb_state *mrb, struct RProc *p); |
| 8 |
|
| 9 |
/* provided by mruby-binding */ |
| 10 |
mrb_value mrb_binding_new(mrb_state *mrb, const struct RProc *proc, mrb_value recv, struct REnv *env); |
| 11 |
|
| 12 |
static mrb_value |
| 13 |
mrb_proc_binding(mrb_state *mrb, mrb_value procval) |
| 14 |
{ |
| 15 |
const struct RProc *proc = mrb_proc_ptr(procval); |
| 16 |
struct REnv *env; |
| 17 |
|
| 18 |
mrb_value receiver; |
| 19 |
if (!proc || MRB_PROC_CFUNC_P(proc) || !proc->upper || MRB_PROC_CFUNC_P(proc->upper)) { |
| 20 |
env = NULL; |
| 21 |
proc = NULL; |
| 22 |
receiver = mrb_nil_value(); |
| 23 |
} |
| 24 |
else { |
| 25 |
env = MRB_PROC_ENV(proc); |
| 26 |
mrb_assert(env); |
| 27 |
proc = proc->upper; |
| 28 |
receiver = MRB_ENV_LEN(env) > 0 ? env->stack[0] : mrb_nil_value(); |
| 29 |
} |
| 30 |
|
| 31 |
mrb_value binding = mrb_binding_new(mrb, proc, receiver, env); |
| 32 |
mrb_iv_set(mrb, binding, MRB_SYM(source_location), mrb_proc_source_location(mrb, mrb_proc_ptr(procval))); |
| 33 |
return binding; |
| 34 |
} |
| 35 |
|
| 36 |
void |
| 37 |
mrb_mruby_proc_binding_gem_init(mrb_state *mrb) |
| 38 |
{ |
| 39 |
mrb_define_method(mrb, mrb->proc_class, "binding", mrb_proc_binding, MRB_ARGS_NONE()); |
| 40 |
} |
| 41 |
|
| 42 |
void |
| 43 |
mrb_mruby_proc_binding_gem_final(mrb_state *mrb) |
| 44 |
{ |
| 45 |
} |