forked from chenee/JavaScriptBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSContext+JavaScriptBridge.m
More file actions
132 lines (98 loc) · 4.22 KB
/
JSContext+JavaScriptBridge.m
File metadata and controls
132 lines (98 loc) · 4.22 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
//
// JSContext+JavaScriptBridge.m
// JavaScriptBridge
//
// Created by kishikawa katsumi on 2014/01/04.
// Copyright (c) 2014 kishikawa katsumi. All rights reserved.
//
#import "JSContext+JavaScriptBridge.h"
#import "JSBScriptingSupport+Private.h"
#import <SpriteKit/SKLabelNode.h>
#import <SpriteKit/SKSpriteNode.h>
@implementation JSContext (JavaScriptBridge)
- (void)addScriptingSupport:(NSString *)framework
{
[JSBScriptingSupport setupSupportFunctionsToContext:self];
static NSMapTable *hashTables;
if (!hashTables) {
hashTables = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsWeakMemory valueOptions:NSPointerFunctionsWeakMemory];
}
NSHashTable *frameworks = [hashTables objectForKey:self];
if (!frameworks) {
frameworks = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
[hashTables setObject:frameworks forKey:self];
}
NSString *prefix = @"JSB";
id classObject = NSClassFromString([NSString stringWithFormat:@"%@%@", prefix, framework]);
if (classObject && ![frameworks containsObject:framework]) {
SEL selector = NSSelectorFromString(@"addScriptingSupportToContext:");
NSMethodSignature *methodSignature = [classObject methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
invocation.target = classObject;
invocation.selector = selector;
JSContext *context = self;
[invocation setArgument:&context atIndex:2];
[invocation invoke];
[frameworks addObject:framework];
}
}
-(void)addCreator
{
self[@"create_withSize"] = ^(NSString *className,CGSize size){
Class aClass = NSClassFromString(className);
id anInstance;
anInstance = [[aClass alloc]initWithSize:size];
return anInstance;
};
self[@"create_withFrame"] = ^(NSString *className,CGRect bounds){
Class aClass = NSClassFromString(className);
id anInstance;
anInstance = [[aClass alloc]initWithFrame:bounds];
return anInstance;
};
self[@"create_withStyle"] = ^(NSString *className,NSInteger styleID){
Class aClass = NSClassFromString(className);
id anInstance;
anInstance = [[aClass alloc]initWithStyle:styleID];
return anInstance;
};
self[@"create_withStyleReuseIdentifier"] = ^(NSString *className,NSInteger styleID,NSString* identifier){
Class aClass = NSClassFromString(className);
id anInstance;
anInstance = [[aClass alloc]initWithStyle:styleID reuseIdentifier:identifier];
return anInstance;
};
self[@"create_alert"] = ^(NSString *title,NSString* msg,NSString* cancel,NSString* ok){
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:title
message:msg
delegate:nil
cancelButtonTitle:cancel otherButtonTitles:ok, nil];
return alert;
};
self[@"create_collectionVC"] = ^(){
UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc]init];
UICollectionViewController * obj = [[UICollectionViewController alloc]initWithCollectionViewLayout:flow];
return obj;
};
self[@"create_action"] = ^(NSString*className, id target,NSString* sel){
Class aClass = NSClassFromString(className);
id anInstance;
anInstance = [[aClass alloc]initWithTarget:target action:NSSelectorFromString(sel)];
return anInstance;
};
//=======
self[@"create_SKLabelNode_withFont"] = ^(NSString*className, NSString* fontName){
Class aClass = NSClassFromString(className);
id anInstance;
// anInstance = [SKLabelNode labelNodeWithFontNamed:fontName];
anInstance = [aClass labelNodeWithFontNamed:fontName];
return anInstance;
};
self[@"create_SKSpriteNode_withImg"] = ^(NSString*className, NSString* imgName){
Class aClass = NSClassFromString(className);
id anInstance;
anInstance = [aClass spriteNodeWithImageNamed:imgName];
return anInstance;
};
}
@end