Project

General

Profile

Feature #14680 ยป freeze.patch

kddeisz (Kevin Deisz), 04/11/2018 02:49 PM

View differences:

array.c
5998 5998
}
5999 5999

  
6000 6000
/*
6001
 * call-seq:
6002
 *   +ary  -> ary (mutable)
6003
 *
6004
 * If the array is frozen, then return duplicated mutable array.
6005
 *
6006
 * If the array is not frozen, then return the array itself.
6007
 */
6008
static VALUE
6009
rb_ary_uplus(VALUE ary)
6010
{
6011
    if (OBJ_FROZEN(ary)) {
6012
	return rb_ary_dup(ary);
6013
    }
6014
    else {
6015
	return ary;
6016
    }
6017
}
6018

  
6019
/*
6020
 * call-seq:
6021
 *   -ary  -> ary (frozen)
6022
 *
6023
 * If the array is frozen, then return the array itself.
6024
 *
6025
 * If the array is not frozen, return a frozen copy of it.
6026
 */
6027
static VALUE
6028
rb_ary_uminus(VALUE ary)
6029
{
6030
    if (OBJ_FROZEN(ary)) {
6031
	return ary;
6032
    }
6033
    else {
6034
	return rb_ary_freeze(ary);
6035
    }
6036
}
6037

  
6038
/*
6001 6039
 *  Arrays are ordered, integer-indexed collections of any object.
6002 6040
 *
6003 6041
 *  Array indexing starts at 0, as in C or Java.  A negative index is assumed
......
6358 6396
    rb_define_method(rb_cArray, "dig", rb_ary_dig, -1);
6359 6397
    rb_define_method(rb_cArray, "sum", rb_ary_sum, -1);
6360 6398

  
6399
    rb_define_method(rb_cArray, "+@", rb_ary_uplus, 0);
6400
    rb_define_method(rb_cArray, "-@", rb_ary_uminus, 0);
6401

  
6361 6402
    id_random = rb_intern("random");
6362 6403
}
hash.c
3179 3179
    return hash_le(other, hash);
3180 3180
}
3181 3181

  
3182
/*
3183
 * call-seq:
3184
 *   +hsh  -> hsh (mutable)
3185
 *
3186
 * If the hash is frozen, then return duplicated mutable hash.
3187
 *
3188
 * If the hash is not frozen, then return the hash itself.
3189
 */
3190
static VALUE
3191
rb_hash_uplus(VALUE hsh)
3192
{
3193
    if (OBJ_FROZEN(hsh)) {
3194
	return rb_hash_dup(hsh);
3195
    }
3196
    else {
3197
	return hsh;
3198
    }
3199
}
3200

  
3201
/*
3202
 * call-seq:
3203
 *   -hsh  -> hsh (frozen)
3204
 *
3205
 * If the hash is frozen, then return the hash itself.
3206
 *
3207
 * If the hash is not frozen, return a frozen copy of it.
3208
 */
3209
static VALUE
3210
rb_hash_uminus(VALUE hsh)
3211
{
3212
    if (OBJ_FROZEN(hsh)) {
3213
	return hsh;
3214
    }
3215
    else {
3216
	return rb_hash_freeze(hsh);
3217
    }
3218
}
3219

  
3182 3220
static VALUE
3183 3221
hash_proc_call(VALUE key, VALUE hash, int argc, const VALUE *argv, VALUE passed_proc)
3184 3222
{
......
4725 4763
    rb_define_method(rb_cHash, ">=", rb_hash_ge, 1);
4726 4764
    rb_define_method(rb_cHash, ">", rb_hash_gt, 1);
4727 4765

  
4766
    rb_define_method(rb_cHash, "+@", rb_hash_uplus, 0);
4767
    rb_define_method(rb_cHash, "-@", rb_hash_uminus, 0);
4768

  
4728 4769
    /* Document-class: ENV
4729 4770
     *
4730 4771
     * ENV is a hash-like accessor for environment variables.
test/ruby/test_array.rb
2988 2988
    assert_raise(TypeError) {[1].sum("")}
2989 2989
  end
2990 2990

  
2991
  def test_uplus
2992
    a = [1, 2, 3].freeze
2993

  
2994
    assert a.frozen?
2995
    refute (+a).frozen?
2996

  
2997
    a = [1, 2, 3]
2998
    assert_equal a, +a
2999
  end
3000

  
3001
  def test_uminus
3002
    a = [1, 2, 3]
3003

  
3004
    refute a.frozen?
3005
    assert (-a).frozen?
3006

  
3007
    a = [1, 2, 3].freeze
3008
    assert_equal a, -a
3009
  end
3010

  
2991 3011
  private
2992 3012
  def need_continuation
2993 3013
    unless respond_to?(:callcc, true)
test/ruby/test_hash.rb
1652 1652
    assert_equal(0, 1_000_000.times.count{a=Object.new.hash; b=Object.new.hash; 0 + a + b != 0 + b + a}, bug14218)
1653 1653
  end
1654 1654

  
1655
  def test_uplus
1656
    a = { a: 1, b: 2, c: 3 }.freeze
1657

  
1658
    assert a.frozen?
1659
    refute (+a).frozen?
1660

  
1661
    a = { a: 1, b: 2, c: 3 }
1662
    assert_equal a, +a
1663
  end
1664

  
1665
  def test_uminus
1666
    a = { a: 1, b: 2, c: 3 }
1667

  
1668
    refute a.frozen?
1669
    assert (-a).frozen?
1670

  
1671
    a = { a: 1, b: 2, c: 3 }.freeze
1672
    assert_equal a, -a
1673
  end
1674

  
1655 1675
  class TestSubHash < TestHash
1656 1676
    class SubHash < Hash
1657 1677
      def reject(*)