diff options
Diffstat (limited to 'hash.c')
| -rw-r--r-- | hash.c | 900 |
1 files changed, 521 insertions, 379 deletions
@@ -71,10 +71,6 @@ #define HASH_DEBUG 0 #endif -#if HASH_DEBUG -#include "internal/gc.h" -#endif - #define SET_DEFAULT(hash, ifnone) ( \ FL_UNSET_RAW(hash, RHASH_PROC_DEFAULT), \ RHASH_SET_IFNONE(hash, ifnone)) @@ -184,7 +180,7 @@ any_hash(VALUE a, st_index_t (*other_func)(VALUE)) hnum = rb_hash_start(hnum); } else { - hnum = RSYMBOL(a)->hashval; + hnum = RSHIFT(RSYMBOL(a)->hashval, 1); } break; case T_FIXNUM: @@ -325,40 +321,35 @@ objid_hash(VALUE obj) #endif } -/** +/* * call-seq: - * obj.hash -> integer - * - * Generates an Integer hash value for this object. This function must have the - * property that <code>a.eql?(b)</code> implies <code>a.hash == b.hash</code>. + * hash -> integer * - * The hash value is used along with #eql? by the Hash class to determine if - * two objects reference the same hash key. Any hash value that exceeds the - * capacity of an Integer will be truncated before being used. + * Returns the integer hash value for +self+; + * has the property that if <tt>foo.eql?(bar)</tt> + * then <tt>foo.hash == bar.hash</tt>. * - * The hash value for an object may not be identical across invocations or - * implementations of Ruby. If you need a stable identifier across Ruby - * invocations and implementations you will need to generate one with a custom - * method. + * \Class Hash uses both #hash and #eql? to determine whether two objects + * used as hash keys are to be treated as the same key. + * A hash value that exceeds the capacity of an Integer is truncated before being used. * - * Certain core classes such as Integer use built-in hash calculations and - * do not call the #hash method when used as a hash key. + * Many core classes override method Object#hash; + * other core classes (e.g., Integer) calculate the hash internally, + * and do not call the #hash method when used as a hash key. * - * When implementing your own #hash based on multiple values, the best - * practice is to combine the class and any values using the hash code of an - * array: - * - * For example: + * When implementing #hash for a user-defined class, + * best practice is to use Array#hash with the class name and the values + * that are important in the instance; + * this takes advantage of that method's logic for safely and efficiently + * generating a hash value: * * def hash * [self.class, a, b, c].hash * end * - * The reason for this is that the Array#hash method already has logic for - * safely and efficiently combining multiple hash values. - *-- - * \private - *++ + * The hash value may differ among invocations or implementations of Ruby. + * If you need stable hash-like identifiers across Ruby invocations and implementations, + * use a custom method to generate them. */ VALUE rb_obj_hash(VALUE obj) @@ -480,58 +471,11 @@ ar_set_entry(VALUE hash, unsigned int index, st_data_t key, st_data_t val, st_ha #define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \ RHASH_AR_TABLE_SIZE_RAW(h)) -#define RHASH_AR_TABLE_BOUND_RAW(h) \ - ((unsigned int)((RBASIC(h)->flags >> RHASH_AR_TABLE_BOUND_SHIFT) & \ - (RHASH_AR_TABLE_BOUND_MASK >> RHASH_AR_TABLE_BOUND_SHIFT))) - -#define RHASH_ST_TABLE_SET(h, s) rb_hash_st_table_set(h, s) -#define RHASH_TYPE(hash) (RHASH_AR_TABLE_P(hash) ? &objhash : RHASH_ST_TABLE(hash)->type) - #define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(HASH_DEBUG, expr, #expr) -static inline unsigned int -RHASH_AR_TABLE_BOUND(VALUE h) -{ - HASH_ASSERT(RHASH_AR_TABLE_P(h)); - const unsigned int bound = RHASH_AR_TABLE_BOUND_RAW(h); - HASH_ASSERT(bound <= RHASH_AR_TABLE_MAX_SIZE); - return bound; -} - #if HASH_DEBUG #define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__) -void -rb_hash_dump(VALUE hash) -{ - rb_obj_info_dump(hash); - - if (RHASH_AR_TABLE_P(hash)) { - unsigned i, bound = RHASH_AR_TABLE_BOUND(hash); - - fprintf(stderr, " size:%u bound:%u\n", - RHASH_AR_TABLE_SIZE(hash), bound); - - for (i=0; i<bound; i++) { - st_data_t k, v; - - if (!ar_cleared_entry(hash, i)) { - char b1[0x100], b2[0x100]; - ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i); - k = pair->key; - v = pair->val; - fprintf(stderr, " %d key:%s val:%s hint:%02x\n", i, - rb_raw_obj_info(b1, 0x100, k), - rb_raw_obj_info(b2, 0x100, v), - ar_hint(hash, i)); - } - else { - fprintf(stderr, " %d empty\n", i); - } - } - } -} - static VALUE hash_verify_(VALUE hash, const char *file, int line) { @@ -584,7 +528,7 @@ hash_st_table_init(VALUE hash, const struct st_hash_type *type, st_index_t size) RHASH_SET_ST_FLAG(hash); } -void +static void rb_hash_st_table_set(VALUE hash, st_table *st) { HASH_ASSERT(st != NULL); @@ -644,6 +588,7 @@ RHASH_AR_TABLE_SIZE_DEC(VALUE h) static inline void RHASH_AR_TABLE_CLEAR(VALUE h) { + RUBY_ASSERT(rb_gc_obj_slot_size(h) >= sizeof(struct RHash) + sizeof(ar_table)); RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK; RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK; @@ -759,6 +704,8 @@ ar_force_convert_table(VALUE hash, const char *file, int line) st_hash_t hashes[RHASH_AR_TABLE_MAX_SIZE]; unsigned int bound, size; + RUBY_ASSERT(rb_gc_obj_slot_size(hash) >= sizeof(struct RHash) + sizeof(ar_table)); + // prepare hash values do { st_data_t keys[RHASH_AR_TABLE_MAX_SIZE]; @@ -783,7 +730,7 @@ ar_force_convert_table(VALUE hash, const char *file, int line) st_init_existing_table_with_size(new_tab, &objhash, size); ar_each_key(ar, bound, ar_each_key_insert, NULL, new_tab, hashes); hash_ar_free_and_clear_table(hash); - RHASH_ST_TABLE_SET(hash, new_tab); + rb_hash_st_table_set(hash, new_tab); return RHASH_ST_TABLE(hash); } } @@ -880,10 +827,11 @@ ar_general_foreach(VALUE hash, st_foreach_check_callback_func *func, st_update_c return 0; case ST_REPLACE: if (replace) { - retval = (*replace)(&key, &val, arg, TRUE); + (*replace)(&key, &val, arg, TRUE); + + // Pair should not have moved + HASH_ASSERT(pair == RHASH_AR_TABLE_REF(hash, i)); - // TODO: pair should be same as pair before. - pair = RHASH_AR_TABLE_REF(hash, i); pair->key = (VALUE)key; pair->val = (VALUE)val; } @@ -951,7 +899,7 @@ ar_foreach_check(VALUE hash, st_foreach_check_callback_func *func, st_data_t arg if (pair->key == never) break; ret = ar_find_entry_hint(hash, hint, key); if (ret == RHASH_AR_TABLE_MAX_BOUND) { - retval = (*func)(0, 0, arg, 1); + (*func)(0, 0, arg, 1); return 2; } } @@ -1181,12 +1129,15 @@ ar_values(VALUE hash, st_data_t *values, st_index_t size) static ar_table* ar_copy(VALUE hash1, VALUE hash2) { + RUBY_ASSERT(rb_gc_obj_slot_size(hash1) >= sizeof(struct RHash) + sizeof(ar_table)); ar_table *old_tab = RHASH_AR_TABLE(hash2); ar_table *new_tab = RHASH_AR_TABLE(hash1); - *new_tab = *old_tab; + unsigned int bound = RHASH_AR_TABLE_BOUND(hash2); + new_tab->ar_hint.word = old_tab->ar_hint.word; + MEMCPY(&new_tab->pairs, &old_tab->pairs, ar_table_pair, bound); RHASH_AR_TABLE(hash1)->ar_hint.word = RHASH_AR_TABLE(hash2)->ar_hint.word; - RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2)); + RHASH_AR_TABLE_BOUND_SET(hash1, bound); RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2)); rb_gc_writebarrier_remember(hash1); @@ -1212,17 +1163,13 @@ hash_st_free(VALUE hash) { HASH_ASSERT(RHASH_ST_TABLE_P(hash)); - st_table *tab = RHASH_ST_TABLE(hash); - - xfree(tab->bins); - xfree(tab->entries); + rb_st_free_embedded_table(RHASH_ST_TABLE(hash)); } static void hash_st_free_and_clear_table(VALUE hash) { hash_st_free(hash); - RHASH_ST_CLEAR(hash); } @@ -1300,7 +1247,6 @@ hash_ar_foreach_iter(st_data_t key, st_data_t value, st_data_t argp, int error) if (error) return ST_STOP; int status = (*arg->func)((VALUE)key, (VALUE)value, arg->arg); - /* TODO: rehash check? rb_raise(rb_eRuntimeError, "rehash occurred during iteration"); */ return hash_iter_status_check(status); } @@ -1312,13 +1258,8 @@ hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp, int error) if (error) return ST_STOP; - st_table *tbl = RHASH_ST_TABLE(arg->hash); int status = (*arg->func)((VALUE)key, (VALUE)value, arg->arg); - if (RHASH_ST_TABLE(arg->hash) != tbl) { - rb_raise(rb_eRuntimeError, "rehash occurred during iteration"); - } - return hash_iter_status_check(status); } @@ -1404,6 +1345,7 @@ hash_foreach_ensure(VALUE hash) return 0; } +/* This does not manage iteration level */ int rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg) { @@ -1415,6 +1357,7 @@ rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg } } +/* This does not manage iteration level */ int rb_hash_stlike_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg) { @@ -1478,14 +1421,9 @@ compact_after_delete(VALUE hash) static VALUE hash_alloc_flags(VALUE klass, VALUE flags, VALUE ifnone, bool st) { - const VALUE wb = (RGENGC_WB_PROTECTED_HASH ? FL_WB_PROTECTED : 0); const size_t size = sizeof(struct RHash) + (st ? sizeof(st_table) : sizeof(ar_table)); - - NEWOBJ_OF(hash, struct RHash, klass, T_HASH | wb | flags, size, 0); - - RHASH_SET_IFNONE((VALUE)hash, ifnone); - - return (VALUE)hash; + VALUE hash = rb_newobj_of(klass, T_HASH | flags, size); + return rb_hash_set_ifnone(hash, ifnone); } static VALUE @@ -1537,9 +1475,30 @@ rb_hash_new_capa(long capa) return rb_hash_new_with_size((st_index_t)capa); } +VALUE +rb_hash_alloc_fixed_size(VALUE klass, st_index_t size) +{ + VALUE ret; + if (size > RHASH_AR_TABLE_MAX_SIZE) { + ret = hash_alloc_flags(klass, 0, Qnil, true); + hash_st_table_init(ret, &objhash, size); + } + else { + size_t slot_size = sizeof(struct RHash) + offsetof(ar_table, pairs) + size * sizeof(ar_table_pair); + ret = rb_newobj_of(klass, T_HASH, slot_size); + } + + RHASH_SET_IFNONE(ret, Qnil); + return ret; +} + static VALUE hash_copy(VALUE ret, VALUE hash) { + if (rb_hash_compare_by_id_p(hash)) { + rb_gc_register_pinning_obj(ret); + } + if (RHASH_AR_TABLE_P(hash)) { if (RHASH_AR_TABLE_P(ret)) { ar_copy(ret, hash); @@ -1595,10 +1554,10 @@ VALUE rb_hash_dup(VALUE hash) { const VALUE flags = RBASIC(hash)->flags; - VALUE ret = hash_dup(hash, rb_obj_class(hash), - flags & (FL_EXIVAR|RHASH_PROC_DEFAULT)); - if (flags & FL_EXIVAR) - rb_copy_generic_ivar(ret, hash); + VALUE ret = hash_dup(hash, rb_obj_class(hash), flags & RHASH_PROC_DEFAULT); + + rb_copy_generic_ivar(ret, hash); + return ret; } @@ -1721,14 +1680,14 @@ tbl_update(VALUE hash, VALUE key, tbl_update_func func, st_data_t optional_arg) .func = func, .hash = hash, .key = key, - .value = (VALUE)optional_arg, + .value = 0 }; int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg); /* write barrier */ RB_OBJ_WRITTEN(hash, Qundef, arg.key); - RB_OBJ_WRITTEN(hash, Qundef, arg.value); + if (arg.value) RB_OBJ_WRITTEN(hash, Qundef, arg.value); return ret; } @@ -1981,6 +1940,9 @@ rb_hash_rehash_i(VALUE key, VALUE value, VALUE arg) else { st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value); } + + RB_OBJ_WRITTEN(arg, Qundef, key); + RB_OBJ_WRITTEN(arg, Qundef, value); return ST_CONTINUE; } @@ -2024,7 +1986,7 @@ rb_hash_rehash(VALUE hash) rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp); hash_st_free(hash); - RHASH_ST_TABLE_SET(hash, tbl); + rb_hash_st_table_set(hash, tbl); RHASH_ST_CLEAR(tmp); } hash_verify(hash); @@ -2253,7 +2215,7 @@ rb_hash_default(int argc, VALUE *argv, VALUE hash) * See {Hash Default}[rdoc-ref:Hash@Hash+Default]. */ -static VALUE +VALUE rb_hash_set_default(VALUE hash, VALUE ifnone) { rb_hash_modify_check(hash); @@ -2683,9 +2645,9 @@ rb_hash_slice(int argc, VALUE *argv, VALUE hash) * Returns a copy of +self+ that excludes entries for the given +keys+; * any +keys+ that are not found are ignored: * - * h = {foo:0, bar: 1, baz: 2} # => {:foo=>0, :bar=>1, :baz=>2} - * h.except(:baz, :foo) # => {:bar=>1} - * h.except(:bar, :nosuch) # => {:foo=>0, :baz=>2} + * h = {foo:0, bar: 1, baz: 2} # => {foo: 0, bar: 1, baz: 2} + * h.except(:baz, :foo) # => {bar: 1} + * h.except(:bar, :nosuch) # => {foo: 0, baz: 2} * * Related: see {Methods for Deleting}[rdoc-ref:Hash@Methods+for+Deleting]. */ @@ -2711,13 +2673,17 @@ rb_hash_except(int argc, VALUE *argv, VALUE hash) * call-seq: * values_at(*keys) -> new_array * - * Returns a new Array containing values for the given +keys+: + * Returns a new array containing values for the given +keys+: + * * h = {foo: 0, bar: 1, baz: 2} * h.values_at(:baz, :foo) # => [2, 0] * * The {hash default}[rdoc-ref:Hash@Hash+Default] is returned * for each key that is not found: + * * h.values_at(:hello, :foo) # => [nil, 0] + * + * Related: see {Methods for Fetching}[rdoc-ref:Hash@Methods+for+Fetching]. */ static VALUE @@ -2914,7 +2880,7 @@ hash_aset(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing) VALUE rb_hash_key_str(VALUE key) { - if (!RB_FL_ANY_RAW(key, FL_EXIVAR) && RBASIC_CLASS(key) == rb_cString) { + if (!rb_obj_gen_fields_p(key) && RBASIC_CLASS(key) == rb_cString) { return rb_fstring(key); } else { @@ -2958,7 +2924,7 @@ NOINSERT_UPDATE_CALLBACK(hash_aset_str) * h = {foo: 0, bar: 1} * h[:baz] = 2 # => 2 * h[:baz] # => 2 - * h # => {:foo=>0, :bar=>1, :baz=>2} + * h # => {foo: 0, bar: 1, baz: 2} * * Related: #[]; see also {Methods for Assigning}[rdoc-ref:Hash@Methods+for+Assigning]. */ @@ -2989,6 +2955,14 @@ rb_hash_aset(VALUE hash, VALUE key, VALUE val) * h = {foo: 0, bar: 1, baz: 2} * h.replace({bat: 3, bam: 4}) # => {bat: 3, bam: 4} * + * Also replaces the default value or proc of +self+ with the default value + * or proc of +other_hash+. + * + * h = {} + * other = Hash.new(:ok) + * h.replace(other) + * h.default # => :ok + * * Related: see {Methods for Assigning}[rdoc-ref:Hash@Methods+for+Assigning]. */ @@ -3325,13 +3299,97 @@ static int flatten_i(VALUE key, VALUE val, VALUE ary); /* * call-seq: - * transform_keys! {|key| ... } -> self - * transform_keys!(hash2) -> self - * transform_keys!(hash2) {|other_key| ...} -> self + * transform_keys! {|old_key| ... } -> self + * transform_keys!(other_hash) -> self + * transform_keys!(other_hash) {|old_key| ...} -> self * transform_keys! -> new_enumerator * - * Same as Hash#transform_keys but modifies the receiver in place - * instead of returning a new hash. + * With an argument, a block, or both given, + * derives keys from the argument, the block, and +self+; + * all, some, or none of the keys in +self+ may be changed. + * + * With a block given and no argument, + * derives keys only from the block; + * all, some, or none of the keys in +self+ may be changed. + * + * For each key/value pair <tt>old_key/value</tt> in +self+, calls the block with +old_key+; + * the block's return value becomes +new_key+; + * removes the entry for +old_key+: <tt>self.delete(old_key)</tt>; + * sets <tt>self[new_key] = value</tt>; + * a duplicate key overwrites: + * + * h = {foo: 0, bar: 1, baz: 2} + * h.transform_keys! {|old_key| old_key.to_s } + * # => {"foo" => 0, "bar" => 1, "baz" => 2} + * h = {foo: 0, bar: 1, baz: 2} + * h.transform_keys! {|old_key| 'xxx' } + * # => {"xxx" => 2} + * + * With argument +other_hash+ given and no block, + * derives keys for +self+ from +other_hash+ and +self+; + * all, some, or none of the keys in +self+ may be changed. + * + * For each key/value pair <tt>old_key/old_value</tt> in +self+, + * looks for key +old_key+ in +other_hash+: + * + * - If +old_key+ is found, takes value <tt>other_hash[old_key]</tt> as +new_key+; + * removes the entry for +old_key+: <tt>self.delete(old_key)</tt>; + * sets <tt>self[new_key] = value</tt>; + * a duplicate key overwrites: + * + * h = {foo: 0, bar: 1, baz: 2} + * h.transform_keys!(baz: :BAZ, bar: :BAR, foo: :FOO) + * # => {FOO: 0, BAR: 1, BAZ: 2} + * h = {foo: 0, bar: 1, baz: 2} + * h.transform_keys!(baz: :FOO, bar: :FOO, foo: :FOO) + * # => {FOO: 2} + * + * - If +old_key+ is not found, does nothing: + * + * h = {foo: 0, bar: 1, baz: 2} + * h.transform_keys!({}) + * # => {foo: 0, bar: 1, baz: 2} + * h.transform_keys!(baz: :foo) + * # => {foo: 2, bar: 1} + * + * Unused keys in +other_hash+ are ignored: + * + * h = {foo: 0, bar: 1, baz: 2} + * h.transform_keys!(bat: 3) + * # => {foo: 0, bar: 1, baz: 2} + * + * With both argument +other_hash+ and a block given, + * derives keys from +other_hash+, the block, and +self+; + * all, some, or none of the keys in +self+ may be changed. + * + * For each pair +old_key+ and +value+ in +self+: + * + * - If +other_hash+ has key +old_key+ (with value +new_key+), + * does not call the block for that key; + * removes the entry for +old_key+: <tt>self.delete(old_key)</tt>; + * sets <tt>self[new_key] = value</tt>; + * a duplicate key overwrites: + * + * h = {foo: 0, bar: 1, baz: 2} + * h.transform_keys!(baz: :BAZ, bar: :BAR, foo: :FOO) {|key| fail 'Not called' } + * # => {FOO: 0, BAR: 1, BAZ: 2} + * + * - If +other_hash+ does not have key +old_key+, + * calls the block with +old_key+ and takes its return value as +new_key+; + * removes the entry for +old_key+: <tt>self.delete(old_key)</tt>; + * sets <tt>self[new_key] = value</tt>; + * a duplicate key overwrites: + * + * h = {foo: 0, bar: 1, baz: 2} + * h.transform_keys!(baz: :BAZ) {|key| key.to_s.reverse } + * # => {"oof" => 0, "rab" => 1, BAZ: 2} + * h = {foo: 0, bar: 1, baz: 2} + * h.transform_keys!(baz: :BAZ) {|key| 'ook' } + * # => {"ook" => 1, BAZ: 2} + * + * With no argument and no block given, returns a new Enumerator. + * + * Related: see {Methods for Transforming Keys and Values}[rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values]. */ static VALUE rb_hash_transform_keys_bang(int argc, VALUE *argv, VALUE hash) @@ -3398,25 +3456,37 @@ transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t arg return ST_CONTINUE; } +static VALUE +transform_values_call(VALUE hash) +{ + rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash); + return hash; +} + +static void +transform_values(VALUE hash) +{ + hash_iter_lev_inc(hash); + rb_ensure(transform_values_call, hash, hash_foreach_ensure, hash); +} + /* * call-seq: * transform_values {|value| ... } -> new_hash * transform_values -> new_enumerator * - * Returns a new +Hash+ object; each entry has: - * * A key from +self+. - * * A value provided by the block. + * With a block given, returns a new hash +new_hash+; + * for each pair +key+/+value+ in +self+, + * calls the block with +value+ and captures its return as +new_value+; + * adds to +new_hash+ the entry +key+/+new_value+: * - * Transform values: * h = {foo: 0, bar: 1, baz: 2} * h1 = h.transform_values {|value| value * 100} * h1 # => {foo: 0, bar: 100, baz: 200} * - * Returns a new Enumerator if no block given: - * h = {foo: 0, bar: 1, baz: 2} - * e = h.transform_values # => #<Enumerator: {foo: 0, bar: 1, baz: 2}:transform_values> - * h1 = e.each { |value| value * 100} - * h1 # => {foo: 0, bar: 100, baz: 200} + * With no block given, returns a new Enumerator. + * + * Related: see {Methods for Transforming Keys and Values}[rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values]. */ static VALUE rb_hash_transform_values(VALUE hash) @@ -3428,7 +3498,7 @@ rb_hash_transform_values(VALUE hash) SET_DEFAULT(result, Qnil); if (!RHASH_EMPTY_P(hash)) { - rb_hash_stlike_foreach_with_replace(result, transform_values_foreach_func, transform_values_foreach_replace, result); + transform_values(result); compact_after_delete(result); } @@ -3437,18 +3507,24 @@ rb_hash_transform_values(VALUE hash) /* * call-seq: - * transform_values! {|value| ... } -> self + * transform_values! {|old_value| ... } -> self * transform_values! -> new_enumerator * - * Returns +self+, whose keys are unchanged, and whose values are determined by the given block. + * + * With a block given, changes the values of +self+ as determined by the block; + * returns +self+. + * + * For each entry +key+/+old_value+ in +self+, + * calls the block with +old_value+, + * captures its return value as +new_value+, + * and sets <tt>self[key] = new_value</tt>: + * * h = {foo: 0, bar: 1, baz: 2} * h.transform_values! {|value| value * 100} # => {foo: 0, bar: 100, baz: 200} * - * Returns a new Enumerator if no block given: - * h = {foo: 0, bar: 1, baz: 2} - * e = h.transform_values! # => #<Enumerator: {foo: 0, bar: 100, baz: 200}:transform_values!> - * h1 = e.each {|value| value * 100} - * h1 # => {foo: 0, bar: 100, baz: 200} + * With no block given, returns a new Enumerator. + * + * Related: see {Methods for Transforming Keys and Values}[rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values]. */ static VALUE rb_hash_transform_values_bang(VALUE hash) @@ -3457,7 +3533,7 @@ rb_hash_transform_values_bang(VALUE hash) rb_hash_modify_check(hash); if (!RHASH_TABLE_EMPTY_P(hash)) { - rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash); + transform_values(hash); } return hash; @@ -3504,24 +3580,24 @@ symbol_key_needs_quote(VALUE str) if (first == '@' || first == '$' || first == '!') return true; if (!at_char_boundary(s, s + len - 1, RSTRING_END(str), rb_enc_get(str))) return false; switch (s[len - 1]) { - case '+': - case '-': - case '*': - case '/': - case '`': - case '%': - case '^': - case '&': - case '|': - case ']': - case '<': - case '=': - case '>': - case '~': - case '@': - return true; - default: - return false; + case '+': + case '-': + case '*': + case '/': + case '`': + case '%': + case '^': + case '&': + case '|': + case ']': + case '<': + case '=': + case '>': + case '~': + case '@': + return true; + default: + return false; } } @@ -3730,9 +3806,12 @@ values_i(VALUE key, VALUE value, VALUE ary) * call-seq: * values -> new_array * - * Returns a new Array containing all values in +self+: + * Returns a new array containing all values in +self+: + * * h = {foo: 0, bar: 1, baz: 2} * h.values # => [0, 1, 2] + * + * Related: see {Methods for Fetching}[rdoc-ref:Hash@Methods+for+Fetching]. */ VALUE @@ -3760,7 +3839,6 @@ rb_hash_values(VALUE hash) } rb_ary_set_len(values, size); } - else { rb_hash_foreach(hash, values_i, values); } @@ -3802,9 +3880,10 @@ rb_hash_search_value(VALUE key, VALUE value, VALUE arg) /* * call-seq: * has_value?(value) -> true or false - * value?(value) -> true or false * - * Returns +true+ if +value+ is a value in +self+, otherwise +false+. + * Returns whether +value+ is a value in +self+. + * + * Related: {Methods for Querying}[rdoc-ref:Hash@Methods+for+Querying]. */ static VALUE @@ -3901,17 +3980,13 @@ hash_equal(VALUE hash1, VALUE hash2, int eql) /* * call-seq: - * self == object -> true or false - * - * Returns whether +self+ and +object+ are equal. - * - * Returns +true+ if all of the following are true: + * self == other -> true or false * - * - +object+ is a +Hash+ object (or can be converted to one). - * - +self+ and +object+ have the same keys (regardless of order). - * - For each key +key+, <tt>self[key] == object[key]</tt>. + * Returns whether all of the following are true: * - * Otherwise, returns +false+. + * - +other+ is a +Hash+ object (or can be converted to one). + * - +self+ and +other+ have the same keys (regardless of order). + * - For each key +key+, <tt>self[key] == other[key]</tt>. * * Examples: * @@ -3920,8 +3995,8 @@ hash_equal(VALUE hash1, VALUE hash2, int eql) * h == {bar: 1, foo: 0} # => true # Equal entries (different order). * h == 1 # => false # Object not a hash. * h == {} # => false # Different number of entries. - * h == {foo: 0, bar: 1} # => false # Different key. - * h == {foo: 0, bar: 1} # => false # Different value. + * h == {foo: 0, bat: 1} # => false # Different key. + * h == {foo: 0, bar: 2} # => false # Different value. * * Related: see {Methods for Comparing}[rdoc-ref:Hash@Methods+for+Comparing]. */ @@ -4043,42 +4118,109 @@ rb_hash_update_i(VALUE key, VALUE value, VALUE hash) return ST_CONTINUE; } +struct update_call_args { + VALUE hash, newvalue, *argv; + int argc; + bool block_given; + bool iterating; +}; + static int rb_hash_update_block_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing) { - st_data_t newvalue = arg->arg; + VALUE k = (VALUE)*key, v = (VALUE)*value; + struct update_call_args *ua = (void *)arg->arg; + VALUE newvalue = ua->newvalue, hash = arg->hash; if (existing) { - newvalue = (st_data_t)rb_yield_values(3, (VALUE)*key, (VALUE)*value, (VALUE)newvalue); + hash_iter_lev_inc(hash); + ua->iterating = true; + newvalue = rb_yield_values(3, k, v, newvalue); + hash_iter_lev_dec(hash); + ua->iterating = false; } - else if (RHASH_STRING_KEY_P(arg->hash, *key) && !RB_OBJ_FROZEN(*key)) { - *key = rb_hash_key_str(*key); + else if (RHASH_STRING_KEY_P(hash, k) && !RB_OBJ_FROZEN(k)) { + *key = (st_data_t)rb_hash_key_str(k); } - *value = newvalue; + *value = (st_data_t)newvalue; return ST_CONTINUE; } NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback) static int -rb_hash_update_block_i(VALUE key, VALUE value, VALUE hash) +rb_hash_update_block_i(VALUE key, VALUE value, VALUE args) { - RHASH_UPDATE(hash, key, rb_hash_update_block_callback, value); + struct update_call_args *ua = (void *)args; + ua->newvalue = value; + RHASH_UPDATE(ua->hash, key, rb_hash_update_block_callback, args); return ST_CONTINUE; } +static VALUE +rb_hash_update_call(VALUE args) +{ + struct update_call_args *arg = (void *)args; + + for (int i = 0; i < arg->argc; i++){ + VALUE hash = to_hash(arg->argv[i]); + if (arg->block_given) { + rb_hash_foreach(hash, rb_hash_update_block_i, args); + } + else { + rb_hash_foreach(hash, rb_hash_update_i, arg->hash); + } + } + return arg->hash; +} + +static VALUE +rb_hash_update_ensure(VALUE args) +{ + struct update_call_args *ua = (void *)args; + if (ua->iterating) hash_iter_lev_dec(ua->hash); + return Qnil; +} + /* * call-seq: * update(*other_hashes) -> self * update(*other_hashes) { |key, old_value, new_value| ... } -> self * - * Like Hash#merge, but modifies and returns +self+ instead of a new hash: + * Updates values and/or adds entries to +self+; returns +self+. + * + * Each argument +other_hash+ in +other_hashes+ must be a hash. + * + * With no block given, for each successive entry +key+/+new_value+ in each successive +other_hash+: + * + * - If +key+ is in +self+, sets <tt>self[key] = new_value</tt>, whose position is unchanged: + * + * h0 = {foo: 0, bar: 1, baz: 2} + * h1 = {bar: 3, foo: -1} + * h0.update(h1) # => {foo: -1, bar: 3, baz: 2} + * + * - If +key+ is not in +self+, adds the entry at the end of +self+: * - * season = {AB: 75, H: 20, HR: 3, SO: 17, W: 11, HBP: 3} - * today = {AB: 3, H: 1, W: 1} - * yesterday = {AB: 4, H: 2, HR: 1} - * season.update(yesterday, today) {|key, old_value, new_value| old_value + new_value } - * # => {AB: 82, H: 23, HR: 4, SO: 17, W: 12, HBP: 3} + * h = {foo: 0, bar: 1, baz: 2} + * h.update({bam: 3, bah: 4}) # => {foo: 0, bar: 1, baz: 2, bam: 3, bah: 4} + * + * With a block given, for each successive entry +key+/+new_value+ in each successive +other_hash+: + * + * - If +key+ is in +self+, fetches +old_value+ from <tt>self[key]</tt>, + * calls the block with +key+, +old_value+, and +new_value+, + * and sets <tt>self[key] = new_value</tt>, whose position is unchanged : + * + * season = {AB: 75, H: 20, HR: 3, SO: 17, W: 11, HBP: 3} + * today = {AB: 3, H: 1, W: 1} + * yesterday = {AB: 4, H: 2, HR: 1} + * season.update(yesterday, today) {|key, old_value, new_value| old_value + new_value } + * # => {AB: 82, H: 23, HR: 4, SO: 17, W: 12, HBP: 3} + * + * - If +key+ is not in +self+, adds the entry at the end of +self+: + * + * h = {foo: 0, bar: 1, baz: 2} + * h.update({bat: 3}) { fail 'Cannot happen' } + * # => {foo: 0, bar: 1, baz: 2, bat: 3} * * Related: see {Methods for Assigning}[rdoc-ref:Hash@Methods+for+Assigning]. */ @@ -4086,20 +4228,17 @@ rb_hash_update_block_i(VALUE key, VALUE value, VALUE hash) static VALUE rb_hash_update(int argc, VALUE *argv, VALUE self) { - int i; - bool block_given = rb_block_given_p(); + struct update_call_args args = { + .hash = self, + .argv = argv, + .argc = argc, + .block_given = rb_block_given_p(), + .iterating = false, + }; + VALUE arg = (VALUE)&args; rb_hash_modify(self); - for (i = 0; i < argc; i++){ - VALUE hash = to_hash(argv[i]); - if (block_given) { - rb_hash_foreach(hash, rb_hash_update_block_i, self); - } - else { - rb_hash_foreach(hash, rb_hash_update_i, self); - } - } - return self; + return rb_ensure(rb_hash_update_call, arg, rb_hash_update_ensure, arg); } struct update_func_arg { @@ -4354,21 +4493,21 @@ flatten_i(VALUE key, VALUE val, VALUE ary) * Examples; note that entry <tt>foo: {bar: 1, baz: 2}</tt> is never flattened. * * h = {foo: {bar: 1, baz: 2}, bat: [:bam, [:bap, [:bah]]]} - * h.flatten(1) # => [:foo, {:bar=>1, :baz=>2}, :bat, [:bam, [:bap, [:bah]]]] - * h.flatten(2) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, [:bap, [:bah]]] - * h.flatten(3) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, [:bah]] - * h.flatten(4) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, :bah] - * h.flatten(5) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, :bah] + * h.flatten(1) # => [:foo, {bar: 1, baz: 2}, :bat, [:bam, [:bap, [:bah]]]] + * h.flatten(2) # => [:foo, {bar: 1, baz: 2}, :bat, :bam, [:bap, [:bah]]] + * h.flatten(3) # => [:foo, {bar: 1, baz: 2}, :bat, :bam, :bap, [:bah]] + * h.flatten(4) # => [:foo, {bar: 1, baz: 2}, :bat, :bam, :bap, :bah] + * h.flatten(5) # => [:foo, {bar: 1, baz: 2}, :bat, :bam, :bap, :bah] * * With negative integer +depth+, * flattens all levels: * - * h.flatten(-1) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, :bah] + * h.flatten(-1) # => [:foo, {bar: 1, baz: 2}, :bat, :bam, :bap, :bah] * * With +depth+ zero, * returns the equivalent of #to_a: * - * h.flatten(0) # => [[:foo, {:bar=>1, :baz=>2}], [:bat, [:bam, [:bap, [:bah]]]]] + * h.flatten(0) # => [[:foo, {bar: 1, baz: 2}], [:bat, [:bam, [:bap, [:bah]]]]] * * Related: see {Methods for Converting}[rdoc-ref:Hash@Methods+for+Converting]. */ @@ -4479,7 +4618,7 @@ rb_hash_compact_bang(VALUE hash) * returns +self+: * * By default, two keys are considered to be the same key - * if and only if they are _equal_ objects (per method #==): + * if and only if they are _equal_ objects (per method #eql?): * * h = {} * h['x'] = 0 @@ -4530,10 +4669,12 @@ rb_hash_compare_by_id(VALUE hash) // We know for sure `identtable` is an st table, // so we can skip `ar_force_convert_table` here. - RHASH_ST_TABLE_SET(hash, identtable); + rb_hash_st_table_set(hash, identtable); RHASH_ST_CLEAR(tmp); } + rb_gc_register_pinning_obj(hash); + return hash; } @@ -4563,6 +4704,7 @@ rb_ident_hash_new(void) { VALUE hash = rb_hash_new(); hash_st_table_init(hash, &identhash, 0); + rb_gc_register_pinning_obj(hash); return hash; } @@ -4571,6 +4713,7 @@ rb_ident_hash_new_with_size(st_index_t size) { VALUE hash = rb_hash_new(); hash_st_table_init(hash, &identhash, size); + rb_gc_register_pinning_obj(hash); return hash; } @@ -4712,7 +4855,7 @@ rb_hash_any_p(int argc, VALUE *argv, VALUE hash) * h.dig(:hello) # => nil * h.default_proc = -> (hash, _key) { hash } * h.dig(:hello, :world) - * # => {:foo=>{:bar=>[:a, :b, :c]}} + * # => {foo: {bar: [:a, :b, :c]}} * * Related: {Methods for Fetching}[rdoc-ref:Hash@Methods+for+Fetching]. */ @@ -4749,10 +4892,9 @@ hash_le(VALUE hash1, VALUE hash2) /* * call-seq: - * self <= other_hash -> true or false + * self <= other -> true or false * - * Returns +true+ if the entries of +self+ are a subset of the entries of +other_hash+, - * +false+ otherwise: + * Returns whether the entries of +self+ are a subset of the entries of +other+: * * h0 = {foo: 0, bar: 1} * h1 = {foo: 0, bar: 1, baz: 2} @@ -4760,7 +4902,7 @@ hash_le(VALUE hash1, VALUE hash2) * h0 <= h1 # => true * h1 <= h0 # => false * - * See {Hash Inclusion}[rdoc-ref:hash_inclusion.rdoc]. + * See {Hash Inclusion}[rdoc-ref:language/hash_inclusion.rdoc]. * * Raises TypeError if +other_hash+ is not a hash and cannot be converted to a hash. * @@ -4776,20 +4918,19 @@ rb_hash_le(VALUE hash, VALUE other) /* * call-seq: - * self < other_hash -> true or false + * self < other -> true or false * - * Returns +true+ if the entries of +self+ are a proper subset of the entries of +other_hash+, - * +false+ otherwise: + * Returns whether the entries of +self+ are a proper subset of the entries of +other+: * * h = {foo: 0, bar: 1} * h < {foo: 0, bar: 1, baz: 2} # => true # Proper subset. * h < {baz: 2, bar: 1, foo: 0} # => true # Order may differ. * h < h # => false # Not a proper subset. * h < {bar: 1, foo: 0} # => false # Not a proper subset. - * h < {foo: 0, bar: 1, baz: 2} # => false # Different key. - * h < {foo: 0, bar: 1, baz: 2} # => false # Different value. + * h < {foo: 0, bat: 1, baz: 2} # => false # Different key. + * h < {foo: 0, bar: 3, baz: 2} # => false # Different value. * - * See {Hash Inclusion}[rdoc-ref:hash_inclusion.rdoc]. + * See {Hash Inclusion}[rdoc-ref:language/hash_inclusion.rdoc]. * * Raises TypeError if +other_hash+ is not a hash and cannot be converted to a hash. * @@ -4805,10 +4946,9 @@ rb_hash_lt(VALUE hash, VALUE other) /* * call-seq: - * self >= other_hash -> true or false + * self >= other -> true or false * - * Returns +true+ if the entries of +self+ are a superset of the entries of +other_hash+, - * +false+ otherwise: + * Returns whether the entries of +self+ are a superset of the entries of +other+: * * h0 = {foo: 0, bar: 1, baz: 2} * h1 = {foo: 0, bar: 1} @@ -4816,7 +4956,7 @@ rb_hash_lt(VALUE hash, VALUE other) * h0 >= h0 # => true * h1 >= h0 # => false * - * See {Hash Inclusion}[rdoc-ref:hash_inclusion.rdoc]. + * See {Hash Inclusion}[rdoc-ref:language/hash_inclusion.rdoc]. * * Raises TypeError if +other_hash+ is not a hash and cannot be converted to a hash. * @@ -4832,20 +4972,19 @@ rb_hash_ge(VALUE hash, VALUE other) /* * call-seq: - * self > other_hash -> true or false + * self > other -> true or false * - * Returns +true+ if the entries of +self+ are a proper superset of the entries of +other_hash+, - * +false+ otherwise: + * Returns whether the entries of +self+ are a proper superset of the entries of +other+: * * h = {foo: 0, bar: 1, baz: 2} * h > {foo: 0, bar: 1} # => true # Proper superset. * h > {bar: 1, foo: 0} # => true # Order may differ. * h > h # => false # Not a proper superset. * h > {baz: 2, bar: 1, foo: 0} # => false # Not a proper superset. - * h > {foo: 0, bar: 1} # => false # Different key. - * h > {foo: 0, bar: 1} # => false # Different value. + * h > {foo: 0, bat: 1} # => false # Different key. + * h > {foo: 0, bar: 3} # => false # Different value. * - * See {Hash Inclusion}[rdoc-ref:hash_inclusion.rdoc]. + * See {Hash Inclusion}[rdoc-ref:language/hash_inclusion.rdoc]. * * Raises TypeError if +other_hash+ is not a hash and cannot be converted to a hash. * @@ -4878,6 +5017,8 @@ hash_proc_call(RB_BLOCK_CALL_FUNC_ARGLIST(key, hash)) * proc.call(:foo) # => 0 * proc.call(:bar) # => 1 * proc.call(:nosuch) # => nil + * h.default_proc = proc { |hash, key| "Missing key: #{key}" } # This affect the existing proc object + * proc.call(:nosuch) # => "Missing key: #{nosuch}" * * Related: see {Methods for Converting}[rdoc-ref:Hash@Methods+for+Converting]. */ @@ -4897,10 +5038,8 @@ rb_hash_deconstruct_keys(VALUE hash, VALUE keys) static int add_new_i(st_data_t *key, st_data_t *val, st_data_t arg, int existing) { - VALUE *args = (VALUE *)arg; if (existing) return ST_STOP; - RB_OBJ_WRITTEN(args[0], Qundef, (VALUE)*key); - RB_OBJ_WRITE(args[0], (VALUE *)val, args[1]); + *val = arg; return ST_CONTINUE; } @@ -4912,22 +5051,25 @@ int rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val) { st_table *tbl; - int ret = 0; - VALUE args[2]; - args[0] = hash; - args[1] = val; + int ret = -1; if (RHASH_AR_TABLE_P(hash)) { - ret = ar_update(hash, (st_data_t)key, add_new_i, (st_data_t)args); - if (ret != -1) { - return ret; + ret = ar_update(hash, (st_data_t)key, add_new_i, (st_data_t)val); + if (ret == -1) { + ar_force_convert_table(hash, __FILE__, __LINE__); } - ar_force_convert_table(hash, __FILE__, __LINE__); } - tbl = RHASH_TBL_RAW(hash); - return st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)args); - + if (ret == -1) { + tbl = RHASH_TBL_RAW(hash); + ret = st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)val); + } + if (!ret) { + // Newly inserted + RB_OBJ_WRITTEN(hash, Qundef, key); + RB_OBJ_WRITTEN(hash, Qundef, val); + } + return ret; } static st_data_t @@ -4967,6 +5109,14 @@ rb_hash_bulk_insert(long argc, const VALUE *argv, VALUE hash) } } +VALUE +rb_hash_new_with_bulk_insert(long argc, const VALUE *argv) +{ + VALUE val = rb_hash_new_with_size(argc / 2); + rb_hash_bulk_insert(argc, argv, val); + return val; +} + static char **origenviron; #ifdef _WIN32 #define GET_ENVIRON(e) ((e) = rb_w32_get_environ()) @@ -4994,8 +5144,7 @@ extern char **environ; #define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0) #endif -#define ENV_LOCK() RB_VM_LOCK_ENTER() -#define ENV_UNLOCK() RB_VM_LOCK_LEAVE() +#define ENV_LOCKING() RB_VM_LOCKING() static inline rb_encoding * env_encoding(void) @@ -5017,28 +5166,27 @@ env_enc_str_new(const char *ptr, long len, rb_encoding *enc) } static VALUE -env_str_new(const char *ptr, long len) +env_str_new(const char *ptr, long len, rb_encoding *enc) { - return env_enc_str_new(ptr, len, env_encoding()); + return env_enc_str_new(ptr, len, enc); } static VALUE -env_str_new2(const char *ptr) +env_str_new2(const char *ptr, rb_encoding *enc) { if (!ptr) return Qnil; - return env_str_new(ptr, strlen(ptr)); + return env_str_new(ptr, strlen(ptr), enc); } static VALUE getenv_with_lock(const char *name) { VALUE ret; - ENV_LOCK(); - { + rb_encoding *enc = env_encoding(); + ENV_LOCKING() { const char *val = getenv(name); - ret = env_str_new2(val); + ret = env_str_new2(val, enc); } - ENV_UNLOCK(); return ret; } @@ -5047,11 +5195,9 @@ has_env_with_lock(const char *name) { const char *val; - ENV_LOCK(); - { + ENV_LOCKING() { val = getenv(name); } - ENV_UNLOCK(); return val ? true : false; } @@ -5201,7 +5347,7 @@ static VALUE env_fetch(int argc, VALUE *argv, VALUE _) { VALUE key; - long block_given; + int block_given; const char *nam; VALUE env; @@ -5224,6 +5370,42 @@ env_fetch(int argc, VALUE *argv, VALUE _) return env; } +/* + * call-seq: + * ENV.fetch_values(*names) -> array of values + * ENV.fetch_values(*names) {|name| ... } -> array of values + * + * Returns an Array containing the environment variable values associated with + * the given names: + * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') + * ENV.fetch_values('foo', 'baz') # => ["0", "2"] + * + * Otherwise if a block is given yields +name+ to + * the block and returns the block's return value: + * ENV.fetch_values('foo', 'bam') {|key| key.to_s} # => ["0", "bam"] + * + * Raises KeyError if +name+ is valid, but not found and block is not given: + * ENV.fetch_values('foo', 'bam') # Raises KeyError (key not found: "bam") + * + * Returns an empty Array if no names given. + * + * Raises an exception if any name is invalid. + * See {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values]. + */ + +static VALUE +env_fetch_values(int argc, VALUE *argv, VALUE ehash) +{ + VALUE result = rb_ary_new2(argc); + long i; + + for (i=0; i<argc; i++) { + rb_ary_push(result, env_fetch(1, &argv[i], ehash)); + } + + return result; +} + #if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV)) #elif defined __sun static int @@ -5301,13 +5483,11 @@ ruby_setenv(const char *name, const char *value) *wvalue = L'\0'; } - ENV_LOCK(); - { + ENV_LOCKING() { /* Use _wputenv_s() instead of SetEnvironmentVariableW() to make sure * special variables like "TZ" are interpret by libc. */ failed = _wputenv_s(wname, wvalue); } - ENV_UNLOCK(); ALLOCV_END(buf); /* even if putenv() failed, clean up and try to delete the @@ -5324,28 +5504,22 @@ ruby_setenv(const char *name, const char *value) #elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV) if (value) { int ret; - ENV_LOCK(); - { + ENV_LOCKING() { ret = setenv(name, value, 1); } - ENV_UNLOCK(); if (ret) rb_sys_fail_sprintf("setenv(%s)", name); } else { #ifdef VOID_UNSETENV - ENV_LOCK(); - { + ENV_LOCKING() { unsetenv(name); } - ENV_UNLOCK(); #else int ret; - ENV_LOCK(); - { + ENV_LOCKING() { ret = unsetenv(name); } - ENV_UNLOCK(); if (ret) rb_sys_fail_sprintf("unsetenv(%s)", name); #endif @@ -5368,8 +5542,7 @@ ruby_setenv(const char *name, const char *value) snprintf(mem_ptr, mem_size, "%s=%s", name, value); } - ENV_LOCK(); - { + ENV_LOCKING() { for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) { if (!strncmp(str, name, len) && str[len] == '=') { if (!in_origenv(str)) free(str); @@ -5378,15 +5551,12 @@ ruby_setenv(const char *name, const char *value) } } } - ENV_UNLOCK(); if (value) { int ret; - ENV_LOCK(); - { + ENV_LOCKING() { ret = putenv(mem_ptr); } - ENV_UNLOCK(); if (ret) { free(mem_ptr); @@ -5397,8 +5567,7 @@ ruby_setenv(const char *name, const char *value) size_t len; int i; - ENV_LOCK(); - { + ENV_LOCKING() { i = envix(name); /* where does it go? */ if (environ == origenviron) { /* need we copy environment? */ @@ -5439,7 +5608,6 @@ ruby_setenv(const char *name, const char *value) finish:; } - ENV_UNLOCK(); #endif /* WIN32 */ } @@ -5524,8 +5692,7 @@ env_keys(int raw) rb_encoding *enc = raw ? 0 : rb_locale_encoding(); VALUE ary = rb_ary_new(); - ENV_LOCK(); - { + ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -5539,7 +5706,6 @@ env_keys(int raw) } FREE_ENVIRON(environ); } - ENV_UNLOCK(); return ary; } @@ -5569,8 +5735,7 @@ rb_env_size(VALUE ehash, VALUE args, VALUE eobj) char **env; long cnt = 0; - ENV_LOCK(); - { + ENV_LOCKING() { env = GET_ENVIRON(environ); for (; *env ; ++env) { if (strchr(*env, '=')) { @@ -5579,7 +5744,6 @@ rb_env_size(VALUE ehash, VALUE args, VALUE eobj) } FREE_ENVIRON(environ); } - ENV_UNLOCK(); return LONG2FIX(cnt); } @@ -5620,20 +5784,19 @@ env_values(void) { VALUE ary = rb_ary_new(); - ENV_LOCK(); - { + rb_encoding *enc = env_encoding(); + ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s) { - rb_ary_push(ary, env_str_new2(s+1)); + rb_ary_push(ary, env_str_new2(s+1, enc)); } env++; } FREE_ENVIRON(environ); } - ENV_UNLOCK(); return ary; } @@ -5714,21 +5877,20 @@ env_each_pair(VALUE ehash) VALUE ary = rb_ary_new(); - ENV_LOCK(); - { + rb_encoding *enc = env_encoding(); + ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s) { - rb_ary_push(ary, env_str_new(*env, s-*env)); - rb_ary_push(ary, env_str_new2(s+1)); + rb_ary_push(ary, env_str_new(*env, s-*env, enc)); + rb_ary_push(ary, env_str_new2(s+1, enc)); } env++; } FREE_ENVIRON(environ); } - ENV_UNLOCK(); if (rb_block_pair_yield_optimizable()) { for (i=0; i<RARRAY_LEN(ary); i+=2) { @@ -6068,8 +6230,7 @@ env_inspect(VALUE _) VALUE str = rb_str_buf_new2("{"); rb_encoding *enc = env_encoding(); - ENV_LOCK(); - { + ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { const char *s = strchr(*env, '='); @@ -6087,7 +6248,6 @@ env_inspect(VALUE _) } FREE_ENVIRON(environ); } - ENV_UNLOCK(); rb_str_buf_cat2(str, "}"); @@ -6108,20 +6268,19 @@ env_to_a(VALUE _) { VALUE ary = rb_ary_new(); - ENV_LOCK(); - { + rb_encoding *enc = env_encoding(); + ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s) { - rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env), - env_str_new2(s+1))); + rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env, enc), + env_str_new2(s+1, enc))); } env++; } FREE_ENVIRON(environ); } - ENV_UNLOCK(); return ary; } @@ -6145,13 +6304,11 @@ env_size_with_lock(void) { int i = 0; - ENV_LOCK(); - { + ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (env[i]) i++; FREE_ENVIRON(environ); } - ENV_UNLOCK(); return i; } @@ -6187,15 +6344,13 @@ env_empty_p(VALUE _) { bool empty = true; - ENV_LOCK(); - { + ENV_LOCKING() { char **env = GET_ENVIRON(environ); if (env[0] != 0) { empty = false; } FREE_ENVIRON(environ); } - ENV_UNLOCK(); return RBOOL(empty); } @@ -6284,8 +6439,7 @@ env_has_value(VALUE dmy, VALUE obj) VALUE ret = Qfalse; - ENV_LOCK(); - { + ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -6300,7 +6454,6 @@ env_has_value(VALUE dmy, VALUE obj) } FREE_ENVIRON(environ); } - ENV_UNLOCK(); return ret; } @@ -6327,13 +6480,12 @@ env_rassoc(VALUE dmy, VALUE obj) VALUE result = Qnil; - ENV_LOCK(); - { + ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { const char *p = *env; - char *s = strchr(p, '='); + const char *s = strchr(p, '='); if (s++) { long len = strlen(s); if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) { @@ -6345,7 +6497,6 @@ env_rassoc(VALUE dmy, VALUE obj) } FREE_ENVIRON(environ); } - ENV_UNLOCK(); return result; } @@ -6372,15 +6523,15 @@ env_key(VALUE dmy, VALUE value) StringValue(value); VALUE str = Qnil; - ENV_LOCK(); - { + rb_encoding *enc = env_encoding(); + ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s++) { long len = strlen(s); if (RSTRING_LEN(value) == len && strncmp(s, RSTRING_PTR(value), len) == 0) { - str = env_str_new(*env, s-*env-1); + str = env_str_new(*env, s-*env-1, enc); break; } } @@ -6388,7 +6539,6 @@ env_key(VALUE dmy, VALUE value) } FREE_ENVIRON(environ); } - ENV_UNLOCK(); return str; } @@ -6398,20 +6548,19 @@ env_to_hash(void) { VALUE hash = rb_hash_new(); - ENV_LOCK(); - { + rb_encoding *enc = env_encoding(); + ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s) { - rb_hash_aset(hash, env_str_new(*env, s-*env), - env_str_new2(s+1)); + rb_hash_aset(hash, env_str_new(*env, s-*env, enc), + env_str_new2(s+1, enc)); } env++; } FREE_ENVIRON(environ); } - ENV_UNLOCK(); return hash; } @@ -6551,21 +6700,20 @@ env_shift(VALUE _) VALUE result = Qnil; VALUE key = Qnil; - ENV_LOCK(); - { + rb_encoding *enc = env_encoding(); + ENV_LOCKING() { char **env = GET_ENVIRON(environ); if (*env) { const char *p = *env; - char *s = strchr(p, '='); + const char *s = strchr(p, '='); if (s) { - key = env_str_new(p, s-p); - VALUE val = env_str_new2(getenv(RSTRING_PTR(key))); + key = env_str_new(p, s-p, enc); + VALUE val = env_str_new2(getenv(RSTRING_PTR(key)), enc); result = rb_assoc_new(key, val); } } FREE_ENVIRON(environ); } - ENV_UNLOCK(); if (!NIL_P(key)) { env_delete(key); @@ -6776,27 +6924,28 @@ static const rb_data_type_t env_data_type = { }; /* - * A +Hash+ maps each of its unique keys to a specific value. + * A \Hash object maps each of its unique keys to a specific value. * - * A +Hash+ has certain similarities to an Array, but: - * - An Array index is always an Integer. - * - A +Hash+ key can be (almost) any object. + * A hash has certain similarities to an Array, but: * - * === +Hash+ \Data Syntax + * - An array index is always an integer. + * - A hash key can be (almost) any object. * - * The older syntax for +Hash+ data uses the "hash rocket," <tt>=></tt>: + * === \Hash \Data Syntax + * + * The original syntax for a hash entry uses the "hash rocket," <tt>=></tt>: * * h = {:foo => 0, :bar => 1, :baz => 2} * h # => {foo: 0, bar: 1, baz: 2} * - * Alternatively, but only for a +Hash+ key that's a Symbol, + * Alternatively, but only for a key that's a symbol, * you can use a newer JSON-style syntax, - * where each bareword becomes a Symbol: + * where each bareword becomes a symbol: * * h = {foo: 0, bar: 1, baz: 2} * h # => {foo: 0, bar: 1, baz: 2} * - * You can also use a String in place of a bareword: + * You can also use a string in place of a bareword: * * h = {'foo': 0, 'bar': 1, 'baz': 2} * h # => {foo: 0, bar: 1, baz: 2} @@ -6807,12 +6956,12 @@ static const rb_data_type_t env_data_type = { * h # => {foo: 0, bar: 1, baz: 2} * * But it's an error to try the JSON-style syntax - * for a key that's not a bareword or a String: + * for a key that's not a bareword or a string: * * # Raises SyntaxError (syntax error, unexpected ':', expecting =>): * h = {0: 'zero'} * - * +Hash+ value can be omitted, meaning that value will be fetched from the context + * The value can be omitted, meaning that value will be fetched from the context * by the name of the key: * * x = 0 @@ -6822,24 +6971,24 @@ static const rb_data_type_t env_data_type = { * * === Common Uses * - * You can use a +Hash+ to give names to objects: + * You can use a hash to give names to objects: * * person = {name: 'Matz', language: 'Ruby'} * person # => {name: "Matz", language: "Ruby"} * - * You can use a +Hash+ to give names to method arguments: + * You can use a hash to give names to method arguments: * * def some_method(hash) * p hash * end * some_method({foo: 0, bar: 1, baz: 2}) # => {foo: 0, bar: 1, baz: 2} * - * Note: when the last argument in a method call is a +Hash+, + * Note: when the last argument in a method call is a hash, * the curly braces may be omitted: * * some_method(foo: 0, bar: 1, baz: 2) # => {foo: 0, bar: 1, baz: 2} * - * You can use a +Hash+ to initialize an object: + * You can use a hash to initialize an object: * * class Dev * attr_accessor :name, :language @@ -6851,57 +7000,49 @@ static const rb_data_type_t env_data_type = { * matz = Dev.new(name: 'Matz', language: 'Ruby') * matz # => #<Dev: @name="Matz", @language="Ruby"> * - * === Creating a +Hash+ + * === Creating a \Hash * - * You can create a +Hash+ object explicitly with: + * You can create a \Hash object explicitly with: * * - A {hash literal}[rdoc-ref:syntax/literals.rdoc@Hash+Literals]. * - * You can convert certain objects to Hashes with: - * - * - Method #Hash. + * You can convert certain objects to hashes with: * - * You can create a +Hash+ by calling method Hash.new. + * - Method Kernel#Hash. * - * Create an empty +Hash+: + * You can create a hash by calling method Hash.new: * + * # Create an empty hash. * h = Hash.new * h # => {} * h.class # => Hash * - * You can create a +Hash+ by calling method Hash.[]. - * - * Create an empty +Hash+: + * You can create a hash by calling method Hash.[]: * + * # Create an empty hash. * h = Hash[] * h # => {} - * - * Create a +Hash+ with initial entries: - * + * # Create a hash with initial entries. * h = Hash[foo: 0, bar: 1, baz: 2] * h # => {foo: 0, bar: 1, baz: 2} * - * You can create a +Hash+ by using its literal form (curly braces). - * - * Create an empty +Hash+: + * You can create a hash by using its literal form (curly braces): * + * # Create an empty hash. * h = {} * h # => {} - * - * Create a +Hash+ with initial entries: - * + * # Create a +Hash+ with initial entries. * h = {foo: 0, bar: 1, baz: 2} * h # => {foo: 0, bar: 1, baz: 2} * + * === \Hash Value Basics * - * === +Hash+ Value Basics - * - * The simplest way to retrieve a +Hash+ value (instance method #[]): + * The simplest way to retrieve a hash value (instance method #[]): * * h = {foo: 0, bar: 1, baz: 2} * h[:foo] # => 0 * - * The simplest way to create or update a +Hash+ value (instance method #[]=): + * The simplest way to create or update a hash value (instance method #[]=): * * h = {foo: 0, bar: 1, baz: 2} * h[:bat] = 3 # => 3 @@ -6909,7 +7050,7 @@ static const rb_data_type_t env_data_type = { * h[:foo] = 4 # => 4 * h # => {foo: 4, bar: 1, baz: 2, bat: 3} * - * The simplest way to delete a +Hash+ entry (instance method #delete): + * The simplest way to delete a hash entry (instance method #delete): * * h = {foo: 0, bar: 1, baz: 2} * h.delete(:bar) # => 1 @@ -6917,13 +7058,13 @@ static const rb_data_type_t env_data_type = { * * === Entry Order * - * A +Hash+ object presents its entries in the order of their creation. This is seen in: + * A \Hash object presents its entries in the order of their creation. This is seen in: * * - Iterative methods such as <tt>each</tt>, <tt>each_key</tt>, <tt>each_pair</tt>, <tt>each_value</tt>. * - Other order-sensitive methods such as <tt>shift</tt>, <tt>keys</tt>, <tt>values</tt>. - * - The String returned by method <tt>inspect</tt>. + * - The string returned by method <tt>inspect</tt>. * - * A new +Hash+ has its initial ordering per the given entries: + * A new hash has its initial ordering per the given entries: * * h = Hash[foo: 0, bar: 1] * h # => {foo: 0, bar: 1} @@ -7122,8 +7263,8 @@ static const rb_data_type_t env_data_type = { * * First, what's elsewhere. Class +Hash+: * - * - Inherits from {class Object}[rdoc-ref:Object@What-27s+Here]. - * - Includes {module Enumerable}[rdoc-ref:Enumerable@What-27s+Here], + * - Inherits from {class Object}[rdoc-ref:Object@Whats+Here]. + * - Includes {module Enumerable}[rdoc-ref:Enumerable@Whats+Here], * which provides dozens of additional methods. * * Here, class +Hash+ provides methods that are useful for: @@ -7187,7 +7328,7 @@ static const rb_data_type_t env_data_type = { * - #keys: Returns an array containing all keys in +self+. * - #rassoc: Returns a 2-element array consisting of the key and value * of the first-found entry having a given value. - * - #values: Returns an array containing all values in +self+/ + * - #values: Returns an array containing all values in +self+. * - #values_at: Returns an array containing values for given keys. * * ==== Methods for Assigning @@ -7236,7 +7377,6 @@ static const rb_data_type_t env_data_type = { * * ==== Methods for Transforming Keys and Values * - * - #flatten!: Returns +self+, flattened. * - #invert: Returns a hash with the each key-value pair inverted. * - #transform_keys: Returns a copy of +self+ with modified keys. * - #transform_keys!: Modifies keys in +self+ @@ -7348,7 +7488,8 @@ Init_Hash(void) rb_define_singleton_method(rb_cHash, "ruby2_keywords_hash?", rb_hash_s_ruby2_keywords_hash_p, 1); rb_define_singleton_method(rb_cHash, "ruby2_keywords_hash", rb_hash_s_ruby2_keywords_hash, 1); - rb_cHash_empty_frozen = rb_hash_freeze(rb_hash_new()); + rb_cHash_empty_frozen = rb_hash_freeze(rb_hash_alloc_fixed_size(rb_cHash, 0)); + RB_OBJ_SET_SHAREABLE(rb_cHash_empty_frozen); rb_vm_register_global_object(rb_cHash_empty_frozen); /* Document-class: ENV @@ -7434,8 +7575,8 @@ Init_Hash(void) * * First, what's elsewhere. Class +ENV+: * - * - Inherits from {class Object}[rdoc-ref:Object@What-27s+Here]. - * - Extends {module Enumerable}[rdoc-ref:Enumerable@What-27s+Here], + * - Inherits from {class Object}[rdoc-ref:Object@Whats+Here]. + * - Extends {module Enumerable}[rdoc-ref:Enumerable@Whats+Here], * * Here, class +ENV+ provides methods that are useful for: * @@ -7484,9 +7625,10 @@ Init_Hash(void) * * - ::assoc: Returns a 2-element array containing the name and value * of the named environment variable if it exists: - * - ::clone: Returns +ENV+ (and issues a warning). + * - ::clone: Raises an exception. * - ::except: Returns a hash of all name/value pairs except those given. * - ::fetch: Returns the value for the given name. + * - ::fetch_values: Returns array containing the values associated with given names. * - ::inspect: Returns the contents of +ENV+ as a string. * - ::invert: Returns a hash whose keys are the +ENV+ values, and whose values are the corresponding +ENV+ names. @@ -7518,11 +7660,11 @@ Init_Hash(void) origenviron = environ; envtbl = TypedData_Wrap_Struct(rb_cObject, &env_data_type, NULL); rb_extend_object(envtbl, rb_mEnumerable); - FL_SET_RAW(envtbl, RUBY_FL_SHAREABLE); - + RB_OBJ_SET_SHAREABLE(envtbl); rb_define_singleton_method(envtbl, "[]", rb_f_getenv, 1); rb_define_singleton_method(envtbl, "fetch", env_fetch, -1); + rb_define_singleton_method(envtbl, "fetch_values", env_fetch_values, -1); rb_define_singleton_method(envtbl, "[]=", env_aset_m, 2); rb_define_singleton_method(envtbl, "store", env_aset_m, 2); rb_define_singleton_method(envtbl, "each", env_each_pair, 0); |
