Skip to content

Commit b8914b0

Browse files
Add require/export function.
1 parent de9a1be commit b8914b0

6 files changed

Lines changed: 166 additions & 128 deletions

File tree

Classes/JSBScriptingSupport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ JSExportAs(defineClass,
2222
staticMembers:(JSValue *)staticMembers
2323
);
2424

25+
JSExportAs(require,
26+
+ (id)require:(NSString *)name
27+
);
28+
2529
@optional
2630
JSExportAs(dump,
2731
+ (void)dump:(id)object

Classes/JSBScriptingSupport.m

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,24 @@ + (void)initialize
257257
[globalContext addScriptingSupport:@"UIKit"];
258258
[globalContext addScriptingSupport:@"QuartzCore"];
259259

260-
globalContext[@"JSB"] = [JSBScriptingSupport class];
260+
globalContext[@"__JSB_JSBScriptingSupport"] = [JSBScriptingSupport class];
261261
[globalContext evaluateScript:
262-
@"JSB.Class = (function() {"
263-
@" var namespace = {"
264-
@" define: function(declaration, instanceMembers, staticMembers) {"
265-
@" return JSB.defineClass(declaration, instanceMembers, staticMembers);"
266-
@" }"
267-
@" };"
268-
@""
269-
@" return namespace;"
270-
@"})();"
262+
@"JSB = (function() {\n"
263+
@" var namespace = {\n"
264+
@" define: function(declaration, instanceMembers, staticMembers) {\n"
265+
@" return __JSB_JSBScriptingSupport.defineClass(declaration, instanceMembers, staticMembers);\n"
266+
@" },\n"
267+
@" require: function(name) {\n"
268+
@" return __JSB_JSBScriptingSupport.require(name);\n"
269+
@" },\n"
270+
@" exports: {},\n"
271+
@" dump: function(obj) {\n"
272+
@" return __JSB_JSBScriptingSupport.dump(obj);\n"
273+
@" }\n"
274+
@" };\n"
275+
@"\n"
276+
@" return namespace;\n"
277+
@"})();\n"
271278
];
272279
});
273280
}
@@ -336,6 +343,28 @@ + (id)defineClass:(NSString *)declaration
336343
return cls;
337344
}
338345

