Skip to content

Commit bf2691c

Browse files
Clement Skaucommit-bot@chromium.org
Clement Skau
authored andcommitted
[VM] Moves FfiNative fields to function parent.
Previously the synthetic field that holds the FfiNative function pointer was injected into the current library. This change makes sure we instead add the field to the relevant parent - Class or Library. TEST=Added regression test for name collision. Bug: #43889 Change-Id: Ifbf2d70de00e4748c179fe7d626c495675c2b338 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/208502 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Clement Skau <[email protected]>
1 parent 3097b72 commit bf2691c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pkg/vm/lib/transformations/ffi_native.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,16 @@ class FfiNativeTransformer extends Transformer {
143143
fileUri: currentLibrary!.fileUri,
144144
getterReference: currentLibraryIndex?.lookupGetterReference(fieldName))
145145
..fileOffset = node.fileOffset;
146-
currentLibrary!.addField(funcPtrField);
146+
// Add field to the parent the FfiNative function belongs to.
147+
final parent = node.parent;
148+
if (parent is Class) {
149+
parent.addField(funcPtrField);
150+
} else if (parent is Library) {
151+
parent.addField(funcPtrField);
152+
} else {
153+
throw 'Unexpected parent of @FfiNative function. '
154+
'Expected Class or Library, but found ${parent}.';
155+
}
147156

148157
// _@FfiNative__square_root(x)
149158
final callFuncPtrInvocation = FunctionInvocation(

tests/ffi/ffi_native_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ class Classy {
4848
external int returnIntPtrMethod(int x); //# 03: compile-time error
4949
}
5050

51+
// Regression test: Ensure same-name FfiNative functions don't collide in the
52+
// top-level namespace, but instead live under their parent (Library, Class).
53+
class A {
54+
@FfiNative<Void Function()>('nop')
55+
external static void foo();
56+
}
57+
class B {
58+
@FfiNative<Void Function()>('nop')
59+
external static void foo();
60+
}
61+
5162
void main() {
5263
// Register test resolver for top-level functions above.
5364
final root_lib_url = getRootLibraryUrl();

0 commit comments

Comments
 (0)