| Replace V with Nim 472eb49 nandithebull 11h ago | 1 | /// 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]. |
| 11 | library nimflutter_ffi; |
| 12 | |
| 13 | import 'dart:typed_data'; |
| 14 | |
| 15 | import 'src/backend_native.dart' |
| 16 | if (dart.library.js_interop) 'src/backend_web.dart' as backend; |
| 17 | import 'src/fractal_view.dart'; |
| 18 | |
| 19 | export 'src/fractal_view.dart' show FractalView; |
| 20 | |
| 21 | /// Which bridge this build is using, for display. 'dart:ffi' or 'wasm'. |
| 22 | const 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 10h ago | 26 | /// 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. |
| 29 | bool get supportsIsolateParallelism => backend.supportsIsolateParallelism; |
| 30 | |
| 31 | /// What one unit of parallelism is called here — 'isolate' or 'worker'. |
| 32 | /// Purely for display. |
| 33 | String get parallelUnitName => backend.parallelUnitName; |
| Replace V with Nim 472eb49 nandithebull 11h ago | 34 | |
| 35 | /// Whether the bridge is ready to take calls. |
| 36 | bool 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. |
| 42 | Future<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. |
| 47 | void ensureInitialized() => backend.ensureInitialized(); |
| 48 | |
| 49 | /// Adds two integers in Nim. The trivial case, useful as a liveness check. |
| 50 | int 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. |
| 57 | String greet(String name) => backend.greet(name); |
| 58 | |
| 59 | /// Runs [greet] off the calling isolate where the backend supports it. |
| 60 | Future<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. |
| 66 | Uint8List renderBand(FractalView view, int y0, int y1) => |
| 67 | backend.renderBand(view, y0, y1); |
| 68 | |
| 69 | /// Renders the whole of [view] on the calling isolate. |
| 70 | Uint8List 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 10h ago | 75 | /// — 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 11h ago | 77 | Future<Uint8List> renderParallel(FractalView view, {int tiles = 4}) => |
| 78 | backend.renderParallel(view, tiles); |