[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>
This commit is contained in:
Daco Harkes 2021-02-11 13:13:32 +00:00 committed by commit-bot@chromium.org
parent 9068fa59a0
commit f358afca9e
14 changed files with 48 additions and 46 deletions

View file

@ -57,7 +57,7 @@ abstract class FfiBindings {
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 @@ abstract class FfiBindings {
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 @@ void scheduleAsyncInvocation(void fun()) {
}
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 @@ Future withPeerPointer(fun(Pointer<Void> peer)) async {
} 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);
}
}

View file

@ -57,7 +57,7 @@ abstract class FfiBindings {
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 @@ abstract class FfiBindings {
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 @@ void scheduleAsyncInvocation(void fun()) {
}
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 @@ Future withPeerPointer(fun(Pointer<Void> peer)) async {
} 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);
}
}

View file

@ -35,7 +35,7 @@ void main() {
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 @@ void main() {
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) {

View file

@ -28,7 +28,7 @@ class Database {
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 @@ class Database {
/// 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 @@ class Database {
/// 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 @@ class Database {
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 @@ class Database {
}
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 @@ class Row {
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 @@ class Row {
/// 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() {

View file

@ -6,6 +6,6 @@ author: Daco Harkes <dacoharkes@google.com>, Samir Jindel <sjindel@google.com>
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

View file

@ -166,8 +166,8 @@ void main() {
});
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);
});
}

View file

@ -30,7 +30,7 @@ class Database {
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 @@ class Database {
/// 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 @@ class Database {
/// 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 @@ class Database {
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 @@ class Database {
}
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 @@ class Row {
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 @@ class Row {
/// 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() {

View file

@ -6,6 +6,6 @@ author: Daco Harkes <dacoharkes@google.com>, Samir Jindel <sjindel@google.com>
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

View file

@ -170,8 +170,8 @@ void main() {
});
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);
});
}

View file

@ -40,7 +40,7 @@ typedef Dart_PropagateError_DartType = void Function(Object);
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>>()

View file

@ -105,7 +105,7 @@ void testTypeTest() {
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);
}

View file

@ -135,8 +135,8 @@ void testTypeTest() {
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);
}

View file

@ -40,7 +40,7 @@ typedef Dart_PropagateError_DartType = void Function(Object);
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>>()

View file

@ -135,8 +135,8 @@ void testTypeTest() {
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);
}