This repository contains Rust bindings for SpiderMonkey that are battle-tested in Servo, split in two crates:
- mozjs-sys: SpiderMonkey and low-level Rust bindings to its C++ API.
- mozjs: Higher-level bindings to the SpiderMonkey API.
Mozjs is currently tracking SpiderMonkey on mozilla-esr140 branch (currently version 140.5).
SpiderMonkey is very large and can take a long time to compile. If building with default features, mozjs provides a pre-built archive which can be linked against. You can also create your own archive and link to it. mozjs currently offers two environment variables to enable this feature:
- MOZJS_CREATE_ARCHIVE=1 will create a SpiderMonkey binary archive for release usage. It will be created in the target directory.
- MOZJS_ARCHIVE can be used to build against a pre-built archive. Using this flag, compiling
SpiderMonkey and the bindgen wrappers is unnecessary. There are two ways to use it:
- MOZJS_ARCHIVE=path/to/libmozjs.tar.gz: This option will look for the archive at the local path, extract it, and then link against the static libraries included in the archive.
- MOZJS_ARCHIVE=https://url/to/release/page: This option will download the archive from the provided base URL, extract it, and then link against the static libraries included in the archive. The base URL should be similar to https://github.com/servo/mozjs/releases. The build script will append the version and target accordingly. See the files at the example URL for more details.
- MOZJS_ATTESTATION allows uses Github Attestations to verify the integrity of the prebuilt archive
and that the archive was built by in CI, for a valid commit on the main branch of the servo/mozjs repo.
Attestation verification requires having a recent version of the github cli tool gh installed.
If artifact verification is enabled and reports an error, the prebuilt archive will be discarded and
mozjs will be built from source instead.
Available values are:
- unset (default): Equivalent to off.
- MOZJS_ATTESTATION=<0|false|off>: Disable artifact verification.
- MOZJS_ATTESTATION=<1|true|on|lenient>: Enable artifact verification and fallback to compiling from source if verification fails or is not possible.
- MOZJS_ATTESTATION=<2|strict|force>: Fail the build if artifact verification fails.
If MOZJS_FROM_SOURCE=1 or MOZJS_CREATE_ARCHIVE are enabled or linking against a pre-built archive fails, mozjs will build SpiderMonkey from source.
Install Python, Clang and build-essential, for example on a Debian-based Linux:
If you have more than one version of Clang installed, you can set the LIBCLANG_PATH environment variable, for example:
-
Download and unzip MozTools 4.0.
-
Download and install Clang (LLVM version 14 or greater) for Windows (64 bit) from https://releases.llvm.org/download.html.
-
Download and install Visual Studio 2019 or Visual Studio 2022 with the C++ desktop development component and the following features:
- Windows 10 SDK
- ATL
To install these dependencies from the command line, you can download vs_buildtools.exe and run the following command:
vs_BuildTools.exe^ --add Microsoft.VisualStudio.Workload.MSBuildTools^ --add Microsoft.VisualStudio.Component.Windows11SDK^ --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64^ --add Microsoft.VisualStudio.Component.VC.ATL^ -
Install Python 3.11.
- Ensure that Python is added to the system PATH
- Ensure that a PYTHON and PYTHON3 environment variable point to the Python binary (ie C:\Python311\python.exe
-
Set the following environment variables according to where you installed the dependencies above:
$env:LIBCLANG_PATH="C:\Program Files\LLVM\lib" $env:MOZTOOLS_PATH="C:\path\to\moztools-4.0" $env:CC="clang-cl" $env:CXX="clang-cl" $env:LD="lld-link"
You can now build and test the crate using cargo:
Mozjs is currently not published to crates.io, but it can be used from git (binaries should use lockfile instead of rev):
Assuming your local servo and mozjs directories are siblings, you can build servo against mozjs by adding the following to servo/Cargo.toml:
In order to upgrade to a new version of SpiderMonkey:
-
Find the mozilla-esr140 commit for the desired version of SpiderMonkey, at https://treeherder.mozilla.org/#/jobs?repo=mozilla-esr140&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).
-
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.xz. Download it and save it locally.
-
Go to https://treeherder.mozilla.org/jobs?repo=mozilla-esr140&revision=${COMMIT} and download artifacts allFUnctions.txt.gz and gcFunctions.txt.gz from job Linux debug > H
-
Create a new release on github with all files you downloaded. Name the new tag mozjs-source-${COMMIT}.
-
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).
-
Run python3 ./mozjs-sys/etc/update.py path/to/tarball.
-
Update mozjs-sys/etc/COMMIT with the commit number and mozjs-sys version with SpiderMonkey version.
-
Run ./mozjs/src/dl_and_gen_noGC.py and ./mozjs/src/generate_wrappers.py to regenerate wrappers.
-
Build and test the bindings as above.
-
Submit a PR!
-
Send companion PR to servo, as SpiderMonkey bump PR will not be merged until it's tested against servo.
To get a dev environment with shell.nix:
To configure rust-analyzer in Visual Studio Code:
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
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:
Start by checking out mozilla-unified (Building Firefox on Linux §§ 1 and 2).
NixOS users: it’s ok if the bootstrap command fails with a NotImplementedError due to NixOS not being a supported distro.
Now create your MOZCONFIG file (Building and testing SpiderMonkey). 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:
If you are a NixOS user, clone mozilla/nixpkgs-mozilla next to your mozilla-unified checkout, and add the following line to the start of your debug.mozconfig:
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).
[NixOS users: wrap the command above in nix-shell]
Otherwise you might get an error with lots of exclamation marks:
[NixOS users: wrap the command above in nix-shell]
Now switch to the commit we forked from, and generate them again.
[NixOS users: wrap the mach command above in nix-shell]
At this point, you should be able to open Visual Studio Code, install the clangd extension, and open a file like js/src/vm/ArrayBufferObject.cpp without seeing any problems in the margin.
If there are no problems in the margin, and you can Go To Definition, you’re done!
If you are a NixOS user and see this in the clangd output panel:
Then you need to replace the mozbuild toolchain’s clangd with one that has been patchelf’d:
If you see this in the clangd output panel:
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:
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:
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]
[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).
Then do a (mixed) reset to remove the commit and unstage its changes.
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:
Otherwise you might get the build failure below:
Now you can build the Rust crates against your modified version of SpiderMonkey!
.png)

