77//
88
99#import " JSBScriptingSupport.h"
10+ #import " JSBScriptingSupport+Private.h"
1011#import " JSBNSObject.h"
1112#import " JSBMessageForwarding.h"
1213#import " JSContext+JavaScriptBridge.h"
@@ -30,50 +31,7 @@ + (void)initialize
3031 [globalContext addScriptingSupport: @" UIKit" ];
3132 [globalContext addScriptingSupport: @" QuartzCore" ];
3233
33- globalContext[@" __JSB_JSBScriptingSupport" ] = [JSBScriptingSupport class ];
34- [globalContext evaluateScript:
35- @" JSB = (function() {\n "
36- @" var namespace = {\n "
37- @" defineClass: function(declaration, instanceMembers, staticMembers) {\n "
38- @" return __JSB_JSBScriptingSupport.defineClass(declaration,"
39- @" instanceMembers == null ? {} : instanceMembers,"
40- @" staticMembers == null ? {} : staticMembers);\n "
41- @" },\n "
42- @" define: function(declaration, instanceMembers, staticMembers) {\n "
43- @" JSB.dump('`define` is deprecated, use `defineClass` instead.');\n "
44- @" return __JSB_JSBScriptingSupport.defineClass(declaration, instanceMembers, staticMembers);\n "
45- @" },\n "
46- @" require: function(name) {\n "
47- @" return __JSB_JSBScriptingSupport.require(name);\n "
48- @" },\n "
49- @" exports: {},\n "
50- @" selector: function(str) {\n "
51- @" return __JSB_JSBScriptingSupport.selectorFromString(str);\n "
52- @" },\n "
53- @" dispatch_async: function(queue, block) {\n "
54- @" return __JSB_JSBScriptingSupport.dispatch_async(queue, block);\n "
55- @" },\n "
56- @" dispatch_get_global_queue: function(priority, flags) {\n "
57- @" return __JSB_JSBScriptingSupport.dispatch_get_global_queue(priority, flags);\n "
58- @" },\n "
59- @" dispatch_get_main_queue: function() {\n "
60- @" return __JSB_JSBScriptingSupport.dispatch_get_main_queue();\n "
61- @" },\n "
62- @" log: function(format) {\n "
63- @" var args = [];\n "
64- @" for (var i = 1; i < arguments.length; i++) {\n "
65- @" args.push(arguments[i]);\n "
66- @" }\n "
67- @" return __JSB_JSBScriptingSupport.log(format, args);\n "
68- @" },\n "
69- @" dump: function(obj) {\n "
70- @" return __JSB_JSBScriptingSupport.dump(obj);\n "
71- @" }\n "
72- @" };\n "
73- @" \n "
74- @" return namespace;\n "
75- @" })();\n "
76- ];
34+ [self setupSupportFunctionsToContext: globalContext];
7735 });
7836}
7937
@@ -109,7 +67,11 @@ + (id)defineClass:(NSString *)declaration instanceMembers:(JSValue *)instanceMem
10967 parentClassName = @" NSObject" ;
11068 }
11169
112- Class cls = objc_allocateClassPair (NSClassFromString (parentClassName), className.UTF8String , 0 );
70+ Class cls = objc_getClass (className.UTF8String );
71+ if (cls) {
72+ objc_disposeClassPair (cls);
73+ }
74+ cls = objc_allocateClassPair (NSClassFromString (parentClassName), className.UTF8String , 0 );
11375 objc_registerClassPair (cls);
11476
11577 Class superClass = class_getSuperclass (cls);
@@ -120,7 +82,7 @@ + (id)defineClass:(NSString *)declaration instanceMembers:(JSValue *)instanceMem
12082 NSString *types;
12183 BOOL result;
12284
123- Class metaClass = objc_getMetaClass (className.UTF8String );
85+ Class metaClass = objc_getClass (className.UTF8String );
12486
12587 types = [NSString stringWithFormat: @" %s%s%s%s " , @encode (NSMethodSignature ), @encode (id ), @encode (SEL ), @encode (SEL )];
12688 result = class_addMethod (cls, @selector (methodSignatureForSelector: ), (IMP )methodSignatureForSelector, types.UTF8String );
@@ -140,10 +102,12 @@ + (id)defineClass:(NSString *)declaration instanceMembers:(JSValue *)instanceMem
140102
141103 class_addProtocol (cls, @protocol (JSBNSObject));
142104
105+ JSContext *context = [self currentContext ];
106+
143107 NSString *key = mangledNameFromClass (cls);
144- globalContext [key] = cls;
145- globalContext [key][JSBInstanceMembersKey] = instanceMembers;
146- globalContext [key][JSBStaticMembersKey] = staticMembers;
108+ context [key] = cls;
109+ context [key][JSBInstanceMembersKey] = instanceMembers;
110+ context [key][JSBStaticMembersKey] = staticMembers;
147111
148112 return cls;
149113}
@@ -160,10 +124,12 @@ + (id)require:(NSString *)name
160124 script = [NSString stringWithContentsOfFile: [name stringByAppendingPathExtension: @" js" ] encoding: NSUTF8StringEncoding error: nil ];
161125 }
162126 if (script) {
163- JSValue *function = globalContext[@" Function" ];
127+ JSContext *context = [self currentContext ];
128+
129+ JSValue *function = context[@" Function" ];
164130 JSValue *value = [function constructWithArguments: @[script]];
165131 [value callWithArguments: nil ];
166- module = globalContext [@" JSB" ][@" exports" ];
132+ module = context [@" JSB" ][@" exports" ];
167133 }
168134
169135 return module;
0 commit comments