nandi/vflutter_ffipublic Fork 0
472eb493bac94bdcb79a38829ccca0c541256fef
Commits
Clone
git clone https://git.rickub.com/nandi/vflutter_ffi.git
git clone ssh://git@rickub.com/nandi/vflutter_ffi.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

nimflutter_ffi.dart · 73 lines · 3.1 KBDart Blame HistoryRaw
Replace V with Nim 472eb49 nandithebull 20h ago1/// Idiomatic Dart surface over the Nim library.
2///
3/// Callers never see a Pointer, and never own Nim memory: every string that
4/// crosses the boundary is copied into Dart and the Nim allocation is released
5/// before the call returns.
6///
7/// Two backends implement the same signatures — `dart:ffi` against the native
8/// shared library, and `dart:js_interop` against the emscripten build — and
9/// the conditional import below picks one at compile time. Callers see no
10/// difference beyond [supportsIsolateParallelism].
11library nimflutter_ffi;
12
13import 'dart:typed_data';
14
15import 'src/backend_native.dart'
16 if (dart.library.js_interop) 'src/backend_web.dart' as backend;
17import 'src/fractal_view.dart';
18
19export 'src/fractal_view.dart' show FractalView;
20
21/// Which bridge this build is using, for display. 'dart:ffi' or 'wasm'.
22const String backendName = backend.backendName;
23
24/// Whether bands can genuinely run at the same time.
25///
26/// False on web: the emscripten module is single-threaded, so a split still
27/// produces the identical frame but renders it sequentially.
28const bool supportsIsolateParallelism = backend.supportsIsolateParallelism;
29
30/// Whether the bridge is ready to take calls.
31bool get isInitialized => backend.isInitialized;
32
33/// Prepares the bridge, and the only form that works on every backend.
34///
35/// Native initialisation is synchronous, but loading a wasm module is not, so
36/// the async form is the common denominator. Await this once at startup.
37Future<void> initialize() => backend.initialize();
38
39/// Synchronous initialisation. Native-only: on web this throws unless
40/// [initialize] has already completed, because the module cannot be loaded
41/// synchronously.
42void ensureInitialized() => backend.ensureInitialized();
43
44/// Adds two integers in Nim. The trivial case, useful as a liveness check.
45int add(int a, int b) => backend.add(a, b);
46
47/// Round-trips a string through Nim.
48///
49/// Nim allocates the result on the shared heap; this copies it into a Dart
50/// [String] and frees the Nim allocation before returning, so there is nothing
51/// for the caller to release.
52String greet(String name) => backend.greet(name);
53
54/// Runs [greet] off the calling isolate where the backend supports it.
55Future<String> greetAsync(String name) => backend.greetAsync(name);
56
57/// Renders rows [y0, y1) of [view] into a fresh RGBA byte buffer.
58///
59/// The buffer is allocated on the Dart side and filled in place by Nim, so no
60/// Nim memory is created and nothing needs freeing.
61Uint8List renderBand(FractalView view, int y0, int y1) =>
62 backend.renderBand(view, y0, y1);
63
64/// Renders the whole of [view] on the calling isolate.
65Uint8List render(FractalView view) => renderBand(view, 0, view.height);
66
67/// Renders [view] as [tiles] horizontal bands.
68///
69/// Bands are independent by construction — each call writes only its own rows
70/// — so on native this is real parallelism across isolates. On web it is the
71/// same split run sequentially; see [supportsIsolateParallelism].
72Future<Uint8List> renderParallel(FractalView view, {int tiles = 4}) =>
73 backend.renderParallel(view, tiles);