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