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

Commit

Permalink
Move package:wasm to a subdirectory (#50)
Browse files Browse the repository at this point in the history
* 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
liamappelbe authored Oct 18, 2021
1 parent d81a6a6 commit 544b12b
Show file tree
Hide file tree
Showing 43 changed files with 78 additions and 63 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
68 changes: 5 additions & 63 deletions README.md
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.
67 changes: 67 additions & 0 deletions wasm/README.md
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.

0 comments on commit 544b12b

Please sign in to comment.