[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: https://github.com/dart-lang/sdk/issues/43889
Change-Id: Ifbf2d70de00e4748c179fe7d626c495675c2b338
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/208502
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Clement Skau <cskau@google.com>
This commit is contained in:
Clement Skau 2021-08-09 09:13:38 +00:00 committed by commit-bot@chromium.org
parent 3097b72b2b
commit bf2691c2fe
2 changed files with 21 additions and 1 deletions

View file

@ -143,7 +143,16 @@ class FfiNativeTransformer extends Transformer {
fileUri: currentLibrary!.fileUri,
getterReference: currentLibraryIndex?.lookupGetterReference(fieldName))
..fileOffset = node.fileOffset;
currentLibrary!.addField(funcPtrField);
// Add field to the parent the FfiNative function belongs to.
final parent = node.parent;
if (parent is Class) {
parent.addField(funcPtrField);
} else if (parent is Library) {
parent.addField(funcPtrField);
} else {
throw 'Unexpected parent of @FfiNative function. '
'Expected Class or Library, but found ${parent}.';
}
// _@FfiNative__square_root(x)
final callFuncPtrInvocation = FunctionInvocation(

View file

@ -48,6 +48,17 @@ class Classy {
external int returnIntPtrMethod(int x); //# 03: compile-time error
}
// Regression test: Ensure same-name FfiNative functions don't collide in the
// top-level namespace, but instead live under their parent (Library, Class).
class A {
@FfiNative<Void Function()>('nop')
external static void foo();
}
class B {
@FfiNative<Void Function()>('nop')
external static void foo();
}
void main() {
// Register test resolver for top-level functions above.
final root_lib_url = getRootLibraryUrl();