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 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
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
_archive
third-party
node_modules

# These are autogenerated files that we shouldn't lint
functional-samples/cookbook.wasm-helloworld-print/wasm/pkg
functional-samples/cookbook.wasm-helloworld-print-nomodule/wasm/pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Using WASM in Manifest V3

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]).

## Overview

### Running this extension

1. Clone this repository.
2. Load this directory in Chrome as an unpacked extension.
3. Find the extension named "WASM Load Example - Helloworld (no-modules)" and inspect the service worker.

You will see the following output:

```
[from wasm] Inited.
[from wasm] Hello World!
[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/manifest/content_security_policy/
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-modules)",
"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'"
}
}
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,37 @@
declare namespace wasm_bindgen {
/* tslint:disable */
/* eslint-disable */
/**
*/
export function main(): void;
/**
*/
export function print(): void;
/**
* @param {string} value
*/
export function print_with_value(value: string): void;

}

declare type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;

declare interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly main: () => void;
readonly print: () => void;
readonly print_with_value: (a: number, b: number) => void;
readonly __wbindgen_malloc: (a: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number) => number;
readonly __wbindgen_start: () => void;
}

/**
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
* for everything else, calls `WebAssembly.instantiate` directly.
*
* @param {InitInput | Promise<InitInput>} module_or_path
*
* @returns {Promise<InitOutput>}
*/
declare function wasm_bindgen (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput>;
Loading