Skip to content

Commit

Permalink
rune
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian committed Nov 21, 2023
1 parent 911b1dd commit 7e0482c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 8 deletions.
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

0 comments on commit 7e0482c

Please sign in to comment.