Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit e45e5fb

Browse files
authored
Async compilation and instantiation (#62)
* Async compilation and instantiation * Fix analysis error
1 parent e3d7d3d commit e45e5fb

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

wasm/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- Add options to setup.dart for configuring the build.
77
- Add Module.serialize and Module.deserialize.
88
- Add bin/precompile.dart, which compiles and serializes wasm modules.
9+
- Add `WasmModule.compileAsync` and `WasmInstanceBuilder.buildAsync`, so that
10+
the web API can be supported in the future.
911

1012
## 0.1.0+1
1113

wasm/lib/src/module.dart

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class WasmModule {
2121
/// Deserialize a module. See [WasmModule.serialize] for more details.
2222
WasmModule.deserialize(Uint8List data) : this._(data, true);
2323

24+
/// Asynchronously compile a module.
25+
static Future<WasmModule> compileAsync(Uint8List data) async =>
26+
Future<WasmModule>(() => WasmModule(data));
27+
2428
/// Returns a [WasmInstanceBuilder] that is used to add all the imports that
2529
/// the module needs before instantiating it.
2630
WasmInstanceBuilder builder() => WasmInstanceBuilder._(this);
@@ -223,6 +227,9 @@ class WasmInstanceBuilder {
223227
}
224228
return WasmInstance._(_module, _importOwner, _imports, _wasiEnv);
225229
}
230+
231+
/// Asynchronously build the module instance.
232+
Future<WasmInstance> buildAsync() async => Future<WasmInstance>(build);
226233
}
227234

228235
// TODO: should not be required once the min supported Dart SDK includes

wasm/test/async_test.dart

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Test that we can load a wasm module, find a function, and call it.
6+
import 'dart:typed_data';
7+
8+
import 'package:test/test.dart';
9+
import 'package:wasm/wasm.dart';
10+
11+
void main() {
12+
test('async compilation', () async {
13+
// int64_t square(int64_t n) { return n * n; }
14+
final data = Uint8List.fromList([
15+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, //
16+
0x01, 0x7e, 0x01, 0x7e, 0x03, 0x02, 0x01, 0x00, 0x04, 0x05, 0x01, 0x70,
17+
0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x02, 0x06, 0x08, 0x01, 0x7f,
18+
0x01, 0x41, 0x80, 0x88, 0x04, 0x0b, 0x07, 0x13, 0x02, 0x06, 0x6d, 0x65,
19+
0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x06, 0x73, 0x71, 0x75, 0x61, 0x72,
20+
0x65, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x00,
21+
0x7e, 0x0b,
22+
]);
23+
24+
final mod = await WasmModule.compileAsync(data);
25+
final inst = await mod.builder().buildAsync();
26+
final fn = inst.lookupFunction('square');
27+
final n = fn(1234) as int;
28+
29+
expect(n, 1234 * 1234);
30+
31+
expect(inst.lookupFunction('not_a_function'), isNull);
32+
});
33+
}

0 commit comments

Comments
 (0)