Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function recFindByExt(base, ext, files, result) {
function writeOutputToFileByPath(tree, srcPath){
var srcFile = srcPath.substr(srcPath.lastIndexOf('/') + 1)
var dartFile = srcFile.substring(0,srcFile.indexOf('.')) + '.dart'
var outputFile = path.join(outputDir, dartFile)
var outputFile = outputDir ? path.join(outputDir, dartFile) : dartFile
if (fs.existsSync(outputFile)) {
fs.appendFileSync(outputFile, '\r\n\r\n' + tree);
}else{
Expand Down
84 changes: 65 additions & 19 deletions lib/objc/DNObjectiveCContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,71 @@ class DNMethodContext extends DNContext {
}

parse() {
if(this.args.length == 0 && this.hasSameMethodDeclaration()){
return ''
}else if(this.args.length == 1 && this.hasSameMethodDeclaration()){
return this.parseForOptionalSingleArg()
}
var result = ' ' + (this.isClassMethod ? 'static ' : '') + this.returnType + ' ' + this.methodDeclaration() + '(' + this.methodArgs() + ')' + ' {\n'
result += ' ' + this.methodImpl()
result += ' }'
return result
}

hasSameMethodDeclaration(){
var methods = this.parent.methods
for(var i = 0 ; i < methods.length; i++){
var method = methods[i]
if(this != method && this.methodDeclaration() == method.methodDeclaration() && this.isClassMethod == method.isClassMethod){
return true
}
}
return false
}

methodDeclaration(){
var methodDeclaration = ''
this.args.forEach((_element, index) => {
methodDeclaration += index >= 1 ? this.names[index].replace(/^\w/, c => c.toUpperCase()) : this.names[index]
});
methodDeclaration = methodDeclaration ? methodDeclaration : this.methodName
return methodDeclaration
}

methodImpl(noArg){
var funcName = ''
this.args.forEach((_element, index) => {
funcName += this.names[index] + (this.args.length >= 1 ? ':' : '')
});
funcName = funcName ? funcName : this.methodName
var callerPrefix = (this.isClassMethod ? ' Class(\'' + this.parent.name + '\').' : ' ')
var args = noArg ? '' : ', args: [' + this.args.map(arg => arg.name) + ']'
return 'return' + callerPrefix + 'perform(\'' + funcName + '\'.toSEL()' + args + ');\n'
}

methodArgs(){
var argList = ''
var returnType = this.returnType == 'instancetype' ? this.parent.name : this.returnType
this.args.forEach((element, index) => {
funcName += this.names[index] + (this.args.length >= 1 ? ':' : '')
if(index == this.args.length - 1){
argList += element.type + ' ' + element.name
}else{
argList += element.type + ' ' + element.name + ', '
}
});
funcName = funcName ? funcName : this.methodName
var callerPrefix = (this.isClassMethod ? ' Class(\'' + this.parent.name + '\').' : ' ')
var result = ' ' + (this.isClassMethod ? 'static ' : '') + returnType + ' ' + this.methodName + '(' + argList + ')' + ' {\n'
result += ' return' + callerPrefix + 'perform(\'' + funcName + '\'.toSEL(), args: [' + this.args.map(arg => arg.name) + ']);\n'
return argList
}

parseForOptionalSingleArg(){
var optionalArgType = this.args[0].type
var optionalArgName = this.args[0].name
var result = ' ' + (this.isClassMethod ? 'static ' : '') + this.returnType + ' ' + this.methodDeclaration() + '([' + optionalArgType + ' ' + optionalArgName + '])' + ' {\n'
result += ' if (' + optionalArgName + ' != null) {\n'
result += ' ' + this.methodImpl()
result += ' } else { \n'
result += ' ' + this.methodImpl(true)
result += ' }\n'
result += ' }'
return result
return result;
}
}

Expand All @@ -62,16 +110,12 @@ class DNMethodDeclarationContext extends DNMethodContext{
}

parse() {
var argList = ''
var returnType = this.returnType == 'instancetype' ? this.parent.name : this.returnType
this.args.forEach((element, index) => {
if(index == this.args.length - 1){
argList += element.type + ' ' + element.name
}else{
argList += element.type + ' ' + element.name + ', '
}
});
return ' ' + (this.isClassMethod ? 'static ' : '') + returnType + ' ' + this.names[0] + '(' + argList + ');'
if(this.args.length == 0 && this.hasSameMethodDeclaration()){
return ''
}else if(this.args.length == 1 && this.hasSameMethodDeclaration()){
return ' ' + (this.isClassMethod ? 'static ' : '') + this.returnType + ' ' + this.methodDeclaration() + '([' + this.methodArgs() + ']);'
}
return ' ' + (this.isClassMethod ? 'static ' : '') + this.returnType + ' ' + this.methodDeclaration() + '(' + this.methodArgs() + ');'
}
}

Expand Down Expand Up @@ -138,10 +182,12 @@ class DNClassContext extends DNContext {
result += ' {\n'
result += ' ' + this.name + '([Class isa]) : super(Class(\'' + this.name + '\'));\n'
this.properties.forEach(element => {
result += element.parse() + '\n'
var parseRet = element.parse()
result += parseRet ? parseRet + '\n' : ''
})
this.methods.forEach(element => {
result += element.parse() + '\n'
var parseRet = element.parse()
result += parseRet ? parseRet + '\n' : ''
})
result += '\n}'
return result
Expand Down
2 changes: 1 addition & 1 deletion lib/objc/DNObjectiveCParserListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ class DNObjectiveCParserListener extends ObjectiveCParserListener {
// Enter a parse tree produced by ObjectiveCParser#typeName.
enterTypeName(ctx) {
if (this.currentContext instanceof DNMethodContext) {
this.currentContext.returnType = ctx.start.text
this.currentContext.returnType = ctx.start.text == 'instancetype' ? this.currentContext.parent.name : ctx.start.text
} else if (this.currentContext instanceof DNArgumentContext) {
this.currentContext.type = ctx.start.text
}
Expand Down
4 changes: 2 additions & 2 deletions test/objc/RuntimeStub.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, class) BOOL foo;
@property (nonatomic) NSObject *bar;
- (BOOL)fooBOOL;
- (BOOL)fooBOOL:(BOOL)a;
- (BOOL)fooBOOL:(BOOL)a bar:(NSObject *)b;
+ (BOOL)barBOOL:(BOOL)a;

@end

@interface RuntimeStub(Foo)
Expand Down