Commit 4004e122 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #2296 from take-cheeze/core_local_vars

Move `Kernel.local_variables` to core.
parents c4268f67 d8d07a23
...@@ -3,5 +3,4 @@ MRuby::Gem::Specification.new('mruby-bin-strip') do |spec| ...@@ -3,5 +3,4 @@ MRuby::Gem::Specification.new('mruby-bin-strip') do |spec|
spec.author = 'mruby developers' spec.author = 'mruby developers'
spec.summary = 'irep dump debug section remover command' spec.summary = 'irep dump debug section remover command'
spec.bins = %w(mruby-strip) spec.bins = %w(mruby-strip)
spec.add_dependency 'mruby-proc-ext', :core =>'mruby-proc-ext'
end end
...@@ -122,51 +122,6 @@ mrb_kernel_proc(mrb_state *mrb, mrb_value self) ...@@ -122,51 +122,6 @@ mrb_kernel_proc(mrb_state *mrb, mrb_value self)
return blk; return blk;
} }
static mrb_value
mrb_local_variables(mrb_state *mrb, mrb_value self)
{
mrb_value ret;
struct RProc *proc;
struct mrb_irep *irep;
size_t i;
proc = mrb->c->ci[-1].proc;
if (MRB_PROC_CFUNC_P(proc)) {
return mrb_ary_new(mrb);
}
irep = proc->body.irep;
if (!irep->lv) {
return mrb_ary_new(mrb);
}
ret = mrb_ary_new_capa(mrb, irep->nlocals - 1);
for (i = 0; i + 1 < irep->nlocals; ++i) {
if (irep->lv[i].name) {
mrb_ary_push(mrb, ret, mrb_symbol_value(irep->lv[i].name));
}
}
if (proc->env) {
struct REnv *e = proc->env;
while (e) {
if (!MRB_PROC_CFUNC_P(mrb->c->cibase[e->cioff].proc)) {
irep = mrb->c->cibase[e->cioff].proc->body.irep;
if (irep->lv) {
for (i = 0; i + 1 < irep->nlocals; ++i) {
if (irep->lv[i].name) {
mrb_ary_push(mrb, ret, mrb_symbol_value(irep->lv[i].name));
}
}
}
}
e = (struct REnv*)e->c;
}
}
return ret;
}
void void
mrb_mruby_proc_ext_gem_init(mrb_state* mrb) mrb_mruby_proc_ext_gem_init(mrb_state* mrb)
{ {
...@@ -178,7 +133,6 @@ mrb_mruby_proc_ext_gem_init(mrb_state* mrb) ...@@ -178,7 +133,6 @@ mrb_mruby_proc_ext_gem_init(mrb_state* mrb)
mrb_define_class_method(mrb, mrb->kernel_module, "proc", mrb_kernel_proc, MRB_ARGS_NONE()); mrb_define_class_method(mrb, mrb->kernel_module, "proc", mrb_kernel_proc, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb->kernel_module, "proc", mrb_kernel_proc, MRB_ARGS_NONE()); mrb_define_method(mrb, mrb->kernel_module, "proc", mrb_kernel_proc, MRB_ARGS_NONE());
mrb_define_module_function(mrb, mrb->kernel_module, "local_variables", mrb_local_variables, MRB_ARGS_NONE());
} }
void void
......
...@@ -74,17 +74,3 @@ assert('mrb_cfunc_env_get') do ...@@ -74,17 +74,3 @@ assert('mrb_cfunc_env_get') do
assert_equal 1, t.get_int(1) assert_equal 1, t.get_int(1)
end end
assert('Kernel.local_variables', '15.3.1.2.7') do
a, b = 0, 1
a += b
vars = Kernel.local_variables.sort
assert_equal [:a, :b, :vars], vars
proc {
c = 2
vars = Kernel.local_variables.sort
assert_equal [:a, :b, :c, :vars], vars
}.call
end
...@@ -1101,6 +1101,51 @@ mrb_obj_ceqq(mrb_state *mrb, mrb_value self) ...@@ -1101,6 +1101,51 @@ mrb_obj_ceqq(mrb_state *mrb, mrb_value self)
return mrb_false_value(); return mrb_false_value();
} }
static mrb_value
mrb_local_variables(mrb_state *mrb, mrb_value self)
{
mrb_value ret;
struct RProc *proc;
struct mrb_irep *irep;
size_t i;
proc = mrb->c->ci[-1].proc;
if (MRB_PROC_CFUNC_P(proc)) {
return mrb_ary_new(mrb);
}
irep = proc->body.irep;
if (!irep->lv) {
return mrb_ary_new(mrb);
}
ret = mrb_ary_new_capa(mrb, irep->nlocals - 1);
for (i = 0; i + 1 < irep->nlocals; ++i) {
if (irep->lv[i].name) {
mrb_ary_push(mrb, ret, mrb_symbol_value(irep->lv[i].name));
}
}
if (proc->env) {
struct REnv *e = proc->env;
while (e) {
if (!MRB_PROC_CFUNC_P(mrb->c->cibase[e->cioff].proc)) {
irep = mrb->c->cibase[e->cioff].proc->body.irep;
if (irep->lv) {
for (i = 0; i + 1 < irep->nlocals; ++i) {
if (irep->lv[i].name) {
mrb_ary_push(mrb, ret, mrb_symbol_value(irep->lv[i].name));
}
}
}
}
e = (struct REnv*)e->c;
}
}
return ret;
}
void void
mrb_init_kernel(mrb_state *mrb) mrb_init_kernel(mrb_state *mrb)
{ {
...@@ -1110,6 +1155,7 @@ mrb_init_kernel(mrb_state *mrb) ...@@ -1110,6 +1155,7 @@ mrb_init_kernel(mrb_state *mrb)
mrb_define_class_method(mrb, krn, "block_given?", mrb_f_block_given_p_m, MRB_ARGS_NONE()); /* 15.3.1.2.2 */ mrb_define_class_method(mrb, krn, "block_given?", mrb_f_block_given_p_m, MRB_ARGS_NONE()); /* 15.3.1.2.2 */
mrb_define_class_method(mrb, krn, "global_variables", mrb_f_global_variables, MRB_ARGS_NONE()); /* 15.3.1.2.4 */ mrb_define_class_method(mrb, krn, "global_variables", mrb_f_global_variables, MRB_ARGS_NONE()); /* 15.3.1.2.4 */
mrb_define_class_method(mrb, krn, "iterator?", mrb_f_block_given_p_m, MRB_ARGS_NONE()); /* 15.3.1.2.5 */ mrb_define_class_method(mrb, krn, "iterator?", mrb_f_block_given_p_m, MRB_ARGS_NONE()); /* 15.3.1.2.5 */
mrb_define_class_method(mrb, krn, "local_variables", mrb_local_variables, MRB_ARGS_NONE()); /* 15.3.1.2.7 */
; /* 15.3.1.2.11 */ ; /* 15.3.1.2.11 */
mrb_define_class_method(mrb, krn, "raise", mrb_f_raise, MRB_ARGS_ANY()); /* 15.3.1.2.12 */ mrb_define_class_method(mrb, krn, "raise", mrb_f_raise, MRB_ARGS_ANY()); /* 15.3.1.2.12 */
...@@ -1140,6 +1186,7 @@ mrb_init_kernel(mrb_state *mrb) ...@@ -1140,6 +1186,7 @@ mrb_init_kernel(mrb_state *mrb)
mrb_define_method(mrb, krn, "is_a?", mrb_obj_is_kind_of_m, MRB_ARGS_REQ(1)); /* 15.3.1.3.24 */ mrb_define_method(mrb, krn, "is_a?", mrb_obj_is_kind_of_m, MRB_ARGS_REQ(1)); /* 15.3.1.3.24 */
mrb_define_method(mrb, krn, "iterator?", mrb_f_block_given_p_m, MRB_ARGS_NONE()); /* 15.3.1.3.25 */ mrb_define_method(mrb, krn, "iterator?", mrb_f_block_given_p_m, MRB_ARGS_NONE()); /* 15.3.1.3.25 */
mrb_define_method(mrb, krn, "kind_of?", mrb_obj_is_kind_of_m, MRB_ARGS_REQ(1)); /* 15.3.1.3.26 */ mrb_define_method(mrb, krn, "kind_of?", mrb_obj_is_kind_of_m, MRB_ARGS_REQ(1)); /* 15.3.1.3.26 */
mrb_define_method(mrb, krn, "local_variables", mrb_local_variables, MRB_ARGS_NONE()); /* 15.3.1.3.28 */
mrb_define_method(mrb, krn, "methods", mrb_obj_methods_m, MRB_ARGS_OPT(1)); /* 15.3.1.3.31 */ mrb_define_method(mrb, krn, "methods", mrb_obj_methods_m, MRB_ARGS_OPT(1)); /* 15.3.1.3.31 */
mrb_define_method(mrb, krn, "nil?", mrb_false, MRB_ARGS_NONE()); /* 15.3.1.3.32 */ mrb_define_method(mrb, krn, "nil?", mrb_false, MRB_ARGS_NONE()); /* 15.3.1.3.32 */
mrb_define_method(mrb, krn, "object_id", mrb_obj_id_m, MRB_ARGS_NONE()); /* 15.3.1.3.33 */ mrb_define_method(mrb, krn, "object_id", mrb_obj_id_m, MRB_ARGS_NONE()); /* 15.3.1.3.33 */
......
...@@ -507,6 +507,20 @@ assert('Kernel#to_s', '15.3.1.3.46') do ...@@ -507,6 +507,20 @@ assert('Kernel#to_s', '15.3.1.3.46') do
assert_equal to_s.class, String assert_equal to_s.class, String
end end
assert('Kernel.local_variables', '15.3.1.2.7') do
a, b = 0, 1
a += b
vars = Kernel.local_variables.sort
assert_equal [:a, :b, :vars], vars
Proc.new {
c = 2
vars = Kernel.local_variables.sort
assert_equal [:a, :b, :c, :vars], vars
}.call
end
assert('Kernel#!=') do assert('Kernel#!=') do
str1 = "hello" str1 = "hello"
str2 = str1 str2 = str1
......
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