Skip to content

Commit 7acecda

Browse files
sjindel-googlecommit-bot@chromium.org
authored andcommitted
[vm/ffi] Fix FFI Utf8 example.
Change-Id: Ic6c93c94f1187595b0dab2c912ebf4851ac85fe5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108406 Reviewed-by: Daco Harkes <[email protected]> Commit-Queue: Samir Jindel <[email protected]>
1 parent e3b3c6f commit 7acecda

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

samples/ffi/sqlite/lib/src/ffi/utf8.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import "arena.dart";
99

1010
/// Represents a String in C memory, managed by an [Arena].
1111
class Utf8 extends Struct<Utf8> {
12-
@Int8()
12+
@Uint8()
1313
int char;
1414

1515
/// Allocates a [CString] in the current [Arena] and populates it with

samples/ffi/sqlite/test/sqlite_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import "package:test/test.dart";
88

99
import '../lib/sqlite.dart';
10+
import '../lib/src/ffi/utf8.dart';
1011

1112
void main() {
1213
test("sqlite integration test", () {
@@ -161,4 +162,10 @@ void main() {
161162
r.close();
162163
d.close();
163164
});
165+
test("Utf8 unit test", () {
166+
final String test = 'Hasta Mañana';
167+
final medium = Utf8.allocate(test);
168+
expect(test, medium.load<Utf8>().toString());
169+
medium.free();
170+
});
164171
}

tests/ffi/object_gc_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'dart:ffi';
1212

1313
import "package:expect/expect.dart";
1414

15-
import 'cstring.dart';
15+
import 'utf8.dart';
1616
import 'dylib_utils.dart';
1717

1818
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");

tests/ffi/structs_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import "package:expect/expect.dart";
1212

1313
import 'coordinate_bare.dart' as bare;
1414
import 'coordinate.dart';
15+
import 'utf8.dart';
1516

1617
void main() {
1718
testStructAllocate();
1819
testStructFromAddress();
1920
testStructWithNulls();
2021
testBareStruct();
2122
testTypeTest();
23+
testUtf8();
2224
}
2325

2426
/// allocates each coordinate separately in c memory
@@ -118,3 +120,10 @@ void testTypeTest() {
118120
Expect.isTrue(c is Struct<Coordinate>);
119121
c.addressOf.free();
120122
}
123+
124+
void testUtf8() {
125+
final String test = 'Hasta Mañana';
126+
final Pointer<Utf8> medium = Utf8.toUtf8(test);
127+
Expect.equals(test, Utf8.fromUtf8(medium));
128+
medium.free();
129+
}

tests/ffi/cstring.dart renamed to tests/ffi/utf8.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
library FfiTest;
5+
library Utf8;
66

77
import 'dart:convert';
88
import 'dart:ffi' as ffi;
99
import 'dart:ffi' show Pointer;
1010

1111
/// Sample non-struct Pointer wrapper for dart:ffi library.
1212
class Utf8 extends ffi.Struct<Utf8> {
13-
@ffi.Int8()
13+
@ffi.Uint8()
1414
int char;
1515

1616
static String fromUtf8(Pointer<Utf8> str) {
@@ -25,12 +25,13 @@ class Utf8 extends ffi.Struct<Utf8> {
2525
}
2626

2727
static Pointer<Utf8> toUtf8(String s) {
28-
Pointer<Utf8> result = Pointer<Utf8>.allocate(count: s.length + 1).cast();
2928
List<int> units = Utf8Encoder().convert(s);
30-
for (int i = 0; i < s.length; i++) {
29+
Pointer<Utf8> result =
30+
Pointer<Utf8>.allocate(count: units.length + 1).cast();
31+
for (int i = 0; i < units.length; i++) {
3132
result.elementAt(i).load<Utf8>().char = units[i];
3233
}
33-
result.elementAt(s.length).load<Utf8>().char = 0;
34+
result.elementAt(units.length).load<Utf8>().char = 0;
3435
return result;
3536
}
3637
}

0 commit comments

Comments
 (0)