Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix instance self.super() 's call loop issue #173 #236

Merged
merged 2 commits into from
Feb 2, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix instance self.super() 's call loop issue #173
  • Loading branch information
Awhisper committed Feb 1, 2016
commit 6fd8c4009953dca624a589d9c3ab39028eecb30a
63 changes: 48 additions & 15 deletions JSPatch/JPEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -423,21 +423,9 @@ static void addMethodToProtocol(Protocol* protocol, NSString *selectorName, NSSt
NSString *superClassName;
NSString *protocolNames;

NSScanner *scanner = [NSScanner scannerWithString:classDeclaration];
[scanner scanUpToString:@":" intoString:&className];
if (!scanner.isAtEnd) {
scanner.scanLocation = scanner.scanLocation + 1;
[scanner scanUpToString:@"<" intoString:&superClassName];
if (!scanner.isAtEnd) {
scanner.scanLocation = scanner.scanLocation + 1;
[scanner scanUpToString:@">" intoString:&protocolNames];
}
}
NSArray *protocols = [protocolNames componentsSeparatedByString:@","];
convertJPDeclarationString(classDeclaration, &className, &superClassName, &protocolNames);

if (!superClassName) superClassName = @"NSObject";
className = trim(className);
superClassName = trim(superClassName);
NSArray *protocols = [protocolNames componentsSeparatedByString:@","];

Class cls = NSClassFromString(className);
if (!cls) {
Expand Down Expand Up @@ -845,6 +833,8 @@ static void overrideMethod(Class cls, NSString *selectorName, JSValue *function,

static id callSelector(NSString *className, NSString *selectorName, JSValue *arguments, JSValue *instance, BOOL isSuper)
{
NSString *clsDeclaration = [[instance valueForProperty:@"__clsDeclaration"] toString];

if (instance) {
instance = formatJSToOC(instance);
if (!instance || instance == _nilObj) return @{@"__isNil": @(YES)};
Expand All @@ -864,7 +854,17 @@ static id callSelector(NSString *className, NSString *selectorName, JSValue *arg
NSString *superSelectorName = [NSString stringWithFormat:@"SUPER_%@", selectorName];
SEL superSelector = NSSelectorFromString(superSelectorName);

Class superCls = [cls superclass];
Class superCls;
if (clsDeclaration && clsDeclaration.length > 0) {
NSString *defineClsName;
convertJPDeclarationString(clsDeclaration, &defineClsName, nil, nil);
Class defineClass = NSClassFromString(defineClsName);
superCls = defineClass?[defineClass superclass]:[cls superclass];
}else
{
superCls = [cls superclass];
}

Method superMethod = class_getInstanceMethod(superCls, selector);
IMP superIMP = method_getImplementation(superMethod);

Expand Down Expand Up @@ -1337,6 +1337,39 @@ static BOOL blockTypeIsObject(NSString *typeString)
return [selectorName stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
}

static void convertJPDeclarationString(NSString *declaration, NSString **retClsName, NSString **retSuperClsName, NSString **retProtocolsNames){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接返回一个NSDictionary用起来更方便些吧?不用事先定义变量再传引用进来


NSScanner *scanner = [NSScanner scannerWithString:declaration];

NSString *className;
NSString *superClassName;
NSString *protocolNames;
[scanner scanUpToString:@":" intoString:&className];
if (!scanner.isAtEnd) {
scanner.scanLocation = scanner.scanLocation + 1;
[scanner scanUpToString:@"<" intoString:&superClassName];
if (!scanner.isAtEnd) {
scanner.scanLocation = scanner.scanLocation + 1;
[scanner scanUpToString:@">" intoString:&protocolNames];
}
}

if (!superClassName) superClassName = @"NSObject";
className = trim(className);
superClassName = trim(superClassName);

if (retClsName) {
*retClsName = className;
}
if (retSuperClsName) {
*retSuperClsName = superClassName;
}
if (retProtocolsNames) {
*retProtocolsNames = protocolNames;
}
return;
}


#pragma mark - Object format

Expand Down
9 changes: 6 additions & 3 deletions JSPatch/JSPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ var global = this
return lastRequire
}

var _formatDefineMethods = function(methods, newMethods) {
var _formatDefineMethods = function(methods, newMethods, declaration) {
for (var methodName in methods) {
(function(){
var originMethod = methods[methodName]
Expand All @@ -125,6 +125,9 @@ var global = this
var ret;
try {
global.self = args[0]
if (global.self.__obj) {
global.self.__obj.__clsDeclaration = declaration
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__clsDeclaration 应该赋在 self 上而不是 self.__obj

}
args.splice(0,1)
ret = originMethod.apply(originMethod, args)
global.self = lastSelf
Expand All @@ -139,8 +142,8 @@ var global = this

global.defineClass = function(declaration, instMethods, clsMethods) {
var newInstMethods = {}, newClsMethods = {}
_formatDefineMethods(instMethods, newInstMethods)
_formatDefineMethods(clsMethods, newClsMethods)
_formatDefineMethods(instMethods, newInstMethods,declaration)
_formatDefineMethods(clsMethods, newClsMethods,declaration)

var ret = _OC_defineClass(declaration, newInstMethods, newClsMethods)

Expand Down
10 changes: 10 additions & 0 deletions JSPatchDemo/JSPatchDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
36F0DC791BF6D5D20090EA4A /* JPLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 36F0DC781BF6D5D20090EA4A /* JPLoader.m */; };
36F0DCC61BFAF41C0090EA4A /* libz.1.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 36F0DCC51BFAF41C0090EA4A /* libz.1.dylib */; };
3F6C38EB1B4A37D600F88662 /* JPMemory.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F6C38EA1B4A37D600F88662 /* JPMemory.m */; };
6C6CAC741C5F0BCA00444348 /* superTest.js in Resources */ = {isa = PBXBuildFile; fileRef = 6C6CAC721C5F0BCA00444348 /* superTest.js */; };
6C6CAC751C5F0BCA00444348 /* SuperTestObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C6CAC731C5F0BCA00444348 /* SuperTestObject.m */; };
6C72B7961C352BA80086C98D /* newProtocolTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C72B7951C352BA80086C98D /* newProtocolTest.m */; };
6C9C0EE41C25A7C700FCAAC5 /* newProtocolTest.js in Resources */ = {isa = PBXBuildFile; fileRef = 6C9C0EE31C25A7C700FCAAC5 /* newProtocolTest.js */; };
6C9C0EE51C25A7DA00FCAAC5 /* newProtocolTest.js in Resources */ = {isa = PBXBuildFile; fileRef = 6C9C0EE31C25A7C700FCAAC5 /* newProtocolTest.js */; };
Expand Down Expand Up @@ -93,6 +95,9 @@
36F0DCC51BFAF41C0090EA4A /* libz.1.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.1.dylib; path = ../../../../../../usr/lib/libz.1.dylib; sourceTree = "<group>"; };
3F6C38E91B4A37D600F88662 /* JPMemory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JPMemory.h; sourceTree = "<group>"; };
3F6C38EA1B4A37D600F88662 /* JPMemory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JPMemory.m; sourceTree = "<group>"; };
6C6CAC711C5F0BCA00444348 /* SuperTestObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SuperTestObject.h; sourceTree = "<group>"; };
6C6CAC721C5F0BCA00444348 /* superTest.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = superTest.js; sourceTree = "<group>"; };
6C6CAC731C5F0BCA00444348 /* SuperTestObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SuperTestObject.m; sourceTree = "<group>"; };
6C72B7941C352BA80086C98D /* newProtocolTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = newProtocolTest.h; sourceTree = "<group>"; };
6C72B7951C352BA80086C98D /* newProtocolTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = newProtocolTest.m; sourceTree = "<group>"; };
6C9C0EE31C25A7C700FCAAC5 /* newProtocolTest.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = newProtocolTest.js; sourceTree = "<group>"; };
Expand Down Expand Up @@ -275,6 +280,9 @@
DE94AE271AF246C000E461D4 /* JSPatchTests */ = {
isa = PBXGroup;
children = (
6C6CAC711C5F0BCA00444348 /* SuperTestObject.h */,
6C6CAC721C5F0BCA00444348 /* superTest.js */,
6C6CAC731C5F0BCA00444348 /* SuperTestObject.m */,
6C72B7941C352BA80086C98D /* newProtocolTest.h */,
6C72B7951C352BA80086C98D /* newProtocolTest.m */,
6C9C0EE31C25A7C700FCAAC5 /* newProtocolTest.js */,
Expand Down Expand Up @@ -434,6 +442,7 @@
files = (
DE6FFF031B42B01C0005EE83 /* protocolTest.js in Resources */,
2D0DF7071B22F75C005695DA /* inheritTest.js in Resources */,
6C6CAC741C5F0BCA00444348 /* superTest.js in Resources */,
DE94AE471AF2480000E461D4 /* test.js in Resources */,
6C9C0EE41C25A7C700FCAAC5 /* newProtocolTest.js in Resources */,
E1B89EAE1B228818000645C2 /* multithreadTest.js in Resources */,
Expand Down Expand Up @@ -481,6 +490,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6C6CAC751C5F0BCA00444348 /* SuperTestObject.m in Sources */,
6C72B7961C352BA80086C98D /* newProtocolTest.m in Sources */,
DE94AE461AF2480000E461D4 /* JPTestObject.m in Sources */,
E1B89EA31B218986000645C2 /* JPInheritanceTestObjects.m in Sources */,
Expand Down
Empty file modified JSPatchDemo/JSPatchDemo/JPViewController.h
100644 → 100755
Empty file.
8 changes: 8 additions & 0 deletions JSPatchDemo/JSPatchTests/JSPatchTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "newProtocolTest.h"
//#import "JPCoreGraphics.h"
//#import "JPUIKit.h"
#import "SuperTestObject.h"
#import "JPMemory.h"
@interface JSPatchTests : XCTestCase

Expand Down Expand Up @@ -174,6 +175,13 @@ - (void)testEngine {
beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
}

- (void)testSuperClass
{
[self loadPatch:@"superTest"];
SuperTestC *testobject = [[SuperTestC alloc]init];
[testobject testSuper];
}

- (void)testInheritance
{
/*get values before patch*/
Expand Down
25 changes: 25 additions & 0 deletions JSPatchDemo/JSPatchTests/SuperTestObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// SuperTest.h
// JSPatchDemo
//
// Created by Awhisper on 16/1/28.
// Copyright © 2016年 bang. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface SuperTestA : NSObject
-(void)testSuper;
@end

@interface SuperTestB : SuperTestA

-(void)testSuper;
@end

@interface SuperTestC : SuperTestB

-(void)testSuper;
@end

37 changes: 37 additions & 0 deletions JSPatchDemo/JSPatchTests/SuperTestObject.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// SuperTest.m
// JSPatchDemo
//
// Created by Awhisper on 16/1/28.
// Copyright © 2016年 bang. All rights reserved.
//

#import "SuperTestObject.h"

@implementation SuperTestB

-(void)testSuper
{
NSLog(@" ==== print test B ===");
}

@end

@implementation SuperTestA

-(void)testSuper
{
NSLog(@" === print test A ====");
}

@end

@implementation SuperTestC

-(void)testSuper
{
[super testSuper];
NSLog(@" === print test C ====");
}

@end
6 changes: 6 additions & 0 deletions JSPatchDemo/JSPatchTests/superTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defineClass('SuperTestB : SuperTestA', {
testSuper: function() {
self.ORIGtestSuper();
self.super().testSuper();
}
})