百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

mozjs

> 编程语言
开源

Servo 的 SpiderMonkey 分支

349 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Servo 的 SpiderMonkey 分支

Mozjs (Rust bindings for SpiderMonkey)

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-esr153 branch (currently version 153.0).

Building from Pre-built Archive

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=: Disable artifact verification.
    • MOZJS_ATTESTATION=: Enable artifact verification and fallback to compiling from source if verification fails or is not possible.
    • MOZJS_ATTESTATION=: Fail the build if artifact verification fails.

Building from Source

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.

Linux

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

bash
sudo apt-get install build-essential python3 python3-distutils llvm libclang-dev clang curl

If you have more than one version of Clang installed, you can set the LIBCLANG_PATH environment variable, for example:

bash
export LIBCLANG_PATH=/usr/lib/clang/4.0/lib

Windows

  1. Download and unzip MozTools 4.0.

  2. Download and install Clang (LLVM version 14 or greater) for Windows (64 bit) from .

  3. 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^
  4. 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
  1. Set the following environment variables according to where you installed the dependencies above:

    powershell
     $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"

Run Cargo

You can now build and test the crate using cargo:

bash
cargo build
cargo test
cargo build --features debugmozjs
cargo test --features debugmozjs

The debugmozjs feature and prebuilt archives

The debugmozjs feature enables SpiderMonkey assertions and GC zeal (to help find GC issues). If debugmozjs is enabled, the optimization level is taken from cargo's OPT_LEVEL (i.e. the active profile), so a default dev-profile build is unoptimized (-O0), while --release produces an optimized build with assertions still enabled. If debugmozjs is disabled, the prebuilt archive is always optimized. The pre-built debugmozjs archive published by CI is built with -O3 (default for the release profile). If you specify any other opt-level (e.g. by building the dev profile), this will be honored, and a build from source will be triggered. You can edit the opt-level for spidermonkey specifically by editing you workspace Cargo.toml to contain:

toml
# replace `dev` with the profile name you are targeting
# Note: This will only affect Spidermonkey code if `debugmozjs` is enabled, otherwise
# optimisations will always be enabled.
[profile.dev.package.mozjs_sys]
opt-level = 3 # or any other opt-level

Usage for downstream consumers

Both mozjs and mozjs_sys crates are published on crates.io.

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:

toml
[patch."https://github.com/servo/mozjs"]
mozjs = { path = "../mozjs/mozjs" }

Upgrading

In order to upgrade to a new version of SpiderMonkey:

  1. Find the mozilla-esr153 commit for the desired version of SpiderMonkey, at . 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.xz. Download it and save it locally.

  3. Go to and download artifacts allFUnctions.txt.gz and gcFunctions.txt.gz from job Linux debug > H

  4. Create a new release on github with all files you downloaded. Name the new tag mozjs-source-${COMMIT}.

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

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

  7. Update mozjs-sys/etc/COMMIT with the commit number and mozjs-sys version with SpiderMonkey version.

  8. Run ./mozjs/src/dl_and_gen_noGC.py and ./mozjs/src/generate_wrappers.py to regenerate wrappers.

  9. Build and test the bindings as above.

  10. Submit a PR!

  11. 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:

bash
nix-shell

To configure rust-analyzer in Visual Studio Code:

json
{
    "rust-analyzer.check.overrideCommand": ["nix-shell", "--run", "cargo check --message-format=json"],
    "rust-analyzer.cargo.buildScripts.overrideCommand": ["nix-shell", "--run", "cargo check --message-format=json"],
    "rust-analyzer.rustfmt.overrideCommand": ["nix-shell", "--run", "cargo fmt"],
}

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

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:

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

C++ editor setup

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:

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

# Enable the debugging tools: Assertions, debug only code etc.
ac_add_options --enable-debug

# Enable optimizations as well so that the test suite runs much faster. If
# you are having trouble using a debugger, you should disable optimization.
ac_add_options --enable-optimize

# Use a dedicated objdir for SpiderMonkey debug builds to avoid
# conflicting with Firefox build with default configuration.
mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/obj-debug-@CONFIG_GUESS@

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:

bash
. ./.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).

bash
~/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:

…

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

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

bash
~/code/mozilla-unified $ hg update -r $(cat ../mozjs/mozjs/etc/COMMIT)
~/code/mozilla-unified $ MOZCONFIG=debug.mozconfig ./mach ide vscode

[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!

Troubleshooting

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

bash
[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 nee

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rust

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言