Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = ["mozjs-sys", "mozjs"]
exclude = ["examples"]
resolver = "2"

[workspace.package]
Expand Down
14 changes: 14 additions & 0 deletions examples/embedder_allocator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "embedder_allocator"
version = "0.1.0"
license = "MPL-2.0"
edition = "2024"
description = "Example usage of mozjs, with the embedder (this crate) providing a custom allocator for mozjs"
publish = false
# note: This crate requires some additional setup (via external environment variables) when building,
# so it can't be added to the workspace (also because we enable the `custom-alloc` feature, which is unwanted
# due to feature-unification.

[dependencies]
mozjs = { path = "../../mozjs", features = ["custom-alloc"] }
mimalloc = "0.1.48"
8 changes: 8 additions & 0 deletions examples/embedder_allocator/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example: Using mozjs with an embedder-provided allocator

1. In your project setup an `include` directory containing `servo_embedder_allocator.h` defining
`SERVO_EMBEDDER_MALLOC_PREFIX` and all required allocation functions with the corresponding prefix.
See `mimalloc/include` for an example based on mimalloc.
2. Enable the feature `custom-alloc` and set the environment variable `SERVO_CUSTOM_ALLOC_INCLUDE_DIR` to
point to the include directory created in step 1.
3. TODO: An environment variable to instruct `build.rs` of SM to link against custom allocator library?
29 changes: 29 additions & 0 deletions examples/embedder_allocator/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# Usage: `python3 build.py cargo run` in this directory to run the example with the custom allocator.

import os
import pathlib
import subprocess
import sys
from typing import Mapping

def create_env() -> Mapping[str, str]:
env = os.environ.copy()
mimalloc_include_dir = pathlib.Path(__file__).parent.joinpath('mimalloc/include')
assert mimalloc_include_dir.is_dir(), "Could not find mimalloc include directory"
env['SERVO_CUSTOM_ALLOC_INCLUDE_DIR'] = mimalloc_include_dir.as_posix()
return env


def main():
completed_process = subprocess.run(sys.argv[1:], env=create_env())
sys.exit(completed_process.returncode)


if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions examples/embedder_allocator/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("cargo:rustc-link-lib=mimalloc");
}
612 changes: 612 additions & 0 deletions examples/embedder_allocator/mimalloc/include/mimalloc.h

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

/** Example of an embedder header providing custom allocations

Re-export the header by simply #including it here.
We expect all allocating functions to be provided via their standard name
with a prefix of SERVO_EMBEDDER_MALLOC_PREFIX,
e.g. `<SERVO_EMBEDDER_MALLOC_PREFIX>malloc` -> `mi_malloc` given a prefix of `mi_`.

If some functions are called differently in the original header, you can also provide
static inline wrapper functions here that translate the expected name to the actual name.
*/
#include <mimalloc.h>
#define SERVO_EMBEDDER_MALLOC_PREFIX mi_
76 changes: 76 additions & 0 deletions examples/embedder_allocator/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::ptr;
use std::sync::mpsc::channel;
use std::thread;

use mozjs::jsapi::GCContext;
use mozjs::jsapi::JSCLASS_FOREGROUND_FINALIZE;
use mozjs::jsapi::{JSClass, JSClassOps, JSObject, OnNewGlobalHookOption};
use mozjs::realm::AutoRealm;
use mozjs::rooted;
use mozjs::rust::wrappers2::{JS_NewGlobalObject, JS_NewObject};
use mozjs::rust::{JSEngine, RealmOptions, Runtime, SIMPLE_GLOBAL_CLASS};

