Merge pull request #3739 from ksss/array-shift

Should only check frozen fix #3737
This commit is contained in:
Yukihiro "Matz" Matsumoto
2017-07-09 19:58:11 +09:00
committed by GitHub
2 changed files with 13 additions and 3 deletions
+9 -3
View File
@@ -106,11 +106,17 @@ ary_fill_with_nil(mrb_value *ptr, mrb_int size)
}
static void
ary_modify(mrb_state *mrb, struct RArray *a)
ary_modify_check(mrb_state *mrb, struct RArray *a)
{
if (MRB_FROZEN_P(a)) {
mrb_raise(mrb, E_RUNTIME_ERROR, "can't modify frozen array");
}
}
static void
ary_modify(mrb_state *mrb, struct RArray *a)
{
ary_modify_check(mrb, a);
if (ARY_SHARED_P(a)) {
mrb_shared_array *shared = a->aux.shared;
@@ -445,7 +451,7 @@ mrb_ary_pop(mrb_state *mrb, mrb_value ary)
{
struct RArray *a = mrb_ary_ptr(ary);
ary_modify(mrb, a);
ary_modify_check(mrb, a);
if (a->len == 0) return mrb_nil_value();
return a->ptr[--a->len];
}
@@ -458,7 +464,7 @@ mrb_ary_shift(mrb_state *mrb, mrb_value self)
struct RArray *a = mrb_ary_ptr(self);
mrb_value val;
ary_modify(mrb, a);
ary_modify_check(mrb, a);
if (a->len == 0) return mrb_nil_value();
if (ARY_SHARED_P(a)) {
L_SHIFT:
+4
View File
@@ -237,6 +237,8 @@ assert('Array#pop', '15.2.12.5.21') do
assert_nil([].pop)
assert_equal([1,2], a)
assert_equal(3, b)
assert_raise(RuntimeError) { [].freeze.pop }
end
assert('Array#push', '15.2.12.5.22') do
@@ -284,6 +286,8 @@ assert('Array#shift', '15.2.12.5.27') do
assert_nil([].shift)
assert_equal([2,3], a)
assert_equal(1, b)
assert_raise(RuntimeError) { [].freeze.shift }
end
assert('Array#size', '15.2.12.5.28') do