Skip to content

Truly independent web browser

LibJS: Expose an embedding API of the Rust runtime

Aliaksandr Kalenik72ed84a07059
diff --git a/Libraries/LibJS/Rust/src/api.rs b/Libraries/LibJS/Rust/src/api.rs
new file mode 100644
index 00000000000..95383ad7c09
--- /dev/null
+++ b/Libraries/LibJS/Rust/src/api.rs
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! The embedding API of the runtime for embedders written in Rust: what a crate that implements host objects and
+//! native functions on this runtime uses, as LibJS's C++ API is what a C++ embedder uses. Code outside LibJS reaches
+//! the runtime only through this module (Meta/Linters/check_libjs_rust_api.py), so the rest of the crate stays the
+//! runtime's own business.
+
+pub use crate::embedding::host::host_object::{HostObject, host_class_of, host_data_of};
+pub use crate::gc::embedder_cell::{EmbedderCell, EmbedderClass, EmbedderValue};
+pub use crate::gc::root::{MarkedVec, Root};
+pub use crate::gc::visitor::{Trace, Visitor};
+pub use crate::interpreter::vm::{TypeErrorRealmScope, Vm};
+pub use crate::layout::cell::Gc;
+pub use crate::layout::function_object::FunctionObject;
+pub use crate::layout::host_class::JSHostClass;
+pub use crate::layout::value::Value;
+pub use crate::layout_forward::{RawNativeFunctionPointer, RawNativeFunctionResult};
+pub use crate::raw_native;
+pub use crate::runtime::abstract_operations::get_function_realm;
+pub use crate::runtime::array_buffer::{
+ ArrayBuffer, DataBlock, Order as ArrayBufferOrder, Shared as ArrayBufferShared,
+};
+pub use crate::runtime::completion::{Throw, ThrowCompletionOr};
+pub use crate::runtime::error::ErrorKind;
+pub use crate::runtime::error_types::ErrorType;
+pub use crate::runtime::intrinsics::Intrinsics;
+pub use crate::runtime::object::Object;
+pub use crate::runtime::primitive_string::PrimitiveString;
+pub use crate::runtime::property_attributes::{Attribute, PropertyAttributes};
+pub use crate::runtime::property_key::PropertyKey;
+pub use crate::runtime::realm::Realm;
+pub use crate::runtime::typed_array::{
+ Kind as TypedArrayKind, TypedArrayBase, TypedArrayWithBufferWitness, create_typed_array_on_buffer,
+ is_typed_array_out_of_bounds, make_typed_array_with_buffer_witness_record, typed_array_byte_length,
+};
+pub use crate::utf16::{Utf16Display, to_well_formed};
+/// Derives Trace for a type of the embedder, which marks the type `#[gc(crate = libjs_rust)]`.
+pub use libjs_runtime_macros::Trace;
diff --git a/Libraries/LibJS/Rust/src/lib.rs b/Libraries/LibJS/Rust/src/lib.rs
index ecfcee1fa22..c7a8394a5d8 100644
--- a/Libraries/LibJS/Rust/src/lib.rs
+++ b/Libraries/LibJS/Rust/src/lib.rs
@@ -92,6 +92,8 @@ macro_rules! utf16 {
}};
}
+#[cfg(not(test))]
+pub mod api;
pub mod ast;
pub mod ast_dump;
#[cfg(not(test))]
diff --git a/Libraries/LibJS/Rust/src/runtime/array_buffer.rs b/Libraries/LibJS/Rust/src/runtime/array_buffer.rs
index 572e6155fea..302a8de0a61 100644
--- a/Libraries/LibJS/Rust/src/runtime/array_buffer.rs
+++ b/Libraries/LibJS/Rust/src/runtime/array_buffer.rs
@@ -6,6 +6,7 @@
use core::cell::Cell;
use core::ffi::c_void;
+use core::mem::MaybeUninit;
use core::num::NonZeroU64;
use core::ops::Deref;
use core::ptr::NonNull;
@@ -936,6 +937,46 @@ impl ArrayBuffer {
array_buffer
}
+ /// A buffer of `byte_length` bytes that `write` fills in place, as an encoder fills the buffer it returns, so that
+ /// the bytes are not zeroed only to be overwritten. `write` returns the number of bytes it initialized, which has
+ /// to be every byte it is given, as nothing else initializes them before the buffer is returned. Throws a
+ /// RangeError when the bytes cannot be allocated, where create_from_bytes() aborts.
+ pub fn try_create_written_by(
+ vm: &Vm,
+ realm: Gc<Realm>,
+ byte_length: usize,
+ is_shared: Shared,
+ write: impl FnOnce(&mut [MaybeUninit<u8>]) -> usize,
+ ) -> ThrowCompletionOr<Gc<ArrayBuffer>> {
+ let Ok(buffer) = allocate_or_retry_after_gc(vm.heap(), || OwnedBackingStore::create_uninitialized(byte_length))
+ else {
+ return vm.throw_completion(
+ ErrorKind::RangeError,
+ ErrorType::NotEnoughMemoryToAllocate,
+ &[&byte_length],
+ );
+ };
+ let prototype = prototype_for_shared_state(vm, realm, is_shared);
+ let array_buffer = realm.create_object(vm, ArrayBuffer::new_with_buffer(vm, buffer, is_shared, prototype));
+ array_buffer.with_data_block(|data_block| {
+ let written = if byte_length == 0 {
+ write(&mut [])
+ } else {
+ let data = data_block.data_at(0).cast::<MaybeUninit<u8>>();
+ assert!(!data.is_null() && data_block.size() == byte_length);
+ // SAFETY: The new block has `byte_length` bytes, which nothing else references yet.
+ write(unsafe { core::slice::from_raw_parts_mut(data, byte_length) })
+ };
+ assert_eq!(
+ written, byte_length,
+ "the writer initializes every byte of the new buffer"
+ );
+ });
+ vm.heap()
+ .did_allocate_external_memory(array_buffer.external_memory_size());
+ Ok(array_buffer)
+ }
+
/// ArrayBuffer::create(Realm&, DataBlock)
pub fn create_from_data_block(vm: &Vm, realm: Gc<Realm>, block: DataBlock) -> Gc<ArrayBuffer> {
let is_shared = block.is_shared;
diff --git a/Libraries/LibJS/Rust/src/runtime/native_function.rs b/Libraries/LibJS/Rust/src/runtime/native_function.rs
index 55b44d3a7b9..0fcb1176a71 100644
--- a/Libraries/LibJS/Rust/src/runtime/native_function.rs
+++ b/Libraries/LibJS/Rust/src/runtime/native_function.rs
@@ -629,7 +629,9 @@ pub fn call_raw_native_function(function: RawNativeFunctionPointer, vm: &Vm) ->
}
/// Turns a `fn(&Vm) -> ThrowCompletionOr<Value>` into the RawNativeFunctionPointer the interpreter calls a raw native
-/// function through, with the calling convention of LibJS's NativeFunctionPointer on the target.
+/// function through, with the calling convention of LibJS's NativeFunctionPointer on the target. Embedders reach it
+/// as api::raw_native.
+#[macro_export]
macro_rules! raw_native {
($function:expr) => {{
#[cfg(not(any(all(target_arch = "x86_64", target_vendor = "apple"), target_os = "windows")))]
@@ -667,7 +669,7 @@ macro_rules! raw_native {
}};
}
-pub(crate) use raw_native;
+pub(crate) use crate::raw_native;
pub static RAW_NATIVE_FUNCTION_VIRTUAL_METHODS: NativeFunctionMethods = NativeFunctionMethods {
call: RawNativeFunction::call,
diff --git a/Meta/Linters/check_libjs_rust_api.py b/Meta/Linters/check_libjs_rust_api.py
new file mode 100755
index 00000000000..c9be7fccbd4
--- /dev/null
+++ b/Meta/Linters/check_libjs_rust_api.py
@@ -0,0 +1,202 @@
+#!/usr/bin/env python3
+"""Keeps Rust code outside LibJS on the embedding API of the Rust LibJS runtime.
+
+The libjs_rust crate is the runtime itself, so nearly all of it is pub for the sake of its own modules. The part that
+embedders use is the api module, which lists the types, traits and functions the runtime supports an embedder with.
+Every path into the crate from outside Libraries/LibJS has to go through the api module, which the generated bindings
+of LibWeb follow as well, so the generators are checked for the Rust they write. The crate cannot be given another name
+either, as a path through that name would escape the check. Comments and the string literals of Rust code are left
+out; the strings of the generators are not, as that is where their Rust is.
+"""
+
+import io
+import pathlib
+import re
+import subprocess
+import sys
+import tokenize
+
+REPO_ROOT = pathlib.Path(__file__).resolve().parents[2]
+
+CRATE = "libjs_rust"
+API_MODULE = "api"
+
+EXEMPT_PATHS = ("Libraries/LibJS/",)
+
+CHECKED_SUFFIXES = {".rs", ".py"}
+
+# The crate's name as a whole word.
+CRATE_NAME = re.compile(rf"(?<![A-Za-z0-9_]){CRATE}\b")
+
+# A `::` right before the name that follows a name of its own, which makes the crate's name a segment of a longer
+# path, such as a module called libjs_rust in some other crate, rather than the crate. A `::` after a space, as in
+# `use ::libjs_rust`, starts an absolute path, whose first segment is the crate.
+SEGMENT_OF_A_LONGER_PATH = re.compile(r"[A-Za-z0-9_>)\]]::\s*$")
+
+PATH_SEPARATOR = re.compile(r"\s*::\s*")
+
+RENAME = re.compile(r"\s+as\b")
+
+IDENTIFIER = re.compile(r"[A-Za-z_][A-Za-z0-9_]*")
+
+RUST_RAW_STRING_START = re.compile(r"b?r(#*)\"")
+
+RUST_CHARACTER_LITERAL = re.compile(r"'(?:[^'\\\n]|\\(?:[^u]|u\{[0-9a-fA-F]+\}))'")
+
+
+def tracked_paths():
+ output = subprocess.check_output(["git", "ls-files"], cwd=REPO_ROOT, text=True)
+ for line in output.splitlines():
+ path = pathlib.PurePosixPath(line)
+ if not line.startswith(EXEMPT_PATHS) and path.suffix in CHECKED_SUFFIXES:
+ yield path
+
+
+def blanked(text, start, end):
+ """The text with the characters of [start, end) other than newlines replaced by spaces, so that every offset and
+ line number of what remains is unchanged."""
+ return text[:start] + re.sub(r"[^\n]", " ", text[start:end]) + text[end:]
+
+
+def rust_code_only(text):
+ """The Rust source without its comments and its string and character literals."""
+ index = 0
+ while index < len(text):
+ if text.startswith("//", index):
+ end = text.find("\n", index)
+ end = len(text) if end == -1 else end
+ elif text.startswith("/*", index):
+ depth = 0
+ end = index
+ while end < len(text):
+ if text.startswith("/*", end):
+ depth += 1
+ end += 2
+ elif text.startswith("*/", end):
+ depth -= 1
+ end += 2
+ if depth == 0:
+ break
+ else:
+ end += 1
+ elif (raw_string := RUST_RAW_STRING_START.match(text, index)) is not None:
+ closing = '"' + raw_string.group(1)
+ end = text.find(closing, raw_string.end())
+ end = len(text) if end == -1 else end + len(closing)
+ elif text.startswith('"', index) or text.startswith('b"', index):
+ end = text.index('"', index) + 1
+ while end < len(text) and text[end] != '"':
+ end += 2 if text[end] == "\\" else 1
+ end += 1
+ elif (character := RUST_CHARACTER_LITERAL.match(text, index)) is not None:
+ end = character.end()
+ else:
+ index += 1
+ continue
+ text = blanked(text, index, end)
+ index = end
+ return text
+
+
+def python_code_only(text):
+ """The Python source without its comments. Its strings stay, as the generators write Rust in them."""
+ line_offsets = [0]
+ for line in text.splitlines(keepends=True):
+ line_offsets.append(line_offsets[-1] + len(line))
+ try:
+ tokens = list(tokenize.generate_tokens(io.StringIO(text).readline))
+ except (tokenize.TokenError, SyntaxError):
+ return text
+ for token in tokens:
+ if token.type == tokenize.COMMENT:
+ start = line_offsets[token.start[0] - 1] + token.start[1]
+ end = line_offsets[token.end[0] - 1] + token.end[1]
+ text = blanked(text, start, end)
+ return text
+
+
+def brace_group(text, start):
+ """The text of the balanced `{...}` group that starts at `start`, without the braces, or None if it never closes."""
+ depth = 0
+ for index in range(start, len(text)):
+ if text[index] == "{":
+ depth += 1
+ elif text[index] == "}":
+ depth -= 1
+ if depth == 0:
+ return text[start + 1 : index]
+ return None
+
+
+def split_at_top_level_commas(group):
+ items = []
+ depth = 0
+ item_start = 0
+ for index, character in enumerate(group):
+ if character == "{":
+ depth += 1
+ elif character == "}":
+ depth -= 1
+ elif character == "," and depth == 0:
+ items.append(group[item_start:index])
+ item_start = index + 1
+ items.append(group[item_start:])
+ return [item.strip() for item in items if item.strip()]
+
+
+def names_the_api_module(path):
+ """Whether a path relative to the crate is the api module or something in it: `api`, `api::Vm`, `api as js`."""
+ match = IDENTIFIER.match(path)
+ return match is not None and match.group(0) == API_MODULE
+
+
+def disallowed_uses(code):
+ """Yields `(offset, use)` for every path into the crate that does not go through the api module, and for every
+ use of the crate under another name."""
+ for match in CRATE_NAME.finditer(code):
+ if SEGMENT_OF_A_LONGER_PATH.search(code, 0, match.start()):
+ continue
+ if (separator := PATH_SEPARATOR.match(code, match.end())) is not None:
+ path_start = separator.end()
+ if code.startswith("{", path_start):
+ group = brace_group(code, path_start)
+ if group is None:
+ yield match.start(), code[match.start() : path_start + 1]
+ continue
+ for item in split_at_top_level_commas(group):
+ if not names_the_api_module(item):
+ yield match.start(), f"{CRATE}::{{{item}}}"
+ elif (segment := IDENTIFIER.match(code, path_start)) is not None:
+ if segment.group(0) != API_MODULE:
+ yield match.start(), f"{CRATE}::{segment.group(0)}"
+ elif code.startswith("*", path_start):
+ yield match.start(), f"{CRATE}::*"
+ elif RENAME.match(code, match.end()) is not None:
+ yield match.start(), f"{CRATE} as ..."
+
+
+def main():
+ failures = []
+ for path in tracked_paths():
+ text = (REPO_ROOT / path).read_text(encoding="utf-8", errors="ignore")
+ if CRATE not in text:
+ continue
+ code = rust_code_only(text) if path.suffix == ".rs" else python_code_only(text)
+ for offset, use in sorted(disallowed_uses(code)):
+ line_number = code.count("\n", 0, offset) + 1
+ failures.append(f"{path}:{line_number}: {use}")
+
+ for failure in failures:
+ print(failure)
+ if failures:
+ print()
+ print(
+ f"Code outside LibJS reaches the Rust runtime through {CRATE}::{API_MODULE}. If the API lacks something, "
+ "add it to Libraries/LibJS/Rust/src/api.rs instead of naming the runtime's own modules."
+ )
+ return 1
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/Meta/lint-ci.sh b/Meta/lint-ci.sh
index 6120507e952..26426f53f82 100755
--- a/Meta/lint-ci.sh
+++ b/Meta/lint-ci.sh
@@ -36,6 +36,7 @@ for cmd in \
Meta/Linters/check_html_doctype.py \
Meta/Linters/check_idl_files.py \
Meta/Linters/check_libjs_includes.py \
+ Meta/Linters/check_libjs_rust_api.py \
Meta/Linters/check_libweb_realm_mentions.py \
Meta/Linters/check_newlines_at_eof.py \
Meta/Linters/check_png_sizes.sh \