14#include "ruby/internal/config.h"
19# ifdef HAVE_CRT_EXTERNS_H
20# include <crt_externs.h>
22# include "missing/crt_externs.h"
26#include "debug_counter.h"
29#include "internal/array.h"
30#include "internal/bignum.h"
31#include "internal/basic_operators.h"
32#include "internal/class.h"
33#include "internal/cont.h"
34#include "internal/error.h"
35#include "internal/gc.h"
36#include "internal/hash.h"
37#include "internal/object.h"
38#include "internal/proc.h"
39#include "internal/ractor.h"
40#include "internal/st.h"
41#include "internal/symbol.h"
42#include "internal/thread.h"
43#include "internal/time.h"
44#include "internal/vm.h"
48#include "ruby_assert.h"
81#define SET_DEFAULT(hash, ifnone) ( \
82 FL_UNSET_RAW(hash, RHASH_PROC_DEFAULT), \
83 RHASH_SET_IFNONE(hash, ifnone))
85#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
87#define COPY_DEFAULT(hash, hash2) copy_default(RHASH(hash), RHASH(hash2))
89#define RHASH_TYPE(hash) (FL_TEST_RAW(hash, RHASH_COMPARE_BY_IDENTITY) ? &identhash : &objhash)
92copy_default(
struct RHash *hash,
const struct RHash *hash2)
94 hash->basic.flags &= ~RHASH_PROC_DEFAULT;
95 hash->basic.flags |= hash2->basic.
flags & RHASH_PROC_DEFAULT;
111static int ar_compact_table(
VALUE hash);
117 if (!
OBJ_FROZEN(hash) && RHASH_AR_TABLE_P(hash)) {
118 ar_compact_table(hash);
124VALUE rb_cHash_empty_frozen;
127static ID id_hash, id_flatten_bang;
128static ID id_hash_iter_lev;
130#define id_default idDefault
142 if (a == b)
return 0;
147 if (UNDEF_P(a) || UNDEF_P(b))
return -1;
152 return !rb_eql(a, b);
156hash_recursive(
VALUE obj,
VALUE arg,
int recurse)
158 if (recurse)
return INT2FIX(0);
162static long rb_objid_hash(st_index_t index);
165dbl_to_index(
double d)
167 union {
double d; st_index_t i;} u;
173rb_dbl_long_hash(
double d)
176 if (d == 0.0) d = 0.0;
177#if SIZEOF_INT == SIZEOF_VOIDP
180 return rb_objid_hash(dbl_to_index(d));
185any_hash(
VALUE a, st_index_t (*other_func)(
VALUE))
197 hnum = RSHIFT(RSYMBOL(a)->hashval, 1);
204 hnum = rb_objid_hash((st_index_t)a);
210 hval = rb_big_hash(a);
214 hnum = rb_dbl_long_hash(rb_float_value(a));
217 hnum = other_func(a);
227VALUE rb_vm_call0(rb_execution_context_t *ec,
VALUE recv,
ID id,
int argc,
const VALUE *argv,
const rb_callable_method_entry_t *cme,
int kw_splat);
230obj_any_hash(
VALUE obj)
235 const rb_callable_method_entry_t *cme = rb_callable_method_entry(klass, id_hash);
236 if (cme && METHOD_ENTRY_BASIC(cme)) {
238 if (cme->def->type == VM_METHOD_TYPE_CFUNC && cme->def->body.cfunc.func == (rb_cfunc_t)rb_obj_hash) {
239 hval = rb_obj_hash(obj);
242 hval = rb_vm_call0(GET_EC(), obj, id_hash, 0, 0, cme, 0);
248 hval = rb_exec_recursive_outer_mid(hash_recursive, obj, 0, id_hash);
255 sign = rb_integer_pack(hval, &ul, 1,
sizeof(ul), 0,
273 return any_hash(a, obj_any_hash);
279 return LONG2FIX(any_hash(obj, obj_any_hash));
288static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
289static const uint32_t prime2 = 0x830fcab9;
292static inline uint64_t
293mult_and_mix(uint64_t m1, uint64_t m2)
295#if defined HAVE_UINT128_T
296 uint128_t r = (uint128_t) m1 * (uint128_t) m2;
297 return (uint64_t) (r >> 64) ^ (uint64_t) r;
299 uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
300 uint64_t lm1 = m1, lm2 = m2;
301 uint64_t v64_128 = hm1 * hm2;
302 uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
303 uint64_t v1_32 = lm1 * lm2;
305 return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
309static inline uint64_t
310key64_hash(uint64_t key, uint32_t seed)
312 return mult_and_mix(key + seed, prime1);
316#define st_index_hash(index) key64_hash(rb_hash_start(index), prime2)
319rb_objid_hash(st_index_t index)
321 return (
long)st_index_hash(index);
327 VALUE object_id = rb_obj_id(obj);
329 object_id = rb_big_hash(object_id);
331#if SIZEOF_LONG == SIZEOF_VOIDP
332 return (st_index_t)st_index_hash((st_index_t)
NUM2LONG(object_id));
333#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
334 return (st_index_t)st_index_hash((st_index_t)
NUM2LL(object_id));
369rb_obj_hash(
VALUE obj)
371 long hnum = any_hash(obj, objid_hash);
375static const struct st_hash_type objhash = {
380#define rb_ident_cmp st_numcmp
383rb_ident_hash(st_data_t n)
392 n ^= dbl_to_index(rb_float_value(n));
396 return (st_index_t)st_index_hash((st_index_t)n);
399#define identhash rb_hashtype_ident
400static const struct st_hash_type rb_hashtype_ident = {
405#define RHASH_IDENTHASH_P(hash) FL_TEST_RAW(hash, RHASH_COMPARE_BY_IDENTITY)
406#define RHASH_STRING_KEY_P(hash, key) (!RHASH_IDENTHASH_P(hash) && (rb_obj_class(key) == rb_cString))
408typedef st_index_t st_hash_t;
418static inline unsigned int
419RHASH_AR_TABLE_MAX_BOUND(
VALUE h)
421 size_t usable_space = rb_obj_shape_slot_size(h) -
sizeof(
struct RHash) - offsetof(ar_table, pairs);
422 usable_space /=
sizeof(ar_table_pair);
424 return usable_space <= RHASH_AR_TABLE_MAX_SIZE ? (unsigned)usable_space : RHASH_AR_TABLE_MAX_SIZE;
427#define RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE (RHASH_AR_TABLE_MAX_SIZE + 1)
428#define RHASH_AR_TABLE_MISS RHASH_AR_TABLE_MAX_SIZE
430#define RHASH_AR_TABLE_REF(hash, n) (&RHASH_AR_TABLE(hash)->pairs[n])
431#define RHASH_AR_CLEARED_HINT 0x00
432#define RHASH_AR_SUBSTITUTION_HINT 0x01
434static inline st_hash_t
435ar_do_hash(
VALUE hash, st_data_t key)
437 if (RHASH_IDENTHASH_P(hash)) {
438 return (st_hash_t)rb_ident_hash(key);
440 return (st_hash_t)rb_any_hash(key);
443static inline ar_hint_t
444ar_do_hash_hint(st_hash_t hash_value)
446 ar_hint_t hint = (ar_hint_t)hash_value;
447 return hint == RHASH_AR_CLEARED_HINT ? RHASH_AR_SUBSTITUTION_HINT : hint;
450static inline ar_hint_t
451ar_hint(
VALUE hash,
unsigned int index)
453 return RHASH_AR_TABLE(hash)->ar_hint.ary[index];
457ar_hint_set_hint(
VALUE hash,
unsigned int index, ar_hint_t hint)
459 RHASH_AR_TABLE(hash)->ar_hint.ary[index] = hint;
463ar_hint_set(
VALUE hash,
unsigned int index, st_hash_t hash_value)
465 ar_hint_set_hint(hash, index, ar_do_hash_hint(hash_value));
469ar_clear_entry(
VALUE hash,
unsigned int index)
471 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
473 ar_hint_set_hint(hash, index, RHASH_AR_CLEARED_HINT);
477ar_cleared_entry(
VALUE hash,
unsigned int index)
479 return ar_hint(hash, index) == RHASH_AR_CLEARED_HINT;
483ar_set_entry(
VALUE hash,
unsigned int index, st_data_t key, st_data_t val, st_hash_t hash_value)
485 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
488 ar_hint_set(hash, index, hash_value);
491#define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
492 RHASH_AR_TABLE_SIZE_RAW(h))
494#define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(HASH_DEBUG, expr, #expr)
497#define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
500hash_verify_(
VALUE hash,
const char *file,
int line)
504 if (RHASH_AR_TABLE_P(hash)) {
505 unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
507 for (i=0; i<bound; i++) {
509 if (!ar_cleared_entry(hash, i)) {
510 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
513 HASH_ASSERT(!UNDEF_P(k));
514 HASH_ASSERT(!UNDEF_P(v));
518 if (n != RHASH_AR_TABLE_SIZE(hash)) {
519 rb_bug(
"n:%u, RHASH_AR_TABLE_SIZE:%u", n, RHASH_AR_TABLE_SIZE(hash));
523 HASH_ASSERT(RHASH_ST_TABLE(hash) != NULL);
524 HASH_ASSERT(RHASH_AR_TABLE_SIZE_RAW(hash) == 0);
525 HASH_ASSERT(RHASH_AR_TABLE_BOUND_RAW(hash) == 0);
526 HASH_ASSERT(!!RHASH_IDENTHASH_P(hash) == (RHASH_ST_TABLE(hash)->
type == &identhash));
533#define hash_verify(h) ((void)0)
537RHASH_TABLE_EMPTY_P(
VALUE hash)
542#define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
543#define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
546hash_st_table_init(
VALUE hash, st_index_t size)
548 RUBY_ASSERT(rb_gc_obj_slot_size(hash) >=
sizeof(
struct RHash) +
sizeof(st_table));
549 st_init_existing_table_with_size(RHASH_ST_TABLE(hash), RHASH_TYPE(hash), size);
550 RHASH_SET_ST_FLAG(hash);
554rb_hash_st_table_set(
VALUE hash, st_table *st)
556 HASH_ASSERT(st != NULL);
557 RHASH_SET_ST_FLAG(hash);
559 *RHASH_ST_TABLE(hash) = *st;
563RHASH_AR_TABLE_BOUND_SET(
VALUE h, st_index_t n)
565 HASH_ASSERT(RHASH_AR_TABLE_P(h));
566 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND(h));
568 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
569 RBASIC(h)->flags |= n << RHASH_AR_TABLE_BOUND_SHIFT;
573RHASH_AR_TABLE_SIZE_SET(
VALUE h, st_index_t n)
575 HASH_ASSERT(RHASH_AR_TABLE_P(h));
576 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND(h));
578 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
579 RBASIC(h)->flags |= n << RHASH_AR_TABLE_SIZE_SHIFT;
583HASH_AR_TABLE_SIZE_ADD(
VALUE h, st_index_t n)
585 HASH_ASSERT(RHASH_AR_TABLE_P(h));
587 RHASH_AR_TABLE_SIZE_SET(h, RHASH_AR_TABLE_SIZE(h) + n);
592#define RHASH_AR_TABLE_SIZE_INC(h) HASH_AR_TABLE_SIZE_ADD(h, 1)
595RHASH_AR_TABLE_SIZE_DEC(
VALUE h)
597 HASH_ASSERT(RHASH_AR_TABLE_P(h));
598 int new_size = RHASH_AR_TABLE_SIZE(h) - 1;
601 RHASH_AR_TABLE_SIZE_SET(h, new_size);
604 RHASH_AR_TABLE_SIZE_SET(h, 0);
605 RHASH_AR_TABLE_BOUND_SET(h, 0);
611RHASH_AR_TABLE_CLEAR(
VALUE h)
613 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
614 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
616 memset(RHASH_AR_TABLE(h), 0, rb_obj_shape_slot_size(h) -
sizeof(
struct RHash));
625 else if (UNLIKELY(RHASH_IDENTHASH_P(hash))) {
628 return rb_any_cmp(x, y) == 0;
632#define AR_HINT_BASE_MASK 0x101010101010101
633#define AR_HINT_NORMALIZE_MASK 0x7F7F7F7F7F7F7F7F
634#ifdef WORDS_BIGENDIAN
635#define AR_HINT_FIND_FIRST_ZERO_BYTE(x) (nlz_int64(x) / CHAR_BIT)
637#define AR_HINT_FIND_FIRST_ZERO_BYTE(x) (ntz_int64(x) / CHAR_BIT)
640#define AR_HINT_BASE_MASK 0x1010101
641#define AR_HINT_NORMALIZE_MASK 0x7F7F7F7F
642#ifdef WORDS_BIGENDIAN
643#define AR_HINT_FIND_FIRST_ZERO_BYTE(x) (nlz_int32(x) / CHAR_BIT)
645#define AR_HINT_FIND_FIRST_ZERO_BYTE(x) (ntz_int32(x) / CHAR_BIT)
649static inline unsigned int
650ar_hint_first_match(ar_hint_t needle,
VALUE haystack)
654 VALUE search_mask = (
VALUE)AR_HINT_BASE_MASK * needle;
655 VALUE matches = haystack ^ search_mask;
658 matches = ~((((matches & AR_HINT_NORMALIZE_MASK) + AR_HINT_NORMALIZE_MASK) | matches) | AR_HINT_NORMALIZE_MASK);
659 unsigned index = AR_HINT_FIND_FIRST_ZERO_BYTE(matches);
664NOINLINE(
static unsigned ar_find_entry_hint_slowpath(
VALUE hash, ar_hint_t hint, st_data_t key,
unsigned first_match));
666ar_find_entry_hint_slowpath(
VALUE hash, ar_hint_t hint, st_data_t key,
unsigned first_match)
668 RUBY_ASSERT(key != RHASH_AR_TABLE_REF(hash, first_match)->key);
669 bool eq = ar_equal(hash, key, RHASH_AR_TABLE_REF(hash, first_match)->key);
670 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
671 return RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE;
681 for (
unsigned i = first_match + 1; i < RHASH_AR_TABLE_BOUND(hash); i++) {
682 const ar_hint_t *hints = RHASH_AR_TABLE(hash)->ar_hint.ary;
683 if (UNLIKELY(hints[i] == hint)) {
684 eq = ar_equal(hash, key, RHASH_AR_TABLE_REF(hash, i)->key);
685 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
686 return RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE;
689 RB_DEBUG_COUNTER_INC(artable_hint_hit);
693 RB_DEBUG_COUNTER_INC(artable_hint_miss);
698 RB_DEBUG_COUNTER_INC(artable_hint_notfound);
699 return RHASH_AR_TABLE_MISS;
704static inline unsigned
705ar_find_entry_hint(
VALUE hash, ar_hint_t hint, st_data_t key)
707 unsigned first_match = ar_hint_first_match(hint, RHASH_AR_TABLE(hash)->ar_hint.word);
709 if (LIKELY(first_match >= RHASH_AR_TABLE_BOUND(hash))) {
710 RB_DEBUG_COUNTER_INC(artable_hint_notfound);
711 return RHASH_AR_TABLE_MISS;
714 RUBY_ASSERT(RHASH_AR_TABLE(hash)->ar_hint.ary[first_match] == hint);
715 if (LIKELY(key == RHASH_AR_TABLE_REF(hash, first_match)->key)) {
716 RB_DEBUG_COUNTER_INC(artable_hint_hit);
720 return ar_find_entry_hint_slowpath(hash, hint, key, first_match);
724ar_find_entry(
VALUE hash, st_hash_t hash_value, st_data_t key)
726 ar_hint_t hint = ar_do_hash_hint(hash_value);
727 return ar_find_entry_hint(hash, hint, key);
731hash_ar_free_and_clear_table(
VALUE hash)
733 RHASH_AR_TABLE_CLEAR(hash);
735 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
736 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
739void rb_st_add_direct_with_hash(st_table *tab, st_data_t key, st_data_t value, st_hash_t hash);
741enum ar_each_key_type {
748ar_each_key(ar_table *ar,
int max,
enum ar_each_key_type
type, st_data_t *dst_keys, st_table *new_tab, st_hash_t *hashes)
750 for (
int i = 0; i < max; i++) {
751 ar_table_pair *pair = &ar->pairs[i];
754 case ar_each_key_copy:
755 dst_keys[i] = pair->key;
757 case ar_each_key_cmp:
758 if (dst_keys[i] != pair->key)
return 1;
760 case ar_each_key_insert:
761 if (UNDEF_P(pair->key))
continue;
762 rb_st_add_direct_with_hash(new_tab, pair->key, pair->val, hashes[i]);
771ar_force_convert_table(
VALUE hash)
773 if (RHASH_ST_TABLE_P(hash)) {
774 return RHASH_ST_TABLE(hash);
777 ar_table *ar = RHASH_AR_TABLE(hash);
778 st_hash_t hashes[RHASH_AR_TABLE_MAX_SIZE];
779 unsigned int bound, size;
780 const struct st_hash_type *
type = RHASH_TYPE(hash);
782 RUBY_ASSERT(rb_gc_obj_slot_size(hash) >=
sizeof(
struct RHash) +
sizeof(st_table));
786 st_data_t keys[RHASH_AR_TABLE_MAX_SIZE];
787 bound = RHASH_AR_TABLE_BOUND(hash);
788 size = RHASH_AR_TABLE_SIZE(hash);
789 ar_each_key(ar, bound, ar_each_key_copy, keys, NULL, NULL);
791 for (
unsigned int i = 0; i < bound; i++) {
793 hashes[i] = UNDEF_P(keys[i]) ? 0 : ar_do_hash(hash, keys[i]);
797 if (UNLIKELY(!RHASH_AR_TABLE_P(hash)))
return RHASH_ST_TABLE(hash);
798 if (UNLIKELY(RHASH_AR_TABLE_BOUND(hash) != bound))
continue;
799 if (UNLIKELY(ar_each_key(ar, bound, ar_each_key_cmp, keys, NULL, NULL)))
continue;
806 st_table *new_tab = &tab;
807 st_init_existing_table_with_size(new_tab,
type, size);
808 ar_each_key(ar, bound, ar_each_key_insert, NULL, new_tab, hashes);
809 hash_ar_free_and_clear_table(hash);
810 rb_hash_st_table_set(hash, new_tab);
811 return RHASH_ST_TABLE(hash);
818 ar_table_pair *dst_pairs = RHASH_AR_TABLE(dst)->pairs;
819 ar_table_pair *src_pairs = RHASH_AR_TABLE(src)->pairs;
821 const unsigned src_bound = RHASH_AR_TABLE_BOUND(src);
822 const unsigned src_size = RHASH_AR_TABLE_SIZE(src);
825 for (
unsigned i = 0; i < src_bound; i++) {
826 if (!ar_cleared_entry(src, i)) {
827 dst_pairs[j] = src_pairs[i];
828 ar_hint_set_hint(dst, j, (st_hash_t)ar_hint(src, i));
832 RHASH_AR_TABLE_BOUND_SET(dst, src_size);
833 RHASH_AR_TABLE_SIZE_SET(dst, src_size);
838ar_compact_table(
VALUE hash)
840 const unsigned bound = RHASH_AR_TABLE_BOUND(hash);
841 const unsigned size = RHASH_AR_TABLE_SIZE(hash);
848 ar_table_pair *pairs = RHASH_AR_TABLE(hash)->pairs;
850 for (i=0; i<bound; i++) {
851 if (ar_cleared_entry(hash, i)) {
853 for (; j<bound; j++) {
854 if (!ar_cleared_entry(hash, j)) {
856 ar_hint_set_hint(hash, i, (st_hash_t)ar_hint(hash, j));
857 ar_clear_entry(hash, j);
868 HASH_ASSERT(i<=bound);
870 RHASH_AR_TABLE_BOUND_SET(hash, size);
877ar_add_direct_with_hash(
VALUE hash, st_data_t key, st_data_t val, st_hash_t hash_value)
879 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
881 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_BOUND(hash)) {
885 if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND(hash))) {
886 bin = ar_compact_table(hash);
888 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND(hash));
890 ar_set_entry(hash, bin, key, val, hash_value);
891 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
892 RHASH_AR_TABLE_SIZE_INC(hash);
898ensure_ar_table(
VALUE hash)
900 if (!RHASH_AR_TABLE_P(hash)) {
901 rb_raise(
rb_eRuntimeError,
"hash representation was changed during iteration");
906ar_general_foreach(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
908 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
909 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
911 for (i = 0; i < bound; i++) {
912 if (ar_cleared_entry(hash, i))
continue;
914 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
915 st_data_t key = (st_data_t)pair->key;
916 st_data_t val = (st_data_t)pair->val;
917 enum st_retval retval = (*func)(key, val, arg, 0);
918 ensure_ar_table(hash);
929 (*replace)(&key, &val, arg, TRUE);
932 HASH_ASSERT(pair == RHASH_AR_TABLE_REF(hash, i));
934 pair->key = (
VALUE)key;
935 pair->val = (
VALUE)val;
939 ar_clear_entry(hash, i);
940 RHASH_AR_TABLE_SIZE_DEC(hash);
949ar_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
951 return ar_general_foreach(hash, func, replace, arg);
955 st_foreach_callback_func *func;
960apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
962 const struct functor *f = (
void *)d;
963 return f->func(k, v, f->arg);
967ar_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
969 const struct functor f = { func, arg };
970 return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
974ar_foreach_check(
VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
977 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
978 unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
979 enum st_retval retval;
984 for (i = 0; i < bound; i++) {
985 if (ar_cleared_entry(hash, i))
continue;
987 pair = RHASH_AR_TABLE_REF(hash, i);
989 hint = ar_hint(hash, i);
991 retval = (*func)(key, pair->val, arg, 0);
992 ensure_ar_table(hash);
997 pair = RHASH_AR_TABLE_REF(hash, i);
998 if (pair->key == never)
break;
999 ret = ar_find_entry_hint(hash, hint, key);
1000 if (UNLIKELY(ret == RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE)) {
1001 ensure_ar_table(hash);
1003 if (ret == RHASH_AR_TABLE_MISS) {
1004 (*func)(0, 0, arg, 1);
1014 if (!ar_cleared_entry(hash, i)) {
1015 ar_clear_entry(hash, i);
1016 RHASH_AR_TABLE_SIZE_DEC(hash);
1027ar_update(
VALUE hash, st_data_t key,
1028 st_update_callback_func *func, st_data_t arg)
1030 int retval, existing;
1031 unsigned bin = RHASH_AR_TABLE_MISS;
1032 st_data_t value = 0, old_key;
1033 st_hash_t hash_value = ar_do_hash(hash, key);
1035 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1040 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1041 bin = ar_find_entry(hash, hash_value, key);
1042 if (UNLIKELY(bin == RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE)) {
1045 existing = (bin != RHASH_AR_TABLE_MISS);
1052 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, bin);
1057 retval = (*func)(&key, &value, arg, existing);
1059 ensure_ar_table(hash);
1064 if (ar_add_direct_with_hash(hash, key, value, hash_value)) {
1069 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, bin);
1070 if (old_key != key) {
1078 ar_clear_entry(hash, bin);
1079 RHASH_AR_TABLE_SIZE_DEC(hash);
1087ar_insert_direct(
VALUE hash, st_data_t key, st_data_t value, st_hash_t hash_value)
1089 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
1090 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1095 bin = ar_find_entry(hash, hash_value, key);
1096 if (UNLIKELY(bin == RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE)) {
1100 if (bin == RHASH_AR_TABLE_MISS) {
1101 if (RHASH_AR_TABLE_SIZE(hash) == RHASH_AR_TABLE_MAX_BOUND(hash)) {
1105 bin = ar_compact_table(hash);
1106 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND(hash));
1108 ar_set_entry(hash, bin, key, value, hash_value);
1109 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
1110 RHASH_AR_TABLE_SIZE_INC(hash);
1114 RHASH_AR_TABLE_REF(hash, bin)->val = value;
1120ar_insert(
VALUE hash, st_data_t key, st_data_t value)
1122 st_hash_t hash_value = ar_do_hash(hash, key);
1123 return ar_insert_direct(hash, key, value, hash_value);
1127ar_lookup(
VALUE hash, st_data_t key, st_data_t *value)
1129 if (RHASH_AR_TABLE_SIZE(hash) == 0) {
1133 st_hash_t hash_value = ar_do_hash(hash, key);
1134 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1136 return st_lookup(RHASH_ST_TABLE(hash), key, value);
1138 unsigned bin = ar_find_entry(hash, hash_value, key);
1140 if (UNLIKELY(bin == RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE)) {
1141 return st_lookup(RHASH_ST_TABLE(hash), key, value);
1144 if (bin == RHASH_AR_TABLE_MISS) {
1148 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND(hash));
1149 if (value != NULL) {
1150 *value = RHASH_AR_TABLE_REF(hash, bin)->val;
1157ar_delete(
VALUE hash, st_data_t *key, st_data_t *value)
1160 st_hash_t hash_value = ar_do_hash(hash, *key);
1162 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1164 return st_delete(RHASH_ST_TABLE(hash), key, value);
1167 bin = ar_find_entry(hash, hash_value, *key);
1168 if (UNLIKELY(bin == RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE)) {
1169 return st_delete(RHASH_ST_TABLE(hash), key, value);
1172 if (bin == RHASH_AR_TABLE_MISS) {
1173 if (value != 0) *value = 0;
1178 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, bin);
1181 ar_clear_entry(hash, bin);
1182 RHASH_AR_TABLE_SIZE_DEC(hash);
1188ar_shift(
VALUE hash, st_data_t *key, st_data_t *value)
1190 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1191 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1193 for (i = 0; i < bound; i++) {
1194 if (!ar_cleared_entry(hash, i)) {
1195 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
1196 if (value != 0) *value = pair->val;
1198 ar_clear_entry(hash, i);
1199 RHASH_AR_TABLE_SIZE_DEC(hash);
1204 if (value != NULL) *value = 0;
1209ar_keys(
VALUE hash, st_data_t *keys, st_index_t size)
1211 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1212 st_data_t *keys_start = keys, *keys_end = keys + size;
1214 for (i = 0; i < bound; i++) {
1215 if (keys == keys_end) {
1219 if (!ar_cleared_entry(hash, i)) {
1220 *keys++ = RHASH_AR_TABLE_REF(hash, i)->key;
1225 return keys - keys_start;
1229ar_values(
VALUE hash, st_data_t *values, st_index_t size)
1231 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1232 st_data_t *values_start = values, *values_end = values + size;
1234 for (i = 0; i < bound; i++) {
1235 if (values == values_end) {
1239 if (!ar_cleared_entry(hash, i)) {
1240 *values++ = RHASH_AR_TABLE_REF(hash, i)->val;
1245 return values - values_start;
1252 ar_table *new_tab = RHASH_AR_TABLE(hash1);
1254 unsigned int bound = RHASH_AR_TABLE_BOUND(hash2);
1255 unsigned int size = RHASH_AR_TABLE_SIZE(hash2);
1256 if (UNLIKELY(bound != size)) {
1257 ar_compact_into(hash1, hash2);
1261 ar_table *old_tab = RHASH_AR_TABLE(hash2);
1262 new_tab->ar_hint.word = old_tab->ar_hint.word;
1263 MEMCPY(&new_tab->pairs, &old_tab->pairs, ar_table_pair, bound);
1264 RHASH_AR_TABLE_BOUND_SET(hash1, bound);
1265 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1266 rb_gc_writebarrier_remember(hash1);
1274 if (RHASH_AR_TABLE(hash) != NULL) {
1275 RHASH_AR_TABLE_SIZE_SET(hash, 0);
1276 RHASH_AR_TABLE_BOUND_SET(hash, 0);
1279 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
1280 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
1285hash_st_free(
VALUE hash)
1287 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
1289 rb_st_free_embedded_table(RHASH_ST_TABLE(hash));
1293hash_st_free_and_clear_table(
VALUE hash)
1296 RHASH_ST_CLEAR(hash);
1300rb_hash_free(
VALUE hash)
1302 if (RHASH_ST_TABLE_P(hash)) {
1307typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);
1311 st_foreach_func *func;
1316foreach_safe_i(st_data_t key, st_data_t value, st_data_t args,
int error)
1321 if (error)
return ST_STOP;
1322 status = (*arg->func)(key, value, arg->arg);
1323 if (status == ST_CONTINUE) {
1335 arg.func = (st_foreach_func *)func;
1346 rb_foreach_func *func;
1351hash_iter_status_check(
int status)
1366hash_ar_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1370 if (error)
return ST_STOP;
1372 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1374 return hash_iter_status_check(status);
1378hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1382 if (error)
return ST_STOP;
1384 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1386 return hash_iter_status_check(status);
1390iter_lev_in_ivar(
VALUE hash)
1395 HASH_ASSERT(lev >= 0);
1396 return (
unsigned long)lev;
1402iter_lev_in_ivar_set(
VALUE hash,
unsigned long lev)
1404 HASH_ASSERT(lev >= RHASH_LEV_MAX);
1406 rb_ivar_set_internal(hash, id_hash_iter_lev,
LONG2FIX((
long)lev));
1409static inline unsigned long
1410iter_lev_in_flags(
VALUE hash)
1412 return (
unsigned long)((
RBASIC(hash)->flags >> RHASH_LEV_SHIFT) & RHASH_LEV_MAX);
1416iter_lev_in_flags_set(
VALUE hash,
unsigned long lev)
1418 HASH_ASSERT(lev <= RHASH_LEV_MAX);
1419 RBASIC(hash)->flags = ((
RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((
VALUE)lev << RHASH_LEV_SHIFT));
1423hash_iterating_p(
VALUE hash)
1425 return iter_lev_in_flags(hash) > 0;
1429hash_iter_lev_inc(
VALUE hash)
1431 unsigned long lev = iter_lev_in_flags(hash);
1432 if (lev == RHASH_LEV_MAX) {
1433 lev = iter_lev_in_ivar(hash) + 1;
1440 iter_lev_in_flags_set(hash, lev);
1441 if (lev < RHASH_LEV_MAX)
return;
1443 iter_lev_in_ivar_set(hash, lev);
1447hash_iter_lev_dec(
VALUE hash)
1449 unsigned long lev = iter_lev_in_flags(hash);
1450 if (lev == RHASH_LEV_MAX) {
1451 lev = iter_lev_in_ivar(hash);
1452 if (lev > RHASH_LEV_MAX) {
1453 iter_lev_in_ivar_set(hash, lev-1);
1456 rb_attr_delete(hash, id_hash_iter_lev);
1458 else if (lev == 0) {
1461 iter_lev_in_flags_set(hash, lev - 1);
1465hash_foreach_ensure(
VALUE hash)
1467 hash_iter_lev_dec(hash);
1473rb_hash_stlike_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
1475 if (RHASH_AR_TABLE_P(hash)) {
1476 return ar_foreach(hash, func, arg);
1479 return st_foreach(RHASH_ST_TABLE(hash), func, arg);
1485rb_hash_stlike_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1487 if (RHASH_AR_TABLE_P(hash)) {
1488 return ar_foreach_with_replace(hash, func, replace, arg);
1491 return st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
1496hash_foreach_call(
VALUE arg)
1500 if (RHASH_AR_TABLE_P(hash)) {
1501 ret = ar_foreach_check(hash, hash_ar_foreach_iter,
1502 (st_data_t)arg, (st_data_t)
Qundef);
1504 else if (RHASH_ST_TABLE_P(hash)) {
1506 (st_data_t)arg, (st_data_t)
Qundef);
1509 rb_raise(
rb_eRuntimeError,
"ret: %d, hash modified during iteration", ret);
1515rb_hash_foreach(
VALUE hash, rb_foreach_func *func,
VALUE farg)
1519 if (RHASH_TABLE_EMPTY_P(hash))
1522 arg.func = (rb_foreach_func *)func;
1525 hash_foreach_call((
VALUE)&arg);
1528 hash_iter_lev_inc(hash);
1529 rb_ensure(hash_foreach_call, (
VALUE)&arg, hash_foreach_ensure, hash);
1534void rb_st_compact_table(st_table *tab);
1537compact_after_delete(
VALUE hash)
1539 if (!hash_iterating_p(hash) && RHASH_ST_TABLE_P(hash)) {
1540 rb_st_compact_table(RHASH_ST_TABLE(hash));
1545hash_slot_size(
size_t capa,
bool frozen)
1547 if (
capa <= RHASH_AR_TABLE_MAX_SIZE) {
1548 const size_t ar_size = RHASH_AR_SLOT_SIZE(
capa);
1551 if (frozen || ar_size >= RHASH_ST_SLOT_SIZE) {
1556 return RHASH_ST_SLOT_SIZE;
1560hash_alloc(
VALUE klass,
VALUE flags,
VALUE ifnone,
size_t size,
bool frozen)
1562 VALUE hash = rb_newobj_of(klass,
T_HASH | flags, hash_slot_size(size, frozen));
1563 rb_hash_set_ifnone(hash, ifnone);
1565 RHASH_AR_TABLE(hash)->ar_hint.word = 0;
1568 if (hash_slot_size(size, frozen) >=
sizeof(
struct RHash) +
sizeof(st_table)) {
1569 RHASH_ST_TABLE(hash)->num_entries = 0;
1570 RHASH_ST_TABLE(hash)->entries = NULL;
1578hash_init_capa(
VALUE hash,
size_t size)
1580 if (size > RHASH_AR_TABLE_MAX_SIZE) {
1581 hash_st_table_init(hash, size);
1584 RUBY_ASSERT(RHASH_AR_TABLE_MAX_BOUND(hash) >= size);
1590hash_hidden_new(
size_t size)
1592 return hash_init_capa(hash_alloc(0, 0,
Qnil, size,
false), size);
1596hash_alloc_capa(
VALUE klass,
size_t size)
1598 return hash_alloc(klass, 0,
Qnil, size,
false);
1604 return hash_alloc_capa(klass,
capa);
1610 return hash_alloc_capa(klass,
RHASH_SIZE(src));
1614empty_hash_alloc(
VALUE klass)
1616 RUBY_DTRACE_CREATE_HOOK(HASH, 0);
1618 return hash_alloc_capa(klass, 0);
1624 if (rb_hash_compare_by_id_p(basis)) {
1625 return rb_hash_compare_by_id(hash);
1633 return hash_init_capa(hash_alloc_capa(klass,
capa),
capa);
1640 rb_raise(rb_eArgError,
"negative hash size (or size too big)");
1648 return rb_hash_new_capa(0);
1652rb_hash_alloc_fixed_size(
VALUE klass, st_index_t size)
1654 return hash_init_capa(hash_alloc(klass, 0,
Qnil, size,
true), size);
1658ar_add_direct_i(st_data_t key, st_data_t value, st_data_t hash_value, st_data_t arg)
1661 ar_insert_direct(ret, key, value, hash_value);
1673 if (RHASH_ST_TABLE_P(ret)) {
1674 RUBY_ASSERT(RHASH_ST_TABLE(ret)->entries == NULL);
1675 RHASH_UNSET_ST_FLAG(ret);
1678 bool compare_by_id = RHASH_IDENTHASH_P(hash);
1680 if (compare_by_id) {
1681 rb_gc_register_pinning_obj(ret);
1688 if (RHASH_AR_TABLE_MAX_BOUND(ret) <
RHASH_SIZE(hash)) {
1689 RHASH_SET_ST_FLAG(ret);
1692 if (RHASH_AR_TABLE_P(hash)) {
1693 if (RHASH_AR_TABLE_P(ret)) {
1697 st_table *tab = RHASH_ST_TABLE(ret);
1699 st_init_existing_table_with_size(RHASH_ST_TABLE(ret),
1700 compare_by_id ? &identhash : &objhash,
1703 int bound = RHASH_AR_TABLE_BOUND(hash);
1704 for (
int i = 0; i < bound; i++) {
1705 if (ar_cleared_entry(hash, i))
continue;
1707 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
1708 st_add_direct(tab, pair->key, pair->val);
1715 if (RHASH_AR_TABLE_P(ret)) {
1716 rb_st_foreach_with_hash(RHASH_ST_TABLE(hash), ar_add_direct_i, (st_data_t)ret);
1719 st_replace(RHASH_ST_TABLE(ret), RHASH_ST_TABLE(hash));
1720 rb_gc_writebarrier_remember(ret);
1727hash_dup_with_compare_by_id(
VALUE hash)
1730 if (RHASH_ST_TABLE_P(hash)) {
1731 RHASH_SET_ST_FLAG(dup);
1734 return hash_copy(dup, hash);
1741 return hash_copy(dup, hash);
1748 if (
capa > RHASH_AR_TABLE_MAX_SIZE) {
1749 RHASH_SET_ST_FLAG(ret);
1750 RHASH_ST_CLEAR(ret);
1755 hash_copy(ret, hash);
1760rb_hash_dup_capa(
VALUE hash,
size_t capa)
1773 return rb_hash_dup_capa(hash,
RHASH_SIZE(hash));
1777rb_hash_resurrect(
VALUE hash)
1784rb_zjit_hash_new_size(
VALUE *flags_out,
size_t size)
1788 return hash_slot_size(size,
false);
1792rb_zjit_hash_dup_can_fastpath(
VALUE hash,
size_t *alloc_size_out,
VALUE *flags_out,
VALUE *ifnone_out,
rb_len_t *bound_out)
1794 if (!RHASH_AR_TABLE_P(hash))
return false;
1795 if (rb_hash_compare_by_id_p(hash))
return false;
1797 const unsigned int bound = RHASH_AR_TABLE_BOUND(hash);
1799 *alloc_size_out = hash_slot_size(bound,
false);
1801 | ((
VALUE)RHASH_AR_TABLE_SIZE(hash) << RHASH_AR_TABLE_SIZE_SHIFT)
1802 | ((
VALUE)bound << RHASH_AR_TABLE_BOUND_SHIFT);
1810rb_hash_modify_check(
VALUE hash)
1812 rb_check_frozen(hash);
1816rb_hash_tbl_raw(
VALUE hash)
1818 return ar_force_convert_table(hash);
1825 return rb_hash_tbl_raw(hash);
1829rb_hash_modify(
VALUE hash)
1831 rb_hash_modify_check(hash);
1834NORETURN(
static void no_new_key(
void));
1838 rb_raise(
rb_eRuntimeError,
"can't add a new key into hash during iteration");
1846#define NOINSERT_UPDATE_CALLBACK(func) \
1848func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1850 if (!existing) no_new_key(); \
1851 return func(key, val, (struct update_arg *)arg, existing); \
1855func##_insert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1857 return func(key, val, (struct update_arg *)arg, existing); \
1862 st_update_callback_func *func;
1868typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
1871rb_hash_stlike_update(
VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
1873 if (RHASH_AR_TABLE_P(hash)) {
1874 int result = ar_update(hash, key, func, arg);
1876 ar_force_convert_table(hash);
1883 return st_update(RHASH_ST_TABLE(hash), key, func, arg);
1887tbl_update_modify(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
1890 st_data_t old_key = *key;
1891 st_data_t old_value = *val;
1892 VALUE hash = p->hash;
1893 int ret = (p->func)(key, val, arg, existing);
1898 if (!existing || *key != old_key || *val != old_value) {
1899 rb_hash_modify(hash);
1906 rb_hash_modify(hash);
1914tbl_update(
VALUE hash,
VALUE key, tbl_update_func func, st_data_t optional_arg)
1917 .arg = optional_arg,
1924 int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg);
1933#define UPDATE_CALLBACK(iter_p, func) ((iter_p) ? func##_noinsert : func##_insert)
1935#define RHASH_UPDATE_ITER(h, iter_p, key, func, a) do { \
1936 tbl_update((h), (key), UPDATE_CALLBACK(iter_p, func), (st_data_t)(a)); \
1939#define RHASH_UPDATE(hash, key, func, arg) \
1940 RHASH_UPDATE_ITER(hash, hash_iterating_p(hash), key, func, arg)
1948 if (n != 2 && (n >= 0 || n < -3)) {
1949 if (n < 0) n = -n-1;
1950 rb_raise(
rb_eTypeError,
"default_proc takes two arguments (2 for %d)", n);
1961 rb_hash_modify(hash);
1964 if (capa_value !=
INT2FIX(0)) {
1966 if (
capa > 0 && RHASH_AR_TABLE_P(hash) &&
RHASH_SIZE(hash) == 0 &&
1968 hash_st_table_init(hash,
capa);
1972 if (!
NIL_P(block)) {
1973 if (ifnone_unset !=
Qtrue) {
1977 SET_PROC_DEFAULT(hash, block);
1990rb_hash_s_new(
int argc,
VALUE *argv,
VALUE klass)
1996 static ID keyword_ids[1];
2005 CONST_ID(keyword_ids[0],
"capacity");
2007 capacity = kwargs[0];
2014 VALUE hash = hash_alloc_capa(klass,
capa);
2060rb_hash_s_create(
int argc,
VALUE *argv,
VALUE klass)
2065 tmp = rb_hash_s_try_convert(
Qnil, argv[0]);
2068 return hash_new_capa(klass, 0);
2071 if (rb_hash_compare_by_id_p(tmp)) {
2075 tmp = rb_hash_to_a(tmp);
2078 hash = hash_alloc_capa(klass,
RHASH_SIZE(tmp));
2079 return hash_copy(hash, tmp);
2088 return hash_new_capa(klass, 0);
2099 rb_raise(rb_eArgError,
"wrong element type %s at %"PRIdLEN
" (expected array)",
2100 rb_builtin_class_name(e), i);
2106 hash = hash_new_capa(klass,
RARRAY_LEN(tmp));
2109 hash = hash_new_capa(klass,
RARRAY_LEN(tmp) / 1);
2116 rb_raise(rb_eArgError,
"invalid number of elements (%"PRIdLEN
" for 1..2)",
2123 rb_hash_aset(hash, key, val);
2129 if (argc % 2 != 0) {
2130 rb_raise(rb_eArgError,
"odd number of arguments for Hash");
2133 hash = hash_new_with_bulk_insert(klass, argc, argv);
2139rb_to_hash_type(
VALUE hash)
2141 return rb_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
2143#define to_hash rb_to_hash_type
2148 return rb_check_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
2164rb_hash_s_try_convert(
VALUE dummy,
VALUE hash)
2166 return rb_check_hash_type(hash);
2189rb_hash_s_ruby2_keywords_hash_p(
VALUE dummy,
VALUE hash)
2192 return RBOOL(RHASH(hash)->basic.flags & RHASH_PASS_AS_KEYWORDS);
2215rb_hash_s_ruby2_keywords_hash(
VALUE dummy,
VALUE hash)
2218 VALUE tmp = rb_hash_dup(hash);
2220 rb_hash_compare_by_id(tmp);
2222 RHASH(tmp)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
2234 if (RHASH_AR_TABLE_P(arg)) {
2235 ar_insert(arg, (st_data_t)key, (st_data_t)value);
2238 st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value);
2260rb_hash_rehash(
VALUE hash)
2265 if (hash_iterating_p(hash)) {
2268 rb_hash_modify_check(hash);
2269 if (RHASH_AR_TABLE_P(hash)) {
2271 if (RHASH_IDENTHASH_P(hash)) {
2274 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
2276 hash_ar_free_and_clear_table(hash);
2279 else if (RHASH_ST_TABLE_P(hash)) {
2280 st_table *old_tab = RHASH_ST_TABLE(hash);
2281 tmp = hash_alloc_capa(0, 0);
2282 if (old_tab->type == &identhash) {
2286 hash_st_table_init(tmp, old_tab->num_entries);
2287 RHASH_ST_TABLE(tmp)->type = old_tab->type;
2288 tbl = RHASH_ST_TABLE(tmp);
2290 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
2293 rb_hash_st_table_set(hash, tbl);
2294 RHASH_ST_CLEAR(tmp);
2303 VALUE args[2] = {hash, key};
2308rb_hash_default_unredefined(
VALUE hash)
2312 return !!BASIC_OP_UNREDEFINED_P(BOP_DEFAULT, HASH_REDEFINED_OP_FLAG);
2324 if (LIKELY(rb_hash_default_unredefined(hash))) {
2326 if (LIKELY(!
FL_TEST_RAW(hash, RHASH_PROC_DEFAULT)))
return ifnone;
2327 if (UNDEF_P(key))
return Qnil;
2328 return call_default_proc(ifnone, hash, key);
2336hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2340 if (RHASH_AR_TABLE_P(hash)) {
2341 return ar_lookup(hash, key, pval);
2344 extern st_index_t rb_iseq_cdhash_hash(
VALUE);
2346 RHASH_ST_TABLE(hash)->
type->hash == rb_ident_hash ||
2347 RHASH_ST_TABLE(hash)->
type->hash == rb_iseq_cdhash_hash);
2348 return st_lookup(RHASH_ST_TABLE(hash), key, pval);
2353rb_hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2355 return hash_stlike_lookup(hash, key, pval);
2380 if (hash_stlike_lookup(hash, key, &val)) {
2384 return rb_hash_default_value(hash, key);
2393 if (hash_stlike_lookup(hash, key, &val)) {
2404 return rb_hash_lookup2(hash, key,
Qnil);
2434rb_hash_fetch_m(
int argc,
VALUE *argv,
VALUE hash)
2444 if (block_given && argc == 2) {
2445 rb_warn(
"block supersedes default value argument");
2448 if (hash_stlike_lookup(hash, key, &val)) {
2455 else if (argc == 1) {
2461 rb_key_err_raise(rb_sprintf(
"key not found: %"PRIsVALUE, desc), hash, key);
2472 return rb_hash_fetch_m(1, &key, hash);
2496rb_hash_default(
int argc,
VALUE *argv,
VALUE hash)
2502 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2503 if (argc == 0)
return Qnil;
2504 return call_default_proc(ifnone, hash, argv[0]);
2525 rb_hash_modify_check(hash);
2526 SET_DEFAULT(hash, ifnone);
2543rb_hash_default_proc(
VALUE hash)
2545 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2566rb_hash_set_default_proc(
VALUE hash,
VALUE proc)
2570 rb_hash_modify_check(hash);
2572 SET_DEFAULT(hash, proc);
2575 b = rb_check_convert_type_with_id(proc,
T_DATA,
"Proc", idTo_proc);
2578 "wrong default_proc type %s (expected Proc)",
2582 SET_PROC_DEFAULT(hash, proc);
2591 if (rb_equal(value, args[0])) {
2622 rb_hash_foreach(hash, key_i, (
VALUE)args);
2628rb_hash_stlike_delete(
VALUE hash, st_data_t *pkey, st_data_t *pval)
2630 if (RHASH_AR_TABLE_P(hash)) {
2631 return ar_delete(hash, pkey, pval);
2634 return st_delete(RHASH_ST_TABLE(hash), pkey, pval);
2646 st_data_t ktmp = (st_data_t)key, val;
2648 if (rb_hash_stlike_delete(hash, &ktmp, &val)) {
2664 VALUE deleted_value = rb_hash_delete_entry(hash, key);
2666 if (!UNDEF_P(deleted_value)) {
2667 return deleted_value;
2713 rb_hash_modify_check(hash);
2714 val = rb_hash_delete_entry(hash, key);
2716 if (!UNDEF_P(val)) {
2717 compact_after_delete(hash);
2762rb_hash_shift(
VALUE hash)
2766 rb_hash_modify_check(hash);
2767 if (RHASH_AR_TABLE_P(hash)) {
2769 if (!hash_iterating_p(hash)) {
2770 if (ar_shift(hash, &var.key, &var.val)) {
2775 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2776 if (!UNDEF_P(var.key)) {
2777 rb_hash_delete_entry(hash, var.key);
2782 if (RHASH_ST_TABLE_P(hash)) {
2784 if (!hash_iterating_p(hash)) {
2785 if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
2790 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2791 if (!UNDEF_P(var.key)) {
2792 rb_hash_delete_entry(hash, var.key);
2804 rb_hash_modify(hash);
2813 return rb_hash_size(hash);
2837 rb_hash_modify_check(hash);
2838 if (!RHASH_TABLE_EMPTY_P(hash)) {
2839 rb_hash_foreach(hash, delete_if_i, hash);
2840 compact_after_delete(hash);
2865rb_hash_reject_bang(
VALUE hash)
2870 rb_hash_modify(hash);
2872 if (!n)
return Qnil;
2873 rb_hash_foreach(hash, delete_if_i, hash);
2898rb_hash_reject(
VALUE hash)
2903 result = hash_dup_with_compare_by_id(hash);
2905 rb_hash_foreach(result, delete_if_i, result);
2906 compact_after_delete(result);
2925rb_hash_slice(
int argc,
VALUE *argv,
VALUE hash)
2928 VALUE key, value, result;
2931 return copy_compare_by_id(rb_hash_new_capa(0), hash);
2933 result = copy_compare_by_id(rb_hash_new_capa(argc), hash);
2935 for (i = 0; i < argc; i++) {
2937 value = rb_hash_lookup2(hash, key,
Qundef);
2938 if (!UNDEF_P(value))
2939 rb_hash_aset(result, key, value);
2960rb_hash_except(
int argc,
VALUE *argv,
VALUE hash)
2965 result = hash_dup_with_compare_by_id(hash);
2967 for (i = 0; i < argc; i++) {
2969 rb_hash_delete(result, key);
2971 compact_after_delete(result);
2994rb_hash_values_at(
int argc,
VALUE *argv,
VALUE hash)
2999 for (i=0; i<argc; i++) {
3030rb_hash_fetch_values(
int argc,
VALUE *argv,
VALUE hash)
3035 for (i=0; i<argc; i++) {
3036 rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
3045 rb_hash_modify(hash);
3068rb_hash_select(
VALUE hash)
3073 result = hash_dup_with_compare_by_id(hash);
3075 rb_hash_foreach(result, keep_if_i, result);
3076 compact_after_delete(result);
3102rb_hash_select_bang(
VALUE hash)
3107 rb_hash_modify_check(hash);
3109 if (!n)
return Qnil;
3110 rb_hash_foreach(hash, keep_if_i, hash);
3133rb_hash_keep_if(
VALUE hash)
3136 rb_hash_modify_check(hash);
3137 if (!RHASH_TABLE_EMPTY_P(hash)) {
3138 rb_hash_foreach(hash, keep_if_i, hash);
3161 rb_hash_modify_check(hash);
3163 if (hash_iterating_p(hash)) {
3164 rb_hash_foreach(hash, clear_i, 0);
3166 else if (RHASH_AR_TABLE_P(hash)) {
3170 st_clear(RHASH_ST_TABLE(hash));
3171 compact_after_delete(hash);
3178hash_aset(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
3185rb_hash_key_str(
VALUE key)
3188 return rb_fstring(key);
3196hash_aset_str(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
3199 *key = rb_hash_key_str(*key);
3201 return hash_aset(key, val, arg, existing);
3204NOINSERT_UPDATE_CALLBACK(hash_aset)
3205NOINSERT_UPDATE_CALLBACK(hash_aset_str)
3239 bool iter_p = hash_iterating_p(hash);
3241 rb_hash_modify(hash);
3243 if (!RHASH_STRING_KEY_P(hash, key)) {
3244 RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset, val);
3247 RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset_str, val);
3276 rb_hash_modify_check(hash);
3277 if (hash == hash2)
return hash;
3278 if (hash_iterating_p(hash)) {
3281 hash2 = to_hash(hash2);
3283 COPY_DEFAULT(hash, hash2);
3285 if (RHASH_AR_TABLE_P(hash)) {
3286 hash_ar_free_and_clear_table(hash);
3289 hash_st_free_and_clear_table(hash);
3292 hash_copy(hash, hash2);
3333rb_hash_empty_p(
VALUE hash)
3366rb_hash_each_value(
VALUE hash)
3369 rb_hash_foreach(hash, each_value_i, 0);
3400rb_hash_each_key(
VALUE hash)
3403 rb_hash_foreach(hash, each_key_i, 0);
3446rb_hash_each_pair(
VALUE hash)
3449 if (rb_block_pair_yield_optimizable())
3450 rb_hash_foreach(hash, each_pair_i_fast, 0);
3452 rb_hash_foreach(hash, each_pair_i, 0);
3466 VALUE trans = p->trans, result = p->result;
3467 VALUE new_key = rb_hash_lookup2(trans, key,
Qundef);
3468 if (UNDEF_P(new_key)) {
3474 rb_hash_aset(result, new_key, value);
3482 rb_hash_aset(result, new_key, value);
3575rb_hash_transform_keys(
int argc,
VALUE *argv,
VALUE hash)
3582 transarg.trans = to_hash(argv[0]);
3590 if (transarg.trans) {
3591 transarg.result = result;
3592 rb_hash_foreach(hash, transform_keys_hash_i, (
VALUE)&transarg);
3595 rb_hash_foreach(hash, transform_keys_i, result);
3699rb_hash_transform_keys_bang(
int argc,
VALUE *argv,
VALUE hash)
3702 int block_given = 0;
3706 trans = to_hash(argv[0]);
3712 rb_hash_modify_check(hash);
3713 if (!RHASH_TABLE_EMPTY_P(hash)) {
3717 rb_hash_foreach(hash, flatten_i, pairs);
3724 else if (!UNDEF_P(new_key = rb_hash_lookup2(trans, key,
Qundef))) {
3727 else if (block_given) {
3734 if (!hash_stlike_lookup(new_keys, key, NULL)) {
3735 rb_hash_stlike_delete(hash, &key, NULL);
3737 rb_hash_aset(hash, new_key, val);
3738 rb_hash_aset(new_keys, new_key,
Qnil);
3742 compact_after_delete(hash);
3747transform_values_foreach_func(st_data_t key, st_data_t value, st_data_t argp,
int error)
3753transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t argp,
int existing)
3757 rb_hash_modify(hash);
3763transform_values_call(
VALUE hash)
3765 rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash);
3770transform_values(
VALUE hash)
3772 hash_iter_lev_inc(hash);
3773 rb_ensure(transform_values_call, hash, hash_foreach_ensure, hash);
3795rb_hash_transform_values(
VALUE hash)
3800 result = hash_dup_with_compare_by_id(hash);
3801 SET_DEFAULT(result,
Qnil);
3804 transform_values(result);
3805 compact_after_delete(result);
3833rb_hash_transform_values_bang(
VALUE hash)
3836 rb_hash_modify_check(hash);
3838 if (!RHASH_TABLE_EMPTY_P(hash)) {
3839 transform_values(hash);
3866rb_hash_to_a(
VALUE hash)
3871 rb_hash_foreach(hash, to_a_i, ary);
3877symbol_key_needs_quote(
VALUE str)
3880 if (
len == 0 || !rb_str_symname_p(str))
return true;
3881 const char *s = RSTRING_PTR(str);
3883 if (first ==
'@' || first ==
'$' || first ==
'!')
return true;
3884 if (!at_char_boundary(s, s +
len - 1,
RSTRING_END(str), rb_enc_get(str)))
return false;
3885 switch (s[
len - 1]) {
3916 quote = symbol_key_needs_quote(str2);
3921 if (RSTRING_LEN(str) > 1) {
3925 rb_enc_copy(str, str2);
3942inspect_hash(
VALUE hash,
VALUE dummy,
int recur)
3948 rb_hash_foreach(hash, inspect_i, str);
3967rb_hash_inspect(
VALUE hash)
3983rb_hash_to_hash(
VALUE hash)
3995 rb_raise(
rb_eTypeError,
"wrong element type %s (expected array)",
3996 rb_builtin_class_name(arg));
3999 rb_raise(rb_eArgError,
"element has wrong array length (expected 2, was %"PRIdLEN
")",
4014rb_hash_to_h_block(
VALUE hash)
4017 rb_hash_foreach(hash, to_h_i, h);
4042rb_hash_to_h(
VALUE hash)
4045 return rb_hash_to_h_block(hash);
4074rb_hash_keys(
VALUE hash)
4079 if (size == 0)
return keys;
4081 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
4083 if (RHASH_AR_TABLE_P(hash)) {
4084 size = ar_keys(hash, ptr, size);
4087 st_table *table = RHASH_ST_TABLE(hash);
4088 size = st_keys(table, ptr, size);
4091 rb_gc_writebarrier_remember(keys);
4092 rb_ary_set_len(keys, size);
4095 rb_hash_foreach(hash, keys_i, keys);
4121rb_hash_values(
VALUE hash)
4127 if (size == 0)
return values;
4129 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
4130 if (RHASH_AR_TABLE_P(hash)) {
4131 rb_gc_writebarrier_remember(values);
4133 size = ar_values(hash, ptr, size);
4136 else if (RHASH_ST_TABLE_P(hash)) {
4137 st_table *table = RHASH_ST_TABLE(hash);
4138 rb_gc_writebarrier_remember(values);
4140 size = st_values(table, ptr, size);
4143 rb_ary_set_len(values, size);
4146 rb_hash_foreach(hash, values_i, values);
4168 return RBOOL(hash_stlike_lookup(hash, key, NULL));
4176 if (rb_equal(value, data[1])) {
4199 rb_hash_foreach(hash, rb_hash_search_value, (
VALUE)data);
4215 if (!hash_stlike_lookup(data->hash, key, &val2)) {
4220 if (!(data->eql ? rb_eql(val1, (
VALUE)val2) : (
int)rb_equal(val1, (
VALUE)val2))) {
4229recursive_eql(
VALUE hash,
VALUE dt,
int recur)
4233 if (recur)
return Qtrue;
4235 data->result =
Qtrue;
4236 rb_hash_foreach(hash, eql_i, dt);
4238 return data->result;
4246 if (hash1 == hash2)
return Qtrue;
4252 if (rb_eql(hash2, hash1)) {
4260 return rb_equal(hash2, hash1);
4265 if (!RHASH_TABLE_EMPTY_P(hash1) && !RHASH_TABLE_EMPTY_P(hash2)) {
4266 if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2)) {
4278 FL_TEST(hash1, RHASH_PROC_DEFAULT) ==
FL_TEST(hash2, RHASH_PROC_DEFAULT)))
4310 return hash_equal(hash1, hash2, FALSE);
4337 return hash_equal(hash1, hash2, TRUE);
4343 st_index_t *hval = (st_index_t *)arg;
4344 st_index_t hdata[2];
4346 hdata[0] = rb_hash(key);
4347 hdata[1] = rb_hash(val);
4348 *hval ^= st_hash(hdata,
sizeof(hdata), 0);
4370rb_hash_hash(
VALUE hash)
4376 rb_hash_foreach(hash, hash_i, (
VALUE)&hval);
4385 rb_hash_aset(hash, value, key);
4409rb_hash_invert(
VALUE hash)
4413 rb_hash_foreach(hash, rb_hash_invert_i, h);
4420 rb_hash_aset(hash, key, value);
4425 VALUE hash, newvalue, *argv;
4432rb_hash_update_block_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
4436 VALUE newvalue = ua->newvalue, hash = arg->hash;
4439 hash_iter_lev_inc(hash);
4440 ua->iterating =
true;
4442 hash_iter_lev_dec(hash);
4443 ua->iterating =
false;
4445 else if (RHASH_STRING_KEY_P(hash, k) && !
RB_OBJ_FROZEN(k)) {
4446 *key = (st_data_t)rb_hash_key_str(k);
4448 *value = (st_data_t)newvalue;
4452NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback)
4458 ua->newvalue = value;
4459 RHASH_UPDATE(ua->hash, key, rb_hash_update_block_callback, args);
4464rb_hash_update_call(
VALUE args)
4468 for (
int i = 0; i < arg->argc; i++){
4469 VALUE hash = to_hash(arg->argv[i]);
4470 if (arg->block_given) {
4471 rb_hash_foreach(hash, rb_hash_update_block_i, args);
4474 rb_hash_foreach(hash, rb_hash_update_i, arg->hash);
4481rb_hash_update_ensure(
VALUE args)
4484 if (ua->iterating) hash_iter_lev_dec(ua->hash);
4533rb_hash_update(
int argc,
VALUE *argv,
VALUE self)
4544 rb_hash_modify(self);
4545 return rb_ensure(rb_hash_update_call, arg, rb_hash_update_ensure, arg);
4555rb_hash_update_func_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
4558 VALUE newvalue = uf_arg->value;
4561 newvalue = (*uf_arg->func)((
VALUE)*key, (
VALUE)*value, newvalue);
4567NOINSERT_UPDATE_CALLBACK(rb_hash_update_func_callback)
4573 VALUE hash = arg->hash;
4576 RHASH_UPDATE(hash, key, rb_hash_update_func_callback, (
VALUE)arg);
4583 rb_hash_modify(hash1);
4584 hash2 = to_hash(hash2);
4589 rb_hash_foreach(hash2, rb_hash_update_func_i, (
VALUE)&arg);
4592 rb_hash_foreach(hash2, rb_hash_update_i, hash1);
4598hash_merge_guess_size(
int argc,
VALUE *argv,
VALUE self)
4608 VALUE other = argv[0];
4614 unsigned bound = RHASH_AR_TABLE_BOUND(other);
4615 for (
unsigned i = 0; i < bound; i++) {
4616 VALUE key = RHASH_AR_TABLE_REF(other, i)->key;
4625 if (!hash_stlike_lookup(self, key, NULL)) {
4680rb_hash_merge(
int argc,
VALUE *argv,
VALUE self)
4682 size_t guessed_size = hash_merge_guess_size(argc, argv, self);
4683 VALUE ret = guessed_size ? rb_hash_dup_capa(self, guessed_size) : rb_hash_dup(self);
4684 return rb_hash_update(argc, argv, copy_compare_by_id(ret, self));
4690 return !
RTEST(rb_equal(a, b));
4699assoc_lookup(
VALUE arg)
4703 if (st_lookup(p->tbl, p->key, &data))
return (
VALUE)data;
4712 if (
RTEST(rb_equal(args[0], key))) {
4741 if (RHASH_ST_TABLE_P(hash) && !RHASH_IDENTHASH_P(hash)) {
4743 st_table assoctable = *RHASH_ST_TABLE(hash);
4744 assoctable.type = &(
struct st_hash_type){
4745 .compare = assoc_cmp,
4746 .hash = assoctable.type->hash,
4750 .key = (st_data_t)key,
4754 value = assoc_lookup(arg);
4757 hash_iter_lev_inc(hash);
4758 value =
rb_ensure(assoc_lookup, arg, hash_foreach_ensure, hash);
4766 rb_hash_foreach(hash, assoc_i, (
VALUE)args);
4775 if (
RTEST(rb_equal(args[0], val))) {
4805 rb_hash_foreach(hash, rassoc_i, (
VALUE)args);
4858rb_hash_flatten(
int argc,
VALUE *argv,
VALUE hash)
4867 if (level == 0)
return rb_hash_to_a(hash);
4870 rb_hash_foreach(hash, flatten_i, ary);
4875 rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level);
4877 else if (level < 0) {
4884 rb_hash_foreach(hash, flatten_i, ary);
4912rb_hash_compact(
VALUE hash)
4914 VALUE result = rb_hash_dup(hash);
4916 rb_hash_foreach(result, delete_if_nil, result);
4917 compact_after_delete(result);
4919 else if (rb_hash_compare_by_id_p(hash)) {
4920 result = rb_hash_compare_by_id(result);
4942rb_hash_compact_bang(
VALUE hash)
4945 rb_hash_modify_check(hash);
4948 rb_hash_foreach(hash, delete_if_nil, hash);
4982rb_hash_compare_by_id(
VALUE hash)
4985 st_table *identtable;
4987 if (rb_hash_compare_by_id_p(hash))
return hash;
4989 rb_hash_modify_check(hash);
4990 if (hash_iterating_p(hash)) {
4994 if (RHASH_AR_TABLE_P(hash)) {
4995 unsigned int bound = RHASH_AR_TABLE_BOUND(hash);
4996 for (
unsigned int i = 0; i < bound; i++) {
4997 if (ar_cleared_entry(hash, i))
continue;
4999 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
5000 ar_hint_set(hash, i, (st_hash_t)rb_ident_hash(pair->key));
5003 else if (RHASH_TABLE_EMPTY_P(hash)) {
5005 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
5007 RHASH_ST_TABLE(hash)->type = &identhash;
5012 tmp = hash_alloc_capa(0, 0);
5015 identtable = RHASH_ST_TABLE(tmp);
5017 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
5022 rb_hash_st_table_set(hash, identtable);
5023 RHASH_ST_CLEAR(tmp);
5028 rb_gc_register_pinning_obj(hash);
5049rb_hash_compare_by_id_p(
VALUE hash)
5051 return RBOOL(RHASH_IDENTHASH_P(hash));
5055rb_ident_hash_new(
void)
5057 VALUE hash = rb_hash_new_capa(0);
5059 hash_st_table_init(hash, 0);
5060 rb_gc_register_pinning_obj(hash);
5065rb_ident_hash_new_capa(
rb_len_t size)
5067 VALUE hash = rb_hash_new_capa(0);
5069 hash_st_table_init(hash, size);
5070 rb_gc_register_pinning_obj(hash);
5075rb_init_identtable(
void)
5077 return st_init_table(&identhash);
5081rb_init_existing_identtable_with_size(st_table *tbl, st_index_t size)
5083 st_init_existing_table_with_size(tbl, &identhash, size);
5160rb_hash_any_p(
int argc,
VALUE *argv,
VALUE hash)
5169 rb_warn(
"given block not used");
5173 rb_hash_foreach(hash, any_p_i_pattern, (
VALUE)args);
5180 if (rb_block_pair_yield_optimizable())
5181 rb_hash_foreach(hash, any_p_i_fast, (
VALUE)args);
5183 rb_hash_foreach(hash, any_p_i, (
VALUE)args);
5224rb_hash_dig(
int argc,
VALUE *argv,
VALUE self)
5227 self = rb_hash_aref(self, *argv);
5228 if (!--argc)
return self;
5230 return rb_obj_dig(argc, argv, self,
Qnil);
5238 if (!UNDEF_P(v) && rb_equal(value, v))
return ST_CONTINUE;
5249 rb_hash_foreach(hash1, hash_le_i, (
VALUE)args);
5274 other = to_hash(other);
5276 return hash_le(hash, other);
5302 other = to_hash(other);
5304 return hash_le(hash, other);
5328 other = to_hash(other);
5330 return hash_le(other, hash);
5356 other = to_hash(other);
5358 return hash_le(other, hash);
5365 return rb_hash_aref(hash, *argv);
5386rb_hash_to_proc(
VALUE hash)
5388 return rb_func_lambda_new(hash_proc_call, hash, 1, 1);
5393rb_hash_deconstruct_keys(
VALUE hash,
VALUE keys)
5399add_new_i(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
5401 if (existing)
return ST_STOP;
5413 int ret = rb_hash_stlike_update(hash, key, add_new_i, val);
5425 return (RHASH_STRING_KEY_P(hash, key) && !
RB_OBJ_FROZEN(key)) ?
5426 rb_hash_key_str(key) : key;
5433 for (i = 0; i < argc; ) {
5434 st_data_t k = key_stringify(hash, argv[i++]);
5435 st_data_t v = argv[i++];
5436 ar_insert(hash, k, v);
5445 HASH_ASSERT(argc % 2 == 0);
5447 st_index_t size = argc / 2;
5449 if (RHASH_AR_TABLE_P(hash) &&
5450 (RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_BOUND(hash))) {
5451 ar_bulk_insert(hash, argc, argv);
5454 rb_hash_bulk_insert_into_st_table(argc, argv, hash);
5462 VALUE val = hash_new_capa(klass, argc / 2);
5463 rb_hash_bulk_insert(argc, argv, val);
5468rb_hash_new_with_bulk_insert(
rb_len_t argc,
const VALUE *argv)
5470 return hash_new_with_bulk_insert(
rb_cHash, argc, argv);
5480 val = hash_dup_capa(val,
RHASH_SIZE(val) + argc / 2);
5482 rb_hash_bulk_insert(argc, argv, val);
5495 rb_hash_foreach(h2, rb_hash_update_i, val);
5499#undef USE_ORIGENVIRON
5500#if !defined(_WIN32) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
5501# define USE_ORIGENVIRON 1
5502static char **origenviron;
5505#define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
5506#define FREE_ENVIRON(e) rb_w32_free_environ(e)
5507static char **my_environ;
5509#define environ my_environ
5510#elif defined(__APPLE__)
5512#define environ (*_NSGetEnviron())
5513#define GET_ENVIRON(e) (e)
5514#define FREE_ENVIRON(e)
5516extern char **environ;
5517#define GET_ENVIRON(e) (e)
5518#define FREE_ENVIRON(e)
5520#ifdef ENV_IGNORECASE
5521#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
5522#define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
5524#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
5525#define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
5528#define ENV_LOCKING() RB_VM_LOCKING()
5530static inline rb_encoding *
5547env_str_encode(
VALUE str, rb_encoding *enc)
5551 int eidx = rb_enc_to_index(enc);
5558 else if (!ienc || ienc == enc) {
5559 rb_enc_associate(str, enc);
5562 rb_enc_associate(str, ienc);
5565 VALUE conv = rb_enc_str_new(NULL, 0, ienc);
5566 if (
NIL_P(rb_str_cat_conv_enc_opts(conv, 0, RSTRING_PTR(str), RSTRING_LEN(str), enc, 0,
Qnil))) {
5568 rb_enc_associate(str, enc);
5580env_ary_encode(
VALUE ary, rb_encoding *enc)
5588getenv_with_lock(
const char *name)
5591 rb_encoding *enc = env_encoding();
5593 const char *val = getenv(name);
5596 return env_str_encode(ret, enc);
5600has_env_with_lock(
const char *name)
5608 return val ? true :
false;
5611static const char TZ_ENV[] =
"TZ";
5614get_env_cstr(
VALUE str,
const char *name)
5617 rb_encoding *enc = rb_enc_get(str);
5618 if (!rb_enc_asciicompat(enc)) {
5619 rb_raise(rb_eArgError,
"bad environment variable %s: ASCII incompatible encoding: %s",
5620 name, rb_enc_name(enc));
5622 var = RSTRING_PTR(str);
5623 if (memchr(var,
'\0', RSTRING_LEN(str))) {
5624 rb_raise(rb_eArgError,
"bad environment variable %s: contains null byte", name);
5626 return rb_str_fill_terminator(str, 1);
5629#define get_env_ptr(var, val) \
5630 (var = get_env_cstr(val, #var))
5632static inline const char *
5633env_name(
volatile VALUE *s)
5637 get_env_ptr(name, *s);
5641#define env_name(s) env_name(&(s))
5646reset_by_modified_env(
const char *nam,
const char *val)
5654 if (ENVMATCH(nam, TZ_ENV)) {
5655 ruby_reset_timezone(val);
5660env_delete(
VALUE name)
5662 const char *nam = env_name(name);
5663 reset_by_modified_env(nam, NULL);
5664 VALUE val = getenv_with_lock(nam);
5667 ruby_setenv(nam, 0);
5702 val = env_delete(name);
5722 const char *nam = env_name(name);
5723 VALUE env = getenv_with_lock(nam);
5763 if (block_given && argc == 2) {
5764 rb_warn(
"block supersedes default value argument");
5766 nam = env_name(key);
5767 env = getenv_with_lock(nam);
5770 if (block_given)
return rb_yield(key);
5772 rb_key_err_raise(rb_sprintf(
"key not found: \"%"PRIsVALUE
"\"", key), envtbl, key);
5820env_fetch_values(
int argc,
VALUE *argv,
VALUE ehash)
5825 for (i=0; i<argc; i++) {
5826 rb_ary_push(result, env_fetch(1, &argv[i], ehash));
5832#if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
5835envix(
const char *nam)
5839 register int i,
len = strlen(nam);
5842 env = GET_ENVIRON(environ);
5843 for (i = 0; env[i]; i++) {
5844 if (ENVNMATCH(env[i],nam,
len) && env[i][
len] ==
'=')
5847 FREE_ENVIRON(environ);
5854NORETURN(
static void invalid_envname(
const char *name));
5857invalid_envname(
const char *name)
5863check_envname(
const char *name)
5865 if (strchr(name,
'=')) {
5866 invalid_envname(name);
5873ruby_setenv(
const char *name,
const char *value)
5881 check_envname(name);
5882 len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
5885 len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
5887 wvalue = wname +
len;
5888 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname,
len);
5889 MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, len2);
5893 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname,
len);
5894 wvalue = wname +
len;
5901 failed = _wputenv_s(wname, wvalue);
5907 if (!value || !*value) {
5909 if (!SetEnvironmentVariableW(wname, value ? wvalue : NULL) &&
5910 GetLastError() != ERROR_ENVVAR_NOT_FOUND)
goto fail;
5914 invalid_envname(name);
5916#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
5920 ret = setenv(name, value, 1);
5923 if (ret) rb_sys_fail_sprintf(
"setenv(%s)", name);
5928 ret = unsetenv(name);
5931 if (ret) rb_sys_fail_sprintf(
"unsetenv(%s)", name);
5940 if (environ == origenviron) {
5945 for (max = i; environ[max]; max++) ;
5946 tmpenv =
ALLOC_N(
char*, max+2);
5947 for (j=0; j<max; j++)
5954 char **envp = origenviron;
5955 while (*envp && *envp != environ[i]) envp++;
5959 while (environ[i]) {
5960 environ[i] = environ[i+1];
5967 if (!value)
goto finish;
5972 len = strlen(name) + strlen(value) + 2;
5974 snprintf(environ[i],
len,
"%s=%s",name,value);
5982ruby_unsetenv(
const char *name)
5984 ruby_setenv(name, 0);
6032 return env_aset(nm, val);
6048 get_env_ptr(name, nm);
6049 get_env_ptr(value, val);
6051 ruby_setenv(name, value);
6052 reset_by_modified_env(name, value);
6059 rb_encoding *enc = raw ? 0 : env_encoding();
6063 char **env = GET_ENVIRON(environ);
6065 char *s = strchr(*env,
'=');
6067 const char *p = *env;
6074 FREE_ENVIRON(environ);
6077 if (!raw) env_ary_encode(ary, enc);
6097 return env_keys(FALSE);
6107 env = GET_ENVIRON(environ);
6108 for (; *env ; ++env) {
6109 if (strchr(*env,
'=')) {
6113 FREE_ENVIRON(environ);
6137env_each_key(
VALUE ehash)
6143 keys = env_keys(FALSE);
6155 rb_encoding *enc = env_encoding();
6157 char **env = GET_ENVIRON(environ);
6160 char *s = strchr(*env,
'=');
6166 FREE_ENVIRON(environ);
6169 env_ary_encode(ary, enc);
6188 return env_values();
6209env_each_value(
VALUE ehash)
6215 values = env_values();
6241env_each_pair(
VALUE ehash)
6249 rb_encoding *enc = env_encoding();
6251 char **env = GET_ENVIRON(environ);
6254 char *s = strchr(*env,
'=');
6261 FREE_ENVIRON(environ);
6264 env_ary_encode(ary, enc);
6266 if (rb_block_pair_yield_optimizable()) {
6303env_reject_bang(
VALUE ehash)
6310 keys = env_keys(FALSE);
6311 RBASIC_CLEAR_CLASS(keys);
6322 if (del == 0)
return Qnil;
6342env_delete_if(
VALUE ehash)
6345 env_reject_bang(ehash);
6373 for (i=0; i<argc; i++) {
6395env_select(
VALUE ehash)
6403 keys = env_keys(FALSE);
6409 rb_hash_aset(result, key, val);
6442env_select_bang(
VALUE ehash)
6449 keys = env_keys(FALSE);
6450 RBASIC_CLEAR_CLASS(keys);
6461 if (del == 0)
return Qnil;
6480env_keep_if(
VALUE ehash)
6483 env_select_bang(ehash);
6503 VALUE key, value, result;
6505 result = rb_hash_new_capa(argc);
6507 for (i = 0; i < argc; i++) {
6509 value = rb_f_getenv(
Qnil, key);
6511 rb_hash_aset(result, key, value);
6523 keys = env_keys(TRUE);
6526 const char *nam = RSTRING_PTR(key);
6527 ruby_setenv(nam, 0);
6575 rb_encoding *enc = env_encoding();
6578 char **env = GET_ENVIRON(environ);
6580 const char *s = strchr(*env,
'=');
6592 FREE_ENVIRON(environ);
6595 env_ary_encode(ary, enc);
6596 for (
long i = 0; i <
RARRAY_LEN(ary); i += 2) {
6626 rb_encoding *enc = env_encoding();
6628 char **env = GET_ENVIRON(environ);
6630 char *s = strchr(*env,
'=');
6637 FREE_ENVIRON(environ);
6661env_size_with_lock(
void)
6666 char **env = GET_ENVIRON(environ);
6668 FREE_ENVIRON(environ);
6687 return INT2FIX(env_size_with_lock());
6706 char **env = GET_ENVIRON(environ);
6710 FREE_ENVIRON(environ);
6713 return RBOOL(empty);
6742 const char *s = env_name(key);
6743 return RBOOL(has_env_with_lock(s));
6769 const char *s = env_name(key);
6770 VALUE e = getenv_with_lock(s);
6801 char **env = GET_ENVIRON(environ);
6803 char *s = strchr(*env,
'=');
6806 if (RSTRING_LEN(obj) ==
len && strncmp(s, RSTRING_PTR(obj),
len) == 0) {
6813 FREE_ENVIRON(environ);
6842 char **env = GET_ENVIRON(environ);
6845 const char *p = *env;
6846 const char *s = strchr(p,
'=');
6849 if (RSTRING_LEN(obj) ==
len && strncmp(s, RSTRING_PTR(obj),
len) == 0) {
6856 FREE_ENVIRON(environ);
6884 rb_encoding *enc = env_encoding();
6886 char **env = GET_ENVIRON(environ);
6888 char *s = strchr(*env,
'=');
6891 if (RSTRING_LEN(value) ==
len && strncmp(s, RSTRING_PTR(value),
len) == 0) {
6898 FREE_ENVIRON(environ);
6901 return env_str_encode(str, enc);
6905environ_size(
char **env)
6920 rb_encoding *enc = env_encoding();
6922 char **env = GET_ENVIRON(environ);
6925 char *s = strchr(*env,
'=');
6932 FREE_ENVIRON(environ);
6936 for (
long i = 0; i <
RARRAY_LEN(ary); i += 2) {
6937 rb_hash_aset(hash, env_str_encode(
RARRAY_AREF(ary, i), enc),
6952 return env_to_hash();
6967 return env_to_hash();
6991 VALUE hash = env_to_hash();
6993 hash = rb_hash_to_h_block(hash);
7011 VALUE key, hash = env_to_hash();
7013 for (i = 0; i < argc; i++) {
7015 rb_hash_delete(hash, key);
7039 return rb_hash_delete_if(env_to_hash());
7042NORETURN(
static VALUE env_freeze(
VALUE self));
7051env_freeze(
VALUE self)
7078 char **env = GET_ENVIRON(environ);
7080 const char *p = *env;
7081 const char *s = strchr(p,
'=');
7086 FREE_ENVIRON(environ);
7091 key = env_str_encode(key, env_encoding());
7092 VALUE val = getenv_with_lock(RSTRING_PTR(key));
7116 return rb_hash_invert(env_to_hash());
7123 const char *keyptr, *eptr;
7131 if (elen != keylen)
continue;
7132 if (!ENVNMATCH(keyptr, eptr, elen))
continue;
7144 keylist_delete(keys, key);
7172 keys = env_keys(TRUE);
7173 if (env == hash)
return env;
7174 hash = to_hash(hash);
7175 rb_hash_foreach(hash, env_replace_i, keys);
7195 if (!
NIL_P(oldval)) {
7242 env_update_block_i : env_update_i;
7243 for (
int i = 0; i < argc; ++i) {
7244 VALUE hash = argv[i];
7245 if (env == hash)
continue;
7246 hash = to_hash(hash);
7247 rb_hash_foreach(hash, func, 0);
7267 rb_get_freeze_opt(1, &opt);
7271 rb_raise(
rb_eTypeError,
"Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash");
7285 rb_raise(
rb_eTypeError,
"Cannot dup ENV, use ENV.to_h to get a copy of ENV as a hash");
7296 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
7766 id_hash_iter_lev = rb_make_internal_id();
7865 rb_cHash_empty_frozen = rb_hash_freeze(rb_hash_alloc_fixed_size(
rb_cHash, 0));
7867 rb_vm_register_global_object(rb_cHash_empty_frozen);
8034#ifdef USE_ORIGENVIRON
8035 origenviron = environ;
8105 HASH_ASSERT(
sizeof(ar_hint_t) * RHASH_AR_TABLE_MAX_SIZE ==
sizeof(
VALUE));
8108#include "hash.rbinc"
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
int rb_block_given_p(void)
Determines if the current method is given a block.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
#define TYPE(_)
Old name of rb_type.
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#define NUM2LL
Old name of RB_NUM2LL.
#define REALLOC_N
Old name of RB_REALLOC_N.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define T_NIL
Old name of RUBY_T_NIL.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define rb_str_buf_new2
Old name of rb_str_buf_new_cstr.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define T_DATA
Old name of RUBY_T_DATA.
#define CLASS_OF
Old name of rb_class_of.
#define LONG2FIX
Old name of RB_INT2FIX.
#define STATIC_SYM_P
Old name of RB_STATIC_SYM_P.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define T_TRUE
Old name of RUBY_T_TRUE.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
#define T_FALSE
Old name of RUBY_T_FALSE.
#define FIXNUM_MIN
Old name of RUBY_FIXNUM_MIN.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
#define FIXNUM_MAX
Old name of RUBY_FIXNUM_MAX.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define POSFIXABLE
Old name of RB_POSFIXABLE.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define FL_TEST
Old name of RB_FL_TEST.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define NUM2LEN
Old name of RB_NUM2LEN.
#define OBJ_WB_UNPROTECT
Old name of RB_OBJ_WB_UNPROTECT.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void rb_syserr_fail_str(int e, VALUE mesg)
Identical to rb_syserr_fail(), except it takes the message in Ruby's String instead of C's.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_mKernel
Kernel module.
VALUE rb_cObject
Object class.
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
Identical to rb_class_new_instance(), except it passes the passed keywords if any to the #initialize ...
VALUE rb_cHash
Hash class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
VALUE rb_cString
String class.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
rb_encoding * rb_default_internal_encoding(void)
Queries the "default internal" encoding.
int rb_ascii8bit_encindex(void)
Identical to rb_ascii8bit_encoding(), except it returns the encoding's index instead of the encoding ...
rb_encoding * rb_locale_encoding(void)
Queries the encoding that represents the current locale.
int rb_usascii_encindex(void)
Identical to rb_usascii_encoding(), except it returns the encoding's index instead of the encoding it...
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_delete_at(VALUE ary, rb_len_t pos)
Destructively removes an element which resides at the specific index of the passed array.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_hidden_new(rb_len_t capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, rb_len_t len)
Destructively appends multiple elements at the end of the array.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value)
Type of callback functions to pass to rb_hash_update_by().
#define st_foreach_safe
Just another name of rb_st_foreach_safe.
VALUE rb_env_clear(void)
Destructively removes every environment variables of the running process.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
VALUE rb_proc_lambda_p(VALUE recv)
Queries if the given object is a lambda.
VALUE rb_proc_call_with_block(VALUE recv, int argc, const VALUE *argv, VALUE proc)
Identical to rb_proc_call(), except you can additionally pass another proc object,...
int rb_proc_arity(VALUE recv)
Queries the number of mandatory arguments of the given Proc.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
#define rb_hash_end(h)
Just another name of st_hash_end.
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
VALUE rb_str_ellipsize(VALUE str, rb_len_t len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
st_index_t rb_memhash(const void *ptr, rb_len_t len)
This is a universal hash function.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
VALUE rb_str_buf_cat_ascii(VALUE dst, const char *src)
Identical to rb_str_cat_cstr(), except it additionally assumes the source string be a NUL terminated ...
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well... Let us hesitate from describing what a "basic definition" is.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
int capa
Designed capacity of the buffer.
int len
Length of the buffer.
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
char * ruby_strdup(const char *str)
This is our own version of strdup(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
VALUE rb_yield_values2(int n, const VALUE *argv)
Identical to rb_yield_values(), except it takes the parameters as a C array instead of variadic argum...
VALUE rb_yield(VALUE val)
Yields the block.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
int st_foreach_check(st_table *q, int_type *w, st_data_t e, st_data_t)
Iteration over the given table.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
#define RARRAY_AREF(a, i)
References an object in an array.
static void RARRAY_ASET(VALUE ary, rb_len_t i, VALUE v)
Assigns an object in an array.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RHASH_SET_IFNONE(h, ifnone)
Destructively updates the default value of the hash.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
@ RUBY_SPECIAL_SHIFT
Least significant 8 bits are reserved.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
VALUE flags
Per-object flags.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
unsigned long rb_ulen_t
An unsigned integer type that has the same width with rb_len_t.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.