open static method
Loads and instantiates the wasm module. Call once per app; the result can be reused for any number of shots/calls.
scriptUrl defaults to where Flutter web serves this package's own
bundled asset (declared under flutter.assets in pubspec.yaml) —
see assets/wasm/ in the package root. Override it if you're loading
a differently-built or differently-hosted artifact (e.g. in a plain
dart test -p chrome run, which doesn't go through Flutter's asset
pipeline).
Implementation
static Future<BcLibCWeb> open({
String scriptUrl =
'assets/packages/bclibc_flutter/assets/wasm/bclibc_ffi.js',
String globalName = 'bclibc_ffi',
}) async {
final module = await _loadModule(scriptUrl, globalName);
final layoutBuf = module.callMethodVarArgs<JSNumber>('_malloc'.toJS, [
(_Layout.fieldCount * 4).toJS,
]).toDartInt;
final n = module.callMethodVarArgs<JSNumber>('_BCLIBCFFI_get_layout'.toJS, [
layoutBuf.toJS,
_Layout.fieldCount.toJS,
]).toDartInt;
if (n != _Layout.fieldCount) {
module.callMethodVarArgs<JSAny?>('_free'.toJS, [layoutBuf.toJS]);
throw StateError(
'BCLIBCFFI_get_layout returned $n fields, expected ${_Layout.fieldCount} '
'(bclibc_ffi_web.dart is out of sync with bclibc_ffi.cpp)',
);
}
final bd = _heap(module);
final values = [
for (var i = 0; i < n; i++) bd.getInt32(layoutBuf + i * 4, Endian.little),
];
module.callMethodVarArgs<JSAny?>('_free'.toJS, [layoutBuf.toJS]);
return BcLibCWeb._(module, _Layout(values));
}