Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some examples for running WASI programs #351

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build]
target = "wasm32-unknown-unknown"

[target.'cfg(target_arch = "wasm32")']
[target.wasm32-unknown-unknown]
# Use "wasmer" for running tests when compiled to WebAssembly
runner = ["wasmer", "run"]
# This is needed so the module is compiled with atomics support (shared memory)
Expand Down
42 changes: 39 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,43 @@ jobs:
run: npm run build:dev
- name: Integration Tests
run: npm run test
- name: Build Examples

examples:
name: Build Examples
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Node
uses: actions/setup-node@v3
with:
node-version: 16
- name: Setup Rust
uses: dsherret/rust-toolchain-file@v1
- name: Install wasm-pack
uses: taiki-e/install-action@wasm-pack
- name: Install wasm-strip and wasm-opt
run: sudo apt-get update && sudo apt-get install -y wabt binaryen
- name: Rust Cache
uses: Swatinem/rust-cache@v2
- name: Install JS Dependencies
run: npm ci
- name: Build Package
run: npm run build
- name: Build the wasmer.sh Example
run: |
npm ci
npm run build
working-directory: "examples/wasmer.sh"
- name: Build the Markdown Editor Example
run: |
npm ci
npm run build
working-directory: "examples/markdown-editor"
- name: Build the (Improved) Markdown Editor Example
run: |
npm ci
npm run build
working-directory: "examples/markdown-editor-improved"

api-docs:
name: API Docs
Expand Down Expand Up @@ -94,13 +126,17 @@ jobs:
workflow-times:
name: Workflow Timings
runs-on: ubuntu-latest
needs: check
needs:
- check
- examples
steps:
- name: Time Reporter
uses: Michael-F-Bryan/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
jobs: Compile and Test
jobs: |
Compile and Test
Build Examples
message: |
Make sure you keep an eye on build times!

Expand Down
6 changes: 6 additions & 0 deletions examples/markdown-editor-improved/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Markdown Editor (Improved)

An improved version of [the original Markdown Editor][original] which uses a
Wasmer package instead of running a `*.wasm` file directly.

[original]: ../markdown-editor/
19 changes: 19 additions & 0 deletions examples/markdown-editor-improved/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wasmer Markdown Editor</title>
<script type="module" defer src="./index.ts"></script>
<link rel="stylesheet" href="./style.css">
</head>

<body>
<div class="editor-container">
<textarea id="markdown-input" placeholder="Type your Markdown here..."></textarea>
<iframe id="html-output"></iframe>
</div>
</body>

</html>
43 changes: 43 additions & 0 deletions examples/markdown-editor-improved/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { init, Wasmer, Command } from "@wasmer/sdk";

async function initialize() {
await init();
return await Wasmer.fromRegistry("wasmer-examples/markdown-renderer");
}

function debounce(func: (...args: any[]) => void, delay: number): (...args: any[]) => void {
let debounceTimer: ReturnType<typeof setTimeout>;

return function(...args: any[]) {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => func(...args), delay);
};
}

async function renderMarkdown(cmd: Command, markdown: string) {
const instance = await cmd.run();
const stdin = instance.stdin.getWriter();
const encoder = new TextEncoder();
await stdin.write(encoder.encode(markdown));
await stdin.close();

const result = await instance.wait();
return result.ok ? result.stdoutUtf8 : null;
}

async function main() {
const pkg = await initialize();
const output = document.getElementById("html-output") as HTMLIFrameElement;
const markdownInput = document.getElementById("markdown-input") as HTMLTextAreaElement;

const debouncedRender = debounce(async () => {
const renderedHtml = await renderMarkdown(pkg.entrypoint!, markdownInput.value);
if (renderedHtml) {
output.srcdoc = renderedHtml;
}
}, 500); // 500 milliseconds debounce period

markdownInput.addEventListener("input", debouncedRender);
}

main();
Loading
Loading