Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 194eb0d

Browse files
committed
Fix #1007
1 parent 62e22c1 commit 194eb0d

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

pkgs/ffigen/lib/src/code_generator/objc_interface.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ class ObjCInterface extends BindingType {
7979
s.write(makeDartDoc(dartDoc!));
8080
}
8181

82-
final uniqueNamer = UniqueNamer({name, 'pointer'});
82+
final uniqueNamer =
83+
UniqueNamer({name, 'pointer'}, parent: w.topLevelUniqueNamer);
8384

8485
final rawObjType = PointerType(objCObjectType).getCType(w);
8586
final wrapObjType = ObjCBuiltInFunctions.objectBase.gen(w);

pkgs/ffigen/lib/src/code_generator/utils.dart

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ class UniqueNamer {
1515
final Set<String> _usedUpNames;
1616

1717
/// Creates a UniqueNamer with given [usedUpNames] and Dart reserved keywords.
18-
UniqueNamer(Set<String> usedUpNames)
18+
///
19+
/// If [parent] is provided, also includes all the parent's names.
20+
UniqueNamer(Set<String> usedUpNames, {UniqueNamer? parent})
1921
: assert(keywords.intersection(usedUpNames).isEmpty),
20-
_usedUpNames = {...keywords, ...usedUpNames};
22+
_usedUpNames = {
23+
...keywords,
24+
...usedUpNames,
25+
...(parent?._usedUpNames ?? {}),
26+
};
2127

2228
/// Creates a UniqueNamer with given [usedUpNames] only.
2329
UniqueNamer._raw(this._usedUpNames);

pkgs/ffigen/test/native_objc_test/method_test.dart

+12
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ void main() {
9393
test('Doubles', () {
9494
expect(testInstance.addDoubles_Y_(1.23, 4.56), closeTo(5.79, 1e-6));
9595
});
96+
97+
test('Method with same name as a type', () {
98+
// Test for https://github.com/dart-lang/native/issues/1007
99+
final resultPtr = calloc<Vec4>();
100+
final result = resultPtr.ref;
101+
testInstance.Vec41(resultPtr); // A slightly unfortunate rename :P
102+
expect(result.x, 1);
103+
expect(result.y, 2);
104+
expect(result.z, 3);
105+
expect(result.w, 4);
106+
calloc.free(resultPtr);
107+
});
96108
});
97109
});
98110
}

pkgs/ffigen/test/native_objc_test/method_test.m

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ +(int32_t)sub:(int32_t)x Y:(int32_t) y Z:(int32_t) z;
2424
-(Vec4)twiddleVec4Components:(Vec4)v;
2525
-(float)addFloats:(float)x Y:(float) y;
2626
-(double)addDoubles:(double)x Y:(double) y;
27+
-(Vec4)Vec4;
2728

2829
@end
2930

@@ -78,4 +79,13 @@ -(double)addDoubles:(double)x Y:(double) y {
7879
return x + y;
7980
}
8081

82+
-(Vec4)Vec4 {
83+
Vec4 u;
84+
u.x = 1;
85+
u.y = 2;
86+
u.z = 3;
87+
u.w = 4;
88+
return u;
89+
}
90+
8191
@end

0 commit comments

Comments
 (0)