diff --git a/wasm/CHANGELOG.md b/wasm/CHANGELOG.md index 0f58aa2..aeda6b3 100644 --- a/wasm/CHANGELOG.md +++ b/wasm/CHANGELOG.md @@ -6,6 +6,8 @@ - Add options to setup.dart for configuring the build. - Add Module.serialize and Module.deserialize. - Add bin/precompile.dart, which compiles and serializes wasm modules. +- Add `WasmModule.compileAsync` and `WasmInstanceBuilder.buildAsync`, so that + the web API can be supported in the future. ## 0.1.0+1 diff --git a/wasm/lib/src/module.dart b/wasm/lib/src/module.dart index 9c1862b..f0d3744 100644 --- a/wasm/lib/src/module.dart +++ b/wasm/lib/src/module.dart @@ -21,6 +21,10 @@ class WasmModule { /// Deserialize a module. See [WasmModule.serialize] for more details. WasmModule.deserialize(Uint8List data) : this._(data, true); + /// Asynchronously compile a module. + static Future compileAsync(Uint8List data) async => + Future(() => WasmModule(data)); + /// Returns a [WasmInstanceBuilder] that is used to add all the imports that /// the module needs before instantiating it. WasmInstanceBuilder builder() => WasmInstanceBuilder._(this); @@ -223,6 +227,9 @@ class WasmInstanceBuilder { } return WasmInstance._(_module, _importOwner, _imports, _wasiEnv); } + + /// Asynchronously build the module instance. + Future buildAsync() async => Future(build); } // TODO: should not be required once the min supported Dart SDK includes diff --git a/wasm/test/async_test.dart b/wasm/test/async_test.dart new file mode 100644 index 0000000..22dbfa9 --- /dev/null +++ b/wasm/test/async_test.dart @@ -0,0 +1,33 @@ +// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Test that we can load a wasm module, find a function, and call it. +import 'dart:typed_data'; + +import 'package:test/test.dart'; +import 'package:wasm/wasm.dart'; + +void main() { + test('async compilation', () async { + // int64_t square(int64_t n) { return n * n; } + final data = Uint8List.fromList([ + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, // + 0x01, 0x7e, 0x01, 0x7e, 0x03, 0x02, 0x01, 0x00, 0x04, 0x05, 0x01, 0x70, + 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x02, 0x06, 0x08, 0x01, 0x7f, + 0x01, 0x41, 0x80, 0x88, 0x04, 0x0b, 0x07, 0x13, 0x02, 0x06, 0x6d, 0x65, + 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x06, 0x73, 0x71, 0x75, 0x61, 0x72, + 0x65, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x00, + 0x7e, 0x0b, + ]); + + final mod = await WasmModule.compileAsync(data); + final inst = await mod.builder().buildAsync(); + final fn = inst.lookupFunction('square'); + final n = fn(1234) as int; + + expect(n, 1234 * 1234); + + expect(inst.lookupFunction('not_a_function'), isNull); + }); +}