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

Added WASI support to mozjs (WIP) #2

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b627e90
Added support for WASI
syrusakbary Sep 21, 2023
ffaffe0
Added libshit
syrusakbary Sep 21, 2023
dbf7a08
[hack] Added rusthook
syrusakbary Sep 21, 2023
78d2c68
[hack] Added bindings manually
syrusakbary Sep 21, 2023
ab0b02b
Merge branch 'wasi' into master-wasi2
syrusakbary Sep 21, 2023
4dec0be
Added full reproducible support for WASI
syrusakbary Sep 21, 2023
8605a6f
Removed unused file
syrusakbary Sep 21, 2023
aa86ad0
Added build to mozjs-sys
syrusakbary Sep 21, 2023
5fafa5f
Fixed js api bindings generation
syrusakbary Sep 22, 2023
ece0076
Fixed WASI build
syrusakbary Sep 22, 2023
a8260e8
Improved build
syrusakbary Sep 22, 2023
4d4b8b7
Use internal job queues check when creating the engine
syrusakbary Sep 22, 2023
e6182a8
Added to wasi build the default flags (only wasix)
syrusakbary Sep 22, 2023
141ea6e
Few fixes
syrusakbary Sep 22, 2023
cf082dd
Added HasJobsPending
syrusakbary Sep 22, 2023
60fb638
Check formatting and wrappers integrity in CI (#415)
sagudev Sep 23, 2023
d4a3ea8
Fixed Windows Compilation with LLVM 17 (#414)
Redfire75369 Sep 23, 2023
fdabed7
Update SM filter (#418)
sagudev Sep 23, 2023
249c9d4
README (#420)
sagudev Sep 24, 2023
5940374
More strict filter for SM (#421)
sagudev Sep 25, 2023
b7207a8
Fix configure
syrusakbary Sep 25, 2023
6296684
Make generate_wrappers work on dirty builds (#400)
sagudev Sep 28, 2023
2fa4b2b
Add wasm example (#344)
sagudev Sep 30, 2023
ec63ca3
Hide js-streams behind feature (#422)
sagudev Oct 5, 2023
f1ccdb4
Merge branch 'master' into wasi
Arshia001 Oct 11, 2023
8f2d892
Fix wasi-makefile.cargo using spaces instead of tab
Arshia001 Oct 11, 2023
64ac49f
Include all object files in final lib
Arshia001 Oct 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
26 changes: 18 additions & 8 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ jobs:
CCACHE: sccache
SCCACHE_GHA_ENABLED: "true"
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install deps
run: brew install python llvm yasm
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
- name: Build
run: |
cargo build --verbose ${{ matrix.features }}
cargo test --verbose ${{ matrix.features }}
cargo test --tests --examples --verbose ${{ matrix.features }}
linux:
env:
RUSTC_WRAPPER: "sccache"
Expand All @@ -45,7 +45,7 @@ jobs:
matrix:
features: ["--features debugmozjs", ""]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Install deps
run: |
Expand All @@ -55,7 +55,14 @@ jobs:
- name: Build
run: |
cargo build --verbose ${{ matrix.features }}
cargo test --verbose ${{ matrix.features }}
cargo test --tests --examples --verbose ${{ matrix.features }}
- name: Check wrappers integrity
# we generate wrappers only without debugmozjs
if: ${{ matrix.features == '' }}
run: |
bash ./mozjs/src/generate_wrappers.sh
git diff --quiet --exit-code

windows:
runs-on: windows-latest
strategy:
Expand All @@ -71,7 +78,7 @@ jobs:
CCACHE: sccache
SCCACHE_GHA_ENABLED: "true"
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
Expand All @@ -89,12 +96,12 @@ jobs:
if: ${{ !contains(matrix.target, 'aarch64') }}
shell: cmd
run: |
cargo test --verbose --target ${{ matrix.target }} ${{ matrix.features }}
cargo test --tests --examples --verbose --target ${{ matrix.target }} ${{ matrix.features }}

android:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install NDK
uses: nttld/setup-ndk@v1
id: setup-ndk
Expand Down Expand Up @@ -126,12 +133,15 @@ jobs:
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- run: cargo test --target ${{ matrix.target }}
- run: cargo test --tests --examples --target ${{ matrix.target }}

integrity:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Check formatting
run: cargo fmt --check
- name: Get mozjs
run: |
bash ./mozjs-sys/etc/get_mozjs.sh
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
*.so
*.dll
*.dylib

# We are skipping the mozjs wasi dir
mozjs-sys/mozjs-wasi
91 changes: 51 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
This repository contains Rust bindings for [SpiderMonkey][sm] for use with
[Servo][s].
# Mozjs (Rust bindings for SpiderMonkey)

[sm]: https://spidermonkey.dev/
[s]: https://servo.org/

This repository contains two crates:
This repository contains Rust bindings for [SpiderMonkey](https://spidermonkey.dev/)
that are battle-tested in [Servo](https://servo.org/), split in two crates:

- `mozjs-sys`: SpiderMonkey and low-level Rust bindings to its C++ API.
- `mozjs`: Higher-level bindings to the SpiderMonkey API.

# Building
Mozjs is currently tracking SpiderMonkey on [ESR-115](https://searchfox.org/mozilla-esr115/source/) branch
(currently version 115.3).

## Building

## Linux
### Linux

Install Python, Clang and `build-essential`, for example on a Debian-based Linux:

Expand All @@ -26,7 +26,7 @@ environment variable, for example:
export LIBCLANG_PATH=/usr/lib/clang/4.0/lib
```

## Windows
### Windows

1. Download and unzip [MozTools 4.0](https://github.com/servo/servo-build-deps/releases/download/msvc-deps/moztools-4.0.zip).

Expand All @@ -48,7 +48,7 @@ export LIBCLANG_PATH=/usr/lib/clang/4.0/lib
set MOZTOOLS_PATH=C:\path\to\moztools-4.0
```

## Run Cargo
### Run Cargo

You can now build and test the crate using cargo:

Expand All @@ -59,7 +59,15 @@ cargo build --features debugmozjs
cargo test --features debugmozjs
```

# Building servo against your local mozjs
### Usage for downstream consumers

Mozjs is currently not published to crates.io, but it can be used from git (binaries should use lockfile instead of `rev`):

```toml
mozjs = { git = "https://github.com/servo/mozjs", rev = "latest-commit-hash" }
```

## Building servo against your local mozjs

Assuming your local `servo` and `mozjs` directories are siblings, you can build `servo` against `mozjs` by adding the following to `servo/Cargo.toml`:

Expand All @@ -68,31 +76,34 @@ Assuming your local `servo` and `mozjs` directories are siblings, you can build
mozjs = { path = "../mozjs/mozjs" }
```

# Upgrading
## Upgrading

In order to upgrade to a new version of SpiderMonkey:

1. Find the mozilla-release commit for the desired version of SpiderMonkey, at
<https://treeherder.mozilla.org/#/jobs?repo=mozilla-release&filter-searchStr=spidermonkey%20pkg>.
1. Find the mozilla-esr115 commit for the desired version of SpiderMonkey, at
<https://treeherder.mozilla.org/#/jobs?repo=mozilla-esr115&filter-searchStr=spidermonkey%20pkg>.
You are looking for an SM(pkg) tagged with FIREFOX_RELEASE.
Take a note of the commit number to the left (a hex number such as ac4fbb7aaca0).

2. Click on the SM(pkg) link, which will open a panel with details of the
commit, including an artefact uploaded link, with a name of the form
mozjs-*version*.tar.bz2. Download it and save it locally.
mozjs-*version*.tar.xz. Download it and save it locally.

3. Look at the patches in `etc/patches/*.patch`, and remove any that no longer apply
3. Look at the patches in `mozjs-sys/etc/patches/*.patch`, and remove any that no longer apply
(with a bit of luck this will be all of them).

4. Run `python3 ./etc/update.py path/to/tarball`.
4. Run `python3 ./mozjs-sys/etc/update.py path/to/tarball`.

5. Update `etc/COMMIT` with the commit number.
5. Update `mozjs-sys/etc/COMMIT` with the commit number.

6. Run `./mozjs/src/generate_wrappers.sh` to regenerate wrappers.

7. Build and test the bindings as above, then submit a PR!

# NixOS users
8. Send companion PR to servo, as SpiderMonkey bump PR will not be merged
until it's tested against servo.

## NixOS users

To get a dev environment with shell.nix:

Expand All @@ -110,21 +121,21 @@ To configure rust-analyzer in Visual Studio Code:
}
```

# Editor support
## Editor support

If you are working on the Rust code only, rust-analyzer should work perfectly out of the box, though NixOS users will need to configure rust-analyzer to wrap cargo invocations (see above).

But if you are working on the C++ code, editor support is only really possible in upstream SpiderMonkey (aka “mozilla-central”), but once you’ve set up your editor in your upstream checkout, you can work on your changes there, then import them here as needed for local testing.

This guide assumes that your code is checked out at:

* **~/code/mozjs** for this repo
* **~/code/mozilla-unified** for upstream SpiderMonkey
* (NixOS users only) **~/code/nixpkgs-mozilla** for [mozilla/nixpkgs-mozilla](https://github.com/mozilla/nixpkgs-mozilla)
- **~/code/mozjs** for this repo
- **~/code/mozilla-unified** for upstream SpiderMonkey
- (NixOS users only) **~/code/nixpkgs-mozilla** for [mozilla/nixpkgs-mozilla](https://github.com/mozilla/nixpkgs-mozilla)

**NixOS users:** some steps have a note in \[brackets] saying they need to be wrapped in nix-shell. Those commands should be wrapped as follows:

```
```shell
nix-shell ~/code/nixpkgs-mozilla/release.nix -A gecko.x86_64-linux.clang --run '...'
```

Expand All @@ -136,7 +147,7 @@ Start by checking out mozilla-unified ([Building Firefox on Linux](https://firef

Now create your MOZCONFIG file ([Building and testing SpiderMonkey](https://firefox-source-docs.mozilla.org/js/build.html)). I recommend (and this guide assumes) that the file is named `debug.mozconfig`, because simple names like `debug` can cause MozconfigFindException problems. The file should look like this:

```
```shell
# Build only the JS shell
ac_add_options --enable-project=js

Expand All @@ -154,21 +165,21 @@ mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/obj-debug-@CONFIG_GUESS@

If you are a NixOS user, clone [mozilla/nixpkgs-mozilla](https://github.com/mozilla/nixpkgs-mozilla) next to your `mozilla-unified` checkout, and add the following line to the start of your `debug.mozconfig`:

```
```shell
. ./.mozconfig.nix-shell
```

You will need to generate your Visual Studio Code config and compilation database against central at least once, before you can do so against the commit we forked from (`mozjs/etc/COMMIT`).

```
```console
~/code/mozilla-unified $ MOZCONFIG=debug.mozconfig ./mach ide vscode
```

> [NixOS users: wrap the command above in nix-shell]

Otherwise you might get an error with lots of exclamation marks:

```
```console
~/code/mozilla-unified $ MOZCONFIG=debug.mozconfig ./mach ide vscode
[snip]
0:05.80 Unable to locate clangd in /home/delan/code/mozilla-unified/.mozbuild/clang-tools/clang-tidy/bin.
Expand All @@ -180,7 +191,7 @@ Otherwise you might get an error with lots of exclamation marks:

Now switch to the commit we forked from, and generate them again.

```
```console
~/code/mozilla-unified $ hg update -r $(cat ../mozjs/mozjs/etc/COMMIT)
~/code/mozilla-unified $ MOZCONFIG=debug.mozconfig ./mach ide vscode
```
Expand All @@ -195,28 +206,28 @@ If there are no problems in the margin, and you can Go To Definition, you’re d

If you are a NixOS user and see this in the clangd output panel:

```
```console
[Error - 7:38:13 pm] Clang Language Server client: couldn't create connection to server.
Launching server using command /home/delan/code/mozilla-unified/.mozbuild/clang-tools/clang-tidy/bin/clangd failed. Error: spawn /home/delan/code/mozilla-unified/.mozbuild/clang-tools/clang-tidy/bin/clangd ENOENT
```

Then you need to replace the mozbuild toolchain’s clangd with one that has been patchelf’d:

```
```console
~/code/mozilla-unified $ ln -sf ~/.nix-profile/bin/clangd .mozbuild/clang-tools/clang-tidy/bin/clangd
```

If you see this in the clangd output panel:

```
```console
I[19:20:28.001] Indexed /home/delan/code/mozilla-unified/js/src/jit/LIR.cpp (61040 symbols, 244267 refs, 738 files)
I[19:20:28.001] Failed to compile /home/delan/code/mozilla-unified/js/src/jit/LIR.cpp, index may be incomplete
I[19:20:28.087] --> $/progress
```

Then the commands in your compilation database might be incorrect. You can try running one of the commands in a terminal to see what happens:

```
```console
~/code/mozilla-unified $ ( f=$PWD/obj-debug-x86_64-pc-linux-gnu/clangd/compile_commands.json; set -x; cd $(< $f jq -r '.[0].directory'); $(< $f jq -r '.[0].command') )
+/run/current-system/sw/bin/zsh:252> jq -r '.[0].directory'
+/run/current-system/sw/bin/zsh:252> cd /home/delan/code/mozilla-unified/obj-debug-x86_64-pc-linux-gnu/js/src
Expand All @@ -227,7 +238,7 @@ gcc: error: unrecognized command-line option ‘-ferror-limit=0’

In this case, it was because your compiler was gcc (which supports `-fmax-errors` but not `-ferror-limit`), but it should always be clang (which supports both) when working with clangd. If you are a NixOS user, make sure you use the `clang` derivation, not the `gcc` derivation, when generating your compilation database:

```
```console
~/code/mozilla-unified $ nix-shell ~/code/nixpkgs-mozilla/release.nix -A gecko.x86_64-linux.clang --run 'MOZCONFIG=debug.mozconfig ./mach ide vscode'
^^^^^
```
Expand All @@ -236,33 +247,33 @@ In this case, it was because your compiler was gcc (which supports `-fmax-errors

Start by making a source tarball from your local upstream SpiderMonkey checkout. [TODO(@delan) the default xz compression is very slow here, we should add an option upstream to make it faster]

```
```console
~/code/mozilla-unified $ AUTOMATION=1 DIST=$PWD/../mozjs/mozjs/etc js/src/make-source-package.py
```

> [NixOS users: wrap the command above in nix-shell]

Now update your vendored copy of SpiderMonkey from that tarball. This creates a commit replacing `mozjs/mozjs` with the *unpatched* contents of the tarball, leaving the changes made by reapplying our patches in your working directory diff (`git diff`).

```
```console
~/code/mozjs $ python3 mozjs/etc/update.py mozjs/etc/mozjs-107.0.0.tar.xz
```

Then do a (mixed) reset to remove the commit and unstage its changes.

```
```console
~/code/mozjs $ git reset @~
```

Your working directory diff (`git diff`) should now contain (and only contain) the changes you’ve made to your upstream SpiderMonkey checkout. If you see changes to `mozjs/mozjs/js/src/old-configure` [TODO(@delan) why does this happen?], you may need to undo them:

```
```console
~/code/mozjs $ git restore -W mozjs/mozjs/js/src/old-configure
```

Otherwise you might get the build failure below:

```
```console
~/code/mozjs $ cargo build
[snip]
configure: error: can not find sources in /home/delan/code/mozjs/mozjs/mozjs/js/src or ..
Expand All @@ -277,6 +288,6 @@ Otherwise you might get the build failure below:

Now you can build the Rust crates against your modified version of SpiderMonkey!

```
```console
~/code/mozjs $ cargo build
```
1 change: 1 addition & 0 deletions mozjs-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ doctest = false
debugmozjs = []
profilemozjs = []
jitspew = []
streams = []

[dependencies]
encoding_c = "0.9.8"
Expand Down
Loading
Loading