Skip to content

Commit

Permalink
Merge #2460
Browse files Browse the repository at this point in the history
2460: Added Wasmer Js API r=syrusakbary a=syrusakbary

<!-- 
Prior to submitting a PR, review the CONTRIBUTING.md document for recommendations on how to test:
https://github.com/wasmerio/wasmer/blob/master/CONTRIBUTING.md#pull-requests

-->

# Description

This PR adds support for compiling Wasmer into JS/Wasm via wasm-bindgen, so 3rd party dependencies can use Wasmer to target the web also.

Things working:
* [x] Type introspection: we have to decide how to do it, either run our own parser on the data or rely on the future APIs for introspect Wasm types (this APIs are yet available in the browser, but a proposal is on the way: [wasm-js-types proposal](https://github.com/WebAssembly/js-types)).
* [x] Wasm-pack wasmer-js tests
* [x] Assert 0 warnings in lint
* [x] Allow for custom JS errors (via `RuntimeError`)
* Externals
   * [x] Functions with and without Environments
      * [x] Native functions
      * [x] Dynamic functions
   * [x] Memory
   * [x] Globals
   * [x] Tables

# Review


- [ ] Add a short description of the change to the CHANGELOG.md file


Co-authored-by: Syrus Akbary <[email protected]>
  • Loading branch information
bors[bot] and syrusakbary authored Jul 23, 2021
2 parents 2cd1df1 + 124672e commit baebb43
Show file tree
Hide file tree
Showing 96 changed files with 9,856 additions and 2,051 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/js.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
on:
push:
branches:
- 'master'
- 'staging'
- 'trying'
tags:
# this is _not_ a regex, see: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#filter-pattern-cheat-sheet
- '[0-9]+.[0-9]+.[0-9]+*'

name: wasmer-js tests

env:
RUST_BACKTRACE: 1

jobs:
wasmer-js:
name: wasmer-js
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
override: true
- name: Install wasm-pack
run: |
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- name: Test wasmer-js
run: make test-js
58 changes: 57 additions & 1 deletion Cargo.lock

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

4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,10 @@ test-packages:
cargo test --manifest-path lib/compiler-singlepass/Cargo.toml --release --no-default-features --features=std
cargo test --manifest-path lib/cli/Cargo.toml $(compiler_features) --release

test-js:
cd lib/api && wasm-pack test --node -- --no-default-features --features js-default,wat


#####
#
# Testing compilers.
Expand Down
2 changes: 2 additions & 0 deletions lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ composed of a set of crates. We can group them as follows:
programatically through the `wasmer` crate,
* `c-api` — The public C API exposes everything a C user needs to use
Wasmer programatically,
* `js-api` — The public JavaScript API exposes everything a user needs to run Wasmer
in a client like a browser or on a server like NodeJS or Deno,
* `cache` — The traits and types to cache compiled WebAssembly
modules,
* `cli` — The Wasmer CLI itself,
Expand Down
178 changes: 114 additions & 64 deletions lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,96 +10,146 @@ license = "MIT"
readme = "README.md"
edition = "2018"

#####
# This crate comes in 2 major flavors:
#
# - `sys`, where `wasmer` will be compiled to a native executable
# which provides compilers, engines, a full VM etc.
# - `js`, where `wasmer` will be compiled to WebAssembly to run in a
# JavaScript host.
#####

# Shared dependencies.
[dependencies]
# - Mandatory shared dependencies.
indexmap = { version = "1.6", features = ["serde-1"] }
cfg-if = "1.0"
thiserror = "1.0"
more-asserts = "0.2"
# - Optional shared dependencies.
wat = { version = "1.0", optional = true }

# Dependencies and Development Dependencies for `sys`.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# - Mandatory dependencies for `sys`.
wasmer-vm = { path = "../vm", version = "2.0.0" }
wasmer-compiler-singlepass = { path = "../compiler-singlepass", version = "2.0.0", optional = true }
wasmer-compiler-cranelift = { path = "../compiler-cranelift", version = "2.0.0", optional = true }
wasmer-compiler-llvm = { path = "../compiler-llvm", version = "2.0.0", optional = true }
wasmer-compiler = { path = "../compiler", version = "2.0.0" }
wasmer-derive = { path = "../derive", version = "2.0.0" }
wasmer-engine = { path = "../engine", version = "2.0.0" }
wasmer-engine-universal = { path = "../engine-universal", version = "2.0.0", optional = true }
wasmer-engine-dylib = { path = "../engine-dylib", version = "2.0.0", optional = true }
wasmer-types = { path = "../types", version = "2.0.0" }
indexmap = { version = "1.6", features = ["serde-1"] }
cfg-if = "1.0"
wat = { version = "1.0", optional = true }
thiserror = "1.0"
more-asserts = "0.2"
target-lexicon = { version = "0.12", default-features = false }
loupe = "0.1"

[target.'cfg(target_os = "windows")'.dependencies]
# - Optional dependencies for `sys`.
wasmer-compiler-singlepass = { path = "../compiler-singlepass", version = "2.0.0", optional = true }
wasmer-compiler-cranelift = { path = "../compiler-cranelift", version = "2.0.0", optional = true }
wasmer-compiler-llvm = { path = "../compiler-llvm", version = "2.0.0", optional = true }
wasmer-engine-universal = { path = "../engine-universal", version = "2.0.0", optional = true }
wasmer-engine-dylib = { path = "../engine-dylib", version = "2.0.0", optional = true }
# - Mandatory dependencies for `sys` on Windows.
[target.'cfg(all(not(target_arch = "wasm32"), target_os = "windows"))'.dependencies]
winapi = "0.3"

[dev-dependencies]
# for the binary wasmer.rs
libc = { version = "^0.2", default-features = false }
# - Development Dependencies for `sys`.
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
wat = "1.0"
tempfile = "3.1"
anyhow = "1.0"

# Dependencies and Develoment Dependencies for `js`.
[target.'cfg(target_arch = "wasm32")'.dependencies]
# - Mandatory dependencies for `js`.
wasmer-types = { path = "../types", version = "2.0.0", default-features = false, features = ["std"] }
wasm-bindgen = "0.2.74"
js-sys = "0.3.51"
wasmer-derive = { path = "../derive", version = "2.0.0" }
# - Optional dependencies for `js`.
wasmparser = { version = "0.78", default-features = false, optional = true }
hashbrown = { version = "0.9", optional = true }
# - Development Dependencies for `js`.
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wat = "1.0"
anyhow = "1.0"
wasm-bindgen-test = "0.3.0"

# Specific to `js`.
#
# `wasm-opt` is on by default in for the release profile, but it can be
# disabled by setting it to `false`
[package.metadata.wasm-pack.profile.release]
wasm-opt = false

[badges]
maintenance = { status = "actively-developed" }

[features]
default = ["wat", "default-cranelift", "default-universal"]
default = ["sys-default"]
std = []
core = ["hashbrown"]

# Features for `sys`.
sys = []
sys-default = ["sys", "wat", "default-cranelift", "default-universal"]
# - Compilers.
compiler = [
"sys",
"wasmer-compiler/translator",
"wasmer-engine-universal/compiler",
"wasmer-engine-dylib/compiler",
]
engine = []
universal = [
"wasmer-engine-universal",
"engine"
]
dylib = [
"wasmer-engine-dylib",
"engine"
]
singlepass = [
"wasmer-compiler-singlepass",
"compiler",
]
cranelift = [
"wasmer-compiler-cranelift",
"compiler",
]
llvm = [
"wasmer-compiler-llvm",
"compiler",
]

default-singlepass = [
"singlepass",
"default-compiler"
]
default-cranelift = [
"cranelift",
"default-compiler"
]
default-llvm = [
"llvm",
"default-compiler"
]
default-universal = [
"universal",
"default-engine"
]
default-dylib = [
"dylib",
"default-engine"
]

singlepass = [
"compiler",
"wasmer-compiler-singlepass",
]
cranelift = [
"compiler",
"wasmer-compiler-cranelift",
]
llvm = [
"compiler",
"wasmer-compiler-llvm",
]
default-compiler = []
default-singlepass = [
"default-compiler",
"singlepass",
]
default-cranelift = [
"default-compiler",
"cranelift",
]
default-llvm = [
"default-compiler",
"llvm",
]
# - Engines.
engine = ["sys"]
universal = [
"engine",
"wasmer-engine-universal",
]
dylib = [
"engine",
"wasmer-engine-dylib",
]
default-engine = []

# experimental / in-development features
default-universal = [
"default-engine",
"universal",
]
default-dylib = [
"default-engine",
"dylib",
]
# - Experimental / in-development features
experimental-reference-types-extern-ref = [
"sys",
"wasmer-types/experimental-reference-types-extern-ref",
]

# Deprecated features.
# - Deprecated features.
jit = ["universal"]
native = ["dylib"]

# Features for `js`.
js = []
js-default = ["js", "std", "wasm-types-polyfill"]

wasm-types-polyfill = ["js", "wasmparser"]
Loading

0 comments on commit baebb43

Please sign in to comment.