Skip to content

Commit 621d253

Browse files
Merge pull request #40 from dart-native/bugfix/property_getter
Bugfix/property getter
2 parents 0887ffb + dbc6d4a commit 621d253

File tree

5 files changed

+77
-35
lines changed

5 files changed

+77
-35
lines changed

bin/codegen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const { program } = require('commander')
33
const main = require('../lib/main').main
44

5-
program.version('1.1.0')
5+
program.version('1.1.1')
66

77
program
88
.arguments('<input>', 'Iutput directory')

lib/common/utils.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
String.prototype.capitalize = function () {
22
return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase()
3-
}
3+
}
4+
5+
function rawGenericType(type) {
6+
var isGeneric = type.indexOf('<') > -1 && type.indexOf('>') > -1
7+
var rawType = isGeneric ? type.substring(0, type.indexOf('<')) : type
8+
return rawType
9+
}
10+
11+
exports.rawGenericType = rawGenericType

lib/objc/DNObjectiveCContext.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var DNObjectiveCTypeConverter = require('./DNObjectiveCTypeConverter').DNObjectiveCTypeConverter
2-
require('../common/utils')
2+
const utils = require('../common/utils')
33

44
class DNContext {
55
constructor(internal) {
@@ -157,7 +157,7 @@ class DNArgumentContext extends DNContext {
157157
if (internal.name && internal.types) {
158158
this.name = internal.name.start.text
159159
}
160-
this.type = null
160+
this.type = ''
161161
this.isBlock = false //user for block arguement
162162
this.isNullable = false
163163
this.isOutParam = false
@@ -200,7 +200,7 @@ class DNMethodContext extends DNContext {
200200
preHandleMutableArgsIfNeed() {
201201
var result = ''
202202
this.args.forEach((element) => {
203-
var rawType = this.rawGenericType(element.type)
203+
var rawType = utils.rawGenericType(element.type)
204204
if (DNObjectiveCTypeConverter.SupportMutableTypes.indexOf(rawType) > -1) {
205205
var tmpArgName = '_' + element.name
206206
result += ' ' + rawType + ' ' + tmpArgName + ' = ' + rawType + '(' + element.name + ');\n'
@@ -238,7 +238,7 @@ class DNMethodContext extends DNContext {
238238
var args = noArg ? '' : ', args: [' + this.args.map(arg => arg.name) + ']'
239239
var impl = callerPrefix + 'perform(SEL(\'' + this.ocMethodName() + '\')' + args + ');\n'
240240

241-
var rawRetType = this.rawGenericType(this.returnType) //remove <> symbol
241+
var rawRetType = utils.rawGenericType(this.returnType) //remove <> symbol
242242
var isMutableRetType = DNObjectiveCTypeConverter.SupportMutableTypes.indexOf(rawRetType) > -1
243243

244244
if (!isMutableRetType && !this.callFromPointer) {
@@ -320,18 +320,12 @@ class DNMethodContext extends DNContext {
320320
}
321321

322322
convertMutableTypeIfNeed(type) {
323-
var rawType = this.rawGenericType(type)
323+
var rawType = utils.rawGenericType(type)
324324
var dartType = DNObjectiveCTypeConverter.SupportMutableTypesMap[rawType]
325325
var ret = dartType ? type.replace(rawType, dartType) : type
326326
return ret
327327
}
328328

329-
rawGenericType(type) {
330-
var isGeneric = type.indexOf('<') > -1 && type.indexOf('>') > -1
331-
var rawType = isGeneric ? type.substring(0, type.indexOf('<')) : type
332-
return rawType
333-
}
334-
335329
ocMethodName() {
336330
var funcName = ''
337331
this.args.forEach((_element, index) => {
@@ -370,6 +364,7 @@ class DNPropertyContext extends DNContext {
370364
this.type = null
371365
this.isClassProperty = false
372366
this.isReadOnly = false
367+
this.isDartPointerType = false
373368
this.macros = []
374369
this.availability = []
375370
}
@@ -382,14 +377,39 @@ class DNPropertyContext extends DNContext {
382377
this.type = 'dynamic'
383378
}
384379
var annotation = ' ' + this.availability.map((a) => a.parse()).join(' ') + '\n'
385-
var get = annotation + ' ' + this.type + ' get ' + this.name +
386-
' => perform(SEL(\'' + this.name + '\'));'
380+
var isClassPrefix = (this.isClassProperty ? ' Class(\'' + this.parent.name + '\').' : ' ')
381+
382+
var get = annotation + ' ' + this.type + ' get ' + this.name + ' {\n' +
383+
this.handleGetter(isClassPrefix) + '\n}'
387384
if (!this.isReadOnly) {
388385
var set = annotation + ' ' + 'set ' + this.name + '(' + this.type + ' ' + this.name + ')' +
389-
' => perform(SEL(\'set' + this.name.replace(/^\w/, c => c.toUpperCase()) + ':\'), args: [' + this.name + ']);'
386+
' =>' + isClassPrefix + 'perform(SEL(\'set' + this.name.replace(/^\w/, c => c.toUpperCase()) + ':\'), args: [' + this.name + ']);'
390387
}
391388
return get + '\n' + set
392389
}
390+
391+
handleGetter(isClassPrefix) {
392+
var impl = isClassPrefix + 'perform(SEL(\'' + this.name + '\')' + ');\n'
393+
var rawRetType = utils.rawGenericType(this.type) //remove <> symbol
394+
var isMutableRetType = DNObjectiveCTypeConverter.SupportMutableTypes.indexOf(rawRetType) > -1
395+
396+
if (!isMutableRetType && !this.isDartPointerType) {
397+
return (this.type == 'void' ? '' : 'return') + impl
398+
}
399+
400+
var newImpl = 'Pointer<Void> result =' + impl.replace(');\n', '') + ', decodeRetVal: false);\n'
401+
if (this.isDartPointerType) {
402+
var supportType = DNObjectiveCTypeConverter.DNDartToOCMap[rawRetType]
403+
if (supportType) {
404+
newImpl += ' return ' + supportType + '.fromPointer(result).raw;\n'
405+
} else if (isMutableRetType) {
406+
newImpl += ' return ' + rawRetType + '.fromPointer(result).raw;\n'
407+
} else {
408+
newImpl += ' return ' + rawRetType + '.fromPointer(result);\n'
409+
}
410+
}
411+
return newImpl
412+
}
393413
}
394414

395415
class DNProtocolContext extends DNContext {

lib/objc/DNObjectiveCParserListener.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,26 @@ class DNObjectiveCParserListener extends ObjectiveCParserListener {
327327
enterBlockType(ctx) {
328328
if (this.currentContext instanceof DNMethodContext) {
329329
this.currentContext.returnType = 'Block'
330+
} else if (this.currentContext instanceof DNArgumentContext) {
331+
this.currentContext.isBlock = true
332+
this.currentContext.blockRetType = null
333+
this.currentContext.blockArgTypes = []
334+
this.currentContext.blockArgNames = []
330335
}
331336
}
332337
// Exit a parse tree produced by ObjectiveCParser#blockType.
333338
exitBlockType(ctx) {
339+
if (this.currentContext instanceof DNArgumentContext) {
340+
var blockArgs = ''
341+
this.currentContext.blockArgTypes.forEach((argType, index) => {
342+
var argName = this.currentContext.blockArgNames[index]
343+
if (argName && argType) {
344+
blockArgs += argType + ' ' + argName + ', '
345+
}
346+
})
347+
blockArgs = '(' + blockArgs.substring(0, blockArgs.length - 2) + ')' // remove ', ' symbol
348+
this.currentContext.type += this.currentContext.blockRetType + ' ' + this.currentContext.name + blockArgs
349+
}
334350
}
335351
// Enter a parse tree produced by ObjectiveCParser#genericsSpecifier.
336352
enterGenericsSpecifier(ctx) {
@@ -377,25 +393,9 @@ class DNObjectiveCParserListener extends ObjectiveCParserListener {
377393
}
378394
// Enter a parse tree produced by ObjectiveCParser#blockParameters.
379395
enterBlockParameters(ctx) {
380-
if (this.currentContext instanceof DNArgumentContext) {
381-
this.currentContext.isBlock = true
382-
this.currentContext.blockArgTypes = []
383-
this.currentContext.blockArgNames = []
384-
}
385396
}
386397
// Exit a parse tree produced by ObjectiveCParser#blockParameters.
387398
exitBlockParameters(ctx) {
388-
if (this.currentContext instanceof DNArgumentContext) {
389-
var blockArgs = ''
390-
this.currentContext.blockArgTypes.forEach((argType, index) => {
391-
var argName = this.currentContext.blockArgNames[index]
392-
if (argName && argType) {
393-
blockArgs += argType + ' ' + argName + ', '
394-
}
395-
})
396-
blockArgs = '(' + blockArgs.substring(0, blockArgs.length - 2) + ')' // remove ', ' symbol
397-
this.currentContext.type += ' ' + this.currentContext.name + blockArgs
398-
}
399399
}
400400
// Enter a parse tree produced by ObjectiveCParser#typeVariableDeclaratorOrName.
401401
enterTypeVariableDeclaratorOrName(ctx) {
@@ -754,17 +754,31 @@ class DNObjectiveCParserListener extends ObjectiveCParserListener {
754754
if (!this.currentContext.isBlock) {
755755
let isArgInBlock = this.currentContext.parent instanceof DNBlockDefContext
756756
this.currentContext.type = TC.convert(type, isArgInBlock)
757-
}else {
758-
this.currentContext.blockArgTypes.push(type)
757+
} else {
758+
let convertedType = TC.convert(type, true)
759+
if (!this.currentContext.blockRetType) {
760+
this.currentContext.blockRetType = convertedType
761+
} else {
762+
this.currentContext.blockArgTypes.push(convertedType)
763+
}
759764
}
760765
// case:(NSError ** )error
761766
this.currentContext.isOutParam = isPointer2Pointer
762767
} else if (this.currentContext instanceof DNBlockDefContext) {
763768
this.currentContext.returnType = TC.convert(type, true)
764769
} else if (this.currentContext instanceof DNPropertyContext) {
765770
if (!this.currentContext.type) { //avoid repeatly setValue
771+
if (type == 'instancetype') {
772+
type = this.currentContext.parent.name
773+
}
766774
this.currentContext.type = TC.convert(type)
767775
}
776+
// FIXME: pointer to struct
777+
/// Both CString and NSString can be converted to Dart String.
778+
/// But converting CString to Dart String is under auto encoding,
779+
/// which doesn't need call `fromPointer`.
780+
this.currentContext.isDartPointerType = !DNObjectiveCTypeConverter.basicAutoConvertReturnTypes.includes(this.currentContext.type) &&
781+
(!DNObjectiveCTypeConverter.ObjcBasicType.includes(type) || (pointer != null && type != 'char *'));
768782
}
769783
}
770784
// Exit a parse tree produced by ObjectiveCParser#typeSpecifier.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dartnative/codegen",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Code generator for dart_native.",
55
"main": "index.js",
66
"bin": {

0 commit comments

Comments
 (0)