Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 202 lines (172 sloc) 5.492 kB
311a41b rewrote class flags to not use the version field anymore, better typi…
Laurent Sansonetti authored
1 /*
2 * This file is covered by the Ruby license. See COPYING for more details.
7d7d3e8 @ferrous26 Change ownership to The MacRuby Team and update copyrights
ferrous26 authored
3 *
4 * Copyright (C) 2012, The MacRuby Team. All rights reserved.
9595725 update copyrights to 2011
Laurent Sansonetti authored
5 * Copyright (C) 2011, Apple Inc. All rights reserved.
311a41b rewrote class flags to not use the version field anymore, better typi…
Laurent Sansonetti authored
6 */
7
8 #ifndef __CLASS_H_
9 #define __CLASS_H_
10
11 #if defined(__cplusplus)
12 extern "C" {
13 #endif
14
d0e13a1 move internal stuff from the public headers
Laurent Sansonetti authored
15 bool rb_objc_hash_is_pure(VALUE);
16 bool rb_objc_str_is_pure(VALUE);
17 bool rb_objc_ary_is_pure(VALUE);
18
19 VALUE rb_objc_create_class(const char *name, VALUE super);
20 void rb_objc_class_sync_version(Class klass, Class super_class);
21 void rb_define_object_special_methods(VALUE klass);
22 VALUE rb_class_new_instance_imp(VALUE, SEL, int, VALUE *);
23 VALUE rb_make_singleton_class(VALUE super);
c1833dd some work on garbage collecting singleton classes - disabled for now …
Laurent Sansonetti authored
24 VALUE rb_singleton_class_attached_object(VALUE klass);
25 void rb_singleton_class_promote_for_gc(VALUE klass);
d0e13a1 move internal stuff from the public headers
Laurent Sansonetti authored
26
8c55086 better ivar slot system for RubyObject + misc fixes/cleanup
Laurent Sansonetti authored
27 #define RCLASS_IS_OBJECT_SUBCLASS (1<<1) /* class is a true RubyObject subclass */
311a41b rewrote class flags to not use the version field anymore, better typi…
Laurent Sansonetti authored
28 #define RCLASS_IS_RUBY_CLASS (1<<2) /* class was created from Ruby */
29 #define RCLASS_IS_MODULE (1<<3) /* class represents a Ruby Module */
30 #define RCLASS_IS_SINGLETON (1<<4) /* class represents a singleton */
31 #define RCLASS_IS_FROZEN (1<<5) /* class is frozen */
32 #define RCLASS_IS_TAINTED (1<<6) /* class is tainted */
33 #define RCLASS_IS_INCLUDED (1<<10) /* module is included */
34 #define RCLASS_HAS_ROBJECT_ALLOC (1<<11) /* class uses the default RObject alloc */
35 #define RCLASS_SCOPE_PRIVATE (1<<12) /* class opened for private methods */
36 #define RCLASS_SCOPE_PROTECTED (1<<13) /* class opened for protected methods */
37 #define RCLASS_SCOPE_MOD_FUNC (1<<14) /* class opened for module_function methods */
38 #define RCLASS_KVO_CHECK_DONE (1<<15) /* class created by KVO and flags merged */
39
f82d58b a more efficient class flags mechanism, reduced the number of default…
Laurent Sansonetti authored
40 typedef struct rb_class_flags_cache {
41 Class klass;
42 unsigned long value;
43 struct rb_class_flags_cache *next;
44 } rb_class_flags_cache_t;
45
70ea0b5 per-vm method cache + misc fixes/improvements
Laurent Sansonetti authored
46 #define CLASS_FLAGS_CACHE_SIZE 0x1000
f82d58b a more efficient class flags mechanism, reduced the number of default…
Laurent Sansonetti authored
47
48 extern rb_class_flags_cache_t *rb_class_flags;
49
50 static unsigned int
51 rb_class_flags_hash(Class k)
52 {
70ea0b5 per-vm method cache + misc fixes/improvements
Laurent Sansonetti authored
53 return ((unsigned long)k >> 2) & (CLASS_FLAGS_CACHE_SIZE - 1);
f82d58b a more efficient class flags mechanism, reduced the number of default…
Laurent Sansonetti authored
54 }
55
56 static inline unsigned long
57 rb_class_get_mask(Class k)
58 {
59 rb_class_flags_cache_t *e = &rb_class_flags[rb_class_flags_hash(k)];
60 while (e != NULL) {
61 if (e->klass == k) {
62 return e->value;
63 }
64 e = e->next;
65 }
66 return 0;
67 }
68
c1833dd some work on garbage collecting singleton classes - disabled for now …
Laurent Sansonetti authored
69 static inline bool
70 rb_class_erase_mask(Class k)
71 {
72 rb_class_flags_cache_t *e = &rb_class_flags[rb_class_flags_hash(k)];
73 while (e != NULL) {
74 if (e->klass == k) {
75 e->klass = 0;
76 return true;
77 }
78 e = e->next;
79 }
80 return false;
81 }
82
f82d58b a more efficient class flags mechanism, reduced the number of default…
Laurent Sansonetti authored
83 static inline void
84 rb_class_set_mask(Class k, unsigned long mask)
85 {
86 rb_class_flags_cache_t *e = &rb_class_flags[rb_class_flags_hash(k)];
87 again:
88 if (e->klass == k) {
89 set_value:
90 e->value = mask;
91 return;
92 }
93 if (e->klass == 0) {
94 e->klass = k;
95 goto set_value;
96 }
97 if (e->next != NULL) {
98 e = e->next;
99 goto again;
100 }
101 rb_class_flags_cache_t *ne = (rb_class_flags_cache_t *)malloc(
102 sizeof(rb_class_flags_cache_t));
103 ne->klass = k;
104 ne->value = mask;
105 ne->next = NULL;
106 e->next = ne;
107 }
108
109 #define RCLASS_MASK_TYPE_SHIFT 16
110
111 static inline unsigned long
112 rb_class_get_flags(Class k)
113 {
114 return rb_class_get_mask(k) >> RCLASS_MASK_TYPE_SHIFT;
115 }
116
117 static inline void
118 rb_class_set_flags(Class k, unsigned long flags)
119 {
97de43e fix regressions in module #dup/#clone, minor coding style cleanups
Laurent Sansonetti authored
120 rb_class_set_mask(k, (flags << RCLASS_MASK_TYPE_SHIFT) | 0);
f82d58b a more efficient class flags mechanism, reduced the number of default…
Laurent Sansonetti authored
121 }
311a41b rewrote class flags to not use the version field anymore, better typi…
Laurent Sansonetti authored
122
123 #define RCLASS_VERSION(m) (rb_class_get_flags((Class)m))
124 #define RCLASS_SET_VERSION(m,f) (rb_class_set_flags((Class)m, (unsigned long)f))
125 #define RCLASS_SET_VERSION_FLAG(m,f) (RCLASS_SET_VERSION((Class)m, (RCLASS_VERSION(m) | f)))
126
127 #define RCLASS_RUBY(m) ((RCLASS_VERSION(m) & RCLASS_IS_RUBY_CLASS) == RCLASS_IS_RUBY_CLASS)
128 #define RCLASS_MODULE(m) ((RCLASS_VERSION(m) & RCLASS_IS_MODULE) == RCLASS_IS_MODULE)
129 #define RCLASS_SINGLETON(m) ((RCLASS_VERSION(m) & RCLASS_IS_SINGLETON) == RCLASS_IS_SINGLETON)
130
131 CFMutableDictionaryRef rb_class_ivar_dict(VALUE);
132 CFMutableDictionaryRef rb_class_ivar_dict_or_create(VALUE);
133 void rb_class_ivar_set_dict(VALUE, CFMutableDictionaryRef);
ee78786 when including a module into a class, copy the module's class variables
Laurent Sansonetti authored
134 void rb_class_merge_ivar_dicts(VALUE orig_class, VALUE dest_class);
311a41b rewrote class flags to not use the version field anymore, better typi…
Laurent Sansonetti authored
135
002ed5d now compile some of the VM primitives into bitcode that will be added…
Laurent Sansonetti authored
136 typedef enum {
137 SCOPE_DEFAULT = 0, // public for everything but Object
138 SCOPE_PUBLIC,
139 SCOPE_PRIVATE,
140 SCOPE_PROTECTED,
141 SCOPE_MODULE_FUNC,
142 } rb_vm_scope_t;
143
144 static inline void
145 rb_vm_check_if_module(VALUE mod)
146 {
147 switch (TYPE(mod)) {
148 case T_CLASS:
149 case T_MODULE:
150 break;
151
152 default:
153 rb_raise(rb_eTypeError, "%s is not a class/module",
154 RSTRING_PTR(rb_inspect(mod)));
155 }
156 }
157
158 static inline void
159 rb_vm_set_current_scope(VALUE mod, rb_vm_scope_t scope)
160 {
161 if (scope == SCOPE_DEFAULT) {
162 scope = mod == rb_cObject ? SCOPE_PRIVATE : SCOPE_PUBLIC;
163 }
164 long v = RCLASS_VERSION(mod);
165 switch (scope) {
166 case SCOPE_PUBLIC:
167 v &= ~RCLASS_SCOPE_PRIVATE;
168 v &= ~RCLASS_SCOPE_PROTECTED;
169 v &= ~RCLASS_SCOPE_MOD_FUNC;
170 break;
171
172 case SCOPE_PRIVATE:
173 v |= RCLASS_SCOPE_PRIVATE;
174 v &= ~RCLASS_SCOPE_PROTECTED;
175 v &= ~RCLASS_SCOPE_MOD_FUNC;
176 break;
177
178 case SCOPE_PROTECTED:
179 v &= ~RCLASS_SCOPE_PRIVATE;
180 v |= RCLASS_SCOPE_PROTECTED;
181 v &= ~RCLASS_SCOPE_MOD_FUNC;
182 break;
183
184 case SCOPE_MODULE_FUNC:
185 v &= ~RCLASS_SCOPE_PRIVATE;
186 v &= ~RCLASS_SCOPE_PROTECTED;
187 v |= RCLASS_SCOPE_MOD_FUNC;
188 break;
189
190 case SCOPE_DEFAULT:
191 abort(); // handled earlier
192 }
193
194 RCLASS_SET_VERSION(mod, v);
195 }
196
311a41b rewrote class flags to not use the version field anymore, better typi…
Laurent Sansonetti authored
197 #if defined(__cplusplus)
198 } // extern "C"
199 #endif
200
201 #endif // __CLASS_H_
Something went wrong with that request. Please try again.