-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathJSObjection.m
More file actions
112 lines (93 loc) · 3.48 KB
/
JSObjection.m
File metadata and controls
112 lines (93 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#import "JSObjection.h"
#import <pthread.h>
#import "JSObjectionInjectorEntry.h"
#import "JSObjectionRuntimePropertyReflector.h"
static NSMutableDictionary *gObjectionContext;
static pthread_mutex_t gObjectionMutex;
static JSObjectionInjector *gGlobalInjector;
static id<JSObjectionPropertyReflector> gPropertyReflector;
@implementation JSObjection
+ (JSObjectionInjector *)createInjector:(JSObjectionModule *)module {
pthread_mutex_lock(&gObjectionMutex);
@try {
return [[JSObjectionInjector alloc] initWithContext:gObjectionContext andModule:module];
}
@finally {
pthread_mutex_unlock(&gObjectionMutex);
}
return nil;
}
+ (JSObjectionInjector *)createInjectorWithModulesArray:(NSArray *)modules {
pthread_mutex_lock(&gObjectionMutex);
@try {
return [[JSObjectionInjector alloc] initWithContext:gObjectionContext andModules:modules];
}
@finally {
pthread_mutex_unlock(&gObjectionMutex);
}
return nil;
}
+ (JSObjectionInjector *)createInjectorWithModules:(JSObjectionModule *)first, ... {
va_list va_modules;
NSMutableArray *modules = [NSMutableArray arrayWithObject:first];
va_start(va_modules, first);
JSObjectionModule *module;
while ((module = va_arg( va_modules, JSObjectionModule *) )) {
[modules addObject:module];
}
va_end(va_modules);
return [self createInjectorWithModulesArray:modules];
}
+ (JSObjectionInjector *)createInjector {
pthread_mutex_lock(&gObjectionMutex);
@try {
return [[JSObjectionInjector alloc] initWithContext:gObjectionContext];
}
@finally {
pthread_mutex_unlock(&gObjectionMutex);
}
return nil;
}
+ (void)initialize {
if (self == [JSObjection class]) {
gObjectionContext = [[NSMutableDictionary alloc] init];
gPropertyReflector = [[JSObjectionRuntimePropertyReflector alloc] init];
pthread_mutexattr_t mutexattr;
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&gObjectionMutex, &mutexattr);
pthread_mutexattr_destroy(&mutexattr);
}
}
+ (void)registerClass:(Class)theClass scope:(JSObjectionScope)scope {
pthread_mutex_lock(&gObjectionMutex);
if (scope != JSObjectionScopeSingleton && scope != JSObjectionScopeNormal) {
@throw [NSException exceptionWithName:@"JSObjectionInjectorException" reason:@"Invalid Instantiation Rule" userInfo:nil];
}
if (theClass && [gObjectionContext objectForKey:NSStringFromClass(theClass)] == nil) {
[gObjectionContext setObject:[JSObjectionInjectorEntry entryWithClass:theClass scope:scope] forKey:NSStringFromClass(theClass)];
}
pthread_mutex_unlock(&gObjectionMutex);
}
+ (void)reset {
pthread_mutex_lock(&gObjectionMutex);
[gObjectionContext removeAllObjects];
pthread_mutex_unlock(&gObjectionMutex);
}
+ (void)setDefaultInjector:(JSObjectionInjector *)anInjector {
if (gGlobalInjector != anInjector) {
gGlobalInjector = anInjector;
}
}
+ (JSObjectionInjector *)defaultInjector {
return gGlobalInjector;
}
+ (JSObjectionPropertyInfo)propertyForClass:(Class)theClass andProperty:(NSString *)propertyName {
return [gPropertyReflector propertyForClass:theClass andProperty: propertyName];
}
+ (void)setPropertyReflector:(id<JSObjectionPropertyReflector>)reflector {
if(gPropertyReflector != reflector) {
gPropertyReflector = reflector;
}
}
@end