Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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