diff --git a/.github/workflows/test-package.yml b/.github/workflows/test-package.yml index 074b428..a48cd8b 100644 --- a/.github/workflows/test-package.yml +++ b/.github/workflows/test-package.yml @@ -18,6 +18,9 @@ jobs: strategy: matrix: sdk: [ dev ] + defaults: + run: + working-directory: wasm steps: - uses: actions/checkout@v2.3.4 - uses: dart-lang/setup-dart@v1.0 @@ -58,6 +61,9 @@ jobs: matrix: os: [ ubuntu-latest, macos-latest ] sdk: [ 2.12.0, dev ] + defaults: + run: + working-directory: wasm steps: - uses: dart-lang/setup-dart@v1.0 with: diff --git a/README.md b/README.md index 1fc04c4..57224a6 100644 --- a/README.md +++ b/README.md @@ -1,67 +1,9 @@ -Provides utilities for loading and running WASM modules. +These packages provide utilities for loading and running WASM modules. -Built on top of the [Wasmer](https://github.com/wasmerio/wasmer) runtime. +## [wasm](https://github.com/dart-lang/wasm/blob/main/wasm/README.md) -Flutter and web are **not supported** at the moment. Stay tuned though ;) +Runs WASM modules in Dart native. -## Setup +## flutter_wasm (coming soon) -Start by installing the tools needed to build the Wasmer runtime: - - * Install the [Rust SDK]. - - * On Windows, to get `link.exe`, install the [Visual Studio build tools] - with the "Desktop development with C++"-option selected. - -[Rust SDK]: https://www.rust-lang.org/tools/install -[Visual Studio build tools]: https://visualstudio.microsoft.com/visual-cpp-build-tools/ - -## Basic Usage - -As a simple example, we'll try to call a simple C function from Dart using -`package:wasm`. For a more detailed example that uses WASI, check out the -example directory. - -1. First create new Dart app folder: `dart create wasmtest` - -1. Add a dependency to package `wasm` in `pubspec.yaml` and run `dart pub get` - -1. Next run `dart run wasm:setup` to build the Wasmer runtime. This will take a few minutes. - -1. Then add a new file `square.cc` with the following contents: - - ```c++ - extern "C" int square(int n) { return n * n; } - ``` - -1. We can compile this C++ code to WASM using a recent version of the -[clang compiler](https://clang.llvm.org/get_started.html): - - ```bash - clang --target=wasm32 -nostdlib "-Wl,--export-all" "-Wl,--no-entry" -o square.wasm square.cc - ``` - -1. Replace the contents of your Dart program (`bin/wasmtest.dart`) with: - - ```dart - import 'dart:io'; - import 'package:wasm/wasm.dart'; - - void main() { - final data = File('square.wasm').readAsBytesSync(); - final mod = WasmModule(data); - print(mod.describe()); - final inst = mod.builder().build(); - final square = inst.lookupFunction('square'); - print(square(12)); - } - ``` - -1. Run the Dart program: `dart run`. This should print: - - ``` - export memory: memory - export function: int32 square(int32) - - 144 - ``` +Runs WASM modules in Flutter. diff --git a/.gitignore b/wasm/.gitignore similarity index 100% rename from .gitignore rename to wasm/.gitignore diff --git a/CHANGELOG.md b/wasm/CHANGELOG.md similarity index 100% rename from CHANGELOG.md rename to wasm/CHANGELOG.md diff --git a/wasm/README.md b/wasm/README.md new file mode 100644 index 0000000..1fc04c4 --- /dev/null +++ b/wasm/README.md @@ -0,0 +1,67 @@ +Provides utilities for loading and running WASM modules. + +Built on top of the [Wasmer](https://github.com/wasmerio/wasmer) runtime. + +Flutter and web are **not supported** at the moment. Stay tuned though ;) + +## Setup + +Start by installing the tools needed to build the Wasmer runtime: + + * Install the [Rust SDK]. + + * On Windows, to get `link.exe`, install the [Visual Studio build tools] + with the "Desktop development with C++"-option selected. + +[Rust SDK]: https://www.rust-lang.org/tools/install +[Visual Studio build tools]: https://visualstudio.microsoft.com/visual-cpp-build-tools/ + +## Basic Usage + +As a simple example, we'll try to call a simple C function from Dart using +`package:wasm`. For a more detailed example that uses WASI, check out the +example directory. + +1. First create new Dart app folder: `dart create wasmtest` + +1. Add a dependency to package `wasm` in `pubspec.yaml` and run `dart pub get` + +1. Next run `dart run wasm:setup` to build the Wasmer runtime. This will take a few minutes. + +1. Then add a new file `square.cc` with the following contents: + + ```c++ + extern "C" int square(int n) { return n * n; } + ``` + +1. We can compile this C++ code to WASM using a recent version of the +[clang compiler](https://clang.llvm.org/get_started.html): + + ```bash + clang --target=wasm32 -nostdlib "-Wl,--export-all" "-Wl,--no-entry" -o square.wasm square.cc + ``` + +1. Replace the contents of your Dart program (`bin/wasmtest.dart`) with: + + ```dart + import 'dart:io'; + import 'package:wasm/wasm.dart'; + + void main() { + final data = File('square.wasm').readAsBytesSync(); + final mod = WasmModule(data); + print(mod.describe()); + final inst = mod.builder().build(); + final square = inst.lookupFunction('square'); + print(square(12)); + } + ``` + +1. Run the Dart program: `dart run`. This should print: + + ``` + export memory: memory + export function: int32 square(int32) + + 144 + ``` diff --git a/bin/.gitignore b/wasm/bin/.gitignore similarity index 100% rename from bin/.gitignore rename to wasm/bin/.gitignore diff --git a/bin/Cargo.toml b/wasm/bin/Cargo.toml similarity index 100% rename from bin/Cargo.toml rename to wasm/bin/Cargo.toml diff --git a/bin/finalizers.c b/wasm/bin/finalizers.c similarity index 100% rename from bin/finalizers.c rename to wasm/bin/finalizers.c diff --git a/bin/module.g.def b/wasm/bin/module.g.def similarity index 100% rename from bin/module.g.def rename to wasm/bin/module.g.def diff --git a/bin/setup.dart b/wasm/bin/setup.dart similarity index 100% rename from bin/setup.dart rename to wasm/bin/setup.dart diff --git a/bin/wasmer.rs b/wasm/bin/wasmer.rs similarity index 100% rename from bin/wasmer.rs rename to wasm/bin/wasmer.rs diff --git a/example/README.md b/wasm/example/README.md similarity index 100% rename from example/README.md rename to wasm/example/README.md diff --git a/example/brotli.dart b/wasm/example/brotli.dart similarity index 100% rename from example/brotli.dart rename to wasm/example/brotli.dart diff --git a/example/libbrotli.wasm b/wasm/example/libbrotli.wasm similarity index 100% rename from example/libbrotli.wasm rename to wasm/example/libbrotli.wasm diff --git a/example/lipsum.txt b/wasm/example/lipsum.txt similarity index 100% rename from example/lipsum.txt rename to wasm/example/lipsum.txt diff --git a/lib/src/module.dart b/wasm/lib/src/module.dart similarity index 100% rename from lib/src/module.dart rename to wasm/lib/src/module.dart diff --git a/lib/src/runtime.dart b/wasm/lib/src/runtime.dart similarity index 100% rename from lib/src/runtime.dart rename to wasm/lib/src/runtime.dart diff --git a/lib/src/runtime.g.dart b/wasm/lib/src/runtime.g.dart similarity index 100% rename from lib/src/runtime.g.dart rename to wasm/lib/src/runtime.g.dart diff --git a/lib/src/shared.dart b/wasm/lib/src/shared.dart similarity index 100% rename from lib/src/shared.dart rename to wasm/lib/src/shared.dart diff --git a/lib/src/wasm_error.dart b/wasm/lib/src/wasm_error.dart similarity index 100% rename from lib/src/wasm_error.dart rename to wasm/lib/src/wasm_error.dart diff --git a/lib/src/wasmer_api.dart b/wasm/lib/src/wasmer_api.dart similarity index 100% rename from lib/src/wasmer_api.dart rename to wasm/lib/src/wasmer_api.dart diff --git a/lib/src/wasmer_api.g.dart b/wasm/lib/src/wasmer_api.g.dart similarity index 100% rename from lib/src/wasmer_api.g.dart rename to wasm/lib/src/wasmer_api.g.dart diff --git a/lib/wasm.dart b/wasm/lib/wasm.dart similarity index 100% rename from lib/wasm.dart rename to wasm/lib/wasm.dart diff --git a/pubspec.yaml b/wasm/pubspec.yaml similarity index 100% rename from pubspec.yaml rename to wasm/pubspec.yaml diff --git a/test/basic_test.dart b/wasm/test/basic_test.dart similarity index 100% rename from test/basic_test.dart rename to wasm/test/basic_test.dart diff --git a/test/corrupted_error_test.dart b/wasm/test/corrupted_error_test.dart similarity index 100% rename from test/corrupted_error_test.dart rename to wasm/test/corrupted_error_test.dart diff --git a/test/fn_call_error_test.dart b/wasm/test/fn_call_error_test.dart similarity index 100% rename from test/fn_call_error_test.dart rename to wasm/test/fn_call_error_test.dart diff --git a/test/fn_import_exception_test.dart b/wasm/test/fn_import_exception_test.dart similarity index 100% rename from test/fn_import_exception_test.dart rename to wasm/test/fn_import_exception_test.dart diff --git a/test/fn_import_test.dart b/wasm/test/fn_import_test.dart similarity index 100% rename from test/fn_import_test.dart rename to wasm/test/fn_import_test.dart diff --git a/test/globals_test.dart b/wasm/test/globals_test.dart similarity index 100% rename from test/globals_test.dart rename to wasm/test/globals_test.dart diff --git a/test/hello_wasi_test.dart b/wasm/test/hello_wasi_test.dart similarity index 100% rename from test/hello_wasi_test.dart rename to wasm/test/hello_wasi_test.dart diff --git a/test/hello_world_test.dart b/wasm/test/hello_world_test.dart similarity index 100% rename from test/hello_world_test.dart rename to wasm/test/hello_world_test.dart diff --git a/test/import_error_test.dart b/wasm/test/import_error_test.dart similarity index 100% rename from test/import_error_test.dart rename to wasm/test/import_error_test.dart diff --git a/test/memory_error_test.dart b/wasm/test/memory_error_test.dart similarity index 100% rename from test/memory_error_test.dart rename to wasm/test/memory_error_test.dart diff --git a/test/memory_test.dart b/wasm/test/memory_test.dart similarity index 100% rename from test/memory_test.dart rename to wasm/test/memory_test.dart diff --git a/test/numerics_test.dart b/wasm/test/numerics_test.dart similarity index 100% rename from test/numerics_test.dart rename to wasm/test/numerics_test.dart diff --git a/test/test_shared.dart b/wasm/test/test_shared.dart similarity index 100% rename from test/test_shared.dart rename to wasm/test/test_shared.dart diff --git a/test/void_test.dart b/wasm/test/void_test.dart similarity index 100% rename from test/void_test.dart rename to wasm/test/void_test.dart diff --git a/test/wasi_error_test.dart b/wasm/test/wasi_error_test.dart similarity index 100% rename from test/wasi_error_test.dart rename to wasm/test/wasi_error_test.dart diff --git a/tool/generate_ffi_boilerplate.py b/wasm/tool/generate_ffi_boilerplate.py similarity index 100% rename from tool/generate_ffi_boilerplate.py rename to wasm/tool/generate_ffi_boilerplate.py diff --git a/tool/module.def.t b/wasm/tool/module.def.t similarity index 100% rename from tool/module.def.t rename to wasm/tool/module.def.t diff --git a/tool/runtime_template.dart.t b/wasm/tool/runtime_template.dart.t similarity index 100% rename from tool/runtime_template.dart.t rename to wasm/tool/runtime_template.dart.t diff --git a/tool/wasmer_api_template.dart.t b/wasm/tool/wasmer_api_template.dart.t similarity index 100% rename from tool/wasmer_api_template.dart.t rename to wasm/tool/wasmer_api_template.dart.t