[vm/ffi] DynamicLibrary.handle

Issue: https://github.com/dart-lang/sdk/issues/35881

Change-Id: I67ff49fef950c7c507f5006c357909cd09914c5f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106903
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Auto-Submit: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Samir Jindel <sjindel@google.com>
diff --git a/runtime/lib/ffi_dynamic_library_patch.dart b/runtime/lib/ffi_dynamic_library_patch.dart
index 29e8c88..8216da0 100644
--- a/runtime/lib/ffi_dynamic_library_patch.dart
+++ b/runtime/lib/ffi_dynamic_library_patch.dart
@@ -32,4 +32,7 @@
   int get hashCode {
     return getHandle().hashCode;
   }
+
+  @patch
+  Pointer<Void> get handle => fromAddress(getHandle());
 }
diff --git a/sdk/lib/ffi/dynamic_library.dart b/sdk/lib/ffi/dynamic_library.dart
index 98aa64a..844c879 100644
--- a/sdk/lib/ffi/dynamic_library.dart
+++ b/sdk/lib/ffi/dynamic_library.dart
@@ -29,4 +29,7 @@
 
   /// The hash code for a DynamicLibrary only depends on the loaded library
   external int get hashCode;
+
+  /// The handle to the dynamic library.
+  external Pointer<Void> get handle;
 }
diff --git a/tests/ffi/dynamic_library_test.dart b/tests/ffi/dynamic_library_test.dart
index 3282f4c..7b29a18 100644
--- a/tests/ffi/dynamic_library_test.dart
+++ b/tests/ffi/dynamic_library_test.dart
@@ -8,7 +8,7 @@
 
 library FfiTest;
 
-import 'dart:ffi' as ffi;
+import 'dart:ffi';
 
 import 'dylib_utils.dart';
 
@@ -21,10 +21,11 @@
   testLookupError();
   testToString();
   testEquality();
+  testHandle();
 }
 
 void testOpen() {
-  ffi.DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
+  DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
   Expect.notEquals(null, l);
 }
 
@@ -33,35 +34,46 @@
       () => dlopenPlatformSpecific("doesnotexistforsurelibrary123409876"));
 }
 
-typedef NativeDoubleUnOp = ffi.Double Function(ffi.Double);
+typedef NativeDoubleUnOp = Double Function(Double);
 
 typedef DoubleUnOp = double Function(double);
 
 void testLookup() {
-  ffi.DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
+  DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
   var timesFour = l.lookupFunction<NativeDoubleUnOp, DoubleUnOp>("timesFour");
   Expect.approxEquals(12.0, timesFour(3));
 }
 
 void testLookupError() {
-  ffi.DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
+  DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
   Expect.throws(() => l.lookupFunction<NativeDoubleUnOp, DoubleUnOp>(
       "functionnamethatdoesnotexistforsure749237593845"));
 }
 
 void testToString() {
-  ffi.DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
+  DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
   Expect.stringEquals(
       "DynamicLibrary: handle=0x", l.toString().substring(0, 25));
 }
 
 void testEquality() {
-  ffi.DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
-  ffi.DynamicLibrary l2 = dlopenPlatformSpecific("ffi_test_dynamic_library");
+  DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
+  DynamicLibrary l2 = dlopenPlatformSpecific("ffi_test_dynamic_library");
   Expect.equals(l, l2);
   Expect.equals(l.hashCode, l2.hashCode);
   Expect.notEquals(l, null);
   Expect.notEquals(null, l);
-  ffi.DynamicLibrary l3 = dlopenPlatformSpecific("ffi_test_functions");
+  DynamicLibrary l3 = dlopenPlatformSpecific("ffi_test_functions");
   Expect.notEquals(l, l3);
 }
+
+void testHandle() {
+  DynamicLibrary l = dlopenPlatformSpecific("ffi_test_dynamic_library");
+  DynamicLibrary l2 = dlopenPlatformSpecific("ffi_test_dynamic_library");
+  Pointer<Void> h = l.handle;
+  Pointer<Void> h2 = l2.handle;
+  Expect.equals(h, h2);
+  DynamicLibrary l3 = dlopenPlatformSpecific("ffi_test_functions");
+  Pointer<Void> h3 = l3.handle;
+  Expect.notEquals(h, h3);
+}