Commit 792f6ac6 authored by dearblue's avatar dearblue

Adjustment of the current HEAD and bindings, and separation

Make changes to make `Binding` work.
At the same time, it separates `Binding#eval`, which depends on `mruby-eval`, from `mruby-binding-core`.
parent 935ffa46
MRuby::Gem::Specification.new('mruby-binding') do |spec| MRuby::Gem::Specification.new('mruby-binding-core') do |spec|
spec.license = 'MIT' spec.license = 'MIT'
spec.author = 'mruby developers' spec.author = 'mruby developers'
spec.summary = 'Binding class' spec.summary = 'Binding class'
spec.add_dependency('mruby-eval', :core => 'mruby-eval')
end end
#include "mruby.h" #include <mruby.h>
#include "mruby/array.h" #include <mruby/array.h>
#include "mruby/hash.h" #include <mruby/hash.h>
#include "mruby/proc.h" #include <mruby/proc.h>
#include "mruby/variable.h" #include <mruby/variable.h>
#include <mruby/presym.h>
mrb_value proc_local_variables(mrb_state *mrb, struct RProc *proc); mrb_value mrb_proc_local_variables(mrb_state *mrb, const struct RProc *proc);
static mrb_value static mrb_value
binding_local_variables(mrb_state *mrb, mrb_value self) binding_local_variables(mrb_state *mrb, mrb_value self)
{ {
struct RProc *proc = mrb_proc_ptr(mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "proc"))); const struct RProc *proc = mrb_proc_ptr(mrb_iv_get(mrb, self, MRB_SYM(proc)));
return proc_local_variables(mrb, proc); return mrb_proc_local_variables(mrb, proc);
} }
const struct RProc *mrb_proc_get_caller(mrb_state *mrb, struct REnv **env);
static mrb_value static mrb_value
mrb_f_binding(mrb_state *mrb, mrb_value self) mrb_f_binding(mrb_state *mrb, mrb_value self)
{ {
struct RObject *obj; struct RObject *obj;
mrb_value binding; mrb_value binding;
struct RProc *proc; struct RProc *proc;
mrb_int i; struct REnv *env;
obj = (struct RObject*)mrb_obj_alloc(mrb, MRB_TT_OBJECT, mrb_class_get(mrb, "Binding")); obj = (struct RObject*)mrb_obj_alloc(mrb, MRB_TT_OBJECT, mrb_class_get(mrb, "Binding"));
binding = mrb_obj_value(obj); binding = mrb_obj_value(obj);
proc = mrb->c->ci[-1].proc; proc = (struct RProc*)mrb_proc_get_caller(mrb, &env);
mrb_iv_set(mrb, binding, mrb_intern_lit(mrb, "proc"), mrb_obj_value(proc)); mrb_iv_set(mrb, binding, MRB_SYM(proc), mrb_obj_value(proc));
mrb_iv_set(mrb, binding, mrb_intern_lit(mrb, "recv"), self); mrb_iv_set(mrb, binding, MRB_SYM(recv), self);
mrb_iv_set(mrb, binding, MRB_SYM(env), mrb_obj_value(env));
return binding; return binding;
} }
void void
mrb_mruby_binding_gem_init(mrb_state *mrb) mrb_mruby_binding_core_gem_init(mrb_state *mrb)
{ {
struct RClass *binding = mrb_define_class(mrb, "Binding", mrb->object_class); struct RClass *binding = mrb_define_class(mrb, "Binding", mrb->object_class);
mrb_undef_class_method(mrb, binding, "new"); mrb_undef_class_method(mrb, binding, "new");
mrb_undef_class_method(mrb, binding, "allocate");
mrb_define_method(mrb, mrb->kernel_module, "binding", mrb_f_binding, MRB_ARGS_NONE()); mrb_define_method(mrb, mrb->kernel_module, "binding", mrb_f_binding, MRB_ARGS_NONE());
...@@ -41,6 +46,6 @@ mrb_mruby_binding_gem_init(mrb_state *mrb) ...@@ -41,6 +46,6 @@ mrb_mruby_binding_gem_init(mrb_state *mrb)
} }
void void
mrb_mruby_binding_gem_final(mrb_state *mrb) mrb_mruby_binding_core_gem_final(mrb_state *mrb)
{ {
} }
...@@ -7,11 +7,5 @@ assert("Binding#local_variables") do ...@@ -7,11 +7,5 @@ assert("Binding#local_variables") do
b = 1 b = 1
binding binding
end end
assert_equal [:a, :b, :block], block.call(0).local_variables assert_equal [:a, :b, :block], block.call(0).local_variables.sort
end
assert("Binding#eval") do
b = nil
1.times { x, y, z = 1, 2, 3; [x,y,z]; b = binding }
assert_equal([1, 2, 3], b.eval("[x, y, z]"))
end end
MRuby::Gem::Specification.new('mruby-binding') do |spec|
spec.license = 'MIT'
spec.author = 'mruby developers'
spec.summary = 'Binding class'
spec.add_dependency('mruby-binding-core', :core => 'mruby-binding-core')
spec.add_dependency('mruby-eval', :core => 'mruby-eval')
end
assert("Binding#eval") do
b = nil
1.times { x, y, z = 1, 2, 3; [x,y,z]; b = binding }
assert_equal([1, 2, 3], b.eval("[x, y, z]"))
end
...@@ -19,24 +19,37 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi ...@@ -19,24 +19,37 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi
{ {
mrbc_context *cxt; mrbc_context *cxt;
struct mrb_parser_state *p; struct mrb_parser_state *p;
struct RProc *proc, *scope; struct RProc *proc;
const struct RProc *scope;
struct REnv *e; struct REnv *e;
mrb_callinfo *ci; /* callinfo of eval caller */ mrb_callinfo *ci; /* callinfo of eval caller */
struct RClass *target_class = NULL; struct RClass *target_class = NULL;
struct mrb_context *c = mrb->c; struct mrb_context *c = mrb->c;
mrb_bool is_binding = FALSE;
if (!mrb_nil_p(binding)) { if (!mrb_nil_p(binding)) {
mrb_value scope_obj;
if (!mrb_class_defined(mrb, "Binding") if (!mrb_class_defined(mrb, "Binding")
|| !mrb_obj_is_kind_of(mrb, binding, mrb_class_get(mrb, "Binding"))) { || !mrb_obj_is_kind_of(mrb, binding, mrb_class_get(mrb, "Binding"))) {
mrb_raisef(mrb, E_TYPE_ERROR, "wrong argument type %S (expected binding)", mrb_raisef(mrb, E_TYPE_ERROR, "wrong argument type %C (expected binding)",
mrb_obj_value(mrb_obj_class(mrb, binding))); mrb_obj_class(mrb, binding));
}
scope_obj = mrb_iv_get(mrb, binding, MRB_SYM(proc));
mrb_assert(mrb_proc_p(scope_obj));
scope = mrb_proc_ptr(scope_obj);
if (MRB_PROC_CFUNC_P(scope)) {
e = NULL;
}
else {
mrb_value env = mrb_iv_get(mrb, binding, MRB_SYM(env));
mrb_assert(mrb_env_p(env));
e = (struct REnv *)mrb_obj_ptr(env);
mrb_assert(e != NULL);
} }
scope = mrb_proc_ptr(mrb_iv_get(mrb, binding, mrb_intern_lit(mrb, "proc")));
is_binding = TRUE;
} }
else { else {
scope = c->ci[-1].proc; ci = (c->ci > c->cibase) ? c->ci - 1 : c->cibase;
scope = ci->proc;
e = NULL;
} }
cxt = mrbc_context_new(mrb); cxt = mrbc_context_new(mrb);
...@@ -45,8 +58,7 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi ...@@ -45,8 +58,7 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi
mrbc_filename(mrb, cxt, file ? file : "(eval)"); mrbc_filename(mrb, cxt, file ? file : "(eval)");
cxt->capture_errors = TRUE; cxt->capture_errors = TRUE;
cxt->no_optimize = TRUE; cxt->no_optimize = TRUE;
ci = (c->ci > c->cibase) ? c->ci - 1 : c->cibase; cxt->upper = scope && MRB_PROC_CFUNC_P(scope) ? NULL : scope;
cxt->upper = ci->proc && MRB_PROC_CFUNC_P(ci->proc) ? NULL : ci->proc;
p = mrb_parse_nstring(mrb, s, len, cxt); p = mrb_parse_nstring(mrb, s, len, cxt);
...@@ -90,18 +102,19 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi ...@@ -90,18 +102,19 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi
} }
if (scope) { if (scope) {
target_class = MRB_PROC_TARGET_CLASS(scope); target_class = MRB_PROC_TARGET_CLASS(scope);
} if (!MRB_PROC_CFUNC_P(scope)) {
if (scope && !MRB_PROC_CFUNC_P(scope)) { if (e == NULL) {
if ((e = mrb_vm_ci_env(ci)) != NULL) { /* when `binding` is nil */
/* do nothing, because e is assigned already */ e = mrb_vm_ci_env(ci);
} if (e == NULL) {
else { e = mrb_env_new(mrb, c, ci, ci->proc->body.irep->nlocals, ci->stack, target_class);
e = mrb_env_new(mrb, c, ci, scope->body.irep->nlocals, ci->stack, target_class); ci->u.env = e;
ci->u.env = e; }
}
proc->e.env = e;
proc->flags |= MRB_PROC_ENVSET;
mrb_field_write_barrier(mrb, (struct RBasic*)proc, (struct RBasic*)e);
} }
proc->e.env = e;
proc->flags |= MRB_PROC_ENVSET;
mrb_field_write_barrier(mrb, (struct RBasic*)proc, (struct RBasic*)e);
} }
proc->upper = scope; proc->upper = scope;
mrb_vm_ci_target_class_set(mrb->c->ci, target_class); mrb_vm_ci_target_class_set(mrb->c->ci, target_class);
...@@ -146,7 +159,7 @@ f_eval(mrb_state *mrb, mrb_value self) ...@@ -146,7 +159,7 @@ f_eval(mrb_state *mrb, mrb_value self)
proc = create_proc_from_string(mrb, s, len, binding, file, line); proc = create_proc_from_string(mrb, s, len, binding, file, line);
if (!mrb_nil_p(binding)) { if (!mrb_nil_p(binding)) {
self = mrb_iv_get(mrb, binding, mrb_intern_lit(mrb, "recv")); self = mrb_iv_get(mrb, binding, MRB_SYM(recv));
} }
mrb_assert(!MRB_PROC_CFUNC_P(proc)); mrb_assert(!MRB_PROC_CFUNC_P(proc));
return exec_irep(mrb, self, proc); return exec_irep(mrb, self, proc);
......
...@@ -117,6 +117,8 @@ mrb_obj_ivar_set(mrb_state *mrb, mrb_value self) ...@@ -117,6 +117,8 @@ mrb_obj_ivar_set(mrb_state *mrb, mrb_value self)
return val; return val;
} }
mrb_value mrb_proc_local_variables(mrb_state *mrb, const struct RProc *proc);
/* 15.3.1.2.7 */ /* 15.3.1.2.7 */
/* 15.3.1.3.28 */ /* 15.3.1.3.28 */
/* /*
...@@ -133,40 +135,7 @@ mrb_obj_ivar_set(mrb_state *mrb, mrb_value self) ...@@ -133,40 +135,7 @@ mrb_obj_ivar_set(mrb_state *mrb, mrb_value self)
static mrb_value static mrb_value
mrb_local_variables(mrb_state *mrb, mrb_value self) mrb_local_variables(mrb_state *mrb, mrb_value self)
{ {
const struct RProc *proc; return mrb_proc_local_variables(mrb, mrb->c->ci[-1].proc);
const mrb_irep *irep;
mrb_value vars;
size_t i;
proc = mrb->c->ci[-1].proc;
if (proc == NULL || MRB_PROC_CFUNC_P(proc)) {
return mrb_ary_new(mrb);
}
vars = mrb_hash_new(mrb);
while (proc) {
if (MRB_PROC_CFUNC_P(proc)) break;
irep = proc->body.irep;
if (irep->lv) {
for (i = 0; i + 1 < irep->nlocals; ++i) {
if (irep->lv[i]) {
mrb_sym sym = irep->lv[i];
const char *name = mrb_sym_name(mrb, sym);
switch (name[0]) {
case '*': case '&':
break;
default:
mrb_hash_set(mrb, vars, mrb_symbol_value(sym), mrb_true_value());
break;
}
}
}
}
if (MRB_PROC_SCOPE_P(proc)) break;
proc = proc->upper;
}
return mrb_hash_keys(mrb, vars);
} }
KHASH_DECLARE(st, mrb_sym, char, FALSE) KHASH_DECLARE(st, mrb_sym, char, FALSE)
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include <mruby/opcode.h> #include <mruby/opcode.h>
#include <mruby/data.h> #include <mruby/data.h>
#include <mruby/presym.h> #include <mruby/presym.h>
#include <mruby/array.h>
#include <mruby/hash.h>
static const mrb_code call_iseq[] = { static const mrb_code call_iseq[] = {
OP_CALL, OP_CALL,
...@@ -305,6 +307,71 @@ mrb_proc_arity(const struct RProc *p) ...@@ -305,6 +307,71 @@ mrb_proc_arity(const struct RProc *p)
return arity; return arity;
} }
mrb_value
mrb_proc_local_variables(mrb_state *mrb, const struct RProc *proc)
{
const mrb_irep *irep;
mrb_value vars;
size_t i;
if (proc == NULL || MRB_PROC_CFUNC_P(proc)) {
return mrb_ary_new(mrb);
}
vars = mrb_hash_new(mrb);
while (proc) {
if (MRB_PROC_CFUNC_P(proc)) break;
irep = proc->body.irep;
if (irep->lv) {
for (i = 0; i + 1 < irep->nlocals; ++i) {
if (irep->lv[i]) {
mrb_sym sym = irep->lv[i];
const char *name = mrb_sym_name(mrb, sym);
switch (name[0]) {
case '*': case '&':
break;
default:
mrb_hash_set(mrb, vars, mrb_symbol_value(sym), mrb_true_value());
break;
}
}
}
}
if (MRB_PROC_SCOPE_P(proc)) break;
proc = proc->upper;
}
return mrb_hash_keys(mrb, vars);
}
const struct RProc *
mrb_proc_get_caller(mrb_state *mrb, struct REnv **envp)
{
struct mrb_context *c = mrb->c;
mrb_callinfo *ci = (c->ci > c->cibase) ? c->ci - 1 : c->cibase;
const struct RProc *proc = ci->proc;
if (!proc || MRB_PROC_CFUNC_P(proc)) {
if (envp) *envp = NULL;
}
else {
struct RClass *tc = MRB_PROC_TARGET_CLASS(proc);
struct REnv *e = mrb_vm_ci_env(ci);
if (e == NULL) {
int nstacks = proc->body.irep->nlocals;
e = mrb_env_new(mrb, c, ci, nstacks, ci->stack, tc);
ci->u.env = e;
}
else if (tc) {
e->c = tc;
mrb_field_write_barrier(mrb, (struct RBasic*)e, (struct RBasic*)tc);
}
if (envp) *envp = e;
}
return proc;
}
void void
mrb_init_proc(mrb_state *mrb) mrb_init_proc(mrb_state *mrb)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment