This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move package:wasm to a subdirectory (#50)
* Move package:wasm to a sub directory * Add a README to the top level directory * Update github workflows to run in sub directory * Move working dir to defaults
- Loading branch information
1 parent
d81a6a6
commit 544b12b
Showing
43 changed files
with
78 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.