@@ -76,19 +76,19 @@ class DNBlockDefContext extends DNContext {
76
76
this . returnType = ''
77
77
this . defName = null
78
78
this . args = [ ]
79
- }
79
+ }
80
80
81
81
parse ( ) {
82
82
var argList = ''
83
83
this . args . forEach ( ( element , index ) => {
84
- if ( index == this . args . length - 1 ) {
84
+ if ( index == this . args . length - 1 ) {
85
85
argList += element . type + ' ' + element . name
86
- } else {
86
+ } else {
87
87
argList += element . type + ' ' + element . name + ', '
88
88
}
89
89
} )
90
90
var result = 'typedef '
91
- result += this . returnType + ' ' + this . defName + '(' + argList + ');'
91
+ result += this . returnType + ' ' + this . defName + '(' + argList + ');'
92
92
return result
93
93
}
94
94
}
@@ -126,7 +126,7 @@ class DNEnumDefContext extends DNContext {
126
126
this . macros = [ ]
127
127
this . availability = [ ]
128
128
}
129
-
129
+
130
130
parse ( ) {
131
131
var result = '\n'
132
132
if ( this . defName ) {
@@ -173,73 +173,73 @@ class DNMethodContext extends DNContext {
173
173
}
174
174
175
175
parse ( ) {
176
- if ( this . args . length == 0 && this . hasSameMethodDeclaration ( ) ) {
176
+ if ( this . args . length == 0 && this . hasSameMethodDeclaration ( ) ) {
177
177
return ''
178
- } else if ( this . args . length == 1 && this . hasSameMethodDeclaration ( ) ) {
178
+ } else if ( this . args . length == 1 && this . hasSameMethodDeclaration ( ) ) {
179
179
return this . parseForOptionalSingleArg ( )
180
180
}
181
181
182
182
var isInstanceConstr = ( this . returnType == this . parent . name ) && ! this . isClassMethod
183
- if ( isInstanceConstr ) {
183
+ if ( isInstanceConstr ) {
184
184
return this . constructorImpl ( )
185
185
} else {
186
186
var result = ' ' + this . availability . map ( ( a ) => a . parse ( ) ) . join ( ' ' ) + '\n'
187
187
result += ' ' + ( this . isClassMethod ? 'static ' : '' ) + this . convertMutableTypeIfNeed ( this . returnType ) + ' ' + this . methodDeclaration ( ) + this . methodArgs ( ) + ' {\n'
188
- result += this . preHandleMutableArgsIfNeed ( ) + ' ' + this . methodImpl ( )
188
+ result += this . preHandleMutableArgsIfNeed ( ) + ' ' + this . methodImpl ( )
189
189
result += ' }'
190
190
return result
191
191
}
192
192
}
193
193
194
- preHandleMutableArgsIfNeed ( ) {
194
+ preHandleMutableArgsIfNeed ( ) {
195
195
var result = ''
196
196
this . args . forEach ( ( element ) => {
197
197
var rawType = this . rawGenericType ( element . type )
198
- if ( DNObjectiveCTypeConverter . SupportMutableTypes . indexOf ( rawType ) > - 1 ) {
198
+ if ( DNObjectiveCTypeConverter . SupportMutableTypes . indexOf ( rawType ) > - 1 ) {
199
199
var tmpArgName = '_' + element . name
200
- result += ' ' + rawType + ' ' + tmpArgName + ' = ' + rawType + '(' + element . name + ');\n'
200
+ result += ' ' + rawType + ' ' + tmpArgName + ' = ' + rawType + '(' + element . name + ');\n'
201
201
element . name = tmpArgName
202
202
}
203
203
} )
204
204
return result
205
205
}
206
206
207
- hasSameMethodDeclaration ( ) {
207
+ hasSameMethodDeclaration ( ) {
208
208
var methods = this . parent . methods
209
- for ( var i = 0 ; i < methods . length ; i ++ ) {
209
+ for ( var i = 0 ; i < methods . length ; i ++ ) {
210
210
var method = methods [ i ]
211
- if ( this != method && this . methodDeclaration ( ) == method . methodDeclaration ( ) && this . isClassMethod == method . isClassMethod ) {
211
+ if ( this != method && this . methodDeclaration ( ) == method . methodDeclaration ( ) && this . isClassMethod == method . isClassMethod ) {
212
212
return true
213
213
}
214
214
}
215
215
return false
216
216
}
217
217
218
- methodDeclaration ( ) {
219
- var methodDeclaration = ''
218
+ methodDeclaration ( ) {
219
+ var methodDeclaration = ''
220
220
this . args . forEach ( ( _element , index ) => {
221
221
methodDeclaration += index >= 1 ? this . names [ index ] . replace ( / ^ \w / , c => c . toUpperCase ( ) ) : this . names [ index ]
222
222
} )
223
223
methodDeclaration = methodDeclaration ? methodDeclaration : this . methodName
224
224
return methodDeclaration
225
225
}
226
226
227
- methodImpl ( noArg ) {
227
+ methodImpl ( noArg ) {
228
228
var callerPrefix = ( this . isClassMethod ? ' Class(\'' + this . parent . name + '\').' : ' ' )
229
- var args = noArg ? '' : ', args: [' + this . args . map ( arg => arg . name ) + ']'
229
+ var args = noArg ? '' : ', args: [' + this . args . map ( arg => arg . name ) + ']'
230
230
var impl = callerPrefix + 'perform(\'' + this . ocMethodName ( ) + '\'.toSEL()' + args + ');\n'
231
231
232
232
var rawRetType = this . rawGenericType ( this . returnType ) //remove <> symbol
233
233
var isMutableRetType = DNObjectiveCTypeConverter . SupportMutableTypes . indexOf ( rawRetType ) > - 1
234
234
235
- if ( ! isMutableRetType && ! this . retValIsObj ) {
235
+ if ( ! isMutableRetType && ! this . retValIsObj ) {
236
236
return ( this . returnType == 'void' ? '' : 'return' ) + impl
237
237
}
238
238
239
- var newImpl = 'Pointer<Void> result = ' + impl . replace ( ');\n' , '' ) + ' ,decodeRetVal: false);\n'
240
- if ( this . retValIsObj ) {
239
+ var newImpl = 'Pointer<Void> result = ' + impl . replace ( ');\n' , '' ) + ' ,decodeRetVal: false);\n'
240
+ if ( this . retValIsObj ) {
241
241
var supportType = DNObjectiveCTypeConverter . DNDartToOCMap [ rawRetType ]
242
- if ( supportType ) {
242
+ if ( supportType ) {
243
243
newImpl += ' return ' + supportType + '.fromPointer(result).raw;\n'
244
244
} else if ( isMutableRetType ) {
245
245
newImpl += ' return ' + rawRetType + '.fromPointer(result).raw;\n'
@@ -250,41 +250,41 @@ class DNMethodContext extends DNContext {
250
250
return newImpl
251
251
}
252
252
253
- constructorImpl ( ) {
253
+ constructorImpl ( ) {
254
254
var result = ''
255
- if ( this . isSingleInstanceConstr ) {
256
- // such as NSError(arg x)
255
+ if ( this . isSingleInstanceConstr ) {
256
+ // such as NSError(arg x)
257
257
result += ' ' + this . parent . name + this . methodArgs ( ) + '\n'
258
- } else {
258
+ } else {
259
259
// such as NSError.initWithxxxx(arg x)
260
260
result += ' ' + this . parent . name + '.' + this . methodDeclaration ( ) + this . methodArgs ( ) + '\n'
261
261
}
262
- result += ' : super.fromPointer(_' + this . methodDeclaration ( ) + '(' + this . args . map ( arg => arg . name ) + '));\n'
262
+ result += ' : super.fromPointer(_' + this . methodDeclaration ( ) + '(' + this . args . map ( arg => arg . name ) + '));\n'
263
263
result += '\n' ;
264
264
result += ' static Pointer<Void> _' + this . methodDeclaration ( ) + this . methodArgs ( ) + ' {\n'
265
- result += this . preHandleMutableArgsIfNeed ( )
266
- result += ' Pointer<Void> target = alloc(Class(\'' + this . parent . name + '\'));\n'
265
+ result += this . preHandleMutableArgsIfNeed ( )
266
+ result += ' Pointer<Void> target = alloc(Class(\'' + this . parent . name + '\'));\n'
267
267
result += ' SEL sel = \'' + this . ocMethodName ( ) + '\'.toSEL();\n'
268
268
result += ' return msgSend(target, sel, ' + 'args: [' + this . args . map ( arg => arg . name ) + ']' + ', decodeRetVal: false);\n'
269
269
result += ' }\n'
270
270
return result
271
271
}
272
272
273
- methodArgs ( optional ) {
273
+ methodArgs ( optional ) {
274
274
//convert as follows: int a, String b, {int c, String d}
275
275
var argList = optional ? '([' : '('
276
276
var nullableArgs = [ ]
277
277
this . args . forEach ( ( element , index ) => {
278
- if ( element . isNullable ) {
278
+ if ( element . isNullable ) {
279
279
nullableArgs . push ( element )
280
- } else {
280
+ } else {
281
281
var argType = element . isOutParam ? 'NSObjectRef<' + element . type + '>' : element . type
282
282
var arg = element . anonDef ? element . anonDef : this . convertMutableTypeIfNeed ( argType ) + ' ' + element . name
283
283
argList += arg + ( index == this . args . length - 1 && nullableArgs . length == 0 ? '' : ', ' )
284
284
}
285
285
} )
286
-
287
- if ( nullableArgs . length > 0 ) {
286
+
287
+ if ( nullableArgs . length > 0 ) {
288
288
argList += '{'
289
289
nullableArgs . forEach ( ( element , index ) => {
290
290
var argType = element . isOutParam ? 'NSObjectRef<' + element . type + '>' : element . type
@@ -297,7 +297,7 @@ class DNMethodContext extends DNContext {
297
297
return argList
298
298
}
299
299
300
- parseForOptionalSingleArg ( ) {
300
+ parseForOptionalSingleArg ( ) {
301
301
var optionalArgType = this . args [ 0 ] . type
302
302
var optionalArgName = this . args [ 0 ] . name
303
303
var result = ' ' + ( this . isClassMethod ? 'static ' : '' ) + this . returnType + ' ' + this . methodDeclaration ( ) + '([' + optionalArgType + ' ' + optionalArgName + '])' + ' {\n'
@@ -310,44 +310,44 @@ class DNMethodContext extends DNContext {
310
310
return result
311
311
}
312
312
313
- convertMutableTypeIfNeed ( type ) {
313
+ convertMutableTypeIfNeed ( type ) {
314
314
var rawType = this . rawGenericType ( type )
315
315
var dartType = DNObjectiveCTypeConverter . SupportMutableTypesMap [ rawType ]
316
- var ret = dartType ? type . replace ( rawType , dartType ) : type
316
+ var ret = dartType ? type . replace ( rawType , dartType ) : type
317
317
return ret
318
318
}
319
319
320
- rawGenericType ( type ) {
321
- var isGeneric = type . indexOf ( '<' ) > - 1 && type . indexOf ( '>' ) > - 1
322
- var rawType = isGeneric ? type . substring ( 0 , type . indexOf ( '<' ) ) : type
320
+ rawGenericType ( type ) {
321
+ var isGeneric = type . indexOf ( '<' ) > - 1 && type . indexOf ( '>' ) > - 1
322
+ var rawType = isGeneric ? type . substring ( 0 , type . indexOf ( '<' ) ) : type
323
323
return rawType
324
324
}
325
325
326
- ocMethodName ( ) {
326
+ ocMethodName ( ) {
327
327
var funcName = ''
328
328
this . args . forEach ( ( _element , index ) => {
329
- funcName += this . names [ index ] + ( this . args . length >= 1 ? ':' : '' )
329
+ funcName += this . names [ index ] + ( this . args . length >= 1 ? ':' : '' )
330
330
} )
331
331
funcName = funcName ? funcName : this . methodName
332
332
return funcName
333
333
}
334
334
}
335
- class DNMethodDeclarationContext extends DNMethodContext {
335
+ class DNMethodDeclarationContext extends DNMethodContext {
336
336
constructor ( internal ) {
337
337
super ( internal )
338
338
}
339
339
340
- parse ( ) {
340
+ parse ( ) {
341
341
// We have to ignore static methods due to this issue: https://github.com/dart-lang/language/issues/356
342
- if ( this . isClassMethod || ( this . args . length == 0 && this . hasSameMethodDeclaration ( ) ) ) {
342
+ if ( this . isClassMethod || ( this . args . length == 0 && this . hasSameMethodDeclaration ( ) ) ) {
343
343
return ''
344
344
}
345
-
345
+
346
346
var result = ' ' + this . availability . map ( ( a ) => a . parse ( ) ) . join ( ' ' ) + '\n'
347
347
result += ' ' + this . returnType + ' ' + this . methodDeclaration ( )
348
348
349
- if ( this . args . length == 1 && this . hasSameMethodDeclaration ( ) ) {
350
- result += this . methodArgs ( true ) + ';'
349
+ if ( this . args . length == 1 && this . hasSameMethodDeclaration ( ) ) {
350
+ result += this . methodArgs ( true ) + ';'
351
351
}
352
352
result += this . methodArgs ( ) + ';'
353
353
return result ;
@@ -367,7 +367,7 @@ class DNPropertyContext extends DNContext {
367
367
368
368
parse ( ) {
369
369
var annotation = ' ' + this . availability . map ( ( a ) => a . parse ( ) ) . join ( ' ' ) + '\n'
370
- var get = annotation + ' ' + this . type + ' get ' + this . name +
370
+ var get = annotation + ' ' + this . type + ' get ' + this . name +
371
371
' => perform(\'' + this . name + '\'.toSEL());'
372
372
if ( ! this . isReadOnly ) {
373
373
var set = annotation + ' ' + 'set ' + this . name + '(' + this . type + ' ' + this . name + ')' +
@@ -386,7 +386,7 @@ class DNProtocolContext extends DNContext {
386
386
this . protocols = [ ]
387
387
}
388
388
389
- parse ( ) {
389
+ parse ( ) {
390
390
var result = 'abstract class ' + this . name
391
391
if ( typeof this . protocols !== 'undefined' && this . protocols . length > 0 ) {
392
392
result += ' implements ' + this . protocols . join ( ',' )
@@ -437,17 +437,17 @@ class DNClassContext extends DNContext {
437
437
}
438
438
439
439
// mark the method if the class has only one instance construction
440
- preMarkConstructMethods ( ) {
440
+ preMarkConstructMethods ( ) {
441
441
var markMethod
442
442
var hasOneInstanceConstr = false
443
- for ( var i = 0 ; i < this . methods . length ; i ++ ) {
443
+ for ( var i = 0 ; i < this . methods . length ; i ++ ) {
444
444
var method = this . methods [ i ]
445
445
var isInstanceConstr = ( method . returnType == this . name ) && ! method . isClassMethod
446
- if ( isInstanceConstr ) {
447
- if ( hasOneInstanceConstr ) {
446
+ if ( isInstanceConstr ) {
447
+ if ( hasOneInstanceConstr ) {
448
448
markMethod . isSingleInstanceConstr = false
449
449
break
450
- } else {
450
+ } else {
451
451
hasOneInstanceConstr = true
452
452
method . isSingleInstanceConstr = true
453
453
markMethod = method
@@ -482,7 +482,7 @@ class DNCategoryContext extends DNContext {
482
482
}
483
483
484
484
class DNImportContext extends DNContext {
485
- constructor ( internal , needExport ) {
485
+ constructor ( internal , needExport ) {
486
486
super ( internal )
487
487
this . needExport = needExport
488
488
var frameworkCtx = internal . frameworkName
@@ -519,7 +519,7 @@ class DNImportContext extends DNContext {
519
519
} else if ( packageName ) {
520
520
result += packageName + '.dart\';'
521
521
}
522
-
522
+
523
523
return result
524
524
}
525
525
}
@@ -548,9 +548,9 @@ class DNRootContext extends DNContext {
548
548
return childResult
549
549
} ) . join ( '\n' )
550
550
return {
551
- dartCode : result ,
552
- packages : packageSet
553
- }
551
+ dartCode : result ,
552
+ packages : packageSet
553
+ }
554
554
}
555
555
}
556
556
0 commit comments