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 wasm sample #841

Merged
merged 17 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# WASM HelloWorld Demo
daidr marked this conversation as resolved.
Show resolved Hide resolved

This recipe shows how to use WASM in Manifest V3.

To load WASM in Manifest V3, we need to use the `wasm-unsafe-eval` CSP directive ([Content Security Policy][0]).

## Running this extension

1. Clone this repository.
2. Load this directory in Chrome as an [unpacked extension][1].
3. Find the extension named "WASM Load Example - Helloworld (no-module)" and open console panel of the background service worker.

You will see the following output:

```
[from wasm] Inited.
[from wasm] Hello World!
[from wasm] Hello John
[from wasm] Hello John
```

## Build WASM locally

We have already built the WASM file for you. If you want to build it yourself, follow the steps below.

1. Install [Rust](https://www.rust-lang.org/install.html).

2. Install [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/).

```bash
cargo install wasm-pack
```

3. Build WASM.

```bash
cd wasm
wasm-pack build --target no-modules
```

[0]: https://developer.chrome.com/docs/extensions/mv3/mv3-migration/#content-security-policy
daidr marked this conversation as resolved.
Show resolved Hide resolved
[1]: https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable no-undef */

importScripts('./wasm/pkg/helloworld_demo.js');

chrome.runtime.onInstalled.addListener(() => {
runDemo();
});

async function runDemo() {
// Initialize the WASM module
await wasm_bindgen('./wasm/pkg/helloworld_demo_bg.wasm');

// Call the exported functions from the WASM module
wasm_bindgen.print();
wasm_bindgen.print_with_value('John');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "WASM Load Example - Helloworld (no-module)",
"version": "1.0",
"manifest_version": 3,
"description": "Inspect background service worker console for output",
"background": {
"service_worker": "background.js"
},
"content_security_policy": {
"extension_pages": "default-src 'self' 'wasm-unsafe-eval'"
}
}
daidr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "helloworld-demo"
version = "0.1.0"
edition = "2021"

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

[dependencies]
wasm-bindgen = "0.2.84"
web-sys = { version = "0.3.61", features = ['console'] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
extern crate wasm_bindgen;
daidr marked this conversation as resolved.
Show resolved Hide resolved

use wasm_bindgen::prelude::*;
use web_sys::console;

// will be called when the wasm module is loaded
// https://rustwasm.github.io/docs/wasm-bindgen/reference/attributes/on-rust-exports/start.html
#[wasm_bindgen(start)]
pub fn main() {
console::log_1(&"[from wasm] Inited.".into());
}

#[wasm_bindgen]
pub fn print() {
console::log_1(&"[from wasm] Hello World!".into());
}

#[wasm_bindgen]
pub fn print_with_value(value: &str) {
// with format! macro
console::log_1(&format!("[from wasm] Hello {}", value).into());
daidr marked this conversation as resolved.
Show resolved Hide resolved

// with 2-args log function
console::log_2(&"[from wasm] Hello ".into(), &value.into());
daidr marked this conversation as resolved.
Show resolved Hide resolved
}
57 changes: 57 additions & 0 deletions functional-samples/cookbook.wasm-helloworld-print/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# WASM HelloWorld Demo
daidr marked this conversation as resolved.
Show resolved Hide resolved

This recipe shows how to use WASM in Manifest V3.

To load WASM in Manifest V3, we need to use the `wasm-unsafe-eval` CSP directive ([Content Security Policy][0]).

## Running this extension

1. Clone this repository.
2. Load this directory in Chrome as an [unpacked extension][2].
3. Find the extension named "WASM Load Example - Helloworld" and open console panel of the background service worker.
daidr marked this conversation as resolved.
Show resolved Hide resolved

You will see the following output:

```
[from wasm] Inited.
[from wasm] Hello World!
[from wasm] Hello John
[from wasm] Hello John
```

## Build WASM locally

We have already built the WASM file for you. If you want to build it yourself, follow the steps below.

1. Install [Rust](https://www.rust-lang.org/install.html).

2. Install [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/).

```bash
cargo install wasm-pack
```

3. Build WASM.

```bash
cd wasm
wasm-pack build --target web
```

## Notes

- To import the script generated by `wasm-pack`, we need to [declare the service worker as an ES Module][1].

```diff
// manifest.json
...
"background": {
"service_worker": "background.js",
+ "type": "module"
},
...
```

[0]: https://developer.chrome.com/docs/extensions/mv3/mv3-migration/#content-security-policy
daidr marked this conversation as resolved.
Show resolved Hide resolved
[1]: https://developer.chrome.com/docs/extensions/mv3/mv3-migration/#man-sw
[2]: https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked
14 changes: 14 additions & 0 deletions functional-samples/cookbook.wasm-helloworld-print/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import init, { print, print_with_value } from './wasm/pkg/helloworld_demo.js';

chrome.runtime.onInstalled.addListener(() => {
runDemo();
});

async function runDemo() {
// Initialize the WASM module
await init();

// Call the exported functions from the WASM module
print();
print_with_value('John');
}
13 changes: 13 additions & 0 deletions functional-samples/cookbook.wasm-helloworld-print/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "WASM Load Example - Helloworld",
"version": "1.0",
"manifest_version": 3,
"description": "Inspect background service worker console for output",
"background": {
"service_worker": "background.js",
"type": "module"
},
"content_security_policy": {
"extension_pages": "default-src 'self' 'wasm-unsafe-eval'"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
Loading