[vm/ffi] Migrate `package:ffi` `Utf8` and `Utf16` uses

Change-Id: I41d79969881015266273a3ce566ee402df11791d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/184481
Reviewed-by: Clement Skau <cskau@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
diff --git a/runtime/tests/vm/dart/isolates/dart_api_create_lightweight_isolate_test.dart b/runtime/tests/vm/dart/isolates/dart_api_create_lightweight_isolate_test.dart
index be0d1ab..83b7cc5 100644
--- a/runtime/tests/vm/dart/isolates/dart_api_create_lightweight_isolate_test.dart
+++ b/runtime/tests/vm/dart/isolates/dart_api_create_lightweight_isolate_test.dart
@@ -57,7 +57,7 @@
 
   static Pointer<Isolate> createLightweightIsolate(
       String name, Pointer<Void> peer) {
-    final cname = Utf8.toUtf8(name);
+    final cname = name.toNativeUtf8();
     IGH_MsanUnpoison(cname.cast(), name.length + 10);
     try {
       final isolate = IGH_CreateIsolate(cname, peer);
@@ -74,9 +74,9 @@
     final dartScriptUri = sdkRoot.resolve(
         'runtime/tests/vm/dart/isolates/dart_api_create_lightweight_isolate_test.dart');
     final dartScript = dartScriptUri.toString();
-    final libraryUri = Utf8.toUtf8(dartScript);
+    final libraryUri = dartScript.toNativeUtf8();
     IGH_MsanUnpoison(libraryUri.cast(), dartScript.length + 1);
-    final functionName = Utf8.toUtf8(name);
+    final functionName = name.toNativeUtf8();
     IGH_MsanUnpoison(functionName.cast(), name.length + 1);
 
     IGH_StartIsolate(
@@ -106,7 +106,7 @@
 }
 
 Future withPeerPointer(fun(Pointer<Void> peer)) async {
-  final Pointer<Void> peer = Utf8.toUtf8('abc').cast();
+  final Pointer<Void> peer = 'abc'.toNativeUtf8().cast();
   FfiBindings.IGH_MsanUnpoison(peer.cast(), 'abc'.length + 1);
   try {
     await fun(peer);
@@ -116,12 +116,12 @@
   } finally {
     // The shutdown callback is called before the exit listeners are notified, so
     // we can validate that a->x has been changed.
-    Expect.isTrue(Utf8.fromUtf8(peer.cast()).startsWith('xb'));
+    Expect.isTrue(peer.cast<Utf8>().toDartString().startsWith('xb'));
 
     // The cleanup callback is called after after notifying exit listeners. So we
     // wait a little here to ensure the write of the callback has arrived.
     await Future.delayed(const Duration(milliseconds: 100));
-    Expect.equals('xbz', Utf8.fromUtf8(peer.cast()));
+    Expect.equals('xbz', peer.cast<Utf8>().toDartString());
     calloc.free(peer);
   }
 }
diff --git a/runtime/tests/vm/dart_2/isolates/dart_api_create_lightweight_isolate_test.dart b/runtime/tests/vm/dart_2/isolates/dart_api_create_lightweight_isolate_test.dart
index 73366bb..4611883 100644
--- a/runtime/tests/vm/dart_2/isolates/dart_api_create_lightweight_isolate_test.dart
+++ b/runtime/tests/vm/dart_2/isolates/dart_api_create_lightweight_isolate_test.dart
@@ -57,7 +57,7 @@
 
   static Pointer<Isolate> createLightweightIsolate(
       String name, Pointer<Void> peer) {
-    final cname = Utf8.toUtf8(name);
+    final cname = name.toNativeUtf8();
     IGH_MsanUnpoison(cname.cast(), name.length + 10);
     try {
       final isolate = IGH_CreateIsolate(cname, peer);
@@ -74,9 +74,9 @@
     final dartScriptUri = sdkRoot.resolve(
         'runtime/tests/vm/dart_2/isolates/dart_api_create_lightweight_isolate_test.dart');
     final dartScript = dartScriptUri.toString();
-    final libraryUri = Utf8.toUtf8(dartScript);
+    final libraryUri = dartScript.toNativeUtf8();
     IGH_MsanUnpoison(libraryUri.cast(), dartScript.length + 1);
-    final functionName = Utf8.toUtf8(name);
+    final functionName = name.toNativeUtf8();
     IGH_MsanUnpoison(functionName.cast(), name.length + 1);
 
     IGH_StartIsolate(
@@ -106,7 +106,7 @@
 }
 
 Future withPeerPointer(fun(Pointer<Void> peer)) async {
-  final Pointer<Void> peer = Utf8.toUtf8('abc').cast();
+  final Pointer<Void> peer = 'abc'.toNativeUtf8().cast();
   FfiBindings.IGH_MsanUnpoison(peer.cast(), 'abc'.length + 1);
   try {
     await fun(peer);
@@ -116,12 +116,12 @@
   } finally {
     // The shutdown callback is called before the exit listeners are notified, so
     // we can validate that a->x has been changed.
-    Expect.isTrue(Utf8.fromUtf8(peer.cast()).startsWith('xb'));
+    Expect.isTrue(peer.cast<Utf8>().toDartString().startsWith('xb'));
 
     // The cleanup callback is called after after notifying exit listeners. So we
     // wait a little here to ensure the write of the callback has arrived.
     await Future.delayed(const Duration(milliseconds: 100));
-    Expect.equals('xbz', Utf8.fromUtf8(peer.cast()));
+    Expect.equals('xbz', peer.cast<Utf8>().toDartString());
     calloc.free(peer);
   }
 }
diff --git a/samples/ffi/sqlite/example/main.dart b/samples/ffi/sqlite/example/main.dart
index 480ef42..59192ac 100644
--- a/samples/ffi/sqlite/example/main.dart
+++ b/samples/ffi/sqlite/example/main.dart
@@ -35,7 +35,7 @@
   for (Row r in result) {
     int id = r.readColumnAsInt("id");
     String name = r.readColumnByIndex(1);
-    String alternativeName = r.readColumn("alternative_name");
+    String? alternativeName = r.readColumn("alternative_name");
     dynamic multiTypedValue = r.readColumn("multi_typed_column");
     print("$id $name $alternativeName $multiTypedValue");
   }
@@ -54,7 +54,7 @@
   for (Row r in result) {
     int id = r.readColumnAsInt("id");
     String name = r.readColumnByIndex(1);
-    String alternativeName = r.readColumn("alternative_name");
+    String? alternativeName = r.readColumn("alternative_name");
     dynamic multiTypedValue = r.readColumn("multi_typed_column");
     print("$id $name $alternativeName $multiTypedValue");
     if (id == 2) {
diff --git a/samples/ffi/sqlite/lib/src/database.dart b/samples/ffi/sqlite/lib/src/database.dart
index b98bf25..9b0570c 100644
--- a/samples/ffi/sqlite/lib/src/database.dart
+++ b/samples/ffi/sqlite/lib/src/database.dart
@@ -28,7 +28,7 @@
   Database(String path,
       [int flags = Flags.SQLITE_OPEN_READWRITE | Flags.SQLITE_OPEN_CREATE]) {
     Pointer<Pointer<types.Database>> dbOut = calloc();
-    final pathC = Utf8.toUtf8(path);
+    final pathC = path.toNativeUtf8();
     final int resultCode =
         bindings.sqlite3_open_v2(pathC, dbOut, flags, nullptr);
     _database = dbOut.value;
@@ -64,7 +64,7 @@
   /// Execute a query, discarding any returned rows.
   void execute(String query) {
     Pointer<Pointer<Statement>> statementOut = calloc();
-    Pointer<Utf8> queryC = Utf8.toUtf8(query);
+    Pointer<Utf8> queryC = query.toNativeUtf8();
     int resultCode = bindings.sqlite3_prepare_v2(
         _database, queryC, -1, statementOut, nullptr);
     Pointer<Statement> statement = statementOut.value;
@@ -83,7 +83,7 @@
   /// Evaluate a query and return the resulting rows as an iterable.
   Result query(String query) {
     Pointer<Pointer<Statement>> statementOut = calloc();
-    Pointer<Utf8> queryC = Utf8.toUtf8(query);
+    Pointer<Utf8> queryC = query.toNativeUtf8();
     int resultCode = bindings.sqlite3_prepare_v2(
         _database, queryC, -1, statementOut, nullptr);
     Pointer<Statement> statement = statementOut.value;
@@ -99,7 +99,7 @@
     int columnCount = bindings.sqlite3_column_count(statement);
     for (int i = 0; i < columnCount; i++) {
       String columnName =
-          Utf8.fromUtf8(bindings.sqlite3_column_name(statement, i));
+          bindings.sqlite3_column_name(statement, i).toDartString();
       columnIndices[columnName] = i;
     }
 
@@ -107,9 +107,9 @@
   }
 
   SQLiteException _loadError(int errorCode) {
-    String errorMessage = Utf8.fromUtf8(bindings.sqlite3_errmsg(_database));
+    String errorMessage = bindings.sqlite3_errmsg(_database).toDartString();
     String errorCodeExplanation =
-        Utf8.fromUtf8(bindings.sqlite3_errstr(errorCode));
+        bindings.sqlite3_errstr(errorCode).toDartString();
     return SQLiteException(
         "$errorMessage (Code $errorCode: $errorCodeExplanation)");
   }
@@ -204,8 +204,9 @@
       dynamicType =
           _typeFromCode(bindings.sqlite3_column_type(_statement, columnIndex));
     } else {
-      dynamicType = _typeFromText(Utf8.fromUtf8(
-          bindings.sqlite3_column_decltype(_statement, columnIndex)));
+      dynamicType = _typeFromText(bindings
+          .sqlite3_column_decltype(_statement, columnIndex)
+          .toDartString());
     }
 
     switch (dynamicType) {
@@ -240,7 +241,7 @@
   /// Reads column [columnIndex] and converts to [Type.Text] if not text.
   String readColumnByIndexAsText(int columnIndex) {
     _checkIsCurrentRow();
-    return Utf8.fromUtf8(bindings.sqlite3_column_text(_statement, columnIndex));
+    return bindings.sqlite3_column_text(_statement, columnIndex).toDartString();
   }
 
   void _checkIsCurrentRow() {
diff --git a/samples/ffi/sqlite/pubspec.yaml b/samples/ffi/sqlite/pubspec.yaml
index 71d9d59..1effadc 100644
--- a/samples/ffi/sqlite/pubspec.yaml
+++ b/samples/ffi/sqlite/pubspec.yaml
@@ -6,6 +6,6 @@
 environment:
   sdk: '>=2.12.0-0 <3.0.0'
 dependencies:
-  ffi: ^0.2.0-nullsafety.1
+  ffi: ^0.3.1-nullsafety.0
 dev_dependencies:
   test: ^1.16.0-nullsafety.12
diff --git a/samples/ffi/sqlite/test/sqlite_test.dart b/samples/ffi/sqlite/test/sqlite_test.dart
index ef7666d..c0c9151 100644
--- a/samples/ffi/sqlite/test/sqlite_test.dart
+++ b/samples/ffi/sqlite/test/sqlite_test.dart
@@ -166,8 +166,8 @@
   });
   test("Utf8 unit test", () {
     final String test = 'Hasta Mañana';
-    final medium = Utf8.toUtf8(test);
-    expect(test, Utf8.fromUtf8(medium));
+    final medium = test.toNativeUtf8();
+    expect(test, medium.toDartString());
     calloc.free(medium);
   });
 }
diff --git a/samples_2/ffi/sqlite/lib/src/database.dart b/samples_2/ffi/sqlite/lib/src/database.dart
index 91bcfe6..d65724f 100644
--- a/samples_2/ffi/sqlite/lib/src/database.dart
+++ b/samples_2/ffi/sqlite/lib/src/database.dart
@@ -30,7 +30,7 @@
   Database(String path,
       [int flags = Flags.SQLITE_OPEN_READWRITE | Flags.SQLITE_OPEN_CREATE]) {
     Pointer<Pointer<types.Database>> dbOut = calloc();
-    final pathC = Utf8.toUtf8(path);
+    final pathC = path.toNativeUtf8();
     final int resultCode =
         bindings.sqlite3_open_v2(pathC, dbOut, flags, nullptr);
     _database = dbOut.value;
@@ -66,7 +66,7 @@
   /// Execute a query, discarding any returned rows.
   void execute(String query) {
     Pointer<Pointer<Statement>> statementOut = calloc();
-    Pointer<Utf8> queryC = Utf8.toUtf8(query);
+    Pointer<Utf8> queryC = query.toNativeUtf8();
     int resultCode = bindings.sqlite3_prepare_v2(
         _database, queryC, -1, statementOut, nullptr);
     Pointer<Statement> statement = statementOut.value;
@@ -85,7 +85,7 @@
   /// Evaluate a query and return the resulting rows as an iterable.
   Result query(String query) {
     Pointer<Pointer<Statement>> statementOut = calloc();
-    Pointer<Utf8> queryC = Utf8.toUtf8(query);
+    Pointer<Utf8> queryC = query.toNativeUtf8();
     int resultCode = bindings.sqlite3_prepare_v2(
         _database, queryC, -1, statementOut, nullptr);
     Pointer<Statement> statement = statementOut.value;
@@ -101,7 +101,7 @@
     int columnCount = bindings.sqlite3_column_count(statement);
     for (int i = 0; i < columnCount; i++) {
       String columnName =
-          Utf8.fromUtf8(bindings.sqlite3_column_name(statement, i));
+          bindings.sqlite3_column_name(statement, i).toDartString();
       columnIndices[columnName] = i;
     }
 
@@ -109,12 +109,12 @@
   }
 
   SQLiteException _loadError([int errorCode]) {
-    String errorMessage = Utf8.fromUtf8(bindings.sqlite3_errmsg(_database));
+    String errorMessage = bindings.sqlite3_errmsg(_database).toDartString();
     if (errorCode == null) {
       return SQLiteException(errorMessage);
     }
     String errorCodeExplanation =
-        Utf8.fromUtf8(bindings.sqlite3_errstr(errorCode));
+        bindings.sqlite3_errstr(errorCode).toDartString();
     return SQLiteException(
         "$errorMessage (Code $errorCode: $errorCodeExplanation)");
   }
@@ -214,8 +214,9 @@
       dynamicType =
           _typeFromCode(bindings.sqlite3_column_type(_statement, columnIndex));
     } else {
-      dynamicType = _typeFromText(Utf8.fromUtf8(
-          bindings.sqlite3_column_decltype(_statement, columnIndex)));
+      dynamicType = _typeFromText(bindings
+          .sqlite3_column_decltype(_statement, columnIndex)
+          .toDartString());
     }
 
     switch (dynamicType) {
@@ -251,7 +252,7 @@
   /// Reads column [columnIndex] and converts to [Type.Text] if not text.
   String readColumnByIndexAsText(int columnIndex) {
     _checkIsCurrentRow();
-    return Utf8.fromUtf8(bindings.sqlite3_column_text(_statement, columnIndex));
+    return bindings.sqlite3_column_text(_statement, columnIndex).toDartString();
   }
 
   void _checkIsCurrentRow() {
diff --git a/samples_2/ffi/sqlite/pubspec.yaml b/samples_2/ffi/sqlite/pubspec.yaml
index dcc5d40..16771df 100644
--- a/samples_2/ffi/sqlite/pubspec.yaml
+++ b/samples_2/ffi/sqlite/pubspec.yaml
@@ -6,6 +6,6 @@
 environment:
   sdk: '>=2.1.0 <3.0.0'
 dependencies:
-  ffi: ^0.1.3
+  ffi: ^0.3.1-nullsafety.0
 dev_dependencies:
   test: ^1.5.3
diff --git a/samples_2/ffi/sqlite/test/sqlite_test.dart b/samples_2/ffi/sqlite/test/sqlite_test.dart
index dffbe5b..91efe13 100644
--- a/samples_2/ffi/sqlite/test/sqlite_test.dart
+++ b/samples_2/ffi/sqlite/test/sqlite_test.dart
@@ -170,8 +170,8 @@
   });
   test("Utf8 unit test", () {
     final String test = 'Hasta Mañana';
-    final medium = Utf8.toUtf8(test);
-    expect(test, Utf8.fromUtf8(medium));
+    final medium = test.toNativeUtf8();
+    expect(test, medium.toDartString());
     calloc.free(medium);
   });
 }
diff --git a/tests/ffi/regress_jump_to_frame_test.dart b/tests/ffi/regress_jump_to_frame_test.dart
index a3d9054..f47800b 100644
--- a/tests/ffi/regress_jump_to_frame_test.dart
+++ b/tests/ffi/regress_jump_to_frame_test.dart
@@ -40,7 +40,7 @@
 final Dart_PropagateError_DartType propagateError = () {
   final Pointer<_DartApi> dlapi = NativeApi.initializeApiDLData.cast();
   for (int i = 0; dlapi.ref.functions[i].name != nullptr; i++) {
-    final name = Utf8.fromUtf8(dlapi.ref.functions[i].name.cast<Utf8>());
+    final name = dlapi.ref.functions[i].name.cast<Utf8>().toDartString();
     if (name == 'Dart_PropagateError') {
       return dlapi.ref.functions[i].function
           .cast<NativeFunction<Dart_PropagateError_NativeType>>()
diff --git a/tests/ffi/structs_nnbd_workaround_test.dart b/tests/ffi/structs_nnbd_workaround_test.dart
index 2ed0dca..cf8f672 100644
--- a/tests/ffi/structs_nnbd_workaround_test.dart
+++ b/tests/ffi/structs_nnbd_workaround_test.dart
@@ -105,7 +105,7 @@
 
 void testUtf8() {
   final String test = 'Hasta Mañana';
-  final Pointer<Utf8> medium = Utf8.toUtf8(test);
-  Expect.equals(test, Utf8.fromUtf8(medium));
+  final Pointer<Utf8> medium = test.toNativeUtf8();
+  Expect.equals(test, medium.toDartString());
   calloc.free(medium);
 }
diff --git a/tests/ffi/structs_test.dart b/tests/ffi/structs_test.dart
index 4a5bb8c..77026a4 100644
--- a/tests/ffi/structs_test.dart
+++ b/tests/ffi/structs_test.dart
@@ -135,8 +135,8 @@
 
 void testUtf8() {
   final String test = 'Hasta Mañana';
-  final Pointer<Utf8> medium = Utf8.toUtf8(test);
-  Expect.equals(test, Utf8.fromUtf8(medium));
+  final Pointer<Utf8> medium = test.toNativeUtf8();
+  Expect.equals(test, medium.toDartString());
   calloc.free(medium);
 }
 
diff --git a/tests/ffi_2/regress_jump_to_frame_test.dart b/tests/ffi_2/regress_jump_to_frame_test.dart
index 9838443..65e8682 100644
--- a/tests/ffi_2/regress_jump_to_frame_test.dart
+++ b/tests/ffi_2/regress_jump_to_frame_test.dart
@@ -40,7 +40,7 @@
 final Dart_PropagateError_DartType propagateError = () {
   final Pointer<_DartApi> dlapi = NativeApi.initializeApiDLData.cast();
   for (int i = 0; dlapi.ref.functions[i].name != nullptr; i++) {
-    final name = Utf8.fromUtf8(dlapi.ref.functions[i].name.cast<Utf8>());
+    final name = dlapi.ref.functions[i].name.cast<Utf8>().toDartString();
     if (name == 'Dart_PropagateError') {
       return dlapi.ref.functions[i].function
           .cast<NativeFunction<Dart_PropagateError_NativeType>>()
diff --git a/tests/ffi_2/structs_test.dart b/tests/ffi_2/structs_test.dart
index 4a5bb8c..77026a4 100644
--- a/tests/ffi_2/structs_test.dart
+++ b/tests/ffi_2/structs_test.dart
@@ -135,8 +135,8 @@
 
 void testUtf8() {
   final String test = 'Hasta Mañana';
-  final Pointer<Utf8> medium = Utf8.toUtf8(test);
-  Expect.equals(test, Utf8.fromUtf8(medium));
+  final Pointer<Utf8> medium = test.toNativeUtf8();
+  Expect.equals(test, medium.toDartString());
   calloc.free(medium);
 }