Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dart: Rune type #364

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions example/dart/lib/lib.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions feature_tests/dart/lib/MyStruct.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions feature_tests/dart/lib/OptionOpaqueChar.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions feature_tests/dart/lib/lib.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tool/src/dart/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl<'tcx> DartFormatter<'tcx> {
if cast {
match prim {
PrimitiveType::Bool => "bool",
PrimitiveType::Char => "int",
PrimitiveType::Char => "Rune",
PrimitiveType::Int(_) | PrimitiveType::IntSize(_) => "int",
PrimitiveType::Int128(_) => panic!("i128 not supported in Dart"),
PrimitiveType::Float(_) => "double",
Expand Down Expand Up @@ -240,7 +240,7 @@ impl<'tcx> DartFormatter<'tcx> {
pub fn fmt_primitive_list_type(&self, prim: hir::PrimitiveType) -> &'static str {
use diplomat_core::hir::{FloatType, IntType, PrimitiveType};
match prim {
PrimitiveType::Char => "Uint32List",
PrimitiveType::Char => "RuneList",
PrimitiveType::Int(IntType::I8) => "Int8List",
PrimitiveType::Int(IntType::U8) => "Uint8List",
PrimitiveType::Int(IntType::I16) => "Int16List",
Expand All @@ -260,7 +260,7 @@ impl<'tcx> DartFormatter<'tcx> {
pub fn fmt_slice_type(&self, prim: hir::PrimitiveType) -> &'static str {
use diplomat_core::hir::{FloatType, IntType, PrimitiveType};
match prim {
PrimitiveType::Char => "_SliceFfiUint32",
PrimitiveType::Char => "_SliceRune",
PrimitiveType::Int(IntType::I8) => "_SliceFfiInt8",
PrimitiveType::Int(IntType::U8) => "_SliceFfiUint8",
PrimitiveType::Int(IntType::I16) => "_SliceFfiInt16",
Expand Down
1 change: 1 addition & 0 deletions tool/src/dart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub fn run<'cx>(
}

directives.insert(formatter.fmt_renamed_import("dart:ffi", "ffi"));
directives.insert(formatter.fmt_import("dart:typed_data"));
directives.insert(formatter.fmt_renamed_import("package:ffi/ffi.dart", "ffi2"));
files.add_file(
formatter.fmt_file_name("lib"),
Expand Down
17 changes: 17 additions & 0 deletions tool/templates/dart/init.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@

/// A [Rune] is a Unicode code point, such as `a`, or `💡`.
///
/// The recommended way to obtain a [Rune] is to create it from a
/// [String], which is conceptually a list of [Runes]. For example,
/// `'a'.runes.first` is equal to the [Rune] `a`.
///
/// Dart does not have a character/rune literal, so integer literals
/// need to be used. For example the Unicode code point U+1F4A1, `💡`,
/// can be represented by `0x1F4A1`. Note that only values in the ranges
/// `0x0..0xD7FF` and `0xE000..0x10FFFF` (both inclusive) are Unicode
/// code points, and hence valid [Rune]s.
///
/// A [String] can be constructed from a [Rune] using [String.fromCharCode].
typedef Rune = int;
/// A list of [Rune]s.
typedef RuneList = Uint32List;

late final ffi.Pointer<T> Function<T extends ffi.NativeType>(String) _capi;
void init(String path) => _capi = ffi.DynamicLibrary.open(path).lookup;

Expand Down
Loading