Last active
May 19, 2023 04:46
-
-
Save sjindel-google/b88c964eb260e09280e588c41c6af3e5 to your computer and use it in GitHub Desktop.
Passing strings between Go and Dart (via FFI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go build -o godart.so -buildmode=c-shared | |
dart godart.dart |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "dart:ffi"; | |
import "dart:convert"; | |
import 'package:ffi/ffi.dart'; | |
class GoString extends Struct { | |
Pointer<Uint8> string; | |
@IntPtr() | |
int length; | |
String toString() { | |
List<int> units = []; | |
for (int i = 0; i < length; ++i) { | |
units.add(string.elementAt(i).value); | |
} | |
return Utf8Decoder().convert(units); | |
} | |
static Pointer<GoString> fromString(String string) { | |
List<int> units = Utf8Encoder().convert(string); | |
final ptr = allocate<Uint8>(count: units.length); | |
for (int i = 0; i < units.length; ++i) { | |
ptr.elementAt(i).value = units[i]; | |
} | |
final GoString str = allocate<GoString>().ref; | |
str.length = units.length; | |
str.string = ptr; | |
return str.addressOf; | |
} | |
} | |
typedef logType = Void Function(Pointer<GoString>); | |
typedef GoLog = void Function(Pointer<GoString>); | |
main() { | |
final lib = DynamicLibrary.open("./godart.so"); | |
final GoLog goLog = lib.lookup<NativeFunction<logType>>("Go_Log").asFunction(); | |
final Pointer<GoString> message = GoString.fromString("Hello, Go!"); | |
goLog(message); | |
free(message); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "dart:ffi"; | |
import "dart:convert"; | |
import 'package:ffi/ffi.dart'; | |
class GoString extends Struct { | |
Pointer<Uint8> string; | |
@IntPtr() | |
int length; | |
String toString() { | |
List<int> units = []; | |
for (int i = 0; i < length; ++i) { | |
units.add(string.elementAt(i).value); | |
} | |
return Utf8Decoder().convert(units); | |
} | |
static Pointer<GoString> fromString(String string) { | |
List<int> units = Utf8Encoder().convert(string); | |
final ptr = allocate<Uint8>(count: units.length); | |
for (int i = 0; i < units.length; ++i) { | |
ptr.elementAt(i).value = units[i]; | |
} | |
final GoString str = allocate<GoString>().ref; | |
str.length = units.length; | |
str.string = ptr; | |
return str.addressOf; | |
} | |
} | |
typedef logType = Void Function(Pointer<GoString>); | |
typedef GoLog = void Function(Pointer<GoString>); | |
main() { | |
final lib = DynamicLibrary.open("./godart.so"); | |
final GoLog goLog = lib.lookup<NativeFunction<logType>>("Go_Log").asFunction(); | |
final Pointer<GoString> message = GoString.fromString("Hello, Go!"); | |
goLog(message); | |
free(message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The same does not work in the new version of ffi?