fn main() {
println!("Hello, world!");
let engine = JSEngine::init().expect("Could not init JSEngine");
println!("JSEngine initialized");
let mut runtime = Runtime::new(engine.handle());
println!("Runtime initialized");
let context = runtime.cx();
let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
let c_option = RealmOptions::default();

unsafe {
rooted!(&in(context) let global = JS_NewGlobalObject(
context,
&SIMPLE_GLOBAL_CLASS,
ptr::null_mut(),
h_option,
&*c_option,
));
let mut realm = AutoRealm::new_from_handle(context, global.handle());
let context = &mut realm;
rooted!(&in(context) let _object = JS_NewObject(context, &CLASS as *const _));
}

let parent = runtime.prepare_for_new_child();
let (sender, receiver) = channel();
thread::spawn(move || {
let runtime = unsafe { Runtime::create_with_parent(parent) };
assert!(Runtime::get().is_some());
drop(runtime);
let _ = sender.send(());
});
let _ = receiver.recv();
println!("Example ran without issues...");
}

unsafe extern "C" fn finalize(_fop: *mut GCContext, _object: *mut JSObject) {
assert!(Runtime::get().is_some());
}

static CLASS_OPS: JSClassOps = JSClassOps {
addProperty: None,
delProperty: None,
enumerate: None,
newEnumerate: None,
resolve: None,
mayResolve: None,
finalize: Some(finalize),
call: None,
construct: None,
trace: None,
};

static CLASS: JSClass = JSClass {
name: c"EventTargetPrototype".as_ptr(),
flags: JSCLASS_FOREGROUND_FINALIZE,
cOps: &CLASS_OPS as *const JSClassOps,
spec: ptr::null(),
ext: ptr::null(),
oOps: ptr::null(),
};
2 changes: 2 additions & 0 deletions mozjs-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ name = "mozjs_sys"
doctest = false

