Dart 2.5 release, support for C Dart and call each other

Dart 2.5 has been released, which comprises two main preview new features: machine learning (ML) driven code completion and dart: ffi external function interface for invoking C code directly from the Dart. Dart 2.5 also improves support for constant expressions. details as follows:

ML Complete, sorted by machine learning code completion

Event model by analyzing a large number of members of Corpus Dart GitHub open source code, according to the given context of training may arise. This model is driven by TensorFlow Lite, it can be used to predict the next symbol developer was about to be edited, this new feature called ML Complete. Here is an  Flutter  framework for developing new MyHome exemplary widget:

Suppose write a small program used to calculate the time from the current time to a certain day. Use ML Complete, as shown below (first figure). First, it can provide in accordance with the variable name is now automatically DateTime.Now () completion. Next, define the variable names tomorrow, using the add (...) method. In the non-ML Complete (second graph), you must manually start entering Datetime will have a prompt completion, and add () method positions completion suggestions in the list to be low.

ML Complete can be used in all supported Dart editor, including Android Studio, IntelliJ VS Code and the like. Because this feature is still out of the preview stage, try to be Flutter in the  dev channel  and Dart  dev channel  version.

dart: ffi External Interface function

目前,直接从 Dart 调用 C 的支持仅限于使用原生扩展深入集成到 Dart VM 中,或者 Flutter 应用程序可以间接调用 C,通过使用 Platform Channel 调用主机,在那里调用 C,这是一个不受欢迎的双重间接。

Dart-C 互操作支持的两种主要方案:

  1. 在主机操作系统(OS)上调用基于 C 的系统 API
  2. 为单个操作系统或跨平台调用基于 C 的库

调用基于 C 的操作系统 API

调用 Linux 命令 system,该命令允许执行任何系统命令,传递给它的参数实际是传递给  shell/terminal,并在其运行。下面是这个命令的 C 头文件:

// C header: int system(const char *command) in stdlib.h

任何互操作机制的难点在于处理两种语言在语义上的差异。对于 dart:ffi,Dart 代码需要表示两件事:

  • C 函数及其参数和返回类型的类型
  • 对应的 Dart 函数及其类型
// C header typedef:
typedef SystemC = ffi.Int32 Function(ffi.Pointer<Utf8> command);

// Dart header typedef:
typedef SystemDart = int Function(ffi.Pointer<Utf8> command);

然后,需要加载库并查找要调用的函数,如何做到这一点取决于操作系统;在本例中,使用的是 MacOS:

// Load `stdlib`. On MacOS this is in libSystem.dylib.
final dylib = ffi.DynamicLibrary.open('/usr/lib/libSystem.dylib');

// Look up the system function.
final systemP = dylib.lookupFunction<SystemC, SystemDart>('system');

接下来,使用与特定操作系统相关的编码对字符串参数进行编码,调用函数,并再次释放参数内存:

// Allocate a pointer to a Utf8 array containing our command.
final cmdP = Utf8.toUtf8('open http://dart.dev');

// Invoke the command.
systemP(cmdP);

// Free the pointer.
cmdP.free();

此代码执行系统命令,导致系统默认浏览器打开 dart.dev:

调用基于 C 的框架和组件

Dart 的第二个核心用途是调用基于 C 的框架和组件。如上的示例,它使用 TensorFlow Lite,一个基于 C 的 API,使用 Dart:ffi 允许在需要提供代码完成的所有操作系统上运行 TensorFlow,并具有本机 TensorFlow 实现的高性能。

目前 dart:ffi 出于预览阶段,所以还会有一些限制。

另外,从 Dart 2.5 开始,支持更多的方法来定义常量表达式,包括强制转换以及在 Dart 2.3 中提供的新的控制流和集合扩展特性:

const Object i = 3;
const list = [i as int];
const set = {if (list is List<int>) ...list};
const map = {if (i is int) i: "int"};

详情见发布说明

Guess you like

Origin www.oschina.net/news/109799/dart-2-5-released