Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 113 lines (96 sloc) 2.198 kb
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
1 /*
2 * MacRuby Hash.
3 *
4 * Copyright (C) 2010, Apple Inc. All rights reserved.
5 */
6
7 #ifndef __HASH_H_
8 #define __HASH_H_
9
10 #if defined(__cplusplus)
11 extern "C" {
12 #endif
13
14 typedef struct {
15 struct RBasic basic;
16 st_table *tbl;
17 VALUE ifnone;
18 bool has_proc_default;
19 } rb_hash_t;
20
21 #define RHASH(x) ((rb_hash_t *)x)
22
23 static inline bool
24 rb_klass_is_rhash(VALUE klass)
25 {
26 do {
27 if (klass == rb_cRubyHash) {
28 return true;
29 }
30 if (klass == rb_cNSHash) {
31 return false;
32 }
33 klass = RCLASS_SUPER(klass);
34 }
35 while (klass != 0);
36 return false;
37 }
38
39 #define IS_RHASH(x) (rb_klass_is_rhash(*(VALUE *)x))
40
41 static inline void
42 rhash_modify(VALUE hash)
43 {
44 const long mask = RBASIC(hash)->flags;
45 if ((mask & FL_FREEZE) == FL_FREEZE) {
46 rb_raise(rb_eRuntimeError, "can't modify frozen/immutable hash");
47 }
a58377f faster r{ary,hash,str}_modify() functions
Laurent Sansonetti authored
48 if ((mask & FL_TAINT) == FL_TAINT) {
49 if (rb_safe_level() >= 4) {
50 rb_raise(rb_eSecurityError, "Insecure: can't modify hash");
51 }
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
52 }
53 }
54
55 static inline long
56 rhash_len(VALUE hash)
57 {
58 return RHASH(hash)->tbl->num_entries;
59 }
60
61 static inline void
62 rhash_foreach(VALUE hash, int (*func)(ANYARGS), VALUE farg)
63 {
64 st_foreach_safe(RHASH(hash)->tbl, func, (st_data_t)farg);
65 }
66
67 static inline VALUE
68 rhash_lookup(VALUE hash, VALUE key)
69 {
70 VALUE val;
71 if (st_lookup(RHASH(hash)->tbl, key, &val)) {
72 return val;
73 }
74 return Qundef;
75 }
76
77 static inline VALUE
002ed5d now compile some of the VM primitives into bitcode that will be added to...
Laurent Sansonetti authored
78 rhash_store(VALUE hash, VALUE key, VALUE val)
79 {
80 rhash_modify(hash);
81 if (TYPE(key) == T_STRING) {
82 key = rb_str_dup(key);
83 OBJ_FREEZE(key);
84 }
85 st_insert(RHASH(hash)->tbl, key, val);
86 return val;
87 }
88
89 static inline VALUE
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
90 rhash_delete_key(VALUE hash, VALUE key)
91 {
92 VALUE val;
93 if (st_delete(RHASH(hash)->tbl, &key, &val)) {
94 return val;
95 }
96 return Qundef;
97 }
98
99 VALUE rhash_dup(VALUE rcv, SEL sel);
100 VALUE rhash_aref(VALUE hash, SEL sel, VALUE key);
101 VALUE rhash_aset(VALUE hash, SEL sel, VALUE key, VALUE val);
102 VALUE rhash_keys(VALUE hash, SEL sel);
103 VALUE rhash_has_key(VALUE hash, SEL sel, VALUE key);
104 VALUE rhash_set_default(VALUE hash, SEL sel, VALUE ifnone);
105
01ec3c8 implement a better RubyHash hashing function for arrays, as the CF one i...
Laurent Sansonetti authored
106 unsigned long rb_hash_code(VALUE obj);
107
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
108 #if defined(__cplusplus)
109 } // extern "C"
110 #endif
111
112 #endif // __HASH_H_
Something went wrong with that request. Please try again.