[features]
default = []
custom-alloc = []
debugmozjs = []
profilemozjs = []
jit = []
Expand Down
45 changes: 43 additions & 2 deletions mozjs-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ fn main() {

fn build_spidermonkey(build_dir: &Path) {
let target = env::var("TARGET").unwrap();
let cargo_manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let make;

#[cfg(windows)]
Expand Down Expand Up @@ -210,24 +211,38 @@ fn build_spidermonkey(build_dir: &Path) {
}

cppflags.push(get_cc_rs_env_os("CPPFLAGS").unwrap_or_default());
cmd.env("CPPFLAGS", cppflags);

if let Some(makeflags) = env::var_os("CARGO_MAKEFLAGS") {
cmd.env("MAKEFLAGS", makeflags);
}

let mut cxxflags = vec![];

if env::var_os("CARGO_FEATURE_CUSTOM_ALLOC").is_some() {
let mut flags = vec![];
println!("cargo:rerun-if-env-changed=SERVO_CUSTOM_ALLOC_INCLUDE_DIR");
let include_dir_str = env::var("SERVO_CUSTOM_ALLOC_INCLUDE_DIR").expect("Required variable not set with feature custom-alloc");
let include_dir = Path::new(&include_dir_str);
assert!(include_dir.is_dir(), "SERVO_CUSTOM_ALLOC_INCLUDE_DIR must be set to a valid directory");
assert!(include_dir.join("servo_embedder_allocator.h").is_file(), "SERVO_CUSTOM_ALLOC_INCLUDE_DIR must contain header `servo_embedder_allocator.h`");
flags.push(format!("-I{}", &include_dir_str.replace("\\", "/")));
println!("cargo:rerun-if-changed={}", include_dir_str);

cppflags.extend(flags.iter().map(|s| OsString::from(s)));
cxxflags.extend(flags);
}

if target.contains("apple") || target.contains("freebsd") || target.contains("ohos") {
cxxflags.push(String::from("-stdlib=libc++"));
}

cmd.env("CPPFLAGS", cppflags);

let base_cxxflags = env::var("CXXFLAGS").unwrap_or_default();
let mut cxxflags = cxxflags.join(" ");
cxxflags.push_str(&base_cxxflags);
cmd.env("CXXFLAGS", cxxflags);

let cargo_manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let result = cmd
.args(&["-R", "-f"])
.arg(cargo_manifest_dir.join("makefile.cargo"))
Expand Down Expand Up @@ -293,6 +308,14 @@ fn build(build_dir: &Path, target: BuildTarget) {
build.flag(include_file_flag(build.get_compiler().is_like_msvc()));
build.flag(&js_config_path(build_dir));

if env::var_os("CARGO_FEATURE_CUSTOM_ALLOC").is_some() {
let include_dir_str = env::var("SERVO_CUSTOM_ALLOC_INCLUDE_DIR").expect("Required variable not set with feature custom-alloc");
let include_dir = Path::new(&include_dir_str);
assert!(include_dir.is_dir(), "SERVO_CUSTOM_ALLOC_INCLUDE_DIR must be set to a valid directory");
assert!(include_dir.join("servo_embedder_allocator.h").is_file(), "SERVO_CUSTOM_ALLOC_INCLUDE_DIR must contain header `servo_embedder_allocator.h`");
build.include(include_dir);
}

for path in target.include_paths(build_dir) {
build.include(path);
}
Expand Down Expand Up @@ -332,6 +355,21 @@ fn build_bindings(build_dir: &Path, target: BuildTarget) {
.clang_arg(env::var("WASI_SYSROOT").unwrap().to_string());
}

let custom_alloc_flags = if env::var_os("CARGO_FEATURE_CUSTOM_ALLOC").is_some() {
let mut flags = vec![];
let include_dir_str = env::var("SERVO_CUSTOM_ALLOC_INCLUDE_DIR").expect("Required variable not set with feature custom-alloc");
let include_dir = Path::new(&include_dir_str);
assert!(include_dir.is_dir(), "SERVO_CUSTOM_ALLOC_INCLUDE_DIR must be set to a valid directory");
assert!(include_dir.join("servo_embedder_allocator.h").is_file(), "SERVO_CUSTOM_ALLOC_INCLUDE_DIR must contain header `servo_embedder_allocator.h`");
flags.push(format!("-I{}", &include_dir_str.replace("\\", "/")));
flags.push("-DSERVO_EMBEDDER_MEMORY".to_string());
flags
} else {
vec![]
};
builder = builder.clang_args(custom_alloc_flags);


if target == BuildTarget::JSGlue {
builder = builder
.parse_callbacks(Box::new(JSGlueCargoCallbacks::default()))
Expand Down Expand Up @@ -462,6 +500,9 @@ fn should_build_from_source() -> bool {
} else if env::var_os("CARGO_FEATURE_INTL").is_none() {
println!("intl feature is disabled. Building from source directly.");
true
} else if env::var_os("CARGO_FEATURE_CUSTOM_ALLOC").is_some() {
println!("custom-alloc feature is enabled. Building from source directly.");
true
} else if !env::var_os("CARGO_FEATURE_JIT").is_some() {
println!("jit feature is NOT enabled. Building from source directly.");
true
Expand Down
4 changes: 4 additions & 0 deletions mozjs-sys/makefile.cargo
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ ifneq (,$(CARGO_FEATURE_PROFILEMOZJS))
CONFIGURE_FLAGS += --enable-profiling
endif

ifneq (,$(CARGO_FEATURE_CUSTOM_ALLOC))
CONFIGURE_FLAGS += --enable-servo-embedder-memory
endif

ifneq (,$(CARGO_FEATURE_LIBZ_RS))
CONFIGURE_FLAGS += --enable-libz-rs
endif
Expand Down
17 changes: 17 additions & 0 deletions mozjs-sys/mozjs/js/moz.configure

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

2 changes: 1 addition & 1 deletion mozjs-sys/mozjs/js/public/Utility.h

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

5 changes: 3 additions & 2 deletions mozjs-sys/mozjs/js/src/jit/arm64/vixl/Disasm-vixl.cpp

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

26 changes: 26 additions & 0 deletions mozjs-sys/mozjs/memory/build/embedder_fallback.cpp

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

4 changes: 4 additions & 0 deletions mozjs-sys/mozjs/memory/build/moz.build

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

16 changes: 16 additions & 0 deletions mozjs-sys/mozjs/memory/build/mozmemory_wrap.h

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

Loading
Loading