forked from kishikawakatsumi/JavaScriptBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSBScriptingSupport.m
More file actions
176 lines (137 loc) · 5.85 KB
/
JSBScriptingSupport.m
File metadata and controls
176 lines (137 loc) · 5.85 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// JSBScriptingSupport.m
// JavaScriptBridge
//
// Created by kishikawa katsumi on 2013/12/31.
// Copyright (c) 2013 kishikawa katsumi. All rights reserved.
//
#import "JSBScriptingSupport.h"
#import "JSBScriptingSupport+Private.h"
#import "JSBNSObject.h"
#import "JSBMessageForwarding.h"
#import "JSContext+JavaScriptBridge.h"
@import ObjectiveC;
@implementation JSBScriptingSupport
+ (JSContext *)globalContext
{
static JSContext *globalContext;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
globalContext = [[JSContext alloc] init];
globalContext.exceptionHandler = ^(JSContext *context, JSValue *value) {
NSLog(@"%@", value);
};
[globalContext addScriptingSupport:@"Foundation"];
[globalContext addScriptingSupport:@"UIKit"];
[globalContext addScriptingSupport:@"QuartzCore"];
});
return globalContext;
}
#pragma mark -
+ (id)defineClass:(NSString *)declaration instanceMembers:(JSValue *)instanceMembers staticMembers:(JSValue *)staticMembers
{
NSScanner *scanner = [NSScanner scannerWithString:declaration];
NSString *className = nil;
NSString *parentClassName = nil;
NSString *protocols = nil;
[scanner scanUpToString:@":" intoString:&className];
if (!scanner.isAtEnd) {
scanner.scanLocation = scanner.scanLocation + 1;
}
[scanner scanUpToString:@"<" intoString:&parentClassName];
if (!scanner.isAtEnd) {
scanner.scanLocation = scanner.scanLocation + 1;
}
[scanner scanUpToString:@">" intoString:&protocols];
className = [className stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
className = [NSString stringWithFormat:@"%@$%@", className, [[[NSUUID UUID] UUIDString] stringByReplacingOccurrencesOfString:@"-" withString:@""]];
parentClassName = [parentClassName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (!parentClassName) {
parentClassName = @"NSObject";
}
Class cls = objc_getClass(className.UTF8String);
if (cls) {
objc_disposeClassPair(cls);
}
cls = objc_allocateClassPair(NSClassFromString(parentClassName), className.UTF8String, 0);
objc_registerClassPair(cls);
Class superClass = class_getSuperclass(cls);
if (superClass) {
setupForwardingImplementations(cls, superClass, instanceMembers, staticMembers);
}
NSString *types;
BOOL result;
Class metaClass = object_getClass(cls);
types = [NSString stringWithFormat:@"%s%s%s%s", @encode(NSMethodSignature), @encode(id), @encode(SEL), @encode(SEL)];
result = class_addMethod(cls, @selector(methodSignatureForSelector:), (IMP)methodSignatureForSelector, types.UTF8String);
result = class_addMethod(metaClass, @selector(methodSignatureForSelector:), (IMP)methodSignatureForSelector, types.UTF8String);
types = [NSString stringWithFormat:@"%s%s%s%s", @encode(void), @encode(id), @encode(SEL), @encode(NSInvocation)];
result = class_addMethod(cls, @selector(forwardInvocation:), (IMP)forwardInvocation, types.UTF8String);
result = class_addMethod(metaClass, @selector(forwardInvocation:), (IMP)forwardInvocation, types.UTF8String);
types = [NSString stringWithFormat:@"%s%s%s%s", @encode(BOOL), @encode(id), @encode(SEL), @encode(SEL)];
result = class_addMethod(cls, @selector(respondsToSelector:), (IMP)respondsToSelector, types.UTF8String);
result = class_addMethod(metaClass, @selector(respondsToSelector:), (IMP)respondsToSelector, types.UTF8String);
for (NSString *protocol in [protocols componentsSeparatedByString:@","]) {
class_addProtocol(cls, NSProtocolFromString([protocol stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]));
}
class_addProtocol(cls, @protocol(JSBNSObject));
JSContext *context = [self currentContext];
NSString *key = mangledNameFromClass(cls);
context[key] = cls;
context[key][JSBInstanceMembersKey] = instanceMembers;
context[key][JSBStaticMembersKey] = staticMembers;
return cls;
}
+ (id)require:(NSString *)name
{
JSValue *module = nil;
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *path = [mainBundle pathForResource:name ofType:@"js"];
NSString *script = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
if (!script) {
script = [NSString stringWithContentsOfFile:[name stringByAppendingPathExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
}
if (script) {
JSContext *context = [self currentContext];
JSValue *function = context[@"Function"];
JSValue *value = [function constructWithArguments:@[script]];
[value callWithArguments:nil];
module = context[@"JSB"][@"exports"];
}
return module;
}
#pragma mark -
+ (void)dispatch_async:(id)queue block:(JSValue *)function
{
JSContext *context = [JSContext currentContext];
id currentSelf = context[@"self"];
dispatch_block_t block = NULL;
if (!function.isUndefined) {
block = ^() {
dispatchFunction(currentSelf, function, nil);
};
}
dispatch_async(queue, block);
}
+ (id)dispatch_get_global_queue:(dispatch_queue_priority_t)priority
flags:(unsigned long)flags
{
return dispatch_get_global_queue(priority, flags);
}
+ (id)dispatch_get_main_queue
{
return dispatch_get_main_queue();
}
#pragma mark - for debug
+ (void)log:(NSString *)format arguments:(NSArray *)arguments
{
__unsafe_unretained id *argList = createVariableArgumentListsFromArray(arguments);
NSString *result = [[NSString alloc] initWithFormat:format, *argList];
free(argList);
NSLog(@"%@", result);
}
+ (void)dump:(id)object
{
NSLog(@"%@", object);
}
@end