Skip to content

Commit

Permalink
add working test harness site
Browse files Browse the repository at this point in the history
  • Loading branch information
MingweiSamuel committed Sep 5, 2024
1 parent af58f64 commit 5b8ed84
Show file tree
Hide file tree
Showing 17 changed files with 1,425 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ members = [
"stageleft_tool",
"topolotree",
"variadics",
"wasm_test_site",
"website_playground",
]

Expand Down
2 changes: 2 additions & 0 deletions docs/src/pages/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ if (siteConfig.customFields.LOAD_PLAYGROUND === '1') {
}

playgroundJS.init();

window.test_hydroflow = playgroundJS.test_hydroflow;
}

import mermaid from "mermaid";
Expand Down
8 changes: 8 additions & 0 deletions wasm_test_site/.github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: cargo
directory: "/"
schedule:
interval: daily
time: "08:00"
open-pull-requests-limit: 10
6 changes: 6 additions & 0 deletions wasm_test_site/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log
27 changes: 27 additions & 0 deletions wasm_test_site/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "wasm_test_site"
version = "0.1.0"
authors = ["Mingwei Samuel <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
hydroflow = { path = "../hydroflow" }
wasm-bindgen = "0.2.84"
wasm-bindgen-futures = "0.4.43"
web-sys = { version = "0.3.51", features = [ "console" ] }

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = "0.1.7"

[dev-dependencies]
wasm-bindgen-test = "0.3.34"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
88 changes: 88 additions & 0 deletions wasm_test_site/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Go to `www/README.md`

---

<div align="center">

<h1><code>wasm-pack-template</code></h1>

<strong>A template for kick starting a Rust and WebAssembly project using <a href="https://github.com/rustwasm/wasm-pack">wasm-pack</a>.</strong>

<p>
<a href="https://travis-ci.org/rustwasm/wasm-pack-template"><img src="https://img.shields.io/travis/rustwasm/wasm-pack-template.svg?style=flat-square" alt="Build Status" /></a>
</p>

<h3>
<a href="https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html">Tutorial</a>
<span> | </span>
<a href="https://discordapp.com/channels/442252698964721669/443151097398296587">Chat</a>
</h3>

<sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub>
</div>

## About

[**📚 Read this template tutorial! 📚**][template-docs]

This template is designed for compiling Rust libraries into WebAssembly and
publishing the resulting package to NPM.

Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other
templates and usages of `wasm-pack`.

[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html
[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html

## 🚴 Usage

### 🐑 Use `cargo generate` to Clone this Template

[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate)

```
cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project
cd my-project
```

### 🛠️ Build with `wasm-pack build`

```
wasm-pack build
```

### 🔬 Test in Headless Browsers with `wasm-pack test`

```
wasm-pack test --headless --firefox
```

### 🎁 Publish to NPM with `wasm-pack publish`

```
wasm-pack publish
```

## 🔋 Batteries Included

* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating
between WebAssembly and JavaScript.
* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook)
for logging panic messages to the developer console.
* `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you

## License

Licensed under either of

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.
31 changes: 31 additions & 0 deletions wasm_test_site/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use wasm_bindgen::prelude::*;

fn log(string: impl AsRef<str>) {
web_sys::console::log_1(&JsValue::from_str(string.as_ref()));
}

#[wasm_bindgen]
pub fn greet() {
console_error_panic_hook::set_once();

log("Hello, wasm_test_site!");
}

#[wasm_bindgen]
pub fn test_hydroflow() -> web_sys::js_sys::Promise {
console_error_panic_hook::set_once();

let mut df = hydroflow::hydroflow_syntax! {
// https://hydro.run/docs/hydroflow/quickstart/example_1_simplest
source_iter(0..10) -> for_each(|n| log(format!("Hello {}", n)));
};

wasm_bindgen_futures::future_to_promise(async move {
let work_done = df.run_available_async().await;
Ok(if work_done {
JsValue::TRUE
} else {
JsValue::FALSE
})
})
}
13 changes: 13 additions & 0 deletions wasm_test_site/tests/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Test suite for the Web and headless browsers.
#![cfg(target_arch = "wasm32")]

extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
}
1 change: 1 addition & 0 deletions wasm_test_site/www/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/
79 changes: 79 additions & 0 deletions wasm_test_site/www/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
Run once to init:
```
npm ci
```
Run to rebuild:
```
npm run wasm
npm run dev
```

---

<div align="center">

<h1><code>create-wasm-app</code></h1>

<strong>An <code>npm init</code> template for kick starting a project that uses NPM packages containing Rust-generated WebAssembly and bundles them with Webpack.</strong>

<p>
<a href="https://travis-ci.org/rustwasm/create-wasm-app"><img src="https://img.shields.io/travis/rustwasm/create-wasm-app.svg?style=flat-square" alt="Build Status" /></a>
</p>

<h3>
<a href="#usage">Usage</a>
<span> | </span>
<a href="https://discordapp.com/channels/442252698964721669/443151097398296587">Chat</a>
</h3>

<sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub>
</div>

## About

This template is designed for depending on NPM packages that contain
Rust-generated WebAssembly and using them to create a Website.

* Want to create an NPM package with Rust and WebAssembly? [Check out
`wasm-pack-template`.](https://github.com/rustwasm/wasm-pack-template)
* Want to make a monorepo-style Website without publishing to NPM? Check out
[`rust-webpack-template`](https://github.com/rustwasm/rust-webpack-template)
and/or
[`rust-parcel-template`](https://github.com/rustwasm/rust-parcel-template).

## 🚴 Usage

```
npm init wasm-app
```

## 🔋 Batteries Included

- `.gitignore`: ignores `node_modules`
- `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you
- `README.md`: the file you are reading now!
- `index.html`: a bare bones html document that includes the webpack bundle
- `index.js`: example js file with a comment showing how to import and use a wasm pkg
- `package.json` and `package-lock.json`:
- pulls in devDependencies for using webpack:
- [`webpack`](https://www.npmjs.com/package/webpack)
- [`webpack-cli`](https://www.npmjs.com/package/webpack-cli)
- [`webpack-dev-server`](https://www.npmjs.com/package/webpack-dev-server)
- defines a `start` script to run `webpack-dev-server`
- `webpack.config.js`: configuration file for bundling your js with webpack

## License

Licensed under either of

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.
10 changes: 10 additions & 0 deletions wasm_test_site/www/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello wasm-pack!</title>
</head>
<body>
<script type="module" src="./index.ts"></script>
</body>
</html>
4 changes: 4 additions & 0 deletions wasm_test_site/www/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import init, { greet, test_hydroflow } from "wasm_test_site";
let { memory } = await init();
greet();
test_hydroflow();
Loading

0 comments on commit 5b8ed84

Please sign in to comment.