Commit 794de331 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #3430 from ksss/local_variables

Kernel#local_variables: Make result array unique
parents f2b18a60 bd72dba8
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <mruby.h> #include <mruby.h>
#include <mruby/array.h> #include <mruby/array.h>
#include <mruby/hash.h>
#include <mruby/class.h> #include <mruby/class.h>
#include <mruby/proc.h> #include <mruby/proc.h>
#include <mruby/string.h> #include <mruby/string.h>
...@@ -1106,8 +1107,8 @@ mrb_obj_ceqq(mrb_state *mrb, mrb_value self) ...@@ -1106,8 +1107,8 @@ mrb_obj_ceqq(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)
{ {
mrb_value ret;
struct RProc *proc; struct RProc *proc;
mrb_value vars;
struct mrb_irep *irep; struct mrb_irep *irep;
size_t i; size_t i;
...@@ -1121,10 +1122,10 @@ mrb_local_variables(mrb_state *mrb, mrb_value self) ...@@ -1121,10 +1122,10 @@ mrb_local_variables(mrb_state *mrb, mrb_value self)
if (!irep->lv) { if (!irep->lv) {
return mrb_ary_new(mrb); return mrb_ary_new(mrb);
} }
ret = mrb_ary_new_capa(mrb, irep->nlocals - 1); vars = mrb_hash_new(mrb);
for (i = 0; i + 1 < irep->nlocals; ++i) { for (i = 0; i + 1 < irep->nlocals; ++i) {
if (irep->lv[i].name) { if (irep->lv[i].name) {
mrb_ary_push(mrb, ret, mrb_symbol_value(irep->lv[i].name)); mrb_hash_set(mrb, vars, mrb_symbol_value(irep->lv[i].name), mrb_true_value());
} }
} }
if (proc->env) { if (proc->env) {
...@@ -1137,7 +1138,7 @@ mrb_local_variables(mrb_state *mrb, mrb_value self) ...@@ -1137,7 +1138,7 @@ mrb_local_variables(mrb_state *mrb, mrb_value self)
if (irep->lv) { if (irep->lv) {
for (i = 0; i + 1 < irep->nlocals; ++i) { for (i = 0; i + 1 < irep->nlocals; ++i) {
if (irep->lv[i].name) { if (irep->lv[i].name) {
mrb_ary_push(mrb, ret, mrb_symbol_value(irep->lv[i].name)); mrb_hash_set(mrb, vars, mrb_symbol_value(irep->lv[i].name), mrb_true_value());
} }
} }
} }
...@@ -1146,7 +1147,7 @@ mrb_local_variables(mrb_state *mrb, mrb_value self) ...@@ -1146,7 +1147,7 @@ mrb_local_variables(mrb_state *mrb, mrb_value self)
} }
} }
return ret; return mrb_hash_keys(mrb, vars);
} }
void void
......
...@@ -549,11 +549,10 @@ assert('Kernel.local_variables', '15.3.1.2.7') do ...@@ -549,11 +549,10 @@ assert('Kernel.local_variables', '15.3.1.2.7') do
vars = Kernel.local_variables.sort vars = Kernel.local_variables.sort
assert_equal [:a, :b, :vars], vars assert_equal [:a, :b, :vars], vars
Proc.new { assert_equal [:a, :b, :c, :vars], Proc.new { |a, b|
c = 2 c = 2
vars = Kernel.local_variables.sort Kernel.local_variables.sort
assert_equal [:a, :b, :c, :vars], vars }.call(-1, -2)
}.call
end end
assert('Kernel#!=') do assert('Kernel#!=') do
......
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