nandi/vflutter_ffipublic Fork 0
4506a586d7400b00d732dcb97167be75ab41cab1
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 · 78 lines · 3.3 KBDart Blame HistoryRaw
Replace V with Nim 472eb49 nandithebull 21h 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///
Render in parallel on web, via a pool of Web Workers 4506a58 nandithebull 21h ago26/// True on native (isolates) and on web (Web Workers, one wasm instance
27/// each). False only if the web build is served without nimflutter_pool.js,
28/// in which case the split still produces identical pixels, sequentially.
29bool get supportsIsolateParallelism => backend.supportsIsolateParallelism;
30
31/// What one unit of parallelism is called here — 'isolate' or 'worker'.
32/// Purely for display.
33String get parallelUnitName => backend.parallelUnitName;
Replace V with Nim 472eb49 nandithebull 21h ago34
35/// Whether the bridge is ready to take calls.
36bool get isInitialized => backend.isInitialized;
37
38/// Prepares the bridge, and the only form that works on every backend.
39///
40/// Native initialisation is synchronous, but loading a wasm module is not, so
41/// the async form is the common denominator. Await this once at startup.
42Future<void> initialize() => backend.initialize();
43
44/// Synchronous initialisation. Native-only: on web this throws unless
45/// [initialize] has already completed, because the module cannot be loaded
46/// synchronously.
47void ensureInitialized() => backend.ensureInitialized();
48
49/// Adds two integers in Nim. The trivial case, useful as a liveness check.
50int add(int a, int b) => backend.add(a, b);
51
52/// Round-trips a string through Nim.
53///
54/// Nim allocates the result on the shared heap; this copies it into a Dart
55/// [String] and frees the Nim allocation before returning, so there is nothing
56/// for the caller to release.
57String greet(String name) => backend.greet(name);
58
59/// Runs [greet] off the calling isolate where the backend supports it.
60Future<String> greetAsync(String name) => backend.greetAsync(name);
61
62/// Renders rows [y0, y1) of [view] into a fresh RGBA byte buffer.
63///
64/// The buffer is allocated on the Dart side and filled in place by Nim, so no
65/// Nim memory is created and nothing needs freeing.
66Uint8List renderBand(FractalView view, int y0, int y1) =>
67 backend.renderBand(view, y0, y1);
68
69/// Renders the whole of [view] on the calling isolate.
70Uint8List render(FractalView view) => renderBand(view, 0, view.height);
71
72/// Renders [view] as [tiles] horizontal bands.
73///
74/// Bands are independent by construction — each call writes only its own rows
Render in parallel on web, via a pool of Web Workers 4506a58 nandithebull 21h ago75/// — so this is real parallelism: isolates on native, Web Workers on web,
76/// each with its own wasm instance.
Replace V with Nim 472eb49 nandithebull 21h ago77Future<Uint8List> renderParallel(FractalView view, {int tiles = 4}) =>
78 backend.renderParallel(view, tiles);