Skip to content

Commit 0fb6d21

Browse files
Update method forwarding.
1 parent 714392d commit 0fb6d21

File tree

4 files changed

+67
-35
lines changed

4 files changed

+67
-35
lines changed

Classes/JSBScriptingSupport.m

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,34 @@ static void setReturnValue(JSValue *value, NSInvocation *invocation)
169169
}
170170
}
171171

172+
static void setupForwardingImplementations(Class cls, JSValue *functions)
173+
{
174+
unsigned int count = 0;
175+
Method *methods = class_copyMethodList(cls, &count);
176+
for (unsigned int i = 0; i < count; i++) {
177+
Method m = methods[i];
178+
struct objc_method_description *description = method_getDescription(m);
179+
180+
NSString *propertyName = propertyNameFromSelector(description->name);
181+
JSValue *function = functions[propertyName];
182+
if (!function.isUndefined) {
183+
method_setImplementation(m, _objc_msgForward);
184+
}
185+
}
186+
if (methods) {
187+
free(methods);
188+
}
189+
190+
Class superClass = class_getSuperclass(cls);
191+
if (superClass) {
192+
setupForwardingImplementations(superClass, functions);
193+
}
194+
}
195+
172196
static void forwardInvocation(id self, SEL _cmd, NSInvocation *invocation)
173197
{
174-
JSValue *value = globalContext[NSStringFromClass([self class])][propertyNameFromSelector(invocation.selector)];
198+
NSString *propertyName = propertyNameFromSelector(invocation.selector);
199+
JSValue *value = globalContext[NSStringFromClass([self class])][@"instanceMembers"][propertyName];
175200

176201
NSArray *arguments = extractArguments(invocation);
177202
JSValue *returnValue = [value callWithArguments:arguments];
@@ -195,7 +220,9 @@ static void forwardInvocation(id self, SEL _cmd, NSInvocation *invocation)
195220

196221
static BOOL respondsToSelector(id self, SEL _cmd, SEL selector)
197222
{
198-
JSValue *value = globalContext[NSStringFromClass([self class])][propertyNameFromSelector(selector)];
223+
NSString *propertyName = propertyNameFromSelector(selector);
224+
JSValue *value = globalContext[NSStringFromClass([self class])][@"instanceMembers"][propertyName];
225+
199226
return !value.isUndefined;
200227
}
201228

@@ -210,18 +237,18 @@ + (void)initialize
210237

211238
};
212239

240+
[globalContext addScriptingSupport:@"Foundation"];
241+
[globalContext addScriptingSupport:@"UIKit"];
242+
[globalContext addScriptingSupport:@"QuartzCore"];
243+
244+
globalContext[@"JSB"] = [JSBScriptingSupport class];
213245
[globalContext evaluateScript:
214246
@"JSB.Class = {};"
215247
@""
216248
@"JSB.Class.define = function(declaration, instanceMembers, staticMembers) {"
217249
@" JSB.defineClass(declaration, instanceMembers, staticMembers);"
218250
@"};"
219251
];
220-
globalContext[@"JSB"] = [JSBScriptingSupport class];
221-
222-
[globalContext addScriptingSupport:@"Foundation"];
223-
[globalContext addScriptingSupport:@"UIKit"];
224-
[globalContext addScriptingSupport:@"QuartzCore"];
225252
});
226253
}
227254

@@ -258,6 +285,12 @@ + (void)defineClass:(NSString *)declaration
258285
}
259286

260287
Class cls = objc_allocateClassPair(NSClassFromString(parentClassName), className.UTF8String, 0);
288+
objc_registerClassPair(cls);
289+
290+
Class superClass = class_getSuperclass(cls);
291+
if (superClass) {
292+
setupForwardingImplementations(superClass, instanceMembers);
293+
}
261294

262295
NSString *types;
263296
BOOL result;
@@ -276,15 +309,12 @@ + (void)defineClass:(NSString *)declaration
276309
}
277310

278311
class_addProtocol(cls, @protocol(JSBNSObject));
312+
#if DEBUG
279313
class_addProtocol(cls, @protocol(JSBScriptingSupport));
280-
objc_registerClassPair(cls);
314+
#endif
281315

282316
globalContext[className] = cls;
283-
284-
for (NSString *key in instanceMembers.toDictionary.allKeys) {
285-
JSValue *instanceMember = instanceMembers[key];
286-
globalContext[className][key] = instanceMember;
287-
}
317+
globalContext[className][@"instanceMembers"] = instanceMembers;
288318
}
289319

290320
+ (void)dump:(id)object

Examples/UICatalog/UICatalog/AppDelegate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010

1111
@interface AppDelegate : UIResponder <UIApplicationDelegate>
1212

13+
@property (nonatomic) UIWindow *window;
14+
1315
@end
Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
// var window = UIWindow.alloc().initWithFrame(UIScreen.mainScreen().bounds);
2-
// window.backgroundColor = UIColor.whiteColor();
3-
4-
// var navigationController = UINavigationController.new();
5-
// window.rootViewController = navigationController;
6-
7-
// window.makeKeyAndVisible();
8-
9-
JSB.Class.define('JSBTableViewDataSource : NSObject <UITableViewDataSource, UITableViewDelegate>',
1+
JSB.Class.define('MainViewController : UITableViewController',
102
{
113
numberOfSectionsInTableView: function (tableView) {
124
return 2;
@@ -23,17 +15,14 @@ JSB.Class.define('JSBTableViewDataSource : NSObject <UITableViewDataSource, UITa
2315

2416
});
2517

26-
var dataSource = JSBTableViewDataSource.new();
27-
28-
var window = UIWindow.new();
29-
window.frame = UIScreen.mainScreen().bounds;
18+
var window = UIWindow.alloc().initWithFrame(UIScreen.mainScreen().bounds);
3019
window.backgroundColor = UIColor.whiteColor();
3120

32-
var tableView = UITableView.new();
33-
tableView.frame = window.bounds;
34-
tableView.rowHeight = 44;
35-
tableView.dataSource = dataSource;
21+
var navigationController = UINavigationController.new();
22+
23+
var mainViewController = MainViewController.new();
24+
navigationController.viewControllers = [mainViewController];
3625

37-
window.addSubview(tableView);
26+
window.rootViewController = navigationController;
3827

3928
window.makeKeyAndVisible();

Project/JavaScriptBridge/JavaScriptBridge/JSBAppDelegate.m

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,26 @@
77
//
88

99
#import "JSBAppDelegate.h"
10+
#import "JavaScriptBridge.h"
1011

1112
@implementation JSBAppDelegate
1213

1314
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1415
{
15-
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
16-
17-
self.window.backgroundColor = [UIColor whiteColor];
18-
[self.window makeKeyAndVisible];
16+
JSContext *context = [JSBScriptingSupport globalContext];
17+
[context evaluateScript:
18+
@"var window = UIWindow.alloc().initWithFrame(UIScreen.mainScreen().bounds);"
19+
@"window.backgroundColor = UIColor.whiteColor();"
20+
@""
21+
@"var navigationController = UINavigationController.new();"
22+
@""
23+
@"var tableViewController = UITableViewController.new();"
24+
@"navigationController.viewControllers = [tableViewController];"
25+
@""
26+
@"window.rootViewController = navigationController;"
27+
@""
28+
@"window.makeKeyAndVisible();"
29+
];
1930

2031
return YES;
2132
}

0 commit comments

Comments
 (0)