346+
+ (id)require:(NSString *)name
347+
{
348+
NSBundle *mainBundle = [NSBundle mainBundle];
349+
NSString *path = [mainBundle pathForResource:name ofType:@"js"];
350+
351+
NSString *script = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
352+
if (!script) {
353+
script = [NSString stringWithContentsOfFile:[name stringByAppendingPathExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
354+
}
355+
if (script) {
356+
NSString *closure = [NSString stringWithFormat:
357+
@"(function() {\n"
358+
@" %@\n"
359+
@"})();\n", script];
360+
[globalContext evaluateScript:closure];
361+
JSValue *module = globalContext[@"JSB"][@"exports"];
362+
return module;
363+
}
364+
365+
return nil;
366+
}
367+
339368
#pragma mark - for debug
340369

341370
+ (void)dump:(id)object

Examples/UICatalog/UICatalog.xcodeproj/project.pbxproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
149AE1091876E8BC00018A8C /* buttonsViewController.js in Resources */ = {isa = PBXBuildFile; fileRef = 149AE1081876E8BC00018A8C /* buttonsViewController.js */; };
11+
149AE10B1876F32D00018A8C /* mainViewController.js in Resources */ = {isa = PBXBuildFile; fileRef = 149AE10A1876F32D00018A8C /* mainViewController.js */; };
1112
149C53A51875C5870003EE16 /* JSBScriptingSupport.m in Sources */ = {isa = PBXBuildFile; fileRef = 149C53A21875C5870003EE16 /* JSBScriptingSupport.m */; };
1213
149C53A61875C5870003EE16 /* JSContext+Helper.m in Sources */ = {isa = PBXBuildFile; fileRef = 149C53A41875C5870003EE16 /* JSContext+Helper.m */; };
1314
149C56691875C5BB0003EE16 /* JSBAVFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = 149C53DC1875C5B90003EE16 /* JSBAVFoundation.m */; };
@@ -53,6 +54,7 @@
5354

5455
/* Begin PBXFileReference section */
5556
149AE1081876E8BC00018A8C /* buttonsViewController.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = buttonsViewController.js; sourceTree = "<group>"; };
57+
149AE10A1876F32D00018A8C /* mainViewController.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = mainViewController.js; sourceTree = "<group>"; };
5658
149C539F1875C5870003EE16 /* JavaScriptBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JavaScriptBridge.h; path = ../../../Classes/JavaScriptBridge.h; sourceTree = "<group>"; };
5759
149C53A01875C5870003EE16 /* JSBNSObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JSBNSObject.h; path = ../../../Classes/JSBNSObject.h; sourceTree = "<group>"; };
5860
149C53A11875C5870003EE16 /* JSBScriptingSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JSBScriptingSupport.h; path = ../../../Classes/JSBScriptingSupport.h; sourceTree = "<group>"; };
@@ -1745,6 +1747,7 @@
17451747
14B5CD2E1875C3740019A135 /* AppDelegate.h */,
17461748
14B5CD2F1875C3740019A135 /* AppDelegate.m */,
17471749
14B5CD4E1875C3FF0019A135 /* main.js */,
1750+
149AE10A1876F32D00018A8C /* mainViewController.js */,
17481751
149AE1081876E8BC00018A8C /* buttonsViewController.js */,
17491752
14B5CD311875C3740019A135 /* Images.xcassets */,
17501753
14B5CD261875C3740019A135 /* Supporting Files */,
@@ -1815,8 +1818,9 @@
18151818
isa = PBXResourcesBuildPhase;
18161819
buildActionMask = 2147483647;
18171820
files = (
1818-
149AE1091876E8BC00018A8C /* buttonsViewController.js in Resources */,
18191821
14B5CD4F1875C3FF0019A135 /* main.js in Resources */,
1822+
149AE10B1876F32D00018A8C /* mainViewController.js in Resources */,
1823+
149AE1091876E8BC00018A8C /* buttonsViewController.js in Resources */,
18201824
14B5CD2A1875C3740019A135 /* InfoPlist.strings in Resources */,
18211825
14B5CD321875C3740019A135 /* Images.xcassets in Resources */,
18221826
);
Lines changed: 84 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,87 @@
1-
(function() {
2-
var ButtonsViewController = JSB.Class.define('ButtonsViewController : UITableViewController', {
3-
viewDidLoad: function() {
4-
self.navigationItem.title = 'Buttons';
5-
6-
this.dataSourceArray = [];
7-
8-
var buttonBackground = UIImage.imageNamed('whiteButton');
9-
var buttonBackgroundPressed = UIImage.imageNamed('blueButton');
10-
11-
var frame = {x: 0, y: 5, width: 106, height: 40};
12-
13-
var button = UIButton.alloc().initWithFrame(frame);
14-
15-
button.contentVerticalAlignment = 0;
16-
button.contentHorizontalAlignment = 0;
17-
button.setTitleForState('Gray', 0);
18-
button.setTitleColorForState(UIColor.blackColor(), 0);
19-
20-
var newImage = buttonBackground.stretchableImageWithLeftCapWidthTopCapHeight(12, 0);
21-
button.setBackgroundImageForState(newImage, 0);
22-
23-
var newPressedImage = buttonBackgroundPressed.stretchableImageWithLeftCapWidthTopCapHeight(12, 0);
24-
button.setBackgroundImageForState(newPressedImage, 1 << 0);
25-
26-
button.backgroundColor = UIColor.clearColor();
27-
button.tag = 1;
28-
29-
dataSourceArray.push({
30-
sectionTitleKey: 'UIButton',
31-
labelKey: 'Background Image',
32-
sourceKey: 'ButtonsViewController.m:\r(UIButton *)grayButton',
33-
viewKey: button
34-
});
35-
},
36-
numberOfSectionsInTableView: function(tableView) {
37-
return dataSourceArray.length;
38-
},
39-
tableViewNumberOfRowsInSection: function(tableView, section) {
40-
return 2;
41-
},
42-
tableViewTitleForHeaderInSection: function(tableView, section) {
43-
return dataSourceArray[section]['sectionTitleKey'];
44-
},
45-
tableViewHeightForRowAtIndexPath: function(tableView, indexPath) {
46-
return (indexPath.row == 0) ? 50 : 38;
47-
},
48-
tableViewCellForRowAtIndexPath: function(tableView, indexPath) {
49-
if (indexPath.row == 0) {
50-
var cell = UITableViewCell.alloc().initWithStyleReuseIdentifier(0, 'DisplayCellID');
51-
cell.selectionStyle = 0;
52-
53-
var viewToRemove = null;
54-
viewToRemove = cell.contentView.viewWithTag(1);
55-
if (viewToRemove) {
56-
viewToRemove.removeFromSuperview();
57-
}
58-
59-
cell.textLabel.text = dataSourceArray[indexPath.section]['labelKey'];
60-
var button = dataSourceArray[indexPath.section]['viewKey'];
61-
62-
var newFrame = button.frame;
63-
newFrame.x = cell.contentView.frame.width - newFrame.width - 10;
64-
button.frame = newFrame;
65-
66-
button.autoresizingMask = 1 << 0;
67-
68-
cell.contentView.addSubview(button);
69-
70-
return cell;
71-
} else {
72-
var cell = UITableViewCell.alloc().initWithStyleReuseIdentifier(0, 'SourceCellID');
73-
cell.selectionStyle = 0;
74-
75-
cell.textLabel.opaque = false;
76-
cell.textLabel.textAlignment = 1;
77-
cell.textLabel.textColor = UIColor.grayColor();
78-
cell.textLabel.numberOfLines = 2;
79-
cell.textLabel.highlightedTextColor = UIColor.blackColor();
80-
cell.textLabel.font = UIFont.systemFontOfSize(12);
81-
cell.textLabel.text = menuList[indexPath.section]['sourceKey'];
82-
83-
return cell;
1+
var ButtonsViewController = JSB.define('ButtonsViewController : UITableViewController', {
2+
viewDidLoad: function() {
3+
self.navigationItem.title = 'Buttons';
4+
5+
this.dataSourceArray = [];
6+
7+
var buttonBackground = UIImage.imageNamed('whiteButton');
8+
var buttonBackgroundPressed = UIImage.imageNamed('blueButton');
9+
10+
var frame = {x: 0, y: 5, width: 106, height: 40};
11+
12+
var button = UIButton.alloc().initWithFrame(frame);
13+
14+
button.contentVerticalAlignment = 0;
15+
button.contentHorizontalAlignment = 0;
16+
button.setTitleForState('Gray', 0);
17+
button.setTitleColorForState(UIColor.blackColor(), 0);
18+
19+
var newImage = buttonBackground.stretchableImageWithLeftCapWidthTopCapHeight(12, 0);
20+
button.setBackgroundImageForState(newImage, 0);
21+
22+
var newPressedImage = buttonBackgroundPressed.stretchableImageWithLeftCapWidthTopCapHeight(12, 0);
23+
button.setBackgroundImageForState(newPressedImage, 1 << 0);
24+
25+
button.backgroundColor = UIColor.clearColor();
26+
button.tag = 1;
27+
28+
dataSourceArray.push({
29+
sectionTitleKey: 'UIButton',
30+
labelKey: 'Background Image',
31+
sourceKey: 'ButtonsViewController.m:\r(UIButton *)grayButton',
32+
viewKey: button
33+
});
34+
},
35+
numberOfSectionsInTableView: function(tableView) {
36+
return dataSourceArray.length;
37+
},
38+
tableViewNumberOfRowsInSection: function(tableView, section) {
39+
return 2;
40+
},
41+
tableViewTitleForHeaderInSection: function(tableView, section) {
42+
return dataSourceArray[section]['sectionTitleKey'];
43+
},
44+
tableViewHeightForRowAtIndexPath: function(tableView, indexPath) {
45+
return (indexPath.row == 0) ? 50 : 38;
46+
},
47+
tableViewCellForRowAtIndexPath: function(tableView, indexPath) {
48+
if (indexPath.row == 0) {
49+
var cell = UITableViewCell.alloc().initWithStyleReuseIdentifier(0, 'DisplayCellID');
50+
cell.selectionStyle = 0;
51+
52+
var viewToRemove = null;
53+
viewToRemove = cell.contentView.viewWithTag(1);
54+
if (viewToRemove) {
55+
viewToRemove.removeFromSuperview();
8456
}
57+
58+
cell.textLabel.text = dataSourceArray[indexPath.section]['labelKey'];
59+
var button = dataSourceArray[indexPath.section]['viewKey'];
60+
61+
var newFrame = button.frame;
62+
newFrame.x = cell.contentView.frame.width - newFrame.width - 10;
63+
button.frame = newFrame;
64+
65+
button.autoresizingMask = 1 << 0;
66+
67+
cell.contentView.addSubview(button);
68+
69+
return cell;
70+
} else {
71+
var cell = UITableViewCell.alloc().initWithStyleReuseIdentifier(0, 'SourceCellID');
72+
cell.selectionStyle = 0;
73+
74+
cell.textLabel.opaque = false;
75+
cell.textLabel.textAlignment = 1;
76+
cell.textLabel.textColor = UIColor.grayColor();
77+
cell.textLabel.numberOfLines = 2;
78+
cell.textLabel.highlightedTextColor = UIColor.blackColor();
79+
cell.textLabel.font = UIFont.systemFontOfSize(12);
80+
cell.textLabel.text = menuList[indexPath.section]['sourceKey'];
81+
82+
return cell;
8583
}
86-
});
87-
})();
84+
}
85+
});
8886

87+
JSB.exports = ButtonsViewController;

Examples/UICatalog/UICatalog/main.js

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,4 @@
1-
2-
JSB.Module.require('buttonsViewController');
3-
4-
var MainViewController = JSB.Class.define('MainViewController : UITableViewController', {
5-
viewDidLoad: function() {
6-
self.navigationItem.title = 'UICatalog';
7-
8-
this.menuList = [];
9-
10-
var viewController = ButtonsViewController.new();
11-
menuList.push({
12-
title: 'Buttons',
13-
explanation: 'Various uses of UIButton',
14-
viewController: viewController
15-
});
16-
},
17-
tableViewNumberOfRowsInSection: function(tableView, section) {
18-
return menuList.length;
19-
},
20-
tableViewCellForRowAtIndexPath: function(tableView, indexPath) {
21-
var cell = UITableViewCell.alloc().initWithStyleReuseIdentifier(3, 'Cell');
22-
cell.accessoryType = 1;
23-
cell.textLabel.text = menuList[indexPath.row]['title'];
24-
cell.detailTextLabel.text = menuList[indexPath.row]['explanation'];
25-
26-
return cell;
27-
},
28-
tableViewDidSelectRowAtIndexPath: function(tableView, indexPath) {
29-
var targetViewController = menuList[indexPath.row]['viewController'];
30-
self.navigationController.pushViewControllerAnimated(targetViewController, true);
31-
}
32-
});
1+
var MainViewController = JSB.require('mainViewController');
332

343
var window = UIWindow.alloc().initWithFrame(UIScreen.mainScreen().bounds);
354
window.backgroundColor = UIColor.whiteColor();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var ButtonsViewController = JSB.require('buttonsViewController');
2+
3+
var MainViewController = JSB.define('MainViewController : UITableViewController', {
4+
viewDidLoad: function() {
5+
this.menuList = [];
6+
7+
self.navigationItem.title = 'UICatalog';
8+
9+
var viewController = ButtonsViewController.new();
10+
menuList.push({
11+
title: 'Buttons',
12+
explanation: 'Various uses of UIButton',
13+
viewController: viewController
14+
});
15+
},
16+
tableViewNumberOfRowsInSection: function(tableView, section) {
17+
return menuList.length;
18+
},
19+
tableViewCellForRowAtIndexPath: function(tableView, indexPath) {
20+
var cell = UITableViewCell.alloc().initWithStyleReuseIdentifier(3, 'Cell');
21+
cell.accessoryType = 1;
22+
cell.textLabel.text = menuList[indexPath.row]['title'];
23+
cell.detailTextLabel.text = menuList[indexPath.row]['explanation'];
24+
25+
return cell;
26+
},
27+
tableViewDidSelectRowAtIndexPath: function(tableView, indexPath) {
28+
var targetViewController = menuList[indexPath.row]['viewController'];
29+
self.navigationController.pushViewControllerAnimated(targetViewController, true);
30+
}
31+
});
32+
33+
JSB.exports = MainViewController;

0 commit comments

Comments
 (0)