-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53c1231
commit c7d1b56
Showing
27 changed files
with
800 additions
and
193 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
defaults: | ||
run: | ||
shell: bash | ||
|
||
# Cancel any in-flight jobs for the same PR/branch so there's only one active | ||
# at a time | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
test: | ||
name: Test | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
os: [ubuntu-latest] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
submodules: true | ||
- name: Install Rust | ||
run: rustup update stable --no-self-update && rustup default stable | ||
- name: Install wasm32-unknown-unknown target | ||
run: rustup target add wasm32-unknown-unknown | ||
- name: Install wasm32-wasi target | ||
run: rustup target add wasm32-wasi | ||
|
||
- run: | | ||
curl https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-16/wasi-sdk-16.0-linux.tar.gz -L | tar xzvf - | ||
echo "WASI_SDK_PATH=`pwd`/wasi-sdk-16.0" >> $GITHUB_ENV | ||
if : matrix.os == 'ubuntu-latest' | ||
- run: | | ||
curl https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-16/wasi-sdk-16.0-macos.tar.gz -L | tar xzvf - | ||
echo "WASI_SDK_PATH=`pwd`/wasi-sdk-16.0" >> $GITHUB_ENV | ||
if : matrix.os == 'macos-latest' | ||
- run: | | ||
curl https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-16/wasi-sdk-16.0-mingw.tar.gz -L | tar xzvf - | ||
echo "WASI_SDK_PATH=`pwd`/wasi-sdk-16.0" >> $GITHUB_ENV | ||
if : matrix.os == 'windows-latest' | ||
- name: "Install Binaryen (linux)" | ||
run: | | ||
set -x | ||
export BINARYEN_VERSION=105 | ||
curl -sS -L "https://github.com/WebAssembly/binaryen/releases/download/version_${BINARYEN_VERSION}/binaryen-version_${BINARYEN_VERSION}-x86_64-linux.tar.gz" | tar xzf - && | ||
mv "$PWD/binaryen-version_${BINARYEN_VERSION}/bin/wasm-opt" "$PWD/binaryen-version_${BINARYEN_VERSION}/bin/wasm-opt-manual" | ||
echo "$PWD/binaryen-version_${BINARYEN_VERSION}/bin" >> $GITHUB_PATH | ||
- name: "Install wasi-sdk-19 (linux)" | ||
run: | | ||
set -x | ||
curl -sS -L -O https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-19/wasi-sdk-19.0-linux.tar.gz | ||
tar xf wasi-sdk-19.0-linux.tar.gz | ||
sudo mkdir -p /opt/wasi-sdk | ||
sudo mv wasi-sdk-19.0/* /opt/wasi-sdk/ | ||
- uses: actions/setup-node@v2 | ||
with: | ||
node-version: '16' | ||
- name: Install NPM packages | ||
run: npm install | ||
|
||
- name: Build | ||
run: npm run build | ||
|
||
- name: Test | ||
run: npm run test | ||
|
||
rustfmt: | ||
name: Rustfmt | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Install Rust | ||
run: rustup update stable && rustup default stable && rustup component add rustfmt | ||
- name: Format source code | ||
run: cargo fmt -- --check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
/obj/ | ||
/deps/ | ||
/dist/ | ||
/test/output/ | ||
/node_modules/ | ||
/target/ | ||
/target/ | ||
/lib/ | ||
/crates/wasmtime-test | ||
/hello.* | ||
/test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[submodule "deps/spidermonkey-wasi-embedding"] | ||
path = deps/spidermonkey-wasi-embedding | ||
url = git@github.com:fastly/spidermonkey-wasi-embedding | ||
url = https://github.com/fastly/spidermonkey-wasi-embedding.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Contributor Covenant Code of Conduct | ||
|
||
*Note*: this Code of Conduct pertains to individuals' behavior. Please also see the [Organizational Code of Conduct][OCoC]. | ||
|
||
## Our Pledge | ||
|
||
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. | ||
|
||
## Our Standards | ||
|
||
Examples of behavior that contributes to creating a positive environment include: | ||
|
||
* Using welcoming and inclusive language | ||
* Being respectful of differing viewpoints and experiences | ||
* Gracefully accepting constructive criticism | ||
* Focusing on what is best for the community | ||
* Showing empathy towards other community members | ||
|
||
Examples of unacceptable behavior by participants include: | ||
|
||
* The use of sexualized language or imagery and unwelcome sexual attention or advances | ||
* Trolling, insulting/derogatory comments, and personal or political attacks | ||
* Public or private harassment | ||
* Publishing others' private information, such as a physical or electronic address, without explicit permission | ||
* Other conduct which could reasonably be considered inappropriate in a professional setting | ||
|
||
## Our Responsibilities | ||
|
||
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. | ||
|
||
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. | ||
|
||
## Scope | ||
|
||
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. | ||
|
||
## Enforcement | ||
|
||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the Bytecode Alliance CoC team at [[email protected]](mailto:[email protected]). The CoC team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The CoC team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. | ||
|
||
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the Bytecode Alliance's leadership. | ||
|
||
## Attribution | ||
|
||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] | ||
|
||
[OCoC]: https://github.com/bytecodealliance/wasmtime/blob/main/ORG_CODE_OF_CONDUCT.md | ||
[homepage]: https://www.contributor-covenant.org | ||
[version]: https://www.contributor-covenant.org/version/1/4/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
# ComponentizeJS Example | ||
|
||
### Creating the Component | ||
|
||
Given a world that descibes a component interface: | ||
|
||
hello.wit | ||
``` | ||
default world hello { | ||
export hello: func(name: string) -> string | ||
} | ||
``` | ||
|
||
Write a JS file that adheres to the same interface: | ||
|
||
hello.js | ||
```js | ||
export function hello (name) { | ||
return `Hello ${name}`; | ||
} | ||
``` | ||
|
||
The component can then be built with the `componentize` API: | ||
|
||
```js | ||
import { componentize } from '@bytecodealliance/componentize-js'; | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
|
||
const jsSource = await readFile('hello.js', 'utf8'); | ||
const witSource = await readFile('hello.wit', 'utf8'); | ||
|
||
const { component } = await componentize(jsSource, witSource); | ||
|
||
await writeFile('hello.component.wasm', component); | ||
``` | ||
|
||
### Running the Component in Wasmtime | ||
|
||
Cargo.toml | ||
```toml | ||
[package] | ||
name = "wasmtime-test" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[workspace] | ||
|
||
[dependencies] | ||
anyhow = "1.0.65" | ||
async-std = { version = "1.12.0", features = ["attributes"] } | ||
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", features = ["component-model"], branch = 'release-6.0.0' } | ||
host = { git = "https://github.com/bytecodealliance/preview2-prototyping" } | ||
wasi-common = { git = "https://github.com/bytecodealliance/preview2-prototyping" } | ||
wasi-cap-std-sync = { git = "https://github.com/bytecodealliance/preview2-prototyping" } | ||
``` | ||
|
||
src/main.rs | ||
```rs | ||
use anyhow::Result; | ||
use host::add_to_linker; | ||
use wasi_cap_std_sync::WasiCtxBuilder; | ||
use wasmtime::{ | ||
component::{Component, Linker}, | ||
Config, Engine, Store, WasmBacktraceDetails, | ||
}; | ||
|
||
wasmtime::component::bindgen!({ | ||
world: "hello", | ||
path: "../hello.wit", | ||
async: true | ||
}); | ||
|
||
#[async_std::main] | ||
async fn main() -> Result<()> { | ||
let mut config = Config::new(); | ||
config.cache_config_load_default().unwrap(); | ||
config.wasm_backtrace_details(WasmBacktraceDetails::Enable); | ||
config.wasm_component_model(true); | ||
config.async_support(true); | ||
|
||
let engine = Engine::new(&config).unwrap(); | ||
let mut linker = Linker::new(&engine); | ||
add_to_linker(&mut linker, |x| x).unwrap(); | ||
|
||
let mut store = Store::new( | ||
&engine, | ||
WasiCtxBuilder::new() | ||
.inherit_stdin() | ||
.inherit_stdout() | ||
.build(), | ||
); | ||
|
||
let component = Component::from_file(&engine, "../hello.component.wasm").unwrap(); | ||
|
||
// after getting the component, we can instantiate a markdown instance. | ||
let (instance, _instance) = Hello::instantiate_async(&mut store, &component, &linker).await?; | ||
let res = instance.call_hello(&mut store, "ComponentizeJS").await?; | ||
println!(res); | ||
Ok(()) | ||
} | ||
``` | ||
|
||
Building and running the binary should print the result: | ||
|
||
``` | ||
cargo build --release | ||
./target/release/wasmtime-test | ||
> Hello CompontizeJS | ||
``` | ||
|
||
### Running the Component in Node.js | ||
|
||
To run the component in Node.js, we need to first transpile the component with `jco`: | ||
|
||
``` | ||
npm install -g @bytecodealliance/jco | ||
``` | ||
|
||
Transpile the component: | ||
|
||
``` | ||
jco transpile hello.component.wasm -o hello --map 'wasi-*=@bytecodealliance/preview2-shim/*' | ||
``` | ||
|
||
The custom WASI mapping argument allows us to direct the WASI component imports to the experimental JS WASI shim. | ||
|
||
We can install this shim itself from npm as well: | ||
|
||
``` | ||
npm install @bytecodealliance/preview2-shim | ||
``` | ||
|
||
To test the component: | ||
|
||
``` | ||
node -e "import('./hello/hello.component.js').then(m => console.log(m.hello('ComponentizeJS')))" | ||
> Hello ComponentizeJS | ||
``` |
Oops, something went wrong.