@@ -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+
172196static 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
196221static 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
0 commit comments