From a43efff7496af145518417ae706655f10a3b5893 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Fri, 20 Nov 2026 11:17:15 -0800 Subject: [PATCH 01/19] build: qualify pinned V8 archives for Linux and macOS --- .github/workflows/v8-artifacts.yml | 140 +++++++ scripts/ci/v8/.gitignore | 1 + scripts/ci/v8/README.md | 99 +++++ scripts/ci/v8/archives.json | 1 + scripts/ci/v8/cppgc-generated-config.patch | 20 + scripts/ci/v8/engine.py | 419 +++++++++++++++++++++ scripts/ci/v8/node-wire-format-15.patch | 117 ++++++ scripts/ci/v8/source.json | 25 ++ scripts/ci/v8/test_engine.py | 159 ++++++++ 9 files changed, 981 insertions(+) create mode 100644 .github/workflows/v8-artifacts.yml create mode 100644 scripts/ci/v8/.gitignore create mode 100644 scripts/ci/v8/README.md create mode 100644 scripts/ci/v8/archives.json create mode 100644 scripts/ci/v8/cppgc-generated-config.patch create mode 100644 scripts/ci/v8/engine.py create mode 100644 scripts/ci/v8/node-wire-format-15.patch create mode 100644 scripts/ci/v8/source.json create mode 100644 scripts/ci/v8/test_engine.py diff --git a/.github/workflows/v8-artifacts.yml b/.github/workflows/v8-artifacts.yml new file mode 100644 index 0000000000..214bd757d2 --- /dev/null +++ b/.github/workflows/v8-artifacts.yml @@ -0,0 +1,140 @@ +name: Build pinned V8 archives +on: + # Build a new pin on a dedicated same-repository branch before it reaches main. + # Once on main, workflow_dispatch covers future refreshes. + push: + branches: ['v8-artifacts/**'] + paths: + - scripts/ci/v8/source.json + - scripts/ci/v8/*.patch + - .github/workflows/v8-artifacts.yml + pull_request: + types: [labeled] + workflow_dispatch: + workflow_call: + +permissions: + contents: read + +jobs: + archive: + if: >- + github.event_name != 'pull_request' || + (github.event.label.name == 'build-pinned-v8' && + github.event.pull_request.head.repo.full_name == github.repository && + startsWith(github.event.pull_request.head.ref, 'v8-artifacts/')) + name: V8 archive (${{ matrix.target }}) + runs-on: ${{ matrix.runner }} + timeout-minutes: 360 + strategy: + fail-fast: false + matrix: + include: + - target: x86_64-unknown-linux-gnu + runner: depot-ubuntu-24.04-8 + - target: aarch64-unknown-linux-gnu + # Chromium's Linux host toolchain is distributed for x64. + runner: depot-ubuntu-24.04-8 + - target: x86_64-apple-darwin + runner: macos-15 + - target: aarch64-apple-darwin + runner: macos-15 + env: + TARGET: ${{ matrix.target }} + CARGO_BUILD_JOBS: '3' + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + ref: ${{ github.event.pull_request.head.sha || github.sha }} + - uses: dtolnay/rust-toolchain@01e2821fc38dbca62c2557c927b33938a2e005fd # 1.98.1 + with: + targets: ${{ matrix.target }} + - name: Prepare pinned engine source + run: python3 scripts/ci/v8/engine.py prepare "$RUNNER_TEMP/rusty-v8" + - name: Select libclang for bindgen (macOS) + if: runner.os == 'macOS' + run: | + brew install llvm@21 + echo "LIBCLANG_PATH=$(brew --prefix llvm@21)/lib" >> "$GITHUB_ENV" + - name: Enable x64 execution on the macOS arm64 host + if: matrix.target == 'x86_64-apple-darwin' + run: softwareupdate --install-rosetta --agree-to-license + - name: Select libclang for bindgen (Linux) + if: runner.os == 'Linux' + run: | + sudo apt-get update -qq + sudo apt-get install -y --no-install-recommends ca-certificates curl gnupg python-is-python3 + curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key -o "$RUNNER_TEMP/llvm.asc" + fingerprint=$(gpg --show-keys --with-colons "$RUNNER_TEMP/llvm.asc" | awk -F: '$1 == "pub" { primary=1 } $1 == "fpr" && primary { print $10; primary=0 }') + test "$fingerprint" = 6084F3CF814B57C1CF12EFD515CF4D18AF4F7421 + gpg --dearmor < "$RUNNER_TEMP/llvm.asc" | sudo tee /usr/share/keyrings/agentos-llvm.gpg >/dev/null + echo 'deb [signed-by=/usr/share/keyrings/agentos-llvm.gpg] https://apt.llvm.org/noble/ llvm-toolchain-noble-21 main' | sudo tee /etc/apt/sources.list.d/agentos-llvm.list + sudo apt-get update -qq + sudo apt-get install -y --no-install-recommends clang-21 libclang-21-dev + echo 'LIBCLANG_PATH=/usr/lib/llvm-21/lib' >> "$GITHUB_ENV" + echo "RUSTY_V8_BINDGEN_RESOURCE_DIR=$(clang-21 -print-resource-dir)" >> "$GITHUB_ENV" + - name: Install arm64 Linux cross compilation and execution tools + if: matrix.target == 'aarch64-unknown-linux-gnu' + run: | + sudo apt-get install -y --no-install-recommends g++-aarch64-linux-gnu libc6-dev-arm64-cross qemu-user + echo 'CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc' >> "$GITHUB_ENV" + echo 'CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUNNER=qemu-aarch64 -L /usr/aarch64-linux-gnu' >> "$GITHUB_ENV" + echo 'RUSTY_V8_GLIBC_PREFIX=/usr/aarch64-linux-gnu' >> "$GITHUB_ENV" + - name: Build release library and bindings + env: + V8_FROM_SOURCE: '1' + GN_ARGS: v8_generate_external_defines_header=true + run: | + cd "$RUNNER_TEMP/rusty-v8" + export CARGO_TARGET_DIR="$RUNNER_TEMP/v8-target" + export BINDGEN_EXTRA_CLANG_ARGS="-I$CARGO_TARGET_DIR/$TARGET/release/gn_out/gen/v8/include" + cargo +1.98.1 build --locked --lib --release --target "$TARGET" + - name: Package target inputs and verify the bundle + run: | + bundle="$RUNNER_TEMP/$TARGET.tar.gz" + python3 scripts/ci/v8/engine.py pack "$RUNNER_TEMP/rusty-v8" "$RUNNER_TEMP/v8-target" "$TARGET" "$bundle" > "$RUNNER_TEMP/$TARGET.sha256" + digest=$(cut -d' ' -f1 "$RUNNER_TEMP/$TARGET.sha256") + python3 scripts/ci/v8/engine.py install "$TARGET" "$RUNNER_TEMP/v8-verify" --bundle "$bundle" --sha256 "$digest" --github-env "$GITHUB_ENV" --cargo-config "$RUNNER_TEMP/v8-source.toml" + cat "$RUNNER_TEMP/$TARGET.sha256" >> "$GITHUB_STEP_SUMMARY" + - name: Link an independent consumer and check the engine version + run: | + expected=$(python3 -c 'import json;print(json.load(open("scripts/ci/v8/source.json"))["version"])') + revision=$(python3 -c 'import json;print(json.load(open("scripts/ci/v8/source.json"))["rustyV8"])') + mkdir -p "$RUNNER_TEMP/consumer/src" + python3 scripts/ci/v8/engine.py consumer-lock "$RUNNER_TEMP/rusty-v8" "$RUNNER_TEMP/consumer/Cargo.lock" + cd "$RUNNER_TEMP/consumer" + cat > Cargo.toml < src/main.rs <<'RS' + fn main() { + let native = v8::V8::get_version(); + assert_eq!(native, v8::VERSION_STRING, "archive and bindgen disagree"); + println!("{}", native.split('-').next().unwrap()); + } + RS + # Narrow the pinned upstream lockfile to this one-crate consumer while + # retaining the locked Git identity. Cargo resolves the vendored source. + cargo +1.98.1 --config "$RUNNER_TEMP/v8-source.toml" metadata --filter-platform "$TARGET" --format-version 1 > /dev/null + python3 "$GITHUB_WORKSPACE/scripts/ci/v8/engine.py" check-temporal-lock "$RUNNER_TEMP/rusty-v8/Cargo.lock" Cargo.lock + actual=$(CARGO_TARGET_DIR="$RUNNER_TEMP/v8-consumer" cargo +1.98.1 --config "$RUNNER_TEMP/v8-source.toml" run --locked --release --target "$TARGET") + test "$actual" = "$expected" + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + id: upload + with: + name: rusty-v8-${{ matrix.target }} + path: | + ${{ runner.temp }}/${{ matrix.target }}.tar.gz + ${{ runner.temp }}/${{ matrix.target }}.sha256 + compression-level: 0 + if-no-files-found: error + - name: Record the immutable Actions artifact ID + env: + ARTIFACT_ID: ${{ steps.upload.outputs.artifact-id }} + run: echo "Draft artifact ID for $TARGET is $ARTIFACT_ID" >> "$GITHUB_STEP_SUMMARY" diff --git a/scripts/ci/v8/.gitignore b/scripts/ci/v8/.gitignore new file mode 100644 index 0000000000..c18dd8d83c --- /dev/null +++ b/scripts/ci/v8/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/scripts/ci/v8/README.md b/scripts/ci/v8/README.md new file mode 100644 index 0000000000..d1ec22f600 --- /dev/null +++ b/scripts/ci/v8/README.md @@ -0,0 +1,99 @@ +# Building the agentOS V8 engine + +Normal PR, benchmark and sidecar release jobs use reviewed binary inputs. The +**Build pinned V8 archives** workflow builds them for GNU Linux and macOS on x64 +and arm64. It has read-only repository permissions. Each output contains the +static library, Rust bindings, public headers, generated GN configuration, ICU +common data, license notices and the Rust crate files from the pinned Git commit. +CI installs those crate files using Cargo's directory source replacement and +keeps the original Git identity in `Cargo.lock`. Consumer jobs can use the +binary inputs without cloning Chromium's native submodules. Before upload, a +separate Cargo build links the bundle and compares the native V8 version to +the generated binding and the pinned source version. + +Pip-Boy's dependency comparison fetches only the Rust sources for each +checkout's locked revision. A registry-only base uses Cargo's normal registry +resolution. Its platform size jobs pass the reviewed bundle to BuildKit as a +read-only named context and verify the digest inside the build; Actions +credentials stay on the runner. + +`source.json` identifies the upstream Rust bindings, the Deno engine base, the +ordered upstream patch commits, each agentOS patch digest, the resulting V8 +Git tree and the ICU revision. The cppgc patch includes the generated build +configuration before its first feature check. The Node serializer patch lets +the agentOS `node:v8` writer emit wire format 15 for interchange with Node 24 +and 26. V8's default writer still emits format 16. The native reader accepts +both formats. +`prepare` checks the tree and the remaining Git submodule revisions. Windows-only +DebugVisualizers are unused for these targets. + +## Refreshing the archives + +1. Review the pinned source and run **Build pinned V8 archives** at that commit. + A push that changes the source pin, patches or build workflow on a + same-repository branch under `v8-artifacts/` starts the four builds. A + maintainer can apply the `build-pinned-v8` + label to that PR to build its current head explicitly, including after + correcting a build script. Check the run's repository, checked-out head SHA, and each + target's successful linked version check. The job summary gives the Actions + artifact ID and SHA-256 of the inner `.tar.gz`. + Draft CI can run before release promotion by adding + `{"artifactId": , "sha256": ""}` for each target in + `archives.json`. Its read-only Actions token downloads that exact ID; + the installer independently checks the tar. Updating only the digest file + leaves the four source builds idle. +2. Copy each unchanged `.tar.gz` to the release origin under + `agent-os/v8///.tar.gz`. The same origin + serves the shipped sidecar binaries. Verify the downloaded bytes match the + SHA-256 from the trusted workflow before adding the four target/digest pairs + to `archives.json` as plain digest strings in a reviewed change. Actions + artifacts expire. The release preflight and packaging containers require + the durable form for all published platforms. +3. Run the normal sidecar builds, glibc-floor and old-container checks and + the executor tests. An absent digest stops CI before it can select an + upstream binary with the same crate version. No digest is credited until + that target has actually been built and inspected. +4. Before a crates.io release, qualify an independent, clean Rust consumer + using the registry dependency and its default artifact resolution. Cargo + ignores this workspace's Git patch in downstream crates. The publish + workflow checks for this before any real release job can publish. + +## Running locally + +Use Python 3.8 or later. Source builds need the corresponding rusty_v8 native +prerequisites, including libclang 21.1+ for bindgen. On Linux leave +`CLANG_BASE_PATH` unset to use the pinned Chromium compiler. Set `LIBCLANG_PATH` +to the host's libclang directory. On Linux, set `RUSTY_V8_BINDGEN_RESOURCE_DIR` +to the output of that Clang installation's `clang -print-resource-dir`. The +Rust bindings also need the target-specific `v8-gn.h` that GN generates during the build. + +```sh +repo=$PWD +target=x86_64-unknown-linux-gnu +python3 scripts/ci/v8/engine.py prepare "$repo/target/rusty-v8-source" +( + cd "$repo/target/rusty-v8-source" + export CARGO_TARGET_DIR="$repo/target/v8-build" + export BINDGEN_EXTRA_CLANG_ARGS="-I$CARGO_TARGET_DIR/$target/release/gn_out/gen/v8/include" + V8_FROM_SOURCE=1 GN_ARGS=v8_generate_external_defines_header=true \ + cargo +1.98.1 build --jobs 2 --locked --release --lib --target "$target" +) +python3 scripts/ci/v8/engine.py pack "$repo/target/rusty-v8-source" \ + "$repo/target/v8-build" "$target" "$repo/target/$target.tar.gz" +``` + +To use an approved archive from the durable origin: + +```sh +python3 scripts/ci/v8/engine.py install "$target" "$repo/target/v8-bundles" \ + --cargo-config "$repo/target/v8-source.toml" \ + --shell-env "$repo/target/v8-build.env" +. "$repo/target/v8-build.env" +cargo --config "$repo/target/v8-source.toml" build --locked -p agentos-sidecar +``` + +A draft checkout using temporary pins can add `--allow-actions` with an +authenticated `gh` CLI that has read access to the artifact. A locally qualified build can use the same install command with +`--bundle --sha256 `. The installer verifies the +entire archive before extracting and checks all inputs against its manifest. The +activation file pins both Cargo's library source and the generated Rust binding. diff --git a/scripts/ci/v8/archives.json b/scripts/ci/v8/archives.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/scripts/ci/v8/archives.json @@ -0,0 +1 @@ +{} diff --git a/scripts/ci/v8/cppgc-generated-config.patch b/scripts/ci/v8/cppgc-generated-config.patch new file mode 100644 index 0000000000..b11db84f91 --- /dev/null +++ b/scripts/ci/v8/cppgc-generated-config.patch @@ -0,0 +1,20 @@ +diff --git a/src/heap/cppgc-internal/gc-info.cc b/src/heap/cppgc-internal/gc-info.cc +index adbcfafc64..031b37cbd7 100644 +--- a/src/heap/cppgc-internal/gc-info.cc ++++ b/src/heap/cppgc-internal/gc-info.cc +@@ -1,12 +1,13 @@ +-#if !defined(CPPGC_ENABLE_OBJECT_SECTION_GCINFO) + // Copyright 2020 the V8 project authors. All rights reserved. + // Use of this source code is governed by a BSD-style license that can be + // found in the LICENSE file. + ++#include "include/v8config.h" ++ ++#if !defined(CPPGC_ENABLE_OBJECT_SECTION_GCINFO) + #include "include/cppgc/internal/gc-info.h" + + #include "include/cppgc/internal/name-trait.h" +-#include "include/v8config.h" + #include "src/heap/cppgc-internal/gc-info-table.h" + + namespace cppgc::internal { diff --git a/scripts/ci/v8/engine.py b/scripts/ci/v8/engine.py new file mode 100644 index 0000000000..9ff50ee2ee --- /dev/null +++ b/scripts/ci/v8/engine.py @@ -0,0 +1,419 @@ +#!/usr/bin/env python3 +"""Prepare the locked engine tree and package or consume one matched target bundle.""" +import argparse +from contextlib import contextmanager +import gzip +import hashlib +import json +import os +from pathlib import Path +import re +import shutil +import subprocess +import sys +import tarfile +import tempfile +import urllib.request +import zipfile + +HERE = Path(__file__).resolve().parent +SOURCE = json.loads((HERE / "source.json").read_text()) +TARGETS = ("x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", + "x86_64-apple-darwin", "aarch64-apple-darwin") +ICU = "third_party/icu/common/icudtl.dat" +NOTICE_PATTERNS = ("LICENSE", "v8/LICENSE*", "third_party/*/LICENSE*", "third_party/*/src/LICENSE*") + + +def git(root, *args): + return subprocess.check_output(["git", "-C", str(root), *args], text=True).strip() + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def digest(path): + result = hashlib.sha256() + with path.open("rb") as stream: + for chunk in iter(lambda: stream.read(1024 * 1024), b""): + result.update(chunk) + return result.hexdigest() + + +def gitlinks(root): + return {path: revision for attrs in git(root, "ls-tree", "-r", "HEAD").splitlines() + for mode, kind, revision, path in [attrs.split(maxsplit=3)] if kind == "commit"} + + +def verify_source(root): + source_patches() + require(git(root, "rev-parse", "HEAD") == SOURCE["rustyV8"], "incorrect rusty_v8 revision") + git(root, "diff", "--quiet", "--ignore-submodules=all", "HEAD") + v8 = root / "v8" + require(git(v8, "write-tree") == SOURCE["v8Tree"], "incorrect patched V8 tree") + git(v8, "diff", "--quiet") + links = gitlinks(root) + require(links["third_party/icu"] == SOURCE["icu"], "incorrect ICU pin") + for path, revision in links.items(): + if path in ("v8", "tools/win"): + continue + require(git(root / path, "rev-parse", "HEAD") == revision, "incorrect revision: " + path) + git(root / path, "diff", "--quiet", "HEAD") + + +def source_patches(): + paths = [] + for patch in SOURCE["agentosPatches"]: + path = HERE / patch["file"] + require(digest(path) == patch["sha256"], f"agentOS V8 patch SHA-256 mismatch: {path.name}") + paths.append(path) + return paths + + +def install_rust_glue(checkout, dest, config): + # Base and PR checks may have different pins, or a released registry crate. + # Interpret the lock and pin from the checkout that Cargo will analyze. + locked = [entry for entry in re.split(r"^\[\[package\]\]$", (checkout / "Cargo.lock").read_text(), flags=re.M) + if re.search(r'^name = "v8"$', entry, re.M) and re.search(r'^source = "git\+', entry, re.M)] + if not locked: + return + revision = json.loads((checkout / "scripts/ci/v8/source.json").read_text())["rustyV8"] + require(re.fullmatch("[0-9a-f]{40}", revision), "invalid rusty_v8 revision") + repo = "https://github.com/denoland/rusty_v8" + identity = f'source = "git+{repo}?rev={revision}#{revision}"' + require(len(locked) == 1 and identity in locked[0].splitlines(), "rusty_v8 lock does not match this checkout's source pin") + dest.mkdir(parents=True, exist_ok=False) + vendor = dest / "vendor/v8" + vendor.mkdir(parents=True) + with tempfile.TemporaryDirectory(dir=dest) as work: + root = Path(work) + git(root, "init", "-q") + git(root, "fetch", "--no-recurse-submodules", "--no-tags", "--depth=1", repo, revision) + git(root, "checkout", "--no-recurse-submodules", "--detach", "FETCH_HEAD") + require(git(root, "rev-parse", "HEAD") == revision, "incorrect fetched Rust glue revision") + inputs = rust_crate_inputs(root) + for name, path in inputs.items(): + dst = vendor / name + dst.parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(path, dst) + (vendor / ".cargo-checksum.json").write_text(json.dumps( + {"package": None, "files": {name: digest(path) for name, path in inputs.items()}}, sort_keys=True)) + write_cargo_config(config, dest, revision) + + +def prepare(root): + require(not root.exists(), "prepare requires a new checkout directory") + root.mkdir(parents=True) + git(root, "init", "-q") + git(root, "remote", "add", "origin", "https://github.com/denoland/rusty_v8.git") + git(root, "fetch", "--depth=1", "--no-tags", "origin", SOURCE["rustyV8"]) + git(root, "checkout", "--detach", "FETCH_HEAD") + modules = sorted(gitlinks(root).keys() - {"v8", "tools/win"}) + git(root, "submodule", "update", "--init", "--depth=1", "--jobs=4", "--", *modules) + v8 = root / "v8" + git(v8, "init", "-q") + git(v8, "fetch", "--no-tags", "--depth=1", "https://github.com/denoland/v8.git", SOURCE["denoV8"]) + git(v8, "checkout", "--detach", "FETCH_HEAD") + patches = SOURCE["upstreamPatches"] + git(v8, "fetch", "--no-tags", "--depth=" + str(len(patches) + 1), + "https://github.com/v8/v8.git", patches[-1]) + git(v8, "cherry-pick", "--no-commit", *patches) + for patch in source_patches(): + git(v8, "apply", "--index", str(patch)) + verify_source(root) + + +def filenames(target): + return "librusty_v8_release_" + target + ".a", "src_binding_release_" + target + ".rs" + + +def rust_crate_inputs(source): + # Only the pinned Rust repository's blobs; a gitlink represents an entire + # native submodule and is intentionally absent from the binary consumer. + inputs = {} + for entry in git(source, "ls-tree", "-rz", "HEAD").split("\0"): + if entry: + attrs, name = entry.split("\t", 1) + mode, kind, _ = attrs.split() + if kind == "blob": + require(mode in ("100644", "100755"), "unexpected non-file Rust crate input: " + name) + inputs[name] = source / name + return inputs + + +def pack(source, build, target, output): + verify_source(source) + gn = build / target / "release" / "gn_out" + args = (gn / "args.gn").read_text() + settings = dict(re.findall(r"^(\w+)\s*=\s*(\S+)\s*$", args, re.M)) + expected = {"is_debug": "false", "v8_enable_sandbox": "false", "use_custom_libcxx": "true", + "v8_enable_pointer_compression": "false", "v8_enable_v8_checks": "false", + "v8_generate_external_defines_header": "true", + "target_cpu": '"arm64"' if target.startswith("aarch64") else '"x64"'} + require(all(settings.get(k) == v for k, v in expected.items()), "wrong GN ABI or target") + if "apple" in target: + require(sys.platform == "darwin", "Darwin archive must be produced on macOS") + else: + require(sys.platform.startswith("linux"), "Linux archive must be produced on Linux") + archive, binding = filenames(target) + inputs = {archive: gn / "obj/librusty_v8.a", binding: gn / "src_binding.rs", + "generated/v8-gn.h": gn / "gen/v8/include/v8-gn.h", ICU: source / ICU} + for header in sorted((source / "v8/include").rglob("*.h")): + inputs[header.relative_to(source).as_posix()] = header + for pattern in NOTICE_PATTERNS: + for notice in source.glob(pattern): + if notice.is_file(): + inputs["licenses/" + notice.relative_to(source).as_posix()] = notice + output.parent.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(dir=output.parent) as work: + work = Path(work) + crate = rust_crate_inputs(source) + checksums = work / ".cargo-checksum.json" + checksums.write_text(json.dumps({"package": None, "files": {name: digest(path) for name, path in crate.items()}}, sort_keys=True)) + inputs.update({"vendor/v8/" + name: path for name, path in crate.items()}) + inputs["vendor/v8/.cargo-checksum.json"] = checksums + receipt = {"source": SOURCE, "target": target, "gn": expected, + "sha256": {name: digest(path) for name, path in inputs.items()}} + manifest = work / "provenance.json" + manifest.write_text(json.dumps(receipt, sort_keys=True, indent=2) + "\n") + inputs[manifest.name] = manifest + packed = work / "bundle.tar.gz" + with packed.open("wb") as raw, gzip.GzipFile(filename="", mode="wb", fileobj=raw, mtime=0, compresslevel=1) as zipped: + with tarfile.open(fileobj=zipped, mode="w|") as tar: + for name, path in sorted(inputs.items()): + info = tarfile.TarInfo(name) + info.size, info.mode = path.stat().st_size, 0o644 + with path.open("rb") as data: + tar.addfile(info, data) + os.replace(packed, output) + print(digest(output), output.name) + + +def verify_bundle(root, target): + manifest = json.loads((root / "provenance.json").read_text()) + require(manifest["source"] == SOURCE and manifest["target"] == target, "V8 source or target mismatch") + archive, binding = filenames(target) + required = {archive, binding, "generated/v8-gn.h", "v8/include/v8.h", ICU, + "licenses/LICENSE", "licenses/v8/LICENSE", "licenses/third_party/icu/LICENSE", + "vendor/v8/Cargo.toml", "vendor/v8/build.rs", "vendor/v8/src/lib.rs", "vendor/v8/.cargo-checksum.json"} + hashes = manifest["sha256"] + require(required <= hashes.keys(), "incomplete V8 bundle") + present = {p.relative_to(root).as_posix() for p in root.rglob("*") if p.is_file()} + require(present == hashes.keys() | {"provenance.json"}, "unexpected or missing V8 bundle files") + for name, checksum in hashes.items(): + notice = name.startswith("licenses/") and Path(name).name.startswith("LICENSE") + require(name in required or notice or name.startswith("vendor/v8/") or (name.startswith("v8/include/") and name.endswith(".h")), "unexpected bundle file " + name) + require(digest(root / name) == checksum, "V8 bundle digest mismatch: " + name) + + +def unpack(bundle, root): + with tarfile.open(bundle, "r:gz") as tar: + for member in tar: + path = Path(member.name) + require(member.isfile() and not path.is_absolute() and ".." not in path.parts, "unexpected archive entry") + dest = root / path + dest.parent.mkdir(parents=True, exist_ok=True) + with tar.extractfile(member) as data, dest.open("xb") as out: + shutil.copyfileobj(data, out) + + +def download_action_bundle(artifact_id, target, work): + require(type(artifact_id) is int and artifact_id > 0, "invalid V8 Actions artifact ID") + zip_path = work / "artifact.zip" + with zip_path.open("wb") as out: + subprocess.run(["gh", "api", f"repos/rivet-dev/agentos/actions/artifacts/{artifact_id}/zip"], + stdout=out, check=True) + with zipfile.ZipFile(zip_path) as archive: + # Read the one expected entry directly; never extract arbitrary ZIP paths. + with archive.open(target + ".tar.gz") as data, (work / "bundle").open("wb") as out: + shutil.copyfileobj(data, out) + + +def pin_for(target, allow_actions): + pin = json.loads((HERE / "archives.json").read_text()).get(target) + require(pin, "No reviewed V8 artifact SHA-256 for " + target + ". See scripts/ci/v8/README.md.") + if isinstance(pin, dict): + require(allow_actions, "Release builds require a durable V8 archive pin for " + target) + require(pin.keys() == {"sha256", "artifactId"}, "invalid temporary V8 artifact pin") + require(type(pin["artifactId"]) is int and pin["artifactId"] > 0, "invalid V8 Actions artifact ID") + checksum, artifact_id = pin["sha256"], pin["artifactId"] + else: + checksum, artifact_id = pin, None + require(re.fullmatch(r"[a-f0-9]{64}", checksum or ""), "invalid reviewed V8 checksum") + return checksum, artifact_id + + +@contextmanager +def reviewed_archive(target, dest, allow_actions): + checksum, artifact_id = pin_for(target, allow_actions) + with tempfile.TemporaryDirectory(dir=dest) as work: + work = Path(work) + if artifact_id is not None: + download_action_bundle(artifact_id, target, work) + else: + url = f"https://releases.rivet.dev/agent-os/v8/{SOURCE['v8Tree']}/{checksum}/{target}.tar.gz" + with urllib.request.urlopen(url, timeout=120) as response, (work / "bundle").open("wb") as out: + shutil.copyfileobj(response, out) + require(digest(work / "bundle") == checksum, "V8 archive SHA-256 mismatch") + yield work / "bundle", checksum + + +def install(target, dest, local_bundle=None, expected_digest=None, allow_actions=False): + dest.mkdir(parents=True, exist_ok=True) + if local_bundle is None: + require(expected_digest is None, "--sha256 requires --bundle") + with reviewed_archive(target, dest, allow_actions) as (bundle, checksum): + return install_verified(bundle, checksum, target, dest) + require(re.fullmatch(r"[a-f0-9]{64}", expected_digest or ""), "expected an independent bundle SHA-256") + require(digest(local_bundle) == expected_digest, "V8 archive SHA-256 mismatch") + return install_verified(local_bundle, expected_digest, target, dest) + + +def install_verified(local_bundle, expected_digest, target, dest): + with tempfile.TemporaryDirectory(dir=dest) as work: + work = Path(work) + root = work / "verified" + root.mkdir() + unpack(local_bundle, root) + verify_bundle(root, target) + ready = dest / expected_digest + try: + root.rename(ready) + except OSError: + if not ready.is_dir(): + raise + # Another consumer installed the same pinned bytes. Validate every + # existing file against the newly authenticated manifest. + require((ready / "provenance.json").read_bytes() == (root / "provenance.json").read_bytes(), "cached V8 provenance mismatch") + verify_bundle(ready, target) + return verified_env_paths(ready, target) + + +def verified_env_paths(ready, target): + archive, binding = filenames(target) + hashes = json.loads((ready / "provenance.json").read_text())["sha256"] + return {"RUSTY_V8_ARCHIVE": str(ready / archive), "RUSTY_V8_ARCHIVE_SHA256": hashes[archive], + "RUSTY_V8_SRC_BINDING_PATH": str(ready / binding), "AGENTOS_RUSTY_V8_SOURCE": str(ready), + "AGENTOS_RUSTY_V8_GN_INCLUDE": str(ready / "generated"), "V8_FROM_SOURCE": "0", + "RUSTY_V8_SKIP_DOWNLOAD": "0", "V8_FORCE_DEBUG": "0"} + + +def write_cargo_config(path, root, revision=None): + revision = revision or SOURCE["rustyV8"] + config = ('[source.agentos-rusty-v8]\ngit = "https://github.com/denoland/rusty_v8"\n' + f'rev = "{revision}"\nreplace-with = "agentos-v8-vendor"\n' + f'[source.agentos-v8-vendor]\ndirectory = {json.dumps(str(root / "vendor"))}\n') + path.parent.mkdir(parents=True, exist_ok=True) + try: + with path.open("x") as out: + out.write(config) + except FileExistsError: + require(path.read_text() == config, f"Cargo config already exists at {path}; specify a new --cargo-config path") + + +def write_consumer_lock(source, output): + # Cargo needs a locked Git identity before it can use a directory source. + # The producer's lock already has the complete dependency graph; its root + # package becomes a Git dependency only in the standalone version check. + revision = SOURCE["rustyV8"] + data = git(source, "show", revision + ":Cargo.lock") + entry = r'^(\[\[package\]\]\nname = "v8"\nversion = "[^"\n]+"\n)(?=dependencies = \[)' + identity = f'source = "git+https://github.com/denoland/rusty_v8?rev={revision}#{revision}"\n' + locked, count = re.subn(entry, lambda m: m.group(1) + identity, data, flags=re.M) + require(count == 1, "expected one local v8 package in the pinned rusty_v8 Cargo.lock") + with output.open("x") as out: + out.write(locked + "\n") + + +def temporal_dependencies(lock): + names = {"temporal_capi", "temporal_rs", "timezone_provider", "zoneinfo64", "diplomat", "diplomat-runtime"} + result = {} + for package in re.split(r"^\[\[package\]\]$", lock.read_text(), flags=re.M): + entry = dict(re.findall(r'^(name|version|checksum) = "([^"]+)"$', package, re.M)) + name = entry.get("name") + if name in names: + require(name not in result, f"ambiguous Temporal dependency {name}") + result[name] = (entry["version"], entry["checksum"]) + require(result.keys() == names, "missing Temporal ABI dependency in Cargo.lock") + return result + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + cmds = parser.add_subparsers(dest="command", required=True) + consumer_lock = cmds.add_parser("consumer-lock") + consumer_lock.add_argument("source", type=Path) + consumer_lock.add_argument("output", type=Path) + temporal = cmds.add_parser("check-temporal-lock") + temporal.add_argument("producer", type=Path) + temporal.add_argument("consumer", type=Path) + glue = cmds.add_parser("install-glue", help="prepare only Rust source for metadata tools") + glue.add_argument("checkout", type=Path) + glue.add_argument("dest", type=Path) + glue.add_argument("--cargo-config", required=True, type=Path) + fetch = cmds.add_parser("fetch", help="stage a reviewed bundle for a separate build context") + fetch.add_argument("target", choices=TARGETS) + fetch.add_argument("dest", type=Path) + fetch.add_argument("--allow-actions", action="store_true") + for name in ("prepare", "check-source", "pack"): + cmd = cmds.add_parser(name) + cmd.add_argument("source", type=Path) + if name == "pack": + cmd.add_argument("build", type=Path) + cmd.add_argument("target", choices=TARGETS) + cmd.add_argument("output", type=Path) + cmd = cmds.add_parser("install") + cmd.add_argument("target", choices=TARGETS) + cmd.add_argument("dest", type=Path) + cmd.add_argument("--bundle", type=Path) + cmd.add_argument("--sha256") + cmd.add_argument("--build-context", type=Path, help="use a staged target bundle if supplied") + cmd.add_argument("--allow-actions", action="store_true", help="allow draft artifact pins, using gh with Actions read permission") + cmd.add_argument("--cargo-config", type=Path, help="write this additional Cargo config for the pinned Git dependency") + activation = cmd.add_mutually_exclusive_group() + activation.add_argument("--github-env", type=Path) + activation.add_argument("--shell-env", type=Path) + args = parser.parse_args() + if args.command == "install-glue": + install_rust_glue(args.checkout, args.dest.resolve(), args.cargo_config) + elif args.command == "fetch": + args.dest.mkdir(parents=True, exist_ok=True) + with reviewed_archive(args.target, args.dest, args.allow_actions) as (bundle, _): + bundle.replace(args.dest / (args.target + ".tar.gz")) + elif args.command == "check-temporal-lock": + require(temporal_dependencies(args.producer) == temporal_dependencies(args.consumer), "Temporal ABI dependency mismatch") + elif args.command == "consumer-lock": + write_consumer_lock(args.source, args.output) + elif args.command == "prepare": + prepare(args.source) + elif args.command == "check-source": + verify_source(args.source) + elif args.command == "pack": + pack(args.source, args.build, args.target, args.output) + else: + if args.build_context: + require(args.bundle is None and args.sha256 is None, "--build-context and --bundle cannot be combined") + staged = args.build_context / (args.target + ".tar.gz") + if staged.is_file(): + args.bundle = staged + args.sha256, _ = pin_for(args.target, allow_actions=True) + env = install(args.target, args.dest.resolve(), args.bundle, args.sha256, args.allow_actions) + if args.cargo_config: + write_cargo_config(args.cargo_config, Path(env["AGENTOS_RUSTY_V8_SOURCE"])) + if args.github_env: + require(all("\n" not in value and "\r" not in value for value in env.values()), "invalid environment path") + with args.github_env.open("a") as out: + out.writelines(f"{k}={v}\n" for k, v in env.items()) + elif args.shell_env: + import shlex + args.shell_env.write_text("".join(f"export {k}={shlex.quote(v)}\n" for k, v in env.items())) + else: + print(json.dumps(env, indent=2)) + + +if __name__ == "__main__": + try: + main() + except (OSError, ValueError, KeyError, tarfile.TarError, subprocess.CalledProcessError) as exc: + sys.exit(str(exc)) diff --git a/scripts/ci/v8/node-wire-format-15.patch b/scripts/ci/v8/node-wire-format-15.patch new file mode 100644 index 0000000000..12ac7f1b91 --- /dev/null +++ b/scripts/ci/v8/node-wire-format-15.patch @@ -0,0 +1,117 @@ +diff --git a/include/v8-value-serializer.h b/include/v8-value-serializer.h +--- a/include/v8-value-serializer.h ++++ b/include/v8-value-serializer.h +@@ -172,6 +172,14 @@ + void WriteHeader(); + + /** ++ * Starts a standalone Node.js wire-format-15 stream. Use once, in place of ++ * WriteHeader(), before writing values. Rejects native buffer fields which ++ * do not fit uint32_t, immutable ArrayBuffers, and shared ArrayBuffers. ++ * Host object data remains the responsibility of the delegate. ++ */ ++ void WriteHeaderForNodeWireFormat15(); ++ ++ /** + * Serializes a JavaScript value into the buffer. + */ + V8_WARN_UNUSED_RESULT Maybe WriteValue(Local context, +diff --git a/src/objects/value-serializer.h b/src/objects/value-serializer.h +--- a/src/objects/value-serializer.h ++++ b/src/objects/value-serializer.h +@@ -69,6 +69,7 @@ + * Writes out a header, which includes the format version. + */ + void WriteHeader(); ++ void WriteHeaderForNodeWireFormat15(); + + /* + * Serializes a V8 object into the buffer. +@@ -197,6 +198,7 @@ + bool has_custom_host_objects_ = false; + bool treat_array_buffer_views_as_host_objects_ = false; + bool out_of_memory_ = false; ++ bool write_node_wire_format_15_ = false; + Zone zone_; + + // To avoid extra lookups in the identity map, ID+1 is actually stored in the +diff --git a/src/api/api.cc b/src/api/api.cc +--- a/src/api/api.cc ++++ b/src/api/api.cc +@@ -3735,6 +3735,10 @@ + + void ValueSerializer::WriteHeader() { private_->serializer.WriteHeader(); } + ++void ValueSerializer::WriteHeaderForNodeWireFormat15() { ++ private_->serializer.WriteHeaderForNodeWireFormat15(); ++} ++ + void ValueSerializer::SetTreatArrayBufferViewsAsHostObjects(bool mode) { + private_->serializer.SetTreatArrayBufferViewsAsHostObjects(mode); + } +diff --git a/src/objects/value-serializer.cc b/src/objects/value-serializer.cc +--- a/src/objects/value-serializer.cc ++++ b/src/objects/value-serializer.cc +@@ -340,7 +340,12 @@ + + void ValueSerializer::WriteHeader() { + WriteTag(SerializationTag::kVersion); +- WriteVarint(kLatestVersion); ++ WriteVarint(write_node_wire_format_15_ ? uint32_t{15} : kLatestVersion); ++} ++ ++void ValueSerializer::WriteHeaderForNodeWireFormat15() { ++ write_node_wire_format_15_ = true; ++ WriteHeader(); + } + + void ValueSerializer::SetTreatArrayBufferViewsAsHostObjects(bool mode) { +@@ -1009,6 +1014,12 @@ + + Maybe ValueSerializer::WriteJSArrayBuffer( + DirectHandle array_buffer) { ++ // Node's standalone streams cannot transfer shared memory out-of-band. ++ // Its v15 reader also predates both immutable ArrayBuffer tags. ++ if (write_node_wire_format_15_ && ++ (array_buffer->is_shared() || array_buffer->is_immutable())) { ++ return ThrowDataCloneError(MessageTemplate::kDataCloneError, array_buffer); ++ } + if (array_buffer->is_shared()) { + if (!delegate_) { + return ThrowDataCloneError(MessageTemplate::kDataCloneError, +@@ -1076,9 +1087,14 @@ + + ResizableFlag is_resizable = array_buffer->is_resizable_by_js(); + if (is_resizable) { ++ const size_t max_byte_length = array_buffer->max_byte_length(); ++ if (write_node_wire_format_15_ && ++ max_byte_length > std::numeric_limits::max()) { ++ return ThrowDataCloneError(MessageTemplate::kDataCloneError, array_buffer); ++ } + WriteTag(SerializationTag::kResizableArrayBuffer); + WriteVarint(byte_length); +- WriteVarint(array_buffer->max_byte_length()); ++ WriteVarint(max_byte_length); + WriteRawBytes(array_buffer->backing_store(), byte_length); + return ThrowIfOutOfMemory(); + } +@@ -1145,9 +1161,17 @@ + + tag = ArrayBufferViewTag::kDataView; + } ++ const size_t offset = view->byte_offset(); ++ const size_t length = view->byte_length(); ++ if (write_node_wire_format_15_ && ++ (offset > std::numeric_limits::max() || ++ length > std::numeric_limits::max())) { ++ return ThrowDataCloneError(MessageTemplate::kDataCloneError, ++ direct_handle(view, isolate_)); ++ } + WriteVarint(static_cast(tag)); +- WriteVarint(view->byte_offset()); +- WriteVarint(view->byte_length()); ++ WriteVarint(offset); ++ WriteVarint(length); + uint32_t flags = + JSArrayBufferViewIsLengthTracking::encode(view->is_length_tracking()) | + JSArrayBufferViewIsBackedByRab::encode(view->is_backed_by_rab()); diff --git a/scripts/ci/v8/source.json b/scripts/ci/v8/source.json new file mode 100644 index 0000000000..095d1101be --- /dev/null +++ b/scripts/ci/v8/source.json @@ -0,0 +1,25 @@ +{ + "rustyV8": "548adfd54db42c670e0f3fd42211c3af109c0a73", + "denoV8": "ac3d6e2607b8a240131d47da8e97dca0965a457c", + "upstreamPatches": [ + "d49521fd906188ecaeb3fcb9e8e8b20955fe8290", + "6b7ccfda00c9525c1edf0fddfd0da85042164a7c", + "0b690e4bf2bb9c37fd870408588c6eadd3ae9761", + "2f24dea22a2931666a8d32f36c20e0607dccea94", + "d03e7ab1999d78f7b20cb3e7901de1dc3f46e8c0", + "79c1ad6a9cecc8b7f759b266caed1f3eee10a0d5" + ], + "agentosPatches": [ + { + "file": "cppgc-generated-config.patch", + "sha256": "48640d3017eb66382df416b6ddfcd683bb1b1b02543afa2e1fd73033357b71b7" + }, + { + "file": "node-wire-format-15.patch", + "sha256": "462897ebb1039638fdbbf6ad9989975c2677445e107a8b024d60dee4c319e244" + } + ], + "v8Tree": "91306fc87da850ec8b222f0030b37d2da90a6dea", + "version": "15.8.62.58", + "icu": "2a51536dfb3b448bdf3dae70c782ea2879c90904" +} diff --git a/scripts/ci/v8/test_engine.py b/scripts/ci/v8/test_engine.py new file mode 100644 index 0000000000..7b8c3d358d --- /dev/null +++ b/scripts/ci/v8/test_engine.py @@ -0,0 +1,159 @@ +"""Small bundle controls; these do not clone V8 or run Cargo.""" +import io +import json +from pathlib import Path +import sys +import tarfile +import tempfile +import unittest +from unittest import mock +import zipfile + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import engine + + +class BundleTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + self.source = self.root / "source" + self.target = engine.TARGETS[0] + self.bundle = self.root / "build.tar.gz" + build = self.root / "build" + gn = build / self.target / "release/gn_out" + files = { + self.source / "LICENSE": "rusty_v8 license", + self.source / "v8/LICENSE": "V8 license", + self.source / "third_party/icu/LICENSE": "ICU license", + self.source / "v8/include/v8.h": "header", + self.source / engine.ICU: "data", + gn / "args.gn": '\n'.join(['is_debug = false', 'use_custom_libcxx = true', + 'v8_enable_sandbox = false', 'v8_enable_pointer_compression = false', + 'v8_enable_v8_checks = false', 'v8_generate_external_defines_header = true', + 'target_cpu = "x64"']), + gn / "obj/librusty_v8.a": "test archive", + gn / "src_binding.rs": "test binding", + gn / "gen/v8/include/v8-gn.h": "test GN header", + } + for path, value in files.items(): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(value) + crate = {} + for name in ("Cargo.toml", "build.rs", "src/lib.rs"): + path = self.source / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("test crate " + name) + crate[name] = path + with mock.patch.object(engine, "verify_source"), mock.patch.object(engine, "rust_crate_inputs", return_value=crate), mock.patch.object(sys, "platform", "linux"): + engine.pack(self.source, build, self.target, self.bundle) + self.pin = engine.digest(self.bundle) + + def install(self): + return engine.install(self.target, self.root / "installed", self.bundle, self.pin) + + def test_matched_local_bundle_can_be_installed_repeatedly(self): + env = self.install() + self.assertEqual(env, self.install()) + self.assertEqual(Path(env["RUSTY_V8_ARCHIVE"]).read_text(), "test archive") + self.assertEqual(Path(env["RUSTY_V8_SRC_BINDING_PATH"]).read_text(), "test binding") + self.assertEqual(engine.digest(Path(env["RUSTY_V8_ARCHIVE"])), env["RUSTY_V8_ARCHIVE_SHA256"]) + vendor = Path(env["AGENTOS_RUSTY_V8_SOURCE"]) / "vendor/v8" + crate_hashes = json.loads((vendor / ".cargo-checksum.json").read_text()) + self.assertEqual(crate_hashes["files"]["build.rs"], engine.digest(vendor / "build.rs")) + config = self.root / "extra.toml" + engine.write_cargo_config(config, vendor.parent.parent) + self.assertIn(engine.SOURCE["rustyV8"], config.read_text()) + config.write_text("[build]\njobs = 2\n") + with self.assertRaisesRegex(ValueError, "already exists"): + engine.write_cargo_config(config, vendor.parent.parent) + self.assertEqual(config.read_text(), "[build]\njobs = 2\n") + Path(env["AGENTOS_RUSTY_V8_SOURCE"], engine.ICU).write_text("wrong") + with self.assertRaisesRegex(ValueError, "digest mismatch"): + self.install() + + def test_rejects_wrong_target_and_independent_checksum(self): + with self.assertRaisesRegex(ValueError, "target mismatch"): + engine.install(engine.TARGETS[1], self.root / "installed", self.bundle, self.pin) + with self.assertRaisesRegex(ValueError, "SHA-256 mismatch"): + engine.install(self.target, self.root / "installed", self.bundle, "0" * 64) + + def test_draft_uses_only_the_reviewed_artifact_id_and_tar_digest(self): + pin_file = self.root / "archives.json" + pin = {"sha256": self.pin, "artifactId": 123} + locked = {self.target: pin} + pin_file.write_text(json.dumps(locked)) + installed = self.root / "artifacts" + def download(command, stdout, check): + self.assertEqual(command, ["gh", "api", "repos/rivet-dev/agentos/actions/artifacts/123/zip"]) + self.assertTrue(check) + with zipfile.ZipFile(stdout, "w") as archive: + archive.write(self.bundle, self.target + ".tar.gz") + with mock.patch.object(engine, "HERE", self.root), mock.patch.object(engine.subprocess, "run", side_effect=download) as gh: + with self.assertRaisesRegex(ValueError, "durable"): + engine.install(self.target, installed) + gh.assert_not_called() + env = engine.install(self.target, installed, allow_actions=True) + self.assertEqual(Path(env["RUSTY_V8_ARCHIVE"]).read_text(), "test archive") + staged = self.root / "docker-input" + with mock.patch.object(sys, "argv", ["engine.py", "fetch", self.target, str(staged), "--allow-actions"]): + engine.main() + docker_command = ["engine.py", "install", self.target, str(installed), "--build-context", str(staged), + "--shell-env", str(self.root / "docker.env")] + gh.reset_mock() + with mock.patch.object(sys, "argv", docker_command): + engine.main() + (staged / (self.target + ".tar.gz")).write_bytes(b"different") + with self.assertRaisesRegex(ValueError, "SHA-256 mismatch"): + engine.main() + gh.assert_not_called() + pin["sha256"] = "0" * 64 + pin_file.write_text(json.dumps(locked)) + with self.assertRaisesRegex(ValueError, "SHA-256 mismatch"): + engine.install(self.target, installed, allow_actions=True) + + def test_rejects_links_and_parent_paths(self): + for name, kind in [("../outside", tarfile.REGTYPE), ("archive", tarfile.SYMTYPE)]: + with tarfile.open(self.bundle, "w:gz") as tar: + info = tarfile.TarInfo(name) + info.type = kind + tar.addfile(info, io.BytesIO()) + with self.assertRaisesRegex(ValueError, "unexpected archive entry"): + engine.unpack(self.bundle, self.root / "unpacked") + self.assertFalse((self.root / "outside").exists()) + + def test_consumer_lock_roots_only_the_exact_upstream_crate(self): + source = '[[package]]\nname = "v8"\nversion = "158.0.0"\ndependencies = [\n "bindgen",\n]\n' + other = '[[package]]\nname = "bindgen"\nversion = "0.72.1"\nsource = "registry+fixture"\n' + dest = self.root / "consumer.lock" + with mock.patch.object(engine, "git", return_value=source + other) as git: + engine.write_consumer_lock(self.source, dest) + git.assert_called_once_with(self.source, "show", engine.SOURCE["rustyV8"] + ":Cargo.lock") + self.assertIn(other, dest.read_text()) + self.assertEqual(dest.read_text().count(engine.SOURCE["rustyV8"]), 2) + with mock.patch.object(engine, "git", return_value=other): + with self.assertRaisesRegex(ValueError, "one local v8 package"): + engine.write_consumer_lock(self.source, self.root / "unknown.lock") + + def test_hygiene_respects_each_checkouts_own_pin_and_registry_base(self): + checkout = self.root / "checkout" + checkout.mkdir() + dest, config = self.root / "glue", self.root / "scoped.toml" + lock = checkout / "Cargo.lock" + lock.write_text('[[package]]\nname = "v8"\nsource = "registry+fixture"\n') + engine.install_rust_glue(checkout, dest, config) + self.assertFalse(config.exists()) + self.assertFalse(dest.exists()) + pin = checkout / "scripts/ci/v8/source.json" + pin.parent.mkdir(parents=True) + pin.write_text(json.dumps({"rustyV8": "1" * 40})) + lock.write_text('[[package]]\nname = "v8"\nsource = "git+https://example.org/unreviewed"\n') + with mock.patch.object(engine, "git") as git: + with self.assertRaisesRegex(ValueError, "does not match"): + engine.install_rust_glue(checkout, dest, config) + git.assert_not_called() + + +if __name__ == "__main__": + unittest.main() From fbc7f9e7373a1709f9d03c11c7a5f2c9c6c49777 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Fri, 20 Nov 2026 14:30:46 -0800 Subject: [PATCH 02/19] fix(ci): install GLib development files for the Linux V8 host tools --- .github/workflows/v8-artifacts.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/v8-artifacts.yml b/.github/workflows/v8-artifacts.yml index 214bd757d2..915ad67719 100644 --- a/.github/workflows/v8-artifacts.yml +++ b/.github/workflows/v8-artifacts.yml @@ -71,7 +71,7 @@ jobs: gpg --dearmor < "$RUNNER_TEMP/llvm.asc" | sudo tee /usr/share/keyrings/agentos-llvm.gpg >/dev/null echo 'deb [signed-by=/usr/share/keyrings/agentos-llvm.gpg] https://apt.llvm.org/noble/ llvm-toolchain-noble-21 main' | sudo tee /etc/apt/sources.list.d/agentos-llvm.list sudo apt-get update -qq - sudo apt-get install -y --no-install-recommends clang-21 libclang-21-dev + sudo apt-get install -y --no-install-recommends clang-21 libclang-21-dev pkg-config libglib2.0-dev echo 'LIBCLANG_PATH=/usr/lib/llvm-21/lib' >> "$GITHUB_ENV" echo "RUSTY_V8_BINDGEN_RESOURCE_DIR=$(clang-21 -print-resource-dir)" >> "$GITHUB_ENV" - name: Install arm64 Linux cross compilation and execution tools From 0402439b86bcfe939b23d196db9ed13a781115d1 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Fri, 20 Nov 2026 11:17:15 -0800 Subject: [PATCH 03/19] fix: harden executor boundaries and release packaging --- .github/workflows/bench.yml | 22 +- .github/workflows/ci-nightly.yml | 64 +- .github/workflows/ci.yml | 149 ++-- .github/workflows/pipboy.yml | 28 +- .github/workflows/publish.yaml | 222 ++++-- Cargo.lock | 688 ++++++------------ Cargo.toml | 9 + crates/client/src/process.rs | 138 +++- crates/client/src/software_registry.rs | 6 +- crates/client/tests/software_registry.rs | 21 + .../tests/guest_clock_policy.rs | 61 ++ .../tests/javascript_v8.rs | 79 +- .../tests/wasi_path_open_read_not_blocked.rs | 4 +- crates/executor-contract/src/backend/event.rs | 31 + .../executor-contract/src/backend/payload.rs | 82 ++- .../src/backend/submission.rs | 13 +- crates/executor-contract/src/guest.rs | 58 ++ crates/executor-contract/src/host/process.rs | 18 + crates/executor-contract/src/lib.rs | 2 +- crates/executor-v8-runtime/CLAUDE.md | 2 +- crates/executor-v8-runtime/Cargo.lock | 584 --------------- crates/executor-v8-runtime/Cargo.toml | 4 +- .../assets/runners/wasi-module.js | 202 ++--- .../assets/runners/wasm-runner.mjs | 6 +- crates/executor-v8-runtime/build.rs | 234 +++--- .../executor-v8-runtime/src/adapter_host.rs | 2 +- .../src/adapter_runtime.rs | 45 +- .../executor-v8-runtime/src/async_context.rs | 4 +- crates/executor-v8-runtime/src/bridge.rs | 300 ++++---- .../src/embedded_runtime.rs | 121 ++- crates/executor-v8-runtime/src/execution.rs | 515 +++++++------ .../src/guest_bridge_policy.rs | 19 +- crates/executor-v8-runtime/src/isolate.rs | 253 +++---- crates/executor-v8-runtime/src/javascript.rs | 510 +++++++++---- .../src/module_code_cache.rs | 6 +- .../src/module_snapshot.rs | 47 +- crates/executor-v8-runtime/src/next_ticks.rs | 46 +- crates/executor-v8-runtime/src/node_brotli.rs | 13 +- .../src/node_v8_deserializer.rs | 13 +- .../src/node_v8_serialization.rs | 525 ++++++++++--- .../src/owned_microtasks.rs | 49 +- crates/executor-v8-runtime/src/session.rs | 145 ++-- .../executor-v8-runtime/src/shared_source.rs | 73 +- crates/executor-v8-runtime/src/snapshot.rs | 167 ++--- .../src/snapshot_disk_cache.rs | 2 +- crates/executor-v8-runtime/src/stream.rs | 12 +- .../src/terminal_callback.rs | 24 +- .../src/uint8array_encoding.rs | 4 +- .../src/v8_array_buffer_allocator.cc | 39 + .../src/v8_bounded_serializer.cc | 149 ++++ .../src/v8_thread_support.cc | 26 +- .../executor-v8-runtime/tests/event_loop.rs | 12 +- .../tests/isolate_lifecycle_concurrency.rs | 6 +- .../tests/shared_source_bound.rs | 4 +- .../tests/support/guest_bridge_policy.rs | 14 +- .../tests/support/module_evaluation_owner.rs | 6 +- .../tests/support/owned_microtasks.rs | 40 +- .../tests/support/promise_rejection_owner.rs | 10 +- .../tests/support/terminal_callback_abort.rs | 22 +- .../tests/support/terminal_callback_owner.rs | 18 +- .../tests/support/terminal_watchdog_fence.rs | 4 +- crates/executor-wasm-v8/src/lib.rs | 29 +- .../executor-wasm-wasmtime/src/lifecycle.rs | 91 +++ .../executor-wasm-wasmtime/src/linker/mod.rs | 37 + .../src/linker/process.rs | 47 +- crates/executor-wasm-wasmtime/src/store.rs | 1 + crates/executor-wasm-wasmtime/src/worker.rs | 15 +- crates/vm-kernel/src/fd_table.rs | 6 +- crates/vm-kernel/src/kernel.rs | 46 +- crates/vm/Cargo.toml | 4 +- crates/vm/src/execution/child_process.rs | 151 +++- crates/vm/src/execution/host_dispatch/mod.rs | 19 +- crates/vm/src/execution/process.rs | 53 +- crates/vm/tests/architecture_guards.rs | 4 +- crates/vm/tests/builtin_conformance.rs | 4 + docker/build/darwin.Dockerfile | 10 +- docker/build/linux-gnu.Dockerfile | 12 +- .../architecture/limits-and-observability.mdx | 7 + .../content/docs/javascript-compatibility.mdx | 9 +- docs/content/docs/resource-limits.mdx | 13 + docs/content/docs/security-model.mdx | 6 +- .../{Cargo.toml => Cargo.toml.disabled} | 0 experiments/obscura/README.md | 4 +- packages/benchmarks/README.md | 2 +- packages/benchmarks/src/baseline.ts | 20 +- packages/benchmarks/src/compare-baseline.ts | 23 +- packages/benchmarks/src/footprint.ts | 58 +- packages/benchmarks/src/fuzz/run.ts | 19 +- packages/benchmarks/src/lib/memory.ts | 48 +- packages/benchmarks/src/quick-gate.ts | 62 +- packages/benchmarks/src/run-all.ts | 2 +- .../benchmarks/tests/compare-baseline.test.ts | 15 + .../bridge-src/builtins/builtin-modules.ts | 60 +- .../bridge-src/builtins/child-process.ts | 54 +- .../build-tools/bridge-src/builtins/fs.ts | 70 +- .../build-tools/bridge-src/builtins/http.ts | 194 +---- .../bridge-src/builtins/module-loader.ts | 10 +- .../build-tools/bridge-src/builtins/timers.ts | 68 ++ .../build-tools/bridge-src/global-exposure.ts | 19 + .../bridge-src/polyfills/compression.ts | 8 +- packages/core/package.json | 2 +- packages/core/src/agent-os.ts | 8 + packages/core/src/code-execution.ts | 28 +- packages/core/src/execution-options.ts | 23 + packages/core/src/host-output-capture.ts | 83 +++ packages/core/src/kernel-proxy.ts | 19 +- packages/core/src/node-runtime.ts | 141 ++-- packages/core/src/runtime-compat.ts | 2 + packages/core/src/runtime.ts | 2 + packages/core/src/sidecar/rpc-client.ts | 33 +- packages/core/src/software-registry.ts | 6 +- packages/core/src/test-runtime.ts | 2 + .../core/tests/code-execution-capture.test.ts | 58 ++ .../node-preload-resolution.native.test.ts | 83 +++ .../node-runtime-execution-options.test.ts | 142 ++++ .../tests/node-v8-native-wire.native.test.ts | 7 +- packages/core/tests/software-registry.test.ts | 8 + scripts/ci-selection.test.mjs | 10 +- scripts/pipboy/src/footprint/assets.ts | 46 +- scripts/pipboy/tests/assets.test.ts | 29 + scripts/publish/secure-exec.md | 3 +- scripts/publish/src/ci/bin.ts | 47 +- scripts/publish/src/ci/publish-packed-npm.ts | 21 + scripts/publish/src/lib/artifacts.test.ts | 6 + scripts/publish/src/lib/artifacts.ts | 7 +- scripts/publish/src/lib/context.test.ts | 47 ++ scripts/publish/src/lib/context.ts | 62 +- scripts/publish/src/lib/npm.ts | 48 +- scripts/publish/src/lib/packages.test.ts | 16 + scripts/publish/src/lib/packages.ts | 28 +- scripts/publish/src/lib/packed-npm.test.ts | 74 ++ scripts/publish/src/lib/packed-npm.ts | 142 ++++ scripts/publish/src/lib/r2.test.ts | 84 +++ scripts/publish/src/lib/r2.ts | 153 ++-- scripts/publish/src/lib/rust-crates.test.ts | 22 +- scripts/publish/src/lib/rust-crates.ts | 21 + scripts/publish/src/lib/version.test.ts | 7 + scripts/publish/src/lib/version.ts | 6 +- scripts/publish/tsconfig.json | 1 + scripts/wasi-delegate-rights.test.mjs | 104 +++ secure-exec/src/runtime.ts | 5 +- secure-exec/tests/runtime-forwarding.test.ts | 20 +- 142 files changed, 5553 insertions(+), 3375 deletions(-) create mode 100644 crates/executor-conformance/tests/guest_clock_policy.rs delete mode 100644 crates/executor-v8-runtime/Cargo.lock create mode 100644 crates/executor-v8-runtime/src/v8_array_buffer_allocator.cc create mode 100644 crates/executor-v8-runtime/src/v8_bounded_serializer.cc rename experiments/obscura/{Cargo.toml => Cargo.toml.disabled} (100%) create mode 100644 packages/core/src/execution-options.ts create mode 100644 packages/core/src/host-output-capture.ts create mode 100644 packages/core/tests/code-execution-capture.test.ts create mode 100644 packages/core/tests/node-runtime-execution-options.test.ts create mode 100644 scripts/pipboy/tests/assets.test.ts create mode 100644 scripts/publish/src/ci/publish-packed-npm.ts create mode 100644 scripts/publish/src/lib/context.test.ts create mode 100644 scripts/publish/src/lib/packed-npm.test.ts create mode 100644 scripts/publish/src/lib/packed-npm.ts create mode 100644 scripts/publish/src/lib/r2.test.ts create mode 100644 scripts/wasi-delegate-rights.test.mjs diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 9b4df3b4c1..fffd551ff4 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -7,23 +7,35 @@ on: schedule: - cron: "17 9 * * *" +permissions: + contents: read + jobs: bench-gate: + permissions: + contents: read + actions: read name: Runtime benchmark gate if: github.event_name != 'pull_request' || github.base_ref == 'main' || contains(github.event.pull_request.labels.*.name, 'aggregate-ci') runs-on: ${{ github.event_name == 'pull_request' && 'depot-ubuntu-24.04-8' || 'ubuntu-latest' }} steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - name: Install reviewed V8 archive + env: + GH_TOKEN: ${{ github.token }} + run: python3 scripts/ci/v8/engine.py install x86_64-unknown-linux-gnu "$RUNNER_TEMP/agentos-v8" --allow-actions --cargo-config "$GITHUB_WORKSPACE/../.cargo/config.toml" --github-env "$GITHUB_ENV" + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm cache-dependency-path: pnpm-lock.yaml - - uses: dtolnay/rust-toolchain@stable + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable with: targets: wasm32-wasip1 - - uses: dtolnay/rust-toolchain@nightly + - uses: dtolnay/rust-toolchain@6a52aad76a94a5fbc50d567bb5be24419801c1fb # nightly with: targets: wasm32-wasip1 - run: pnpm install --frozen-lockfile --filter '@rivet-dev/agentos-benchmarks...' --filter '@rivet-dev/agentos...' --filter '@rivet-dev/agentos-build-tools' diff --git a/.github/workflows/ci-nightly.yml b/.github/workflows/ci-nightly.yml index a285f3d3c6..70a97e8cfc 100644 --- a/.github/workflows/ci-nightly.yml +++ b/.github/workflows/ci-nightly.yml @@ -10,8 +10,14 @@ on: schedule: - cron: "41 10 * * *" +permissions: + contents: read + jobs: xfstests: + permissions: + contents: read + actions: read name: "xfstests (${{ matrix.wasm_backend }}, ${{ matrix.storage_backend }})" runs-on: depot-ubuntu-24.04-8 timeout-minutes: 420 @@ -21,12 +27,18 @@ jobs: wasm_backend: [v8] storage_backend: [chunked_local, memory, chunked_s3] steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - name: Install reviewed V8 archive + env: + GH_TOKEN: ${{ github.token }} + run: python3 scripts/ci/v8/engine.py install x86_64-unknown-linux-gnu "$RUNNER_TEMP/agentos-v8" --allow-actions --cargo-config "$GITHUB_WORKSPACE/../.cargo/config.toml" --github-env "$GITHUB_ENV" + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 - - uses: dtolnay/rust-toolchain@1.98.1 + - uses: dtolnay/rust-toolchain@01e2821fc38dbca62c2557c927b33938a2e005fd # 1.98.1 with: targets: wasm32-wasip1 - run: pnpm install --frozen-lockfile @@ -36,7 +48,7 @@ jobs: XFSTESTS_WASM_BACKENDS: ${{ matrix.wasm_backend }} XFSTESTS_BACKENDS: ${{ matrix.storage_backend }} XFSTESTS_CONCURRENCY: '2' - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 if: always() with: name: xfstests-${{ matrix.wasm_backend }}-${{ matrix.storage_backend }} @@ -44,6 +56,9 @@ jobs: if-no-files-found: warn xfstests-endurance: + permissions: + contents: read + actions: read name: "xfstests endurance (${{ matrix.wasm_backend }})" if: ${{ github.event_name != 'workflow_dispatch' || !inputs.xfstests_only }} runs-on: depot-ubuntu-24.04-8 @@ -60,12 +75,18 @@ jobs: CARGO_TARGET_DIR: ${{ github.workspace }}/tests/xfstests/.cache/cargo-target CARGO_BUILD_JOBS: '1' steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - name: Install reviewed V8 archive + env: + GH_TOKEN: ${{ github.token }} + run: python3 scripts/ci/v8/engine.py install x86_64-unknown-linux-gnu "$RUNNER_TEMP/agentos-v8" --allow-actions --cargo-config "$GITHUB_WORKSPACE/../.cargo/config.toml" --github-env "$GITHUB_ENV" + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 - - uses: dtolnay/rust-toolchain@1.98.1 + - uses: dtolnay/rust-toolchain@01e2821fc38dbca62c2557c927b33938a2e005fd # 1.98.1 with: targets: wasm32-wasip1 - run: pnpm install --frozen-lockfile @@ -123,6 +144,9 @@ jobs: --ignored --exact --nocapture --test-threads=1 nightly: + permissions: + contents: read + actions: read name: Nightly runtime validation if: ${{ github.event_name != 'workflow_dispatch' || !inputs.xfstests_only }} runs-on: ubuntu-latest @@ -133,21 +157,27 @@ jobs: AGENTOS_REQUIRE_UDP_ANCILLARY: '1' XFSTESTS_ROOT: ${{ github.workspace }}/tests/xfstests/.work/xfstests steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - name: Install reviewed V8 archive + env: + GH_TOKEN: ${{ github.token }} + run: python3 scripts/ci/v8/engine.py install x86_64-unknown-linux-gnu "$RUNNER_TEMP/agentos-v8" --allow-actions --cargo-config "$GITHUB_WORKSPACE/../.cargo/config.toml" --github-env "$GITHUB_ENV" + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 - - uses: oven-sh/setup-bun@v2 + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2 - run: corepack enable yarn - - uses: dtolnay/rust-toolchain@1.98.1 + - uses: dtolnay/rust-toolchain@01e2821fc38dbca62c2557c927b33938a2e005fd # 1.98.1 with: components: clippy, rustfmt targets: wasm32-wasip1 - - uses: dtolnay/rust-toolchain@nightly + - uses: dtolnay/rust-toolchain@6a52aad76a94a5fbc50d567bb5be24419801c1fb # nightly with: targets: wasm32-wasip1 - - uses: dtolnay/rust-toolchain@master + - uses: dtolnay/rust-toolchain@df2a88e9fe4098bad09cb42c3dec7187f15861fc # master with: toolchain: nightly-2026-03-01 components: rust-src @@ -201,7 +231,7 @@ jobs: AGENTOS_SIDECAR_BIN: ${{ github.workspace }}/target/release/agentos-sidecar NATIVE_BASELINE_BIN: ${{ github.workspace }}/target/release/agentos-benchmark-baseline NATIVE_BASELINE_WASM: ${{ github.workspace }}/target/wasm32-wasip1/release/agentos-benchmark-baseline.wasm - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 if: always() with: name: nightly-benchmark-results diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 06bb77872c..718cd5040e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,14 +23,16 @@ jobs: AGENTOS_SKIP_NATIVE_META_BUILD: '1' AGENTOS_CI_CODE_ONLY_BUILD: '1' steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm cache-dependency-path: pnpm-lock.yaml - - uses: dtolnay/rust-toolchain@1.98.1 + - uses: dtolnay/rust-toolchain@01e2821fc38dbca62c2557c927b33938a2e005fd # 1.98.1 with: components: rustfmt - run: pnpm install --frozen-lockfile @@ -41,6 +43,7 @@ jobs: --concurrency=4 - run: pnpm --dir scripts/publish run check-types - run: pnpm --dir scripts/publish test + - run: python3 -B -m unittest discover -s scripts/ci/v8 - run: node --test scripts/check-rust-package-metadata.test.mjs - run: node scripts/check-rust-package-metadata.mjs - run: node --test scripts/check-agentos-client-protocol-compat.test.mjs @@ -71,7 +74,7 @@ jobs: --exclude='software/*/dist/package.aospkg' \ -czf /tmp/agentos-js-dist.tar.gz \ packages/*/dist software/*/dist - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 with: name: js-dist path: /tmp/agentos-js-dist.tar.gz @@ -84,9 +87,11 @@ jobs: if: github.event_name != 'pull_request' || github.base_ref == 'main' || contains(github.event.pull_request.labels.*.name, 'aggregate-ci') runs-on: depot-ubuntu-24.04-8 steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm @@ -106,12 +111,12 @@ jobs: with open(os.environ["GITHUB_OUTPUT"], "a") as output: output.write(f"channel={channel}\n") PYTHON - - uses: dtolnay/rust-toolchain@nightly + - uses: dtolnay/rust-toolchain@6a52aad76a94a5fbc50d567bb5be24419801c1fb # nightly with: toolchain: ${{ steps.wasm-toolchain.outputs.channel }} components: rust-src targets: wasm32-wasip1 - - uses: Swatinem/rust-cache@v2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2 with: workspaces: toolchain -> target key: wasm-commands-${{ hashFiles('toolchain/Cargo.lock') }} @@ -199,7 +204,7 @@ jobs: ) > "$RUNNER_TEMP/agentos-software-build-inputs.sha256" cp "$RUNNER_TEMP/agentos-software-build-inputs.sha256" "$RUNNER_TEMP/agentos-software-build-inputs/" tar -C "$RUNNER_TEMP/agentos-software-build-inputs" -czf "$RUNNER_TEMP/agentos-software-build-inputs.tar.gz" . - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'aggregate-ci') with: name: software-build-inputs @@ -207,28 +212,28 @@ jobs: compression-level: 0 retention-days: 1 if-no-files-found: error - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'aggregate-ci') with: name: wasm-commands path: packages/core/commands retention-days: 1 if-no-files-found: error - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 with: name: default-software path: /tmp/agentos-default-software.tar.gz compression-level: 0 retention-days: 1 if-no-files-found: error - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'aggregate-ci') with: name: wasm-command-licenses path: toolchain/c/build/awk.COPYING retention-days: 1 if-no-files-found: error - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'aggregate-ci') with: name: wasm-thread-fixtures @@ -237,7 +242,7 @@ jobs: toolchain/c/build/pthread_benchmark.wasm toolchain/c/build/exec_variants if-no-files-found: error - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'aggregate-ci') with: name: wasm-c-parity-fixtures @@ -245,6 +250,9 @@ jobs: if-no-files-found: error executor-feature-isolation: + permissions: + contents: read + actions: read name: Executor Feature Isolation if: github.event_name != 'pull_request' || github.base_ref == 'main' || contains(github.event.pull_request.labels.*.name, 'aggregate-ci') runs-on: ubuntu-latest @@ -252,17 +260,23 @@ jobs: CARGO_PROFILE_DEV_DEBUG: '0' CARGO_PROFILE_TEST_DEBUG: '0' steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - name: Install reviewed V8 archive + env: + GH_TOKEN: ${{ github.token }} + run: python3 scripts/ci/v8/engine.py install x86_64-unknown-linux-gnu "$RUNNER_TEMP/agentos-v8" --allow-actions --cargo-config "$GITHUB_WORKSPACE/../.cargo/config.toml" --github-env "$GITHUB_ENV" + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm cache-dependency-path: pnpm-lock.yaml - - uses: dtolnay/rust-toolchain@stable + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable with: components: rustfmt, clippy - - uses: Swatinem/rust-cache@v2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2 with: key: feature-isolation-${{ hashFiles('Cargo.lock') }} - name: Install V8 bridge build dependencies @@ -298,6 +312,9 @@ jobs: done rust-clippy: + permissions: + contents: read + actions: read name: Rust Clippy if: github.event_name != 'pull_request' || github.base_ref == 'main' || contains(github.event.pull_request.labels.*.name, 'aggregate-ci') runs-on: ubuntu-latest @@ -305,22 +322,31 @@ jobs: CARGO_PROFILE_DEV_DEBUG: '0' CARGO_PROFILE_TEST_DEBUG: '0' steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - name: Install reviewed V8 archive + env: + GH_TOKEN: ${{ github.token }} + run: python3 scripts/ci/v8/engine.py install x86_64-unknown-linux-gnu "$RUNNER_TEMP/agentos-v8" --allow-actions --cargo-config "$GITHUB_WORKSPACE/../.cargo/config.toml" --github-env "$GITHUB_ENV" + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm cache-dependency-path: pnpm-lock.yaml - - uses: dtolnay/rust-toolchain@1.98.1 + - uses: dtolnay/rust-toolchain@01e2821fc38dbca62c2557c927b33938a2e005fd # 1.98.1 with: components: clippy - - uses: Swatinem/rust-cache@v2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2 - name: Install V8 bridge build dependencies run: pnpm install --frozen-lockfile --filter '@rivet-dev/agentos-build-tools...' - run: cargo clippy --workspace --all-targets -- -D warnings rust: + permissions: + contents: read + actions: read name: Rust if: github.event_name != 'pull_request' || github.base_ref == 'main' || contains(github.event.pull_request.labels.*.name, 'aggregate-ci') runs-on: ubuntu-latest @@ -328,17 +354,23 @@ jobs: CARGO_PROFILE_DEV_DEBUG: '0' CARGO_PROFILE_TEST_DEBUG: '0' steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - name: Install reviewed V8 archive + env: + GH_TOKEN: ${{ github.token }} + run: python3 scripts/ci/v8/engine.py install x86_64-unknown-linux-gnu "$RUNNER_TEMP/agentos-v8" --allow-actions --cargo-config "$GITHUB_WORKSPACE/../.cargo/config.toml" --github-env "$GITHUB_ENV" + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm cache-dependency-path: pnpm-lock.yaml - - uses: dtolnay/rust-toolchain@1.98.1 + - uses: dtolnay/rust-toolchain@01e2821fc38dbca62c2557c927b33938a2e005fd # 1.98.1 with: components: rustfmt - - uses: Swatinem/rust-cache@v2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2 - name: Install V8 bridge build dependencies run: pnpm install --frozen-lockfile --filter '@rivet-dev/agentos-build-tools...' - name: Verify generated VM config bindings @@ -406,7 +438,7 @@ jobs: mkdir -p ci-artifacts/agentos-sidecar cp target/debug/agentos-sidecar ci-artifacts/agentos-sidecar/agentos-sidecar strip --strip-all ci-artifacts/agentos-sidecar/agentos-sidecar - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 with: name: agentos-sidecar path: ci-artifacts/agentos-sidecar/agentos-sidecar @@ -420,15 +452,17 @@ jobs: env: AGENTOS_SKIP_NATIVE_META_BUILD: '1' steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm cache-dependency-path: pnpm-lock.yaml - run: pnpm install --frozen-lockfile --filter '@rivet-dev/agentos...' - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: js-dist path: ci-artifacts/js @@ -445,23 +479,25 @@ jobs: env: AGENTOS_SKIP_NATIVE_META_BUILD: '1' steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm cache-dependency-path: pnpm-lock.yaml - run: pnpm install --frozen-lockfile --filter '@rivet-dev/agentos...' --filter 'secure-exec...' --filter '@rivet-dev/agentos-toolchain...' - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: js-dist path: ci-artifacts/js - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: agentos-sidecar path: ci-artifacts/agentos-sidecar - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: default-software path: ci-artifacts/default-software @@ -541,6 +577,9 @@ jobs: fi wasm-backend-matrix: + permissions: + contents: read + actions: read name: "WASM backend (${{ matrix.backend }})" if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'aggregate-ci') needs: [wasm-commands] @@ -559,35 +598,41 @@ jobs: AGENTOS_C_WASM_COMMANDS_DIR: ${{ github.workspace }}/toolchain/c/build/conformance XFSTESTS_ROOT: ${{ github.workspace }}/tests/xfstests/.work/xfstests steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - name: Install reviewed V8 archive + env: + GH_TOKEN: ${{ github.token }} + run: python3 scripts/ci/v8/engine.py install x86_64-unknown-linux-gnu "$RUNNER_TEMP/agentos-v8" --allow-actions --cargo-config "$GITHUB_WORKSPACE/../.cargo/config.toml" --github-env "$GITHUB_ENV" + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 - - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v2 + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2 with: key: wasm-backend-${{ matrix.backend }} - run: pnpm install --frozen-lockfile - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: wasm-commands path: packages/core/commands - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: wasm-command-licenses path: toolchain/c/build - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: wasm-thread-fixtures path: toolchain/c/build - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: wasm-c-parity-fixtures path: toolchain/c/build/conformance - name: Restore native C parity executable modes run: find toolchain/c/build/conformance/native -maxdepth 1 -type f -exec chmod 0755 '{}' + - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: software-build-inputs path: ci-artifacts/software-build-inputs diff --git a/.github/workflows/pipboy.yml b/.github/workflows/pipboy.yml index 6078a3ded1..205c0e67c1 100644 --- a/.github/workflows/pipboy.yml +++ b/.github/workflows/pipboy.yml @@ -55,8 +55,8 @@ jobs: {"platform":"darwin-arm64","target":"aarch64-apple-darwin","runner":"${{ github.event_name == 'pull_request' && 'depot-ubuntu-24.04-8' || 'ubuntu-latest' }}","dockerfile":"darwin","clang":"aarch64-apple-darwin20.4","tier":"all"} ] run: | - RUST='(\.rs$|(^|/)Cargo\.(toml|lock)$|^deny\.toml$|^rust-toolchain\.toml$|^docker/build/|^scripts/pipboy/|^\.github/workflows/pipboy\.yml$)' - PLATFORM_SENSITIVE='((^|/)Cargo\.(toml|lock)$|(^|/)build\.rs$|^docker/build/|^rust-toolchain\.toml$)' + RUST='(\.rs$|(^|/)Cargo\.(toml|lock)$|^deny\.toml$|^rust-toolchain\.toml$|^docker/build/|^scripts/ci/v8/(archives\.json|source\.json|engine\.py|.*\.patch)$|^scripts/pipboy/|^\.github/workflows/pipboy\.yml$)' + PLATFORM_SENSITIVE='((^|/)Cargo\.(toml|lock)$|(^|/)build\.rs$|^docker/build/|^scripts/ci/v8/(archives\.json|source\.json|engine\.py|.*\.patch)$|^rust-toolchain\.toml$)' matrix() { jq -c --arg tier "$1" 'map(select($tier == "all" or .tier == $tier))' <<< "$NATIVE_ALL"; } if [ "$EVENT" != "pull_request" ]; then @@ -135,6 +135,10 @@ jobs: # Full command and software builds are explicit on PRs and run on main for baselines. runs-on: depot-ubuntu-24.04-8 timeout-minutes: 90 + env: + # This measures npm tarballs; the platform binary is measured by native. + # Core's Turbo dependency can otherwise build the excluded sidecar package. + AGENTOS_SKIP_NATIVE_META_BUILD: '1' permissions: contents: read actions: read # download the base tarballs from a main run @@ -247,14 +251,20 @@ jobs: if: github.event_name == 'pull_request' env: BASE_SHA: ${{ needs.changes.outputs.base }} - run: git worktree add "$RUNNER_TEMP/base-src" "$BASE_SHA" + run: git worktree add "$RUNNER_TEMP/pipboy-base/src" "$BASE_SHA" + - name: Stage the matching Rust source for each dependency comparison + run: | + python3 scripts/ci/v8/engine.py install-glue "$GITHUB_WORKSPACE" "$RUNNER_TEMP/v8-pr-glue" --cargo-config "$GITHUB_WORKSPACE/../.cargo/config.toml" + if test -d "$RUNNER_TEMP/pipboy-base/src"; then + python3 scripts/ci/v8/engine.py install-glue "$RUNNER_TEMP/pipboy-base/src" "$RUNNER_TEMP/v8-base-glue" --cargo-config "$RUNNER_TEMP/pipboy-base/.cargo/config.toml" + fi - name: Diff findings env: EVENT: ${{ github.event_name }} run: | mkdir -p "$RUNNER_TEMP/reports" base=() - if [ "$EVENT" = "pull_request" ]; then base=(--base-root "$RUNNER_TEMP/base-src"); fi + if [ "$EVENT" = "pull_request" ]; then base=(--base-root "$RUNNER_TEMP/pipboy-base/src"); fi pnpm --silent --filter pipboy pipboy hygiene "${base[@]}" --out "$RUNNER_TEMP/reports/hygiene.json" - if: always() uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 @@ -272,6 +282,7 @@ jobs: timeout-minutes: 120 permissions: contents: read + actions: read strategy: fail-fast: false matrix: @@ -282,6 +293,11 @@ jobs: persist-credentials: false # The publish workflow's Dockerfile and build arguments in release mode, so # the size is what ships. Toolchain versions are read from publish.yaml. + - name: Stage the target's reviewed engine archive + env: + GH_TOKEN: ${{ github.token }} + TARGET: ${{ matrix.target }} + run: python3 scripts/ci/v8/engine.py fetch "$TARGET" "$RUNNER_TEMP/v8-inputs" --allow-actions - name: Build with the publish Dockerfile env: PLATFORM: ${{ matrix.platform }} @@ -298,7 +314,7 @@ jobs: args+=(--build-arg "LINUX_GNU_LLVM_VERSION=$(publish_env LINUX_GNU_LLVM_VERSION)" --build-arg "LINUX_GNU_SYSROOT_TAG=$(publish_env LINUX_GNU_SYSROOT_TAG)") fi - docker buildx build -f "docker/build/$DOCKERFILE.Dockerfile" "${args[@]}" -t pipboy-native --load . + docker buildx build --build-context "agentos-v8-inputs=$RUNNER_TEMP/v8-inputs" -f "docker/build/$DOCKERFILE.Dockerfile" "${args[@]}" -t pipboy-native --load . cid="$(docker create pipboy-native)" docker cp "$cid:/artifacts/agentos-sidecar" "$RUNNER_TEMP/agentos-native-sidecar" docker rm "$cid" @@ -372,4 +388,4 @@ jobs: with: persist-credentials: false - uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7.6.0 - - run: uvx zizmor@1.31.1 --offline --min-severity low .github/workflows/pipboy.yml + - run: uvx zizmor@1.31.1 --offline --min-severity low .github/workflows diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 160457614a..4b7adf2079 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -4,11 +4,11 @@ on: workflow_dispatch: inputs: version: - description: "Version to release (e.g. 0.2.0 or 0.2.0-rc.1). Leave empty for preview publish on the dispatched branch." + description: "Version to release (e.g. 0.2.0 or 0.2.0-rc.1). Leave empty to publish to a preview-- tag." required: false type: string latest: - description: "Tag as @latest (release only)" + description: "Promote to latest (stable releases only)" required: true type: boolean default: true @@ -35,6 +35,9 @@ env: LINUX_GNU_LLVM_VERSION: "22" LINUX_GNU_SYSROOT_TAG: "sysroot-20250207" +permissions: + contents: read + jobs: context: name: Context @@ -47,11 +50,12 @@ jobs: latest: ${{ steps.ctx.outputs.latest }} build_profile: ${{ steps.mode.outputs.profile }} steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 with: + persist-credentials: false fetch-depth: 1 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm @@ -85,17 +89,19 @@ jobs: name: WASM Commands runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm cache-dependency-path: pnpm-lock.yaml - - uses: dtolnay/rust-toolchain@nightly + - uses: dtolnay/rust-toolchain@6a52aad76a94a5fbc50d567bb5be24419801c1fb # nightly with: targets: wasm32-wasip1 - - uses: Swatinem/rust-cache@v2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2 with: workspaces: toolchain -> toolchain/target cache-workspace-crates: true @@ -107,12 +113,12 @@ jobs: - name: Build required heavy release commands run: make -C toolchain cmd/duckdb cmd/vim - run: node packages/core/scripts/copy-wasm-commands.mjs --require - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 with: name: wasm-commands path: packages/core/commands if-no-files-found: error - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 with: name: wasm-command-licenses path: toolchain/c/build/awk.COPYING @@ -133,12 +139,14 @@ jobs: target: aarch64-unknown-linux-gnu runs-on: ${{ matrix.runner }} steps: - - uses: actions/checkout@v4 - - uses: docker/setup-buildx-action@v3 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3 with: name: agentos-${{ matrix.platform }} - name: Build linux artifacts - uses: docker/build-push-action@v6 + uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6 with: context: . file: docker/build/linux-gnu.Dockerfile @@ -163,14 +171,14 @@ jobs: test -f "$sidecar_out/agentos-sidecar" echo "sidecar_dir=$sidecar_out" >> "$GITHUB_OUTPUT" - name: Check glibc floor - run: | - scripts/ci/check-linux-glibc-floor.sh \ - "${{ steps.extract.outputs.sidecar_dir }}/agentos-sidecar" + env: + SIDECAR_DIR: ${{ steps.extract.outputs.sidecar_dir }} + run: scripts/ci/check-linux-glibc-floor.sh "$SIDECAR_DIR/agentos-sidecar" - name: Smoke-run binaries in old Linux containers - run: | - scripts/ci/smoke-linux-artifacts.sh binary \ - "${{ steps.extract.outputs.sidecar_dir }}/agentos-sidecar" - - uses: actions/upload-artifact@v4 + env: + SIDECAR_DIR: ${{ steps.extract.outputs.sidecar_dir }} + run: scripts/ci/smoke-linux-artifacts.sh binary "$SIDECAR_DIR/agentos-sidecar" + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 with: name: sidecar-${{ matrix.platform }} path: ${{ steps.extract.outputs.sidecar_dir }} @@ -192,15 +200,22 @@ jobs: target: aarch64-apple-darwin clang: aarch64-apple-darwin20.4 runs-on: ${{ matrix.runner }} + permissions: + contents: read + packages: read steps: - - uses: actions/checkout@v4 - - uses: docker/setup-buildx-action@v3 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3 with: name: agentos-${{ matrix.platform }} - name: Log in to ghcr.io - run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin + env: + GH_TOKEN: ${{ github.token }} + run: printf "%s\n" "$GH_TOKEN" | docker login ghcr.io -u "$GITHUB_ACTOR" --password-stdin - name: Cross-compile via osxcross - uses: docker/build-push-action@v6 + uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6 with: context: . file: docker/build/darwin.Dockerfile @@ -227,7 +242,7 @@ jobs: *) echo "unknown darwin platform: ${{ matrix.platform }}" >&2; exit 2 ;; esac file "$sidecar_out/agentos-sidecar" | grep -F "$expected_arch" - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 with: name: sidecar-${{ matrix.platform }} path: target/sidecar-artifacts/${{ matrix.platform }} @@ -250,22 +265,24 @@ jobs: runner: macos-15 runs-on: ${{ matrix.runner }} steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 22 cache: pnpm cache-dependency-path: pnpm-lock.yaml - - uses: dtolnay/rust-toolchain@master + - uses: dtolnay/rust-toolchain@df2a88e9fe4098bad09cb42c3dec7187f15861fc # master with: toolchain: ${{ env.RUST_TOOLCHAIN }} - run: pnpm install --frozen-lockfile --filter '@rivet-dev/agentos...' --filter @rivet-dev/agentos-build-tools - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: wasm-commands path: packages/core/commands - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: sidecar-${{ matrix.platform }} path: target/smoke-sidecar @@ -281,29 +298,27 @@ jobs: node scripts/ci/smoke-packed-wasm-backends.mjs \ --platform-package "packages/sidecar/npm/${{ matrix.platform }}" - publish-npm: + prepare-npm: needs: [context, wasm-commands, build-sidecar, build-sidecar-darwin, smoke-sidecar-artifacts] - name: Publish npm + name: Prepare npm tarballs if: ${{ !cancelled() && needs.wasm-commands.result == 'success' && needs.build-sidecar.result == 'success' && needs.build-sidecar-darwin.result == 'success' && needs.smoke-sidecar-artifacts.result == 'success' }} runs-on: ubuntu-latest - permissions: - contents: read - id-token: write steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm cache-dependency-path: pnpm-lock.yaml - registry-url: https://registry.npmjs.org - run: pnpm install --frozen-lockfile - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: wasm-commands path: packages/core/commands - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: wasm-command-licenses path: toolchain/c/build @@ -314,7 +329,7 @@ jobs: mkdir -p "$commands_dir" cp -a packages/core/commands/. "$commands_dir/" test -s "$commands_dir/sh" - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: pattern: sidecar-* path: artifacts @@ -330,9 +345,11 @@ jobs: chmod +x "${agent_dest}/agentos-sidecar" done - name: Bump package versions for build + env: + VERSION: ${{ needs.context.outputs.version }} run: | pnpm --filter=publish exec tsx src/ci/bin.ts bump-versions \ - --version ${{ needs.context.outputs.version }} \ + --version "$VERSION" \ --version-only - run: node packages/core/scripts/copy-wasm-commands.mjs --require - name: Build TypeScript packages @@ -346,26 +363,78 @@ jobs: run: | pnpm --filter=publish exec tsx src/ci/bin.ts stage-software \ --output "$GITHUB_WORKSPACE/software-artifacts" - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 with: name: software-artifacts path: software-artifacts if-no-files-found: error - name: Finalize package versions for publish + env: + VERSION: ${{ needs.context.outputs.version }} run: | pnpm --filter=publish exec tsx src/ci/bin.ts bump-versions \ - --version ${{ needs.context.outputs.version }} - - name: Publish npm packages + --version "$VERSION" + - name: Pack npm packages env: - NODE_AUTH_TOKEN: "" + VERSION: ${{ needs.context.outputs.version }} run: | - pnpm --filter=publish exec tsx src/ci/bin.ts publish-npm \ - --tag ${{ needs.context.outputs.npm_tag }} \ - --parallel 16 \ - --retries 3 \ - ${{ needs.context.outputs.trigger == 'release' && '--release-mode' || '' }} + pnpm --filter=publish exec tsx src/ci/bin.ts prepare-packed-npm \ + --version "$VERSION" --output "$RUNNER_TEMP/npm-packages" + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: npm-publish-tarballs + path: ${{ runner.temp }}/npm-packages/ + if-no-files-found: error + compression-level: 0 + retention-days: 1 + + publish-npm: + needs: [context, prepare-npm] + name: Publish npm + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + sparse-checkout: scripts/publish + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 + with: + node-version: 24 + registry-url: https://registry.npmjs.org + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 + with: + name: npm-publish-tarballs + path: ${{ runner.temp }}/npm-packages/ + - name: Publish prepared npm packages + env: + NODE_AUTH_TOKEN: "" + VERSION: ${{ needs.context.outputs.version }} + NPM_TAG: ${{ needs.context.outputs.npm_tag }} + TRIGGER: ${{ needs.context.outputs.trigger }} + run: node scripts/publish/src/ci/publish-packed-npm.ts "$RUNNER_TEMP/npm-packages" + + publish-software-mirror: + needs: [context, publish-npm] + name: Publish software mirror + if: ${{ needs.context.outputs.trigger == 'release' }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 + with: + node-version: 24 + - run: pnpm install --frozen-lockfile --filter=publish --filter=@rivet-dev/agentos-toolchain + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 + with: + name: software-artifacts + path: software-artifacts - name: Upload software artifacts to R2 - if: ${{ needs.context.outputs.trigger == 'release' }} env: R2_RELEASES_ACCESS_KEY_ID: ${{ secrets.R2_RELEASES_ACCESS_KEY_ID }} R2_RELEASES_SECRET_ACCESS_KEY: ${{ secrets.R2_RELEASES_SECRET_ACCESS_KEY }} @@ -390,22 +459,23 @@ jobs: release-assets: needs: [context, build-sidecar, build-sidecar-darwin, smoke-sidecar-artifacts] name: Release assets - if: ${{ !cancelled() && needs.build-sidecar.result == 'success' && needs.build-sidecar-darwin.result == 'success' && needs.smoke-sidecar-artifacts.result == 'success' }} + if: ${{ !cancelled() && needs.context.outputs.trigger == 'release' && needs.build-sidecar.result == 'success' && needs.build-sidecar-darwin.result == 'success' && needs.smoke-sidecar-artifacts.result == 'success' }} runs-on: ubuntu-latest permissions: contents: write steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 with: + persist-credentials: false fetch-depth: 0 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm cache-dependency-path: pnpm-lock.yaml - run: pnpm install --frozen-lockfile --filter=publish --filter=@rivet-dev/agentos-toolchain - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: pattern: sidecar-* path: artifacts @@ -478,18 +548,22 @@ jobs: if: ${{ !cancelled() && needs.build-sidecar.result == 'success' && (needs.release-assets.result == 'success' || needs.release-assets.result == 'skipped') }} runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - name: Install reviewed V8 archive + run: python3 scripts/ci/v8/engine.py install x86_64-unknown-linux-gnu "$RUNNER_TEMP/agentos-v8" --cargo-config "$GITHUB_WORKSPACE/../.cargo/config.toml" --github-env "$GITHUB_ENV" + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24 cache: pnpm cache-dependency-path: pnpm-lock.yaml - - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v2 + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2 with: workspaces: . -> target - - uses: actions/cache@v4 + - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 with: path: ~/.cargo/.rusty_v8 key: ${{ runner.os }}-x86_64-unknown-linux-gnu-rusty-v8-${{ hashFiles('Cargo.lock') }} @@ -497,9 +571,11 @@ jobs: ${{ runner.os }}-x86_64-unknown-linux-gnu-rusty-v8- - run: pnpm install --frozen-lockfile - name: Bump Cargo versions + env: + VERSION: ${{ needs.context.outputs.version }} run: | pnpm --filter=publish exec tsx src/ci/bin.ts bump-versions \ - --version ${{ needs.context.outputs.version }} \ + --version "$VERSION" \ --version-only - name: Stage vendored V8 bridge bundles and base filesystem run: | @@ -519,23 +595,27 @@ jobs: cp crates/vfs-core/assets/base-filesystem.json crates/vm/assets/base-filesystem.json - name: Dry-run crate publish if: ${{ needs.context.outputs.trigger != 'release' }} + env: + VERSION: ${{ needs.context.outputs.version }} run: | pnpm --filter=publish exec tsx src/ci/bin.ts publish-crates \ - --version ${{ needs.context.outputs.version }} \ + --version "$VERSION" \ --dry-run \ --allow-dirty - name: Publish crates if: ${{ needs.context.outputs.trigger == 'release' }} env: + VERSION: ${{ needs.context.outputs.version }} CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} run: | pnpm --filter=publish exec tsx src/ci/bin.ts publish-crates \ - --version ${{ needs.context.outputs.version }} \ + --version "$VERSION" \ --allow-dirty notify-managed-services: name: Notify managed services - needs: [context, publish-npm, release-assets, publish-crates] + permissions: {} + needs: [context, publish-npm, publish-software-mirror, release-assets, publish-crates] if: ${{ success() && needs.context.outputs.trigger == 'release' }} runs-on: ubuntu-latest steps: diff --git a/Cargo.lock b/Cargo.lock index 2415343672..186d7f7fa9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,12 +11,6 @@ dependencies = [ "gimli", ] -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - [[package]] name = "adler2" version = "2.0.1" @@ -251,7 +245,6 @@ dependencies = [ "cc", "ciborium", "crossbeam-channel", - "deno_core", "flume", "getrandom 0.2.17", "libc", @@ -1143,7 +1136,7 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6dc683efb34b9e755675b37fedbe0103141e5b6df7bdc9eb6967756a8c167d8" dependencies = [ - "base64-simd 0.8.0", + "base64-simd", "bytes", "bytes-utils", "futures-core", @@ -1186,7 +1179,7 @@ dependencies = [ "aws-smithy-runtime-api", "aws-smithy-schema", "aws-smithy-types", - "rustc_version 0.4.1", + "rustc_version", "tracing", ] @@ -1301,12 +1294,6 @@ dependencies = [ "syn 2.0.118", ] -[[package]] -name = "az" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be5eb007b7cacc6c660343e96f650fedf4b5a77512399eb952ca6642cf8d13f7" - [[package]] name = "base16ct" version = "0.2.0" @@ -1319,22 +1306,13 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" -[[package]] -name = "base64-simd" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "781dd20c3aff0bd194fe7d2a977dd92f21c173891f3a03b677359e5fa457e5d5" -dependencies = [ - "simd-abstraction", -] - [[package]] name = "base64-simd" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "339abbe78e73178762e23bea9dfd08e697eb3f3301cd4be981c0f78ba5859195" dependencies = [ - "outref 0.5.2", + "outref", "vsimd", ] @@ -1344,20 +1322,11 @@ version = "1.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" -[[package]] -name = "bincode" -version = "1.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" -dependencies = [ - "serde", -] - [[package]] name = "bindgen" -version = "0.70.1" +version = "0.72.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f49d8fed880d473ea71efb9bf597651e77201bdd4893efe54c9e5d65ae04ce6f" +checksum = "993776b509cfb49c750f11b8f07a46fa23e0a1386ffc01fb1e7d343efc387895" dependencies = [ "bitflags", "cexpr", @@ -1368,44 +1337,17 @@ dependencies = [ "proc-macro2", "quote", "regex", - "rustc-hash 1.1.0", + "rustc-hash", "shlex 1.3.0", "syn 2.0.118", ] -[[package]] -name = "bit-set" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1" -dependencies = [ - "bit-vec", -] - -[[package]] -name = "bit-vec" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" - [[package]] name = "bitflags" version = "2.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4388bee8683e3d04af747c73422af53102d2bd24d9eadb6cbc100baef4b43f8" -[[package]] -name = "bitvec" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddcec3d12c579d40898fe0a9a358a803c23e9c52ca3c425707f81c9436211837" -dependencies = [ - "funty", - "radium", - "tap", - "wyz", -] - [[package]] name = "blake3" version = "1.8.5" @@ -1488,12 +1430,13 @@ dependencies = [ ] [[package]] -name = "capacity_builder" -version = "0.1.3" +name = "calendrical_calculations" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58ec49028cb308564429cd8fac4ef21290067a0afe8f5955330a8d487d0d790c" +checksum = "5abbd6eeda6885048d357edc66748eea6e0268e3dd11f326fff5bd248d779c26" dependencies = [ - "itoa", + "core_maths", + "displaydoc", ] [[package]] @@ -1707,12 +1650,6 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d52eff69cd5e647efe296129160853a42795992097e8af39800e1060caeea9b" -[[package]] -name = "cooked-waker" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147be55d677052dabc6b22252d5dd0fd4c29c8c27aa4f2fbef0f94aa003b406f" - [[package]] name = "core-foundation" version = "0.9.4" @@ -1745,6 +1682,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" +[[package]] +name = "core_maths" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77745e017f5edba1a9c1d854f6f3a52dac8a12dd5af5d2f54aecf61e43d80d30" +dependencies = [ + "libm", +] + [[package]] name = "cow-utils" version = "0.1.3" @@ -1839,7 +1785,7 @@ dependencies = [ "postcard", "pulley-interpreter", "regalloc2", - "rustc-hash 2.1.3", + "rustc-hash", "serde", "serde_derive", "sha2 0.10.9", @@ -1857,7 +1803,7 @@ dependencies = [ "cranelift-assembler-x64-meta", "cranelift-codegen-shared", "cranelift-srcgen", - "heck 0.5.0", + "heck", "pulley-interpreter", ] @@ -2044,127 +1990,6 @@ version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4ae5f15dda3c708c0ade84bfee31ccab44a3da4f88015ed22f63732abe300c8" -[[package]] -name = "debugid" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef552e6f588e446098f6ba40d89ac146c8c7b64aade83c051ee00bb5d2bc18d" -dependencies = [ - "serde", - "uuid", -] - -[[package]] -name = "deno_core" -version = "0.336.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdd50476c4325d5fa52bb906804a1e35b127d2a1dcf674e3447b53dcf25525bf" -dependencies = [ - "anyhow", - "az", - "bincode", - "bit-set", - "bit-vec", - "bytes", - "capacity_builder", - "cooked-waker", - "deno_core_icudata", - "deno_error", - "deno_ops", - "deno_path_util", - "deno_unsync", - "futures", - "indexmap 2.14.0", - "libc", - "memoffset", - "parking_lot", - "percent-encoding", - "pin-project", - "serde", - "serde_json", - "serde_v8", - "smallvec", - "sourcemap", - "static_assertions", - "thiserror 2.0.21", - "tokio", - "url", - "v8", - "wasm_dep_analyzer", -] - -[[package]] -name = "deno_core_icudata" -version = "0.74.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe4dccb6147bb3f3ba0c7a48e993bfeb999d2c2e47a81badee80e2b370c8d695" - -[[package]] -name = "deno_error" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e983933fb4958fbe1e0a63c1e89a2af72b12c409e86404e547955564e6e217b8" -dependencies = [ - "deno_error_macro", - "libc", - "serde", - "serde_json", - "tokio", - "url", -] - -[[package]] -name = "deno_error_macro" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1ad5ae3ef15db33e917d6ed54b53d0a98d068c4d1217eb35a4997423203c3ef" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.118", -] - -[[package]] -name = "deno_ops" -version = "0.212.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2d328067139909aa81522a5d90f119368b541fbddd73ab630e4d9f777865f0d" -dependencies = [ - "indexmap 2.14.0", - "proc-macro-rules", - "proc-macro2", - "quote", - "stringcase", - "strum", - "strum_macros", - "syn 2.0.118", - "thiserror 2.0.21", -] - -[[package]] -name = "deno_path_util" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8850326ea9cb786aafd938f3de9866432904c0bae3aa0139a7a4e570b0174f6" -dependencies = [ - "deno_error", - "percent-encoding", - "sys_traits", - "thiserror 2.0.21", - "url", -] - -[[package]] -name = "deno_unsync" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6742a724e8becb372a74c650a1aefb8924a5b8107f7d75b3848763ea24b27a87" -dependencies = [ - "futures-util", - "parking_lot", - "tokio", -] - [[package]] name = "der" version = "0.7.10" @@ -2206,6 +2031,38 @@ dependencies = [ "ctutils", ] +[[package]] +name = "diplomat" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7935649d00000f5c5d735448ad3dc07b9738160727017914cf42138b8e8e6611" +dependencies = [ + "diplomat_core", + "proc-macro2", + "quote", + "syn 2.0.118", +] + +[[package]] +name = "diplomat-runtime" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "970ac38ad677632efcee6d517e783958da9bc78ec206d8d5e35b459ffc5e4864" + +[[package]] +name = "diplomat_core" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cf41b94101a4bce993febaf0098092b0bb31deaf0ecaf6e0a2562465f61b383" +dependencies = [ + "proc-macro2", + "quote", + "serde", + "smallvec", + "strck", + "syn 2.0.118", +] + [[package]] name = "displaydoc" version = "0.2.6" @@ -2373,7 +2230,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c" dependencies = [ "crc32fast", - "miniz_oxide 0.8.9", + "miniz_oxide", ] [[package]] @@ -2450,12 +2307,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "funty" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" - [[package]] name = "futures" version = "0.3.32" @@ -2715,12 +2566,6 @@ dependencies = [ "hashbrown 0.17.1", ] -[[package]] -name = "heck" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" - [[package]] name = "heck" version = "0.5.0" @@ -3013,6 +2858,28 @@ dependencies = [ "cc", ] +[[package]] +name = "icu_calendar" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a68ffb801cc7acb8dcae62da7c692c47934698d2dd4f479ddee1eb442763dea1" +dependencies = [ + "calendrical_calculations", + "displaydoc", + "icu_calendar_data", + "icu_locale_core", + "icu_locale_fallback", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_calendar_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "514da54e79efa36b5976232d5b3d1d8ee1b47b19ab658f1b1ebe39221696c2d8" + [[package]] name = "icu_collections" version = "2.2.0" @@ -3029,17 +2896,38 @@ dependencies = [ [[package]] name = "icu_locale_core" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92219b62b3e2b4d88ac5119f8904c10f8f61bf7e95b640d25ba3075e6cac2c29" +checksum = "25d29b57c17c6528d0dc4c2141cfa2d08200465acb537a3909c8b9b8a4a984c0" dependencies = [ "displaydoc", "litemap", + "serde", "tinystr", "writeable", "zerovec", ] +[[package]] +name = "icu_locale_fallback" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "406e9ae5b95d8aa37db6293405931b766a6172d1848daee22d70eb44aa78a43c" +dependencies = [ + "icu_locale_core", + "icu_locale_fallback_data", + "icu_provider", + "potential_utf", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_locale_fallback_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3632ebbd0c5c3637ba1a40bdf8288f2cb9d2268e73441a69d5fc73d7cfbaa87c" + [[package]] name = "icu_normalizer" version = "2.2.0" @@ -3082,12 +2970,14 @@ checksum = "8e2bbb201e0c04f7b4b3e14382af113e17ba4f63e2c9d2ee626b720cbce54a14" [[package]] name = "icu_provider" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "139c4cf31c8b5f33d7e199446eff9c1e02decfc2f0eec2c8d71f65befa45b421" +checksum = "413099315636c588ee8f011190b329b971a09a8fae9d157e01dcae8379468514" dependencies = [ "displaydoc", "icu_locale_core", + "serde", + "stable_deref_trait", "writeable", "yoke", "zerofrom", @@ -3122,12 +3012,6 @@ dependencies = [ "icu_properties", ] -[[package]] -name = "if_chain" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd62e6b5e86ea8eeeb8db1de02880a6abc01a397b2ebb64b5d74ac255318f5cb" - [[package]] name = "include_dir" version = "0.7.4" @@ -3242,6 +3126,12 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" +[[package]] +name = "ixdtf" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4754a1731458af0443709c9667302793d3f63cc90ec4ba4c8259f2b9515478e5" + [[package]] name = "jni" version = "0.22.4" @@ -3267,7 +3157,7 @@ checksum = "a00109accc170f0bdb141fed3e393c565b6f5e072365c3bd58f5b062591560a3" dependencies = [ "proc-macro2", "quote", - "rustc_version 0.4.1", + "rustc_version", "simd_cesu8", "syn 2.0.118", ] @@ -3534,15 +3424,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" -[[package]] -name = "miniz_oxide" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" -dependencies = [ - "adler", -] - [[package]] name = "miniz_oxide" version = "0.8.9" @@ -3672,7 +3553,6 @@ checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" dependencies = [ "num-integer", "num-traits", - "rand 0.8.8", ] [[package]] @@ -3862,12 +3742,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "outref" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f222829ae9293e33a9f5e9f440c6760a3d450a64affe1846486b140db81c1f4" - [[package]] name = "outref" version = "0.5.2" @@ -3888,7 +3762,7 @@ checksum = "abb7a1163a5501f935f8722d839b576491b749c695e7a066aa0b8df988b806df" dependencies = [ "flate2", "postcard", - "rustc-hash 2.1.3", + "rustc-hash", "serde", "serde_json", "thiserror 2.0.21", @@ -3929,7 +3803,7 @@ dependencies = [ "allocator-api2", "hashbrown 0.16.1", "oxc_data_structures", - "rustc-hash 2.1.3", + "rustc-hash", ] [[package]] @@ -3991,7 +3865,7 @@ dependencies = [ "oxc_sourcemap", "oxc_span", "oxc_syntax", - "rustc-hash 2.1.3", + "rustc-hash", ] [[package]] @@ -4003,7 +3877,7 @@ dependencies = [ "cow-utils", "oxc-browserslist", "oxc_syntax", - "rustc-hash 2.1.3", + "rustc-hash", "serde", ] @@ -4078,7 +3952,7 @@ dependencies = [ "oxc_regular_expression", "oxc_span", "oxc_syntax", - "rustc-hash 2.1.3", + "rustc-hash", "seq-macro", ] @@ -4094,7 +3968,7 @@ dependencies = [ "oxc_diagnostics", "oxc_span", "phf 0.13.1", - "rustc-hash 2.1.3", + "rustc-hash", "unicode-id-start", ] @@ -4115,7 +3989,7 @@ dependencies = [ "oxc_index", "oxc_span", "oxc_syntax", - "rustc-hash 2.1.3", + "rustc-hash", "self_cell", "smallvec", ] @@ -4126,9 +4000,9 @@ version = "6.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d378eb8bad20e89d66276aebab51f6a5408571092cac94abdd3eabb773713d6" dependencies = [ - "base64-simd 0.8.0", + "base64-simd", "json-escape-simd", - "rustc-hash 2.1.3", + "rustc-hash", "serde", "serde_json", ] @@ -4189,7 +4063,7 @@ dependencies = [ "oxc_span", "oxc_syntax", "oxc_traverse", - "rustc-hash 2.1.3", + "rustc-hash", "serde", "serde_json", "sha1 0.10.6", @@ -4210,7 +4084,7 @@ dependencies = [ "oxc_semantic", "oxc_span", "oxc_syntax", - "rustc-hash 2.1.3", + "rustc-hash", ] [[package]] @@ -4493,6 +4367,8 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0103b1cef7ec0cf76490e969665504990193874ea05c85ff9bab8b911d0a0564" dependencies = [ + "serde_core", + "writeable", "zerovec", ] @@ -4541,29 +4417,6 @@ dependencies = [ "elliptic-curve", ] -[[package]] -name = "proc-macro-rules" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07c277e4e643ef00c1233393c673f655e3672cf7eb3ba08a00bdd0ea59139b5f" -dependencies = [ - "proc-macro-rules-macros", - "proc-macro2", - "syn 2.0.118", -] - -[[package]] -name = "proc-macro-rules-macros" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "207fffb0fe655d1d47f6af98cc2793405e85929bdbc420d685554ff07be27ac7" -dependencies = [ - "once_cell", - "proc-macro2", - "quote", - "syn 2.0.118", -] - [[package]] name = "proc-macro2" version = "1.0.106" @@ -4689,7 +4542,7 @@ dependencies = [ "pin-project-lite", "quinn-proto", "quinn-udp", - "rustc-hash 2.1.3", + "rustc-hash", "rustls", "socket2 0.6.4", "thiserror 2.0.21", @@ -4710,7 +4563,7 @@ dependencies = [ "rand 0.10.2", "rand_pcg", "ring", - "rustc-hash 2.1.3", + "rustc-hash", "rustls", "rustls-pki-types", "slab", @@ -4755,12 +4608,6 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" -[[package]] -name = "radium" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" - [[package]] name = "rand" version = "0.8.8" @@ -4865,7 +4712,7 @@ dependencies = [ "bumpalo", "hashbrown 0.17.1", "log", - "rustc-hash 2.1.3", + "rustc-hash", "serde", "smallvec", ] @@ -4951,6 +4798,16 @@ dependencies = [ "webpki-roots 1.0.8", ] +[[package]] +name = "resb" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22d392791f3c6802a1905a509e9d1a6039cbbcb5e9e00e5a6d3661f7c874f390" +dependencies = [ + "potential_utf", + "serde_core", +] + [[package]] name = "resolv-conf" version = "0.7.6" @@ -5088,7 +4945,7 @@ version = "0.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd4070bf7508533980a812761f8b743cb4747b5e540c3635719e1a9181bd6000" dependencies = [ - "heck 0.5.0", + "heck", "pest", "pest_derive", "proc-macro2", @@ -5322,34 +5179,19 @@ version = "0.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b74b56ffa8bb2830709a538c2cbcae9aa062db0d2a42563bfb09bdaae44020eb" -[[package]] -name = "rustc-hash" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" - [[package]] name = "rustc-hash" version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" -[[package]] -name = "rustc_version" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" -dependencies = [ - "semver 0.9.0", -] - [[package]] name = "rustc_version" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" dependencies = [ - "semver 1.0.28", + "semver", ] [[package]] @@ -5577,15 +5419,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2ab42ca02749e120097e328d91d415325bdf43b1c72c4c8badf37375fe40a813" -[[package]] -name = "semver" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" -dependencies = [ - "semver-parser", -] - [[package]] name = "semver" version = "1.0.28" @@ -5596,12 +5429,6 @@ dependencies = [ "serde_core", ] -[[package]] -name = "semver-parser" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" - [[package]] name = "seq-macro" version = "0.3.6" @@ -5673,7 +5500,6 @@ version = "1.0.150" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9" dependencies = [ - "indexmap 2.14.0", "itoa", "memchr", "serde", @@ -5704,20 +5530,6 @@ dependencies = [ "serde", ] -[[package]] -name = "serde_v8" -version = "0.245.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "945f93c91e0c7e4799b5fefff076756141aae92e262c4dc4833310dd3d2d845e" -dependencies = [ - "deno_error", - "num-bigint", - "serde", - "smallvec", - "thiserror 2.0.21", - "v8", -] - [[package]] name = "sha1" version = "0.10.6" @@ -5813,15 +5625,6 @@ dependencies = [ "rand_core 0.6.4", ] -[[package]] -name = "simd-abstraction" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cadb29c57caadc51ff8346233b5cec1d240b68ce55cf1afc764818791876987" -dependencies = [ - "outref 0.1.0", -] - [[package]] name = "simd-adler32" version = "0.3.9" @@ -5834,7 +5637,7 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94f90157bb87cddf702797c5dadfa0be7d266cdf49e22da2fcaa32eff75b2c33" dependencies = [ - "rustc_version 0.4.1", + "rustc_version", "simdutf8", ] @@ -5903,25 +5706,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "sourcemap" -version = "8.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "208d40b9e8cad9f93613778ea295ed8f3c2b1824217c6cfc7219d3f6f45b96d4" -dependencies = [ - "base64-simd 0.7.0", - "bitvec", - "data-encoding", - "debugid", - "if_chain", - "rustc-hash 1.1.0", - "rustc_version 0.2.3", - "serde", - "serde_json", - "unicode-id-start", - "url", -] - [[package]] name = "spin" version = "0.9.9" @@ -5976,31 +5760,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d08889ec5408683408db66ad89e0e1f93dff55c73a4ccc71c427d5b277ee47e6" [[package]] -name = "stringcase" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04028eeb851ed08af6aba5caa29f2d59a13ed168cee4d6bd753aeefcf1d636b0" - -[[package]] -name = "strum" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" -dependencies = [ - "strum_macros", -] - -[[package]] -name = "strum_macros" -version = "0.25.3" +name = "strck" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23dc1fa9ac9c169a78ba62f0b841814b7abae11bdd047b9c58f893439e309ea0" +checksum = "42316e70da376f3d113a68d138a60d8a9883c604fe97942721ec2068dab13a9f" dependencies = [ - "heck 0.4.1", - "proc-macro2", - "quote", - "rustversion", - "syn 2.0.118", + "unicode-ident", ] [[package]] @@ -6058,23 +5823,14 @@ dependencies = [ ] [[package]] -name = "sys_traits" -version = "0.1.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7a35f11f9773f11930a7d1e6276766f1577400ee2530b7f0cf3844c2dcbf12b" -dependencies = [ - "sys_traits_macros", -] - -[[package]] -name = "sys_traits_macros" -version = "0.1.0" +name = "synstructure" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "181f22127402abcf8ee5c83ccd5b408933fec36a6095cf82cda545634692657e" +checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02" dependencies = [ "proc-macro2", "quote", - "syn 2.0.118", + "syn 3.0.10", ] [[package]] @@ -6104,12 +5860,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417" -[[package]] -name = "tap" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" - [[package]] name = "tar" version = "0.4.46" @@ -6134,12 +5884,46 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom 0.4.3", "once_cell", "rustix 1.1.4", "windows-sys 0.61.2", ] +[[package]] +name = "temporal_capi" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a2a1f001e756a9f5f2d175a9965c4c0b3a054f09f30de3a75ab49765f2deb36" +dependencies = [ + "diplomat", + "diplomat-runtime", + "icu_calendar", + "icu_locale_core", + "num-traits", + "temporal_rs", + "timezone_provider", + "writeable", + "zoneinfo64", +] + +[[package]] +name = "temporal_rs" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a902a45282e5175186b21d355efc92564601efe6e2d92818dc9e333d50bd4de" +dependencies = [ + "calendrical_calculations", + "core_maths", + "icu_calendar", + "icu_locale_core", + "ixdtf", + "num-traits", + "timezone_provider", + "tinystr", + "writeable", +] + [[package]] name = "termcolor" version = "1.4.1" @@ -6239,13 +6023,26 @@ dependencies = [ "time-core", ] +[[package]] +name = "timezone_provider" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c48f9b04628a2b813051e4dfe97c65281e49625eabd09ec343190e31e399a8c2" +dependencies = [ + "tinystr", + "zerotrie", + "zerovec", + "zoneinfo64", +] + [[package]] name = "tinystr" -version = "0.8.3" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8323304221c2a851516f22236c5722a72eaa19749016521d6dff0824447d96d" +checksum = "abc840c238154d58f9e86b2792261edfde565a17696e1cda1a449de27979801a" dependencies = [ "displaydoc", + "serde_core", "zerovec", ] @@ -6768,18 +6565,17 @@ dependencies = [ [[package]] name = "v8" -version = "130.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a511192602f7b435b0a241c1947aa743eb7717f20a9195f4b5e8ed1952e01db1" +version = "158.0.0" +source = "git+https://github.com/denoland/rusty_v8?rev=548adfd54db42c670e0f3fd42211c3af109c0a73#548adfd54db42c670e0f3fd42211c3af109c0a73" dependencies = [ "bindgen", "bitflags", "fslock", "gzip-header", "home", - "miniz_oxide 0.7.4", - "once_cell", + "miniz_oxide", "paste", + "temporal_capi", "which", ] @@ -6826,7 +6622,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43a93a1d4c831d6b0d43075e11079de97b18f945e35d2953d2d5d340a68d2624" dependencies = [ - "heck 0.5.0", + "heck", "pest", "pest_derive", "proc-macro2", @@ -7007,16 +6803,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "wasm_dep_analyzer" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2eeee3bdea6257cc36d756fa745a70f9d393571e47d69e0ed97581676a5369ca" -dependencies = [ - "deno_error", - "thiserror 2.0.21", -] - [[package]] name = "wasmparser" version = "0.248.0" @@ -7025,7 +6811,7 @@ checksum = "aa4439c5eee9df71ee0c6efb37f63b1fcb1fec38f85f5142c54e7ed05d33091a" dependencies = [ "bitflags", "indexmap 2.14.0", - "semver 1.0.28", + "semver", ] [[package]] @@ -7037,7 +6823,7 @@ dependencies = [ "bitflags", "hashbrown 0.17.1", "indexmap 2.14.0", - "semver 1.0.28", + "semver", "serde", ] @@ -7050,7 +6836,7 @@ dependencies = [ "bitflags", "hashbrown 0.17.1", "indexmap 2.14.0", - "semver 1.0.28", + "semver", "serde", ] @@ -7121,7 +6907,7 @@ dependencies = [ "object", "postcard", "rustc-demangle", - "semver 1.0.28", + "semver", "serde", "serde_derive", "sha2 0.10.9", @@ -7258,7 +7044,7 @@ checksum = "62a5137cc642ddf52082099b6aa9d38bed5105cb2918fe1c8779013842132022" dependencies = [ "anyhow", "bitflags", - "heck 0.5.0", + "heck", "indexmap 2.14.0", "wit-component", "wit-parser", @@ -7594,7 +7380,7 @@ dependencies = [ "id-arena", "indexmap 2.14.0", "log", - "semver 1.0.28", + "semver", "serde", "serde_derive", "serde_json", @@ -7616,18 +7402,9 @@ dependencies = [ [[package]] name = "writeable" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ffae5123b2d3fc086436f8834ae3ab053a283cfac8fe0a0b8eaae044768a4c4" - -[[package]] -name = "wyz" -version = "0.5.1" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" -dependencies = [ - "tap", -] +checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" [[package]] name = "xattr" @@ -7647,9 +7424,9 @@ checksum = "66fee0b777b0f5ac1c69bb06d361268faafa61cd4682ae064a171c16c433e9e4" [[package]] name = "yoke" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5" +checksum = "417ef3fcf6c300b7dbd25c85531a746931c02ad536f3cf4786ed1e88223340b3" dependencies = [ "stable_deref_trait", "yoke-derive", @@ -7658,14 +7435,14 @@ dependencies = [ [[package]] name = "yoke-derive" -version = "0.8.2" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e" +checksum = "ec8ebde2db3681e8c9980cc27822030e68752690ddfa9473e739aeb4dbde6d71" dependencies = [ "proc-macro2", "quote", - "syn 2.0.118", - "synstructure", + "syn 3.0.10", + "synstructure 0.14.0", ] [[package]] @@ -7706,7 +7483,7 @@ dependencies = [ "proc-macro2", "quote", "syn 2.0.118", - "synstructure", + "synstructure 0.13.2", ] [[package]] @@ -7717,21 +7494,23 @@ checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e" [[package]] name = "zerotrie" -version = "0.2.4" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f9152d31db0792fa83f70fb2f83148effb5c1f5b8c7686c3459e361d9bc20bf" +checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f" dependencies = [ "displaydoc", "yoke", "zerofrom", + "zerovec", ] [[package]] name = "zerovec" -version = "0.11.6" +version = "0.11.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90f911cbc359ab6af17377d242225f4d75119aec87ea711a880987b18cd7b239" +checksum = "f58a2799cfe9a1670c7ea8edbd52ef8edb93cb37a7f83bf31ba9e35d9dae6a40" dependencies = [ + "serde", "yoke", "zerofrom", "zerovec-derive", @@ -7739,13 +7518,13 @@ dependencies = [ [[package]] name = "zerovec-derive" -version = "0.11.3" +version = "0.11.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "625dc425cab0dca6dc3c3319506e6593dcb08a9f387ea3b284dbd52a92c40555" +checksum = "c3743ddbf47738d929cd1ae2e199d40d0162979dfe31aef8ae646d2bf00bf97a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.118", + "syn 3.0.10", ] [[package]] @@ -7753,3 +7532,16 @@ name = "zmij" version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" + +[[package]] +name = "zoneinfo64" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed6eb2607e906160c457fd573e9297e65029669906b9ac8fb1b5cd5e055f0705" +dependencies = [ + "calendrical_calculations", + "icu_locale_core", + "potential_utf", + "resb", + "serde", +] diff --git a/Cargo.toml b/Cargo.toml index 67544172ce..2c29dcae71 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ repository = "https://github.com/rivet-dev/agent-os" # AgentOS crates are published as a lock-step chain and are self-contained in # this workspace. [workspace.dependencies] +v8 = "=158.0.0" agentos-actor = { path = "crates/actor", version = "0.0.1" } agentos-actor-contract = { path = "crates/actor-contract", version = "0.0.1" } agentos-preload = { path = "crates/preload", version = "0.0.1" } @@ -89,3 +90,11 @@ lto = "fat" codegen-units = 1 panic = "abort" strip = "symbols" + +# Keep the embedder bindings pinned until the matching crate is published. +[patch.crates-io] +v8 = { git = "https://github.com/denoland/rusty_v8", rev = "548adfd54db42c670e0f3fd42211c3af109c0a73" } + +# Upstream requires optimized Rust bindings even with a debug embedder. +[profile.dev.package."v8:158.0.0"] +opt-level = 1 diff --git a/crates/client/src/process.rs b/crates/client/src/process.rs index 1891bf031f..d4e4fc0cca 100644 --- a/crates/client/src/process.rs +++ b/crates/client/src/process.rs @@ -577,11 +577,32 @@ pub struct ExecOptions { pub on_stdout: Option, pub on_stderr: Option, pub capture_stdio: Option, + /// Combined host-captured stdout and stderr bytes. Defaults to 16 MiB. + pub max_captured_output_bytes: Option, pub file_path: Option, + /// Native execution requires the CPU limit to be configured on the VM. pub cpu_time_limit_ms: Option, + /// Off preserves the VM clock policy. Freeze is unsupported by native execution. pub timing_mitigation: Option, } +fn check_execution_options(options: &ExecOptions) -> std::result::Result<(), ClientError> { + let message = if options.cpu_time_limit_ms.is_some() { + "Per-run cpu_time_limit_ms is unsupported by the native sidecar. Configure VM limits.js_runtime.cpu_time_limit_ms or limits.wasm.active_cpu_time_limit_ms." + } else if options + .timing_mitigation + .is_some_and(|mode| mode != TimingMitigation::Off) + { + "Per-run timing_mitigation is unsupported by the native sidecar. Configure VM-level high_resolution_time to control clock precision." + } else { + return Ok(()); + }; + Err(ClientError::Kernel { + code: "ERR_AGENTOS_UNSUPPORTED_EXECUTION_OPTION".into(), + message: message.into(), + }) +} + impl Default for ExecOptions { fn default() -> Self { Self { @@ -593,6 +614,7 @@ impl Default for ExecOptions { on_stdout: None, on_stderr: None, capture_stdio: None, + max_captured_output_bytes: None, file_path: None, cpu_time_limit_ms: None, timing_mitigation: None, @@ -825,6 +847,17 @@ impl AgentOs { args: &[String], mut options: ExecOptions, ) -> Result { + check_execution_options(&options)?; + let capture_limit = options + .max_captured_output_bytes + .unwrap_or(EXEC_OUTPUT_CAPTURE_LIMIT_BYTES); + if capture_limit == 0 { + return Err(ClientError::Kernel { + code: "EINVAL".into(), + message: "max_captured_output_bytes must be positive".into(), + } + .into()); + } // The deadline covers launch, stdin delivery, and execution. Cleanup has its own bounded // confirmation window; it must not silently consume an unbounded slice of the actor's // action deadline. @@ -990,6 +1023,7 @@ impl AgentOs { &mut stdout, &output.chunk, &mut captured_output_bytes, + capture_limit, "stdout", ) { Ok(()) => {} @@ -1012,6 +1046,7 @@ impl AgentOs { &mut stderr, &output.chunk, &mut captured_output_bytes, + capture_limit, "stderr", ) { Ok(()) => {} @@ -2491,25 +2526,41 @@ fn append_exec_output( buffer: &mut Vec, chunk: &[u8], captured_output_bytes: &mut usize, + limit: usize, channel: &str, ) -> std::result::Result<(), ClientError> { - let next_total = captured_output_bytes - .checked_add(chunk.len()) - .ok_or_else(|| exec_output_limit_error(channel, usize::MAX))?; - if next_total > EXEC_OUTPUT_CAPTURE_LIMIT_BYTES { - return Err(exec_output_limit_error(channel, next_total)); + let next_total = captured_output_bytes.saturating_add(chunk.len()); + if chunk.len() > limit.saturating_sub(*captured_output_bytes) { + return Err(ClientError::ResourceLimit { + code: "ERR_AGENTOS_CAPTURED_OUTPUT_LIMIT".into(), + message: format!( + "exec {channel} capture is {next_total} bytes, limit is {limit}; raise max_captured_output_bytes", + ), + details: Box::new(ResourceLimitDetails { + limit_name: Some("max_captured_output_bytes".into()), + configuration_path: Some("max_captured_output_bytes".into()), + configured_limit: Some(limit as u64), + current_usage: Some(*captured_output_bytes as u64), + requested: Some(next_total as u64), + unit: Some("bytes".into()), + operation: Some("process.exec".into()), + ..Default::default() + }), + }); + } + let warning_threshold = limit - limit / 5; + if *captured_output_bytes < warning_threshold && next_total >= warning_threshold { + tracing::warn!( + next_total, + limit, + "captured output near limit; raise max_captured_output_bytes" + ); } buffer.extend_from_slice(chunk); *captured_output_bytes = next_total; Ok(()) } -fn exec_output_limit_error(channel: &str, size: usize) -> ClientError { - ClientError::Sidecar(format!( - "exec {channel} capture is {size} bytes, limit is {EXEC_OUTPUT_CAPTURE_LIMIT_BYTES}" - )) -} - fn exited_pids_to_prune(mut entries: Vec<(u32, bool, i64)>, target_len: usize) -> Vec { if entries.len() <= target_len { return Vec::new(); @@ -2695,9 +2746,12 @@ mod tests { use super::{ append_exec_output, drain_process_output_tasks, exited_pids_to_prune, install_output_callback, process_registry_limit_error, prune_string_f64_map, ExecOptions, - OutputCallback, DEFAULT_EXEC_CWD, EXEC_OUTPUT_CAPTURE_LIMIT_BYTES, PROCESS_REGISTRY_LIMIT, + OutputCallback, DEFAULT_EXEC_CWD, PROCESS_REGISTRY_LIMIT, + }; + use super::{ + check_execution_options, confirm_exec_exit, wire, ClientError, EventPayload, + TimingMitigation, }; - use super::{confirm_exec_exit, wire, ClientError, EventPayload}; use crate::agent_os::ProcessEntry; use scc::HashMap as SccHashMap; use tokio::sync::{broadcast, watch}; @@ -3177,22 +3231,50 @@ mod tests { } #[test] - fn append_exec_output_rejects_capture_over_limit() { - let mut buffer = vec![0u8; EXEC_OUTPUT_CAPTURE_LIMIT_BYTES - 1]; - let mut captured = buffer.len(); - - append_exec_output(&mut buffer, &[1], &mut captured, "stdout") - .expect("chunk at limit should fit"); - assert_eq!(captured, EXEC_OUTPUT_CAPTURE_LIMIT_BYTES); + fn exec_options_reject_unsupported_cpu_and_timing_overrides() { + assert!(check_execution_options(&ExecOptions::default()).is_ok()); + assert!(check_execution_options(&ExecOptions { + timing_mitigation: Some(TimingMitigation::Off), + ..Default::default() + }) + .is_ok()); + for options in [ + ExecOptions { + timing_mitigation: Some(TimingMitigation::Freeze), + ..Default::default() + }, + ExecOptions { + cpu_time_limit_ms: Some(10.0), + ..Default::default() + }, + ] { + let error = check_execution_options(&options).unwrap_err(); + assert!( + matches!(error, ClientError::Kernel { code, .. } if code == "ERR_AGENTOS_UNSUPPORTED_EXECUTION_OPTION") + ); + } + } - let error = append_exec_output(&mut buffer, &[2], &mut captured, "stdout") - .expect_err("chunk over limit should fail"); - assert!( - error.to_string().contains("exec stdout capture is"), - "unexpected error: {error}" - ); - assert_eq!(captured, EXEC_OUTPUT_CAPTURE_LIMIT_BYTES); - assert_eq!(buffer.len(), EXEC_OUTPUT_CAPTURE_LIMIT_BYTES); + #[test] + fn append_exec_output_rejects_capture_over_combined_limit() { + let mut stdout = vec![1]; + let mut stderr = Vec::new(); + let mut captured = stdout.len(); + append_exec_output(&mut stderr, &[2], &mut captured, 2, "stderr") + .expect("chunk at combined limit should fit"); + let error = append_exec_output(&mut stdout, &[3], &mut captured, 2, "stdout") + .expect_err("chunk over combined limit should fail"); + match error { + ClientError::ResourceLimit { code, details, .. } => { + assert_eq!(code, "ERR_AGENTOS_CAPTURED_OUTPUT_LIMIT"); + assert_eq!(details.configured_limit, Some(2)); + assert_eq!(details.requested, Some(3)); + } + error => panic!("unexpected error: {error}"), + } + assert_eq!(captured, 2); + assert_eq!(stdout, [1]); + assert_eq!(stderr, [2]); } #[test] diff --git a/crates/client/src/software_registry.rs b/crates/client/src/software_registry.rs index 6a8421dd6e..9e7b4713c2 100644 --- a/crates/client/src/software_registry.rs +++ b/crates/client/src/software_registry.rs @@ -409,11 +409,7 @@ pub async fn resolve_registry_software( "agentOS software manifest {manifest_url} lists {name} at an invalid path: {error}" )) })?; - if url.scheme() != "https" && url.scheme() != "http" { - return Err(ClientError::PackageDownload(format!( - "agentOS software manifest {manifest_url} lists {name} at a non-http(s) URL: {url}" - ))); - } + ensure_secure_registry_url(&url, "artifact URL")?; Ok(ResolvedRegistrySoftware { name: name.to_string(), version, diff --git a/crates/client/tests/software_registry.rs b/crates/client/tests/software_registry.rs index 3e18a040ed..d570ca2410 100644 --- a/crates/client/tests/software_registry.rs +++ b/crates/client/tests/software_registry.rs @@ -120,6 +120,27 @@ fn default_version_matches_the_client_release() { ); } +#[tokio::test] +async fn checks_transport_security_when_resolving_each_artifact_url() { + let registry = Registry::start().await; + let manifest = serde_json::json!({"schemaVersion": 1, "artifacts": [{ + "name": "git", "size": 1, "digest": format!("sha256:{DIGEST_HEX}"), + "path": "http://registry.gameinc.io/git.aospkg" + }]}); + registry.serve_raw("latest", 200, manifest.to_string().as_bytes()); + let options = SoftwareRegistryOptions { + url: Some(registry.url.clone()), + version: None, + }; + let error = resolve_registry_software("git", Some("latest"), Some(&options)) + .await + .unwrap_err(); + assert!( + error.to_string().contains("artifact URL must use https"), + "{error}" + ); +} + #[tokio::test] async fn resolves_a_name_to_a_digest_pinned_url() { let registry = Registry::start().await; diff --git a/crates/executor-conformance/tests/guest_clock_policy.rs b/crates/executor-conformance/tests/guest_clock_policy.rs new file mode 100644 index 0000000000..08cf2e47ae --- /dev/null +++ b/crates/executor-conformance/tests/guest_clock_policy.rs @@ -0,0 +1,61 @@ +mod support; + +use std::collections::BTreeMap; + +use agentos_executor_conformance::{ + CreateJavascriptContextRequest, StartJavascriptExecutionRequest, +}; +use tempfile::tempdir; + +#[test] +fn javascript_execution_high_resolution_time_default_off_keeps_coarse_clock() { + let temp = tempdir().expect("create temp dir"); + let mut engine = support::javascript_engine(); + let context = engine.create_context(CreateJavascriptContextRequest { + vm_id: String::from("vm-js-high-res-off"), + bootstrap_module: None, + compile_cache_root: None, + }); + + let execution = engine + .start_execution(StartJavascriptExecutionRequest { + limits: Default::default(), + argv0: None, + guest_runtime: Default::default(), + vm_id: String::from("vm-js-high-res-off"), + context_id: context.context_id, + argv: vec![String::from("./entry.mjs")], + env: BTreeMap::new(), + cwd: temp.path().to_path_buf(), + wasm_module_bytes: None, + inline_code: Some(String::from( + r#" +import vm from "node:vm"; + +if (typeof __agentOsHrNowUs !== "undefined") { + throw new Error("high-resolution host clock exists without opt-in"); +} +for (let attempt = 0; attempt < 20; attempt++) { + const now = process.hrtime.bigint(); + if (now % 1000000n !== 0n) { + throw new Error("process.hrtime.bigint was not millisecond aligned: " + now); + } +} +const childClockIsCoarse = ` + typeof __agentOsHrNowUs === "undefined" && + Number.isInteger(Date.now()) && + (typeof performance === "undefined" || Number.isInteger(performance.now())) +`; +if (!vm.runInNewContext(childClockIsCoarse)) { + throw new Error("node:vm child exposes sub-millisecond time without opt-in"); +} +"#, + )), + }) + .expect("start JavaScript execution"); + + let result = execution.wait().expect("wait for JavaScript execution"); + let stdout = String::from_utf8_lossy(&result.stdout); + let stderr = String::from_utf8_lossy(&result.stderr); + assert_eq!(result.exit_code, 0, "stdout:\n{stdout}\nstderr:\n{stderr}"); +} diff --git a/crates/executor-conformance/tests/javascript_v8.rs b/crates/executor-conformance/tests/javascript_v8.rs index 91e4dab235..59b37de22e 100644 --- a/crates/executor-conformance/tests/javascript_v8.rs +++ b/crates/executor-conformance/tests/javascript_v8.rs @@ -2873,48 +2873,6 @@ if (!sawSubMs) { assert_eq!(result.exit_code, 0, "stdout:\n{stdout}\nstderr:\n{stderr}"); } -fn javascript_execution_high_resolution_time_default_off_keeps_coarse_clock() { - let temp = tempdir().expect("create temp dir"); - let mut engine = support::javascript_engine(); - let context = engine.create_context(CreateJavascriptContextRequest { - vm_id: String::from("vm-js-high-res-off"), - bootstrap_module: None, - compile_cache_root: None, - }); - - let execution = engine - .start_execution(StartJavascriptExecutionRequest { - limits: Default::default(), - argv0: None, - guest_runtime: Default::default(), - vm_id: String::from("vm-js-high-res-off"), - context_id: context.context_id, - argv: vec![String::from("./entry.mjs")], - env: BTreeMap::new(), - cwd: temp.path().to_path_buf(), - wasm_module_bytes: None, - inline_code: Some(String::from( - r#" -if (typeof __agentOsHrNowUs !== "undefined") { - throw new Error("high-resolution host clock exists without opt-in"); -} -for (let attempt = 0; attempt < 20; attempt++) { - const now = process.hrtime.bigint(); - if (now % 1000000n !== 0n) { - throw new Error("process.hrtime.bigint was not millisecond aligned: " + now); - } -} -"#, - )), - }) - .expect("start JavaScript execution"); - - let result = execution.wait().expect("wait for JavaScript execution"); - let stdout = String::from_utf8_lossy(&result.stdout); - let stderr = String::from_utf8_lossy(&result.stderr); - assert_eq!(result.exit_code, 0, "stdout:\n{stdout}\nstderr:\n{stderr}"); -} - fn javascript_execution_exposes_compatibility_shims_and_denies_escape_builtins() { let temp = tempdir().expect("create temp dir"); let mut engine = support::javascript_engine(); @@ -7010,13 +6968,16 @@ console.log(outcome); ); } -// Rust `panic=unwind` Wasm (for example Cloudflare's Kitesurf) uses the exnref -// form of exception handling. V8 reads flags once per process, so each case -// runs in a fresh copy of this test binary with its own environment. +// Rust `panic=unwind` Wasm uses finalized exception references. Run each +// configuration in a fresh process so its V8 platform starts independently. #[test] -fn javascript_execution_v8_wasm_exnref_is_opt_in() { +fn javascript_execution_v8_runs_finalized_wasm_exceptions() { let current_exe = std::env::current_exe().expect("current test binary path"); - for (case, knob) in [("enabled", Some("1")), ("default", None)] { + for (case, knob) in [ + ("enabled", Some("1")), + ("default", None), + ("disabled", Some("0")), + ] { let mut command = Command::new(¤t_exe); command .args(["--exact", "__wasm_exnref_case_runner", "--nocapture"]) @@ -7046,6 +7007,23 @@ fn __wasm_exnref_case_runner() { return; }; + if case == "disabled" { + use agentos_executor_v8_runtime::embedded_runtime::{ + spawn_embedded_runtime_ipc, EmbeddedV8Runtime, + }; + for error in [ + EmbeddedV8Runtime::new(None, support::runtime_context()).err(), + spawn_embedded_runtime_ipc(None, support::runtime_context()).err(), + ] { + let error = error.expect("unsupported exnref opt-out must prevent startup"); + assert_eq!(error.kind(), std::io::ErrorKind::InvalidInput); + assert!(error + .to_string() + .contains("ERR_AGENTOS_UNSUPPORTED_V8_OPTION")); + } + return; + } + // The inner `catch_ref` captures the exception as an exnref, `throw_ref` // rethrows it, and the outer `catch` returns its payload to JS. let wasm = wat::parse_str( @@ -7106,11 +7084,7 @@ console.log(outcome); assert!(stderr.is_empty(), "unexpected stderr: {stderr}"); let outcome = stdout.trim(); match case.as_str() { - "enabled" => assert_eq!(outcome, "42"), - "default" => assert!( - outcome.starts_with("CompileError"), - "exnref Wasm must not compile without the opt-in: {outcome}" - ), + "enabled" | "default" => assert_eq!(outcome, "42"), other => panic!("unknown wasm exnref case {other}"), } } @@ -8323,7 +8297,6 @@ fn javascript_v8_suite() { javascript_execution_imports_node_fs_promises_without_hanging(); javascript_execution_imports_node_perf_hooks_without_hanging(); javascript_execution_high_resolution_time_opt_in_enables_sub_ms_hrtime(); - javascript_execution_high_resolution_time_default_off_keeps_coarse_clock(); javascript_execution_exposes_compatibility_shims_and_denies_escape_builtins(); javascript_execution_denies_dns_and_subpaths_on_every_resolution_path(); javascript_execution_v8_util_format_with_options_matches_node(); diff --git a/crates/executor-conformance/tests/wasi_path_open_read_not_blocked.rs b/crates/executor-conformance/tests/wasi_path_open_read_not_blocked.rs index 985da07b17..e58afc63f9 100644 --- a/crates/executor-conformance/tests/wasi_path_open_read_not_blocked.rs +++ b/crates/executor-conformance/tests/wasi_path_open_read_not_blocked.rs @@ -126,8 +126,8 @@ fn writable_wasm_preopens_include_create_file_right() { let runner = read_source("assets/runners/wasm-runner.mjs"); assert!( - runner.contains("const WASI_RIGHT_PATH_CREATE_FILE = 1n << 12n;"), - "WASI PATH_CREATE_FILE must use preview1 bit 12" + runner.contains("const WASI_RIGHT_PATH_CREATE_FILE = 1n << 10n;"), + "WASI PATH_CREATE_FILE must use preview1 bit 10" ); let rights_start = runner .find("const READ_WRITE_PREOPEN_RIGHTS_BASE =") diff --git a/crates/executor-contract/src/backend/event.rs b/crates/executor-contract/src/backend/event.rs index f5b2cf9c7f..e8c022c630 100644 --- a/crates/executor-contract/src/backend/event.rs +++ b/crates/executor-contract/src/backend/event.rs @@ -27,6 +27,19 @@ impl BoundedHostServiceError { self.encoded_bytes } + pub fn heap_bytes(&self) -> usize { + self.error + .code + .capacity() + .saturating_add(self.error.message.capacity()) + .saturating_add( + self.error + .details + .as_ref() + .map_or(0, crate::json_heap_bytes), + ) + } + pub fn into_error(self) -> HostServiceError { self.error } @@ -139,3 +152,21 @@ mod tests { ); } } + +#[cfg(test)] +mod retained_error_tests { + use super::*; + + #[test] + fn warning_json_heap_counts_reserved_array_storage_even_when_wire_is_small() { + let mut details = Vec::with_capacity(16); + details.push(serde_json::Value::Null); + let slots = details.capacity() * std::mem::size_of::(); + let error = + HostServiceError::new("EIO", "read").with_details(serde_json::Value::Array(details)); + let limit = PayloadLimit::with_warning_hook("maxBridgeResponseBytes", 128, None).unwrap(); + let bounded = BoundedHostServiceError::try_new(error, &limit).unwrap(); + assert!(bounded.encoded_bytes() <= limit.maximum()); + assert!(bounded.heap_bytes() >= slots); + } +} diff --git a/crates/executor-contract/src/backend/payload.rs b/crates/executor-contract/src/backend/payload.rs index 2cd8e73169..dd4f9f4046 100644 --- a/crates/executor-contract/src/backend/payload.rs +++ b/crates/executor-contract/src/backend/payload.rs @@ -2,6 +2,7 @@ use super::HostServiceError; use serde::Serialize; use std::fmt; use std::io; +use std::mem::{size_of, size_of_val}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -120,9 +121,39 @@ impl PayloadLimit { Ok(()) } + /// Measure compact wire bytes, stopping at the configured limit without + /// allocating the encoding. This does not measure an owned value's heap. pub fn admit_json(&self, value: &T) -> Result { + self.measure_json(value, serde_json::ser::CompactFormatter, 0) + } + + /// Charge inline adapter data, out-of-band bytes and JSON collection slots + /// as well as their contents. Tiny retained-byte budgets may therefore + /// reject even a request whose compact wire representation fits. + pub(super) fn admit_retained_json( + &self, + value: &T, + additional_raw_bytes: usize, + ) -> Result { + let base = size_of_val(value).saturating_add(additional_raw_bytes); + self.measure_json(value, RetainedJsonFormatter, base) + } + + fn measure_json( + &self, + value: &T, + formatter: F, + base: usize, + ) -> Result { + if base > self.maximum() { + self.admit(base)?; + } let mut writer = LimitedCountingWriter::new(self.inner.maximum); - match serde_json::to_writer(&mut writer, value) { + writer.observed = base; + match value.serialize(&mut serde_json::Serializer::with_formatter( + &mut writer, + formatter, + )) { Ok(()) => { self.admit(writer.observed)?; Ok(writer.observed) @@ -162,6 +193,32 @@ impl PayloadLimit { } } +// This formatter is used exclusively with a counting writer. Padding charges +// collection slots without allocating them or creating another copy of the JSON. +struct RetainedJsonFormatter; + +impl serde_json::ser::Formatter for RetainedJsonFormatter { + fn begin_array_value( + &mut self, + writer: &mut W, + _first: bool, + ) -> io::Result<()> { + writer.write_all(&[0; size_of::() * 2 + 1]) + } + + fn begin_object_key( + &mut self, + writer: &mut W, + first: bool, + ) -> io::Result<()> { + const SLOT: usize = size_of::<(String, serde_json::Value, usize)>(); + if first { + writer.write_all(&[0; 11 * SLOT])?; + } + writer.write_all(&[0; 3 * SLOT + 1]) + } +} + struct LimitedCountingWriter { maximum: usize, observed: usize, @@ -239,6 +296,29 @@ mod tests { ); } + #[test] + fn retained_json_accounts_for_slots_and_warns_in_retained_bytes() { + let value = [0u8; 4]; + let warnings = Arc::new(RecordingWarnings::default()); + let measured = PayloadLimit::with_warning_hook("pendingEventBytes", 4096, None) + .unwrap() + .admit_retained_json(&value, 5) + .unwrap(); + let retained = + PayloadLimit::with_warning_hook("pendingEventBytes", measured, Some(warnings.clone())) + .unwrap(); + let wire = PayloadLimit::with_warning_hook("maxBridgeResponseBytes", 9, None).unwrap(); + assert_eq!(wire.admit_json(&value).unwrap(), 9); + assert!(measured >= value.len() * size_of::() + 5); + assert_eq!(retained.admit_retained_json(&value, 5).unwrap(), measured); + assert_eq!(warnings.0.lock().unwrap()[0].observed, measured); + assert_eq!( + retained.admit_retained_json(&value, 6).unwrap_err().code, + "E2BIG" + ); + assert_eq!(warnings.0.lock().unwrap().len(), 1); + } + #[test] fn near_limit_warning_is_coalesced_and_rearms_below_seventy_percent() { let warnings = Arc::new(RecordingWarnings::default()); diff --git a/crates/executor-contract/src/backend/submission.rs b/crates/executor-contract/src/backend/submission.rs index 632cb91273..51627bf6dc 100644 --- a/crates/executor-contract/src/backend/submission.rs +++ b/crates/executor-contract/src/backend/submission.rs @@ -127,11 +127,12 @@ impl ExecutionEventSubmitHandle { value: &T, additional_raw_bytes: usize, ) -> Result { - let encoded = self.queue.retained_bytes.limit.admit_json(value)?; - let retained_bytes = encoded.checked_add(additional_raw_bytes).ok_or_else(|| { - HostServiceError::new("EOVERFLOW", "common event retained-byte charge overflowed") - })?; - self.admit(retained_bytes) + let retained_bytes = self + .queue + .retained_bytes + .limit + .admit_retained_json(value, additional_raw_bytes)?; + Ok(ExecutionEventAdmission { retained_bytes }) } pub fn submit( @@ -313,7 +314,7 @@ pub fn bounded_execution_event_channel( } let queue = Arc::new(BoundedExecutionEventQueue { state: Mutex::new(ExecutionEventQueueState { - events: VecDeque::with_capacity(capacity), + events: VecDeque::new(), }), capacity, retained_bytes: Arc::new(RetainedEventByteLedger { diff --git a/crates/executor-contract/src/guest.rs b/crates/executor-contract/src/guest.rs index 222cf7f895..9fcdcf2162 100644 --- a/crates/executor-contract/src/guest.rs +++ b/crates/executor-contract/src/guest.rs @@ -1,6 +1,7 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use std::collections::HashMap; +use std::mem::size_of; /// Owned compatibility host-call envelope used by legacy synchronous guest /// adapters. Native adapters should prefer typed [`crate::host::HostOperation`] @@ -13,6 +14,63 @@ pub struct HostRpcRequest { pub raw_bytes_args: HashMap>, } +/// Heap charge for an owned JSON value whose inline slot is already counted. +/// Arrays and strings expose their allocated capacity. Object maps do not, so +/// reserve space for tree node slack and child links as well as keys and values. +/// Refuse values beyond the normal JSON decoder's depth without recursing further. +pub fn json_heap_bytes(value: &Value) -> usize { + fn visit(value: &Value, depth: usize) -> usize { + if depth > 128 { + return usize::MAX; + } + match value { + Value::String(text) => text.capacity(), + Value::Array(values) => values.iter().fold( + values.capacity().saturating_mul(size_of::()), + |bytes, child| bytes.saturating_add(visit(child, depth + 1)), + ), + Value::Object(entries) if !entries.is_empty() => { + // A B-tree node holds up to eleven entries. Allow for the + // sparsely populated root and partially filled child nodes. + let slots = 11usize.saturating_add(entries.len().saturating_mul(3)); + entries.iter().fold( + slots.saturating_mul(size_of::<(String, Value, usize)>()), + |bytes, (key, child)| { + bytes + .saturating_add(key.capacity()) + .saturating_add(visit(child, depth + 1)) + }, + ) + } + _ => 0, + } + } + visit(value, 0) +} + +impl HostRpcRequest { + /// Bytes retained outside of this envelope, including spare buffer slots. + pub fn heap_bytes(&self) -> usize { + let args = self.args.iter().fold( + self.args.capacity().saturating_mul(size_of::()), + |bytes, arg| bytes.saturating_add(json_heap_bytes(arg)), + ); + // Hash table growth leaves spare buckets and a control byte per bucket. + let raw_slots = self + .raw_bytes_args + .capacity() + .saturating_mul(2) + .saturating_mul(size_of::<(usize, Vec)>() + 1); + self.raw_bytes_args.values().fold( + self.method + .capacity() + .saturating_add(args) + .saturating_add(raw_slots), + |bytes, raw| bytes.saturating_add(raw.capacity()), + ) + } +} + /// Per-execution guest identity and operating-system projection supplied by /// the sidecar. Concrete executors translate these owned values into their /// guest-specific bootstrap representation. diff --git a/crates/executor-contract/src/host/process.rs b/crates/executor-contract/src/host/process.rs index f75be72a3a..3cd7c5db2c 100644 --- a/crates/executor-contract/src/host/process.rs +++ b/crates/executor-contract/src/host/process.rs @@ -114,6 +114,18 @@ pub struct ProcessSpawnHostNetworkDescriptor { pub metadata: Value, } +/// Tiers at which the prepared WebAssembly module's imports and signatures +/// passed linker validation. This is a compatibility constraint, never a +/// permission grant. The kernel selects the actual tier at exec commit. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +pub struct ExecImportTiers { + pub isolated: bool, + pub read_only: bool, + pub read_write: bool, + pub full: bool, +} + /// Runtime-neutral process launch options shared by V8, Wasmtime, and Python /// adapters. These fields describe Linux process semantics and sidecar /// runtime selection; they do not contain engine handles or guest memory. @@ -125,6 +137,12 @@ pub struct ProcessLaunchOptions { pub cloexec_fds: Vec, #[serde(rename = "localReplacement", default)] pub local_replacement: bool, + #[serde( + rename = "validatedImportTiers", + default, + skip_serializing_if = "Option::is_none" + )] + pub validated_import_tiers: Option, /// Kernel signal-thread identity whose mask survives exec; omitted means main. #[serde(rename = "execSignalThreadId", default)] pub exec_signal_thread_id: Option, diff --git a/crates/executor-contract/src/lib.rs b/crates/executor-contract/src/lib.rs index 4fa36b13c1..966471ee52 100644 --- a/crates/executor-contract/src/lib.rs +++ b/crates/executor-contract/src/lib.rs @@ -8,5 +8,5 @@ mod guest; pub mod host; mod signal; -pub use guest::{GuestBridgeLimits, GuestRuntimeConfig, HostRpcRequest}; +pub use guest::{json_heap_bytes, GuestBridgeLimits, GuestRuntimeConfig, HostRpcRequest}; pub use signal::{ExecutionSignalDispositionAction, ExecutionSignalHandlerRegistration}; diff --git a/crates/executor-v8-runtime/CLAUDE.md b/crates/executor-v8-runtime/CLAUDE.md index cf79d4ce11..a146db1906 100644 --- a/crates/executor-v8-runtime/CLAUDE.md +++ b/crates/executor-v8-runtime/CLAUDE.md @@ -8,7 +8,7 @@ - The bundled builtin registry in `assets/polyfill-registry.json` is the source of truth for guest Node builtins. Bridge implementations use the VM kernel for filesystem, process, and network operations. Pure JavaScript implementations have no host access. Denied modules fail with `ERR_ACCESS_DENIED`. - Core runner assets and the Pyodide distribution are immutable and shared between VMs. Each VM owns its writable Pyodide package cache. Keep a cache lease for every live execution or blocking materialization that uses the VM directory. - Module snapshots may evaluate static ES modules from read-only host mounts or packed packages. Keep module graph validation before snapshot selection and use the bounded blocking executor for discovery and compilation. -- `AGENTOS_V8_WASM_EXNREF=1` enables exnref support for the pinned V8 build. Composing the V8 WASM executor enables it by default for compatibility with the guest toolchain. `AGENTOS_V8_WASM_EXNREF=0` disables it for the process. +- V8 supports finalized Wasm exception references. Explicitly setting `AGENTOS_V8_WASM_EXNREF=0` is a startup configuration error. - Keep `BinaryFrame` at the compatibility boundary only. In-process embedded-runtime session plumbing should use `runtime_protocol::{RuntimeCommand, SessionMessage, RuntimeEvent}` so bridge responses, stream events, and execution results do not bounce through IPC framing inside the same process. - In `src/session.rs`, a session may reuse its isolate only while the effective bridge code is unchanged. Snapshot-restored contexts clone the snapshot's default context, so changing bridge code between `Execute` calls must rebuild the isolate or the session will keep restoring the old bridge snapshot. - In `src/session.rs`, top-level `SessionMessage` handling must never silently drop late `BridgeResponse`, `StreamEvent`, or `TerminateExecution` frames after an execution finishes. If there is no active event loop to consume them, emit a structured `RuntimeEvent::Log` warning with an explicit `ERR_LATE_*` code so host-side diagnostics can see the loss. Frames that race a process exit by design carry no loss and must not become guest stderr: `timer`, `net_socket`, and `stdin_end` wakes are dropped, and late `stdin` bytes (discarded like a dead reader's pipe on Linux) and `TerminateExecution` (a kill of a finished execution) are logged host-side with `tracing::debug!` and their `ERR_LATE_*` code. diff --git a/crates/executor-v8-runtime/Cargo.lock b/crates/executor-v8-runtime/Cargo.lock deleted file mode 100644 index 5f8417db16..0000000000 --- a/crates/executor-v8-runtime/Cargo.lock +++ /dev/null @@ -1,584 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - -[[package]] -name = "aho-corasick" -version = "1.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" -dependencies = [ - "memchr", -] - -[[package]] -name = "bindgen" -version = "0.70.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f49d8fed880d473ea71efb9bf597651e77201bdd4893efe54c9e5d65ae04ce6f" -dependencies = [ - "bitflags", - "cexpr", - "clang-sys", - "itertools", - "log", - "prettyplease", - "proc-macro2", - "quote", - "regex", - "rustc-hash", - "shlex", - "syn", -] - -[[package]] -name = "bitflags" -version = "2.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af" - -[[package]] -name = "cexpr" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" -dependencies = [ - "nom", -] - -[[package]] -name = "cfg-if" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" - -[[package]] -name = "ciborium" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" -dependencies = [ - "ciborium-io", - "ciborium-ll", - "serde", -] - -[[package]] -name = "ciborium-io" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" - -[[package]] -name = "ciborium-ll" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" -dependencies = [ - "ciborium-io", - "half", -] - -[[package]] -name = "clang-sys" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" -dependencies = [ - "glob", - "libc", - "libloading", -] - -[[package]] -name = "crc32fast" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "crossbeam-channel" -version = "0.5.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" - -[[package]] -name = "crunchy" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" - -[[package]] -name = "either" -version = "1.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" - -[[package]] -name = "errno" -version = "0.3.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" -dependencies = [ - "libc", - "windows-sys", -] - -[[package]] -name = "fslock" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04412b8935272e3a9bae6f48c7bfff74c2911f60525404edfdd28e49884c3bfb" -dependencies = [ - "libc", - "winapi", -] - -[[package]] -name = "glob" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" - -[[package]] -name = "gzip-header" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95cc527b92e6029a62960ad99aa8a6660faa4555fe5f731aab13aa6a921795a2" -dependencies = [ - "crc32fast", -] - -[[package]] -name = "half" -version = "2.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" -dependencies = [ - "cfg-if", - "crunchy", - "zerocopy", -] - -[[package]] -name = "home" -version = "0.5.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589533453244b0995c858700322199b2becb13b627df2851f64a2775d024abcf" -dependencies = [ - "windows-sys", -] - -[[package]] -name = "itertools" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" -dependencies = [ - "either", -] - -[[package]] -name = "libc" -version = "0.2.183" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d" - -[[package]] -name = "libloading" -version = "0.8.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" -dependencies = [ - "cfg-if", - "windows-link", -] - -[[package]] -name = "linux-raw-sys" -version = "0.4.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" - -[[package]] -name = "log" -version = "0.4.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" - -[[package]] -name = "memchr" -version = "2.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" - -[[package]] -name = "minimal-lexical" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" - -[[package]] -name = "miniz_oxide" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" -dependencies = [ - "adler", -] - -[[package]] -name = "nom" -version = "7.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" -dependencies = [ - "memchr", - "minimal-lexical", -] - -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - -[[package]] -name = "paste" -version = "1.0.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" - -[[package]] -name = "prettyplease" -version = "0.2.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" -dependencies = [ - "proc-macro2", - "syn", -] - -[[package]] -name = "proc-macro2" -version = "1.0.106" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "regex" -version = "1.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "regex-automata" -version = "0.4.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.8.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" - -[[package]] -name = "rustc-hash" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" - -[[package]] -name = "rustix" -version = "0.38.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys", -] - -[[package]] -name = "agentos-executor-v8-runtime" -version = "0.1.0" -dependencies = [ - "ciborium", - "crossbeam-channel", - "libc", - "signal-hook", - "v8", -] - -[[package]] -name = "serde" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" -dependencies = [ - "serde_core", - "serde_derive", -] - -[[package]] -name = "serde_core" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "signal-hook" -version = "0.3.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2" -dependencies = [ - "libc", - "signal-hook-registry", -] - -[[package]] -name = "signal-hook-registry" -version = "1.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" -dependencies = [ - "errno", - "libc", -] - -[[package]] -name = "syn" -version = "2.0.117" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "unicode-ident" -version = "1.0.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" - -[[package]] -name = "v8" -version = "130.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a511192602f7b435b0a241c1947aa743eb7717f20a9195f4b5e8ed1952e01db1" -dependencies = [ - "bindgen", - "bitflags", - "fslock", - "gzip-header", - "home", - "miniz_oxide", - "once_cell", - "paste", - "which", -] - -[[package]] -name = "which" -version = "6.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4ee928febd44d98f2f459a4a79bd4d928591333a494a10a868418ac1b39cf1f" -dependencies = [ - "either", - "home", - "rustix", - "winsafe", -] - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "windows-link" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" - -[[package]] -name = "windows-sys" -version = "0.59.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" - -[[package]] -name = "winsafe" -version = "0.0.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" - -[[package]] -name = "zerocopy" -version = "0.8.48" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eed437bf9d6692032087e337407a86f04cd8d6a16a37199ed57949d415bd68e9" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.8.48" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70e3cd084b1788766f53af483dd21f93881ff30d7320490ec3ef7526d203bad4" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] diff --git a/crates/executor-v8-runtime/Cargo.toml b/crates/executor-v8-runtime/Cargo.toml index 6171ab15cf..06c02804e2 100644 --- a/crates/executor-v8-runtime/Cargo.toml +++ b/crates/executor-v8-runtime/Cargo.toml @@ -18,15 +18,13 @@ exclude = [ [features] test-support = [] wasm-exnref = [] -obscura-embedding = ["dep:deno_core"] [dependencies] agentos-vm-host-interface = { workspace = true } agentos-executor-contract = { workspace = true } agentos-resource-accounting = { workspace = true } agentos-driver-tokio = { workspace = true } -v8 = "130" -deno_core = { version = "=0.336.0", optional = true } +v8 = { workspace = true } crossbeam-channel = "0.5" flume = { version = "0.11", features = ["async"] } getrandom = "0.2" diff --git a/crates/executor-v8-runtime/assets/runners/wasi-module.js b/crates/executor-v8-runtime/assets/runners/wasi-module.js index 4620654c88..b9875a752b 100644 --- a/crates/executor-v8-runtime/assets/runners/wasi-module.js +++ b/crates/executor-v8-runtime/assets/runners/wasi-module.js @@ -62,6 +62,17 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = const __agentOSWasiFdflagsNonblock = 4; const __agentOSWasiRightFdRead = 1n << 1n; const __agentOSWasiRightFdWrite = 1n << 6n; + const __agentOSWasiRightPathCreateDirectory = 1n << 9n; + const __agentOSWasiRightPathCreateFile = 1n << 10n; + const __agentOSWasiRightPathLinkSource = 1n << 11n; + const __agentOSWasiRightPathLinkTarget = 1n << 12n; + const __agentOSWasiRightPathOpen = 1n << 13n; + const __agentOSWasiRightPathFilestatSetSize = 1n << 19n; + const __agentOSWasiRightPathRenameSource = 1n << 16n; + const __agentOSWasiRightPathRenameTarget = 1n << 17n; + const __agentOSWasiRightPathSymlink = 1n << 24n; + const __agentOSWasiRightPathRemoveDirectory = 1n << 25n; + const __agentOSWasiRightPathUnlinkFile = 1n << 26n; const __agentOSWasiDefaultRightsBase = 0xffffffffffffffffn; const __agentOSWasiDefaultRightsInheriting = 0xffffffffffffffffn; const __agentOSWasiWhenceSet = 0; @@ -388,6 +399,10 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = ); } + _descriptorHasRight(fd, right) { + return (this._descriptorRightsBase(this._descriptorEntry(fd)) & right) !== 0n; + } + _descriptorRightsInheriting(entry) { return this._normalizeRights( entry?.rightsInheriting, @@ -817,6 +832,10 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = case "EACCES": case "EPERM": return __agentOSWasiErrnoAcces; + case "EBADF": + return __agentOSWasiErrnoBadf; + case "EAGAIN": + return __agentOSWasiErrnoAgain; case "ENOENT": return __agentOSWasiErrnoNoent; case "ENOTDIR": @@ -920,17 +939,8 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = } _sidecarManagedProcess() { - if ( - typeof globalThis.__agentOSWasmInternalEnv?.AGENTOS_SANDBOX_ROOT === - "string" && - globalThis.__agentOSWasmInternalEnv.AGENTOS_SANDBOX_ROOT.length > 0 - ) { - return true; - } - return ( - typeof process?.env?.AGENTOS_SANDBOX_ROOT === "string" && - process.env.AGENTOS_SANDBOX_ROOT.length > 0 - ); + // Installed by the trusted executor before this execution starts. + return globalThis.__agentOSManagedKernelHost === true; } _descriptorDirectoryFsPath(entry) { @@ -1220,6 +1230,9 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = if (!entry) { return { error: __agentOSWasiErrnoBadf }; } + if (options.requiredRight && (this._descriptorRightsBase(entry) & options.requiredRight) === 0n) { + return { error: __agentOSWasiErrnoNotcapable }; + } const target = this._measureWasiPhase("readString", () => this._readString(pathPtr, pathLen)); const base = this._measureWasiPhase("descriptorPathBase", () => this._descriptorPathBase(entry, target)); if (!base || typeof base.guestPath !== "string") { @@ -1414,8 +1427,11 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = _fdWrite(fd, iovs, iovsLen, nwrittenPtr) { try { - const bytes = this._measureWasiPhase("collectIovs", () => this._collectIovs(iovs, iovsLen)); const descriptor = Number(fd) >>> 0; + if (!this._descriptorHasRight(descriptor, __agentOSWasiRightFdWrite)) { + return __agentOSWasiErrnoNotcapable; + } + const bytes = this._measureWasiPhase("collectIovs", () => this._collectIovs(iovs, iovsLen)); const handle = this._measureWasiPhase("externalFdHandle", () => this._externalFdHandle(descriptor)); if (handle?.kind === "pipe-write" && handle.pipe) { if (bytes.length > 0 && !this._pipeHasReaders(handle.pipe)) { @@ -1433,9 +1449,7 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = return __agentOSWasiErrnoRofs; } if (descriptor === 1 || descriptor === 2) { - const sidecarManagedProcess = - typeof process?.env?.AGENTOS_SANDBOX_ROOT === "string" && - process.env.AGENTOS_SANDBOX_ROOT.length > 0; + const sidecarManagedProcess = this._sidecarManagedProcess(); const useKernelStdioSyncRpc = sidecarManagedProcess || __agentOSKernelStdioSyncRpcEnabled(); if (useKernelStdioSyncRpc) { @@ -1506,9 +1520,7 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = if (handle?.kind === "stdio" && typeof handle.targetFd === "number") { const targetFd = Number(handle.targetFd) >>> 0; if (targetFd === 1 || targetFd === 2) { - const sidecarManagedProcess = - typeof process?.env?.AGENTOS_SANDBOX_ROOT === "string" && - process.env.AGENTOS_SANDBOX_ROOT.length > 0; + const sidecarManagedProcess = this._sidecarManagedProcess(); const useKernelStdioSyncRpc = sidecarManagedProcess || __agentOSKernelStdioSyncRpcEnabled(); const written = useKernelStdioSyncRpc @@ -1527,9 +1539,7 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = return __agentOSWasiErrnoBadf; } if (entry.kind === "stdout") { - const sidecarManagedProcess = - typeof process?.env?.AGENTOS_SANDBOX_ROOT === "string" && - process.env.AGENTOS_SANDBOX_ROOT.length > 0; + const sidecarManagedProcess = this._sidecarManagedProcess(); const useKernelStdioSyncRpc = sidecarManagedProcess || __agentOSKernelStdioSyncRpcEnabled(); const written = useKernelStdioSyncRpc @@ -1542,9 +1552,7 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = return this._measureWasiPhase("writeResultPtr", () => this._writeUint32(nwrittenPtr, written)); } if (entry.kind === "stderr") { - const sidecarManagedProcess = - typeof process?.env?.AGENTOS_SANDBOX_ROOT === "string" && - process.env.AGENTOS_SANDBOX_ROOT.length > 0; + const sidecarManagedProcess = this._sidecarManagedProcess(); const useKernelStdioSyncRpc = sidecarManagedProcess || __agentOSKernelStdioSyncRpcEnabled(); const written = useKernelStdioSyncRpc @@ -1600,8 +1608,11 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = _fdPwrite(fd, iovs, iovsLen, offset, nwrittenPtr) { try { - const bytes = this._collectIovs(iovs, iovsLen); const descriptor = Number(fd) >>> 0; + if (!this._descriptorHasRight(descriptor, __agentOSWasiRightFdWrite)) { + return __agentOSWasiErrnoNotcapable; + } + const bytes = this._collectIovs(iovs, iovsLen); const explicitOffset = this._positionedIoOffset(offset); if (explicitOffset === null) { return __agentOSWasiErrnoInval; @@ -1646,6 +1657,9 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = _fdPread(fd, iovs, iovsLen, offset, nreadPtr) { try { const descriptor = Number(fd) >>> 0; + if (!this._descriptorHasRight(descriptor, __agentOSWasiRightFdRead)) { + return __agentOSWasiErrnoNotcapable; + } const explicitOffset = this._positionedIoOffset(offset); if (explicitOffset === null) { return __agentOSWasiErrnoInval; @@ -1688,6 +1702,9 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = _fdRead(fd, iovs, iovsLen, nreadPtr) { try { const descriptor = Number(fd) >>> 0; + if (!this._descriptorHasRight(descriptor, __agentOSWasiRightFdRead)) { + return __agentOSWasiErrnoNotcapable; + } const handle = this._externalFdHandle(descriptor); if (handle?.kind === "pipe-read" && handle.pipe) { const totalLength = this._boundedReadLength(iovs, iovsLen); @@ -1701,7 +1718,7 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = const written = this._writeToIovs(iovs, iovsLen, chunk); return this._writeUint32(nreadPtr, written); } - if (handle?.kind === "stdio" && Number(handle.targetFd) === 0) { + if (handle?.kind === "stdio" && Number(handle.targetFd) === 0 && !this._sidecarManagedProcess()) { const totalLength = this._boundedReadLength(iovs, iovsLen); if (typeof __agentOSWasiHost.readStdin === "function") { const value = __agentOSWasiHost.readStdin(totalLength); @@ -1726,70 +1743,63 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = return this._writeUint32(nreadPtr, written); } const entry = this.fdTable.get(descriptor); - if (!entry) { + const stdinHandle = handle?.kind === "stdio" && Number(handle.targetFd) === 0; + if (!entry && !stdinHandle) { return __agentOSWasiErrnoBadf; } - if (entry.kind === "stdin") { + if (entry?.kind === "stdin" || stdinHandle) { const totalLength = this._boundedReadLength(iovs, iovsLen); - const syncRpc = - typeof globalThis?.__agentOSSyncRpc?.callSync === "function" - ? __agentOSWasiSyncRpc() - : null; - const sidecarManagedProcess = - typeof process?.env?.AGENTOS_SANDBOX_ROOT === "string" && - process.env.AGENTOS_SANDBOX_ROOT.length > 0; - if (syncRpc && (sidecarManagedProcess || __agentOSKernelStdioSyncRpcEnabled())) { - try { - const nonblocking = - ((Number(entry.fdFlags) >>> 0) & __agentOSWasiFdflagsNonblock) !== 0; - const waitMs = nonblocking ? 0 : 10; - let chunk = null; - while (true) { - let response; - try { - response = syncRpc.callSync("__kernel_stdin_read", [ - totalLength, - waitMs, - ]); - } catch (error) { - if (error?.code === "EINTR") { - // Node/libuv retries interrupted stream reads. A caught - // guest signal must not turn an open kernel PTY into EOF. - continue; - } - throw error; - } - if ( - response && - typeof response === "object" && - typeof response.dataBase64 === "string" - ) { - chunk = Buffer.from(response.dataBase64, "base64"); - break; - } - if (response && typeof response === "object" && response.done === true) { - chunk = Buffer.alloc(0); - break; - } - if (nonblocking) { - return __agentOSWasiErrnoAgain; - } - if ( - typeof Atomics?.wait === "function" && - typeof syntheticWaitArray !== "undefined" - ) { - Atomics.wait(syntheticWaitArray, 0, 0, 10); + if (this._sidecarManagedProcess() || __agentOSKernelStdioSyncRpcEnabled()) { + const syncRpc = __agentOSWasiSyncRpc(); + if (typeof syncRpc?.callSync !== "function") { + return __agentOSWasiErrnoIo; + } + const nonblocking = + ((Number(entry?.fdFlags) >>> 0) & __agentOSWasiFdflagsNonblock) !== 0; + const waitMs = nonblocking ? 0 : 10; + let chunk = null; + while (true) { + let response; + try { + response = syncRpc.callSync("__kernel_stdin_read", [ + totalLength, + waitMs, + ]); + } catch (error) { + if (error?.code === "EINTR") { + // Node/libuv retries interrupted stream reads. A caught + // guest signal must not turn an open kernel PTY into EOF. + continue; } + throw error; } - if (!chunk || chunk.length === 0) { - return this._writeUint32(nreadPtr, 0); + if ( + response && + typeof response === "object" && + typeof response.dataBase64 === "string" + ) { + chunk = Buffer.from(response.dataBase64, "base64"); + break; } - const written = this._writeToIovs(iovs, iovsLen, chunk); - return this._writeUint32(nreadPtr, written); - } catch { - // Fall back to direct stdin reads when the sync bridge is unavailable - // in the standalone runner bootstrap. + if (response && typeof response === "object" && response.done === true) { + chunk = Buffer.alloc(0); + break; + } + if (nonblocking) { + return __agentOSWasiErrnoAgain; + } + if ( + typeof Atomics?.wait === "function" && + typeof syntheticWaitArray !== "undefined" + ) { + Atomics.wait(syntheticWaitArray, 0, 0, 10); + } + } + if (!chunk || chunk.length === 0) { + return this._writeUint32(nreadPtr, 0); } + const written = this._writeToIovs(iovs, iovsLen, chunk); + return this._writeUint32(nreadPtr, written); } // Host-seam stdin (a non-native backend whose stdin is delivered through // the runtime process object, not a kernel fd): read the queued bytes @@ -1857,13 +1867,6 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = if (entry.kind !== "file") { return __agentOSWasiErrnoBadf; } - // WASI rights: a descriptor opened without FD_READ cannot be read. - if ( - typeof entry.rightsBase === "bigint" && - (entry.rightsBase & __agentOSWasiRightFdRead) === 0n - ) { - return __agentOSWasiErrnoNotcapable; - } const totalLength = this._boundedReadLength(iovs, iovsLen); const buffer = Buffer.alloc(totalLength); const position = typeof entry.offset === "number" ? entry.offset : null; @@ -2247,6 +2250,7 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = try { const resolved = this._resolveDescriptorPath(fd, pathPtr, pathLen, { preferCreateParent: true, + requiredRight: __agentOSWasiRightPathCreateDirectory, }); if (resolved.error !== __agentOSWasiErrnoSuccess) { return resolved.error; @@ -2264,11 +2268,11 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = _pathLink(oldFd, _oldFlags, oldPathPtr, oldPathLen, newFd, newPathPtr, newPathLen) { try { - const source = this._resolveDescriptorPath(oldFd, oldPathPtr, oldPathLen); + const source = this._resolveDescriptorPath(oldFd, oldPathPtr, oldPathLen, { requiredRight: __agentOSWasiRightPathLinkSource }); if (source.error !== __agentOSWasiErrnoSuccess) { return source.error; } - const destination = this._resolveDescriptorPath(newFd, newPathPtr, newPathLen); + const destination = this._resolveDescriptorPath(newFd, newPathPtr, newPathLen, { requiredRight: __agentOSWasiRightPathLinkTarget }); if (destination.error !== __agentOSWasiErrnoSuccess) { return destination.error; } @@ -2315,6 +2319,10 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = const fsPath = this._resolvedFsPath(resolved); const openDirectory = (requestedFlags & __agentOSWasiOpenDirectory) !== 0; const allowedRightsBase = this._descriptorRightsBase(entry); + let requiredRights = __agentOSWasiRightPathOpen; + if (requestedFlags & __agentOSWasiOpenCreate) requiredRights |= __agentOSWasiRightPathCreateFile; + if (requestedFlags & __agentOSWasiOpenTruncate) requiredRights |= __agentOSWasiRightPathFilestatSetSize; + if ((allowedRightsBase & requiredRights) !== requiredRights) return __agentOSWasiErrnoNotcapable; const allowedRightsInheriting = this._descriptorRightsInheriting(entry); const requestedRightsBase = this._normalizeRights(rightsBase, allowedRightsInheriting); const requestedRightsInheriting = this._normalizeRights( @@ -2402,7 +2410,7 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = _pathSymlink(targetPtr, targetLen, fd, pathPtr, pathLen) { try { - const resolved = this._resolveDescriptorPath(fd, pathPtr, pathLen); + const resolved = this._resolveDescriptorPath(fd, pathPtr, pathLen, { requiredRight: __agentOSWasiRightPathSymlink }); if (resolved.error !== __agentOSWasiErrnoSuccess) { return resolved.error; } @@ -2420,7 +2428,7 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = _pathRemoveDirectory(fd, pathPtr, pathLen) { try { - const resolved = this._resolveDescriptorPath(fd, pathPtr, pathLen); + const resolved = this._resolveDescriptorPath(fd, pathPtr, pathLen, { requiredRight: __agentOSWasiRightPathRemoveDirectory }); if (resolved.error !== __agentOSWasiErrnoSuccess) { return resolved.error; } @@ -2437,11 +2445,11 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = _pathRename(oldFd, oldPathPtr, oldPathLen, newFd, newPathPtr, newPathLen) { try { - const source = this._resolveDescriptorPath(oldFd, oldPathPtr, oldPathLen); + const source = this._resolveDescriptorPath(oldFd, oldPathPtr, oldPathLen, { requiredRight: __agentOSWasiRightPathRenameSource }); if (source.error !== __agentOSWasiErrnoSuccess) { return source.error; } - const destination = this._resolveDescriptorPath(newFd, newPathPtr, newPathLen); + const destination = this._resolveDescriptorPath(newFd, newPathPtr, newPathLen, { requiredRight: __agentOSWasiRightPathRenameTarget }); if (destination.error !== __agentOSWasiErrnoSuccess) { return destination.error; } @@ -2458,7 +2466,7 @@ if (typeof globalThis !== "undefined" && typeof globalThis.__agentOSWasiModule = _pathUnlinkFile(fd, pathPtr, pathLen) { try { - const resolved = this._resolveDescriptorPath(fd, pathPtr, pathLen); + const resolved = this._resolveDescriptorPath(fd, pathPtr, pathLen, { requiredRight: __agentOSWasiRightPathUnlinkFile }); if (resolved.error !== __agentOSWasiErrnoSuccess) { return resolved.error; } diff --git a/crates/executor-v8-runtime/assets/runners/wasm-runner.mjs b/crates/executor-v8-runtime/assets/runners/wasm-runner.mjs index 5bae5922e8..43e8505b88 100644 --- a/crates/executor-v8-runtime/assets/runners/wasm-runner.mjs +++ b/crates/executor-v8-runtime/assets/runners/wasm-runner.mjs @@ -1233,9 +1233,9 @@ const WASI_RIGHT_FD_TELL = 1n << 5n; const WASI_RIGHT_FD_ADVISE = 1n << 7n; const WASI_RIGHT_FD_ALLOCATE = 1n << 8n; const WASI_RIGHT_PATH_CREATE_DIRECTORY = 1n << 9n; -const WASI_RIGHT_PATH_LINK_SOURCE = 1n << 10n; -const WASI_RIGHT_PATH_LINK_TARGET = 1n << 11n; -const WASI_RIGHT_PATH_CREATE_FILE = 1n << 12n; +const WASI_RIGHT_PATH_CREATE_FILE = 1n << 10n; +const WASI_RIGHT_PATH_LINK_SOURCE = 1n << 11n; +const WASI_RIGHT_PATH_LINK_TARGET = 1n << 12n; const WASI_RIGHT_PATH_OPEN = 1n << 13n; const WASI_RIGHT_FD_READDIR = 1n << 14n; const WASI_RIGHT_PATH_READLINK = 1n << 15n; diff --git a/crates/executor-v8-runtime/build.rs b/crates/executor-v8-runtime/build.rs index 8e9105d530..efb8957943 100644 --- a/crates/executor-v8-runtime/build.rs +++ b/crates/executor-v8-runtime/build.rs @@ -5,145 +5,95 @@ use std::path::{Path, PathBuf}; mod build_assets; mod build_bridge; -fn cargo_home() -> PathBuf { - if let Some(home) = env::var_os("CARGO_HOME") { - return PathBuf::from(home); - } - - let home = env::var_os("HOME").expect("HOME must be set when CARGO_HOME is unset"); - PathBuf::from(home).join(".cargo") +// This is the exact rusty_v8 binding plus the product's backported V8 tree. +// An upstream package with the same Rust version can have different V8 headers. +const RUSTY_V8_REV: &str = "548adfd54db42c670e0f3fd42211c3af109c0a73"; +const V8_TREE: &str = "91306fc87da850ec8b222f0030b37d2da90a6dea"; +const ICU_REV: &str = "2a51536dfb3b448bdf3dae70c782ea2879c90904"; +const HEADER_SHA256: &str = "3ab78300ff42414d7706891bf101ae9fa18ca1b5eaec27d38ff56a08bad1b6ac"; +const ICU_SHA256: &str = "e1ed8f674bb8449731b79cae956042da59f9ecbd37c8d3f86586a22b4836e5b2"; + +fn required_file(path: &Path) -> Vec { + fs::read(path).unwrap_or_else(|error| panic!("cannot read {}: {error}", path.display())) } -fn read_v8_version(lock_path: &Path) -> String { - let lock = fs::read_to_string(lock_path) - .unwrap_or_else(|error| panic!("failed to read {}: {}", lock_path.display(), error)); - - let mut in_v8_package = false; - for line in lock.lines() { - match line.trim() { - "[[package]]" => in_v8_package = false, - "name = \"v8\"" => in_v8_package = true, - _ if in_v8_package && line.trim_start().starts_with("version = \"") => { - let version = line - .trim() - .trim_start_matches("version = \"") - .trim_end_matches('"'); - return version.to_owned(); - } - _ => {} - } +fn v8_source_root() -> PathBuf { + println!("cargo:rerun-if-env-changed=AGENTOS_RUSTY_V8_SOURCE"); + println!("cargo:rerun-if-env-changed=RUSTY_V8_ARCHIVE"); + if let Some(root) = env::var_os("AGENTOS_RUSTY_V8_SOURCE") { + return root.into(); } - - panic!("failed to locate v8 version in {}", lock_path.display()); -} - -fn find_v8_crate_root(v8_version: &str) -> PathBuf { - let registry_src = cargo_home().join("registry").join("src"); - let entries = fs::read_dir(®istry_src).unwrap_or_else(|error| { - panic!( - "failed to read cargo registry src {}: {}", - registry_src.display(), - error - ) - }); - - for entry in entries { - let entry = entry - .unwrap_or_else(|error| panic!("failed to inspect cargo registry entry: {}", error)); - let crate_root = entry.path().join(format!("v8-{}", v8_version)); - if crate_root.is_dir() { - return crate_root; + if let Some(archive) = env::var_os("RUSTY_V8_ARCHIVE").map(PathBuf::from) { + if archive.is_file() { + return archive + .parent() + .expect("archive parent directory") + .to_owned(); } } - - panic!( - "failed to locate v8-{} under {}", - v8_version, - registry_src.display(), - ); + panic!("set AGENTOS_RUSTY_V8_SOURCE to the prepared agentOS V8 source or pass the local matched bundle archive via RUSTY_V8_ARCHIVE"); } -fn find_v8_icu_data(crate_root: &Path) -> PathBuf { - for relative in [ - Path::new("third_party/icu/common/icudtl.dat"), - Path::new("third_party/icu/flutter_desktop/icudtl.dat"), - Path::new("third_party/icu/chromecast_video/icudtl.dat"), - ] { - let candidate = crate_root.join(relative); - if candidate.exists() { - return candidate; +fn header_paths(dir: &Path, files: &mut Vec) { + for entry in + fs::read_dir(dir).unwrap_or_else(|error| panic!("cannot read {}: {error}", dir.display())) + { + let path = entry.expect("V8 header directory entry").path(); + if path.is_dir() { + header_paths(&path, files); + } else if path.extension().is_some_and(|extension| extension == "h") { + files.push(path); } } - - panic!("failed to locate ICU data under {}", crate_root.display(),); } -fn prepare_v8_include_overlay(crate_root: &Path, out_dir: &Path) -> PathBuf { - const OLD_ITERATOR_ALIAS: &str = "using iterator_concept = Iterator::iterator_concept;"; - const FIXED_ITERATOR_ALIAS: &str = - "using iterator_concept = typename Iterator::iterator_concept;"; - - let source_include = crate_root.join("v8/include"); - let overlay_include = out_dir.join("v8-include"); - fs::create_dir_all(&overlay_include).unwrap_or_else(|error| { - panic!( - "failed to create V8 include overlay {}: {}", - overlay_include.display(), - error, - ) - }); - - let sandbox_header = source_include.join("v8-sandbox.h"); - fs::copy(&sandbox_header, overlay_include.join("v8-sandbox.h")).unwrap_or_else(|error| { - panic!( - "failed to copy V8 sandbox header {}: {}", - sandbox_header.display(), - error, - ) - }); - - let internal_header = source_include.join("v8-internal.h"); - let internal = fs::read_to_string(&internal_header).unwrap_or_else(|error| { - panic!( - "failed to read V8 internal header {}: {}", - internal_header.display(), - error, - ) - }); - let patched = if internal.contains(FIXED_ITERATOR_ALIAS) { - internal - } else if internal.matches(OLD_ITERATOR_ALIAS).count() == 1 { - internal.replacen(OLD_ITERATOR_ALIAS, FIXED_ITERATOR_ALIAS, 1) - } else { - panic!( - "V8 iterator alias in {} did not match the expected declaration", - internal_header.display(), - ); - }; - fs::write(overlay_include.join("v8-internal.h"), patched).unwrap_or_else(|error| { - panic!( - "failed to write patched V8 internal header overlay: {}", - error, - ) +fn verify_v8_headers(include: &Path) { + use sha2::{Digest, Sha256}; + let mut files = Vec::new(); + header_paths(include, &mut files); + files.sort_by_cached_key(|path| { + path.strip_prefix(include) + .unwrap() + .to_string_lossy() + .replace('\\', "/") }); - - println!("cargo:rerun-if-changed={}", sandbox_header.display()); - println!("cargo:rerun-if-changed={}", internal_header.display()); - overlay_include + let mut digest = Sha256::new(); + for file in files { + let relative = file + .strip_prefix(include) + .unwrap() + .to_string_lossy() + .replace('\\', "/"); + digest.update(relative.as_bytes()); + digest.update([0]); + digest.update(Sha256::digest(required_file(&file))); + } + assert_eq!( + format!("{:x}", digest.finalize()), + HEADER_SHA256, + "public V8 headers in {} do not match engine tree {V8_TREE}", + include.display() + ); } -fn build_v8_thread_support(manifest_dir: &Path, crate_root: &Path, out_dir: &Path) { +fn build_v8_thread_support(manifest_dir: &Path, include: &Path, gn_include: &Path) { let source = manifest_dir.join("src/v8_thread_support.cc"); - let include = crate_root.join("v8/include"); - let overlay_include = prepare_v8_include_overlay(crate_root, out_dir); + let serializer = manifest_dir.join("src/v8_bounded_serializer.cc"); + let allocator = manifest_dir.join("src/v8_array_buffer_allocator.cc"); println!("cargo:rerun-if-changed={}", source.display()); + println!("cargo:rerun-if-changed={}", serializer.display()); + println!("cargo:rerun-if-changed={}", allocator.display()); cc::Build::new() .cpp(true) - .std("c++20") + .std("c++23") + .flag_if_supported("-fno-rtti") .warnings(false) - .include(overlay_include) + .define("V8_GN_HEADER", None) + .include(gn_include) .include(include) .file(source) + .file(serializer) + .file(allocator) .compile("agentos_v8_thread_support"); } @@ -151,7 +101,7 @@ fn build_v8_thread_support(manifest_dir: &Path, crate_root: &Path, out_dir: &Pat /// V8 version, the target, and the sources that define external references, /// bridge stub functions, and snapshot creation. Persisted blobs are only /// loaded by a build with the same fingerprint. -fn emit_snapshot_fingerprint(manifest_dir: &Path, v8_version: &str) { +fn emit_snapshot_fingerprint(manifest_dir: &Path, gn_header: &[u8]) { use sha2::{Digest, Sha256}; fn collect(path: &Path, files: &mut Vec) { if path.is_dir() { @@ -177,7 +127,10 @@ fn emit_snapshot_fingerprint(manifest_dir: &Path, v8_version: &str) { collect(&root, &mut files); } let mut hasher = Sha256::new(); - hasher.update(v8_version.as_bytes()); + for pin in [RUSTY_V8_REV, V8_TREE, ICU_REV, HEADER_SHA256, ICU_SHA256] { + hasher.update(pin.as_bytes()); + } + hasher.update(gn_header); hasher.update(env::var("TARGET").unwrap_or_default().as_bytes()); for file in files { let relative = file.strip_prefix(manifest_dir).unwrap_or(&file); @@ -198,28 +151,35 @@ fn emit_snapshot_fingerprint(manifest_dir: &Path, v8_version: &str) { fn main() { let manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set")); - let lock_path = manifest_dir.join("Cargo.lock"); let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set")); - println!("cargo:rerun-if-changed={}", lock_path.display()); println!("cargo:rerun-if-changed=build.rs"); build_bridge::build_v8_bridge(&manifest_dir, &out_dir); build_assets::stage_executor_assets(); - let v8_version = read_v8_version(&lock_path); - emit_snapshot_fingerprint(&manifest_dir, &v8_version); - let v8_crate_root = find_v8_crate_root(&v8_version); - build_v8_thread_support(&manifest_dir, &v8_crate_root, &out_dir); - let icu_data = find_v8_icu_data(&v8_crate_root); - let dest_path = out_dir.join("icudtl.dat"); - - fs::copy(&icu_data, &dest_path).unwrap_or_else(|error| { - panic!( - "failed to copy ICU data from {} to {}: {}", - icu_data.display(), - dest_path.display(), - error, - ) - }); + let root = v8_source_root(); + let include = root.join("v8/include"); + println!("cargo:rerun-if-changed={}", include.display()); + verify_v8_headers(&include); + println!("cargo:rerun-if-env-changed=AGENTOS_RUSTY_V8_GN_INCLUDE"); + let gn_include = env::var_os("AGENTOS_RUSTY_V8_GN_INCLUDE") + .map(PathBuf::from) + .unwrap_or_else(|| root.join("generated")); + let gn_header = gn_include.join("v8-gn.h"); + println!("cargo:rerun-if-changed={}", gn_header.display()); + emit_snapshot_fingerprint(&manifest_dir, &required_file(&gn_header)); + build_v8_thread_support(&manifest_dir, &include, &gn_include); + + let icu_data = root.join("third_party/icu/common/icudtl.dat"); + println!("cargo:rerun-if-changed={}", icu_data.display()); + let icu_bytes = required_file(&icu_data); + use sha2::{Digest, Sha256}; + assert_eq!( + format!("{:x}", Sha256::digest(&icu_bytes)), + ICU_SHA256, + "ICU common data in {} does not match ICU revision {ICU_REV}", + icu_data.display() + ); + fs::write(out_dir.join("icudtl.dat"), icu_bytes).expect("stage pinned ICU common data"); } diff --git a/crates/executor-v8-runtime/src/adapter_host.rs b/crates/executor-v8-runtime/src/adapter_host.rs index 2026f2717e..7515511595 100644 --- a/crates/executor-v8-runtime/src/adapter_host.rs +++ b/crates/executor-v8-runtime/src/adapter_host.rs @@ -100,7 +100,7 @@ impl V8RuntimeHost { heap_limit_mb: non_zero_option(heap_limit_mb), cpu_time_limit_ms: non_zero_option(cpu_time_limit_ms), wall_clock_limit_ms: non_zero_option(wall_clock_limit_ms), - limit_external_memory: false, + limit_external_memory: true, warm_hint, }) } diff --git a/crates/executor-v8-runtime/src/adapter_runtime.rs b/crates/executor-v8-runtime/src/adapter_runtime.rs index e7be3560ed..1f847c7975 100644 --- a/crates/executor-v8-runtime/src/adapter_runtime.rs +++ b/crates/executor-v8-runtime/src/adapter_runtime.rs @@ -241,9 +241,12 @@ pub(crate) fn cbor_payload_raw_byte_arg_for_decoded( cbor_payload_raw_byte_arg(payload, index) } +// Ciborium's decoder uses a default recursion limit of 256. +const MAX_BRIDGE_CBOR_NESTING: usize = 256; + /// Serialize a JSON value to CBOR bytes for bridge responses. pub fn json_to_cbor_payload(value: &Value) -> io::Result> { - let cbor_value = json_to_cbor(value); + let cbor_value = json_to_cbor(value, 0)?; let mut buf = Vec::new(); ciborium::ser::into_writer(&cbor_value, &mut buf).map_err(|e| { io::Error::new( @@ -315,9 +318,15 @@ fn cbor_to_json(value: ciborium::value::Value) -> Value { } } -fn json_to_cbor(value: &Value) -> ciborium::value::Value { +fn json_to_cbor(value: &Value, depth: usize) -> io::Result { use ciborium::value::Value as Cbor; - match value { + if depth >= MAX_BRIDGE_CBOR_NESTING && (value.is_array() || value.is_object()) { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + format!("CBOR bridge response exceeds the nesting limit of {MAX_BRIDGE_CBOR_NESTING}"), + )); + } + Ok(match value { Value::Null => Cbor::Null, Value::Bool(b) => Cbor::Bool(*b), Value::Number(n) => { @@ -332,23 +341,27 @@ fn json_to_cbor(value: &Value) -> ciborium::value::Value { } } Value::String(s) => Cbor::Text(s.clone()), - Value::Array(arr) => Cbor::Array(arr.iter().map(json_to_cbor).collect()), + Value::Array(arr) => Cbor::Array( + arr.iter() + .map(|value| json_to_cbor(value, depth + 1)) + .collect::>()?, + ), Value::Object(map) => { // Check for Buffer type marker if map.get("__type").and_then(Value::as_str) == Some("Buffer") { if let Some(data) = map.get("data").and_then(Value::as_str) { if let Ok(bytes) = base64_decode(data) { - return Cbor::Bytes(bytes); + return Ok(Cbor::Bytes(bytes)); } } } Cbor::Map( map.iter() - .map(|(k, v)| (Cbor::Text(k.clone()), json_to_cbor(v))) - .collect(), + .map(|(k, v)| Ok((Cbor::Text(k.clone()), json_to_cbor(v, depth + 1)?))) + .collect::>()?, ) } - } + }) } /// Public base64 encode for use in bridge call handlers. @@ -423,6 +436,22 @@ mod tests { use super::{json_to_cbor_payload, map_bridge_method}; use serde_json::json; + #[test] + fn cbor_encoder_rejects_excessive_nesting() { + use serde_json::Value; + + let mut nested = Value::Null; + for _ in 0..super::MAX_BRIDGE_CBOR_NESTING { + nested = Value::Array(vec![nested]); + } + json_to_cbor_payload(&nested).expect("encode at the nesting limit"); + + let excessive = Value::Object([("nested".to_owned(), nested)].into_iter().collect()); + let error = json_to_cbor_payload(&excessive).expect_err("reject excessive nesting"); + assert_eq!(error.kind(), std::io::ErrorKind::InvalidData); + assert!(error.to_string().contains("nesting limit")); + } + #[test] fn cbor_byte_string_boundary_includes_five_byte_header() { let payload_limit = 256 * 1024; diff --git a/crates/executor-v8-runtime/src/async_context.rs b/crates/executor-v8-runtime/src/async_context.rs index 06cfc7468d..e63ec888fc 100644 --- a/crates/executor-v8-runtime/src/async_context.rs +++ b/crates/executor-v8-runtime/src/async_context.rs @@ -17,7 +17,7 @@ const SET_GLOBAL: &str = "__agentOsSetAsyncContext"; /// globals, so guest code never sees them. They are V8 builtins from the extras /// binding: TurboFan inlines them, and a snapshot can hold them with no external /// reference. -pub fn install_async_context_globals(scope: &mut v8::HandleScope) { +pub fn install_async_context_globals(scope: &mut v8::PinScope<'_, '_>) { let context = scope.get_current_context(); let global = context.global(scope); let binding = context.get_extras_binding_object(scope); @@ -35,7 +35,7 @@ pub fn install_async_context_globals(scope: &mut v8::HandleScope) { } /// Starts a host execution or event with no async context. -pub fn clear_async_context(scope: &mut v8::HandleScope) { +pub fn clear_async_context(scope: &mut v8::PinScope<'_, '_>) { let undefined = v8::undefined(scope); scope.set_continuation_preserved_embedder_data(undefined.into()); } diff --git a/crates/executor-v8-runtime/src/bridge.rs b/crates/executor-v8-runtime/src/bridge.rs index 30b9688ad0..f257000958 100644 --- a/crates/executor-v8-runtime/src/bridge.rs +++ b/crates/executor-v8-runtime/src/bridge.rs @@ -6,7 +6,6 @@ use std::ffi::c_void; use std::mem::MaybeUninit; use std::rc::Rc; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; -use std::sync::OnceLock; use agentos_vm_host_interface::bridge_contract; use serde::de; @@ -65,14 +64,13 @@ fn configured_cbor_codec_enabled() -> bool { /// External references for V8 snapshot serialization. /// Maps function pointer indices in the snapshot to current addresses. /// Must be identical at snapshot creation and restore time. -pub fn external_refs() -> &'static v8::ExternalReferences { - static REFS: OnceLock = OnceLock::new(); - REFS.get_or_init(|| v8::ExternalReferences::new(&base_external_references())) +pub fn external_refs() -> &'static [v8::ExternalReference] { + crate::shared_source::base_references() } /// Native callbacks a snapshot heap can reference. Snapshots with shared /// sources append their source constants after these. -pub(crate) fn base_external_references() -> Vec> { +pub(crate) fn base_external_references() -> Vec { vec![ v8::ExternalReference { function: sync_bridge_callback.map_fn_to(), @@ -99,7 +97,7 @@ struct DefaultSerializerDelegate; impl v8::ValueSerializerImpl for DefaultSerializerDelegate { fn throw_data_clone_error<'s>( &self, - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, message: v8::Local<'s, v8::String>, ) { let exc = v8::Exception::error(scope, message); @@ -117,7 +115,7 @@ impl v8::ValueDeserializerImpl for DefaultDeserializerDelegate {} /// Uint8Array, Date, Map, Set, RegExp, Error, and circular references. /// When CBOR codec is active, uses ciborium instead. pub fn serialize_v8_value( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, value: v8::Local, ) -> Result, String> { if is_cbor_codec() { @@ -129,7 +127,7 @@ pub fn serialize_v8_value( /// Serialize a V8 value to bytes using V8's native wire format regardless of /// the process-wide codec toggle. pub fn serialize_v8_wire_value( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, value: v8::Local, ) -> Result, String> { let context = scope.get_current_context(); @@ -144,7 +142,7 @@ pub fn serialize_v8_wire_value( /// Deserialize bytes back to a V8 value using V8's built-in ValueDeserializer. /// The bytes must have been produced by serialize_v8_value() or node:v8.serialize(). pub fn deserialize_v8_value<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, data: &[u8], ) -> Result, String> { if is_cbor_codec() { @@ -156,7 +154,7 @@ pub fn deserialize_v8_value<'s>( /// Deserialize bytes from V8's native wire format regardless of the /// process-wide codec toggle. pub fn deserialize_v8_wire_value<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, data: &[u8], ) -> Result, String> { let context = scope.get_current_context(); @@ -174,7 +172,7 @@ pub fn deserialize_v8_wire_value<'s>( /// Convert a V8 value to a ciborium::Value for CBOR serialization. fn v8_to_cbor( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, value: v8::Local, ) -> Result { let mut object_stack = Vec::new(); @@ -182,7 +180,7 @@ fn v8_to_cbor( } fn v8_to_cbor_inner( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, value: v8::Local, depth: usize, object_stack: &mut Vec>, @@ -272,7 +270,7 @@ fn v8_to_cbor_inner( } fn enter_cbor_object( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, object_stack: &mut Vec>, object: v8::Local, ) -> Result<(), String> { @@ -518,14 +516,14 @@ fn limited_cbor_item_count(kind: &str, item_count: usize) -> Resul /// Convert a ciborium::Value to a V8 value. fn cbor_to_v8<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, value: &ciborium::Value, ) -> Result, String> { cbor_to_v8_inner(scope, value, 0) } fn cbor_to_v8_inner<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, value: &ciborium::Value, depth: usize, ) -> Result, String> { @@ -577,7 +575,7 @@ fn cbor_to_v8_inner<'s>( } fn raw_bytes_to_uint8array<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, bytes: &[u8], ) -> Option> { // Host data uses a Rust-owned backing store. `v8::ArrayBuffer::new` draws @@ -591,7 +589,7 @@ fn raw_bytes_to_uint8array<'s>( } fn bridge_response_payload_to_v8<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, status: u8, payload: &[u8], ) -> Option> { @@ -600,7 +598,7 @@ fn bridge_response_payload_to_v8<'s>( } let v8_val = { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); deserialize_v8_value(tc, payload).ok() }; if v8_val.is_some() { @@ -654,7 +652,7 @@ fn decode_bridge_error(payload: &[u8]) -> Option { } fn bridge_error_exception<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, payload: &[u8], ) -> v8::Local<'s, v8::Value> { let decoded = decode_bridge_error(payload); @@ -697,14 +695,14 @@ fn bridge_error_exception<'s>( } fn runtime_error_exception<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, message: &str, ) -> v8::Local<'s, v8::Value> { runtime_error_exception_with_code(scope, message, "ERR_AGENTOS_BRIDGE_RUNTIME") } fn runtime_error_exception_with_code<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, message: &str, code: &str, ) -> v8::Local<'s, v8::Value> { @@ -725,7 +723,7 @@ fn runtime_error_exception_with_code<'s>( /// Serialize a V8 value to CBOR bytes. pub fn serialize_cbor_value( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, value: v8::Local, ) -> Result, String> { let cbor_val = v8_to_cbor(scope, value)?; @@ -736,7 +734,7 @@ pub fn serialize_cbor_value( /// Deserialize CBOR bytes to a V8 value. pub fn deserialize_cbor_value<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, data: &[u8], ) -> Result, String> { let LimitedCborValue(cbor_val) = @@ -769,7 +767,7 @@ impl Drop for ActiveBridgeExecutionGuard { } pub(crate) fn activate_bridge_execution( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, context: Rc, pending: Rc, ) -> ActiveBridgeExecutionGuard { @@ -793,11 +791,11 @@ pub(crate) fn activate_bridge_execution( /// functions and apply aliases therefore remain valid for the context lifetime, /// while each invocation obtains the current operation's response/abort channels. pub(crate) fn install_session_bridge_fns( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, sync_methods: &[&str], async_methods: &[&str], ) -> Result<(), String> { - let scope = &mut v8::TryCatch::new(scope); + v8::tc_scope!(scope, scope); reset_vm_context_registry(scope); crate::next_ticks::capture(scope)?; let context = scope.get_current_context(); @@ -868,7 +866,7 @@ impl BridgeCallbackData { } fn bridge_callback_data( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, value: v8::Local, is_async: bool, ) -> Result { @@ -1216,7 +1214,7 @@ fn current_thread_resource_usage() -> Result( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, object: v8::Local<'s, v8::Object>, key: &str, value: &str, @@ -1227,7 +1225,7 @@ fn set_object_string_property<'s>( } fn set_object_number_property<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, object: v8::Local<'s, v8::Object>, key: &str, value: f64, @@ -1238,7 +1236,7 @@ fn set_object_number_property<'s>( } fn number_property_or_zero<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, object: v8::Local<'s, v8::Object>, key: &str, ) -> u64 { @@ -1250,9 +1248,8 @@ fn number_property_or_zero<'s>( .unwrap_or_default() } -fn process_memory_usage_value<'s>(scope: &mut v8::HandleScope<'s>) -> v8::Local<'s, v8::Value> { - let mut stats = v8::HeapStatistics::default(); - scope.get_heap_statistics(&mut stats); +fn process_memory_usage_value<'s>(scope: &mut v8::PinScope<'s, '_>) -> v8::Local<'s, v8::Value> { + let stats = scope.get_heap_statistics(); let object = v8::Object::new(scope); set_object_number_property(scope, object, "rss", stats.total_physical_size() as f64); @@ -1270,10 +1267,9 @@ fn process_memory_usage_value<'s>(scope: &mut v8::HandleScope<'s>) -> v8::Local< // This reads only the currently executing isolate; it is not process RSS. fn v8_heap_statistics_value<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, ) -> Result, String> { - let mut statistics = v8::HeapStatistics::default(); - scope.get_heap_statistics(&mut statistics); + let statistics = scope.get_heap_statistics(); let object = v8::Object::new(scope); for (name, value) in [ ("total_heap_size", statistics.total_heap_size()), @@ -1287,7 +1283,10 @@ fn v8_heap_statistics_value<'s>( ("heap_size_limit", statistics.heap_size_limit()), ("malloced_memory", statistics.malloced_memory()), ("peak_malloced_memory", statistics.peak_malloced_memory()), - ("does_zap_garbage", statistics.does_zap_garbage()), + ( + "does_zap_garbage", + usize::from(statistics.does_zap_garbage()), + ), ( "number_of_native_contexts", statistics.number_of_native_contexts(), @@ -1321,7 +1320,7 @@ fn v8_heap_statistics_value<'s>( } fn process_cpu_usage_value<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, args: &v8::FunctionCallbackArguments, ) -> Result, String> { let usage = current_thread_resource_usage()?; @@ -1353,7 +1352,7 @@ fn process_cpu_usage_value<'s>( } fn process_resource_usage_value<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, ) -> Result, String> { let usage = current_thread_resource_usage()?; let object = v8::Object::new(scope); @@ -1411,7 +1410,7 @@ fn process_resource_usage_value<'s>( Ok(object.into()) } -fn process_versions_value<'s>(scope: &mut v8::HandleScope<'s>) -> v8::Local<'s, v8::Value> { +fn process_versions_value<'s>(scope: &mut v8::PinScope<'s, '_>) -> v8::Local<'s, v8::Value> { let object = v8::Object::new(scope); set_object_string_property(scope, object, "v8", v8::V8::get_version()); set_object_string_property(scope, object, "openssl", EMULATED_OPENSSL_VERSION); @@ -1474,7 +1473,7 @@ fn vm_context_capacity_error(current_contexts: usize) -> Option { } fn reserve_vm_context_slot<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, context: v8::Local<'s, v8::Context>, ) -> Result { let registry = vm_context_registry(scope); @@ -1554,7 +1553,7 @@ pub(crate) fn clear_vm_context_registry_for_test(isolate: &mut v8::Isolate) { #[cfg(test)] pub(crate) fn create_registered_child_for_test<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, ) -> v8::Local<'s, v8::Context> { let context = crate::owned_microtasks::create_child(scope, Default::default()) .expect("owned child admission"); @@ -1564,7 +1563,7 @@ pub(crate) fn create_registered_child_for_test<'s>( #[cfg(test)] fn fill_vm_context_registry_for_test<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, context: v8::Local<'s, v8::Context>, count: usize, ) { @@ -1615,7 +1614,7 @@ macro_rules! vm_property_step { ($tc:ident, $operation:expr) => {{ let result = $operation; if $tc.has_caught() || $tc.has_terminated() { - return v8::Intercepted::No; + return v8::Intercepted::kNo; } result }}; @@ -1623,7 +1622,7 @@ macro_rules! vm_property_step { macro_rules! vm_property_callback { ($scope:expr, $args:expr, |$tc:ident, $sandbox:ident, $global:ident| $body:block) => {{ - let $tc = &mut v8::TryCatch::new($scope); + v8::tc_scope!($tc, $scope); let result = (|| { // Only these native callbacks can reach this ordinary dense array. // [sandbox, ready] is traced by V8; no Rust address or Global is kept. @@ -1631,7 +1630,7 @@ macro_rules! vm_property_callback { .expect("native contextification binding"); let ready = vm_property_step!($tc, binding.get_index($tc, 1)); if !ready.is_some_and(|value| value.is_true()) { - return v8::Intercepted::No; + return v8::Intercepted::kNo; } let sandbox_value = vm_property_step!($tc, binding.get_index($tc, 0)) .expect("native contextification sandbox field"); @@ -1642,15 +1641,16 @@ macro_rules! vm_property_callback { let $global = context.global($tc); // Receiver accesses may originate in the parent. Route operations // in the child's context, as Node contextify does. - let context_scope = &mut v8::ContextScope::new($tc, context); - let $tc = &mut v8::TryCatch::new(context_scope); + let handles: &mut v8::PinScope = $tc; + let context_scope = &mut v8::ContextScope::new(handles, context); + v8::tc_scope!($tc, context_scope); let result = (|| $body)(); if $tc.has_caught() || $tc.has_terminated() { assert!( $tc.rethrow().is_some(), "property exception can be rethrown" ); - v8::Intercepted::Yes + v8::Intercepted::kYes } else { result } @@ -1660,7 +1660,7 @@ macro_rules! vm_property_callback { $tc.rethrow().is_some(), "property exception can be rethrown" ); - v8::Intercepted::Yes + v8::Intercepted::kYes } else { result } @@ -1668,7 +1668,7 @@ macro_rules! vm_property_callback { } fn vm_named_get<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, name: v8::Local<'s, v8::Name>, args: v8::PropertyCallbackArguments<'s>, mut rv: v8::ReturnValue, @@ -1685,15 +1685,15 @@ fn vm_named_get<'s>( } else { value }); - v8::Intercepted::Yes + v8::Intercepted::kYes } else { - v8::Intercepted::No + v8::Intercepted::kNo } }) } fn vm_named_query<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, name: v8::Local<'s, v8::Name>, args: v8::PropertyCallbackArguments<'s>, mut rv: v8::ReturnValue, @@ -1706,21 +1706,21 @@ fn vm_named_query<'s>( vm_property_step!(tc, object.get_real_named_property_attributes(tc, name)) { rv.set_uint32(attributes.as_u32()); - return v8::Intercepted::Yes; + return v8::Intercepted::kYes; } - return v8::Intercepted::No; + return v8::Intercepted::kNo; } } - v8::Intercepted::No + v8::Intercepted::kNo }) } fn vm_named_set<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, name: v8::Local<'s, v8::Name>, value: v8::Local<'s, v8::Value>, args: v8::PropertyCallbackArguments<'s>, - mut rv: v8::ReturnValue<()>, + mut rv: v8::ReturnValue, ) -> v8::Intercepted { vm_property_callback!(scope, args, |tc, sandbox, global| { let global_attributes = @@ -1734,19 +1734,16 @@ fn vm_named_set<'s>( .as_ref() .is_some_and(v8::PropertyAttribute::is_read_only) { - return v8::Intercepted::No; + return v8::Intercepted::kNo; } let declared = global_attributes.is_some() || sandbox_attributes.is_some(); - let contextual = !global.strict_equals(args.this().into()); - if !declared && args.should_throw_on_error() && contextual && !value.is_function() { - return v8::Intercepted::No; - } + // V8 checks strict contextual stores for an existing binding before invoking the setter. if !declared && name.is_symbol() { - return v8::Intercepted::No; + return v8::Intercepted::kNo; } if vm_property_step!(tc, sandbox.set(tc, name.into(), value)) != Some(true) { // V8's normal store supplies the readonly/strict-mode outcome. - return v8::Intercepted::No; + return v8::Intercepted::kNo; } if sandbox_attributes.is_some() { if let Some(descriptor) = @@ -1759,7 +1756,7 @@ fn vm_named_set<'s>( == Some(true) { rv.set_bool(true); - return v8::Intercepted::Yes; + return v8::Intercepted::kYes; } } } @@ -1767,12 +1764,12 @@ fn vm_named_set<'s>( } // Match Node's contextual-store/definer fallthrough, including the // ordinary global's declaration attributes. - v8::Intercepted::No + v8::Intercepted::kNo }) } fn vm_named_descriptor<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, name: v8::Local<'s, v8::Name>, args: v8::PropertyCallbackArguments<'s>, mut rv: v8::ReturnValue, @@ -1783,19 +1780,19 @@ fn vm_named_descriptor<'s>( vm_property_step!(tc, sandbox.get_own_property_descriptor(tc, name)) { rv.set(descriptor); - return v8::Intercepted::Yes; + return v8::Intercepted::kYes; } } - v8::Intercepted::No + v8::Intercepted::kNo }) } fn vm_named_define<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, name: v8::Local<'s, v8::Name>, descriptor: &v8::PropertyDescriptor, args: v8::PropertyCallbackArguments<'s>, - _rv: v8::ReturnValue<()>, + _rv: v8::ReturnValue, ) -> v8::Intercepted { vm_property_callback!(scope, args, |tc, sandbox, global| { let attributes = vm_property_step!(tc, global.get_real_named_property_attributes(tc, name)); @@ -1803,7 +1800,7 @@ fn vm_named_define<'s>( .as_ref() .is_some_and(|attr| attr.is_read_only() && attr.is_dont_delete()) { - return v8::Intercepted::No; + return v8::Intercepted::kNo; } let undefined = v8::undefined(tc).into(); let mut forwarded = if descriptor.has_get() || descriptor.has_set() { @@ -1840,29 +1837,29 @@ fn vm_named_define<'s>( match vm_property_step!(tc, sandbox.define_property(tc, name, &forwarded)) { // Node intentionally allows the real global definition after this // operation; its current definer fallthrough is observable behavior. - Some(true) | Some(false) | None => v8::Intercepted::No, + Some(true) | Some(false) | None => v8::Intercepted::kNo, } }) } fn vm_named_delete<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, name: v8::Local<'s, v8::Name>, args: v8::PropertyCallbackArguments<'s>, mut rv: v8::ReturnValue, ) -> v8::Intercepted { vm_property_callback!(scope, args, |tc, sandbox, _global| { if vm_property_step!(tc, sandbox.delete(tc, name.into())) == Some(true) { - v8::Intercepted::No + v8::Intercepted::kNo } else { rv.set_bool(false); - v8::Intercepted::Yes + v8::Intercepted::kYes } }) } fn vm_named_enumerate<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, args: v8::PropertyCallbackArguments<'s>, mut rv: v8::ReturnValue, ) { @@ -1881,12 +1878,12 @@ fn vm_named_enumerate<'s>( ) { rv.set(names); } - v8::Intercepted::Yes + v8::Intercepted::kYes }); } fn vm_indexed_enumerate<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, args: v8::PropertyCallbackArguments<'s>, mut rv: v8::ReturnValue, ) { @@ -1907,42 +1904,42 @@ fn vm_indexed_enumerate<'s>( let mut next = 0; for index in 0..names.length() { let Some(name) = vm_property_step!(tc, names.get_index(tc, index)) else { - return v8::Intercepted::No; + return v8::Intercepted::kNo; }; if name.is_uint32() { let key = v8::Integer::new_from_unsigned(tc, next); let Some(key) = vm_property_step!(tc, key.to_string(tc)) else { - return v8::Intercepted::No; + return v8::Intercepted::kNo; }; if vm_property_step!(tc, indices.create_data_property(tc, key.into(), name)) != Some(true) { - return v8::Intercepted::No; + return v8::Intercepted::kNo; } next += 1; } } rv.set(indices); } - v8::Intercepted::Yes + v8::Intercepted::kYes }); } macro_rules! vm_indexed_callback { ($scope:expr, $index:expr, |$tc:ident, $name:ident| $body:expr) => {{ - let $tc = &mut v8::TryCatch::new($scope); + v8::tc_scope!($tc, $scope); let result = if let Some($name) = v8::Integer::new_from_unsigned($tc, $index).to_string($tc) { $body } else { - v8::Intercepted::No + v8::Intercepted::kNo }; if $tc.has_caught() || $tc.has_terminated() { assert!( $tc.rethrow().is_some(), "indexed property exception can be rethrown" ); - v8::Intercepted::Yes + v8::Intercepted::kYes } else { result } @@ -1950,7 +1947,7 @@ macro_rules! vm_indexed_callback { } fn vm_indexed_get<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, index: u32, args: v8::PropertyCallbackArguments<'s>, rv: v8::ReturnValue, @@ -1963,7 +1960,7 @@ fn vm_indexed_get<'s>( )) } fn vm_indexed_query<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, index: u32, args: v8::PropertyCallbackArguments<'s>, rv: v8::ReturnValue, @@ -1976,11 +1973,11 @@ fn vm_indexed_query<'s>( )) } fn vm_indexed_set<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, index: u32, value: v8::Local<'s, v8::Value>, args: v8::PropertyCallbackArguments<'s>, - rv: v8::ReturnValue<()>, + rv: v8::ReturnValue, ) -> v8::Intercepted { vm_indexed_callback!(scope, index, |tc, name| vm_named_set( tc, @@ -1991,7 +1988,7 @@ fn vm_indexed_set<'s>( )) } fn vm_indexed_descriptor<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, index: u32, args: v8::PropertyCallbackArguments<'s>, rv: v8::ReturnValue, @@ -2004,11 +2001,11 @@ fn vm_indexed_descriptor<'s>( )) } fn vm_indexed_define<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, index: u32, descriptor: &v8::PropertyDescriptor, args: v8::PropertyCallbackArguments<'s>, - rv: v8::ReturnValue<()>, + rv: v8::ReturnValue, ) -> v8::Intercepted { vm_indexed_callback!(scope, index, |tc, name| vm_named_define( tc, @@ -2019,7 +2016,7 @@ fn vm_indexed_define<'s>( )) } fn vm_indexed_delete<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, index: u32, args: v8::PropertyCallbackArguments<'s>, rv: v8::ReturnValue, @@ -2033,7 +2030,7 @@ fn vm_indexed_delete<'s>( } fn vm_context_template<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, sandbox: v8::Local<'s, v8::Object>, ) -> (v8::Local<'s, v8::ObjectTemplate>, v8::Local<'s, v8::Array>) { let ready = v8::Boolean::new(scope, false); @@ -2065,7 +2062,7 @@ fn vm_context_template<'s>( } fn vm_options_from_value<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, value: v8::Local<'s, v8::Value>, ) -> VmRunOptions { if value.is_null_or_undefined() { @@ -2081,20 +2078,20 @@ fn vm_options_from_value<'s>( return VmRunOptions::default(); }; let mut result = VmRunOptions::default(); - let read_string = |scope: &mut v8::HandleScope<'s>, key: &str| { + let read_string = |scope: &mut v8::PinScope<'s, '_>, key: &str| { let key_value = v8::String::new(scope, key).expect("V8 string key"); options .get(scope, key_value.into()) .filter(|value| value.is_string()) .map(|value| value.to_rust_string_lossy(scope)) }; - let read_i32 = |scope: &mut v8::HandleScope<'s>, key: &str| { + let read_i32 = |scope: &mut v8::PinScope<'s, '_>, key: &str| { let key_value = v8::String::new(scope, key).expect("V8 string key"); options .get(scope, key_value.into()) .and_then(|value| value.int32_value(scope)) }; - let read_u32 = |scope: &mut v8::HandleScope<'s>, key: &str| { + let read_u32 = |scope: &mut v8::PinScope<'s, '_>, key: &str| { let key_value = v8::String::new(scope, key).expect("V8 string key"); options .get(scope, key_value.into()) @@ -2116,7 +2113,7 @@ fn vm_options_from_value<'s>( } fn vm_throw_error<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, message: &str, code: Option<&str>, type_error: bool, @@ -2139,7 +2136,7 @@ fn vm_throw_error<'s>( } fn vm_run_script_in_context<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, isolate_handle: v8::IsolateHandle, context: v8::Local<'s, v8::Context>, code: &str, @@ -2171,7 +2168,7 @@ fn vm_run_script_in_context<'s>( let mut exception = None; { let context_scope = &mut v8::ContextScope::new(scope, context); - let tc = &mut v8::TryCatch::new(context_scope); + v8::tc_scope!(tc, context_scope); let source = v8::String::new(tc, code) .ok_or_else(|| String::from("vm source string too large for V8"))?; let filename = v8::String::new(tc, &options.filename) @@ -2286,8 +2283,8 @@ fn vm_run_script_in_context<'s>( // Context::new also restores the isolate's default snapshot when a custom // global template is supplied. Clear only the original host-backed globals // before activating sandbox routing; user-supplied properties still take precedence. -fn vm_scrub_child_globals(scope: &mut v8::HandleScope) -> Result<(), String> { - let tc = &mut v8::TryCatch::new(scope); +fn vm_scrub_child_globals(scope: &mut v8::PinScope<'_, '_>) -> Result<(), String> { + v8::tc_scope!(tc, scope); let global = tc.get_current_context().global(tc); for name in [ "Buffer", @@ -2337,7 +2334,7 @@ fn vm_scrub_child_globals(scope: &mut v8::HandleScope) -> Result<(), String> { } fn vm_create_context_value<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, args: &mut v8::FunctionCallbackArguments<'s>, ) -> Result, String> { let sandbox_value = args.get(0); @@ -2407,7 +2404,7 @@ fn vm_create_context_value<'s>( } fn vm_run_in_context_value<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, args: &mut v8::FunctionCallbackArguments<'s>, bridge_ctx: &BridgeCallContext, ) -> Result, String> { @@ -2422,7 +2419,7 @@ fn vm_run_in_context_value<'s>( .get(3) .to_object(scope) .ok_or_else(|| String::from("vm.runInContext missing sandbox object"))?; - let isolate_handle = unsafe { args.get_isolate() }.thread_safe_handle(); + let isolate_handle = scope.thread_safe_handle(); let context_global = scope.get_slot::().and_then(|registry| { registry .0 @@ -2452,7 +2449,7 @@ fn vm_run_in_context_value<'s>( } fn vm_run_in_this_context_value<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, args: &mut v8::FunctionCallbackArguments<'s>, bridge_ctx: &BridgeCallContext, ) -> Result, String> { @@ -2460,7 +2457,7 @@ fn vm_run_in_this_context_value<'s>( let options_value = args.get(1); let options = vm_options_from_value(scope, options_value); let context = scope.get_current_context(); - let isolate_handle = unsafe { args.get_isolate() }.thread_safe_handle(); + let isolate_handle = scope.thread_safe_handle(); vm_run_script_in_context( scope, isolate_handle, @@ -2473,7 +2470,7 @@ fn vm_run_in_this_context_value<'s>( } fn handle_local_bridge_call<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, method: &str, args: &mut v8::FunctionCallbackArguments<'s>, bridge_ctx: &BridgeCallContext, @@ -2556,9 +2553,23 @@ fn handle_local_bridge_call<'s>( .unwrap_or((limit, "limits.reactor.maxBridgeResponseBytes")); let value = args.get(0); if method == "v8.serialize" { - crate::node_v8_serialization::serialize(scope, value, limit, path).map(Some) + crate::node_v8_serialization::serialize( + scope, + value, + limit, + path, + runtime.resources().clone(), + ) + .map(Some) } else { - crate::node_v8_serialization::deserialize(scope, value, limit, path).map(Some) + crate::node_v8_serialization::deserialize( + scope, + value, + limit, + path, + runtime.resources().clone(), + ) + .map(Some) } } "process.cpuUsage" => process_cpu_usage_value(scope, args).map(Some), @@ -2572,7 +2583,7 @@ fn handle_local_bridge_call<'s>( } fn read_response_length_argument( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, method: &str, args: &v8::FunctionCallbackArguments, ) -> Option { @@ -2611,7 +2622,7 @@ pub(crate) fn declared_bridge_response_bytes( } fn bridge_response_declaration( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, method: &str, args: &v8::FunctionCallbackArguments, ) -> usize { @@ -2629,7 +2640,7 @@ fn bridge_response_declaration( /// The BridgeCallContext pointer must remain valid for the lifetime of the V8 context. /// The returned BridgeFnStore must also be kept alive. pub fn register_sync_bridge_fns( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, ctx: *const BridgeCallContext, methods: &[&str], ) -> BridgeFnStore { @@ -2668,7 +2679,7 @@ pub fn register_sync_bridge_fns( /// V8 FunctionTemplate callback for sync-blocking bridge calls. fn sync_bridge_callback<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, args: v8::FunctionCallbackArguments<'s>, mut rv: v8::ReturnValue, ) { @@ -2684,7 +2695,7 @@ fn sync_bridge_callback<'s>( let ctx = data.context(); { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); match handle_local_bridge_call(tc, &data.method, &mut args, ctx) { Ok(Some(value)) => { if tc.has_caught() { @@ -2763,7 +2774,7 @@ fn sync_bridge_callback<'s>( /// The BridgeCallContext and PendingPromises pointers must remain valid /// for the lifetime of the V8 context. pub fn register_async_bridge_fns( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, ctx: *const BridgeCallContext, pending: *const PendingPromises, methods: &[&str], @@ -2798,7 +2809,7 @@ pub fn register_async_bridge_fns( } fn attach_bridge_function_aliases<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, factory: Option>, func: v8::Local<'s, v8::Function>, aliases: &[&str], @@ -2819,7 +2830,7 @@ fn attach_bridge_function_aliases<'s>( /// Compile the `apply`-style alias factory once per bridge installation; every /// bridge function's aliases are closures from this one factory. fn bridge_apply_wrapper_factory<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, ) -> Option> { let source = v8::String::new( scope, @@ -2831,7 +2842,7 @@ fn bridge_apply_wrapper_factory<'s>( } fn build_bridge_apply_wrapper<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, factory: v8::Local<'s, v8::Function>, func: v8::Local<'s, v8::Function>, ) -> Option> { @@ -2843,7 +2854,7 @@ fn build_bridge_apply_wrapper<'s>( } fn reject_promise_with_error( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, resolver: v8::Local, message: &str, code: Option<&str>, @@ -2861,7 +2872,7 @@ fn reject_promise_with_error( /// V8 FunctionTemplate callback for async promise-returning bridge calls. fn async_bridge_callback( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { @@ -2951,7 +2962,7 @@ fn async_bridge_callback( /// Returns (BridgeFnStore, AsyncBridgeFnStore) that must be kept alive /// for the lifetime of the V8 context. pub fn replace_bridge_fns( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, ctx: *const BridgeCallContext, pending: *const PendingPromises, sync_fns: &[&str], @@ -2976,7 +2987,7 @@ pub fn replace_bridge_fns( /// After snapshot restore, these stubs are replaced with real functions /// that have proper External data pointing to a session-local BridgeCallContext. pub fn register_stub_bridge_fns( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, sync_fns: &[&str], async_fns: &[&str], ) { @@ -3002,7 +3013,7 @@ pub fn register_stub_bridge_fns( /// Serialize V8 function arguments as an array. fn serialize_v8_args( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, args: &v8::FunctionCallbackArguments, ) -> Result, String> { let count = args.length(); @@ -3018,7 +3029,7 @@ fn serialize_v8_args( /// Called when a BridgeResponse arrives during the session event loop. /// Flushes microtasks after resolution to process .then() handlers. pub fn resolve_pending_promise( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, pending: &PendingPromises, call_id: u64, status: u8, @@ -3126,7 +3137,7 @@ mod tests { isolate::init_v8_platform(); let mut isolate = isolate::create_isolate(None); let context = isolate::create_context(&mut isolate); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); install_session_bridge_fns( @@ -3135,8 +3146,8 @@ mod tests { &["_kernelPoll"], ) .unwrap(); - fn run(scope: &mut v8::HandleScope, source: &str) { - let scope = &mut v8::TryCatch::new(scope); + fn run(scope: &mut v8::PinScope<'_, '_>, source: &str) { + v8::tc_scope!(scope, scope); let source = v8::String::new(scope, source).unwrap(); let script = v8::Script::compile(scope, source, None).unwrap(); assert!(script.run(scope).is_some(), "bridge guard script failed"); @@ -3287,7 +3298,7 @@ mod tests { isolate::create_isolate(None) }; let root = isolate::create_context(&mut isolate); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &root); let scope = &mut v8::ContextScope::new(scope, context); let writer = Arc::new(Mutex::new(Vec::new())); @@ -3300,8 +3311,7 @@ mod tests { register_sync_bridge_fns(scope, &bridge_ctx, &["v8.getHeapStatistics"]); // Owning-test collection only; production queries and memory workers do not collect. scope.low_memory_notification(); - let mut native = v8::HeapStatistics::default(); - scope.get_heap_statistics(&mut native); + let native = scope.get_heap_statistics(); let source = v8::String::new( scope, r#" @@ -3320,7 +3330,7 @@ mod tests { ) .unwrap(); let result = { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let script = v8::Script::compile(tc, source, None).expect("compile heap-statistics control"); let result = script.run(tc).expect("run local heap-statistics control"); @@ -3392,7 +3402,7 @@ mod tests { }; { let root = isolate::create_context(&mut isolate); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &root); let scope = &mut v8::ContextScope::new(scope, context); let bridge_ctx = BridgeCallContext::new( @@ -3511,7 +3521,7 @@ mod tests { let mut isolate = isolate::create_isolate(None); let context = isolate::create_context(&mut isolate); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); @@ -3618,7 +3628,7 @@ mod tests { }) "#; { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let source = v8::String::new(tc, source).unwrap(); let script = v8::Script::compile(tc, source, None).unwrap(); let result = script.run(tc); @@ -3665,7 +3675,7 @@ mod tests { })() "#; { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let source = v8::String::new(tc, source).unwrap(); let script = v8::Script::compile(tc, source, None).unwrap(); let result = script.run(tc); @@ -3708,7 +3718,7 @@ mod tests { // The handler/template data must not root a sandbox once the bounded // registry and ordinary handles release its only child context. let weak_sandbox = { - let inner = &mut v8::HandleScope::new(scope); + v8::scope!(inner, scope); let sandbox = v8::Object::new(inner); let global = context.global(inner); let name = v8::String::new(inner, "_vmCreateContext").unwrap(); @@ -3753,7 +3763,7 @@ mod tests { fill_count = MAX_PENDING_PROMISES, ); { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let source = v8::String::new(tc, &source).unwrap(); let script = v8::Script::compile(tc, source, None).unwrap(); assert!(script.run(tc).is_some()); @@ -3812,7 +3822,7 @@ mod tests { fill_count = MAX_PENDING_PROMISES - 1, ); { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let source = v8::String::new(tc, &source).unwrap(); let script = v8::Script::compile(tc, source, None).unwrap(); assert!(script.run(tc).is_some()); @@ -3865,7 +3875,7 @@ mod tests { globalThis.__bufferInnerPromise = bufferInnerPromise; "#; { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let source = v8::String::new(tc, source).unwrap(); let script = v8::Script::compile(tc, source, None).unwrap(); assert!(script.run(tc).is_some()); @@ -3917,7 +3927,7 @@ mod tests { isolate::init_v8_platform(); let mut isolate = isolate::create_isolate(None); let context = isolate::create_context(&mut isolate); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); diff --git a/crates/executor-v8-runtime/src/embedded_runtime.rs b/crates/executor-v8-runtime/src/embedded_runtime.rs index 2c34620286..7f886b7082 100644 --- a/crates/executor-v8-runtime/src/embedded_runtime.rs +++ b/crates/executor-v8-runtime/src/embedded_runtime.rs @@ -9,7 +9,7 @@ use std::thread; use std::time::{Duration, Instant}; use crate::host_call::{record_sync_bridge_host_phase, BridgeCallRegistry, CallIdRouter}; -use crate::ipc_binary::BinaryFrame; +use crate::ipc_binary::{BinaryFrame, ExecutionErrorBin}; #[cfg(test)] use crate::runtime_protocol::RuntimeEvent; use crate::runtime_protocol::{ @@ -55,6 +55,7 @@ impl EmbeddedV8Runtime { max_concurrency: Option, runtime: agentos_driver_tokio::DriverHandle, ) -> io::Result { + isolate::validate_v8_feature_configuration()?; bridge::init_codec(); bridge::acquire_embedded_cbor_codec(); isolate::init_v8_platform(); @@ -812,6 +813,7 @@ pub fn spawn_embedded_runtime_ipc( max_concurrency: Option, runtime: agentos_driver_tokio::DriverHandle, ) -> io::Result<(UnixStream, EmbeddedRuntimeHandle)> { + isolate::validate_v8_feature_configuration()?; bridge::init_codec(); bridge::acquire_embedded_cbor_codec(); isolate::init_v8_platform(); @@ -876,7 +878,7 @@ fn run_embedded_runtime( // AGENTOS_THREAD_SITE: embedded-v8-writer let writer_handle = match thread::Builder::new() .name(format!("v8-ipc-writer-{connection_id}")) - .spawn(move || ipc_writer_thread(event_rx, writer_stream)) + .spawn(move || ipc_writer_thread(event_rx, writer_stream, connection_id)) { Ok(handle) => handle, Err(error) => { @@ -904,21 +906,51 @@ fn run_embedded_runtime( fn ipc_writer_thread( rx: crossbeam_channel::Receiver, mut writer: UnixStream, + connection_id: u64, ) { - while let Ok(envelope) = rx.recv() { - let frame: BinaryFrame = envelope.event.into(); - let bytes = match crate::ipc_binary::frame_to_bytes(&frame) { - Ok(bytes) => bytes, - Err(error) => { - eprintln!("embedded V8 runtime writer encode error: {error}"); - break; - } - }; - if let Err(error) = writer.write_all(&bytes) { - eprintln!("embedded V8 runtime writer error: {error}"); + for envelope in rx { + let frame = BinaryFrame::from(envelope.event); + let result = encode_runtime_event(frame).and_then(|bytes| writer.write_all(&bytes)); + if let Err(error) = result { + eprintln!("embedded V8 runtime writer error on connection {connection_id}: {error}"); break; } } + + // Closing this descriptor leaves the dispatcher clone connected. Shut down + // both directions to wake its read loop and retire the connection's sessions. + if let Err(error) = writer.shutdown(Shutdown::Both) { + eprintln!("embedded V8 runtime writer shutdown on connection {connection_id}: {error}"); + } +} + +fn encode_runtime_event(mut frame: BinaryFrame) -> io::Result> { + let encode_error = match crate::ipc_binary::frame_to_bytes(&frame) { + Ok(bytes) => return Ok(bytes), + Err(error) => error, + }; + + // A terminal result can still complete its execution if the guest's + // result is too large for the wire. Other frames have no error substitute. + if let BinaryFrame::ExecutionResult { + exit_code, + exports, + error, + .. + } = &mut frame + { + *exit_code = 1; + *exports = None; + *error = Some(ExecutionErrorBin { + error_type: "Error".to_owned(), + message: format!("Execution result exceeds the IPC framing limit: {encode_error}"), + stack: String::new(), + code: "ERR_AGENTOS_EXECUTION_RESULT_FRAME".to_owned(), + }); + return crate::ipc_binary::frame_to_bytes(&frame); + } + + Err(encode_error) } fn handle_connection( @@ -1264,6 +1296,69 @@ mod tests { } } + #[test] + fn ipc_writer_shuts_down_both_sides_on_unencodable_event() { + use std::io::Read; + + let (mut host, mut dispatcher) = UnixStream::pair().expect("create IPC sockets"); + let writer = dispatcher.try_clone().expect("clone dispatcher socket"); + let (sender, receiver) = crossbeam_channel::bounded(1); + sender + .send(runtime_envelope( + 1, + RuntimeEvent::BridgeCall { + session_id: "session".to_owned(), + call_id: 1, + method: "m".repeat(usize::from(u16::MAX) + 1), + payload: Vec::new(), + }, + )) + .expect("queue frame"); + + let writer_thread = thread::spawn(move || ipc_writer_thread(receiver, writer, 1)); + for endpoint in [&mut host, &mut dispatcher] { + endpoint + .set_read_timeout(Some(Duration::from_secs(1))) + .expect("set bounded read timeout"); + assert_eq!(endpoint.read(&mut [0]).expect("connection EOF"), 0); + } + writer_thread + .join() + .expect("writer exits after failed frame"); + } + + #[test] + fn unencodable_execution_result_keeps_a_typed_terminal_response() { + let frame = BinaryFrame::from(RuntimeEvent::ExecutionResult { + session_id: "session".to_owned(), + exit_code: 1, + exports: None, + error: Some(ExecutionErrorBin { + error_type: "Error".to_owned(), + message: "m".repeat(usize::from(u16::MAX) + 1), + stack: String::new(), + code: String::new(), + }), + limit_exceeded: None, + context_invalidated: true, + }); + let bytes = encode_runtime_event(frame).expect("encode terminal fallback"); + let result = crate::ipc_binary::read_frame(&mut bytes.as_slice()).expect("read fallback"); + match result { + BinaryFrame::ExecutionResult { + session_id, + exit_code: 1, + error: Some(error), + context_invalidated: true, + .. + } => { + assert_eq!(session_id, "session"); + assert_eq!(error.code, "ERR_AGENTOS_EXECUTION_RESULT_FRAME"); + } + other => panic!("unexpected terminal frame: {other:?}"), + } + } + #[test] fn session_outputs_share_one_vm_completion_limit() { use agentos_driver_tokio::accounting::{ResourceLedger, ResourceLimit}; diff --git a/crates/executor-v8-runtime/src/execution.rs b/crates/executor-v8-runtime/src/execution.rs index 466a7454e9..4b87bb48cb 100644 --- a/crates/executor-v8-runtime/src/execution.rs +++ b/crates/executor-v8-runtime/src/execution.rs @@ -80,7 +80,7 @@ impl BridgeCodeCache { /// Must be called within a ContextScope. #[cfg(test)] pub fn inject_globals( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, process_config: &ProcessConfig, os_config: &OsConfig, ) { @@ -106,7 +106,10 @@ pub fn inject_globals( // its initialization (whatwg-url/webidl-conversions uses it). } -pub fn install_high_resolution_time_global(scope: &mut v8::HandleScope, origin: *const Instant) { +pub fn install_high_resolution_time_global( + scope: &mut v8::PinScope<'_, '_>, + origin: *const Instant, +) { let context = scope.get_current_context(); let global = context.global(scope); let external = v8::External::new(scope, origin as *mut c_void); @@ -121,7 +124,7 @@ pub fn install_high_resolution_time_global(scope: &mut v8::HandleScope, origin: global.define_own_property(scope, key.into(), func.into(), attr); } -pub fn install_require_esm_sync_global(scope: &mut v8::HandleScope) { +pub fn install_require_esm_sync_global(scope: &mut v8::PinScope<'_, '_>) { let context = scope.get_current_context(); let global = context.global(scope); let template = v8::FunctionTemplate::builder(require_esm_sync_callback).build(scope); @@ -136,7 +139,7 @@ pub fn install_require_esm_sync_global(scope: &mut v8::HandleScope) { /// Compile stdin source as a real V8 ES module after the guest stdin pipe has /// reached EOF. This uses the same resolver and module state as file entrypoints; /// source is read through the guest fs bridge before this callback is invoked. -fn install_stdin_module_global(scope: &mut v8::HandleScope, file_path: &str) { +fn install_stdin_module_global(scope: &mut v8::PinScope<'_, '_>, file_path: &str) { let context = scope.get_current_context(); let global = context.global(scope); let Some(path) = v8::String::new(scope, file_path) else { @@ -154,7 +157,7 @@ fn install_stdin_module_global(scope: &mut v8::HandleScope, file_path: &str) { } fn execute_stdin_module_callback( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { @@ -217,7 +220,7 @@ fn execute_stdin_module_callback( } fn require_esm_sync_callback( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { @@ -309,7 +312,7 @@ fn require_esm_sync_callback( } fn high_resolution_time_callback( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { @@ -335,7 +338,7 @@ fn high_resolution_time_callback( /// Deserializes into V8, extracts processConfig and osConfig, freezes them, /// and sets them as non-writable, non-configurable global properties. pub fn inject_globals_from_payload( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, payload: &[u8], ) -> Result<(), ExecutionError> { let context = scope.get_current_context(); @@ -378,7 +381,7 @@ pub fn inject_globals_from_payload( } fn required_object_property<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, obj: v8::Local<'s, v8::Object>, name: &str, ) -> Result<(v8::Local<'s, v8::Value>, v8::Local<'s, v8::Object>), ExecutionError> { @@ -386,7 +389,7 @@ fn required_object_property<'s>( } fn required_object_property_with_label<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, obj: v8::Local<'s, v8::Object>, name: &str, error_label: &str, @@ -411,7 +414,7 @@ fn required_object_property_with_label<'s>( Ok((value, object)) } -fn is_plain_config_object(scope: &mut v8::HandleScope, object: v8::Local) -> bool { +fn is_plain_config_object(scope: &mut v8::PinScope<'_, '_>, object: v8::Local) -> bool { let Some(prototype) = object.get_prototype(scope) else { return false; }; @@ -430,7 +433,7 @@ fn is_plain_config_object(scope: &mut v8::HandleScope, object: v8::Local, object: v8::Local, label: &str, ) -> Result<(), ExecutionError> { @@ -459,7 +462,7 @@ fn invalid_globals_payload_error(message: impl Into) -> ExecutionError { /// Creates its own TryCatch scope internally so the caller's scope is released. /// Returns (exit_code, error) — exit code 0 on success. pub(crate) fn run_bridge_cached( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, bridge_code: &str, cache: &mut Option, ) -> (i32, Option) { @@ -475,7 +478,7 @@ pub(crate) fn run_bridge_cached( }), ); } - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let v8_source = match v8::String::new(tc, bridge_code) { Some(s) => s, @@ -595,11 +598,14 @@ pub(crate) fn run_bridge_cached( /// Run a short init script (e.g. post-restore config). Compiles and executes /// via v8::Script, returning (exit_code, error) on failure. No code caching. -pub fn run_init_script(scope: &mut v8::HandleScope, code: &str) -> (i32, Option) { +pub fn run_init_script( + scope: &mut v8::PinScope<'_, '_>, + code: &str, +) -> (i32, Option) { if code.is_empty() { return (0, None); } - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let source = match v8::String::new(tc, code) { Some(s) => s, None => { @@ -631,8 +637,8 @@ pub fn run_init_script(scope: &mut v8::HandleScope, code: &str) -> (i32, Option< crate::next_ticks::begin_entry(tc, false); let result = { let mut storage = std::pin::pin!(MicrotaskSuppressionStorage::new()); - let mut explicit = ExplicitMicrotaskScope::new(tc, storage.as_mut()); - script.run(&mut explicit) + let explicit = ExplicitMicrotaskScope::new(tc, storage.as_mut()); + script.run(&explicit) }; if result.is_none() { return match tc.exception() { @@ -653,7 +659,7 @@ pub fn run_init_script(scope: &mut v8::HandleScope, code: &str) -> (i32, Option< /// via v8::Script. Returns (exit_code, error) — exit code 0 on success, 1 on error. /// The `bridge_cache` parameter enables code caching for repeated bridge compilations. pub fn execute_script( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, bridge_code: &str, user_code: &str, bridge_cache: &mut Option, @@ -662,7 +668,7 @@ pub fn execute_script( } pub fn execute_script_with_options( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, bridge_ctx: Option<&BridgeCallContext>, bridge_code: &str, user_code: &str, @@ -704,7 +710,7 @@ pub fn execute_script_with_options( // Run user code { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let source = match v8::String::new(tc, user_code) { Some(s) => s, None => { @@ -761,8 +767,8 @@ pub fn execute_script_with_options( crate::next_ticks::begin_entry(tc, false); let script_result = { let mut suppression_storage = std::pin::pin!(MicrotaskSuppressionStorage::new()); - let mut run_scope = ExplicitMicrotaskScope::new(tc, suppression_storage.as_mut()); - script.run(&mut run_scope) + let run_scope = ExplicitMicrotaskScope::new(tc, suppression_storage.as_mut()); + script.run(&run_scope) }; let completion = match script_result { Some(result) => result, @@ -861,7 +867,7 @@ enum GuestExceptionOutcome { } fn route_entry_exception( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, exception: v8::Local, origin: &str, ) -> GuestExceptionOutcome { @@ -873,7 +879,7 @@ fn route_entry_exception( if extract_process_exit_code(scope, exception).is_some() { return terminal(exception_to_result(scope, exception)); } - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let global = tc.get_current_context().global(tc); let Some(key) = v8::String::new(tc, "_dispatchUncaughtException") else { return terminal(exception_to_result(tc, exception)); @@ -913,7 +919,7 @@ fn route_entry_exception( /// ProcessExitError is detected by sentinel property, not by regex matching on the /// error message or constructor name. pub fn extract_process_exit_code( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, exception: v8::Local, ) -> Option { if !exception.is_object() { @@ -937,7 +943,7 @@ pub fn extract_process_exit_code( } } -pub(crate) fn extract_global_process_exit_code(scope: &mut v8::HandleScope) -> Option { +pub(crate) fn extract_global_process_exit_code(scope: &mut v8::PinScope<'_, '_>) -> Option { let context = scope.get_current_context(); let global = context.global(scope); let process_key = v8::String::new(scope, "process")?; @@ -958,7 +964,7 @@ pub(crate) fn extract_global_process_exit_code(scope: &mut v8::HandleScope) -> O } } -pub(crate) fn global_process_exit_requested(scope: &mut v8::HandleScope) -> bool { +pub(crate) fn global_process_exit_requested(scope: &mut v8::PinScope<'_, '_>) -> bool { let context = scope.get_current_context(); let global = context.global(scope); let Some(key) = v8::String::new(scope, "_processExitRequested") else { @@ -982,11 +988,11 @@ pub(crate) fn global_process_exit_requested(scope: &mut v8::HandleScope) -> bool /// Node process bridge retain normal completion and warm isolate ownership. #[cfg_attr(test, allow(dead_code))] pub(crate) fn finalize_natural_process_exit( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, ) -> Result { let mut suppression_storage = std::pin::pin!(MicrotaskSuppressionStorage::new()); let mut explicit = ExplicitMicrotaskScope::new(scope, suppression_storage.as_mut()); - let tc = &mut v8::TryCatch::new(&mut *explicit); + v8::tc_scope!(tc, &mut *explicit); let failure = || { ( 1, @@ -1073,7 +1079,7 @@ pub(crate) enum TerminalExceptionKind { /// For ProcessExitError (detected via _isProcessExit sentinel), returns the error's exit code. /// For other errors, returns exit code 1. pub(crate) fn exception_to_result( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, exception: v8::Local, ) -> (i32, ExecutionError) { classify_terminal_exception(scope, exception).1 @@ -1082,7 +1088,7 @@ pub(crate) fn exception_to_result( // Preserve the kind from the same sentinel reads that determine the outcome. // A legacy message-based exit-code fallback is not an explicit process exit. pub(crate) fn classify_terminal_exception( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, exception: v8::Local, ) -> (TerminalExceptionKind, (i32, ExecutionError)) { if let Some(exit_code) = extract_process_exit_code(scope, exception) { @@ -1135,7 +1141,7 @@ fn parse_process_exit_code_from_error(error: &ExecutionError) -> Option { /// Reads constructor.name for error type, .message for the message, /// .stack for the stack trace, and optional .code for Node-style error codes. pub(crate) fn extract_error_info( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, exception: v8::Local, ) -> ExecutionError { if scope.is_execution_terminating() { @@ -1218,7 +1224,7 @@ pub(crate) fn extract_error_info( /// Build the _processConfig JS object: { cwd, env, timing_mitigation, frozen_time_ms, high_resolution_time } #[cfg(test)] fn build_process_config<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, config: &ProcessConfig, ) -> v8::Local<'s, v8::Object> { let obj = v8::Object::new(scope); @@ -1263,7 +1269,7 @@ fn build_process_config<'s>( /// Build the _osConfig JS object: { homedir, tmpdir, platform, arch } #[cfg(test)] fn build_os_config<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, config: &OsConfig, ) -> v8::Local<'s, v8::Object> { let obj = v8::Object::new(scope); @@ -1456,7 +1462,7 @@ pub fn has_pending_script_evaluation() -> bool { PENDING_SCRIPT_EVALUATION.with(|cell| cell.borrow().is_some()) } -pub fn pending_module_evaluation_needs_wait(scope: &mut v8::HandleScope) -> bool { +pub fn pending_module_evaluation_needs_wait(scope: &mut v8::PinScope<'_, '_>) -> bool { PENDING_MODULE_EVALUATION.with(|cell| { let borrow = cell.borrow(); let Some(pending) = borrow.as_ref() else { @@ -1467,7 +1473,7 @@ pub fn pending_module_evaluation_needs_wait(scope: &mut v8::HandleScope) -> bool }) } -pub fn pending_script_evaluation_needs_wait(scope: &mut v8::HandleScope) -> bool { +pub fn pending_script_evaluation_needs_wait(scope: &mut v8::PinScope<'_, '_>) -> bool { PENDING_SCRIPT_EVALUATION.with(|cell| { let borrow = cell.borrow(); let Some(pending) = borrow.as_ref() else { @@ -1479,7 +1485,7 @@ pub fn pending_script_evaluation_needs_wait(scope: &mut v8::HandleScope) -> bool } fn set_pending_module_evaluation( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, module: v8::Local, promise: v8::Local, ) { @@ -1491,7 +1497,10 @@ fn set_pending_module_evaluation( }); } -pub fn set_pending_script_evaluation(scope: &mut v8::HandleScope, promise: v8::Local) { +pub fn set_pending_script_evaluation( + scope: &mut v8::PinScope<'_, '_>, + promise: v8::Local, +) { PENDING_SCRIPT_EVALUATION.with(|cell| { *cell.borrow_mut() = Some(PendingScriptEvaluation { promise: v8::Global::new(scope, promise), @@ -1567,7 +1576,7 @@ impl MicrotaskSuppressionStorage { unsafe extern "C" { fn agentos_v8_suppress_microtasks_begin( storage: *mut std::ffi::c_void, - isolate: *mut v8::Isolate, + isolate: v8::UnsafeRawIsolatePtr, ); fn agentos_v8_suppress_microtasks_end(storage: *mut std::ffi::c_void); } @@ -1592,23 +1601,23 @@ pub(crate) fn collect_without_microtasks(isolate: &mut v8::Isolate) { unsafe { agentos_v8_suppress_microtasks_begin( storage.as_mut().get_unchecked_mut() as *mut _ as *mut std::ffi::c_void, - isolate, + isolate.as_raw_isolate_ptr(), ); } let _guard = Guard(storage.as_mut()); isolate.low_memory_notification(); } -pub(crate) struct ExplicitMicrotaskScope<'a, 's> { - scope: &'a mut v8::HandleScope<'s>, +pub(crate) struct ExplicitMicrotaskScope<'a, 's, 'i> { + scope: &'a mut v8::PinScope<'s, 'i>, previous: v8::MicrotasksPolicy, storage: std::pin::Pin<&'a mut MicrotaskSuppressionStorage>, native_suppression: bool, } -impl<'a, 's> ExplicitMicrotaskScope<'a, 's> { +impl<'a, 's, 'i> ExplicitMicrotaskScope<'a, 's, 'i> { pub(crate) fn new( - scope: &'a mut v8::HandleScope<'s>, + scope: &'a mut v8::PinScope<'s, 'i>, mut storage: std::pin::Pin<&'a mut MicrotaskSuppressionStorage>, ) -> Self { let previous = scope.get_microtasks_policy(); @@ -1620,7 +1629,7 @@ impl<'a, 's> ExplicitMicrotaskScope<'a, 's> { unsafe { agentos_v8_suppress_microtasks_begin( storage.as_mut().get_unchecked_mut() as *mut _ as *mut std::ffi::c_void, - &mut ***scope, + scope.as_raw_isolate_ptr(), ); } } @@ -1633,18 +1642,18 @@ impl<'a, 's> ExplicitMicrotaskScope<'a, 's> { } } } -impl<'a, 's> std::ops::Deref for ExplicitMicrotaskScope<'a, 's> { - type Target = v8::HandleScope<'s>; +impl<'a, 's, 'i> std::ops::Deref for ExplicitMicrotaskScope<'a, 's, 'i> { + type Target = v8::PinScope<'s, 'i>; fn deref(&self) -> &Self::Target { &*self.scope } } -impl<'a, 's> std::ops::DerefMut for ExplicitMicrotaskScope<'a, 's> { +impl<'a, 's, 'i> std::ops::DerefMut for ExplicitMicrotaskScope<'a, 's, 'i> { fn deref_mut(&mut self) -> &mut Self::Target { &mut *self.scope } } -impl Drop for ExplicitMicrotaskScope<'_, '_> { +impl Drop for ExplicitMicrotaskScope<'_, '_, '_> { fn drop(&mut self) { if self.native_suppression { unsafe { @@ -1658,7 +1667,7 @@ impl Drop for ExplicitMicrotaskScope<'_, '_> { } pub(crate) fn take_unhandled_promise_rejection( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, ) -> Option<(i32, ExecutionError)> { if let Some(result) = crate::terminal_callback::result(scope) { return Some(result); @@ -1668,7 +1677,7 @@ pub(crate) fn take_unhandled_promise_rejection( drain_promise_rejections(&mut checkpoint_scope) } -fn drain_promise_rejections(scope: &mut v8::HandleScope) -> Option<(i32, ExecutionError)> { +fn drain_promise_rejections(scope: &mut v8::PinScope<'_, '_>) -> Option<(i32, ExecutionError)> { if !scope .get_slot_mut::()? .begin_drain() @@ -1734,7 +1743,7 @@ fn drain_promise_rejections(scope: &mut v8::HandleScope) -> Option<(i32, Executi state.finish_drain(); return None; } - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); crate::owned_microtasks::checkpoint(tc); handled_effects_need_checkpoint = false; if let Some(exception) = tc.exception() { @@ -1747,7 +1756,7 @@ fn drain_promise_rejections(scope: &mut v8::HandleScope) -> Option<(i32, Executi } fn dispatch_promise_rejection( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, event: crate::isolate::PendingPromiseRejection, ) -> PromiseRejectionDispatchOutcome { use crate::isolate::PendingPromiseRejection; @@ -1762,7 +1771,7 @@ fn dispatch_promise_rejection( }, )) }; - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let (name, reason, promise, frame, unhandled) = match event { PendingPromiseRejection::Unhandled { promise, @@ -1844,10 +1853,10 @@ fn dispatch_promise_rejection( } pub fn finalize_pending_script_evaluation( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, ) -> Option<(i32, Option)> { let pending = PENDING_SCRIPT_EVALUATION.with(|cell| cell.borrow_mut().take())?; - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let promise = v8::Local::new(tc, &pending.promise); crate::owned_microtasks::checkpoint(tc); @@ -1881,7 +1890,7 @@ pub fn finalize_pending_script_evaluation( /// Cancel the timers and close the handles a settle-complete call left behind. /// The bridge owns both, so a context without the bridge has nothing to release. -pub fn release_leftover_handles(scope: &mut v8::HandleScope) -> Result<(), ExecutionError> { +pub fn release_leftover_handles(scope: &mut v8::PinScope<'_, '_>) -> Result<(), ExecutionError> { let interrupted = || ExecutionError { error_type: "Error".into(), message: "context handle cleanup was interrupted".into(), @@ -1895,7 +1904,7 @@ pub fn release_leftover_handles(scope: &mut v8::HandleScope) -> Result<(), Execu // throwing closer. Successful cleanup retains its explicit checkpoint. let mut suppression_storage = std::pin::pin!(MicrotaskSuppressionStorage::new()); let mut explicit = ExplicitMicrotaskScope::new(scope, suppression_storage.as_mut()); - let tc = &mut v8::TryCatch::new(&mut *explicit); + v8::tc_scope!(tc, &mut *explicit); let context = tc.get_current_context(); let global = context.global(tc); let Some(key) = v8::String::new(tc, "_releaseLeftoverHandles") else { @@ -1934,7 +1943,7 @@ pub fn release_leftover_handles(scope: &mut v8::HandleScope) -> Result<(), Execu /// Report a failure that happened after a settle-complete call's result was /// fixed. It goes to the call's stderr (sidecar stderr when the bridge is /// absent) instead of changing the call's outcome. -pub fn report_after_call_failure(scope: &mut v8::HandleScope, error: &ExecutionError) { +pub fn report_after_call_failure(scope: &mut v8::PinScope<'_, '_>, error: &ExecutionError) { let detail = if error.stack.is_empty() { format!("{}: {}", error.error_type, error.message) } else { @@ -1946,13 +1955,13 @@ pub fn report_after_call_failure(scope: &mut v8::HandleScope, error: &ExecutionE } } -fn write_guest_stderr(scope: &mut v8::HandleScope, message: &str) -> bool { +fn write_guest_stderr(scope: &mut v8::PinScope<'_, '_>, message: &str) -> bool { if scope.is_execution_terminating() { return false; } let mut suppression_storage = std::pin::pin!(MicrotaskSuppressionStorage::new()); let mut explicit = ExplicitMicrotaskScope::new(scope, suppression_storage.as_mut()); - let tc = &mut v8::TryCatch::new(&mut *explicit); + v8::tc_scope!(tc, &mut *explicit); let global = tc.get_current_context().global(tc); let (Some(error_key), Some(apply_key), Some(text)) = ( v8::String::new(tc, "_error"), @@ -1982,7 +1991,7 @@ fn write_guest_stderr(scope: &mut v8::HandleScope, message: &str) -> bool { } fn serialize_module_exports( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, module: v8::Local, ) -> Result, ExecutionError> { // Serialize module namespace (exports) @@ -2036,14 +2045,14 @@ fn serialize_module_exports( // it reports through uncaughtException with the rejection origin, without an // ordinary unhandledRejection event or serialization of incomplete exports. fn module_entry_exception_result( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, exception: v8::Local, ) -> (i32, Option>, Option) { match route_entry_exception(scope, exception, "unhandledRejection") { GuestExceptionOutcome::Handled => { // Consuming the entry failure does not consume work created by its // handler. Match the CJS boundary's ordinary post-handler drain. - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); crate::owned_microtasks::checkpoint(tc); if let Some(exception) = tc.exception() { let (code, error) = exception_to_result(tc, exception); @@ -2064,10 +2073,10 @@ fn module_entry_exception_result( #[cfg_attr(test, allow(dead_code))] pub fn finalize_pending_module_evaluation( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, ) -> Option<(i32, Option>, Option)> { let pending = PENDING_MODULE_EVALUATION.with(|cell| cell.borrow_mut().take())?; - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let module = v8::Local::new(tc, &pending.module); let promise = v8::Local::new(tc, &pending.promise); @@ -2126,7 +2135,7 @@ pub fn finalize_pending_module_evaluation( /// Returns (exit_code, serialized_exports, error). /// The `bridge_cache` parameter enables code caching for repeated bridge compilations. pub fn execute_module( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, bridge_ctx: &BridgeCallContext, bridge_code: &str, user_code: &str, @@ -2146,7 +2155,7 @@ pub fn execute_module( /// Execute an ES module after any process-specific runtime setup. pub(crate) fn execute_module_in_context( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, bridge_ctx: &BridgeCallContext, bridge_code: &str, runtime_initialized: bool, @@ -2212,7 +2221,7 @@ pub(crate) fn execute_module_in_context( // Compile and evaluate as ES module { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let resource_name_str = file_path.unwrap_or(""); // A process entry gets `require` from the runtime shim's @@ -2302,8 +2311,8 @@ pub(crate) fn execute_module_in_context( crate::next_ticks::begin_entry(tc, true); let eval_result = { let mut storage = std::pin::pin!(MicrotaskSuppressionStorage::new()); - let mut explicit = ExplicitMicrotaskScope::new(tc, storage.as_mut()); - module.evaluate(&mut explicit) + let explicit = ExplicitMicrotaskScope::new(tc, storage.as_mut()); + module.evaluate(&explicit) }; if eval_result.is_none() { clear_module_state(); @@ -2416,7 +2425,7 @@ pub(crate) fn execute_module_in_context( /// Returns a list of (specifier, referrer_name) pairs for all imports /// that are not already in the module cache. fn extract_uncached_imports( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, module: v8::Local, referrer_name: &str, ) -> Vec<(String, String)> { @@ -2451,7 +2460,7 @@ fn extract_uncached_imports( /// for any newly discovered imports. Falls back silently if the host doesn't /// support batch resolution (the resolve callback handles individual resolution). fn prefetch_module_imports( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, bridge_ctx: &BridgeCallContext, root_module: v8::Local, root_name: &str, @@ -2556,7 +2565,7 @@ fn prefetch_module_imports( } fn resolve_or_compile_module<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, specifier_str: &str, referrer_name: &str, ) -> Option> { @@ -2716,7 +2725,7 @@ fn cache_resolved_module( } pub(crate) fn import_meta_resolve_callback( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { @@ -2780,7 +2789,7 @@ pub extern "C" fn import_meta_object_callback( module: v8::Local, meta: v8::Local, ) { - let scope = &mut unsafe { v8::CallbackScope::new(context) }; + v8::callback_scope!(unsafe scope, context); // Look up the module's resource name from MODULE_RESOLVE_STATE.module_names // which maps identity_hash → resource_name. @@ -2810,7 +2819,7 @@ pub extern "C" fn import_meta_object_callback( /// Set `import.meta.url` and `import.meta.resolve` for a module at `path`. pub(crate) fn set_import_meta_fields( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, meta: v8::Local, path: &str, ) { @@ -2823,7 +2832,7 @@ pub(crate) fn set_import_meta_fields( } fn set_import_meta_url_and_resolve( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, meta: v8::Local, referrer: &str, url: &str, @@ -2844,7 +2853,7 @@ fn set_import_meta_url_and_resolve( #[cfg_attr(test, allow(dead_code))] fn dynamic_import_namespace_callback( - _scope: &mut v8::HandleScope, + _scope: &mut v8::PinScope<'_, '_>, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { @@ -2853,7 +2862,7 @@ fn dynamic_import_namespace_callback( #[cfg_attr(test, allow(dead_code))] fn dynamic_import_reject_callback( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { @@ -2864,13 +2873,13 @@ fn dynamic_import_reject_callback( #[cfg_attr(test, allow(dead_code))] pub fn dynamic_import_callback<'a>( - scope: &mut v8::HandleScope<'a>, + scope: &mut v8::PinScope<'a, '_>, _host_defined_options: v8::Local<'a, v8::Data>, resource_name: v8::Local<'a, v8::Value>, specifier: v8::Local<'a, v8::String>, _import_attributes: v8::Local<'a, v8::FixedArray>, ) -> Option> { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let specifier_str = specifier.to_rust_string_lossy(tc); let referrer_name = resolve_dynamic_import_referrer_name(tc, resource_name); @@ -2947,7 +2956,7 @@ pub fn dynamic_import_callback<'a>( } fn resolve_dynamic_import_referrer_name( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, resource_name: v8::Local, ) -> String { let candidate = resource_name.to_rust_string_lossy(scope); @@ -2986,7 +2995,7 @@ fn resolve_dynamic_import_referrer_name( #[cfg_attr(test, allow(dead_code))] fn resolved_promise<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, value: v8::Local<'s, v8::Value>, ) -> Option> { let resolver = v8::PromiseResolver::new(scope)?; @@ -2996,7 +3005,7 @@ fn resolved_promise<'s>( #[cfg_attr(test, allow(dead_code))] fn rejected_promise<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, reason: v8::Local<'s, v8::Value>, ) -> Option> { let resolver = v8::PromiseResolver::new(scope)?; @@ -3010,7 +3019,7 @@ fn rejected_promise<'s>( /// {resolved, source} results (null entries for unresolvable modules). /// Returns None if the host doesn't support batch resolution or on IPC error. fn batch_resolve_via_ipc( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, ctx: &BridgeCallContext, batch: &[(String, String)], ) -> Option>> { @@ -3081,7 +3090,7 @@ fn module_resolve_callback<'a>( referrer: v8::Local<'a, v8::Module>, ) -> Option> { // SAFETY: CallbackScope can be constructed from Local within a V8 callback - let scope = &mut unsafe { v8::CallbackScope::new(context) }; + v8::callback_scope!(unsafe scope, context); let specifier_str = specifier.to_rust_string_lossy(scope); let referrer_hash = referrer.get_identity_hash(); @@ -3097,7 +3106,7 @@ fn module_resolve_callback<'a>( /// Send _resolveModule(specifier, referrer_path) via sync-blocking IPC. fn resolve_module_via_ipc( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, ctx: &BridgeCallContext, specifier: &str, referrer: &str, @@ -3171,7 +3180,7 @@ const MODULE_SOURCE_CHUNK_BYTES: usize = 64 * 1024; const MAX_MODULE_SOURCE_BYTES: usize = 64 * 1024 * 1024; fn load_module_via_ipc( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, ctx: &BridgeCallContext, resolved_path: &str, ) -> Option<(String, Option)> { @@ -3271,7 +3280,7 @@ pub enum ResolvedModuleFormat { } fn lookup_module_format_via_ipc( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, ctx: &BridgeCallContext, resolved_path: &str, ) -> Option { @@ -3316,13 +3325,13 @@ fn lookup_module_format_via_ipc( } /// Throw a V8 exception for module resolution errors. -fn throw_module_error(scope: &mut v8::HandleScope, message: &str) { +fn throw_module_error(scope: &mut v8::PinScope<'_, '_>, message: &str) { let msg = v8::String::new(scope, message).unwrap(); let exc = v8::Exception::error(scope, msg); scope.throw_exception(exc); } -fn throw_module_error_with_code(scope: &mut v8::HandleScope, message: &str, code: &str) { +fn throw_module_error_with_code(scope: &mut v8::PinScope<'_, '_>, message: &str, code: &str) { let message = v8::String::new(scope, message).unwrap(); let exception = v8::Exception::error(scope, message); if let Ok(object) = v8::Local::::try_from(exception) { @@ -3350,7 +3359,7 @@ fn strip_leading_shebang(source: &str) -> &str { } fn build_module_source( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, raw_source: impl Into, resolved_path: &str, module_format: Option, @@ -3398,7 +3407,7 @@ fn build_json_esm_shim(resolved_path: &str) -> String { } fn build_cjs_esm_shim( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, raw_source: &str, resolved_path: &str, ) -> String { @@ -3443,7 +3452,7 @@ fn build_cjs_esm_shim( /// for a path somehow re-enters extraction for the same path, the inner call /// returns an empty list instead of recursing. fn extract_runtime_cjs_export_names( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, resolved_path: &str, ) -> Vec { let already_in_progress = CJS_RUNTIME_EXTRACTION_IN_PROGRESS.with(|cell| { @@ -3461,10 +3470,10 @@ fn extract_runtime_cjs_export_names( } fn extract_runtime_cjs_export_names_inner( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, resolved_path: &str, ) -> Vec { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let context = tc.get_current_context(); let global = context.global(tc); @@ -4278,7 +4287,7 @@ fn add_esm_runtime_prelude(source: impl Into) -> String { && !source_declares_identifier(&source, "require") { prelude - .push_str("const require = globalThis._moduleModule.createRequire(import.meta.url);\n"); + .push_str("const require = globalThis.__agentOsGuestCreateRequire(import.meta.url);\n"); } if !prelude.is_empty() { @@ -4598,7 +4607,7 @@ mod tests { fn esm_runtime_prelude_injects_require_when_it_is_only_called() { let source = "const fs = require(\"node:fs\");"; assert!(add_esm_runtime_prelude(source).starts_with( - "const require = globalThis._moduleModule.createRequire(import.meta.url);\n" + "const require = globalThis.__agentOsGuestCreateRequire(import.meta.url);\n" )); } @@ -4680,12 +4689,8 @@ export const file = new File([], "empty.txt"); } /// Helper: serialize a V8 string value for test BridgeResponse payloads - fn v8_serialize_str( - iso: &mut v8::OwnedIsolate, - ctx: &v8::Global, - s: &str, - ) -> Vec { - let scope = &mut v8::HandleScope::new(iso); + fn v8_serialize_str(iso: &mut v8::Isolate, ctx: &v8::Global, s: &str) -> Vec { + v8::scope!(scope, iso); let local = v8::Local::new(scope, ctx); let scope = &mut v8::ContextScope::new(scope, local); let val = v8::String::new(scope, s).unwrap(); @@ -4693,12 +4698,8 @@ export const file = new File([], "empty.txt"); } /// Helper: serialize a V8 integer value for test BridgeResponse payloads - fn v8_serialize_int( - iso: &mut v8::OwnedIsolate, - ctx: &v8::Global, - n: i64, - ) -> Vec { - let scope = &mut v8::HandleScope::new(iso); + fn v8_serialize_int(iso: &mut v8::Isolate, ctx: &v8::Global, n: i64) -> Vec { + v8::scope!(scope, iso); let local = v8::Local::new(scope, ctx); let scope = &mut v8::ContextScope::new(scope, local); let val = v8::Number::new(scope, n as f64); @@ -4706,8 +4707,8 @@ export const file = new File([], "empty.txt"); } /// Helper: serialize a V8 null value for test BridgeResponse payloads - fn v8_serialize_null(iso: &mut v8::OwnedIsolate, ctx: &v8::Global) -> Vec { - let scope = &mut v8::HandleScope::new(iso); + fn v8_serialize_null(iso: &mut v8::Isolate, ctx: &v8::Global) -> Vec { + v8::scope!(scope, iso); let local = v8::Local::new(scope, ctx); let scope = &mut v8::ContextScope::new(scope, local); let val = v8::null(scope); @@ -4716,11 +4717,11 @@ export const file = new File([], "empty.txt"); /// Helper: serialize a V8 object (from JS expression) for test BridgeResponse payloads fn v8_serialize_eval( - iso: &mut v8::OwnedIsolate, + iso: &mut v8::Isolate, ctx: &v8::Global, expr: &str, ) -> Vec { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(scope, iso); let local = v8::Local::new(scope, ctx); let scope = &mut v8::ContextScope::new(scope, local); let source = v8::String::new(scope, expr).unwrap(); @@ -4730,12 +4731,8 @@ export const file = new File([], "empty.txt"); } /// Enter a context, run JS, return the string result. - fn eval( - isolate: &mut v8::OwnedIsolate, - context: &v8::Global, - code: &str, - ) -> String { - let scope = &mut v8::HandleScope::new(isolate); + fn eval(isolate: &mut v8::Isolate, context: &v8::Global, code: &str) -> String { + v8::scope!(scope, isolate); let local = v8::Local::new(scope, context); let scope = &mut v8::ContextScope::new(scope, local); let source = v8::String::new(scope, code).unwrap(); @@ -4745,12 +4742,8 @@ export const file = new File([], "empty.txt"); } /// Enter a context, run JS, return true if the result is truthy. - fn eval_bool( - isolate: &mut v8::OwnedIsolate, - context: &v8::Global, - code: &str, - ) -> bool { - let scope = &mut v8::HandleScope::new(isolate); + fn eval_bool(isolate: &mut v8::Isolate, context: &v8::Global, code: &str) -> bool { + v8::scope!(scope, isolate); let local = v8::Local::new(scope, context); let scope = &mut v8::ContextScope::new(scope, local); let source = v8::String::new(scope, code).unwrap(); @@ -4761,14 +4754,14 @@ export const file = new File([], "empty.txt"); /// Enter a context, run JS, return true if an exception was thrown. fn eval_throws( - isolate: &mut v8::OwnedIsolate, + isolate: &mut v8::Isolate, context: &v8::Global, code: &str, ) -> bool { - let scope = &mut v8::HandleScope::new(isolate); + v8::scope!(scope, isolate); let local = v8::Local::new(scope, context); let scope = &mut v8::ContextScope::new(scope, local); - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let source = v8::String::new(tc, code).unwrap(); if let Some(script) = v8::Script::compile(tc, source, None) { script.run(tc); @@ -4816,7 +4809,7 @@ export const file = new File([], "empty.txt"); let mut isolate = isolate::create_isolate(None); let context = isolate::create_context(&mut isolate); let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); execute_script( @@ -4841,7 +4834,7 @@ export const file = new File([], "empty.txt"); let mut isolate = isolate::create_isolate(None); let context = isolate::create_context(&mut isolate); let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); execute_script( @@ -4895,7 +4888,7 @@ export const file = new File([], "empty.txt"); // Inject globals { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); inject_globals(scope, &process_config, &os_config); @@ -4956,7 +4949,7 @@ export const file = new File([], "empty.txt"); ); let err = { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); inject_globals_from_payload(scope, &payload).expect_err("missing osConfig") @@ -4999,7 +4992,7 @@ export const file = new File([], "empty.txt"); ); let err = { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); inject_globals_from_payload(scope, &payload).expect_err("primitive processConfig") @@ -5042,7 +5035,7 @@ export const file = new File([], "empty.txt"); ); let err = { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); inject_globals_from_payload(scope, &payload).expect_err("primitive env") @@ -5084,7 +5077,7 @@ export const file = new File([], "empty.txt"); ); let err = { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); inject_globals_from_payload(scope, &payload).expect_err("missing env") @@ -5127,7 +5120,7 @@ export const file = new File([], "empty.txt"); ); let err = { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); inject_globals_from_payload(scope, &payload).expect_err("typed array env") @@ -5171,7 +5164,7 @@ export const file = new File([], "empty.txt"); ); { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); inject_globals_from_payload(scope, &payload).expect("valid globals payload"); @@ -5209,7 +5202,7 @@ export const file = new File([], "empty.txt"); }; { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); inject_globals(scope, &process_config, &os_config); @@ -5245,7 +5238,7 @@ export const file = new File([], "empty.txt"); }; { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); inject_globals(scope, &process_config, &os_config); @@ -5322,7 +5315,7 @@ export const file = new File([], "empty.txt"); }; { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); inject_globals(scope, &process_config, &os_config); @@ -5361,7 +5354,7 @@ export const file = new File([], "empty.txt"); }; { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); inject_globals(scope, &process_config, &os_config); @@ -5489,7 +5482,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_sync_bridge_fns( @@ -5549,7 +5542,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_sync_bridge_fns( @@ -5608,7 +5601,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_sync_bridge_fns( @@ -5647,7 +5640,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_sync_bridge_fns( @@ -5675,7 +5668,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -5712,7 +5705,7 @@ export const file = new File([], "empty.txt"); let result_v8 = v8_serialize_str(&mut iso, &ctx, "async result"); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); bridge::resolve_pending_promise(scope, &pending, 1, 0, Some(result_v8)).unwrap(); @@ -5722,7 +5715,7 @@ export const file = new File([], "empty.txt"); // Verify promise is fulfilled with correct value { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let source = v8::String::new(scope, "_promise").unwrap(); @@ -5751,7 +5744,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -5767,7 +5760,7 @@ export const file = new File([], "empty.txt"); // Reject the promise { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); bridge::resolve_pending_promise( @@ -5808,7 +5801,7 @@ export const file = new File([], "empty.txt"); // Verify promise is rejected with error { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let source = v8::String::new(scope, "_promise").unwrap(); @@ -5846,7 +5839,7 @@ export const file = new File([], "empty.txt"); ); assert_eq!(pending.len(), 1); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); bridge::resolve_pending_promise( @@ -5859,7 +5852,7 @@ export const file = new File([], "empty.txt"); .unwrap(); } { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let source = v8::String::new(scope, "_spoofedErrorPromise").unwrap(); @@ -5895,7 +5888,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -5916,7 +5909,7 @@ export const file = new File([], "empty.txt"); // Resolve in reverse order (p2 first, then p1) let r2 = v8_serialize_str(&mut iso, &ctx, "dns-result"); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); bridge::resolve_pending_promise(scope, &pending, 2, 0, Some(r2)).unwrap(); @@ -5925,7 +5918,7 @@ export const file = new File([], "empty.txt"); let r1 = v8_serialize_str(&mut iso, &ctx, "fetch-result"); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); bridge::resolve_pending_promise(scope, &pending, 1, 0, Some(r1)).unwrap(); @@ -5934,7 +5927,7 @@ export const file = new File([], "empty.txt"); // Verify both promises fulfilled correctly { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -5974,7 +5967,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -5989,7 +5982,7 @@ export const file = new File([], "empty.txt"); // Resolve with None (null result) { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); bridge::resolve_pending_promise(scope, &pending, 1, 0, None).unwrap(); @@ -5997,7 +5990,7 @@ export const file = new File([], "empty.txt"); // Promise should be fulfilled with undefined { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let source = v8::String::new(scope, "_promise").unwrap(); @@ -6023,7 +6016,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -6046,7 +6039,7 @@ export const file = new File([], "empty.txt"); // Resolve the promise (microtasks flushed inside resolve_pending_promise) { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); bridge::resolve_pending_promise(scope, &pending, 1, 0, None).unwrap(); @@ -6062,7 +6055,7 @@ export const file = new File([], "empty.txt"); let ctx = isolate::create_context(&mut iso); let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, "", "var x = 1 + 2;", &mut None) @@ -6082,7 +6075,7 @@ export const file = new File([], "empty.txt"); let bridge = "(function() { globalThis._bridgeReady = true; })()"; let user = "var _sawBridge = _bridgeReady;"; let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, bridge, user, &mut None) @@ -6100,7 +6093,7 @@ export const file = new File([], "empty.txt"); let ctx = isolate::create_context(&mut iso); let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script( @@ -6123,7 +6116,7 @@ export const file = new File([], "empty.txt"); let ctx = isolate::create_context(&mut iso); let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, "", "var x = {;", &mut None) @@ -6141,7 +6134,7 @@ export const file = new File([], "empty.txt"); let ctx = isolate::create_context(&mut iso); let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, "", "null.foo", &mut None) @@ -6160,7 +6153,7 @@ export const file = new File([], "empty.txt"); let ctx = isolate::create_context(&mut iso); let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, "function {", "var x = 1;", &mut None) @@ -6179,7 +6172,7 @@ export const file = new File([], "empty.txt"); let ctx = isolate::create_context(&mut iso); let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, "", "'hello'", &mut None) @@ -6195,7 +6188,7 @@ export const file = new File([], "empty.txt"); let ctx = isolate::create_context(&mut iso); let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script( @@ -6219,7 +6212,7 @@ export const file = new File([], "empty.txt"); let ctx = isolate::create_context(&mut iso); let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, "", "throw 'raw string error';", &mut None) @@ -6246,7 +6239,7 @@ export const file = new File([], "empty.txt"); let user_code = "export const x = 42;\nexport const msg = 'hello';"; let (code, exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module(scope, &bridge_ctx, "", user_code, None, &mut None) @@ -6256,7 +6249,7 @@ export const file = new File([], "empty.txt"); assert!(error.is_none()); let exports = exports.unwrap(); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let val = crate::bridge::deserialize_v8_value(scope, &exports).unwrap(); @@ -6291,7 +6284,7 @@ export const file = new File([], "empty.txt"); "test-session".into(), ); let (code, exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module( @@ -6329,7 +6322,7 @@ export const file = new File([], "empty.txt"); export default result; "#; let (code, exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module(scope, &bridge_ctx, bridge_code, user_code, None, &mut None) @@ -6339,7 +6332,7 @@ export const file = new File([], "empty.txt"); assert!(error.is_none()); let exports = exports.unwrap(); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let val = crate::bridge::deserialize_v8_value(scope, &exports).unwrap(); @@ -6375,7 +6368,7 @@ export const file = new File([], "empty.txt"); ); let (code, exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module( @@ -6392,7 +6385,7 @@ export const file = new File([], "empty.txt"); assert!(error.is_none()); let exports = exports.unwrap(); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let val = crate::bridge::deserialize_v8_value(scope, &exports).unwrap(); @@ -6420,7 +6413,7 @@ export const file = new File([], "empty.txt"); ); let (code, _exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module( @@ -6450,7 +6443,7 @@ export const file = new File([], "empty.txt"); ); let (code, _exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module( @@ -6482,7 +6475,7 @@ export const file = new File([], "empty.txt"); let bridge = "(function() { globalThis._bridgeReady = true; })()"; let user = "export const saw = _bridgeReady;"; let (code, exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module(scope, &bridge_ctx, bridge, user, None, &mut None) @@ -6492,7 +6485,7 @@ export const file = new File([], "empty.txt"); assert!(error.is_none()); let exports = exports.unwrap(); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let val = crate::bridge::deserialize_v8_value(scope, &exports).unwrap(); @@ -6567,7 +6560,7 @@ export const file = new File([], "empty.txt"); let user_code = "import { dep_val } from './dep.mjs';\nexport const result = dep_val + 1;"; let (code, exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module( @@ -6584,7 +6577,7 @@ export const file = new File([], "empty.txt"); assert!(error.is_none()); let exports = exports.unwrap(); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let val = crate::bridge::deserialize_v8_value(scope, &exports).unwrap(); @@ -6616,7 +6609,7 @@ export const file = new File([], "empty.txt"); // Register async bridge function let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -6653,7 +6646,7 @@ export const file = new File([], "empty.txt"); // Run event loop let completed = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None) @@ -6711,7 +6704,7 @@ export const file = new File([], "empty.txt"); )) .unwrap(); let completed = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None) @@ -6741,7 +6734,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -6791,7 +6784,7 @@ export const file = new File([], "empty.txt"); .unwrap(); let completed = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None) @@ -6820,7 +6813,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -6842,7 +6835,7 @@ export const file = new File([], "empty.txt"); .unwrap(); let completed = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None) @@ -6873,7 +6866,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -6892,7 +6885,7 @@ export const file = new File([], "empty.txt"); tx.send(crate::session::SessionCommand::Shutdown).unwrap(); let completed = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None) @@ -6914,7 +6907,7 @@ export const file = new File([], "empty.txt"); // No pending promises — event loop should exit immediately let completed = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None) @@ -6940,7 +6933,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -6994,7 +6987,7 @@ export const file = new File([], "empty.txt"); .unwrap(); let completed = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None) @@ -7032,7 +7025,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -7067,7 +7060,7 @@ export const file = new File([], "empty.txt"); .unwrap(); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None); @@ -7091,7 +7084,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -7155,7 +7148,7 @@ export const file = new File([], "empty.txt"); .unwrap(); let completed = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None) @@ -7186,7 +7179,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -7239,7 +7232,7 @@ export const file = new File([], "empty.txt"); .unwrap(); let completed = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None) @@ -7269,7 +7262,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -7319,7 +7312,7 @@ export const file = new File([], "empty.txt"); .unwrap(); let completed = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None) @@ -7347,7 +7340,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -7392,7 +7385,7 @@ export const file = new File([], "empty.txt"); // Should not crash even without dispatch function registered let completed = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None) @@ -7418,7 +7411,7 @@ export const file = new File([], "empty.txt"); let _fn_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _fn_store = bridge::register_async_bridge_fns( @@ -7469,7 +7462,7 @@ export const file = new File([], "empty.txt"); .unwrap(); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop(scope, &rx, &pending, None, None, None); @@ -7501,7 +7494,7 @@ export const file = new File([], "empty.txt"); // Run an infinite loop — timeout should terminate it let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, "", "while(true) {}", &mut None) @@ -7529,7 +7522,7 @@ export const file = new File([], "empty.txt"); .expect("timeout guard should start"); let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, "", "1 + 1", &mut None) @@ -7573,7 +7566,7 @@ export const file = new File([], "empty.txt"); ); let _async_store; { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); _async_store = bridge::register_async_bridge_fns( @@ -7586,7 +7579,7 @@ export const file = new File([], "empty.txt"); // Execute code that calls async bridge function (creates a pending promise) let (_code, _error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, "", "_slowFn('never-responds')", &mut None) @@ -7602,7 +7595,7 @@ export const file = new File([], "empty.txt"); // Run event loop — it should be terminated by the timeout // (no messages on cmd_rx, so it blocks until abort_rx fires) let completed = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); crate::session::run_event_loop( @@ -7645,7 +7638,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -7674,7 +7667,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -7698,7 +7691,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -7717,7 +7710,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -7745,7 +7738,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -7774,7 +7767,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -7796,7 +7789,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -7821,7 +7814,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script( @@ -7841,7 +7834,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script( @@ -7866,7 +7859,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -7901,7 +7894,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -7957,7 +7950,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -7991,7 +7984,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -8046,7 +8039,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -8096,7 +8089,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -8133,7 +8126,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -8193,7 +8186,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -8258,7 +8251,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -8308,7 +8301,7 @@ export const file = new File([], "empty.txt"); let bridge = "(function() { globalThis._cached = 'yes'; })()"; let (code, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, bridge, "var _saw = _cached;", &mut cache) @@ -8335,7 +8328,7 @@ export const file = new File([], "empty.txt"); { let ctx = isolate::create_context(&mut iso); let (code, _) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, bridge, "", &mut cache) @@ -8350,7 +8343,7 @@ export const file = new File([], "empty.txt"); { let ctx = isolate::create_context(&mut iso); let (code, _) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, bridge, "", &mut cache) @@ -8377,7 +8370,7 @@ export const file = new File([], "empty.txt"); { let ctx = isolate::create_context(&mut iso); let (code, _) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script( @@ -8397,7 +8390,7 @@ export const file = new File([], "empty.txt"); { let ctx = isolate::create_context(&mut iso); let (code, _) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script( @@ -8433,7 +8426,7 @@ export const file = new File([], "empty.txt"); { let ctx = isolate::create_context(&mut iso); let (code, _, _) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module( @@ -8453,7 +8446,7 @@ export const file = new File([], "empty.txt"); { let ctx = isolate::create_context(&mut iso); let (code, exports, _) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module( @@ -8478,7 +8471,7 @@ export const file = new File([], "empty.txt"); let mut cache: Option = None; let (code, _) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_script(scope, "", "var x = 1;", &mut cache) @@ -8544,7 +8537,7 @@ export const file = new File([], "empty.txt"); let user_code = "import { a } from './a.mjs';\nimport { b } from './b.mjs';\nexport const sum = a + b;"; let (code, exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module( @@ -8561,7 +8554,7 @@ export const file = new File([], "empty.txt"); assert!(error.is_none()); let exports = exports.unwrap(); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let val = crate::bridge::deserialize_v8_value(scope, &exports).unwrap(); @@ -8678,7 +8671,7 @@ export const file = new File([], "empty.txt"); let user_code = "import { val } from './dep.mjs';\nexport const result = val;"; let (code, exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module( @@ -8695,7 +8688,7 @@ export const file = new File([], "empty.txt"); assert!(error.is_none()); let exports = exports.unwrap(); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let val = crate::bridge::deserialize_v8_value(scope, &exports).unwrap(); @@ -8780,7 +8773,7 @@ export const file = new File([], "empty.txt"); let user_code = "import { a } from './a.mjs';\nexport const result = a;"; let (code, exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module( @@ -8797,7 +8790,7 @@ export const file = new File([], "empty.txt"); assert!(error.is_none()); let exports = exports.unwrap(); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let val = crate::bridge::deserialize_v8_value(scope, &exports).unwrap(); @@ -8827,7 +8820,7 @@ export const file = new File([], "empty.txt"); let user_code = "export const x = 42;"; let (code, _exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module(scope, &bridge_ctx, "", user_code, None, &mut None) @@ -8848,7 +8841,7 @@ export const file = new File([], "empty.txt"); { let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -8897,7 +8890,7 @@ export const file = new File([], "empty.txt"); { let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -9018,7 +9011,7 @@ export const file = new File([], "empty.txt"); ); let results = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); batch_resolve_via_ipc( @@ -9057,7 +9050,7 @@ export const file = new File([], "empty.txt"); "test-session".into(), ); let capped_result = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); batch_resolve_via_ipc( @@ -9230,7 +9223,7 @@ export const file = new File([], "empty.txt"); let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); let shim = @@ -9245,7 +9238,7 @@ export const file = new File([], "empty.txt"); { let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -9269,7 +9262,7 @@ export const file = new File([], "empty.txt"); { let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -9313,7 +9306,7 @@ export const file = new File([], "empty.txt"); { let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -9357,7 +9350,7 @@ export const file = new File([], "empty.txt"); { let mut iso = isolate::create_isolate(None); let ctx = isolate::create_context(&mut iso); - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); @@ -9450,7 +9443,7 @@ export const file = new File([], "empty.txt"); export const ready = true; "#; let (code, exports, error) = { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); execute_module( @@ -9468,10 +9461,10 @@ export const file = new File([], "empty.txt"); assert!(exports.is_some()); { - let scope = &mut v8::HandleScope::new(&mut iso); + v8::scope!(scope, &mut *iso); let local = v8::Local::new(scope, &ctx); let scope = &mut v8::ContextScope::new(scope, local); - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let source = v8::String::new( tc, "globalThis.__depPromise = globalThis.loadDep().then((value) => { globalThis.__depValue = value; return value; });", diff --git a/crates/executor-v8-runtime/src/guest_bridge_policy.rs b/crates/executor-v8-runtime/src/guest_bridge_policy.rs index ef09acb1fd..25578ff9a1 100644 --- a/crates/executor-v8-runtime/src/guest_bridge_policy.rs +++ b/crates/executor-v8-runtime/src/guest_bridge_policy.rs @@ -73,7 +73,7 @@ fn validate(limits: &GuestBridgeLimits) -> Result<(), String> { } fn limits_from_object( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, object: v8::Local, ) -> Result { let mut values = [0; 4]; @@ -115,7 +115,7 @@ fn limits_from_object( } fn invoke( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, installer: v8::Local, limits: &GuestBridgeLimits, ) -> Result<(), String> { @@ -135,7 +135,7 @@ fn invoke( return Err("bridge policy object installation failed".into()); } } - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let receiver = v8::undefined(tc); if installer .call(tc, receiver.into(), &[object.into()]) @@ -153,7 +153,7 @@ fn invoke( /// Capture/hide and install BEFORE any snapshot userland. The serialized tuple /// contains a V8 function plus exactly four primitives, never a VM id or Global. pub(crate) fn capture_installer<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, ) -> Result>, String> { let context = scope.get_current_context(); let global = context.global(scope); @@ -217,7 +217,7 @@ pub(crate) fn capture_installer<'s>( /// Preserve the existing cold bridge compiler/cache and its original errors. /// The session calls this inside the original watchdog lifecycle. pub(crate) fn prepare( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, supplied: Option<&GuestBridgePolicy>, from_snapshot: bool, bridge_code: &str, @@ -260,7 +260,7 @@ pub(crate) fn prepare( /// Called after bridge bindings are ready, before trusted init/prelude or guest. /// The context slot contains plain Rust values only, with no rooting-handle cycle. pub(crate) fn install( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, supplied: Option<&GuestBridgePolicy>, from_snapshot: bool, bridge_code: &str, @@ -274,7 +274,7 @@ pub(crate) fn install( validate(&expected.limits)?; let context = scope.get_current_context(); if let Some(installed) = context.get_slot::() { - return if installed == &expected { + return if installed.as_ref() == &expected { Ok(()) } else { Err("bridge policy owner or limits changed; create a new execution context".into()) @@ -326,7 +326,10 @@ pub(crate) fn install( // An actual policy-aware bridge must never silently skip installation. return Err("required native bridge policy initializer unavailable".into()); } - if !context.set_slot(expected) { + // The initializer ran guest callbacks above. Preserve any policy installed + // during that call rather than overwriting a newly pinned owner. + if let Some(previous) = context.set_slot(std::rc::Rc::new(expected)) { + drop(context.set_slot(previous)); return Err("bridge policy context state unexpectedly replaced".into()); } Ok(()) diff --git a/crates/executor-v8-runtime/src/isolate.rs b/crates/executor-v8-runtime/src/isolate.rs index d627df7ae0..6113816eff 100644 --- a/crates/executor-v8-runtime/src/isolate.rs +++ b/crates/executor-v8-runtime/src/isolate.rs @@ -15,11 +15,15 @@ static V8_INIT: Once = Once::new(); pub(crate) const MAX_UNHANDLED_PROMISE_REJECTIONS: usize = 1024; unsafe extern "C" { - // rusty_v8 130 does not expose these public V8 embedder hooks in Rust. The + // The Rust bindings do not expose these public V8 embedder hooks. The // build-script C++ shim is compiled against the pinned V8 headers so this // boundary does not depend on platform-specific C++ mangled names. fn agentos_v8_initialize_sandbox_hardware_before_thread_creation(); - fn agentos_v8_set_default_thread_isolation_permissions(); + fn agentos_v8_set_default_thread_isolation_permissions(platform: *const v8::Platform) -> i32; + fn agentos_v8_limit_array_buffer_allocator( + inner: *mut v8::Allocator, + max_bytes: usize, + ) -> *mut v8::Allocator; } // These limits are primitive per-thread ownership metadata, never snapshot or @@ -42,9 +46,9 @@ struct NativeTerminalStack { unsafe extern "C" { fn agentos_v8_plan_terminal_stack(plan: *mut NativeTerminalStackPlan) -> i32; - fn agentos_v8_set_owned_stack_limit(isolate: *mut v8::Isolate, limit: usize); + fn agentos_v8_set_owned_stack_limit(isolate: v8::UnsafeRawIsolatePtr, limit: usize); fn agentos_v8_open_terminal_stack( - isolate: *mut v8::Isolate, + isolate: v8::UnsafeRawIsolatePtr, plan: *const NativeTerminalStackPlan, ) -> i32; } @@ -69,7 +73,7 @@ pub(crate) fn install_native_terminal_stack( isolate.get_slot::().is_none(), "native stack policy installed once before guest entry" ); - unsafe { agentos_v8_set_owned_stack_limit(isolate, plan.ordinary_limit) }; + unsafe { agentos_v8_set_owned_stack_limit(isolate.as_raw_isolate_ptr(), plan.ordinary_limit) }; isolate.set_slot(NativeTerminalStack { plan, thread: thread::current().id(), @@ -80,8 +84,8 @@ pub(crate) fn install_native_terminal_stack( // borrowed live scope/isolate. Restore occurs before its caller can rethrow a // TryCatch termination. No cancellation or checkpoint occurs here. pub(crate) fn with_terminal_stack_margin( - scope: &mut v8::HandleScope, - entry: impl FnOnce(&mut v8::HandleScope) -> T, + scope: &mut v8::PinScope<'_, '_>, + entry: impl FnOnce(&mut v8::PinScope<'_, '_>) -> T, ) -> Result { let registered = scope .get_slot::() @@ -92,7 +96,7 @@ pub(crate) fn with_terminal_stack_margin( ); } let plan = registered.plan; - let isolate: *mut v8::Isolate = &mut ***scope; + let isolate = unsafe { scope.as_raw_isolate_ptr() }; let error = unsafe { agentos_v8_open_terminal_stack(isolate, &plan) }; if error != 0 { return Err(format!( @@ -101,7 +105,7 @@ pub(crate) fn with_terminal_stack_margin( )); } struct Restore { - isolate: *mut v8::Isolate, + isolate: v8::UnsafeRawIsolatePtr, ordinary_limit: usize, } impl Drop for Restore { @@ -430,13 +434,15 @@ pub(crate) fn rejection_limit_error() -> ExecutionError { // The fixed private key cannot be read/forged from guest JavaScript. Only a // reported/overflow marker survives the pending owner, never a strong handle. -fn rejection_marker_key<'s>(scope: &mut v8::HandleScope<'s>) -> Option> { +fn rejection_marker_key<'s>( + scope: &mut v8::PinScope<'s, '_>, +) -> Option> { let name = v8::String::new(scope, "agentos#promise-rejection-state")?; Some(v8::Private::for_api(scope, Some(name))) } pub(crate) fn set_rejection_marker( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, promise: v8::Local, marker: i32, ) -> bool { @@ -485,7 +491,7 @@ pub(crate) fn set_rejection_marker( } fn rejection_marker( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, promise: v8::Local, ) -> Option<(i32, u64)> { let key = rejection_marker_key(scope)?; @@ -502,7 +508,7 @@ fn rejection_marker( } fn current_rejection_marker( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, promise: v8::Local, ) -> Option { let (marker, generation) = rejection_marker(scope, promise)?; @@ -516,7 +522,7 @@ fn current_rejection_marker( const MODULE_EVALUATION_REJECTION_OWNER: i32 = 3; pub(crate) fn claim_module_evaluation_rejection( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, promise: v8::Local, ) -> Result<(), ExecutionError> { let failed = || ExecutionError { @@ -552,7 +558,7 @@ pub(crate) fn claim_module_evaluation_rejection( } extern "C" fn promise_reject_callback(msg: v8::PromiseRejectMessage) { - let scope = &mut unsafe { v8::CallbackScope::new(&msg) }; + v8::callback_scope!(unsafe scope, &msg); let promise = msg.get_promise(); match msg.get_event() { v8::PromiseRejectEvent::PromiseRejectWithNoHandler => { @@ -603,7 +609,7 @@ extern "C" fn promise_reject_callback(msg: v8::PromiseRejectMessage) { } } -pub fn configure_isolate(isolate: &mut v8::OwnedIsolate) { +pub fn configure_isolate(isolate: &mut v8::Isolate) { isolate.set_slot(PromiseRejectState::default()); isolate.set_slot(crate::terminal_callback::TerminalCallbackOwner::default()); isolate.set_promise_reject_callback(promise_reject_callback); @@ -614,36 +620,23 @@ pub fn configure_isolate(isolate: &mut v8::OwnedIsolate) { /// background compilation while keeping the trusted thread census bounded. const V8_PLATFORM_WORKER_THREADS: u32 = 4; -/// Opt-in env knob for Wasm exnref exception handling (`try_table`, -/// `throw_ref`), which Rust `panic=unwind` Wasm uses. V8 turns exnref on by -/// default from 13.7 (Chrome 137, workerd): -/// https://github.com/v8/v8/blob/33e7739c134d24c94fdda02ab631eefed6bd18df/src/wasm/wasm-feature-flags.h#L131-L135 -/// The pinned V8 13.0 never shipped it and lacks the later exnref fixes, so -/// it is opt-in for the JavaScript runtime. The WASM executor enables it by -/// default for toolchain compatibility. The setting applies to every VM in -/// the process. The snapshot helper process inherits this environment, -/// so snapshots are built and restored with the same flags. -const WASM_EXNREF_ENV: &str = "AGENTOS_V8_WASM_EXNREF"; - -pub(crate) fn wasm_exnref_enabled() -> bool { - static ENABLED: std::sync::OnceLock = std::sync::OnceLock::new(); - *ENABLED.get_or_init(|| match std::env::var_os(WASM_EXNREF_ENV) { - None => cfg!(feature = "wasm-exnref"), - Some(value) if value == "1" => true, - Some(value) if value == "0" => false, - Some(value) => { - tracing::warn!( - "ignoring {WASM_EXNREF_ENV}={value:?}: set 1 to enable Wasm exnref or 0 to disable it" - ); - cfg!(feature = "wasm-exnref") - } - }) +/// V8 15.8 includes finalized Wasm exception references unconditionally. +/// Refuse an explicit opt-out before any guest can start. +pub(crate) fn validate_v8_feature_configuration() -> std::io::Result<()> { + if std::env::var_os("AGENTOS_V8_WASM_EXNREF").is_some_and(|value| value != "1") { + return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, + "ERR_AGENTOS_UNSUPPORTED_V8_OPTION: V8 always supports Wasm exception references; unset AGENTOS_V8_WASM_EXNREF or set it to 1")); + } + Ok(()) } /// Initialize the V8 platform (once per process). /// Safe to call multiple times; only the first call takes effect. pub fn init_v8_platform() { V8_INIT.call_once(|| { + // The public runtime constructors validate first and return InvalidInput. + // Direct infallible low-level isolate constructors must fail closed too. + validate_v8_feature_configuration().unwrap_or_else(|error| panic!("{error}")); // V8 requires sandbox hardware keys to be allocated before any thread // which may access its sandbox is created. This call precedes both the // platform owner below and V8's own default-platform workers. @@ -658,27 +651,20 @@ pub fn init_v8_platform() { thread::Builder::new() .name(String::from("agentos-v8-platform")) .spawn(move || { - v8::icu::set_common_data_74(&ICU_COMMON_DATA.0) + v8::icu::set_common_data_78(&ICU_COMMON_DATA.0) .expect("failed to initialize V8 ICU common data"); - // V8 reads flags once, before initialization. - if wasm_exnref_enabled() { - v8::V8::set_flags_from_string("--experimental-wasm-exnref"); - } - // Native lifetime tests use V8's deterministic full-GC test API. #[cfg(test)] v8::V8::set_flags_from_string("--expose_gc"); let platform = v8::new_default_platform(V8_PLATFORM_WORKER_THREADS, false).make_shared(); - // Deno's embedding layer also owns cppgc initialization and - // records whether V8 has started. Initialize it on this same - // process-lifetime owner before either kind of isolate runs. - #[cfg(feature = "obscura-embedding")] - deno_core::JsRuntime::init_platform(Some(platform), false); - #[cfg(not(feature = "obscura-embedding"))] - { - v8::V8::initialize_platform(platform); - v8::V8::initialize(); - } + // Independently generated guest snapshots share one process-wide + // read-only heap. Preserve the built-in layout in every blob. + v8::V8::set_flags_from_string("--no-extensible-ro-snapshot"); + // A disposed guest releases its pages. Live isolates can still + // reuse pages locally for the rest of their own lifetime. + v8::V8::set_flags_from_string("--no-memory-pool-share-memory-on-teardown"); + v8::V8::initialize_platform(platform); + v8::V8::initialize(); ready_tx .send(()) .expect("V8 platform initializer lost its caller"); @@ -706,7 +692,9 @@ pub fn init_v8_platform() { /// before the thread first enters V8. pub fn prepare_current_thread() { init_v8_platform(); - unsafe { agentos_v8_set_default_thread_isolation_permissions() }; + let platform = v8::V8::get_current_platform(); + let status = unsafe { agentos_v8_set_default_thread_isolation_permissions(&*platform) }; + assert_eq!(status, 0, "ERR_AGENTOS_V8_THREAD_PERMISSIONS: could not install the isolate thread protection key: {}", std::io::Error::from_raw_os_error(status)); } // Headroom granted to V8 above the current heap size when the near-heap-limit @@ -720,7 +708,7 @@ const NEAR_HEAP_LIMIT_HEADROOM_BYTES: usize = 16 * 1024 * 1024; /// State the near-heap-limit callback needs for one isolate. struct HeapLimitGuard { handle: v8::IsolateHandle, - isolate: *mut v8::Isolate, + isolate: v8::UnsafeRawIsolatePtr, exceeded: Arc, } @@ -734,7 +722,7 @@ impl Drop for HeapLimitGuardRegistration { // isolate is still alive, but its annex/IsolateHandle is already // invalidated. Remove the callback directly before freeing its data. // A zero limit avoids changing the heap limit during teardown. - unsafe { &mut *self.guard.isolate } + unsafe { v8::Isolate::from_raw_isolate_ptr(self.guard.isolate) } .remove_near_heap_limit_callback(near_heap_limit_callback, 0); } } @@ -789,8 +777,8 @@ extern "C" fn near_heap_limit_callback( // One large allocation (for example a 256 MiB string) can take the heap // far past the limit before this callback runs, so size the new limit // from the heap, not from the old limit. - let mut statistics = v8::HeapStatistics::default(); - unsafe { (*guard.isolate).get_heap_statistics(&mut statistics) }; + let statistics = + unsafe { v8::Isolate::from_raw_isolate_ptr(guard.isolate) }.get_heap_statistics(); heap_size = statistics.total_heap_size(); } warn_limit_exhausted( @@ -813,7 +801,7 @@ extern "C" fn near_heap_limit_callback( /// /// Must be called for every isolate created with a non-`None` heap limit, /// regardless of whether it was built fresh or restored from a snapshot. -pub fn install_heap_limit_guard(isolate: &mut v8::OwnedIsolate) { +pub fn install_heap_limit_guard(isolate: &mut v8::Isolate) { if isolate.get_slot::().is_some() { return; } @@ -821,7 +809,7 @@ pub fn install_heap_limit_guard(isolate: &mut v8::OwnedIsolate) { isolate.set_slot(HeapLimitExceeded(Arc::clone(&exceeded))); let mut guard = Box::new(HeapLimitGuard { handle: isolate.thread_safe_handle(), - isolate: &mut **isolate, + isolate: unsafe { isolate.as_raw_isolate_ptr() }, exceeded, }); let data = guard.as_mut() as *mut HeapLimitGuard as *mut c_void; @@ -841,6 +829,10 @@ pub fn install_heap_limit_guard(isolate: &mut v8::OwnedIsolate) { /// V8 allocates WebAssembly memory and resizable buffers through its page /// allocator instead, so this budget does not count them. struct ArrayBufferBudget { + // V8 retains its shared allocator on the native isolate through disposal. + // Keep startup bytes and external source resources tied to that identity, + // even when a Rust isolate handle moves or disposal unwinds. + snapshot: Option, used: AtomicUsize, limit: usize, warned: AtomicBool, @@ -903,26 +895,6 @@ unsafe extern "C" fn budget_free(budget: &ArrayBufferBudget, data: *mut c_void, budget.release(len); } -unsafe extern "C" fn budget_reallocate( - budget: &ArrayBufferBudget, - data: *mut c_void, - old_len: usize, - new_len: usize, -) -> *mut c_void { - if new_len > old_len && !budget.reserve(new_len - old_len) { - return std::ptr::null_mut(); - } - let resized = unsafe { libc::realloc(data, new_len.max(1)) }; - if resized.is_null() { - if new_len > old_len { - budget.release(new_len - old_len); - } - } else if new_len < old_len { - budget.release(old_len - new_len); - } - resized -} - unsafe extern "C" fn budget_drop(budget: *const ArrayBufferBudget) { drop(unsafe { Arc::from_raw(budget) }); } @@ -932,15 +904,18 @@ static ARRAY_BUFFER_BUDGET_VTABLE: v8::RustAllocatorVtable = allocate: budget_allocate, allocate_uninitialized: budget_allocate_uninitialized, free: budget_free, - reallocate: budget_reallocate, drop: budget_drop, }; /// Create params whose V8 heap and, separately, ArrayBuffer memory are each /// capped at `limit_mb`. -fn budgeted_create_params(limit_mb: u32) -> (v8::CreateParams, Arc) { +fn budgeted_create_params( + limit_mb: u32, + snapshot: Option, +) -> (v8::CreateParams, Arc) { let limit_bytes = (limit_mb as usize) * 1024 * 1024; let budget = Arc::new(ArrayBufferBudget { + snapshot, used: AtomicUsize::new(0), limit: limit_bytes, warned: AtomicBool::new(false), @@ -953,28 +928,63 @@ fn budgeted_create_params(limit_mb: u32) -> (v8::CreateParams, Arc unsafe { blob.apply_to_create_params(params) }, + None => params, + }; (params, budget) } /// Bounded create params for an isolate that is not a guest session isolate, /// such as the snapshot creator in the helper process. pub(crate) fn bounded_create_params(limit_mb: u32) -> v8::CreateParams { - budgeted_create_params(limit_mb).0 + budgeted_create_params(limit_mb, None).0 +} + +/// A thread-affine guest isolate. Keep ownership private so callers cannot +/// convert it to a shared isolate and bypass the session lifecycle. +pub struct GuestIsolate { + isolate: v8::OwnedIsolate, +} + +impl std::ops::Deref for GuestIsolate { + type Target = v8::Isolate; + fn deref(&self) -> &Self::Target { + &self.isolate + } } -/// Create a guest isolate, fresh or snapshot-restored (`customize` adds the -/// snapshot parameters). The heap limit in MB (`None` applies +impl std::ops::DerefMut for GuestIsolate { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.isolate + } +} + +/// Create a fresh or snapshot-restored guest isolate. The heap limit in MB +/// (`None` applies /// `DEFAULT_HEAP_LIMIT_MB`) caps the V8 heap and, separately, the isolate's /// ArrayBuffer memory. pub(crate) fn create_guest_isolate( heap_limit_mb: Option, - customize: impl FnOnce(v8::CreateParams) -> v8::CreateParams, -) -> v8::OwnedIsolate { - let (params, budget) = budgeted_create_params(heap_limit_mb.unwrap_or(DEFAULT_HEAP_LIMIT_MB)); - let params = customize(params); + snapshot: Option, +) -> GuestIsolate { + let (params, budget) = + budgeted_create_params(heap_limit_mb.unwrap_or(DEFAULT_HEAP_LIMIT_MB), snapshot); // The existing constructor is infallible: reject unsupported/undersized // native stacks before creating an isolate, never from a guest callback. let terminal_stack = plan_native_terminal_stack() @@ -990,16 +1000,16 @@ pub(crate) fn create_guest_isolate( install_heap_limit_guard(&mut isolate); crate::terminal_callback::initialize_delivery_key(&mut isolate); isolate.set_slot(budget); - isolate + GuestIsolate { isolate } } /// Create a new V8 isolate with an optional heap limit in MB. `None` applies the /// bounded-by-default cap (`DEFAULT_HEAP_LIMIT_MB`) — an isolate is NEVER created /// with an unbounded heap, so a guest heap bomb terminates its own isolate rather /// than fatal-aborting the shared process. -pub fn create_isolate(heap_limit_mb: Option) -> v8::OwnedIsolate { +pub fn create_isolate(heap_limit_mb: Option) -> GuestIsolate { prepare_current_thread(); - create_guest_isolate(heap_limit_mb, |params| params) + create_guest_isolate(heap_limit_mb, None) } /// State the external-memory GC check needs for one isolate. @@ -1010,7 +1020,7 @@ struct ExternalMemoryGuard { } struct ExternalMemoryGuardRegistration { - isolate: *mut v8::Isolate, + isolate: v8::UnsafeRawIsolatePtr, guard: Box, } @@ -1019,7 +1029,7 @@ impl Drop for ExternalMemoryGuardRegistration { let data = self.guard.as_mut() as *mut ExternalMemoryGuard as *mut c_void; // As with the heap guard, slot teardown precedes native V8 disposal. // Keep the callback's Box alive until removal of this exact data pointer. - unsafe { &mut *self.isolate } + unsafe { v8::Isolate::from_raw_isolate_ptr(self.isolate) } .remove_gc_prologue_callback(external_memory_gc_callback, data); } } @@ -1036,7 +1046,7 @@ impl Drop for ExternalMemoryGuardRegistration { /// Install this only for guest JavaScript sessions. Python (Pyodide) and the /// WASM runner keep their own program memory in WebAssembly and bound it with /// their own limits. Calling it again on the same isolate does nothing. -pub fn install_external_memory_guard(isolate: &mut v8::OwnedIsolate) { +pub fn install_external_memory_guard(isolate: &mut v8::Isolate) { if isolate .get_slot::() .is_some() @@ -1060,9 +1070,9 @@ pub fn install_external_memory_guard(isolate: &mut v8::OwnedIsolate) { warned: AtomicBool::new(false), }); let data = guard.as_mut() as *mut ExternalMemoryGuard as *mut c_void; - isolate.add_gc_prologue_callback(external_memory_gc_callback, data, v8::GCType::ALL); + isolate.add_gc_prologue_callback(external_memory_gc_callback, data, v8::GCType::kGCTypeAll); let registration = ExternalMemoryGuardRegistration { - isolate: &mut **isolate, + isolate: unsafe { isolate.as_raw_isolate_ptr() }, guard, }; isolate.set_slot(registration); @@ -1080,8 +1090,7 @@ pub(crate) fn admit_external_allocation( .cloned() .ok_or("Uint8Array encoding requires the VM external-memory budget")?; let outside = |isolate: &mut v8::Isolate| { - let mut statistics = v8::HeapStatistics::default(); - isolate.get_heap_statistics(&mut statistics); + let statistics = isolate.get_heap_statistics(); statistics .external_memory() .saturating_sub(budget.used.load(Ordering::Acquire)) @@ -1106,7 +1115,7 @@ pub(crate) fn admit_external_allocation( } extern "C" fn external_memory_gc_callback( - isolate: *mut v8::Isolate, + isolate: v8::UnsafeRawIsolatePtr, _gc_type: v8::GCType, _flags: v8::GCCallbackFlags, data: *mut c_void, @@ -1114,9 +1123,8 @@ extern "C" fn external_memory_gc_callback( // Safety: the registration owns this stable Box and unregisters the callback // before freeing it. V8 runs GC prologue callbacks on the isolate's thread. let guard = unsafe { &*(data as *const ExternalMemoryGuard) }; - let isolate = unsafe { &mut *isolate }; - let mut statistics = v8::HeapStatistics::default(); - isolate.get_heap_statistics(&mut statistics); + let mut isolate = unsafe { v8::Isolate::from_raw_isolate_ptr(isolate) }; + let statistics = isolate.get_heap_statistics(); // External memory also holds the non-shared ArrayBuffers the budget already // counts, so only the remainder is checked here. let outside_budget = statistics @@ -1133,7 +1141,7 @@ extern "C" fn external_memory_gc_callback( } } -pub fn drop_isolate(isolate: Option) { +pub fn drop_isolate(isolate: Option) { if let Some(mut isolate) = isolate { crate::bridge::release_vm_context_registry(&mut isolate); drop(isolate); @@ -1142,8 +1150,8 @@ pub fn drop_isolate(isolate: Option) { /// Create a new V8 context on the given isolate. /// Returns a Global handle so the context can be reused across scopes. -pub fn create_context(isolate: &mut v8::OwnedIsolate) -> v8::Global { - let scope = &mut v8::HandleScope::new(isolate); +pub fn create_context(isolate: &mut v8::Isolate) -> v8::Global { + v8::scope!(let scope, isolate); let context = v8::Context::new(scope, Default::default()); v8::Global::new(scope, context) } @@ -1229,10 +1237,10 @@ mod memory_budget_tests { // Exercise the registered callback without allocating to the heap limit. let limit = 8 * 1024 * 1024; let (headroom, rejected, terminated) = { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); - let scope = &mut v8::TryCatch::new(scope); + v8::tc_scope!(let scope, scope); let code = v8::String::new(scope, "for (let i = 0; i < 4; i++) {} 42").unwrap(); let script = v8::Script::compile(scope, code, None).unwrap(); let headroom = near_heap_limit_callback(data, limit, limit); @@ -1262,10 +1270,10 @@ mod memory_budget_tests { install_external_memory_guard(&mut isolate); install_external_memory_guard(&mut isolate); let (rejected, terminated, external_bytes) = { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); - let scope = &mut v8::TryCatch::new(scope); + v8::tc_scope!(let scope, scope); let code = v8::String::new(scope, "for (let i = 0; i < 4; i++) {} 42").unwrap(); let script = v8::Script::compile(scope, code, None).unwrap(); // Host-provided backing stores are outside the custom allocator's @@ -1274,8 +1282,7 @@ mod memory_budget_tests { let backing = v8::ArrayBuffer::new_backing_store_from_bytes(vec![0u8; 9 * 1024 * 1024]); let buffer = v8::ArrayBuffer::with_backing_store(scope, &backing.make_shared()); scope.low_memory_notification(); - let mut statistics = v8::HeapStatistics::default(); - scope.get_heap_statistics(&mut statistics); + let statistics = scope.get_heap_statistics(); let rejected = script.run(scope).is_none(); let terminated = scope.has_terminated(); // Referencing the handle after GC makes its intended lifetime clear. @@ -1324,7 +1331,7 @@ mod memory_budget_tests { fn guest_array_buffer_over_budget_throws_without_killing_isolate() { let mut isolate = create_isolate(Some(8)); { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new( @@ -1353,8 +1360,9 @@ mod memory_budget_tests { } #[test] - fn allocator_rejected_growth_preserves_existing_allocation_and_budget() { + fn allocator_rejection_preserves_existing_allocation_and_budget() { let budget = ArrayBufferBudget { + snapshot: None, used: AtomicUsize::new(0), limit: 32, warned: AtomicBool::new(false), @@ -1362,12 +1370,13 @@ mod memory_budget_tests { unsafe { let allocation = budget_allocate(&budget, 16); assert!(!allocation.is_null()); - assert!(budget_reallocate(&budget, allocation, 16, 33).is_null()); + assert!(budget_allocate(&budget, 17).is_null()); assert_eq!(budget.used.load(Ordering::Acquire), 16); - let allocation = budget_reallocate(&budget, allocation, 16, 8); - assert!(!allocation.is_null()); - assert_eq!(budget.used.load(Ordering::Acquire), 8); - budget_free(&budget, allocation, 8); + let second = budget_allocate(&budget, 8); + assert!(!second.is_null()); + assert_eq!(budget.used.load(Ordering::Acquire), 24); + budget_free(&budget, second, 8); + budget_free(&budget, allocation, 16); assert_eq!(budget.used.load(Ordering::Acquire), 0); } } diff --git a/crates/executor-v8-runtime/src/javascript.rs b/crates/executor-v8-runtime/src/javascript.rs index c36c9a65b1..de24ec47fb 100644 --- a/crates/executor-v8-runtime/src/javascript.rs +++ b/crates/executor-v8-runtime/src/javascript.rs @@ -825,6 +825,7 @@ struct LocalBridgeState { next_timer_id: u64, timers: Arc>>, kernel_stdin: Arc, + pending_stdin_read: Option, forward_kernel_stdin_rpc: bool, kernel_stdio: bool, v8_session: Option, @@ -861,6 +862,7 @@ impl Default for LocalBridgeState { next_timer_id: 0, timers: Arc::new(Mutex::new(HashMap::new())), kernel_stdin: Arc::new(LocalKernelStdinBridge::default()), + pending_stdin_read: None, forward_kernel_stdin_rpc: false, kernel_stdio: false, v8_session: None, @@ -888,6 +890,7 @@ impl Drop for LocalBridgeState { /// finds its entry gone and suppresses its callback via `timer_should_fire`, /// so a destroyed session's timers do not fire after the fact. fn drop(&mut self) { + self.kernel_stdin.close(); if let Some(wheel) = JAVASCRIPT_TIMER_WHEEL.get() { wheel.cancel_registry(&self.timers); } @@ -901,12 +904,23 @@ impl Drop for LocalBridgeState { struct LocalKernelStdinBridge { state: Mutex, ready: Condvar, + async_ready: Notify, } #[derive(Debug, Default)] struct LocalKernelStdinState { bytes: VecDeque, closed: bool, + generation: u64, +} + +#[derive(Clone, Copy)] +struct LocalStdinRead { + call_id: u64, + generation: u64, + max_bytes: usize, + deadline: Option, + python_raw: bool, } #[derive(Debug, Clone, Default)] @@ -4063,7 +4077,11 @@ impl JavascriptExecutionEngine { build_v8_user_code(&guest_entrypoint, &request.env) }; let runtime_shim = build_v8_runtime_shim( - &guest_entrypoint, + RuntimeEntrypoint { + path: &guest_entrypoint, + commonjs_main: !use_module_mode + && !matches!(guest_entrypoint.as_str(), "-e" | "--eval"), + }, &process_argv, request.argv0.as_deref(), translator.guest_cwd(), @@ -4512,11 +4530,7 @@ fn build_v8_user_code(entrypoint: &str, env: &BTreeMap) -> Strin } else { // Module entrypoint - load it as Node's main CommonJS module so // require.main, module.parent, and process.mainModule are coherent. - format!( - "_moduleModule._load({}, null, true);\n//# sourceURL={}", - serde_json::to_string(entrypoint).unwrap_or_else(|_| format!("\"{}\"", entrypoint)), - entrypoint - ) + format!("globalThis.__agentOsRunCommonJsMain();\n//# sourceURL={entrypoint}") } } @@ -4847,11 +4861,16 @@ fn resolve_v8_entrypoint(cwd: &Path, entrypoint: &str) -> String { // Keep each injected process/runtime value explicit at this one serialization // boundary; grouping them would duplicate the already-typed guest config. #[allow(clippy::too_many_arguments)] +struct RuntimeEntrypoint<'a> { + path: &'a str, + commonjs_main: bool, +} + /// The per-process classic script that sets up `process` (argv, env, identity) /// before guest code runs. It travels in the Execute frame's `runtime_shim` /// field, separate from the guest's own code. fn build_v8_runtime_shim( - entrypoint: &str, + entrypoint: RuntimeEntrypoint<'_>, argv: &[String], argv0: Option<&str>, cwd: &str, @@ -4868,7 +4887,7 @@ fn build_v8_runtime_shim( let argv0_json = serde_json::to_string(&argv0.unwrap_or("node")) .unwrap_or_else(|_| String::from("\"node\"")); let entry_json = - serde_json::to_string(entrypoint).unwrap_or_else(|_| String::from("\"/\"")); + serde_json::to_string(entrypoint.path).unwrap_or_else(|_| String::from("\"/\"")); let cwd_json = serde_json::to_string(cwd).unwrap_or_else(|_| String::from("\"/\"")); let env_json = serde_json::to_string(env).unwrap_or_else(|_| String::from("{}")); // Virtual process identity object. `Option` fields serialize to `null`, which @@ -4903,6 +4922,7 @@ fn build_v8_runtime_shim( .to_string(); let high_resolution_time = guest_runtime.high_resolution_time; let kernel_ipc = guest_runtime.kernel_ipc; + let run_commonjs_main = entrypoint.commonjs_main; format!( r#"(function () {{ @@ -4916,6 +4936,9 @@ fn build_v8_runtime_shim( const nextArgv = {argv_json}; const nextArgv0 = {argv0_json}; const entryFile = {entry_json}; + const createRequire = globalThis.__agentOsGuestCreateRequire; + const mainLoader = {run_commonjs_main} ? globalThis._moduleModule : undefined; + const requireFrom = globalThis._requireFrom; const nextCwd = {cwd_json}; const nextEnv = {env_json}; const nextHighResolutionTime = {high_resolution_time}; @@ -4989,7 +5012,7 @@ fn build_v8_runtime_shim( const originalProcessBinding = process.binding.bind(process); let constantsBinding = null; try {{ - const bindingRequire = globalThis._moduleModule?.createRequire?.( + const bindingRequire = createRequire?.( nextCwd === "/" ? "/__agentos_runtime__.js" : `${{nextCwd.replace(/\/+$/, "")}}/__agentos_runtime__.js`, @@ -5094,7 +5117,7 @@ fn build_v8_runtime_shim( if ( typeof globalThis.require === "undefined" && - typeof globalThis._moduleModule?.createRequire === "function" + typeof createRequire === "function" ) {{ const requireEntryFile = entryFile === "-e" || entryFile === "--eval" @@ -5103,7 +5126,23 @@ fn build_v8_runtime_shim( : `${{nextCwd.replace(/\/+$/, "")}}/__agentos_eval__.js` : entryFile; globalThis.require = - globalThis._moduleModule.createRequire(requireEntryFile); + createRequire(requireEntryFile); + }} + + // Install the builtin allow-list gate consulted by the bridge's + // rejectRestrictedBuiltinRequest (covers require + ESM builtin loads). Present + // for non-node platforms (empty => deny all) and node + explicit allow-list; + // absent => unrestricted (node default). + {{ + const __builtinAllowRaw = nextEnv.AGENTOS_JS_BUILTIN_ALLOWLIST; + if (typeof __builtinAllowRaw === "string") {{ + let __allowList = []; + try {{ __allowList = JSON.parse(__builtinAllowRaw); }} catch (_e) {{ __allowList = []; }} + if (typeof globalThis.__agentOSInitJsRuntime === "function") {{ + globalThis.__agentOSInitJsRuntime(Array.isArray(__allowList) ? __allowList : []); + }} + try {{ delete globalThis.__agentOSInitJsRuntime; }} catch (_e) {{}} + }} }} if (typeof globalThis.require === "function") {{ @@ -5140,33 +5179,35 @@ fn build_v8_runtime_shim( ? "/__agentos_preload__.js" : `${{nextCwd.replace(/\/+$/, "")}}/__agentos_preload__.js`; const preloadRequire = - typeof globalThis._moduleModule?.createRequire === "function" - ? globalThis._moduleModule.createRequire(preloadBase) + typeof createRequire === "function" + ? createRequire(preloadBase) : globalThis.require; for (const preloadModule of preloadModules) {{ preloadRequire(preloadModule); }} }} + if ({run_commonjs_main}) {{ + let mainLoaded = false; + Object.defineProperty(globalThis, "__agentOsRunCommonJsMain", {{ + configurable: true, + enumerable: false, + writable: false, + value: () => {{ + if (mainLoaded) return; + mainLoaded = true; + delete globalThis.__agentOsRunCommonJsMain; + if (mainLoader) return mainLoader._load(entryFile, null, true); + // The builtin gate may already have removed the public module alias. + return requireFrom(entryFile, "/", true); + }}, + }}); + }} + // jsRuntime platform tiering: the guest JS host surface is baked into the // shared V8 snapshot, so non-node platforms are produced by subtractively // scrubbing baked globals here, per execution. const __jsPlatform = nextEnv.AGENTOS_JS_PLATFORM || "node"; - // Install the builtin allow-list gate consulted by the bridge's - // rejectRestrictedBuiltinRequest (covers require + ESM builtin loads). Present - // for non-node platforms (empty => deny all) and node + explicit allow-list; - // absent => unrestricted (node default). - {{ - const __builtinAllowRaw = nextEnv.AGENTOS_JS_BUILTIN_ALLOWLIST; - if (typeof __builtinAllowRaw === "string") {{ - let __allowList = []; - try {{ __allowList = JSON.parse(__builtinAllowRaw); }} catch (_e) {{ __allowList = []; }} - if (typeof globalThis.__agentOSInitJsRuntime === "function") {{ - globalThis.__agentOSInitJsRuntime(Array.isArray(__allowList) ? __allowList : []); - }} - try {{ delete globalThis.__agentOSInitJsRuntime; }} catch (_e) {{}} - }} - }} if (__jsPlatform !== "node") {{ const __dropGlobal = (name) => {{ try {{ delete globalThis[name]; }} catch (_e) {{}} @@ -5228,6 +5269,32 @@ fn build_v8_runtime_shim( ) } +fn send_local_bridge_reply( + session: &V8SessionHandle, + call_id: u64, + reply: Result, +) -> bool { + let encoded = match reply { + Ok(value) => v8_runtime::json_to_cbor_payload(&value) + .map(|payload| (0, payload)) + .map_err(|error| error.to_string()), + Err(error) => encode_host_service_error_payload(&error) + .map(|payload| (1, payload)) + .map_err(|error| error.to_string()), + }; + let (status, payload) = match encoded { + Ok(encoded) => encoded, + Err(error) => { + terminate_session_after_bridge_encoding_failure(session, call_id, &error); + return false; + } + }; + if let Err(error) = session.send_bridge_response(call_id, status, payload) { + eprintln!("INFO_AGENTOS_STALE_BRIDGE_COMPLETION: call_id={call_id} error={error}"); + } + true +} + /// Spawn a supervised task on the process runtime that converts V8 BinaryFrame /// messages into JavascriptExecutionEvent values for the sidecar event loop. /// @@ -5257,7 +5324,18 @@ fn spawn_v8_event_bridge( loop { let local_bridge = bridge_state.as_mut().expect("active event bridge owns local state"); let frame_recv_start = Instant::now(); - let Ok(frame) = frame_receiver.recv_async().await else { + let pending_read = local_bridge.pending_stdin_read; + let frame_result = tokio::select! { + frame = frame_receiver.recv_async() => frame, + (call_id, reply) = local_bridge.kernel_stdin.wait_for_pending_read(pending_read) => { + local_bridge.pending_stdin_read = None; + if !send_local_bridge_reply(&v8_session, call_id, reply) { + break; + } + continue; + } + }; + let Ok(frame) = frame_result else { break; }; let frame_recv_wait = frame_recv_start.elapsed(); @@ -5328,43 +5406,13 @@ fn spawn_v8_event_bridge( if let Some(response) = local_bridge.handle_internal_bridge_call(call_id, &method, &args) { - let encoded = match response { - LocalBridgeCallResult::Immediate(response) => { - match v8_runtime::json_to_cbor_payload(&response) { - Ok(payload) => (0, payload), - Err(error) => { - terminate_session_after_bridge_encoding_failure( - &v8_session, - call_id, - &error.to_string(), - ); - break; - } - } - } - LocalBridgeCallResult::Error(error) => { - match encode_host_service_error_payload(&error) { - Ok(payload) => (1, payload), - Err(error) => { - terminate_session_after_bridge_encoding_failure( - &v8_session, - call_id, - &error.to_string(), - ); - break; - } - } - } + let response = match response { + LocalBridgeCallResult::Immediate(value) => Ok(value), + LocalBridgeCallResult::Error(error) => Err(error), LocalBridgeCallResult::Deferred => continue, }; - if let Err(error) = v8_session.send_bridge_response( - call_id, - encoded.0, - encoded.1, - ) { - eprintln!( - "INFO_AGENTOS_STALE_BRIDGE_COMPLETION: call_id={call_id} error={error}" - ); + if !send_local_bridge_reply(&v8_session, call_id, response) { + break; } continue; } @@ -5960,8 +6008,24 @@ impl LocalBridgeState { self.handle_polyfill_dispatch(args), )), "_cryptoRandomFill" => { - let size = args.first().and_then(Value::as_u64).unwrap_or(16) as usize; - let mut bytes = vec![0u8; size]; + // This host primitive supplies one Web Crypto entropy chunk. + // Validate at the trust boundary: guests can call it directly, + // bypassing the JavaScript getRandomValues size check. + let size = match args.first().and_then(Value::as_u64) { + Some(size) if size <= 65_536 => size as usize, + _ => return Some(LocalBridgeCallResult::Error(HostServiceError::new( + "ERR_OUT_OF_RANGE", + "_cryptoRandomFill requires an integer byte count from 0 through 65536; request larger random values in chunks", + ))), + }; + let mut bytes = Vec::new(); + if bytes.try_reserve_exact(size).is_err() { + return Some(LocalBridgeCallResult::Error(HostServiceError::new( + "ENOMEM", + "unable to allocate the requested entropy chunk", + ))); + } + bytes.resize(size, 0); if let Err(error) = getrandom(&mut bytes) { return Some(LocalBridgeCallResult::Error(HostServiceError::new( "ERR_AGENTOS_ENTROPY_UNAVAILABLE", @@ -6009,12 +6073,9 @@ impl LocalBridgeState { Some(LocalBridgeCallResult::Immediate(Value::Bool(false))) } "_kernelStdinRead" | "_kernelStdinReadRaw" if self.forward_kernel_stdin_rpc => None, - "_kernelStdinRead" | "_kernelStdinReadRaw" => Some(LocalBridgeCallResult::Immediate( - self.kernel_stdin.read(args), - )), - "_pythonStdinRead" => Some(LocalBridgeCallResult::Immediate( - self.kernel_stdin.read_python_raw(args), - )), + "_kernelStdinRead" | "_kernelStdinReadRaw" | "_pythonStdinRead" => { + Some(self.begin_stdin_read(call_id, args, method == "_pythonStdinRead")) + } "_scheduleTimer" => { self.schedule_bridge_timer_response(call_id, timer_delay_ms(args.first())); Some(LocalBridgeCallResult::Deferred) @@ -7136,11 +7197,90 @@ fn hex_digit(byte: u8) -> Option { } } +impl LocalBridgeState { + fn begin_stdin_read( + &mut self, + call_id: u64, + args: &[Value], + python_raw: bool, + ) -> LocalBridgeCallResult { + if self.pending_stdin_read.is_some() { + return LocalBridgeCallResult::Error(HostServiceError::new( + "EBUSY", + "a local stdin read is already pending", + )); + } + let mut state = self + .kernel_stdin + .state + .lock() + .expect("kernel stdin state poisoned"); + let timeout = if !python_raw && args.get(1).is_some_and(Value::is_null) { + None + } else { + let millis = args.get(1).and_then(Value::as_u64).unwrap_or(100); + if millis > MAX_TIMER_DELAY_MS { + return LocalBridgeCallResult::Error(HostServiceError::new( + "EINVAL", + format!("local stdin timeout exceeds {MAX_TIMER_DELAY_MS} milliseconds"), + )); + } + Some(millis) + }; + let read = LocalStdinRead { + call_id, + generation: state.generation, + max_bytes: stdin_read_max_bytes(args), + deadline: timeout.map(|millis| time::Instant::now() + Duration::from_millis(millis)), + python_raw, + }; + if let Some(value) = state.take_readable(read.max_bytes, python_raw) { + return LocalBridgeCallResult::Immediate(value); + } + if timeout == Some(0) { + return LocalBridgeCallResult::Immediate(Value::Null); + } + self.pending_stdin_read = Some(read); + LocalBridgeCallResult::Deferred + } +} + +fn stdin_read_max_bytes(args: &[Value]) -> usize { + args.first() + .and_then(Value::as_u64) + .map(|value| value.clamp(1, 64 * 1024) as usize) + .unwrap_or(64 * 1024) +} + +impl LocalKernelStdinState { + fn take_readable(&mut self, max_bytes: usize, python_raw: bool) -> Option { + if !self.bytes.is_empty() { + let read_len = self.bytes.len().min(max_bytes); + let bytes = self.bytes.drain(..read_len).collect::>(); + let data = v8_runtime::base64_encode_pub(&bytes); + return Some(if python_raw { + Value::String(data) + } else { + json!({"dataBase64": data}) + }); + } + self.closed.then(|| { + if python_raw { + json!("__AGENTOS_PYTHON_STDIN_DONE__") + } else { + json!({"done": true}) + } + }) + } +} + impl LocalKernelStdinBridge { fn reset(&self) { let mut state = self.state.lock().expect("kernel stdin state poisoned"); state.bytes.clear(); state.closed = false; + state.generation = state.generation.wrapping_add(1); + self.async_ready.notify_waiters(); } fn write(&self, chunk: &[u8]) -> Result<(), JavascriptExecutionError> { @@ -7163,6 +7303,7 @@ impl LocalKernelStdinBridge { state.bytes.extend(chunk.iter().copied()); self.ready.notify_all(); + self.async_ready.notify_waiters(); Ok(()) } @@ -7170,14 +7311,12 @@ impl LocalKernelStdinBridge { let mut state = self.state.lock().expect("kernel stdin state poisoned"); state.closed = true; self.ready.notify_all(); + self.async_ready.notify_waiters(); } + // Synchronous compatibility entrypoints are called outside the Tokio bridge. fn read(&self, args: &[Value]) -> Value { - let max_bytes = args - .first() - .and_then(Value::as_u64) - .map(|value| value.clamp(1, 64 * 1024) as usize) - .unwrap_or(64 * 1024); + let max_bytes = stdin_read_max_bytes(args); let deadline = if args.get(1).is_some_and(Value::is_null) { None } else { @@ -7205,53 +7344,45 @@ impl LocalKernelStdinBridge { } } - if !state.bytes.is_empty() { - let read_len = state.bytes.len().min(max_bytes); - let bytes = state.bytes.drain(..read_len).collect::>(); - return json!({ - "dataBase64": v8_runtime::base64_encode_pub(&bytes), - }); - } - - json!({ - "done": true, - }) + state + .take_readable(max_bytes, false) + .expect("stdin became readable") } - fn read_python_raw(&self, args: &[Value]) -> Value { - const PYTHON_STDIN_DONE_SENTINEL: &str = "__AGENTOS_PYTHON_STDIN_DONE__"; - - let max_bytes = args - .first() - .and_then(Value::as_u64) - .map(|value| value.clamp(1, 64 * 1024) as usize) - .unwrap_or(64 * 1024); - let timeout = Duration::from_millis(args.get(1).and_then(Value::as_u64).unwrap_or(100)); - let deadline = Instant::now() + timeout; - let mut state = self.state.lock().expect("kernel stdin state poisoned"); - - while state.bytes.is_empty() && !state.closed { - let remaining = deadline.saturating_duration_since(Instant::now()); - if remaining.is_zero() { - return Value::Null; + async fn wait_for_pending_read( + &self, + read: Option, + ) -> (u64, Result) { + let Some(read) = read else { + return std::future::pending().await; + }; + loop { + // Register before inspecting the level state. A wake between the + // state check and parking cannot leave the read stranded. + let ready = self.async_ready.notified(); + tokio::pin!(ready); + ready.as_mut().enable(); + { + let mut state = self.state.lock().expect("kernel stdin state poisoned"); + if state.generation != read.generation { + let error = HostServiceError::new("ECANCELED", "local stdin read reset"); + return (read.call_id, Err(error)); + } + if let Some(value) = state.take_readable(read.max_bytes, read.python_raw) { + return (read.call_id, Ok(value)); + } } - let (next_state, wait_result) = self - .ready - .wait_timeout(state, remaining) - .expect("kernel stdin wait poisoned"); - state = next_state; - if wait_result.timed_out() && state.bytes.is_empty() && !state.closed { - return Value::Null; + if let Some(deadline) = read.deadline { + if deadline <= time::Instant::now() { + return (read.call_id, Ok(Value::Null)); + } + // Recheck readiness once the deadline wakes, preserving data + // that became readable concurrently with the timeout. + let _ = time::timeout_at(deadline, ready).await; + } else { + ready.await; } } - - if !state.bytes.is_empty() { - let read_len = state.bytes.len().min(max_bytes); - let bytes = state.bytes.drain(..read_len).collect::>(); - return Value::String(v8_runtime::base64_encode_pub(&bytes)); - } - - Value::String(String::from(PYTHON_STDIN_DONE_SENTINEL)) } } @@ -9839,27 +9970,128 @@ mod tests { } #[test] - fn kernel_stdin_bridge_null_timeout_waits_for_readiness_without_polling() { - let bridge = Arc::new(LocalKernelStdinBridge::default()); - let reader = Arc::clone(&bridge); - let (sender, receiver) = std::sync::mpsc::channel(); - // AGENTOS_THREAD_SITE: test-only-stdin-readiness - let thread = std::thread::spawn(move || { - sender - .send(reader.read(&[json!(64), Value::Null])) - .expect("publish stdin result"); - }); + fn crypto_entropy_native_boundary_rejects_invalid_sizes() { + let mut bridge = LocalBridgeState::default(); + for value in [ + json!(-1), + json!(1.5), + Value::Null, + json!(65537), + json!(u64::MAX), + ] { + assert!( + matches!(bridge.handle_internal_bridge_call(1, "_cryptoRandomFill", &[value]), + Some(LocalBridgeCallResult::Error(error)) if error.code == "ERR_OUT_OF_RANGE") + ); + } + for size in [0, 16, 65536] { + match bridge.handle_internal_bridge_call(1, "_cryptoRandomFill", &[json!(size)]) { + Some(LocalBridgeCallResult::Immediate(Value::String(value))) => { + assert_eq!( + v8_runtime::base64_decode_pub(&value) + .expect("valid entropy base64") + .len(), + size + ); + } + _ => panic!("expected bounded entropy output"), + } + } + } - assert!(receiver.recv_timeout(Duration::from_millis(20)).is_err()); - bridge.write(b"ready").expect("make stdin readable"); - let value = receiver - .recv_timeout(Duration::from_secs(1)) - .expect("readiness should wake the parked read"); - assert_eq!( - value["dataBase64"], - Value::String(v8_runtime::base64_encode_pub(b"ready")) - ); - thread.join().expect("stdin reader exits"); + #[test] + fn local_stdin_read_keeps_single_worker_responsive() { + tokio::runtime::Builder::new_current_thread() + .enable_time() + .build() + .expect("build current-thread test runtime") + .block_on(async { + let mut bridge = LocalBridgeState::default(); + let args = [json!(64), json!(500)]; + assert!(matches!(bridge.begin_stdin_read(1, &args, false), LocalBridgeCallResult::Deferred)); + assert!(matches!( + bridge.begin_stdin_read(2, &args, false), + LocalBridgeCallResult::Error(error) if error.code == "EBUSY" + )); + + let pending = bridge.pending_stdin_read; + let stdin = &bridge.kernel_stdin; + // Poll the pending read first. When it yields, the same worker + // can complete an unrelated event before any stdin data arrives. + tokio::select! { + biased; + reply = stdin.wait_for_pending_read(pending) => panic!("read completed early: {reply:?}"), + () = tokio::task::yield_now() => {} + } + let write = async { + tokio::task::yield_now().await; + stdin.write(b"ready").expect("make stdin readable"); + }; + let ((id, value), ()) = time::timeout( + Duration::from_secs(1), + async { tokio::join!(stdin.wait_for_pending_read(pending), write) }, + ) + .await + .expect("stdin readiness wakes the pending read"); + assert_eq!(id, 1); + assert_eq!(value.expect("read succeeds")["dataBase64"], json!("cmVhZHk=")); + }); + } + + #[test] + fn local_stdin_read_closes_and_resets_without_consuming_new_input() { + tokio::runtime::Builder::new_current_thread() + .enable_time() + .build() + .expect("build current-thread test runtime") + .block_on(async { + let mut bridge = LocalBridgeState::default(); + let args = [json!(64), json!(1_000)]; + assert!(matches!( + bridge.begin_stdin_read(1, &args, true), + LocalBridgeCallResult::Deferred + )); + let pending = bridge.pending_stdin_read.take(); + let stdin = Arc::clone(&bridge.kernel_stdin); + let close = async { + tokio::task::yield_now().await; + stdin.close(); + }; + let ((_, value), ()) = time::timeout(Duration::from_secs(1), async { + tokio::join!(stdin.wait_for_pending_read(pending), close) + }) + .await + .expect("stdin close wakes the pending read"); + assert_eq!( + value.expect("read returns EOF"), + json!("__AGENTOS_PYTHON_STDIN_DONE__") + ); + + stdin.reset(); + assert!(matches!( + bridge.begin_stdin_read(2, &args, false), + LocalBridgeCallResult::Deferred + )); + let pending = bridge.pending_stdin_read.take(); + stdin.reset(); + stdin.write(b"new").expect("write input for next execution"); + let (id, value) = + time::timeout(Duration::from_secs(1), stdin.wait_for_pending_read(pending)) + .await + .expect("reset cancels the pending read"); + assert_eq!(id, 2); + assert_eq!(value.expect_err("reset cancels the read").code, "ECANCELED"); + assert!(matches!( + bridge.begin_stdin_read(3, &args, true), + LocalBridgeCallResult::Immediate(value) if value == json!("bmV3") + )); + + drop(bridge); + assert!(matches!( + stdin.write(b"x"), + Err(JavascriptExecutionError::StdinClosed) + )); + }); } #[test] diff --git a/crates/executor-v8-runtime/src/module_code_cache.rs b/crates/executor-v8-runtime/src/module_code_cache.rs index 73a5cdc3d9..610cd8f515 100644 --- a/crates/executor-v8-runtime/src/module_code_cache.rs +++ b/crates/executor-v8-runtime/src/module_code_cache.rs @@ -87,7 +87,7 @@ thread_local! { } /// Creates an isolate that matches the session isolate's startup snapshot. -pub type PristineIsolateFactory = Arc v8::OwnedIsolate + Send + Sync>; +pub type PristineIsolateFactory = Arc crate::isolate::GuestIsolate + Send + Sync>; /// Set (or clear) how record jobs from this session thread build their /// isolate. Call whenever the session installs an isolate. @@ -192,7 +192,7 @@ fn compile_and_record_pristine( crate::isolate::prepare_current_thread(); let mut isolate = crate::owned_microtasks::SessionIsolate::new(factory()); let result = { - let scope = &mut v8::HandleScope::new(&mut *isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); let resource = v8::String::new(scope, resource_name).ok_or("resource name too large")?; @@ -328,7 +328,7 @@ pub fn module_code_cache() -> &'static ModuleCodeCache { /// `v8_source` must be the V8 string for `source`; callers create it so they /// keep their own error reporting for sources V8 cannot represent. pub fn compile_module_with_code_cache<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, resource_name: &str, source: &str, v8_source: v8::Local<'s, v8::String>, diff --git a/crates/executor-v8-runtime/src/module_snapshot.rs b/crates/executor-v8-runtime/src/module_snapshot.rs index d4b202d183..b28b161791 100644 --- a/crates/executor-v8-runtime/src/module_snapshot.rs +++ b/crates/executor-v8-runtime/src/module_snapshot.rs @@ -665,14 +665,14 @@ fn remember_verified_sources(graph: &ModuleSnapshotGraph, identities: Vec( - f: impl FnOnce(&mut v8::HandleScope) -> Result, + f: impl FnOnce(&mut v8::PinScope<'_, '_>) -> Result, ) -> Result { crate::isolate::init_v8_platform(); crate::isolate::prepare_current_thread(); let mut isolate = Some(crate::isolate::create_isolate(None)); let result = { - let isolate = isolate.as_mut().expect("parse isolate"); - let scope = &mut v8::HandleScope::new(isolate); + let isolate: &mut v8::Isolate = isolate.as_mut().expect("parse isolate"); + v8::scope!(scope, isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); f(scope) @@ -687,12 +687,12 @@ fn with_parse_scope( /// build. Discovery runs in the sidecar, so it compiles from a heap copy that /// its parse isolate frees, and lazily: it only reads import requests. fn compile_snapshot_module<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, path: &str, source: &str, for_snapshot: bool, ) -> Result, String> { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let v8_source = if for_snapshot { crate::shared_source::snapshot_source_string(tc, source) .map_err(|error| graph_error(format!("{path}: {error}")))? @@ -741,12 +741,12 @@ fn compile_snapshot_module<'s>( } fn module_specifiers( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, path: &str, source: &str, ) -> Result, String> { // Each module's source and record become garbage once its requests are read. - let scope = &mut v8::HandleScope::new(scope); + v8::scope!(scope, scope); let module = compile_snapshot_module(scope, path, source, false)?; let requests = module.get_module_requests(); let mut specifiers = Vec::with_capacity(requests.length()); @@ -781,7 +781,7 @@ fn build_resolve_callback<'a>( _import_attributes: v8::Local<'a, v8::FixedArray>, referrer: v8::Local<'a, v8::Module>, ) -> Option> { - let scope = &mut unsafe { v8::CallbackScope::new(context) }; + v8::callback_scope!(unsafe scope, context); let specifier = specifier.to_rust_string_lossy(scope); let target = BUILD_STATE.with(|cell| { let state = cell.borrow(); @@ -815,7 +815,7 @@ extern "C" fn build_import_meta_callback( module: v8::Local, meta: v8::Local, ) { - let scope = &mut unsafe { v8::CallbackScope::new(context) }; + v8::callback_scope!(unsafe scope, context); let path = BUILD_STATE.with(|cell| { let state = cell.borrow(); let state = state.as_ref()?; @@ -831,7 +831,7 @@ extern "C" fn build_import_meta_callback( } fn build_dynamic_import_callback<'a>( - scope: &mut v8::HandleScope<'a>, + scope: &mut v8::PinScope<'a, '_>, _host_defined_options: v8::Local<'a, v8::Data>, _resource_name: v8::Local<'a, v8::Value>, specifier: v8::Local<'a, v8::String>, @@ -947,8 +947,10 @@ const SNAPSHOT_BUILD_GUARDS: &str = r#"(() => { })()"#; /// Install [`SNAPSHOT_BUILD_GUARDS`] and return the function that removes them. -fn install_build_guards(scope: &mut v8::HandleScope) -> Result, String> { - let tc = &mut v8::TryCatch::new(scope); +fn install_build_guards( + scope: &mut v8::PinScope<'_, '_>, +) -> Result, String> { + v8::tc_scope!(tc, scope); let source = v8::String::new(tc, SNAPSHOT_BUILD_GUARDS).expect("static script"); let remove = v8::Script::compile(tc, source, None) .and_then(|script| script.run(tc)) @@ -963,10 +965,10 @@ fn install_build_guards(scope: &mut v8::HandleScope) -> Result, remove: v8::Global, ) -> Result<(), String> { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let remove = v8::Local::new(tc, remove); let receiver = v8::undefined(tc).into(); if remove.call(tc, receiver, &[]).is_none() { @@ -979,7 +981,7 @@ fn remove_build_guards( /// Compile, link, and evaluate `graph` in the snapshot creator context, then /// attach the evaluated modules as context data for restore. pub(crate) fn evaluate_module_graph_for_snapshot( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, graph: &ModuleSnapshotGraph, ) -> Result<(), String> { scope.set_host_initialize_import_meta_object_callback(build_import_meta_callback); @@ -1031,11 +1033,11 @@ pub(crate) fn evaluate_module_graph_for_snapshot( } fn evaluate_roots( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, graph: &ModuleSnapshotGraph, modules: &[v8::Global], ) -> Result<(), String> { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); for (specifier, index) in &graph.entry_requests { let module = v8::Local::new(tc, &modules[*index]); let path = &graph.modules[*index].path; @@ -1080,7 +1082,10 @@ fn evaluate_roots( Ok(()) } -fn exception_text(tc: &mut v8::TryCatch, fallback: &str) -> String { +fn exception_text( + tc: &mut v8::PinnedRef<'_, v8::TryCatch<'_, '_, v8::HandleScope<'_>>>, + fallback: &str, +) -> String { match tc.exception() { Some(exception) => value_text(tc, exception), None => fallback.to_owned(), @@ -1088,7 +1093,7 @@ fn exception_text(tc: &mut v8::TryCatch, fallback: &str) -> Str } /// An exception with the module and line it came from. -fn value_text(scope: &mut v8::HandleScope, value: v8::Local) -> String { +fn value_text(scope: &mut v8::PinScope<'_, '_>, value: v8::Local) -> String { if let Ok(object) = v8::Local::::try_from(value) { let key = v8::String::new(scope, "stack").unwrap(); if let Some(stack) = object @@ -1119,7 +1124,7 @@ fn value_text(scope: &mut v8::HandleScope, value: v8::Local) -> Strin /// Timers the graph left pending at module top level. A snapshot cannot carry /// a pending timer, so a count that cannot be read fails the snapshot instead /// of passing it. -fn pending_timer_count(scope: &mut v8::HandleScope) -> Result { +fn pending_timer_count(scope: &mut v8::PinScope<'_, '_>) -> Result { let context = scope.get_current_context(); let global = context.global(scope); let key = v8::String::new(scope, "_getPendingTimerCount") @@ -1149,7 +1154,7 @@ fn pending_timer_count(scope: &mut v8::HandleScope) -> Result { /// snapshot. Returns `None` for any other context. V8 hands context data out /// once, so call this once per context. pub(crate) fn take_snapshot_modules( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, ) -> Option)>> { let paths = scope .get_context_data_from_snapshot_once::(MODULE_PATHS_DATA_INDEX) diff --git a/crates/executor-v8-runtime/src/next_ticks.rs b/crates/executor-v8-runtime/src/next_ticks.rs index 29babe7a81..161a0db116 100644 --- a/crates/executor-v8-runtime/src/next_ticks.rs +++ b/crates/executor-v8-runtime/src/next_ticks.rs @@ -8,28 +8,28 @@ const PENDING: &str = "agentos.nextTick.pending"; const BEFORE: &str = "agentos.nextTick.beforeCheckpoint"; const ENTRY: &str = "agentos.nextTick.entryIsModule"; -fn key<'s>(scope: &mut v8::HandleScope<'s>, name: &str) -> v8::Local<'s, v8::Private> { +fn key<'s>(scope: &mut v8::PinScope<'s, '_>, name: &str) -> v8::Local<'s, v8::Private> { let text = v8::String::new(scope, name).expect("nextTick private key"); v8::Private::for_api(scope, Some(text)) } -fn get<'s>(scope: &mut v8::HandleScope<'s>, name: &str) -> Option> { +fn get<'s>(scope: &mut v8::PinScope<'s, '_>, name: &str) -> Option> { let global = scope.get_current_context().global(scope); let key = key(scope, name); global.get_private(scope, key) } -fn set(scope: &mut v8::HandleScope, name: &str, value: bool) { +fn set(scope: &mut v8::PinScope<'_, '_>, name: &str, value: bool) { let global = scope.get_current_context().global(scope); let key = key(scope, name); let value = v8::Boolean::new(scope, value); assert_eq!(global.set_private(scope, key, value.into()), Some(true)); } -fn clear(scope: &mut v8::HandleScope, name: &str) { +fn clear(scope: &mut v8::PinScope<'_, '_>, name: &str) { let global = scope.get_current_context().global(scope); let key = key(scope, name); assert_eq!(global.delete_private(scope, key), Some(true)); } fn field<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, object: v8::Local, name: &str, ) -> Option> { @@ -37,7 +37,7 @@ fn field<'s>( object.get(scope, key.into()) } -pub(crate) fn capture(scope: &mut v8::HandleScope) -> Result<(), String> { +pub(crate) fn capture(scope: &mut v8::PinScope<'_, '_>) -> Result<(), String> { let global = scope.get_current_context().global(scope); let name = v8::String::new(scope, BOOTSTRAP).ok_or("nextTick bootstrap key allocation")?; let value = global @@ -62,20 +62,21 @@ pub(crate) fn capture(scope: &mut v8::HandleScope) -> Result<(), String> { Ok(()) } -pub(crate) fn begin_entry(scope: &mut v8::HandleScope, module: bool) { +pub(crate) fn begin_entry(scope: &mut v8::PinScope<'_, '_>, module: bool) { set(scope, ENTRY, module); } -pub(crate) fn scheduled(scope: &mut v8::HandleScope) -> Result<(), String> { +pub(crate) fn scheduled(scope: &mut v8::PinScope<'_, '_>) -> Result<(), String> { + let context = scope.get_current_context(); + let queue = context + .get_microtask_queue() + .ok_or("ERR_AGENTOS_DETACHED_CONTEXT: nextTick requires an attached context")?; if !get(scope, HOOKS).is_some_and(|value| value.is_object()) { return Err("nextTick hooks are not installed".into()); } let deferred = match get(scope, ENTRY) { Some(value) if value.is_boolean() => value.boolean_value(scope), - _ => scope - .get_current_context() - .get_microtask_queue() - .is_running_microtasks(), + _ => queue.is_running_microtasks(), }; set(scope, PENDING, true); if !deferred { @@ -84,20 +85,20 @@ pub(crate) fn scheduled(scope: &mut v8::HandleScope) -> Result<(), String> { Ok(()) } -pub(crate) fn pending(scope: &mut v8::HandleScope) -> bool { +pub(crate) fn pending(scope: &mut v8::PinScope<'_, '_>) -> bool { get(scope, PENDING).is_some_and(|value| value.is_true()) } -fn stopped(scope: &mut v8::HandleScope) -> bool { +fn stopped(scope: &mut v8::PinScope<'_, '_>) -> bool { crate::terminal_callback::pending(scope) || scope.is_execution_terminating() } -fn flush(scope: &mut v8::HandleScope) { +fn flush(scope: &mut v8::PinScope<'_, '_>) { if stopped(scope) || !pending(scope) { return; } let mut storage = std::pin::pin!(MicrotaskSuppressionStorage::new()); let mut explicit = ExplicitMicrotaskScope::new(scope, storage.as_mut()); - let tc = &mut v8::TryCatch::new(&mut *explicit); + v8::tc_scope!(tc, &mut *explicit); let hooks = get(tc, HOOKS) .and_then(|value| v8::Local::::try_from(value).ok()) .expect("scheduled nextTick hooks"); @@ -123,8 +124,8 @@ fn flush(scope: &mut v8::HandleScope) { } } -pub(crate) fn checkpoint(scope: &mut v8::HandleScope) { - if stopped(scope) { +pub(crate) fn checkpoint(scope: &mut v8::PinScope<'_, '_>) { + if stopped(scope) || scope.get_current_context().get_microtask_queue().is_none() { return; } clear(scope, ENTRY); @@ -136,7 +137,12 @@ pub(crate) fn checkpoint(scope: &mut v8::HandleScope) { return; } let context = scope.get_current_context(); - context.get_microtask_queue().perform_checkpoint(scope); + // A detached context no longer owns a job queue; never deliver its + // pending work on the isolate default or another VM context. + let Some(queue) = context.get_microtask_queue() else { + return; + }; + queue.perform_checkpoint(scope); if stopped(scope) || !pending(scope) { return; } @@ -146,7 +152,7 @@ pub(crate) fn checkpoint(scope: &mut v8::HandleScope) { /// Replaces runtime-owned queue storage without invoking callbacks or setters /// supplied by guest code. The private state object never leaves the runtime. -pub(crate) fn discard(scope: &mut v8::HandleScope) { +pub(crate) fn discard(scope: &mut v8::PinScope<'_, '_>) { if let Some(hooks) = get(scope, HOOKS).and_then(|value| v8::Local::::try_from(value).ok()) { diff --git a/crates/executor-v8-runtime/src/node_brotli.rs b/crates/executor-v8-runtime/src/node_brotli.rs index 454dc562cc..a6590acc16 100644 --- a/crates/executor-v8-runtime/src/node_brotli.rs +++ b/crates/executor-v8-runtime/src/node_brotli.rs @@ -47,6 +47,9 @@ impl Write for Output { } else { self.reservation = Some(extra); } + self.bytes.try_reserve_exact(bytes.len()).map_err(|_| { + io::Error::other("ERR_MEMORY_ALLOCATION_FAILED: unable to allocate Brotli output") + })?; self.bytes.extend_from_slice(bytes); Ok(bytes.len()) } @@ -56,7 +59,7 @@ impl Write for Output { } pub(crate) fn transform<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, args: &v8::FunctionCallbackArguments<'s>, decode: bool, limit: usize, @@ -64,14 +67,11 @@ pub(crate) fn transform<'s>( ) -> Result, String> { let view = v8::Local::::try_from(args.get(0)) .map_err(|_| "ERR_INVALID_ARG_TYPE: Brotli input must be a byte view")?; - if view.byte_length() > limit { - return Err("ERR_AGENTOS_BRIDGE_RESPONSE_LIMIT: Brotli input exceeds limits.reactor.maxBridgeResponseBytes".into()); - } let _input_reservation = resources .reserve(ResourceClass::BridgeRequestBytes, view.byte_length()) .map_err(|error| error.to_string())?; - let mut bytes = vec![0; view.byte_length()]; - view.copy_contents(&mut bytes); + let bytes = crate::node_v8_serialization::copy_view(scope, view) + .map_err(|_| "ERR_MEMORY_ALLOCATION_FAILED: unable to copy Brotli input")?; let requested = args.get(4).integer_value(scope).unwrap_or(limit as i64); let mut output = Output { bytes: Vec::new(), @@ -111,6 +111,7 @@ pub(crate) fn transform<'s>( ); } let length = output.bytes.len(); + crate::isolate::admit_external_allocation(scope, length)?; let backing = v8::ArrayBuffer::new_backing_store_from_vec(output.bytes).make_shared(); let buffer = v8::ArrayBuffer::with_backing_store(scope, &backing); v8::Uint8Array::new(scope, buffer, 0, length) diff --git a/crates/executor-v8-runtime/src/node_v8_deserializer.rs b/crates/executor-v8-runtime/src/node_v8_deserializer.rs index 5ec80f0511..37a06f58f1 100644 --- a/crates/executor-v8-runtime/src/node_v8_deserializer.rs +++ b/crates/executor-v8-runtime/src/node_v8_deserializer.rs @@ -27,7 +27,7 @@ fn registry(scope: &mut v8::Isolate) -> Registry { scope.set_slot(registry.clone()); registry } -fn key<'s>(scope: &mut v8::HandleScope<'s>) -> v8::Local<'s, v8::Private> { +fn key<'s>(scope: &mut v8::PinScope<'s, '_>) -> v8::Local<'s, v8::Private> { let name = v8::String::new(scope, "agentOS.node.v8.deserializer").unwrap(); v8::Private::for_api(scope, Some(name)) } @@ -62,12 +62,12 @@ struct Delegate(v8::Weak); impl v8::ValueDeserializerImpl for Delegate { fn read_host_object<'s>( &self, - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, _decoder: &dyn ValueDeserializerHelper, ) -> Option> { // V8 invokes ReadHostObject under a no-JavaScript scope. Node permits // its user hook here; keep that permission scoped to this callback. - let scope = &mut v8::AllowJavascriptExecutionScope::new(scope); + v8::allow_javascript_execution_scope!(let scope, scope); let receiver = self.0.to_local(scope)?; let name = v8::String::new(scope, "_readHostObject")?; let method = receiver.get(scope, name.into())?; @@ -80,7 +80,7 @@ impl v8::ValueDeserializerImpl for Delegate { } pub(crate) fn call<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, args: &v8::FunctionCallbackArguments<'s>, method: &str, limit: usize, @@ -110,8 +110,7 @@ pub(crate) fn call<'s>( let bytes = resources .reserve(ResourceClass::ExecutorBytes, input.byte_length()) .map_err(|error| error.to_string())?; - let mut input_bytes = vec![0; input.byte_length()]; - input.copy_contents(&mut input_bytes); + let input_bytes = crate::node_v8_serialization::copy_view(scope, input)?; let delegate_receiver = v8::Weak::new(scope, receiver); let decoder = v8::ValueDeserializer::new(scope, Box::new(Delegate(delegate_receiver)), &input_bytes); @@ -221,7 +220,7 @@ pub(crate) fn call<'s>( .read_raw_bytes(count as usize) .ok_or("ReadRawBytes failed")?; let length = bytes.len(); - let backing = v8::ArrayBuffer::new_backing_store_from_vec(bytes.to_vec()).make_shared(); + let backing = crate::node_v8_serialization::copied_backing_store(scope, bytes)?; let buffer = v8::ArrayBuffer::with_backing_store(scope, &backing); Ok(v8::Uint8Array::new(scope, buffer, 0, length) .ok_or("Unable to allocate raw bytes")? diff --git a/crates/executor-v8-runtime/src/node_v8_serialization.rs b/crates/executor-v8-runtime/src/node_v8_serialization.rs index 7c44bba28a..32e570d4a6 100644 --- a/crates/executor-v8-runtime/src/node_v8_serialization.rs +++ b/crates/executor-v8-runtime/src/node_v8_serialization.rs @@ -1,15 +1,10 @@ //! Node's default V8 wire format, evaluated entirely on the owning isolate. -use std::cell::Cell; -use v8::{ValueDeserializerHelper, ValueSerializerHelper}; - -struct SerializerDelegate { - remaining: Cell, - limit: usize, - path: &'static str, -} +use agentos_driver_tokio::accounting::{Reservation, ResourceClass, ResourceLedger}; +use std::{ffi::c_void, sync::Arc}; +use v8::ValueDeserializerHelper; pub(crate) fn throw_limit( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, limit: usize, requested: usize, path: &'static str, @@ -39,93 +34,30 @@ pub(crate) fn throw_limit( scope.throw_exception(error); } -impl v8::ValueSerializerImpl for SerializerDelegate { - fn throw_data_clone_error<'s>( - &self, - scope: &mut v8::HandleScope<'s>, - message: v8::Local<'s, v8::String>, - ) { - let error = v8::Exception::error(scope, message); - scope.throw_exception(error); - } - - fn write_host_object<'s>( - &self, - scope: &mut v8::HandleScope<'s>, - object: v8::Local<'s, v8::Object>, - serializer: &dyn ValueSerializerHelper, - ) -> Option { - let view = v8::Local::::try_from(object).ok()?; - let length = view.byte_length(); - if length > self.remaining.get() { - throw_limit( - scope, - self.limit, - (self.limit - self.remaining.get()).saturating_add(length), - self.path, - ); - return None; - } - self.remaining.set(self.remaining.get() - length); - // Node lib/v8.js uses these stable type indexes and serializes only the - // visible view. Serializing a pooled Buffer's entire backing store leaks - // unrelated bytes and must never be used as a shortcut. - let constructor_key = v8::String::new(scope, "constructor")?; - let constructor = object.get(scope, constructor_key.into())?; - let buffer_key = v8::String::new(scope, "Buffer")?; - let global = scope.get_current_context().global(scope); - let buffer = global.get(scope, buffer_key.into())?; - let kind = if object.is_uint8_array() && constructor.strict_equals(buffer) { - 10 - } else if object.is_int8_array() { - 0 - } else if object.is_uint8_array() { - 1 - } else if object.is_uint8_clamped_array() { - 2 - } else if object.is_int16_array() { - 3 - } else if object.is_uint16_array() { - 4 - } else if object.is_int32_array() { - 5 - } else if object.is_uint32_array() { - 6 - } else if object.is_float32_array() { - 7 - } else if object.is_float64_array() { - 8 - } else if object.is_data_view() { - 9 - } else if object.is_big_int64_array() { - 11 - } else if object.is_big_uint64_array() { - 12 - } else { - return None; - }; - serializer.write_uint32(kind); - serializer.write_uint32(u32::try_from(length).ok()?); - let mut bytes = vec![0; length]; - view.copy_contents(&mut bytes); - serializer.write_raw_bytes(&bytes); - Some(true) - } -} - struct DeserializerDelegate; impl v8::ValueDeserializerImpl for DeserializerDelegate { fn read_host_object<'s>( &self, - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, deserializer: &dyn ValueDeserializerHelper, ) -> Option> { let (mut kind, mut length) = (0, 0); if !deserializer.read_uint32(&mut kind) || !deserializer.read_uint32(&mut length) { return None; } + // Validate tags/alignment before allocating even a byte for malformed input. + let width = match kind { + 0..=2 | 9 | 10 => 1, + 3 | 4 | 13 => 2, + 5..=7 => 4, + 8 | 11 | 12 => 8, + _ => return None, + }; + if !(length as usize).is_multiple_of(width) { + return None; + } let bytes = deserializer.read_raw_bytes(length as usize)?; - let backing = v8::ArrayBuffer::new_backing_store_from_vec(bytes.to_vec()).make_shared(); + let backing = copied_backing_store(scope, bytes).ok()?; let buffer = v8::ArrayBuffer::with_backing_store(scope, &backing); let length = length as usize; macro_rules! view { @@ -160,47 +92,146 @@ impl v8::ValueDeserializerImpl for DeserializerDelegate { } 11 => view!(BigInt64Array, 8), 12 => view!(BigUint64Array, 8), + 13 => v8::Float16Array::new(scope, buffer, 0, length / 2)?.to_object(scope), _ => None, } } } +// Bound serializer allocations before V8 requests host memory. The C++ shim +// owns only V8's standard serializer and delegates allocation admission here. +struct SerializationBudget { + resources: Arc, + reservation: Option, + allocated: usize, + error: Option, +} +unsafe extern "C" fn admit_serialization(data: *mut c_void, bytes: usize) -> bool { + // SAFETY: the synchronous C++ call borrows this stack value; it never stores + // the callback or calls it after returning, and never runs it concurrently. + let budget = unsafe { &mut *data.cast::() }; + if bytes <= budget.allocated { + return true; + } + match budget + .resources + .reserve(ResourceClass::ExecutorBytes, bytes - budget.allocated) + { + Ok(extra) => { + if let Some(reservation) = &mut budget.reservation { + if reservation.merge(extra).is_err() { + budget.error = Some("Serialization allocation accounting failed".into()); + return false; + } + } else { + budget.reservation = Some(extra); + } + budget.allocated = bytes; + true + } + Err(error) => { + budget.error = Some(error.to_string()); + false + } + } +} +extern "C" { + fn agentos_v8_serialize_bounded( + context: *const v8::Context, + value: *const v8::Value, + limit: usize, + admit: unsafe extern "C" fn(*mut c_void, usize) -> bool, + budget: *mut c_void, + bytes: *mut *mut u8, + length: *mut usize, + rejected: *mut usize, + allocation_failed: *mut bool, + ) -> bool; +} +unsafe extern "C" fn free_serialized_buffer( + data: *mut c_void, + _length: usize, + _state: *mut c_void, +) { + // SAFETY: the C++ serializer hands off a unique malloc allocation exactly + // once. V8 owns it until this backing-store deleter runs. + unsafe { libc::free(data) }; +} + pub(crate) fn serialize<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, value: v8::Local<'s, v8::Value>, limit: usize, path: &'static str, + resources: Arc, ) -> Result, String> { - let context = scope.get_current_context(); - let serializer = v8::ValueSerializer::new( - scope, - Box::new(SerializerDelegate { - remaining: Cell::new(limit), + v8::tc_scope!(let scope, scope); + let mut budget = SerializationBudget { + resources, + reservation: None, + allocated: 0, + error: None, + }; + let (mut data, mut length, mut rejected, mut allocation_failed) = + (std::ptr::null_mut(), 0, 0, false); + // SAFETY: these handles belong to this live isolate/context, every output + // pointer is valid for the call, and the budget callback cannot outlive it. + let ok = unsafe { + agentos_v8_serialize_bounded( + &*scope.get_current_context(), + &*value, limit, - path, - }), - ); - serializer.set_treat_array_buffer_views_as_host_objects(true); - serializer.write_header(); - serializer - .write_value(context, value) - .filter(|ok| *ok) - .ok_or("Unable to serialize value")?; - let bytes = serializer.release(); - if bytes.len() > limit { - throw_limit(scope, limit, bytes.len(), path); - return Err(format!("Serialized value exceeds {path}")); + admit_serialization, + (&mut budget as *mut SerializationBudget).cast(), + &mut data, + &mut length, + &mut rejected, + &mut allocation_failed, + ) + }; + if !ok { + if rejected != 0 { + scope.reset(); + if let Some(error) = budget.error { + return Err(error); + } + throw_limit(scope, limit, rejected, path); + scope.rethrow(); + return Err(format!("Serialized value exceeds {path}")); + } + if allocation_failed { + return Err( + "ERR_AGENTOS_V8_SERIALIZATION_ALLOCATION: unable to allocate serialized bytes" + .into(), + ); + } + scope.rethrow(); + return Err("Unable to serialize value".into()); } - if bytes.len() >= limit.saturating_mul(4) / 5 { + if let Err(error) = crate::isolate::admit_external_allocation(scope, length) { + // SAFETY: no backing store owns this successful C++ allocation yet. + unsafe { libc::free(data.cast()) }; + return Err(error); + } + if length >= limit.saturating_mul(4) / 5 { tracing::warn!( - bytes = bytes.len(), + bytes = length, limit, configuration_path = path, "V8 serialization approaches its configured byte limit" ); } - let length = bytes.len(); - let backing = v8::ArrayBuffer::new_backing_store_from_vec(bytes).make_shared(); + // SAFETY: the serializer shrank its unique malloc allocation to `length`; + // the deleter matches that allocator and V8 takes over lifetime management. + let backing = unsafe { + v8::ArrayBuffer::new_backing_store_from_ptr( + data.cast(), + length, + free_serialized_buffer, + std::ptr::null_mut(), + ) + } + .make_shared(); let buffer = v8::ArrayBuffer::with_backing_store(scope, &backing); v8::Uint8Array::new(scope, buffer, 0, length) .map(Into::into) @@ -208,10 +239,11 @@ pub(crate) fn serialize<'s>( } pub(crate) fn deserialize<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, value: v8::Local<'s, v8::Value>, limit: usize, path: &'static str, + resources: Arc, ) -> Result, String> { let view = v8::Local::::try_from(value) .map_err(|_| "buffer must be a TypedArray or DataView")?; @@ -219,8 +251,10 @@ pub(crate) fn deserialize<'s>( throw_limit(scope, limit, view.byte_length(), path); return Err(format!("Serialized input exceeds {path}")); } - let mut bytes = vec![0; view.byte_length()]; - view.copy_contents(&mut bytes); + let _bytes = resources + .reserve(ResourceClass::ExecutorBytes, view.byte_length()) + .map_err(|error| error.to_string())?; + let bytes = copy_view(scope, view)?; let context = scope.get_current_context(); let deserializer = v8::ValueDeserializer::new(scope, Box::new(DeserializerDelegate), &bytes); deserializer @@ -233,8 +267,275 @@ pub(crate) fn deserialize<'s>( } pub(crate) fn read_default_host_object<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, reader: &dyn ValueDeserializerHelper, ) -> Option> { v8::ValueDeserializerImpl::read_host_object(&DeserializerDelegate, scope, reader) } + +/// Copy native input only after its owning caller has admitted the byte count. +/// Do not use infallible Vec allocation at the guest/native boundary. +pub(crate) fn copy_view( + scope: &mut v8::PinScope<'_, '_>, + view: v8::Local, +) -> Result, String> { + let length = view.byte_length(); + let mut bytes = Vec::new(); + bytes + .try_reserve_exact(length) + .map_err(|_| "ERR_AGENTOS_V8_SERIALIZATION_ALLOCATION: unable to copy serialized bytes")?; + bytes.resize(length, 0); + let backing = view + .buffer(scope) + .ok_or("Serialized input has no buffer")? + .get_backing_store(); + let offset = view.byte_offset(); + if offset > backing.byte_length() || length > backing.byte_length() - offset { + return Err("Serialized input changed before copying".into()); + } + if length != 0 { + let data = backing + .data() + .ok_or("Serialized input has no backing data")?; + // SAFETY: the held backing store covers this validated range. No guest + // code runs between capture and copy, and workers cannot share it. + // Avoid the binding's i32 CopyContents length conversion and stale stored + // lengths for length-tracking resizable views. + unsafe { + std::ptr::copy_nonoverlapping( + data.as_ptr().cast::().add(offset), + bytes.as_mut_ptr(), + length, + ); + } + } + Ok(bytes) +} + +pub(crate) fn copied_backing_store( + scope: &mut v8::PinScope<'_, '_>, + bytes: &[u8], +) -> Result, String> { + crate::isolate::admit_external_allocation(scope, bytes.len()).inspect_err(|error| { + if let Some(message) = v8::String::new(scope, error) { + let exception = v8::Exception::range_error(scope, message); + scope.throw_exception(exception); + } + })?; + // V8's C++ ArrayBuffer::New aborts the process on allocator exhaustion. + // Use a fallible allocation here, after external-memory admission, and + // transfer ownership without a second copy. V8 then accounts its lifetime. + let mut owned = Vec::new(); + owned + .try_reserve_exact(bytes.len()) + .map_err(|_| "ERR_AGENTOS_V8_SERIALIZATION_ALLOCATION: unable to copy host object")?; + owned.extend_from_slice(bytes); + Ok(v8::ArrayBuffer::new_backing_store_from_vec(owned).make_shared()) +} + +#[cfg(test)] +mod tests { + use super::*; + use agentos_driver_tokio::accounting::ResourceLimit; + + fn eval<'s>(scope: &mut v8::PinScope<'s, '_>, source: &str) -> v8::Local<'s, v8::Value> { + let source = v8::String::new(scope, source).unwrap(); + v8::Script::compile(scope, source, None) + .unwrap() + .run(scope) + .unwrap() + } + fn ledger(limit: usize) -> Arc { + Arc::new(ResourceLedger::root( + "serializer-test", + [( + ResourceClass::ExecutorBytes, + ResourceLimit::new(limit, "runtime.blocking.maxQueuedBytes"), + )], + )) + } + #[test] + fn node_writer_emits_wire_15_and_reader_accepts_native_wire_16() { + use v8::ValueSerializerHelper; + + struct NativeDelegate; + impl v8::ValueSerializerImpl for NativeDelegate { + fn throw_data_clone_error<'s>( + &self, + scope: &mut v8::PinScope<'s, '_>, + message: v8::Local<'s, v8::String>, + ) { + let error = v8::Exception::error(scope, message); + scope.throw_exception(error); + } + } + + let mut isolate = crate::isolate::create_isolate(Some(32)); + let context = crate::isolate::create_context(&mut isolate); + v8::scope!(let scope, &mut *isolate); + let context = v8::Local::new(scope, &context); + let scope = &mut v8::ContextScope::new(scope, context); + let value = eval(scope, "(() => { const b = new ArrayBuffer(2, {maxByteLength:9}); new Uint8Array(b).set([17,31]); return b; })()"); + let resources = ledger(1024 * 1024); + let path = "limits.jsRuntime.v8SerializationLimitBytes"; + let wire = serialize(scope, value, 4096, path, resources.clone()).unwrap(); + let wire = v8::Local::::try_from(wire).unwrap(); + // Produced by node:v8.serialize on Node 24.18.0 and 26.13.0. + assert_eq!( + copy_view(scope, wire).unwrap(), + [255, 15, 126, 2, 9, 17, 31] + ); + + let native = v8::ValueSerializer::new(scope, Box::new(NativeDelegate)); + native.write_header(); + assert_eq!(native.write_value(context, value), Some(true)); + let bytes = native.release(); + assert_eq!(&bytes[..2], &[255, 16]); + let backing = copied_backing_store(scope, &bytes).unwrap(); + let buffer = v8::ArrayBuffer::with_backing_store(scope, &backing); + let input = v8::Uint8Array::new(scope, buffer, 0, bytes.len()).unwrap(); + let restored = deserialize(scope, input.into(), 4096, path, resources).unwrap(); + let restored = v8::Local::::try_from(restored).unwrap(); + assert!(restored.is_resizable_by_user_javascript()); + assert_eq!(restored.byte_length(), 2); + assert_eq!(restored.get_backing_store().max_byte_length(), 9); + let view = v8::Uint8Array::new(scope, restored, 0, 2).unwrap(); + assert_eq!(copy_view(scope, view.into()).unwrap(), [17, 31]); + } + + #[test] + fn bounded_serializer_preserves_graphs_and_exact_wire_limit() { + let mut isolate = crate::isolate::create_isolate(Some(32)); + let context = crate::isolate::create_context(&mut isolate); + v8::scope!(let scope, &mut *isolate); + let context = v8::Local::new(scope, &context); + let scope = &mut v8::ContextScope::new(scope, context); + let value = eval(scope, "(()=>{const v={n:9n,typed:new Uint16Array([500,600])};v.self=v;v.alias=v.typed;return v})()"); + let resources = ledger(1024 * 1024); + let wire = serialize( + scope, + value, + 4096, + "limits.jsRuntime.v8SerializationLimitBytes", + resources.clone(), + ) + .unwrap(); + let length = v8::Local::::try_from(wire) + .unwrap() + .byte_length(); + assert!( + length < 66, + "exercise V8's minimum allocation above the exact wire limit" + ); + assert!(serialize( + scope, + value, + length, + "limits.jsRuntime.v8SerializationLimitBytes", + resources.clone() + ) + .is_ok()); + let restored = deserialize( + scope, + wire, + length, + "limits.jsRuntime.v8SerializationLimitBytes", + resources.clone(), + ) + .unwrap(); + let key = v8::String::new(scope, "restored").unwrap(); + scope + .get_current_context() + .global(scope) + .set(scope, key.into(), restored); + assert!(eval(scope, "restored.self===restored&&restored.alias===restored.typed&&restored.n===9n&&restored.typed[1]===600").is_true()); + assert_eq!(resources.usage(ResourceClass::ExecutorBytes).used, 0); + } + #[test] + fn bounded_serializer_rejects_growth_and_releases_reservations() { + let mut isolate = crate::isolate::create_isolate(Some(32)); + let context = crate::isolate::create_context(&mut isolate); + v8::scope!(let scope, &mut *isolate); + let context = v8::Local::new(scope, &context); + let scope = &mut v8::ContextScope::new(scope, context); + let resources = ledger(256); + let value = eval(scope, "'x'.repeat(1024)"); + let result = serialize( + scope, + value, + 4096, + "limits.jsRuntime.v8SerializationLimitBytes", + resources.clone(), + ); + assert!(result + .unwrap_err() + .contains("runtime.blocking.maxQueuedBytes")); + assert_eq!(resources.usage(ResourceClass::ExecutorBytes).used, 0); + v8::tc_scope!(let scope, scope); + assert!(serialize( + scope, + value, + 128, + "limits.jsRuntime.v8SerializationLimitBytes", + ledger(1024 * 1024) + ) + .is_err()); + assert!(scope.has_caught()); + } + #[test] + fn bounded_serializer_copies_only_visible_and_length_tracking_bytes() { + let mut isolate = crate::isolate::create_isolate(Some(32)); + let context = crate::isolate::create_context(&mut isolate); + v8::scope!(let scope, &mut *isolate); + let context = v8::Local::new(scope, &context); + let scope = &mut v8::ContextScope::new(scope, context); + let value = eval(scope, "(()=>{const b=new ArrayBuffer(4,{maxByteLength:16});const v=new Uint8Array(b);b.resize(8);v.set([1,2,3,4,5,6,7,8]);return v})()"); + let view = v8::Local::::try_from(value).unwrap(); + assert_eq!( + copy_view(scope, view).unwrap(), + vec![1, 2, 3, 4, 5, 6, 7, 8] + ); + let wire = serialize( + scope, + value, + 128, + "limits.jsRuntime.v8SerializationLimitBytes", + ledger(1024), + ) + .unwrap(); + let restored = deserialize( + scope, + wire, + 128, + "limits.jsRuntime.v8SerializationLimitBytes", + ledger(1024), + ) + .unwrap(); + assert_eq!( + copy_view( + scope, + v8::Local::::try_from(restored).unwrap() + ) + .unwrap(), + vec![1, 2, 3, 4, 5, 6, 7, 8] + ); + let value = eval( + scope, + "(()=>{const all=new Uint8Array([99,98,1,2,97]);return all.subarray(2,4)})()", + ); + let wire = serialize( + scope, + value, + 128, + "limits.jsRuntime.v8SerializationLimitBytes", + ledger(1024), + ) + .unwrap(); + let raw = copy_view( + scope, + v8::Local::::try_from(wire).unwrap(), + ) + .unwrap(); + assert!(!raw.contains(&99) && !raw.contains(&98) && !raw.contains(&97)); + } +} diff --git a/crates/executor-v8-runtime/src/owned_microtasks.rs b/crates/executor-v8-runtime/src/owned_microtasks.rs index 4d9df62120..7960caba9f 100644 --- a/crates/executor-v8-runtime/src/owned_microtasks.rs +++ b/crates/executor-v8-runtime/src/owned_microtasks.rs @@ -7,7 +7,7 @@ use std::rc::Rc; const MAX_FAMILY_CONTEXTS: usize = 1026; struct Family { - queue: v8::UniqueRef, + queue: v8::MicrotaskQueueHandle, default_queue: *const v8::MicrotaskQueue, contexts: Vec>, policy: v8::MicrotasksPolicy, @@ -19,16 +19,16 @@ struct FamilySlot(Rc>); /// Ensures custom queues are detached before the underlying isolate is disposed, /// including early returns and Rust unwinding. No realm-valued native root is added. -pub(crate) struct SessionIsolate(Option); +pub(crate) struct SessionIsolate(Option); impl SessionIsolate { - pub(crate) fn new(isolate: v8::OwnedIsolate) -> Self { + pub(crate) fn new(isolate: crate::isolate::GuestIsolate) -> Self { Self(Some(isolate)) } } impl std::ops::Deref for SessionIsolate { - type Target = v8::OwnedIsolate; + type Target = v8::Isolate; fn deref(&self) -> &Self::Target { self.0.as_ref().expect("live session isolate") } @@ -48,9 +48,12 @@ impl Drop for SessionIsolate { let mut family = slot.0.borrow_mut(); assert!(!family.queue.is_running_microtasks()); assert_eq!(family.queue.get_microtasks_scope_depth(), 0); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); for weak in &family.contexts { - if let Some(context) = weak.to_local(scope) { + if let Some(context) = weak + .to_local(scope) + .filter(|c| c.get_microtask_queue().is_some()) + { // Saved from the first context; the default queue belongs to // this still-live isolate and outlives every custom queue. context.set_microtask_queue(unsafe { &*family.default_queue }); @@ -109,17 +112,21 @@ fn room(isolate: &mut v8::Isolate, slot: &FamilySlot) -> Result<(), ContextAdmis } pub(crate) fn create_root( - isolate: &mut v8::OwnedIsolate, + isolate: &mut v8::Isolate, ) -> Result, ContextAdmissionError> { let existing = isolate.get_slot::().cloned(); if let Some(slot) = &existing { room(isolate, slot)?; } - let scope = &mut v8::HandleScope::new(isolate); + v8::scope!(scope, isolate); let context = v8::Context::new(scope, Default::default()); let slot = existing.unwrap_or_else(|| { let policy = scope.get_microtasks_policy(); - let default_queue = context.get_microtask_queue() as *const _; + let default_queue = std::ptr::from_ref( + context + .get_microtask_queue() + .expect("a new root context is attached to its isolate's default queue"), + ); let slot = FamilySlot(Rc::new(RefCell::new(Family { queue: v8::MicrotaskQueue::new(scope, policy), default_queue, @@ -139,35 +146,40 @@ pub(crate) fn create_root( /// Called by the sole node:vm native context factory before guest code can run. pub(crate) fn create_child<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, mut options: v8::ContextOptions<'s>, ) -> Result, ContextAdmissionError> { let Some(slot) = scope.get_slot::().cloned() else { return Ok(v8::Context::new(scope, options)); }; let parent = scope.get_current_context(); - if !std::ptr::eq(parent.get_microtask_queue(), &*slot.0.borrow().queue) { + if !parent + .get_microtask_queue() + .is_some_and(|queue| std::ptr::eq(queue, &*slot.0.borrow().queue)) + { return Ok(v8::Context::new(scope, options)); } room(scope, &slot)?; let mut family = slot.0.borrow_mut(); - let queue = &mut *family.queue as *mut v8::MicrotaskQueue; + let queue = std::ptr::from_ref(&*family.queue).cast_mut(); options.microtask_queue = Some(queue); let context = v8::Context::new(scope, options); family.contexts.push(v8::Weak::new(scope, context)); Ok(context) } -pub(crate) fn is_owned(scope: &mut v8::HandleScope) -> bool { +pub(crate) fn is_owned(scope: &mut v8::PinScope<'_, '_>) -> bool { let Some(slot) = scope.get_slot::().cloned() else { return false; }; let context = scope.get_current_context(); - let owned = std::ptr::eq(context.get_microtask_queue(), &*slot.0.borrow().queue); + let owned = context + .get_microtask_queue() + .is_some_and(|queue| std::ptr::eq(queue, &*slot.0.borrow().queue)); owned } -pub(crate) fn checkpoint(scope: &mut v8::HandleScope) { +pub(crate) fn checkpoint(scope: &mut v8::PinScope<'_, '_>) { // A pending terminal owner must reach outer disposal before any queued job. // In particular, V8 forbids entering RunMicrotasks with delivered termination. if crate::terminal_callback::pending(scope) || scope.is_execution_terminating() { @@ -187,10 +199,13 @@ pub(crate) fn discard_failed_call(isolate: &mut v8::Isolate) { assert_eq!(family.queue.get_microtasks_scope_depth(), 0); let replacement = v8::MicrotaskQueue::new(isolate, family.policy); { - let scope = &mut v8::HandleScope::new(isolate); + v8::scope!(scope, isolate); family.contexts.retain(|context| !context.is_empty()); for weak in &family.contexts { - if let Some(context) = weak.to_local(scope) { + if let Some(context) = weak + .to_local(scope) + .filter(|c| c.get_microtask_queue().is_some()) + { context.set_microtask_queue(&replacement); let scope = &mut v8::ContextScope::new(scope, context); crate::next_ticks::discard(scope); diff --git a/crates/executor-v8-runtime/src/session.rs b/crates/executor-v8-runtime/src/session.rs index 4da12bb062..c0fc8ee3c9 100644 --- a/crates/executor-v8-runtime/src/session.rs +++ b/crates/executor-v8-runtime/src/session.rs @@ -288,7 +288,10 @@ impl SessionPauseControl { } #[cfg(not(test))] -extern "C" fn pause_isolate_interrupt(_isolate: &mut v8::Isolate, data: *mut std::ffi::c_void) { +extern "C" fn pause_isolate_interrupt( + _isolate: v8::UnsafeRawIsolatePtr, + data: *mut std::ffi::c_void, +) { // SAFETY: pause_session passes one Arc strong reference with into_raw for // this callback. V8 invokes each accepted interrupt exactly once. let control = unsafe { Arc::from_raw(data.cast::()) }; @@ -1870,6 +1873,9 @@ impl SessionManager { .sessions .drain() .filter_map(|(session_id, mut entry)| { + // V8 termination cannot wake a thread parked on our pause + // condition variable, including from an interrupt callback. + entry.pause_control.shutdown(); signal_execution_abort_durable( &entry.execution_abort, ExecutionAbortReason::Terminated, @@ -2344,7 +2350,7 @@ fn handle_late_session_message( } #[cfg(not(test))] -fn install_wasm_module_bytes_global<'s>(scope: &mut v8::HandleScope<'s>, bytes: &[u8]) -> bool { +fn install_wasm_module_bytes_global<'s>(scope: &mut v8::PinScope<'s, '_>, bytes: &[u8]) -> bool { let global = scope.get_current_context().global(scope); let Some(name) = v8::String::new(scope, "__agentOSWasmModuleBytes") else { return false; @@ -2362,11 +2368,7 @@ fn install_wasm_module_bytes_global<'s>(scope: &mut v8::HandleScope<'s>, bytes: /// to first Execute (when bridge code is known for snapshot lookup), and /// processes commands until shutdown. fn spawn_session_thread(assignment: SessionAssignment) -> std::io::Result> { - let name_prefix = if assignment.session_id.len() > 8 { - assignment.session_id[..8].to_string() - } else { - assignment.session_id.clone() - }; + let name_prefix: String = assignment.session_id.chars().take(8).collect(); // AGENTOS_THREAD_SITE: admitted-v8-session-executor thread::Builder::new() .name(format!("session-{}", name_prefix)) @@ -2919,7 +2921,7 @@ fn session_thread( }; if retain_context && retained_bridge_fns_installed { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); crate::async_context::clear_async_context(scope); @@ -2946,7 +2948,7 @@ fn session_thread( } if high_resolution_time { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); execution::install_high_resolution_time_global( @@ -2957,7 +2959,7 @@ fn session_thread( // Inject globals from last InjectGlobals payload if let Some(ref payload) = last_globals_payload { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); if let Err(error) = @@ -3044,7 +3046,7 @@ fn session_thread( let pending = Rc::new(bridge::PendingPromises::new()); let _active_bridge_execution; { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); _active_bridge_execution = bridge::activate_bridge_execution( @@ -3108,7 +3110,7 @@ fn session_thread( } if let Some(wasm_module_bytes) = wasm_module_bytes.as_ref() { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); if !install_wasm_module_bytes_global(scope, wasm_module_bytes) { @@ -3272,7 +3274,7 @@ fn session_thread( // through the same guard cancellation and retirement as // ordinary execution failures; no early result bypass. let initialization_error = { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); match crate::guest_bridge_policy::prepare( @@ -3332,7 +3334,7 @@ fn session_thread( if let Some(error) = initialization_error { (1, None, Some(error)) } else if !module_mode { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); crate::async_context::clear_async_context(scope); @@ -3346,7 +3348,7 @@ fn session_thread( ); (c, None, e) } else { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); crate::async_context::clear_async_context(scope); @@ -3366,7 +3368,7 @@ fn session_thread( // needing a bridge event-loop round-trip. if module_mode && error.is_none() && !crate::terminal_callback::pending(iso) { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); if let Some((next_code, next_exports, next_error)) = @@ -3383,7 +3385,7 @@ fn session_thread( // Retire its queued callbacks before handle cleanup performs // a checkpoint; cleanup may still schedule its own work. if complete_on_settle && error.is_some() { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); crate::next_ticks::discard(scope); @@ -3400,7 +3402,7 @@ fn session_thread( || execution::has_pending_module_evaluation() || execution::has_pending_script_evaluation()); let event_loop_status = if should_enter_event_loop { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); run_event_loop_with_readiness( @@ -3439,7 +3441,7 @@ fn session_thread( && error.is_none() && !crate::terminal_callback::pending(iso) { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); if let Some((next_code, next_exports, next_error)) = @@ -3475,7 +3477,7 @@ fn session_thread( // Failures // after this point go to stderr; they never // rewrite the completed call's outcome. - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); if !module_mode && error.is_none() { @@ -3550,7 +3552,7 @@ fn session_thread( // keep-alive socket and prevent an otherwise-complete // one-shot script from ever exiting. { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); let global = ctx.global(scope); @@ -3582,7 +3584,7 @@ fn session_thread( if pending.has_refed_calls() || execution::has_pending_script_evaluation() { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); let event_loop_status = run_event_loop_with_readiness( @@ -3619,7 +3621,7 @@ fn session_thread( && error.is_none() && !crate::terminal_callback::pending(iso) { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); if let Some((next_code, next_error)) = @@ -3639,7 +3641,7 @@ fn session_thread( && error.is_none() && !crate::terminal_callback::pending(iso) { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); if let Some(process_exit_code) = @@ -3663,7 +3665,7 @@ fn session_thread( { terminated = true; } else { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let ctx = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, ctx); match execution::finalize_natural_process_exit(scope) { @@ -3690,7 +3692,7 @@ fn session_thread( crate::terminal_callback::result(iso) { if crate::terminal_callback::after_settle(iso) { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let context = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, context); execution::report_after_call_failure(scope, &terminal_error); @@ -3723,7 +3725,7 @@ fn session_thread( // Keep the original guards active while guest closers // run; external stops publish before V8 termination. if !invalidate_context { - let scope = &mut v8::HandleScope::new(iso); + v8::scope!(let scope, iso); let context = v8::Local::new(scope, &exec_context); let scope = &mut v8::ContextScope::new(scope, context); if let Err(cleanup_error) = @@ -3996,7 +3998,7 @@ pub fn reset_pending_promises(pending: &mut crate::bridge::PendingPromises) { } #[cfg(not(test))] -fn reset_process_exit_state(scope: &mut v8::HandleScope) -> Result<(), ExecutionError> { +fn reset_process_exit_state(scope: &mut v8::PinScope<'_, '_>) -> Result<(), ExecutionError> { let failure = || ExecutionError { error_type: "Error".into(), message: "could not reset call-owned process exit state".into(), @@ -4006,7 +4008,7 @@ fn reset_process_exit_state(scope: &mut v8::HandleScope) -> Result<(), Execution // Do not flush continuations before the new call's initialization. let mut suppression_storage = std::pin::pin!(execution::MicrotaskSuppressionStorage::new()); let mut explicit = execution::ExplicitMicrotaskScope::new(scope, suppression_storage.as_mut()); - let tc = &mut v8::TryCatch::new(&mut *explicit); + v8::tc_scope!(let tc, &mut *explicit); let global = tc.get_current_context().global(tc); let Some(key) = v8::String::new(tc, "__runtimeResetProcessExitState") else { return Err(failure()); @@ -4042,8 +4044,8 @@ fn reset_process_exit_state(scope: &mut v8::HandleScope) -> Result<(), Execution } } -fn retire_stdin_operation(scope: &mut v8::HandleScope) -> Result<(), ExecutionError> { - let tc = &mut v8::TryCatch::new(scope); +fn retire_stdin_operation(scope: &mut v8::PinScope<'_, '_>) -> Result<(), ExecutionError> { + v8::tc_scope!(let tc, scope); let context = tc.get_current_context(); let global = context.global(tc); let failure = |message: &str| ExecutionError { @@ -4094,7 +4096,7 @@ fn retire_stdin_operation(scope: &mut v8::HandleScope) -> Result<(), ExecutionEr /// Returns true if execution completed normally, false if terminated. #[doc(hidden)] pub fn run_event_loop( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, rx: &Receiver, pending: &crate::bridge::PendingPromises, abort_rx: Option<&crossbeam_channel::Receiver<()>>, @@ -4148,7 +4150,7 @@ enum EventLoopExit { } fn event_loop_can_exit( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, pending: &crate::bridge::PendingPromises, exit: EventLoopExit, ) -> bool { @@ -4170,7 +4172,7 @@ fn event_loop_can_exit( } fn run_event_loop_with_readiness( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, sources: EventLoopSources<'_>, pending: &crate::bridge::PendingPromises, ) -> EventLoopStatus { @@ -4494,7 +4496,7 @@ impl IdleHeapReduction { self.checked_this_idle_period = false; } - fn on_idle_tick(&mut self, scope: &mut v8::HandleScope) { + fn on_idle_tick(&mut self, scope: &mut v8::PinScope<'_, '_>) { if self.checked_this_idle_period || self.idle_since.elapsed() < IDLE_HEAP_REDUCTION_DELAY { return; } @@ -4518,9 +4520,8 @@ impl IdleHeapReduction { } } -fn used_heap_bytes(scope: &mut v8::HandleScope) -> usize { - let mut statistics = v8::HeapStatistics::default(); - scope.get_heap_statistics(&mut statistics); +fn used_heap_bytes(scope: &mut v8::PinScope<'_, '_>) -> usize { + let statistics = scope.get_heap_statistics(); statistics.used_heap_size() } @@ -4545,7 +4546,7 @@ fn report_unexplained_abort_lane_disconnect( } fn try_recv_session_command( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, rx: &Receiver, ready_rx: &Receiver, ready_broker: &SessionReadiness, @@ -4606,7 +4607,7 @@ fn try_recv_session_command( } fn dispatch_session_command( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, cmd: SessionCommand, pending: &crate::bridge::PendingPromises, ready_broker: &SessionReadiness, @@ -4631,7 +4632,7 @@ fn dispatch_session_command( /// return reaches `complete_ready_batch_dispatch` before control returns to the /// reusable session loop. fn dispatch_ready_batch( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, batch: RuntimeReadyBatch, ready_broker: &SessionReadiness, ) -> EventLoopStatus { @@ -4642,13 +4643,13 @@ fn dispatch_ready_batch( } fn dispatch_ready_batch_callbacks( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, batch: &RuntimeReadyBatch, ready_broker: &SessionReadiness, delivered: &mut Vec, ) -> EventLoopStatus { for entry in &batch.entries { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(let tc, scope); let dispatch = crate::stream::dispatch_readiness( tc, entry.capability_id, @@ -4695,7 +4696,7 @@ fn dispatch_ready_batch_callbacks( let Some(signal_name) = signal_name_for_stream_event(signal.signal) else { continue; }; - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(let tc, scope); crate::stream::dispatch_signal_event( tc, signal_name, @@ -4721,7 +4722,7 @@ fn dispatch_ready_batch_callbacks( Err(error) => return readiness_dispatch_failure(error), }; for timer_id in timers { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(let tc, scope); crate::stream::dispatch_timer_event(tc, timer_id); crate::owned_microtasks::checkpoint(tc); if let Some((code, error)) = crate::terminal_callback::result(tc) { @@ -4803,8 +4804,8 @@ fn signal_name_for_stream_event(signal: i32) -> Option<&'static str> { } } -fn pending_guest_timer_count(scope: &mut v8::HandleScope) -> usize { - let tc = &mut v8::TryCatch::new(scope); +fn pending_guest_timer_count(scope: &mut v8::PinScope<'_, '_>) -> usize { + v8::tc_scope!(let tc, scope); let context = tc.get_current_context(); let global = context.global(tc); let key = match v8::String::new(tc, "_getPendingTimerCount") { @@ -4827,8 +4828,8 @@ fn pending_guest_timer_count(scope: &mut v8::HandleScope) -> usize { .unwrap_or(0) } -fn pending_guest_immediate_count(scope: &mut v8::HandleScope) -> usize { - let tc = &mut v8::TryCatch::new(scope); +fn pending_guest_immediate_count(scope: &mut v8::PinScope<'_, '_>) -> usize { + v8::tc_scope!(let tc, scope); let context = tc.get_current_context(); let global = context.global(tc); let key = match v8::String::new(tc, "_getPendingImmediateCount") { @@ -4851,10 +4852,10 @@ fn pending_guest_immediate_count(scope: &mut v8::HandleScope) -> usize { .unwrap_or(0) } -fn drain_guest_immediates(scope: &mut v8::HandleScope) -> EventLoopStatus { +fn drain_guest_immediates(scope: &mut v8::PinScope<'_, '_>) -> EventLoopStatus { let mut suppression_storage = std::pin::pin!(execution::MicrotaskSuppressionStorage::new()); let mut explicit = execution::ExplicitMicrotaskScope::new(scope, suppression_storage.as_mut()); - let tc = &mut v8::TryCatch::new(&mut *explicit); + v8::tc_scope!(let tc, &mut *explicit); let context = tc.get_current_context(); let global = context.global(tc); let key = match v8::String::new(tc, "_drainImmediates") { @@ -4886,7 +4887,7 @@ fn drain_guest_immediates(scope: &mut v8::HandleScope) -> EventLoopStatus { } /// Drain runnable V8 foreground tasks. Returns whether any task ran. -fn pump_v8_message_loop(scope: &mut v8::HandleScope) -> bool { +fn pump_v8_message_loop(scope: &mut v8::PinScope<'_, '_>) -> bool { let platform = v8::V8::get_current_platform(); let mut ran = false; while v8::Platform::pump_message_loop(&platform, scope, false) { @@ -4932,7 +4933,7 @@ pub enum EventLoopStatus { } fn dispatch_event_loop_frame( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, frame: SessionMessage, pending: &crate::bridge::PendingPromises, ) -> EventLoopStatus { @@ -4970,7 +4971,7 @@ fn dispatch_event_loop_frame( event_type, payload, }) => { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(let tc, scope); crate::stream::dispatch_stream_event(tc, &event_type, &payload); crate::owned_microtasks::checkpoint(tc); if let Some((code, error)) = crate::terminal_callback::result(tc) { @@ -5045,7 +5046,7 @@ mod tests { Ok(()) } } - fn run(scope: &mut v8::HandleScope, source: &str) { + fn run(scope: &mut v8::PinScope<'_, '_>, source: &str) { let source = v8::String::new(scope, source).unwrap(); let script = v8::Script::compile(scope, source, None).unwrap(); assert!(script.run(scope).is_some(), "stdin fixture must execute"); @@ -5054,7 +5055,7 @@ mod tests { let mut isolate = isolate::create_isolate(None); let context = isolate::create_context(&mut isolate); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); assert!( @@ -5626,6 +5627,34 @@ mod tests { } } + #[test] + fn security_shutdown_releases_paused_sessions() { + let mut mgr = test_manager(1); + mgr.create_session("paused-shutdown".into(), None, None, None) + .expect("create session"); + let control = Arc::clone(&mgr.sessions["paused-shutdown"].pause_control); + control.pause(); + let (done_tx, done_rx) = std::sync::mpsc::channel(); + let waiting = Arc::clone(&control); + let waiter = std::thread::spawn(move || { + waiting.wait_while_paused(); + done_tx.send(()).expect("report resumed waiter"); + }); + let handles = mgr.take_session_shutdown_handles(); + let resumed = done_rx.recv_timeout(Duration::from_millis(200)); + // Retire every test thread even when exercising the unfixed path. + control.shutdown(); + waiter.join().expect("join pause waiter"); + for handle in handles { + handle.join().expect("join session"); + } + assert!( + resumed.is_ok(), + "whole-runtime shutdown must wake paused sessions" + ); + assert_eq!(mgr.active_slot_count(), 0); + } + #[test] fn detach_session_clears_call_id_routes_for_session() { let mut mgr = test_manager(1); @@ -6239,7 +6268,7 @@ mod tests { let mut v8_isolate = isolate::create_isolate(None); let context = isolate::create_context(&mut v8_isolate); - let scope = &mut v8::HandleScope::new(&mut v8_isolate); + v8::scope!(let scope, &mut *v8_isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); @@ -6263,7 +6292,7 @@ mod tests { ); let source = format!("for (let i = 0; i < {REGISTERED}; i++) {{ _asyncFn(i); }}"); { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(let tc, scope); let code = v8::String::new(tc, &source).unwrap(); let script = v8::Script::compile(tc, code, None).unwrap(); assert!( diff --git a/crates/executor-v8-runtime/src/shared_source.rs b/crates/executor-v8-runtime/src/shared_source.rs index d25da04a63..e7877ced96 100644 --- a/crates/executor-v8-runtime/src/shared_source.rs +++ b/crates/executor-v8-runtime/src/shared_source.rs @@ -38,7 +38,7 @@ type Digest32 = [u8; 32]; extern "C" { fn agentos_v8_static_twobyte_resource(data: *const u16, length: usize) -> *mut c_void; fn agentos_v8_new_static_twobyte_string( - isolate: *mut v8::Isolate, + isolate: v8::UnsafeRawIsolatePtr, resource: *mut c_void, ) -> *const v8::String; } @@ -68,7 +68,7 @@ impl SharedSource { fn new_string<'s>( self, - scope: &mut v8::HandleScope<'s, ()>, + scope: &mut v8::PinScope<'s, '_, ()>, ) -> Option> { match self { Self::OneByte(constant) => v8::String::new_from_onebyte_const(scope, constant), @@ -78,7 +78,10 @@ impl SharedSource { // and is never freed. The string is created in `scope`'s handle // scope, and `Local` is a `repr(C)` wrapper of the handle pointer. unsafe { - let raw = agentos_v8_new_static_twobyte_string(isolate, resource); + let raw = agentos_v8_new_static_twobyte_string( + isolate.as_raw_isolate_ptr(), + resource, + ); let raw = std::ptr::NonNull::new(raw as *mut v8::String)?; Some(std::mem::transmute::< std::ptr::NonNull, @@ -279,22 +282,45 @@ fn shared_source_entry( /// Shared sources and the external reference table of one snapshot. A /// snapshot blob owns it and each restored isolate keeps it alive. pub(crate) struct SnapshotSources { - refs: SnapshotRefs, + refs: Arc, _backings: Vec>, } -enum SnapshotRefs { - Base(&'static v8::ExternalReferences), - Shared(Arc), +/// Immutable callback and external string resource addresses shared by snapshots. +/// The callbacks have process lifetime. SnapshotSources holds the string backing +/// allocations for as long as a snapshot or restored isolate uses the table. +struct SnapshotRefs(Box<[v8::ExternalReference]>); + +// SAFETY: only static callbacks and the immutable resource headers described +// above enter this table. Reading the table never mutates the referenced data. +unsafe impl Sync for SnapshotRefs {} +unsafe impl Send for SnapshotRefs {} + +impl SnapshotRefs { + fn new(sources: &[SharedSource]) -> Self { + let mut refs = crate::bridge::base_external_references(); + refs.extend(sources.iter().map(|source| v8::ExternalReference { + pointer: source.address(), + })); + refs.push(v8::ExternalReference { + pointer: std::ptr::null_mut(), + }); + Self(refs.into_boxed_slice()) + } +} + +fn base_refs() -> &'static Arc { + static BASE: OnceLock> = OnceLock::new(); + BASE.get_or_init(|| Arc::new(SnapshotRefs::new(&[]))) +} + +pub(crate) fn base_references() -> &'static [v8::ExternalReference] { + &base_refs().0 } impl SnapshotSources { - /// The external references for restoring this snapshot. - pub(crate) fn refs(&self) -> &v8::ExternalReferences { - match &self.refs { - SnapshotRefs::Base(refs) => refs, - SnapshotRefs::Shared(refs) => refs, - } + pub(crate) fn refs(&self) -> &[v8::ExternalReference] { + &self.refs.0 } } @@ -320,7 +346,7 @@ pub(crate) fn snapshot_sources(sources: &[&str]) -> Result( - scope: &mut v8::HandleScope<'s, ()>, + scope: &mut v8::PinScope<'s, '_, ()>, source: &str, ) -> Result, String> { if is_shareable(source) { @@ -335,12 +361,11 @@ pub(crate) fn snapshot_source_string<'s>( /// External references for a snapshot whose shared sources are `sources`, in /// order. Interned while in use, so each distinct table is built once. -fn external_refs_with_sources(sources: &[SharedSource]) -> SnapshotRefs { +fn external_refs_with_sources(sources: &[SharedSource]) -> Arc { if sources.is_empty() { - return SnapshotRefs::Base(crate::bridge::external_refs()); + return Arc::clone(base_refs()); } - static TABLES: OnceLock, Weak>>> = - OnceLock::new(); + static TABLES: OnceLock, Weak>>> = OnceLock::new(); let key: Vec = sources .iter() .map(|source| source.address() as usize) @@ -350,16 +375,10 @@ fn external_refs_with_sources(sources: &[SharedSource]) -> SnapshotRefs { .lock() .expect("external reference tables poisoned"); if let Some(table) = tables.get(&key).and_then(Weak::upgrade) { - return SnapshotRefs::Shared(table); + return table; } tables.retain(|_, table| table.strong_count() > 0); - let mut references = crate::bridge::base_external_references(); - for source in sources { - references.push(v8::ExternalReference { - pointer: source.address(), - }); - } - let table = Arc::new(v8::ExternalReferences::new(&references)); + let table = Arc::new(SnapshotRefs::new(sources)); tables.insert(key, Arc::downgrade(&table)); - SnapshotRefs::Shared(table) + table } diff --git a/crates/executor-v8-runtime/src/snapshot.rs b/crates/executor-v8-runtime/src/snapshot.rs index 7c713cd1b7..b525ca0078 100644 --- a/crates/executor-v8-runtime/src/snapshot.rs +++ b/crates/executor-v8-runtime/src/snapshot.rs @@ -139,12 +139,12 @@ const SNAPSHOT_VERSIONS_PREP: &str = r#" /// descriptive error (with the V8 exception message) on failure. `label` /// identifies the phase (e.g. "bridge code" / "userland code") in error text. fn run_snapshot_script( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, code: &str, label: &str, eager_compile: bool, ) -> Result<(), String> { - let try_catch = &mut v8::TryCatch::new(scope); + v8::tc_scope!(let try_catch, scope); let source = crate::shared_source::snapshot_source_string(try_catch, code) .map_err(|error| format!("failed to create V8 string for {label}: {error}"))?; // NOTE(perf, measured 2026-07-02): EagerCompile moves cost into the snapshot @@ -253,7 +253,7 @@ const RESTORE_BUILTIN_ROOTS: &str = r#"(() => { ]; })()"#; -fn builtin_roots<'s>(scope: &mut v8::HandleScope<'s>) -> Result, String> { +fn builtin_roots<'s>(scope: &mut v8::PinScope<'s, '_>) -> Result, String> { let source = v8::String::new(scope, RESTORE_BUILTIN_ROOTS).expect("static script"); v8::Script::compile(scope, source, None) .and_then(|script| script.run(scope)) @@ -262,7 +262,7 @@ fn builtin_roots<'s>(scope: &mut v8::HandleScope<'s>) -> Result, roots: v8::Local, ) -> Vec> { (0..roots.length()) @@ -298,7 +298,7 @@ fn builtin_property_names_of_restored_context() -> Result>, let mut isolate = crate::owned_microtasks::SessionIsolate::new(crate::isolate::create_isolate(None)); let names = { - let scope = &mut v8::HandleScope::new(&mut *isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); builtin_roots(scope).map(|roots| own_string_keys(scope, roots)) @@ -311,7 +311,7 @@ fn builtin_property_names_of_restored_context() -> Result>, /// names V8 adds on restore that it lacks. Call before any script runs, so a /// script that replaces a global does not change which objects are checked. fn restore_only_builtin_names( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, restored: &[HashSet], ) -> Result<(v8::Global, Vec>), String> { let roots = builtin_roots(scope)?; @@ -330,7 +330,7 @@ fn restore_only_builtin_names( /// keeps it; later lookups get the native builtin, as in a process where the /// builtin existed before userland ran. fn remove_restore_only_builtins( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, roots: &v8::Global, names: &[HashSet], ) -> Result<(), String> { @@ -432,10 +432,9 @@ fn create_snapshot_inner_in_process( .map(SnapshotUserland::sources) .unwrap_or_default(); let sources = snapshot_sources(bridge_code, &userland_sources)?; - // SAFETY: `sources` owns the table and outlives the creator isolate, which - // `create_blob` consumes before this function returns. - let refs: &'static v8::ExternalReferences = - unsafe { &*(sources.refs() as *const v8::ExternalReferences) }; + // The creator owns its small table copy. Source text remains owned by + // `sources` through create_blob, including on initialization failure. + let refs = std::borrow::Cow::Owned(sources.refs().to_vec()); // Builtins V8 adds to a context only when it restores one; userland must // not leave its own definitions of them in the snapshot. let restored_names = match userland { @@ -448,7 +447,7 @@ fn create_snapshot_inner_in_process( let mut isolate = v8::Isolate::snapshot_creator(Some(refs), Some(params)); crate::isolate::install_native_terminal_stack(&mut isolate, terminal_stack); let bridge_result = { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); // Errors are returned after `create_blob`: a snapshot creator must @@ -1000,7 +999,7 @@ fn read_u64_from_slice(input: &[u8]) -> Result<(u64, &[u8]), String> { /// Properties are set as READ_ONLY (not DONT_DELETE) so they remain /// configurable — inject_globals_from_payload can redefine them with /// READ_ONLY | DONT_DELETE after restore. -pub(crate) fn inject_snapshot_defaults(scope: &mut v8::HandleScope) -> Result<(), String> { +pub(crate) fn inject_snapshot_defaults(scope: &mut v8::PinScope<'_, '_>) -> Result<(), String> { let context = scope.get_current_context(); let global = context.global(scope); @@ -1108,63 +1107,34 @@ fn snapshot_sources( crate::shared_source::snapshot_sources(&shareable).map(Arc::new) } -/// The external reference table of a snapshot, owned by its isolate. It -/// also holds the snapshot's shared sources, so every isolate restored from -/// the snapshot keeps the source text it reads alive. -struct RestoreRefs(Arc); - -impl std::ops::Deref for RestoreRefs { - type Target = [isize]; - fn deref(&self) -> &Self::Target { - self.0.refs() - } -} - -impl std::borrow::Borrow<[isize]> for RestoreRefs { - fn borrow(&self) -> &[isize] { - self.0.refs() - } -} - /// Create a V8 isolate restored from a snapshot blob whose external /// references are the base table (`bridge::external_refs()`). Only valid for a /// snapshot without shared sources (every source under /// `shared_source::MIN_SHARED_SOURCE_BYTES`); everything else restores through /// [`create_isolate_from_cached_snapshot`]. #[cfg(any(test, feature = "test-support"))] -pub fn create_isolate_from_snapshot(blob: B, heap_limit_mb: Option) -> v8::OwnedIsolate -where - B: std::ops::Deref + std::borrow::Borrow<[u8]> + 'static, -{ - create_isolate_with_refs(blob, &**crate::bridge::external_refs(), heap_limit_mb) -} - -/// Create a V8 isolate restored from a cached snapshot, sharing its bytes and -/// its shared sources with every other isolate restored from it. -pub fn create_isolate_from_cached_snapshot( - blob: &SnapshotBlob, +pub fn create_isolate_from_snapshot( + bytes: impl Into>, heap_limit_mb: Option, -) -> v8::OwnedIsolate { - create_isolate_with_refs( - Arc::clone(&blob.bytes), - RestoreRefs(Arc::clone(&blob.sources)), +) -> crate::isolate::GuestIsolate { + let sources = snapshot_sources("", &[]).expect("empty snapshot source table"); + create_isolate_from_cached_snapshot( + &SnapshotBlob { + bytes: bytes.into(), + sources, + }, heap_limit_mb, ) } -fn create_isolate_with_refs(blob: B, refs: R, heap_limit_mb: Option) -> v8::OwnedIsolate -where - B: std::ops::Deref + std::borrow::Borrow<[u8]> + 'static, - R: std::ops::Deref + std::borrow::Borrow<[isize]> + 'static, -{ - init_v8_platform(); +/// Restore a guest isolate sharing the backing bytes and external strings +/// of this snapshot with other live isolates. +pub fn create_isolate_from_cached_snapshot( + blob: &SnapshotBlob, + heap_limit_mb: Option, +) -> crate::isolate::GuestIsolate { crate::isolate::prepare_current_thread(); - - // Same heap and ArrayBuffer caps and OOM guard as the fresh-isolate path, so - // a snapshot-restored isolate is never unbounded (F-003). - crate::isolate::create_guest_isolate(heap_limit_mb, |params| { - params.snapshot_blob(blob).external_references(refs) - }) + crate::isolate::create_guest_isolate(heap_limit_mb, Some(blob.clone())) } /// A factory for never-run isolates restored from `blob`, for module code @@ -1177,9 +1147,8 @@ pub fn pristine_isolate_factory( pub type SnapshotCacheKey = [u8; 32]; -/// A cached snapshot. rusty_v8 keeps the blob alive for each isolate's -/// lifetime, so isolates share one allocation instead of a blob-sized copy -/// each. +/// A cached snapshot. Each restored native isolate's shared allocator retains +/// the same startup bytes and external reference table through disposal. #[derive(Clone)] pub struct SnapshotBlob { bytes: Arc<[u8]>, @@ -1188,6 +1157,23 @@ pub struct SnapshotBlob { } impl SnapshotBlob { + /// The owner must retain this blob, or a clone, until V8 disposal completes. + pub(crate) unsafe fn apply_to_create_params( + &self, + params: v8::CreateParams, + ) -> v8::CreateParams { + // The allocator owns this blob through native disposal. The Arc bytes + // and the sentinel-terminated boxed table cannot move or be mutated. + let bytes: &'static [u8] = + unsafe { std::slice::from_raw_parts(self.bytes.as_ptr(), self.bytes.len()) }; + let table = self.sources.refs(); + let refs: &'static [v8::ExternalReference] = + unsafe { std::slice::from_raw_parts(table.as_ptr(), table.len()) }; + params + .snapshot_blob(bytes.into()) + .external_references(std::borrow::Cow::Borrowed(refs)) + } + /// Whether both handles share one blob allocation. pub fn ptr_eq(left: &Self, right: &Self) -> bool { Arc::ptr_eq(&left.bytes, &right.bytes) @@ -1432,8 +1418,8 @@ fn compute_snapshot_cache_key(bridge_code: &str, userland_code: Option<&str>) -> #[doc(hidden)] #[cfg(any(test, feature = "test-support"))] pub fn run_snapshot_consolidated_checks() { - fn eval(isolate: &mut v8::OwnedIsolate, code: &str) -> String { - let scope = &mut v8::HandleScope::new(isolate); + fn eval(isolate: &mut v8::Isolate, code: &str) -> String { + v8::scope!(let scope, isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new(scope, code).unwrap(); @@ -1613,7 +1599,7 @@ pub fn run_snapshot_consolidated_checks() { let blob = create_snapshot(bridge_code).expect("snapshot creation"); let mut isolate = create_isolate_from_snapshot(blob, None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -1659,7 +1645,7 @@ pub fn run_snapshot_consolidated_checks() { 2, "isolate must share the cached allocation instead of copying it" ); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -1689,7 +1675,7 @@ pub fn run_snapshot_consolidated_checks() { 2, "isolate must share the cached allocation instead of copying it" ); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -1750,7 +1736,7 @@ pub fn run_snapshot_consolidated_checks() { ); let pending = PendingPromises::new(); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -1798,7 +1784,7 @@ pub fn run_snapshot_consolidated_checks() { let blob = create_snapshot(bridge_code).expect("snapshot creation"); let mut isolate = create_isolate_from_snapshot(blob, None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -1840,10 +1826,12 @@ pub fn run_snapshot_consolidated_checks() { { use crate::bridge::register_stub_bridge_fns; - let mut snapshot_isolate = - v8::Isolate::snapshot_creator(Some(crate::bridge::external_refs()), None); + let mut snapshot_isolate = v8::Isolate::snapshot_creator( + Some(std::borrow::Cow::Borrowed(crate::bridge::external_refs())), + None, + ); { - let scope = &mut v8::HandleScope::new(&mut snapshot_isolate); + v8::scope!(let scope, &mut snapshot_isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -2039,7 +2027,7 @@ pub fn run_snapshot_consolidated_checks() { // Restore and verify default context has the bridge infrastructure let blob_bytes: Vec = blob.to_vec(); let mut isolate = create_isolate_from_snapshot(blob_bytes, None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -2137,7 +2125,7 @@ pub fn run_snapshot_consolidated_checks() { let pending = PendingPromises::new(); // Restore context and replace bridge functions - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -2191,7 +2179,7 @@ pub fn run_snapshot_consolidated_checks() { let blob = create_snapshot(bridge_code).expect("snapshot creation"); let mut isolate = create_isolate_from_snapshot(blob, None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -2246,7 +2234,7 @@ pub fn run_snapshot_consolidated_checks() { let blob = create_snapshot(bridge_code).expect("snapshot creation"); let mut isolate = create_isolate_from_snapshot(blob, None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -2283,7 +2271,7 @@ pub fn run_snapshot_consolidated_checks() { .expect("snapshot creation"); let mut isolate = create_isolate_from_cached_snapshot(&blob, None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -2324,7 +2312,7 @@ pub fn run_snapshot_consolidated_checks() { for throwing_spawn in [false, true] { let mut isolate = create_isolate_from_cached_snapshot(&blob, None); isolate.set_microtasks_policy(v8::MicrotasksPolicy::Explicit); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); let setup = r#" @@ -2406,11 +2394,8 @@ pub fn run_snapshot_consolidated_checks() { let setup = setup .replace("EARLY_FLAG", if early { "true" } else { "false" }) .replace("THROW_FLAG", if throwing_spawn { "true" } else { "false" }); - fn run_child_schedule( - scope: &mut v8::ContextScope, - code: &str, - ) -> String { - let tc = &mut v8::TryCatch::new(scope); + fn run_child_schedule(scope: &mut v8::PinScope<'_, '_>, code: &str) -> String { + v8::tc_scope!(let tc, scope); let source = v8::String::new(tc, code).unwrap(); let script = v8::Script::compile(tc, source, None) .expect("compile child schedule control"); @@ -2531,7 +2516,7 @@ pub fn run_snapshot_consolidated_checks() { // Restore A: set a session-specific global { let mut isolate = create_isolate_from_snapshot(blob_bytes.clone(), None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -2550,7 +2535,7 @@ pub fn run_snapshot_consolidated_checks() { // Restore B: session A's state should not be visible { let mut isolate = create_isolate_from_snapshot(blob_bytes.clone(), None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -2578,7 +2563,7 @@ pub fn run_snapshot_consolidated_checks() { // Evaluate a string in a fresh context on a restored isolate. Unlike the // function-level `eval`, this takes an existing ContextScope so successive // checks observe globals set by earlier scripts in the SAME context. - fn run_in(scope: &mut v8::ContextScope, code: &str) -> String { + fn run_in(scope: &mut v8::PinScope<'_, '_>, code: &str) -> String { let source = v8::String::new(scope, code).unwrap(); let script = v8::Script::compile(scope, source, None).unwrap(); let result = script.run(scope).unwrap(); @@ -2609,7 +2594,7 @@ pub fn run_snapshot_consolidated_checks() { // context. The userland top-level must NOT run again here. { let mut isolate = create_isolate_from_snapshot(blob_bytes.clone(), None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -2637,7 +2622,7 @@ pub fn run_snapshot_consolidated_checks() { // captured userland state but NOT session A's mutation (isolation). { let mut isolate = create_isolate_from_snapshot(blob_bytes.clone(), None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); @@ -2699,7 +2684,7 @@ pub fn run_snapshot_consolidated_checks() { .expect("build thread join"); let mut isolate = create_isolate_from_snapshot(blob_bytes, None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); let check = v8::String::new(scope, "String(globalThis.__xt.f())").unwrap(); @@ -2726,7 +2711,7 @@ pub fn run_snapshot_consolidated_checks() { // Session A: mutate a global, the captured SDK object, AND a built-in prototype. { let mut isolate = create_isolate_from_snapshot(blob_bytes.clone(), None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); let src = v8::String::new( @@ -2743,7 +2728,7 @@ pub fn run_snapshot_consolidated_checks() { // snapshot state but NONE of session A's mutations. { let mut isolate = create_isolate_from_snapshot(blob_bytes.clone(), None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); let check = v8::String::new( @@ -2801,7 +2786,7 @@ pub fn run_snapshot_consolidated_checks() { .expect("userland snapshot with a lazy process.versions getter"); let mut isolate = create_isolate_from_snapshot(blob, None); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(let scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); diff --git a/crates/executor-v8-runtime/src/snapshot_disk_cache.rs b/crates/executor-v8-runtime/src/snapshot_disk_cache.rs index 56723cd7cf..3a957cb8fa 100644 --- a/crates/executor-v8-runtime/src/snapshot_disk_cache.rs +++ b/crates/executor-v8-runtime/src/snapshot_disk_cache.rs @@ -60,7 +60,7 @@ pub(crate) fn cache_dir() -> Option { fn file_key(key: &SnapshotCacheKey) -> [u8; 32] { let mut hasher = Sha256::new(); hasher.update(BUILD_FINGERPRINT.as_bytes()); - hasher.update([u8::from(crate::isolate::wasm_exnref_enabled())]); + hasher.update(v8::V8::get_version().as_bytes()); hasher.update(key); hasher.finalize().into() } diff --git a/crates/executor-v8-runtime/src/stream.rs b/crates/executor-v8-runtime/src/stream.rs index 69f3961b4c..daf1dada2b 100644 --- a/crates/executor-v8-runtime/src/stream.rs +++ b/crates/executor-v8-runtime/src/stream.rs @@ -14,7 +14,7 @@ use crate::execution::{ExplicitMicrotaskScope, MicrotaskSuppressionStorage}; /// - "net_socket" → _netSocketDispatch /// - "signal" → __agentOsWasmSignalDispatch or _signalDispatch /// - "timer" → _timerDispatch -pub fn dispatch_stream_event(scope: &mut v8::HandleScope, event_type: &str, payload: &[u8]) { +pub fn dispatch_stream_event(scope: &mut v8::PinScope<'_, '_>, event_type: &str, payload: &[u8]) { let mut storage = std::pin::pin!(MicrotaskSuppressionStorage::new()); let mut explicit = ExplicitMicrotaskScope::new(scope, storage.as_mut()); let scope = &mut *explicit; @@ -46,7 +46,7 @@ pub fn dispatch_stream_event(scope: &mut v8::HandleScope, event_type: &str, payl let event_str = v8::String::new(scope, event_type).unwrap(); let payload_val = if !payload.is_empty() { let maybe_v8_payload = { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); crate::bridge::deserialize_v8_value(tc, payload).ok() }; match maybe_v8_payload { @@ -74,7 +74,7 @@ pub fn dispatch_stream_event(scope: &mut v8::HandleScope, event_type: &str, payl } pub fn dispatch_signal_event( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, signal_name: &str, signal: i32, delivery_token: u64, @@ -99,13 +99,13 @@ pub fn dispatch_signal_event( dispatch_stream_value(scope, "signal", payload.into()); } -pub fn dispatch_timer_event(scope: &mut v8::HandleScope, timer_id: u64) { +pub fn dispatch_timer_event(scope: &mut v8::PinScope<'_, '_>, timer_id: u64) { let timer_id = v8::Number::new(scope, timer_id as f64); dispatch_stream_value(scope, "timer", timer_id.into()); } fn dispatch_stream_value( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, event_type: &str, payload: v8::Local, ) { @@ -150,7 +150,7 @@ pub enum ReadinessDispatch { } pub fn dispatch_readiness( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, capability_id: u64, capability_generation: u64, flags: agentos_driver_tokio::readiness::ReadyFlags, diff --git a/crates/executor-v8-runtime/src/terminal_callback.rs b/crates/executor-v8-runtime/src/terminal_callback.rs index 55157b9b1a..197d173fc3 100644 --- a/crates/executor-v8-runtime/src/terminal_callback.rs +++ b/crates/executor-v8-runtime/src/terminal_callback.rs @@ -18,7 +18,7 @@ const DELIVERY_PRIVATE: &str = "agentos.native.terminal.entry"; struct DeliveryKey(Rc>); fn private_delivery_key<'s>( - scope: &mut v8::HandleScope<'s, ()>, + scope: &mut v8::PinScope<'s, '_, ()>, ) -> Option> { let name = v8::String::new(scope, DELIVERY_PRIVATE)?; Some(v8::Private::for_api(scope, Some(name))) @@ -134,7 +134,7 @@ impl TerminalInput { } pub(crate) fn request_process_exit( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, args: v8::FunctionCallbackArguments, _return_value: v8::ReturnValue, ) { @@ -160,19 +160,19 @@ pub(crate) fn request_process_exit( } pub(crate) fn report_terminal_callback( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, args: v8::FunctionCallbackArguments, _return_value: v8::ReturnValue, ) { record_terminal(scope, args.get(0), TerminalInput::Callback); } -pub(crate) fn record_exception(scope: &mut v8::HandleScope, original: v8::Local) { +pub(crate) fn record_exception(scope: &mut v8::PinScope<'_, '_>, original: v8::Local) { record_terminal(scope, original, TerminalInput::Callback); } fn record_terminal( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, original: v8::Local, input: TerminalInput, ) { @@ -222,7 +222,7 @@ fn record_terminal( }; // No mutable state borrow crosses this guest-observable classifier. let (kind, result, error_disposition) = { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); let classified = match input { TerminalInput::Callback => classify_terminal_exception(tc, original), TerminalInput::ProcessExit { code, diagnostic } => ( @@ -284,7 +284,7 @@ fn record_terminal( deliver_terminal_stop(scope); } -fn initialize_delivery_key_in_scope(scope: &mut v8::HandleScope<'_, ()>) -> Result<(), String> { +fn initialize_delivery_key_in_scope(scope: &mut v8::PinScope<'_, '_, ()>) -> Result<(), String> { if scope.get_slot::().is_none() { let key = private_delivery_key(scope).ok_or("terminal private key allocation failed")?; let key = DeliveryKey(Rc::new(v8::Global::new(scope, key))); @@ -297,7 +297,7 @@ fn initialize_delivery_key_in_scope(scope: &mut v8::HandleScope<'_, ()>) -> Resu // the scope-preserving helper: reopening from Isolate tries to activate its root. pub(crate) fn initialize_delivery_key(isolate: &mut v8::Isolate) { if isolate.get_slot::().is_none() { - let scope = &mut v8::HandleScope::new(&mut *isolate); + v8::scope!(scope, &mut *isolate); initialize_delivery_key_in_scope(scope) .expect("terminal private key initialized before guest entry"); } @@ -309,7 +309,7 @@ pub(crate) fn release_delivery_key(isolate: &mut v8::Isolate) { drop(isolate.remove_slot::()); } -pub(crate) fn validate_delivery(scope: &mut v8::HandleScope) -> Result<(), String> { +pub(crate) fn validate_delivery(scope: &mut v8::PinScope<'_, '_>) -> Result<(), String> { let key = scope .get_slot::() .map(|key| key.0.clone()) @@ -328,13 +328,13 @@ pub(crate) fn validate_delivery(scope: &mut v8::HandleScope) -> Result<(), Strin // Only a real JavaScript entry has the interrupt stack check needed to make // the recorded native stop uncatchable before this callback returns. The trusted // function is precompiled at context setup, not stored in FunctionTemplate data. -fn deliver_terminal_stop(scope: &mut v8::HandleScope) { +fn deliver_terminal_stop(scope: &mut v8::PinScope<'_, '_>) { if scope.is_execution_terminating() { return; } let mut suppression_storage = std::pin::pin!(MicrotaskSuppressionStorage::new()); let mut explicit = ExplicitMicrotaskScope::new(scope, suppression_storage.as_mut()); - let tc = &mut v8::TryCatch::new(&mut *explicit); + v8::tc_scope!(tc, &mut *explicit); let key = tc.get_slot::().map(|key| key.0.clone()); let barrier = key.and_then(|key| { let key = v8::Local::new(tc, &*key); @@ -378,7 +378,7 @@ fn deliver_terminal_stop(scope: &mut v8::HandleScope) { /// Install before bridge evaluation for both fresh and snapshot-created realms. /// The bridge captures/deletes this native hook; snapshots use bridge::external_refs. -pub(crate) fn install(scope: &mut v8::HandleScope) -> Result<(), String> { +pub(crate) fn install(scope: &mut v8::PinScope<'_, '_>) -> Result<(), String> { initialize_delivery_key_in_scope(scope)?; if scope.get_slot::().is_none() { scope.set_slot(TerminalCallbackOwner::default()); diff --git a/crates/executor-v8-runtime/src/uint8array_encoding.rs b/crates/executor-v8-runtime/src/uint8array_encoding.rs index b02386acb7..91aa5c3558 100644 --- a/crates/executor-v8-runtime/src/uint8array_encoding.rs +++ b/crates/executor-v8-runtime/src/uint8array_encoding.rs @@ -1,6 +1,6 @@ //! One V8-owned ASCII allocation per encoding, with no IPC or host resources. -fn range_error(scope: &mut v8::HandleScope, message: &str, code: &str) { +fn range_error(scope: &mut v8::PinScope<'_, '_>, message: &str, code: &str) { let message = v8::String::new(scope, message).unwrap(); let error = v8::Exception::range_error(scope, message); let object = error.to_object(scope).unwrap(); @@ -11,7 +11,7 @@ fn range_error(scope: &mut v8::HandleScope, message: &str, code: &str) { } pub(crate) fn encode<'s>( - scope: &mut v8::HandleScope<'s>, + scope: &mut v8::PinScope<'s, '_>, args: &v8::FunctionCallbackArguments<'s>, ) -> Result, String> { let input = v8::Local::::try_from(args.get(0)) diff --git a/crates/executor-v8-runtime/src/v8_array_buffer_allocator.cc b/crates/executor-v8-runtime/src/v8_array_buffer_allocator.cc new file mode 100644 index 0000000000..5cea8d8cdd --- /dev/null +++ b/crates/executor-v8-runtime/src/v8_array_buffer_allocator.cc @@ -0,0 +1,39 @@ +#include "v8-array-buffer.h" + +#include +#include +#include +#include + +namespace { +using Allocator = v8::ArrayBuffer::Allocator; + +// V8 asks for this limit before entering its allocation-failure GC path. +// The existing allocator still accounts for every live backing store. +class BoundedAllocator final : public Allocator { + public: + BoundedAllocator(std::unique_ptr inner, size_t limit) + : inner_(std::move(inner)), + limit_(std::min(limit, inner_->MaxAllocationSize())) {} + + size_t MaxAllocationSize() const override { return limit_; } + void* Allocate(size_t length) override { return inner_->Allocate(length); } + void* AllocateUninitialized(size_t length) override { + return inner_->AllocateUninitialized(length); + } + void Free(void* data, size_t length) override { inner_->Free(data, length); } + v8::PageAllocator* GetPageAllocator() override { + return inner_->GetPageAllocator(); + } + + private: + std::unique_ptr inner_; + const size_t limit_; +}; +} // namespace + +extern "C" Allocator* agentos_v8_limit_array_buffer_allocator(Allocator* inner, + size_t limit) { + std::unique_ptr owner(inner); + return new (std::nothrow) BoundedAllocator(std::move(owner), limit); +} diff --git a/crates/executor-v8-runtime/src/v8_bounded_serializer.cc b/crates/executor-v8-runtime/src/v8_bounded_serializer.cc new file mode 100644 index 0000000000..e47cbc9cf8 --- /dev/null +++ b/crates/executor-v8-runtime/src/v8_bounded_serializer.cc @@ -0,0 +1,149 @@ +#include +#include +#include +#include + +// Rusty V8 builds its archive with its own C++ standard library and ABI. +// Keep all STL return types on that side of the boundary. +extern "C" void v8__ValueSerializer__Release(v8::ValueSerializer*, uint8_t**, size_t*); + +// The pinned Rust V8 binding does not expose Delegate::ReallocateBufferMemory. +// Keep the allocation fuse here, inside V8's writer, rather than walking guest +// objects twice (getters can change them between a preflight and serialization). +namespace { +template v8::Local from_handle(const T* handle) { + static_assert(sizeof(v8::Local) == sizeof(handle)); + v8::Local local; + std::memcpy(&local, &handle, sizeof(local)); + return local; +} + +using Admit = bool (*)(void*, size_t); +class BoundedSerializer final : public v8::ValueSerializer::Delegate { + public: + BoundedSerializer(v8::Isolate* isolate, size_t limit, Admit admit, void* budget) + : isolate_(isolate), limit_(limit), admit_(admit), budget_(budget), writer(isolate, this) {} + + void ThrowDataCloneError(v8::Local message) override { + isolate_->ThrowException(v8::Exception::Error(message)); + } + + void* ReallocateBufferMemory(void* previous, size_t size, size_t* actual) override { + // V8 requests geometric spare capacity (2 * old capacity + 64). + // Keep logical wire limits unchanged while bounding that temporary growth. + const size_t allocation_limit = limit_ > (SIZE_MAX - 64) / 2 + ? SIZE_MAX : limit_ * 2 + 64; + if (size > allocation_limit || size > SIZE_MAX - capacity || + !admit_(budget_, capacity + size)) { + rejected = size; + *actual = 0; + return nullptr; + } + void* allocation = std::realloc(previous, size); + *actual = allocation ? size : 0; + if (!allocation) allocation_failed = true; + else capacity = size; + return allocation; + } + void FreeBufferMemory(void* buffer) override { std::free(buffer); } + + v8::Maybe WriteHostObject(v8::Isolate* isolate, v8::Local object) override { + if (!object->IsArrayBufferView()) return fail("Unsupported serialized host object"); + auto view = object.As(); + auto context = isolate->GetCurrentContext(); + v8::Local constructor, buffer; + if (!object->Get(context, v8::String::NewFromUtf8Literal(isolate, "constructor")).ToLocal(&constructor) || + !context->Global()->Get(context, v8::String::NewFromUtf8Literal(isolate, "Buffer")).ToLocal(&buffer)) { + return v8::Nothing(); + } + // All guest getters have completed before capturing the backing store and + // length. No guest code runs between this check and WriteRawBytes. + auto array_buffer = view->Buffer(); + if (array_buffer->WasDetached()) return fail("Cannot serialize a detached ArrayBufferView"); + const size_t length = view->ByteLength(); + if (length > limit_ || length > std::numeric_limits::max()) { + rejected = length; + return fail("Serialized view exceeds the configured byte limit"); + } + uint32_t kind; + if (object->IsUint8Array() && constructor->StrictEquals(buffer)) kind = 10; + else if (object->IsInt8Array()) kind = 0; + else if (object->IsUint8Array()) kind = 1; + else if (object->IsUint8ClampedArray()) kind = 2; + else if (object->IsInt16Array()) kind = 3; + else if (object->IsUint16Array()) kind = 4; + else if (object->IsInt32Array()) kind = 5; + else if (object->IsUint32Array()) kind = 6; + else if (object->IsFloat32Array()) kind = 7; + else if (object->IsFloat64Array()) kind = 8; + else if (object->IsDataView()) kind = 9; + else if (object->IsBigInt64Array()) kind = 11; + else if (object->IsBigUint64Array()) kind = 12; + else if (object->IsFloat16Array()) kind = 13; + else return fail("Unsupported serialized view"); + // The Local keeps the V8 allocation alive. Data and ByteLength + // are scalar ABI calls. GetBackingStore returns an ABI-dependent shared_ptr. + const size_t buffer_length = array_buffer->ByteLength(); + const size_t offset = view->ByteOffset(); + if (offset > buffer_length || length > buffer_length - offset) + return fail("Serialized view changed before copying"); + writer.WriteUint32(kind); + writer.WriteUint32(static_cast(length)); + if (length) writer.WriteRawBytes(static_cast(array_buffer->Data()) + offset, length); + return v8::Just(true); + } + + v8::Maybe fail(const char* message) { + ThrowDataCloneError(v8::String::NewFromUtf8(isolate_, message).ToLocalChecked()); + return v8::Nothing(); + } + v8::Isolate* isolate_; + size_t limit_; + Admit admit_; + void* budget_; + v8::ValueSerializer writer; + size_t capacity = 0; + size_t rejected = 0; + bool allocation_failed = false; +}; +} + +extern "C" bool agentos_v8_serialize_bounded( + const v8::Context* context_handle, const v8::Value* value_handle, + size_t limit, Admit admit, void* budget, uint8_t** bytes, size_t* length, + size_t* rejected, bool* allocation_failed) { + auto context = from_handle(context_handle); + auto value = from_handle(value_handle); + // Rust enters this context on the owning isolate thread before making the call. + BoundedSerializer serializer(v8::Isolate::GetCurrent(), limit, admit, budget); + serializer.writer.SetTreatArrayBufferViewsAsHostObjects(true); + serializer.writer.WriteHeaderForNodeWireFormat15(); + const bool ok = serializer.writer.WriteValue(context, value).FromMaybe(false); + *rejected = serializer.rejected; + *allocation_failed = serializer.allocation_failed; + if (!ok || serializer.rejected || serializer.allocation_failed) return false; + uint8_t* released_bytes = nullptr; + size_t released_length = 0; + v8__ValueSerializer__Release(&serializer.writer, &released_bytes, &released_length); + if (released_length > limit) { + *rejected = released_length; + std::free(released_bytes); + return false; + } + // Transfer exactly the logical bytes, not unused capacity, to ArrayBuffer. + if (released_length > SIZE_MAX - serializer.capacity || + !admit(budget, serializer.capacity + released_length)) { + *rejected = released_length; + std::free(released_bytes); + return false; + } + void* exact = std::realloc(released_bytes, released_length); + if (!exact) { + std::free(released_bytes); + *allocation_failed = true; + return false; + } + *bytes = static_cast(exact); + *length = released_length; + return true; +} diff --git a/crates/executor-v8-runtime/src/v8_thread_support.cc b/crates/executor-v8-runtime/src/v8_thread_support.cc index 8f294607da..047738f3bd 100644 --- a/crates/executor-v8-runtime/src/v8_thread_support.cc +++ b/crates/executor-v8-runtime/src/v8_thread_support.cc @@ -2,6 +2,14 @@ #define _GNU_SOURCE 1 #endif +#include +#if defined(__linux__) && defined(__x86_64__) +#include +#if defined(PKEY_DISABLE_WRITE) +#pragma weak pkey_set +#endif +#endif + #include "v8-platform.h" #include "v8-sandbox.h" @@ -9,8 +17,21 @@ extern "C" void agentos_v8_initialize_sandbox_hardware_before_thread_creation() v8::SandboxHardwareSupport::InitializeBeforeThreadCreation(); } -extern "C" void agentos_v8_set_default_thread_isolation_permissions() { - v8::ThreadIsolatedAllocator::SetDefaultPermissionsForSignalHandler(); +extern "C" int agentos_v8_set_default_thread_isolation_permissions(v8::Platform* platform) { + // The platform owns this allocator for its lifetime. A kernel without PKU + // gets a null allocator. Never change keys owned by a different embedder. + auto* allocator = platform->GetThreadIsolatedAllocator(); + if (!allocator) return 0; +#if defined(__linux__) && defined(__x86_64__) && defined(PKEY_DISABLE_WRITE) + if (allocator->Type() == v8::ThreadIsolatedAllocator::Type::kPkey && pkey_set) { + // Match the access rights used by V8's DefaultThreadIsolatedAllocator + // when it allocates its key. This also prepares pre-existing host threads. + const int key = allocator->Pkey(); + if (key <= 0) return EINVAL; + return pkey_set(key, PKEY_DISABLE_WRITE) == 0 ? 0 : errno; + } +#endif + return ENOTSUP; } @@ -35,7 +56,6 @@ extern "C" void agentos_v8_suppress_microtasks_end(void* storage) { } -#include #include #include #if defined(__linux__) diff --git a/crates/executor-v8-runtime/tests/event_loop.rs b/crates/executor-v8-runtime/tests/event_loop.rs index 53714b4c70..2cb7958215 100644 --- a/crates/executor-v8-runtime/tests/event_loop.rs +++ b/crates/executor-v8-runtime/tests/event_loop.rs @@ -66,7 +66,7 @@ impl Drop for EventLoopWatchdog { } fn run_event_loop_with_watchdog( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, rx: &Receiver, pending: &PendingPromises, ) -> EventLoopStatus { @@ -89,7 +89,7 @@ fn fresh_isolate_supports_rgi_emoji_unicode_sets() { let mut isolate = isolate::create_isolate(None); let context = isolate::create_context(&mut isolate); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new( @@ -122,7 +122,7 @@ fn event_loop_pumps_v8_platform_tasks_for_native_wasm_promises() { let (_tx, rx) = crossbeam_channel::unbounded::(); let mut bridge_cache = None; - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); @@ -190,7 +190,7 @@ fn event_loop_completes_native_async_wasm_instantiate_promises() { let (_tx, rx) = crossbeam_channel::unbounded::(); let mut bridge_cache = None; - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); @@ -259,7 +259,7 @@ fn event_loop_surfaces_native_async_wasm_compile_errors_without_hanging() { let (_tx, rx) = crossbeam_channel::unbounded::(); let mut bridge_cache = None; - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); @@ -323,7 +323,7 @@ fn event_loop_waits_for_refed_guest_timers_between_interval_ticks() { let (tx, rx) = crossbeam_channel::unbounded::(); let mut bridge_cache = None; - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, ctx); diff --git a/crates/executor-v8-runtime/tests/isolate_lifecycle_concurrency.rs b/crates/executor-v8-runtime/tests/isolate_lifecycle_concurrency.rs index a90b76f62f..a6e2b0c5de 100644 --- a/crates/executor-v8-runtime/tests/isolate_lifecycle_concurrency.rs +++ b/crates/executor-v8-runtime/tests/isolate_lifecycle_concurrency.rs @@ -14,7 +14,7 @@ const WASM_ADD_MODULE: &str = "new Uint8Array([0,97,115,109,1,0,0,0,1,7,1,96,2,1 fn run_add_in_fresh_isolate(left: i32, right: i32) -> i32 { let mut guest = isolate::create_isolate(None); let sum = { - let scope = &mut v8::HandleScope::new(&mut guest); + v8::scope!(let scope, &mut *guest); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); let source = format!( @@ -136,8 +136,8 @@ fn discover() -> agentos_executor_v8_runtime::module_snapshot::ModuleSnapshotGra .expect("discover graph") } -fn eval_i32(isolate: &mut v8::OwnedIsolate, code: &str) -> Option { - let scope = &mut v8::HandleScope::new(isolate); +fn eval_i32(isolate: &mut v8::Isolate, code: &str) -> Option { + v8::scope!(let scope, isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new(scope, code)?; diff --git a/crates/executor-v8-runtime/tests/shared_source_bound.rs b/crates/executor-v8-runtime/tests/shared_source_bound.rs index ef3a520945..1ad72d886e 100644 --- a/crates/executor-v8-runtime/tests/shared_source_bound.rs +++ b/crates/executor-v8-runtime/tests/shared_source_bound.rs @@ -16,8 +16,8 @@ fn userland(marker: u32) -> String { format!("globalThis.probe = function marker{marker}() {{ /* {padding} */ return {marker}; }};") } -fn probe_text(isolate: &mut v8::OwnedIsolate) -> String { - let scope = &mut v8::HandleScope::new(isolate); +fn probe_text(isolate: &mut v8::Isolate) -> String { + v8::scope!(scope, isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); let code = v8::String::new(scope, "probe.toString()").unwrap(); diff --git a/crates/executor-v8-runtime/tests/support/guest_bridge_policy.rs b/crates/executor-v8-runtime/tests/support/guest_bridge_policy.rs index eb76d83a57..f4d196f8b4 100644 --- a/crates/executor-v8-runtime/tests/support/guest_bridge_policy.rs +++ b/crates/executor-v8-runtime/tests/support/guest_bridge_policy.rs @@ -1,6 +1,6 @@ use super::*; -fn eval(scope: &mut v8::HandleScope, code: &str) -> String { +fn eval(scope: &mut v8::PinScope<'_, '_>, code: &str) -> String { let source = v8::String::new(scope, code).unwrap(); let result = v8::Script::compile(scope, source, None) .unwrap() @@ -8,7 +8,7 @@ fn eval(scope: &mut v8::HandleScope, code: &str) -> String { .unwrap(); result.to_rust_string_lossy(scope) } -fn check_history(scope: &mut v8::HandleScope, raised: bool) { +fn check_history(scope: &mut v8::PinScope<'_, '_>, raised: bool) { let result = eval( scope, r#"(() => { @@ -99,7 +99,7 @@ pub(crate) fn owner_controls() { let mut isolate = crate::isolate::create_isolate(None); let context = crate::isolate::create_context(&mut isolate); { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); crate::bridge::register_stub_bridge_fns( @@ -144,14 +144,14 @@ pub(crate) fn owner_controls() { let context = crate::isolate::create_context(&mut isolate); isolate.terminate_execution(); { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); assert!(prepare(scope, None, false, bridge, &mut None).is_err()); } isolate.cancel_terminate_execution(); { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); crate::bridge::register_stub_bridge_fns( @@ -204,7 +204,7 @@ pub(crate) fn owner_controls() { }; let context = crate::isolate::create_context(&mut isolate); { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); // First context also exercises legacy default(None); other @@ -234,7 +234,7 @@ pub(crate) fn owner_controls() { let mut isolate = crate::snapshot::create_isolate_from_cached_snapshot(&blob, None); let context = crate::isolate::create_context(&mut isolate); { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); let mut wrong = policy.clone(); diff --git a/crates/executor-v8-runtime/tests/support/module_evaluation_owner.rs b/crates/executor-v8-runtime/tests/support/module_evaluation_owner.rs index 046d7e77e6..5d79d05832 100644 --- a/crates/executor-v8-runtime/tests/support/module_evaluation_owner.rs +++ b/crates/executor-v8-runtime/tests/support/module_evaluation_owner.rs @@ -4,7 +4,7 @@ fn module_evaluation_transfer_controls() { for rejected_first in [false, true] { let mut isolate = isolate::create_isolate(None); let context = isolate::create_context(&mut isolate); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); let resolver = v8::PromiseResolver::new(scope).unwrap(); @@ -33,7 +33,7 @@ fn module_evaluation_transfer_controls() { { let mut isolate = isolate::create_isolate(None); let context = isolate::create_context(&mut isolate); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); let resolver = v8::PromiseResolver::new(scope).unwrap(); @@ -58,7 +58,7 @@ fn module_evaluation_transfer_controls() { for other_overflow in [false, true] { let mut isolate = isolate::create_isolate(None); let context = isolate::create_context(&mut isolate); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); let reason = v8::String::new(scope, "bounded safeguard").unwrap(); diff --git a/crates/executor-v8-runtime/tests/support/owned_microtasks.rs b/crates/executor-v8-runtime/tests/support/owned_microtasks.rs index 0d1227fb20..6b55a0a060 100644 --- a/crates/executor-v8-runtime/tests/support/owned_microtasks.rs +++ b/crates/executor-v8-runtime/tests/support/owned_microtasks.rs @@ -1,7 +1,7 @@ // Invoked by the existing consolidated native owner controls. use super::*; -fn run(scope: &mut v8::HandleScope, source: &str) -> Option> { +fn run(scope: &mut v8::PinScope<'_, '_>, source: &str) -> Option> { let source = v8::String::new(scope, source).unwrap(); let script = v8::Script::compile(scope, source, None).unwrap(); script.run(scope).map(|value| v8::Global::new(scope, value)) @@ -14,7 +14,7 @@ fn native_child_exit_control() { let root = create_root(isolate).unwrap(); let _call = crate::terminal_callback::begin_call(isolate, 1); { - let scope = &mut v8::HandleScope::new(&mut *isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &root); let scope = &mut v8::ContextScope::new(scope, context); crate::terminal_callback::install(scope).unwrap(); @@ -28,7 +28,7 @@ fn native_child_exit_control() { &bridge, &["_vmCreateContext", "_vmRunInContext"], ); - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); assert!(run(tc, "globalThis.box={stop:__agentOsRequestProcessExit,shared:{continued:false}};globalThis.childId=_vmCreateContext(box);try{_vmRunInContext(childId, 'stop(9);shared.continued=true', {}, box);globalThis.BAD_PARENT=true}catch{globalThis.BAD_CATCH=true}finally{globalThis.BAD_FINALLY=true}").is_none()); assert!(tc.has_terminated()); assert_eq!(crate::terminal_callback::result(tc).unwrap().0, 9); @@ -38,7 +38,7 @@ fn native_child_exit_control() { isolate.cancel_terminate_execution(); crate::terminal_callback::acknowledge_stop(isolate); { - let scope = &mut v8::HandleScope::new(&mut *isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &root); let scope = &mut v8::ContextScope::new(scope, context); let value = run(scope, "typeof BAD_PARENT==='undefined' && typeof BAD_CATCH==='undefined' && typeof BAD_FINALLY==='undefined' && box.shared.continued===false").unwrap(); @@ -48,11 +48,11 @@ fn native_child_exit_control() { // Exercise the real owner rather than manually sweeping a test registry. fn registry_lifecycle_eval( - isolate: &mut v8::OwnedIsolate, + isolate: &mut v8::Isolate, root: &v8::Global, source: &str, ) -> bool { - let scope = &mut v8::HandleScope::new(isolate); + v8::scope!(scope, isolate); let context = v8::Local::new(scope, root); let scope = &mut v8::ContextScope::new(scope, context); let bridge = crate::host_call::BridgeCallContext::new( @@ -130,7 +130,29 @@ fn registry_owner_retirement_controls() { } } +fn detached_context_has_no_job_queue() { + let mut owner = SessionIsolate::new(crate::isolate::create_isolate(None)); + let root = create_root(&mut owner).unwrap(); + { + v8::scope!(scope, &mut *owner); + let context = v8::Local::new(scope, &root); + let scope = &mut v8::ContextScope::new(scope, context); + crate::next_ticks::begin_entry(scope, true); + context.detach_global(); + assert!(context.get_microtask_queue().is_none()); + let error = crate::next_ticks::scheduled(scope).unwrap_err(); + assert!(error.starts_with("ERR_AGENTOS_DETACHED_CONTEXT")); + checkpoint(scope); + } + // Queue replacement and session teardown must both tolerate a detached + // context that is still reachable through an embedder handle. + discard_failed_call(&mut owner); + drop(root); + drop(owner); +} + pub(crate) fn retained_family_controls() { + detached_context_has_no_job_queue(); registry_owner_retirement_controls(); native_child_exit_control(); for restored in [false, true] { @@ -155,7 +177,7 @@ pub(crate) fn retained_family_controls() { let _call = crate::terminal_callback::begin_call(isolate, (index * 2 + 1) as u64); { - let scope = &mut v8::HandleScope::new(&mut *isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, root); let scope = &mut v8::ContextScope::new(scope, context); if !restored { @@ -206,7 +228,7 @@ pub(crate) fn retained_family_controls() { { let _call = crate::terminal_callback::begin_call(isolate, (index * 2 + 2) as u64); - let scope = &mut v8::HandleScope::new(&mut *isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, root); let scope = &mut v8::ContextScope::new(scope, context); assert!(run( @@ -231,7 +253,7 @@ pub(crate) fn retained_family_controls() { let isolate = &mut *owner; let root = create_root(isolate).unwrap(); for _ in 0..4096 { - let scope = &mut v8::HandleScope::new(&mut *isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &root); let scope = &mut v8::ContextScope::new(scope, context); for _ in 0..3 { diff --git a/crates/executor-v8-runtime/tests/support/promise_rejection_owner.rs b/crates/executor-v8-runtime/tests/support/promise_rejection_owner.rs index a043846e59..2fea939723 100644 --- a/crates/executor-v8-runtime/tests/support/promise_rejection_owner.rs +++ b/crates/executor-v8-runtime/tests/support/promise_rejection_owner.rs @@ -96,7 +96,7 @@ fn rejection_owner_controls() { let context = isolate::create_context(&mut isolate); let previous_policy=isolate.get_microtasks_policy(); let (code,error) = { - let scope=&mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx=v8::Local::new(scope,&context); let scope=&mut v8::ContextScope::new(scope,ctx); crate::async_context::install_async_context_globals(scope); @@ -119,7 +119,7 @@ fn rejection_owner_controls() { for(let i=0;i<{rejections};i++)Promise.reject(i);void 0; "#); let (code,error)={ - let scope=&mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx=v8::Local::new(scope,&context); let scope=&mut v8::ContextScope::new(scope,ctx); execute_script(scope,"",&script,&mut None) @@ -136,7 +136,7 @@ fn rejection_owner_controls() { let mut isolate=isolate::create_isolate(None); let context=isolate::create_context(&mut isolate); let (code,error)={ - let scope=&mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx=v8::Local::new(scope,&context); let scope=&mut v8::ContextScope::new(scope,ctx); execute_script(scope,"",r#" @@ -152,7 +152,7 @@ fn rejection_owner_controls() { } fn nested_rejection_owner_checkpoint( - scope: &mut v8::HandleScope, + scope: &mut v8::PinScope<'_, '_>, _args: v8::FunctionCallbackArguments, mut result: v8::ReturnValue, ) { @@ -180,7 +180,7 @@ fn rejection_owner_policy_restoration_controls() { isolate.set_microtasks_policy(policy); let context=isolate::create_context(&mut isolate); let (code,error)={ - let scope=&mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let ctx=v8::Local::new(scope,&context); let scope=&mut v8::ContextScope::new(scope,ctx); let callback=v8::Function::new(scope,nested_rejection_owner_checkpoint).expect("nested checkpoint callback"); diff --git a/crates/executor-v8-runtime/tests/support/terminal_callback_abort.rs b/crates/executor-v8-runtime/tests/support/terminal_callback_abort.rs index 048c06ee19..c11650aed7 100644 --- a/crates/executor-v8-runtime/tests/support/terminal_callback_abort.rs +++ b/crates/executor-v8-runtime/tests/support/terminal_callback_abort.rs @@ -7,7 +7,7 @@ pub(crate) fn terminal_callback_abort_controls() { let mut isolate = crate::isolate::create_isolate(None); // Compile before requesting termination; execution, not the asynchronous // request flag, proves interruption at V8's actual interrupt check. - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new( @@ -23,7 +23,7 @@ pub(crate) fn terminal_callback_abort_controls() { scope.terminate_execution(); assert!(!cancel_owned_callback_stop(scope, &abort)); { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); assert!( script.run(tc).is_none(), "external termination interrupts finite work" @@ -69,7 +69,7 @@ pub(crate) fn terminal_callback_abort_controls() { .expect("external stop reasserted after cancellation"); external.join().unwrap(); { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); assert!( script.run(tc).is_none(), "external termination interrupts finite work" @@ -100,7 +100,7 @@ fn native_process_exit_abort_controls() { let mut isolate = crate::isolate::create_isolate(None); let context = crate::isolate::create_context(&mut isolate); let _call = crate::terminal_callback::begin_call(&mut isolate, 1); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); crate::terminal_callback::install(scope).expect("terminal hooks installed"); @@ -127,7 +127,7 @@ fn native_process_exit_abort_controls() { assert!(!cancel_owned_callback_stop(scope, &abort)); assert_eq!(execution_abort_reason(&abort), Some(reason)); { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); assert!(finite.run(tc).is_none()); assert!(tc.has_terminated(), "external stop remains noncatchable"); } @@ -146,8 +146,10 @@ fn owned_queue_abort_fence_controls() { let isolate = &mut *owner; let context = crate::owned_microtasks::create_root(isolate).unwrap(); let queue = { - let scope = &mut v8::HandleScope::new(&mut *isolate); - v8::Local::new(scope, &context).get_microtask_queue() as *const v8::MicrotaskQueue + v8::scope!(scope, &mut *isolate); + v8::Local::new(scope, &context) + .get_microtask_queue() + .expect("attached test context") as *const v8::MicrotaskQueue }; let abort = new_execution_abort(); let (_active, _rx) = ActiveExecutionAbort::arm(&abort); @@ -155,8 +157,10 @@ fn owned_queue_abort_fence_controls() { isolate.terminate_execution(); assert!(!cancel_owned_callback_stop(isolate, &abort)); let after = { - let scope = &mut v8::HandleScope::new(&mut *isolate); - v8::Local::new(scope, &context).get_microtask_queue() as *const v8::MicrotaskQueue + v8::scope!(scope, &mut *isolate); + v8::Local::new(scope, &context) + .get_microtask_queue() + .expect("attached test context") as *const v8::MicrotaskQueue }; assert!( std::ptr::eq(queue, after), diff --git a/crates/executor-v8-runtime/tests/support/terminal_callback_owner.rs b/crates/executor-v8-runtime/tests/support/terminal_callback_owner.rs index acaa68e4f1..b92fe8a438 100644 --- a/crates/executor-v8-runtime/tests/support/terminal_callback_owner.rs +++ b/crates/executor-v8-runtime/tests/support/terminal_callback_owner.rs @@ -67,7 +67,7 @@ pub(crate) fn terminal_callback_owner_controls() { isolate.set_microtasks_policy(v8::MicrotasksPolicy::Explicit); let context = crate::isolate::create_context(&mut isolate); { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); if !restored { @@ -105,7 +105,7 @@ pub(crate) fn terminal_callback_owner_controls() { let mut original_weak = None; let sibling_weak; { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); let global = context.global(scope); @@ -177,13 +177,13 @@ pub(crate) fn terminal_callback_owner_controls() { .borrow() .occupied()); { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); scope.request_garbage_collection_for_testing(v8::GarbageCollectionType::Full); } assert!(sibling_weak.is_empty(), "unowned sibling must collect after same Local scope; restored={restored}, source={source}"); if let Some(original_weak) = original_weak.as_ref() { { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); scope.request_garbage_collection_for_testing(v8::GarbageCollectionType::Full); } assert!( @@ -204,7 +204,7 @@ pub(crate) fn terminal_callback_owner_controls() { ); if let Some(original_weak) = original_weak { { - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); scope.request_garbage_collection_for_testing(v8::GarbageCollectionType::Full); } let released = original_weak.is_empty(); @@ -231,7 +231,7 @@ pub(crate) fn terminal_callback_owner_controls() { .generation(); let _call = begin_call(&mut isolate, generation); mark_settled(&mut isolate); - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); install(scope).expect("native terminal hooks installed"); @@ -333,7 +333,7 @@ fn native_process_exit_owner_controls() { { let _call = begin_call(isolate, cycle * 2 + 1); { - let scope = &mut v8::HandleScope::new(&mut *isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); if !restored { @@ -368,7 +368,7 @@ fn native_process_exit_owner_controls() { crate::owned_microtasks::discard_failed_call(isolate); isolate.cancel_terminate_execution(); acknowledge_stop(isolate); - let scope = &mut v8::HandleScope::new(&mut *isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); crate::owned_microtasks::checkpoint(scope); @@ -385,7 +385,7 @@ fn native_process_exit_owner_controls() { assert!(result(isolate).is_none()); let _next_call = begin_call(isolate, cycle * 2 + 2); assert!(!is_process_exit(isolate), "next call never inherits exit"); - let scope = &mut v8::HandleScope::new(&mut *isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Local::new(scope, &context); let scope = &mut v8::ContextScope::new(scope, context); let code = v8::String::new( diff --git a/crates/executor-v8-runtime/tests/support/terminal_watchdog_fence.rs b/crates/executor-v8-runtime/tests/support/terminal_watchdog_fence.rs index 3f5fe3cabb..205c81d41c 100644 --- a/crates/executor-v8-runtime/tests/support/terminal_watchdog_fence.rs +++ b/crates/executor-v8-runtime/tests/support/terminal_watchdog_fence.rs @@ -17,7 +17,7 @@ pub(crate) fn terminal_watchdog_fence_controls() { let mut isolate = crate::isolate::create_isolate(None); // Compile before requesting termination; execution, not the asynchronous // request flag, proves interruption at V8's actual interrupt check. - let scope = &mut v8::HandleScope::new(&mut isolate); + v8::scope!(scope, &mut *isolate); let context = v8::Context::new(scope, Default::default()); let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new( @@ -63,7 +63,7 @@ pub(crate) fn terminal_watchdog_fence_controls() { firing.join().unwrap(); cancelling.join().unwrap(); { - let tc = &mut v8::TryCatch::new(scope); + v8::tc_scope!(tc, scope); assert!( script.run(tc).is_none(), "external termination interrupts finite work" diff --git a/crates/executor-wasm-v8/src/lib.rs b/crates/executor-wasm-v8/src/lib.rs index b610c0547a..55c4f42a14 100644 --- a/crates/executor-wasm-v8/src/lib.rs +++ b/crates/executor-wasm-v8/src/lib.rs @@ -481,13 +481,9 @@ fn wasm_event_retained_bytes(event: &WasmExecutionEvent) -> usize { envelope.saturating_add(bytes.len()) } WasmExecutionEvent::SyncRpcRequest(request) - | WasmExecutionEvent::HostCall { request, .. } => envelope - .saturating_add(request.method.len()) - .saturating_add(request.raw_bytes_args.values().map(Vec::len).sum::()) - // JSON args arrive through an independently frame-bounded bridge; - // retain a conservative envelope without reserializing attacker - // input merely to account it. - .saturating_add(4 * 1024), + | WasmExecutionEvent::HostCall { request, .. } => { + envelope.saturating_add(request.heap_bytes()) + } WasmExecutionEvent::SignalState { registration, .. } => envelope.saturating_add( registration .mask @@ -5680,6 +5676,25 @@ mod tests { assert_eq!(budget.usage(), (0, 0)); } + #[test] + fn wasm_pending_event_budget_charges_owned_json_capacity() { + let mut request = wasm_sync_rpc_request("read"); + let values = vec![serde_json::Value::Null; 200]; + let slots = values.capacity() * std::mem::size_of::(); + request.args.push(serde_json::Value::Array(values)); + let event = WasmExecutionEvent::SyncRpcRequest(request); + let charge = super::wasm_event_retained_bytes(&event); + assert!(charge >= std::mem::size_of::() + slots); + let budget = WasmPendingEventBudget::new(2, charge - 1); + let mut queue = WasmEventQueue::new(Arc::clone(&budget)); + assert!( + matches!(queue.push_back(event), Err(WasmExecutionError::PendingEventLimit { + limit_name: "limits.process.pendingEventBytes", observed, .. + }) if observed == charge) + ); + assert_eq!(budget.usage(), (0, 0)); + } + #[test] fn wasm_outer_and_internal_signal_events_share_one_budget() { let signal_event = WasmExecutionEvent::SignalState { diff --git a/crates/executor-wasm-wasmtime/src/lifecycle.rs b/crates/executor-wasm-wasmtime/src/lifecycle.rs index e83e8e19a6..41f76d503c 100644 --- a/crates/executor-wasm-wasmtime/src/lifecycle.rs +++ b/crates/executor-wasm-wasmtime/src/lifecycle.rs @@ -201,6 +201,9 @@ impl Control { fn interrupt(&self) { self.cancelled.store(true, Ordering::Release); + // The epoch callback cannot observe cancellation while its pause + // future is parked. Release that future before notifying it. + self.paused.store(false, Ordering::Release); self.started.store(true, Ordering::Release); self.start_notify.notify_waiters(); self.pause_notify.notify_waiters(); @@ -957,6 +960,7 @@ async fn run_loaded_module_bytes( ) })?); } + request.permission_tier = replacement.permission_tier?; request.argv = replacement.argv; request.env = replacement.env; environment_is_guest_visible = true; @@ -1152,6 +1156,65 @@ pub(super) async fn load_executable_image( Ok(bytes) } +/// Both replacement launchers use the kernel's commit acknowledgment. A missing +/// tier is a protocol error after commit, so callers must retire the old image. +pub(super) fn committed_exec_tier( + reply: HostCallReply, +) -> Result { + #[derive(serde::Deserialize)] + #[serde(rename_all = "camelCase")] + struct Commit { + committed: bool, + permission_tier: agentos_executor_wasm_abi::WasmPermissionTier, + } + if let HostCallReply::Json(value) = reply { + if let Ok(Commit { + committed: true, + permission_tier, + }) = serde_json::from_value(value) + { + return Ok(permission_tier); + } + } + Err(HostServiceError::new( + "EIO", + "exec commit omitted confirmation or a valid kernel permission tier", + )) +} + +#[cfg(test)] +#[test] +fn replacement_uses_only_a_confirmed_kernel_tier() { + use agentos_executor_wasm_abi::WasmPermissionTier::*; + use serde_json::json; + for (label, tier) in [ + ("isolated", Isolated), + ("read-only", ReadOnly), + ("read-write", ReadWrite), + ("full", Full), + ] { + assert_eq!( + committed_exec_tier(HostCallReply::Json( + json!({"committed": true, "permissionTier": label}) + )) + .unwrap(), + tier + ); + } + for incomplete in [ + json!({"committed": true}), + json!({"committed": false, "permissionTier": "full"}), + json!({"committed": true, "permissionTier": "unknown"}), + ] { + assert_eq!( + committed_exec_tier(HostCallReply::Json(incomplete)) + .unwrap_err() + .code, + "EIO" + ); + } +} + pub(super) async fn read_open_executable_image( host: &WasmtimeHostClient, open: HostCallReply, @@ -1380,6 +1443,34 @@ mod tests { use std::collections::BTreeMap; use std::path::PathBuf; + #[test] + fn interrupt_releases_a_parked_epoch_pause_waiter() { + use std::future::Future; + use std::task::{Context, Poll, Waker}; + + let control = Control::new(true, 1, Duration::from_secs(1)); + control.paused.store(true, Ordering::Release); + + // Match the epoch callback's YieldCustom future. Poll it once to + // register the notification before interrupting the execution. + let mut pause_waiter = std::pin::pin!(async { + loop { + let notified = control.pause_notify.notified(); + if !control.paused.load(Ordering::Acquire) { + break; + } + notified.await; + } + }); + let mut context = Context::from_waker(Waker::noop()); + assert_eq!(pause_waiter.as_mut().poll(&mut context), Poll::Pending); + + control.interrupt(); + + assert_eq!(pause_waiter.as_mut().poll(&mut context), Poll::Ready(())); + assert!(control.cancelled.load(Ordering::Acquire)); + } + #[test] fn queued_event_bytes_are_bounded_and_released_on_handoff() { let resources = Arc::new(ResourceLedger::root( diff --git a/crates/executor-wasm-wasmtime/src/linker/mod.rs b/crates/executor-wasm-wasmtime/src/linker/mod.rs index 24aa1f6565..a28091e364 100644 --- a/crates/executor-wasm-wasmtime/src/linker/mod.rs +++ b/crates/executor-wasm-wasmtime/src/linker/mod.rs @@ -100,6 +100,28 @@ pub fn validate_module_imports( Ok(()) } +/// Compile-time compatibility of one replacement image at each ABI tier. Exec +/// may narrow authority, and the sidecar chooses the tier at the commit point. +fn exec_import_tiers( + module: &Module, + threaded: bool, +) -> Result { + let valid = |tier| validate_module_imports(module, tier, threaded).is_ok(); + let tiers = crate::host::ExecImportTiers { + isolated: valid(WasmPermissionTier::Isolated), + read_only: valid(WasmPermissionTier::ReadOnly), + read_write: valid(WasmPermissionTier::ReadWrite), + full: valid(WasmPermissionTier::Full), + }; + if !(tiers.isolated || tiers.read_only || tiers.read_write || tiers.full) { + return Err(HostServiceError::new( + "ENOEXEC", + "replacement has unsupported imports", + )); + } + Ok(tiers) +} + // Name allowlisting does not establish linkability. Validate the exact ABI // signature before exec can commit destructive changes to the old image. fn import_type_supported(module: &str, name: &str, ty: wasmtime::ExternType) -> bool { @@ -672,6 +694,21 @@ mod tests { validate_module_imports(&module, WasmPermissionTier::Isolated, false) .expect("generated registry permits import"); + let tiers = exec_import_tiers(&module, false).expect("preflight generated import"); + assert_eq!( + tiers.isolated, + permitted(*allowed, WasmPermissionTier::Isolated) + ); + assert_eq!( + tiers.read_only, + permitted(*allowed, WasmPermissionTier::ReadOnly) + ); + assert_eq!( + tiers.read_write, + permitted(*allowed, WasmPermissionTier::ReadWrite) + ); + assert_eq!(tiers.full, permitted(*allowed, WasmPermissionTier::Full)); + let hostile = Module::new( &engine, wat::parse_str("(module (import \"ambient_host\" \"escape\" (func)))") diff --git a/crates/executor-wasm-wasmtime/src/linker/process.rs b/crates/executor-wasm-wasmtime/src/linker/process.rs index 76cd8236a3..fe773b4d42 100644 --- a/crates/executor-wasm-wasmtime/src/linker/process.rs +++ b/crates/executor-wasm-wasmtime/src/linker/process.rs @@ -683,14 +683,7 @@ async fn exec(caller: &mut Caller<'_, WasmtimeStoreState>, params: &[Val], by_fd engine.profile().feature_profile, crate::WasmtimeFeatureProfile::AgentOsOwnedWasiV1Threads ); - super::validate_module_imports( - &compiled, - agentos_executor_wasm_abi::WasmPermissionTier::Full, - threaded, - ) - .map_err(|_| { - crate::backend::HostServiceError::new("ENOEXEC", "replacement has unsupported imports") - })?; + let import_tiers = super::exec_import_tiers(&compiled, threaded)?; if !matches!(compiled.get_export("_start"), Some(wasmtime::ExternType::Func(ty)) if ty.params().next().is_none() && ty.results().next().is_none()) { @@ -699,9 +692,9 @@ async fn exec(caller: &mut Caller<'_, WasmtimeStoreState>, params: &[Val], by_fd "replacement has no valid _start", )); } - Ok((compiled, resolved_argv)) + Ok((compiled, resolved_argv, import_tiers)) })(); - let (prepared_replacement, resolved_argv) = match prepared_replacement { + let (prepared_replacement, resolved_argv, import_tiers) = match prepared_replacement { Ok(prepared) => prepared, Err(error) => { if let Some(open) = retained_open.as_ref() { @@ -751,6 +744,7 @@ async fn exec(caller: &mut Caller<'_, WasmtimeStoreState>, params: &[Val], by_fd "cloexecFds": close_fds, "execSignalThreadId": caller.data().thread_id, "localReplacement": true, + "validatedImportTiers": import_tiers, "executableFd": executable_fd, "internalBootstrapEnv": {}, } @@ -806,30 +800,33 @@ async fn exec(caller: &mut Caller<'_, WasmtimeStoreState>, params: &[Val], by_fd } else { call(caller, method, vec![request], HashMap::new()).await }; - match result { - Ok(_) => { - record_exec_replacement( - caller, - Some(PendingExecReplacement { - module: prepared_replacement, - argv, - env, - }), - ); - ERRNO_IO - } + // A successful commit or a post-commit cleanup failure must unwind the + // old guest before reporting an error or starting the replacement. + let permission_tier = match result { + Ok(reply) => lifecycle::committed_exec_tier(reply), + Err(error) if error.code == "ERR_AGENTOS_WASM_EXEC_COMMITTED_FAILURE" => Err(error), Err(error) if error.code == "ERR_AGENTOS_EXEC_REPLACED" => { record_exec_replacement(caller, None); - ERRNO_IO + return ERRNO_IO; } Err(error) => { eprintln!( "ERR_AGENTOS_WASMTIME_EXEC_COMMIT: method={method} command={command:?} code={} message={}", error.code, error.message ); - errno(&error) + return errno(&error); } - } + }; + record_exec_replacement( + caller, + Some(PendingExecReplacement { + module: prepared_replacement, + permission_tier, + argv, + env, + }), + ); + ERRNO_IO } fn record_exec_replacement( diff --git a/crates/executor-wasm-wasmtime/src/store.rs b/crates/executor-wasm-wasmtime/src/store.rs index 61be4f1a9a..ceb1fe8e2f 100644 --- a/crates/executor-wasm-wasmtime/src/store.rs +++ b/crates/executor-wasm-wasmtime/src/store.rs @@ -27,6 +27,7 @@ pub const DEFAULT_MAX_HOST_REPLY_BYTES: usize = 16 * 1024 * 1024; #[derive(Debug)] pub struct PendingExecReplacement { + pub permission_tier: Result, pub module: Arc, pub argv: Vec, pub env: BTreeMap, diff --git a/crates/executor-wasm-wasmtime/src/worker.rs b/crates/executor-wasm-wasmtime/src/worker.rs index 3c7502bc73..0c4473c684 100644 --- a/crates/executor-wasm-wasmtime/src/worker.rs +++ b/crates/executor-wasm-wasmtime/src/worker.rs @@ -741,6 +741,7 @@ fn remaining_cpu_after_handoff( } struct WorkerReplacement { + permission_tier: agentos_executor_wasm_abi::WasmPermissionTier, module: Vec, argv: Vec, env: std::collections::BTreeMap, @@ -790,6 +791,7 @@ pub(super) async fn run_worker_process( WorkerOutcome::Finished(code) => return Ok(code), WorkerOutcome::Replaced(replacement) => { module = replacement.module; + request.permission_tier = replacement.permission_tier; request.argv = replacement.argv; request.env = replacement.env; request.limits.active_cpu_time_limit_ms = replacement.remaining_cpu_ms; @@ -930,7 +932,18 @@ async fn run_worker_image( let outcome = match prepared { Ok((module, Some(argv))) => { match serde_json::from_value::>(exec_request["options"]["env"].clone()) { - Ok(env) => host.submit_adapter_call(method, vec![exec_request], HashMap::new()).await.map(|_| WorkerReplacement { module, argv, env, remaining_cpu_ms, cpu_sample, remaining_fuel }), + Ok(env) if matches!(method.as_str(), "process.exec" | "process.exec_fd_image_commit") && exec_request["options"]["localReplacement"] == true => { + match host.submit_adapter_call(method, vec![exec_request], HashMap::new()).await { + Err(error) if error.code == "ERR_AGENTOS_WASM_EXEC_COMMITTED_FAILURE" => break Err(error), + Err(error) => Err(error), + Ok(reply) => match super::lifecycle::committed_exec_tier(reply) { + Ok(permission_tier) => Ok(WorkerReplacement { permission_tier, module, argv, env, remaining_cpu_ms, cpu_sample, remaining_fuel }), + // The kernel may have committed: kill the old worker. + Err(error) => break Err(error), + }, + } + } + Ok(_) => Err(HostServiceError::new("EINVAL", "invalid local exec commit request")), Err(error) => Err(HostServiceError::new("EINVAL", format!("invalid exec environment: {error}"))), } } diff --git a/crates/vm-kernel/src/fd_table.rs b/crates/vm-kernel/src/fd_table.rs index b63bd51ea9..4e570d0da9 100644 --- a/crates/vm-kernel/src/fd_table.rs +++ b/crates/vm-kernel/src/fd_table.rs @@ -61,8 +61,9 @@ pub const WASI_RIGHT_FD_WRITE: u64 = 1 << 6; pub const WASI_RIGHT_FD_ADVISE: u64 = 1 << 7; pub const WASI_RIGHT_FD_ALLOCATE: u64 = 1 << 8; pub const WASI_RIGHT_PATH_CREATE_DIRECTORY: u64 = 1 << 9; -pub const WASI_RIGHT_PATH_LINK_SOURCE: u64 = 1 << 10; -pub const WASI_RIGHT_PATH_LINK_TARGET: u64 = 1 << 11; +pub const WASI_RIGHT_PATH_CREATE_FILE: u64 = 1 << 10; +pub const WASI_RIGHT_PATH_LINK_SOURCE: u64 = 1 << 11; +pub const WASI_RIGHT_PATH_LINK_TARGET: u64 = 1 << 12; pub const WASI_RIGHT_PATH_OPEN: u64 = 1 << 13; pub const WASI_RIGHT_FD_READDIR: u64 = 1 << 14; pub const WASI_RIGHT_PATH_READLINK: u64 = 1 << 15; @@ -105,6 +106,7 @@ pub const WASI_PREOPEN_READ_WRITE_RIGHTS_BASE: u64 = WASI_PREOPEN_READ_RIGHTS_BA | WASI_RIGHT_FD_ADVISE | WASI_RIGHT_FD_ALLOCATE | WASI_RIGHT_PATH_CREATE_DIRECTORY + | WASI_RIGHT_PATH_CREATE_FILE | WASI_RIGHT_PATH_FILESTAT_SET_SIZE | WASI_RIGHT_PATH_FILESTAT_SET_TIMES | WASI_RIGHT_FD_FILESTAT_SET_SIZE diff --git a/crates/vm-kernel/src/kernel.rs b/crates/vm-kernel/src/kernel.rs index 391d3c7a0c..f1fc963384 100644 --- a/crates/vm-kernel/src/kernel.rs +++ b/crates/vm-kernel/src/kernel.rs @@ -7797,6 +7797,22 @@ impl KernelVm { )); } if let Some((base, inheriting)) = requested_rights { + if flags & O_CREAT != 0 + && parent.rights & crate::fd_table::WASI_RIGHT_PATH_CREATE_FILE == 0 + { + return Err(KernelError::new( + "EACCES", + "path_open requires PATH_CREATE_FILE for O_CREAT", + )); + } + if flags & O_TRUNC != 0 + && parent.rights & crate::fd_table::WASI_RIGHT_PATH_FILESTAT_SET_SIZE == 0 + { + return Err(KernelError::new( + "EACCES", + "path_open requires PATH_FILESTAT_SET_SIZE for O_TRUNC", + )); + } let unavailable = (base | inheriting) & !parent.rights_inheriting; if unavailable != 0 { return Err(KernelError::new( @@ -15149,7 +15165,7 @@ mod tests { WASI_PREOPEN_WRITE_RIGHTS_INHERITING ); - let child_fd = kernel + let denied = kernel .fd_open_with_rights( "wasm", pid, @@ -15159,6 +15175,34 @@ mod tests { Some(0o600), Some((WASI_RIGHT_FD_READ | WASI_RIGHT_FD_WRITE, 0)), ) + .expect_err("inheriting write rights alone cannot create a directory entry"); + assert_eq!(denied.code(), "EACCES"); + assert!(!kernel.exists("/cap/child").expect("check child path")); + + let create_fd = kernel + .fd_open_with_rights( + "wasm", + pid, + Some(root_fd), + "/cap", + O_DIRECTORY | O_RDONLY, + None, + Some(( + WASI_PREOPEN_READ_RIGHTS_BASE | crate::fd_table::WASI_RIGHT_PATH_CREATE_FILE, + WASI_PREOPEN_WRITE_RIGHTS_INHERITING, + )), + ) + .expect("open nested directory with create permission"); + let child_fd = kernel + .fd_open_with_rights( + "wasm", + pid, + Some(create_fd), + "/cap/child", + O_CREAT | O_RDWR, + Some(0o600), + Some((WASI_RIGHT_FD_READ | WASI_RIGHT_FD_WRITE, 0)), + ) .expect("openat through ordinary POSIX directory fd"); assert!( kernel diff --git a/crates/vm/Cargo.toml b/crates/vm/Cargo.toml index ef90fba20e..d620549e67 100644 --- a/crates/vm/Cargo.toml +++ b/crates/vm/Cargo.toml @@ -215,7 +215,7 @@ tar = "0.4" tempfile = "3" vbare.workspace = true wat = "1.0" -v8 = "130" +v8 = { workspace = true } [[test]] name = "bidirectional_frames" @@ -231,7 +231,7 @@ required-features = ["runtime"] [[test]] name = "builtin_conformance" -required-features = ["runtime"] +required-features = ["runtime", "node-v8"] [[test]] name = "connection_auth" diff --git a/crates/vm/src/execution/child_process.rs b/crates/vm/src/execution/child_process.rs index d1fd8d0b79..719921d2a7 100644 --- a/crates/vm/src/execution/child_process.rs +++ b/crates/vm/src/execution/child_process.rs @@ -4277,6 +4277,71 @@ pub(super) fn validate_process_launch_request( Ok(()) } +/// Only the trusted kernel selects authority. Verify image compatibility under +/// the same VM borrow as commit, including policy changes since image open. +fn validate_in_place_exec_tier( + backend: ExecutionStandaloneWasmBackend, + effective: ProcessPermissionTier, + tiers: Option, +) -> Result<(), VmError> { + if backend == ExecutionStandaloneWasmBackend::V8 { + return Ok(()); + } + let tiers = tiers + .ok_or_else(|| VmError::host("EINVAL", "Wasmtime exec requires validated import tiers"))?; + let admitted = match effective { + ProcessPermissionTier::Isolated => tiers.isolated, + ProcessPermissionTier::ReadOnly => tiers.read_only, + ProcessPermissionTier::ReadWrite => tiers.read_write, + ProcessPermissionTier::Full => tiers.full, + }; + if !admitted { + return Err(VmError::host( + "ENOEXEC", + "replacement imports are unavailable at the kernel permission tier", + )); + } + Ok(()) +} + +fn in_place_exec_cleanup_error(backend: ExecutionStandaloneWasmBackend, error: VmError) -> VmError { + if backend == ExecutionStandaloneWasmBackend::V8 { + return error; + } + tracing::error!(%error, "Wasm exec committed but replacement cleanup failed; retiring the old image"); + VmError::host("ERR_AGENTOS_WASM_EXEC_COMMITTED_FAILURE", error.to_string()) +} + +#[cfg(all(test, feature = "wasm-wasmtime"))] +#[test] +fn wasmtime_exec_uses_the_kernel_selected_tier_for_admission() { + use crate::executor::host::ExecImportTiers; + use ProcessPermissionTier::*; + let tiers = ExecImportTiers { + isolated: false, + read_only: true, + read_write: false, + full: true, + }; + for backend in [ + ExecutionStandaloneWasmBackend::Wasmtime, + ExecutionStandaloneWasmBackend::WasmtimeThreads, + ] { + for (tier, compatible) in [ + (Isolated, false), + (ReadOnly, true), + (ReadWrite, false), + (Full, true), + ] { + assert_eq!( + validate_in_place_exec_tier(backend, tier, Some(tiers)).is_ok(), + compatible + ); + assert!(validate_in_place_exec_tier(backend, tier, None).is_err()); + } + } +} + pub(super) fn validate_wasm_fd_image_commit_request( request: &ProcessLaunchRequest, ) -> Result<(), VmError> { @@ -7455,7 +7520,7 @@ where root_process_id: &str, process_path: &[&str], mut request: ProcessLaunchRequest, - ) -> Result<(), VmError> { + ) -> Result, VmError> { if request.options.executable_fd.is_some() { return Err(VmError::host( "EINVAL", @@ -7550,6 +7615,12 @@ where }; apply_child_process_argv0(&mut resolved, request.options.argv0.as_deref()); if resolved.host_function_command { + if request.options.local_replacement { + return Err(VmError::host( + "ENOEXEC", + "in-place Wasm replacement requires a WebAssembly image", + )); + } let process_event_capacity = self.config.runtime.protocol.max_process_events; let bridge = self.bridge.clone(); let sidecar_requests = self.sidecar_requests.clone(); @@ -7684,7 +7755,7 @@ where vm_pending_event_bytes_budget, event_notify, }); - return Ok(()); + return Ok(None); } if request.options.local_replacement && current_adapter_policy.supports_prepared_in_place_exec @@ -7768,6 +7839,19 @@ where .and_then(|process| process.kernel_stdin_writer_fd) .into_iter() .collect::>(); + let effective = vm + .kernel + .effective_exec_permission_tier( + EXECUTION_DRIVER_NAME, + kernel_pid, + requested_exec_permission_tier, + ) + .map_err(kernel_error)?; + validate_in_place_exec_tier( + standalone_wasm_backend, + effective, + request.options.validated_import_tiers, + )?; vm.kernel .exec_process_retaining_internal_fds_from_thread( EXECUTION_DRIVER_NAME, @@ -7783,19 +7867,28 @@ where Some(requested_exec_permission_tier), ) .map_err(kernel_error)?; - prune_managed_process_routes_without_aliases(&bridge, vm_id, &mut vm, kernel_pid)?; + prune_managed_process_routes_without_aliases(&bridge, vm_id, &mut vm, kernel_pid) + .map_err(|error| in_place_exec_cleanup_error(standalone_wasm_backend, error))?; let vm = &mut *vm; let root = vm .active_processes .get_mut(root_process_id) - .ok_or_else(|| missing_process_error(vm_id, root_process_id))?; + .ok_or_else(|| { + in_place_exec_cleanup_error( + standalone_wasm_backend, + missing_process_error(vm_id, root_process_id), + ) + })?; let process = Self::active_process_by_path_mut(root, process_path).ok_or_else(|| { - VmError::InvalidState(format!( - "process disappeared during execve: {}", - Self::child_process_path_label(root_process_id, process_path) - )) + in_place_exec_cleanup_error( + standalone_wasm_backend, + VmError::InvalidState(format!( + "process disappeared during execve: {}", + Self::child_process_path_label(root_process_id, process_path) + )), + ) })?; process.guest_cwd = resolved.guest_cwd; process.host_cwd = resolved.host_cwd; @@ -7815,7 +7908,7 @@ where } discard_replaced_image_pending_events(process); - return Ok(()); + return Ok(Some(execution_wasm_permission_tier(effective))); } let bridge = self.bridge.clone(); @@ -8184,7 +8277,7 @@ where "execve replacement failed after commit; terminating the replacement process" ); } - Ok(()) + Ok(None) } /// Commit metadata for an fexecve image that the trusted WASM runner has @@ -8197,10 +8290,10 @@ where root_process_id: &str, process_path: &[&str], request: ProcessLaunchRequest, - ) -> Result<(), VmError> { + ) -> Result, VmError> { validate_wasm_fd_image_commit_request(&request)?; - let (kernel_pid, retained_internal_fds) = { + let (kernel_pid, backend, retained_internal_fds) = { let vm = self.vms.get(vm_id).ok_or_else(|| missing_vm_error(vm_id))?; let root = vm .active_processes @@ -8222,6 +8315,7 @@ where } ( process.kernel_pid, + process.standalone_wasm_backend, process .kernel_stdin_writer_fd .into_iter() @@ -8245,6 +8339,12 @@ where .vms .get_mut(vm_id) .ok_or_else(|| missing_vm_error(vm_id))?; + // Descriptor exec inherits the calling image's kernel tier. + let effective = vm + .kernel + .process_permission_tier(EXECUTION_DRIVER_NAME, kernel_pid) + .map_err(kernel_error)?; + validate_in_place_exec_tier(backend, effective, request.options.validated_import_tiers)?; vm.kernel .exec_process_retaining_internal_fds_from_thread( EXECUTION_DRIVER_NAME, @@ -8260,17 +8360,23 @@ where None, ) .map_err(kernel_error)?; - prune_managed_process_routes_without_aliases(&bridge, vm_id, &mut vm, kernel_pid)?; + prune_managed_process_routes_without_aliases(&bridge, vm_id, &mut vm, kernel_pid) + .map_err(|error| in_place_exec_cleanup_error(backend, error))?; let root = vm .active_processes .get_mut(root_process_id) - .ok_or_else(|| missing_process_error(vm_id, root_process_id))?; + .ok_or_else(|| { + in_place_exec_cleanup_error(backend, missing_process_error(vm_id, root_process_id)) + })?; let process = Self::active_process_by_path_mut(root, process_path).ok_or_else(|| { - VmError::InvalidState(format!( - "process disappeared during fexecve commit: {}", - Self::child_process_path_label(root_process_id, process_path) - )) + in_place_exec_cleanup_error( + backend, + VmError::InvalidState(format!( + "process disappeared during fexecve commit: {}", + Self::child_process_path_label(root_process_id, process_path) + )), + ) })?; process.env = replacement_guest_env; process.exit_signal = None; @@ -8287,7 +8393,7 @@ where process.module_resolution_cache_generation = None; } discard_replaced_image_pending_events(process); - Ok(()) + Ok(Some(execution_wasm_permission_tier(effective))) } // The FIFO VM startup permit serializes engine access. Engine APIs borrow @@ -10398,7 +10504,6 @@ where if !reply.claim().map_err(VmError::from)? { return Ok(Box::pin(async { Ok(()) })); } - let local_replacement = request.options.local_replacement; let result = if fd_image_commit { self.commit_wasm_fd_process_image( vm_id, @@ -10410,13 +10515,13 @@ where self.exec_process_image(vm_id, root_process_id, caller_process_path, request) }; match result { - Ok(()) if local_replacement => { + Ok(Some(tier)) => { reply - .succeed_json(json!({ "committed": true })) + .succeed_json(json!({ "committed": true, "permissionTier": tier })) .map_err(VmError::from)?; return Ok(Box::pin(async { Ok(()) })); } - Ok(()) => { + Ok(None) => { reply.dismiss_claimed().map_err(VmError::from)?; return Ok(Box::pin(async { Ok(()) })); } diff --git a/crates/vm/src/execution/host_dispatch/mod.rs b/crates/vm/src/execution/host_dispatch/mod.rs index 6f3514ee4f..53f18792b2 100644 --- a/crates/vm/src/execution/host_dispatch/mod.rs +++ b/crates/vm/src/execution/host_dispatch/mod.rs @@ -60,11 +60,12 @@ use agentos_vm_kernel::fd_table::{ WASI_RIGHT_FD_ALLOCATE, WASI_RIGHT_FD_DATASYNC, WASI_RIGHT_FD_FDSTAT_SET_FLAGS, WASI_RIGHT_FD_FILESTAT_GET, WASI_RIGHT_FD_FILESTAT_SET_SIZE, WASI_RIGHT_FD_FILESTAT_SET_TIMES, WASI_RIGHT_FD_READ, WASI_RIGHT_FD_READDIR, WASI_RIGHT_FD_SEEK, WASI_RIGHT_FD_SYNC, - WASI_RIGHT_FD_WRITE, WASI_RIGHT_PATH_CREATE_DIRECTORY, WASI_RIGHT_PATH_FILESTAT_GET, - WASI_RIGHT_PATH_FILESTAT_SET_SIZE, WASI_RIGHT_PATH_FILESTAT_SET_TIMES, - WASI_RIGHT_PATH_LINK_SOURCE, WASI_RIGHT_PATH_LINK_TARGET, WASI_RIGHT_PATH_OPEN, - WASI_RIGHT_PATH_READLINK, WASI_RIGHT_PATH_REMOVE_DIRECTORY, WASI_RIGHT_PATH_RENAME_SOURCE, - WASI_RIGHT_PATH_RENAME_TARGET, WASI_RIGHT_PATH_SYMLINK, WASI_RIGHT_PATH_UNLINK_FILE, + WASI_RIGHT_FD_WRITE, WASI_RIGHT_PATH_CREATE_DIRECTORY, WASI_RIGHT_PATH_CREATE_FILE, + WASI_RIGHT_PATH_FILESTAT_GET, WASI_RIGHT_PATH_FILESTAT_SET_SIZE, + WASI_RIGHT_PATH_FILESTAT_SET_TIMES, WASI_RIGHT_PATH_LINK_SOURCE, WASI_RIGHT_PATH_LINK_TARGET, + WASI_RIGHT_PATH_OPEN, WASI_RIGHT_PATH_READLINK, WASI_RIGHT_PATH_REMOVE_DIRECTORY, + WASI_RIGHT_PATH_RENAME_SOURCE, WASI_RIGHT_PATH_RENAME_TARGET, WASI_RIGHT_PATH_SYMLINK, + WASI_RIGHT_PATH_UNLINK_FILE, }; use agentos_vm_kernel::kernel::KernelError; @@ -226,6 +227,7 @@ fn filesystem_operation_is_read_only_mutation(operation: &FilesystemOperation) - | WASI_RIGHT_FD_FILESTAT_SET_SIZE | WASI_RIGHT_FD_FILESTAT_SET_TIMES | WASI_RIGHT_PATH_CREATE_DIRECTORY + | WASI_RIGHT_PATH_CREATE_FILE | WASI_RIGHT_PATH_FILESTAT_SET_SIZE | WASI_RIGHT_PATH_FILESTAT_SET_TIMES | WASI_RIGHT_PATH_LINK_SOURCE @@ -3489,17 +3491,16 @@ where if !reply.claim().map_err(VmError::from)? { return Ok(None); } - let local_replacement = request.options.local_replacement; let result = if fd_image_commit { sidecar.commit_wasm_fd_process_image(vm_id, process_id, &[], request) } else { sidecar.exec_process_image(vm_id, process_id, &[], request) }; match result { - Ok(()) if local_replacement => reply - .succeed_json(json!({ "committed": true })) + Ok(Some(tier)) => reply + .succeed_json(json!({ "committed": true, "permissionTier": tier })) .map_err(VmError::from)?, - Ok(()) => reply.dismiss_claimed().map_err(VmError::from)?, + Ok(None) => reply.dismiss_claimed().map_err(VmError::from)?, Err(error) => reply .fail(host_service_error(&error)) .map_err(VmError::from)?, diff --git a/crates/vm/src/execution/process.rs b/crates/vm/src/execution/process.rs index 34ce7d9f95..5ba3187803 100644 --- a/crates/vm/src/execution/process.rs +++ b/crates/vm/src/execution/process.rs @@ -2735,22 +2735,36 @@ impl ActiveExecutionEvent { | Self::Common(ExecutionEvent::Exited(_)) => 4 * 1024, Self::Common(ExecutionEvent::Warning(error)) | Self::Common(ExecutionEvent::RuntimeFault(error)) => { - std::mem::size_of::().saturating_add(error.encoded_bytes()) + std::mem::size_of::().saturating_add(error.heap_bytes()) } Self::Common(_) => 4 * 1024, Self::Stdout(bytes) | Self::Stderr(bytes) | Self::RoutedStdout(bytes) | Self::RoutedStderr(bytes) => std::mem::size_of::().saturating_add(bytes.len()), - // Internal RPC events are serviced eagerly rather than retained; - // account a conservative fixed envelope if briefly deferred. The - // wire payload is independently frame-bounded. Self::ManagedWriteRecheck(pending) => { std::mem::size_of::().saturating_add(pending.bytes.len()) } - Self::HostRpcRequest(_) - | Self::HostCallCompletion(_) - | Self::DeferredPosixPollWake + Self::HostRpcRequest(call) => { + std::mem::size_of::().saturating_add(call.request.heap_bytes()) + } + Self::HostCallCompletion(completion) => { + let heap = match &completion.result { + Ok(value) => agentos_executor_contract::json_heap_bytes(value), + Err(error) => error + .code + .capacity() + .saturating_add(error.message.capacity()) + .saturating_add( + error + .details + .as_ref() + .map_or(0, agentos_executor_contract::json_heap_bytes), + ), + }; + std::mem::size_of::().saturating_add(heap) + } + Self::DeferredPosixPollWake | Self::ManagedStreamReadRecheck(_) | Self::ManagedUdpPollRecheck(_) => 4 * 1024, Self::SignalState { .. } | Self::Exited(_) => std::mem::size_of::(), @@ -3936,32 +3950,11 @@ fn route_compatibility_host_call( max_reply_bytes: usize, host: Option<&ProcessHostCapabilitySet>, ) -> Result, VmError> { - #[derive(Serialize)] - struct CompatibilityRequestCharge<'a> { - method: &'a str, - args: &'a [Value], - } - let reply = call.reply.clone(); let admission = match host .map(|host| { - let raw_bytes = call - .request - .raw_bytes_args - .values() - .try_fold(0usize, |total, bytes| total.checked_add(bytes.len())) - .ok_or_else(|| { - HostServiceError::new( - "EOVERFLOW", - "compatibility host-call raw payload size overflowed", - ) - })?; - host.admit_json_request( - &CompatibilityRequestCharge { - method: &call.request.method, - args: &call.request.args, - }, - raw_bytes, + host.admit_request( + std::mem::size_of::().saturating_add(call.request.heap_bytes()), ) }) .transpose() diff --git a/crates/vm/tests/architecture_guards.rs b/crates/vm/tests/architecture_guards.rs index 124eb2a0b2..fbede4e526 100644 --- a/crates/vm/tests/architecture_guards.rs +++ b/crates/vm/tests/architecture_guards.rs @@ -4092,8 +4092,8 @@ fn canonical_wasm_exceptions_use_one_finalized_artifact_on_both_engines() { let v8 = std::fs::read_to_string(root.join("crates/executor-v8-runtime/src/isolate.rs")) .expect("read V8 platform source"); assert!( - v8.contains("v8::V8::set_flags_from_string(\"--experimental-wasm-exnref\")"), - "V8 130 must enable the finalized exception encoding used by canonical C++ commands" + !v8.contains("--experimental-wasm-exnref"), + "finalized exception handling must not depend on the removed V8 experiment flag" ); let profile = std::fs::read_to_string(root.join("crates/executor-wasm-abi/src/profile.rs")) diff --git a/crates/vm/tests/builtin_conformance.rs b/crates/vm/tests/builtin_conformance.rs index 8ba4983c3d..ed60811c09 100644 --- a/crates/vm/tests/builtin_conformance.rs +++ b/crates/vm/tests/builtin_conformance.rs @@ -4886,6 +4886,10 @@ try { const deflated = zlib.deflateSync(Buffer.from("agentos", "utf8")); const inflated = zlib.inflateSync(deflated).toString("utf8"); +const brotliInput = Buffer.from("ordinary Brotli round trip"); +const brotliPacked = zlib.brotliCompressSync(brotliInput); +assert.deepEqual(zlib.brotliDecompressSync(brotliPacked, { maxOutputLength: brotliInput.length }), brotliInput); +assert.throws(() => zlib.brotliDecompressSync(brotliPacked, { maxOutputLength: brotliInput.length - 1 })); console.log(JSON.stringify({ constants: { diff --git a/docker/build/darwin.Dockerfile b/docker/build/darwin.Dockerfile index a09de79f59..15f1044402 100644 --- a/docker/build/darwin.Dockerfile +++ b/docker/build/darwin.Dockerfile @@ -5,6 +5,9 @@ # # TARGET = aarch64-apple-darwin | x86_64-apple-darwin # CLANG = aarch64-apple-darwin20.4 | x86_64-apple-darwin20.4 +FROM scratch AS agentos-v8-inputs +COPY scripts/ci/v8/archives.json /archives.json + FROM ghcr.io/rivet-dev/rivet/builder-base-osxcross:0e33ceb98 ARG TARGET=aarch64-apple-darwin @@ -19,6 +22,10 @@ ENV SDK=/root/osxcross/target/SDK/MacOSX11.3.sdk \ SCCACHE_IDLE_TIMEOUT=0 WORKDIR /build +COPY scripts/ci/v8 /build/scripts/ci/v8 +RUN --mount=type=bind,from=agentos-v8-inputs,target=/opt/engine-inputs \ + python3 scripts/ci/v8/engine.py install "${TARGET}" /opt/agentos-v8 --build-context /opt/engine-inputs --cargo-config /opt/agentos-v8.toml --shell-env /opt/agentos-v8.sh + COPY . . RUN rustup toolchain install "$RUST_TOOLCHAIN" --profile minimal && \ @@ -54,7 +61,8 @@ RUN --mount=type=cache,id=cargo-registry-agentos-darwin,target=/usr/local/cargo/ export RANLIB_${tl}=${CLANG}-ranlib && \ export CARGO_TARGET_${tu}_LINKER=${CLANG}-clang && \ if [ "$BUILD_PROFILE" = "release" ]; then FLAG="--release"; PROF=release; else FLAG=""; PROF=debug; fi && \ - cargo build $FLAG -p agentos-sidecar --target "$TARGET" && \ + . /opt/agentos-v8.sh && \ + cargo --config /opt/agentos-v8.toml build $FLAG -p agentos-sidecar --target "$TARGET" && \ mkdir -p /artifacts && \ cp "target/$TARGET/$PROF/agentos-sidecar" /artifacts/agentos-sidecar && \ (sccache --show-stats 2>/dev/null || true) diff --git a/docker/build/linux-gnu.Dockerfile b/docker/build/linux-gnu.Dockerfile index f977274551..e629ebb7f7 100644 --- a/docker/build/linux-gnu.Dockerfile +++ b/docker/build/linux-gnu.Dockerfile @@ -3,6 +3,9 @@ # Build AgentOS Linux GNU native artifacts in one cached container. The sysroot # setup mirrors scripts/ci/setup-linux-gnu-sysroot.sh, but runs inside Docker so # Cargo caches can be persisted with BuildKit/GHA like the Darwin build. +FROM scratch AS agentos-v8-inputs +COPY scripts/ci/v8/archives.json /archives.json + FROM ubuntu:24.04 ARG RUST_TOOLCHAIN=1.95.0 @@ -31,7 +34,7 @@ RUN set -eux; \ . /etc/os-release; \ codename="${VERSION_CODENAME:?missing VERSION_CODENAME}"; \ apt-get update; \ - apt-get install -y --no-install-recommends ca-certificates curl gnupg xz-utils binutils sccache build-essential pkg-config libssl-dev; \ + apt-get install -y --no-install-recommends ca-certificates curl gnupg xz-utils binutils sccache build-essential pkg-config libssl-dev python3; \ curl -fsSL "https://deb.nodesource.com/setup_${NODE_MAJOR}.x" | bash -; \ apt-get install -y --no-install-recommends nodejs; \ corepack enable; \ @@ -65,6 +68,10 @@ RUN set -eux; \ xzcat /tmp/agentos-sysroot.tar.xz | tar -x; \ rm -rf /var/lib/apt/lists/* /tmp/agentos-sysroot.tar.xz +COPY scripts/ci/v8 /build/scripts/ci/v8 +RUN --mount=type=bind,from=agentos-v8-inputs,target=/opt/engine-inputs \ + python3 scripts/ci/v8/engine.py install "${TARGET}" /opt/agentos-v8 --build-context /opt/engine-inputs --cargo-config /opt/agentos-v8.toml --shell-env /opt/agentos-v8.sh + COPY . . RUN --mount=type=cache,id=cargo-registry-agentos-${CACHE_PLATFORM},target=/usr/local/cargo/registry,sharing=locked \ @@ -99,7 +106,8 @@ RUN --mount=type=cache,id=cargo-registry-agentos-${CACHE_PLATFORM},target=/usr/l -C link-arg=/tmp/agentos_renameat2_shim.o \ ${RUSTFLAGS:-}"; \ if [ "$BUILD_PROFILE" = "release" ]; then FLAG="--release"; PROF=release; else FLAG=""; PROF=debug; fi; \ - cargo build $FLAG -p agentos-sidecar --target "$TARGET"; \ + . /opt/agentos-v8.sh; \ + cargo --config /opt/agentos-v8.toml build $FLAG -p agentos-sidecar --target "$TARGET"; \ mkdir -p /artifacts; \ cp "target/$TARGET/$PROF/agentos-sidecar" /artifacts/agentos-sidecar; \ (sccache --show-stats 2>/dev/null || true) diff --git a/docs/content/docs/architecture/limits-and-observability.mdx b/docs/content/docs/architecture/limits-and-observability.mdx index 3ea6e694a7..a72e24dd1b 100644 --- a/docs/content/docs/architecture/limits-and-observability.mdx +++ b/docs/content/docs/architecture/limits-and-observability.mdx @@ -51,6 +51,13 @@ host-function callback output, reject the crossing event with an error naming `limits.process.pendingEventCount` or `limits.process.pendingEventBytes`. No path silently drops data or crashes the sidecar. +For queued executor host calls, `limits.process.pendingEventBytes` charges +the request envelope, raw buffers and storage for decoded JSON strings, arrays +and object entries. A small configured budget can reject a single valid request +even when its compact JSON fits. For example, an array of small numbers needs +one in-memory value slot per number. The separate +`limits.reactor.maxBridgeResponseBytes` bounds the encoded reply. + All VMs on one sidecar share its queue of process events waiting for the host. Guest stdout and stderr may fill at most seven eighths of that queue (`runtime.protocol.maxProcessEvents`); the rest is kept for exits and other diff --git a/docs/content/docs/javascript-compatibility.mdx b/docs/content/docs/javascript-compatibility.mdx index 4a10e46b02..82f7b329be 100644 --- a/docs/content/docs/javascript-compatibility.mdx +++ b/docs/content/docs/javascript-compatibility.mdx @@ -162,11 +162,10 @@ does not establish correct fd0 EOF behavior. and `setFromBase64`, with the `alphabet`, `omitPadding`, and `lastChunkHandling` options. Encoded results use the VM memory budget; oversized results fail before allocation. -- V8 WebAssembly exception handling with `exnref` (`try_table`, `throw_ref`) - is enabled when the `wasm-v8` executor is included. Set - `AGENTOS_V8_WASM_EXNREF=0` in the host sidecar environment to disable it. - The setting applies to every VM in that sidecar. Modules requiring it fail - with `CompileError` when disabled. +- V8 supports finalized WebAssembly exception handling with `exnref` + (`try_table`, `throw_ref`) in every VM. Remove the old host setting + `AGENTOS_V8_WASM_EXNREF=0` before upgrading. A sidecar configured to + disable this feature fails startup with `ERR_AGENTOS_UNSUPPORTED_V8_OPTION`. ## `node:sqlite` diff --git a/docs/content/docs/resource-limits.mdx b/docs/content/docs/resource-limits.mdx index 2135a3c22d..9fae6c003b 100644 --- a/docs/content/docs/resource-limits.mdx +++ b/docs/content/docs/resource-limits.mdx @@ -189,6 +189,19 @@ skipped chunks. Recover the output with `read_process_output`. The TypeScript client delivers every byte written before a process exits to `onStdout` and `onStderr`, including when the host is slow to drain them. +The host captures up to 16 MiB of combined stdout and stderr per call. +If output exceeds that budget, the call fails with +`ERR_AGENTOS_CAPTURED_OUTPUT_LIMIT`. Set `maxCapturedOutputBytes` in +TypeScript or `ExecOptions.max_captured_output_bytes` in Rust to raise it. +For larger output without retaining it in memory, spawn the process and stream +its output. + +Native execution rejects per-run CPU overrides and requests for the legacy +`freeze` timing mode with `ERR_AGENTOS_UNSUPPORTED_EXECUTION_OPTION`. +Configure CPU time at VM creation. Set VM-level `highResolutionTime` in +TypeScript or `high_resolution_time` in Rust to control clock precision. +The legacy `off` option preserves the VM clock policy. + `dispose()` is one request: the sidecar stops the VM's running processes (`SIGTERM`, then `SIGKILL`) and then removes the VM. If `dispose()` fails, the VM can still be alive. A failed disposal is retained because repeating a partially completed teardown diff --git a/docs/content/docs/security-model.mdx b/docs/content/docs/security-model.mdx index 7518bf5c6a..749d058dbd 100644 --- a/docs/content/docs/security-model.mdx +++ b/docs/content/docs/security-model.mdx @@ -205,7 +205,11 @@ embedded capabilities, not actor capabilities. - Set timeouts and CPU limits on code you did not write. - Harden the host or container that runs the sidecar, and run it as a non-root user. Rivet Cloud runs the sidecar unprivileged under gVisor. -- Give each mutually untrusted tenant its own VM. +- Give mutually untrusted tenants separate VMs and dedicated sidecar processes + when they require OS-level memory isolation. V8 isolates on a shared sidecar + share its address space. The embedded V8 build disables the V8 Sandbox. + Use a separate `AgentOs.createSidecar()` per trust domain in the + [embedded client](/agentos/docs/quickstart-embedded). See [Permissions](/agentos/docs/permissions), [Resource Limits](/agentos/docs/resource-limits), and diff --git a/experiments/obscura/Cargo.toml b/experiments/obscura/Cargo.toml.disabled similarity index 100% rename from experiments/obscura/Cargo.toml rename to experiments/obscura/Cargo.toml.disabled diff --git a/experiments/obscura/README.md b/experiments/obscura/README.md index 6734aa1f98..c584942f14 100644 --- a/experiments/obscura/README.md +++ b/experiments/obscura/README.md @@ -1,6 +1,6 @@ # Obscura executor embedding experiment -**This is an unfinished integration, not an installed `obscura` VM command.** +**Disabled for the V8 158 upgrade.** The standalone experiment requires Deno and V8 130. Its manifest is archived as `Cargo.toml.disabled` until the experiment supports the same V8 as agentOS. It runs the real upstream browser against agentOS's executor host-call contract. It is deliberately outside the shipping workspace and executor registry. Do not pass untrusted pages to this experiment. @@ -11,7 +11,7 @@ Apache-2.0 license. They are not reimplemented here. Deno is MIT-licensed. `prepare.py` verifies both archive checksums, applies the checked-in patches, and keeps upstream licenses in the ignored `vendor/` directory. -## Run the fixture +## Previous fixture on V8 130 From the repository root, with Python 3.12+, GNU patch, the repository Rust toolchain, and pnpm installed: diff --git a/packages/benchmarks/README.md b/packages/benchmarks/README.md index f305b7c1d3..74f324b652 100644 --- a/packages/benchmarks/README.md +++ b/packages/benchmarks/README.md @@ -136,7 +136,7 @@ Gate rows: | `control/cpu_loop` | `wasm` | WASM runtime lane sanity check. | | `ecosystem/ls_100` | `vmCmd` | End-to-end WASM command tier. | -The CI baseline is environment-specific because GitHub-hosted runner hardware differs from the canonical machine. In CI, `bench:gate` uses `packages/benchmarks/results/baseline-ci.json`. If it is missing, PR gates skip loudly. The nightly workflow runs the full matrix, writes a candidate `baseline-ci.json`, and uploads it as an artifact. Bootstrap is: +The CI baseline is environment-specific because GitHub-hosted runner hardware differs from the canonical machine. In CI, `bench:gate` uses `packages/benchmarks/results/baseline-ci.json`. A missing baseline fails the gate. The nightly workflow runs the full matrix, writes a candidate `baseline-ci.json`, and uploads it as an artifact. Bootstrap is: 1. Let the first nightly workflow finish. 2. Download `baseline-ci.json` from the `nightly-benchmark-results` artifact. diff --git a/packages/benchmarks/src/baseline.ts b/packages/benchmarks/src/baseline.ts index 2ee35906f3..7ddf59f69b 100644 --- a/packages/benchmarks/src/baseline.ts +++ b/packages/benchmarks/src/baseline.ts @@ -81,7 +81,25 @@ export function baselinePathForEnvironment(): string { export function loadMatrixBaseline(path: string): MatrixBaseline | null { if (!existsSync(path)) return null; - return JSON.parse(readFileSync(path, "utf8")) as MatrixBaseline; + const baseline = JSON.parse(readFileSync(path, "utf8")); + if ( + baseline?.schemaVersion !== 1 || + !["local", "ci"].includes(baseline.kind) || + typeof baseline.engine?.cold !== "boolean" || + typeof baseline.engine?.sharedVm !== "boolean" || + !Array.isArray(baseline.rows) || + !baseline.rows.every( + (row: Partial) => + typeof row?.key === "string" && + row.lanes && + typeof row.lanes === "object", + ) || + new Set(baseline.rows.map((row: Partial) => row.key)) + .size !== baseline.rows.length + ) { + throw new Error(`Invalid benchmark matrix baseline: ${path}`); + } + return baseline as MatrixBaseline; } export function rowKey(row: Pick): string { diff --git a/packages/benchmarks/src/compare-baseline.ts b/packages/benchmarks/src/compare-baseline.ts index f2aa87c794..920698a1c0 100644 --- a/packages/benchmarks/src/compare-baseline.ts +++ b/packages/benchmarks/src/compare-baseline.ts @@ -37,6 +37,11 @@ export function compareMatrixBaseline( tinyCurrentFloorMs: number; }, ): MatrixComparison[] { + if (!Number.isFinite(options.threshold) || options.threshold <= 0) { + throw new RangeError( + "Benchmark threshold must be a finite positive number", + ); + } const currentByKey = new Map(currentRows.map((row) => [row.key, row])); const baselineByKey = new Map(baseline.rows.map((row) => [row.key, row])); return gateRows.map((gate) => { @@ -44,22 +49,32 @@ export function compareMatrixBaseline( const base = baselineByKey.get(gate.key); const currentMetric = current ? laneMetric(current, gate.lane) : undefined; const baselineMetric = base ? laneMetric(base, gate.lane) : undefined; - if (!base || !baselineMetric) { + if ( + !base || + !baselineMetric || + !Number.isFinite(baselineMetric.p50Ms) || + baselineMetric.p50Ms <= 0 + ) { return { key: gate.key, lane: gate.lane, currentP50Ms: currentMetric?.p50Ms, status: "missing-baseline", - reason: "baseline has no p50 for this row/lane", + reason: "baseline has no valid positive p50 for this row/lane", }; } - if (!current || !currentMetric) { + if ( + !current || + !currentMetric || + !Number.isFinite(currentMetric.p50Ms) || + currentMetric.p50Ms < 0 + ) { return { key: gate.key, lane: gate.lane, baselineP50Ms: baselineMetric.p50Ms, status: "missing-current", - reason: "current run has no p50 for this row/lane", + reason: "current run has no valid p50 for this row/lane", }; } const ratio = round(currentMetric.p50Ms / baselineMetric.p50Ms, 2); diff --git a/packages/benchmarks/src/footprint.ts b/packages/benchmarks/src/footprint.ts index 9eb48b44d8..ff4edb9881 100644 --- a/packages/benchmarks/src/footprint.ts +++ b/packages/benchmarks/src/footprint.ts @@ -1,64 +1,22 @@ -import { findSidecarPids, readRssBytes } from "./lib/memory.js"; -import type { Finding } from "./lib/report.js"; +import { readRssBytes } from "./lib/memory.js"; import { writeJson } from "./lib/report.js"; import { type BenchVm, createBenchSidecar, createBenchVm } from "./lib/vm.js"; const RESULTS_DIR = new URL("../results/", import.meta.url).pathname; export async function runFootprint() { - const beforePids = findSidecarPids(); - const beforeSet = new Set(beforePids); const sidecar = createBenchSidecar(); let vm: BenchVm | null = null; try { vm = await createBenchVm({ sidecar }); await new Promise((resolve) => setTimeout(resolve, 250)); - const afterPids = findSidecarPids(); - const newPids = afterPids.filter((pid) => !beforeSet.has(pid)); - const internalPid = vm.sidecarPid(); - const measuredPid = - internalPid && afterPids.includes(internalPid) - ? internalPid - : newPids.length === 1 - ? newPids[0] - : null; - const after = readRssBytes(measuredPid); - const resource = await vm.getResourceSnapshot(); - const total = measuredPid === null ? 0 : after; - const components = sortComponents([ - { name: "empty_v8_isolate_baseline", bytes: Math.round(total * 0.5) }, - { name: "wasm_modules_loaded", bytes: 0 }, - { name: "sidecar_kernel_structs", bytes: Math.round(total * 0.3) }, - { - name: "mounts_vfs", - bytes: total - Math.round(total * 0.5) - Math.round(total * 0.3), - }, - ]); - const findings: Finding[] = [ - { - family: "footprint", - op: "idle_vm_rss_floor", - emulation_ratio: total, - total_ratio: total, - confirmed: true, - suspected_cause: - "idle VM floor dominated by V8 isolate baseline and sidecar structs", - file_line: "crates/executor-v8-runtime/src/session.rs:294", - reproducer: "createBenchVm(); sample /proc//status VmRSS", - evidence: `rss_floor_bytes=${total} measured_pid=${measuredPid} internal_pid=${internalPid} before_pids=${JSON.stringify(beforePids)} after_pids=${JSON.stringify(afterPids)} new_pids=${JSON.stringify(newPids)} resource=${JSON.stringify(resource)}`, - }, - ]; + const measuredPid = vm.sidecarPid(); const out = { - idleRssFloorBytes: total, + idleRssFloorBytes: readRssBytes(measuredPid), measuredPid, - internalPid, - beforePids, - afterPids, - newPids, - components, - topReducibleContributors: components.slice(0, 3), - resource, - findings, + resource: await vm.getResourceSnapshot(), + components: [], + findings: [], }; writeJson(`${RESULTS_DIR}/footprint.json`, out); return out; @@ -68,10 +26,6 @@ export async function runFootprint() { } } -function sortComponents(components: T[]): T[] { - return [...components].sort((a, b) => b.bytes - a.bytes); -} - if (import.meta.url === `file://${process.argv[1]}`) { const out = await runFootprint(); console.log(JSON.stringify(out, null, 2)); diff --git a/packages/benchmarks/src/fuzz/run.ts b/packages/benchmarks/src/fuzz/run.ts index c6be6553ce..fd23e3deb6 100644 --- a/packages/benchmarks/src/fuzz/run.ts +++ b/packages/benchmarks/src/fuzz/run.ts @@ -12,9 +12,13 @@ export async function runFuzz(options: { iterations: number; warmup: number }) { const findings: Finding[] = []; const refuted: RefutedCandidate[] = []; const programs = generateSeedCorpus(); + const skipped = programs + .filter((program) => program.family !== "process") + .map(({ id, family }) => ({ id, family, reason: "No benchmark driver" })); const vm = await createBenchVm(); try { for (const program of programs) { + if (program.family !== "process") continue; const result = await runProgram( program, vm, @@ -55,7 +59,7 @@ export async function runFuzz(options: { iterations: number; warmup: number }) { } finally { await vm.dispose(); } - const out = { programs, findings, refuted }; + const out = { programs, findings, refuted, skipped }; writeJson(`${RESULTS_DIR}/fuzz-findings.json`, out); return out; } @@ -66,16 +70,9 @@ async function runProgram( iters: number, warmup: number, ) { - if (program.family === "process") { - const node = stats(await runProcessProgramNode(program, iters, warmup)); - const guest = stats( - await runProcessProgramGuest(program, vm, iters, warmup), - ); - return { node, guest, emulation_ratio: round(guest.p50 / node.p50) }; - } - const node = stats([1]); - const guest = stats([1]); - return { node, guest, emulation_ratio: 1 }; + const node = stats(await runProcessProgramNode(program, iters, warmup)); + const guest = stats(await runProcessProgramGuest(program, vm, iters, warmup)); + return { node, guest, emulation_ratio: round(guest.p50 / node.p50) }; } async function runProcessProgramNode( diff --git a/packages/benchmarks/src/lib/memory.ts b/packages/benchmarks/src/lib/memory.ts index 3e33ce815c..c589109738 100644 --- a/packages/benchmarks/src/lib/memory.ts +++ b/packages/benchmarks/src/lib/memory.ts @@ -15,35 +15,15 @@ export interface MemorySample { pipes: number; } -export function findSidecarPid(): number | null { - return findSidecarPids()[0] ?? null; -} - -export function findSidecarPids(): number[] { - const pids: number[] = []; - for (const pid of readdirSync("/proc")) { - if (!/^\d+$/.test(pid)) continue; - try { - const comm = readFileSync(`/proc/${pid}/comm`, "utf8").trim(); - if (comm === "agentos-sidecar") { - pids.push(Number(pid)); - } - } catch { - // Process exited while scanning. - } - } - return pids.sort((a, b) => a - b); -} - export function readRssBytes(pid: number | null): number { - if (pid === null) return 0; - try { - const status = readFileSync(`/proc/${pid}/status`, "utf8"); - const match = status.match(/^VmRSS:\s+(\d+)\s+kB/m); - return match ? Number(match[1]) * 1024 : 0; - } catch { - return 0; + if (pid === null || !Number.isSafeInteger(pid) || pid <= 0) { + throw new Error("Benchmark VM has no owned sidecar PID"); + } + const bytes = readStatusBytes(pid, "VmRSS"); + if (!Number.isSafeInteger(bytes) || bytes <= 0) { + throw new Error(`Invalid VmRSS measurement for benchmark sidecar ${pid}`); } + return bytes; } export interface ProcessMemorySnapshot { @@ -392,7 +372,7 @@ export async function sampleMemory( return { cycle, guestHeapRss, - sidecarRss: readRssBytes(findSidecarPid()), + sidecarRss: readRssBytes(vm.sidecarPid()), runningProcesses: resource.runningProcesses, stoppedProcesses: resource.stoppedProcesses, exitedProcesses: resource.exitedProcesses, @@ -431,6 +411,14 @@ async function sampleGuestHeap(vm: BenchVm): Promise { }, }); const code = await vm.waitProcess(proc.pid); - if (code !== 0) return 0; - return Number(stdout.trim() || 0); + const bytes = Number(stdout.trim()); + if ( + code !== 0 || + !/^\d+$/.test(stdout.trim()) || + !Number.isSafeInteger(bytes) || + bytes <= 0 + ) { + throw new Error(`Guest heap measurement failed (exit ${code})`); + } + return bytes; } diff --git a/packages/benchmarks/src/quick-gate.ts b/packages/benchmarks/src/quick-gate.ts index 35f4640d40..2756d146da 100644 --- a/packages/benchmarks/src/quick-gate.ts +++ b/packages/benchmarks/src/quick-gate.ts @@ -1,4 +1,3 @@ -import { existsSync } from "node:fs"; import { pathToFileURL } from "node:url"; import { baselinePathForEnvironment, @@ -95,12 +94,6 @@ class GateExitError extends Error { async function main(): Promise { const baselinePath = baselinePathForEnvironment(); - if (process.env.GITHUB_ACTIONS === "true" && !existsSync(baselinePath)) { - console.error( - `BENCH GATE SKIPPED: ${baselinePath} is missing. Run the nightly benchmark, download its candidate baseline-ci.json artifact, commit it, and PR gates will enforce from then on.`, - ); - return; - } const baseline = loadMatrixBaseline(baselinePath); if (!baseline) { throw new GateExitError( @@ -109,6 +102,16 @@ async function main(): Promise { ); } + const threshold = Number( + process.env.BENCH_GATE_THRESHOLD ?? GATE_DEFAULTS.threshold, + ); + if (!Number.isFinite(threshold) || threshold <= 0) { + throw new GateExitError( + 2, + "BENCH_GATE_THRESHOLD must be a finite positive number", + ); + } + const sidecar = resolveBenchSidecarProvenance(); console.error(formatSidecarProvenance(sidecar)); if (sidecar.profile !== "release") { @@ -118,6 +121,17 @@ async function main(): Promise { ); } + if ( + baseline.kind !== + (process.env.GITHUB_ACTIONS === "true" ? "ci" : "local") || + baseline.engine.cold !== (process.env.BENCH_COLD === "1") || + baseline.engine.sharedVm !== (process.env.BENCH_SHARED_VM === "1") + ) { + throw new GateExitError( + 2, + "Benchmark baseline does not match the run environment or VM mode", + ); + } const gateRows = selectedGateRows(); process.env.BENCH_OP_FILTER = gateRows.map((row) => row.key).join(","); process.env.BENCH_ITERATIONS ??= String(GATE_DEFAULTS.iterations); @@ -125,12 +139,23 @@ async function main(): Promise { process.env.BENCH_REQUIRED_WASM_COMMANDS ??= requiredWasmCommands(gateRows).join(","); + for (const [key, minimum] of [ + ["BENCH_ITERATIONS", 1], + ["BENCH_WARMUP", 0], + ] as const) { + const value = Number(process.env[key]); + if ( + !/^\d+$/.test(process.env[key] ?? "") || + !Number.isSafeInteger(value) || + value < minimum + ) { + throw new GateExitError(2, `${key} must be a safe integer >= ${minimum}`); + } + } + const { runLatencyMatrix } = await import("./run-all.js"); const matrix = await runLatencyMatrix(); const currentRows = matrix.results.map(baselineRowFromLatency); - const threshold = Number( - process.env.BENCH_GATE_THRESHOLD ?? GATE_DEFAULTS.threshold, - ); const comparisons = compareMatrixBaseline(currentRows, baseline, gateRows, { threshold, tinyBaselineFloorMs: GATE_DEFAULTS.tinyBaselineFloorMs, @@ -168,18 +193,31 @@ async function main(): Promise { function selectedGateRows(): GateRow[] { const override = process.env.BENCH_GATE_ROWS; if (!override) return GATE_DEFAULTS.rows; - return override + const rows = override .split(",") .map((entry) => entry.trim()) .filter(Boolean) .map((entry) => { - const [key, lane] = entry.split(":"); + const [key, lane, extra] = entry.split(":"); + if ( + !key || + extra !== undefined || + (lane !== undefined && + !["native", "node", "guest", "wasm", "hostCmd", "vmCmd"].includes( + lane, + )) + ) { + throw new GateExitError(2, `Invalid BENCH_GATE_ROWS entry ${entry}`); + } const configured = GATE_DEFAULTS.rows.find((row) => row.key === key); return { key, lane: (lane ?? configured?.lane ?? "guest") as GateLane, }; }); + if (rows.length === 0) + throw new GateExitError(2, "BENCH_GATE_ROWS selected no comparisons"); + return rows; } function requiredWasmCommands(gateRows: GateRow[]): string[] { diff --git a/packages/benchmarks/src/run-all.ts b/packages/benchmarks/src/run-all.ts index c687594500..b0da917409 100644 --- a/packages/benchmarks/src/run-all.ts +++ b/packages/benchmarks/src/run-all.ts @@ -157,7 +157,7 @@ async function main(): Promise { const permissionFindings = permissionPolicyFindings(permissionPolicyTax); const resourceSnapshotStubbed = false; const fuzz = FAMILY_FILTER - ? { programs: [], findings: [], refuted: [] } + ? { programs: [], findings: [], refuted: [], skipped: [] } : await runFuzz({ iterations: ITERATIONS, warmup: WARMUP }); const leak = FAMILY_FILTER ? { findings: [], streams: [] } diff --git a/packages/benchmarks/tests/compare-baseline.test.ts b/packages/benchmarks/tests/compare-baseline.test.ts index 5d8c72436c..d952460dee 100644 --- a/packages/benchmarks/tests/compare-baseline.test.ts +++ b/packages/benchmarks/tests/compare-baseline.test.ts @@ -52,6 +52,21 @@ describe("compareMatrixBaseline", () => { assert.equal(result.status, "fail"); }); + test("rejects invalid baseline and measurement values", () => { + assert.equal(compare(0, 1).status, "missing-baseline"); + assert.equal(compare(1, Number.NaN).status, "missing-current"); + assert.throws( + () => + compareMatrixBaseline( + [row(1)], + { rows: [row(1)] } as MatrixBaseline, + [{ key: "fs/fs_read_small", lane: "guest" }], + { ...OPTIONS, threshold: Number.NaN }, + ), + RangeError, + ); + }); + test("keeps ratio-only enforcement for normal baselines", () => { const result = compare(5, 10.1); assert.equal(result.ratio, 2.02); diff --git a/packages/build-tools/bridge-src/builtins/builtin-modules.ts b/packages/build-tools/bridge-src/builtins/builtin-modules.ts index 21e75f31ac..ef25e35640 100644 --- a/packages/build-tools/bridge-src/builtins/builtin-modules.ts +++ b/packages/build-tools/bridge-src/builtins/builtin-modules.ts @@ -784,35 +784,55 @@ function ensureBuiltinUrlStdlibModule() { } return builtinUrlStdlibModule; } +const builtinRequestString = String; +const removeNodePrefix = Function.call.bind(String.prototype.replace); +const splitBuiltinName = Function.call.bind(String.prototype.split); +const builtinAllowed = Function.call.bind(Set.prototype.has); + function normalizeBuiltinRequest(request) { - return String(request).replace(/^node:/, ""); + return removeNodePrefix(builtinRequestString(request), /^node:/, ""); +} +function builtinRootName(request) { + return splitBuiltinName(normalizeBuiltinRequest(request), "/")[0]; } -let __jsRuntimeBuiltinAllowlist = null; +let __jsRuntimeBuiltinAllowlist: Set | null = null; function rejectRestrictedBuiltinRequest(request) { const normalized = normalizeBuiltinRequest(request); - // jsRuntime builtin allow-list gate. When the per-execution shim installed an - // allow-list (non-node platforms => empty => deny all; node + explicit list), - // deny any builtin whose root name is not permitted. Absent => unrestricted. const allow = __jsRuntimeBuiltinAllowlist; - if (Array.isArray(allow)) { - const root = String(normalized == null ? request : normalized) - .replace(/^node:/, "") - .split("/")[0]; - if (!allow.includes(root)) { - throw createAccessDeniedBuiltinError(request); - } + if (allow && !builtinAllowed(allow, builtinRootName(normalized))) { + throw createAccessDeniedBuiltinError(request); } return normalized; } exposeCustomGlobal("__agentOSInitJsRuntime", (allowlist) => { - __jsRuntimeBuiltinAllowlist = Array.isArray(allowlist) - ? allowlist.map( - (name) => - String(name) - .replace(/^node:/, "") - .split("/")[0], - ) - : null; + if (__jsRuntimeBuiltinAllowlist !== null) { + throw new Error("Builtin allow-list is already installed"); + } + const roots = Array.isArray(allowlist) ? allowlist.map(builtinRootName) : []; + const allow = new Set(roots); + __jsRuntimeBuiltinAllowlist = allow; + // These compatibility aliases are the same module objects exposed by + // require() and static imports. Internal code uses their lexical bindings. + for (const [name, builtin] of [ + ["_asyncHooksModule", "async_hooks"], + ["_childProcessModule", "child_process"], + ["_dgramModule", "dgram"], + ["_dnsModule", "dns"], + ["_eventsModule", "events"], + ["_fsModule", "fs"], + ["_httpModule", "http"], + ["_http2Module", "http2"], + ["_httpsModule", "https"], + ["_moduleModule", "module"], + ["_netModule", "net"], + ["_osModule", "os"], + ["_sqliteModule", "sqlite"], + ["_tlsModule", "tls"], + ]) { + if (!builtinAllowed(allow, builtin)) { + Reflect.deleteProperty(globalThis, name); + } + } }); function loadBuiltinModule(request) { const normalized = rejectRestrictedBuiltinRequest(request); diff --git a/packages/build-tools/bridge-src/builtins/child-process.ts b/packages/build-tools/bridge-src/builtins/child-process.ts index cdf1ffa7d0..80105fd626 100644 --- a/packages/build-tools/bridge-src/builtins/child-process.ts +++ b/packages/build-tools/bridge-src/builtins/child-process.ts @@ -7,7 +7,7 @@ import { runInAsyncContextFrame, } from "./async-context.js"; import { _stderr, _stdout } from "./console.js"; -import { _fdClose, _fdGetPath, encodeBridgeBytes, fs } from "./fs.js"; +import { _fdClose, encodeBridgeBytes, fs } from "./fs.js"; import { normalizeChildProcessSignal } from "./os.js"; import { routeAsyncCallbackError } from "./process.js"; import { ipcInfo, openIpcChannel } from "./process-ipc.js"; @@ -91,15 +91,6 @@ function releaseChildInheritedFd(fd) { } catch {} } } -function childInheritedFdPath(fd) { - if (typeof fd !== "number") return null; - try { - const path = _fdGetPath.applySyncPromise(void 0, [fd]); - return typeof path === "string" && path.startsWith("/") ? path : null; - } catch { - return null; - } -} function normalizeChildProcessSessionId(payload) { if (!payload || typeof payload !== "object") { return null; @@ -442,24 +433,35 @@ function splitChildProcessIpcFrames(buffer, chunk) { // bytes straight to that descriptor (matching native node, where the child's // output lands in the inherited file/pipe rather than on child.stdout). Returns // true when the data was consumed by the fd so the caller skips stream emission. -function writeChildOutputToInheritedFd(fd, buf, path = null) { +function writeChildOutputToInheritedFd(fd, buf) { + if (typeof fd !== "number") return false; const bytes = typeof Buffer !== "undefined" && Buffer.isBuffer(buf) ? buf : typeof Buffer !== "undefined" ? Buffer.from(buf) : buf; - if (typeof path === "string") { - try { - fs.appendFileSync(path, bytes); - return true; - } catch {} - } - if (typeof fd !== "number") return false; - try { - fs.writeSync(fd, bytes, 0, bytes.length, null); - } catch { - return false; + for (let offset = 0; offset < bytes.length; ) { + const written = fs.writeSync( + fd, + bytes, + offset, + bytes.length - offset, + null, + ); + if ( + !Number.isInteger(written) || + written <= 0 || + written > bytes.length - offset + ) { + throw Object.assign( + new Error("Inherited descriptor write made no progress"), + { + code: "EIO", + }, + ); + } + offset += written; } return true; } @@ -538,13 +540,11 @@ function deliverChildProcessEventInner(child, sessionId, type, data) { if (type === "stdout") { const buf = typeof Buffer !== "undefined" ? Buffer.from(data) : data; - if (writeChildOutputToInheritedFd(child._stdoutFd, buf, child._stdoutPath)) - return; + if (writeChildOutputToInheritedFd(child._stdoutFd, buf)) return; child.stdout.emit("data", buf); } else if (type === "stderr") { const buf = typeof Buffer !== "undefined" ? Buffer.from(data) : data; - if (writeChildOutputToInheritedFd(child._stderrFd, buf, child._stderrPath)) - return; + if (writeChildOutputToInheritedFd(child._stderrFd, buf)) return; child.stderr.emit("data", buf); } else if (type === "exit") { if ( @@ -1785,8 +1785,6 @@ function spawn(command, args, options) { // (which native node leaves null in that mode). child._stdoutFd = typeof stdio[1] === "number" ? stdio[1] : null; child._stderrFd = typeof stdio[2] === "number" ? stdio[2] : null; - child._stdoutPath = childInheritedFdPath(child._stdoutFd); - child._stderrPath = childInheritedFdPath(child._stderrFd); child._inheritedFds = []; for (const fd of [child._stdoutFd, child._stderrFd]) { if (typeof fd === "number") { diff --git a/packages/build-tools/bridge-src/builtins/fs.ts b/packages/build-tools/bridge-src/builtins/fs.ts index e80bff0092..b12894328f 100644 --- a/packages/build-tools/bridge-src/builtins/fs.ts +++ b/packages/build-tools/bridge-src/builtins/fs.ts @@ -2052,6 +2052,14 @@ var ReadStream = class extends CanonicalFileReadable { }; var MAX_WRITE_STREAM_BYTES = 16 * 1024 * 1024; +function checkStreamWriteProgress(written, remaining) { + if (!Number.isInteger(written) || written <= 0 || written > remaining) { + throw Object.assign(new Error("WriteStream write made no valid progress"), { + code: "EIO", + }); + } +} + var WriteStream = class { constructor(filePath, _options) { this._options = _options; @@ -2205,6 +2213,15 @@ var WriteStream = class { queueMicrotask(() => cb?.(null)); return true; } + async _writeChunk(chunk, write) { + for (let offset = 0; offset < chunk.byteLength; ) { + const remaining = chunk.byteLength - offset; + const written = await write(offset, remaining); + checkStreamWriteProgress(written, remaining); + offset += written; + if (typeof this._position === "number") this._position += written; + } + } end(chunkOrCb, encodingOrCallback, callback) { if (this.writableEnded) { return this; @@ -2232,15 +2249,15 @@ var WriteStream = class { try { if (this._fileHandle) { for (const chunk of this._chunks) { - const result = await this._fileHandle.write( - chunk, - 0, - chunk.byteLength, - this._position, - ); - if (typeof this._position === "number") { - this._position += result?.bytesWritten ?? chunk.byteLength; - } + await this._writeChunk(chunk, async (offset, length) => { + const { bytesWritten } = await this._fileHandle.write( + chunk, + offset, + length, + this._position, + ); + return bytesWritten; + }); } if (this.autoClose && !this._fileHandle.closed) { await this._fileHandle.close(); @@ -2253,13 +2270,28 @@ var WriteStream = class { ? this.path.toString() : null; if (this.fd !== null && this.fd >= 0) { - const bytesWritten = fs.writevSync( - this.fd, - this._chunks, - this._position, + // Keep the normal path to one vectored write. A short + // result advances only through the consumed prefix. + let chunks = this._chunks.filter((chunk) => chunk.byteLength > 0); + let remaining = chunks.reduce( + (total, chunk) => total + chunk.byteLength, + 0, ); - if (typeof this._position === "number") { - this._position += bytesWritten; + while (remaining > 0) { + const written = fs.writevSync(this.fd, chunks, this._position); + checkStreamWriteProgress(written, remaining); + remaining -= written; + if (typeof this._position === "number") + this._position += written; + if (remaining === 0) break; + let consumed = written; + let index = 0; + while (consumed >= chunks[index].byteLength) { + consumed -= chunks[index].byteLength; + index++; + } + chunks = chunks.slice(index); + if (consumed > 0) chunks[0] = chunks[0].subarray(consumed); } if (this.autoClose) { await this._closeUnderlying(); @@ -4810,16 +4842,16 @@ var fs = { buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); - totalBytesWritten += fs.writeSync( + const written = fs.writeSync( normalizedFd, bytes, 0, bytes.length, nextPosition, ); - if (nextPosition !== null) { - nextPosition += bytes.length; - } + totalBytesWritten += written; + if (nextPosition !== null) nextPosition += written; + if (written < bytes.length) break; } return totalBytesWritten; }, diff --git a/packages/build-tools/bridge-src/builtins/http.ts b/packages/build-tools/bridge-src/builtins/http.ts index 30e96fc1aa..21c3f5a085 100644 --- a/packages/build-tools/bridge-src/builtins/http.ts +++ b/packages/build-tools/bridge-src/builtins/http.ts @@ -26,7 +26,7 @@ import { dispatchCustomEmitterListeners, isProcessExitError, } from "./process.js"; -import { setImmediate } from "./timers.js"; +import { drainHttpRequestTimers, setImmediate } from "./timers.js"; import { TLSSocket, tlsConnect } from "./tls.js"; import { UndiciClient, undiciRequest } from "./undici.js"; @@ -4983,100 +4983,13 @@ async function dispatchServerRequest(serverId, requestJson) { const outgoing = new ServerResponseBridge(); incoming.socket = outgoing.socket; incoming.connection = outgoing.socket; - const pendingImmediates = []; - const pendingTimers = []; - const trackedTimers = /* @__PURE__ */ new Map(); - let consumedTimerCount = 0; - let consumedImmediateCount = 0; try { try { - const originalSetImmediate = globalThis.setImmediate; - const originalSetTimeout = globalThis.setTimeout; - const originalClearTimeout = globalThis.clearTimeout; - if (typeof originalSetImmediate === "function") { - globalThis.setImmediate = (callback, ...args) => { - const pending = new Promise((resolve) => { - queueMicrotask(() => { - try { - callback(...args); - } finally { - resolve(); - } - }); - }); - pendingImmediates.push(pending); - return 0; - }; - } - if (typeof originalSetTimeout === "function") { - globalThis.setTimeout = (callback, delay, ...args) => { - if (typeof callback !== "function") { - return originalSetTimeout(callback, delay, ...args); - } - const normalizedDelay = - typeof delay === "number" && Number.isFinite(delay) - ? Math.max(0, delay) - : 0; - if (normalizedDelay > 1e3) { - return originalSetTimeout(callback, normalizedDelay, ...args); - } - let resolvePending: () => void; - const pending = new Promise((resolve) => { - resolvePending = resolve; - }); - let handle: ReturnType; - handle = originalSetTimeout(() => { - trackedTimers.delete(handle); - try { - callback(...args); - } finally { - resolvePending(); - } - }, normalizedDelay); - trackedTimers.set(handle, resolvePending); - pendingTimers.push(pending); - return handle; - }; - } - if (typeof originalClearTimeout === "function") { - globalThis.clearTimeout = (handle) => { - if (handle != null) { - const resolvePending = trackedTimers.get(handle); - if (resolvePending) { - trackedTimers.delete(handle); - resolvePending(); - } - } - return originalClearTimeout(handle); - }; - } - try { - const listenerResult = listener(incoming, outgoing); + await drainHttpRequestTimers(() => { + const result = listener(incoming, outgoing); incoming._markBodyReady(); - await Promise.resolve(listenerResult); - while ( - consumedTimerCount < pendingTimers.length || - consumedImmediateCount < pendingImmediates.length - ) { - const pending = [ - ...pendingTimers.slice(consumedTimerCount), - ...pendingImmediates.slice(consumedImmediateCount), - ]; - consumedTimerCount = pendingTimers.length; - consumedImmediateCount = pendingImmediates.length; - await Promise.allSettled(pending); - } - } finally { - if (typeof originalSetImmediate === "function") { - globalThis.setImmediate = originalSetImmediate; - } - if (typeof originalSetTimeout === "function") { - globalThis.setTimeout = originalSetTimeout; - } - if (typeof originalClearTimeout === "function") { - globalThis.clearTimeout = originalClearTimeout; - } - } + return result; + }); } catch (err) { outgoing.statusCode = 500; try { @@ -5089,7 +5002,6 @@ async function dispatchServerRequest(serverId, requestJson) { outgoing.end(); } await outgoing.waitForClose(); - await Promise.allSettled([...pendingTimers, ...pendingImmediates]); return JSON.stringify(outgoing.serialize()); } finally { server._endRequestDispatch(); @@ -5168,101 +5080,14 @@ async function dispatchLoopbackServerRequest(serverOrId, requestInput) { const outgoing = new ServerResponseBridge(); incoming.socket = outgoing.socket; incoming.connection = outgoing.socket; - const pendingImmediates = []; - const pendingTimers = []; - const trackedTimers = /* @__PURE__ */ new Map(); - let consumedTimerCount = 0; - let consumedImmediateCount = 0; server._beginRequestDispatch(); try { try { - const originalSetImmediate = globalThis.setImmediate; - const originalSetTimeout = globalThis.setTimeout; - const originalClearTimeout = globalThis.clearTimeout; - if (typeof originalSetImmediate === "function") { - globalThis.setImmediate = (callback, ...args) => { - const pending = new Promise((resolve) => { - queueMicrotask(() => { - try { - callback(...args); - } finally { - resolve(); - } - }); - }); - pendingImmediates.push(pending); - return 0; - }; - } - if (typeof originalSetTimeout === "function") { - globalThis.setTimeout = (callback, delay, ...args) => { - if (typeof callback !== "function") { - return originalSetTimeout(callback, delay, ...args); - } - const normalizedDelay = - typeof delay === "number" && Number.isFinite(delay) - ? Math.max(0, delay) - : 0; - if (normalizedDelay > 1e3) { - return originalSetTimeout(callback, normalizedDelay, ...args); - } - let resolvePending: () => void; - const pending = new Promise((resolve) => { - resolvePending = resolve; - }); - let handle: ReturnType; - handle = originalSetTimeout(() => { - trackedTimers.delete(handle); - try { - callback(...args); - } finally { - resolvePending(); - } - }, normalizedDelay); - trackedTimers.set(handle, resolvePending); - pendingTimers.push(pending); - return handle; - }; - } - if (typeof originalClearTimeout === "function") { - globalThis.clearTimeout = (handle) => { - if (handle != null) { - const resolvePending = trackedTimers.get(handle); - if (resolvePending) { - trackedTimers.delete(handle); - resolvePending(); - } - } - return originalClearTimeout(handle); - }; - } - try { - const listenerResult = server._requestListener(incoming, outgoing); + await drainHttpRequestTimers(() => { + const result = server._requestListener(incoming, outgoing); incoming._markBodyReady(); - await Promise.resolve(listenerResult); - while ( - consumedTimerCount < pendingTimers.length || - consumedImmediateCount < pendingImmediates.length - ) { - const pending = [ - ...pendingTimers.slice(consumedTimerCount), - ...pendingImmediates.slice(consumedImmediateCount), - ]; - consumedTimerCount = pendingTimers.length; - consumedImmediateCount = pendingImmediates.length; - await Promise.allSettled(pending); - } - } finally { - if (typeof originalSetImmediate === "function") { - globalThis.setImmediate = originalSetImmediate; - } - if (typeof originalSetTimeout === "function") { - globalThis.setTimeout = originalSetTimeout; - } - if (typeof originalClearTimeout === "function") { - globalThis.clearTimeout = originalClearTimeout; - } - } + return result; + }); } catch (err) { outgoing.statusCode = 500; try { @@ -5275,7 +5100,6 @@ async function dispatchLoopbackServerRequest(serverOrId, requestInput) { outgoing.end(); } await outgoing.waitForClose(); - await Promise.allSettled([...pendingTimers, ...pendingImmediates]); let aborted = false; return { responseJson: JSON.stringify(outgoing.serialize()), diff --git a/packages/build-tools/bridge-src/builtins/module-loader.ts b/packages/build-tools/bridge-src/builtins/module-loader.ts index b93f289e91..ac34e0468a 100644 --- a/packages/build-tools/bridge-src/builtins/module-loader.ts +++ b/packages/build-tools/bridge-src/builtins/module-loader.ts @@ -2,7 +2,6 @@ import { exposeCustomGlobal, exposeMutableRuntimeStateGlobal, } from "../global-exposure.js"; -import { defineGlobal } from "../polyfills/index.js"; import { builtinModules, loadBuiltinModule, @@ -140,10 +139,8 @@ function createRequire(filename) { const filepath = _parseFileUrl(String(filename)); const dirname = _pathDirname(filepath); const resolve = createRequireResolve(dirname); - const rejectRestrictedBuiltin = function (_request) {}; const requireFn = function (request) { - rejectRestrictedBuiltin(request); - return _requireFrom(request, dirname); + return requireFrom(request, dirname); }; requireFn.resolve = resolve; return attachRequireMetadata(requireFn); @@ -155,7 +152,10 @@ function createRequire(filename) { // module wrapper, and resolution still flows through _requireFrom with the same // permission checks — and the namespaced name avoids changing `typeof require` for // guest code that branches on CJS-vs-ESM. -defineGlobal("__agentOsGuestCreateRequire", createRequire); +Object.defineProperty(globalThis, "__agentOsGuestCreateRequire", { + enumerable: true, + value: createRequire, +}); var Module = class _Module { id; path; diff --git a/packages/build-tools/bridge-src/builtins/timers.ts b/packages/build-tools/bridge-src/builtins/timers.ts index 86899e1a54..206fd2fba9 100644 --- a/packages/build-tools/bridge-src/builtins/timers.ts +++ b/packages/build-tools/bridge-src/builtins/timers.ts @@ -1,6 +1,7 @@ import { exposeCustomGlobal } from "../global-exposure.js"; import { bridgeDispatchSync } from "../transport.js"; import { + builtinAsyncHooksModule, getAsyncContextFrame, runInAsyncContextFrame, } from "./async-context.js"; @@ -399,6 +400,8 @@ function timerDispatch(_eventType, payload) { } return; } finally { + entry.complete?.(); + entry.complete = undefined; entry.handle._firing = false; _firingTimer = previousFiring; } @@ -412,6 +415,50 @@ function timerDispatch(_eventType, payload) { checkTimerDrain(); } +// Legacy HTTP dispatch waits for short, request-owned callback continuations. +const httpRequestTimers = new builtinAsyncHooksModule.AsyncLocalStorage(); +const MAX_HTTP_REQUEST_TIMERS = 1024; + +export async function drainHttpRequestTimers(listener) { + const tracker = { pending: new Set>(), active: true }; + try { + await httpRequestTimers.run(tracker, listener); + while (tracker.pending.size > 0) { + await Promise.allSettled(tracker.pending); + } + } finally { + tracker.active = false; + tracker.pending.clear(); + } +} + +function trackHttpRequestTimer() { + const tracker = httpRequestTimers.getStore(); + if (!tracker?.active) return undefined; + if (tracker.pending.size >= MAX_HTTP_REQUEST_TIMERS) { + throw Object.assign( + new RangeError("HTTP pending request timer limit (1024) exceeded"), + { + code: "ERR_AGENTOS_HTTP_REQUEST_TIMER_LIMIT", + }, + ); + } + if (tracker.pending.size === Math.floor(MAX_HTTP_REQUEST_TIMERS * 0.8)) { + console.warn( + "HTTP request timer tracking approaches its pending timer limit", + ); + } + let resolve: () => void; + const pending = new Promise((done) => { + resolve = done; + }); + tracker.pending.add(pending); + return () => { + tracker.pending.delete(pending); + resolve(); + }; +} + function createTimer(callback, delay, args, repeat) { const actualDelay = Math.max(1, normalizeTimerDelay(delay)); const id = createKernelTimer(actualDelay, repeat); @@ -433,7 +480,23 @@ function createTimer(callback, delay, args, repeat) { args, repeat, delay: actualDelay, + complete: undefined, }; + if (!repeat && actualDelay <= 1000) { + try { + entry.complete = trackHttpRequestTimer(); + } catch (error) { + try { + bridgeDispatchSync(TIMER_DISPATCH.clear, id); + } catch (cleanupError) { + throw new AggregateError( + [error, cleanupError], + "Timer admission failed", + ); + } + throw error; + } + } handle._entry = entry; _timerEntries.set(id, entry); _timerHandles.set(id, handle); @@ -456,6 +519,7 @@ function clearTimeout2(timer) { bridgeDispatchSync(TIMER_DISPATCH.clear, handle._kernelId); handle._destroyed = true; handle._cleared = true; + handle._entry?.complete?.(); handle._entry = void 0; _timerEntries.delete(handle._kernelId); _timerHandles.delete(handle._id); @@ -497,6 +561,8 @@ function _drainImmediates() { throw outcome.rethrow; } return; + } finally { + entry.complete?.(); } if (typeof _exited !== "undefined" && _exited) { checkTimerDrain(); @@ -523,6 +589,7 @@ function setImmediate(callback, ...args) { asyncContextFrame: getAsyncContextFrame(), args, repeat: false, + complete: trackHttpRequestTimer(), }); return handle; } @@ -534,6 +601,7 @@ function clearImmediate(timer) { if (entry) { entry.handle._destroyed = true; _immediateEntries.delete(id); + entry.complete?.(); checkTimerDrain(); return; } diff --git a/packages/build-tools/bridge-src/global-exposure.ts b/packages/build-tools/bridge-src/global-exposure.ts index 3a9aec36e0..82d2141292 100644 --- a/packages/build-tools/bridge-src/global-exposure.ts +++ b/packages/build-tools/bridge-src/global-exposure.ts @@ -1959,7 +1959,26 @@ function exposeGlobalBinding(target, name, value, options = {}) { enumerable, }); } +// Runtime event dispatch, module loading, and lifecycle accounting use these bindings; +// the platform-specific scrub does not remove them. +const stableRuntimeGlobals = new Set([ + "_dgramSocketDispatch", + "_httpServerDispatch", + "_netSocketDispatch", + "_processExitRequested", + "_registerHandle", + "_requireFrom", +]); function exposeCustomGlobal(name, value) { + if (stableRuntimeGlobals.has(name)) { + Object.defineProperty(globalThis, name, { + value, + writable: false, + configurable: false, + enumerable: true, + }); + return; + } exposeGlobalBinding(globalThis, name, value); } function exposeInstallCompatibleHardenedGlobal(name, value) { diff --git a/packages/build-tools/bridge-src/polyfills/compression.ts b/packages/build-tools/bridge-src/polyfills/compression.ts index 9b5c370a22..c14707f0a5 100644 --- a/packages/build-tools/bridge-src/polyfills/compression.ts +++ b/packages/build-tools/bridge-src/polyfills/compression.ts @@ -30,7 +30,13 @@ function compressionPair(format, decompress) { throw error; } const zlib = globalThis.__agentOsBuiltinZlibModule; - const stream = zlib[methods[name]](); + const factory = zlib?.[methods[name]]; + if (typeof factory !== "function") { + throw Object.assign(new Error("Web compression backend is unavailable"), { + code: "ERR_AGENTOS_COMPRESSION_BACKEND", + }); + } + const stream = factory.call(zlib); let readEnded = false, writeEnded = false; let readController: ReadableStreamDefaultController; diff --git a/packages/core/package.json b/packages/core/package.json index 97823fc3ff..7e9a980f25 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -146,7 +146,7 @@ "check-types": "tsc --noEmit && tsc -p tsconfig.type-tests.json", "build": "pnpm run build:protocols && tsc && node scripts/stage-default-software.mjs && pnpm run copy-commands", "test": "vitest run --exclude '**/*.nightly.test.ts' --reporter=verbose", - "test:unit": "pnpm test:source && vitest run tests/buffer-source.test.ts tests/fork-ipc-ownership.native.test.ts tests/ipc-transport.test.ts tests/http-child-listener.native.test.ts tests/node-fork-ipc.native.test.ts tests/node-test-context.native.test.ts tests/node-v8-serialization.native.test.ts tests/node-v8-native-wire.native.test.ts tests/node-top-level-identity.native.test.ts tests/node-server-request-iterator.native.test.ts tests/node-url-format.native.test.ts tests/node-web-streams-crypto.native.test.ts tests/agentos-package.test.ts tests/allowed-node-builtins.test.ts tests/host-functions-zod.test.ts tests/host-functions.test.ts tests/hosted-client.test.ts tests/host-function-command-exec.test.ts tests/host-function-permissions.test.ts tests/host-function-file-authorization.test.ts tests/child-process-credentials.test.ts tests/child-process-binary-output.test.ts tests/child-pipe-eof.test.ts tests/child-output-once.test.ts tests/canonical-timers.test.ts tests/child-process-executable-authorization.test.ts tests/child-process-shebang-privacy.test.ts tests/child-process-posix-authorization.test.ts tests/host-function-globals.test.ts tests/http-agent-factory.test.ts tests/http-server-error-lifetime.test.ts tests/cron-manager.test.ts tests/code-mode-execution.test.ts tests/cron-timer-driver.test.ts tests/exec-stdin.test.ts tests/exec-timeout.test.ts tests/execution-abort.test.ts tests/creation-cleanup-error.test.ts tests/fetch-stream-disposal.native.test.ts tests/filesystem-quota.native.test.ts tests/generated-protocol.test.ts tests/node-signal-registration.test.ts tests/node-stdio-write.test.ts tests/network.test.ts tests/leak-agent-os-processes.test.ts tests/leak-rpc-client.test.ts tests/mount-descriptors.test.ts tests/mount-reconfigure.test.ts tests/native-owner-scope.test.ts tests/network-deny-vm-local.test.ts tests/options-schema.test.ts tests/output-replay.test.ts tests/terminal-pending-exit.test.ts tests/process-exit-signal-routing.test.ts tests/process-exit-signal.test.ts tests/process-tree-after-drain.test.ts tests/kernel-bootstrap-base.test.ts tests/kernel-proxy-dispose.test.ts tests/process-output-exit-ordering.test.ts tests/public-api-exports.test.ts tests/python-workspace-fs.test.ts tests/retained-bridge-lifetime.test.ts tests/root-filesystem-descriptors.test.ts tests/runtime-compat-mount.test.ts tests/shared-sidecar-callback-isolation.test.ts tests/shared-sidecar-clean-exit.test.ts tests/sidecar-client.test.ts tests/sidecar-exit.test.ts tests/sidecar-permission-descriptors.test.ts tests/stdio-client.test.ts tests/stdio-permission-rules.test.ts tests/stdin-readable.test.ts tests/sync-entry-errors.native.test.ts tests/software-install.test.ts tests/software-source-url.native.test.ts tests/software-registry.test.ts tests/wasm-permission-tiers.test.ts tests/wasm-stdio-stat.test.ts tests/uint8array-encoding.test.ts --fileParallelism=false", + "test:unit": "pnpm test:source && vitest run tests/buffer-source.test.ts tests/fork-ipc-ownership.native.test.ts tests/ipc-transport.test.ts tests/http-child-listener.native.test.ts tests/node-fork-ipc.native.test.ts tests/node-test-context.native.test.ts tests/node-v8-serialization.native.test.ts tests/node-v8-native-wire.native.test.ts tests/node-top-level-identity.native.test.ts tests/node-server-request-iterator.native.test.ts tests/node-url-format.native.test.ts tests/node-web-streams-crypto.native.test.ts tests/agentos-package.test.ts tests/allowed-node-builtins.test.ts tests/host-functions-zod.test.ts tests/host-functions.test.ts tests/hosted-client.test.ts tests/host-function-command-exec.test.ts tests/host-function-permissions.test.ts tests/host-function-file-authorization.test.ts tests/child-process-credentials.test.ts tests/child-process-binary-output.test.ts tests/child-pipe-eof.test.ts tests/child-output-once.test.ts tests/canonical-timers.test.ts tests/child-process-executable-authorization.test.ts tests/child-process-shebang-privacy.test.ts tests/child-process-posix-authorization.test.ts tests/host-function-globals.test.ts tests/http-agent-factory.test.ts tests/http-server-error-lifetime.test.ts tests/cron-manager.test.ts tests/code-mode-execution.test.ts tests/cron-timer-driver.test.ts tests/exec-stdin.test.ts tests/exec-timeout.test.ts tests/execution-abort.test.ts tests/creation-cleanup-error.test.ts tests/fetch-stream-disposal.native.test.ts tests/filesystem-quota.native.test.ts tests/generated-protocol.test.ts tests/node-signal-registration.test.ts tests/node-stdio-write.test.ts tests/network.test.ts tests/leak-agent-os-processes.test.ts tests/leak-rpc-client.test.ts tests/mount-descriptors.test.ts tests/mount-reconfigure.test.ts tests/native-owner-scope.test.ts tests/network-deny-vm-local.test.ts tests/options-schema.test.ts tests/output-replay.test.ts tests/terminal-pending-exit.test.ts tests/process-exit-signal-routing.test.ts tests/process-exit-signal.test.ts tests/process-tree-after-drain.test.ts tests/kernel-bootstrap-base.test.ts tests/kernel-proxy-dispose.test.ts tests/process-output-exit-ordering.test.ts tests/public-api-exports.test.ts tests/python-workspace-fs.test.ts tests/retained-bridge-lifetime.test.ts tests/root-filesystem-descriptors.test.ts tests/runtime-compat-mount.test.ts tests/shared-sidecar-callback-isolation.test.ts tests/shared-sidecar-clean-exit.test.ts tests/sidecar-client.test.ts tests/sidecar-exit.test.ts tests/sidecar-permission-descriptors.test.ts tests/stdio-client.test.ts tests/stdio-permission-rules.test.ts tests/stdin-readable.test.ts tests/sync-entry-errors.native.test.ts tests/software-install.test.ts tests/software-source-url.native.test.ts tests/software-registry.test.ts tests/wasm-permission-tiers.test.ts tests/wasm-stdio-stat.test.ts tests/uint8array-encoding.test.ts --fileParallelism=false tests/code-execution-capture.test.ts tests/node-runtime-execution-options.test.ts", "test:pr": "pnpm test:unit", "test:nightly": "vitest run tests/*.nightly.test.ts --reporter=verbose --passWithNoTests", "build:sidecar-protocol": "pnpm --dir ../build-tools build:protocol", diff --git a/packages/core/src/agent-os.ts b/packages/core/src/agent-os.ts index 526c33971f..23e010c4a9 100644 --- a/packages/core/src/agent-os.ts +++ b/packages/core/src/agent-os.ts @@ -13,6 +13,10 @@ import type { MountConfigJsonValue, NativeMountPluginDescriptor, } from "./descriptors.js"; +import { + checkExecutionCpuLimit, + checkExecutionTimingMitigation, +} from "./execution-options.js"; import type { ReactorLimitsConfig } from "./generated/ReactorLimitsConfig.js"; import * as executionProtocol from "./generated-protocol.js"; import { @@ -3332,6 +3336,8 @@ export class AgentOs { command: string, options?: KernelExecOptions, ): Promise { + checkExecutionCpuLimit(options?.cpuTimeLimitMs); + checkExecutionTimingMitigation(options?.timingMitigation); return this.#kernel.exec(command, options); } @@ -3341,6 +3347,8 @@ export class AgentOs { args: readonly string[] = [], options?: KernelExecOptions, ): Promise { + checkExecutionCpuLimit(options?.cpuTimeLimitMs); + checkExecutionTimingMitigation(options?.timingMitigation); const kernel = this.#kernel as unknown as { execArgv( command: string, diff --git a/packages/core/src/code-execution.ts b/packages/core/src/code-execution.ts index f520891efb..b5a40260d9 100644 --- a/packages/core/src/code-execution.ts +++ b/packages/core/src/code-execution.ts @@ -1,3 +1,7 @@ +import { HostTextOutputCapture } from "./host-output-capture.js"; + +export { CapturedOutputLimitError } from "./host-output-capture.js"; + export interface CodeExecutionSpawnOptions { cwd?: string; env?: Record; @@ -35,6 +39,8 @@ export interface CodeExecutionOptions { argv?: string[]; stdin?: string | Uint8Array; timeoutMs?: number; + /** Combined stdout/stderr capture limit in bytes. Defaults to 16 MiB. */ + maxCapturedOutputBytes?: number; signal?: AbortSignal; onStdout?: (chunk: string) => void; onStderr?: (chunk: string) => void; @@ -110,11 +116,8 @@ export function spawnCodeProcess( throw new RangeError("timeoutMs must be a finite, non-negative number"); } + const capture = new HostTextOutputCapture(options.maxCapturedOutputBytes); const { pid } = vm.spawn(command, args, toSpawnOptions(options)); - const stdoutDecoder = new TextDecoder(); - const stderrDecoder = new TextDecoder(); - let stdout = ""; - let stderr = ""; let callbackError: unknown; let aborted = false; let finalized = false; @@ -122,15 +125,9 @@ export function spawnCodeProcess( const unsubscribeOutput = vm.onProcessOutput(pid, (event) => { try { - if (event.stream === "stdout") { - const text = stdoutDecoder.decode(event.data, { stream: true }); - stdout += text; - options.onStdout?.(text); - } else { - const text = stderrDecoder.decode(event.data, { stream: true }); - stderr += text; - options.onStderr?.(text); - } + const text = capture.append(event.stream, event.data); + if (event.stream === "stdout") options.onStdout?.(text); + else options.onStderr?.(text); } catch (error) { callbackError ??= error; vm.killProcess(pid); @@ -153,8 +150,6 @@ export function spawnCodeProcess( unsubscribeOutput(); options.signal?.removeEventListener("abort", abort); if (timeout !== undefined) clearTimeout(timeout); - stdout += stdoutDecoder.decode(); - stderr += stderrDecoder.decode(); await options.onComplete?.(); }; @@ -180,8 +175,7 @@ export function spawnCodeProcess( return { success: exitCode === 0, exitCode, - stdout, - stderr, + ...capture.finish(), }; })(); return waitPromise; diff --git a/packages/core/src/execution-options.ts b/packages/core/src/execution-options.ts new file mode 100644 index 0000000000..d961e7f911 --- /dev/null +++ b/packages/core/src/execution-options.ts @@ -0,0 +1,23 @@ +export function checkExecutionCpuLimit(limit: number | undefined): void { + if (limit !== undefined) { + throw unsupportedExecutionOption( + "Per-run cpuTimeLimitMs is unsupported by the native sidecar; configure VM limits.jsRuntime.cpuTimeLimitMs or limits.wasm.activeCpuTimeLimitMs instead", + ); + } +} + +export function checkExecutionTimingMitigation( + mode: "off" | "freeze" | undefined, +): void { + if (mode !== undefined && mode !== "off") { + throw unsupportedExecutionOption( + "Per-run timingMitigation is unsupported by the native sidecar; configure VM-level highResolutionTime to control clock precision", + ); + } +} + +function unsupportedExecutionOption(message: string): Error { + return Object.assign(new Error(message), { + code: "ERR_AGENTOS_UNSUPPORTED_EXECUTION_OPTION", + }); +} diff --git a/packages/core/src/host-output-capture.ts b/packages/core/src/host-output-capture.ts new file mode 100644 index 0000000000..5564018234 --- /dev/null +++ b/packages/core/src/host-output-capture.ts @@ -0,0 +1,83 @@ +export const DEFAULT_HOST_CAPTURE_LIMIT_BYTES = 16 * 1024 * 1024; + +export class CapturedOutputLimitError extends Error { + readonly code = "ERR_AGENTOS_CAPTURED_OUTPUT_LIMIT"; + + constructor( + readonly limitBytes: number, + configuration: string, + ) { + super(`Captured output exceeded ${limitBytes} bytes; ${configuration}`); + this.name = "CapturedOutputLimitError"; + } +} + +/** Budget host-owned capture independently of the sidecar's output policy. */ +export class HostOutputCaptureBudget { + private error: CapturedOutputLimitError | undefined; + private bytes = 0; + private warned = false; + + constructor( + readonly limit: number, + private readonly configuration: string, + ) { + if (!Number.isSafeInteger(limit) || limit < 1) { + throw new RangeError( + "maxCapturedOutputBytes must be a positive safe integer", + ); + } + } + + append(bytes: number): CapturedOutputLimitError | undefined { + if (this.error) return this.error; + if (bytes > this.limit - this.bytes) { + this.error = new CapturedOutputLimitError(this.limit, this.configuration); + return this.error; + } + this.bytes += bytes; + if (!this.warned && this.bytes >= this.limit * 0.8) { + this.warned = true; + console.warn( + `[agentOS] captured output reached ${this.bytes} of ${this.limit} bytes; ${this.configuration}`, + ); + } + return undefined; + } + + check(): void { + if (this.error) throw this.error; + } +} + +export class HostTextOutputCapture { + private readonly budget: HostOutputCaptureBudget; + private readonly decoders = { + stdout: new TextDecoder(), + stderr: new TextDecoder(), + }; + private readonly text = { stdout: "", stderr: "" }; + + constructor(limit = DEFAULT_HOST_CAPTURE_LIMIT_BYTES) { + this.budget = new HostOutputCaptureBudget( + limit, + "raise maxCapturedOutputBytes", + ); + } + + /** Decode each stream separately, and keep streaming after capture fills. */ + append(stream: "stdout" | "stderr", chunk: Uint8Array): string { + const error = this.budget.append(chunk.byteLength); + const text = this.decoders[stream].decode(chunk, { stream: true }); + if (!error) this.text[stream] += text; + return text; + } + + finish(): { stdout: string; stderr: string } { + this.budget.check(); + return { + stdout: this.text.stdout + this.decoders.stdout.decode(), + stderr: this.text.stderr + this.decoders.stderr.decode(), + }; + } +} diff --git a/packages/core/src/kernel-proxy.ts b/packages/core/src/kernel-proxy.ts index 112818a15c..bf88daf9d9 100644 --- a/packages/core/src/kernel-proxy.ts +++ b/packages/core/src/kernel-proxy.ts @@ -3,6 +3,7 @@ import { rmSync } from "node:fs"; import { constants as osConstants } from "node:os"; import { posix as posixPath } from "node:path"; import type { NativeMountPluginDescriptor } from "./descriptors.js"; +import { HostTextOutputCapture } from "./host-output-capture.js"; import type { AuthenticatedSession, CreatedVm, @@ -534,8 +535,7 @@ export class SidecarKernelProxy { ); } - const stdoutChunks: Uint8Array[] = []; - const stderrChunks: Uint8Array[] = []; + const capture = new HostTextOutputCapture(options?.maxCapturedOutputBytes); const effectiveCwd = options?.cwd ?? this.defaultExecCwd ?? this.cwd; const parsedCommand = parseSimpleExecCommand(command); const resolveExecPath = (targetPath: string) => @@ -581,12 +581,7 @@ export class SidecarKernelProxy { return { exitCode, - stdout: Buffer.concat( - stdoutChunks.map((chunk) => Buffer.from(chunk)), - ).toString("utf8"), - stderr: Buffer.concat( - stderrChunks.map((chunk) => Buffer.from(chunk)), - ).toString("utf8"), + ...capture.finish(), }; }; if ( @@ -656,11 +651,11 @@ export class SidecarKernelProxy { ...options, cwd: effectiveCwd, onStdout: (chunk) => { - stdoutChunks.push(chunk); + capture.append("stdout", chunk); options?.onStdout?.(chunk); }, onStderr: (chunk) => { - stderrChunks.push(chunk); + capture.append("stderr", chunk); options?.onStderr?.(chunk); }, }), @@ -670,11 +665,11 @@ export class SidecarKernelProxy { ...options, cwd: effectiveCwd, onStdout: (chunk) => { - stdoutChunks.push(chunk); + capture.append("stdout", chunk); options?.onStdout?.(chunk); }, onStderr: (chunk) => { - stderrChunks.push(chunk); + capture.append("stderr", chunk); options?.onStderr?.(chunk); }, }); diff --git a/packages/core/src/node-runtime.ts b/packages/core/src/node-runtime.ts index 0b6c95525a..0ee28d6933 100644 --- a/packages/core/src/node-runtime.ts +++ b/packages/core/src/node-runtime.ts @@ -1,3 +1,4 @@ +import { checkExecutionCpuLimit } from "./execution-options.js"; /** * NodeRuntime — ergonomic façade for running guest JavaScript end-to-end. * @@ -28,6 +29,12 @@ import type { HostFunctionCollections, HostFunctionSchemas, } from "./host-functions.js"; +import { + CapturedOutputLimitError, + DEFAULT_HOST_CAPTURE_LIMIT_BYTES, + HostOutputCaptureBudget, + HostTextOutputCapture, +} from "./host-output-capture.js"; import { parseNodeRuntimeCreateOptions } from "./node-runtime-options-schema.js"; import type { SidecarProcess } from "./sidecar-process.js"; import type { @@ -353,7 +360,9 @@ export interface NodeRuntimeExecOptions { stdin?: string | Uint8Array; /** Abort the run after this many milliseconds. */ timeout?: number; - /** Bound active guest CPU time independently of elapsed wall time. */ + /** Maximum combined host-captured stdout and stderr bytes. Defaults to 16 MiB. */ + maxCapturedOutputBytes?: number; + /** Unsupported by the native sidecar. Configure VM limits.jsRuntime.cpuTimeLimitMs. */ cpuTimeLimitMs?: number; /** * Cancel the run when this signal aborts. On abort the guest process is @@ -374,14 +383,14 @@ export interface NodeRuntimeExecOptions { * Called with each chunk the guest writes to stdout as it is produced, * letting you observe output incrementally instead of waiting for the run to * finish. Chunks arrive as raw bytes; decode with a `TextDecoder` for text. - * The complete output is still returned as `result.stdout` when the run ends. + * The run returns `result.stdout` when capture stays within its byte limit. */ onStdout?: (chunk: Uint8Array) => void; /** * Called with each chunk the guest writes to stderr as it is produced, * letting you observe output incrementally instead of waiting for the run to * finish. Chunks arrive as raw bytes; decode with a `TextDecoder` for text. - * The complete output is still returned as `result.stderr` when the run ends. + * The run returns `result.stderr` when capture stays within its byte limit. */ onStderr?: (chunk: Uint8Array) => void; } @@ -531,6 +540,8 @@ export interface NodeRuntimeResourceSnapshot { export interface NodeRuntimeResidentRunnerExecOptions { /** Abort the guest eval after this many milliseconds. */ timeout?: number; + /** Maximum combined output per request, including framing. Defaults to 16 MiB. */ + maxCapturedOutputBytes?: number; } export type NodeRuntimeResidentRunnerOptions = {}; @@ -710,6 +721,7 @@ export class NodeRuntime { code: string, options: NodeRuntimeExecOptions = {}, ): Promise { + checkExecutionCpuLimit(options.cpuTimeLimitMs); const programPath = `/tmp/agentos-program-${nextProgramId++}.mjs`; await this.kernel.writeFile(programPath, withHostFunctionPreamble(code)); return this.runProgram(programPath, options); @@ -732,10 +744,12 @@ export class NodeRuntime { const signal = options.signal; if (!signal) { const result = await this.kernel.exec(`node ${programPath}`, { + wasmBackend: options.wasmBackend, env: options.env, cwd: options.cwd, stdin: options.stdin, timeout: options.timeout, + maxCapturedOutputBytes: options.maxCapturedOutputBytes, onStdout: options.onStdout, onStderr: options.onStderr, }); @@ -750,17 +764,17 @@ export class NodeRuntime { // kill: aborting the signal maps to a kernel kill of the underlying // process. Aggregate the streamed output ourselves to reproduce the // run-to-completion result that the shell path returns. - const stdoutChunks: Uint8Array[] = []; - const stderrChunks: Uint8Array[] = []; + const capture = new HostTextOutputCapture(options.maxCapturedOutputBytes); const proc = this.kernel.spawn("node", [programPath], { + wasmBackend: options.wasmBackend, env: options.env, cwd: options.cwd, onStdout: (chunk) => { - stdoutChunks.push(chunk); + capture.append("stdout", chunk); options.onStdout?.(chunk); }, onStderr: (chunk) => { - stderrChunks.push(chunk); + capture.append("stderr", chunk); options.onStderr?.(chunk); }, streamStdin: options.stdin !== undefined, @@ -790,8 +804,7 @@ export class NodeRuntime { throw toAbortError(signal); } return { - stdout: decodeChunks(stdoutChunks), - stderr: decodeChunks(stderrChunks), + ...capture.finish(), exitCode, }; } finally { @@ -829,9 +842,11 @@ export class NodeRuntime { code: string, options: NodeRuntimeSpawnOptions = {}, ): Promise { + checkExecutionCpuLimit(options.cpuTimeLimitMs); const programPath = `/tmp/agentos-program-${nextProgramId++}.mjs`; await this.kernel.writeFile(programPath, withHostFunctionPreamble(code)); const proc = this.kernel.spawn("node", [programPath], { + wasmBackend: options.wasmBackend, env: options.env, cwd: options.cwd, onStdout: options.onStdout, @@ -871,6 +886,7 @@ export class NodeRuntime { args: string[] = [], options: NodeRuntimeSpawnOptions = {}, ): NodeRuntimeProcess { + checkExecutionCpuLimit(options.cpuTimeLimitMs); const proc = this.kernel.spawn(command, args, { wasmBackend: options.wasmBackend, env: options.env, @@ -909,19 +925,18 @@ export class NodeRuntime { args: string[] = [], options: NodeRuntimeExecOptions = {}, ): Promise { - const stdoutChunks: Uint8Array[] = []; - const stderrChunks: Uint8Array[] = []; + const capture = new HostTextOutputCapture(options.maxCapturedOutputBytes); const proc = this.spawnCommand(command, args, { wasmBackend: options.wasmBackend, env: options.env, cwd: options.cwd, cpuTimeLimitMs: options.cpuTimeLimitMs, onStdout: (chunk) => { - stdoutChunks.push(chunk); + capture.append("stdout", chunk); options.onStdout?.(chunk); }, onStderr: (chunk) => { - stderrChunks.push(chunk); + capture.append("stderr", chunk); options.onStderr?.(chunk); }, }); @@ -951,8 +966,7 @@ export class NodeRuntime { throw toAbortError(options.signal); } return { - stdout: decodeChunks(stdoutChunks), - stderr: decodeChunks(stderrChunks), + ...capture.finish(), exitCode, }; } finally { @@ -975,6 +989,7 @@ export class NodeRuntime { code: string, options: NodeRuntimeExecOptions = {}, ): Promise> { + checkExecutionCpuLimit(options.cpuTimeLimitMs); const id = nextProgramId++; const resultPath = `/tmp/agentos-result-${id}.json`; const programPath = `/tmp/agentos-program-${id}.mjs`; @@ -1218,10 +1233,14 @@ for await (const line of rl) { class ResidentNodeRunner implements NodeRuntimeResidentRunner { private proc: NodeRuntimeProcess | null = null; private stdoutBuffer = ""; + private readonly stdoutDecoder = new TextDecoder(); + private failure: Error | undefined; private active: { id: number; - stdout: Uint8Array[]; - stderr: Uint8Array[]; + stdout: string; + stderr: string; + stderrDecoder: InstanceType; + capture: HostOutputCaptureBudget; resolve: (result: NodeRuntimeExecResult) => void; reject: (error: Error) => void; timer?: ReturnType; @@ -1235,6 +1254,8 @@ class ResidentNodeRunner implements NodeRuntimeResidentRunner { this.resolveReady = resolve; this.rejectReady = reject; }); + // Output may arrive before create() finishes obtaining its process handle. + void this.readyPromise.catch(() => {}); } static async create(runtime: NodeRuntime): Promise { @@ -1260,7 +1281,12 @@ class ResidentNodeRunner implements NodeRuntimeResidentRunner { runner.active = null; }, ); - await runner.readyPromise; + try { + await runner.readyPromise; + } catch (error) { + await runner.dispose(); + throw error; + } return runner; } @@ -1269,6 +1295,7 @@ class ResidentNodeRunner implements NodeRuntimeResidentRunner { options: NodeRuntimeResidentRunnerExecOptions = {}, ): Promise { await this.readyPromise; + if (this.failure) throw this.failure; if (!this.proc) { throw new Error("resident runner is not running"); } @@ -1277,11 +1304,17 @@ class ResidentNodeRunner implements NodeRuntimeResidentRunner { } const proc = this.proc; const id = nextResidentRequestId++; + const capture = new HostOutputCaptureBudget( + options.maxCapturedOutputBytes ?? DEFAULT_HOST_CAPTURE_LIMIT_BYTES, + "raise maxCapturedOutputBytes", + ); return new Promise((resolve, reject) => { const active = { id, - stdout: [], - stderr: [], + stdout: "", + stderr: "", + stderrDecoder: new TextDecoder(), + capture, resolve, reject, timer: undefined as ReturnType | undefined, @@ -1312,7 +1345,29 @@ class ResidentNodeRunner implements NodeRuntimeResidentRunner { } private handleStdout(chunk: Uint8Array): void { - this.stdoutBuffer += new TextDecoder().decode(chunk); + if (this.failure) return; + const active = this.active; + const captureError = active?.capture.append(chunk.byteLength); + if (captureError) { + this.failOutput(captureError); + return; + } + // Idle output has no request budget. Bound the parser's carry and input + // even if a resident writes before it is ready or after a request ends. + if ( + !active && + chunk.byteLength > + DEFAULT_HOST_CAPTURE_LIMIT_BYTES - this.stdoutBuffer.length + ) { + this.failOutput( + new CapturedOutputLimitError( + DEFAULT_HOST_CAPTURE_LIMIT_BYTES, + "limit unsolicited resident output or use a streaming process", + ), + ); + return; + } + this.stdoutBuffer += this.stdoutDecoder.decode(chunk, { stream: true }); while (true) { const newlineIndex = this.stdoutBuffer.indexOf("\n"); if (newlineIndex < 0) { @@ -1329,12 +1384,30 @@ class ResidentNodeRunner implements NodeRuntimeResidentRunner { this.finishRequest(line.slice(RESIDENT_RESULT_PREFIX.length)); continue; } - this.active?.stdout.push(new TextEncoder().encode(`${line}\n`)); + if (this.active) this.active.stdout += `${line}\n`; } } private handleStderr(chunk: Uint8Array): void { - this.active?.stderr.push(chunk); + const active = this.active; + if (!active || this.failure || chunk.byteLength === 0) return; + const captureError = active.capture.append(chunk.byteLength); + if (captureError) { + this.failOutput(captureError); + return; + } + active.stderr += active.stderrDecoder.decode(chunk, { stream: true }); + } + + private failOutput(error: CapturedOutputLimitError): void { + this.failure = error; + this.stdoutBuffer = ""; + this.rejectReady(error); + const active = this.active; + this.active = null; + if (active?.timer !== undefined) clearTimeout(active.timer); + active?.reject(error); + this.proc?.kill("SIGKILL"); } private finishRequest(payload: string): void { @@ -1365,9 +1438,10 @@ class ResidentNodeRunner implements NodeRuntimeResidentRunner { clearTimeout(active.timer); } this.active = null; - const stderr = `${decodeChunks(active.stderr)}${parsed.stderr ?? ""}`; + const stderr = + active.stderr + active.stderrDecoder.decode() + (parsed.stderr ?? ""); active.resolve({ - stdout: decodeChunks(active.stdout), + stdout: active.stdout, stderr, exitCode: parsed.exitCode ?? 1, }); @@ -1465,23 +1539,6 @@ function delayUntil(ms: number, signal?: AbortSignal): Promise { * {@link NodeRuntime.exec} path returns when a run is driven as a process for * cancellation support. */ -function decodeChunks(chunks: Uint8Array[]): string { - if (chunks.length === 0) { - return ""; - } - let total = 0; - for (const chunk of chunks) { - total += chunk.length; - } - const merged = new Uint8Array(total); - let offset = 0; - for (const chunk of chunks) { - merged.set(chunk, offset); - offset += chunk.length; - } - return new TextDecoder().decode(merged); -} - function toExecResult(result: ExecResult): NodeRuntimeExecResult { return { stdout: result.stdout, diff --git a/packages/core/src/runtime-compat.ts b/packages/core/src/runtime-compat.ts index 8e702b805e..33b4415810 100644 --- a/packages/core/src/runtime-compat.ts +++ b/packages/core/src/runtime-compat.ts @@ -275,6 +275,8 @@ export interface ExecOptions { onStdout?: (data: Uint8Array) => void; onStderr?: (data: Uint8Array) => void; captureStdio?: boolean; + /** Maximum combined host-captured stdout and stderr bytes. Defaults to 16 MiB. */ + maxCapturedOutputBytes?: number; filePath?: string; cpuTimeLimitMs?: number; timingMitigation?: TimingMitigation; diff --git a/packages/core/src/runtime.ts b/packages/core/src/runtime.ts index 7cfdcc24fe..6651305916 100644 --- a/packages/core/src/runtime.ts +++ b/packages/core/src/runtime.ts @@ -161,6 +161,8 @@ export interface ExecOptions { cwd?: string; stdin?: string | Uint8Array; timeout?: number; + /** Maximum combined host-captured stdout and stderr bytes. Defaults to 16 MiB. */ + maxCapturedOutputBytes?: number; onStdout?: (data: Uint8Array) => void; onStderr?: (data: Uint8Array) => void; captureStdio?: boolean; diff --git a/packages/core/src/sidecar/rpc-client.ts b/packages/core/src/sidecar/rpc-client.ts index b8bebad47b..07928cdc1e 100644 --- a/packages/core/src/sidecar/rpc-client.ts +++ b/packages/core/src/sidecar/rpc-client.ts @@ -9,6 +9,7 @@ import type { RootLowerInput, } from "../agent-os.js"; import type { FilesystemEntry } from "../filesystem-snapshot.js"; +import { HostTextOutputCapture } from "../host-output-capture.js"; import type { RootSnapshotExport } from "../layers.js"; import type { ProcessForestRecord } from "../process-forest.js"; import { @@ -770,8 +771,7 @@ export class SidecarKernelProxy { ); } - const stdoutChunks: Uint8Array[] = []; - const stderrChunks: Uint8Array[] = []; + const capture = new HostTextOutputCapture(options?.maxCapturedOutputBytes); const deadlineMs = typeof options?.timeout === "number" && Number.isFinite(options.timeout) && @@ -810,12 +810,7 @@ export class SidecarKernelProxy { return { exitCode, ...(proc.exitSignal != null ? { exitSignal: proc.exitSignal } : {}), - stdout: Buffer.concat( - stdoutChunks.map((chunk) => Buffer.from(chunk)), - ).toString("utf8"), - stderr: Buffer.concat( - stderrChunks.map((chunk) => Buffer.from(chunk)), - ).toString("utf8"), + ...capture.finish(), }; }; const parsedCommandDriver = parsedCommand @@ -839,11 +834,11 @@ export class SidecarKernelProxy { stdin: undefined, cwd: effectiveCwd, onStdout: (chunk) => { - stdoutChunks.push(chunk); + capture.append("stdout", chunk); options?.onStdout?.(chunk); }, onStderr: (chunk) => { - stderrChunks.push(chunk); + capture.append("stderr", chunk); options?.onStderr?.(chunk); }, }), @@ -854,11 +849,11 @@ export class SidecarKernelProxy { stdin: undefined, cwd: effectiveCwd, onStdout: (chunk) => { - stdoutChunks.push(chunk); + capture.append("stdout", chunk); options?.onStdout?.(chunk); }, onStderr: (chunk) => { - stderrChunks.push(chunk); + capture.append("stderr", chunk); options?.onStderr?.(chunk); }, }); @@ -870,8 +865,7 @@ export class SidecarKernelProxy { args: readonly string[] = [], options?: KernelExecOptions, ): Promise { - const stdoutChunks: Uint8Array[] = []; - const stderrChunks: Uint8Array[] = []; + const capture = new HostTextOutputCapture(options?.maxCapturedOutputBytes); const deadlineMs = typeof options?.timeout === "number" && Number.isFinite(options.timeout) && @@ -898,12 +892,7 @@ export class SidecarKernelProxy { return { exitCode, ...(proc.exitSignal != null ? { exitSignal: proc.exitSignal } : {}), - stdout: Buffer.concat( - stdoutChunks.map((chunk) => Buffer.from(chunk)), - ).toString("utf8"), - stderr: Buffer.concat( - stderrChunks.map((chunk) => Buffer.from(chunk)), - ).toString("utf8"), + ...capture.finish(), }; }; @@ -918,11 +907,11 @@ export class SidecarKernelProxy { stdin: undefined, cwd: effectiveCwd, onStdout: (chunk) => { - stdoutChunks.push(chunk); + capture.append("stdout", chunk); options?.onStdout?.(chunk); }, onStderr: (chunk) => { - stderrChunks.push(chunk); + capture.append("stderr", chunk); options?.onStderr?.(chunk); }, }), diff --git a/packages/core/src/software-registry.ts b/packages/core/src/software-registry.ts index df849eb2bf..43fd6a468c 100644 --- a/packages/core/src/software-registry.ts +++ b/packages/core/src/software-registry.ts @@ -376,11 +376,7 @@ export async function resolveRegistrySoftware( ); } const url = new URL(artifact.path, manifestUrl); - if (url.protocol !== "https:" && url.protocol !== "http:") { - throw new Error( - `agentOS software manifest ${manifestUrl} lists ${packageName} at a non-http(s) URL: ${url.href}`, - ); - } + assertSecureRegistryUrl(url, "artifact URL"); return { name: packageName, version, diff --git a/packages/core/src/test-runtime.ts b/packages/core/src/test-runtime.ts index b3e7610331..f3da9aac28 100644 --- a/packages/core/src/test-runtime.ts +++ b/packages/core/src/test-runtime.ts @@ -291,6 +291,8 @@ export interface ExecOptions { onStdout?: (data: Uint8Array) => void; onStderr?: (data: Uint8Array) => void; captureStdio?: boolean; + /** Maximum combined host-captured stdout and stderr bytes. Defaults to 16 MiB. */ + maxCapturedOutputBytes?: number; filePath?: string; cpuTimeLimitMs?: number; timingMitigation?: TimingMitigation; diff --git a/packages/core/tests/code-execution-capture.test.ts b/packages/core/tests/code-execution-capture.test.ts new file mode 100644 index 0000000000..a11d197ed3 --- /dev/null +++ b/packages/core/tests/code-execution-capture.test.ts @@ -0,0 +1,58 @@ +import { expect, test, vi } from "vitest"; +import { + type CodeExecutionProcessOutput, + type CodeExecutionVm, + spawnCodeProcess, +} from "../src/code-execution.js"; + +test("code capture accepts its exact byte budget and decodes split UTF-8", async () => { + let output!: (event: CodeExecutionProcessOutput) => void; + const vm = { + spawn: () => ({ pid: 1 }), + onProcessOutput: (_pid, handler) => { + output = handler; + return () => {}; + }, + waitProcess: async () => 0, + } as CodeExecutionVm; + const child = spawnCodeProcess(vm, "node", [], { maxCapturedOutputBytes: 2 }); + output({ pid: 1, stream: "stdout", data: Uint8Array.of(0xc3) }); + output({ pid: 1, stream: "stdout", data: Uint8Array.of(0xa9) }); + await expect(child.wait()).resolves.toMatchObject({ + stdout: "é", + stderr: "", + }); +}); + +test("the combined code capture budget preserves streaming callbacks and reports a typed error", async () => { + let output!: (event: CodeExecutionProcessOutput) => void; + const unsubscribe = vi.fn(); + const onComplete = vi.fn(); + const vm = { + spawn: () => ({ pid: 1 }), + onProcessOutput: (_pid, handler) => { + output = handler; + return unsubscribe; + }, + waitProcess: async () => 0, + } as CodeExecutionVm; + const onStdout = vi.fn(); + const onStderr = vi.fn(); + const child = spawnCodeProcess(vm, "node", [], { + maxCapturedOutputBytes: 3, + onStdout, + onStderr, + onComplete, + }); + const encoder = new TextEncoder(); + output({ pid: 1, stream: "stdout", data: encoder.encode("ab") }); + output({ pid: 1, stream: "stderr", data: encoder.encode("cd") }); + await expect(child.wait()).rejects.toMatchObject({ + code: "ERR_AGENTOS_CAPTURED_OUTPUT_LIMIT", + limitBytes: 3, + }); + expect(onStdout).toHaveBeenCalledWith("ab"); + expect(onStderr).toHaveBeenCalledWith("cd"); + expect(unsubscribe).toHaveBeenCalledOnce(); + expect(onComplete).toHaveBeenCalledOnce(); +}); diff --git a/packages/core/tests/node-preload-resolution.native.test.ts b/packages/core/tests/node-preload-resolution.native.test.ts index c3b2eb836a..90a18c3830 100644 --- a/packages/core/tests/node-preload-resolution.native.test.ts +++ b/packages/core/tests/node-preload-resolution.native.test.ts @@ -47,3 +47,86 @@ test("NODE_OPTIONS runs a quoted CommonJS preload before the entry module", asyn "42\n", ); }, 15000); + +test("CommonJS file and preload preserve the builtin gate", async () => { + const vm = await AgentOs.create({ + defaultSoftware: false, + allowedNodeBuiltins: ["path"], + environment: { NODE_OPTIONS: "--require /tmp/gated-preload.cjs" }, + }); + try { + await vm.filesystem.writeFile( + "/tmp/gated-preload.cjs", + `const requireFrom = globalThis.__agentOsGuestCreateRequire(__filename); + let denied; + try { requireFrom("node:module"); } catch (error) { denied = error.code; } + globalThis.preloadResult = [ + denied, + Reflect.set(globalThis, "__agentOsGuestCreateRequire", () => () => null), + typeof globalThis._moduleModule + ];`, + ); + await vm.filesystem.writeFile( + "/tmp/gated-main.cjs", + `console.log(JSON.stringify([ + globalThis.preloadResult, + require("node:path").basename(__filename), + require.main === module, + process.mainModule === module + ]));`, + ); + const result = await vm.process.execFile("node", ["/tmp/gated-main.cjs"], { + timeoutMs: 3_000, + output: { capture: "all" }, + }); + expect(result.exitCode, result.stderr).toBe(0); + expect(JSON.parse(result.stdout)).toEqual([ + ["ERR_ACCESS_DENIED", false, "undefined"], + "gated-main.cjs", + true, + true, + ]); + } finally { + await vm.dispose(); + } +}, 10_000); + +test("CommonJS retained calls preserve the builtin gate and context", async () => { + const vm = await AgentOs.create({ + defaultSoftware: false, + allowedNodeBuiltins: ["path"], + }); + try { + await vm.createContext("gated-commonjs"); + for (const count of [1, 2]) { + const result = await vm.javascript.execute( + `{ + let denied; + try { require("node:module"); } catch (error) { denied = error.code; } + globalThis.gatedCount = (globalThis.gatedCount ?? 0) + 1; + console.log(JSON.stringify([ + denied, + typeof globalThis._moduleModule, + require("node:path").basename("/tmp/gated-main.cjs"), + globalThis.gatedCount + ])); + }`, + { + contextId: "gated-commonjs", + format: "commonjs", + timeoutMs: 2_000, + output: { capture: "all" }, + }, + ); + expect(result.outcome, JSON.stringify(result)).toBe("succeeded"); + expect(JSON.parse(result.stdout ?? "")).toEqual([ + "ERR_ACCESS_DENIED", + "undefined", + "gated-main.cjs", + count, + ]); + } + } finally { + await vm.dispose(); + } +}, 10_000); diff --git a/packages/core/tests/node-runtime-execution-options.test.ts b/packages/core/tests/node-runtime-execution-options.test.ts new file mode 100644 index 0000000000..6715f2c228 --- /dev/null +++ b/packages/core/tests/node-runtime-execution-options.test.ts @@ -0,0 +1,142 @@ +import { expect, test, vi } from "vitest"; +import { AgentOs } from "../src/agent-os.js"; +import { NodeRuntime } from "../src/node-runtime.js"; + +test("NodeRuntime forwards WASM backend selection on every execution path", async () => { + const wait = vi.fn().mockResolvedValue(0); + const kernel = { + exec: vi.fn().mockResolvedValue({ stdout: "", stderr: "", exitCode: 0 }), + spawn: vi.fn().mockReturnValue({ wait }), + writeFile: vi.fn().mockResolvedValue(undefined), + }; + const runtime = Reflect.construct(NodeRuntime, [kernel]) as NodeRuntime; + await runtime.exec("", { wasmBackend: "wasmtime" }); + await runtime.exec("", { + wasmBackend: "wasmtime-threads", + signal: new AbortController().signal, + }); + await runtime.spawn("", { wasmBackend: "v8" }); + expect(kernel.exec.mock.calls[0][1]).toMatchObject({ + wasmBackend: "wasmtime", + }); + expect(kernel.spawn.mock.calls.map((args) => args[2].wasmBackend)).toEqual([ + "wasmtime-threads", + "v8", + ]); +}); + +test("unsupported native per-run CPU overrides reject before dispatching guest work", async () => { + const kernel = { exec: vi.fn(), spawn: vi.fn(), writeFile: vi.fn() }; + const runtime = Reflect.construct(NodeRuntime, [kernel]) as NodeRuntime; + const options = { cpuTimeLimitMs: 10 }; + for (const result of [ + runtime.exec("", options), + runtime.spawn("", options), + runtime.run("", options), + runtime.execCommand("node", [], options), + ]) { + await expect(result).rejects.toMatchObject({ + code: "ERR_AGENTOS_UNSUPPORTED_EXECUTION_OPTION", + }); + } + expect(() => runtime.spawnCommand("node", [], options)).toThrow( + "cpuTimeLimitMs", + ); + expect(kernel.writeFile).not.toHaveBeenCalled(); + expect(kernel.exec).not.toHaveBeenCalled(); + expect(kernel.spawn).not.toHaveBeenCalled(); +}); + +test("deprecated AgentOs execution rejects unsupported CPU and timing overrides before dispatch", async () => { + const result = { stdout: "", stderr: "", exitCode: 0 }; + const kernel = { + exec: vi.fn().mockResolvedValue(result), + execArgv: vi.fn().mockResolvedValue(result), + }; + const sidecarClient = { + onEvent: vi.fn(), + onClose: vi.fn(), + }; + const vm = Reflect.construct(AgentOs, [ + kernel, + {}, + [], + [], + {}, + {}, + sidecarClient, + {}, + {}, + ]) as AgentOs; + + for (const options of [ + { cpuTimeLimitMs: 10 }, + { timingMitigation: "freeze" as const }, + ]) { + await expect(vm.exec("node", options)).rejects.toMatchObject({ + code: "ERR_AGENTOS_UNSUPPORTED_EXECUTION_OPTION", + }); + await expect(vm.execArgv("node", [], options)).rejects.toMatchObject({ + code: "ERR_AGENTOS_UNSUPPORTED_EXECUTION_OPTION", + }); + } + expect(kernel.exec).not.toHaveBeenCalled(); + expect(kernel.execArgv).not.toHaveBeenCalled(); + await expect(vm.exec("node")).resolves.toEqual(result); + await expect( + vm.execArgv("node", [], { + cpuTimeLimitMs: undefined, + timingMitigation: "off", + }), + ).resolves.toEqual(result); +}); + +test("resident output preserves split UTF-8 and rejects output beyond the request budget", async () => { + const encode = (text: string) => new TextEncoder().encode(text); + const exit = Promise.withResolvers(); + const kill = vi.fn(() => exit.resolve(0)); + let firstRequest = true; + const kernel = { + writeFile: async () => {}, + spawn: vi.fn((_command, _args, options) => { + const stdout: (chunk: Uint8Array) => void = options.onStdout; + queueMicrotask(() => stdout(encode("__AGENTOS_RESIDENT_READY__\n"))); + return { + kill, + wait: () => exit.promise, + writeStdin(line: string) { + const { id } = JSON.parse(line); + if (firstRequest) { + firstRequest = false; + stdout(Uint8Array.of(0xc3)); + stdout(Uint8Array.of(0xa9, 10)); + stdout( + encode( + `__AGENTOS_RESIDENT_RESULT__${JSON.stringify({ id, exitCode: 0 })}\n`, + ), + ); + } else { + stdout(encode("abc")); + } + }, + }; + }), + }; + const runtime = Reflect.construct(NodeRuntime, [kernel]) as NodeRuntime; + const runner = await runtime.createResidentRunner(); + try { + await expect(runner.exec("small output")).resolves.toMatchObject({ + stdout: "é\n", + exitCode: 0, + }); + await expect( + runner.exec("small budget", { maxCapturedOutputBytes: 2 }), + ).rejects.toMatchObject({ + code: "ERR_AGENTOS_CAPTURED_OUTPUT_LIMIT", + limitBytes: 2, + }); + expect(kill).toHaveBeenCalledWith(9); + } finally { + await runner.dispose(); + } +}); diff --git a/packages/core/tests/node-v8-native-wire.native.test.ts b/packages/core/tests/node-v8-native-wire.native.test.ts index dd015e14f3..49fbeb6a81 100644 --- a/packages/core/tests/node-v8-native-wire.native.test.ts +++ b/packages/core/tests/node-v8-native-wire.native.test.ts @@ -8,6 +8,7 @@ test("node:v8 preserves graphs and exchanges native serialization with Node.js", date: new Date(1234), map: new Map([["key", 3n]]), bytes: Buffer.from([7, 8]), + half: new Float16Array([1.5]), }).toString("base64"); const source = ` import {serialize,deserialize} from 'node:v8'; @@ -16,13 +17,13 @@ import {createRequire} from 'node:module'; const cjs=createRequire(import.meta.url)('node:v8'); const backing=Buffer.from('private-prefix:visible:private-suffix'); const visible=backing.subarray(15,22); -const child={value:3};const input={left:child,right:child,map:new Map([['key',3n]]),set:new Set([4]),date:new Date(1234),bytes:Buffer.from([7,8]),view:new Uint16Array([500,600]),regexp:/a+/gi,negative:-0,missing:undefined};input.self=input; +const child={value:3};const input={left:child,right:child,map:new Map([['key',3n]]),set:new Set([4]),date:new Date(1234),bytes:Buffer.from([7,8]),half:new Float16Array([1.5]),view:new Uint16Array([500,600]),regexp:/a+/gi,negative:-0,missing:undefined};input.self=input; const bytes=serialize(input), copy=deserialize(bytes); const host=deserialize(Buffer.from(${JSON.stringify(fixture)},'base64')); const foreign=deserialize(serialize(runInNewContext('({date:new Date(1234),view:new Uint16Array([500,600])})'))); const error=fn=>{try{fn();return null;}catch(e){return e.name;}}; console.log(JSON.stringify({ - summary:{sameExports:cjs.serialize===serialize&&cjs.deserialize===deserialize,viewPrivacy:!serialize(visible).includes(Buffer.from('private-')),cycle:copy.self===copy,alias:copy.left===copy.right,map:copy.map.get('key').toString(),set:copy.set.has(4),date:copy.date.getTime(),buffer:Buffer.isBuffer(copy.bytes),bytes:[...copy.bytes],view:[...copy.view],viewType:copy.view.constructor.name,regexp:copy.regexp.toString(),negative:Object.is(copy.negative,-0),missing:'missing' in copy&©.missing===undefined,host:[host.date.getTime(),host.map.get('key').toString(),Buffer.isBuffer(host.bytes),[...host.bytes]],foreign:[foreign.date.getTime(),[...foreign.view]],rejected:[error(()=>serialize(()=>0)),error(()=>deserialize(Buffer.from([1,2,3])))]}, + summary:{sameExports:cjs.serialize===serialize&&cjs.deserialize===deserialize,viewPrivacy:!serialize(visible).includes(Buffer.from('private-')),cycle:copy.self===copy,alias:copy.left===copy.right,map:copy.map.get('key').toString(),set:copy.set.has(4),date:copy.date.getTime(),buffer:Buffer.isBuffer(copy.bytes),bytes:[...copy.bytes],view:[...copy.view],viewType:copy.view.constructor.name,regexp:copy.regexp.toString(),negative:Object.is(copy.negative,-0),missing:'missing' in copy&©.missing===undefined,half:[host.half.constructor.name,host.half[0],copy.half[0]],host:[host.date.getTime(),host.map.get('key').toString(),Buffer.isBuffer(host.bytes),[...host.bytes]],foreign:[foreign.date.getTime(),[...foreign.view]],rejected:[error(()=>serialize(()=>0)),error(()=>deserialize(Buffer.from([1,2,3])))]}, bytes:bytes.toString('base64') }));`; const host = spawnSync( @@ -64,6 +65,8 @@ console.log(JSON.stringify({ expect(decoded.date.getTime()).toBe(1234); expect(Buffer.isBuffer(decoded.bytes)).toBe(true); expect(decoded.map.get("key")).toBe(3n); + expect(decoded.half).toBeInstanceOf(Float16Array); + expect(decoded.half[0]).toBe(1.5); } finally { try { await vm?.dispose(); diff --git a/packages/core/tests/software-registry.test.ts b/packages/core/tests/software-registry.test.ts index 296c09f202..74c07a8b5c 100644 --- a/packages/core/tests/software-registry.test.ts +++ b/packages/core/tests/software-registry.test.ts @@ -159,6 +159,14 @@ describe("resolveRegistrySoftware", () => { }); }); + it("checks transport security when resolving each artifact URL", async () => { + const release = manifest(["git"]); + release.artifacts[0].path = "http://registry.gameinc.io/git.aospkg"; + serve("latest", release); + await expect(resolveRegistrySoftware("git", { registry: { url: registry.url } })) + .rejects.toThrow("artifact URL must use https"); + }); + it("reads an explicit version, which wins over the registry default", async () => { serve("0.4.2", manifest(["git"])); serve("0.4.1", manifest(["git"])); diff --git a/scripts/ci-selection.test.mjs b/scripts/ci-selection.test.mjs index c39c4852fb..a43cb36316 100644 --- a/scripts/ci-selection.test.mjs +++ b/scripts/ci-selection.test.mjs @@ -159,7 +159,7 @@ test("CI builds code before one default-software producer and restores artifacts test("backend packaging restores the verified GNU license from the command producer", () => { const producer = step("name: wasm-command-licenses"); - assert.match(producer, /uses: actions\/upload-artifact@v4/); + assert.match(producer, /uses: actions\/upload-artifact@[0-9a-f]{40}\s+# v4\./); assert.match(producer, /path: toolchain\/c\/build\/awk\.COPYING/); assert.match(producer, /if-no-files-found: error/); const backend = job("wasm-backend-matrix"); @@ -168,7 +168,7 @@ test("backend packaging restores the verified GNU license from the command produ source.includes("name: wasm-command-licenses"), ); assert.ok(restoreIndex >= 0, "backend downloads the license artifact"); - assert.match(steps[restoreIndex], /uses: actions\/download-artifact@v4/); + assert.match(steps[restoreIndex], /uses: actions\/download-artifact@[0-9a-f]{40}\s+# v4\./); assert.match(steps[restoreIndex], /path: toolchain\/c\/build\s*$/); const packageIndex = steps.findIndex((source) => source.includes("pnpm --filter '@agentos-software/*' build"), @@ -194,17 +194,17 @@ test("publish restores the GNU license before building software packages", () => .split(/\n - /) .find((source) => source.includes("name: wasm-command-licenses")); assert.ok(producer, "publish uploads the license artifact"); - assert.match(producer, /uses: actions\/upload-artifact@v4/); + assert.match(producer, /uses: actions\/upload-artifact@[0-9a-f]{40}\s+# v4\./); assert.match(producer, /path: toolchain\/c\/build\/awk\.COPYING/); assert.match(producer, /if-no-files-found: error/); - const consumer = job("publish-npm", publish) + const consumer = job("prepare-npm", publish) .split(/\n - /) .slice(1); const restoreIndex = consumer.findIndex((source) => source.includes("name: wasm-command-licenses"), ); assert.ok(restoreIndex >= 0, "publish downloads the license artifact"); - assert.match(consumer[restoreIndex], /uses: actions\/download-artifact@v4/); + assert.match(consumer[restoreIndex], /uses: actions\/download-artifact@[0-9a-f]{40}\s+# v4\./); assert.match(consumer[restoreIndex], /path: toolchain\/c\/build\s*$/); const buildIndex = consumer.findIndex((source) => source.includes("npx turbo build"), diff --git a/scripts/pipboy/src/footprint/assets.ts b/scripts/pipboy/src/footprint/assets.ts index b52205e7bf..083da32b12 100644 --- a/scripts/pipboy/src/footprint/assets.ts +++ b/scripts/pipboy/src/footprint/assets.ts @@ -1,4 +1,5 @@ -import { dirname, join } from "node:path"; +import { lstat, mkdir, writeFile } from "node:fs/promises"; +import { basename, join, resolve } from "node:path"; import { Effect, FileSystem, Schema } from "effect"; import { ASSET_SEEDS } from "../config.ts"; import { npmEnv, run } from "../exec.ts"; @@ -6,6 +7,36 @@ import { readTarball } from "./tarball.ts"; const PackOutput = Schema.fromJsonString(Schema.Tuple([Schema.Struct({ filename: Schema.String })])); +function archivePathParts(path: string): string[] { + const parts = path.split("/"); + if (path.includes("\\") || parts.some((part) => !part || part === "." || part === "..")) { + throw new Error("Invalid package archive path"); + } + return parts; +} + +export async function writeSeedAsset(root: string, target: string, data: Uint8Array): Promise { + const parts = archivePathParts(target); + let path = resolve(root); + for (const part of parts.slice(0, -1)) { + path = join(path, part); + await mkdir(path).catch((error: NodeJS.ErrnoException) => { + if (error.code !== "EEXIST") throw error; + }); + if (!(await lstat(path)).isDirectory()) { + throw new Error(`Package asset directory is not a real directory: ${path}`); + } + } + + try { + await writeFile(join(path, basename(target)), data, { flag: "wx" }); + return true; + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error; + return false; + } +} + /** * Copies the assets in `ASSET_SEEDS` from the latest published tarballs into * the checkout at `root`, where the build expects them. Files that already @@ -16,17 +47,16 @@ export const seedAssets = Effect.fn("Footprint.seedAssets")(function* (root: str const dir = yield* fs.makeTempDirectoryScoped({ prefix: "pipboy-assets-" }); let seeded = 0; for (const seed of ASSET_SEEDS) { - const output = yield* run("npm", ["pack", `${seed.package}@latest`, "--pack-destination", dir, "--json"], { env: npmEnv() }); + const output = yield* run("npm", ["pack", "--ignore-scripts", `${seed.package}@latest`, "--pack-destination", dir, "--json"], { env: npmEnv() }); const [{ filename }] = yield* Schema.decodeUnknownEffect(PackOutput)(output); + if (archivePathParts(filename).length !== 1) throw new Error("Invalid npm pack filename"); for (const entry of readTarball(yield* fs.readFile(join(dir, filename)))) { if (!entry.path.startsWith(seed.from)) continue; - const target = seed.to(entry.path.slice(seed.from.length)); + const archivePath = entry.path.slice(seed.from.length); + archivePathParts(archivePath); + const target = seed.to(archivePath); if (target === undefined) continue; - const path = join(root, target); - if (yield* fs.exists(path)) continue; - yield* fs.makeDirectory(dirname(path), { recursive: true }); - yield* fs.writeFile(path, entry.data); - seeded++; + if (yield* Effect.promise(() => writeSeedAsset(root, target, entry.data))) seeded++; } } return seeded; diff --git a/scripts/pipboy/tests/assets.test.ts b/scripts/pipboy/tests/assets.test.ts new file mode 100644 index 0000000000..475721c3a8 --- /dev/null +++ b/scripts/pipboy/tests/assets.test.ts @@ -0,0 +1,29 @@ +import { mkdir, mkdtemp, readFile, rm, symlink } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { expect, test } from "vitest"; +import { writeSeedAsset } from "../src/footprint/assets.ts"; + +test("seeds new assets and preserves existing files", async () => { + const root = await mkdtemp(join(tmpdir(), "pipboy-seeds-")); + try { + expect(await writeSeedAsset(root, "commands/sh", Buffer.from("original"))).toBe(true); + expect(await writeSeedAsset(root, "commands/sh", Buffer.from("replacement"))).toBe(false); + expect(await readFile(join(root, "commands/sh"), "utf8")).toBe("original"); + } finally { + await rm(root, { recursive: true, force: true }); + } +}); + +test("rejects ambiguous paths and directory links during asset seeding", async () => { + const root = await mkdtemp(join(tmpdir(), "pipboy-seeds-")); + try { + await mkdir(join(root, "cache")); + await symlink(join(root, "cache"), join(root, "commands")); + await expect(writeSeedAsset(root, "commands/sh", Buffer.from("content"))).rejects.toThrow("not a real directory"); + await expect(writeSeedAsset(root, "cache/./sh", Buffer.from("content"))).rejects.toThrow("Invalid package archive path"); + await expect(readFile(join(root, "cache/sh"))).rejects.toMatchObject({ code: "ENOENT" }); + } finally { + await rm(root, { recursive: true, force: true }); + } +}); diff --git a/scripts/publish/secure-exec.md b/scripts/publish/secure-exec.md index 2c0f393336..4e8f20b97c 100644 --- a/scripts/publish/secure-exec.md +++ b/scripts/publish/secure-exec.md @@ -16,7 +16,8 @@ publisher at : - Allowed actions: enable direct `npm publish` The existing `publish-npm` job uses a GitHub-hosted runner, Node.js 24, and -`id-token: write`. npm automatically exchanges the job's OIDC token; no +`id-token: write`. npm automatically exchanges the job's OIDC token. The preparation job builds and packs the packages before the publish job +validates and publishes the tarballs with lifecycle scripts disabled. No `NPM_TOKEN` or interactive npm login is needed in CI. Trusted publisher settings live on npm and cannot be enabled by committing a workflow alone. diff --git a/scripts/publish/src/ci/bin.ts b/scripts/publish/src/ci/bin.ts index 8a92152fec..ab066f9b09 100644 --- a/scripts/publish/src/ci/bin.ts +++ b/scripts/publish/src/ci/bin.ts @@ -21,16 +21,22 @@ import { releaseUserAgent, } from "../lib/artifacts.js"; import { + canPromoteLatest, resolveContext, + validatePublishSha, + validatePublishVersion, writeContextToGithubOutput, type Trigger, } from "../lib/context.js"; import { createGhRelease, tagAndPush } from "../lib/git.js"; import { scoped } from "../lib/logger.js"; -import { publishAll } from "../lib/npm.js"; +import { publishPackages } from "../lib/npm.js"; +import { preparePackedNpm } from "../lib/packed-npm.ts"; +import { assertDiscoverySanity, discoverPackages } from "../lib/packages.js"; import { copyPrefix, uploadDir } from "../lib/r2.js"; import { stageSoftwareArtifacts } from "../lib/software-artifacts.js"; import { + assertPublishableV8Source, discoverRustCrates, STANDALONE_RUST_CRATE_MANIFESTS, } from "../lib/rust-crates.js"; @@ -134,6 +140,7 @@ program if (opts.latest !== undefined) overrides.latest = opts.latest === "true"; if (opts.branch) overrides.branch = opts.branch; const ctx = await resolveContext(overrides); + if (ctx.trigger === "release") assertPublishableV8Source(ctx.repoRoot); log.info( `resolved: trigger=${ctx.trigger} version=${ctx.version} npm_tag=${ctx.npmTag} sha=${ctx.sha} latest=${ctx.latest}${ctx.branch !== undefined ? ` branch=${ctx.branch}` : ""}`, ); @@ -159,7 +166,7 @@ program .action(async (opts) => { const repoRoot = findRepoRoot(); const ctx = await resolveContext(); - const version = opts.version ?? ctx.version; + const version = validatePublishVersion(opts.version ?? ctx.version); await bumpPackageJsons(repoRoot, version, { dryRun: !!opts.dryRun, versionOnly: !!opts.versionOnly, @@ -190,7 +197,9 @@ program releaseMode = ctx.trigger === "release"; } } - await publishAll(repoRoot, { + const packages = discoverPackages(repoRoot); + assertDiscoverySanity(packages); + await publishPackages(packages, { tag, parallel: Number(opts.parallel), retries: Number(opts.retries), @@ -199,6 +208,18 @@ program }); }); +program + .command("prepare-packed-npm") + .description("Pack publishable npm packages for the credential-scoped publish job") + .requiredOption("--version ", "Expected packed version") + .requiredOption("--output ", "New artifact directory") + .action(async (opts) => { + const root = findRepoRoot(); + const packages = discoverPackages(root); + assertDiscoverySanity(packages); + await preparePackedNpm(packages, validatePublishVersion(opts.version), opts.output); + }); + // --------------------------------------------------------------------------- // publish-crates — ordered, idempotent crates.io publish // --------------------------------------------------------------------------- @@ -211,7 +232,7 @@ program .option("--allow-dirty", "Pass --allow-dirty to cargo publish") .action(async (opts) => { const repoRoot = findRepoRoot(); - const version = opts.version ?? (await resolveContext()).version; + const version = validatePublishVersion(opts.version ?? (await resolveContext()).version); const crates = discoverRustCrates(repoRoot); const waitSeconds = Number(opts.waitSeconds); if (!Number.isFinite(waitSeconds) || waitSeconds <= 0) { @@ -238,6 +259,7 @@ program return; } + assertPublishableV8Source(repoRoot); if (!process.env.CARGO_REGISTRY_TOKEN) { throw new Error("CARGO_REGISTRY_TOKEN must be set to publish crates"); } @@ -282,10 +304,10 @@ program .command("upload-r2") .description("Upload an artifact directory to R2") .requiredOption("--source ", "Local directory to upload") - .option("--sha ", "Short sha (defaults to resolved context)") + .option("--sha ", "Full source commit SHA (defaults to resolved context)") .option("--name ", "R2 sub-path name", "sidecar") .action(async (opts) => { - const sha = opts.sha ?? (await resolveContext()).sha; + const sha = validatePublishSha(opts.sha ?? (await resolveContext()).sha); const prefix = releaseArtifactPrefix({ ref: sha, name: opts.name }); await uploadDir(opts.source, prefix); }); @@ -296,15 +318,16 @@ program program .command("copy-r2") .description("Copy R2 artifacts from {sha} to {version} (+latest)") - .option("--sha ", "Source sha (defaults to resolved context)") + .option("--sha ", "Full source commit SHA (defaults to resolved context)") .option("--version ", "Target version (defaults to resolved context)") .option("--latest ", "Also copy to /latest/ (defaults to resolved context)") .option("--name ", "R2 sub-path name", "sidecar") .action(async (opts) => { const ctx = await resolveContext(); - const sha: string = opts.sha ?? ctx.sha; - const version: string = opts.version ?? ctx.version; - const latest = opts.latest !== undefined ? opts.latest === "true" : ctx.latest; + const sha = validatePublishSha(opts.sha ?? ctx.sha); + const version = validatePublishVersion(opts.version ?? ctx.version); + const latest = canPromoteLatest(version) && + (opts.latest !== undefined ? opts.latest === "true" : ctx.latest); const source = releaseArtifactPrefix({ ref: sha, name: opts.name }); await copyPrefix( source, @@ -326,7 +349,7 @@ program .description("Create and force-push v{version} tag") .option("--version ", "Version (defaults to resolved context)") .action(async (opts) => { - const version = opts.version ?? (await resolveContext()).version; + const version = validatePublishVersion(opts.version ?? (await resolveContext()).version); await tagAndPush(version); }); @@ -338,7 +361,7 @@ program .description("Create or update GitHub release") .option("--version ", "Version (defaults to resolved context)") .action(async (opts) => { - const version = opts.version ?? (await resolveContext()).version; + const version = validatePublishVersion(opts.version ?? (await resolveContext()).version); await createGhRelease(version); }); diff --git a/scripts/publish/src/ci/publish-packed-npm.ts b/scripts/publish/src/ci/publish-packed-npm.ts new file mode 100644 index 0000000000..1b709c03cf --- /dev/null +++ b/scripts/publish/src/ci/publish-packed-npm.ts @@ -0,0 +1,21 @@ +import { publishPackages } from "../lib/npm.ts"; +import { readPackedNpm } from "../lib/packed-npm.ts"; + +const [directory] = process.argv.slice(2); +const { VERSION, NPM_TAG, TRIGGER, SIDECAR_PLATFORMS } = process.env; +if (!directory || !VERSION || !NPM_TAG || !SIDECAR_PLATFORMS || + !["branch", "release"].includes(TRIGGER ?? "")) { + throw new Error("Packed npm publish requires a directory and resolved publish context"); +} +if (TRIGGER === "release" ? !["latest", "next", "rc"].includes(NPM_TAG) + : !/^preview-[a-z0-9-]+-[a-f0-9]{16}$/.test(NPM_TAG)) { + throw new Error("Invalid npm tag for publish trigger"); +} + +const platforms = [...new Set(SIDECAR_PLATFORMS.trim().split(/\s+/))]; +const packages = await readPackedNpm(directory, VERSION, platforms); +await publishPackages(packages, { + tag: NPM_TAG, + releaseMode: TRIGGER === "release", + platformCount: platforms.length, +}); diff --git a/scripts/publish/src/lib/artifacts.test.ts b/scripts/publish/src/lib/artifacts.test.ts index 6a03688199..7e0e78cbb4 100644 --- a/scripts/publish/src/lib/artifacts.test.ts +++ b/scripts/publish/src/lib/artifacts.test.ts @@ -24,3 +24,9 @@ test("rejects invalid release artifact namespaces", () => { /invalid RELEASE_ARTIFACT_NAMESPACE/, ); }); + +test("artifact coordinates each occupy exactly one key segment", () => { + assert.equal(releaseArtifactPrefix({ ref: "1.2.3+build", name: "sidecar" }, {}), "agent-os/1.2.3+build/sidecar/"); + assert.throws(() => releaseArtifactPrefix({ ref: "", name: "sidecar" }, {}), /artifact ref/); + assert.throws(() => releaseArtifactPrefix({ ref: "1.2.3", name: "sidecar/linux" }, {}), /artifact name/); +}); diff --git a/scripts/publish/src/lib/artifacts.ts b/scripts/publish/src/lib/artifacts.ts index fced734b5e..e3fa70001e 100644 --- a/scripts/publish/src/lib/artifacts.ts +++ b/scripts/publish/src/lib/artifacts.ts @@ -16,7 +16,12 @@ export function releaseArtifactNamespace(env: ArtifactEnv = process.env): string export function releaseArtifactPrefix( artifact: ReleaseArtifactRef, env: ArtifactEnv = process.env, -): string { +): string { + for (const field of ["ref", "name"] as const) { + if (!/^[A-Za-z0-9][A-Za-z0-9._+-]*$/.test(artifact[field])) { + throw new Error(`Invalid release artifact ${field}`); + } + } return `${releaseArtifactNamespace(env)}/${artifact.ref}/${artifact.name}/`; } diff --git a/scripts/publish/src/lib/context.test.ts b/scripts/publish/src/lib/context.test.ts new file mode 100644 index 0000000000..5779b71ef9 --- /dev/null +++ b/scripts/publish/src/lib/context.test.ts @@ -0,0 +1,47 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import semver from "semver"; +import { resolveContext } from "./context.js"; + +const sha = "0123456789abcdef0123456789abcdef01234567"; + +test("explicit release versions require canonical SemVer", async () => { + for (const version of ["1.2", "v1.2.3", "1.2.3 ", "01.2.3"]) { + await assert.rejects(resolveContext({ trigger: "release", version, sha }), TypeError); + } + for (const [version, latest, tag, promoted] of [ + ["1.2.3", true, "latest", true], + ["1.2.3+build", false, "next", false], + ["1.2.3-rc.0", true, "rc", false], + ["1.2.3-beta.0", true, "next", false], + ] as const) { + const context = await resolveContext({ trigger: "release", version, latest, sha }); + assert.equal(context.version, version); + assert.equal(context.npmTag, tag); + assert.equal(context.latest, promoted); + assert.equal(context.sha, sha); + } +}); + +test("preview tags reserve release names and distinguish normalized branches", async () => { + const names = ["latest", "NEXT", "rc", "feature/x", "feature-x", "123", "名前"]; + const contexts = await Promise.all(names.map((branch) => + resolveContext({ trigger: "branch", branch, sha }), + )); + assert.equal(new Set(contexts.map((ctx) => ctx.npmTag)).size, names.length); + for (const context of contexts) { + assert.match(context.npmTag, /^preview-/); + assert.equal(semver.valid(context.version), context.version); + } + const next = await resolveContext({ trigger: "branch", branch: names[0], sha: `${sha.slice(0, -1)}8` }); + assert.equal(next.npmTag, contexts[0].npmTag); + assert.notEqual(next.version, contexts[0].version); +}); + + +test("artifact staging refuses abbreviated commit IDs", async () => { + await assert.rejects( + resolveContext({ trigger: "release", version: "1.2.3", sha: sha.slice(0, 7) }), + /full hexadecimal commit SHA/, + ); +}); diff --git a/scripts/publish/src/lib/context.ts b/scripts/publish/src/lib/context.ts index 28792057b8..d0b8d2791f 100644 --- a/scripts/publish/src/lib/context.ts +++ b/scripts/publish/src/lib/context.ts @@ -1,7 +1,9 @@ +import { createHash } from "node:crypto"; import { appendFileSync, existsSync, readFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { $ } from "execa"; +import semver from "semver"; /** * Publish context. Resolved once per workflow run by the `context-output` CI @@ -16,7 +18,7 @@ export interface PublishContext { version: string; /** npm dist-tag. */ npmTag: string; - /** Short commit sha (7 chars). */ + /** Full commit object ID. */ sha: string; /** Only meaningful when trigger === "release". */ latest: boolean; @@ -48,7 +50,7 @@ function findRepoRoot(): string { /** * Base for all preview pre-release strings. Hardcoded to `0.0.0` so preview - * versions like `0.0.0-my-branch.abc1234` never look like real releases and + * versions like `0.0.0-preview-my-branch-0123456789abcdef.g` never look like real releases and * always sort below any published `X.Y.Z`. Using a committed `package.json` * version as the base would just embed whatever stale number happened to be * committed there. It has no semantic relationship to the branch being @@ -56,11 +58,11 @@ function findRepoRoot(): string { */ const PREVIEW_BASE_VERSION = "0.0.0"; -async function readShortSha(repoRoot: string): Promise { +async function readCommitSha(repoRoot: string): Promise { const envSha = process.env.GITHUB_SHA; - if (envSha) return envSha.slice(0, 7); + if (envSha) return envSha; const { stdout } = await $({ cwd: repoRoot })`git rev-parse HEAD`; - return stdout.trim().slice(0, 7); + return stdout.trim(); } async function readBranchName(repoRoot: string): Promise { @@ -73,21 +75,18 @@ async function readBranchName(repoRoot: string): Promise { } /** - * Sanitize a branch name into something safe to use as both an npm dist-tag - * and a semver prerelease identifier. Lowercases, replaces any - * non-alphanumeric character (other than hyphens) with a hyphen, collapses - * runs of hyphens, and trims leading/trailing hyphens. + * Keep preview tags outside the release namespace and distinguish branches + * that normalize to the same readable slug. */ -function sanitizeBranch(branch: string): string { +function previewBranchIdentifier(branch: string): string { const cleaned = branch .toLowerCase() .replace(/[^a-z0-9-]+/g, "-") .replace(/-+/g, "-") - .replace(/^-+|-+$/g, ""); - if (cleaned.length === 0) { - throw new Error(`branch name "${branch}" sanitized to empty string`); - } - return cleaned; + .replace(/^-+|-+$/g, "") + .slice(0, 64); + const digest = createHash("sha256").update(branch).digest("hex").slice(0, 16); + return `preview-${cleaned || "branch"}-${digest}`; } function readInputFromEvent(name: string): T | undefined { @@ -136,10 +135,10 @@ function computeNpmTag( if (!branch) { throw new Error("branch trigger requires branch to compute npm tag"); } - return sanitizeBranch(branch); + return previewBranchIdentifier(branch); } // release - if (version.includes("-rc.")) return "rc"; + if (semver.prerelease(version)?.[0] === "rc") return "rc"; return latest ? "latest" : "next"; } @@ -155,11 +154,30 @@ function computeVersion( if (!branch) { throw new Error("branch trigger requires branch to compute version"); } - return `${base}-${sanitizeBranch(branch)}.${sha}`; + return `${base}-${previewBranchIdentifier(branch)}.g${sha}`; } throw new Error("release trigger requires an explicit version override"); } +/** Validate a canonical version before publishing or writing version metadata. */ +export function validatePublishVersion(version: string): string { + if (semver.valid(version) === null || !/^[0-9][0-9A-Za-z.+-]*$/.test(version)) { + throw new TypeError("Publish version must be a valid SemVer string"); + } + return version; +} + +export function validatePublishSha(sha: string): string { + if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/i.test(sha)) { + throw new TypeError("Publish artifact staging requires a full hexadecimal commit SHA"); + } + return sha.toLowerCase(); +} + +export function canPromoteLatest(version: string): boolean { + return semver.prerelease(version) === null; +} + /** * Resolve the publish context. Pure function of environment + overrides. * Not memoized: each subcommand process re-reads env, and the `context-output` @@ -172,7 +190,7 @@ export async function resolveContext( const repoRoot = findRepoRoot(); const trigger = deriveTrigger(overrides); - const sha = overrides.sha ?? (await readShortSha(repoRoot)); + const sha = validatePublishSha(overrides.sha ?? (await readCommitSha(repoRoot))); let branch = overrides.branch; if (trigger === "branch" && !branch) { @@ -200,13 +218,15 @@ export async function resolveContext( ); } + validatePublishVersion(version); + // Latest: override > workflow_dispatch input > false. let latest = overrides.latest; if (latest === undefined) { const input = readInputFromEvent("latest"); latest = parseBoolInput(input, false); } - if (trigger !== "release") latest = false; + if (trigger !== "release" || !canPromoteLatest(version)) latest = false; const npmTag = computeNpmTag(trigger, version, latest, branch); @@ -231,7 +251,6 @@ export function writeContextToGithubOutput(ctx: PublishContext): void { console.log(`npm_tag=${ctx.npmTag}`); console.log(`sha=${ctx.sha}`); console.log(`latest=${ctx.latest}`); - if (ctx.branch !== undefined) console.log(`branch=${ctx.branch}`); return; } const lines = [ @@ -241,7 +260,6 @@ export function writeContextToGithubOutput(ctx: PublishContext): void { `sha=${ctx.sha}`, `latest=${ctx.latest}`, ]; - if (ctx.branch !== undefined) lines.push(`branch=${ctx.branch}`); // Append (do not overwrite) in case other steps also wrote to GITHUB_OUTPUT. appendFileSync(path, `${lines.join("\n")}\n`); } diff --git a/scripts/publish/src/lib/npm.ts b/scripts/publish/src/lib/npm.ts index 4d05a29601..32ed0ce59a 100644 --- a/scripts/publish/src/lib/npm.ts +++ b/scripts/publish/src/lib/npm.ts @@ -12,20 +12,20 @@ * runs, so plain `npm publish` resolves them correctly. */ import { spawn } from "node:child_process"; -import { scoped } from "./logger.js"; -import { - assertDiscoverySanity, - discoverPackages, - type Package, -} from "./packages.js"; +import { scoped } from "./logger.ts"; +import type { Package } from "./packages.ts"; + +export type NpmPackage = Package & { tarball?: string }; const log = scoped("npm"); export interface PublishAllOptions { - /** npm dist-tag (e.g. my-branch, rc, next, latest). */ + /** npm dist-tag (e.g. a preview tag, rc, next, latest). */ tag: string; /** Max simultaneous publishes. */ parallel?: number; + /** Publish this many platform packages before starting dependent packages. */ + platformCount?: number; /** Max retries per package. */ retries?: number; /** Initial backoff in ms (doubled per retry). */ @@ -47,7 +47,7 @@ export type PublishStatus = | "failed"; export interface PublishResult { - pkg: Package; + pkg: NpmPackage; status: PublishStatus; attempts: number; lastError?: string; @@ -105,13 +105,16 @@ function extractError(output: string, maxLines = 3): string { } function runNpmPublish( - pkg: Package, + pkg: NpmPackage, tag: string, dryRun: boolean, ): Promise<{ code: number; output: string }> { return new Promise((resolvePromise) => { const args = ["publish", "--access", "public", "--tag", tag]; if (dryRun) args.push("--dry-run"); + if (pkg.tarball) { + args.push("--ignore-scripts", "--registry", "https://registry.npmjs.org", "--", pkg.tarball); + } const child = spawn("npm", args, { cwd: pkg.dir, stdio: ["ignore", "pipe", "pipe"], @@ -120,6 +123,9 @@ function runNpmPublish( const chunks: Buffer[] = []; child.stdout.on("data", (c) => chunks.push(c)); child.stderr.on("data", (c) => chunks.push(c)); + child.once("error", (error) => { + resolvePromise({ code: 1, output: error.message }); + }); child.on("close", (code) => { resolvePromise({ code: code ?? 1, @@ -130,7 +136,7 @@ function runNpmPublish( } async function publishOne( - pkg: Package, + pkg: NpmPackage, opts: Required< Pick >, @@ -179,8 +185,8 @@ function printResult(r: PublishResult): void { log.info(` ${symbol} ${name}${suffix}`); } -export async function publishAll( - repoRoot: string, +export async function publishPackages( + packages: readonly NpmPackage[], opts: PublishAllOptions, ): Promise { const parallel = opts.parallel ?? 16; @@ -189,14 +195,11 @@ export async function publishAll( const tag = opts.tag; const dryRun = opts.dryRun ?? false; - const packages = discoverPackages(repoRoot); - assertDiscoverySanity(packages); - log.info( `publishing ${packages.length} packages | tag=${tag} | parallel=${parallel} | retries=${retries}${dryRun ? " | DRY RUN" : ""}`, ); - const queue = [...packages]; + const queue: NpmPackage[] = []; const results: PublishResult[] = []; const startedAt = Date.now(); @@ -215,11 +218,16 @@ export async function publishAll( } } - const workers: Promise[] = []; - for (let i = 0; i < Math.min(parallel, packages.length); i++) { - workers.push(worker()); + async function publishBatch(batch: readonly NpmPackage[]): Promise { + queue.push(...batch); + await Promise.all(Array.from({ length: Math.min(parallel, batch.length) }, worker)); + } + + const platformCount = opts.platformCount ?? 0; + await publishBatch(packages.slice(0, platformCount)); + if (!results.some((result) => result.status === "failed")) { + await publishBatch(packages.slice(platformCount)); } - await Promise.all(workers); const elapsed = (Date.now() - startedAt) / 1000; const counts = { diff --git a/scripts/publish/src/lib/packages.test.ts b/scripts/publish/src/lib/packages.test.ts index 09bfd1cb1e..d3d1f0c9c8 100644 --- a/scripts/publish/src/lib/packages.test.ts +++ b/scripts/publish/src/lib/packages.test.ts @@ -95,3 +95,19 @@ test("archived browser packages are outside publication discovery", () => { assert(!names.includes("@rivet-dev/agentos-runtime-browser")); assert(!names.includes("@rivet-dev/agentos-playground")); }); + +test("the platform override governs workspace discovery and the meta package", () => { + const old = process.env.SIDECAR_PLATFORMS; + try { + process.env.SIDECAR_PLATFORMS = "linux-x64-gnu"; + const packages = discoverPackages(repoRoot); + assert.deepEqual(buildMetaPlatformMap(packages).get("@rivet-dev/agentos-sidecar"), [ + "@rivet-dev/agentos-sidecar-linux-x64-gnu", + ]); + process.env.SIDECAR_PLATFORMS = "linux-x64-gnu unknown-architecture"; + assert.throws(() => discoverPackages(repoRoot), /Unknown or unavailable SIDECAR_PLATFORMS/); + } finally { + if (old === undefined) delete process.env.SIDECAR_PLATFORMS; + else process.env.SIDECAR_PLATFORMS = old; + } +}); diff --git a/scripts/publish/src/lib/packages.ts b/scripts/publish/src/lib/packages.ts index b8f09723c6..62096fce3d 100644 --- a/scripts/publish/src/lib/packages.ts +++ b/scripts/publish/src/lib/packages.ts @@ -9,7 +9,7 @@ * before anyone installs the meta. */ import { execSync } from "node:child_process"; -import { existsSync, readFileSync, readdirSync, statSync } from "node:fs"; +import { existsSync, readFileSync } from "node:fs"; import { join, relative, resolve } from "node:path"; export interface Package { @@ -126,16 +126,21 @@ export function discoverPackages( // the meta package resolves at install time. Only the allowlisted // platforms are included so unbuilt platform dirs are never published. const platformAllowlist = new Set(sidecarPlatforms()); - for (const packageDir of SIDECAR_BINARY_PACKAGE_DIRS) { - const npmDir = join(repoRoot, packageDir); - if (existsSync(npmDir)) { - for (const entry of readdirSync(npmDir).sort()) { - if (!platformAllowlist.has(entry)) continue; - const platDir = join(npmDir, entry); - if (!statSync(platDir).isDirectory()) continue; - add(platDir); - } + for (const platform of [...platformAllowlist].sort()) { + if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(platform)) { + throw new Error(`Invalid SIDECAR_PLATFORMS entry: ${platform}`); } + let found = false; + for (const packageDir of SIDECAR_BINARY_PACKAGE_DIRS) { + const dir = join(repoRoot, packageDir, platform); + const pkg = readPackageJson(dir); + if (!pkg || !isPublishable(pkg) || !META_PACKAGES.some( + ({ platformPrefix }) => pkg.name === `${platformPrefix}${platform}`, + )) continue; + add(dir); + found = true; + } + if (!found) throw new Error(`Unknown or unavailable SIDECAR_PLATFORMS entry: ${platform}`); } // 2. pnpm workspace packages. Registry software is packed into .aospkg @@ -152,6 +157,9 @@ export function discoverPackages( }> = JSON.parse(pnpmList); for (const p of workspacePkgs) { if (!p.name) continue; + const relDir = relative(repoRoot, resolve(p.path)); + if (SIDECAR_BINARY_PACKAGE_DIRS.some((dir) => relDir.startsWith(`${dir}/`))) continue; + if (META_PACKAGES.some(({ platformPrefix }) => p.name.startsWith(platformPrefix))) continue; if ( !p.name.startsWith("@rivet-dev/agentos-") && p.name !== "@rivet-dev/agentos" && diff --git a/scripts/publish/src/lib/packed-npm.test.ts b/scripts/publish/src/lib/packed-npm.test.ts new file mode 100644 index 0000000000..87285c7e8a --- /dev/null +++ b/scripts/publish/src/lib/packed-npm.test.ts @@ -0,0 +1,74 @@ +import assert from "node:assert/strict"; +import { execFile } from "node:child_process"; +import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import test from "node:test"; +import { promisify } from "node:util"; +import { discoverPackages, sidecarPlatforms } from "./packages.js"; +import { preparePackedNpm, readPackedNpm } from "./packed-npm.js"; + +test("packs the discovered release set and verifies identity and digest before publish", async () => { + const dir = await mkdtemp(join(tmpdir(), "packed-npm-")); + const version = "1.2.3-rc.1"; + const platforms = sidecarPlatforms(); + const output = join(dir, "out"); + try { + const packages = discoverPackages(resolve(import.meta.dirname, "../../../..")); + const fixtures = await Promise.all(packages.map(async (pkg, index) => { + const path = join(dir, String(index)); + await mkdir(path); + await writeFile(join(path, "package.json"), JSON.stringify({ name: pkg.name, version })); + return { ...pkg, dir: path }; + })); + await preparePackedNpm(fixtures, version, output); + const packed = await readPackedNpm(output, version, platforms); + assert.deepEqual(packed.map(({ name }) => name), packages.map(({ name }) => name)); + + const fakeBin = join(dir, "bin"); + const calls = join(dir, "calls"); + await mkdir(fakeBin); + await writeFile(join(fakeBin, "npm"), String.raw`#!/usr/bin/env node +const fs = require("node:fs"); +const args = process.argv.slice(2); +if (args[0] !== "publish" || !args.includes("--ignore-scripts") || + args[args.indexOf("--tag") + 1] !== "rc" || + args[args.indexOf("--registry") + 1] !== "https://registry.npmjs.org") process.exit(1); +fs.appendFileSync(process.env.PUBLISH_TEST_CALLS, args.at(-1) + "\n"); +`, { mode: 0o700 }); + await promisify(execFile)(process.execPath, [ + resolve(import.meta.dirname, "../ci/publish-packed-npm.ts"), + output, + ], { + env: { + ...process.env, + PATH: `${fakeBin}:${process.env.PATH}`, + PUBLISH_TEST_CALLS: calls, + VERSION: version, + NPM_TAG: "rc", + TRIGGER: "release", + SIDECAR_PLATFORMS: platforms.join(" "), + }, + }); + const published = (await readFile(calls, "utf8")).trim().split("\n"); + assert.deepEqual(new Set(published.slice(0, platforms.length)), new Set( + packed.slice(0, platforms.length).map(({ tarball }) => tarball), + )); + assert.deepEqual(new Set(published), new Set(packed.map(({ tarball }) => tarball))); + + const manifestPath = join(output, "manifest.json"); + const manifest = JSON.parse(await readFile(manifestPath, "utf8")); + const original = structuredClone(manifest); + manifest.entries[0].name = "@rivet-dev/agentos-sidecar-fixture"; + await writeFile(manifestPath, JSON.stringify(manifest)); + await assert.rejects(readPackedNpm(output, version, platforms), /unexpected packed npm package entry/); + + await writeFile(manifestPath, JSON.stringify(original)); + await writeFile(packed[0].tarball!, "modified"); + await assert.rejects(readPackedNpm(output, version, platforms), { + message: `Invalid packed npm digest for ${packed[0].name}`, + }); + } finally { + await rm(dir, { recursive: true, force: true }); + } +}); diff --git a/scripts/publish/src/lib/packed-npm.ts b/scripts/publish/src/lib/packed-npm.ts new file mode 100644 index 0000000000..d00d12f3f2 --- /dev/null +++ b/scripts/publish/src/lib/packed-npm.ts @@ -0,0 +1,142 @@ +import { execFile } from "node:child_process"; +import { createHash } from "node:crypto"; +import { createReadStream } from "node:fs"; +import { lstat, mkdir, readFile, readdir, rename, writeFile } from "node:fs/promises"; +import { join, resolve } from "node:path"; +import { promisify } from "node:util"; +import type { NpmPackage } from "./npm.ts"; +import type { Package } from "./packages.ts"; + +const run = promisify(execFile); +const maxPackages = 64; +const maxManifestBytes = 64 * 1024; +const platformPrefix = "@rivet-dev/agentos-sidecar-"; + +interface PackedPackage { + name: string; + file: string; + sha512: string; +} + +async function digest(path: string): Promise { + const hash = createHash("sha512"); + for await (const chunk of createReadStream(path)) hash.update(chunk); + return hash.digest("hex"); +} + +function tarballName(index: number): string { + return `package-${index}.tgz`; +} + +async function readPackedIdentity(path: string) { + // Read only package metadata. No files from the archive are extracted. + const { stdout } = await run("tar", ["-xOzf", path, "package/package.json"], { + maxBuffer: maxManifestBytes, + }); + return JSON.parse(stdout) as { + name?: string; + version?: string; + private?: boolean; + publishConfig?: { registry?: string; access?: string }; + }; +} + +async function checkIdentity(path: string, name: string, version: string) { + const pkg = await readPackedIdentity(path); + if (pkg.private || pkg.name !== name || pkg.version !== version) { + throw new Error(`Packed npm identity mismatch for ${name}@${version}`); + } + const config = pkg.publishConfig ?? {}; + if (Object.entries(config).some(([key, value]) => + key === "registry" ? value !== "https://registry.npmjs.org" && value !== "https://registry.npmjs.org/" + : key !== "access" || value !== "public")) { + throw new Error(`Unexpected publishConfig for packed npm package ${name}`); + } +} + +export async function preparePackedNpm( + packages: readonly Package[], + version: string, + directory: string, +): Promise { + if (packages.length === 0 || packages.length > maxPackages) { + throw new Error(`Expected 1 to ${maxPackages} npm packages`); + } + const out = resolve(directory); + await mkdir(out); + const entries: PackedPackage[] = []; + for (const [index, pkg] of packages.entries()) { + const { stdout } = await run("npm", ["pack", "--json", "--pack-destination", out], { + cwd: pkg.dir, + maxBuffer: 8 * 1024 * 1024, + }); + // npm 11 returns an array. npm 12 keys its result by package name. + const packResult = JSON.parse(stdout); + const packed = Array.isArray(packResult) ? packResult[0] : packResult?.[pkg.name]; + if (typeof packed?.filename !== "string" || !/^[A-Za-z0-9][A-Za-z0-9_.+-]*\.tgz$/.test(packed.filename)) { + throw new Error(`Unexpected npm pack filename for ${pkg.name}`); + } + const file = tarballName(index); + const path = join(out, file); + await rename(join(out, packed.filename), path); + await checkIdentity(path, pkg.name, version); + entries.push({ name: pkg.name, file, sha512: await digest(path) }); + } + await writeFile(join(out, "manifest.json"), JSON.stringify({ version, entries }), { flag: "wx" }); +} + +export async function readPackedNpm( + directory: string, + version: string, + platforms: readonly string[], +): Promise { + const root = resolve(directory); + const manifestPath = join(root, "manifest.json"); + const stat = await lstat(manifestPath); + if (!stat.isFile() || stat.size > maxManifestBytes) { + throw new Error("Invalid packed npm manifest file"); + } + const manifest = JSON.parse(await readFile(manifestPath, "utf8")) as { + version?: unknown; + entries?: unknown; + }; + if (!manifest || manifest.version !== version || !Array.isArray(manifest.entries) || + manifest.entries.length === 0 || manifest.entries.length > maxPackages) { + throw new Error("Invalid packed npm manifest version or package count"); + } + const required = new Set([ + ...platforms.map((platform) => platformPrefix + platform), + "@rivet-dev/agentos", + "@rivet-dev/agentos-node-pty", + "@rivet-dev/agentos-sandbox", + "@rivet-dev/agentos-toolchain", + "@rivet-dev/agentos-sidecar", + "secure-exec", + ]); + const allowed = new Set(required); + const seen = new Set(); + const files = new Set(await readdir(root)); + files.delete("manifest.json"); + const packages: NpmPackage[] = []; + for (const [index, entry] of manifest.entries.entries()) { + const { name, file, sha512 } = (entry ?? {}) as Partial; + if (typeof name !== "string" || !allowed.has(name) || seen.has(name) || + file !== tarballName(index) || typeof sha512 !== "string" || + !/^[a-f0-9]{128}$/.test(sha512) || !files.delete(file)) { + throw new Error("Invalid or unexpected packed npm package entry"); + } + if (index < platforms.length && !name.startsWith(platformPrefix)) { + throw new Error("Sidecar platform packages must be published first"); + } + const path = join(root, file); + if (!(await lstat(path)).isFile() || await digest(path) !== sha512) { + throw new Error(`Invalid packed npm digest for ${name}`); + } + await checkIdentity(path, name, version); + seen.add(name); + required.delete(name); + packages.push({ name, dir: root, relDir: file, tarball: path }); + } + if (files.size || required.size) throw new Error("Unexpected or missing packed npm artifacts"); + return packages; +} diff --git a/scripts/publish/src/lib/r2.test.ts b/scripts/publish/src/lib/r2.test.ts new file mode 100644 index 0000000000..666dd03652 --- /dev/null +++ b/scripts/publish/src/lib/r2.test.ts @@ -0,0 +1,84 @@ +import assert from "node:assert/strict"; +import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test from "node:test"; +import { copyPrefix, uploadDir } from "./r2.js"; + +test("staging and mirror publication order manifests last and prune only after success", async () => { + const root = await mkdtemp(join(tmpdir(), "release-r2-")); + const calls = join(root, "calls"); + const env = { ...process.env }; + try { + await writeFile(join(root, "aws"), `#!/usr/bin/env node +const fs = require("node:fs"); +const args = process.argv.slice(2); +const operation = args[1]; +const arg = (name) => args[args.indexOf(name) + 1]; +if (operation === "list-objects-v2") { + const prefix = arg("--prefix"); + const names = prefix === "stage/" + ? ["default-software.json", "manifest.json", "packages/one", "packages/two"] + : ["default-software.json", "manifest.json", "packages/one", "stale"]; + console.log(JSON.stringify({ Contents: names.map((name) => ({ Key: prefix + name })) })); +} else { + const key = operation === "cp" + ? args[3].replace("s3://rivet-releases/", "") + : arg("--key"); + fs.appendFileSync(process.env.CALLS, operation + " " + key + "\\n"); + if (process.env.FAIL_COPY === key) process.exitCode = 1; +} +`, { mode: 0o700 }); + Object.assign(process.env, { + PATH: `${root}:${env.PATH}`, + CALLS: calls, + R2_RELEASES_ACCESS_KEY_ID: "test", + R2_RELEASES_SECRET_ACCESS_KEY: "test", + FAIL_COPY: "release/packages/two", + }); + const recorded = async () => (await readFile(calls, "utf8")).trim().split("\n"); + await assert.rejects(copyPrefix("stage/", "release/"), /failed after copying 1 of 4 objects/); + assert.deepEqual(await recorded(), [ + "copy-object release/packages/one", + "copy-object release/packages/two", + ]); + + delete process.env.FAIL_COPY; + await writeFile(calls, ""); + await copyPrefix("stage/", "release/"); + assert.deepEqual(await recorded(), [ + "copy-object release/packages/one", + "copy-object release/packages/two", + "copy-object release/manifest.json", + "copy-object release/default-software.json", + "delete-object release/stale", + ]); + + const local = join(root, "local"); + await mkdir(join(local, "packages"), { recursive: true }); + await writeFile(join(local, "packages/new"), "artifact"); + await writeFile(join(local, "default-software.json"), "{}"); + await writeFile(join(local, "manifest.json"), "{}"); + await writeFile(calls, ""); + process.env.FAIL_COPY = "stage/packages/new"; + await assert.rejects(uploadDir(local, "stage/"), /failed after uploading 0 of 3 objects/); + assert.deepEqual(await recorded(), ["cp stage/packages/new"]); + + delete process.env.FAIL_COPY; + await writeFile(calls, ""); + await uploadDir(local, "stage/"); + assert.deepEqual(await recorded(), [ + "cp stage/packages/new", + "cp stage/manifest.json", + "cp stage/default-software.json", + "delete-object stage/packages/one", + "delete-object stage/packages/two", + ]); + } finally { + for (const key of ["PATH", "CALLS", "R2_RELEASES_ACCESS_KEY_ID", "R2_RELEASES_SECRET_ACCESS_KEY", "FAIL_COPY"]) { + if (env[key] === undefined) delete process.env[key]; + else process.env[key] = env[key]; + } + await rm(root, { recursive: true, force: true }); + } +}); diff --git a/scripts/publish/src/lib/r2.ts b/scripts/publish/src/lib/r2.ts index 70b3d3194d..cea4100752 100644 --- a/scripts/publish/src/lib/r2.ts +++ b/scripts/publish/src/lib/r2.ts @@ -12,6 +12,8 @@ * avoids it. See: * https://community.cloudflare.com/t/r2-s3-compat-doesnt-support-net-sdk-for-copy-operations-due-to-tagging-header/616867 */ +import { opendir } from "node:fs/promises"; +import { join } from "node:path"; import { $ } from "execa"; import { scoped } from "./logger.js"; @@ -21,6 +23,9 @@ const BUCKET = "rivet-releases"; const ENDPOINT_URL = "https://2a94c6a0ced8d35ea63cddc86c2681e7.r2.cloudflarestorage.com"; +const MAX_ARTIFACT_ENTRIES = 4096; +const MAX_ARTIFACT_DEPTH = 32; + type R2Env = Record; let cached: R2Env | null = null; @@ -55,15 +60,15 @@ export async function listObjects(prefix: string): Promise { const contents: ListEntry[] = []; let continuationToken: string | undefined; - while (true) { + for (let pageIndex = 0; pageIndex < MAX_ARTIFACT_ENTRIES; pageIndex++) { const { stdout } = continuationToken ? await $({ env, - })`aws s3api list-objects-v2 --bucket ${BUCKET} --prefix ${prefix} --continuation-token ${continuationToken} --endpoint-url ${ENDPOINT_URL}` + })`aws s3api list-objects-v2 --no-paginate --bucket ${BUCKET} --prefix ${prefix} --continuation-token ${continuationToken} --endpoint-url ${ENDPOINT_URL}` : await $({ env, - })`aws s3api list-objects-v2 --bucket ${BUCKET} --prefix ${prefix} --endpoint-url ${ENDPOINT_URL}`; - if (!stdout.trim()) break; + })`aws s3api list-objects-v2 --no-paginate --bucket ${BUCKET} --prefix ${prefix} --endpoint-url ${ENDPOINT_URL}`; + if (!stdout.trim()) throw new Error(`R2 returned an empty listing for ${prefix}`); const page = JSON.parse(stdout) as { Contents?: ListEntry[]; @@ -71,15 +76,23 @@ export async function listObjects(prefix: string): Promise { NextContinuationToken?: string; }; if (Array.isArray(page.Contents)) { - contents.push(...page.Contents); + for (const entry of page.Contents) { + if (typeof entry.Key !== "string" || !entry.Key.startsWith(prefix)) { + throw new Error(`R2 listed an object outside prefix ${prefix}`); + } + if (contents.length === MAX_ARTIFACT_ENTRIES) { + throw new Error(`R2 listing for ${prefix} exceeds ${MAX_ARTIFACT_ENTRIES} objects`); + } + contents.push(entry); + } } - if (!page.IsTruncated || !page.NextContinuationToken) { - break; + if (!page.IsTruncated) return { Contents: contents }; + if (!page.NextContinuationToken || page.NextContinuationToken === continuationToken) { + throw new Error(`R2 listing for ${prefix} has an invalid continuation token`); } continuationToken = page.NextContinuationToken; } - - return { Contents: contents }; + throw new Error(`R2 listing for ${prefix} exceeds ${MAX_ARTIFACT_ENTRIES} pages`); } /** Upload a single file to R2. */ @@ -95,27 +108,68 @@ export async function uploadFile( })`aws s3 cp ${localPath} s3://${BUCKET}/${r2Key} --endpoint-url ${ENDPOINT_URL} --checksum-algorithm CRC32`; } -/** Recursively upload a directory to an R2 prefix. */ -export async function uploadDir( - localDir: string, - r2Prefix: string, +// Software manifests are commit markers for their content-addressed packages. +function publishOrder(relativeKey: string): number { + if (relativeKey === "default-software.json") return 2; + if (relativeKey === "manifest.json") return 1; + return 0; +} + +async function localArtifactFiles(directory: string): Promise { + const pending = [""]; + const files: string[] = []; + let entries = 0; + while (pending.length > 0) { + const parent = pending.pop()!; + for await (const entry of await opendir(join(directory, parent))) { + const relative = parent ? `${parent}/${entry.name}` : entry.name; + if (++entries > MAX_ARTIFACT_ENTRIES || relative.split("/").length > MAX_ARTIFACT_DEPTH) { + throw new Error(`Artifact tree in ${directory} exceeds its entry or depth limit`); + } + if (entry.isDirectory()) pending.push(relative); + else if (entry.isFile()) files.push(relative); + else throw new Error(`Artifact entry must be a regular file or directory: ${relative}`); + } + } + if (files.length === 0) throw new Error(`Artifact directory ${directory} has no files`); + return files.sort((a, b) => publishOrder(a) - publishOrder(b) || a.localeCompare(b)); +} + +async function pruneStaleObjects( + destination: ListResult, + published: ReadonlySet, ): Promise { const env = getR2Env(); - log.info(`uploading directory ${localDir} -> ${r2Prefix}`); - await $({ - env, - stdio: "inherit", - })`aws s3 cp ${localDir} s3://${BUCKET}/${r2Prefix} --recursive --endpoint-url ${ENDPOINT_URL} --checksum-algorithm CRC32`; + for (const { Key } of destination.Contents) { + if (published.has(Key)) continue; + await $({ + env, + })`aws s3api delete-object --bucket ${BUCKET} --key ${Key} --endpoint-url ${ENDPOINT_URL}`; + } } -/** Delete every object under an R2 prefix. */ -export async function deletePrefix(r2Prefix: string): Promise { - const env = getR2Env(); - log.info(`deleting ${r2Prefix}`); - await $({ - env, - stdio: "inherit", - })`aws s3 rm s3://${BUCKET}/${r2Prefix} --recursive --endpoint-url ${ENDPOINT_URL}`; +/** Stage the exact artifact set, preserving previous objects on upload failure. */ +export async function uploadDir( + localDir: string, + r2Prefix: string, +): Promise { + if (!r2Prefix.endsWith("/")) throw new Error("R2 upload requires a directory prefix"); + const files = await localArtifactFiles(localDir); + const destination = await listObjects(r2Prefix); + const uploaded = new Set(); + try { + for (const file of files) { + const key = r2Prefix + file; + await uploadFile(join(localDir, file), key); + uploaded.add(key); + } + } catch (cause) { + throw new Error( + `R2 upload to ${r2Prefix} failed after uploading ${uploaded.size} of ${files.length} objects; stale objects retained`, + { cause }, + ); + } + await pruneStaleObjects(destination, uploaded); } /** @@ -126,30 +180,39 @@ export async function copyPrefix( sourcePrefix: string, targetPrefix: string, ): Promise { + if (!sourcePrefix.endsWith("/") || !targetPrefix.endsWith("/") || + sourcePrefix.startsWith(targetPrefix) || targetPrefix.startsWith(sourcePrefix)) { + throw new Error("R2 copy requires distinct, non-overlapping directory prefixes"); + } const env = getR2Env(); log.info(`copying ${sourcePrefix} -> ${targetPrefix}`); - const list = await listObjects(sourcePrefix); - if (list.Contents.length === 0) { - log.warn( - `source prefix ${sourcePrefix} is empty. Skipping copy to ${targetPrefix}.`, - ); - return; + const source = await listObjects(sourcePrefix); + if (source.Contents.length === 0) { + throw new Error(`R2 source prefix ${sourcePrefix} has no objects`); } - - // Delete the target first so stale files from a prior publish are cleaned. + source.Contents.sort((a, b) => + publishOrder(a.Key.slice(sourcePrefix.length)) - publishOrder(b.Key.slice(sourcePrefix.length)), + ); + const destination = await listObjects(targetPrefix); + const published = new Set(); try { - await deletePrefix(targetPrefix); - } catch { - // Target may not exist yet — that's fine. + for (const { Key: sourceKey } of source.Contents) { + const targetKey = targetPrefix + sourceKey.slice(sourcePrefix.length); + const encodedSource = sourceKey.split("/").map(encodeURIComponent).join("/"); + log.info(` ${sourceKey} -> ${targetKey}`); + await $({ + env, + })`aws s3api copy-object --bucket ${BUCKET} --key ${targetKey} --copy-source ${BUCKET}/${encodedSource} --endpoint-url ${ENDPOINT_URL}`; + published.add(targetKey); + } + } catch (cause) { + throw new Error( + `R2 copy to ${targetPrefix} failed after copying ${published.size} of ${source.Contents.length} objects; stale objects retained`, + { cause }, + ); } - for (const obj of list.Contents) { - const sourceKey = obj.Key; - const targetKey = sourceKey.replace(sourcePrefix, targetPrefix); - log.info(` ${sourceKey} -> ${targetKey}`); - await $({ - env, - })`aws s3api copy-object --bucket ${BUCKET} --key ${targetKey} --copy-source ${BUCKET}/${sourceKey} --endpoint-url ${ENDPOINT_URL}`; - } + // Remove only previously listed keys after every new object has arrived. + await pruneStaleObjects(destination, published); } diff --git a/scripts/publish/src/lib/rust-crates.test.ts b/scripts/publish/src/lib/rust-crates.test.ts index 5d7b1fdb3e..a4929b0b9f 100644 --- a/scripts/publish/src/lib/rust-crates.test.ts +++ b/scripts/publish/src/lib/rust-crates.test.ts @@ -3,7 +3,7 @@ import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "nod import { tmpdir } from "node:os"; import { join } from "node:path"; import test from "node:test"; -import { discoverRustCrates, RUST_CRATES } from "./rust-crates.js"; +import { assertPublishableV8Source, discoverRustCrates, RUST_CRATES } from "./rust-crates.js"; function withFixture(fn: (root: string) => void) { const root = mkdtempSync(join(tmpdir(), "publish-rust-crates-")); @@ -148,3 +148,23 @@ test("hosted crates publish before their actual manifest dependents", () => { } } }); + +test("real crate releases require every locked V8 dependency to resolve through crates.io", () => { + withFixture((root) => { + const registry = '[[package]]\nname = "v8"\nversion = "1.0.0"\nsource = "registry+https://github.com/rust-lang/crates.io-index"\n'; + write(root, "Cargo.lock", registry); + const sha256 = "a".repeat(64); + const pins = Object.fromEntries([ + "x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", + "x86_64-apple-darwin", "aarch64-apple-darwin", + ].map((target) => [target, sha256])); + write(root, "scripts/ci/v8/archives.json", JSON.stringify({ ...pins, "x86_64-apple-darwin": { sha256, artifactId: 123 } })); + assert.throws(() => assertPublishableV8Source(root), /durable V8 archive SHA-256/); + write(root, "scripts/ci/v8/archives.json", JSON.stringify(pins)); + assert.doesNotThrow(() => assertPublishableV8Source(root)); + for (const source of ['', 'source = "git+https://example.org/engine#abc"\n']) { + write(root, "Cargo.lock", registry + '[[package]]\nname = "v8"\nversion = "2.0.0"\n' + source); + assert.throws(() => assertPublishableV8Source(root), /outside the agentOS workspace/); + } + }); +}); diff --git a/scripts/publish/src/lib/rust-crates.ts b/scripts/publish/src/lib/rust-crates.ts index 77d22c6378..4e198e0767 100644 --- a/scripts/publish/src/lib/rust-crates.ts +++ b/scripts/publish/src/lib/rust-crates.ts @@ -41,6 +41,27 @@ export const STANDALONE_RUST_CRATE_MANIFESTS = { "crates/executor-wasm-wasmtime/Cargo.toml", } as const; +/** Workspace patches do not follow an agentOS crate to its downstream consumers. */ +export function assertPublishableV8Source(repoRoot: string): void { + const lock = readFileSync(join(repoRoot, "Cargo.lock"), "utf8"); + const engines = lock.split(/^\[\[package\]\]$/m).filter((p) => /^name = "v8"$/m.test(p)); + if (engines.length === 0 || engines.some((p) => !/^source = "registry\+https:\/\/github\.com\/rust-lang\/crates\.io-index"$/m.test(p))) { + throw new Error( + "Rust publication requires a crates.io V8 dependency that works outside the agentOS workspace. Qualify the registry crate and its matching engine bundle before releasing.", + ); + } + const pins = JSON.parse(readFileSync(join(repoRoot, "scripts/ci/v8/archives.json"), "utf8")); + for (const os of ["unknown-linux-gnu", "apple-darwin"]) { + for (const arch of ["x86_64", "aarch64"]) { + const target = `${arch}-${os}`; + const pin = pins[target]; + if (typeof pin !== "string" || !/^[0-9a-f]{64}$/.test(pin)) { + throw new Error(`Release requires a durable V8 archive SHA-256 for ${target}.`); + } + } + } +} + function readPackageName(manifestPath: string): string | undefined { const manifest = readFileSync(manifestPath, "utf8"); const match = manifest.match(/^\s*name\s*=\s*"([^"]+)"/m); diff --git a/scripts/publish/src/lib/version.test.ts b/scripts/publish/src/lib/version.test.ts index 338627dedc..067fc51895 100644 --- a/scripts/publish/src/lib/version.test.ts +++ b/scripts/publish/src/lib/version.test.ts @@ -255,6 +255,13 @@ test("bumpPackageJsons rejects unpublished registry software runtime dependencie version: "0.0.1", }); + for (const platform of DEFAULT_SIDECAR_PLATFORMS) { + await writeJson(repoRoot, `packages/sidecar/npm/${platform}/package.json`, { + name: `@rivet-dev/agentos-sidecar-${platform}`, + version: "0.0.1", + }); + } + await assert.rejects( bumpPackageJsons(repoRoot, "0.0.0-preview.abc1234", { repository: "rivet-dev/agentos", diff --git a/scripts/publish/src/lib/version.ts b/scripts/publish/src/lib/version.ts index fb29a6d77f..ca89ea1bf6 100644 --- a/scripts/publish/src/lib/version.ts +++ b/scripts/publish/src/lib/version.ts @@ -20,6 +20,7 @@ import * as fs from "node:fs/promises"; import { join } from "node:path"; import { $ } from "execa"; import * as semver from "semver"; +import { validatePublishVersion } from "./context.js"; import { scoped } from "./logger.js"; import { buildMetaPlatformMap, discoverPackages } from "./packages.js"; @@ -364,10 +365,7 @@ export async function resolveVersion( opts: ResolveVersionOpts, ): Promise { if (opts.version) { - if (!semver.valid(opts.version)) { - throw new Error(`invalid semantic version: ${opts.version}`); - } - return opts.version; + return validatePublishVersion(opts.version); } if (!opts.major && !opts.minor && !opts.patch) { throw new Error("must provide --version, --major, --minor, or --patch"); diff --git a/scripts/publish/tsconfig.json b/scripts/publish/tsconfig.json index bf5a3ac465..8b14094d42 100644 --- a/scripts/publish/tsconfig.json +++ b/scripts/publish/tsconfig.json @@ -6,6 +6,7 @@ "moduleResolution": "NodeNext", "strict": true, "noEmit": true, + "allowImportingTsExtensions": true, "esModuleInterop": true, "skipLibCheck": true, "resolveJsonModule": true, diff --git a/scripts/wasi-delegate-rights.test.mjs b/scripts/wasi-delegate-rights.test.mjs new file mode 100644 index 0000000000..76809d9f24 --- /dev/null +++ b/scripts/wasi-delegate-rights.test.mjs @@ -0,0 +1,104 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, readFileSync, rmSync, statSync } from "node:fs"; +import { createRequire } from "node:module"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test from "node:test"; +import { runInNewContext } from "node:vm"; + +const source = readFileSync( + new URL("../crates/executor-v8-runtime/assets/runners/wasi-module.js", import.meta.url), + "utf8", +); +const requireBuiltin = createRequire(import.meta.url); + +function createWasi({ managed = false, host = {}, preopens = {} } = {}) { + const context = { + Buffer, WebAssembly, process: { env: {} }, + __agentOSManagedKernelHost: managed, + __agentOSWasiHost: { requireBuiltin, syncReadLimitBytes: 1024, ...host }, + }; + runInNewContext(source, context); + const instance = new context.__agentOSWasiModule.WASI({ preopens }); + const memory = new WebAssembly.Memory({ initial: 1 }); + instance.instance = { exports: { memory } }; + return { api: instance.wasiImport, data: new DataView(memory.buffer), bytes: Buffer.from(memory.buffer) }; +} + +test("managed stdin uses the kernel for both fd 0 and its alias, preserving EOF and errno", () => { + const calls = []; + const results = [{ dataBase64: "QQ==" }, { done: true }, Object.assign(new Error("closed"), { code: "EBADF" })]; + const { api, data, bytes } = createWasi({ + managed: true, + host: { + lookupFdHandle: fd => fd === 7 ? { kind: "stdio", targetFd: 0 } : null, + syncRpc: { + callSync(name) { + calls.push(name); + const result = results.shift(); + if (result instanceof Error) throw result; + return result; + }, + }, + }, + }); + data.setUint32(0, 32, true); + data.setUint32(4, 1, true); + assert.equal(api.fd_read(0, 0, 1, 16), 0); + assert.equal(data.getUint32(16, true), 1); + assert.equal(bytes[32], 65); + assert.equal(api.fd_read(7, 0, 1, 16), 0); + assert.equal(data.getUint32(16, true), 0); + assert.equal(api.fd_read(0, 0, 1, 16), 8); + assert.deepEqual(calls, Array(3).fill("__kernel_stdin_read")); +}); + +test("a limited directory grants individual directory, create, link and file rights", t => { + const root = mkdtempSync(join(tmpdir(), "agentos-wasi-rights-")); + t.after(() => rmSync(root, { recursive: true, force: true })); + const read = 1n << 1n; + const write = 1n << 6n; + const rights = write | (1n << 9n) | (1n << 10n) | (1n << 11n) | (1n << 12n) | (1n << 13n); + const { api, data, bytes } = createWasi({ + preopens: { "/fixture": { hostPath: root, rightsBase: rights, rightsInheriting: read | write } }, + }); + bytes.write("dir", 32); + assert.equal(api.path_create_directory(3, 32, 3), 0); + assert.ok(statSync(join(root, "dir")).isDirectory()); + assert.equal(api.path_remove_directory(3, 32, 3), 76); + bytes.write("a", 32); + assert.equal(api.path_open(3, 0, 32, 1, 1, write, 0n, 0, 16), 0); + const fd = data.getUint32(16, true); + try { + data.setUint32(0, 40, true); + data.setUint32(4, 2, true); + bytes.write("ok", 40); + assert.equal(api.fd_write(fd, 0, 1, 24), 0); + assert.equal(data.getUint32(24, true), 2); + assert.equal(api.fd_read(fd, 0, 1, 24), 76); + } finally { + assert.equal(api.fd_close(fd), 0); + } + bytes.write("b", 36); + assert.equal(api.path_link(3, 0, 32, 1, 3, 36, 1), 0); + assert.equal(readFileSync(join(root, "b"), "utf8"), "ok"); + assert.equal(api.path_unlink_file(3, 36, 1), 76); +}); + +test("Preview 1 link source and target work with separately attenuated directory rights", t => { + const root = mkdtempSync(join(tmpdir(), "agentos-wasi-link-")); + t.after(() => rmSync(root, { recursive: true, force: true })); + const { api, data, bytes } = createWasi({ + preopens: { + "/source": { hostPath: root, rightsBase: (1n << 6n) | (1n << 10n) | (1n << 13n), rightsInheriting: 1n << 6n }, + "/link-source": { hostPath: root, rightsBase: 1n << 11n }, + "/link-target": { hostPath: root, rightsBase: 1n << 12n }, + }, + }); + bytes.write("old", 32); + bytes.write("new", 40); + assert.equal(api.path_open(3, 0, 32, 3, 1, 1n << 6n, 0n, 0, 16), 0); + assert.equal(api.fd_close(data.getUint32(16, true)), 0); + assert.equal(api.path_link(4, 0, 32, 3, 5, 40, 3), 0); + assert.equal(statSync(join(root, "old")).ino, statSync(join(root, "new")).ino); +}); diff --git a/secure-exec/src/runtime.ts b/secure-exec/src/runtime.ts index 96f873b8f4..b582e55b97 100644 --- a/secure-exec/src/runtime.ts +++ b/secure-exec/src/runtime.ts @@ -63,7 +63,10 @@ export interface Vm extends AsyncDisposable { export async function createVm( options: VmOptions = {}, ): Promise { - const vm = await AgentOs.create({ defaultsProfile: "secure", ...options }); + const vm = await AgentOs.create({ + ...options, + defaultsProfile: options.defaultsProfile ?? "secure", + }); const { npm, ...javascript } = vm.javascript; const dispose = () => vm.dispose(); return { diff --git a/secure-exec/tests/runtime-forwarding.test.ts b/secure-exec/tests/runtime-forwarding.test.ts index 12b151f16c..cc682793f7 100644 --- a/secure-exec/tests/runtime-forwarding.test.ts +++ b/secure-exec/tests/runtime-forwarding.test.ts @@ -1,6 +1,6 @@ import { AgentOs } from "@rivet-dev/agentos"; import { afterEach, expect, test, vi } from "vitest"; -import { execute } from "../src/index.js"; +import { createVm, execute } from "../src/index.js"; afterEach(() => vi.restoreAllMocks()); @@ -37,3 +37,21 @@ test("one-shot calls select secure defaults and forward VM options", async () => }); expect(dispose).toHaveBeenCalledOnce(); }); + +test("undefined selects secure defaults and an explicit profile is retained", async () => { + const dispose = vi.fn().mockResolvedValue(undefined); + const create = vi.spyOn(AgentOs, "create").mockResolvedValue({ + javascript: { + npm: {}, + execute: vi.fn().mockResolvedValue({ outcome: "succeeded" }), + }, + dispose, + } as unknown as AgentOs); + const vm = await createVm({ defaultsProfile: undefined }); + await vm.dispose(); + await execute("", { defaultsProfile: undefined }); + await execute("", { defaultsProfile: "agent_os" }); + expect( + create.mock.calls.map(([options]) => options?.defaultsProfile), + ).toEqual(["secure", "secure", "agent_os"]); +}); From e6ae5ed422c1498214947703fe660fd9e5d7fc84 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Fri, 20 Nov 2026 23:50:21 -0800 Subject: [PATCH 04/19] fix: update affected runtime and client dependencies --- Cargo.lock | 402 +++++++++--------- crates/client/Cargo.toml | 2 +- crates/client/src/agent_os.rs | 86 ++-- crates/client/src/config.rs | 21 +- crates/client/src/cron.rs | 31 +- crates/client/src/process.rs | 109 +++-- crates/client/src/shell.rs | 53 +-- crates/client/src/sidecar.rs | 51 ++- .../tests/support/terminal_pending_exit.rs | 4 +- .../support/terminal_replay_admission.rs | 4 +- crates/sidecar-client/Cargo.toml | 2 +- crates/sidecar-client/src/transport.rs | 18 +- crates/vm/Cargo.toml | 6 +- .../execution/host_dispatch/network_compat.rs | 189 +++----- crates/vm/src/execution/network/tls.rs | 22 +- crates/vm/tests/service.rs | 9 +- scripts/publish/src/lib/artifacts.ts | 2 +- scripts/publish/src/lib/r2.ts | 6 +- 18 files changed, 510 insertions(+), 507 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 186d7f7fa9..cff15da15d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -136,7 +136,7 @@ dependencies = [ "once_cell", "parking_lot", "reqwest", - "scc 2.4.0", + "scc", "serde", "serde_bare", "serde_bytes", @@ -416,7 +416,7 @@ dependencies = [ "command-fds", "futures", "parking_lot", - "scc 2.4.0", + "scc", "thiserror 2.0.21", "tokio", "tracing", @@ -525,9 +525,8 @@ dependencies = [ "oxc_transformer", "pbkdf2", "rusqlite", - "rustix 1.1.4", + "rustix 1.1.6", "rustls", - "rustls-pemfile", "scrypt", "serde", "serde_bare", @@ -721,7 +720,7 @@ dependencies = [ "aws-smithy-json", "aws-smithy-runtime", "aws-smithy-runtime-api", - "aws-smithy-schema", + "aws-smithy-schema 0.2.0", "aws-smithy-types", "aws-types", "bytes", @@ -738,9 +737,9 @@ dependencies = [ [[package]] name = "aws-credential-types" -version = "1.3.0" +version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e93964ffdaf57857f544be3666a5f57570bb699e934700f11b49708f61bb556e" +checksum = "576e8da35751dc85409ec1cd15999f692c277a1fbe9336780fe6f15e49dd637d" dependencies = [ "aws-smithy-async", "aws-smithy-runtime-api", @@ -750,9 +749,9 @@ dependencies = [ [[package]] name = "aws-lc-rs" -version = "1.17.1" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4342d8937fc7e5dd9b1c60292261c0670c882a2cd1719cfc11b1af41731e32ad" +checksum = "048006790404103c488e6c5616b3e7e702a71292aadda08e8f7698a9e0d2eb53" dependencies = [ "aws-lc-sys", "zeroize", @@ -760,9 +759,9 @@ dependencies = [ [[package]] name = "aws-lc-sys" -version = "0.42.0" +version = "0.47.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d9ceb1da931507a12f4fccea479dccd00da1943e1b4ae72d8e502d707361444" +checksum = "8bfbe32ac36b3c428696ce315464181bc4029d689fd58e3f2a4e58c252242f52" dependencies = [ "cc", "cmake", @@ -817,7 +816,7 @@ dependencies = [ "aws-smithy-observability", "aws-smithy-runtime", "aws-smithy-runtime-api", - "aws-smithy-schema", + "aws-smithy-schema 0.2.0", "aws-smithy-types", "aws-smithy-xml", "aws-types", @@ -851,7 +850,7 @@ dependencies = [ "aws-smithy-observability", "aws-smithy-runtime", "aws-smithy-runtime-api", - "aws-smithy-schema", + "aws-smithy-schema 0.2.0", "aws-smithy-types", "aws-types", "bytes", @@ -877,7 +876,7 @@ dependencies = [ "aws-smithy-observability", "aws-smithy-runtime", "aws-smithy-runtime-api", - "aws-smithy-schema", + "aws-smithy-schema 0.2.0", "aws-smithy-types", "aws-types", "bytes", @@ -904,7 +903,7 @@ dependencies = [ "aws-smithy-query", "aws-smithy-runtime", "aws-smithy-runtime-api", - "aws-smithy-schema", + "aws-smithy-schema 0.2.0", "aws-smithy-types", "aws-smithy-xml", "aws-types", @@ -917,9 +916,9 @@ dependencies = [ [[package]] name = "aws-sigv4" -version = "1.5.1" +version = "1.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "723c2234ad7511ceef63eab016b7ba6ff7c55590fefb96fa8467af014a07309f" +checksum = "6c5aa04c533733a77879294f1854c27b3715cc50ea71756d1f6a958702b1a9fb" dependencies = [ "aws-credential-types", "aws-smithy-eventstream", @@ -928,6 +927,7 @@ dependencies = [ "aws-smithy-types", "bytes", "crypto-bigint", + "ecdsa", "form_urlencoded", "hex", "hmac 0.13.0", @@ -944,9 +944,9 @@ dependencies = [ [[package]] name = "aws-smithy-async" -version = "1.3.0" +version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f02e407fb3b54891734224b9ffac8a71fdd35f542500fa1af95754a6b2beb316" +checksum = "de681a9df7ff79823b759ee17061076ec049cd61a8fa28ece4293d89c9f6964e" dependencies = [ "futures-util", "pin-project-lite", @@ -976,9 +976,9 @@ dependencies = [ [[package]] name = "aws-smithy-eventstream" -version = "0.61.1" +version = "0.61.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a9381123ab62d20c13082b151f30f962a3b112b727345394536dfa39a482944" +checksum = "14c8a643d0e114cae39e45415e3225793a6396698b7779a0c9e3ede731d8fc71" dependencies = [ "aws-smithy-types", "bytes", @@ -987,9 +987,9 @@ dependencies = [ [[package]] name = "aws-smithy-http" -version = "0.64.0" +version = "0.64.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37843d9add67c3aff5856f409c6dc315d3cdff60f9c0cb5b670dab1e9920306d" +checksum = "19af609dd5edf8b056cd0bb001c706c8b3b1458055328b334ec13d7f46f6aaa8" dependencies = [ "aws-smithy-eventstream", "aws-smithy-runtime-api", @@ -1003,15 +1003,14 @@ dependencies = [ "http-body-util", "percent-encoding", "pin-project-lite", - "pin-utils", "tracing", ] [[package]] name = "aws-smithy-http-client" -version = "1.2.0" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "635d23afda0a6ab48d666c4d447c4873e8d1e83518a2be2093122397e50b838e" +checksum = "203e36a754ced29dab0d710dab8c4fecf181d78f229e90fa6e75c6406ecb55a3" dependencies = [ "aws-smithy-async", "aws-smithy-runtime-api", @@ -1038,17 +1037,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dc65a121adb4b33729919fcfa14fa36fb33c1555a8f06bb0e2188dbfdc1d9ef" dependencies = [ "aws-smithy-runtime-api", - "aws-smithy-schema", + "aws-smithy-schema 0.2.0", "aws-smithy-types", ] [[package]] name = "aws-smithy-observability" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e86338c869539a581bf161247762a6e87f92c5c075060057b5ed6d06632ed0c" +checksum = "a416257d7b1b0f32a517a470a8c1dd354d8c4aa697ccab81a11a1e6cfb7418e3" dependencies = [ "aws-smithy-runtime-api", + "aws-smithy-types", + "tracing", ] [[package]] @@ -1058,7 +1059,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512346c7212ab7436df2d77a16d976a468ae44a418835511d2a69269810aaf62" dependencies = [ "aws-smithy-runtime-api", - "aws-smithy-schema", + "aws-smithy-schema 0.2.0", "aws-smithy-types", "aws-smithy-xml", "urlencoding", @@ -1066,35 +1067,32 @@ dependencies = [ [[package]] name = "aws-smithy-runtime" -version = "1.12.1" +version = "1.16.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07505b34e8f4b3591a4fa69e9792b52289b95488dbbc68c3c0075b7bedb245e1" +checksum = "7ed38b842f93dd8220cc8816750c72ce914b22c6eb3df02beb9632333ccb366d" dependencies = [ "aws-smithy-async", "aws-smithy-http", "aws-smithy-http-client", "aws-smithy-observability", "aws-smithy-runtime-api", - "aws-smithy-schema", + "aws-smithy-schema 1.2.0", "aws-smithy-types", "bytes", "fastrand", - "http 0.2.12", "http 1.4.2", - "http-body 0.4.6", "http-body 1.0.1", "http-body-util", "pin-project-lite", - "pin-utils", "tokio", "tracing", ] [[package]] name = "aws-smithy-runtime-api" -version = "1.14.0" +version = "1.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b98f2e1fd67ec06618f9c291e5e495a468e60519e44c9c1979cd0521f3affdb" +checksum = "c0b79dbb5331ccd297686df811d50ab3f39db171f5c765372a69272a0990b014" dependencies = [ "aws-smithy-async", "aws-smithy-runtime-api-macros", @@ -1110,9 +1108,9 @@ dependencies = [ [[package]] name = "aws-smithy-runtime-api-macros" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "221eaa237ddf1ca79b60d1372aad77e47f9c0ea5b3ce5099da8c61d027dc77b3" +checksum = "ce237f890a2283c9b328955c3b0918d2d5bf66a1cb6ceccd36a0047246fbc6e6" dependencies = [ "proc-macro2", "quote", @@ -1130,25 +1128,32 @@ dependencies = [ "http 1.4.2", ] +[[package]] +name = "aws-smithy-schema" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7ef30d39e131651359b60a1c560de9a9502a4c3754172ab2d6e8734fe21bcba" +dependencies = [ + "aws-smithy-runtime-api", + "aws-smithy-types", + "http 1.4.2", +] + [[package]] name = "aws-smithy-types" -version = "1.6.1" +version = "1.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6dc683efb34b9e755675b37fedbe0103141e5b6df7bdc9eb6967756a8c167d8" +checksum = "aff4bf014c512840b2bba80a20417b723cbd6a0a65c02e2d81f50b4268aedfc7" dependencies = [ "base64-simd", "bytes", "bytes-utils", "futures-core", - "http 0.2.12", "http 1.4.2", - "http-body 0.4.6", "http-body 1.0.1", "http-body-util", "itoa", - "num-integer", "pin-project-lite", - "pin-utils", "ryu", "serde", "time", @@ -1163,7 +1168,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce84f71c72fee2cbbadde6e7d082f5fb466e3a84733855295fa7aafd1b31b7d8" dependencies = [ "aws-smithy-runtime-api", - "aws-smithy-schema", + "aws-smithy-schema 0.2.0", "aws-smithy-types", "xmlparser", ] @@ -1177,7 +1182,7 @@ dependencies = [ "aws-credential-types", "aws-smithy-async", "aws-smithy-runtime-api", - "aws-smithy-schema", + "aws-smithy-schema 0.2.0", "aws-smithy-types", "rustc_version", "tracing", @@ -1296,9 +1301,9 @@ dependencies = [ [[package]] name = "base16ct" -version = "0.2.0" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" +checksum = "fd307490d624467aa6f74b0eabb77633d1f758a7b25f12bceb0b22e08d9726f6" [[package]] name = "base64" @@ -1623,21 +1628,6 @@ dependencies = [ "static_assertions", ] -[[package]] -name = "concurrent-queue" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "const-oid" -version = "0.9.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" - [[package]] name = "const-oid" version = "0.10.2" @@ -1706,6 +1696,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "cpubits" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15b85f9c39137c3a891689859392b1bd49812121d0d61c9caf00d46ed5ce06ae" + [[package]] name = "cpufeatures" version = "0.2.17" @@ -1936,12 +1932,16 @@ checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" [[package]] name = "crypto-bigint" -version = "0.5.5" +version = "0.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" +checksum = "914c195c4d7fbc5ed7f88d14cbf85914d5c664967658d43404b263913a477e82" dependencies = [ - "generic-array", - "rand_core 0.6.4", + "cpubits", + "ctutils", + "getrandom 0.4.3", + "hybrid-array", + "num-traits", + "rand_core 0.10.1", "subtle", "zeroize", ] @@ -1963,7 +1963,9 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" dependencies = [ + "getrandom 0.4.3", "hybrid-array", + "rand_core 0.10.1", ] [[package]] @@ -1977,11 +1979,12 @@ dependencies = [ [[package]] name = "ctutils" -version = "0.4.2" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d5515a3834141de9eafb9717ad39eea8247b5674e6066c404e8c4b365d2a29e" +checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f" dependencies = [ "cmov", + "subtle", ] [[package]] @@ -1992,11 +1995,11 @@ checksum = "a4ae5f15dda3c708c0ade84bfee31ccab44a3da4f88015ed22f63732abe300c8" [[package]] name = "der" -version = "0.7.10" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" +checksum = "6a996a108feb8b427a6e12fcf6af4aa83409b79d72c15337d7741368131ee004" dependencies = [ - "const-oid 0.9.6", + "const-oid", "pem-rfc7468", "zeroize", ] @@ -2014,7 +2017,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer 0.10.4", - "const-oid 0.9.6", "crypto-common 0.1.7", "subtle", ] @@ -2026,7 +2028,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" dependencies = [ "block-buffer 0.12.1", - "const-oid 0.10.2", + "const-oid", "crypto-common 0.2.2", "ctutils", ] @@ -2088,16 +2090,17 @@ checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" [[package]] name = "ecdsa" -version = "0.16.9" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee27f32b5c5292967d2d4a9d7f1e0b0aed2c15daded5a60300e4abb9d8020bca" +checksum = "c0681a4fc24c767085329728d8dfba959af91228aa4610cca4f8ce317ba46ae0" dependencies = [ "der", - "digest 0.10.7", + "digest 0.11.3", "elliptic-curve", "rfc6979", "signature", "spki", + "zeroize", ] [[package]] @@ -2108,19 +2111,20 @@ checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e" [[package]] name = "elliptic-curve" -version = "0.13.8" +version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5e6043086bf7973472e0c7dff2142ea0b680d30e18d9cc40f267efbf222bd47" +checksum = "900f37e7e18d74a2347389af6cd69e3a30d5cc9e697f40004865679aa75e9c7c" dependencies = [ "base16ct", "crypto-bigint", - "digest 0.10.7", + "crypto-common 0.2.2", + "digest 0.11.3", "ff", - "generic-array", "group", + "hybrid-array", "pem-rfc7468", "pkcs8", - "rand_core 0.6.4", + "rand_core 0.10.1", "sec1", "subtle", "zeroize", @@ -2165,16 +2169,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] name = "event-listener" -version = "5.4.1" +version = "5.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e13b66accf52311f30a0db42147dadea9850cb48cd070028831ae5f5d4b856ab" +checksum = "5a23add41df1562121a9393cb065eab5146a1242410f23a644851e90cfd669d2" dependencies = [ - "concurrent-queue", "parking", "pin-project-lite", ] @@ -2199,11 +2202,11 @@ checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6" [[package]] name = "ff" -version = "0.13.1" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0b50bfb653653f9ca9095b427bed08ab8d75a137839d9ad64eb11810d5b6393" +checksum = "a1f686ab92a9fb0eaf188f6c6c87b89490baa6fdb0db4544ba4dc47f7942489f" dependencies = [ - "rand_core 0.6.4", + "rand_core 0.10.1", "subtle", ] @@ -2403,7 +2406,6 @@ checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ "typenum", "version_check", - "zeroize", ] [[package]] @@ -2475,12 +2477,12 @@ checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" [[package]] name = "group" -version = "0.13.0" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" +checksum = "7fd1a1c7a5206c5b7a3f5a0d7ccd3ff85d0c8f5133d62a02680255b0004af5f4" dependencies = [ "ff", - "rand_core 0.6.4", + "rand_core 0.10.1", "subtle", ] @@ -2495,9 +2497,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.4.15" +version = "0.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6cb093c84e8bd9b188d4c4a8cb6579fc016968d14c99882163cd3ff402a4f155" +checksum = "a9f37a958b41b3b19ee2707c06439c0e9e547e847223eb791ecb0cb821c65e27" dependencies = [ "atomic-waker", "bytes", @@ -2754,7 +2756,9 @@ version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "818356c5132c1fede50f837ca96afbe78ff42413047f4abb886217845e1b6c8c" dependencies = [ + "subtle", "typenum", + "zeroize", ] [[package]] @@ -2793,7 +2797,7 @@ dependencies = [ "tokio", "tokio-rustls", "tower-service", - "webpki-roots 1.0.8", + "webpki-roots 1.0.10", ] [[package]] @@ -2826,7 +2830,7 @@ dependencies = [ "libc", "percent-encoding", "pin-project-lite", - "socket2 0.6.4", + "socket2 0.5.10", "system-configuration", "tokio", "tower-service", @@ -3381,7 +3385,7 @@ version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad38eb12aea514a0466ea40a80fd8cc83637065948eb4a426e4aa46261175227" dependencies = [ - "rustix 1.1.4", + "rustix 1.1.6", ] [[package]] @@ -4089,14 +4093,15 @@ dependencies = [ [[package]] name = "p256" -version = "0.13.2" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9863ad85fa8f4460f9c48cb909d38a0d689dba1f6f6988a5e3e0d31071bcd4b" +checksum = "d2c9239b2dbc807adbbe147e8cf72ea7450c3a0aabe62cb8e75ff4ec22e1f72a" dependencies = [ "ecdsa", "elliptic-curve", + "primefield", "primeorder", - "sha2 0.10.9", + "sha2 0.11.0", ] [[package]] @@ -4167,9 +4172,9 @@ dependencies = [ [[package]] name = "pem-rfc7468" -version = "0.7.0" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88b39c9bfcfc231068454382784bb460aae594343fb030d46e9f50a645418412" +checksum = "a6305423e0e7738146434843d1694d621cce767262b2a86910beab705e4493d9" dependencies = [ "base64ct", ] @@ -4309,17 +4314,11 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - [[package]] name = "pkcs8" -version = "0.10.2" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +checksum = "451913da69c775a56034ea8d9003d27ee8948e12443eae7c038ba100a4f21cb7" dependencies = [ "der", "spki", @@ -4408,13 +4407,31 @@ dependencies = [ "syn 2.0.118", ] +[[package]] +name = "primefield" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c555a6e4eb7d4e158fcb028c835c3b8642206ddc279b5c6b202ef9a8bdb592f4" +dependencies = [ + "crypto-bigint", + "crypto-common 0.2.2", + "ff", + "rand_core 0.10.1", + "subtle", + "zeroize", +] + [[package]] name = "primeorder" -version = "0.13.6" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "353e1ca18966c16d9deb1c69278edbc5f194139612772bd9537af60ac231e1e6" +checksum = "5c9f42978c78a00e3d68f69fc03e57a234debae69da4020a4fb588fcdcd07b06" dependencies = [ "elliptic-curve", + "once_cell", + "primefield", + "serdect", + "wnaf", ] [[package]] @@ -4544,7 +4561,7 @@ dependencies = [ "quinn-udp", "rustc-hash", "rustls", - "socket2 0.6.4", + "socket2 0.5.10", "thiserror 2.0.21", "tokio", "tracing", @@ -4582,9 +4599,9 @@ dependencies = [ "cfg_aliases", "libc", "once_cell", - "socket2 0.6.4", + "socket2 0.5.10", "tracing", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -4795,7 +4812,7 @@ dependencies = [ "wasm-bindgen-futures", "wasm-streams", "web-sys", - "webpki-roots 1.0.8", + "webpki-roots 1.0.10", ] [[package]] @@ -4816,12 +4833,12 @@ checksum = "1e061d1b48cb8d38042de4ae0a7a6401009d6143dc80d2e2d6f31f0bdd6470c7" [[package]] name = "rfc6979" -version = "0.4.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" +checksum = "b4a459cddafb3fe76b31fd8f1108007566c40301feb64dc7b54656eb7388172b" dependencies = [ - "hmac 0.12.1", - "subtle", + "crypto-bigint", + "hmac 0.13.0", ] [[package]] @@ -5010,7 +5027,7 @@ dependencies = [ "reqwest", "rivetkit-client-protocol", "rivetkit-shared-types", - "scc 3.8.8", + "scc", "serde", "serde_bare", "serde_cbor", @@ -5075,7 +5092,7 @@ dependencies = [ "rivetkit-engine-process", "rivetkit-inspector-protocol", "rivetkit-shared-types", - "scc 3.8.8", + "scc", "serde", "serde_bare", "serde_bytes", @@ -5204,27 +5221,27 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.4.15", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] name = "rustix" -version = "1.1.4" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" dependencies = [ "bitflags", "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] name = "rustls" -version = "0.23.41" +version = "0.23.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b92b125634d9b795e7beca796cc790df15a7fb38323bf3196fda83292d06b1f" +checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" dependencies = [ "aws-lc-rs", "log", @@ -5248,15 +5265,6 @@ dependencies = [ "security-framework", ] -[[package]] -name = "rustls-pemfile" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" -dependencies = [ - "rustls-pki-types", -] - [[package]] name = "rustls-pki-types" version = "1.15.0" @@ -5269,9 +5277,9 @@ dependencies = [ [[package]] name = "rustls-webpki" -version = "0.103.13" +version = "0.103.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61c429a8649f110dddef65e2a5ad240f747e85f7758a6bccc7e5777bd33f756e" +checksum = "e3a0760a887c14823fca5a1c970b01d47c328c696c0c64829d1df50e8f853fd5" dependencies = [ "aws-lc-rs", "ring", @@ -5315,15 +5323,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "scc" -version = "2.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46e6f046b7fef48e2660c57ed794263155d713de679057f2d0c169bfc6e756cc" -dependencies = [ - "sdd 3.0.10", -] - [[package]] name = "scc" version = "3.8.8" @@ -5331,7 +5330,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d5f8ca67c4f6208caf824037a6a9df0211b4e0937fccc4f543441f294d04511" dependencies = [ "saa", - "sdd 4.8.11", + "sdd", ] [[package]] @@ -5361,12 +5360,6 @@ dependencies = [ "sha2 0.10.9", ] -[[package]] -name = "sdd" -version = "3.0.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "490dcfcbfef26be6800d11870ff2df8774fa6e86d047e3e8c8a76b25655e41ca" - [[package]] name = "sdd" version = "4.8.11" @@ -5378,14 +5371,14 @@ dependencies = [ [[package]] name = "sec1" -version = "0.7.3" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" +checksum = "d56d437c2f19203ce5f7122e507831de96f3d2d4d3be5af44a0b0a09d8a80e4d" dependencies = [ "base16ct", + "ctutils", "der", - "generic-array", - "pkcs8", + "hybrid-array", "subtle", "zeroize", ] @@ -5437,9 +5430,9 @@ checksum = "1bc711410fbe7399f390ca1c3b60ad0f53f80e95c5eb935e52268a0e2cd49acc" [[package]] name = "serde" -version = "1.0.228" +version = "1.0.229" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" dependencies = [ "serde_core", "serde_derive", @@ -5476,22 +5469,22 @@ dependencies = [ [[package]] name = "serde_core" -version = "1.0.228" +version = "1.0.229" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.228" +version = "1.0.229" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" dependencies = [ "proc-macro2", "quote", - "syn 2.0.118", + "syn 3.0.10", ] [[package]] @@ -5530,6 +5523,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serdect" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66cf8fedced2fcf12406bcb34223dffb92eaf34908ede12fed414c82b7f00b3e" +dependencies = [ + "base16ct", + "serde", +] + [[package]] name = "sha1" version = "0.10.6" @@ -5617,12 +5620,12 @@ dependencies = [ [[package]] name = "signature" -version = "2.2.0" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +checksum = "28d567dcbaf0049cb8ac2608a76cd95ff9e4412e1899d389ee400918ca7537f5" dependencies = [ - "digest 0.10.7", - "rand_core 0.6.4", + "digest 0.11.3", + "rand_core 0.10.1", ] [[package]] @@ -5723,9 +5726,9 @@ checksum = "d5fe4ccb98d9c292d56fec89a5e07da7fc4cf0dc11e156b41793132775d3e591" [[package]] name = "spki" -version = "0.7.3" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +checksum = "7ef958a98b9d5da290cfc78946e9f3e61e1e62a18db0d92cac0b83cc161491a9" dependencies = [ "base64ct", "der", @@ -5884,10 +5887,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.4.3", + "getrandom 0.3.4", "once_cell", - "rustix 1.1.4", - "windows-sys 0.61.2", + "rustix 1.1.6", + "windows-sys 0.52.0", ] [[package]] @@ -6063,9 +6066,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.52.3" +version = "1.53.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe" +checksum = "ce3335fa71841cda333a58d7615b03901380ecf09d59b3296d21f8bbac0dde4e" dependencies = [ "bytes", "libc", @@ -6871,7 +6874,7 @@ dependencies = [ "once_cell", "postcard", "pulley-interpreter", - "rustix 1.1.4", + "rustix 1.1.6", "serde", "serde_derive", "smallvec", @@ -6986,7 +6989,7 @@ checksum = "818599f226d134c04432c4d7a34541687a6d965fbfa9bc73b95a2d4b7ae99c8a" dependencies = [ "cc", "libc", - "rustix 1.1.4", + "rustix 1.1.6", "wasmtime-environ", "wasmtime-internal-versioned-export-macros", "windows-sys 0.61.2", @@ -7103,9 +7106,9 @@ dependencies = [ [[package]] name = "webpki-root-certs" -version = "1.0.8" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d46a5a140e6f7afeccd8eae97eff335163939eac8b929834875168b29b3d267" +checksum = "58ec80e73fd8baf57bf0d9615554c75348bda990d6d5bbeab964dd068319fecb" dependencies = [ "rustls-pki-types", ] @@ -7116,14 +7119,14 @@ version = "0.26.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9" dependencies = [ - "webpki-roots 1.0.8", + "webpki-roots 1.0.10", ] [[package]] name = "webpki-roots" -version = "1.0.8" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf85cb06032201fa7c6f829d7db5a7e5aa45bcc0655327713065f6f0576731bf" +checksum = "1c1c8e20c8a38fc7d65abfe74eb7280e4aa667e8eec8bc84996d2874810748d9" dependencies = [ "rustls-pki-types", ] @@ -7168,7 +7171,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -7256,15 +7259,6 @@ dependencies = [ "windows-targets", ] -[[package]] -name = "windows-sys" -version = "0.59.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" -dependencies = [ - "windows-targets", -] - [[package]] name = "windows-sys" version = "0.61.2" @@ -7400,6 +7394,18 @@ dependencies = [ "wast 35.0.2", ] +[[package]] +name = "wnaf" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "795ca18b3fdb5e62bf982199278341ddcf7ebf7d32e25e212ad05d496e95f6fa" +dependencies = [ + "ff", + "group", + "hybrid-array", + "primefield", +] + [[package]] name = "writeable" version = "0.6.4" @@ -7413,7 +7419,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156" dependencies = [ "libc", - "rustix 1.1.4", + "rustix 1.1.6", ] [[package]] diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index 80026c71de..109e3c7862 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -31,7 +31,7 @@ http = "1" once_cell = "1" parking_lot = "0.12" reqwest = { version = "0.12", default-features = false, features = ["http2", "rustls-tls", "stream"] } -scc = "2" +scc = "3.8.4" serde = { version = "1.0", features = ["derive"] } serde_bare = "0.5" serde_bytes = "0.11" diff --git a/crates/client/src/agent_os.rs b/crates/client/src/agent_os.rs index 3e21791708..0ddb84e186 100644 --- a/crates/client/src/agent_os.rs +++ b/crates/client/src/agent_os.rs @@ -394,14 +394,14 @@ impl AgentOs { let mut events = transport.subscribe_wire_events(); let create_vm_config = serialize_create_vm_config_for_sidecar(&config)?; if let Some(callback) = config.sidecar_js_bridge_callback.clone() { - let _ = session_js_bridge_callbacks().insert(session_key.clone(), callback); + let _ = session_js_bridge_callbacks().insert_sync(session_key.clone(), callback); transport.register_wire_callback( "js_bridge_call", js_bridge_call_callback(transport_id), ); } if let Some(callback) = config.sidecar_sqlite_callback.clone() { - let _ = session_sqlite_callbacks().insert(session_key.clone(), callback); + let _ = session_sqlite_callbacks().insert_sync(session_key.clone(), callback); transport.register_wire_callback("ext", sqlite_callback_callback(transport_id)); } @@ -695,7 +695,7 @@ impl AgentOs { } } } - let _ = vm_host_functions().insert( + let _ = vm_host_functions().insert_sync( vm_route_key(transport_id, &vm_id), Arc::new(VmHostFunctionRegistry { host_functions: resolved_host_functions.clone(), @@ -772,11 +772,11 @@ impl AgentOs { tracing::error!(%vm_id, %error, "failed to dispose partially initialized VM; retaining sidecar ownership until explicit worker shutdown"); } let route = vm_route_key(transport.instance_id(), &vm_id); - vm_host_functions().remove(&route); + vm_host_functions().remove_sync(&route); } if let Some(key) = created_session_key { - session_js_bridge_callbacks().remove(&key); - session_sqlite_callbacks().remove(&key); + session_js_bridge_callbacks().remove_sync(&key); + session_sqlite_callbacks().remove_sync(&key); } if let Some(lease) = lease { if cleanup_confirmed { @@ -1047,7 +1047,7 @@ impl AgentOs { // 2-5. Best-effort drain tracked shell and terminal tasks before the VM is disposed, bounded // by SHELL_DISPOSE_TIMEOUT_MS so late output cannot race a closed transport. let mut exit_tasks = Vec::new(); - self.inner.pending_shell_exits.retain(|_, pending| { + self.inner.pending_shell_exits.retain_sync(|_, pending| { exit_tasks.push(std::mem::replace(&mut pending.task, tokio::spawn(async {}))); false }); @@ -1055,7 +1055,7 @@ impl AgentOs { { let _terminal_lifecycle_guard = self.inner.terminal_lifecycle_lock.lock().await; let mut terminal_entries = Vec::new(); - self.inner.terminals.retain(|process_id, entry| { + self.inner.terminals.retain_sync(|process_id, entry| { terminal_entries.push(( process_id.clone(), std::mem::replace(&mut entry.exit_task, tokio::spawn(async {})), @@ -1148,9 +1148,9 @@ impl AgentOs { &self.inner.connection_id, &self.inner.session_id, ); - let _ = vm_host_functions().remove(&vm_route); - let _ = session_js_bridge_callbacks().remove(&session_key); - let _ = session_sqlite_callbacks().remove(&session_key); + let _ = vm_host_functions().remove_sync(&vm_route); + let _ = session_js_bridge_callbacks().remove_sync(&session_key); + let _ = session_sqlite_callbacks().remove_sync(&session_key); } let sidecar = self.inner.sidecar.clone(); let lease = if confirmed { @@ -1687,9 +1687,9 @@ fn session_sqlite_callbacks() -> &'static SccHashMap WireSidecarCallback { Ok(request) => { let callback = wire_ownership_session_key(transport_id, &ownership).and_then(|key| { - session_sqlite_callbacks().read(&key, |_, callback| callback.clone()) + session_sqlite_callbacks() + .read_sync(&key, |_, callback| callback.clone()) }); match callback { Some(callback) => match callback(request).await { @@ -1832,8 +1833,9 @@ async fn run_js_bridge_callback( }; } }; - let callback = wire_ownership_session_key(transport_id, ownership) - .and_then(|key| session_js_bridge_callbacks().read(&key, |_, callback| callback.clone())); + let callback = wire_ownership_session_key(transport_id, ownership).and_then(|key| { + session_js_bridge_callbacks().read_sync(&key, |_, callback| callback.clone()) + }); let Some(callback) = callback else { return wire::JsBridgeResultResponse { call_id, @@ -1929,7 +1931,7 @@ async fn run_host_callback( }; } }; - let registry = vm_host_functions().read(vm_route, |_, registry| registry.clone()); + let registry = vm_host_functions().read_sync(vm_route, |_, registry| registry.clone()); let Some(registry) = registry else { return wire::HostCallbackResultResponse { invocation_id: request.invocation_id, @@ -3136,7 +3138,7 @@ mod tests { let session_keys = ids.map(|id| sidecar_session_key(id, "connection", "session")); for i in 0..2 { assert!(vm_host_functions() - .insert( + .insert_sync( vm_keys[i].clone(), Arc::new(VmHostFunctionRegistry { host_functions: Vec::new(), @@ -3146,7 +3148,7 @@ mod tests { .is_ok()); let js: SidecarJsBridgeCallback = Arc::new(|_| Box::pin(async { Ok(None) })); assert!(session_js_bridge_callbacks() - .insert(session_keys[i].clone(), js) + .insert_sync(session_keys[i].clone(), js) .is_ok()); let sqlite: SidecarSqliteCallback = Arc::new(|_| { Box::pin(async { @@ -3156,14 +3158,14 @@ mod tests { }) }); assert!(session_sqlite_callbacks() - .insert(session_keys[i].clone(), sqlite) + .insert_sync(session_keys[i].clone(), sqlite) .is_ok()); } let has_routes = |i: usize| { [ - vm_host_functions().contains(&vm_keys[i]), - session_js_bridge_callbacks().contains(&session_keys[i]), - session_sqlite_callbacks().contains(&session_keys[i]), + vm_host_functions().contains_sync(&vm_keys[i]), + session_js_bridge_callbacks().contains_sync(&session_keys[i]), + session_sqlite_callbacks().contains_sync(&session_keys[i]), ] }; *vm.inner.disposal_test_request.lock() = Some(Arc::new(|| { @@ -3191,9 +3193,9 @@ mod tests { let second = vm.shutdown().await.unwrap_err().to_string(); peer.dispose().await.unwrap(); for i in 0..2 { - vm_host_functions().remove(&vm_keys[i]); - session_js_bridge_callbacks().remove(&session_keys[i]); - session_sqlite_callbacks().remove(&session_keys[i]); + vm_host_functions().remove_sync(&vm_keys[i]); + session_js_bridge_callbacks().remove_sync(&session_keys[i]); + session_sqlite_callbacks().remove_sync(&session_keys[i]); } assert_eq!(retained_before_exit, [true; 3]); assert!(cancelled.is_err()); @@ -3222,7 +3224,7 @@ mod tests { owner.attach_test_transport(transport.clone()).await; let key = vm_route_key(transport.instance_id(), "dropped-vm"); assert!(vm_host_functions() - .insert( + .insert_sync( key.clone(), Arc::new(VmHostFunctionRegistry { host_functions: Vec::new(), @@ -3240,9 +3242,9 @@ mod tests { let first = tokio::time::timeout(Duration::from_secs(2), owner.dispose()) .await .unwrap(); - let retained = vm_host_functions().contains(&key); + let retained = vm_host_functions().contains_sync(&key); let second = owner.dispose().await; - vm_host_functions().remove(&key); + vm_host_functions().remove_sync(&key); assert!(transport.has_confirmed_child_exit()); let first = first.unwrap_err().to_string(); assert!(first.contains("17")); @@ -3258,7 +3260,7 @@ mod tests { let (vm, transport, peer) = injected_disposal_vm().await; let peer_route = vm_route_key(transport.instance_id(), "peer-vm"); assert!(vm_host_functions() - .insert( + .insert_sync( peer_route.clone(), Arc::new(VmHostFunctionRegistry { host_functions: Vec::new(), @@ -3276,8 +3278,8 @@ mod tests { .unwrap(); let result = vm.shutdown().await; let confirmed = transport.has_confirmed_child_exit(); - let peer_route_retained = vm_host_functions().contains(&peer_route); - vm_host_functions().remove(&peer_route); + let peer_route_retained = vm_host_functions().contains_sync(&peer_route); + vm_host_functions().remove_sync(&peer_route); let count = vm.inner.sidecar.describe().active_vm_count; tokio::time::timeout(Duration::from_secs(2), transport.terminate_child()) .await @@ -3481,8 +3483,8 @@ mod tests { impl Drop for CallbackRouteTestCleanup { fn drop(&mut self) { for key in &self.0 { - super::session_js_bridge_callbacks().remove(key); - super::session_sqlite_callbacks().remove(key); + super::session_js_bridge_callbacks().remove_sync(key); + super::session_sqlite_callbacks().remove_sync(key); } } } @@ -3511,7 +3513,7 @@ mod tests { Box::pin(async move { Ok(Some(json!({ "owner": id }))) }) }); assert!(session_js_bridge_callbacks() - .insert(key.clone(), callback) + .insert_sync(key.clone(), callback) .is_ok()); } let request = || { @@ -3536,7 +3538,9 @@ mod tests { assert_eq!(response.result, Some(json!({ "owner": id }).to_string())); } } - assert!(session_js_bridge_callbacks().remove(&keys[0]).is_some()); + assert!(session_js_bridge_callbacks() + .remove_sync(&keys[0]) + .is_some()); for owner in &owners { let response = js_bridge_call_callback(transports[0])(request(), owner.clone()) .await @@ -3608,7 +3612,7 @@ mod tests { Box::pin(async move { Ok(response) }) }); assert!(session_sqlite_callbacks() - .insert(key.clone(), callback) + .insert_sync(key.clone(), callback) .is_ok()); } let request = || { @@ -3632,7 +3636,7 @@ mod tests { assert_eq!(decode(response), answer(id)); } } - assert!(session_sqlite_callbacks().remove(&keys[0]).is_some()); + assert!(session_sqlite_callbacks().remove_sync(&keys[0]).is_some()); for owner in &owners { let response = sqlite_callback_callback(transports[0])(request(), owner.clone()) .await @@ -3710,7 +3714,7 @@ mod tests { (String::from("math:allowed"), allowed.clone()), (String::from("math:danger"), danger.clone()), ]); - let _ = vm_host_functions().insert( + let _ = vm_host_functions().insert_sync( vm_id.clone(), Arc::new(VmHostFunctionRegistry { host_functions: vec![ResolvedHostFunctions { @@ -3737,7 +3741,7 @@ mod tests { }, ) .await; - vm_host_functions().remove(&vm_id); + vm_host_functions().remove_sync(&vm_id); assert_eq!(response.error, None); assert_eq!(response.result.as_deref(), Some("\"allowed\"")); diff --git a/crates/client/src/config.rs b/crates/client/src/config.rs index 399ee6c101..f858bba69f 100644 --- a/crates/client/src/config.rs +++ b/crates/client/src/config.rs @@ -1333,7 +1333,7 @@ impl TimerScheduleDriver { let parsed = match crate::cron::parse_schedule(&entry.schedule) { Ok(parsed) => parsed, Err(_) => { - let _ = timers.remove(&entry.id); + let _ = timers.remove_sync(&entry.id); return; } }; @@ -1342,7 +1342,7 @@ impl TimerScheduleDriver { Some(next) => next, None => { // No upcoming run (one-shot in the past, or exhausted cron). - let _ = timers.remove(&entry.id); + let _ = timers.remove_sync(&entry.id); return; } }; @@ -1362,10 +1362,10 @@ impl TimerScheduleDriver { // The driver is fire-and-forget; errors are the caller's responsibility. (entry.callback)().await; - if is_cron && timers.contains(&entry.id) { + if is_cron && timers.contains_sync(&entry.id) { Self::schedule_next(Arc::clone(&timers), entry, cancel); } else { - let _ = timers.remove(&entry.id); + let _ = timers.remove_sync(&entry.id); } }); } @@ -1376,10 +1376,10 @@ impl ScheduleDriver for TimerScheduleDriver { let id = entry.id.clone(); let cancel = tokio_util::sync::CancellationToken::new(); // Replace any existing timer for this id, cancelling it first. - if let Some((_, old)) = self.timers.remove(&id) { + if let Some((_, old)) = self.timers.remove_sync(&id) { old.cancel(); } - let _ = self.timers.insert(id.clone(), cancel.clone()); + let _ = self.timers.insert_sync(id.clone(), cancel.clone()); Self::schedule_next(Arc::clone(&self.timers), entry, cancel); @@ -1387,13 +1387,16 @@ impl ScheduleDriver for TimerScheduleDriver { } fn cancel(&self, handle: &ScheduleHandle) { - if let Some((_, cancel)) = self.timers.remove(&handle.id) { + if let Some((_, cancel)) = self.timers.remove_sync(&handle.id) { cancel.cancel(); } } fn dispose(&self) { - self.timers.scan(|_, cancel| cancel.cancel()); - self.timers.clear(); + self.timers.iter_sync(|_, cancel| { + cancel.cancel(); + true + }); + self.timers.clear_sync(); } } diff --git a/crates/client/src/cron.rs b/crates/client/src/cron.rs index ed8d4236ad..d70685cde4 100644 --- a/crates/client/src/cron.rs +++ b/crates/client/src/cron.rs @@ -197,7 +197,7 @@ impl CronManager { /// and remove the job from the registry. pub(crate) fn cancel_job(&self, id: &str) { let _guard = self.schedule_lock.lock(); - if let Some((_, state)) = self.jobs.remove(id) { + if let Some((_, state)) = self.jobs.remove_sync(id) { self.driver.cancel(&state.handle); } } @@ -208,10 +208,12 @@ impl CronManager { /// registry, then call `this.driver.dispose()` to tear down all driver-held timer state. pub(crate) fn dispose(&self) { let _guard = self.schedule_lock.lock(); - self.jobs.scan(|_, state| { + self.jobs.iter_sync(|_, state| { self.driver.cancel(&state.handle); + + true }); - self.jobs.clear(); + self.jobs.clear_sync(); self.driver.dispose(); } } @@ -241,7 +243,7 @@ async fn execute_job_inner(manager: Arc, vm: AgentOs, id: String) { { let mut should_return = false; let mut should_queue = false; - manager.jobs.read(id, |_, state| { + manager.jobs.read_sync(id, |_, state| { if state.running.load(Ordering::SeqCst) { match state.overlap { CronOverlap::Allow => {} @@ -254,7 +256,7 @@ async fn execute_job_inner(manager: Arc, vm: AgentOs, id: String) { return; } if should_queue { - manager.jobs.read(id, |_, state| { + manager.jobs.read_sync(id, |_, state| { state.queued.store(true, Ordering::SeqCst); }); return; @@ -262,7 +264,7 @@ async fn execute_job_inner(manager: Arc, vm: AgentOs, id: String) { } // Mark running, record this run, and snapshot the action to dispatch. - let action = match manager.jobs.read(id, |_, state| { + let action = match manager.jobs.read_sync(id, |_, state| { state.running.store(true, Ordering::SeqCst); *state.last_run.lock() = Some(Utc::now()); state.run_count.fetch_add(1, Ordering::SeqCst); @@ -301,7 +303,7 @@ async fn execute_job_inner(manager: Arc, vm: AgentOs, id: String) { // Clear running, recompute the next run, and drain a queued run if one was deferred. let mut run_queued = false; - manager.jobs.read(id, |_, state| { + manager.jobs.read_sync(id, |_, state| { state.running.store(false, Ordering::SeqCst); *state.next_run.lock() = compute_next_time(&state.schedule, Utc::now()); if state.queued.swap(false, Ordering::SeqCst) { @@ -1131,7 +1133,7 @@ impl AgentOs { /// Snapshot all cron jobs. Mirrors TS `CronManager.list`. pub fn list_cron_jobs(&self) -> Vec { let mut result = Vec::new(); - self.cron().jobs.scan(|id, state| { + self.cron().jobs.iter_sync(|id, state| { result.push(CronJobInfo { id: id.clone(), schedule: state.schedule.clone(), @@ -1150,6 +1152,8 @@ impl AgentOs { run_count: state.run_count.load(Ordering::SeqCst), running: state.running.load(Ordering::SeqCst), }); + + true }); result } @@ -1167,7 +1171,7 @@ impl AgentOs { } fn ensure_cron_capacity(cron: &CronManager, id: &str) -> std::result::Result<(), ClientError> { - if cron.jobs.contains(id) || cron.jobs.len() < crate::CRON_JOB_LIMIT { + if cron.jobs.contains_sync(id) || cron.jobs.len() < crate::CRON_JOB_LIMIT { return Ok(()); } @@ -1192,7 +1196,7 @@ fn register_cron_job( // If replacing an existing id, cancel the old driver-armed timer before scheduling the new one. // The default timer driver's handles are id-based, so cancelling after the new schedule would // cancel the replacement. - if let Some((_, old)) = cron.jobs.remove(&id) { + if let Some((_, old)) = cron.jobs.remove_sync(&id) { cron.driver.cancel(&old.handle); } @@ -1214,7 +1218,7 @@ fn register_cron_job( handle, }; - let _ = cron.jobs.insert(id.clone(), state); + let _ = cron.jobs.insert_sync(id.clone(), state); Ok(CronJobHandle { id, @@ -1272,7 +1276,10 @@ mod tests { for index in 0..CRON_JOB_LIMIT { let id = format!("job-{index}"); assert!( - manager.jobs.insert(id.clone(), dummy_state(id)).is_ok(), + manager + .jobs + .insert_sync(id.clone(), dummy_state(id)) + .is_ok(), "seed cron job" ); } diff --git a/crates/client/src/process.rs b/crates/client/src/process.rs index d4e4fc0cca..5bc5aa856d 100644 --- a/crates/client/src/process.rs +++ b/crates/client/src/process.rs @@ -1230,7 +1230,7 @@ impl AgentOs { let mut rx = self .inner() .processes - .read(&pid, |_, entry| entry.output_tx.subscribe()) + .read_sync(&pid, |_, entry| entry.output_tx.subscribe()) .ok_or(ClientError::ProcessNotFound(pid))?; let task = tokio::spawn(async move { loop { @@ -1262,7 +1262,7 @@ impl AgentOs { let mut rx = self .inner() .processes - .read(&pid, |_, entry| entry.exit_tx.subscribe()) + .read_sync(&pid, |_, entry| entry.exit_tx.subscribe()) .ok_or(ClientError::ProcessNotFound(pid))?; // Already-exited branch: fire immediately + synchronously, return a no-op unsubscribe. @@ -1308,7 +1308,7 @@ impl AgentOs { let (rx, process_id) = self .inner() .processes - .read(&pid, |_, entry| { + .read_sync(&pid, |_, entry| { (entry.exit_tx.subscribe(), entry.process_id.clone()) }) .ok_or(ClientError::ProcessNotFound(pid))?; @@ -1334,7 +1334,7 @@ impl AgentOs { ) = self .inner() .processes - .read(&pid, |_, entry| { + .read_sync(&pid, |_, entry| { ( entry.process_id.clone(), entry.retain_output, @@ -1495,7 +1495,7 @@ impl AgentOs { let observed_exit = self .inner() .processes - .read(&pid, |_, entry| { + .read_sync(&pid, |_, entry| { if entry.process_id != process_id { return None; } @@ -1530,7 +1530,7 @@ impl AgentOs { /// List SDK-spawned processes only. `running = exit_code.is_none()`. pub fn list_processes(&self) -> Vec { let mut out = Vec::new(); - self.inner().processes.scan(|pid, entry| { + self.inner().processes.iter_sync(|pid, entry| { let observed = entry.exit_tx.borrow(); let exit_code = observed.exit_code(); let exit_signal = observed.exit(*pid).and_then(|exit| exit.exit_signal); @@ -1543,6 +1543,8 @@ impl AgentOs { exit_signal, started_at: entry.started_at, }); + + true }); out } @@ -1595,7 +1597,7 @@ impl AgentOs { } let mut tracked_by_process_id: BTreeMap = BTreeMap::new(); let mut display_pid_by_kernel_pid: BTreeMap = BTreeMap::new(); - self.inner().processes.scan(|display_pid, entry| { + self.inner().processes.iter_sync(|display_pid, entry| { let observed = entry.exit_tx.borrow(); let exit_code = observed.exit_code(); let exit_signal = observed @@ -1614,6 +1616,8 @@ impl AgentOs { args: entry.args.clone(), }, ); + + true }); let now_ms = epoch_ms_now(); @@ -1630,7 +1634,7 @@ impl AgentOs { let observed = self .inner() .processes - .read(&tracked.display_pid, |_, process| { + .read_sync(&tracked.display_pid, |_, process| { if process.process_id != entry.process_id { return None; } @@ -1728,9 +1732,9 @@ impl AgentOs { // Tracked processes not yet present in the snapshot (the spawn `Execute` has not surfaced in // the kernel table yet). TS fills these with `ppid:0, pgid/sid = pid`. - self.inner().processes.scan(|display_pid, entry| { + self.inner().processes.iter_sync(|display_pid, entry| { if seen_process_ids.contains(&entry.process_id) { - return; + return true; } let observed = entry.exit_tx.borrow(); let exit_code = observed.exit_code(); @@ -1766,6 +1770,8 @@ impl AgentOs { exit_time, }, }); + + true }); out.sort_by_key(|record| record.info.pid); @@ -1779,14 +1785,14 @@ impl AgentOs { if let Some(existing) = self .inner() .observed_process_start_times - .read(process_key, |_, value| *value) + .read_sync(process_key, |_, value| *value) { return existing; } let _ = self .inner() .observed_process_start_times - .insert(process_key.to_owned(), now_ms); + .insert_sync(process_key.to_owned(), now_ms); prune_string_f64_map( &self.inner().observed_process_start_times, OBSERVED_PROCESS_TIME_LIMIT, @@ -1795,7 +1801,7 @@ impl AgentOs { // timestamp. self.inner() .observed_process_start_times - .read(process_key, |_, value| *value) + .read_sync(process_key, |_, value| *value) .unwrap_or(now_ms) } @@ -1805,21 +1811,21 @@ impl AgentOs { if let Some(existing) = self .inner() .observed_process_exit_times - .read(process_id, |_, value| *value) + .read_sync(process_id, |_, value| *value) { return existing; } let _ = self .inner() .observed_process_exit_times - .insert(process_id.to_owned(), now_ms); + .insert_sync(process_id.to_owned(), now_ms); prune_string_f64_map( &self.inner().observed_process_exit_times, OBSERVED_PROCESS_TIME_LIMIT, ); self.inner() .observed_process_exit_times - .read(process_id, |_, value| *value) + .read_sync(process_id, |_, value| *value) .unwrap_or(now_ms) } @@ -1833,7 +1839,7 @@ impl AgentOs { pub fn get_process(&self, pid: u32) -> std::result::Result { self.inner() .processes - .read(&pid, |pid, entry| { + .read_sync(&pid, |pid, entry| { let observed = entry.exit_tx.borrow(); let exit_code = observed.exit_code(); let exit_signal = observed.exit(*pid).and_then(|exit| exit.exit_signal); @@ -1869,7 +1875,7 @@ impl AgentOs { let (process_id, already_exited) = self .inner() .processes - .read(&pid, |_, entry| { + .read_sync(&pid, |_, entry| { ( entry.process_id.clone(), entry.exit_tx.borrow().reclaimable(), @@ -1939,7 +1945,7 @@ impl AgentOs { ) -> std::result::Result, ClientError> { self.inner() .processes - .read(&pid, |_, entry| { + .read_sync(&pid, |_, entry| { entry .exit_tx .borrow() @@ -1953,7 +1959,7 @@ impl AgentOs { fn live_process_id(&self, pid: u32) -> std::result::Result, ClientError> { self.inner() .processes - .read(&pid, |_, entry| { + .read_sync(&pid, |_, entry| { entry .exit_tx .borrow() @@ -1968,7 +1974,7 @@ impl AgentOs { fn lookup_process_id(&self, pid: u32) -> std::result::Result { self.inner() .processes - .read(&pid, |_, entry| entry.process_id.clone()) + .read_sync(&pid, |_, entry| entry.process_id.clone()) .ok_or(ClientError::ProcessNotFound(pid)) } @@ -1976,7 +1982,7 @@ impl AgentOs { let (process_id, mut started, mut outcome) = self .inner() .processes - .read(&pid, |_, entry| { + .read_sync(&pid, |_, entry| { ( entry.process_id.clone(), entry.kernel_pid.subscribe(), @@ -2066,7 +2072,7 @@ impl AgentOs { let (process_id, already_exited) = self .inner() .processes - .read(&pid, |_, entry| { + .read_sync(&pid, |_, entry| { ( entry.process_id.clone(), entry.exit_tx.borrow().reclaimable(), @@ -2134,8 +2140,10 @@ impl AgentOs { fn process_registry_len_locked(&self) -> usize { let mut count = 0usize; - self.inner().processes.scan(|_, _| { + self.inner().processes.iter_sync(|_, _| { count += 1; + + true }); count } @@ -2178,8 +2186,10 @@ impl AgentOs { fn prune_exited_processes_locked(&self, reserve_slots: usize) { let mut entries = Vec::new(); - self.inner().processes.scan(|pid, entry| { + self.inner().processes.iter_sync(|pid, entry| { entries.push((*pid, entry.exit_tx.borrow().reclaimable(), entry.started_at)); + + true }); let target_len = PROCESS_REGISTRY_LIMIT.saturating_sub(reserve_slots); if entries.len() <= target_len { @@ -2192,20 +2202,23 @@ impl AgentOs { } fn remove_process_tracking_locked(&self, pid: u32) { - if let Some((_, entry)) = self.inner().processes.remove(&pid) { + if let Some((_, entry)) = self.inner().processes.remove_sync(&pid) { let _time_guard = self.inner().observed_process_time_lock.lock(); let _ = self .inner() .observed_process_exit_times - .remove(&entry.process_id); + .remove_sync(&entry.process_id); let fallback_start_key = format!("{}:{pid}", entry.process_id); let _ = self .inner() .observed_process_start_times - .remove(&fallback_start_key); + .remove_sync(&fallback_start_key); if let Some(kernel_pid) = *entry.kernel_pid.borrow() { let start_key = format!("{}:{kernel_pid}", entry.process_id); - let _ = self.inner().observed_process_start_times.remove(&start_key); + let _ = self + .inner() + .observed_process_start_times + .remove_sync(&start_key); } } } @@ -2356,11 +2369,15 @@ impl ProcessRegistryReservation { ) -> std::result::Result<(), ClientError> { let client = self.client.clone(); let _guard = client.inner().process_registry_lock.lock(); - client.inner().processes.insert(pid, entry).map_err(|_| { - ClientError::Sidecar(format!( - "process registry already contains public pid {pid}; retry the spawn" - )) - })?; + client + .inner() + .processes + .insert_sync(pid, entry) + .map_err(|_| { + ClientError::Sidecar(format!( + "process registry already contains public pid {pid}; retry the spawn" + )) + })?; client .inner() .pending_process_registrations @@ -2583,8 +2600,10 @@ fn exited_pids_to_prune(mut entries: Vec<(u32, bool, i64)>, target_len: usize) - fn prune_string_f64_map(map: &SccHashMap, limit: usize) { let mut keys = Vec::new(); - map.scan(|key, _| { + map.iter_sync(|key, _| { keys.push(key.clone()); + + true }); if keys.len() <= limit { return; @@ -2592,7 +2611,7 @@ fn prune_string_f64_map(map: &SccHashMap, limit: usize) { let remove_count = keys.len() - limit; keys.sort(); for key in keys.into_iter().take(remove_count) { - let _ = map.remove(&key); + let _ = map.remove_sync(&key); } } @@ -2652,7 +2671,7 @@ pub(crate) fn drain_process_output_tasks( ) -> Vec { let mut tasks = Vec::new(); let mut pending_language = Vec::new(); - processes.retain(|_, entry| { + processes.retain_sync(|_, entry| { let pending = matches!(*entry.exit_tx.borrow(), ProcessOutcome::Pending); match entry.execution_id.clone() { Some(execution_id) if pending => pending_language.push(PendingLanguageExit { @@ -3125,7 +3144,7 @@ mod tests { started_at: 0, stdin: super::ProcessStdin::channel().0, }; - let _ = processes.insert(1, entry); + let _ = processes.insert_sync(1, entry); assert!(!abort_handle.is_finished(), "task should start alive"); @@ -3217,7 +3236,7 @@ mod tests { // And it is the handle `drain_process_output_tasks` aborts on teardown. let processes: SccHashMap = SccHashMap::new(); - let _ = processes.insert(1, entry); + let _ = processes.insert_sync(1, entry); drain_process_output_tasks(&processes); assert!(processes.is_empty(), "registry must be cleared on drain"); } @@ -3318,15 +3337,15 @@ mod tests { #[test] fn observed_time_pruning_enforces_limit() { let map = SccHashMap::new(); - let _ = map.insert("b".to_string(), 2.0); - let _ = map.insert("a".to_string(), 1.0); - let _ = map.insert("c".to_string(), 3.0); + let _ = map.insert_sync("b".to_string(), 2.0); + let _ = map.insert_sync("a".to_string(), 1.0); + let _ = map.insert_sync("c".to_string(), 3.0); prune_string_f64_map(&map, 2); - assert!(map.read("a", |_, _| ()).is_none()); - assert!(map.read("b", |_, _| ()).is_some()); - assert!(map.read("c", |_, _| ()).is_some()); + assert!(map.read_sync("a", |_, _| ()).is_none()); + assert!(map.read_sync("b", |_, _| ()).is_some()); + assert!(map.read_sync("c", |_, _| ()).is_some()); } #[tokio::test] async fn complete_wait_status_keeps_observed_signal_distinct_from_exit_code() { diff --git a/crates/client/src/shell.rs b/crates/client/src/shell.rs index 31a7997595..4260075ea1 100644 --- a/crates/client/src/shell.rs +++ b/crates/client/src/shell.rs @@ -306,7 +306,7 @@ impl AgentOs { } pub(crate) fn finish_terminal(&self, process_id: &str) { - if self.inner().terminals.remove(process_id).is_some() { + if self.inner().terminals.remove_sync(process_id).is_some() { release_counter(&self.inner().terminal_count); } } @@ -416,7 +416,7 @@ impl AgentOs { exit_tx: exit_tx.clone(), }; // `insert` fails only if the key already exists; the monotonic counter guarantees it cannot. - let _ = inner.shells.insert(shell_id.clone(), entry); + let _ = inner.shells.insert_sync(shell_id.clone(), entry); let command = options .command @@ -489,8 +489,8 @@ impl AgentOs { ); spawned_tx.send_replace(Some(Err(error.clone()))); publish_shell_outcome(&exit_tx, Err(error)); - agent.inner().shells.remove(&exit_shell_id); - agent.inner().pending_shell_exits.remove(&exit_key); + agent.inner().shells.remove_sync(&exit_shell_id); + agent.inner().pending_shell_exits.remove_sync(&exit_key); return; } }; @@ -500,7 +500,7 @@ impl AgentOs { agent .inner() .shells - .update(&exit_shell_id, |_, existing| existing.pid = kernel_pid); + .update_sync(&exit_shell_id, |_, existing| existing.pid = kernel_pid); // send_replace, not send: `watch::Sender::send` REFUSES to store the // value while no receiver exists (and the initial receiver is dropped // at channel creation), which left the spawn gate permanently false @@ -596,14 +596,17 @@ impl AgentOs { // The `.finally` equivalent: remove from both the tracking set and the shells map (only // if it is still our entry, matching the TS identity check). - agent.inner().pending_shell_exits.remove(&exit_key); - agent.inner().shells.remove_if(&exit_shell_id, |existing| { - existing.process_id == route_process_id - }); + agent.inner().pending_shell_exits.remove_sync(&exit_key); + agent + .inner() + .shells + .remove_if_sync(&exit_shell_id, |existing| { + existing.process_id == route_process_id + }); // remove_if takes `&mut V`; the comparison only reads, which is fine. }); - let _ = inner.pending_shell_exits.insert( + let _ = inner.pending_shell_exits.insert_sync( shell_id.clone(), PendingShellExit { task: handle, @@ -769,7 +772,7 @@ impl AgentOs { match self .inner() .terminals - .insert(process_id.clone(), TerminalEntry { exit_task }) + .insert_sync(process_id.clone(), TerminalEntry { exit_task }) { Ok(()) => {} Err((_, entry)) => { @@ -835,7 +838,7 @@ impl AgentOs { /// List actor-addressable shells currently retained by Core. pub fn list_shells(&self) -> Vec { let mut shells = Vec::new(); - self.inner().shells.scan(|shell_id, entry| { + self.inner().shells.iter_sync(|shell_id, entry| { let outcome = entry.exit_tx.borrow(); let exit = outcome .as_ref() @@ -848,6 +851,8 @@ impl AgentOs { exit_code: exit.map(|exit| exit.exit_code), exit_signal: exit.and_then(|exit| exit.exit_signal), }); + + true }); shells.sort_by(|left, right| left.shell_id.cmp(&right.shell_id)); shells @@ -880,12 +885,12 @@ impl AgentOs { let spawned = self .inner() .shells - .read(shell_id, |_, entry| entry.spawned_tx.subscribe()); + .read_sync(shell_id, |_, entry| entry.spawned_tx.subscribe()); if let Some(spawned) = spawned { wait_for_spawn(spawned).await?; } // Re-read after admission so the snapshot carries the confirmed kernel pid. - let live = self.inner().shells.read(shell_id, |_, entry| { + let live = self.inner().shells.read_sync(shell_id, |_, entry| { ( entry.pid, entry.process_id.clone(), @@ -943,7 +948,7 @@ impl AgentOs { }; // Retain the same pair before waking any live waiter. self.retain_shell_outcome(shell_id, &process_id, pid, Ok(exit)); - self.inner().shells.read(shell_id, |_, entry| { + self.inner().shells.read_sync(shell_id, |_, entry| { if entry.process_id == process_id { publish_shell_outcome(&entry.exit_tx, Ok(exit)); } @@ -1015,7 +1020,7 @@ impl AgentOs { let mut rx = self .inner() .shells - .read(shell_id, |_, entry| entry.data_tx.subscribe()) + .read_sync(shell_id, |_, entry| entry.data_tx.subscribe()) .ok_or_else(|| ClientError::ShellNotFound(shell_id.to_string()))?; let shell_id = shell_id.to_string(); let task = tokio::spawn(async move { @@ -1042,7 +1047,7 @@ impl AgentOs { let rx = self .inner() .shells - .read(shell_id, |_, entry| entry.event_tx.subscribe()); + .read_sync(shell_id, |_, entry| entry.event_tx.subscribe()); let Some(mut rx) = rx else { // A fast exit can race actor event registration. The final output // is available through replay; no future hints will be emitted. @@ -1078,7 +1083,7 @@ impl AgentOs { let mut rx = self .inner() .shells - .read(shell_id, |_, entry| entry.stderr_tx.subscribe()) + .read_sync(shell_id, |_, entry| entry.stderr_tx.subscribe()) .ok_or_else(|| ClientError::ShellNotFound(shell_id.to_string()))?; let shell_id = shell_id.to_string(); let task = tokio::spawn(async move { @@ -1104,7 +1109,7 @@ impl AgentOs { let rx = self .inner() .shells - .read(shell_id, |_, entry| entry.exit_tx.subscribe()); + .read_sync(shell_id, |_, entry| entry.exit_tx.subscribe()); let Some(mut rx) = rx else { let exit = self.retained_shell_outcome(shell_id)?; handler(ShellExit { @@ -1232,11 +1237,11 @@ impl AgentOs { let exit_rx = self .inner() .shells - .read(shell_id, |_, entry| entry.exit_tx.subscribe()) + .read_sync(shell_id, |_, entry| entry.exit_tx.subscribe()) .or_else(|| { self.inner() .pending_shell_exits - .read(shell_id, |_, pending| pending.exit_tx.subscribe()) + .read_sync(shell_id, |_, pending| pending.exit_tx.subscribe()) }); let Some(exit_rx) = exit_rx else { // Entry already dropped: fall back to the recorded exit code (TS retention behavior). @@ -1255,7 +1260,7 @@ impl AgentOs { // Immediate map delete, exactly like the TS `_shells.delete(shellId)`; the pending-exit task // remains tracked so `dispose` still drains it (two-phase teardown). - self.inner().shells.remove(shell_id); + self.inner().shells.remove_sync(shell_id); // Fire-and-forget kill (SIGTERM) after the spawn lands so the kill addresses a live process. let agent = self.clone(); @@ -1296,7 +1301,7 @@ impl AgentOs { .await?; match response { wire::ResponsePayload::ProcessKilledResponse(_) => { - self.inner().shells.remove(shell_id); + self.inner().shells.remove_sync(shell_id); Ok(()) } wire::ResponsePayload::RejectedResponse(rejected) => Err(rejected_to_error(rejected)), @@ -1314,7 +1319,7 @@ impl AgentOs { ) -> std::result::Result<(String, ShellSpawnReceiver), ClientError> { self.inner() .shells - .read(shell_id, |_, entry| { + .read_sync(shell_id, |_, entry| { (entry.process_id.clone(), entry.spawned_tx.subscribe()) }) .ok_or_else(|| { diff --git a/crates/client/src/sidecar.rs b/crates/client/src/sidecar.rs index 3400f9c006..9a5914cea8 100644 --- a/crates/client/src/sidecar.rs +++ b/crates/client/src/sidecar.rs @@ -651,9 +651,10 @@ impl AgentOsSidecar { #[cfg(any(feature = "actor-internals", test))] let mut _admission = _admission; let self_ptr = self as *const AgentOsSidecar; - let _ = shared_sidecars().remove_if(&(self.runtime_id, pool.to_owned()), |cached| { - std::ptr::eq(Arc::as_ptr(cached), self_ptr) - }); + let _ = shared_sidecars() + .remove_if_sync(&(self.runtime_id, pool.to_owned()), |cached| { + std::ptr::eq(Arc::as_ptr(cached), self_ptr) + }); #[cfg(any(feature = "actor-internals", test))] if let Some(runtime) = self.runtime_id { _admission.forget(runtime, self); @@ -1082,21 +1083,25 @@ fn shared_sidecar_pool_lock() -> &'static parking_lot::Mutex>) -> usize { let mut len = 0; - cache.scan(|_, _| { + cache.iter_sync(|_, _| { len += 1; + + true }); len } fn prune_disposed_shared_sidecars(cache: &SccHashMap>) { let mut disposed_pools = Vec::new(); - cache.scan(|pool, sidecar| { + cache.iter_sync(|pool, sidecar| { if sidecar.describe().state == SidecarState::Disposed { disposed_pools.push(pool.clone()); } + + true }); for pool in disposed_pools { - let _ = cache.remove_if(&pool, |sidecar| { + let _ = cache.remove_if_sync(&pool, |sidecar| { sidecar.describe().state == SidecarState::Disposed }); } @@ -1182,7 +1187,7 @@ impl AgentOs { let cache = shared_sidecars(); // Reap an unusable generation before replacing its cached owner. Do not // hold the synchronous pool lock across process teardown. - if let Some(existing) = cache.read(&key, |_, sidecar| sidecar.clone()) { + if let Some(existing) = cache.read_sync(&key, |_, sidecar| sidecar.clone()) { if existing.process_lost() { if let Err(error) = existing.dispose().await { if existing.current_state() != SidecarState::Disposed { @@ -1199,7 +1204,7 @@ impl AgentOs { _admission.admit(runtime_id)?; // Fast path: reuse a cached, non-disposed sidecar for this pool. - if let Some(existing) = cache.read(&key, |_, sidecar| sidecar.clone()) { + if let Some(existing) = cache.read_sync(&key, |_, sidecar| sidecar.clone()) { if existing.describe().state != SidecarState::Disposed { if existing.runtime_args != runtime_args { return Err(ClientError::Sidecar( @@ -1234,7 +1239,7 @@ impl AgentOs { // Insert atomically, replacing a stale (disposed) entry but yielding to a live one that a // concurrent caller may have just installed. let cache_len = shared_sidecar_pool_len(cache); - match cache.entry(key) { + match cache.entry_sync(key) { scc::hash_map::Entry::Occupied(mut occupied) => { if occupied.get().describe().state == SidecarState::Disposed { #[cfg(any(feature = "actor-internals", test))] @@ -1534,11 +1539,11 @@ mod tests { #[test] fn prune_disposed_shared_sidecars_keeps_live_entries() { let cache = SccHashMap::new(); - let _ = cache.insert( + let _ = cache.insert_sync( (None, "live".to_string()), shared("live", SidecarState::Ready), ); - let _ = cache.insert( + let _ = cache.insert_sync( (None, "disposed".to_string()), shared("disposed", SidecarState::Disposed), ); @@ -1546,9 +1551,11 @@ mod tests { prune_disposed_shared_sidecars(&cache); assert_eq!(shared_sidecar_pool_len(&cache), 1); - assert!(cache.read(&(None, "live".to_string()), |_, _| ()).is_some()); assert!(cache - .read(&(None, "disposed".to_string()), |_, _| ()) + .read_sync(&(None, "live".to_string()), |_, _| ()) + .is_some()); + assert!(cache + .read_sync(&(None, "disposed".to_string()), |_, _| ()) .is_none()); } @@ -1557,7 +1564,7 @@ mod tests { let cache = SccHashMap::new(); for index in 0..SHARED_SIDECAR_POOL_LIMIT { let pool = format!("pool-{index}"); - let _ = cache.insert((None, pool.clone()), shared(&pool, SidecarState::Ready)); + let _ = cache.insert_sync((None, pool.clone()), shared(&pool, SidecarState::Ready)); } let error = @@ -1581,7 +1588,7 @@ mod tests { } else { SidecarState::Ready }; - let _ = cache.insert((None, pool.clone()), shared(&pool, state)); + let _ = cache.insert_sync((None, pool.clone()), shared(&pool, state)); } prune_disposed_shared_sidecars(&cache); @@ -1617,24 +1624,24 @@ mod tests { let cache = shared_sidecars(); let _guard = shared_sidecar_pool_lock().lock(); - let _ = cache.insert((None, pool.clone()), replacement.clone()); + let _ = cache.insert_sync((None, pool.clone()), replacement.clone()); old.state .store(SidecarState::Disposing.as_u8(), Ordering::SeqCst); old.active_vm_count.store(0, Ordering::SeqCst); old.state .store(SidecarState::Disposed.as_u8(), Ordering::SeqCst); let old_ptr = Arc::as_ptr(&old); - let _ = cache.remove_if(&(None, pool.clone()), |cached| { + let _ = cache.remove_if_sync(&(None, pool.clone()), |cached| { std::ptr::eq(Arc::as_ptr(cached), old_ptr) }); let cached = cache - .read(&(None, pool.clone()), |_, cached| cached.clone()) + .read_sync(&(None, pool.clone()), |_, cached| cached.clone()) .expect("replacement should remain cached"); assert!(Arc::ptr_eq(&cached, &replacement)); let replacement_ptr = Arc::as_ptr(&replacement); - let _ = cache.remove_if(&(None, pool.clone()), |cached| { + let _ = cache.remove_if_sync(&(None, pool.clone()), |cached| { std::ptr::eq(Arc::as_ptr(cached), replacement_ptr) }); } @@ -1649,7 +1656,7 @@ mod tests { // Cache replacement must not discard ownership of a not-yet-reaped child. // No native child is started in this deterministic ownership test. shared_sidecars() - .remove(&(Some(runtime), pool.clone())) + .remove_sync(&(Some(runtime), pool.clone())) .unwrap(); let current = AgentOs::get_shared_sidecar(Some(pool), None).await.unwrap(); assert!(!Arc::ptr_eq(&old, ¤t)); @@ -1784,7 +1791,9 @@ mod tests { .await .unwrap(); let weak = Arc::downgrade(&old); - shared_sidecars().remove(&(Some(runtime), pool)).unwrap(); + shared_sidecars() + .remove_sync(&(Some(runtime), pool)) + .unwrap(); drop(old); let retained = weak .upgrade() diff --git a/crates/client/tests/support/terminal_pending_exit.rs b/crates/client/tests/support/terminal_pending_exit.rs index fbc4907344..a5d4e17b37 100644 --- a/crates/client/tests/support/terminal_pending_exit.rs +++ b/crates/client/tests/support/terminal_pending_exit.rs @@ -10,7 +10,7 @@ async fn terminal_close_preserves_pending_wait_without_admitting_writes() { let (spawned, exit, process_id) = vm .inner .shells - .read(&shell.shell_id, |_, entry| { + .read_sync(&shell.shell_id, |_, entry| { ( entry.spawned_tx.clone(), entry.exit_tx.clone(), @@ -20,7 +20,7 @@ async fn terminal_close_preserves_pending_wait_without_admitting_writes() { .unwrap(); vm.inner .shells - .update(&shell.shell_id, |_, entry| entry.pid = 42); + .update_sync(&shell.shell_id, |_, entry| entry.pid = 42); spawned.send_replace(Some(Ok(()))); vm.close_shell(&shell.shell_id).unwrap(); assert_eq!( diff --git a/crates/client/tests/support/terminal_replay_admission.rs b/crates/client/tests/support/terminal_replay_admission.rs index 211bcbc7c7..174473a322 100644 --- a/crates/client/tests/support/terminal_replay_admission.rs +++ b/crates/client/tests/support/terminal_replay_admission.rs @@ -11,7 +11,7 @@ async fn terminal_snapshot_waits_for_launch_and_preserves_typed_failure() { assert!(vm .inner .shells - .insert( + .insert_sync( "shell-gate".into(), ShellEntry { pid: 0, @@ -38,7 +38,7 @@ async fn terminal_snapshot_waits_for_launch_and_preserves_typed_failure() { let result = tokio::time::timeout(Duration::from_millis(100), &mut snapshot).await; (was_pending, result) }; - vm.inner.shells.remove("shell-gate"); + vm.inner.shells.remove_sync("shell-gate"); tokio::time::timeout(Duration::from_secs(2), transport.terminate_child()) .await .unwrap() diff --git a/crates/sidecar-client/Cargo.toml b/crates/sidecar-client/Cargo.toml index 239b7480d2..cf7833bc1a 100644 --- a/crates/sidecar-client/Cargo.toml +++ b/crates/sidecar-client/Cargo.toml @@ -11,7 +11,7 @@ agentos-sidecar-protocol = { workspace = true } futures = "0.3" parking_lot = "0.12" -scc = "2" +scc = "3.8.4" thiserror = "2" tokio = { version = "1", features = ["io-util", "macros", "net", "process", "rt", "sync", "time"] } tracing = "0.1" diff --git a/crates/sidecar-client/src/transport.rs b/crates/sidecar-client/src/transport.rs index f5bea83b91..9a1c2770ea 100644 --- a/crates/sidecar-client/src/transport.rs +++ b/crates/sidecar-client/src/transport.rs @@ -372,7 +372,7 @@ impl SidecarTransport { let _pending_guard = PendingRequestGuard::new(self, request_id); if self.request_writer_tx.send(bytes).await.is_err() { - self.pending.remove(&request_id); + self.pending.remove_sync(&request_id); return Err(TransportError::Disconnected( "sidecar transport closed".to_string(), )); @@ -401,7 +401,7 @@ impl SidecarTransport { /// Register a callback that answers a class of sidecar-initiated requests using generated wire /// protocol types. pub fn register_wire_callback(&self, key: &'static str, callback: WireSidecarCallback) { - let _ = self.callbacks.insert(key, callback); + let _ = self.callbacks.insert_sync(key, callback); } /// Return the currently negotiated max frame size. @@ -553,7 +553,7 @@ impl SidecarTransport { async fn handle_wire_frame(self: &Arc, frame: wire::ProtocolFrame) { match frame { wire::ProtocolFrame::ResponseFrame(response) => { - match self.pending.remove(&response.request_id) { + match self.pending.remove_sync(&response.request_id) { Some((_, tx)) => { let _ = tx.send(response.payload); } @@ -598,7 +598,7 @@ impl SidecarTransport { /// issues through this transport. async fn dispatch_sidecar_request(self: &Arc, frame: wire::SidecarRequestFrame) { let key = sidecar_request_key(&frame.payload); - let callback = self.callbacks.read(&key, |_, value| value.clone()); + let callback = self.callbacks.read_sync(&key, |_, value| value.clone()); match callback { Some(callback) => { let transport = Arc::downgrade(self); @@ -634,7 +634,7 @@ impl SidecarTransport { // Serialized with `register_pending_request`, so no request can register after this // clear without seeing `disconnected`. let _guard = self.pending_request_lock.lock(); - self.pending.clear(); + self.pending.clear_sync(); } fn disconnect(&self) { @@ -698,7 +698,7 @@ impl SidecarTransport { "sidecar pending request limit exceeded: at most {PENDING_REQUEST_LIMIT} requests can be in flight" ))); } - let _ = self.pending.insert(request_id, tx); + let _ = self.pending.insert_sync(request_id, tx); Ok(()) } } @@ -737,14 +737,16 @@ impl<'a> PendingRequestGuard<'a> { impl Drop for PendingRequestGuard<'_> { fn drop(&mut self) { - let _ = self.transport.pending.remove(&self.request_id); + let _ = self.transport.pending.remove_sync(&self.request_id); } } fn pending_request_count(transport: &SidecarTransport) -> usize { let mut count = 0; - transport.pending.scan(|_, _| { + transport.pending.iter_sync(|_, _| { count += 1; + + true }); count } diff --git a/crates/vm/Cargo.toml b/crates/vm/Cargo.toml index d620549e67..05ab6c5a88 100644 --- a/crates/vm/Cargo.toml +++ b/crates/vm/Cargo.toml @@ -76,7 +76,6 @@ crypto = [ "dep:openssl", "dep:pbkdf2", "dep:rustls", - "dep:rustls-pemfile", "dep:scrypt", "dep:sha1", "dep:sha2", @@ -178,7 +177,6 @@ oxc_span = { version = "0.109.0", optional = true } oxc_transformer = { version = "0.109.0", optional = true } pbkdf2 = { version = "0.12", optional = true } rustls = { version = "0.23.37", default-features = false, features = ["aws_lc_rs", "std", "tls12"], optional = true } -rustls-pemfile = { version = "2.2", optional = true } tokio-rustls = { version = "0.26", default-features = false, features = ["aws_lc_rs", "tls12"], optional = true } rusqlite = { version = "0.43", features = ["backup", "bundled", "fallible_uint", "hooks"], optional = true } # `rustix` provides the safe (owned-fd, no `unsafe`) `openat(2)`/`statat`/ @@ -196,7 +194,7 @@ sha2 = { version = "0.10", optional = true } shlex = { version = "1.3", optional = true } socket2 = { version = "0.6", optional = true } thiserror = { version = "2", optional = true } -tokio = { version = "1", features = ["io-std", "io-util", "macros", "net", "rt", "rt-multi-thread", "sync", "time"], optional = true } +tokio = { version = "1.53.3", features = ["io-std", "io-util", "macros", "net", "rt", "rt-multi-thread", "sync", "time"], optional = true } tracing = { version = "0.1", optional = true } tracing-subscriber = { version = "0.3", features = ["fmt"], optional = true } ureq = { version = "2.10", features = ["json"], optional = true } @@ -206,7 +204,7 @@ agentos-vfs-core = { workspace = true, features = ["package-filesystem"], option [build-dependencies] base64 = { version = "=0.22.1", optional = true } -webpki-root-certs = { version = "=1.0.8", optional = true } +webpki-root-certs = { version = "=1.0.10", optional = true } [dev-dependencies] agentos-executor-wasm-abi-generator = { path = "../executor-wasm-abi-generator" } diff --git a/crates/vm/src/execution/host_dispatch/network_compat.rs b/crates/vm/src/execution/host_dispatch/network_compat.rs index 11c1fd295f..96f9b9a507 100644 --- a/crates/vm/src/execution/host_dispatch/network_compat.rs +++ b/crates/vm/src/execution/host_dispatch/network_compat.rs @@ -252,50 +252,6 @@ fn indexed_posix_poll_response( json!({ "readyCount": ready_count, "fds": fds }) } -fn native_tcp_listener_waiters( - managed_descriptions: &ManagedHostNetDescriptionRegistry, - kernel: &SidecarKernel, - process: &ActiveProcess, - interests: &[KernelPollInterest], -) -> Result>, VmError> { - let descriptions = managed_descriptions - .lock() - .map_err(|_| VmError::host("EIO", "managed description registry lock poisoned"))?; - let mut waiters = Vec::new(); - for interest in interests - .iter() - .filter(|interest| interest.events & POSIX_READ_EVENTS != 0) - { - let Ok((description_id, _)) = - kernel.fd_description_identity(EXECUTION_DRIVER_NAME, process.kernel_pid, interest.fd) - else { - continue; - }; - let Some(ManagedHostNetRoute::TcpListener(listener_id)) = descriptions - .get(&description_id) - .and_then(|description| description.route_for(process.kernel_pid)) - else { - continue; - }; - let Some(listener) = process - .tcp_listeners - .get(listener_id) - .and_then(|listener| listener.listener.as_ref()) - else { - continue; - }; - waiters.push( - tokio::io::unix::AsyncFd::new(listener.try_clone().map_err(|error| { - VmError::host("EIO", format!("clone TCP listener for poll: {error}")) - })?) - .map_err(|error| { - VmError::host("EIO", format!("register TCP listener poll waiter: {error}")) - })?, - ); - } - Ok(waiters) -} - fn managed_posix_poll_read_notifies( managed_descriptions: &ManagedHostNetDescriptionRegistry, kernel: &SidecarKernel, @@ -334,10 +290,24 @@ fn managed_posix_poll_read_notifies( .udp_sockets .get(socket_id) .map(|socket| Arc::clone(&socket.read_event_notify)), + ManagedHostNetRoute::TcpListener(id) => { + let listener = process + .tcp_listeners + .get(id) + .ok_or_else(|| VmError::host("EBADF", "managed TCP listener is stale"))?; + // Guest listeners are kernel sockets. The kernel poll handle wakes + // this wait when a connection is ready for accept. + if listener.kernel_socket_id.is_none() { + return Err(VmError::host( + "ENOTSUP", + "poll requires a kernel TCP listener", + )); + } + None + } ManagedHostNetRoute::Unbound | ManagedHostNetRoute::TcpBound { .. } | ManagedHostNetRoute::UnixBound { .. } - | ManagedHostNetRoute::TcpListener(_) | ManagedHostNetRoute::UnixListener(_) => None, }; if let Some(notify) = notify { @@ -377,24 +347,11 @@ async fn wait_for_managed_posix_poll_readiness(notifies: Vec>, managed_read_notifies: Vec>, deadline: Option, ) { // The global process-event broker is intentionally absent. It wakes the // owner pump, and a poll waiting on it can consume its own published wake. - let native_listener_ready = std::future::poll_fn(|cx| { - if native_listener_waiters.is_empty() { - return std::task::Poll::Pending; - } - for waiter in &native_listener_waiters { - if waiter.poll_read_ready(cx).is_ready() { - return std::task::Poll::Ready(()); - } - } - std::task::Poll::Pending - }); - tokio::pin!(native_listener_ready); let managed_read_ready = wait_for_managed_posix_poll_readiness(managed_read_notifies); tokio::pin!(managed_read_ready); match deadline { @@ -402,7 +359,6 @@ async fn wait_for_posix_poll_change( let delay = deadline.saturating_duration_since(Instant::now()); tokio::select! { _ = wait_handle.wait_for_change_async(observed) => {} - _ = &mut native_listener_ready => {} _ = &mut managed_read_ready => {} _ = tokio::time::sleep(delay) => {} } @@ -410,7 +366,6 @@ async fn wait_for_posix_poll_change( None => { tokio::select! { _ = wait_handle.wait_for_change_async(observed) => {} - _ = &mut native_listener_ready => {} _ = &mut managed_read_ready => {} } } @@ -710,17 +665,6 @@ pub(in crate::execution) fn service_deferred_posix_poll( } let deadline = poll.deadline; - let native_listener_waiters = match native_tcp_listener_waiters( - &managed_descriptions, - kernel, - process, - poll.interests.as_slice(), - ) { - Ok(waiters) => waiters, - Err(error) => { - return fail_deferred_posix_poll(process, poll, host_service_error(&error)); - } - }; let managed_read_notifies = match managed_posix_poll_read_notifies( &managed_descriptions, kernel, @@ -734,14 +678,7 @@ pub(in crate::execution) fn service_deferred_posix_poll( }; let task_wake_lane = wake_lane.clone(); let wake_task = runtime.spawn(agentos_driver_tokio::TaskClass::Vm, async move { - wait_for_posix_poll_change( - wait_handle, - observed, - native_listener_waiters, - managed_read_notifies, - deadline, - ) - .await; + wait_for_posix_poll_change(wait_handle, observed, managed_read_notifies, deadline).await; task_wake_lane.publish().await; }); match wake_task { @@ -4084,11 +4021,7 @@ where if !(-1..=255).contains(&value) { return Err(VmError::host("EINVAL", "IPV6_TCLASS must be -1 or0..255")); } - if value == -1 { - 0 - } else { - value as u8 - } + if value == -1 { 0 } else { value as u8 } } else { value as u8 } & !3; @@ -4124,7 +4057,7 @@ where return Err(VmError::host( "ENOPROTOOPT", "traffic class option is unsupported for this transport", - )) + )); } }; let description = descriptions @@ -4252,14 +4185,16 @@ where "receive metadata requires a UDP route", )); }; - json!(context - .process - .udp_sockets - .get(id) - .ok_or_else(|| VmError::host("EBADF", "managed UDP route is stale"))? - .receive_metadata_option(udp_receive_metadata_bit(name).ok_or_else( - || VmError::host("EINVAL", "unknown receive metadata option") - )?)? as u8) + json!( + context + .process + .udp_sockets + .get(id) + .ok_or_else(|| VmError::host("EBADF", "managed UDP route is stale"))? + .receive_metadata_option(udp_receive_metadata_bit(name).ok_or_else( + || VmError::host("EINVAL", "unknown receive metadata option") + )?)? as u8 + ) } SocketOptionName::Domain => json!(match description.domain { HostSocketDomain::Unix => 1, @@ -4280,27 +4215,39 @@ where .route_for(context.process.kernel_pid) .ok_or_else(|| VmError::host("ESTALE", "managed socket route is missing"))? { - ManagedHostNetRoute::UdpSocket(id) => json!(context - .process - .udp_sockets - .get(id) - .ok_or_else(|| VmError::host("EBADF", "managed UDP route is stale"))? - .traffic_class(ipv6)?), - ManagedHostNetRoute::TcpSocket(id) => json!(context - .process - .tcp_sockets - .get(id) - .ok_or_else(|| VmError::host("EBADF", "managed TCP route is stale"))? - .traffic_class(context.kernel, ipv6)?), - ManagedHostNetRoute::TcpListener(id) => json!(context - .process - .tcp_listeners - .get(id) - .ok_or_else(|| VmError::host( - "EBADF", - "managed TCP listener route is stale" - ))? - .traffic_class(context.kernel, ipv6)?), + ManagedHostNetRoute::UdpSocket(id) => json!( + context + .process + .udp_sockets + .get(id) + .ok_or_else(|| VmError::host( + "EBADF", + "managed UDP route is stale" + ))? + .traffic_class(ipv6)? + ), + ManagedHostNetRoute::TcpSocket(id) => json!( + context + .process + .tcp_sockets + .get(id) + .ok_or_else(|| VmError::host( + "EBADF", + "managed TCP route is stale" + ))? + .traffic_class(context.kernel, ipv6)? + ), + ManagedHostNetRoute::TcpListener(id) => json!( + context + .process + .tcp_listeners + .get(id) + .ok_or_else(|| VmError::host( + "EBADF", + "managed TCP listener route is stale" + ))? + .traffic_class(context.kernel, ipv6)? + ), ManagedHostNetRoute::Unbound | ManagedHostNetRoute::TcpBound { .. } => { json!(if ipv6 { description.ipv6_traffic_class @@ -4312,7 +4259,7 @@ where return Err(VmError::host( "ENOPROTOOPT", "traffic class option is unsupported for this transport", - )) + )); } } } @@ -5853,7 +5800,7 @@ mod managed_tests { #[test] fn every_frozen_network_family_rpc_has_a_typed_operation_route() { - use crate::execution::host_dispatch::inventory::{capability_family, HostCapabilityFamily}; + use crate::execution::host_dispatch::inventory::{HostCapabilityFamily, capability_family}; for method in WASM_RUNNER_RPC_INVENTORY .iter() .copied() @@ -6154,14 +6101,8 @@ mod managed_tests { }; let waiter_socket_ready = Arc::clone(&socket_ready); let waiter = tokio::spawn(async move { - wait_for_posix_poll_change( - wait_handle, - observed, - Vec::new(), - vec![waiter_socket_ready], - None, - ) - .await; + wait_for_posix_poll_change(wait_handle, observed, vec![waiter_socket_ready], None) + .await; lane.publish().await; }); tokio::task::yield_now().await; diff --git a/crates/vm/src/execution/network/tls.rs b/crates/vm/src/execution/network/tls.rs index e5f7970e22..3abfcfcde0 100644 --- a/crates/vm/src/execution/network/tls.rs +++ b/crates/vm/src/execution/network/tls.rs @@ -1,4 +1,5 @@ use super::super::*; +use rustls::pki_types::pem::PemObject; #[derive(Debug)] struct InsecureTlsVerifier { @@ -618,15 +619,18 @@ fn tls_data_value(value: &TlsDataValue) -> Result, VmError> { } } +fn tls_pem_error(error: rustls::pki_types::pem::Error) -> VmError { + sidecar_net_error(std::io::Error::new(std::io::ErrorKind::InvalidData, error)) +} + fn tls_certificates_from_material( material: &TlsMaterial, ) -> Result>, VmError> { let mut certificates = Vec::new(); for entry in tls_material_entries(material)? { - let mut reader = std::io::BufReader::new(Cursor::new(entry.clone())); - let parsed = rustls_pemfile::certs(&mut reader) + let parsed = CertificateDer::pem_slice_iter(&entry) .collect::, _>>() - .map_err(sidecar_net_error)?; + .map_err(tls_pem_error)?; if parsed.is_empty() { certificates.push(CertificateDer::from(entry)); } else { @@ -645,8 +649,11 @@ fn tls_private_key_from_material( material: &TlsMaterial, ) -> Result, VmError> { for entry in tls_material_entries(material)? { - let mut reader = std::io::BufReader::new(Cursor::new(entry)); - if let Some(key) = rustls_pemfile::private_key(&mut reader).map_err(sidecar_net_error)? { + if let Some(key) = PrivateKeyDer::pem_slice_iter(&entry) + .next() + .transpose() + .map_err(tls_pem_error)? + { return Ok(key); } } @@ -782,10 +789,9 @@ fn tls_root_store( return Ok(roots); } - let mut reader = std::io::BufReader::new(Cursor::new(default_ca_bundle)); - let certificates = rustls_pemfile::certs(&mut reader) + let certificates = CertificateDer::pem_slice_iter(default_ca_bundle) .collect::, _>>() - .map_err(sidecar_net_error)?; + .map_err(tls_pem_error)?; if certificates.is_empty() { return Err(VmError::InvalidState(format!( "VM TLS trust store {CA_CERTIFICATES_GUEST_PATH} did not contain any certificates" diff --git a/crates/vm/tests/service.rs b/crates/vm/tests/service.rs index cc121e8f13..ebedace065 100644 --- a/crates/vm/tests/service.rs +++ b/crates/vm/tests/service.rs @@ -467,6 +467,7 @@ mod service { HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier, }; use rustls::crypto::aws_lc_rs; + use rustls::pki_types::pem::PemObject; use rustls::pki_types::{CertificateDer, ServerName}; use rustls::{ ClientConfig, ClientConnection, DigitallySignedStruct, RootCertStore, ServerConfig, @@ -475,7 +476,7 @@ mod service { use serde_json::{json, Value}; use std::collections::{BTreeMap, HashMap}; use std::fs; - use std::io::{BufReader, Read, Write}; + use std::io::{Read, Write}; use std::net::{SocketAddr, TcpListener, UdpSocket}; use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; @@ -5656,13 +5657,15 @@ console.log(JSON.stringify({ status: "ok", summary })); } fn tls_test_certificates() -> Vec> { - rustls_pemfile::certs(&mut BufReader::new(TLS_TEST_CERT_PEM.as_bytes())) + rustls::pki_types::CertificateDer::pem_slice_iter(TLS_TEST_CERT_PEM.as_bytes()) .collect::, _>>() .expect("parse TLS test certificate") } fn tls_test_private_key() -> rustls::pki_types::PrivateKeyDer<'static> { - rustls_pemfile::private_key(&mut BufReader::new(TLS_TEST_KEY_PEM.as_bytes())) + rustls::pki_types::PrivateKeyDer::pem_slice_iter(TLS_TEST_KEY_PEM.as_bytes()) + .next() + .transpose() .expect("parse TLS test private key") .expect("TLS test private key") } diff --git a/scripts/publish/src/lib/artifacts.ts b/scripts/publish/src/lib/artifacts.ts index e3fa70001e..a237c42c5b 100644 --- a/scripts/publish/src/lib/artifacts.ts +++ b/scripts/publish/src/lib/artifacts.ts @@ -16,7 +16,7 @@ export function releaseArtifactNamespace(env: ArtifactEnv = process.env): string export function releaseArtifactPrefix( artifact: ReleaseArtifactRef, env: ArtifactEnv = process.env, -): string { +): string { for (const field of ["ref", "name"] as const) { if (!/^[A-Za-z0-9][A-Za-z0-9._+-]*$/.test(artifact[field])) { throw new Error(`Invalid release artifact ${field}`); diff --git a/scripts/publish/src/lib/r2.ts b/scripts/publish/src/lib/r2.ts index cea4100752..2e00f61ec3 100644 --- a/scripts/publish/src/lib/r2.ts +++ b/scripts/publish/src/lib/r2.ts @@ -23,13 +23,13 @@ const BUCKET = "rivet-releases"; const ENDPOINT_URL = "https://2a94c6a0ced8d35ea63cddc86c2681e7.r2.cloudflarestorage.com"; -const MAX_ARTIFACT_ENTRIES = 4096; -const MAX_ARTIFACT_DEPTH = 32; - type R2Env = Record; let cached: R2Env | null = null; +const MAX_ARTIFACT_ENTRIES = 4096; +const MAX_ARTIFACT_DEPTH = 32; + function getR2Env(): R2Env { if (cached) return cached; const ak = process.env.R2_RELEASES_ACCESS_KEY_ID; From a49bf800eb09d1587c837ebb93bdd044cb8d8f0e Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Fri, 20 Nov 2026 23:52:37 -0800 Subject: [PATCH 05/19] fix: build Linux x64 V8 against the release glibc sysroot --- .github/workflows/v8-artifacts.yml | 87 +++++++++++++++++++++++----- scripts/ci/v8/README.md | 17 +++++- scripts/ci/v8/engine.py | 11 ++++ scripts/ci/v8/linux-x64-sysroot.json | 5 ++ scripts/ci/v8/prepare-linux-x64.sh | 36 ++++++++++++ scripts/ci/v8/test_engine.py | 14 ++++- 6 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 scripts/ci/v8/linux-x64-sysroot.json create mode 100644 scripts/ci/v8/prepare-linux-x64.sh diff --git a/.github/workflows/v8-artifacts.yml b/.github/workflows/v8-artifacts.yml index 915ad67719..31600aa456 100644 --- a/.github/workflows/v8-artifacts.yml +++ b/.github/workflows/v8-artifacts.yml @@ -11,34 +11,61 @@ on: pull_request: types: [labeled] workflow_dispatch: + inputs: + target: + description: Archive to rebuild (reuse already qualified targets) + type: choice + default: all + options: + - all + - x86_64-unknown-linux-gnu + - aarch64-unknown-linux-gnu + - x86_64-apple-darwin + - aarch64-apple-darwin workflow_call: permissions: contents: read jobs: - archive: + select-targets: if: >- github.event_name != 'pull_request' || (github.event.label.name == 'build-pinned-v8' && github.event.pull_request.head.repo.full_name == github.repository && startsWith(github.event.pull_request.head.ref, 'v8-artifacts/')) + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.targets.outputs.matrix }} + steps: + - id: targets + env: + SELECTED_TARGET: ${{ inputs.target || 'all' }} + run: | + python3 - <<'PY' + import json, os + targets = [ + {"target": "x86_64-unknown-linux-gnu", "runner": "depot-ubuntu-24.04-8"}, + {"target": "aarch64-unknown-linux-gnu", "runner": "depot-ubuntu-24.04-8"}, + {"target": "x86_64-apple-darwin", "runner": "macos-15"}, + {"target": "aarch64-apple-darwin", "runner": "macos-15"}, + ] + selected = os.environ["SELECTED_TARGET"] + if selected != "all": + targets = [target for target in targets if target["target"] == selected] + if not targets: + raise SystemExit("unknown archive target") + with open(os.environ["GITHUB_OUTPUT"], "a") as out: + out.write("matrix=" + json.dumps({"include": targets}) + "\n") + PY + archive: + needs: select-targets name: V8 archive (${{ matrix.target }}) runs-on: ${{ matrix.runner }} timeout-minutes: 360 strategy: fail-fast: false - matrix: - include: - - target: x86_64-unknown-linux-gnu - runner: depot-ubuntu-24.04-8 - - target: aarch64-unknown-linux-gnu - # Chromium's Linux host toolchain is distributed for x64. - runner: depot-ubuntu-24.04-8 - - target: x86_64-apple-darwin - runner: macos-15 - - target: aarch64-apple-darwin - runner: macos-15 + matrix: ${{ fromJSON(needs.select-targets.outputs.matrix) }} env: TARGET: ${{ matrix.target }} CARGO_BUILD_JOBS: '3' @@ -71,7 +98,7 @@ jobs: gpg --dearmor < "$RUNNER_TEMP/llvm.asc" | sudo tee /usr/share/keyrings/agentos-llvm.gpg >/dev/null echo 'deb [signed-by=/usr/share/keyrings/agentos-llvm.gpg] https://apt.llvm.org/noble/ llvm-toolchain-noble-21 main' | sudo tee /etc/apt/sources.list.d/agentos-llvm.list sudo apt-get update -qq - sudo apt-get install -y --no-install-recommends clang-21 libclang-21-dev pkg-config libglib2.0-dev + sudo apt-get install -y --no-install-recommends clang-21 lld-21 libclang-21-dev pkg-config libglib2.0-dev echo 'LIBCLANG_PATH=/usr/lib/llvm-21/lib' >> "$GITHUB_ENV" echo "RUSTY_V8_BINDGEN_RESOURCE_DIR=$(clang-21 -print-resource-dir)" >> "$GITHUB_ENV" - name: Install arm64 Linux cross compilation and execution tools @@ -81,6 +108,9 @@ jobs: echo 'CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc' >> "$GITHUB_ENV" echo 'CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUNNER=qemu-aarch64 -L /usr/aarch64-linux-gnu' >> "$GITHUB_ENV" echo 'RUSTY_V8_GLIBC_PREFIX=/usr/aarch64-linux-gnu' >> "$GITHUB_ENV" + - name: Prepare verified Linux x64 release headers + if: matrix.target == 'x86_64-unknown-linux-gnu' + run: bash scripts/ci/v8/prepare-linux-x64.sh "$RUNNER_TEMP/v8-sysroot" "$RUNNER_TEMP/v8-linux.env" - name: Build release library and bindings env: V8_FROM_SOURCE: '1' @@ -88,11 +118,17 @@ jobs: run: | cd "$RUNNER_TEMP/rusty-v8" export CARGO_TARGET_DIR="$RUNNER_TEMP/v8-target" - export BINDGEN_EXTRA_CLANG_ARGS="-I$CARGO_TARGET_DIR/$TARGET/release/gn_out/gen/v8/include" + if [ "$TARGET" = x86_64-unknown-linux-gnu ]; then + . "$RUNNER_TEMP/v8-linux.env" + fi + export BINDGEN_EXTRA_CLANG_ARGS="${BINDGEN_EXTRA_CLANG_ARGS:-} -I$CARGO_TARGET_DIR/$TARGET/release/gn_out/gen/v8/include" cargo +1.98.1 build --locked --lib --release --target "$TARGET" - name: Package target inputs and verify the bundle run: | bundle="$RUNNER_TEMP/$TARGET.tar.gz" + if [ "$TARGET" = x86_64-unknown-linux-gnu ]; then + . "$RUNNER_TEMP/v8-linux.env" + fi python3 scripts/ci/v8/engine.py pack "$RUNNER_TEMP/rusty-v8" "$RUNNER_TEMP/v8-target" "$TARGET" "$bundle" > "$RUNNER_TEMP/$TARGET.sha256" digest=$(cut -d' ' -f1 "$RUNNER_TEMP/$TARGET.sha256") python3 scripts/ci/v8/engine.py install "$TARGET" "$RUNNER_TEMP/v8-verify" --bundle "$bundle" --sha256 "$digest" --github-env "$GITHUB_ENV" --cargo-config "$RUNNER_TEMP/v8-source.toml" @@ -101,6 +137,15 @@ jobs: run: | expected=$(python3 -c 'import json;print(json.load(open("scripts/ci/v8/source.json"))["version"])') revision=$(python3 -c 'import json;print(json.load(open("scripts/ci/v8/source.json"))["rustyV8"])') + if [ "$TARGET" = x86_64-unknown-linux-gnu ]; then + . "$RUNNER_TEMP/v8-linux.env" + root="$AGENTOS_V8_RELEASE_SYSROOT" + export RUSTFLAGS="-C linker=clang-21 -C link-arg=-fuse-ld=lld-21 -C link-arg=--sysroot=$root -C link-arg=-L$root/lib/x86_64-linux-gnu -C link-arg=-L$root/usr/lib/x86_64-linux-gnu" + for shim in deno-memfd-create agentos-gettid agentos-renameat2; do + clang-21 --sysroot="$root" -fPIC -c "scripts/ci/$shim-shim.c" -o "$RUNNER_TEMP/$shim.o" + export RUSTFLAGS="$RUSTFLAGS -C link-arg=$RUNNER_TEMP/$shim.o" + done + fi mkdir -p "$RUNNER_TEMP/consumer/src" python3 scripts/ci/v8/engine.py consumer-lock "$RUNNER_TEMP/rusty-v8" "$RUNNER_TEMP/consumer/Cargo.lock" cd "$RUNNER_TEMP/consumer" @@ -116,6 +161,14 @@ jobs: fn main() { let native = v8::V8::get_version(); assert_eq!(native, v8::VERSION_STRING, "archive and bindgen disagree"); + let platform = v8::new_default_platform(4, false).make_shared(); + v8::V8::initialize_platform(platform); + v8::V8::initialize(); + { + let mut isolate = v8::Isolate::new(Default::default()); + v8::scope!(let scope, &mut isolate); + let _context = v8::Context::new(scope, Default::default()); + } println!("{}", native.split('-').next().unwrap()); } RS @@ -125,6 +178,12 @@ jobs: python3 "$GITHUB_WORKSPACE/scripts/ci/v8/engine.py" check-temporal-lock "$RUNNER_TEMP/rusty-v8/Cargo.lock" Cargo.lock actual=$(CARGO_TARGET_DIR="$RUNNER_TEMP/v8-consumer" cargo +1.98.1 --config "$RUNNER_TEMP/v8-source.toml" run --locked --release --target "$TARGET") test "$actual" = "$expected" + if [ "$TARGET" = x86_64-unknown-linux-gnu ]; then + binary="$RUNNER_TEMP/v8-consumer/$TARGET/release/agentos-v8-version-check" + bash "$GITHUB_WORKSPACE/scripts/ci/check-linux-glibc-floor.sh" "$binary" + actual=$(docker run --rm -v "$(dirname "$binary"):/probe:ro" ubuntu:18.04 timeout 30 /probe/agentos-v8-version-check) + test "$actual" = "$expected" + fi - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 id: upload with: diff --git a/scripts/ci/v8/README.md b/scripts/ci/v8/README.md index d1ec22f600..72e758f81c 100644 --- a/scripts/ci/v8/README.md +++ b/scripts/ci/v8/README.md @@ -11,6 +11,13 @@ binary inputs without cloning Chromium's native submodules. Before upload, a separate Cargo build links the bundle and compares the native V8 version to the generated binding and the pinned source version. +Linux x64 builds use the checksum-pinned glibc 2.27 sysroot in +`linux-x64-sysroot.json` for both native compilation and bindgen. The bundle +records this provenance; older runner-header bundles are rejected. The separate +consumer creates and disposes an isolate/context, links against the release +sysroot, passes the ELF glibc-floor check and runs on Ubuntu 18.04. A full +sidecar release link and runtime checks are still required before release. + Pip-Boy's dependency comparison fetches only the Rust sources for each checkout's locked revision. A registry-only base uses Cargo's normal registry resolution. Its platform size jobs pass the reviewed bundle to BuildKit as a @@ -42,6 +49,8 @@ DebugVisualizers are unused for these targets. `archives.json`. Its read-only Actions token downloads that exact ID; the installer independently checks the tar. Updating only the digest file leaves the four source builds idle. + Manual dispatch accepts a single `target` to rebuild a rejected archive + without rebuilding unaffected platforms. 2. Copy each unchanged `.tar.gz` to the release origin under `agent-os/v8///.tar.gz`. The same origin serves the shipped sidecar binaries. Verify the downloaded bytes match the @@ -71,13 +80,17 @@ Rust bindings also need the target-specific `v8-gn.h` that GN generates during t repo=$PWD target=x86_64-unknown-linux-gnu python3 scripts/ci/v8/engine.py prepare "$repo/target/rusty-v8-source" +bash scripts/ci/v8/prepare-linux-x64.sh "$repo/target/v8-release-sysroot" \ + "$repo/target/v8-linux.env" ( cd "$repo/target/rusty-v8-source" + . "$repo/target/v8-linux.env" export CARGO_TARGET_DIR="$repo/target/v8-build" - export BINDGEN_EXTRA_CLANG_ARGS="-I$CARGO_TARGET_DIR/$target/release/gn_out/gen/v8/include" - V8_FROM_SOURCE=1 GN_ARGS=v8_generate_external_defines_header=true \ + export BINDGEN_EXTRA_CLANG_ARGS="$BINDGEN_EXTRA_CLANG_ARGS -I$CARGO_TARGET_DIR/$target/release/gn_out/gen/v8/include" + V8_FROM_SOURCE=1 \ cargo +1.98.1 build --jobs 2 --locked --release --lib --target "$target" ) +. "$repo/target/v8-linux.env" python3 scripts/ci/v8/engine.py pack "$repo/target/rusty-v8-source" \ "$repo/target/v8-build" "$target" "$repo/target/$target.tar.gz" ``` diff --git a/scripts/ci/v8/engine.py b/scripts/ci/v8/engine.py index 9ff50ee2ee..9143d16f36 100644 --- a/scripts/ci/v8/engine.py +++ b/scripts/ci/v8/engine.py @@ -18,6 +18,7 @@ HERE = Path(__file__).resolve().parent SOURCE = json.loads((HERE / "source.json").read_text()) +LINUX_X64_SYSROOT = json.loads((HERE / "linux-x64-sysroot.json").read_text()) TARGETS = ("x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "aarch64-apple-darwin") ICU = "third_party/icu/common/icudtl.dat" @@ -152,6 +153,11 @@ def pack(source, build, target, output): "v8_generate_external_defines_header": "true", "target_cpu": '"arm64"' if target.startswith("aarch64") else '"x64"'} require(all(settings.get(k) == v for k, v in expected.items()), "wrong GN ABI or target") + if target == "x86_64-unknown-linux-gnu": + sysroot = os.environ.get("AGENTOS_V8_RELEASE_SYSROOT") + require(sysroot and settings.get("sysroot") == json.dumps(sysroot), + "Linux x64 archive must use the verified release sysroot") + require(settings.get("use_glib") == "false", "standalone V8 must not use runner GLib headers") if "apple" in target: require(sys.platform == "darwin", "Darwin archive must be produced on macOS") else: @@ -175,6 +181,8 @@ def pack(source, build, target, output): inputs["vendor/v8/.cargo-checksum.json"] = checksums receipt = {"source": SOURCE, "target": target, "gn": expected, "sha256": {name: digest(path) for name, path in inputs.items()}} + if target == "x86_64-unknown-linux-gnu": + receipt["releaseSysroot"] = LINUX_X64_SYSROOT manifest = work / "provenance.json" manifest.write_text(json.dumps(receipt, sort_keys=True, indent=2) + "\n") inputs[manifest.name] = manifest @@ -193,6 +201,9 @@ def pack(source, build, target, output): def verify_bundle(root, target): manifest = json.loads((root / "provenance.json").read_text()) require(manifest["source"] == SOURCE and manifest["target"] == target, "V8 source or target mismatch") + if target == "x86_64-unknown-linux-gnu": + require(manifest.get("releaseSysroot") == LINUX_X64_SYSROOT, + "Linux x64 bundle lacks matching release sysroot provenance") archive, binding = filenames(target) required = {archive, binding, "generated/v8-gn.h", "v8/include/v8.h", ICU, "licenses/LICENSE", "licenses/v8/LICENSE", "licenses/third_party/icu/LICENSE", diff --git a/scripts/ci/v8/linux-x64-sysroot.json b/scripts/ci/v8/linux-x64-sysroot.json new file mode 100644 index 0000000000..e9b77c8242 --- /dev/null +++ b/scripts/ci/v8/linux-x64-sysroot.json @@ -0,0 +1,5 @@ +{ + "url": "https://github.com/denoland/deno_sysroot_build/releases/download/sysroot-20250207/sysroot-x86_64.tar.xz", + "sha256": "64513b04d8a38da6f47c0a9c4cb47b2ce7bcb735250b10eb10751aec6afe46ee", + "glibcFloor": "2.27" +} diff --git a/scripts/ci/v8/prepare-linux-x64.sh b/scripts/ci/v8/prepare-linux-x64.sh new file mode 100644 index 0000000000..057485b644 --- /dev/null +++ b/scripts/ci/v8/prepare-linux-x64.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# Keep native compilation and bindgen on the same release libc headers. +set -euo pipefail +dest="${1:?usage: prepare-linux-x64.sh }" +env_file="${2:?environment-file is required}" +script_dir="$(cd "$(dirname "$0")" && pwd)" +test ! -e "$dest" +mkdir -p "$dest" +dest="$(cd "$dest" && pwd)" +url=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["url"])' "$script_dir/linux-x64-sysroot.json") +curl --fail --location --silent --show-error --proto '=https' "$url" -o "$dest/sysroot.tar.xz" +python3 - "$script_dir/linux-x64-sysroot.json" "$dest/sysroot.tar.xz" <<'PY' +import hashlib, json, sys +from pathlib import Path +expected = json.loads(Path(sys.argv[1]).read_text())["sha256"] +actual = hashlib.sha256(Path(sys.argv[2]).read_bytes()).hexdigest() +if actual != expected: + raise SystemExit("release sysroot SHA-256 mismatch") +PY +# Device nodes from the distribution image are unrelated to compiler inputs +# and cannot be materialized by an unprivileged build runner. +tar --exclude=sysroot/dev -xJf "$dest/sysroot.tar.xz" -C "$dest" +test -f "$dest/sysroot/usr/include/stdlib.h" +python3 - "$dest/sysroot" "$env_file" <<'PY' +import shlex, sys +from pathlib import Path +root, output = sys.argv[1:] +# GN's target_sysroot is not used for GNU x64 in this pinned upstream tree. +# V8-as-a-library does not use GLib; do not mix runner GLib headers into it. +values = { + "AGENTOS_V8_RELEASE_SYSROOT": root, + "GN_ARGS": f'v8_generate_external_defines_header=true use_glib=false sysroot="{root}"', + "BINDGEN_EXTRA_CLANG_ARGS": f"--sysroot={root} -isystem{root}/usr/include/x86_64-linux-gnu", +} +Path(output).write_text("".join(f"export {key}={shlex.quote(value)}\n" for key, value in values.items())) +PY diff --git a/scripts/ci/v8/test_engine.py b/scripts/ci/v8/test_engine.py index 7b8c3d358d..f5f8e78ed8 100644 --- a/scripts/ci/v8/test_engine.py +++ b/scripts/ci/v8/test_engine.py @@ -2,6 +2,7 @@ import io import json from pathlib import Path +import os import sys import tarfile import tempfile @@ -32,6 +33,7 @@ def setUp(self): gn / "args.gn": '\n'.join(['is_debug = false', 'use_custom_libcxx = true', 'v8_enable_sandbox = false', 'v8_enable_pointer_compression = false', 'v8_enable_v8_checks = false', 'v8_generate_external_defines_header = true', + 'sysroot = "/fixture/sysroot"', 'use_glib = false', 'target_cpu = "x64"']), gn / "obj/librusty_v8.a": "test archive", gn / "src_binding.rs": "test binding", @@ -46,7 +48,7 @@ def setUp(self): path.parent.mkdir(parents=True, exist_ok=True) path.write_text("test crate " + name) crate[name] = path - with mock.patch.object(engine, "verify_source"), mock.patch.object(engine, "rust_crate_inputs", return_value=crate), mock.patch.object(sys, "platform", "linux"): + with mock.patch.object(engine, "verify_source"), mock.patch.object(engine, "rust_crate_inputs", return_value=crate), mock.patch.object(sys, "platform", "linux"), mock.patch.dict(os.environ, {"AGENTOS_V8_RELEASE_SYSROOT": "/fixture/sysroot"}): engine.pack(self.source, build, self.target, self.bundle) self.pin = engine.digest(self.bundle) @@ -79,6 +81,16 @@ def test_rejects_wrong_target_and_independent_checksum(self): with self.assertRaisesRegex(ValueError, "SHA-256 mismatch"): engine.install(self.target, self.root / "installed", self.bundle, "0" * 64) + def test_rejects_pre_sysroot_linux_archive_even_with_valid_file_hashes(self): + root = self.root / "old-bundle" + engine.unpack(self.bundle, root) + manifest = root / "provenance.json" + data = json.loads(manifest.read_text()) + del data["releaseSysroot"] + manifest.write_text(json.dumps(data)) + with self.assertRaisesRegex(ValueError, "release sysroot provenance"): + engine.verify_bundle(root, self.target) + def test_draft_uses_only_the_reviewed_artifact_id_and_tar_digest(self): pin_file = self.root / "archives.json" pin = {"sha256": self.pin, "artifactId": 123} From d0befec3a69c4e3d12e67ec3bdba4ac7e3209c83 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 00:39:57 -0800 Subject: [PATCH 06/19] build: pin verified V8 archives for draft qualification --- scripts/ci/v8/archives.json | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/scripts/ci/v8/archives.json b/scripts/ci/v8/archives.json index 0967ef424b..8576287c8c 100644 --- a/scripts/ci/v8/archives.json +++ b/scripts/ci/v8/archives.json @@ -1 +1,18 @@ -{} +{ + "aarch64-apple-darwin": { + "sha256": "07fcc9f69cfe977d8b3567c14e300f74ab5f842e0ee672c9121a32cf0b7f0cac", + "artifactId": 14679159204 + }, + "aarch64-unknown-linux-gnu": { + "sha256": "72a71c276d8c44440cb25e412ec99f72bb5b331a81a62998f86d6e9bf5b471f8", + "artifactId": 14679705551 + }, + "x86_64-apple-darwin": { + "sha256": "4bd58f1a1268448bd688eab504123570b6c73344faa73139ff1e17b728521b63", + "artifactId": 14680823472 + }, + "x86_64-unknown-linux-gnu": { + "artifactId": 14699571567, + "sha256": "6fea0688c054ab5046219ad231ec76ced6a35b188fce6e4282614fb1f1927389" + } +} From 0bef4790930f6f68802b9b599ec1289afe894e8c Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 00:41:41 -0800 Subject: [PATCH 07/19] ci: disable persisted credentials in new input checks --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e808bec468..d946d5740f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -553,6 +553,7 @@ jobs: steps: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 with: + persist-credentials: false fetch-depth: 0 - id: changes env: @@ -603,6 +604,7 @@ jobs: steps: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 with: + persist-credentials: false fetch-depth: 0 - id: changes env: From 24d1afb61fd9ddb163a324e29c0823789f106c24 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 00:57:14 -0800 Subject: [PATCH 08/19] style: format merged network compatibility dispatch --- .../execution/host_dispatch/network_compat.rs | 80 ++++++++----------- 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/crates/vm/src/execution/host_dispatch/network_compat.rs b/crates/vm/src/execution/host_dispatch/network_compat.rs index 96f9b9a507..224b86a7ca 100644 --- a/crates/vm/src/execution/host_dispatch/network_compat.rs +++ b/crates/vm/src/execution/host_dispatch/network_compat.rs @@ -4021,7 +4021,11 @@ where if !(-1..=255).contains(&value) { return Err(VmError::host("EINVAL", "IPV6_TCLASS must be -1 or0..255")); } - if value == -1 { 0 } else { value as u8 } + if value == -1 { + 0 + } else { + value as u8 + } } else { value as u8 } & !3; @@ -4185,16 +4189,14 @@ where "receive metadata requires a UDP route", )); }; - json!( - context - .process - .udp_sockets - .get(id) - .ok_or_else(|| VmError::host("EBADF", "managed UDP route is stale"))? - .receive_metadata_option(udp_receive_metadata_bit(name).ok_or_else( - || VmError::host("EINVAL", "unknown receive metadata option") - )?)? as u8 - ) + json!(context + .process + .udp_sockets + .get(id) + .ok_or_else(|| VmError::host("EBADF", "managed UDP route is stale"))? + .receive_metadata_option(udp_receive_metadata_bit(name).ok_or_else( + || VmError::host("EINVAL", "unknown receive metadata option") + )?)? as u8) } SocketOptionName::Domain => json!(match description.domain { HostSocketDomain::Unix => 1, @@ -4215,39 +4217,27 @@ where .route_for(context.process.kernel_pid) .ok_or_else(|| VmError::host("ESTALE", "managed socket route is missing"))? { - ManagedHostNetRoute::UdpSocket(id) => json!( - context - .process - .udp_sockets - .get(id) - .ok_or_else(|| VmError::host( - "EBADF", - "managed UDP route is stale" - ))? - .traffic_class(ipv6)? - ), - ManagedHostNetRoute::TcpSocket(id) => json!( - context - .process - .tcp_sockets - .get(id) - .ok_or_else(|| VmError::host( - "EBADF", - "managed TCP route is stale" - ))? - .traffic_class(context.kernel, ipv6)? - ), - ManagedHostNetRoute::TcpListener(id) => json!( - context - .process - .tcp_listeners - .get(id) - .ok_or_else(|| VmError::host( - "EBADF", - "managed TCP listener route is stale" - ))? - .traffic_class(context.kernel, ipv6)? - ), + ManagedHostNetRoute::UdpSocket(id) => json!(context + .process + .udp_sockets + .get(id) + .ok_or_else(|| VmError::host("EBADF", "managed UDP route is stale"))? + .traffic_class(ipv6)?), + ManagedHostNetRoute::TcpSocket(id) => json!(context + .process + .tcp_sockets + .get(id) + .ok_or_else(|| VmError::host("EBADF", "managed TCP route is stale"))? + .traffic_class(context.kernel, ipv6)?), + ManagedHostNetRoute::TcpListener(id) => json!(context + .process + .tcp_listeners + .get(id) + .ok_or_else(|| VmError::host( + "EBADF", + "managed TCP listener route is stale" + ))? + .traffic_class(context.kernel, ipv6)?), ManagedHostNetRoute::Unbound | ManagedHostNetRoute::TcpBound { .. } => { json!(if ipv6 { description.ipv6_traffic_class @@ -5800,7 +5790,7 @@ mod managed_tests { #[test] fn every_frozen_network_family_rpc_has_a_typed_operation_route() { - use crate::execution::host_dispatch::inventory::{HostCapabilityFamily, capability_family}; + use crate::execution::host_dispatch::inventory::{capability_family, HostCapabilityFamily}; for method in WASM_RUNNER_RPC_INVENTORY .iter() .copied() From bc05f96783df10aa10d9d69c57732737ae2045e1 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 01:09:38 -0800 Subject: [PATCH 09/19] fix(node): install the CommonJS compiler before runtime preloads --- crates/executor-v8-runtime/src/execution.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/executor-v8-runtime/src/execution.rs b/crates/executor-v8-runtime/src/execution.rs index 4dc33cf178..d9cf5a967b 100644 --- a/crates/executor-v8-runtime/src/execution.rs +++ b/crates/executor-v8-runtime/src/execution.rs @@ -671,6 +671,9 @@ pub fn run_init_script( if code.is_empty() { return (0, None); } + // NODE_OPTIONS preloads run in the runtime shim before entrypoint setup. + // They need the same native CommonJS compiler as ordinary file execution. + install_compile_commonjs_wrapper_global(scope); v8::tc_scope!(tc, scope); let source = match v8::String::new(tc, code) { Some(s) => s, From acec6069f57ae46f8432ed0d68ca95fdf5d40e32 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 01:09:39 -0800 Subject: [PATCH 10/19] test: correct test-only runtime accounting and registry formatting --- crates/vm/tests/architecture_guards.rs | 31 ++++++++++++------- packages/core/tests/software-registry.test.ts | 5 +-- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/crates/vm/tests/architecture_guards.rs b/crates/vm/tests/architecture_guards.rs index ceaa86e2fd..4bc68bdb43 100644 --- a/crates/vm/tests/architecture_guards.rs +++ b/crates/vm/tests/architecture_guards.rs @@ -1593,9 +1593,10 @@ impl CfgTestTracker { return true; } - if trimmed.starts_with("#[cfg(") - && trimmed.contains("test") - && !trimmed.contains("not(test)") + if trimmed == "#[test]" + || (trimmed.starts_with("#[cfg(") + && trimmed.contains("test") + && !trimmed.contains("not(test)")) { self.pending_cfg_test = true; return false; @@ -1628,6 +1629,22 @@ impl CfgTestTracker { } } +#[test] +fn architecture_tracker_excludes_test_functions_but_keeps_production_builders() { + let source = r#" +#[test] +fn fixture_runtime() { + tokio::runtime::Builder::new_current_thread(); +} +fn production_runtime() { + tokio::runtime::Builder::new_multi_thread(); +} +"#; + let production = production_source_text(source); + assert!(!production.contains("new_current_thread")); + assert!(production.contains("new_multi_thread")); +} + fn production_source_text(source: &str) -> String { let mut tracker = CfgTestTracker::new(); source @@ -4136,14 +4153,6 @@ fn production_threads_match_the_reviewed_topology_manifest() { "serialized-v8-maintenance", "crates/executor-v8-runtime/src/adapter_host.rs", ), - ( - "test-only-stdin-readiness", - "crates/executor-v8-runtime/src/javascript.rs", - ), - ( - "test-only-backpressure-drainer", - "crates/executor-v8-runtime/src/javascript.rs", - ), ( "process-wasmtime-epoch-ticker", "crates/executor-wasm-wasmtime/src/engine.rs", diff --git a/packages/core/tests/software-registry.test.ts b/packages/core/tests/software-registry.test.ts index 74c07a8b5c..a58a939c3f 100644 --- a/packages/core/tests/software-registry.test.ts +++ b/packages/core/tests/software-registry.test.ts @@ -163,8 +163,9 @@ describe("resolveRegistrySoftware", () => { const release = manifest(["git"]); release.artifacts[0].path = "http://registry.gameinc.io/git.aospkg"; serve("latest", release); - await expect(resolveRegistrySoftware("git", { registry: { url: registry.url } })) - .rejects.toThrow("artifact URL must use https"); + await expect( + resolveRegistrySoftware("git", { registry: { url: registry.url } }), + ).rejects.toThrow("artifact URL must use https"); }); it("reads an explicit version, which wins over the registry default", async () => { From ece096a039818b45ea2e3a57e95fbd5b91dcb134 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 01:36:33 -0800 Subject: [PATCH 11/19] fix(node): preserve bounded base64 decoding with native V8 methods --- .../bridge-src/builtins/uint8array-encoding.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/build-tools/bridge-src/builtins/uint8array-encoding.ts b/packages/build-tools/bridge-src/builtins/uint8array-encoding.ts index 97cfd5e76e..00d6ca79b2 100644 --- a/packages/build-tools/bridge-src/builtins/uint8array-encoding.ts +++ b/packages/build-tools/bridge-src/builtins/uint8array-encoding.ts @@ -3,8 +3,10 @@ import { createBridgeSyncFacade } from "./fs.js"; const nativeEncoding = createBridgeSyncFacade("uint8array.encode"); // `Uint8Array` hex and base64 methods from tc39/proposal-arraybuffer-base64. -// V8 130 has no native version, so the guest gets this one. A later V8 with -// native methods keeps its own: `defineMissingMethods` skips them. +// Keep native methods when available, except setFromBase64: V8 15.8 still +// validates trailing input after filling the destination and overcounts trailing +// whitespace. Retain our bounded decoder until native behavior matches +// https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64. // // These act as built-ins, so they read the view through captured intrinsics. // A subclass's `length`, or a guest patch of `slice` or `Buffer`, cannot change @@ -375,9 +377,10 @@ function decodeHexInto(target, string, maxLength) { return { read, written }; } -function defineMissingMethods(target, methods) { +function installEncodingMethods(target, methods) { for (const [name, value] of Object.entries(methods)) { - if (typeof target[name] === "function") continue; + if (name !== "setFromBase64" && typeof target[name] === "function") + continue; Object.defineProperty(target, name, { configurable: true, writable: true, @@ -388,7 +391,7 @@ function defineMissingMethods(target, methods) { // Methods are object-literal shorthand so they have no [[Construct]]. A default // parameter keeps a method's `length` as the spec requires. -defineMissingMethods(Uint8Array.prototype, { +installEncodingMethods(Uint8Array.prototype, { toHex() { assertUint8Array(this, "toHex"); return encodeHex(this, readableLength(this)); @@ -431,7 +434,7 @@ defineMissingMethods(Uint8Array.prototype, { }, }); -defineMissingMethods(Uint8Array, { +installEncodingMethods(Uint8Array, { fromHex(string) { assertString(string, "fromHex"); assertEvenHexLength(string); From 34252683277566b564cce6ee7149220e48840516 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 17:08:43 -0800 Subject: [PATCH 12/19] fix: update macOS compiler and preserve server close context --- docker/build/darwin.Dockerfile | 17 +++++++++++++++++ packages/build-tools/bridge-src/builtins/net.ts | 8 +++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docker/build/darwin.Dockerfile b/docker/build/darwin.Dockerfile index 15f1044402..d58ff44662 100644 --- a/docker/build/darwin.Dockerfile +++ b/docker/build/darwin.Dockerfile @@ -15,12 +15,29 @@ ARG CLANG=aarch64-apple-darwin20.4 ARG BUILD_PROFILE=debug ARG CACHE_PLATFORM=darwin-arm64 ARG RUST_TOOLCHAIN=1.95.0 +ARG DARWIN_LLVM_VERSION=21 ENV SDK=/root/osxcross/target/SDK/MacOSX11.3.sdk \ RUSTC_WRAPPER=sccache \ SCCACHE_DIR=/root/.cache/sccache \ SCCACHE_IDLE_TIMEOUT=0 +# V8's public headers and agentOS C++ shims require a C++23 compiler. +# Keep the osxcross SDK/linker wrappers, but select the modern host compiler. +RUN set -eux; \ + . /etc/os-release; \ + apt-get update; \ + apt-get install -y --no-install-recommends ca-certificates curl gnupg; \ + curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key \ + | gpg --dearmor > /etc/apt/trusted.gpg.d/llvm-snapshot.gpg; \ + echo "deb https://apt.llvm.org/${VERSION_CODENAME}/ llvm-toolchain-${VERSION_CODENAME}-${DARWIN_LLVM_VERSION} main" \ + > /etc/apt/sources.list.d/agentos-llvm.list; \ + apt-get update; \ + apt-get install -y --no-install-recommends "clang-${DARWIN_LLVM_VERSION}"; \ + rm -rf /var/lib/apt/lists/* + +ENV PATH="/root/osxcross/target/bin:/usr/lib/llvm-${DARWIN_LLVM_VERSION}/bin:${PATH}" + WORKDIR /build COPY scripts/ci/v8 /build/scripts/ci/v8 RUN --mount=type=bind,from=agentos-v8-inputs,target=/opt/engine-inputs \ diff --git a/packages/build-tools/bridge-src/builtins/net.ts b/packages/build-tools/bridge-src/builtins/net.ts index e4808af094..ba9d375d2e 100644 --- a/packages/build-tools/bridge-src/builtins/net.ts +++ b/packages/build-tools/bridge-src/builtins/net.ts @@ -3489,6 +3489,7 @@ var NetServer = class extends (eventsStdlibModuleNs.EventEmitter ?? _handleRefId = null; _connections = /* @__PURE__ */ new Set(); _closePending = false; + _closeAsyncContextFrame = undefined; _closeQueued = false; _pendingTransportCloses = 0; _pendingRelisten = null; @@ -3650,6 +3651,9 @@ var NetServer = class extends (eventsStdlibModuleNs.EventEmitter ?? return this; } close(callback) { + if (!this._closePending) { + this._closeAsyncContextFrame = getAsyncContextFrame(); + } countNetBridgeMetric("serverCloseCalls"); countNetBridgeMetric( "serverCloseConnectionsAtCall", @@ -3736,7 +3740,9 @@ var NetServer = class extends (eventsStdlibModuleNs.EventEmitter ?? this._closeQueued = false; this._closePending = false; countNetBridgeMetric("serverCloseEmits"); - this._emit("close"); + const frame = this._closeAsyncContextFrame; + this._closeAsyncContextFrame = undefined; + runInAsyncContextFrame(frame, this._emit, this, ["close"]); }); } address() { From 4cec9fc11fcc42f3cfbdfa5a8396aff02f72286d Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 17:11:29 -0800 Subject: [PATCH 13/19] build: refresh macOS SDK and promote verified V8 archives --- .github/workflows/promote-v8-archives.yml | 33 +++++++++++++++ docker/build/darwin.Dockerfile | 8 +++- scripts/publish/src/ci/publish-v8-archives.ts | 42 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/promote-v8-archives.yml create mode 100644 scripts/publish/src/ci/publish-v8-archives.ts diff --git a/.github/workflows/promote-v8-archives.yml b/.github/workflows/promote-v8-archives.yml new file mode 100644 index 0000000000..d78ae8e556 --- /dev/null +++ b/.github/workflows/promote-v8-archives.yml @@ -0,0 +1,33 @@ +name: Promote reviewed V8 archives +on: + pull_request: + types: [labeled] + workflow_dispatch: +permissions: + contents: read + actions: read +jobs: + promote: + if: >- + github.event_name == 'workflow_dispatch' || + (github.event.label.name == 'promote-reviewed-v8' && + github.event.pull_request.head.repo.full_name == github.repository && + github.event.pull_request.head.ref == 'security/v8-upgrade') + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + persist-credentials: false + - uses: pnpm/action-setup@f40ffcd9367d9f12939873eb1018b921a783ffaa + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 + with: + node-version: 24 + - run: pnpm install --frozen-lockfile --filter publish + - name: Verify, publish and download every reviewed archive + env: + GH_TOKEN: ${{ github.token }} + R2_RELEASES_ACCESS_KEY_ID: ${{ secrets.R2_RELEASES_ACCESS_KEY_ID }} + R2_RELEASES_SECRET_ACCESS_KEY: ${{ secrets.R2_RELEASES_SECRET_ACCESS_KEY }} + run: pnpm --filter publish exec tsx src/ci/publish-v8-archives.ts "$GITHUB_WORKSPACE" diff --git a/docker/build/darwin.Dockerfile b/docker/build/darwin.Dockerfile index d58ff44662..65e78fd491 100644 --- a/docker/build/darwin.Dockerfile +++ b/docker/build/darwin.Dockerfile @@ -17,7 +17,9 @@ ARG CACHE_PLATFORM=darwin-arm64 ARG RUST_TOOLCHAIN=1.95.0 ARG DARWIN_LLVM_VERSION=21 -ENV SDK=/root/osxcross/target/SDK/MacOSX11.3.sdk \ +ENV SDK=/root/osxcross/target/SDK/MacOSX14.5.sdk \ + SDKROOT=/root/osxcross/target/SDK/MacOSX14.5.sdk \ + OSXCROSS_SDK=MacOSX14.5.sdk \ RUSTC_WRAPPER=sccache \ SCCACHE_DIR=/root/.cache/sccache \ SCCACHE_IDLE_TIMEOUT=0 @@ -34,6 +36,10 @@ RUN set -eux; \ > /etc/apt/sources.list.d/agentos-llvm.list; \ apt-get update; \ apt-get install -y --no-install-recommends "clang-${DARWIN_LLVM_VERSION}"; \ + curl -fsSL https://github.com/joseluisq/macosx-sdks/releases/download/14.5/MacOSX14.5.sdk.tar.xz -o /tmp/macos-sdk.tar.xz; \ + echo "6e146275d19f027faa2e8354da5e0267513abf013b8f16ad65a231653a2b1c5d /tmp/macos-sdk.tar.xz" | sha256sum -c -; \ + tar -xJf /tmp/macos-sdk.tar.xz -C /root/osxcross/target/SDK; \ + rm /tmp/macos-sdk.tar.xz; \ rm -rf /var/lib/apt/lists/* ENV PATH="/root/osxcross/target/bin:/usr/lib/llvm-${DARWIN_LLVM_VERSION}/bin:${PATH}" diff --git a/scripts/publish/src/ci/publish-v8-archives.ts b/scripts/publish/src/ci/publish-v8-archives.ts new file mode 100644 index 0000000000..766c4927e1 --- /dev/null +++ b/scripts/publish/src/ci/publish-v8-archives.ts @@ -0,0 +1,42 @@ +/** Promote reviewed native archives without rebuilding or changing their bytes. */ +import { createHash } from "node:crypto"; +import { createReadStream } from "node:fs"; +import { mkdtemp, readFile, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { $ } from "execa"; +import { uploadFile } from "../lib/r2.js"; + +const root = resolve(process.argv[2] ?? "."); +const source = JSON.parse(await readFile(join(root, "scripts/ci/v8/source.json"), "utf8")); +const pins = JSON.parse(await readFile(join(root, "scripts/ci/v8/archives.json"), "utf8")); +const targets = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "aarch64-apple-darwin"]; +if (!/^[a-f0-9]{40}$/.test(source.v8Tree)) throw new Error("Invalid V8 tree"); +const work = await mkdtemp(join(tmpdir(), "agentos-v8-promotion-")); +try { + for (const target of targets) { + const pin = pins[target]; + const sha = typeof pin === "string" ? pin : pin?.sha256; + if (!/^[a-f0-9]{64}$/.test(sha)) throw new Error(`Missing reviewed digest for ${target}`); + const run = $({ cwd: root, stdio: "inherit" }); + await run`python3 scripts/ci/v8/engine.py fetch ${target} ${work} --allow-actions`; + const archive = join(work, `${target}.tar.gz`); + const verified = join(work, target); + await run`python3 scripts/ci/v8/engine.py install ${target} ${verified} --bundle ${archive} --sha256 ${sha}`; + const hash = createHash("sha256"); + for await (const chunk of createReadStream(archive)) hash.update(chunk); + if (hash.digest("hex") !== sha) throw new Error(`Archive changed before upload: ${target}`); + const key = `agent-os/v8/${source.v8Tree}/${sha}/${target}.tar.gz`; + await uploadFile(archive, key); + const response = await fetch(`https://releases.rivet.dev/${key}`, { signal: AbortSignal.timeout(120000) }); + if (!response.ok || !response.body) throw new Error(`Published archive unavailable: ${target}: ${response.status}`); + const downloaded = createHash("sha256"); + for await (const chunk of response.body) downloaded.update(chunk); + if (downloaded.digest("hex") !== sha) throw new Error(`Published digest mismatch: ${target}`); + console.log(`Verified durable archive: ${target} ${sha}`); + await rm(archive); + await rm(verified, { recursive: true }); + } +} finally { + await rm(work, { recursive: true, force: true }); +} From b313dc6e9cacb0bcfa4c9d8a9ea7c4fc5a08aadd Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 17:15:24 -0800 Subject: [PATCH 14/19] build: ship a matched namespaced V8 binding --- Cargo.lock | 217 +- Cargo.toml | 11 +- crates/executor-v8-runtime/build.rs | 23 +- crates/v8/Cargo.toml | 41 + crates/v8/README.md | 21 + crates/v8/UPSTREAM.json | 83 + crates/v8/build.rs | 44 + crates/v8/licenses/LICENSE | 21 + .../licenses/third_party/abseil-cpp/LICENSE | 203 + .../dragonbox/src/LICENSE-Apache2-LLVM | 218 + .../third_party/dragonbox/src/LICENSE-Boost | 23 + .../third_party/fast_float/src/LICENSE-APACHE | 190 + .../third_party/fast_float/src/LICENSE-BOOST | 23 + .../third_party/fast_float/src/LICENSE-MIT | 27 + .../v8/licenses/third_party/fp16/src/LICENSE | 11 + .../licenses/third_party/highway/src/LICENSE | 371 ++ crates/v8/licenses/third_party/icu/LICENSE | 568 ++ .../licenses/third_party/jinja2/LICENSE.rst | 28 + .../third_party/libc++/src/LICENSE.TXT | 311 + .../third_party/libc++abi/src/LICENSE.TXT | 311 + .../third_party/libunwind/src/LICENSE.TXT | 311 + .../third_party/llvm-libc/src/LICENSE.TXT | 278 + .../licenses/third_party/markupsafe/LICENSE | 33 + .../v8/licenses/third_party/simdutf/LICENSE | 18 + crates/v8/licenses/v8/LICENSE | 61 + crates/v8/licenses/v8/LICENSE.fdlibm | 6 + crates/v8/licenses/v8/LICENSE.strongtalk | 29 + crates/v8/licenses/v8/LICENSE.v8 | 26 + crates/v8/provider/archives.json | 6 + crates/v8/provider/engine.py | 431 ++ crates/v8/provider/linux-x64-sysroot.json | 5 + crates/v8/provider/prepare.py | 17 + crates/v8/provider/source.json | 25 + crates/v8/rustfmt.toml | 2 + crates/v8/src/V8.rs | 349 ++ crates/v8/src/array_buffer.rs | 1002 +++ crates/v8/src/array_buffer_view.rs | 154 + crates/v8/src/bigint.rs | 143 + crates/v8/src/binding.cc | 5482 +++++++++++++++++ crates/v8/src/binding.hpp | 85 + crates/v8/src/binding.rs | 6 + crates/v8/src/context.rs | 651 ++ crates/v8/src/cppgc.rs | 771 +++ crates/v8/src/crdtp.rs | 832 +++ crates/v8/src/crdtp_binding.cc | 384 ++ crates/v8/src/data.rs | 1743 ++++++ crates/v8/src/date.rs | 85 + crates/v8/src/deno_inspector/BUILD.gn | 110 + crates/v8/src/deno_inspector/domain_io.pdl | 24 + .../v8/src/deno_inspector/domain_network.pdl | 228 + .../deno_inspector/domain_node_runtime.pdl | 22 + .../deno_inspector/domain_node_tracing.pdl | 34 + .../src/deno_inspector/domain_node_worker.pdl | 59 + .../v8/src/deno_inspector/domain_target.pdl | 26 + .../inspector_protocol_config.json | 26 + .../v8/src/deno_inspector/node_protocol.pdl | 11 + crates/v8/src/deno_inspector/string_util.cc | 197 + crates/v8/src/deno_inspector/string_util.h | 99 + crates/v8/src/exception.rs | 452 ++ crates/v8/src/external.rs | 35 + crates/v8/src/external_references.rs | 50 + crates/v8/src/fast_api.rs | 202 + crates/v8/src/fixed_array.rs | 33 + crates/v8/src/function.rs | 1160 ++++ crates/v8/src/gc.rs | 2 + .../v8/src/get_property_names_args_builder.rs | 126 + crates/v8/src/handle.rs | 1478 +++++ crates/v8/src/icu.rs | 114 + crates/v8/src/inspector.rs | 1463 +++++ crates/v8/src/isolate.rs | 3362 ++++++++++ crates/v8/src/isolate_create_params.rs | 529 ++ crates/v8/src/isolate_group.rs | 178 + crates/v8/src/json.rs | 46 + crates/v8/src/lib.rs | 243 + crates/v8/src/locker.rs | 370 ++ crates/v8/src/microtask.rs | 157 + crates/v8/src/module.rs | 667 ++ crates/v8/src/name.rs | 22 + crates/v8/src/number.rs | 104 + crates/v8/src/object.rs | 1557 +++++ crates/v8/src/platform.rs | 558 ++ crates/v8/src/primitive_array.rs | 82 + crates/v8/src/primitives.rs | 53 + crates/v8/src/private.rs | 66 + crates/v8/src/promise.rs | 240 + crates/v8/src/property_attribute.rs | 115 + crates/v8/src/property_descriptor.rs | 181 + crates/v8/src/property_filter.rs | 137 + crates/v8/src/property_handler_flags.rs | 108 + crates/v8/src/proxy.rs | 53 + crates/v8/src/regexp.rs | 131 + crates/v8/src/scope.rs | 2609 ++++++++ crates/v8/src/scope/raw.rs | 314 + crates/v8/src/script.rs | 165 + crates/v8/src/script_compiler.rs | 429 ++ crates/v8/src/script_or_module.rs | 43 + crates/v8/src/shared_array_buffer.rs | 330 + crates/v8/src/simdutf.rs | 869 +++ crates/v8/src/snapshot.rs | 281 + crates/v8/src/string.rs | 1697 +++++ crates/v8/src/support.h | 209 + crates/v8/src/support.rs | 759 +++ crates/v8/src/symbol.rs | 117 + crates/v8/src/template.rs | 1279 ++++ crates/v8/src/typed_array.rs | 90 + crates/v8/src/unbound_module_script.rs | 61 + crates/v8/src/unbound_script.rs | 86 + crates/v8/src/value.rs | 829 +++ crates/v8/src/value_deserializer.rs | 521 ++ crates/v8/src/value_serializer.rs | 608 ++ crates/v8/src/wasm.rs | 642 ++ crates/v8/tests/linked.rs | 21 + docker/build/darwin.Dockerfile | 5 +- scripts/ci/v8/archives.json | 20 +- scripts/ci/v8/engine.py | 3 +- scripts/ci/v8/test_engine.py | 13 + scripts/publish/src/lib/rust-crates.test.ts | 17 + scripts/publish/src/lib/rust-crates.ts | 11 +- 118 files changed, 42405 insertions(+), 213 deletions(-) create mode 100644 crates/v8/Cargo.toml create mode 100644 crates/v8/README.md create mode 100644 crates/v8/UPSTREAM.json create mode 100644 crates/v8/build.rs create mode 100644 crates/v8/licenses/LICENSE create mode 100644 crates/v8/licenses/third_party/abseil-cpp/LICENSE create mode 100644 crates/v8/licenses/third_party/dragonbox/src/LICENSE-Apache2-LLVM create mode 100644 crates/v8/licenses/third_party/dragonbox/src/LICENSE-Boost create mode 100644 crates/v8/licenses/third_party/fast_float/src/LICENSE-APACHE create mode 100644 crates/v8/licenses/third_party/fast_float/src/LICENSE-BOOST create mode 100644 crates/v8/licenses/third_party/fast_float/src/LICENSE-MIT create mode 100644 crates/v8/licenses/third_party/fp16/src/LICENSE create mode 100644 crates/v8/licenses/third_party/highway/src/LICENSE create mode 100644 crates/v8/licenses/third_party/icu/LICENSE create mode 100644 crates/v8/licenses/third_party/jinja2/LICENSE.rst create mode 100644 crates/v8/licenses/third_party/libc++/src/LICENSE.TXT create mode 100644 crates/v8/licenses/third_party/libc++abi/src/LICENSE.TXT create mode 100644 crates/v8/licenses/third_party/libunwind/src/LICENSE.TXT create mode 100644 crates/v8/licenses/third_party/llvm-libc/src/LICENSE.TXT create mode 100644 crates/v8/licenses/third_party/markupsafe/LICENSE create mode 100644 crates/v8/licenses/third_party/simdutf/LICENSE create mode 100644 crates/v8/licenses/v8/LICENSE create mode 100644 crates/v8/licenses/v8/LICENSE.fdlibm create mode 100644 crates/v8/licenses/v8/LICENSE.strongtalk create mode 100644 crates/v8/licenses/v8/LICENSE.v8 create mode 100644 crates/v8/provider/archives.json create mode 100644 crates/v8/provider/engine.py create mode 100644 crates/v8/provider/linux-x64-sysroot.json create mode 100644 crates/v8/provider/prepare.py create mode 100644 crates/v8/provider/source.json create mode 100644 crates/v8/rustfmt.toml create mode 100644 crates/v8/src/V8.rs create mode 100644 crates/v8/src/array_buffer.rs create mode 100644 crates/v8/src/array_buffer_view.rs create mode 100644 crates/v8/src/bigint.rs create mode 100644 crates/v8/src/binding.cc create mode 100644 crates/v8/src/binding.hpp create mode 100644 crates/v8/src/binding.rs create mode 100644 crates/v8/src/context.rs create mode 100644 crates/v8/src/cppgc.rs create mode 100644 crates/v8/src/crdtp.rs create mode 100644 crates/v8/src/crdtp_binding.cc create mode 100644 crates/v8/src/data.rs create mode 100644 crates/v8/src/date.rs create mode 100644 crates/v8/src/deno_inspector/BUILD.gn create mode 100644 crates/v8/src/deno_inspector/domain_io.pdl create mode 100644 crates/v8/src/deno_inspector/domain_network.pdl create mode 100644 crates/v8/src/deno_inspector/domain_node_runtime.pdl create mode 100644 crates/v8/src/deno_inspector/domain_node_tracing.pdl create mode 100644 crates/v8/src/deno_inspector/domain_node_worker.pdl create mode 100644 crates/v8/src/deno_inspector/domain_target.pdl create mode 100644 crates/v8/src/deno_inspector/inspector_protocol_config.json create mode 100644 crates/v8/src/deno_inspector/node_protocol.pdl create mode 100644 crates/v8/src/deno_inspector/string_util.cc create mode 100644 crates/v8/src/deno_inspector/string_util.h create mode 100644 crates/v8/src/exception.rs create mode 100644 crates/v8/src/external.rs create mode 100644 crates/v8/src/external_references.rs create mode 100644 crates/v8/src/fast_api.rs create mode 100644 crates/v8/src/fixed_array.rs create mode 100644 crates/v8/src/function.rs create mode 100644 crates/v8/src/gc.rs create mode 100644 crates/v8/src/get_property_names_args_builder.rs create mode 100644 crates/v8/src/handle.rs create mode 100644 crates/v8/src/icu.rs create mode 100644 crates/v8/src/inspector.rs create mode 100644 crates/v8/src/isolate.rs create mode 100644 crates/v8/src/isolate_create_params.rs create mode 100644 crates/v8/src/isolate_group.rs create mode 100644 crates/v8/src/json.rs create mode 100644 crates/v8/src/lib.rs create mode 100644 crates/v8/src/locker.rs create mode 100644 crates/v8/src/microtask.rs create mode 100644 crates/v8/src/module.rs create mode 100644 crates/v8/src/name.rs create mode 100644 crates/v8/src/number.rs create mode 100644 crates/v8/src/object.rs create mode 100644 crates/v8/src/platform.rs create mode 100644 crates/v8/src/primitive_array.rs create mode 100644 crates/v8/src/primitives.rs create mode 100644 crates/v8/src/private.rs create mode 100644 crates/v8/src/promise.rs create mode 100644 crates/v8/src/property_attribute.rs create mode 100644 crates/v8/src/property_descriptor.rs create mode 100644 crates/v8/src/property_filter.rs create mode 100644 crates/v8/src/property_handler_flags.rs create mode 100644 crates/v8/src/proxy.rs create mode 100644 crates/v8/src/regexp.rs create mode 100644 crates/v8/src/scope.rs create mode 100644 crates/v8/src/scope/raw.rs create mode 100644 crates/v8/src/script.rs create mode 100644 crates/v8/src/script_compiler.rs create mode 100644 crates/v8/src/script_or_module.rs create mode 100644 crates/v8/src/shared_array_buffer.rs create mode 100644 crates/v8/src/simdutf.rs create mode 100644 crates/v8/src/snapshot.rs create mode 100644 crates/v8/src/string.rs create mode 100644 crates/v8/src/support.h create mode 100644 crates/v8/src/support.rs create mode 100644 crates/v8/src/symbol.rs create mode 100644 crates/v8/src/template.rs create mode 100644 crates/v8/src/typed_array.rs create mode 100644 crates/v8/src/unbound_module_script.rs create mode 100644 crates/v8/src/unbound_script.rs create mode 100644 crates/v8/src/value.rs create mode 100644 crates/v8/src/value_deserializer.rs create mode 100644 crates/v8/src/value_serializer.rs create mode 100644 crates/v8/src/wasm.rs create mode 100644 crates/v8/tests/linked.rs diff --git a/Cargo.lock b/Cargo.lock index cff15da15d..8cc806f4cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -240,6 +240,7 @@ dependencies = [ "agentos-driver-tokio", "agentos-executor-contract", "agentos-resource-accounting", + "agentos-v8", "agentos-vm-host-interface", "brotli", "cc", @@ -256,7 +257,6 @@ dependencies = [ "tempfile", "tokio", "tracing", - "v8", ] [[package]] @@ -435,6 +435,20 @@ dependencies = [ "vbare 0.0.4", ] +[[package]] +name = "agentos-v8" +version = "0.0.1" +dependencies = [ + "bitflags", + "diplomat", + "diplomat-runtime", + "pastey", + "temporal_capi", + "temporal_rs", + "timezone_provider", + "zoneinfo64", +] + [[package]] name = "agentos-vfs-core" version = "0.0.1" @@ -492,6 +506,7 @@ dependencies = [ "agentos-resource-accounting", "agentos-rivetkit-ars-client", "agentos-sidecar-protocol", + "agentos-v8", "agentos-vfs-core", "agentos-vfs-storage", "agentos-vm-config", @@ -546,7 +561,6 @@ dependencies = [ "ureq", "url", "uuid", - "v8", "vbare 0.0.4", "vte", "wat", @@ -1327,26 +1341,6 @@ version = "1.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" -[[package]] -name = "bindgen" -version = "0.72.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "993776b509cfb49c750f11b8f07a46fa23e0a1386ffc01fb1e7d343efc387895" -dependencies = [ - "bitflags", - "cexpr", - "clang-sys", - "itertools 0.13.0", - "log", - "prettyplease", - "proc-macro2", - "quote", - "regex", - "rustc-hash", - "shlex 1.3.0", - "syn 2.0.118", -] - [[package]] name = "bitflags" version = "2.13.0" @@ -1465,15 +1459,6 @@ dependencies = [ "shlex 2.0.1", ] -[[package]] -name = "cexpr" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" -dependencies = [ - "nom", -] - [[package]] name = "cfg-if" version = "1.0.4" @@ -1558,17 +1543,6 @@ dependencies = [ "inout", ] -[[package]] -name = "clang-sys" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" -dependencies = [ - "glob", - "libc", - "libloading", -] - [[package]] name = "cmake" version = "0.1.58" @@ -2169,7 +2143,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2300,16 +2274,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" -[[package]] -name = "fslock" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04412b8935272e3a9bae6f48c7bfff74c2911f60525404edfdd28e49884c3bfb" -dependencies = [ - "libc", - "winapi", -] - [[package]] name = "futures" version = "0.3.32" @@ -2486,15 +2450,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "gzip-header" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86848f4fd157d91041a62c78046fb7b248bcc2dce78376d436a1756e9a038577" -dependencies = [ - "crc32fast", -] - [[package]] name = "h2" version = "0.4.16" @@ -2668,15 +2623,6 @@ dependencies = [ "digest 0.11.3", ] -[[package]] -name = "home" -version = "0.5.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc627f471c528ff0c4a49e1d5e60450c8f6461dd6d10ba9dcd3a61d3dff7728d" -dependencies = [ - "windows-sys 0.61.2", -] - [[package]] name = "http" version = "0.2.12" @@ -2830,7 +2776,7 @@ dependencies = [ "libc", "percent-encoding", "pin-project-lite", - "socket2 0.5.10", + "socket2 0.6.4", "system-configuration", "tokio", "tower-service", @@ -3106,15 +3052,6 @@ dependencies = [ "serde", ] -[[package]] -name = "itertools" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" -dependencies = [ - "either", -] - [[package]] name = "itertools" version = "0.14.0" @@ -3136,6 +3073,12 @@ version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4754a1731458af0443709c9667302793d3f63cc90ec4ba4c8259f2b9515478e5" +[[package]] +name = "jiff-tzdb" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0980fca2cc272465bd60e82397607ba9f33add9e48defd77923301bbd03808f2" + [[package]] name = "jni" version = "0.22.4" @@ -3251,16 +3194,6 @@ version = "0.2.186" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" -[[package]] -name = "libloading" -version = "0.8.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" -dependencies = [ - "cfg-if", - "windows-link", -] - [[package]] name = "libm" version = "0.2.16" @@ -3422,12 +3355,6 @@ dependencies = [ "unicase", ] -[[package]] -name = "minimal-lexical" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" - [[package]] name = "miniz_oxide" version = "0.8.9" @@ -3524,16 +3451,6 @@ dependencies = [ "libc", ] -[[package]] -name = "nom" -version = "7.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" -dependencies = [ - "memchr", - "minimal-lexical", -] - [[package]] name = "nonmax" version = "0.5.5" @@ -3982,7 +3899,7 @@ version = "0.109.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3bd964260860903f38fee03ec6966d66e44cc6f4b517b912caf11be556afc51d" dependencies = [ - "itertools 0.14.0", + "itertools", "memchr", "oxc_allocator", "oxc_ast", @@ -4145,10 +4062,10 @@ dependencies = [ ] [[package]] -name = "paste" -version = "1.0.15" +name = "pastey" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4" [[package]] name = "pbkdf2" @@ -4499,7 +4416,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d" dependencies = [ "anyhow", - "itertools 0.14.0", + "itertools", "proc-macro2", "quote", "syn 2.0.118", @@ -4561,7 +4478,7 @@ dependencies = [ "quinn-udp", "rustc-hash", "rustls", - "socket2 0.5.10", + "socket2 0.6.4", "thiserror 2.0.21", "tokio", "tracing", @@ -4599,9 +4516,9 @@ dependencies = [ "cfg_aliases", "libc", "once_cell", - "socket2 0.5.10", + "socket2 0.6.4", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -4734,18 +4651,6 @@ dependencies = [ "smallvec", ] -[[package]] -name = "regex" -version = "1.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1292b7759ae1cb9ec195452d1390a074f0cd8541ab7a5a8c31cd6db45d4a6ba" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - [[package]] name = "regex-automata" version = "0.4.14" @@ -5234,7 +5139,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -5887,10 +5792,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom 0.4.3", "once_cell", "rustix 1.1.6", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -5918,12 +5823,14 @@ checksum = "9a902a45282e5175186b21d355efc92564601efe6e2d92818dc9e333d50bd4de" dependencies = [ "calendrical_calculations", "core_maths", + "iana-time-zone", "icu_calendar", "icu_locale_core", "ixdtf", "num-traits", "timezone_provider", "tinystr", + "web-time", "writeable", ] @@ -6032,7 +5939,10 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c48f9b04628a2b813051e4dfe97c65281e49625eabd09ec343190e31e399a8c2" dependencies = [ + "combine", + "jiff-tzdb", "tinystr", + "tzif", "zerotrie", "zerovec", "zoneinfo64", @@ -6423,6 +6333,15 @@ version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +[[package]] +name = "tzif" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0376dfa52cce372f3b095010fd064fb850a5d8fbfd5be8b0ffa3d64eeab5a5d" +dependencies = [ + "combine", +] + [[package]] name = "ucd-trie" version = "0.1.7" @@ -6566,22 +6485,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "v8" -version = "158.0.0" -source = "git+https://github.com/denoland/rusty_v8?rev=548adfd54db42c670e0f3fd42211c3af109c0a73#548adfd54db42c670e0f3fd42211c3af109c0a73" -dependencies = [ - "bindgen", - "bitflags", - "fslock", - "gzip-header", - "home", - "miniz_oxide", - "paste", - "temporal_capi", - "which", -] - [[package]] name = "valuable" version = "0.1.1" @@ -6967,7 +6870,7 @@ dependencies = [ "cranelift-frontend", "cranelift-native", "gimli", - "itertools 0.14.0", + "itertools", "log", "object", "pulley-interpreter", @@ -7131,18 +7034,6 @@ dependencies = [ "rustls-pki-types", ] -[[package]] -name = "which" -version = "6.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4ee928febd44d98f2f459a4a79bd4d928591333a494a10a868418ac1b39cf1f" -dependencies = [ - "either", - "home", - "rustix 0.38.44", - "winsafe", -] - [[package]] name = "widestring" version = "1.2.1" @@ -7171,7 +7062,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -7332,12 +7223,6 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" -[[package]] -name = "winsafe" -version = "0.0.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" - [[package]] name = "wit-bindgen" version = "0.57.1" diff --git a/Cargo.toml b/Cargo.toml index 2c29dcae71..c3e7de7039 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ members = [ "crates/sidecar-client", "crates/sidecar-protocol", "crates/executor-v8-runtime", + "crates/v8", "crates/vfs-core", "crates/vfs-storage", "crates/vm-config", @@ -39,7 +40,7 @@ repository = "https://github.com/rivet-dev/agent-os" # AgentOS crates are published as a lock-step chain and are self-contained in # this workspace. [workspace.dependencies] -v8 = "=158.0.0" +v8 = { package = "agentos-v8", path = "crates/v8", version = "0.0.1" } agentos-actor = { path = "crates/actor", version = "0.0.1" } agentos-actor-contract = { path = "crates/actor-contract", version = "0.0.1" } agentos-preload = { path = "crates/preload", version = "0.0.1" } @@ -91,10 +92,6 @@ codegen-units = 1 panic = "abort" strip = "symbols" -# Keep the embedder bindings pinned until the matching crate is published. -[patch.crates-io] -v8 = { git = "https://github.com/denoland/rusty_v8", rev = "548adfd54db42c670e0f3fd42211c3af109c0a73" } - -# Upstream requires optimized Rust bindings even with a debug embedder. -[profile.dev.package."v8:158.0.0"] +# The binding's upstream code requires optimization even with a debug embedder. +[profile.dev.package.agentos-v8] opt-level = 1 diff --git a/crates/executor-v8-runtime/build.rs b/crates/executor-v8-runtime/build.rs index efb8957943..a03c040100 100644 --- a/crates/executor-v8-runtime/build.rs +++ b/crates/executor-v8-runtime/build.rs @@ -18,20 +18,10 @@ fn required_file(path: &Path) -> Vec { } fn v8_source_root() -> PathBuf { - println!("cargo:rerun-if-env-changed=AGENTOS_RUSTY_V8_SOURCE"); - println!("cargo:rerun-if-env-changed=RUSTY_V8_ARCHIVE"); - if let Some(root) = env::var_os("AGENTOS_RUSTY_V8_SOURCE") { - return root.into(); - } - if let Some(archive) = env::var_os("RUSTY_V8_ARCHIVE").map(PathBuf::from) { - if archive.is_file() { - return archive - .parent() - .expect("archive parent directory") - .to_owned(); - } - } - panic!("set AGENTOS_RUSTY_V8_SOURCE to the prepared agentOS V8 source or pass the local matched bundle archive via RUSTY_V8_ARCHIVE"); + PathBuf::from( + env::var_os("DEP_AGENTOS_V8_BUNDLE_ROOT") + .expect("matched agentOS V8 binding bundle metadata"), + ) } fn header_paths(dir: &Path, files: &mut Vec) { @@ -162,10 +152,7 @@ fn main() { let include = root.join("v8/include"); println!("cargo:rerun-if-changed={}", include.display()); verify_v8_headers(&include); - println!("cargo:rerun-if-env-changed=AGENTOS_RUSTY_V8_GN_INCLUDE"); - let gn_include = env::var_os("AGENTOS_RUSTY_V8_GN_INCLUDE") - .map(PathBuf::from) - .unwrap_or_else(|| root.join("generated")); + let gn_include = root.join("generated"); let gn_header = gn_include.join("v8-gn.h"); println!("cargo:rerun-if-changed={}", gn_header.display()); emit_snapshot_fingerprint(&manifest_dir, &required_file(&gn_header)); diff --git a/crates/v8/Cargo.toml b/crates/v8/Cargo.toml new file mode 100644 index 0000000000..4755970f20 --- /dev/null +++ b/crates/v8/Cargo.toml @@ -0,0 +1,41 @@ +[package] +name = "agentos-v8" +version.workspace = true +edition = "2024" +license = "MIT" +repository.workspace = true +description = "Pinned Rust V8 bindings and verified native archives for agentOS" +links = "agentos_v8" +readme = "README.md" + +[lib] +name = "v8" + +[features] +default = ["use_custom_libcxx"] +use_custom_libcxx = [] +simdutf = [] +v8_enable_pointer_compression = [] +v8_enable_multi_cage = ["v8_enable_pointer_compression"] +v8_enable_sandbox = ["v8_enable_pointer_compression"] +v8_enable_v8_checks = [] + +[dependencies] +bitflags = "2.5" +paste = { package = "pastey", version = "=0.2.3" } +# These exact versions match the native Temporal ABI in the source archives. +temporal_capi = { version = "=0.2.3", features = ["zoneinfo64"] } +temporal_rs = "=0.2.3" +timezone_provider = "=0.2.3" +diplomat-runtime = "=0.15.1" +zoneinfo64 = "=0.3.0" + +[build-dependencies] +diplomat = "=0.15.0" + +# Keep upstream sources byte-identical. These two style/deprecation diagnostics +# are new in Rust 1.99; neither changes the safety or ABI of the pinned code. +[lints.rust] +deprecated = "allow" +[lints.clippy] +nonnull_unchecked_on_box_ptr = "allow" diff --git a/crates/v8/README.md b/crates/v8/README.md new file mode 100644 index 0000000000..3c3bb33f7a --- /dev/null +++ b/crates/v8/README.md @@ -0,0 +1,21 @@ +# agentOS V8 binding + +Rust bindings from rusty_v8 commit `548adfd54db42c670e0f3fd42211c3af109c0a73`, +paired with agentOS's pinned V8 15.8.62.58 native archives. `UPSTREAM.json` +records the unchanged upstream Rust source digests; license notices are retained +in `licenses/`. This package follows the agentOS release version in lockstep. + +Building requires Python 3, a C++23 compiler for the embedding crate, and access +to `releases.rivet.dev`. The build verifies the target archive SHA-256, its source +identity and every member before linking. Linux x64/ARM64 and macOS x64/ARM64 are +supported. Build-time overrides are for trusted toolchains only: +`AGENTOS_RUSTY_V8_SOURCE` can select an already verified bundle directory. + +The fixed native configuration uses the bundled C++ runtime. Feature changes +that would disagree with the published native ABI are rejected. Temporal's +native ABI dependencies are pinned to the exact producer versions. + +Update the Rust source and native source pins together, preserve upstream +notices, and qualify the complete sidecar and a clean Cargo consumer before +publishing a new binding. Prefer returning to the upstream registry binding +when a compatible version is published and qualified. diff --git a/crates/v8/UPSTREAM.json b/crates/v8/UPSTREAM.json new file mode 100644 index 0000000000..15d6b59667 --- /dev/null +++ b/crates/v8/UPSTREAM.json @@ -0,0 +1,83 @@ +{ + "repository": "https://github.com/denoland/rusty_v8", + "revision": "548adfd54db42c670e0f3fd42211c3af109c0a73", + "rustSources": { + "src/V8.rs": "4c50eb8ac6aa618f258882edaeecb8487cfe8cc22054a91e80922e17e4a15243", + "src/array_buffer.rs": "c8d6fee0e2764cc36890f6d66d2d614afe8482b1ef9b512fd72278e51a31db50", + "src/array_buffer_view.rs": "c02f0b685716f589c3659320cadb88d0e99fd23bc6fae40e83fd36c12c81465a", + "src/bigint.rs": "60f3b1710b5af28d965aebc5816b8fa53df72ffd92e66f655bd3858dea04a716", + "src/binding.cc": "6640bf62257dc9abab3c450e64d1c5997002ffdf4c76d676b447ce3793275c12", + "src/binding.hpp": "58f2339c9b16bd4fc9f024e10c1beb6511ab80bb6a37d572f930cfddde8345db", + "src/binding.rs": "e0c40566255d55a8a8ad5df7930cea8a7ef434e6f372ccccabb7f7a1ca46119e", + "src/context.rs": "7253c096cb70a4ee288a9b36e18531862e85aefc2e5268617186f38588e7e692", + "src/cppgc.rs": "14f81dc52ec57adc9777449aaf2f1485955d41f29dbc647a84f7c1444a62a547", + "src/crdtp.rs": "de3a87cc8b26f7faea52527dfca884702aeebe142bf2de1748816f7361e73baf", + "src/crdtp_binding.cc": "8264bd985d9392b29c2f4cfca51ce96bbaa8abddcffbb76e9f949710f88c86e9", + "src/data.rs": "cc276aba3a1195b88cc84d85f74f48c05412608dd6b5126edc3403d5a692f9bd", + "src/date.rs": "91a7dac800e9f2b5b770ce146a30aecb85c8655ae777ff17b4e39e4fb64397a1", + "src/deno_inspector/BUILD.gn": "aff7256c5ec976f58f34b7ef298f240401ce61054f33b916bd84877e64aebecc", + "src/deno_inspector/domain_io.pdl": "7968fec1674aa99fa05bcb79f8f485cbace890b77fb3a385d3d3e5a764bbfe26", + "src/deno_inspector/domain_network.pdl": "9c84a6c81923a611bcc7114d69e27ef36302b643e0fbc10b05622785f22b47c0", + "src/deno_inspector/domain_node_runtime.pdl": "29ec2fca0c2ea57bf7698b0b5a712dd37d9d03e85017fea0649e9f0ece41b7e4", + "src/deno_inspector/domain_node_tracing.pdl": "120c8b5c928cf27638627642446dc40a0b6171b562eb48f06d6940e6c58ab830", + "src/deno_inspector/domain_node_worker.pdl": "4ac32cd00e312f61bc6630ee006a9a354fea1da32f9adb6dc877c605f604c122", + "src/deno_inspector/domain_target.pdl": "32ade87abcbac27d008c27329a826bdf97ae2382bd87ddce9a7f08419a35f615", + "src/deno_inspector/inspector_protocol_config.json": "d253e8598cd0f28df3f72fc3ab56200ec08122395922a8efe34fb94ad7210782", + "src/deno_inspector/node_protocol.pdl": "b969fd9807b52412db1f6b2420bf476ca2224c7f0bccec49b6d98af9883d34fb", + "src/deno_inspector/string_util.cc": "f6f669911b848288788462605826055263768a5582ce13481019c5d09693a1d1", + "src/deno_inspector/string_util.h": "a269b52de8f1072c3e7098aea5e17c04a9b8998dd5020b91f697cfac1f4c253d", + "src/exception.rs": "1b0d3adbc04f1ba8d767e9d6f9144ded9405ef79bc21334f6955769b2206a1f0", + "src/external.rs": "9fc00c57439347de94ccc8ab2d74448116de6e6dd3731fc671b6fa3fa5697121", + "src/external_references.rs": "a2533fa12556731b358cf40be726c8ea219e6d3b217903a5c73ade0b1523d5bb", + "src/fast_api.rs": "cdfe4aeba1e464a162aae8d55f5725512616694387a89cf47f25af9fc48e6962", + "src/fixed_array.rs": "bf198552233442622b41f934f122e43eeda9ecb1963784100e4cd627cd8607e0", + "src/function.rs": "2bf3770679be14800686ca673974ee4a388fb8eb46ce30991d40d18c4265e04c", + "src/gc.rs": "051fae332ef035a66aa2837445d7f04b253080494e2a2a632ac53d37a5652259", + "src/get_property_names_args_builder.rs": "1751d30c7ec62bf8c1b657ce2473d73ae952c6836b5354ff55f8890ef08bd27d", + "src/handle.rs": "86e1382fd268d3c12ba6a84830438d89157a7c64bc0eb709ebd62659aeb99c0c", + "src/icu.rs": "41fa305e83a07dcadd6add8c72b1e71673326287b7be0af4cba2ea124a201c68", + "src/inspector.rs": "48ec3827b7b748adec6c1dc2fd42a25a1b1d9147760ea458f877ef45d0d0d6b4", + "src/isolate.rs": "6b97a97e1428b51215f0d4f41a3f2676b49afdd3a7f8518e874aafe381f5de01", + "src/isolate_create_params.rs": "1dac78dee81685d13e065a2e750685403c7c2dcf3271e0c250ded4f5804d8dd1", + "src/isolate_group.rs": "0d4977b02441ca803fe3a055ca5308a67d43ff7dd5b1b0bec9432c745cd86d3b", + "src/json.rs": "59b1432a2f27c190e1fed6592a4e59b3edf2c77c367019095206b6eb6db3d34d", + "src/lib.rs": "73ee01f525dae7dc42ebc01debe6020b5e87f119c6477ac3381aad094a34ade8", + "src/locker.rs": "595e94ad6a7f16b34a4ab5c59ed9ea48d80bee8e0d8e7557cc89481efde8b69e", + "src/microtask.rs": "9c275b32bb3232b4a5c064d875996b196473d304368de0a65f837ffa9ddf556d", + "src/module.rs": "d8bbd77836ab973b987d567435ce47ea1993b193adee8972275de98dbe57651f", + "src/name.rs": "98ec0e6f8168ceb721b25b8166c3d34683fd99c6f53a2ffc0a7876c80d06eb12", + "src/number.rs": "6319f68bee5542c44c881b3c8dfc8500904c3ed19bb92da7a8c29da20330c8d5", + "src/object.rs": "331fc06112c78e92cea058894d0b4d0ad8441ee123715744a3f76ecbbd8f9f55", + "src/platform.rs": "5804f45cb48fdbebecf1663950b3a2e1dbfc6ffdce909844d7ea70abb8a67782", + "src/primitive_array.rs": "41f30af83f597dbaf3b3671b54e05823fa93bcebbc88008b34641ed332368e0d", + "src/primitives.rs": "16f51b342d5938e24ab700e849287a55122398f788c767bbcc7c3e580b7823f8", + "src/private.rs": "ee1ecf1b57a121d95fb2ec1b0807c4ac517ac2b7adc91b0799678c7b3bb69dbb", + "src/promise.rs": "c8987066af276ac37112350b54a1a11aa87854ae2e5a183f28128a63b18e8cc2", + "src/property_attribute.rs": "91f89552d0da0d3bfcbfaececad170995d7efb0bf77e0f06afa0cb07fa0173af", + "src/property_descriptor.rs": "491a3a25c515d544bbe0c9ce5bac45cf00e22184838be297517c06a7bd95ca7b", + "src/property_filter.rs": "3fb114aa672be7ffb411b202d6456c9f79ae069a0b735219f839cf56c797200d", + "src/property_handler_flags.rs": "75bf24a643f404fa0a3ebc755aafdfc94df223cf6b0d302a69404ee30667b540", + "src/proxy.rs": "1e9cf72f1b37bf128f831c682aa39fe23fdd24f4b255a916c96ea34b551bb0ed", + "src/regexp.rs": "7eb058ebd76c9519b0a0e6de754989bf6e9446d55d16adb8d44d176a79522d85", + "src/scope/raw.rs": "80de5b9a187b8713e9cecfa7ec653c28be7e7b97ca64a57ed11688c873948f94", + "src/scope.rs": "68b7c642c93a9867d8e773b7e77f68504d03d4b0af93cee1aad23aa56938358d", + "src/script.rs": "8fc2de977957e9248276616cecdd14439815793fd94a6ceea774f0bb8def32b2", + "src/script_compiler.rs": "de54dbf2feb63796569f5769818d5f4d9c176063b483dcb932e891cccb4890fb", + "src/script_or_module.rs": "aeb1c043262c5cdc06f0318f776bf0476fd42877e3395fc038c98519e7d9ade3", + "src/shared_array_buffer.rs": "099dcaac0fb8de502ce16ab4c317107fee0ac98f37f1c41d9f0ae457aa247653", + "src/simdutf.rs": "5e19c679bbab53b8eed5296f60d661ee9dd2e02a1856e2517e296ea18aef5f49", + "src/snapshot.rs": "03200ae6db40b9f7962a42c2c3d0c1ffa9dae9f0b2e9b2512c86f03b8203bd63", + "src/string.rs": "fcc46b6a920e8405207e0ea0866a871e37aecb81af7fbd6311364207d2da9e9a", + "src/support.h": "56e8f7bc040146c147738edde4b6c359d266b3e610a10a126dd9abe96be29a17", + "src/support.rs": "f2072633547c1ebf952a77b0a848dbe47609025124a84565d6ce718a77933327", + "src/symbol.rs": "e645e490948c6a7b29a6d35fd0241b72d61e24c1e550fba9fdd0fe6e082308a5", + "src/template.rs": "c9ff8a340b38af1328b054f7b44923a0d25e6ed2cae042e5063966879cad78a0", + "src/typed_array.rs": "a106bcfe6d11c11fec4e9b994007ee91bc2e5d81289161f025ed0dd7a813774c", + "src/unbound_module_script.rs": "c8f95f82c9b7243cb8a06387bdecad1fed29ecf814f3ffc79300b6aa6674aa6b", + "src/unbound_script.rs": "eb42b9509421c1e77f1f7c665377568b705c08e5dc144d0b47ad85901b3e5d44", + "src/value.rs": "ca0e0f533d9da8cfd2f7aa01096422042a08ccdcb7b4b9b6d5271bbb21b248f7", + "src/value_deserializer.rs": "6bbf24918a913539e1cd75394620ff2fbcdabd381b82d9e451fd3dc7e51d25a8", + "src/value_serializer.rs": "1e034e7418bfb06dd2bc70b68da7e0a2bda631e844d429949b512b7fa877a3e8", + "src/wasm.rs": "d121efb6455c38def30ae5784c462a17fdf98ce253ac9eb30b52a975afbd4e42" + } +} diff --git a/crates/v8/build.rs b/crates/v8/build.rs new file mode 100644 index 0000000000..7ac2face13 --- /dev/null +++ b/crates/v8/build.rs @@ -0,0 +1,44 @@ +use std::{env, fs, path::PathBuf, process::Command}; + +fn main() { + println!("cargo:rerun-if-changed=provider"); + println!("cargo:rerun-if-env-changed=AGENTOS_RUSTY_V8_SOURCE"); + assert!( + env::var_os("CARGO_FEATURE_USE_CUSTOM_LIBCXX").is_some(), + "agentOS V8 archives require the bundled C++ runtime" + ); + for feature in [ + "SIMDUTF", + "V8_ENABLE_POINTER_COMPRESSION", + "V8_ENABLE_MULTI_CAGE", + "V8_ENABLE_SANDBOX", + "V8_ENABLE_V8_CHECKS", + ] { + assert!( + env::var_os(format!("CARGO_FEATURE_{feature}")).is_none(), + "feature {feature} does not match the fixed agentOS V8 archive ABI" + ); + } + let target = env::var("TARGET").expect("Cargo target"); + let status = Command::new("python3") + .args(["-B", "provider/prepare.py", &target]) + .status() + .expect("Python 3 is required to verify agentOS V8 archives"); + assert!(status.success(), "agentOS V8 archive verification failed"); + let out = + PathBuf::from(env::var_os("OUT_DIR").expect("Cargo output directory")); + let root = PathBuf::from( + fs::read_to_string(out.join("bundle-root")).expect("verified bundle root"), + ); + let library = root.join(format!("librusty_v8_release_{target}.a")); + println!("cargo:rerun-if-changed={}", library.display()); + fs::copy(library, out.join("librusty_v8.a")) + .expect("stage matched V8 library"); + println!("cargo::metadata=bundle_root={}", root.display()); + println!( + "cargo:rustc-env=RUSTY_V8_SRC_BINDING_PATH={}/src_binding_release_{target}.rs", + root.display() + ); + println!("cargo:rustc-link-search=native={}", out.display()); + println!("cargo:rustc-link-lib=static=rusty_v8"); +} diff --git a/crates/v8/licenses/LICENSE b/crates/v8/licenses/LICENSE new file mode 100644 index 0000000000..5a671244de --- /dev/null +++ b/crates/v8/licenses/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018-2019 the Deno authors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crates/v8/licenses/third_party/abseil-cpp/LICENSE b/crates/v8/licenses/third_party/abseil-cpp/LICENSE new file mode 100644 index 0000000000..ccd61dcfe3 --- /dev/null +++ b/crates/v8/licenses/third_party/abseil-cpp/LICENSE @@ -0,0 +1,203 @@ + + Apache License + Version 2.0, January 2004 + https://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/crates/v8/licenses/third_party/dragonbox/src/LICENSE-Apache2-LLVM b/crates/v8/licenses/third_party/dragonbox/src/LICENSE-Apache2-LLVM new file mode 100644 index 0000000000..bd8b243dfa --- /dev/null +++ b/crates/v8/licenses/third_party/dragonbox/src/LICENSE-Apache2-LLVM @@ -0,0 +1,218 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +--- LLVM Exceptions to the Apache 2.0 License ---- + +As an exception, if, as a result of your compiling your source code, portions +of this Software are embedded into an Object form of such source code, you +may redistribute such embedded portions in such Object form without complying +with the conditions of Sections 4(a), 4(b) and 4(d) of the License. + +In addition, if you combine or link compiled forms of this Software with +software that is licensed under the GPLv2 ("Combined Software") and if a +court of competent jurisdiction determines that the patent provision (Section +3), the indemnity provision (Section 9) or other Section of the License +conflicts with the conditions of the GPLv2, you may retroactively and +prospectively choose to deem waived or otherwise exclude such Section(s) of +the License, but only in their entirety and only with respect to the Combined +Software. diff --git a/crates/v8/licenses/third_party/dragonbox/src/LICENSE-Boost b/crates/v8/licenses/third_party/dragonbox/src/LICENSE-Boost new file mode 100644 index 0000000000..36b7cd93cd --- /dev/null +++ b/crates/v8/licenses/third_party/dragonbox/src/LICENSE-Boost @@ -0,0 +1,23 @@ +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/crates/v8/licenses/third_party/fast_float/src/LICENSE-APACHE b/crates/v8/licenses/third_party/fast_float/src/LICENSE-APACHE new file mode 100644 index 0000000000..26f4398f24 --- /dev/null +++ b/crates/v8/licenses/third_party/fast_float/src/LICENSE-APACHE @@ -0,0 +1,190 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + Copyright 2021 The fast_float authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/crates/v8/licenses/third_party/fast_float/src/LICENSE-BOOST b/crates/v8/licenses/third_party/fast_float/src/LICENSE-BOOST new file mode 100644 index 0000000000..127a5bc39b --- /dev/null +++ b/crates/v8/licenses/third_party/fast_float/src/LICENSE-BOOST @@ -0,0 +1,23 @@ +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/crates/v8/licenses/third_party/fast_float/src/LICENSE-MIT b/crates/v8/licenses/third_party/fast_float/src/LICENSE-MIT new file mode 100644 index 0000000000..2fb2a37ad7 --- /dev/null +++ b/crates/v8/licenses/third_party/fast_float/src/LICENSE-MIT @@ -0,0 +1,27 @@ +MIT License + +Copyright (c) 2021 The fast_float authors + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/crates/v8/licenses/third_party/fp16/src/LICENSE b/crates/v8/licenses/third_party/fp16/src/LICENSE new file mode 100644 index 0000000000..eabec6c86a --- /dev/null +++ b/crates/v8/licenses/third_party/fp16/src/LICENSE @@ -0,0 +1,11 @@ +The MIT License (MIT) + +Copyright (c) 2017 Facebook Inc. +Copyright (c) 2017 Georgia Institute of Technology +Copyright 2019 Google LLC + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/crates/v8/licenses/third_party/highway/src/LICENSE b/crates/v8/licenses/third_party/highway/src/LICENSE new file mode 100644 index 0000000000..1af4f15ca7 --- /dev/null +++ b/crates/v8/licenses/third_party/highway/src/LICENSE @@ -0,0 +1,371 @@ +This project is primarily dual-licensed under your choice of either the Apache +License 2.0 or the BSD 3-Clause License. + +The following files are licensed under different terms: +* hwy/contrib/random/random-inl.h: CC0 1.0 Universal + +The full texts of all applicable licenses are included below, separated by +'---'. + +-------------------------------------------------------------------------------- +Apache License 2.0 +-------------------------------------------------------------------------------- + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +-------------------------------------------------------------------------------- +BSD 3-Clause License +-------------------------------------------------------------------------------- + +Copyright (c) The Highway Project Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +-------------------------------------------------------------------------------- +CC0 1.0 Universal +-------------------------------------------------------------------------------- + +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. \ No newline at end of file diff --git a/crates/v8/licenses/third_party/icu/LICENSE b/crates/v8/licenses/third_party/icu/LICENSE new file mode 100644 index 0000000000..5a2eda6295 --- /dev/null +++ b/crates/v8/licenses/third_party/icu/LICENSE @@ -0,0 +1,568 @@ +UNICODE LICENSE V3 + +COPYRIGHT AND PERMISSION NOTICE + +Copyright © 2016-2025 Unicode, Inc. + +NOTICE TO USER: Carefully read the following legal agreement. BY +DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR +SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE +TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT +DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of data files and any associated documentation (the "Data Files") or +software and any associated documentation (the "Software") to deal in the +Data Files or Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, and/or sell +copies of the Data Files or Software, and to permit persons to whom the +Data Files or Software are furnished to do so, provided that either (a) +this copyright and permission notice appear with all copies of the Data +Files or Software, or (b) this copyright and permission notice appear in +associated Documentation. + +THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF +THIRD PARTY RIGHTS. + +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE +BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, +OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA +FILES OR SOFTWARE. + +Except as contained in this notice, the name of a copyright holder shall +not be used in advertising or otherwise to promote the sale, use or other +dealings in these Data Files or Software without prior written +authorization of the copyright holder. + +SPDX-License-Identifier: Unicode-3.0 + +---------------------------------------------------------------------- + +Third-Party Software Licenses + +This section contains third-party software notices and/or additional +terms for licensed third-party software components included within ICU +libraries. + +---------------------------------------------------------------------- + +ICU License - ICU 1.8.1 to ICU 57.1 + +COPYRIGHT AND PERMISSION NOTICE + +Copyright (c) 1995-2016 International Business Machines Corporation and others +All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, and/or sell copies of the Software, and to permit persons +to whom the Software is furnished to do so, provided that the above +copyright notice(s) and this permission notice appear in all copies of +the Software and that both the above copyright notice(s) and this +permission notice appear in supporting documentation. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY +SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER +RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF +CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +Except as contained in this notice, the name of a copyright holder +shall not be used in advertising or otherwise to promote the sale, use +or other dealings in this Software without prior written authorization +of the copyright holder. + +All trademarks and registered trademarks mentioned herein are the +property of their respective owners. + +---------------------------------------------------------------------- + +Chinese/Japanese Word Break Dictionary Data (cjdict.txt) + + # The Google Chrome software developed by Google is licensed under + # the BSD license. Other software included in this distribution is + # provided under other licenses, as set forth below. + # + # The BSD License + # http://opensource.org/licenses/bsd-license.php + # Copyright (C) 2006-2008, Google Inc. + # + # All rights reserved. + # + # Redistribution and use in source and binary forms, with or without + # modification, are permitted provided that the following conditions are met: + # + # Redistributions of source code must retain the above copyright notice, + # this list of conditions and the following disclaimer. + # Redistributions in binary form must reproduce the above + # copyright notice, this list of conditions and the following + # disclaimer in the documentation and/or other materials provided with + # the distribution. + # Neither the name of Google Inc. nor the names of its + # contributors may be used to endorse or promote products derived from + # this software without specific prior written permission. + # + # + # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + # CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + # + # + # The word list in cjdict.txt are generated by combining three word lists + # listed below with further processing for compound word breaking. The + # frequency is generated with an iterative training against Google web + # corpora. + # + # * Libtabe (Chinese) + # - https://sourceforge.net/project/?group_id=1519 + # - Its license terms and conditions are shown below. + # + # * IPADIC (Japanese) + # - http://chasen.aist-nara.ac.jp/chasen/distribution.html + # - Its license terms and conditions are shown below. + # + # ---------COPYING.libtabe ---- BEGIN-------------------- + # + # /* + # * Copyright (c) 1999 TaBE Project. + # * Copyright (c) 1999 Pai-Hsiang Hsiao. + # * All rights reserved. + # * + # * Redistribution and use in source and binary forms, with or without + # * modification, are permitted provided that the following conditions + # * are met: + # * + # * . Redistributions of source code must retain the above copyright + # * notice, this list of conditions and the following disclaimer. + # * . Redistributions in binary form must reproduce the above copyright + # * notice, this list of conditions and the following disclaimer in + # * the documentation and/or other materials provided with the + # * distribution. + # * . Neither the name of the TaBE Project nor the names of its + # * contributors may be used to endorse or promote products derived + # * from this software without specific prior written permission. + # * + # * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + # * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + # * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + # * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + # * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + # * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + # * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + # * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + # * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + # * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + # * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + # * OF THE POSSIBILITY OF SUCH DAMAGE. + # */ + # + # /* + # * Copyright (c) 1999 Computer Systems and Communication Lab, + # * Institute of Information Science, Academia + # * Sinica. All rights reserved. + # * + # * Redistribution and use in source and binary forms, with or without + # * modification, are permitted provided that the following conditions + # * are met: + # * + # * . Redistributions of source code must retain the above copyright + # * notice, this list of conditions and the following disclaimer. + # * . Redistributions in binary form must reproduce the above copyright + # * notice, this list of conditions and the following disclaimer in + # * the documentation and/or other materials provided with the + # * distribution. + # * . Neither the name of the Computer Systems and Communication Lab + # * nor the names of its contributors may be used to endorse or + # * promote products derived from this software without specific + # * prior written permission. + # * + # * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + # * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + # * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + # * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + # * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + # * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + # * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + # * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + # * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + # * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + # * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + # * OF THE POSSIBILITY OF SUCH DAMAGE. + # */ + # + # Copyright 1996 Chih-Hao Tsai @ Beckman Institute, + # University of Illinois + # c-tsai4@uiuc.edu http://casper.beckman.uiuc.edu/~c-tsai4 + # + # ---------------COPYING.libtabe-----END-------------------------------- + # + # + # ---------------COPYING.ipadic-----BEGIN------------------------------- + # + # Copyright 2000, 2001, 2002, 2003 Nara Institute of Science + # and Technology. All Rights Reserved. + # + # Use, reproduction, and distribution of this software is permitted. + # Any copy of this software, whether in its original form or modified, + # must include both the above copyright notice and the following + # paragraphs. + # + # Nara Institute of Science and Technology (NAIST), + # the copyright holders, disclaims all warranties with regard to this + # software, including all implied warranties of merchantability and + # fitness, in no event shall NAIST be liable for + # any special, indirect or consequential damages or any damages + # whatsoever resulting from loss of use, data or profits, whether in an + # action of contract, negligence or other tortuous action, arising out + # of or in connection with the use or performance of this software. + # + # A large portion of the dictionary entries + # originate from ICOT Free Software. The following conditions for ICOT + # Free Software applies to the current dictionary as well. + # + # Each User may also freely distribute the Program, whether in its + # original form or modified, to any third party or parties, PROVIDED + # that the provisions of Section 3 ("NO WARRANTY") will ALWAYS appear + # on, or be attached to, the Program, which is distributed substantially + # in the same form as set out herein and that such intended + # distribution, if actually made, will neither violate or otherwise + # contravene any of the laws and regulations of the countries having + # jurisdiction over the User or the intended distribution itself. + # + # NO WARRANTY + # + # The program was produced on an experimental basis in the course of the + # research and development conducted during the project and is provided + # to users as so produced on an experimental basis. Accordingly, the + # program is provided without any warranty whatsoever, whether express, + # implied, statutory or otherwise. The term "warranty" used herein + # includes, but is not limited to, any warranty of the quality, + # performance, merchantability and fitness for a particular purpose of + # the program and the nonexistence of any infringement or violation of + # any right of any third party. + # + # Each user of the program will agree and understand, and be deemed to + # have agreed and understood, that there is no warranty whatsoever for + # the program and, accordingly, the entire risk arising from or + # otherwise connected with the program is assumed by the user. + # + # Therefore, neither ICOT, the copyright holder, or any other + # organization that participated in or was otherwise related to the + # development of the program and their respective officials, directors, + # officers and other employees shall be held liable for any and all + # damages, including, without limitation, general, special, incidental + # and consequential damages, arising out of or otherwise in connection + # with the use or inability to use the program or any product, material + # or result produced or otherwise obtained by using the program, + # regardless of whether they have been advised of, or otherwise had + # knowledge of, the possibility of such damages at any time during the + # project or thereafter. Each user will be deemed to have agreed to the + # foregoing by his or her commencement of use of the program. The term + # "use" as used herein includes, but is not limited to, the use, + # modification, copying and distribution of the program and the + # production of secondary products from the program. + # + # In the case where the program, whether in its original form or + # modified, was distributed or delivered to or received by a user from + # any person, organization or entity other than ICOT, unless it makes or + # grants independently of ICOT any specific warranty to the user in + # writing, such person, organization or entity, will also be exempted + # from and not be held liable to the user for any such damages as noted + # above as far as the program is concerned. + # + # ---------------COPYING.ipadic-----END---------------------------------- + +---------------------------------------------------------------------- + +Lao Word Break Dictionary Data (laodict.txt) + + # Copyright (C) 2016 and later: Unicode, Inc. and others. + # License & terms of use: http://www.unicode.org/copyright.html + # Copyright (c) 2015 International Business Machines Corporation + # and others. All Rights Reserved. + # + # Project: https://github.com/rober42539/lao-dictionary + # Dictionary: https://github.com/rober42539/lao-dictionary/laodict.txt + # License: https://github.com/rober42539/lao-dictionary/LICENSE.txt + # (copied below) + # + # This file is derived from the above dictionary version of Nov 22, 2020 + # ---------------------------------------------------------------------- + # Copyright (C) 2013 Brian Eugene Wilson, Robert Martin Campbell. + # All rights reserved. + # + # Redistribution and use in source and binary forms, with or without + # modification, are permitted provided that the following conditions are met: + # + # Redistributions of source code must retain the above copyright notice, this + # list of conditions and the following disclaimer. Redistributions in binary + # form must reproduce the above copyright notice, this list of conditions and + # the following disclaimer in the documentation and/or other materials + # provided with the distribution. + # + # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + # OF THE POSSIBILITY OF SUCH DAMAGE. + # -------------------------------------------------------------------------- + +---------------------------------------------------------------------- + +Burmese Word Break Dictionary Data (burmesedict.txt) + + # Copyright (c) 2014 International Business Machines Corporation + # and others. All Rights Reserved. + # + # This list is part of a project hosted at: + # github.com/kanyawtech/myanmar-karen-word-lists + # + # -------------------------------------------------------------------------- + # Copyright (c) 2013, LeRoy Benjamin Sharon + # All rights reserved. + # + # Redistribution and use in source and binary forms, with or without + # modification, are permitted provided that the following conditions + # are met: Redistributions of source code must retain the above + # copyright notice, this list of conditions and the following + # disclaimer. Redistributions in binary form must reproduce the + # above copyright notice, this list of conditions and the following + # disclaimer in the documentation and/or other materials provided + # with the distribution. + # + # Neither the name Myanmar Karen Word Lists, nor the names of its + # contributors may be used to endorse or promote products derived + # from this software without specific prior written permission. + # + # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + # CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS + # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + # SUCH DAMAGE. + # -------------------------------------------------------------------------- + +---------------------------------------------------------------------- + +Time Zone Database + + ICU uses the public domain data and code derived from Time Zone +Database for its time zone support. The ownership of the TZ database +is explained in BCP 175: Procedure for Maintaining the Time Zone +Database section 7. + + # 7. Database Ownership + # + # The TZ database itself is not an IETF Contribution or an IETF + # document. Rather it is a pre-existing and regularly updated work + # that is in the public domain, and is intended to remain in the + # public domain. Therefore, BCPs 78 [RFC5378] and 79 [RFC3979] do + # not apply to the TZ Database or contributions that individuals make + # to it. Should any claims be made and substantiated against the TZ + # Database, the organization that is providing the IANA + # Considerations defined in this RFC, under the memorandum of + # understanding with the IETF, currently ICANN, may act in accordance + # with all competent court orders. No ownership claims will be made + # by ICANN or the IETF Trust on the database or the code. Any person + # making a contribution to the database or code waives all rights to + # future claims in that contribution or in the TZ Database. + +---------------------------------------------------------------------- + +Google double-conversion + +Copyright 2006-2011, the V8 project authors. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +---------------------------------------------------------------------- + +JSON parsing library (nlohmann/json) + +File: vendor/json/upstream/single_include/nlohmann/json.hpp (only for ICU4C) + +MIT License + +Copyright (c) 2013-2022 Niels Lohmann + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +---------------------------------------------------------------------- + +File: aclocal.m4 (only for ICU4C) +Section: pkg.m4 - Macros to locate and utilise pkg-config. + + +Copyright © 2004 Scott James Remnant . +Copyright © 2012-2015 Dan Nicholson + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +02111-1307, USA. + +As a special exception to the GNU General Public License, if you +distribute this file as part of a program that contains a +configuration script generated by Autoconf, you may include it under +the same distribution terms that you use for the rest of that +program. + + +(The condition for the exception is fulfilled because +ICU4C includes a configuration script generated by Autoconf, +namely the `configure` script.) + +---------------------------------------------------------------------- + +File: config.guess (only for ICU4C) + + +This file is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, see . + +As a special exception to the GNU General Public License, if you +distribute this file as part of a program that contains a +configuration script generated by Autoconf, you may include it under +the same distribution terms that you use for the rest of that +program. This Exception is an additional permission under section 7 +of the GNU General Public License, version 3 ("GPLv3"). + + +(The condition for the exception is fulfilled because +ICU4C includes a configuration script generated by Autoconf, +namely the `configure` script.) + +---------------------------------------------------------------------- + +File: install-sh (only for ICU4C) + + +Copyright 1991 by the Massachusetts Institute of Technology + +Permission to use, copy, modify, distribute, and sell this software and its +documentation for any purpose is hereby granted without fee, provided that +the above copyright notice appear in all copies and that both that +copyright notice and this permission notice appear in supporting +documentation, and that the name of M.I.T. not be used in advertising or +publicity pertaining to distribution of the software without specific, +written prior permission. M.I.T. makes no representations about the +suitability of this software for any purpose. It is provided "as is" +without express or implied warranty. + +---------------------------------------------------------------------- + +File: sorttable.js (only for ICU4J) + +The MIT Licence, for code from kryogenix.org + +Code downloaded from the Browser Experiments section of kryogenix.org is +licenced under the so-called MIT licence. The licence is below. + +Copyright (c) 1997-date Stuart Langridge + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crates/v8/licenses/third_party/jinja2/LICENSE.rst b/crates/v8/licenses/third_party/jinja2/LICENSE.rst new file mode 100644 index 0000000000..c37cae49ec --- /dev/null +++ b/crates/v8/licenses/third_party/jinja2/LICENSE.rst @@ -0,0 +1,28 @@ +Copyright 2007 Pallets + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/crates/v8/licenses/third_party/libc++/src/LICENSE.TXT b/crates/v8/licenses/third_party/libc++/src/LICENSE.TXT new file mode 100644 index 0000000000..e159d28344 --- /dev/null +++ b/crates/v8/licenses/third_party/libc++/src/LICENSE.TXT @@ -0,0 +1,311 @@ +============================================================================== +The LLVM Project is under the Apache License v2.0 with LLVM Exceptions: +============================================================================== + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +---- LLVM Exceptions to the Apache 2.0 License ---- + +As an exception, if, as a result of your compiling your source code, portions +of this Software are embedded into an Object form of such source code, you +may redistribute such embedded portions in such Object form without complying +with the conditions of Sections 4(a), 4(b) and 4(d) of the License. + +In addition, if you combine or link compiled forms of this Software with +software that is licensed under the GPLv2 ("Combined Software") and if a +court of competent jurisdiction determines that the patent provision (Section +3), the indemnity provision (Section 9) or other Section of the License +conflicts with the conditions of the GPLv2, you may retroactively and +prospectively choose to deem waived or otherwise exclude such Section(s) of +the License, but only in their entirety and only with respect to the Combined +Software. + +============================================================================== +Software from third parties included in the LLVM Project: +============================================================================== +The LLVM Project contains third party software which is under different license +terms. All such code will be identified clearly using at least one of two +mechanisms: +1) It will be in a separate directory tree with its own `LICENSE.txt` or + `LICENSE` file at the top containing the specific license and restrictions + which apply to that software, or +2) It will contain specific license and restriction terms at the top of every + file. + +============================================================================== +Legacy LLVM License (https://llvm.org/docs/DeveloperPolicy.html#legacy): +============================================================================== + +The libc++ library is dual licensed under both the University of Illinois +"BSD-Like" license and the MIT license. As a user of this code you may choose +to use it under either license. As a contributor, you agree to allow your code +to be used under both. + +Full text of the relevant licenses is included below. + +============================================================================== + +University of Illinois/NCSA +Open Source License + +Copyright (c) 2009-2019 by the contributors listed in CREDITS.TXT + +All rights reserved. + +Developed by: + + LLVM Team + + University of Illinois at Urbana-Champaign + + http://llvm.org + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimers. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimers in the + documentation and/or other materials provided with the distribution. + + * Neither the names of the LLVM Team, University of Illinois at + Urbana-Champaign, nor the names of its contributors may be used to + endorse or promote products derived from this Software without specific + prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE +SOFTWARE. + +============================================================================== + +Copyright (c) 2009-2014 by the contributors listed in CREDITS.TXT + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/crates/v8/licenses/third_party/libc++abi/src/LICENSE.TXT b/crates/v8/licenses/third_party/libc++abi/src/LICENSE.TXT new file mode 100644 index 0000000000..b75c0441bc --- /dev/null +++ b/crates/v8/licenses/third_party/libc++abi/src/LICENSE.TXT @@ -0,0 +1,311 @@ +============================================================================== +The LLVM Project is under the Apache License v2.0 with LLVM Exceptions: +============================================================================== + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +---- LLVM Exceptions to the Apache 2.0 License ---- + +As an exception, if, as a result of your compiling your source code, portions +of this Software are embedded into an Object form of such source code, you +may redistribute such embedded portions in such Object form without complying +with the conditions of Sections 4(a), 4(b) and 4(d) of the License. + +In addition, if you combine or link compiled forms of this Software with +software that is licensed under the GPLv2 ("Combined Software") and if a +court of competent jurisdiction determines that the patent provision (Section +3), the indemnity provision (Section 9) or other Section of the License +conflicts with the conditions of the GPLv2, you may retroactively and +prospectively choose to deem waived or otherwise exclude such Section(s) of +the License, but only in their entirety and only with respect to the Combined +Software. + +============================================================================== +Software from third parties included in the LLVM Project: +============================================================================== +The LLVM Project contains third party software which is under different license +terms. All such code will be identified clearly using at least one of two +mechanisms: +1) It will be in a separate directory tree with its own `LICENSE.txt` or + `LICENSE` file at the top containing the specific license and restrictions + which apply to that software, or +2) It will contain specific license and restriction terms at the top of every + file. + +============================================================================== +Legacy LLVM License (https://llvm.org/docs/DeveloperPolicy.html#legacy): +============================================================================== + +The libc++abi library is dual licensed under both the University of Illinois +"BSD-Like" license and the MIT license. As a user of this code you may choose +to use it under either license. As a contributor, you agree to allow your code +to be used under both. + +Full text of the relevant licenses is included below. + +============================================================================== + +University of Illinois/NCSA +Open Source License + +Copyright (c) 2009-2019 by the contributors listed in CREDITS.TXT + +All rights reserved. + +Developed by: + + LLVM Team + + University of Illinois at Urbana-Champaign + + http://llvm.org + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimers. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimers in the + documentation and/or other materials provided with the distribution. + + * Neither the names of the LLVM Team, University of Illinois at + Urbana-Champaign, nor the names of its contributors may be used to + endorse or promote products derived from this Software without specific + prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE +SOFTWARE. + +============================================================================== + +Copyright (c) 2009-2014 by the contributors listed in CREDITS.TXT + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/crates/v8/licenses/third_party/libunwind/src/LICENSE.TXT b/crates/v8/licenses/third_party/libunwind/src/LICENSE.TXT new file mode 100644 index 0000000000..1e3120621c --- /dev/null +++ b/crates/v8/licenses/third_party/libunwind/src/LICENSE.TXT @@ -0,0 +1,311 @@ +============================================================================== +The LLVM Project is under the Apache License v2.0 with LLVM Exceptions: +============================================================================== + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +---- LLVM Exceptions to the Apache 2.0 License ---- + +As an exception, if, as a result of your compiling your source code, portions +of this Software are embedded into an Object form of such source code, you +may redistribute such embedded portions in such Object form without complying +with the conditions of Sections 4(a), 4(b) and 4(d) of the License. + +In addition, if you combine or link compiled forms of this Software with +software that is licensed under the GPLv2 ("Combined Software") and if a +court of competent jurisdiction determines that the patent provision (Section +3), the indemnity provision (Section 9) or other Section of the License +conflicts with the conditions of the GPLv2, you may retroactively and +prospectively choose to deem waived or otherwise exclude such Section(s) of +the License, but only in their entirety and only with respect to the Combined +Software. + +============================================================================== +Software from third parties included in the LLVM Project: +============================================================================== +The LLVM Project contains third party software which is under different license +terms. All such code will be identified clearly using at least one of two +mechanisms: +1) It will be in a separate directory tree with its own `LICENSE.txt` or + `LICENSE` file at the top containing the specific license and restrictions + which apply to that software, or +2) It will contain specific license and restriction terms at the top of every + file. + +============================================================================== +Legacy LLVM License (https://llvm.org/docs/DeveloperPolicy.html#legacy): +============================================================================== + +The libunwind library is dual licensed under both the University of Illinois +"BSD-Like" license and the MIT license. As a user of this code you may choose +to use it under either license. As a contributor, you agree to allow your code +to be used under both. + +Full text of the relevant licenses is included below. + +============================================================================== + +University of Illinois/NCSA +Open Source License + +Copyright (c) 2009-2019 by the contributors listed in CREDITS.TXT + +All rights reserved. + +Developed by: + + LLVM Team + + University of Illinois at Urbana-Champaign + + http://llvm.org + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimers. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimers in the + documentation and/or other materials provided with the distribution. + + * Neither the names of the LLVM Team, University of Illinois at + Urbana-Champaign, nor the names of its contributors may be used to + endorse or promote products derived from this Software without specific + prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE +SOFTWARE. + +============================================================================== + +Copyright (c) 2009-2014 by the contributors listed in CREDITS.TXT + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/crates/v8/licenses/third_party/llvm-libc/src/LICENSE.TXT b/crates/v8/licenses/third_party/llvm-libc/src/LICENSE.TXT new file mode 100644 index 0000000000..24806ab4c9 --- /dev/null +++ b/crates/v8/licenses/third_party/llvm-libc/src/LICENSE.TXT @@ -0,0 +1,278 @@ +============================================================================== +The LLVM Project is under the Apache License v2.0 with LLVM Exceptions: +============================================================================== + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +---- LLVM Exceptions to the Apache 2.0 License ---- + +As an exception, if, as a result of your compiling your source code, portions +of this Software are embedded into an Object form of such source code, you +may redistribute such embedded portions in such Object form without complying +with the conditions of Sections 4(a), 4(b) and 4(d) of the License. + +In addition, if you combine or link compiled forms of this Software with +software that is licensed under the GPLv2 ("Combined Software") and if a +court of competent jurisdiction determines that the patent provision (Section +3), the indemnity provision (Section 9) or other Section of the License +conflicts with the conditions of the GPLv2, you may retroactively and +prospectively choose to deem waived or otherwise exclude such Section(s) of +the License, but only in their entirety and only with respect to the Combined +Software. + +============================================================================== +Software from third parties included in the LLVM Project: +============================================================================== +The LLVM Project contains third party software which is under different license +terms. All such code will be identified clearly using at least one of two +mechanisms: +1) It will be in a separate directory tree with its own `LICENSE.txt` or + `LICENSE` file at the top containing the specific license and restrictions + which apply to that software, or +2) It will contain specific license and restriction terms at the top of every + file. + +============================================================================== +Legacy LLVM License (https://llvm.org/docs/DeveloperPolicy.html#legacy): +============================================================================== +University of Illinois/NCSA +Open Source License + +Copyright (c) 2007-2019 University of Illinois at Urbana-Champaign. +All rights reserved. + +Developed by: + + LLVM Team + + University of Illinois at Urbana-Champaign + + http://llvm.org + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimers. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimers in the + documentation and/or other materials provided with the distribution. + + * Neither the names of the LLVM Team, University of Illinois at + Urbana-Champaign, nor the names of its contributors may be used to + endorse or promote products derived from this Software without specific + prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE +SOFTWARE. diff --git a/crates/v8/licenses/third_party/markupsafe/LICENSE b/crates/v8/licenses/third_party/markupsafe/LICENSE new file mode 100644 index 0000000000..5d2693890d --- /dev/null +++ b/crates/v8/licenses/third_party/markupsafe/LICENSE @@ -0,0 +1,33 @@ +Copyright (c) 2010 by Armin Ronacher and contributors. See AUTHORS +for more details. + +Some rights reserved. + +Redistribution and use in source and binary forms of the software as well +as documentation, with or without modification, are permitted provided +that the following conditions are met: + +* Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + +* The names of the contributors may not be used to endorse or + promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT +NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER +OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +DAMAGE. diff --git a/crates/v8/licenses/third_party/simdutf/LICENSE b/crates/v8/licenses/third_party/simdutf/LICENSE new file mode 100644 index 0000000000..74c8302ba9 --- /dev/null +++ b/crates/v8/licenses/third_party/simdutf/LICENSE @@ -0,0 +1,18 @@ +Copyright 2021 The simdutf authors + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/crates/v8/licenses/v8/LICENSE b/crates/v8/licenses/v8/LICENSE new file mode 100644 index 0000000000..9898b2d483 --- /dev/null +++ b/crates/v8/licenses/v8/LICENSE @@ -0,0 +1,61 @@ +This license applies to all parts of V8 that are not externally +maintained libraries. The externally maintained libraries used by V8 +are: + + - PCRE test suite, located in + test/mjsunit/third_party/regexp-pcre/regexp-pcre.js. This is based on the + test suite from PCRE-7.3, which is copyrighted by the University + of Cambridge and Google, Inc. The copyright notice and license + are embedded in regexp-pcre.js. + + - Layout tests, located in test/mjsunit/third_party/object-keys. These are + based on layout tests from webkit.org which are copyrighted by + Apple Computer, Inc. and released under a 3-clause BSD license. + + - Strongtalk assembler, the basis of the files assembler-arm-inl.h, + assembler-arm.cc, assembler-arm.h, assembler-ia32-inl.h, + assembler-ia32.cc, assembler-ia32.h, assembler-x64-inl.h, + assembler-x64.cc, assembler-x64.h, assembler.cc and assembler.h. + This code is copyrighted by Sun Microsystems Inc. and released + under a 3-clause BSD license. + + - Valgrind client API header, located at third_party/valgrind/valgrind.h + This is released under the BSD license. + + - The Wasm C/C++ API headers, located at third_party/wasm-api/wasm.{h,hh} + This is released under the Apache license. The API's upstream prototype + implementation also formed the basis of V8's implementation in + src/wasm/c-api.cc. + +These libraries have their own licenses; we recommend you read them, +as their terms may differ from the terms below. + +Further license information can be found in LICENSE files located in +sub-directories. + +Copyright 2014, the V8 project authors. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/crates/v8/licenses/v8/LICENSE.fdlibm b/crates/v8/licenses/v8/LICENSE.fdlibm new file mode 100644 index 0000000000..b54cb52278 --- /dev/null +++ b/crates/v8/licenses/v8/LICENSE.fdlibm @@ -0,0 +1,6 @@ +Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunSoft, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. diff --git a/crates/v8/licenses/v8/LICENSE.strongtalk b/crates/v8/licenses/v8/LICENSE.strongtalk new file mode 100644 index 0000000000..9bd62e4f23 --- /dev/null +++ b/crates/v8/licenses/v8/LICENSE.strongtalk @@ -0,0 +1,29 @@ +Copyright (c) 1994-2006 Sun Microsystems Inc. +All Rights Reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +- Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. + +- Redistribution in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +- Neither the name of Sun Microsystems or the names of contributors may +be used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/crates/v8/licenses/v8/LICENSE.v8 b/crates/v8/licenses/v8/LICENSE.v8 new file mode 100644 index 0000000000..933718a9ef --- /dev/null +++ b/crates/v8/licenses/v8/LICENSE.v8 @@ -0,0 +1,26 @@ +Copyright 2006-2011, the V8 project authors. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/crates/v8/provider/archives.json b/crates/v8/provider/archives.json new file mode 100644 index 0000000000..54fdf1f44d --- /dev/null +++ b/crates/v8/provider/archives.json @@ -0,0 +1,6 @@ +{ + "aarch64-apple-darwin": "07fcc9f69cfe977d8b3567c14e300f74ab5f842e0ee672c9121a32cf0b7f0cac", + "aarch64-unknown-linux-gnu": "72a71c276d8c44440cb25e412ec99f72bb5b331a81a62998f86d6e9bf5b471f8", + "x86_64-apple-darwin": "4bd58f1a1268448bd688eab504123570b6c73344faa73139ff1e17b728521b63", + "x86_64-unknown-linux-gnu": "6fea0688c054ab5046219ad231ec76ced6a35b188fce6e4282614fb1f1927389" +} diff --git a/crates/v8/provider/engine.py b/crates/v8/provider/engine.py new file mode 100644 index 0000000000..53048d5079 --- /dev/null +++ b/crates/v8/provider/engine.py @@ -0,0 +1,431 @@ +#!/usr/bin/env python3 +"""Prepare the locked engine tree and package or consume one matched target bundle.""" +import argparse +from contextlib import contextmanager +import gzip +import hashlib +import json +import os +from pathlib import Path +import re +import shutil +import subprocess +import sys +import tarfile +import tempfile +import urllib.request +import zipfile + +HERE = Path(__file__).resolve().parent +SOURCE = json.loads((HERE / "source.json").read_text()) +LINUX_X64_SYSROOT = json.loads((HERE / "linux-x64-sysroot.json").read_text()) +TARGETS = ("x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", + "x86_64-apple-darwin", "aarch64-apple-darwin") +ICU = "third_party/icu/common/icudtl.dat" +NOTICE_PATTERNS = ("LICENSE", "v8/LICENSE*", "third_party/*/LICENSE*", "third_party/*/src/LICENSE*") + + +def git(root, *args): + return subprocess.check_output(["git", "-C", str(root), *args], text=True).strip() + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def digest(path): + result = hashlib.sha256() + with path.open("rb") as stream: + for chunk in iter(lambda: stream.read(1024 * 1024), b""): + result.update(chunk) + return result.hexdigest() + + +def gitlinks(root): + return {path: revision for attrs in git(root, "ls-tree", "-r", "HEAD").splitlines() + for mode, kind, revision, path in [attrs.split(maxsplit=3)] if kind == "commit"} + + +def verify_source(root): + source_patches() + require(git(root, "rev-parse", "HEAD") == SOURCE["rustyV8"], "incorrect rusty_v8 revision") + git(root, "diff", "--quiet", "--ignore-submodules=all", "HEAD") + v8 = root / "v8" + require(git(v8, "write-tree") == SOURCE["v8Tree"], "incorrect patched V8 tree") + git(v8, "diff", "--quiet") + links = gitlinks(root) + require(links["third_party/icu"] == SOURCE["icu"], "incorrect ICU pin") + for path, revision in links.items(): + if path in ("v8", "tools/win"): + continue + require(git(root / path, "rev-parse", "HEAD") == revision, "incorrect revision: " + path) + git(root / path, "diff", "--quiet", "HEAD") + + +def source_patches(): + paths = [] + for patch in SOURCE["agentosPatches"]: + path = HERE / patch["file"] + require(digest(path) == patch["sha256"], f"agentOS V8 patch SHA-256 mismatch: {path.name}") + paths.append(path) + return paths + + +def install_rust_glue(checkout, dest, config): + # Base and PR checks may have different pins, or a released registry crate. + # Interpret the lock and pin from the checkout that Cargo will analyze. + locked = [entry for entry in re.split(r"^\[\[package\]\]$", (checkout / "Cargo.lock").read_text(), flags=re.M) + if re.search(r'^name = "v8"$', entry, re.M) and re.search(r'^source = "git\+', entry, re.M)] + if not locked: + return + revision = json.loads((checkout / "scripts/ci/v8/source.json").read_text())["rustyV8"] + require(re.fullmatch("[0-9a-f]{40}", revision), "invalid rusty_v8 revision") + repo = "https://github.com/denoland/rusty_v8" + identity = f'source = "git+{repo}?rev={revision}#{revision}"' + require(len(locked) == 1 and identity in locked[0].splitlines(), "rusty_v8 lock does not match this checkout's source pin") + dest.mkdir(parents=True, exist_ok=False) + vendor = dest / "vendor/v8" + vendor.mkdir(parents=True) + with tempfile.TemporaryDirectory(dir=dest) as work: + root = Path(work) + git(root, "init", "-q") + git(root, "fetch", "--no-recurse-submodules", "--no-tags", "--depth=1", repo, revision) + git(root, "checkout", "--no-recurse-submodules", "--detach", "FETCH_HEAD") + require(git(root, "rev-parse", "HEAD") == revision, "incorrect fetched Rust glue revision") + inputs = rust_crate_inputs(root) + for name, path in inputs.items(): + dst = vendor / name + dst.parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(path, dst) + (vendor / ".cargo-checksum.json").write_text(json.dumps( + {"package": None, "files": {name: digest(path) for name, path in inputs.items()}}, sort_keys=True)) + write_cargo_config(config, dest, revision) + + +def prepare(root): + require(not root.exists(), "prepare requires a new checkout directory") + root.mkdir(parents=True) + git(root, "init", "-q") + git(root, "remote", "add", "origin", "https://github.com/denoland/rusty_v8.git") + git(root, "fetch", "--depth=1", "--no-tags", "origin", SOURCE["rustyV8"]) + git(root, "checkout", "--detach", "FETCH_HEAD") + modules = sorted(gitlinks(root).keys() - {"v8", "tools/win"}) + git(root, "submodule", "update", "--init", "--depth=1", "--jobs=4", "--", *modules) + v8 = root / "v8" + git(v8, "init", "-q") + git(v8, "fetch", "--no-tags", "--depth=1", "https://github.com/denoland/v8.git", SOURCE["denoV8"]) + git(v8, "checkout", "--detach", "FETCH_HEAD") + patches = SOURCE["upstreamPatches"] + git(v8, "fetch", "--no-tags", "--depth=" + str(len(patches) + 1), + "https://github.com/v8/v8.git", patches[-1]) + git(v8, "cherry-pick", "--no-commit", *patches) + for patch in source_patches(): + git(v8, "apply", "--index", str(patch)) + verify_source(root) + + +def filenames(target): + return "librusty_v8_release_" + target + ".a", "src_binding_release_" + target + ".rs" + + +def rust_crate_inputs(source): + # Only the pinned Rust repository's blobs; a gitlink represents an entire + # native submodule and is intentionally absent from the binary consumer. + inputs = {} + for entry in git(source, "ls-tree", "-rz", "HEAD").split("\0"): + if entry: + attrs, name = entry.split("\t", 1) + mode, kind, _ = attrs.split() + if kind == "blob": + require(mode in ("100644", "100755"), "unexpected non-file Rust crate input: " + name) + inputs[name] = source / name + return inputs + + +def pack(source, build, target, output): + verify_source(source) + gn = build / target / "release" / "gn_out" + args = (gn / "args.gn").read_text() + settings = dict(re.findall(r"^(\w+)\s*=\s*(\S+)\s*$", args, re.M)) + expected = {"is_debug": "false", "v8_enable_sandbox": "false", "use_custom_libcxx": "true", + "v8_enable_pointer_compression": "false", "v8_enable_v8_checks": "false", + "v8_generate_external_defines_header": "true", + "target_cpu": '"arm64"' if target.startswith("aarch64") else '"x64"'} + require(all(settings.get(k) == v for k, v in expected.items()), "wrong GN ABI or target") + if target == "x86_64-unknown-linux-gnu": + sysroot = os.environ.get("AGENTOS_V8_RELEASE_SYSROOT") + require(sysroot and settings.get("sysroot") == json.dumps(sysroot), + "Linux x64 archive must use the verified release sysroot") + require(settings.get("use_glib") == "false", "standalone V8 must not use runner GLib headers") + if "apple" in target: + require(sys.platform == "darwin", "Darwin archive must be produced on macOS") + else: + require(sys.platform.startswith("linux"), "Linux archive must be produced on Linux") + archive, binding = filenames(target) + inputs = {archive: gn / "obj/librusty_v8.a", binding: gn / "src_binding.rs", + "generated/v8-gn.h": gn / "gen/v8/include/v8-gn.h", ICU: source / ICU} + for header in sorted((source / "v8/include").rglob("*.h")): + inputs[header.relative_to(source).as_posix()] = header + for pattern in NOTICE_PATTERNS: + for notice in source.glob(pattern): + if notice.is_file(): + inputs["licenses/" + notice.relative_to(source).as_posix()] = notice + output.parent.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(dir=output.parent) as work: + work = Path(work) + crate = rust_crate_inputs(source) + checksums = work / ".cargo-checksum.json" + checksums.write_text(json.dumps({"package": None, "files": {name: digest(path) for name, path in crate.items()}}, sort_keys=True)) + inputs.update({"vendor/v8/" + name: path for name, path in crate.items()}) + inputs["vendor/v8/.cargo-checksum.json"] = checksums + receipt = {"source": SOURCE, "target": target, "gn": expected, + "sha256": {name: digest(path) for name, path in inputs.items()}} + if target == "x86_64-unknown-linux-gnu": + receipt["releaseSysroot"] = LINUX_X64_SYSROOT + manifest = work / "provenance.json" + manifest.write_text(json.dumps(receipt, sort_keys=True, indent=2) + "\n") + inputs[manifest.name] = manifest + packed = work / "bundle.tar.gz" + with packed.open("wb") as raw, gzip.GzipFile(filename="", mode="wb", fileobj=raw, mtime=0, compresslevel=1) as zipped: + with tarfile.open(fileobj=zipped, mode="w|") as tar: + for name, path in sorted(inputs.items()): + info = tarfile.TarInfo(name) + info.size, info.mode = path.stat().st_size, 0o644 + with path.open("rb") as data: + tar.addfile(info, data) + os.replace(packed, output) + print(digest(output), output.name) + + +def verify_bundle(root, target): + manifest = json.loads((root / "provenance.json").read_text()) + require(manifest["source"] == SOURCE and manifest["target"] == target, "V8 source or target mismatch") + if target == "x86_64-unknown-linux-gnu": + require(manifest.get("releaseSysroot") == LINUX_X64_SYSROOT, + "Linux x64 bundle lacks matching release sysroot provenance") + archive, binding = filenames(target) + required = {archive, binding, "generated/v8-gn.h", "v8/include/v8.h", ICU, + "licenses/LICENSE", "licenses/v8/LICENSE", "licenses/third_party/icu/LICENSE", + "vendor/v8/Cargo.toml", "vendor/v8/build.rs", "vendor/v8/src/lib.rs", "vendor/v8/.cargo-checksum.json"} + hashes = manifest["sha256"] + require(required <= hashes.keys(), "incomplete V8 bundle") + present = {p.relative_to(root).as_posix() for p in root.rglob("*") if p.is_file()} + require(present == hashes.keys() | {"provenance.json"}, "unexpected or missing V8 bundle files") + for name, checksum in hashes.items(): + notice = name.startswith("licenses/") and Path(name).name.startswith("LICENSE") + require(name in required or notice or name.startswith("vendor/v8/") or (name.startswith("v8/include/") and name.endswith(".h")), "unexpected bundle file " + name) + require(digest(root / name) == checksum, "V8 bundle digest mismatch: " + name) + + +def unpack(bundle, root): + with tarfile.open(bundle, "r:gz") as tar: + for member in tar: + path = Path(member.name) + require(member.isfile() and not path.is_absolute() and ".." not in path.parts, "unexpected archive entry") + dest = root / path + dest.parent.mkdir(parents=True, exist_ok=True) + with tar.extractfile(member) as data, dest.open("xb") as out: + shutil.copyfileobj(data, out) + + +def download_action_bundle(artifact_id, target, work): + require(type(artifact_id) is int and artifact_id > 0, "invalid V8 Actions artifact ID") + zip_path = work / "artifact.zip" + with zip_path.open("wb") as out: + subprocess.run(["gh", "api", f"repos/rivet-dev/agentos/actions/artifacts/{artifact_id}/zip"], + stdout=out, check=True) + with zipfile.ZipFile(zip_path) as archive: + # Read the one expected entry directly; never extract arbitrary ZIP paths. + with archive.open(target + ".tar.gz") as data, (work / "bundle").open("wb") as out: + shutil.copyfileobj(data, out) + + +def pin_for(target, allow_actions): + pin = json.loads((HERE / "archives.json").read_text()).get(target) + require(pin, "No reviewed V8 artifact SHA-256 for " + target + ". See scripts/ci/v8/README.md.") + if isinstance(pin, dict): + require(allow_actions, "Release builds require a durable V8 archive pin for " + target) + require(pin.keys() == {"sha256", "artifactId"}, "invalid temporary V8 artifact pin") + require(type(pin["artifactId"]) is int and pin["artifactId"] > 0, "invalid V8 Actions artifact ID") + checksum, artifact_id = pin["sha256"], pin["artifactId"] + else: + checksum, artifact_id = pin, None + require(re.fullmatch(r"[a-f0-9]{64}", checksum or ""), "invalid reviewed V8 checksum") + return checksum, artifact_id + + +@contextmanager +def reviewed_archive(target, dest, allow_actions): + checksum, artifact_id = pin_for(target, allow_actions) + with tempfile.TemporaryDirectory(dir=dest) as work: + work = Path(work) + if artifact_id is not None: + download_action_bundle(artifact_id, target, work) + else: + url = f"https://releases.rivet.dev/agent-os/v8/{SOURCE['v8Tree']}/{checksum}/{target}.tar.gz" + request = urllib.request.Request(url, headers={"User-Agent": "agentos-v8/" + SOURCE["version"]}) + with urllib.request.urlopen(request, timeout=120) as response, (work / "bundle").open("wb") as out: + shutil.copyfileobj(response, out) + require(digest(work / "bundle") == checksum, "V8 archive SHA-256 mismatch") + yield work / "bundle", checksum + + +def install(target, dest, local_bundle=None, expected_digest=None, allow_actions=False): + dest.mkdir(parents=True, exist_ok=True) + if local_bundle is None: + require(expected_digest is None, "--sha256 requires --bundle") + with reviewed_archive(target, dest, allow_actions) as (bundle, checksum): + return install_verified(bundle, checksum, target, dest) + require(re.fullmatch(r"[a-f0-9]{64}", expected_digest or ""), "expected an independent bundle SHA-256") + require(digest(local_bundle) == expected_digest, "V8 archive SHA-256 mismatch") + return install_verified(local_bundle, expected_digest, target, dest) + + +def install_verified(local_bundle, expected_digest, target, dest): + with tempfile.TemporaryDirectory(dir=dest) as work: + work = Path(work) + root = work / "verified" + root.mkdir() + unpack(local_bundle, root) + verify_bundle(root, target) + ready = dest / expected_digest + try: + root.rename(ready) + except OSError: + if not ready.is_dir(): + raise + # Another consumer installed the same pinned bytes. Validate every + # existing file against the newly authenticated manifest. + require((ready / "provenance.json").read_bytes() == (root / "provenance.json").read_bytes(), "cached V8 provenance mismatch") + verify_bundle(ready, target) + return verified_env_paths(ready, target) + + +def verified_env_paths(ready, target): + archive, binding = filenames(target) + hashes = json.loads((ready / "provenance.json").read_text())["sha256"] + return {"RUSTY_V8_ARCHIVE": str(ready / archive), "RUSTY_V8_ARCHIVE_SHA256": hashes[archive], + "RUSTY_V8_SRC_BINDING_PATH": str(ready / binding), "AGENTOS_RUSTY_V8_SOURCE": str(ready), + "AGENTOS_RUSTY_V8_GN_INCLUDE": str(ready / "generated"), "V8_FROM_SOURCE": "0", + "RUSTY_V8_SKIP_DOWNLOAD": "0", "V8_FORCE_DEBUG": "0"} + + +def write_cargo_config(path, root, revision=None): + revision = revision or SOURCE["rustyV8"] + config = ('[source.agentos-rusty-v8]\ngit = "https://github.com/denoland/rusty_v8"\n' + f'rev = "{revision}"\nreplace-with = "agentos-v8-vendor"\n' + f'[source.agentos-v8-vendor]\ndirectory = {json.dumps(str(root / "vendor"))}\n') + path.parent.mkdir(parents=True, exist_ok=True) + try: + with path.open("x") as out: + out.write(config) + except FileExistsError: + require(path.read_text() == config, f"Cargo config already exists at {path}; specify a new --cargo-config path") + + +def write_consumer_lock(source, output): + # Cargo needs a locked Git identity before it can use a directory source. + # The producer's lock already has the complete dependency graph; its root + # package becomes a Git dependency only in the standalone version check. + revision = SOURCE["rustyV8"] + data = git(source, "show", revision + ":Cargo.lock") + entry = r'^(\[\[package\]\]\nname = "v8"\nversion = "[^"\n]+"\n)(?=dependencies = \[)' + identity = f'source = "git+https://github.com/denoland/rusty_v8?rev={revision}#{revision}"\n' + locked, count = re.subn(entry, lambda m: m.group(1) + identity, data, flags=re.M) + require(count == 1, "expected one local v8 package in the pinned rusty_v8 Cargo.lock") + with output.open("x") as out: + out.write(locked + "\n") + + +def temporal_dependencies(lock): + names = {"temporal_capi", "temporal_rs", "timezone_provider", "zoneinfo64", "diplomat", "diplomat-runtime"} + result = {} + for package in re.split(r"^\[\[package\]\]$", lock.read_text(), flags=re.M): + entry = dict(re.findall(r'^(name|version|checksum) = "([^"]+)"$', package, re.M)) + name = entry.get("name") + if name in names: + require(name not in result, f"ambiguous Temporal dependency {name}") + result[name] = (entry["version"], entry["checksum"]) + require(result.keys() == names, "missing Temporal ABI dependency in Cargo.lock") + return result + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + cmds = parser.add_subparsers(dest="command", required=True) + consumer_lock = cmds.add_parser("consumer-lock") + consumer_lock.add_argument("source", type=Path) + consumer_lock.add_argument("output", type=Path) + temporal = cmds.add_parser("check-temporal-lock") + temporal.add_argument("producer", type=Path) + temporal.add_argument("consumer", type=Path) + glue = cmds.add_parser("install-glue", help="prepare only Rust source for metadata tools") + glue.add_argument("checkout", type=Path) + glue.add_argument("dest", type=Path) + glue.add_argument("--cargo-config", required=True, type=Path) + fetch = cmds.add_parser("fetch", help="stage a reviewed bundle for a separate build context") + fetch.add_argument("target", choices=TARGETS) + fetch.add_argument("dest", type=Path) + fetch.add_argument("--allow-actions", action="store_true") + for name in ("prepare", "check-source", "pack"): + cmd = cmds.add_parser(name) + cmd.add_argument("source", type=Path) + if name == "pack": + cmd.add_argument("build", type=Path) + cmd.add_argument("target", choices=TARGETS) + cmd.add_argument("output", type=Path) + cmd = cmds.add_parser("install") + cmd.add_argument("target", choices=TARGETS) + cmd.add_argument("dest", type=Path) + cmd.add_argument("--bundle", type=Path) + cmd.add_argument("--sha256") + cmd.add_argument("--build-context", type=Path, help="use a staged target bundle if supplied") + cmd.add_argument("--allow-actions", action="store_true", help="allow draft artifact pins, using gh with Actions read permission") + cmd.add_argument("--cargo-config", type=Path, help="write this additional Cargo config for the pinned Git dependency") + activation = cmd.add_mutually_exclusive_group() + activation.add_argument("--github-env", type=Path) + activation.add_argument("--shell-env", type=Path) + args = parser.parse_args() + if args.command == "install-glue": + install_rust_glue(args.checkout, args.dest.resolve(), args.cargo_config) + elif args.command == "fetch": + args.dest.mkdir(parents=True, exist_ok=True) + with reviewed_archive(args.target, args.dest, args.allow_actions) as (bundle, _): + bundle.replace(args.dest / (args.target + ".tar.gz")) + elif args.command == "check-temporal-lock": + require(temporal_dependencies(args.producer) == temporal_dependencies(args.consumer), "Temporal ABI dependency mismatch") + elif args.command == "consumer-lock": + write_consumer_lock(args.source, args.output) + elif args.command == "prepare": + prepare(args.source) + elif args.command == "check-source": + verify_source(args.source) + elif args.command == "pack": + pack(args.source, args.build, args.target, args.output) + else: + if args.build_context: + require(args.bundle is None and args.sha256 is None, "--build-context and --bundle cannot be combined") + staged = args.build_context / (args.target + ".tar.gz") + if staged.is_file(): + args.bundle = staged + args.sha256, _ = pin_for(args.target, allow_actions=True) + env = install(args.target, args.dest.resolve(), args.bundle, args.sha256, args.allow_actions) + if args.cargo_config: + write_cargo_config(args.cargo_config, Path(env["AGENTOS_RUSTY_V8_SOURCE"])) + if args.github_env: + require(all("\n" not in value and "\r" not in value for value in env.values()), "invalid environment path") + with args.github_env.open("a") as out: + out.writelines(f"{k}={v}\n" for k, v in env.items()) + elif args.shell_env: + import shlex + args.shell_env.write_text("".join(f"export {k}={shlex.quote(v)}\n" for k, v in env.items())) + else: + print(json.dumps(env, indent=2)) + + +if __name__ == "__main__": + try: + main() + except (OSError, ValueError, KeyError, tarfile.TarError, subprocess.CalledProcessError) as exc: + sys.exit(str(exc)) diff --git a/crates/v8/provider/linux-x64-sysroot.json b/crates/v8/provider/linux-x64-sysroot.json new file mode 100644 index 0000000000..e9b77c8242 --- /dev/null +++ b/crates/v8/provider/linux-x64-sysroot.json @@ -0,0 +1,5 @@ +{ + "url": "https://github.com/denoland/deno_sysroot_build/releases/download/sysroot-20250207/sysroot-x86_64.tar.xz", + "sha256": "64513b04d8a38da6f47c0a9c4cb47b2ce7bcb735250b10eb10751aec6afe46ee", + "glibcFloor": "2.27" +} diff --git a/crates/v8/provider/prepare.py b/crates/v8/provider/prepare.py new file mode 100644 index 0000000000..43ae0952fc --- /dev/null +++ b/crates/v8/provider/prepare.py @@ -0,0 +1,17 @@ +"""Resolve and verify the fixed native ABI used by this binding package.""" +from pathlib import Path +import os +import sys +import engine + +target = sys.argv[1] +engine.require(target in engine.TARGETS, "unsupported agentOS V8 target: " + target) +out = Path(os.environ["OUT_DIR"]) +local = os.environ.get("AGENTOS_RUSTY_V8_SOURCE") +if local: + root = Path(local).resolve() + engine.verify_bundle(root, target) +else: + env = engine.install(target, out / "native") + root = Path(env["AGENTOS_RUSTY_V8_SOURCE"]) +(out / "bundle-root").write_text(str(root)) diff --git a/crates/v8/provider/source.json b/crates/v8/provider/source.json new file mode 100644 index 0000000000..095d1101be --- /dev/null +++ b/crates/v8/provider/source.json @@ -0,0 +1,25 @@ +{ + "rustyV8": "548adfd54db42c670e0f3fd42211c3af109c0a73", + "denoV8": "ac3d6e2607b8a240131d47da8e97dca0965a457c", + "upstreamPatches": [ + "d49521fd906188ecaeb3fcb9e8e8b20955fe8290", + "6b7ccfda00c9525c1edf0fddfd0da85042164a7c", + "0b690e4bf2bb9c37fd870408588c6eadd3ae9761", + "2f24dea22a2931666a8d32f36c20e0607dccea94", + "d03e7ab1999d78f7b20cb3e7901de1dc3f46e8c0", + "79c1ad6a9cecc8b7f759b266caed1f3eee10a0d5" + ], + "agentosPatches": [ + { + "file": "cppgc-generated-config.patch", + "sha256": "48640d3017eb66382df416b6ddfcd683bb1b1b02543afa2e1fd73033357b71b7" + }, + { + "file": "node-wire-format-15.patch", + "sha256": "462897ebb1039638fdbbf6ad9989975c2677445e107a8b024d60dee4c319e244" + } + ], + "v8Tree": "91306fc87da850ec8b222f0030b37d2da90a6dea", + "version": "15.8.62.58", + "icu": "2a51536dfb3b448bdf3dae70c782ea2879c90904" +} diff --git a/crates/v8/rustfmt.toml b/crates/v8/rustfmt.toml new file mode 100644 index 0000000000..4c1eefaed5 --- /dev/null +++ b/crates/v8/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 80 +tab_spaces = 2 diff --git a/crates/v8/src/V8.rs b/crates/v8/src/V8.rs new file mode 100644 index 0000000000..a3c66329fe --- /dev/null +++ b/crates/v8/src/V8.rs @@ -0,0 +1,349 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. +use std::ffi::CStr; +use std::ffi::CString; +use std::sync::Mutex; +use std::vec::Vec; + +use crate::platform::Platform; +use crate::support::MapFnFrom; +use crate::support::MapFnTo; +use crate::support::SharedRef; +use crate::support::ToCFn; +use crate::support::UnitType; +use crate::support::char; +use crate::support::int; + +unsafe extern "C" { + fn v8__V8__SetFlagsFromCommandLine( + argc: *mut int, + argv: *mut *mut char, + usage: *const char, + ); + fn v8__V8__SetFlagsFromString(flags: *const u8, length: usize); + fn v8__V8__SetEntropySource(callback: EntropySource); + fn v8__V8__GetVersion() -> *const char; + fn v8__V8__InitializePlatform(platform: *mut Platform); + fn v8__V8__Initialize(); + fn v8__V8__Dispose() -> bool; + fn v8__V8__DisposePlatform(); + fn v8__V8__SetFatalErrorHandler(that: V8FatalErrorCallback); + fn v8__V8__EnableWebAssemblyTrapHandler(use_v8_signal_handler: bool) -> bool; +} + +pub type V8FatalErrorCallback = unsafe extern "C" fn( + file: *const std::ffi::c_char, + line: std::ffi::c_int, + message: *const std::ffi::c_char, +); + +/// EntropySource is used as a callback function when v8 needs a source +/// of entropy. +#[derive(Copy, Clone)] +#[repr(transparent)] +pub struct EntropySource(RawEntropySource); + +pub trait IntoEntropySource: + UnitType + Into + FnOnce(&mut [u8]) -> bool +{ +} + +impl IntoEntropySource for F where + F: UnitType + Into + FnOnce(&mut [u8]) -> bool +{ +} + +type RawEntropySource = unsafe extern "C" fn(*mut u8, usize) -> bool; + +impl From for EntropySource +where + F: UnitType + FnOnce(&mut [u8]) -> bool, +{ + fn from(_: F) -> Self { + #[inline(always)] + unsafe extern "C" fn adapter( + buffer: *mut u8, + length: usize, + ) -> bool { + let buffer = unsafe { std::slice::from_raw_parts_mut(buffer, length) }; + (F::get())(buffer) + } + + Self(adapter::) + } +} + +#[derive(Debug)] +enum GlobalState { + Uninitialized, + PlatformInitialized(SharedRef), + Initialized(SharedRef), + Disposed(#[allow(dead_code)] SharedRef), + PlatformShutdown, +} +use GlobalState::*; + +static GLOBAL_STATE: Mutex = Mutex::new(Uninitialized); + +pub fn assert_initialized() { + let global_state_guard = GLOBAL_STATE.lock().unwrap(); + match *global_state_guard { + Initialized(_) => {} + _ => panic!("Invalid global state"), + }; +} + +/// Pass the command line arguments to v8. +/// The first element of args (which usually corresponds to the binary name) is +/// ignored. +/// Returns a vector of command line arguments that V8 did not understand. +/// TODO: Check whether this is safe to do after globally initializing v8. +pub fn set_flags_from_command_line(args: Vec) -> Vec { + set_flags_from_command_line_with_usage(args, None) +} + +/// The example code below is here to avoid the V8 usage string and options +/// that are printed to stdout by this function. Placing this here instead of +/// in a test allows the output to be suppressed. +/// +/// # Examples +/// +/// ``` +/// let r = v8::V8::set_flags_from_command_line_with_usage( +/// vec!["binaryname".to_string(), "--help".to_string()], +/// Some("Usage: binaryname --startup-src=file\n\n"), +/// ); +/// assert_eq!(r, vec!["binaryname".to_string()]); +/// ``` +pub fn set_flags_from_command_line_with_usage( + args: Vec, + usage: Option<&str>, +) -> Vec { + // deno_set_v8_flags(int* argc, char** argv) mutates argc and argv to remove + // flags that v8 understands. + + // Make a new array, that can be modified by V8::SetFlagsFromCommandLine(), + // containing mutable raw pointers to the individual command line args. + let mut raw_argv = args + .iter() + .map(|arg| CString::new(arg.as_str()).unwrap().into_bytes_with_nul()) + .collect::>(); + let mut c_argv = raw_argv + .iter_mut() + .map(|arg| arg.as_mut_ptr() as *mut char) + .collect::>(); + + // Store the length of the c_argv array in a local variable. We'll pass + // a pointer to this local variable to deno_set_v8_flags(), which then + // updates its value. + let mut c_argv_len = c_argv.len() as int; + // Let v8 parse the arguments it recognizes and remove them from c_argv. + let c_usage = match usage { + Some(str) => CString::new(str).unwrap().into_raw() as *const char, + None => std::ptr::null(), + }; + unsafe { + v8__V8__SetFlagsFromCommandLine( + &mut c_argv_len, + c_argv.as_mut_ptr(), + c_usage, + ); + }; + // If c_argv_len was updated we have to change the length of c_argv to match. + c_argv.truncate(c_argv_len as usize); + // Copy the modified arguments list into a proper rust vec and return it. + c_argv + .iter() + .map(|ptr| unsafe { + let cstr = CStr::from_ptr(*ptr as *const char); + let slice = cstr.to_str().unwrap(); + slice.to_string() + }) + .collect() +} + +/// Sets V8 flags from a string. +pub fn set_flags_from_string(flags: &str) { + unsafe { + v8__V8__SetFlagsFromString(flags.as_ptr(), flags.len()); + } +} + +/// Allows the host application to provide a callback which can be used +/// as a source of entropy for random number generators. +pub fn set_entropy_source( + callback: impl UnitType + Into + FnOnce(&mut [u8]) -> bool, +) { + unsafe { v8__V8__SetEntropySource(callback.into()) }; +} + +/// Get the version string. +pub fn get_version() -> &'static str { + let version = unsafe { v8__V8__GetVersion() }; + let c_str = unsafe { CStr::from_ptr(version) }; + c_str.to_str().unwrap() +} + +/// Sets the v8::Platform to use. This should be invoked before V8 is +/// initialized. +pub fn initialize_platform(platform: SharedRef) { + let mut global_state_guard = GLOBAL_STATE.lock().unwrap(); + *global_state_guard = match *global_state_guard { + Uninitialized => PlatformInitialized(platform.clone()), + _ => panic!("Invalid global state"), + }; + + { + unsafe { + v8__V8__InitializePlatform(&*platform as *const Platform as *mut _); + }; + } +} + +/// Initializes V8. This function needs to be called before the first Isolate +/// is created. It always returns true. +/// +/// # Call this before creating threads that will enter an isolate +/// +/// On platforms with memory protection keys (x86-64 with `pku`/`ospke`, +/// which covers most current server hardware), V8 allocates a protection +/// key here and uses it to guard its internal pointer tables. Access to a +/// protection key is governed by the `PKRU` register, which is **per +/// thread** and is **inherited from the creating thread at thread creation +/// time**. +/// +/// A thread created *before* this call therefore never gains access to that +/// key. Such a thread runs correctly until it enters an isolate, at which +/// point its first read of a pointer table traps with `SIGSEGV` and +/// `si_code == SEGV_PKUERR`. The faulting address lies in a mapping that +/// `/proc//maps` reports as present and readable, because protection +/// keys are enforced by the MMU and appear in no mapping flag. +/// +/// This matters most for embedders that build a thread pool up front and +/// then enter a [`SharedIsolate`](crate::SharedIsolate) from its workers: +/// +/// ```no_run +/// // Correct: the pool inherits access to the key. +/// v8::V8::initialize_platform( +/// v8::new_default_platform(0, false).make_shared(), +/// ); +/// v8::V8::initialize(); +/// let pool = std::thread::spawn(|| { /* may enter an isolate */ }); +/// # let _ = pool; +/// ``` +/// +/// Creating the pool first compiles and runs, and fails only once a worker +/// first executes JavaScript. V8 states the same requirement internally, in +/// the name of `SandboxHardwareSupport::TryActivateBeforeThreadCreation`. +pub fn initialize() { + let mut global_state_guard = GLOBAL_STATE.lock().unwrap(); + *global_state_guard = match *global_state_guard { + PlatformInitialized(ref platform) => Initialized(platform.clone()), + _ => panic!("Invalid global state"), + }; + unsafe { v8__V8__Initialize() } +} + +/// Sets the v8::Platform to use. This should be invoked before V8 is +/// initialized. +pub fn get_current_platform() -> SharedRef { + let global_state_guard = GLOBAL_STATE.lock().unwrap(); + match *global_state_guard { + Initialized(ref platform) => platform.clone(), + _ => panic!("Invalid global state"), + } +} + +/// Releases any resources used by v8 and stops any utility threads +/// that may be running. Note that disposing v8 is permanent, it +/// cannot be reinitialized. +/// +/// It should generally not be necessary to dispose v8 before exiting +/// a process, this should happen automatically. It is only necessary +/// to use if the process needs the resources taken up by v8. +/// +/// # Safety +/// +/// Calling this function before completely disposing all isolates and dropping +/// all [`crate::IsolateGroup`] handles will lead to a crash. +pub unsafe fn dispose() -> bool { + let mut global_state_guard = GLOBAL_STATE.lock().unwrap(); + *global_state_guard = match *global_state_guard { + Initialized(ref platform) => Disposed(platform.clone()), + _ => panic!("Invalid global state"), + }; + assert!(unsafe { v8__V8__Dispose() }); + true +} + +/// Clears all references to the v8::Platform. This should be invoked after +/// V8 was disposed. If it is called if V8 is not disposed, it will panic. +pub fn dispose_platform() { + let mut global_state_guard = GLOBAL_STATE.lock().unwrap(); + // First check that the global state is disposed. If it is we dispose the + // platform. We then drop the platform. + *global_state_guard = match *global_state_guard { + Disposed(_) => { + unsafe { v8__V8__DisposePlatform() }; + PlatformShutdown + } + _ => panic!("Invalid global state"), + }; +} + +impl MapFnFrom for V8FatalErrorCallback +where + F: UnitType + Fn(&str, i32, &str), +{ + fn mapping() -> Self { + let f = |file, line, message| unsafe { + let file = std::ffi::CStr::from_ptr(file).to_str().unwrap_or_default(); + let message = std::ffi::CStr::from_ptr(message) + .to_str() + .unwrap_or_default(); + (F::get())(file, line, message); + }; + f.to_c_fn() + } +} + +/// Set the callback to invoke in the case of CHECK failures or fatal +/// errors. This is distinct from Isolate::SetFatalErrorHandler, which +/// is invoked in response to API usage failures. +pub fn set_fatal_error_handler(that: impl MapFnTo) { + unsafe { + v8__V8__SetFatalErrorHandler(that.map_fn_to()); + } +} + +/// Activate trap-based bounds checking for WebAssembly. +/// +/// If `use_v8_signal_handler` is true, V8 installs its own signal handler. +/// Otherwise, V8 relies on the embedder to invoke its trap handler. +pub fn enable_web_assembly_trap_handler(use_v8_signal_handler: bool) -> bool { + unsafe { v8__V8__EnableWebAssemblyTrapHandler(use_v8_signal_handler) } +} + +#[cfg(test)] +mod test_sandbox_use { + // __IsSandboxEnabled is used for testing that sandbox is actually + // enabled and is not a stable API + unsafe extern "C" { + fn v8__V8__IsSandboxEnabled() -> bool; + } + + fn is_sandboxed() -> bool { + unsafe { v8__V8__IsSandboxEnabled() } + } + + #[test] + #[cfg(feature = "v8_enable_sandbox")] + fn test_sandbox_on() { + assert!(is_sandboxed()); + } + + #[test] + #[cfg(not(feature = "v8_enable_sandbox"))] + fn test_sandbox_off() { + assert!(!is_sandboxed()); + } +} diff --git a/crates/v8/src/array_buffer.rs b/crates/v8/src/array_buffer.rs new file mode 100644 index 0000000000..675b077d7a --- /dev/null +++ b/crates/v8/src/array_buffer.rs @@ -0,0 +1,1002 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +use std::cell::Cell; +use std::ffi::c_void; +use std::ops::Deref; +use std::ptr::NonNull; +use std::ptr::null; +use std::slice; + +use crate::ArrayBuffer; +use crate::DataView; +use crate::Isolate; +use crate::IsolateGroup; +use crate::Local; +use crate::SharedArrayBuffer; +use crate::Value; +use crate::isolate::RealIsolate; +use crate::scope::PinScope; +use crate::support::MaybeBool; +use crate::support::Opaque; +use crate::support::Shared; +use crate::support::SharedPtrBase; +use crate::support::SharedRef; +use crate::support::UniquePtr; +use crate::support::UniqueRef; +use crate::support::long; + +unsafe extern "C" { + fn v8__ArrayBuffer__Allocator__NewDefaultAllocator() -> *mut Allocator; + fn v8__ArrayBuffer__Allocator__NewDefaultAllocator__with_group( + group: *const IsolateGroup, + ) -> *mut Allocator; + fn v8__ArrayBuffer__Allocator__DELETE(this: *mut Allocator); + fn v8__ArrayBuffer__New__with_byte_length( + isolate: *mut RealIsolate, + byte_length: usize, + ) -> *const ArrayBuffer; + fn v8__ArrayBuffer__MaybeNew( + isolate: *mut RealIsolate, + byte_length: usize, + initialization_mode: BackingStoreInitializationMode, + ) -> *const ArrayBuffer; + fn v8__ArrayBuffer__New__with_backing_store( + isolate: *mut RealIsolate, + backing_store: *const SharedRef, + ) -> *const ArrayBuffer; + fn v8__ArrayBuffer__Detach( + this: *const ArrayBuffer, + key: *const Value, + ) -> MaybeBool; + fn v8__ArrayBuffer__SetDetachKey(this: *const ArrayBuffer, key: *const Value); + fn v8__ArrayBuffer__Data(this: *const ArrayBuffer) -> *mut c_void; + fn v8__ArrayBuffer__IsDetachable(this: *const ArrayBuffer) -> bool; + fn v8__ArrayBuffer__WasDetached(this: *const ArrayBuffer) -> bool; + fn v8__ArrayBuffer__IsResizableByUserJavaScript( + this: *const ArrayBuffer, + ) -> bool; + fn v8__ArrayBuffer__IsImmutable(this: *const ArrayBuffer) -> bool; + fn v8__ArrayBuffer__ByteLength(this: *const ArrayBuffer) -> usize; + fn v8__ArrayBuffer__GetBackingStore( + this: *const ArrayBuffer, + ) -> SharedRef; + fn v8__ArrayBuffer__NewBackingStore__with_byte_length( + isolate: *mut RealIsolate, + byte_length: usize, + ) -> *mut BackingStore; + fn v8__ArrayBuffer__NewBackingStore__with_mode( + isolate: *mut RealIsolate, + byte_length: usize, + initialization_mode: BackingStoreInitializationMode, + on_failure: BackingStoreOnFailureMode, + ) -> *mut BackingStore; + fn v8__ArrayBuffer__NewResizableBackingStore( + byte_length: usize, + max_byte_length: usize, + ) -> *mut BackingStore; + fn v8__ArrayBuffer__NewBackingStore__with_data( + data: *mut c_void, + byte_length: usize, + deleter: BackingStoreDeleterCallback, + deleter_data: *mut c_void, + ) -> *mut BackingStore; + fn v8__BackingStore__Data(this: *const BackingStore) -> *mut c_void; + fn v8__BackingStore__ByteLength(this: *const BackingStore) -> usize; + fn v8__BackingStore__MaxByteLength(this: *const BackingStore) -> usize; + fn v8__BackingStore__IsShared(this: *const BackingStore) -> bool; + fn v8__BackingStore__IsResizableByUserJavaScript( + this: *const BackingStore, + ) -> bool; + fn v8__BackingStore__DELETE(this: *mut BackingStore); + + fn v8__DataView__New( + arraybuffer: *const ArrayBuffer, + byte_offset: usize, + length: usize, + ) -> *const DataView; + fn v8__DataView__New__with_shared_buffer( + shared_array_buffer: *const SharedArrayBuffer, + byte_offset: usize, + length: usize, + ) -> *const DataView; + + fn std__shared_ptr__v8__BackingStore__COPY( + ptr: *const SharedPtrBase, + ) -> SharedPtrBase; + fn std__shared_ptr__v8__BackingStore__CONVERT__std__unique_ptr( + unique_ptr: UniquePtr, + ) -> SharedPtrBase; + fn std__shared_ptr__v8__BackingStore__get( + ptr: *const SharedPtrBase, + ) -> *mut BackingStore; + fn std__shared_ptr__v8__BackingStore__reset( + ptr: *mut SharedPtrBase, + ); + fn std__shared_ptr__v8__BackingStore__use_count( + ptr: *const SharedPtrBase, + ) -> long; + + fn std__shared_ptr__v8__ArrayBuffer__Allocator__COPY( + ptr: *const SharedPtrBase, + ) -> SharedPtrBase; + fn std__shared_ptr__v8__ArrayBuffer__Allocator__CONVERT__std__unique_ptr( + unique_ptr: UniquePtr, + ) -> SharedPtrBase; + fn std__shared_ptr__v8__ArrayBuffer__Allocator__get( + ptr: *const SharedPtrBase, + ) -> *mut Allocator; + fn std__shared_ptr__v8__ArrayBuffer__Allocator__reset( + ptr: *mut SharedPtrBase, + ); + fn std__shared_ptr__v8__ArrayBuffer__Allocator__use_count( + ptr: *const SharedPtrBase, + ) -> long; +} + +// Rust allocator feature is only available in non-sandboxed mode +#[cfg(not(feature = "v8_enable_sandbox"))] +unsafe extern "C" { + fn v8__ArrayBuffer__Allocator__NewRustAllocator( + handle: *const c_void, + vtable: *const RustAllocatorVtable, + ) -> *mut Allocator; +} + +/// Whether the memory of a newly allocated backing store is zero-initialized +/// or left uninitialized. +/// +/// Requesting uninitialized memory is faster, but the embedder must then make +/// sure the contents are never observed before they are written. That +/// obligation cannot be checked, so the constructors that leave memory +/// uninitialized — [`ArrayBuffer::maybe_new_uninitialized`], +/// [`ArrayBuffer::new_backing_store_uninitialized`] and +/// [`SharedArrayBuffer::new_backing_store_uninitialized`] — are `unsafe`. +#[repr(C)] +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub enum BackingStoreInitializationMode { + #[default] + ZeroInitialized, + Uninitialized, +} + +/// What a backing store allocation should do when it cannot satisfy the +/// request, even after garbage collection. +#[repr(C)] +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub enum BackingStoreOnFailureMode { + /// Return no backing store, letting the caller handle the failure. + ReturnNull, + /// Crash the process with an out-of-memory error. + #[default] + OutOfMemory, +} + +/// A thread-safe allocator that V8 uses to allocate |ArrayBuffer|'s memory. +/// The allocator is a global V8 setting. It has to be set via +/// Isolate::CreateParams. +/// +/// Memory allocated through this allocator by V8 is accounted for as external +/// memory by V8. Note that V8 keeps track of the memory for all internalized +/// |ArrayBuffer|s. Responsibility for tracking external memory (using +/// Isolate::AdjustAmountOfExternalAllocatedMemory) is handed over to the +/// embedder upon externalization and taken over upon internalization (creating +/// an internalized buffer from an existing buffer). +/// +/// Note that it is unsafe to call back into V8 from any of the allocator +/// functions. +/// +/// This is called v8::ArrayBuffer::Allocator in C++. Rather than use the +/// namespace array_buffer, which will contain only the Allocator we opt in Rust +/// to allow it to live in the top level: v8::Allocator +#[repr(C)] +#[derive(Debug)] +pub struct Allocator(Opaque); + +/// A wrapper around the V8 Allocator class. +#[cfg(not(feature = "v8_enable_sandbox"))] +#[repr(C)] +pub struct RustAllocatorVtable { + pub allocate: unsafe extern "C" fn(handle: &T, len: usize) -> *mut c_void, + pub allocate_uninitialized: + unsafe extern "C" fn(handle: &T, len: usize) -> *mut c_void, + pub free: unsafe extern "C" fn(handle: &T, data: *mut c_void, len: usize), + pub drop: unsafe extern "C" fn(handle: *const T), +} + +impl Shared for Allocator { + fn clone(ptr: &SharedPtrBase) -> SharedPtrBase { + unsafe { std__shared_ptr__v8__ArrayBuffer__Allocator__COPY(ptr) } + } + fn from_unique_ptr(unique_ptr: UniquePtr) -> SharedPtrBase { + unsafe { + std__shared_ptr__v8__ArrayBuffer__Allocator__CONVERT__std__unique_ptr( + unique_ptr, + ) + } + } + fn get(ptr: &SharedPtrBase) -> *const Self { + unsafe { std__shared_ptr__v8__ArrayBuffer__Allocator__get(ptr) } + } + fn reset(ptr: &mut SharedPtrBase) { + unsafe { std__shared_ptr__v8__ArrayBuffer__Allocator__reset(ptr) } + } + fn use_count(ptr: &SharedPtrBase) -> long { + unsafe { std__shared_ptr__v8__ArrayBuffer__Allocator__use_count(ptr) } + } +} + +/// malloc/free based convenience allocator. +#[inline(always)] +pub fn new_default_allocator() -> UniqueRef { + unsafe { + UniqueRef::from_raw(v8__ArrayBuffer__Allocator__NewDefaultAllocator()) + } +} + +/// Convenience allocator scoped to an isolate group. +/// +/// When the sandbox is enabled, this allocator allocates its backing memory +/// inside the sandbox belonging to `group`; otherwise it relies on +/// malloc/free. +/// +/// V8 only declares this overload when it is built with pointer compression in +/// multi-cage mode, the same configuration that +/// [`IsolateGroup::can_create_new_groups`] reports on. `None` is returned when +/// the V8 this crate was linked against was built differently. +#[inline(always)] +pub fn new_default_allocator_for_group( + group: &IsolateGroup, +) -> Option> { + let ptr = unsafe { + v8__ArrayBuffer__Allocator__NewDefaultAllocator__with_group(group) + }; + if ptr.is_null() { + None + } else { + Some(unsafe { UniqueRef::from_raw(ptr) }) + } +} + +/// Creates an allocator managed by Rust code. +/// +/// Marked `unsafe` because the caller must ensure that `handle` is valid and matches what `vtable` expects. +/// +/// Not usable in sandboxed mode +#[inline(always)] +#[cfg(not(feature = "v8_enable_sandbox"))] +pub unsafe fn new_rust_allocator( + handle: *const T, + vtable: &'static RustAllocatorVtable, +) -> UniqueRef { + unsafe { + UniqueRef::from_raw(v8__ArrayBuffer__Allocator__NewRustAllocator( + handle as *const c_void, + vtable as *const RustAllocatorVtable + as *const RustAllocatorVtable, + )) + } +} + +#[test] +#[cfg(not(feature = "v8_enable_sandbox"))] +fn test_rust_allocator() { + use std::sync::Arc; + use std::sync::atomic::{AtomicUsize, Ordering}; + + unsafe extern "C" fn allocate(_: &AtomicUsize, _: usize) -> *mut c_void { + unimplemented!() + } + unsafe extern "C" fn allocate_uninitialized( + _: &AtomicUsize, + _: usize, + ) -> *mut c_void { + unimplemented!() + } + unsafe extern "C" fn free(_: &AtomicUsize, _: *mut c_void, _: usize) { + unimplemented!() + } + unsafe extern "C" fn drop(x: *const AtomicUsize) { + unsafe { + let arc = Arc::from_raw(x); + arc.store(42, Ordering::SeqCst); + } + } + + let retval = Arc::new(AtomicUsize::new(0)); + + let vtable: &'static RustAllocatorVtable = + &RustAllocatorVtable { + allocate, + allocate_uninitialized, + free, + drop, + }; + unsafe { new_rust_allocator(Arc::into_raw(retval.clone()), vtable) }; + assert_eq!(retval.load(Ordering::SeqCst), 42); + assert_eq!(Arc::strong_count(&retval), 1); +} + +#[test] +fn test_default_allocator() { + crate::initialize_v8(); + new_default_allocator(); +} + +impl Drop for Allocator { + fn drop(&mut self) { + unsafe { v8__ArrayBuffer__Allocator__DELETE(self) }; + } +} + +pub type BackingStoreDeleterCallback = unsafe extern "C" fn( + data: *mut c_void, + byte_length: usize, + deleter_data: *mut c_void, +); + +#[cfg(not(feature = "v8_enable_sandbox"))] +pub(crate) mod sealed { + pub trait Rawable { + fn byte_len(&mut self) -> usize; + fn into_raw(self) -> (*const (), *const u8); + unsafe fn drop_raw(ptr: *const (), size: usize); + } +} + +#[cfg(not(feature = "v8_enable_sandbox"))] +macro_rules! rawable { + ($ty:ty) => { + impl sealed::Rawable for Box<[$ty]> { + fn byte_len(&mut self) -> usize { + self.as_mut().len() * std::mem::size_of::<$ty>() + } + + fn into_raw(mut self) -> (*const (), *const u8) { + // Thin the fat pointer + let ptr = self.as_mut_ptr(); + std::mem::forget(self); + (ptr as _, ptr as _) + } + + unsafe fn drop_raw(ptr: *const (), len: usize) { + // Fatten the thin pointer + _ = unsafe { + Self::from_raw(std::ptr::slice_from_raw_parts_mut(ptr as _, len)) + }; + } + } + + impl sealed::Rawable for Vec<$ty> { + fn byte_len(&mut self) -> usize { + Vec::<$ty>::len(self) * std::mem::size_of::<$ty>() + } + + unsafe fn drop_raw(ptr: *const (), size: usize) { + unsafe { + as sealed::Rawable>::drop_raw(ptr, size); + } + } + + fn into_raw(self) -> (*const (), *const u8) { + self.into_boxed_slice().into_raw() + } + } + }; +} + +#[cfg(not(feature = "v8_enable_sandbox"))] +rawable!(u8); +#[cfg(not(feature = "v8_enable_sandbox"))] +rawable!(u16); +#[cfg(not(feature = "v8_enable_sandbox"))] +rawable!(u32); +#[cfg(not(feature = "v8_enable_sandbox"))] +rawable!(u64); +#[cfg(not(feature = "v8_enable_sandbox"))] +rawable!(i8); +#[cfg(not(feature = "v8_enable_sandbox"))] +rawable!(i16); +#[cfg(not(feature = "v8_enable_sandbox"))] +rawable!(i32); +#[cfg(not(feature = "v8_enable_sandbox"))] +rawable!(i64); +#[cfg(not(feature = "v8_enable_sandbox"))] +rawable!(f32); +#[cfg(not(feature = "v8_enable_sandbox"))] +rawable!(f64); + +#[cfg(not(feature = "v8_enable_sandbox"))] +impl sealed::Rawable for Box +where + T: AsMut<[u8]>, +{ + fn byte_len(&mut self) -> usize { + self.as_mut().as_mut().len() + } + + fn into_raw(mut self) -> (*const (), *const u8) { + let data = self.as_mut().as_mut().as_mut_ptr(); + let ptr = Self::into_raw(self); + (ptr as _, data) + } + + unsafe fn drop_raw(ptr: *const (), _len: usize) { + unsafe { + _ = Self::from_raw(ptr as _); + } + } +} + +/// A wrapper around the backing store (i.e. the raw memory) of an array buffer. +/// See a document linked in http://crbug.com/v8/9908 for more information. +/// +/// The allocation and destruction of backing stores is generally managed by +/// V8. Clients should always use standard C++ memory ownership types (i.e. +/// std::unique_ptr and std::shared_ptr) to manage lifetimes of backing stores +/// properly, since V8 internal objects may alias backing stores. +/// +/// This object does not keep the underlying |ArrayBuffer::Allocator| alive by +/// default. Use Isolate::CreateParams::array_buffer_allocator_shared when +/// creating the Isolate to make it hold a reference to the allocator itself. +#[repr(C)] +#[derive(Debug)] +pub struct BackingStore([usize; 6]); + +unsafe impl Send for BackingStore {} + +impl BackingStore { + /// Return a pointer to the beginning of the memory block for this backing + /// store. The pointer is only valid as long as this backing store object + /// lives. + /// + /// Might return `None` if the backing store has zero length. + #[inline(always)] + pub fn data(&self) -> Option> { + let raw_ptr = + unsafe { v8__BackingStore__Data(self as *const _ as *mut Self) }; + NonNull::new(raw_ptr) + } + + /// The length (in bytes) of this backing store. + #[inline(always)] + pub fn byte_length(&self) -> usize { + unsafe { v8__BackingStore__ByteLength(self) } + } + + /// The maximum length (in bytes) that this backing store can grow to. + /// + /// For a non-resizable backing store this is the same as + /// [`BackingStore::byte_length`]. + #[inline(always)] + pub fn max_byte_length(&self) -> usize { + unsafe { v8__BackingStore__MaxByteLength(self) } + } + + /// Indicates whether the backing store was created for an ArrayBuffer or + /// a SharedArrayBuffer. + #[inline(always)] + pub fn is_shared(&self) -> bool { + unsafe { v8__BackingStore__IsShared(self) } + } + + /// Indicates whether the backing store was created for a resizable ArrayBuffer + /// or a growable SharedArrayBuffer, and thus may be resized by user + /// JavaScript code. + #[inline(always)] + pub fn is_resizable_by_user_javascript(&self) -> bool { + unsafe { v8__BackingStore__IsResizableByUserJavaScript(self) } + } +} + +impl Deref for BackingStore { + type Target = [Cell]; + + /// Returns a [u8] slice refencing the data in the backing store. + #[inline] + fn deref(&self) -> &Self::Target { + // We use a dangling pointer if `self.data()` returns None because it's UB + // to create even an empty slice from a null pointer. + let data = self + .data() + .unwrap_or_else(NonNull::dangling) + .cast::>(); + let len = self.byte_length(); + unsafe { slice::from_raw_parts(data.as_ptr(), len) } + } +} + +impl Drop for BackingStore { + #[inline] + fn drop(&mut self) { + unsafe { v8__BackingStore__DELETE(self) }; + } +} + +impl Shared for BackingStore { + #[inline] + fn clone(ptr: &SharedPtrBase) -> SharedPtrBase { + unsafe { std__shared_ptr__v8__BackingStore__COPY(ptr) } + } + #[inline] + fn from_unique_ptr(unique_ptr: UniquePtr) -> SharedPtrBase { + unsafe { + std__shared_ptr__v8__BackingStore__CONVERT__std__unique_ptr(unique_ptr) + } + } + #[inline] + fn get(ptr: &SharedPtrBase) -> *const Self { + unsafe { std__shared_ptr__v8__BackingStore__get(ptr) } + } + #[inline] + fn reset(ptr: &mut SharedPtrBase) { + unsafe { std__shared_ptr__v8__BackingStore__reset(ptr) } + } + #[inline] + fn use_count(ptr: &SharedPtrBase) -> long { + unsafe { std__shared_ptr__v8__BackingStore__use_count(ptr) } + } +} + +impl ArrayBuffer { + /// The maximum length (in bytes) of an `ArrayBuffer`, i.e. the largest value + /// that can be passed to [`ArrayBuffer::new`]. + /// + /// This is V8's `ArrayBuffer::kMaxByteLength`, which V8 also exposes as + /// `TypedArray::kMaxByteLength`. + pub const MAX_BYTE_LENGTH: usize = + crate::binding::v8__TypedArray__kMaxByteLength; + + /// Create a new ArrayBuffer. Allocate |byte_length| bytes. + /// Allocated memory will be owned by a created ArrayBuffer and + /// will be deallocated when it is garbage-collected, + /// unless the object is externalized. + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_, ()>, + byte_length: usize, + ) -> Local<'s, ArrayBuffer> { + unsafe { + scope.cast_local(|sd| { + v8__ArrayBuffer__New__with_byte_length( + sd.get_isolate_ptr(), + byte_length, + ) + }) + } + .unwrap() + } + + #[inline(always)] + pub fn with_backing_store<'s>( + scope: &PinScope<'s, '_, ()>, + backing_store: &SharedRef, + ) -> Local<'s, ArrayBuffer> { + unsafe { + scope.cast_local(|sd| { + v8__ArrayBuffer__New__with_backing_store( + sd.get_isolate_ptr(), + backing_store, + ) + }) + } + .unwrap() + } + + /// Create a new ArrayBuffer, allocating |byte_length| zero-initialized + /// bytes. + /// + /// Unlike [`ArrayBuffer::new`], `None` is returned if the allocation fails + /// rather than crashing the process. + #[inline(always)] + pub fn maybe_new<'s>( + scope: &PinScope<'s, '_, ()>, + byte_length: usize, + ) -> Option> { + unsafe { + Self::maybe_new_with_mode( + scope, + byte_length, + BackingStoreInitializationMode::ZeroInitialized, + ) + } + } + + /// Like [`ArrayBuffer::maybe_new`], but the newly allocated bytes are left + /// uninitialized, which is faster but leaves whatever the allocator handed + /// out visible in the buffer. + /// + /// # Safety + /// + /// The contents of the returned ArrayBuffer are uninitialized memory. The + /// caller must overwrite every byte before the buffer is read, either from + /// Rust or by JavaScript; in particular the buffer must not be made + /// reachable from script until it has been fully initialized. Reading it + /// beforehand is undefined behaviour and may disclose unrelated process + /// memory. + #[inline(always)] + pub unsafe fn maybe_new_uninitialized<'s>( + scope: &PinScope<'s, '_, ()>, + byte_length: usize, + ) -> Option> { + unsafe { + Self::maybe_new_with_mode( + scope, + byte_length, + BackingStoreInitializationMode::Uninitialized, + ) + } + } + + /// # Safety + /// + /// See [`ArrayBuffer::maybe_new_uninitialized`]: passing + /// [`BackingStoreInitializationMode::Uninitialized`] yields an ArrayBuffer + /// whose contents must be fully written before they are read or exposed. + #[inline(always)] + unsafe fn maybe_new_with_mode<'s>( + scope: &PinScope<'s, '_, ()>, + byte_length: usize, + initialization_mode: BackingStoreInitializationMode, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__ArrayBuffer__MaybeNew( + sd.get_isolate_ptr(), + byte_length, + initialization_mode, + ) + }) + } + } + + /// Data length in bytes. + #[inline(always)] + pub fn byte_length(&self) -> usize { + unsafe { v8__ArrayBuffer__ByteLength(self) } + } + + /// Returns true if this ArrayBuffer may be resized by user JavaScript code, + /// i.e. whether it is a resizable ArrayBuffer. Equivalent to + /// `get_backing_store().is_resizable_by_user_javascript()`. + #[inline(always)] + pub fn is_resizable_by_user_javascript(&self) -> bool { + unsafe { v8__ArrayBuffer__IsResizableByUserJavaScript(self) } + } + + /// Returns true if this ArrayBuffer is immutable. + #[inline(always)] + pub fn is_immutable(&self) -> bool { + unsafe { v8__ArrayBuffer__IsImmutable(self) } + } + + /// Returns true if this ArrayBuffer may be detached. + #[inline(always)] + pub fn is_detachable(&self) -> bool { + unsafe { v8__ArrayBuffer__IsDetachable(self) } + } + + /// Returns true if this ArrayBuffer was detached. + #[inline(always)] + pub fn was_detached(&self) -> bool { + if self.byte_length() != 0 { + return false; + } + unsafe { v8__ArrayBuffer__WasDetached(self) } + } + + /// Detaches this ArrayBuffer and all its views (typed arrays). + /// Detaching sets the byte length of the buffer and all typed arrays to zero, + /// preventing JavaScript from ever accessing underlying backing store. + /// ArrayBuffer should have been externalized and must be detachable. Returns + /// `None` if the key didn't pass the `[[ArrayBufferDetachKey]]` check, + /// and `Some(true)` otherwise. + #[inline(always)] + pub fn detach(&self, key: Option>) -> Option { + // V8 terminates when the ArrayBuffer is not detachable. Non-detachable + // buffers are buffers that are in use by WebAssembly or asm.js. + if self.is_detachable() { + let key = key.map_or(null(), |v| &*v as *const Value); + unsafe { v8__ArrayBuffer__Detach(self, key) }.into() + } else { + Some(true) + } + } + + /// Sets the `[[ArrayBufferDetachKey]]`. + #[inline(always)] + pub fn set_detach_key(&self, key: Local) { + unsafe { v8__ArrayBuffer__SetDetachKey(self, &*key) }; + } + + /// More efficient shortcut for GetBackingStore()->Data(). + /// The returned pointer is valid as long as the ArrayBuffer is alive. + #[inline(always)] + pub fn data(&self) -> Option> { + let raw_ptr = unsafe { v8__ArrayBuffer__Data(self) }; + NonNull::new(raw_ptr) + } + + /// Get a shared pointer to the backing store of this array buffer. This + /// pointer coordinates the lifetime management of the internal storage + /// with any live ArrayBuffers on the heap, even across isolates. The embedder + /// should not attempt to manage lifetime of the storage through other means. + #[inline(always)] + pub fn get_backing_store(&self) -> SharedRef { + unsafe { v8__ArrayBuffer__GetBackingStore(self) } + } + + /// Returns a new standalone BackingStore that is allocated using the array + /// buffer allocator of the isolate. The result can be later passed to + /// ArrayBuffer::New. + /// + /// If the allocator returns nullptr, then the function may cause GCs in the + /// given isolate and re-try the allocation. If GCs do not help, then the + /// function will crash with an out-of-memory error. + #[inline(always)] + pub fn new_backing_store( + scope: &mut Isolate, + byte_length: usize, + ) -> UniqueRef { + unsafe { + UniqueRef::from_raw(v8__ArrayBuffer__NewBackingStore__with_byte_length( + (*scope).as_real_ptr(), + byte_length, + )) + } + } + + /// Returns a new zero-initialized standalone BackingStore that is allocated + /// using the array buffer allocator of the isolate, with control over what + /// happens when the allocation fails. + /// + /// If the allocator returns null, then the function may cause GCs in the + /// given isolate and re-try the allocation. + /// + /// If `on_failure` is [`BackingStoreOnFailureMode::OutOfMemory`] and GCs do + /// not help, then the process crashes with an out-of-memory error. If it is + /// [`BackingStoreOnFailureMode::ReturnNull`], `None` is returned instead. + #[inline(always)] + pub fn new_backing_store_with_mode( + isolate: &mut Isolate, + byte_length: usize, + on_failure: BackingStoreOnFailureMode, + ) -> Option> { + unsafe { + Self::new_backing_store_with_modes( + isolate, + byte_length, + BackingStoreInitializationMode::ZeroInitialized, + on_failure, + ) + } + } + + /// Like [`ArrayBuffer::new_backing_store_with_mode`], but the allocated + /// memory is left uninitialized, which is faster but leaves whatever the + /// allocator handed out visible in the backing store. + /// + /// # Safety + /// + /// The returned backing store contains uninitialized memory, which its safe + /// `Deref]>` would otherwise let anyone read. The caller + /// must overwrite every byte before the backing store is read, either from + /// Rust or through an ArrayBuffer handed to JavaScript. Reading it + /// beforehand is undefined behaviour and may disclose unrelated process + /// memory. + #[inline(always)] + pub unsafe fn new_backing_store_uninitialized( + isolate: &mut Isolate, + byte_length: usize, + on_failure: BackingStoreOnFailureMode, + ) -> Option> { + unsafe { + Self::new_backing_store_with_modes( + isolate, + byte_length, + BackingStoreInitializationMode::Uninitialized, + on_failure, + ) + } + } + + /// # Safety + /// + /// See [`ArrayBuffer::new_backing_store_uninitialized`]: passing + /// [`BackingStoreInitializationMode::Uninitialized`] yields a backing store + /// whose contents must be fully written before they are read or exposed. + #[inline(always)] + unsafe fn new_backing_store_with_modes( + isolate: &mut Isolate, + byte_length: usize, + initialization_mode: BackingStoreInitializationMode, + on_failure: BackingStoreOnFailureMode, + ) -> Option> { + let ptr = unsafe { + v8__ArrayBuffer__NewBackingStore__with_mode( + isolate.as_real_ptr(), + byte_length, + initialization_mode, + on_failure, + ) + }; + if ptr.is_null() { + None + } else { + Some(unsafe { UniqueRef::from_raw(ptr) }) + } + } + + /// Returns a new resizable standalone BackingStore that is allocated using + /// the array buffer allocator of the isolate. The result can be later passed + /// to [`ArrayBuffer::with_backing_store`]. + /// + /// `byte_length` must be `<= max_byte_length`. + /// + /// This function is usable without an isolate. Unlike the isolate-scoped + /// constructors, GCs cannot be triggered and there are no retries; + /// allocation failure crashes the process with an out-of-memory error. + #[inline(always)] + pub fn new_resizable_backing_store( + byte_length: usize, + max_byte_length: usize, + ) -> UniqueRef { + unsafe { + UniqueRef::from_raw(v8__ArrayBuffer__NewResizableBackingStore( + byte_length, + max_byte_length, + )) + } + } + + /// Returns a new standalone BackingStore that takes over the ownership of + /// the given buffer. + /// + /// The destructor of the BackingStore frees owned buffer memory. + /// + /// The result can be later passed to ArrayBuffer::New. The raw pointer + /// to the buffer must not be passed again to any V8 API function. + /// + /// Not available in Sandbox Mode, see new_backing_store_from_bytes for a potential alternative + #[inline(always)] + #[cfg(not(feature = "v8_enable_sandbox"))] + pub fn new_backing_store_from_boxed_slice( + data: Box<[u8]>, + ) -> UniqueRef { + Self::new_backing_store_from_bytes(data) + } + + /// Returns a new standalone BackingStore that takes over the ownership of + /// the given buffer. + /// + /// The destructor of the BackingStore frees owned buffer memory. + /// + /// The result can be later passed to ArrayBuffer::New. The raw pointer + /// to the buffer must not be passed again to any V8 API function. + /// + /// Not available in Sandbox Mode, see new_backing_store_from_bytes for a potential alternative + #[inline(always)] + #[cfg(not(feature = "v8_enable_sandbox"))] + pub fn new_backing_store_from_vec(data: Vec) -> UniqueRef { + Self::new_backing_store_from_bytes(data) + } + + /// Returns a new standalone BackingStore backed by a container that dereferences + /// to a mutable slice of bytes. The object is dereferenced once, and the resulting slice's + /// memory is used for the lifetime of the buffer. + /// + /// This method may be called with most single-ownership containers that implement `AsMut<[u8]>`, including + /// `Box<[u8]>`, and `Vec`. This will also support most other mutable bytes containers (including `bytes::BytesMut`), + /// though these buffers will need to be boxed to manage ownership of memory. + /// + /// Not available in sandbox mode. Sandbox mode requires data to be allocated + /// within the sandbox's address space. Within sandbox mode, consider the below alternatives + /// + /// 1. consider using new_backing_store and BackingStore::data() followed by doing a std::ptr::copy to copy the data into a BackingStore. + /// 2. If you truly do have data that is allocated inside the sandbox address space, consider using the unsafe new_backing_store_from_ptr API + /// + /// ``` + /// // Vector of bytes + /// let backing_store = v8::ArrayBuffer::new_backing_store_from_bytes(vec![1, 2, 3]); + /// // Boxes slice of bytes + /// let boxed_slice: Box<[u8]> = vec![1, 2, 3].into_boxed_slice(); + /// let backing_store = v8::ArrayBuffer::new_backing_store_from_bytes(boxed_slice); + /// // BytesMut from bytes crate + /// let backing_store = v8::ArrayBuffer::new_backing_store_from_bytes(Box::new(bytes::BytesMut::new())); + /// ``` + #[inline(always)] + #[cfg(not(feature = "v8_enable_sandbox"))] + pub fn new_backing_store_from_bytes( + mut bytes: T, + ) -> UniqueRef + where + T: sealed::Rawable, + { + let len = bytes.byte_len(); + + let (ptr, slice) = T::into_raw(bytes); + + unsafe extern "C" fn drop_rawable( + _ptr: *mut c_void, + len: usize, + data: *mut c_void, + ) { + // SAFETY: We know that data is a raw T from above + unsafe { T::drop_raw(data as _, len) } + } + + // SAFETY: We are extending the lifetime of a slice, but we're locking away the box that we + // derefed from so there's no way to get another mutable reference. + unsafe { + Self::new_backing_store_from_ptr( + slice as _, + len, + drop_rawable::, + ptr as _, + ) + } + } + + /// Returns a new standalone BackingStore backed by given ptr. + /// + /// SAFETY: This API consumes raw pointers so is inherently + /// unsafe. Usually you should use new_backing_store_from_boxed_slice. + /// + /// WARNING: Using sandbox mode has extra limitations that may cause crashes + /// or memory safety violations if this API is used incorrectly: + /// + /// 1. Sandbox mode requires data to be allocated within the sandbox's address space. + /// 2. It is very easy to cause memory safety errors when using this API with sandbox mode + #[inline(always)] + pub unsafe fn new_backing_store_from_ptr( + data_ptr: *mut c_void, + byte_length: usize, + deleter_callback: BackingStoreDeleterCallback, + deleter_data: *mut c_void, + ) -> UniqueRef { + unsafe { + UniqueRef::from_raw(v8__ArrayBuffer__NewBackingStore__with_data( + data_ptr, + byte_length, + deleter_callback, + deleter_data, + )) + } + } +} + +impl DataView { + /// Returns a new DataView. + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_, ()>, + arraybuffer: Local<'s, ArrayBuffer>, + byte_offset: usize, + length: usize, + ) -> Local<'s, DataView> { + unsafe { + scope + .cast_local(|_| v8__DataView__New(&*arraybuffer, byte_offset, length)) + } + .unwrap() + } + + /// Returns a new DataView over a SharedArrayBuffer. + #[inline(always)] + pub fn new_with_shared_buffer<'s>( + scope: &PinScope<'s, '_, ()>, + shared_array_buffer: Local<'s, SharedArrayBuffer>, + byte_offset: usize, + length: usize, + ) -> Local<'s, DataView> { + unsafe { + scope.cast_local(|_| { + v8__DataView__New__with_shared_buffer( + &*shared_array_buffer, + byte_offset, + length, + ) + }) + } + .unwrap() + } +} diff --git a/crates/v8/src/array_buffer_view.rs b/crates/v8/src/array_buffer_view.rs new file mode 100644 index 0000000000..e81367914b --- /dev/null +++ b/crates/v8/src/array_buffer_view.rs @@ -0,0 +1,154 @@ +use crate::ArrayBuffer; +use crate::ArrayBufferView; +use crate::BackingStore; +use crate::Local; +use crate::SharedRef; +use crate::binding::memory_span_t; +use crate::scope::PinScope; +use crate::support::int; +use std::convert::TryInto; +use std::ffi::c_void; +use std::mem::MaybeUninit; + +unsafe extern "C" { + fn v8__ArrayBufferView__Buffer( + this: *const ArrayBufferView, + ) -> *const ArrayBuffer; + fn v8__ArrayBufferView__Buffer__Data( + this: *const ArrayBufferView, + ) -> *mut c_void; + fn v8__ArrayBufferView__ByteLength(this: *const ArrayBufferView) -> usize; + fn v8__ArrayBufferView__ByteOffset(this: *const ArrayBufferView) -> usize; + fn v8__ArrayBufferView__HasBuffer(this: *const ArrayBufferView) -> bool; + fn v8__ArrayBufferView__CopyContents( + this: *const ArrayBufferView, + dest: *mut c_void, + byte_length: int, + ) -> usize; + fn v8__ArrayBufferView__GetContents( + this: *const ArrayBufferView, + storage: memory_span_t, + ) -> memory_span_t; +} + +impl ArrayBufferView { + /// Returns underlying ArrayBuffer. + #[inline(always)] + pub fn buffer<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + ) -> Option> { + unsafe { scope.cast_local(|_| v8__ArrayBufferView__Buffer(self)) } + } + + /// Returns true if ArrayBufferView's backing ArrayBuffer has already been allocated. + pub fn has_buffer(&self) -> bool { + unsafe { v8__ArrayBufferView__HasBuffer(self) } + } + + /// Get a shared pointer to the backing store of this array buffer. This + /// pointer coordinates the lifetime management of the internal storage + /// with any live ArrayBuffers on the heap, even across isolates. The embedder + /// should not attempt to manage lifetime of the storage through other means. + #[inline(always)] + pub fn get_backing_store(&self) -> Option> { + let buffer = unsafe { v8__ArrayBufferView__Buffer(self) }; + unsafe { buffer.as_ref().map(|buffer| buffer.get_backing_store()) } + } + + /// Returns the underlying storage for this `ArrayBufferView`, including the built-in `byte_offset`. + /// This is a more efficient way of calling `buffer(scope)->data()`, and may be called without a + /// scope. + #[inline(always)] + pub fn data(&self) -> *mut c_void { + unsafe { + v8__ArrayBufferView__Buffer__Data(self) + .add(v8__ArrayBufferView__ByteOffset(self)) + } + } + + /// Size of a view in bytes. + #[inline(always)] + pub fn byte_length(&self) -> usize { + unsafe { v8__ArrayBufferView__ByteLength(self) } + } + + /// Byte offset in |Buffer|. + #[inline(always)] + pub fn byte_offset(&self) -> usize { + unsafe { v8__ArrayBufferView__ByteOffset(self) } + } + + /// Copy the contents of the ArrayBufferView's buffer to an embedder defined + /// memory without additional overhead that calling ArrayBufferView::Buffer + /// might incur. + /// Returns the number of bytes actually written. + #[inline(always)] + pub fn copy_contents(&self, dest: &mut [u8]) -> usize { + unsafe { + v8__ArrayBufferView__CopyContents( + self, + dest.as_mut_ptr() as *mut c_void, + dest.len().try_into().unwrap(), + ) + } + } + + /// Copy the contents of the ArrayBufferView's buffer to an embedder defined + /// memory without additional overhead that calling ArrayBufferView::Buffer + /// might incur. + /// Returns the number of bytes actually written. + #[inline(always)] + pub fn copy_contents_uninit(&self, dest: &mut [MaybeUninit]) -> usize { + unsafe { + v8__ArrayBufferView__CopyContents( + self, + dest.as_mut_ptr() as *mut c_void, + dest.len().try_into().unwrap(), + ) + } + } + + /// Returns the contents of the ArrayBufferView's buffer as a MemorySpan. If + /// the contents are on the V8 heap, they get copied into `storage`. Otherwise + /// a view into the off-heap backing store is returned. The provided storage + /// should be at least as large as the maximum on-heap size of a TypedArray, + /// which is available as `v8::TYPED_ARRAY_MAX_SIZE_IN_HEAP`. + #[inline(always)] + pub unsafe fn get_contents_raw_parts( + &self, + storage: &mut [u8], + ) -> (*mut u8, usize) { + unsafe { + let span = v8__ArrayBufferView__GetContents( + self, + memory_span_t { + data: storage.as_mut_ptr(), + size: storage.len(), + }, + ); + (span.data, span.size) + } + } + + /// Returns the contents of the ArrayBufferView's buffer as a MemorySpan. If + /// the contents are on the V8 heap, they get copied into `storage`. Otherwise + /// a view into the off-heap backing store is returned. The provided storage + /// should be at least as large as the maximum on-heap size of a TypedArray, + /// which is available as `v8::TYPED_ARRAY_MAX_SIZE_IN_HEAP`. + #[inline(always)] + pub fn get_contents<'s, 'a>(&'s self, storage: &'a mut [u8]) -> &'a [u8] + where + 's: 'a, + { + unsafe { + let (data, size) = self.get_contents_raw_parts(storage); + if data.is_null() { + debug_assert_eq!(size, 0); + std::slice::from_raw_parts(std::ptr::dangling(), size) + } else { + std::slice::from_raw_parts(data, size) + } + } + } +} diff --git a/crates/v8/src/bigint.rs b/crates/v8/src/bigint.rs new file mode 100644 index 0000000000..306383d7cb --- /dev/null +++ b/crates/v8/src/bigint.rs @@ -0,0 +1,143 @@ +use crate::BigInt; +use crate::Context; +use crate::Local; +use crate::isolate::RealIsolate; +use crate::scope::PinScope; +use crate::support::int; + +use std::mem::MaybeUninit; + +unsafe extern "C" { + fn v8__BigInt__New(isolate: *mut RealIsolate, value: i64) -> *const BigInt; + fn v8__BigInt__NewFromUnsigned( + isolate: *mut RealIsolate, + value: u64, + ) -> *const BigInt; + fn v8__BigInt__NewFromWords( + context: *const Context, + sign_bit: int, + word_count: int, + words: *const u64, + ) -> *const BigInt; + fn v8__BigInt__Uint64Value(this: *const BigInt, lossless: *mut bool) -> u64; + fn v8__BigInt__Int64Value(this: *const BigInt, lossless: *mut bool) -> i64; + fn v8__BigInt__WordCount(this: *const BigInt) -> int; + fn v8__BigInt__ToWordsArray( + this: *const BigInt, + sign_bit: *mut int, + word_count: *mut int, + words: *mut u64, + ); +} + +impl BigInt { + #[inline(always)] + pub fn new_from_i64<'s>( + scope: &PinScope<'s, '_, ()>, + value: i64, + ) -> Local<'s, BigInt> { + unsafe { + scope.cast_local(|sd| v8__BigInt__New(sd.get_isolate_ptr(), value)) + } + .unwrap() + } + + #[inline(always)] + pub fn new_from_u64<'s>( + scope: &PinScope<'s, '_, ()>, + value: u64, + ) -> Local<'s, BigInt> { + unsafe { + scope.cast_local(|sd| { + v8__BigInt__NewFromUnsigned(sd.get_isolate_ptr(), value) + }) + } + .unwrap() + } + + /// Creates a new BigInt object using a specified sign bit and a + /// specified list of digits/words. + /// The resulting number is calculated as: + /// + /// (-1)^sign_bit * (words[0] * (2^64)^0 + words[1] * (2^64)^1 + ...) + #[inline(always)] + pub fn new_from_words<'s>( + scope: &PinScope<'s, '_, ()>, + sign_bit: bool, + words: &[u64], + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__BigInt__NewFromWords( + sd.get_current_context(), + sign_bit as int, + words.len() as int, + words.as_ptr(), + ) + }) + } + } + + /// Returns the value of this BigInt as an unsigned 64-bit integer, and a + /// `bool` indicating that the conversion was lossless when `true`. + /// The boolean value will be `false` if the return value was truncated or wrapped around, + /// in particular if the BigInt is negative. + #[inline(always)] + pub fn u64_value(&self) -> (u64, bool) { + let mut lossless = MaybeUninit::uninit(); + let v = unsafe { v8__BigInt__Uint64Value(self, lossless.as_mut_ptr()) }; + let lossless = unsafe { lossless.assume_init() }; + (v, lossless) + } + + /// Returns the value of this BigInt as a signed 64-bit integer, and a `bool` + /// indicating that the conversion was lossless when `true`. + /// The boolean value will be `false` if the return value was truncated or wrapped around. + #[inline(always)] + pub fn i64_value(&self) -> (i64, bool) { + let mut lossless = MaybeUninit::uninit(); + let v = unsafe { v8__BigInt__Int64Value(self, lossless.as_mut_ptr()) }; + let lossless = unsafe { lossless.assume_init() }; + (v, lossless) + } + + /// Returns the number of 64-bit words needed to store the result of + /// `to_words_array`. + #[inline(always)] + pub fn word_count(&self) -> usize { + unsafe { v8__BigInt__WordCount(self) as usize } + } + + /// Converts this BigInt to a (sign_bit, words) pair. `sign_bit` will be true + /// if this BigInt is negative. If `words` has too few elements, the result will + /// be truncated to fit. + #[inline] + pub fn to_words_array<'a>( + &self, + words: &'a mut [u64], + ) -> (bool, &'a mut [u64]) { + let mut sign_bit = MaybeUninit::uninit(); + let mut word_count = words.len() as int; + unsafe { + v8__BigInt__ToWordsArray( + self, + sign_bit.as_mut_ptr(), + &mut word_count, + words.as_mut_ptr(), + ); + } + + let sign_bit = unsafe { sign_bit.assume_init() }; + debug_assert!(sign_bit == 0 || sign_bit == 1); + let word_count = word_count as usize; + + ( + sign_bit == 1, + if word_count < words.len() { + &mut words[..word_count] + } else { + words + }, + ) + } +} diff --git a/crates/v8/src/binding.cc b/crates/v8/src/binding.cc new file mode 100644 index 0000000000..f281e2e346 --- /dev/null +++ b/crates/v8/src/binding.cc @@ -0,0 +1,5482 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cppgc/allocation.h" +#include "cppgc/persistent.h" +#include "cppgc/platform.h" +#include "libplatform/libplatform.h" +#include "support.h" +#include "unicode/locid.h" +#include "unicode/timezone.h" +#include "unicode/unistr.h" +#include "v8-callbacks.h" +#include "v8-cppgc.h" +#include "v8-fast-api-calls.h" +#include "v8-inspector-protocol.h" +#include "v8-inspector.h" +#include "v8-internal.h" +#include "v8-platform.h" +#include "v8-profiler.h" +#include "v8.h" +#include "v8/src/execution/isolate-inl.h" +#include "v8/src/execution/isolate.h" +#include "v8/src/execution/microtask-queue.h" +#include "v8/src/flags/flags.h" +#include "v8/src/heap/base-page.h" +#include "v8/src/heap/memory-chunk-inl.h" +#include "v8/src/libplatform/default-platform.h" + +using namespace support; + +// TODO(bartlomieju): ideally we could ignore only some of the deprecated APIs +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + +template +constexpr size_t align_to(size_t size) { + return (size + sizeof(T) - 1) & ~(sizeof(T) - 1); +} + +#ifdef V8_ENABLE_CHECKS +constexpr size_t handle_scope_size = 4; +#else +constexpr size_t handle_scope_size = 3; +#endif + +static_assert(sizeof(two_pointers_t) == + sizeof(std::shared_ptr), + "std::shared_ptr size mismatch"); + +static_assert(sizeof(v8::HandleScope) == sizeof(size_t) * handle_scope_size, + "HandleScope size mismatch"); + +static_assert(sizeof(v8::EscapableHandleScope) == + sizeof(size_t) * (handle_scope_size + 1), + "EscapableHandleScope size mismatch"); + +static_assert(sizeof(v8::PromiseRejectMessage) == sizeof(size_t) * 3, + "PromiseRejectMessage size mismatch"); + +static_assert(sizeof(v8::IsolateGroup) == sizeof(size_t), + "IsolateGroup size mismatch"); + +static_assert(sizeof(v8::Locker) == sizeof(size_t) * 2, "Locker size mismatch"); + +static_assert(sizeof(v8::Unlocker) == sizeof(size_t) * 1, + "Unlocker size mismatch"); + +static_assert(sizeof(v8::ScriptCompiler::CompilationDetails) == + sizeof(int64_t) * 3, + "CompilationDetails size mismatch"); + +static_assert( + sizeof(v8::ScriptCompiler::Source) == + align_to(sizeof(size_t) * 8 + sizeof(int) * 2 + + // the last field before CompilationDetails on 32-bit + // systems will have a padding + align_to(sizeof(size_t)) + + sizeof(v8::ScriptCompiler::CompilationDetails)), + "Source size mismatch"); + +static_assert(sizeof(v8::ReturnValue) == sizeof(size_t) * 1, + "ReturnValue size mismatch"); + +static_assert(sizeof(v8::TryCatch) == sizeof(size_t) * 6, + "TryCatch size mismatch"); + +static_assert(sizeof(v8::Isolate::AllowJavascriptExecutionScope) == + sizeof(size_t) * 2, + "AllowJavascriptExecutionScope size mismatch"); + +static_assert(sizeof(v8::Location) == sizeof(int) * 2, + "Location size mismatch"); + +static_assert(sizeof(v8::SnapshotCreator) == sizeof(size_t) * 1, + "SnapshotCreator size mismatch"); + +static_assert(sizeof(v8::CFunction) == sizeof(size_t) * 2, + "CFunction size mismatch"); + +static_assert(sizeof(three_pointers_t) == sizeof(v8_inspector::StringView), + "StringView size mismatch"); + +#if INTPTR_MAX == INT64_MAX // 64-bit platforms +static_assert(sizeof(v8::ScriptCompiler::CachedData) == 24, + "CachedData size mismatch"); +static_assert(offsetof(v8::ScriptCompiler::CachedData, data) == 0, + "CachedData.data offset mismatch"); +static_assert(offsetof(v8::ScriptCompiler::CachedData, length) == 8, + "CachedData.length offset mismatch"); +static_assert(offsetof(v8::ScriptCompiler::CachedData, rejected) == 12, + "CachedData.rejected offset mismatch"); +static_assert(offsetof(v8::ScriptCompiler::CachedData, buffer_policy) == 16, + "CachedData.buffer_policy offset mismatch"); +static_assert(sizeof(v8::Isolate::DisallowJavascriptExecutionScope) == 16, + "DisallowJavascriptExecutionScope size mismatch"); +#else +static_assert(sizeof(v8::ScriptCompiler::CachedData) == 16, + "CachedData size mismatch"); +static_assert(offsetof(v8::ScriptCompiler::CachedData, data) == 0, + "CachedData.data offset mismatch"); +static_assert(offsetof(v8::ScriptCompiler::CachedData, length) == 4, + "CachedData.length offset mismatch"); +static_assert(offsetof(v8::ScriptCompiler::CachedData, rejected) == 8, + "CachedData.rejected offset mismatch"); +static_assert(offsetof(v8::ScriptCompiler::CachedData, buffer_policy) == 12, + "CachedData.buffer_policy offset mismatch"); +static_assert(sizeof(v8::Isolate::DisallowJavascriptExecutionScope) == 12, + "DisallowJavascriptExecutionScope size mismatch"); +#endif + +// Note: this currently uses an internal API to determine if the v8 sandbox is +// enabled in the testsuite etc. +extern "C" bool v8__V8__IsSandboxEnabled() { + return v8::internal::SandboxIsEnabled(); +} + +namespace { + +// Returns the isolate whose heap holds `value`, or nullptr if `value` is a Smi +// or lives in read-only or writable shared space. Those spaces belong to an +// isolate group rather than to an isolate. +const v8::internal::Isolate* MaybeGetIsolate(v8::internal::Address value) { + namespace i = v8::internal; + if (!i::Internals::HasHeapObjectTag(value)) return nullptr; + const i::MemoryChunk* chunk = i::MemoryChunk::FromAddress(value); + if (chunk->InReadOnlySpace() || chunk->InWritableSharedSpace()) { + return nullptr; + } + return i::Isolate::FromHeap(chunk->MetadataNoIsolateCheck()->heap()); +} + +// Returns whether two heap objects belong to the same isolate group. Each +// group has its own pointer compression cage when there can be more than one +// group; otherwise all isolates are in the same group. +bool InSameIsolateGroup(v8::internal::Address a, v8::internal::Address b) { +#ifdef V8_COMPRESS_POINTERS_IN_MULTIPLE_CAGES + namespace i = v8::internal; + constexpr i::Address kCageMask = ~(i::kPtrComprCageBaseAlignment - 1); + return (a & kCageMask) == (b & kCageMask); +#else + return true; +#endif +} + +} // namespace + +extern "C" { + +const v8::Isolate* v8__Data__MaybeGetIsolate(const v8::Data& self) { + namespace i = v8::internal; + return reinterpret_cast( + MaybeGetIsolate(i::ValueHelper::ValueAsAddress(&self))); +} + +// Smis can be used in any isolate, read-only and writable shared space values +// in any isolate of the group they belong to, and all other values only in the +// isolate whose heap holds them. +bool v8__Data__IsCompatibleWithIsolate(const v8::Data& self, + const v8::Isolate& isolate) { + namespace i = v8::internal; + i::Address value = i::ValueHelper::ValueAsAddress(&self); + if (!i::Internals::HasHeapObjectTag(value)) return true; + const i::Isolate* i_isolate = reinterpret_cast(&isolate); + if (!InSameIsolateGroup(value, i_isolate->cage_base())) return false; + const i::Isolate* owner = MaybeGetIsolate(value); + return owner == nullptr || owner == i_isolate; +} + +// Returns whether `self` can be used in an isolate that `other` can be used +// in. When neither value identifies an isolate, only their groups are +// compared. +bool v8__Data__IsCompatibleWith(const v8::Data& self, const v8::Data& other) { + namespace i = v8::internal; + i::Address a = i::ValueHelper::ValueAsAddress(&self); + i::Address b = i::ValueHelper::ValueAsAddress(&other); + if (!i::Internals::HasHeapObjectTag(a) || + !i::Internals::HasHeapObjectTag(b)) { + return true; + } + if (!InSameIsolateGroup(a, b)) return false; + const i::Isolate* owner_a = MaybeGetIsolate(a); + const i::Isolate* owner_b = MaybeGetIsolate(b); + return owner_a == nullptr || owner_b == nullptr || owner_a == owner_b; +} + +void v8__V8__SetFlagsFromCommandLine(int* argc, char** argv, + const char* usage) { + namespace i = v8::internal; + using HelpOptions = i::FlagList::HelpOptions; + HelpOptions help_options = HelpOptions(HelpOptions::kExit, usage); + i::FlagList::SetFlagsFromCommandLine(argc, argv, true, help_options); +} + +void v8__V8__SetFlagsFromString(const char* flags, size_t length) { + v8::V8::SetFlagsFromString(flags, length); +} + +void v8__V8__SetEntropySource(v8::EntropySource callback) { + v8::V8::SetEntropySource(callback); +} + +const char* v8__V8__GetVersion() { return v8::V8::GetVersion(); } + +void v8__V8__InitializePlatform(v8::Platform* platform) { + v8::V8::InitializePlatform(platform); +} + +void v8__V8__Initialize() { v8::V8::Initialize(); } + +bool v8__V8__Dispose() { return v8::V8::Dispose(); } + +void v8__V8__DisposePlatform() { v8::V8::DisposePlatform(); } + +void v8__V8__SetFatalErrorHandler(v8::V8FatalErrorCallback that) { + v8::V8::SetFatalErrorHandler(that); +} + +bool v8__V8__EnableWebAssemblyTrapHandler(bool use_v8_signal_handler) { + return v8::V8::EnableWebAssemblyTrapHandler(use_v8_signal_handler); +} + +v8::Isolate* v8__Isolate__New(const v8::Isolate::CreateParams& params) { + return v8::Isolate::New(params); +} + +v8::Isolate* v8__Isolate__NewInGroup(const v8::IsolateGroup& group, + const v8::Isolate::CreateParams& params) { + return v8::Isolate::New(group, params); +} + +// `v8::IsolateGroup` is a reference-counted handle whose only member is an +// `internal::IsolateGroup*`. `make_pod` moves that pointer out without running +// the destructor, so the reference acquired by `GetDefault`/`Create`/the copy +// constructor is handed to Rust rather than released here. +v8::internal::IsolateGroup* v8__IsolateGroup__GetDefault() { + return make_pod(v8::IsolateGroup::GetDefault()); +} + +bool v8__IsolateGroup__CanCreateNewGroups() { + return v8::IsolateGroup::CanCreateNewGroups(); +} + +v8::internal::IsolateGroup* v8__IsolateGroup__Create() { + return make_pod(v8::IsolateGroup::Create()); +} + +v8::internal::IsolateGroup* v8__IsolateGroup__CLONE( + const v8::IsolateGroup& self) { + return make_pod(v8::IsolateGroup(self)); +} + +v8::internal::IsolateGroup* v8__Isolate__GetGroup(const v8::Isolate* isolate) { + return make_pod(isolate->GetGroup()); +} + +void v8__IsolateGroup__DESTRUCT(v8::IsolateGroup* self) { + self->~IsolateGroup(); +} + +bool v8__IsolateGroup__EQ(const v8::IsolateGroup& self, + const v8::IsolateGroup& other) { + return self == other; +} + +void v8__Isolate__Dispose(v8::Isolate* isolate) { isolate->Dispose(); } + +void v8__Isolate__Enter(v8::Isolate* isolate) { isolate->Enter(); } + +void v8__Locker__CONSTRUCT(uninit_t* buf, v8::Isolate* isolate) { + construct_in_place(buf, isolate); +} + +void v8__Locker__DESTRUCT(v8::Locker* self) { self->~Locker(); } + +void v8__Unlocker__CONSTRUCT(uninit_t* buf, + v8::Isolate* isolate) { + construct_in_place(buf, isolate); +} + +void v8__Unlocker__DESTRUCT(v8::Unlocker* self) { self->~Unlocker(); } + +bool v8__Locker__IsLocked(v8::Isolate* isolate) { + return v8::Locker::IsLocked(isolate); +} + +void v8__Isolate__Exit(v8::Isolate* isolate) { isolate->Exit(); } + +v8::Isolate* v8__Isolate__GetCurrent() { return v8::Isolate::GetCurrent(); } + +v8::Isolate* v8__Isolate__TryGetCurrent() { + return v8::Isolate::TryGetCurrent(); +} + +const v8::Data* v8__Isolate__GetCurrentHostDefinedOptions( + v8::Isolate* isolate) { + return maybe_local_to_ptr(isolate->GetCurrentHostDefinedOptions()); +} + +void v8__Isolate__MemoryPressureNotification(v8::Isolate* isolate, + v8::MemoryPressureLevel level) { + isolate->MemoryPressureNotification(level); +} + +void v8__Isolate__ClearKeptObjects(v8::Isolate* isolate) { + isolate->ClearKeptObjects(); +} + +void v8__Isolate__LowMemoryNotification(v8::Isolate* isolate) { + isolate->LowMemoryNotification(); +} + +void v8__Isolate__ContextDisposedNotification( + v8::Isolate* isolate, v8::ContextDependants dependants) { + isolate->ContextDisposedNotification(dependants); +} + +void v8__Isolate__SetIdle(v8::Isolate* isolate, bool is_idle) { + isolate->SetIdle(is_idle); +} + +void v8__Isolate__GetHeapStatistics(v8::Isolate* isolate, + v8::HeapStatistics* s) { + isolate->GetHeapStatistics(s); +} + +const v8::Context* v8__Isolate__GetCurrentContext(v8::Isolate* isolate) { + return local_to_ptr(isolate->GetCurrentContext()); +} + +const v8::Context* v8__Isolate__GetEnteredOrMicrotaskContext( + v8::Isolate* isolate) { + return local_to_ptr(isolate->GetEnteredOrMicrotaskContext()); +} + +uint32_t v8__Isolate__GetNumberOfDataSlots(v8::Isolate* isolate) { + return isolate->GetNumberOfDataSlots(); +} + +void* v8__Isolate__GetData(v8::Isolate* isolate, uint32_t slot) { + return isolate->GetData(slot); +} + +void v8__Isolate__SetData(v8::Isolate* isolate, uint32_t slot, void* data) { + isolate->SetData(slot, data); +} + +const v8::Data* v8__Isolate__GetDataFromSnapshotOnce(v8::Isolate* isolate, + size_t index) { + return maybe_local_to_ptr(isolate->GetDataFromSnapshotOnce(index)); +} + +v8::MicrotasksPolicy v8__Isolate__GetMicrotasksPolicy( + const v8::Isolate* isolate) { + return isolate->GetMicrotasksPolicy(); +} + +void v8__Isolate__SetMicrotasksPolicy(v8::Isolate* isolate, + v8::MicrotasksPolicy policy) { + static_assert(0 == static_cast(v8::MicrotasksPolicy::kExplicit), + "v8::MicrotasksPolicy::kExplicit mismatch"); + static_assert(1 == static_cast(v8::MicrotasksPolicy::kScoped), + "v8::MicrotasksPolicy::kScoped mismatch"); + static_assert(2 == static_cast(v8::MicrotasksPolicy::kAuto), + "v8::MicrotasksPolicy::kAuto mismatch"); + isolate->SetMicrotasksPolicy(policy); +} + +void v8__Isolate__PerformMicrotaskCheckpoint(v8::Isolate* isolate) { + isolate->PerformMicrotaskCheckpoint(); +} + +void v8__Isolate__EnqueueMicrotask(v8::Isolate* isolate, + const v8::Function& function) { + isolate->EnqueueMicrotask(ptr_to_local(&function)); +} + +void v8__Isolate__RequestInterrupt(v8::Isolate* isolate, + v8::InterruptCallback callback, void* data) { + isolate->RequestInterrupt(callback, data); +} + +void v8__Isolate__SetPrepareStackTraceCallback( + v8::Isolate* isolate, v8::PrepareStackTraceCallback callback) { + isolate->SetPrepareStackTraceCallback(callback); +} + +void v8__Isolate__SetPromiseHook(v8::Isolate* isolate, v8::PromiseHook hook) { + isolate->SetPromiseHook(hook); +} + +void v8__Isolate__SetPromiseRejectCallback(v8::Isolate* isolate, + v8::PromiseRejectCallback callback) { + isolate->SetPromiseRejectCallback(callback); +} + +void v8__Isolate__SetWasmAsyncResolvePromiseCallback( + v8::Isolate* isolate, v8::WasmAsyncResolvePromiseCallback callback) { + isolate->SetWasmAsyncResolvePromiseCallback(callback); +} + +void v8__Isolate__SetCaptureStackTraceForUncaughtExceptions( + v8::Isolate* isolate, bool capture, int frame_limit) { + isolate->SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit); +} + +void v8__Isolate__SetHostInitializeImportMetaObjectCallback( + v8::Isolate* isolate, v8::HostInitializeImportMetaObjectCallback callback) { + isolate->SetHostInitializeImportMetaObjectCallback(callback); +} + +void v8__Isolate__SetHostImportModuleDynamicallyCallback( + v8::Isolate* isolate, v8::HostImportModuleDynamicallyCallback callback) { + isolate->SetHostImportModuleDynamicallyCallback(callback); +} + +void v8__Isolate__SetHostImportModuleWithPhaseDynamicallyCallback( + v8::Isolate* isolate, + v8::HostImportModuleWithPhaseDynamicallyCallback callback) { + isolate->SetHostImportModuleWithPhaseDynamicallyCallback(callback); +} + +void v8__Isolate__SetHostCreateShadowRealmContextCallback( + v8::Isolate* isolate, v8::HostCreateShadowRealmContextCallback callback) { + isolate->SetHostCreateShadowRealmContextCallback(callback); +} + +void v8__Isolate__SetUseCounterCallback( + v8::Isolate* isolate, v8::Isolate::UseCounterCallback callback) { + isolate->SetUseCounterCallback(callback); +} + +void v8__Isolate__SetModifyCodeGenerationFromStringsCallback( + v8::Isolate* isolate, + v8::ModifyCodeGenerationFromStringsCallback2 callback) { + isolate->SetModifyCodeGenerationFromStringsCallback(callback); +} + +bool v8__Isolate__AddMessageListener(v8::Isolate* isolate, + v8::MessageCallback callback) { + return isolate->AddMessageListener(callback); +} + +bool v8__Isolate__AddMessageListenerWithErrorLevel(v8::Isolate* isolate, + v8::MessageCallback callback, + int error_level) { + return isolate->AddMessageListenerWithErrorLevel(callback, error_level); +} + +void v8__Isolate__AddGCPrologueCallback( + v8::Isolate* isolate, v8::Isolate::GCCallbackWithData callback, void* data, + v8::GCType gc_type_filter) { + isolate->AddGCPrologueCallback(callback, data, gc_type_filter); +} + +void v8__Isolate__RemoveGCPrologueCallback( + v8::Isolate* isolate, v8::Isolate::GCCallbackWithData callback, + void* data) { + isolate->RemoveGCPrologueCallback(callback, data); +} + +void v8__Isolate__AddGCEpilogueCallback( + v8::Isolate* isolate, v8::Isolate::GCCallbackWithData callback, void* data, + v8::GCType gc_type_filter) { + isolate->AddGCEpilogueCallback(callback, data, gc_type_filter); +} + +void v8__Isolate__RemoveGCEpilogueCallback( + v8::Isolate* isolate, v8::Isolate::GCCallbackWithData callback, + void* data) { + isolate->RemoveGCEpilogueCallback(callback, data); +} + +void v8__Isolate__AddNearHeapLimitCallback(v8::Isolate* isolate, + v8::NearHeapLimitCallback callback, + void* data) { + isolate->AddNearHeapLimitCallback(callback, data); +} + +size_t v8__Isolate__NumberOfHeapSpaces(v8::Isolate* isolate) { + return isolate->NumberOfHeapSpaces(); +} + +bool v8__Isolate__GetHeapSpaceStatistics( + v8::Isolate* isolate, v8::HeapSpaceStatistics* space_statistics, + size_t index) { + return isolate->GetHeapSpaceStatistics(space_statistics, index); +} + +bool v8__Isolate__GetHeapCodeAndMetadataStatistics(v8::Isolate* isolate, + v8::HeapCodeStatistics* s) { + return isolate->GetHeapCodeAndMetadataStatistics(s); +} + +void v8__Isolate__RemoveNearHeapLimitCallback( + v8::Isolate* isolate, v8::NearHeapLimitCallback callback, + size_t heap_limit) { + isolate->RemoveNearHeapLimitCallback(callback, heap_limit); +} + +int64_t v8__Isolate__AdjustAmountOfExternalAllocatedMemory( + v8::Isolate* isolate, int64_t change_in_bytes) { + return isolate->AdjustAmountOfExternalAllocatedMemory(change_in_bytes); +} + +void v8__Isolate__SetOOMErrorHandler(v8::Isolate* isolate, + v8::OOMErrorCallback callback) { + isolate->SetOOMErrorHandler(callback); +} + +const v8::Value* v8__Isolate__ThrowException(v8::Isolate* isolate, + const v8::Value& exception) { + return local_to_ptr(isolate->ThrowException(ptr_to_local(&exception))); +} + +void v8__Isolate__TerminateExecution(v8::Isolate* isolate) { + isolate->TerminateExecution(); +} + +bool v8__Isolate__IsExecutionTerminating(v8::Isolate* isolate) { + return isolate->IsExecutionTerminating(); +} + +void v8__Isolate__CancelTerminateExecution(v8::Isolate* isolate) { + isolate->CancelTerminateExecution(); +} + +void v8__Isolate__SetAllowAtomicsWait(v8::Isolate* isolate, bool allow) { + isolate->SetAllowAtomicsWait(allow); +} + +void v8__Isolate__SetWasmStreamingCallback(v8::Isolate* isolate, + v8::WasmStreamingCallback callback) { + isolate->SetWasmStreamingCallback(callback); +} + +void v8__Isolate__SetAllowWasmCodeGenerationCallback( + v8::Isolate* isolate, v8::AllowWasmCodeGenerationCallback callback) { + isolate->SetAllowWasmCodeGenerationCallback(callback); +} + +bool v8__Isolate__HasPendingBackgroundTasks(v8::Isolate* isolate) { + return isolate->HasPendingBackgroundTasks(); +} + +void v8__Isolate__RequestGarbageCollectionForTesting( + v8::Isolate* isolate, v8::Isolate::GarbageCollectionType type) { + isolate->RequestGarbageCollectionForTesting(type); +} + +void v8__Isolate__CreateParams__CONSTRUCT( + uninit_t* buf) { + construct_in_place(buf); +} + +size_t v8__Isolate__CreateParams__SIZEOF() { + return sizeof(v8::Isolate::CreateParams); +} + +void v8__Isolate__DateTimeConfigurationChangeNotification( + v8::Isolate* isolate, v8::Isolate::TimeZoneDetection time_zone_detection) { + isolate->DateTimeConfigurationChangeNotification(time_zone_detection); +} + +void v8__ResourceConstraints__ConfigureDefaultsFromHeapSize( + v8::ResourceConstraints* constraints, size_t initial_heap_size_in_bytes, + size_t maximum_heap_size_in_bytes) { + constraints->ConfigureDefaultsFromHeapSize(initial_heap_size_in_bytes, + maximum_heap_size_in_bytes); +} + +void v8__ResourceConstraints__ConfigureDefaults( + v8::ResourceConstraints* constraints, uint64_t physical_memory, + uint64_t virtual_memory_limit) { + constraints->ConfigureDefaults(physical_memory, virtual_memory_limit); +} + +size_t v8__ResourceConstraints__max_old_generation_size_in_bytes( + const v8::ResourceConstraints* constraints) { + return constraints->max_old_generation_size_in_bytes(); +} + +void v8__ResourceConstraints__set_max_old_generation_size_in_bytes( + v8::ResourceConstraints* constraints, size_t limit) { + constraints->set_max_old_generation_size_in_bytes(limit); +} + +size_t v8__ResourceConstraints__max_young_generation_size_in_bytes( + const v8::ResourceConstraints* constraints) { + return constraints->max_young_generation_size_in_bytes(); +} + +void v8__ResourceConstraints__set_max_young_generation_size_in_bytes( + v8::ResourceConstraints* constraints, size_t limit) { + constraints->set_max_young_generation_size_in_bytes(limit); +} + +size_t v8__ResourceConstraints__code_range_size_in_bytes( + const v8::ResourceConstraints* constraints) { + return constraints->code_range_size_in_bytes(); +} + +void v8__ResourceConstraints__set_code_range_size_in_bytes( + v8::ResourceConstraints* constraints, size_t limit) { + constraints->set_code_range_size_in_bytes(limit); +} + +uint32_t* v8__ResourceConstraints__stack_limit( + const v8::ResourceConstraints* constraints) { + return constraints->stack_limit(); +} + +void v8__ResourceConstraints__set_stack_limit( + v8::ResourceConstraints* constraints, uint32_t* value) { + constraints->set_stack_limit(value); +} + +size_t v8__ResourceConstraints__initial_old_generation_size_in_bytes( + const v8::ResourceConstraints* constraints) { + return constraints->initial_old_generation_size_in_bytes(); +} + +void v8__ResourceConstraints__set_initial_old_generation_size_in_bytes( + v8::ResourceConstraints* constraints, size_t initial_size) { + constraints->set_initial_old_generation_size_in_bytes(initial_size); +} + +size_t v8__ResourceConstraints__initial_young_generation_size_in_bytes( + const v8::ResourceConstraints* constraints) { + return constraints->initial_young_generation_size_in_bytes(); +} + +void v8__ResourceConstraints__set_initial_young_generation_size_in_bytes( + v8::ResourceConstraints* constraints, size_t initial_size) { + constraints->set_initial_young_generation_size_in_bytes(initial_size); +} + +void v8__HandleScope__CONSTRUCT(uninit_t* buf, + v8::Isolate* isolate) { + construct_in_place(buf, isolate); +} + +void v8__HandleScope__DESTRUCT(v8::HandleScope* self) { self->~HandleScope(); } + +const v8::Data* v8__Local__New(v8::Isolate* isolate, const v8::Data& other) { + return local_to_ptr(v8::Local::New(isolate, ptr_to_local(&other))); +} + +const v8::Data* v8__Global__New(v8::Isolate* isolate, const v8::Data& other) { + // We have to use `std::move()` here because v8 disables the copy constructor + // for class `v8::Global`. + auto global = v8::Global(isolate, ptr_to_local(&other)); + return make_pod(std::move(global)); +} + +const v8::Data* v8__Global__NewWeak( + v8::Isolate* isolate, const v8::Data& other, void* parameter, + v8::WeakCallbackInfo::Callback callback) { + auto global = v8::Global(isolate, ptr_to_local(&other)); + global.SetWeak(parameter, callback, v8::WeakCallbackType::kParameter); + return make_pod(std::move(global)); +} + +void v8__Global__Reset(const v8::Data* data) { + auto global = ptr_to_global(data); + global.Reset(); +} + +void v8__TracedReference__CONSTRUCT( + uninit_t>* buf) { + construct_in_place>(buf); +} + +void v8__TracedReference__DESTRUCT(v8::TracedReference* self) { + self->~TracedReference(); +} + +void v8__TracedReference__Reset(v8::TracedReference* self, + v8::Isolate* isolate, const v8::Data* other) { + self->Reset(isolate, ptr_to_local(other)); +} + +const v8::Data* v8__TracedReference__Get(v8::TracedReference* self, + v8::Isolate* isolate) { + return local_to_ptr(self->Get(isolate)); +} + +void v8__Eternal__CONSTRUCT(uninit_t>* buf) { + construct_in_place>(buf); +} + +void v8__Eternal__DESTRUCT(v8::Eternal* self) { self->~Eternal(); } + +void v8__Eternal__Clear(v8::Eternal* self) { self->Clear(); } + +bool v8__Eternal__IsEmpty(v8::Eternal* self) { + return self->IsEmpty(); +} + +void v8__Eternal__Set(v8::Eternal* self, v8::Isolate* isolate, + const v8::Data* value) { + self->Set(isolate, ptr_to_local(value)); +} + +const v8::Data* v8__Eternal__Get(v8::Eternal* self, + v8::Isolate* isolate) { + return local_to_ptr(self->Get(isolate)); +} + +v8::Isolate* v8__WeakCallbackInfo__GetIsolate( + const v8::WeakCallbackInfo* self) { + return self->GetIsolate(); +} + +void* v8__WeakCallbackInfo__GetParameter( + const v8::WeakCallbackInfo* self) { + return self->GetParameter(); +} + +void v8__WeakCallbackInfo__SetSecondPassCallback( + const v8::WeakCallbackInfo* self, + v8::WeakCallbackInfo::Callback callback) { + self->SetSecondPassCallback(callback); +} + +void v8__ScriptCompiler__Source__CONSTRUCT( + uninit_t* buf, const v8::String& source_string, + const v8::ScriptOrigin* origin, + v8::ScriptCompiler::CachedData* cached_data) { + if (origin) { + construct_in_place( + buf, ptr_to_local(&source_string), *origin, cached_data); + } else { + construct_in_place( + buf, ptr_to_local(&source_string), cached_data); + } +} + +void v8__ScriptCompiler__Source__DESTRUCT(v8::ScriptCompiler::Source* self) { + self->~Source(); +} + +v8::ScriptCompiler::CachedData* v8__ScriptCompiler__CachedData__NEW( + const uint8_t* data, int length) { + return new v8::ScriptCompiler::CachedData( + data, length, v8::ScriptCompiler::CachedData::BufferNotOwned); +} + +void v8__ScriptCompiler__CachedData__DELETE( + v8::ScriptCompiler::CachedData* self) { + delete self; +} + +int v8__ScriptCompiler__CachedData__CompatibilityCheck( + v8::ScriptCompiler::CachedData* self, v8::Isolate* isolate) { + return static_cast(self->CompatibilityCheck(isolate)); +} + +bool v8__ScriptCompiler__CompileOptionsIsValid(int compile_options) { + return v8::ScriptCompiler::CompileOptionsIsValid( + static_cast(compile_options)); +} + +const v8::ScriptCompiler::CachedData* v8__ScriptCompiler__Source__GetCachedData( + const v8::ScriptCompiler::Source* source) { + return source->GetCachedData(); +} + +const v8::Module* v8__ScriptCompiler__CompileModule( + v8::Isolate* isolate, v8::ScriptCompiler::Source* source, + v8::ScriptCompiler::CompileOptions options, + v8::ScriptCompiler::NoCacheReason no_cache_reason) { + v8::MaybeLocal maybe_local = v8::ScriptCompiler::CompileModule( + isolate, source, options, no_cache_reason); + return maybe_local_to_ptr(maybe_local); +} + +const v8::Script* v8__ScriptCompiler__Compile( + const v8::Context* context, v8::ScriptCompiler::Source* source, + v8::ScriptCompiler::CompileOptions options, + v8::ScriptCompiler::NoCacheReason no_cache_reason) { + v8::MaybeLocal maybe_local = v8::ScriptCompiler::Compile( + ptr_to_local(context), source, options, no_cache_reason); + return maybe_local_to_ptr(maybe_local); +} + +const v8::Function* v8__ScriptCompiler__CompileFunction( + const v8::Context* context, v8::ScriptCompiler::Source* source, + size_t arguments_count, const v8::String** arguments, + size_t context_extensions_count, const v8::Object** context_extensions, + v8::ScriptCompiler::CompileOptions options, + v8::ScriptCompiler::NoCacheReason no_cache_reason) { + return maybe_local_to_ptr(v8::ScriptCompiler::CompileFunction( + ptr_to_local(context), source, arguments_count, + reinterpret_cast*>(arguments), + context_extensions_count, + reinterpret_cast*>(context_extensions), options, + no_cache_reason)); +} + +const v8::UnboundScript* v8__ScriptCompiler__CompileUnboundScript( + v8::Isolate* isolate, v8::ScriptCompiler::Source* source, + v8::ScriptCompiler::CompileOptions options, + v8::ScriptCompiler::NoCacheReason no_cache_reason) { + v8::MaybeLocal maybe_local = + v8::ScriptCompiler::CompileUnboundScript(isolate, source, options, + no_cache_reason); + return maybe_local_to_ptr(maybe_local); +} + +uint32_t v8__ScriptCompiler__CachedDataVersionTag() { + return v8::ScriptCompiler::CachedDataVersionTag(); +} + +size_t v8__TypedArray__Length(const v8::TypedArray* self) { + return ptr_to_local(self)->Length(); +} + +bool v8__Data__EQ(const v8::Data& self, const v8::Data& other) { + return ptr_to_local(&self) == ptr_to_local(&other); +} + +bool v8__Data__IsBigInt(const v8::Data& self) { + return self.IsValue() && reinterpret_cast(self).IsBigInt(); +} + +bool v8__Data__IsBoolean(const v8::Data& self) { + return self.IsValue() && reinterpret_cast(self).IsBoolean(); +} + +bool v8__Data__IsContext(const v8::Data& self) { return self.IsContext(); } + +bool v8__Data__IsFixedArray(const v8::Data& self) { + return self.IsFixedArray(); +} + +bool v8__Data__IsFunctionTemplate(const v8::Data& self) { + return self.IsFunctionTemplate(); +} + +bool v8__Data__IsModule(const v8::Data& self) { return self.IsModule(); } + +bool v8__Data__IsModuleRequest(const v8::Data& self) { + return self.IsModuleRequest(); +} + +bool v8__Data__IsName(const v8::Data& self) { + return self.IsValue() && reinterpret_cast(self).IsName(); +} + +bool v8__Data__IsNumber(const v8::Data& self) { + return self.IsValue() && reinterpret_cast(self).IsNumber(); +} + +bool v8__Data__IsObjectTemplate(const v8::Data& self) { + return self.IsObjectTemplate(); +} + +bool v8__Data__IsDictionaryTemplate(const v8::Data& self) { + return self.IsDictionaryTemplate(); +} + +bool v8__Data__IsPrimitive(const v8::Data& self) { + return self.IsValue() && + reinterpret_cast(self).IsPrimitive(); +} + +bool v8__Data__IsPrivate(const v8::Data& self) { return self.IsPrivate(); } + +bool v8__Data__IsString(const v8::Data& self) { + return self.IsValue() && reinterpret_cast(self).IsString(); +} + +bool v8__Data__IsSymbol(const v8::Data& self) { + return self.IsValue() && reinterpret_cast(self).IsSymbol(); +} + +bool v8__Data__IsValue(const v8::Data& self) { return self.IsValue(); } + +bool v8__Value__IsUndefined(const v8::Value& self) { + return self.IsUndefined(); +} + +bool v8__Value__IsNull(const v8::Value& self) { return self.IsNull(); } + +bool v8__Value__IsNullOrUndefined(const v8::Value& self) { + return self.IsNullOrUndefined(); +} + +bool v8__Value__IsTrue(const v8::Value& self) { return self.IsTrue(); } + +bool v8__Value__IsFalse(const v8::Value& self) { return self.IsFalse(); } + +bool v8__Value__IsName(const v8::Value& self) { return self.IsName(); } + +bool v8__Value__IsString(const v8::Value& self) { return self.IsString(); } + +bool v8__Value__IsSymbol(const v8::Value& self) { return self.IsSymbol(); } + +bool v8__Value__IsFunction(const v8::Value& self) { return self.IsFunction(); } + +bool v8__Value__IsArray(const v8::Value& self) { return self.IsArray(); } + +bool v8__Value__IsObject(const v8::Value& self) { return self.IsObject(); } + +bool v8__Value__IsBigInt(const v8::Value& self) { return self.IsBigInt(); } + +bool v8__Value__IsBoolean(const v8::Value& self) { return self.IsBoolean(); } + +bool v8__Value__IsNumber(const v8::Value& self) { return self.IsNumber(); } + +bool v8__Value__IsExternal(const v8::Value& self) { return self.IsExternal(); } + +bool v8__Value__IsInt32(const v8::Value& self) { return self.IsInt32(); } + +bool v8__Value__IsUint32(const v8::Value& self) { return self.IsUint32(); } + +bool v8__Value__IsDate(const v8::Value& self) { return self.IsDate(); } + +bool v8__Value__IsArgumentsObject(const v8::Value& self) { + return self.IsArgumentsObject(); +} + +bool v8__Value__IsBigIntObject(const v8::Value& self) { + return self.IsBigIntObject(); +} + +bool v8__Value__IsBooleanObject(const v8::Value& self) { + return self.IsBooleanObject(); +} + +bool v8__Value__IsNumberObject(const v8::Value& self) { + return self.IsNumberObject(); +} + +bool v8__Value__IsStringObject(const v8::Value& self) { + return self.IsStringObject(); +} + +bool v8__Value__IsSymbolObject(const v8::Value& self) { + return self.IsSymbolObject(); +} + +bool v8__Value__IsNativeError(const v8::Value& self) { + return self.IsNativeError(); +} + +bool v8__Value__IsRegExp(const v8::Value& self) { return self.IsRegExp(); } + +bool v8__Value__IsAsyncFunction(const v8::Value& self) { + return self.IsAsyncFunction(); +} + +bool v8__Value__IsGeneratorFunction(const v8::Value& self) { + return self.IsGeneratorFunction(); +} + +bool v8__Value__IsGeneratorObject(const v8::Value& self) { + return self.IsGeneratorObject(); +} + +bool v8__Value__IsPromise(const v8::Value& self) { return self.IsPromise(); } + +bool v8__Value__IsMap(const v8::Value& self) { return self.IsMap(); } + +bool v8__Value__IsSet(const v8::Value& self) { return self.IsSet(); } + +bool v8__Value__IsMapIterator(const v8::Value& self) { + return self.IsMapIterator(); +} + +bool v8__Value__IsSetIterator(const v8::Value& self) { + return self.IsSetIterator(); +} + +bool v8__Value__IsSetGeneratorObject(const v8::Value& self) { + return self.IsGeneratorObject(); +} + +bool v8__Value__IsWeakMap(const v8::Value& self) { return self.IsWeakMap(); } + +bool v8__Value__IsWeakSet(const v8::Value& self) { return self.IsWeakSet(); } + +bool v8__Value__IsWeakRef(const v8::Value& self) { return self.IsWeakRef(); } + +bool v8__Value__IsWasmNull(const v8::Value& self) { return self.IsWasmNull(); } + +bool v8__Value__IsArrayBuffer(const v8::Value& self) { + return self.IsArrayBuffer(); +} + +bool v8__Value__IsArrayBufferView(const v8::Value& self) { + return self.IsArrayBufferView(); +} + +bool v8__Value__IsTypedArray(const v8::Value& self) { + return self.IsTypedArray(); +} + +bool v8__Value__IsUint8Array(const v8::Value& self) { + return self.IsUint8Array(); +} + +bool v8__Value__IsUint8ClampedArray(const v8::Value& self) { + return self.IsUint8ClampedArray(); +} + +bool v8__Value__IsInt8Array(const v8::Value& self) { + return self.IsInt8Array(); +} + +bool v8__Value__IsUint16Array(const v8::Value& self) { + return self.IsUint16Array(); +} + +bool v8__Value__IsInt16Array(const v8::Value& self) { + return self.IsInt16Array(); +} + +bool v8__Value__IsUint32Array(const v8::Value& self) { + return self.IsUint32Array(); +} + +bool v8__Value__IsInt32Array(const v8::Value& self) { + return self.IsInt32Array(); +} + +bool v8__Value__IsFloat16Array(const v8::Value& self) { + return self.IsFloat16Array(); +} + +bool v8__Value__IsFloat32Array(const v8::Value& self) { + return self.IsFloat32Array(); +} + +bool v8__Value__IsFloat64Array(const v8::Value& self) { + return self.IsFloat64Array(); +} + +bool v8__Value__IsBigInt64Array(const v8::Value& self) { + return self.IsBigInt64Array(); +} + +bool v8__Value__IsBigUint64Array(const v8::Value& self) { + return self.IsBigUint64Array(); +} + +bool v8__Value__IsDataView(const v8::Value& self) { return self.IsDataView(); } + +bool v8__Value__IsSharedArrayBuffer(const v8::Value& self) { + return self.IsSharedArrayBuffer(); +} + +bool v8__Value__IsProxy(const v8::Value& self) { return self.IsProxy(); } + +bool v8__Value__IsWasmModuleObject(const v8::Value& self) { + return self.IsWasmModuleObject(); +} + +bool v8__Value__IsWasmMemoryObject(const v8::Value& self) { + return self.IsWasmMemoryObject(); +} + +bool v8__Value__IsModuleNamespaceObject(const v8::Value& self) { + return self.IsModuleNamespaceObject(); +} + +bool v8__Value__StrictEquals(const v8::Value& self, const v8::Value& that) { + return self.StrictEquals(ptr_to_local(&that)); +} + +bool v8__Value__SameValue(const v8::Value& self, const v8::Value& that) { + return self.SameValue(ptr_to_local(&that)); +} + +const v8::Uint32* v8__Value__ToUint32(const v8::Value& self, + const v8::Context& context) { + return maybe_local_to_ptr(self.ToUint32(ptr_to_local(&context))); +} + +const v8::Int32* v8__Value__ToInt32(const v8::Value& self, + const v8::Context& context) { + return maybe_local_to_ptr(self.ToInt32(ptr_to_local(&context))); +} + +const v8::Integer* v8__Value__ToInteger(const v8::Value& self, + const v8::Context& context) { + return maybe_local_to_ptr(self.ToInteger(ptr_to_local(&context))); +} + +const v8::BigInt* v8__Value__ToBigInt(const v8::Value& self, + const v8::Context& context) { + return maybe_local_to_ptr(self.ToBigInt(ptr_to_local(&context))); +} + +const v8::String* v8__Value__ToString(const v8::Value& self, + const v8::Context& context) { + return maybe_local_to_ptr(self.ToString(ptr_to_local(&context))); +} + +const v8::String* v8__Value__ToDetailString(const v8::Value& self, + const v8::Context& context) { + return maybe_local_to_ptr(self.ToDetailString(ptr_to_local(&context))); +} + +const v8::Number* v8__Value__ToNumber(const v8::Value& self, + const v8::Context& context) { + return maybe_local_to_ptr(self.ToNumber(ptr_to_local(&context))); +} + +const v8::Object* v8__Value__ToObject(const v8::Value& self, + const v8::Context& context) { + return maybe_local_to_ptr(self.ToObject(ptr_to_local(&context))); +} + +const v8::Boolean* v8__Value__ToBoolean(const v8::Value& self, + v8::Isolate* isolate) { + return local_to_ptr(self.ToBoolean(isolate)); +} + +void v8__Value__InstanceOf(const v8::Value& self, const v8::Context& context, + const v8::Object& object, v8::Maybe* out) { + v8::Value* self_non_const = const_cast(&self); + *out = + self_non_const->InstanceOf(ptr_to_local(&context), ptr_to_local(&object)); +} + +void v8__Value__NumberValue(const v8::Value& self, const v8::Context& context, + v8::Maybe* out) { + *out = self.NumberValue(ptr_to_local(&context)); +} + +void v8__Value__IntegerValue(const v8::Value& self, const v8::Context& context, + v8::Maybe* out) { + *out = self.IntegerValue(ptr_to_local(&context)); +} + +void v8__Value__Uint32Value(const v8::Value& self, const v8::Context& context, + v8::Maybe* out) { + *out = self.Uint32Value(ptr_to_local(&context)); +} + +void v8__Value__Int32Value(const v8::Value& self, const v8::Context& context, + v8::Maybe* out) { + *out = self.Int32Value(ptr_to_local(&context)); +} + +bool v8__Value__BooleanValue(const v8::Value& self, v8::Isolate* isolate) { + return self.BooleanValue(isolate); +} + +const v8::String* v8__Value__TypeOf(v8::Value& self, v8::Isolate* isolate) { + return local_to_ptr(self.TypeOf(isolate)); +} + +uint32_t v8__Value__GetHash(v8::Value& self) { return self.GetHash(); } + +const v8::Primitive* v8__Null(v8::Isolate* isolate) { + return local_to_ptr(v8::Null(isolate)); +} + +const v8::Primitive* v8__Undefined(v8::Isolate* isolate) { + return local_to_ptr(v8::Undefined(isolate)); +} + +const v8::Boolean* v8__Boolean__New(v8::Isolate* isolate, bool value) { + return local_to_ptr(v8::Boolean::New(isolate, value)); +} + +bool v8__Boolean__Value(const v8::Boolean& self) { return self.Value(); } + +int v8__FixedArray__Length(const v8::FixedArray& self) { return self.Length(); } + +const v8::Data* v8__FixedArray__Get(const v8::FixedArray& self, int index) { + return local_to_ptr(ptr_to_local(&self)->Get(index)); +} + +const v8::PrimitiveArray* v8__PrimitiveArray__New(v8::Isolate* isolate, + int length) { + return local_to_ptr(v8::PrimitiveArray::New(isolate, length)); +} + +int v8__PrimitiveArray__Length(const v8::PrimitiveArray& self) { + return self.Length(); +} + +void v8__PrimitiveArray__Set(const v8::PrimitiveArray& self, + v8::Isolate* isolate, int index, + const v8::Primitive& item) { + ptr_to_local(&self)->Set(isolate, index, ptr_to_local(&item)); +} + +const v8::Primitive* v8__PrimitiveArray__Get(const v8::PrimitiveArray& self, + v8::Isolate* isolate, int index) { + return local_to_ptr(ptr_to_local(&self)->Get(isolate, index)); +} + +v8::BackingStore* v8__ArrayBuffer__NewBackingStore__with_byte_length( + v8::Isolate* isolate, size_t byte_length) { + std::unique_ptr u = + v8::ArrayBuffer::NewBackingStore(isolate, byte_length); + return u.release(); +} + +v8::BackingStore* v8__ArrayBuffer__NewBackingStore__with_mode( + v8::Isolate* isolate, size_t byte_length, + v8::BackingStoreInitializationMode initialization_mode, + v8::BackingStoreOnFailureMode on_failure) { + std::unique_ptr u = v8::ArrayBuffer::NewBackingStore( + isolate, byte_length, initialization_mode, on_failure); + return u.release(); +} + +v8::BackingStore* v8__ArrayBuffer__NewResizableBackingStore( + size_t byte_length, size_t max_byte_length) { + std::unique_ptr u = + v8::ArrayBuffer::NewResizableBackingStore(byte_length, max_byte_length); + return u.release(); +} + +v8::BackingStore* v8__ArrayBuffer__NewBackingStore__with_data( + void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, + void* deleter_data) { + std::unique_ptr u = v8::ArrayBuffer::NewBackingStore( + data, byte_length, deleter, deleter_data); + return u.release(); +} + +two_pointers_t v8__ArrayBuffer__GetBackingStore(const v8::ArrayBuffer& self) { + return make_pod(ptr_to_local(&self)->GetBackingStore()); +} + +bool v8__BackingStore__IsResizableByUserJavaScript( + const v8::BackingStore& self) { + return ptr_to_local(&self)->IsResizableByUserJavaScript(); +} + +void* v8__ArrayBuffer__Data(const v8::ArrayBuffer& self) { + return ptr_to_local(&self)->Data(); +} + +MaybeBool v8__ArrayBuffer__Detach(const v8::ArrayBuffer& self, + const v8::Value* key) { + return maybe_to_maybe_bool(ptr_to_local(&self)->Detach(ptr_to_local(key))); +} + +bool v8__ArrayBuffer__IsDetachable(const v8::ArrayBuffer& self) { + return ptr_to_local(&self)->IsDetachable(); +} + +bool v8__ArrayBuffer__WasDetached(const v8::ArrayBuffer& self) { + return ptr_to_local(&self)->WasDetached(); +} + +bool v8__ArrayBuffer__IsResizableByUserJavaScript(const v8::ArrayBuffer& self) { + return ptr_to_local(&self)->IsResizableByUserJavaScript(); +} + +bool v8__ArrayBuffer__IsImmutable(const v8::ArrayBuffer& self) { + return ptr_to_local(&self)->IsImmutable(); +} + +void v8__ArrayBuffer__SetDetachKey(const v8::ArrayBuffer& self, + const v8::Value* key) { + return ptr_to_local(&self)->SetDetachKey(ptr_to_local(key)); +} + +void* v8__BackingStore__Data(const v8::BackingStore& self) { + return self.Data(); +} + +size_t v8__BackingStore__ByteLength(const v8::BackingStore& self) { + return self.ByteLength(); +} + +size_t v8__BackingStore__MaxByteLength(const v8::BackingStore& self) { + return self.MaxByteLength(); +} + +bool v8__BackingStore__IsShared(const v8::BackingStore& self) { + return self.IsShared(); +} + +void v8__BackingStore__DELETE(v8::BackingStore* self) { delete self; } + +two_pointers_t std__shared_ptr__v8__BackingStore__COPY( + const std::shared_ptr& ptr) { + return make_pod(ptr); +} + +two_pointers_t std__shared_ptr__v8__BackingStore__CONVERT__std__unique_ptr( + v8::BackingStore* unique_ptr) { + return make_pod( + std::shared_ptr(unique_ptr)); +} + +v8::BackingStore* std__shared_ptr__v8__BackingStore__get( + const std::shared_ptr& ptr) { + return ptr.get(); +} + +void std__shared_ptr__v8__BackingStore__reset( + std::shared_ptr* ptr) { + ptr->reset(); +} + +long std__shared_ptr__v8__BackingStore__use_count( + const std::shared_ptr& ptr) { + return ptr.use_count(); +} + +two_pointers_t std__shared_ptr__v8__ArrayBuffer__Allocator__COPY( + const std::shared_ptr& ptr) { + return make_pod(ptr); +} + +two_pointers_t +std__shared_ptr__v8__ArrayBuffer__Allocator__CONVERT__std__unique_ptr( + v8::ArrayBuffer::Allocator* unique_ptr) { + return make_pod( + std::shared_ptr(unique_ptr)); +} + +v8::ArrayBuffer::Allocator* std__shared_ptr__v8__ArrayBuffer__Allocator__get( + const std::shared_ptr& ptr) { + return ptr.get(); +} + +void std__shared_ptr__v8__ArrayBuffer__Allocator__reset( + std::shared_ptr* ptr) { + ptr->reset(); +} + +long std__shared_ptr__v8__ArrayBuffer__Allocator__use_count( + const std::shared_ptr& ptr) { + return ptr.use_count(); +} + +int v8__Name__GetIdentityHash(const v8::Name& self) { + return ptr_to_local(&self)->GetIdentityHash(); +} + +const v8::String* v8__String__Empty(v8::Isolate* isolate) { + return local_to_ptr(v8::String::Empty(isolate)); +} + +const v8::String* v8__String__NewFromUtf8(v8::Isolate* isolate, + const char* data, + v8::NewStringType new_type, + int length) { + return maybe_local_to_ptr( + v8::String::NewFromUtf8(isolate, data, new_type, length)); +} + +const v8::String* v8__String__NewFromOneByte(v8::Isolate* isolate, + const uint8_t* data, + v8::NewStringType new_type, + int length) { + return maybe_local_to_ptr( + v8::String::NewFromOneByte(isolate, data, new_type, length)); +} + +const v8::String* v8__String__NewFromTwoByte(v8::Isolate* isolate, + const uint16_t* data, + v8::NewStringType new_type, + int length) { + return maybe_local_to_ptr( + v8::String::NewFromTwoByte(isolate, data, new_type, length)); +} + +const v8::String* v8__String__Concat(v8::Isolate* isolate, + const v8::String& left, + const v8::String& right) { + return local_to_ptr( + v8::String::Concat(isolate, ptr_to_local(&left), ptr_to_local(&right))); +} + +int v8__String__Length(const v8::String& self) { return self.Length(); } + +int v8__String__Utf8Length(const v8::String& self, v8::Isolate* isolate) { + return self.Utf8LengthV2(isolate); +} + +void v8__String__Write_v2(const v8::String& self, v8::Isolate* isolate, + uint32_t offset, uint32_t length, uint16_t* buffer, + int flags) { + return self.WriteV2(isolate, offset, length, buffer, flags); +} + +void v8__String__WriteOneByte_v2(const v8::String& self, v8::Isolate* isolate, + uint32_t offset, uint32_t length, + uint8_t* buffer, int flags) { + return self.WriteOneByteV2(isolate, offset, length, buffer, flags); +} + +size_t v8__String__WriteUtf8_v2(const v8::String& self, v8::Isolate* isolate, + char* buffer, size_t capacity, int flags, + size_t* processed_characters_return) { + return self.WriteUtf8V2(isolate, buffer, capacity, flags, + processed_characters_return); +} + +const v8::String::ExternalStringResource* v8__String__GetExternalStringResource( + const v8::String& self) { + return self.GetExternalStringResource(); +} + +const v8::String::ExternalStringResourceBase* +v8__String__GetExternalStringResourceBase(const v8::String& self, + v8::String::Encoding* encoding_out) { + return self.GetExternalStringResourceBase(encoding_out); +} + +class ExternalOneByteString : public v8::String::ExternalOneByteStringResource { + public: + using RustDestroy = void (*)(char*, size_t); + ExternalOneByteString(char* data, size_t length, RustDestroy rustDestroy, + v8::Isolate* isolate) + : data_(data), + length_(length), + rustDestroy_(rustDestroy), + isolate_(isolate) { + isolate_->AdjustAmountOfExternalAllocatedMemory( + static_cast(length_)); + } + ~ExternalOneByteString() override { + (*rustDestroy_)(data_, length_); + isolate_->AdjustAmountOfExternalAllocatedMemory( + -static_cast(length_)); + } + + const char* data() const override { return data_; } + + size_t length() const override { return length_; } + + private: + char* data_; + const size_t length_; + RustDestroy rustDestroy_; + v8::Isolate* isolate_; +}; + +class ExternalStaticOneByteStringResource + : public v8::String::ExternalOneByteStringResource { + public: + ExternalStaticOneByteStringResource(const char* data, int length) + : _data(data), _length(length) {} + const char* data() const override { return _data; } + size_t length() const override { return _length; } + + private: + const char* _data; + const int _length; +}; + +const v8::String* v8__String__NewExternalOneByteConst( + v8::Isolate* isolate, v8::String::ExternalOneByteStringResource* resource) { + return maybe_local_to_ptr(v8::String::NewExternalOneByte(isolate, resource)); +} + +const v8::String* v8__String__NewExternalOneByteStatic(v8::Isolate* isolate, + const char* data, + int length) { + return maybe_local_to_ptr(v8::String::NewExternalOneByte( + isolate, new ExternalStaticOneByteStringResource(data, length))); +} + +const v8::String* v8__String__NewExternalOneByte( + v8::Isolate* isolate, char* data, size_t length, + ExternalOneByteString::RustDestroy rustDestroy) { + return maybe_local_to_ptr(v8::String::NewExternalOneByte( + isolate, new ExternalOneByteString(data, length, rustDestroy, isolate))); +} + +const char* v8__ExternalOneByteStringResource__data( + v8::String::ExternalOneByteStringResource* self) { + return self->data(); +} + +size_t v8__ExternalOneByteStringResource__length( + v8::String::ExternalOneByteStringResource* self) { + return self->length(); +} + +class ExternalTwoByteString : public v8::String::ExternalStringResource { + public: + using RustDestroy = void (*)(uint16_t*, size_t); + ExternalTwoByteString(uint16_t* data, size_t length, RustDestroy rustDestroy, + v8::Isolate* isolate) + : data_(data), + length_(length), + rustDestroy_(rustDestroy), + isolate_(isolate) { + isolate_->AdjustAmountOfExternalAllocatedMemory( + static_cast(length_ * 2)); + } + ~ExternalTwoByteString() override { + (*rustDestroy_)(data_, length_); + isolate_->AdjustAmountOfExternalAllocatedMemory( + -static_cast(length_ * 2)); + } + + const uint16_t* data() const override { return data_; } + + size_t length() const override { return length_; } + + private: + uint16_t* data_; + const size_t length_; + RustDestroy rustDestroy_; + v8::Isolate* isolate_; +}; + +class ExternalStaticStringResource : public v8::String::ExternalStringResource { + public: + ExternalStaticStringResource(const uint16_t* data, int length) + : _data(data), _length(length) {} + const uint16_t* data() const override { return _data; } + size_t length() const override { return _length; } + + private: + const uint16_t* _data; + const int _length; +}; + +const v8::String* v8__String__NewExternalTwoByteStatic(v8::Isolate* isolate, + const uint16_t* data, + int length) { + return maybe_local_to_ptr(v8::String::NewExternalTwoByte( + isolate, new ExternalStaticStringResource(data, length))); +} + +const v8::String* v8__String__NewExternalTwoByte( + v8::Isolate* isolate, uint16_t* data, size_t length, + ExternalTwoByteString::RustDestroy rustDestroy) { + return maybe_local_to_ptr(v8::String::NewExternalTwoByte( + isolate, new ExternalTwoByteString(data, length, rustDestroy, isolate))); +} + +bool v8__String__IsExternal(const v8::String& self) { + return self.IsExternal(); +} +bool v8__String__IsExternalOneByte(const v8::String& self) { + return self.IsExternalOneByte(); +} +bool v8__String__IsExternalTwoByte(const v8::String& self) { + return self.IsExternalTwoByte(); +} +bool v8__String__IsOneByte(const v8::String& self) { return self.IsOneByte(); } +bool v8__String__ContainsOnlyOneByte(const v8::String& self) { + return self.ContainsOnlyOneByte(); +} + +bool v8__String__StringEquals(const v8::String& self, const v8::String* that) { + return self.StringEquals(ptr_to_local(that)); +} + +const v8::String* v8__String__InternalizeString(v8::String& self, + v8::Isolate* isolate) { + return local_to_ptr(ptr_to_local(&self)->InternalizeString(isolate)); +} + +void v8__String__ValueView__CONSTRUCT(uninit_t* buf, + v8::Isolate* isolate, + const v8::String& string) { + construct_in_place(buf, isolate, + ptr_to_local(&string)); +} + +void v8__String__ValueView__DESTRUCT(v8::String::ValueView* self) { + self->~ValueView(); +} + +const v8::Symbol* v8__Symbol__New(v8::Isolate* isolate, + const v8::String* description) { + return local_to_ptr(v8::Symbol::New(isolate, ptr_to_local(description))); +} + +const v8::Symbol* v8__Symbol__For(v8::Isolate* isolate, + const v8::String& description) { + return local_to_ptr(v8::Symbol::For(isolate, ptr_to_local(&description))); +} + +const v8::Symbol* v8__Symbol__ForApi(v8::Isolate* isolate, + const v8::String& description) { + return local_to_ptr(v8::Symbol::ForApi(isolate, ptr_to_local(&description))); +} + +#define V(NAME) \ + const v8::Symbol* v8__Symbol__Get##NAME(v8::Isolate* isolate) { \ + return local_to_ptr(v8::Symbol::Get##NAME(isolate)); \ + } + +V(AsyncIterator) +V(HasInstance) +V(IsConcatSpreadable) +V(Iterator) +V(Match) +V(Replace) +V(Search) +V(Split) +V(ToPrimitive) +V(ToStringTag) +V(Unscopables) +V(Dispose) +V(AsyncDispose) +#undef V + +const v8::Value* v8__Symbol__Description(const v8::Symbol& self, + v8::Isolate* isolate) { + return local_to_ptr(ptr_to_local(&self)->Description(isolate)); +} + +const v8::Private* v8__Private__New(v8::Isolate* isolate, + const v8::String* name) { + return local_to_ptr(v8::Private::New(isolate, ptr_to_local(name))); +} + +const v8::Private* v8__Private__ForApi(v8::Isolate* isolate, + const v8::String* name) { + return local_to_ptr(v8::Private::ForApi(isolate, ptr_to_local(name))); +} + +const v8::Value* v8__Private__Name(const v8::Private& self) { + return local_to_ptr(ptr_to_local(&self)->Name()); +} + +void v8__Template__Set(const v8::Template& self, const v8::Name& key, + const v8::Data& value, v8::PropertyAttribute attr) { + ptr_to_local(&self)->Set(ptr_to_local(&key), ptr_to_local(&value), attr); +} + +void v8__Template__SetIntrinsicDataProperty(const v8::Template& self, + const v8::Name& key, + v8::Intrinsic intrinsic, + v8::PropertyAttribute attr) { + ptr_to_local(&self)->SetIntrinsicDataProperty(ptr_to_local(&key), intrinsic, + attr); +} + +// Rust passes accessor setters with the old PropertyCallbackInfo +// signature. V8 calls every setter through AccessorNameSetterCallbackV2 and +// treats an unset return value as success, so this cast matches what V8 +// does internally (see PropertyCallbackArguments::CallAccessorSetter). +using RustAccessorNameSetterCallback = + void (*)(v8::Local property, v8::Local value, + const v8::PropertyCallbackInfo& info); + +static v8::AccessorNameSetterCallbackV2 to_setter_v2( + RustAccessorNameSetterCallback setter) { + return reinterpret_cast(setter); +} + +void v8__Template__SetNativeDataProperty(const v8::Template& self, + const v8::Name& key, + v8::AccessorNameGetterCallback getter, + RustAccessorNameSetterCallback setter, + const v8::Value* data_or_null, + v8::PropertyAttribute attr) { + ptr_to_local(&self)->SetNativeDataProperty(ptr_to_local(&key), getter, + to_setter_v2(setter), + ptr_to_local(data_or_null), attr); +} + +void v8__Template__SetLazyDataProperty(const v8::Template& self, + const v8::Name& key, + v8::AccessorNameGetterCallback getter, + const v8::Value* data_or_null, + v8::PropertyAttribute attr) { + ptr_to_local(&self)->SetLazyDataProperty(ptr_to_local(&key), getter, + ptr_to_local(data_or_null), attr); +} + +const v8::ObjectTemplate* v8__ObjectTemplate__New( + v8::Isolate* isolate, const v8::FunctionTemplate& templ) { + return local_to_ptr(v8::ObjectTemplate::New(isolate, ptr_to_local(&templ))); +} + +const v8::Object* v8__ObjectTemplate__NewInstance( + const v8::ObjectTemplate& self, const v8::Context& context) { + return maybe_local_to_ptr( + ptr_to_local(&self)->NewInstance(ptr_to_local(&context))); +} + +int v8__ObjectTemplate__InternalFieldCount(const v8::ObjectTemplate& self) { + return ptr_to_local(&self)->InternalFieldCount(); +} + +void v8__ObjectTemplate__SetInternalFieldCount(const v8::ObjectTemplate& self, + int value) { + ptr_to_local(&self)->SetInternalFieldCount(value); +} + +void v8__ObjectTemplate__SetNativeDataProperty( + const v8::ObjectTemplate& self, const v8::Name& key, + v8::AccessorNameGetterCallback getter, + RustAccessorNameSetterCallback setter, const v8::Value* data_or_null, + v8::PropertyAttribute attr) { + ptr_to_local(&self)->SetNativeDataProperty(ptr_to_local(&key), getter, + to_setter_v2(setter), + ptr_to_local(data_or_null), attr); +} + +void v8__ObjectTemplate__SetNamedPropertyHandler( + const v8::ObjectTemplate& self, v8::NamedPropertyGetterCallback getter, + v8::NamedPropertySetterCallbackV2 setter, + v8::NamedPropertyQueryCallback query, + v8::NamedPropertyDeleterCallback deleter, + v8::NamedPropertyEnumeratorCallback enumerator, + v8::NamedPropertyDefinerCallbackV2 definer, + v8::NamedPropertyDescriptorCallback descriptor, + const v8::Value* data_or_null, v8::PropertyHandlerFlags flags) { + ptr_to_local(&self)->SetHandler(v8::NamedPropertyHandlerConfiguration( + getter, setter, query, deleter, enumerator, definer, descriptor, + ptr_to_local(data_or_null), flags)); +} + +void v8__ObjectTemplate__SetIndexedPropertyHandler( + const v8::ObjectTemplate& self, v8::IndexedPropertyGetterCallbackV2 getter, + v8::IndexedPropertySetterCallback setter, + v8::IndexedPropertyQueryCallbackV2 query, + v8::IndexedPropertyDeleterCallbackV2 deleter, + v8::IndexedPropertyEnumeratorCallback enumerator, + v8::IndexedPropertyDefinerCallback definer, + v8::IndexedPropertyDescriptorCallbackV2 descriptor, + const v8::Value* data_or_null, v8::PropertyHandlerFlags flags) { + ptr_to_local(&self)->SetHandler(v8::IndexedPropertyHandlerConfiguration( + getter, setter, query, deleter, enumerator, definer, descriptor, + ptr_to_local(data_or_null), flags)); +} + +void v8__ObjectTemplate__SetAccessorProperty(const v8::ObjectTemplate& self, + const v8::Name& key, + v8::FunctionTemplate& getter, + v8::FunctionTemplate& setter, + v8::PropertyAttribute attr) { + ptr_to_local(&self)->SetAccessorProperty( + ptr_to_local(&key), ptr_to_local(&getter), ptr_to_local(&setter), attr); +} + +void v8__ObjectTemplate__SetImmutableProto(const v8::ObjectTemplate& self) { + return ptr_to_local(&self)->SetImmutableProto(); +} + +void v8__ObjectTemplate__SetCallAsFunctionHandler( + const v8::ObjectTemplate& self, v8::FunctionCallback callback, + const v8::Value* data_or_null) { + ptr_to_local(&self)->SetCallAsFunctionHandler(callback, + ptr_to_local(data_or_null)); +} + +const v8::Object* v8__Object__New(v8::Isolate* isolate) { + return local_to_ptr(v8::Object::New(isolate)); +} + +const v8::Object* v8__Object__New__with_prototype_and_properties( + v8::Isolate* isolate, const v8::Value& prototype_or_null, + const v8::Name* const names[], const v8::Value* const values[], + size_t length) { + return local_to_ptr(v8::Object::New(isolate, ptr_to_local(&prototype_or_null), + const_ptr_array_to_local_array(names), + const_ptr_array_to_local_array(values), + length)); +} + +const v8::Value* v8__Object__Get(const v8::Object& self, + const v8::Context& context, + const v8::Value& key) { + return maybe_local_to_ptr( + ptr_to_local(&self)->Get(ptr_to_local(&context), ptr_to_local(&key))); +} + +const v8::Value* v8__Object__GetWithReceiver(const v8::Object& self, + const v8::Context& context, + const v8::Value& key, + const v8::Object& receiver) { + return maybe_local_to_ptr(ptr_to_local(&self)->Get( + ptr_to_local(&context), ptr_to_local(&key), ptr_to_local(&receiver))); +} + +const v8::Value* v8__Object__GetIndex(const v8::Object& self, + const v8::Context& context, + uint32_t index) { + return maybe_local_to_ptr( + ptr_to_local(&self)->Get(ptr_to_local(&context), index)); +} + +void* v8__Object__GetAlignedPointerFromInternalField( + const v8::Object& self, int index, v8::EmbedderDataTypeTag tag) { + return ptr_to_local(&self)->GetAlignedPointerFromInternalField(index, tag); +} + +void v8__Object__SetAlignedPointerInInternalField(const v8::Object& self, + int index, void* value, + v8::EmbedderDataTypeTag tag) { + ptr_to_local(&self)->SetAlignedPointerInInternalField(index, value, tag); +} + +bool v8__Object__IsApiWrapper(const v8::Object& self) { + return ptr_to_local(&self)->IsApiWrapper(); +} + +const v8::Value* v8__Object__GetPrototype(const v8::Object& self) { + return local_to_ptr(ptr_to_local(&self)->GetPrototype()); +} + +const v8::Object* v8__Object__FindInstanceInPrototypeChain( + const v8::Object& self, const v8::FunctionTemplate& tmpl) { + return local_to_ptr( + ptr_to_local(&self)->FindInstanceInPrototypeChain(ptr_to_local(&tmpl))); +} + +MaybeBool v8__Object__Set(const v8::Object& self, const v8::Context& context, + const v8::Value& key, const v8::Value& value) { + return maybe_to_maybe_bool(ptr_to_local(&self)->Set( + ptr_to_local(&context), ptr_to_local(&key), ptr_to_local(&value))); +} + +MaybeBool v8__Object__SetWithReceiver(const v8::Object& self, + const v8::Context& context, + const v8::Value& key, + const v8::Value& value, + const v8::Object& receiver) { + return maybe_to_maybe_bool( + ptr_to_local(&self)->Set(ptr_to_local(&context), ptr_to_local(&key), + ptr_to_local(&value), ptr_to_local(&receiver))); +} + +MaybeBool v8__Object__SetIndex(const v8::Object& self, + const v8::Context& context, uint32_t index, + const v8::Value& value) { + return maybe_to_maybe_bool(ptr_to_local(&self)->Set( + ptr_to_local(&context), index, ptr_to_local(&value))); +} + +MaybeBool v8__Object__SetPrototype(const v8::Object& self, + const v8::Context& context, + const v8::Value& prototype) { + return maybe_to_maybe_bool(ptr_to_local(&self)->SetPrototype( + ptr_to_local(&context), ptr_to_local(&prototype))); +} + +const v8::String* v8__Object__GetConstructorName(v8::Object& self) { + return local_to_ptr(self.GetConstructorName()); +} + +MaybeBool v8__Object__CreateDataProperty(const v8::Object& self, + const v8::Context& context, + const v8::Name& key, + const v8::Value& value) { + return maybe_to_maybe_bool(ptr_to_local(&self)->CreateDataProperty( + ptr_to_local(&context), ptr_to_local(&key), ptr_to_local(&value))); +} + +MaybeBool v8__Object__DefineOwnProperty(const v8::Object& self, + const v8::Context& context, + const v8::Name& key, + const v8::Value& value, + v8::PropertyAttribute attr) { + return maybe_to_maybe_bool(ptr_to_local(&self)->DefineOwnProperty( + ptr_to_local(&context), ptr_to_local(&key), ptr_to_local(&value), attr)); +} + +MaybeBool v8__Object__DefineProperty(const v8::Object& self, + const v8::Context& context, + const v8::Name& key, + v8::PropertyDescriptor& desc) { + return maybe_to_maybe_bool(ptr_to_local(&self)->DefineProperty( + ptr_to_local(&context), ptr_to_local(&key), desc)); +} + +MaybeBool v8__Object__SetAccessor(const v8::Object& self, + const v8::Context& context, + const v8::Name& key, + v8::AccessorNameGetterCallback getter, + RustAccessorNameSetterCallback setter, + const v8::Value* data_or_null, + v8::PropertyAttribute attr) { + return maybe_to_maybe_bool(ptr_to_local(&self)->SetNativeDataProperty( + ptr_to_local(&context), ptr_to_local(&key), getter, to_setter_v2(setter), + ptr_to_local(data_or_null), attr)); +} + +MaybeBool v8__Object__SetLazyDataProperty( + const v8::Object& self, const v8::Context& context, const v8::Name& key, + v8::AccessorNameGetterCallback getter, const v8::Value* data_or_null, + v8::PropertyAttribute attr, v8::SideEffectType getter_side_effect_type, + v8::SideEffectType setter_side_effect_type) { + return maybe_to_maybe_bool(ptr_to_local(&self)->SetLazyDataProperty( + ptr_to_local(&context), ptr_to_local(&key), getter, + ptr_to_local(data_or_null), attr, getter_side_effect_type, + setter_side_effect_type)); +} + +int v8__Object__GetIdentityHash(const v8::Object& self) { + return ptr_to_local(&self)->GetIdentityHash(); +} + +const v8::Context* v8__Object__GetCreationContext(const v8::Object& self) { + return maybe_local_to_ptr(ptr_to_local(&self)->GetCreationContext()); +} + +// v8::PropertyFilter +static_assert(v8::ALL_PROPERTIES == 0, "v8::ALL_PROPERTIES is not 0"); +static_assert(v8::ONLY_WRITABLE == 1, "v8::ONLY_WRITABLE is not 1"); +static_assert(v8::ONLY_ENUMERABLE == 2, "v8::ONLY_ENUMERABLE is not 2"); +static_assert(v8::ONLY_CONFIGURABLE == 4, "v8::ONLY_CONFIGURABLE is not 4"); +static_assert(v8::SKIP_STRINGS == 8, "v8::SKIP_STRINGS is not 8"); +static_assert(v8::SKIP_SYMBOLS == 16, "v8::SKIP_SYMBOLS is not 16"); + +// v8::KeyConversionMode +static_assert(static_cast(v8::KeyConversionMode::kConvertToString) == 0, + "v8::KeyConversionMode::kConvertToString is not 0"); +static_assert(static_cast(v8::KeyConversionMode::kKeepNumbers) == 1, + "v8::KeyConversionMode::kKeepNumbers is not 1"); +static_assert(static_cast(v8::KeyConversionMode::kNoNumbers) == 2, + "v8::KeyConversionMode::kNoNumbers is not 2"); + +// v8::KeyCollectionMode +static_assert(static_cast(v8::KeyCollectionMode::kOwnOnly) == 0, + "v8::KeyCollectionMode::kOwnOnly is not 0"); +static_assert(static_cast(v8::KeyCollectionMode::kIncludePrototypes) == 1, + "v8::KeyCollectionMode::kIncludePrototypes is not 1"); + +// v8::IndexFilter +static_assert(static_cast(v8::IndexFilter::kIncludeIndices) == 0, + "v8::IndexFilter::kIncludeIndices is not 0"); +static_assert(static_cast(v8::IndexFilter::kSkipIndices) == 1, + "v8::IndexFilter::kSkipIndices is not 1"); + +const v8::Array* v8__Object__GetOwnPropertyNames( + const v8::Object* self, const v8::Context* context, + v8::PropertyFilter filter, v8::KeyConversionMode key_conversion) { + return maybe_local_to_ptr(ptr_to_local(self)->GetOwnPropertyNames( + ptr_to_local(context), filter, key_conversion)); +} + +const v8::Array* v8__Object__GetPropertyNames( + const v8::Object* self, const v8::Context* context, + v8::KeyCollectionMode mode, v8::PropertyFilter property_filter, + v8::IndexFilter index_filter, v8::KeyConversionMode key_conversion) { + return maybe_local_to_ptr(ptr_to_local(self)->GetPropertyNames( + ptr_to_local(context), mode, property_filter, index_filter, + key_conversion)); +} + +MaybeBool v8__Object__Has(const v8::Object& self, const v8::Context& context, + const v8::Value& key) { + return maybe_to_maybe_bool( + ptr_to_local(&self)->Has(ptr_to_local(&context), ptr_to_local(&key))); +} + +MaybeBool v8__Object__HasIndex(const v8::Object& self, + const v8::Context& context, uint32_t index) { + return maybe_to_maybe_bool( + ptr_to_local(&self)->Has(ptr_to_local(&context), index)); +} + +MaybeBool v8__Object__HasOwnProperty(const v8::Object& self, + const v8::Context& context, + const v8::Name& key) { + return maybe_to_maybe_bool(ptr_to_local(&self)->HasOwnProperty( + ptr_to_local(&context), ptr_to_local(&key))); +} + +MaybeBool v8__Object__Delete(const v8::Object& self, const v8::Context& context, + const v8::Value& key) { + return maybe_to_maybe_bool( + ptr_to_local(&self)->Delete(ptr_to_local(&context), ptr_to_local(&key))); +} + +MaybeBool v8__Object__DeleteIndex(const v8::Object& self, + const v8::Context& context, uint32_t index) { + return maybe_to_maybe_bool( + ptr_to_local(&self)->Delete(ptr_to_local(&context), index)); +} + +int v8__Object__InternalFieldCount(const v8::Object& self) { + return ptr_to_local(&self)->InternalFieldCount(); +} + +const v8::Data* v8__Object__GetInternalField(const v8::Object& self, + int index) { + return local_to_ptr(ptr_to_local(&self)->GetInternalField(index)); +} + +static_assert(static_cast(v8::IntegrityLevel::kFrozen) == 0, + "v8::IntegrityLevel::kFrozen is not 0"); +static_assert(static_cast(v8::IntegrityLevel::kSealed) == 1, + "v8::IntegrityLevel::kSealed is not 1"); + +MaybeBool v8__Object__SetIntegrityLevel(const v8::Object& self, + const v8::Context& context, + v8::IntegrityLevel level) { + return maybe_to_maybe_bool( + ptr_to_local(&self)->SetIntegrityLevel(ptr_to_local(&context), level)); +} + +void v8__Object__SetInternalField(const v8::Object& self, int index, + const v8::Data& data) { + ptr_to_local(&self)->SetInternalField(index, ptr_to_local(&data)); +} + +const v8::Value* v8__Object__GetPrivate(const v8::Object& self, + const v8::Context& context, + const v8::Private& key) { + return maybe_local_to_ptr(ptr_to_local(&self)->GetPrivate( + ptr_to_local(&context), ptr_to_local(&key))); +} + +MaybeBool v8__Object__SetPrivate(const v8::Object& self, + const v8::Context& context, + const v8::Private& key, + const v8::Value& value) { + return maybe_to_maybe_bool(ptr_to_local(&self)->SetPrivate( + ptr_to_local(&context), ptr_to_local(&key), ptr_to_local(&value))); +} + +MaybeBool v8__Object__DeletePrivate(const v8::Object& self, + const v8::Context& context, + const v8::Private& key) { + return maybe_to_maybe_bool(ptr_to_local(&self)->DeletePrivate( + ptr_to_local(&context), ptr_to_local(&key))); +} + +MaybeBool v8__Object__HasPrivate(const v8::Object& self, + const v8::Context& context, + const v8::Private& key) { + return maybe_to_maybe_bool(ptr_to_local(&self)->HasPrivate( + ptr_to_local(&context), ptr_to_local(&key))); +} + +void v8__Object__GetPropertyAttributes(const v8::Object& self, + const v8::Context& context, + const v8::Value& key, + v8::Maybe* out) { + *out = ptr_to_local(&self)->GetPropertyAttributes(ptr_to_local(&context), + ptr_to_local(&key)); +} + +const v8::Value* v8__Object__GetOwnPropertyDescriptor( + const v8::Object& self, const v8::Context& context, const v8::Name& key) { + return maybe_local_to_ptr(ptr_to_local(&self)->GetOwnPropertyDescriptor( + ptr_to_local(&context), ptr_to_local(&key))); +} + +const v8::Value* v8__Object__GetRealNamedProperty(const v8::Object& self, + const v8::Context& context, + const v8::Name& key) { + return maybe_local_to_ptr(ptr_to_local(&self)->GetRealNamedProperty( + ptr_to_local(&context), ptr_to_local(&key))); +} + +MaybeBool v8__Object__HasRealNamedProperty(const v8::Object& self, + const v8::Context& context, + const v8::Name& key) { + return maybe_to_maybe_bool(ptr_to_local(&self)->HasRealNamedProperty( + ptr_to_local(&context), ptr_to_local(&key))); +} + +void v8__Object__GetRealNamedPropertyAttributes( + const v8::Object& self, const v8::Context& context, const v8::Name& key, + v8::Maybe* out) { + *out = ptr_to_local(&self)->GetRealNamedPropertyAttributes( + ptr_to_local(&context), ptr_to_local(&key)); +} + +const v8::Array* v8__Object__PreviewEntries(const v8::Object& self, + bool* is_key_value) { + return maybe_local_to_ptr(ptr_to_local(&self)->PreviewEntries(is_key_value)); +} + +bool v8__Object__IsCallable(const v8::Object& self) { + return ptr_to_local(&self)->IsCallable(); +} + +bool v8__Object__IsConstructor(const v8::Object& self) { + return ptr_to_local(&self)->IsConstructor(); +} + +const v8::Value* v8__Object__CallAsFunction(const v8::Object& self, + const v8::Context& context, + const v8::Value& recv, int argc, + const v8::Value* const argv[]) { + return maybe_local_to_ptr(ptr_to_local(&self)->CallAsFunction( + ptr_to_local(&context), ptr_to_local(&recv), argc, + const_ptr_array_to_local_array(argv))); +} + +const v8::Value* v8__Object__CallAsConstructor(const v8::Object& self, + const v8::Context& context, + int argc, + const v8::Value* const argv[]) { + return maybe_local_to_ptr(ptr_to_local(&self)->CallAsConstructor( + ptr_to_local(&context), argc, const_ptr_array_to_local_array(argv))); +} + +const v8::Array* v8__Array__New(v8::Isolate* isolate, int length) { + return local_to_ptr(v8::Array::New(isolate, length)); +} + +const v8::Array* v8__Array__New_with_elements(v8::Isolate* isolate, + const v8::Value* const elements[], + size_t length) { + return local_to_ptr(v8::Array::New( + isolate, const_ptr_array_to_local_array(elements), length)); +} + +uint32_t v8__Array__Length(const v8::Array& self) { return self.Length(); } + +// The Rust `ArrayIterationResult` enum is declared `#[repr(i32)]` with its +// variants in this order, and is returned directly from the Rust callback, so +// both the discriminants and the width must keep matching V8's. A scoped enum +// with no fixed underlying type is `int` ([dcl.enum]/5); if V8 ever gives +// CallbackResult an explicit underlying type, the size assert catches it even +// though the discriminants would be unchanged. +static_assert(static_cast(v8::Array::CallbackResult::kException) == 0 && + static_cast(v8::Array::CallbackResult::kBreak) == 1 && + static_cast(v8::Array::CallbackResult::kContinue) == 2, + "Array::CallbackResult discriminant mismatch"); +static_assert(sizeof(v8::Array::CallbackResult) == sizeof(int), + "Array::CallbackResult size mismatch"); + +// Trampoline that adapts V8's `Local`-taking iteration callback to a +// plain pointer-based callback that can be implemented in Rust. +namespace { +struct ArrayIterateCallbackData { + v8::Array::CallbackResult (*callback)(uint32_t index, + const v8::Value* element, void* data); + void* data; +}; + +v8::Array::CallbackResult array_iterate_trampoline(uint32_t index, + v8::Local element, + void* data) { + auto* wrapped = static_cast(data); + return wrapped->callback(index, local_to_ptr(element), wrapped->data); +} +} // namespace + +// Returns false if iteration was terminated by an exception, true otherwise. +bool v8__Array__Iterate( + const v8::Array& self, const v8::Context& context, + v8::Array::CallbackResult (*callback)(uint32_t index, + const v8::Value* element, void* data), + void* callback_data) { + ArrayIterateCallbackData wrapped{callback, callback_data}; + return ptr_to_local(&self) + ->Iterate(ptr_to_local(&context), array_iterate_trampoline, &wrapped) + .IsJust(); +} + +const v8::Date* v8__Date__New(const v8::Context& context, double time) { + // v8::Date::New() is kind of weird in that it returns a v8::Value, + // not a v8::Date, even though the object is always a Date object. + // Let's paper over that quirk here. + v8::MaybeLocal maybe_date; + + v8::Local value; + if (v8::Date::New(ptr_to_local(&context), time).ToLocal(&value)) { + assert(value->IsDate()); + maybe_date = value.As(); + } + + return maybe_local_to_ptr(maybe_date); +} + +const v8::Date* v8__Date__Parse(const v8::Context& context, + const v8::String& date_string) { + // Like v8::Date::New(), v8::Date::Parse() returns a v8::Value rather than + // a v8::Date, even though the resulting object is always a Date object. + v8::MaybeLocal maybe_date; + + v8::Local value; + if (v8::Date::Parse(ptr_to_local(&context), ptr_to_local(&date_string)) + .ToLocal(&value)) { + assert(value->IsDate()); + maybe_date = value.As(); + } + + return maybe_local_to_ptr(maybe_date); +} + +double v8__Date__ValueOf(const v8::Date& self) { return self.ValueOf(); } + +const v8::String* v8__Date__ToISOString(const v8::Date& self) { + return local_to_ptr(self.ToISOString()); +} + +const v8::String* v8__Date__ToUTCString(const v8::Date& self) { + return local_to_ptr(self.ToUTCString()); +} + +const v8::External* v8__External__New(v8::Isolate* isolate, void* value) { + return local_to_ptr( + v8::External::New(isolate, value, v8::kExternalPointerTypeTagDefault)); +} + +void* v8__External__Value(const v8::External& self) { + return self.Value(v8::kExternalPointerTypeTagDefault); +} + +const v8::Map* v8__Map__New(v8::Isolate* isolate) { + return local_to_ptr(v8::Map::New(isolate)); +} + +size_t v8__Map__Size(const v8::Map& self) { return self.Size(); } + +void v8__Map__Clear(const v8::Map& self) { + return ptr_to_local(&self)->Clear(); +} + +const v8::Value* v8__Map__Get(const v8::Map& self, const v8::Context& context, + const v8::Value& key) { + return maybe_local_to_ptr( + ptr_to_local(&self)->Get(ptr_to_local(&context), ptr_to_local(&key))); +} + +v8::Map* v8__Map__Set(const v8::Map& self, const v8::Context& context, + const v8::Value& key, const v8::Value& value) { + return maybe_local_to_ptr(ptr_to_local(&self)->Set( + ptr_to_local(&context), ptr_to_local(&key), ptr_to_local(&value))); +} + +MaybeBool v8__Map__Has(const v8::Map& self, const v8::Context& context, + const v8::Value& key) { + return maybe_to_maybe_bool( + ptr_to_local(&self)->Has(ptr_to_local(&context), ptr_to_local(&key))); +} + +MaybeBool v8__Map__Delete(const v8::Map& self, const v8::Context& context, + const v8::Value& key) { + return maybe_to_maybe_bool( + ptr_to_local(&self)->Delete(ptr_to_local(&context), ptr_to_local(&key))); +} + +const v8::Array* v8__Map__As__Array(const v8::Map& self) { + return local_to_ptr(self.AsArray()); +} + +const v8::Set* v8__Set__New(v8::Isolate* isolate) { + return local_to_ptr(v8::Set::New(isolate)); +} + +size_t v8__Set__Size(const v8::Set& self) { return self.Size(); } + +void v8__Set__Clear(const v8::Set& self) { + return ptr_to_local(&self)->Clear(); +} + +v8::Set* v8__Set__Add(const v8::Set& self, const v8::Context& context, + const v8::Value& key) { + return maybe_local_to_ptr( + ptr_to_local(&self)->Add(ptr_to_local(&context), ptr_to_local(&key))); +} + +MaybeBool v8__Set__Has(const v8::Set& self, const v8::Context& context, + const v8::Value& key) { + return maybe_to_maybe_bool( + ptr_to_local(&self)->Has(ptr_to_local(&context), ptr_to_local(&key))); +} + +MaybeBool v8__Set__Delete(const v8::Set& self, const v8::Context& context, + const v8::Value& key) { + return maybe_to_maybe_bool( + ptr_to_local(&self)->Delete(ptr_to_local(&context), ptr_to_local(&key))); +} + +const v8::Array* v8__Set__As__Array(const v8::Set& self) { + return local_to_ptr(self.AsArray()); +} + +const v8::Number* v8__Number__New(v8::Isolate* isolate, double value) { + return *v8::Number::New(isolate, value); +} + +double v8__Number__Value(const v8::Number& self) { return self.Value(); } + +const v8::Integer* v8__Integer__New(v8::Isolate* isolate, int32_t value) { + return *v8::Integer::New(isolate, value); +} + +const v8::Integer* v8__Integer__NewFromUnsigned(v8::Isolate* isolate, + uint32_t value) { + return *v8::Integer::NewFromUnsigned(isolate, value); +} + +int64_t v8__Integer__Value(const v8::Integer& self) { return self.Value(); } + +uint32_t v8__Uint32__Value(const v8::Uint32& self) { return self.Value(); } + +int32_t v8__Int32__Value(const v8::Int32& self) { return self.Value(); } + +const v8::BigInt* v8__BigInt__New(v8::Isolate* isolate, int64_t value) { + return local_to_ptr(v8::BigInt::New(isolate, value)); +} + +const v8::BigInt* v8__BigInt__NewFromUnsigned(v8::Isolate* isolate, + uint64_t value) { + return local_to_ptr(v8::BigInt::NewFromUnsigned(isolate, value)); +} + +const v8::BigInt* v8__BigInt__NewFromWords(const v8::Context& context, + int sign_bit, int word_count, + const uint64_t words[]) { + return maybe_local_to_ptr(v8::BigInt::NewFromWords( + ptr_to_local(&context), sign_bit, word_count, words)); +} + +uint64_t v8__BigInt__Uint64Value(const v8::BigInt& self, bool* lossless) { + return ptr_to_local(&self)->Uint64Value(lossless); +} + +int64_t v8__BigInt__Int64Value(const v8::BigInt& self, bool* lossless) { + return ptr_to_local(&self)->Int64Value(lossless); +} + +int v8__BigInt__WordCount(const v8::BigInt& self) { + return ptr_to_local(&self)->WordCount(); +} + +void v8__BigInt__ToWordsArray(const v8::BigInt& self, int* sign_bit, + int* word_count, uint64_t words[]) { + ptr_to_local(&self)->ToWordsArray(sign_bit, word_count, words); +} + +const v8::ArrayBuffer* v8__ArrayBufferView__Buffer( + const v8::ArrayBufferView& self) { + return local_to_ptr(ptr_to_local(&self)->Buffer()); +} + +const void* v8__ArrayBufferView__Buffer__Data(const v8::ArrayBufferView& self) { + return ptr_to_local(&self)->Buffer()->Data(); +} + +size_t v8__ArrayBufferView__ByteLength(const v8::ArrayBufferView& self) { + return ptr_to_local(&self)->ByteLength(); +} + +size_t v8__ArrayBufferView__ByteOffset(const v8::ArrayBufferView& self) { + return ptr_to_local(&self)->ByteOffset(); +} + +bool v8__ArrayBufferView__HasBuffer(const v8::ArrayBufferView& self) { + return ptr_to_local(&self)->HasBuffer(); +} + +size_t v8__ArrayBufferView__CopyContents(const v8::ArrayBufferView& self, + void* dest, int byte_length) { + return ptr_to_local(&self)->CopyContents(dest, byte_length); +} + +memory_span_t v8__ArrayBufferView__GetContents(const v8::ArrayBufferView& self, + memory_span_t rstorage) { + v8::MemorySpan storage{rstorage.data, rstorage.size}; + v8::MemorySpan span = ptr_to_local(&self)->GetContents(storage); + return {span.data(), span.size()}; +} + +struct RustAllocatorVtable { + void* (*allocate)(void* handle, size_t length); + void* (*allocate_uninitialized)(void* handle, size_t length); + void (*free)(void* handle, void* data, size_t length); + void (*drop)(void* handle); +}; + +class RustAllocator : public v8::ArrayBuffer::Allocator { + private: + void* handle; + const RustAllocatorVtable* vtable; + + public: + RustAllocator(void* handle, const RustAllocatorVtable* vtable) { + this->handle = handle; + this->vtable = vtable; + } + + RustAllocator(const RustAllocator& that) = delete; + RustAllocator(RustAllocator&& that) = delete; + void operator=(const RustAllocator& that) = delete; + void operator=(RustAllocator&& that) = delete; + + virtual ~RustAllocator() { vtable->drop(handle); } + + void* Allocate(size_t length) final { + return vtable->allocate(handle, length); + } + + void* AllocateUninitialized(size_t length) final { + return vtable->allocate_uninitialized(handle, length); + } + + void Free(void* data, size_t length) final { + vtable->free(handle, data, length); + } +}; + +v8::ArrayBuffer::Allocator* v8__ArrayBuffer__Allocator__NewDefaultAllocator() { + return v8::ArrayBuffer::Allocator::NewDefaultAllocator(); +} + +// Returns nullptr unless V8 was built with pointer compression in multi-cage +// mode, which is the only configuration in which V8 declares the group-scoped +// overload. +v8::ArrayBuffer::Allocator* +v8__ArrayBuffer__Allocator__NewDefaultAllocator__with_group( + const v8::IsolateGroup& group) { +#if defined(V8_COMPRESS_POINTERS) && \ + !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE) + return v8::ArrayBuffer::Allocator::NewDefaultAllocator(group); +#else + (void)group; + return nullptr; +#endif +} + +v8::ArrayBuffer::Allocator* v8__ArrayBuffer__Allocator__NewRustAllocator( + void* handle, const RustAllocatorVtable* vtable) { + return new RustAllocator(handle, vtable); +} + +void v8__ArrayBuffer__Allocator__DELETE(v8::ArrayBuffer::Allocator* self) { + delete self; +} + +const v8::ArrayBuffer* v8__ArrayBuffer__New__with_byte_length( + v8::Isolate* isolate, size_t byte_length) { + return local_to_ptr(v8::ArrayBuffer::New(isolate, byte_length)); +} + +const v8::ArrayBuffer* v8__ArrayBuffer__MaybeNew( + v8::Isolate* isolate, size_t byte_length, + v8::BackingStoreInitializationMode initialization_mode) { + return maybe_local_to_ptr( + v8::ArrayBuffer::MaybeNew(isolate, byte_length, initialization_mode)); +} + +const v8::ArrayBuffer* v8__ArrayBuffer__New__with_backing_store( + v8::Isolate* isolate, + const std::shared_ptr& backing_store) { + return local_to_ptr(v8::ArrayBuffer::New(isolate, backing_store)); +} + +size_t v8__ArrayBuffer__ByteLength(const v8::ArrayBuffer& self) { + return self.ByteLength(); +} + +const v8::DataView* v8__DataView__New(const v8::ArrayBuffer& ab, size_t offset, + size_t length) { + return local_to_ptr(v8::DataView::New(ptr_to_local(&ab), offset, length)); +} + +const v8::DataView* v8__DataView__New__with_shared_buffer( + const v8::SharedArrayBuffer& sab, size_t offset, size_t length) { + return local_to_ptr(v8::DataView::New(ptr_to_local(&sab), offset, length)); +} + +struct InternalFieldData { + uint32_t data; +}; + +std::vector deserialized_data; + +void DeserializeInternalFields(v8::Local holder, int index, + v8::StartupData payload, void* data) { + assert(data == nullptr); + if (payload.raw_size == 0) { + holder->SetAlignedPointerInInternalField(index, nullptr, + v8::kEmbedderDataTypeTagDefault); + return; + } + InternalFieldData* embedder_field = new InternalFieldData{0}; + memcpy(embedder_field, payload.data, payload.raw_size); + holder->SetAlignedPointerInInternalField(index, embedder_field, + v8::kEmbedderDataTypeTagDefault); + deserialized_data.push_back(embedder_field); +} + +const v8::Context* v8__Context__New(v8::Isolate* isolate, + const v8::ObjectTemplate* templ, + const v8::Value* global_object, + v8::MicrotaskQueue* microtask_queue) { + return local_to_ptr(v8::Context::New( + isolate, nullptr, ptr_to_maybe_local(templ), + ptr_to_maybe_local(global_object), + v8::DeserializeInternalFieldsCallback(DeserializeInternalFields, nullptr), + microtask_queue)); +} + +bool v8__Context__EQ(const v8::Context& self, const v8::Context& other) { + return ptr_to_local(&self) == ptr_to_local(&other); +} + +void v8__Context__Enter(const v8::Context& self) { + ptr_to_local(&self)->Enter(); +} + +void v8__Context__Exit(const v8::Context& self) { ptr_to_local(&self)->Exit(); } + +const v8::Object* v8__Context__Global(const v8::Context& self) { + return local_to_ptr(ptr_to_local(&self)->Global()); +} + +void v8__Context__DetachGlobal(v8::Context& self) { + ptr_to_local(&self)->DetachGlobal(); +} + +uint32_t v8__Context__GetNumberOfEmbedderDataFields(const v8::Context& self) { + return ptr_to_local(&self)->GetNumberOfEmbedderDataFields(); +} + +// Embedder data tags cross the FFI boundary as a bare `u16`, and Rust +// hardcodes the default tag as `Context::EMBEDDER_DATA_TAG_DEFAULT = 0` and +// the tag count as `Context::EMBEDDER_DATA_TAG_COUNT`. Out-of-range tags make +// v8::ToExternalPointerTag abort, so Rust range-checks against that constant; +// if V8 raises the count, bump the Rust constant to match. +static_assert(sizeof(v8::EmbedderDataTypeTag) == sizeof(uint16_t), + "EmbedderDataTypeTag size mismatch"); +static_assert(v8::kEmbedderDataTypeTagDefault == 0, + "kEmbedderDataTypeTagDefault mismatch"); +static_assert(V8_EMBEDDER_DATA_TAG_COUNT == 15, + "V8_EMBEDDER_DATA_TAG_COUNT changed; update " + "Context::EMBEDDER_DATA_TAG_COUNT in src/context.rs"); + +void* v8__Context__GetAlignedPointerFromEmbedderData( + const v8::Context& self, int index, v8::EmbedderDataTypeTag tag) { + return ptr_to_local(&self)->GetAlignedPointerFromEmbedderData(index, tag); +} + +void v8__Context__SetAlignedPointerInEmbedderData(v8::Context& self, int index, + void* value, + v8::EmbedderDataTypeTag tag) { + ptr_to_local(&self)->SetAlignedPointerInEmbedderData(index, value, tag); +} + +const v8::Value* v8__Context__GetEmbedderData(const v8::Context& self, + int index) { + return local_to_ptr(ptr_to_local(&self)->GetEmbedderData(index)); +} + +void v8__Context__SetEmbedderData(v8::Context& self, int index, + const v8::Value* value) { + ptr_to_local(&self)->SetEmbedderData(index, ptr_to_local(value)); +} + +const v8::Data* v8__Context__GetDataFromSnapshotOnce(v8::Context& self, + size_t index) { + return maybe_local_to_ptr( + ptr_to_local(&self)->GetDataFromSnapshotOnce(index)); +} + +const v8::Object* v8__Context__GetExtrasBindingObject(v8::Context& self) { + return local_to_ptr(ptr_to_local(&self)->GetExtrasBindingObject()); +} + +void v8__Context__SetPromiseHooks(v8::Context& self, + const v8::Function* init_hook, + const v8::Function* before_hook, + const v8::Function* after_hook, + const v8::Function* resolve_hook) { + ptr_to_local(&self)->SetPromiseHooks( + ptr_to_local(init_hook), ptr_to_local(before_hook), + ptr_to_local(after_hook), ptr_to_local(resolve_hook)); +} + +void v8__Context__SetTemporalHostSystemUTCEpochNanosecondsCallback( + v8::Context& self, + v8::Context::TemporalHostSystemUTCEpochNanosecondsCallback callback) { + ptr_to_local(&self)->SetTemporalHostSystemUTCEpochNanosecondsCallback( + callback); +} + +const v8::Value* v8__Context__GetSecurityToken(const v8::Context& self) { + auto value = ptr_to_local(&self)->GetSecurityToken(); + return local_to_ptr(value); +} + +void v8__Context__SetSecurityToken(v8::Context& self, const v8::Value* token) { + auto c = ptr_to_local(&self); + c->SetSecurityToken(ptr_to_local(token)); +} + +void v8__Context__UseDefaultSecurityToken(v8::Context& self) { + ptr_to_local(&self)->UseDefaultSecurityToken(); +} + +void v8__Context__AllowCodeGenerationFromStrings(v8::Context& self, + bool allow) { + ptr_to_local(&self)->AllowCodeGenerationFromStrings(allow); +} + +bool v8__Context_IsCodeGenerationFromStringsAllowed(v8::Context& self) { + return ptr_to_local(&self)->IsCodeGenerationFromStringsAllowed(); +} + +v8::MicrotaskQueue* v8__Context__GetMicrotaskQueue(v8::Context& self) { + return ptr_to_local(&self)->GetMicrotaskQueue(); +} + +void v8__Context__SetMicrotaskQueue(v8::Context& self, + v8::MicrotaskQueue* microtask_queue) { + ptr_to_local(&self)->SetMicrotaskQueue(microtask_queue); +} + +const v8::Context* v8__Context__FromSnapshot( + v8::Isolate* isolate, size_t context_snapshot_index, + v8::Value* global_object, v8::MicrotaskQueue* microtask_queue) { + v8::MaybeLocal maybe_local = v8::Context::FromSnapshot( + isolate, context_snapshot_index, + v8::DeserializeInternalFieldsCallback(DeserializeInternalFields, nullptr), + nullptr, ptr_to_maybe_local(global_object), microtask_queue); + return maybe_local_to_ptr(maybe_local); +} + +void v8__Context__SetContinuationPreservedEmbedderData(v8::Isolate* isolate, + const v8::Value* data) { + isolate->SetContinuationPreservedEmbedderData(ptr_to_local(data)); +} + +const v8::Value* v8__Context__GetContinuationPreservedEmbedderData( + v8::Isolate* isolate) { + // Since 15.8 this returns Local. The setter only takes values, so + // whatever is stored is a Value (undefined by default). + auto value = isolate->GetContinuationPreservedEmbedderData(); + return local_to_ptr(value.As()); +} + +class RustyMicrotaskQueueHandle { + public: + RustyMicrotaskQueueHandle(v8::Isolate* isolate, v8::MicrotasksPolicy policy) + : queue_(v8::MicrotaskQueue::New(isolate, policy)) {} + + v8::MicrotaskQueue* Get() const { return queue_.Get(); } + + private: + // Microtask queues live on the cppgc heap, so hold them with a Persistent. + cppgc::Persistent queue_; +}; + +RustyMicrotaskQueueHandle* v8__MicrotaskQueueHandle__New( + v8::Isolate* isolate, v8::MicrotasksPolicy policy) { + return new RustyMicrotaskQueueHandle(isolate, policy); +} + +void v8__MicrotaskQueueHandle__DELETE(RustyMicrotaskQueueHandle* self) { + delete self; +} + +v8::MicrotaskQueue* v8__MicrotaskQueueHandle__Get( + const RustyMicrotaskQueueHandle* self) { + return self->Get(); +} + +void v8__MicrotaskQueue__PerformCheckpoint(v8::Isolate* isolate, + v8::MicrotaskQueue* self) { + self->PerformCheckpoint(isolate); +} + +// Returns whether `self` is one of `isolate`'s microtask queues. Only the +// queues of `isolate` are looked at, never `self`, which may belong to +// another isolate. +bool v8__MicrotaskQueue__BelongsTo(const v8::MicrotaskQueue* self, + v8::Isolate* isolate) { + namespace i = v8::internal; + const i::MicrotaskQueue* queue = static_cast(self); + i::Isolate* i_isolate = reinterpret_cast(isolate); + if (queue == i_isolate->default_microtask_queue()) return true; + for (const auto& weak : i_isolate->microtask_queues()) { + if (weak.Get() == queue) return true; + } + return false; +} + +bool v8__MicrotaskQueue__IsRunningMicrotasks(v8::MicrotaskQueue* self) { + return self->IsRunningMicrotasks(); +} + +int v8__MicrotaskQueue__GetMicrotasksScopeDepth(v8::MicrotaskQueue* self) { + return self->GetMicrotasksScopeDepth(); +} + +void v8__MicrotaskQueue__EnqueueMicrotask(v8::Isolate* isolate, + v8::MicrotaskQueue* self, + v8::Function* callback) { + self->EnqueueMicrotask(isolate, ptr_to_local(callback)); +} + +const v8::String* v8__Message__Get(const v8::Message& self) { + return local_to_ptr(self.Get()); +} + +const v8::String* v8__Message__GetSourceLine(const v8::Message& self, + const v8::Context& context) { + return maybe_local_to_ptr(self.GetSourceLine(ptr_to_local(&context))); +} + +const v8::Value* v8__Message__GetScriptResourceName(const v8::Message& self) { + return local_to_ptr(self.GetScriptResourceName()); +} + +int v8__Message__GetLineNumber(const v8::Message& self, + const v8::Context& context) { + v8::Maybe maybe = self.GetLineNumber(ptr_to_local(&context)); + if (maybe.IsJust()) { + return maybe.ToChecked(); + } else { + return -1; + } +} + +const v8::StackTrace* v8__Message__GetStackTrace(const v8::Message& self) { + return local_to_ptr(self.GetStackTrace()); +} + +int v8__Message__GetStartPosition(const v8::Message& self) { + return self.GetStartPosition(); +} + +int v8__Message__GetEndPosition(const v8::Message& self) { + return self.GetEndPosition(); +} + +int v8__Message__GetWasmFunctionIndex(const v8::Message& self) { + return self.GetWasmFunctionIndex(); +} + +int v8__Message__ErrorLevel(const v8::Message& self) { + return self.ErrorLevel(); +} + +int v8__Message__GetStartColumn(const v8::Message& self) { + return self.GetStartColumn(); +} + +int v8__Message__GetEndColumn(const v8::Message& self) { + return self.GetEndColumn(); +} + +bool v8__Message__IsSharedCrossOrigin(const v8::Message& self) { + return self.IsSharedCrossOrigin(); +} + +bool v8__Message__IsOpaque(const v8::Message& self) { return self.IsOpaque(); } + +const v8::Value* v8__Exception__RangeError(const v8::String& message) { + return local_to_ptr(v8::Exception::RangeError(ptr_to_local(&message))); +} + +const v8::Value* v8__Exception__ReferenceError(const v8::String& message) { + return local_to_ptr(v8::Exception::ReferenceError(ptr_to_local(&message))); +} + +const v8::Value* v8__Exception__SyntaxError(const v8::String& message) { + return local_to_ptr(v8::Exception::SyntaxError(ptr_to_local(&message))); +} + +const v8::Value* v8__Exception__TypeError(const v8::String& message) { + return local_to_ptr(v8::Exception::TypeError(ptr_to_local(&message))); +} + +const v8::Value* v8__Exception__Error(const v8::String& message) { + return local_to_ptr(v8::Exception::Error(ptr_to_local(&message))); +} + +const v8::Message* v8__Exception__CreateMessage(v8::Isolate* isolate, + const v8::Value& exception) { + return local_to_ptr( + v8::Exception::CreateMessage(isolate, ptr_to_local(&exception))); +} + +const v8::StackTrace* v8__Exception__GetStackTrace(const v8::Value& exception) { + return local_to_ptr(v8::Exception::GetStackTrace(ptr_to_local(&exception))); +} + +MaybeBool v8__Exception__CaptureStackTrace(const v8::Context& context, + const v8::Object& object) { + return maybe_to_maybe_bool(v8::Exception::CaptureStackTrace( + ptr_to_local(&context), ptr_to_local(&object))); +} + +const v8::Function* v8__Function__New( + const v8::Context& context, v8::FunctionCallback callback, + const v8::Value* data_or_null, int length, + v8::ConstructorBehavior constructor_behavior, + v8::SideEffectType side_effect_type) { + return maybe_local_to_ptr(v8::Function::New( + ptr_to_local(&context), callback, ptr_to_local(data_or_null), length, + constructor_behavior, side_effect_type)); +} + +const v8::Value* v8__Function__Call(const v8::Function& self, + const v8::Context& context, + const v8::Value& recv, int argc, + const v8::Value* const argv[]) { + return maybe_local_to_ptr( + ptr_to_local(&self)->Call(ptr_to_local(&context), ptr_to_local(&recv), + argc, const_ptr_array_to_local_array(argv))); +} + +const v8::Object* v8__Function__NewInstance(const v8::Function& self, + const v8::Context& context, + int argc, + const v8::Value* const argv[]) { + return maybe_local_to_ptr(ptr_to_local(&self)->NewInstance( + ptr_to_local(&context), argc, const_ptr_array_to_local_array(argv))); +} + +const v8::Value* v8__Function__GetName(const v8::Function& self) { + return local_to_ptr(self.GetName()); +} + +void v8__Function__SetName(const v8::Function& self, const v8::String& name) { + return ptr_to_local(&self)->SetName(ptr_to_local(&name)); +} + +int v8__Function__GetScriptColumnNumber(const v8::Function& self) { + return ptr_to_local(&self)->GetScriptColumnNumber(); +} + +int v8__Function__GetScriptLineNumber(const v8::Function& self) { + return ptr_to_local(&self)->GetScriptLineNumber(); +} + +int v8__Function__ScriptId(const v8::Function& self) { + return ptr_to_local(&self)->ScriptId(); +} + +void v8__Function__GetScriptOrigin(const v8::Function& self, + uninit_t* out) { + construct_in_place(out, + ptr_to_local(&self)->GetScriptOrigin()); +} + +const v8::Signature* v8__Signature__New(v8::Isolate* isolate, + const v8::FunctionTemplate* templ) { + return local_to_ptr(v8::Signature::New(isolate, ptr_to_local(templ))); +} + +const v8::FunctionTemplate* v8__FunctionTemplate__New( + v8::Isolate* isolate, v8::FunctionCallback callback, + const v8::Value* data_or_null, const v8::Signature* signature_or_null, + int length, v8::ConstructorBehavior constructor_behavior, + v8::SideEffectType side_effect_type, const v8::CFunction* c_functions, + size_t c_functions_len) { + v8::MemorySpan overloads{c_functions, c_functions_len}; + return local_to_ptr(v8::FunctionTemplate::NewWithCFunctionOverloads( + isolate, callback, ptr_to_local(data_or_null), + ptr_to_local(signature_or_null), length, constructor_behavior, + side_effect_type, overloads)); +} + +const v8::Function* v8__FunctionTemplate__GetFunction( + const v8::FunctionTemplate& self, const v8::Context& context) { + return maybe_local_to_ptr( + ptr_to_local(&self)->GetFunction(ptr_to_local(&context))); +} + +void v8__FunctionTemplate__SetClassName(const v8::FunctionTemplate& self, + const v8::String& name) { + ptr_to_local(&self)->SetClassName(ptr_to_local(&name)); +} + +void v8__FunctionTemplate__SetAccessorProperty(const v8::FunctionTemplate& self, + const v8::Name& key, + v8::FunctionTemplate& getter, + v8::FunctionTemplate& setter, + v8::PropertyAttribute attr) { + ptr_to_local(&self)->SetAccessorProperty( + ptr_to_local(&key), ptr_to_local(&getter), ptr_to_local(&setter), attr); +} + +void v8__FunctionTemplate__Inherit(const v8::FunctionTemplate& self, + const v8::FunctionTemplate& parent) { + ptr_to_local(&self)->Inherit(ptr_to_local(&parent)); +} + +void v8__FunctionTemplate__ReadOnlyPrototype(const v8::FunctionTemplate& self) { + ptr_to_local(&self)->ReadOnlyPrototype(); +} + +void v8__FunctionTemplate__RemovePrototype(const v8::FunctionTemplate& self) { + ptr_to_local(&self)->RemovePrototype(); +} + +bool v8__FunctionTemplate__HasInstance(const v8::FunctionTemplate& self, + const v8::Value& object) { + return ptr_to_local(&self)->HasInstance(ptr_to_local(&object)); +} + +const v8::DictionaryTemplate* v8__DictionaryTemplate__New( + v8::Isolate* isolate, const char* const* names, const size_t* name_lengths, + size_t names_len) { + std::vector vec; + vec.reserve(names_len); + for (size_t i = 0; i < names_len; i++) { + vec.emplace_back(names[i], name_lengths[i]); + } + return local_to_ptr(v8::DictionaryTemplate::New( + isolate, std::span(vec.data(), vec.size()))); +} + +const v8::Object* v8__DictionaryTemplate__NewInstance( + const v8::DictionaryTemplate& self, const v8::Context& context, + const v8::Value* const* values, size_t values_len) { + std::vector> vec; + vec.reserve(values_len); + for (size_t i = 0; i < values_len; i++) { + if (values[i] == nullptr) { + vec.emplace_back(); + } else { + vec.emplace_back(ptr_to_local(values[i])); + } + } + return local_to_ptr(ptr_to_local(&self)->NewInstance( + ptr_to_local(&context), + std::span>(vec.data(), vec.size()))); +} + +const v8::ObjectTemplate* v8__FunctionTemplate__PrototypeTemplate( + const v8::FunctionTemplate& self) { + return local_to_ptr(ptr_to_local(&self)->PrototypeTemplate()); +} + +const v8::ObjectTemplate* v8__FunctionTemplate__InstanceTemplate( + const v8::FunctionTemplate& self) { + return local_to_ptr(ptr_to_local(&self)->InstanceTemplate()); +} + +v8::Isolate* v8__FunctionCallbackInfo__GetIsolate( + const v8::FunctionCallbackInfo& self) { + return self.GetIsolate(); +} + +function_callback_info_parts_t v8__FunctionCallbackInfo__GetParts( + const v8::FunctionCallbackInfo& self) { + v8::ReturnValue rv = self.GetReturnValue(); + return { + self.GetIsolate(), + *reinterpret_cast(&rv), + local_to_ptr(self.Data()), + self.Length(), + }; +} + +const v8::Value* v8__FunctionCallbackInfo__Data( + const v8::FunctionCallbackInfo& self) { + return local_to_ptr(self.Data()); +} + +const v8::Value* v8__FunctionCallbackInfo__NewTarget( + const v8::FunctionCallbackInfo& self) { + return local_to_ptr(self.NewTarget()); +} + +const v8::Object* v8__FunctionCallbackInfo__This( + const v8::FunctionCallbackInfo& self) { + return local_to_ptr(self.This()); +} + +const v8::Value* v8__FunctionCallbackInfo__Get( + const v8::FunctionCallbackInfo& self, int index) { + return local_to_ptr(self[index]); +} + +int v8__FunctionCallbackInfo__Length( + const v8::FunctionCallbackInfo& self) { + return self.Length(); +} + +bool v8__FunctionCallbackInfo__IsConstructCall( + const v8::FunctionCallbackInfo& self) { + return self.IsConstructCall(); +} + +uintptr_t* v8__FunctionCallbackInfo__GetReturnValue( + const v8::FunctionCallbackInfo& self) { + v8::ReturnValue rv = self.GetReturnValue(); + return *reinterpret_cast(&rv); +} + +v8::Isolate* v8__PropertyCallbackInfo__GetIsolate( + const v8::PropertyCallbackInfo& self) { + return self.GetIsolate(); +} + +const v8::Value* v8__PropertyCallbackInfo__Data( + const v8::PropertyCallbackInfo& self) { + return local_to_ptr(self.Data()); +} + +const v8::Object* v8__PropertyCallbackInfo__Holder( + const v8::PropertyCallbackInfo& self) { + return local_to_ptr(self.Holder()); +} + +uintptr_t* v8__PropertyCallbackInfo__GetReturnValue( + const v8::PropertyCallbackInfo& self) { + v8::ReturnValue rv = self.GetReturnValue(); + return *reinterpret_cast(&rv); +} + +bool v8__PropertyCallbackInfo__ShouldThrowOnError( + const v8::PropertyCallbackInfo& self) { + return self.ShouldThrowOnError(); +} + +const v8::RegExp* v8__RegExp__New(const v8::Context& context, + const v8::String& pattern, + v8::RegExp::Flags flags) { + return maybe_local_to_ptr( + v8::RegExp::New(ptr_to_local(&context), ptr_to_local(&pattern), flags)); +} + +const v8::Object* v8__RegExp__Exec(v8::RegExp& self, const v8::Context& context, + const v8::String& subject) { + return maybe_local_to_ptr( + self.Exec(ptr_to_local(&context), ptr_to_local(&subject))); +} + +// The largest value v8::RegExp::NewWithBacktrackLimit accepts. It ApiChecks +// (i.e. aborts, even in release builds) that the limit is a valid Smi, and the +// Smi range depends on whether pointer compression is enabled, so report it to +// Rust rather than hardcoding a bound there. +uint32_t v8__RegExp__MAX_BACKTRACK_LIMIT() { + return static_cast(v8::internal::kSmiMaxValue); +} + +const v8::RegExp* v8__RegExp__NewWithBacktrackLimit(const v8::Context& context, + const v8::String& pattern, + v8::RegExp::Flags flags, + uint32_t backtrack_limit) { + return maybe_local_to_ptr(v8::RegExp::NewWithBacktrackLimit( + ptr_to_local(&context), ptr_to_local(&pattern), flags, backtrack_limit)); +} + +const v8::String* v8__RegExp__GetSource(const v8::RegExp& self) { + return local_to_ptr(self.GetSource()); +} + +void v8__ReturnValue__Value__Set(v8::ReturnValue* self, + const v8::Value& value) { + self->Set(ptr_to_local(&value)); +} + +void v8__ReturnValue__Value__Set__Bool(v8::ReturnValue* self, + bool i) { + self->Set(i); +} + +void v8__ReturnValue__Value__Set__Int32(v8::ReturnValue* self, + int32_t i) { + self->Set(i); +} + +void v8__ReturnValue__Value__Set__Uint32(v8::ReturnValue* self, + uint32_t i) { + self->Set(i); +} + +void v8__ReturnValue__Value__Set__Double(v8::ReturnValue* self, + double i) { + self->Set(i); +} + +void v8__ReturnValue__Value__SetNull(v8::ReturnValue* self) { + self->SetNull(); +} + +void v8__ReturnValue__Value__SetUndefined(v8::ReturnValue* self) { + self->SetUndefined(); +} + +void v8__ReturnValue__Value__SetEmptyString(v8::ReturnValue* self) { + self->SetEmptyString(); +} + +const v8::Value* v8__ReturnValue__Value__Get( + const v8::ReturnValue& self) { + return local_to_ptr(self.Get()); +} + +// Note: StackTraceOptions is deprecated, kDetailed is always used +const v8::StackTrace* v8__StackTrace__CurrentStackTrace(v8::Isolate* isolate, + int frame_limit) { + return local_to_ptr(v8::StackTrace::CurrentStackTrace(isolate, frame_limit)); +} + +const v8::String* v8__StackTrace__CurrentScriptNameOrSourceURL( + v8::Isolate* isolate) { + return local_to_ptr(v8::StackTrace::CurrentScriptNameOrSourceURL(isolate)); +} + +int v8__StackTrace__GetFrameCount(const v8::StackTrace& self) { + return self.GetFrameCount(); +} + +const v8::StackFrame* v8__StackTrace__GetFrame(const v8::StackTrace& self, + v8::Isolate* isolate, + uint32_t index) { + return local_to_ptr(self.GetFrame(isolate, index)); +} + +int v8__StackFrame__GetLineNumber(const v8::StackFrame& self) { + return self.GetLineNumber(); +} + +int v8__StackFrame__GetColumn(const v8::StackFrame& self) { + return self.GetColumn(); +} + +int v8__StackFrame__GetScriptId(const v8::StackFrame& self) { + return self.GetScriptId(); +} + +const v8::String* v8__StackFrame__GetScriptName(const v8::StackFrame& self) { + return local_to_ptr(self.GetScriptName()); +} + +const v8::String* v8__StackFrame__GetScriptNameOrSourceURL( + const v8::StackFrame& self) { + return local_to_ptr(self.GetScriptNameOrSourceURL()); +} + +const v8::String* v8__StackFrame__GetScriptSource(const v8::StackFrame& self) { + return local_to_ptr(self.GetScriptSource()); +} + +const v8::String* v8__StackFrame__GetScriptSourceMappingURL( + const v8::StackFrame& self) { + return local_to_ptr(self.GetScriptSourceMappingURL()); +} + +const v8::String* v8__StackFrame__GetFunctionName(const v8::StackFrame& self) { + return local_to_ptr(self.GetFunctionName()); +} + +bool v8__StackFrame__IsEval(const v8::StackFrame& self) { + return self.IsEval(); +} + +bool v8__StackFrame__IsConstructor(const v8::StackFrame& self) { + return self.IsConstructor(); +} + +bool v8__StackFrame__IsWasm(const v8::StackFrame& self) { + return self.IsWasm(); +} + +bool v8__StackFrame__IsUserJavaScript(const v8::StackFrame& self) { + return self.IsUserJavaScript(); +} + +void v8__TryCatch__CONSTRUCT(uninit_t* buf, + v8::Isolate* isolate) { + construct_in_place(buf, isolate); +} + +void v8__TryCatch__DESTRUCT(v8::TryCatch* self) { self->~TryCatch(); } + +bool v8__TryCatch__HasCaught(const v8::TryCatch& self) { + return self.HasCaught(); +} + +bool v8__TryCatch__CanContinue(const v8::TryCatch& self) { + return self.CanContinue(); +} + +bool v8__TryCatch__HasTerminated(const v8::TryCatch& self) { + return self.HasTerminated(); +} + +const v8::Value* v8__TryCatch__Exception(const v8::TryCatch& self) { + return local_to_ptr(self.Exception()); +} + +const v8::Value* v8__TryCatch__StackTrace(const v8::TryCatch& self, + const v8::Context& context) { + return maybe_local_to_ptr(self.StackTrace(ptr_to_local(&context))); +} + +const v8::Message* v8__TryCatch__Message(const v8::TryCatch& self) { + return local_to_ptr(self.Message()); +} + +void v8__TryCatch__Reset(v8::TryCatch* self) { self->Reset(); } + +const v8::Value* v8__TryCatch__ReThrow(v8::TryCatch* self) { + return local_to_ptr(self->ReThrow()); +} + +bool v8__TryCatch__IsVerbose(const v8::TryCatch& self) { + return self.IsVerbose(); +} + +void v8__TryCatch__SetVerbose(v8::TryCatch* self, bool value) { + self->SetVerbose(value); +} + +void v8__TryCatch__SetCaptureMessage(v8::TryCatch* self, bool value) { + self->SetCaptureMessage(value); +} + +void v8__DisallowJavascriptExecutionScope__CONSTRUCT( + uninit_t* buf, + v8::Isolate* isolate, + v8::Isolate::DisallowJavascriptExecutionScope::OnFailure on_failure) { + construct_in_place( + buf, isolate, on_failure); +} + +void v8__DisallowJavascriptExecutionScope__DESTRUCT( + v8::Isolate::DisallowJavascriptExecutionScope* self) { + self->~DisallowJavascriptExecutionScope(); +} + +void v8__AllowJavascriptExecutionScope__CONSTRUCT( + uninit_t* buf, + v8::Isolate* isolate) { + construct_in_place(buf, isolate); +} + +void v8__AllowJavascriptExecutionScope__DESTRUCT( + v8::Isolate::AllowJavascriptExecutionScope* self) { + self->~AllowJavascriptExecutionScope(); +} + +#define V(NAME) \ + const v8::NAME* v8__##NAME##__New(const v8::ArrayBuffer& buf_ptr, \ + size_t byte_offset, size_t length) { \ + return local_to_ptr( \ + v8::NAME::New(ptr_to_local(&buf_ptr), byte_offset, length)); \ + } +EACH_TYPED_ARRAY(V) +#undef V + +#define V(NAME) \ + const v8::NAME* v8__##NAME##__New__with_shared_buffer( \ + const v8::SharedArrayBuffer& buf_ptr, size_t byte_offset, \ + size_t length) { \ + return local_to_ptr( \ + v8::NAME::New(ptr_to_local(&buf_ptr), byte_offset, length)); \ + } +EACH_TYPED_ARRAY(V) +#undef V + +const v8::Script* v8__Script__Compile(const v8::Context& context, + const v8::String& source, + const v8::ScriptOrigin& origin) { + return maybe_local_to_ptr( + v8::Script::Compile(ptr_to_local(&context), ptr_to_local(&source), + const_cast(&origin))); +} + +const v8::UnboundScript* v8__Script__GetUnboundScript( + const v8::Script& script) { + return local_to_ptr(ptr_to_local(&script)->GetUnboundScript()); +} + +int v8__Script__ScriptId(const v8::Script& script) { + return ptr_to_local(&script)->ScriptId(); +} + +const v8::Script* v8__UnboundScript__BindToCurrentContext( + const v8::UnboundScript& unbound_script) { + return local_to_ptr(ptr_to_local(&unbound_script)->BindToCurrentContext()); +} + +int v8__UnboundScript__ScriptId(const v8::UnboundScript& unbound_script) { + return ptr_to_local(&unbound_script)->ScriptId(); +} + +v8::ScriptCompiler::CachedData* v8__UnboundScript__CreateCodeCache( + const v8::UnboundScript& unbound_script) { + return v8::ScriptCompiler::CreateCodeCache(ptr_to_local(&unbound_script)); +} + +v8::Value* v8__UnboundScript__GetSourceMappingURL( + const v8::UnboundScript& unbound_script) { + return local_to_ptr(ptr_to_local(&unbound_script)->GetSourceMappingURL()); +} + +v8::Value* v8__UnboundScript__GetSourceURL( + const v8::UnboundScript& unbound_script) { + return local_to_ptr(ptr_to_local(&unbound_script)->GetSourceURL()); +} + +v8::ScriptCompiler::CachedData* v8__UnboundModuleScript__CreateCodeCache( + const v8::UnboundModuleScript& unbound_module_script) { + return v8::ScriptCompiler::CreateCodeCache( + ptr_to_local(&unbound_module_script)); +} + +v8::Value* v8__UnboundModuleScript__GetSourceMappingURL( + const v8::UnboundModuleScript& unbound_module_script) { + return local_to_ptr( + ptr_to_local(&unbound_module_script)->GetSourceMappingURL()); +} + +v8::Value* v8__UnboundModuleScript__GetSourceURL( + const v8::UnboundModuleScript& unbound_module_script) { + return local_to_ptr(ptr_to_local(&unbound_module_script)->GetSourceURL()); +} + +v8::ScriptCompiler::CachedData* v8__Function__CreateCodeCache( + const v8::Function& self) { + return v8::ScriptCompiler::CreateCodeCacheForFunction(ptr_to_local(&self)); +} + +const v8::Value* v8__Script__Run(const v8::Script& script, + const v8::Context& context) { + return maybe_local_to_ptr(ptr_to_local(&script)->Run(ptr_to_local(&context))); +} + +void v8__ScriptOrigin__CONSTRUCT( + uninit_t* buf, const v8::Value& resource_name, + int resource_line_offset, int resource_column_offset, + bool resource_is_shared_cross_origin, int script_id, + const v8::Value* source_map_url, bool resource_is_opaque, bool is_wasm, + bool is_module, const v8::Data* host_defined_options) { + construct_in_place( + buf, ptr_to_local(&resource_name), resource_line_offset, + resource_column_offset, resource_is_shared_cross_origin, script_id, + ptr_to_local(source_map_url), resource_is_opaque, is_wasm, is_module, + ptr_to_local(host_defined_options)); +} + +int v8__ScriptOrigin__ScriptId(const v8::ScriptOrigin& self) { + return self.ScriptId(); +} + +const v8::Value* v8__ScriptOrigin__ResourceName(const v8::ScriptOrigin& self) { + return local_to_ptr(self.ResourceName()); +} + +const v8::Value* v8__ScriptOrigin__SourceMapUrl(const v8::ScriptOrigin& self) { + return local_to_ptr(self.SourceMapUrl()); +} + +const v8::Value* v8__ScriptOrModule__GetResourceName( + const v8::ScriptOrModule& self) { + return local_to_ptr(ptr_to_local(&self)->GetResourceName()); +} + +const v8::Data* v8__ScriptOrModule__HostDefinedOptions( + const v8::ScriptOrModule& self) { + return local_to_ptr(ptr_to_local(&self)->HostDefinedOptions()); +} + +const v8::SharedArrayBuffer* v8__SharedArrayBuffer__New__with_byte_length( + v8::Isolate* isolate, size_t byte_length) { + return local_to_ptr(v8::SharedArrayBuffer::New(isolate, byte_length)); +} + +const v8::SharedArrayBuffer* v8__SharedArrayBuffer__New__with_backing_store( + v8::Isolate* isolate, + const std::shared_ptr& backing_store) { + return local_to_ptr(v8::SharedArrayBuffer::New(isolate, backing_store)); +} + +size_t v8__SharedArrayBuffer__ByteLength(const v8::SharedArrayBuffer& self) { + return self.ByteLength(); +} + +two_pointers_t v8__SharedArrayBuffer__GetBackingStore( + const v8::SharedArrayBuffer& self) { + return make_pod(ptr_to_local(&self)->GetBackingStore()); +} + +v8::BackingStore* v8__SharedArrayBuffer__NewBackingStore__with_byte_length( + v8::Isolate* isolate, size_t byte_length) { + std::unique_ptr u = + v8::SharedArrayBuffer::NewBackingStore(isolate, byte_length); + return u.release(); +} + +v8::BackingStore* v8__SharedArrayBuffer__NewBackingStore__with_mode( + v8::Isolate* isolate, size_t byte_length, + v8::BackingStoreInitializationMode initialization_mode, + v8::BackingStoreOnFailureMode on_failure) { + std::unique_ptr u = v8::SharedArrayBuffer::NewBackingStore( + isolate, byte_length, initialization_mode, on_failure); + return u.release(); +} + +void* v8__SharedArrayBuffer__Data(const v8::SharedArrayBuffer& self) { + return ptr_to_local(&self)->Data(); +} + +v8::BackingStore* v8__SharedArrayBuffer__NewBackingStore__with_data( + void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, + void* deleter_data) { + std::unique_ptr u = v8::SharedArrayBuffer::NewBackingStore( + data, byte_length, deleter, deleter_data); + return u.release(); +} + +const v8::Value* v8__JSON__Parse(const v8::Context& context, + const v8::String& json_string) { + return maybe_local_to_ptr( + v8::JSON::Parse(ptr_to_local(&context), ptr_to_local(&json_string))); +} + +const v8::String* v8__JSON__Stringify(const v8::Context& context, + const v8::Value& json_object) { + return maybe_local_to_ptr( + v8::JSON::Stringify(ptr_to_local(&context), ptr_to_local(&json_object))); +} + +const v8::Promise::Resolver* v8__Promise__Resolver__New( + const v8::Context& context) { + return maybe_local_to_ptr(v8::Promise::Resolver::New(ptr_to_local(&context))); +} + +const v8::Promise* v8__Promise__Resolver__GetPromise( + const v8::Promise::Resolver& self) { + return local_to_ptr(ptr_to_local(&self)->GetPromise()); +} + +MaybeBool v8__Promise__Resolver__Resolve(const v8::Promise::Resolver& self, + const v8::Context& context, + const v8::Value& value) { + return maybe_to_maybe_bool(ptr_to_local(&self)->Resolve( + ptr_to_local(&context), ptr_to_local(&value))); +} + +MaybeBool v8__Promise__Resolver__Reject(const v8::Promise::Resolver& self, + const v8::Context& context, + const v8::Value& value) { + return maybe_to_maybe_bool(ptr_to_local(&self)->Reject(ptr_to_local(&context), + ptr_to_local(&value))); +} + +v8::Promise::PromiseState v8__Promise__State(const v8::Promise& self) { + return ptr_to_local(&self)->State(); +} + +bool v8__Promise__HasHandler(const v8::Promise& self) { + return ptr_to_local(&self)->HasHandler(); +} + +void v8__Promise__MarkAsHandled(const v8::Promise& self) { + ptr_to_local(&self)->MarkAsHandled(); +} + +const v8::Value* v8__Promise__Result(const v8::Promise& self) { + return local_to_ptr(ptr_to_local(&self)->Result()); +} + +const v8::Promise* v8__Promise__Catch(const v8::Promise& self, + const v8::Context& context, + const v8::Function& handler) { + return maybe_local_to_ptr(ptr_to_local(&self)->Catch(ptr_to_local(&context), + ptr_to_local(&handler))); +} + +const v8::Promise* v8__Promise__Then(const v8::Promise& self, + const v8::Context& context, + const v8::Function& handler) { + return maybe_local_to_ptr(ptr_to_local(&self)->Then(ptr_to_local(&context), + ptr_to_local(&handler))); +} + +const v8::Promise* v8__Promise__Then2(const v8::Promise& self, + const v8::Context& context, + const v8::Function& on_fulfilled, + const v8::Function& on_rejected) { + return maybe_local_to_ptr(ptr_to_local(&self)->Then( + ptr_to_local(&context), ptr_to_local(&on_fulfilled), + ptr_to_local(&on_rejected))); +} + +v8::PromiseRejectEvent v8__PromiseRejectMessage__GetEvent( + const v8::PromiseRejectMessage& self) { + return self.GetEvent(); +} + +const v8::Promise* v8__PromiseRejectMessage__GetPromise( + const v8::PromiseRejectMessage& self) { + return local_to_ptr(self.GetPromise()); +} + +const v8::Value* v8__PromiseRejectMessage__GetValue( + const v8::PromiseRejectMessage& self) { + return local_to_ptr(self.GetValue()); +} + +const v8::Proxy* v8__Proxy__New(const v8::Context& context, + const v8::Object& target, + const v8::Object& handler) { + return maybe_local_to_ptr(v8::Proxy::New( + ptr_to_local(&context), ptr_to_local(&target), ptr_to_local(&handler))); +} + +const v8::Value* v8__Proxy__GetHandler(const v8::Proxy& self) { + return local_to_ptr(ptr_to_local(&self)->GetHandler()); +} + +const v8::Value* v8__Proxy__GetTarget(const v8::Proxy& self) { + return local_to_ptr(ptr_to_local(&self)->GetTarget()); +} + +bool v8__Proxy__IsRevoked(const v8::Proxy& self) { + return ptr_to_local(&self)->IsRevoked(); +} + +void v8__Proxy__Revoke(const v8::Proxy& self) { ptr_to_local(&self)->Revoke(); } + +void v8__SnapshotCreator__CONSTRUCT(uninit_t* buf, + const v8::Isolate::CreateParams& params) { + construct_in_place(buf, params); +} + +void v8__SnapshotCreator__DESTRUCT(v8::SnapshotCreator* self) { + self->~SnapshotCreator(); +} + +bool v8__StartupData__CanBeRehashed(const v8::StartupData& self) { + return self.CanBeRehashed(); +} + +bool v8__StartupData__IsValid(const v8::StartupData& self) { + return self.IsValid(); +} + +void v8__StartupData__data__DELETE(const char* data) { delete[] data; } + +v8::Isolate* v8__SnapshotCreator__GetIsolate(const v8::SnapshotCreator& self) { + // `v8::SnapshotCreator::GetIsolate()` is not declared as a const method, but + // this appears to be a mistake. + auto self_ptr = const_cast(&self); + return self_ptr->GetIsolate(); +} + +v8::StartupData SerializeInternalFields(v8::Local holder, int index, + void* data) { + assert(data == nullptr); + InternalFieldData* embedder_field = static_cast( + holder->GetAlignedPointerFromInternalField( + index, v8::kEmbedderDataTypeTagDefault)); + if (embedder_field == nullptr) return {nullptr, 0}; + int size = sizeof(*embedder_field); + char* payload = new char[size]; + // We simply use memcpy to serialize the content. + memcpy(payload, embedder_field, size); + return {payload, size}; +} + +void v8__SnapshotCreator__SetDefaultContext(v8::SnapshotCreator* self, + const v8::Context& context) { + self->SetDefaultContext(ptr_to_local(&context), SerializeInternalFields); +} + +size_t v8__SnapshotCreator__AddContext(v8::SnapshotCreator* self, + const v8::Context& context) { + return self->AddContext(ptr_to_local(&context), SerializeInternalFields); +} + +size_t v8__SnapshotCreator__AddData_to_isolate(v8::SnapshotCreator* self, + const v8::Data& data) { + return self->AddData(ptr_to_local(&data)); +} + +size_t v8__SnapshotCreator__AddData_to_context(v8::SnapshotCreator* self, + const v8::Context& context, + const v8::Data& data) { + return self->AddData(ptr_to_local(&context), ptr_to_local(&data)); +} + +v8::StartupData v8__SnapshotCreator__CreateBlob( + v8::SnapshotCreator* self, + v8::SnapshotCreator::FunctionCodeHandling function_code_handling) { + return self->CreateBlob(function_code_handling); +} + +// Rust-side callbacks for trait-based CustomPlatform (PlatformImpl trait). +// Each callback corresponds to a C++ virtual method on TaskRunner or Platform. +// `context` is a pointer to the Rust Box. +// Task ownership is transferred to Rust — Rust is responsible for calling +// Run() and deleting the task. +extern "C" { +void v8__Platform__CustomPlatform__BASE__PostTask(void* context, void* isolate, + v8::Task* task); +void v8__Platform__CustomPlatform__BASE__PostNonNestableTask(void* context, + void* isolate, + v8::Task* task); +void v8__Platform__CustomPlatform__BASE__PostDelayedTask( + void* context, void* isolate, v8::Task* task, double delay_in_seconds); +void v8__Platform__CustomPlatform__BASE__PostNonNestableDelayedTask( + void* context, void* isolate, v8::Task* task, double delay_in_seconds); +void v8__Platform__CustomPlatform__BASE__PostIdleTask(void* context, + void* isolate, + v8::IdleTask* task); +void v8__Platform__CustomPlatform__BASE__DROP(void* context); +} + +// FFI functions for running and deleting V8 tasks from Rust. +extern "C" { +void v8__Task__Run(v8::Task* task) { task->Run(); } +void v8__Task__DELETE(v8::Task* task) { delete task; } +void v8__IdleTask__Run(v8::IdleTask* task, double deadline_in_seconds) { + task->Run(deadline_in_seconds); +} +void v8__IdleTask__DELETE(v8::IdleTask* task) { delete task; } +} + +// TaskRunner wrapper that intercepts all PostTask* virtual methods and +// transfers task ownership to Rust via the PlatformImpl trait. The Rust +// side is responsible for scheduling and calling task->Run(). +// +// The wrapped runner is kept for capability queries (IdleTasksEnabled, etc.) +// but tasks are NOT forwarded to it — Rust owns them entirely. +class CustomTaskRunner final : public v8::TaskRunner { + public: + CustomTaskRunner(std::shared_ptr wrapped, void* context, + v8::Isolate* isolate) + : wrapped_(std::move(wrapped)), context_(context), isolate_(isolate) {} + + bool IdleTasksEnabled() override { return wrapped_->IdleTasksEnabled(); } + bool NonNestableTasksEnabled() const override { + return wrapped_->NonNestableTasksEnabled(); + } + bool NonNestableDelayedTasksEnabled() const override { + return wrapped_->NonNestableDelayedTasksEnabled(); + } + + protected: + void PostTaskImpl(std::unique_ptr task, + const v8::SourceLocation& location) override { + v8__Platform__CustomPlatform__BASE__PostTask( + context_, static_cast(isolate_), task.release()); + } + void PostNonNestableTaskImpl(std::unique_ptr task, + const v8::SourceLocation& location) override { + v8__Platform__CustomPlatform__BASE__PostNonNestableTask( + context_, static_cast(isolate_), task.release()); + } + void PostDelayedTaskImpl(std::unique_ptr task, + double delay_in_seconds, + const v8::SourceLocation& location) override { + v8__Platform__CustomPlatform__BASE__PostDelayedTask( + context_, static_cast(isolate_), task.release(), + delay_in_seconds > 0 ? delay_in_seconds : 0.0); + } + void PostNonNestableDelayedTaskImpl( + std::unique_ptr task, double delay_in_seconds, + const v8::SourceLocation& location) override { + v8__Platform__CustomPlatform__BASE__PostNonNestableDelayedTask( + context_, static_cast(isolate_), task.release(), + delay_in_seconds > 0 ? delay_in_seconds : 0.0); + } + void PostIdleTaskImpl(std::unique_ptr task, + const v8::SourceLocation& location) override { + v8__Platform__CustomPlatform__BASE__PostIdleTask( + context_, static_cast(isolate_), task.release()); + } + + private: + std::shared_ptr wrapped_; + void* context_; + v8::Isolate* isolate_; +}; + +// Platform subclass that wraps each isolate's TaskRunner to notify Rust +// when foreground tasks are posted. Follows the inspector API pattern. +// +// NotifyIsolateShutdown is NOT intercepted here because it is not virtual +// on DefaultPlatform — V8's free function does static_cast +// and calls it directly, bypassing any override. Isolate cleanup must be +// handled on the Rust side (e.g. in the isolate's Drop impl). +class CustomPlatform : public v8::platform::DefaultPlatform { + using IdleTaskSupport = v8::platform::IdleTaskSupport; + + public: + CustomPlatform(int thread_pool_size, IdleTaskSupport idle_task_support, + bool unprotected, void* context) + : DefaultPlatform(thread_pool_size, idle_task_support), + unprotected_(unprotected), + context_(context) {} + + // SAFETY: The platform is single-owner (via unique_ptr). The destructor + // runs after all isolates have been disposed and no more task runner + // callbacks can fire, so DROP does not race with other callbacks. + ~CustomPlatform() override { + v8__Platform__CustomPlatform__BASE__DROP(context_); + } + + std::shared_ptr GetForegroundTaskRunner( + v8::Isolate* isolate, v8::TaskPriority priority) override { + auto original = DefaultPlatform::GetForegroundTaskRunner(isolate, priority); + std::lock_guard lock(mutex_); + auto key = std::make_pair(isolate, priority); + auto it = runners_.find(key); + if (it != runners_.end()) { + auto runner = it->second.lock(); + if (runner) return runner; + } + auto custom = + std::make_shared(original, context_, isolate); + runners_[key] = custom; + return custom; + } + + // When unprotected, disable thread-isolated allocations (same as + // UnprotectedDefaultPlatform). Required when isolates may be created on + // threads other than the one that called v8::V8::Initialize (e.g. worker + // threads in Deno). + v8::ThreadIsolatedAllocator* GetThreadIsolatedAllocator() override { + if (unprotected_) return nullptr; + return DefaultPlatform::GetThreadIsolatedAllocator(); + } + + private: + bool unprotected_; + void* context_; + std::mutex mutex_; + // weak_ptr so runners are kept alive only while V8 holds a reference. + // When V8 drops its shared_ptr (e.g. on isolate shutdown), the weak_ptr + // expires and a fresh wrapper is created if GetForegroundTaskRunner is + // called again. This avoids preventing cleanup of the underlying runner. + std::map, + std::weak_ptr> + runners_; +}; + +class UnprotectedDefaultPlatform : public v8::platform::DefaultPlatform { + using IdleTaskSupport = v8::platform::IdleTaskSupport; + using InProcessStackDumping = v8::platform::InProcessStackDumping; + using PriorityMode = v8::platform::PriorityMode; + using TracingController = v8::TracingController; + + public: + explicit UnprotectedDefaultPlatform( + int thread_pool_size, IdleTaskSupport idle_task_support, + std::unique_ptr tracing_controller = {}, + PriorityMode priority_mode = PriorityMode::kDontApply) + : v8::platform::DefaultPlatform(thread_pool_size, idle_task_support, + std::move(tracing_controller), + priority_mode) {} + + v8::ThreadIsolatedAllocator* GetThreadIsolatedAllocator() override { + // DefaultThreadIsolatedAllocator is PKU protected + return nullptr; + } +}; + +v8::Platform* v8__Platform__NewDefaultPlatform(int thread_pool_size, + bool idle_task_support) { + return v8::platform::NewDefaultPlatform( + thread_pool_size, + idle_task_support ? v8::platform::IdleTaskSupport::kEnabled + : v8::platform::IdleTaskSupport::kDisabled, + v8::platform::InProcessStackDumping::kDisabled, nullptr) + .release(); +} + +v8::Platform* v8__Platform__NewUnprotectedDefaultPlatform( + int thread_pool_size, bool idle_task_support) { + if (thread_pool_size < 1) { + thread_pool_size = std::thread::hardware_concurrency(); + } + thread_pool_size = std::max(std::min(thread_pool_size, 16), 1); + return std::make_unique( + thread_pool_size, idle_task_support + ? v8::platform::IdleTaskSupport::kEnabled + : v8::platform::IdleTaskSupport::kDisabled) + .release(); +} + +v8::Platform* v8__Platform__NewSingleThreadedDefaultPlatform( + bool idle_task_support) { + return v8::platform::NewSingleThreadedDefaultPlatform( + idle_task_support ? v8::platform::IdleTaskSupport::kEnabled + : v8::platform::IdleTaskSupport::kDisabled, + v8::platform::InProcessStackDumping::kDisabled, nullptr) + .release(); +} + +v8::Platform* v8__Platform__NewCustomPlatform(int thread_pool_size, + bool idle_task_support, + bool unprotected, void* context) { + if (thread_pool_size < 1) { + thread_pool_size = std::thread::hardware_concurrency(); + } + thread_pool_size = std::max(std::min(thread_pool_size, 16), 1); + return std::make_unique( + thread_pool_size, + idle_task_support ? v8::platform::IdleTaskSupport::kEnabled + : v8::platform::IdleTaskSupport::kDisabled, + unprotected, context) + .release(); +} + +bool v8__Platform__PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate, + bool wait_for_work) { + return v8::platform::PumpMessageLoop( + platform, isolate, + wait_for_work ? v8::platform::MessageLoopBehavior::kWaitForWork + : v8::platform::MessageLoopBehavior::kDoNotWait); +} + +void v8__Platform__RunIdleTasks(v8::Platform* platform, v8::Isolate* isolate, + double idle_time_in_seconds) { + v8::platform::RunIdleTasks(platform, isolate, idle_time_in_seconds); +} + +void v8__Platform__NotifyIsolateShutdown(v8::Platform* platform, + v8::Isolate* isolate) { + v8::platform::NotifyIsolateShutdown(platform, isolate); +} + +void v8__Platform__DELETE(v8::Platform* self) { delete self; } + +two_pointers_t std__shared_ptr__v8__Platform__CONVERT__std__unique_ptr( + v8::Platform* unique_ptr) { + return make_pod(std::shared_ptr(unique_ptr)); +} + +v8::Platform* std__shared_ptr__v8__Platform__get( + const std::shared_ptr& ptr) { + return ptr.get(); +} + +two_pointers_t std__shared_ptr__v8__Platform__COPY( + const std::shared_ptr& ptr) { + return make_pod(ptr); +} + +void std__shared_ptr__v8__Platform__reset(std::shared_ptr* ptr) { + ptr->reset(); +} + +long std__shared_ptr__v8__Platform__use_count( + const std::shared_ptr& ptr) { + return ptr.use_count(); +} + +void v8_inspector__V8Inspector__Channel__BASE__sendResponse( + v8_inspector::V8Inspector::Channel* self, int callId, + v8_inspector::StringBuffer* message); +void v8_inspector__V8Inspector__Channel__BASE__sendNotification( + v8_inspector::V8Inspector::Channel* self, + v8_inspector::StringBuffer* message); +void v8_inspector__V8Inspector__Channel__BASE__flushProtocolNotifications( + v8_inspector::V8Inspector::Channel* self); + +void v8_inspector__V8Inspector__DELETE(v8_inspector::V8Inspector* self) { + delete self; +} + +v8_inspector::V8Inspector* v8_inspector__V8Inspector__create( + v8::Isolate* isolate, v8_inspector::V8InspectorClient* client) { + std::unique_ptr u = + v8_inspector::V8Inspector::create(isolate, client); + return u.release(); +} + +v8_inspector::V8InspectorSession* v8_inspector__V8Inspector__connect( + v8_inspector::V8Inspector* self, int context_group_id, + v8_inspector::V8Inspector::Channel* channel, v8_inspector::StringView state, + v8_inspector::V8Inspector::ClientTrustLevel client_trust_level) { + std::unique_ptr u = + self->connect(context_group_id, channel, state, client_trust_level); + return u.release(); +} + +void v8_inspector__V8Inspector__contextCreated( + v8_inspector::V8Inspector* self, const v8::Context& context, + int contextGroupId, v8_inspector::StringView humanReadableName, + v8_inspector::StringView auxData) { + v8_inspector::V8ContextInfo info(ptr_to_local(&context), contextGroupId, + humanReadableName); + info.auxData = auxData; + self->contextCreated(info); +} + +void v8_inspector__V8Inspector__contextDestroyed( + v8_inspector::V8Inspector* self, const v8::Context& context) { + self->contextDestroyed(ptr_to_local(&context)); +} + +void v8_inspector__V8Inspector__idleStarted(v8_inspector::V8Inspector* self) { + self->idleStarted(); +} + +void v8_inspector__V8Inspector__idleFinished(v8_inspector::V8Inspector* self) { + self->idleFinished(); +} + +void v8_inspector__V8Inspector__asyncTaskScheduled( + v8_inspector::V8Inspector* self, v8_inspector::StringView task_name, + void* task, bool recurring) { + self->asyncTaskScheduled(task_name, task, recurring); +} + +void v8_inspector__V8Inspector__asyncTaskCanceled( + v8_inspector::V8Inspector* self, void* task) { + self->asyncTaskCanceled(task); +} + +void v8_inspector__V8Inspector__asyncTaskStarted( + v8_inspector::V8Inspector* self, void* task) { + self->asyncTaskStarted(task); +} + +void v8_inspector__V8Inspector__asyncTaskFinished( + v8_inspector::V8Inspector* self, void* task) { + self->asyncTaskFinished(task); +} + +void v8_inspector__V8Inspector__allAsyncTasksCanceled( + v8_inspector::V8Inspector* self) { + self->allAsyncTasksCanceled(); +} + +bool v8_inspector__V8InspectorSession__canDispatchMethod( + v8_inspector::StringView method) { + return v8_inspector::V8InspectorSession::canDispatchMethod(method); +} + +unsigned v8_inspector__V8Inspector__exceptionThrown( + v8_inspector::V8Inspector* self, const v8::Context& context, + v8_inspector::StringView message, const v8::Value& exception, + v8_inspector::StringView detailed_message, v8_inspector::StringView url, + unsigned line_number, unsigned column_number, + v8_inspector::V8StackTrace* stack_trace, int script_id) { + return self->exceptionThrown( + ptr_to_local(&context), message, ptr_to_local(&exception), + detailed_message, url, line_number, column_number, + static_cast>(stack_trace), + script_id); +} + +v8_inspector::V8StackTrace* v8_inspector__V8Inspector__createStackTrace( + v8_inspector::V8Inspector* self, const v8::StackTrace& stack_trace) { + std::unique_ptr u = + self->createStackTrace(ptr_to_local(&stack_trace)); + return u.release(); +} + +void v8_inspector__V8StackTrace__DELETE(v8_inspector::V8StackTrace* self) { + delete self; +} + +void v8_inspector__V8InspectorSession__DELETE( + v8_inspector::V8InspectorSession* self) { + delete self; +} + +void v8_inspector__V8InspectorSession__dispatchProtocolMessage( + v8_inspector::V8InspectorSession* self, v8_inspector::StringView message) { + self->dispatchProtocolMessage(message); +} + +void v8_inspector__V8InspectorSession__releaseObjectGroup( + v8_inspector::V8InspectorSession* self, + v8_inspector::StringView object_group) { + self->releaseObjectGroup(object_group); +} + +v8_inspector::protocol::Runtime::API::RemoteObject* +v8_inspector__V8InspectorSession__wrapObject( + v8_inspector::V8InspectorSession* self, const v8::Context* context, + const v8::Value* value, v8_inspector::StringView object_group, + bool generate_preview) { + return self + ->wrapObject(ptr_to_local(context), ptr_to_local(value), object_group, + generate_preview) + .release(); +} + +bool v8_inspector__V8InspectorSession__unwrapObject( + v8_inspector::V8InspectorSession* self, v8_inspector::StringBuffer** error, + v8_inspector::StringView object_id, const v8::Value** value, + const v8::Context** context, v8_inspector::StringBuffer** object_group) { + std::unique_ptr error_buffer; + v8::Local local_value; + v8::Local local_context; + std::unique_ptr object_group_buffer; + bool success = self->unwrapObject(&error_buffer, object_id, &local_value, + &local_context, &object_group_buffer); + if (!success) { + *error = error_buffer.release(); + return false; + } + + *value = local_to_ptr(local_value); + *context = local_to_ptr(local_context); + *object_group = object_group_buffer.release(); + return true; +} + +void v8_inspector__RemoteObject__DELETE( + v8_inspector::protocol::Runtime::API::RemoteObject* self) { + delete self; +} + +void v8_inspector__V8InspectorSession__schedulePauseOnNextStatement( + v8_inspector::V8InspectorSession* self, v8_inspector::StringView reason, + v8_inspector::StringView detail) { + self->schedulePauseOnNextStatement(reason, detail); +} + +void v8_inspector__V8InspectorSession__cancelPauseOnNextStatement( + v8_inspector::V8InspectorSession* self) { + self->cancelPauseOnNextStatement(); +} +} // extern "C" + +extern "C" { +const v8::Value* v8_inspector__V8InspectorSession__Inspectable__BASE__get( + void* rust_impl, const v8::Context* context); +void v8_inspector__V8InspectorSession__Inspectable__BASE__DROP(void* rust_impl); +} + +class v8_inspector__V8InspectorSession__Inspectable__BASE final + : public v8_inspector::V8InspectorSession::Inspectable { + public: + explicit v8_inspector__V8InspectorSession__Inspectable__BASE(void* rust_impl) + : rust_impl_(rust_impl) {} + + ~v8_inspector__V8InspectorSession__Inspectable__BASE() override { + v8_inspector__V8InspectorSession__Inspectable__BASE__DROP(rust_impl_); + } + + v8::Local get(v8::Local context) override { + // The Rust CallbackScope relies on `NewCallbackScope for Local` + // having `NEEDS_SCOPE == false` and must not open its own HandleScope. + // Handles created by the Rust implementation are allocated in this scope + // and must be escaped here. Opening a nested HandleScope in Rust would + // destroy that scope before Escape uses its handle, causing a + // use-after-free. + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + if (isolate == nullptr) return v8::Local(); + v8::EscapableHandleScope handle_scope(isolate); + v8::Context::Scope context_scope(context); + return handle_scope.Escape( + ptr_to_local(v8_inspector__V8InspectorSession__Inspectable__BASE__get( + rust_impl_, local_to_ptr(context)))); + } + + private: + void* rust_impl_; +}; + +extern "C" { +v8_inspector::V8InspectorSession::Inspectable* +v8_inspector__V8InspectorSession__Inspectable__NEW(void* rust_impl) { + return new v8_inspector__V8InspectorSession__Inspectable__BASE(rust_impl); +} + +void v8_inspector__V8InspectorSession__Inspectable__DELETE( + v8_inspector::V8InspectorSession::Inspectable* inspectable) { + delete inspectable; +} + +void v8_inspector__V8InspectorSession__addInspectedObject( + v8_inspector::V8InspectorSession* self, + v8_inspector::V8InspectorSession::Inspectable* inspectable) { + self->addInspectedObject( + std::unique_ptr( + inspectable)); +} +} + +struct v8_inspector__V8Inspector__Channel__BASE + : public v8_inspector::V8Inspector::Channel { + using v8_inspector::V8Inspector::Channel::Channel; + + void sendResponse( + int callId, + std::unique_ptr message) override { + v8_inspector__V8Inspector__Channel__BASE__sendResponse(this, callId, + message.release()); + } + void sendNotification( + std::unique_ptr message) override { + v8_inspector__V8Inspector__Channel__BASE__sendNotification( + this, message.release()); + } + void flushProtocolNotifications() override { + v8_inspector__V8Inspector__Channel__BASE__flushProtocolNotifications(this); + } +}; + +extern "C" { +void v8_inspector__V8Inspector__Channel__BASE__CONSTRUCT( + uninit_t* buf) { + construct_in_place(buf); +} + +void v8_inspector__V8Inspector__Channel__sendResponse( + v8_inspector::V8Inspector::Channel* self, int callId, + v8_inspector::StringBuffer* message) { + self->sendResponse( + callId, + static_cast>(message)); +} +void v8_inspector__V8Inspector__Channel__sendNotification( + v8_inspector::V8Inspector::Channel* self, + v8_inspector::StringBuffer* message) { + self->sendNotification( + static_cast>(message)); +} +void v8_inspector__V8Inspector__Channel__flushProtocolNotifications( + v8_inspector::V8Inspector::Channel* self) { + self->flushProtocolNotifications(); +} + +int64_t v8_inspector__V8InspectorClient__BASE__generateUniqueId( + v8_inspector::V8InspectorClient* self); +void v8_inspector__V8InspectorClient__BASE__runMessageLoopOnPause( + v8_inspector::V8InspectorClient* self, int contextGroupId); +void v8_inspector__V8InspectorClient__BASE__quitMessageLoopOnPause( + v8_inspector::V8InspectorClient* self); +void v8_inspector__V8InspectorClient__BASE__runIfWaitingForDebugger( + v8_inspector::V8InspectorClient* self, int contextGroupId); +void v8_inspector__V8InspectorClient__BASE__consoleAPIMessage( + v8_inspector::V8InspectorClient* self, int contextGroupId, + v8::Isolate::MessageErrorLevel level, + const v8_inspector::StringView& message, + const v8_inspector::StringView& url, unsigned lineNumber, + unsigned columnNumber, v8_inspector::V8StackTrace* stackTrace); +v8_inspector::StringBuffer* v8_inspector__V8InspectorClient__BASE__valueSubtype( + v8_inspector::V8InspectorClient* self, v8::Context* context, + v8::Value* value); +v8_inspector::StringBuffer* +v8_inspector__V8InspectorClient__BASE__descriptionForValueSubtype( + v8_inspector::V8InspectorClient* self, v8::Context* context, + v8::Value* value); +v8::Context* v8_inspector__V8InspectorClient__BASE__ensureDefaultContextInGroup( + v8_inspector::V8InspectorClient* self, int context_group_id); +v8_inspector::StringBuffer* +v8_inspector__V8InspectorClient__BASE__resourceNameToUrl( + v8_inspector::V8InspectorClient* self, + const v8_inspector::StringView& resource_name_view); + +} // extern "C" + +struct v8_inspector__V8InspectorClient__BASE + : public v8_inspector::V8InspectorClient { + using v8_inspector::V8InspectorClient::V8InspectorClient; + + int64_t generateUniqueId() override { + return v8_inspector__V8InspectorClient__BASE__generateUniqueId(this); + } + void runMessageLoopOnPause(int contextGroupId) override { + v8_inspector__V8InspectorClient__BASE__runMessageLoopOnPause( + this, contextGroupId); + } + void quitMessageLoopOnPause() override { + v8_inspector__V8InspectorClient__BASE__quitMessageLoopOnPause(this); + } + void runIfWaitingForDebugger(int contextGroupId) override { + v8_inspector__V8InspectorClient__BASE__runIfWaitingForDebugger( + this, contextGroupId); + } + void consoleAPIMessage(int contextGroupId, + v8::Isolate::MessageErrorLevel level, + const v8_inspector::StringView& message, + const v8_inspector::StringView& url, + unsigned lineNumber, unsigned columnNumber, + v8_inspector::V8StackTrace* stackTrace) override { + v8_inspector__V8InspectorClient__BASE__consoleAPIMessage( + this, contextGroupId, level, message, url, lineNumber, columnNumber, + stackTrace); + } + std::unique_ptr valueSubtype( + v8::Local value) override { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + if (isolate == nullptr) return nullptr; + v8::Local context = isolate->GetCurrentContext(); + if (context.IsEmpty()) return nullptr; + v8_inspector::StringBuffer* b = + v8_inspector__V8InspectorClient__BASE__valueSubtype( + this, local_to_ptr(context), local_to_ptr(value)); + return std::unique_ptr(b); + } + std::unique_ptr descriptionForValueSubtype( + v8::Local context, v8::Local value) override { + v8_inspector::StringBuffer* b = + v8_inspector__V8InspectorClient__BASE__descriptionForValueSubtype( + this, local_to_ptr(context), local_to_ptr(value)); + return std::unique_ptr(b); + } + v8::Local ensureDefaultContextInGroup( + int context_group_id) override { + return ptr_to_local( + v8_inspector__V8InspectorClient__BASE__ensureDefaultContextInGroup( + this, context_group_id)); + } + std::unique_ptr resourceNameToUrl( + const v8_inspector::StringView& resource_name_view) override { + v8_inspector::StringBuffer* b = + v8_inspector__V8InspectorClient__BASE__resourceNameToUrl( + this, resource_name_view); + return std::unique_ptr(b); + } +}; + +extern "C" { +void v8_inspector__V8InspectorClient__BASE__CONSTRUCT( + uninit_t* buf) { + construct_in_place(buf); +} + +int64_t v8_inspector__V8InspectorClient__generateUniqueId( + v8_inspector::V8InspectorClient* self) { + return self->generateUniqueId(); +} + +void v8_inspector__V8InspectorClient__runMessageLoopOnPause( + v8_inspector::V8InspectorClient* self, int contextGroupId) { + self->runMessageLoopOnPause(contextGroupId); +} +void v8_inspector__V8InspectorClient__quitMessageLoopOnPause( + v8_inspector::V8InspectorClient* self) { + self->quitMessageLoopOnPause(); +} +void v8_inspector__V8InspectorClient__runIfWaitingForDebugger( + v8_inspector::V8InspectorClient* self, int contextGroupId) { + self->runIfWaitingForDebugger(contextGroupId); +} + +void v8_inspector__V8InspectorClient__consoleAPIMessage( + v8_inspector::V8InspectorClient* self, int contextGroupId, + v8::Isolate::MessageErrorLevel level, + const v8_inspector::StringView& message, + const v8_inspector::StringView& url, unsigned lineNumber, + unsigned columnNumber, v8_inspector::V8StackTrace* stackTrace) { + self->consoleAPIMessage(contextGroupId, level, message, url, lineNumber, + columnNumber, stackTrace); +} + +void v8_inspector__StringBuffer__DELETE(v8_inspector::StringBuffer* self) { + delete self; +} + +three_pointers_t v8_inspector__StringBuffer__string( + const v8_inspector::StringBuffer& self) { + return make_pod(self.string()); +} + +v8_inspector::StringBuffer* v8_inspector__StringBuffer__create( + v8_inspector::StringView source) { + return v8_inspector::StringBuffer::create(source).release(); +} + +int v8__Location__GetLineNumber(v8::Location* self) { + return self->GetLineNumber(); +} + +int v8__Location__GetColumnNumber(v8::Location* self) { + return self->GetColumnNumber(); +} + +v8::Module::Status v8__Module__GetStatus(const v8::Module& self) { + return self.GetStatus(); +} + +const v8::Value* v8__Module__GetException(const v8::Module& self) { + return local_to_ptr(self.GetException()); +} + +const v8::FixedArray* v8__Module__GetModuleRequests(const v8::Module& self) { + return local_to_ptr(self.GetModuleRequests()); +} + +void v8__Module__SourceOffsetToLocation(const v8::Module& self, int offset, + v8::Location* out) { + *out = self.SourceOffsetToLocation(offset); +} + +const v8::Value* v8__Module__GetModuleNamespace(const v8::Module& self) { + return local_to_ptr(ptr_to_local(&self)->GetModuleNamespace()); +} + +const v8::Value* v8__Module__GetModuleNamespace2(const v8::Module& self, + v8::ModuleImportPhase phase) { + return local_to_ptr(ptr_to_local(&self)->GetModuleNamespace(phase)); +} + +const v8::Value* v8__Module__EvaluateForImportDefer( + const v8::Module& self, const v8::Context& context) { + return maybe_local_to_ptr( + ptr_to_local(&self)->EvaluateForImportDefer(ptr_to_local(&context))); +} + +int v8__Module__GetIdentityHash(const v8::Module& self) { + return self.GetIdentityHash(); +} + +int v8__Module__ScriptId(const v8::Module& self) { + // Module::ScriptId() isn't marked const but its implementation is + // so this const_cast is sound. + // TODO(bnoordhuis) Open V8 CL to mark Module::ScriptId() and + // UnboundScript::GetId() const. + return const_cast(self).ScriptId(); +} + +MaybeBool v8__Module__InstantiateModule( + const v8::Module& self, const v8::Context& context, + v8::Module::ResolveModuleCallback cb, + v8::Module::ResolveSourceCallback source_callback) { + return maybe_to_maybe_bool(ptr_to_local(&self)->InstantiateModule( + ptr_to_local(&context), cb, source_callback)); +} + +const v8::Value* v8__Module__Evaluate(const v8::Module& self, + const v8::Context& context) { + return maybe_local_to_ptr( + ptr_to_local(&self)->Evaluate(ptr_to_local(&context))); +} + +bool v8__Module__IsGraphAsync(const v8::Module& self) { + return ptr_to_local(&self)->IsGraphAsync(); +} + +bool v8__Module__HasTopLevelAwait(const v8::Module& self) { + return ptr_to_local(&self)->HasTopLevelAwait(); +} + +bool v8__Module__IsSourceTextModule(const v8::Module& self) { + return ptr_to_local(&self)->IsSourceTextModule(); +} + +bool v8__Module__IsSyntheticModule(const v8::Module& self) { + return ptr_to_local(&self)->IsSyntheticModule(); +} + +const v8::Module* v8__Module__CreateSyntheticModule( + v8::Isolate* isolate, const v8::String* module_name, + size_t export_names_len, const v8::String* export_names_raw[], + v8::Module::SyntheticModuleEvaluationSteps evaluation_steps) { + std::vector> export_names_vec{}; + for (size_t i = 0; i < export_names_len; i += 1) { + export_names_vec.push_back(ptr_to_local(export_names_raw[i])); + } + auto export_names = v8::MemorySpan>{ + export_names_vec.data(), export_names_len}; + return local_to_ptr(v8::Module::CreateSyntheticModule( + isolate, ptr_to_local(module_name), export_names, evaluation_steps)); +} + +MaybeBool v8__Module__SetSyntheticModuleExport(const v8::Module& self, + v8::Isolate* isolate, + const v8::String* export_name, + const v8::Value* export_value) { + return maybe_to_maybe_bool(ptr_to_local(&self)->SetSyntheticModuleExport( + isolate, ptr_to_local(export_name), ptr_to_local(export_value))); +} + +const v8::UnboundModuleScript* v8__Module__GetUnboundModuleScript( + const v8::Module& self) { + return local_to_ptr(ptr_to_local(&self)->GetUnboundModuleScript()); +} + +struct StalledTopLevelAwaitMessage { + const v8::Module* module; + const v8::Message* message; +}; + +size_t v8__Module__GetStalledTopLevelAwaitMessage( + const v8::Module& self, v8::Isolate* isolate, + StalledTopLevelAwaitMessage* out_vec, size_t out_len) { + auto [modules, messages] = + ptr_to_local(&self)->GetStalledTopLevelAwaitMessages(isolate); + auto len = std::min(messages.size(), out_len); + for (size_t i = 0; i < len; i += 1) { + StalledTopLevelAwaitMessage stalled_message; + stalled_message.module = local_to_ptr(modules[i]); + stalled_message.message = local_to_ptr(messages[i]); + out_vec[i] = stalled_message; + } + return len; +} + +const v8::String* v8__ModuleRequest__GetSpecifier( + const v8::ModuleRequest& self) { + return local_to_ptr(self.GetSpecifier()); +} + +v8::ModuleImportPhase v8__ModuleRequest__GetPhase( + const v8::ModuleRequest& self) { + return self.GetPhase(); +} + +int v8__ModuleRequest__GetSourceOffset(const v8::ModuleRequest& self) { + return self.GetSourceOffset(); +} + +const v8::FixedArray* v8__ModuleRequest__GetImportAttributes( + const v8::ModuleRequest& self) { + return local_to_ptr(self.GetImportAttributes()); +} + +struct WasmStreamingSharedPtr { + std::shared_ptr inner; +}; + +static_assert(sizeof(WasmStreamingSharedPtr) <= 2 * sizeof(void*), + "std::shared_ptr size mismatch"); + +void v8__WasmStreaming__Unpack(v8::Isolate* isolate, const v8::Value& value, + WasmStreamingSharedPtr* self) { + new (self) WasmStreamingSharedPtr(); + self->inner = v8::WasmStreaming::Unpack(isolate, ptr_to_local(&value)); +} + +void v8__WasmStreaming__shared_ptr_DESTRUCT(WasmStreamingSharedPtr* self) { + self->~WasmStreamingSharedPtr(); +} + +void v8__WasmStreaming__SetHasCompiledModuleBytes( + WasmStreamingSharedPtr* self) { + self->inner->SetHasCompiledModuleBytes(); +} + +void v8__WasmStreaming__OnBytesReceived(WasmStreamingSharedPtr* self, + const uint8_t* data, size_t len) { + self->inner->OnBytesReceived(data, len); +} + +void v8__WasmStreaming__Finish( + WasmStreamingSharedPtr* self, + void (*callback)(v8::WasmStreaming::ModuleCachingInterface&)) { + self->inner->Finish(callback); +} + +void v8__WasmStreaming__Abort(WasmStreamingSharedPtr* self, + const v8::Value* exception) { + self->inner->Abort(ptr_to_maybe_local(exception)); +} + +void v8__WasmStreaming__SetUrl(WasmStreamingSharedPtr* self, const char* url, + size_t len) { + self->inner->SetUrl(url, len); +} + +const_memory_span_t v8__ModuleCachingInterface__GetWireBytes( + const v8::WasmStreaming::ModuleCachingInterface& self) { + v8::MemorySpan span = self.GetWireBytes(); + return {span.data(), span.size()}; +} + +bool v8__ModuleCachingInterface__SetCachedCompiledModuleBytes( + v8::WasmStreaming::ModuleCachingInterface& self, + const_memory_span_t bytes) { + v8::MemorySpan bytes_span{bytes.data, bytes.size}; + return self.SetCachedCompiledModuleBytes(bytes_span); +} + +const v8::ArrayBuffer* v8__WasmMemoryObject__Buffer( + const v8::WasmMemoryObject& self) { + return local_to_ptr(ptr_to_local(&self)->Buffer()); +} + +using HeapSnapshotCallback = bool (*)(void*, const char*, size_t); + +void v8__HeapProfiler__TakeHeapSnapshot(v8::Isolate* isolate, + HeapSnapshotCallback callback, + void* arg) { + struct OutputStream : public v8::OutputStream { + OutputStream(HeapSnapshotCallback callback, void* arg) + : callback_(callback), arg_(arg) {} + void EndOfStream() override { + static_cast(callback_(arg_, nullptr, 0)); + } + v8::OutputStream::WriteResult WriteAsciiChunk(char* data, + int size) override { + assert(size >= 0); // Can never be < 0 barring bugs in V8. + if (callback_(arg_, data, static_cast(size))) + return v8::OutputStream::kContinue; + return v8::OutputStream::kAbort; + } + HeapSnapshotCallback const callback_; + void* const arg_; + }; + + const v8::HeapSnapshot* snapshot = + isolate->GetHeapProfiler()->TakeHeapSnapshot(); + if (snapshot == nullptr) return; // Snapshotting failed, probably OOM. + OutputStream stream(callback, arg); + snapshot->Serialize(&stream); + // We don't want to call HeapProfiler::DeleteAllHeapSnapshots() because that + // invalidates snapshots we don't own. The const_cast hack has been in use + // in node-heapdump for the last 8 years and I think there is a pretty + // good chance it'll keep working for 8 more. + const_cast(snapshot)->Delete(); +} + +void v8__CpuProfiler__CollectSample(v8::Isolate* isolate, + const uint64_t* trace_id) { + if (trace_id == nullptr) { + v8::CpuProfiler::CollectSample(isolate); + } else { + v8::CpuProfiler::CollectSample(isolate, *trace_id); + } +} + +void v8__CpuProfiler__UseDetailedSourcePositionsForProfiling( + v8::Isolate* isolate) { + v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate); +} + +} // extern "C" + +// v8::ValueSerializer::Delegate + +extern "C" { +void v8__ValueSerializer__Delegate__ThrowDataCloneError( + v8::ValueSerializer::Delegate* self, v8::Local message); + +bool v8__ValueSerializer__Delegate__HasCustomHostObject( + v8::ValueSerializer::Delegate* self, v8::Isolate* isolate); + +MaybeBool v8__ValueSerializer__Delegate__IsHostObject( + v8::ValueSerializer::Delegate* self, v8::Isolate* isolate, + v8::Local object); + +MaybeBool v8__ValueSerializer__Delegate__WriteHostObject( + v8::ValueSerializer::Delegate* self, v8::Isolate* isolate, + v8::Local object); + +bool v8__ValueSerializer__Delegate__GetSharedArrayBufferId( + v8::ValueSerializer::Delegate* self, v8::Isolate* isolate, + v8::Local shared_array_buffer, uint32_t* result); + +bool v8__ValueSerializer__Delegate__GetWasmModuleTransferId( + v8::ValueSerializer::Delegate* self, v8::Isolate* isolate, + v8::Local module, uint32_t* result); + +void* v8__ValueSerializer__Delegate__ReallocateBufferMemory( + v8::ValueSerializer::Delegate* self, void* old_buffer, size_t size, + size_t* actual_size); + +void v8__ValueSerializer__Delegate__FreeBufferMemory( + v8::ValueSerializer::Delegate* self, void* buffer); +} + +struct v8__ValueSerializer__Delegate : public v8::ValueSerializer::Delegate { + void ThrowDataCloneError(v8::Local message) override { + v8__ValueSerializer__Delegate__ThrowDataCloneError(this, message); + } + + bool HasCustomHostObject(v8::Isolate* isolate) override { + return v8__ValueSerializer__Delegate__HasCustomHostObject(this, isolate); + } + + v8::Maybe IsHostObject(v8::Isolate* isolate, + v8::Local object) override { + return maybe_bool_to_maybe( + v8__ValueSerializer__Delegate__IsHostObject(this, isolate, object)); + } + + v8::Maybe WriteHostObject(v8::Isolate* isolate, + v8::Local object) override { + return maybe_bool_to_maybe( + v8__ValueSerializer__Delegate__WriteHostObject(this, isolate, object)); + } + + v8::Maybe GetSharedArrayBufferId( + v8::Isolate* isolate, + v8::Local shared_array_buffer) override { + uint32_t result = 0; + if (!v8__ValueSerializer__Delegate__GetSharedArrayBufferId( + this, isolate, shared_array_buffer, &result)) { + // Forward to the original method. It'll throw DataCloneError. + return v8::ValueSerializer::Delegate::GetSharedArrayBufferId( + isolate, shared_array_buffer); + } + return v8::Just(result); + } + + v8::Maybe GetWasmModuleTransferId( + v8::Isolate* isolate, v8::Local module) override { + uint32_t result = 0; + if (!v8__ValueSerializer__Delegate__GetWasmModuleTransferId( + this, isolate, module, &result)) + return v8::Nothing(); + return v8::Just(result); + } + + void* ReallocateBufferMemory(void* old_buffer, size_t size, + size_t* actual_size) override { + return v8__ValueSerializer__Delegate__ReallocateBufferMemory( + this, old_buffer, size, actual_size); + } + + void FreeBufferMemory(void* buffer) override { + v8__ValueSerializer__Delegate__FreeBufferMemory(this, buffer); + } +}; + +extern "C" { +void v8__ValueSerializer__Delegate__CONSTRUCT( + uninit_t* buf) { + static_assert(sizeof(v8__ValueSerializer__Delegate) == sizeof(size_t), + "v8__ValueSerializer__Delegate size mismatch"); + construct_in_place(buf); +} +} + +// v8::ValueSerializer + +extern "C" { +void v8__ValueSerializer__CONSTRUCT(uninit_t* buf, + v8::Isolate* isolate, + v8::ValueSerializer::Delegate* delegate) { + static_assert(sizeof(v8::ValueSerializer) == sizeof(size_t), + "v8::ValueSerializer size mismatch"); + construct_in_place(buf, isolate, delegate); +} + +void v8__ValueSerializer__DESTRUCT(v8::ValueSerializer* self) { + self->~ValueSerializer(); +} + +void v8__ValueSerializer__Release(v8::ValueSerializer* self, uint8_t** ptr, + size_t* size) { + auto result = self->Release(); + *ptr = result.first; + *size = result.second; +} + +void v8__ValueSerializer__SetTreatArrayBufferViewsAsHostObjects( + v8::ValueSerializer* self, bool mode) { + self->SetTreatArrayBufferViewsAsHostObjects(mode); +} + +void v8__ValueSerializer__WriteHeader(v8::ValueSerializer* self) { + self->WriteHeader(); +} + +MaybeBool v8__ValueSerializer__WriteValue(v8::ValueSerializer* self, + v8::Local context, + v8::Local value) { + return maybe_to_maybe_bool(self->WriteValue(context, value)); +} + +void v8__ValueSerializer__TransferArrayBuffer( + v8::ValueSerializer* self, uint32_t transfer_id, + v8::Local array_buffer) { + self->TransferArrayBuffer(transfer_id, array_buffer); +} + +void v8__ValueSerializer__WriteUint32(v8::ValueSerializer* self, + uint32_t value) { + self->WriteUint32(value); +} + +void v8__ValueSerializer__WriteUint64(v8::ValueSerializer* self, + uint64_t value) { + self->WriteUint64(value); +} + +void v8__ValueSerializer__WriteDouble(v8::ValueSerializer* self, double value) { + self->WriteDouble(value); +} + +void v8__ValueSerializer__WriteRawBytes(v8::ValueSerializer* self, + const void* source, size_t length) { + self->WriteRawBytes(source, length); +} +} + +// v8::ValueDeserializer::Delegate + +extern "C" { +v8::Object* v8__ValueDeserializer__Delegate__ReadHostObject( + v8::ValueDeserializer::Delegate* self, v8::Isolate* isolate); + +v8::SharedArrayBuffer* +v8__ValueDeserializer__Delegate__GetSharedArrayBufferFromId( + v8::ValueDeserializer::Delegate* self, v8::Isolate* isolate, + uint32_t transfer_id); + +v8::WasmModuleObject* v8__ValueDeserializer__Delegate__GetWasmModuleFromId( + v8::ValueDeserializer::Delegate* self, v8::Isolate* isolate, + uint32_t clone_id); +} + +struct v8__ValueDeserializer__Delegate + : public v8::ValueDeserializer::Delegate { + v8::MaybeLocal ReadHostObject(v8::Isolate* isolate) override { + return ptr_to_maybe_local( + v8__ValueDeserializer__Delegate__ReadHostObject(this, isolate)); + } + + v8::MaybeLocal GetSharedArrayBufferFromId( + v8::Isolate* isolate, uint32_t transfer_id) override { + return ptr_to_maybe_local( + v8__ValueDeserializer__Delegate__GetSharedArrayBufferFromId( + this, isolate, transfer_id)); + } + + v8::MaybeLocal GetWasmModuleFromId( + v8::Isolate* isolate, uint32_t clone_id) override { + return ptr_to_maybe_local( + v8__ValueDeserializer__Delegate__GetWasmModuleFromId(this, isolate, + clone_id)); + } +}; + +extern "C" { +void v8__ValueDeserializer__Delegate__CONSTRUCT( + uninit_t* buf) { + static_assert(sizeof(v8__ValueDeserializer__Delegate) == sizeof(size_t), + "v8__ValueDeserializer__Delegate size mismatch"); + construct_in_place(buf); +} +} + +// v8::ValueDeserializer + +extern "C" { +void v8__ValueDeserializer__CONSTRUCT( + uninit_t* buf, v8::Isolate* isolate, + const uint8_t* data, size_t size, + v8::ValueDeserializer::Delegate* delegate) { + static_assert(sizeof(v8::ValueDeserializer) == sizeof(size_t), + "v8::ValueDeserializer size mismatch"); + construct_in_place(buf, isolate, data, size, delegate); +} + +void v8__ValueDeserializer__DESTRUCT(v8::ValueDeserializer* self) { + self->~ValueDeserializer(); +} + +MaybeBool v8__ValueDeserializer__ReadHeader(v8::ValueDeserializer* self, + v8::Local context) { + return maybe_to_maybe_bool(self->ReadHeader(context)); +} + +v8::Value* v8__ValueDeserializer__ReadValue(v8::ValueDeserializer* self, + v8::Local context) { + return maybe_local_to_ptr(self->ReadValue(context)); +} + +void v8__ValueDeserializer__TransferArrayBuffer( + v8::ValueDeserializer* self, uint32_t transfer_id, + v8::Local array_buffer) { + self->TransferArrayBuffer(transfer_id, array_buffer); +} + +void v8__ValueDeserializer__TransferSharedArrayBuffer( + v8::ValueDeserializer* self, uint32_t transfer_id, + v8::Local shared_array_buffer) { + self->TransferSharedArrayBuffer(transfer_id, shared_array_buffer); +} + +void v8__ValueDeserializer__SetSupportsLegacyWireFormat( + v8::ValueDeserializer* self, bool supports_legacy_wire_format) { + self->SetSupportsLegacyWireFormat(supports_legacy_wire_format); +} + +bool v8__ValueDeserializer__ReadUint32(v8::ValueDeserializer* self, + uint32_t* value) { + return self->ReadUint32(value); +} + +bool v8__ValueDeserializer__ReadUint64(v8::ValueDeserializer* self, + uint64_t* value) { + return self->ReadUint64(value); +} + +bool v8__ValueDeserializer__ReadDouble(v8::ValueDeserializer* self, + double* value) { + return self->ReadDouble(value); +} + +bool v8__ValueDeserializer__ReadRawBytes(v8::ValueDeserializer* self, + size_t length, const void** data) { + return self->ReadRawBytes(length, data); +} + +uint32_t v8__ValueDeserializer__GetWireFormatVersion( + v8::ValueDeserializer* self) { + return self->GetWireFormatVersion(); +} +} // extern "C" + +// v8::WasmModuleCompilation + +extern "C" { + +v8::WasmModuleCompilation* v8__WasmModuleCompilation__NEW() { + return new v8::WasmModuleCompilation(); +} + +void v8__WasmModuleCompilation__DELETE(v8::WasmModuleCompilation* self) { + delete self; +} + +void v8__WasmModuleCompilation__OnBytesReceived(v8::WasmModuleCompilation* self, + const uint8_t* bytes, + size_t size) { + self->OnBytesReceived(bytes, size); +} + +void v8__WasmModuleCompilation__Finish( + v8::WasmModuleCompilation* self, v8::Isolate* isolate, + void (*caching_callback)(v8::WasmStreaming::ModuleCachingInterface&), + void (*resolution_callback)(void* data, const v8::WasmModuleObject* module, + const v8::Value* error), + void* resolution_data, void (*drop_resolution_data)(void* data)) { + // Use shared_ptr to reference-count the Rust closure data through + // std::function's copy semantics. The custom deleter calls back into Rust + // to drop the boxed closure when the last copy is destroyed. + auto shared_data = std::shared_ptr( + resolution_data, + [drop_resolution_data](void* p) { drop_resolution_data(p); }); + self->Finish( + isolate, caching_callback, + [resolution_callback, shared_data](auto result) { + if (auto* module = + std::get_if>(&result)) { + resolution_callback(shared_data.get(), local_to_ptr(*module), + nullptr); + } else { + resolution_callback( + shared_data.get(), nullptr, + local_to_ptr(std::get>(result))); + } + }); +} + +void v8__WasmModuleCompilation__Abort(v8::WasmModuleCompilation* self) { + self->Abort(); +} + +void v8__WasmModuleCompilation__SetHasCompiledModuleBytes( + v8::WasmModuleCompilation* self) { + self->SetHasCompiledModuleBytes(); +} + +void v8__WasmModuleCompilation__SetMoreFunctionsCanBeSerializedCallback( + v8::WasmModuleCompilation* self, + void (*callback)(void* data, v8::CompiledWasmModule* compiled_module), + void* data, void (*drop_data)(void* data)) { + auto shared_data = + std::shared_ptr(data, [drop_data](void* p) { drop_data(p); }); + self->SetMoreFunctionsCanBeSerializedCallback( + [callback, shared_data](v8::CompiledWasmModule module) { + auto* heap_module = new v8::CompiledWasmModule(std::move(module)); + callback(shared_data.get(), heap_module); + }); +} + +void v8__WasmModuleCompilation__SetUrl(v8::WasmModuleCompilation* self, + const char* url, size_t length) { + self->SetUrl(url, length); +} + +} // extern "C" + +// v8::CompiledWasmModule + +extern "C" { +const v8::WasmModuleObject* v8__WasmModuleObject__FromCompiledModule( + v8::Isolate* isolate, const v8::CompiledWasmModule* compiled_module) { + return maybe_local_to_ptr( + v8::WasmModuleObject::FromCompiledModule(isolate, *compiled_module)); +} + +v8::CompiledWasmModule* v8__WasmModuleObject__GetCompiledModule( + const v8::WasmModuleObject* self) { + v8::CompiledWasmModule cwm = ptr_to_local(self)->GetCompiledModule(); + return new v8::CompiledWasmModule(std::move(cwm)); +} + +const v8::WasmModuleObject* v8__WasmModuleObject__Compile( + v8::Isolate* isolate, uint8_t* wire_bytes_data, size_t length) { + v8::MemorySpan wire_bytes(wire_bytes_data, length); + return maybe_local_to_ptr(v8::WasmModuleObject::Compile(isolate, wire_bytes)); +} + +const uint8_t* v8__CompiledWasmModule__GetWireBytesRef( + v8::CompiledWasmModule* self, size_t* length) { + v8::MemorySpan span = self->GetWireBytesRef(); + *length = span.size(); + return span.data(); +} + +const char* v8__CompiledWasmModule__SourceUrl(v8::CompiledWasmModule* self, + size_t* length) { + const std::string& source_url = self->source_url(); + *length = source_url.size(); + return source_url.data(); +} + +void v8__CompiledWasmModule__DELETE(v8::CompiledWasmModule* self) { + delete self; +} +} // extern "C" + +// icu + +extern "C" { + +size_t icu_get_default_locale(char* output, size_t output_len) { + const icu::Locale& default_locale = icu::Locale::getDefault(); + icu::CheckedArrayByteSink sink(output, static_cast(output_len)); + UErrorCode status = U_ZERO_ERROR; + default_locale.toLanguageTag(sink, status); + assert(status == U_ZERO_ERROR); + assert(!sink.Overflowed()); + return sink.NumberOfBytesAppended(); +} + +void icu_set_default_locale(const char* locale) { + UErrorCode status = U_ZERO_ERROR; + icu::Locale::setDefault(icu::Locale(locale), status); +} + +size_t icu_get_default_time_zone(char* output, size_t output_len) { + icu::UnicodeString id; + std::unique_ptr tz(icu::TimeZone::createDefault()); + tz->getID(id); + icu::CheckedArrayByteSink sink(output, static_cast(output_len)); + id.toUTF8(sink); + assert(!sink.Overflowed()); + // Deliberately not `NumberOfBytesAppended()`: that counts bytes the sink was + // asked to write, so on overflow it exceeds `output_len` and the caller would + // read past the buffer (the assert above is compiled out in release builds). + return sink.NumberOfBytesWritten(); +} + +// NOTE: the parameter is deliberately not named `timezone`; glibc's +// declares a global with that name and the build uses -Werror,-Wshadow. +bool icu_set_default_time_zone(const char* time_zone_id) { + // `createTimeZone()` never fails: an id it does not recognize yields the + // special "unknown" zone (which behaves as GMT). Detect that and report it + // instead of silently installing GMT. + std::unique_ptr tz(icu::TimeZone::createTimeZone( + icu::UnicodeString::fromUTF8(time_zone_id))); + icu::UnicodeString id; + tz->getID(id); + if (id == icu::UnicodeString(UCAL_UNKNOWN_ZONE_ID, -1, US_INV)) { + return false; + } + // Takes ownership of the created TimeZone and installs it as the process + // wide default, so ICU (and therefore V8's Date implementation) resolves + // the given id regardless of how the host reports its time zone. + icu::TimeZone::adoptDefault(tz.release()); + return true; +} + +} // extern "C" + +// v8::PropertyDescriptor + +extern "C" { + +static_assert(sizeof(v8::PropertyDescriptor) == sizeof(size_t), + "v8::PropertyDescriptor size mismatch"); + +void v8__PropertyDescriptor__CONSTRUCT(uninit_t* buf) { + construct_in_place(buf); +} + +void v8__PropertyDescriptor__CONSTRUCT__Value_Writable( + uninit_t* buf, v8::Local value, + bool writable) { + construct_in_place(buf, value, writable); +} + +void v8__PropertyDescriptor__CONSTRUCT__Value( + uninit_t* buf, v8::Local value) { + construct_in_place(buf, value); +} + +void v8__PropertyDescriptor__CONSTRUCT__Get_Set( + uninit_t* buf, v8::Local get, + v8::Local set) { + construct_in_place(buf, get, set); +} + +void v8__PropertyDescriptor__DESTRUCT(v8::PropertyDescriptor* self) { + self->~PropertyDescriptor(); +} + +bool v8__PropertyDescriptor__configurable(const v8::PropertyDescriptor* self) { + return self->configurable(); +} + +bool v8__PropertyDescriptor__enumerable(const v8::PropertyDescriptor* self) { + return self->enumerable(); +} + +bool v8__PropertyDescriptor__writable(const v8::PropertyDescriptor* self) { + return self->writable(); +} + +const v8::Value* v8__PropertyDescriptor__value( + const v8::PropertyDescriptor* self) { + return local_to_ptr(self->value()); +} + +const v8::Value* v8__PropertyDescriptor__get( + const v8::PropertyDescriptor* self) { + return local_to_ptr(self->get()); +} + +const v8::Value* v8__PropertyDescriptor__set( + const v8::PropertyDescriptor* self) { + return local_to_ptr(self->set()); +} + +bool v8__PropertyDescriptor__has_configurable( + const v8::PropertyDescriptor* self) { + return self->has_configurable(); +} + +bool v8__PropertyDescriptor__has_enumerable( + const v8::PropertyDescriptor* self) { + return self->has_enumerable(); +} + +bool v8__PropertyDescriptor__has_writable(const v8::PropertyDescriptor* self) { + return self->has_writable(); +} + +bool v8__PropertyDescriptor__has_value(const v8::PropertyDescriptor* self) { + return self->has_value(); +} + +bool v8__PropertyDescriptor__has_get(const v8::PropertyDescriptor* self) { + return self->has_get(); +} + +bool v8__PropertyDescriptor__has_set(const v8::PropertyDescriptor* self) { + return self->has_set(); +} + +void v8__PropertyDescriptor__set_enumerable(v8::PropertyDescriptor* self, + bool enumurable) { + self->set_enumerable(enumurable); +} + +void v8__PropertyDescriptor__set_configurable(v8::PropertyDescriptor* self, + bool configurable) { + self->set_configurable(configurable); +} + +} // extern "C" + +// cppgc + +extern "C" { + +void rusty_v8_RustObj_trace(const RustObj*, cppgc::Visitor*); +const char* rusty_v8_RustObj_get_name(const RustObj*); +void rusty_v8_RustObj_drop(RustObj*); + +RustObj::~RustObj() { rusty_v8_RustObj_drop(this); } + +void RustObj::Trace(cppgc::Visitor* visitor) const { + rusty_v8_RustObj_trace(this, visitor); +} + +const char* RustObj::GetHumanReadableName() const { + return rusty_v8_RustObj_get_name(this); +} + +RustObj* v8__Object__Unwrap(v8::Isolate* isolate, const v8::Object& wrapper, + v8::CppHeapPointerTag tag) { + v8::CppHeapPointerTagRange tag_range(tag, tag); + return static_cast( + v8::Object::Unwrap(isolate, ptr_to_local(&wrapper), tag_range)); +} + +void v8__Object__Wrap(v8::Isolate* isolate, const v8::Object& wrapper, + RustObj* value, v8::CppHeapPointerTag tag) { + v8::Object::Wrap(isolate, ptr_to_local(&wrapper), value, tag); +} + +v8::CppHeap* v8__Isolate__GetCppHeap(v8::Isolate* isolate) { + return isolate->GetCppHeap(); +} + +void cppgc__initialize_process(v8::Platform* platform) { + cppgc::InitializeProcess(platform->GetPageAllocator()); +} + +void cppgc__shutdown_process() { cppgc::ShutdownProcess(); } + +v8::CppHeap* v8__CppHeap__Create(v8::Platform* platform, + cppgc::Heap::MarkingType marking_support, + cppgc::Heap::SweepingType sweeping_support) { + v8::CppHeapCreateParams params{{}}; + params.marking_support = marking_support; + params.sweeping_support = sweeping_support; + std::unique_ptr heap = v8::CppHeap::Create(platform, params); + return heap.release(); +} + +void v8__CppHeap__DELETE(v8::CppHeap* self) { delete self; } + +void cppgc__heap__enable_detached_garbage_collections_for_testing( + v8::CppHeap* heap) { + heap->EnableDetachedGarbageCollectionsForTesting(); +} + +void cppgc__heap__collect_garbage_for_testing( + v8::CppHeap* heap, cppgc::EmbedderStackState stack_state) { + heap->CollectGarbageForTesting(stack_state); +} + +class alignas(16) RustObjButAlign16 : public RustObj {}; + +RustObj* cppgc__make_garbage_collectable(v8::CppHeap* heap, size_t size, + size_t alignment) { + if (alignment <= 8) { + return cppgc::MakeGarbageCollected(heap->GetAllocationHandle(), + cppgc::AdditionalBytes(size)); + } + if (alignment <= 16) { + return cppgc::MakeGarbageCollected( + heap->GetAllocationHandle(), cppgc::AdditionalBytes(size)); + } + return nullptr; +} + +void cppgc__Visitor__Trace__Member(cppgc::Visitor* visitor, + cppgc::Member* member) { + visitor->Trace(*member); +} + +void cppgc__Visitor__Trace__WeakMember(cppgc::Visitor* visitor, + cppgc::WeakMember* member) { + visitor->Trace(*member); +} + +void cppgc__Visitor__Trace__TracedReference( + cppgc::Visitor* visitor, v8::TracedReference* ref) { + visitor->Trace(*ref); +} + +void cppgc__Member__CONSTRUCT(uninit_t>* buf, + RustObj* other) { + construct_in_place>(buf, other); +} + +void cppgc__Member__DESTRUCT(cppgc::Member* self) { + self->~BasicMember(); +} + +RustObj* cppgc__Member__Get(cppgc::Member* member) { + return member->Get(); +} + +void cppgc__Member__Assign(cppgc::Member* member, RustObj* other) { + member->operator=(other); +} + +void cppgc__WeakMember__CONSTRUCT(uninit_t>* buf, + RustObj* other) { + construct_in_place>(buf, other); +} + +void cppgc__WeakMember__DESTRUCT(cppgc::WeakMember* self) { + self->~BasicMember(); +} + +RustObj* cppgc__WeakMember__Get(cppgc::WeakMember* member) { + return member->Get(); +} + +void cppgc__WeakMember__Assign(cppgc::WeakMember* member, + RustObj* other) { + member->operator=(other); +} + +cppgc::Persistent* cppgc__Persistent__CONSTRUCT(RustObj* obj) { + return new cppgc::Persistent(obj); +} + +void cppgc__Persistent__DESTRUCT(cppgc::Persistent* self) { + delete self; +} + +void cppgc__Persistent__Assign(cppgc::Persistent* self, RustObj* ptr) { + self->operator=(ptr); +} + +RustObj* cppgc__Persistent__Get(cppgc::Persistent* self) { + return self->Get(); +} + +cppgc::WeakPersistent* cppgc__WeakPersistent__CONSTRUCT(RustObj* obj) { + return new cppgc::WeakPersistent(obj); +} + +void cppgc__WeakPersistent__DESTRUCT(cppgc::WeakPersistent* self) { + delete self; +} + +void cppgc__WeakPersistent__Assign(cppgc::WeakPersistent* self, + RustObj* ptr) { + self->operator=(ptr); +} + +RustObj* cppgc__WeakPersistent__Get(cppgc::WeakPersistent* self) { + return self->Get(); +} + +} // extern "C" + +// ============================================================================= +// simdutf bindings +// ============================================================================= + +#include "third_party/simdutf/simdutf.h" + +struct simdutf__result { + int error; + size_t count; +}; + +static simdutf__result to_ffi_result(simdutf::result r) { + return {static_cast(r.error), r.count}; +} + +extern "C" { + +// --- Validation --- + +bool simdutf__validate_utf8(const char* buf, size_t len) { + return simdutf::validate_utf8(buf, len); +} + +simdutf__result simdutf__validate_utf8_with_errors(const char* buf, + size_t len) { + return to_ffi_result(simdutf::validate_utf8_with_errors(buf, len)); +} + +bool simdutf__validate_ascii(const char* buf, size_t len) { + return simdutf::validate_ascii(buf, len); +} + +simdutf__result simdutf__validate_ascii_with_errors(const char* buf, + size_t len) { + return to_ffi_result(simdutf::validate_ascii_with_errors(buf, len)); +} + +bool simdutf__validate_utf16le(const char16_t* buf, size_t len) { + return simdutf::validate_utf16le(buf, len); +} + +simdutf__result simdutf__validate_utf16le_with_errors(const char16_t* buf, + size_t len) { + return to_ffi_result(simdutf::validate_utf16le_with_errors(buf, len)); +} + +bool simdutf__validate_utf16be(const char16_t* buf, size_t len) { + return simdutf::validate_utf16be(buf, len); +} + +simdutf__result simdutf__validate_utf16be_with_errors(const char16_t* buf, + size_t len) { + return to_ffi_result(simdutf::validate_utf16be_with_errors(buf, len)); +} + +bool simdutf__validate_utf32(const char32_t* buf, size_t len) { + return simdutf::validate_utf32(buf, len); +} + +simdutf__result simdutf__validate_utf32_with_errors(const char32_t* buf, + size_t len) { + return to_ffi_result(simdutf::validate_utf32_with_errors(buf, len)); +} + +// --- Conversion: UTF-8 <-> UTF-16LE --- + +size_t simdutf__convert_utf8_to_utf16le(const char* input, size_t length, + char16_t* output) { + return simdutf::convert_utf8_to_utf16le(input, length, output); +} + +simdutf__result simdutf__convert_utf8_to_utf16le_with_errors(const char* input, + size_t length, + char16_t* output) { + return to_ffi_result( + simdutf::convert_utf8_to_utf16le_with_errors(input, length, output)); +} + +size_t simdutf__convert_valid_utf8_to_utf16le(const char* input, size_t length, + char16_t* output) { + return simdutf::convert_valid_utf8_to_utf16le(input, length, output); +} + +size_t simdutf__convert_utf16le_to_utf8(const char16_t* input, size_t length, + char* output) { + return simdutf::convert_utf16le_to_utf8(input, length, output); +} + +simdutf__result simdutf__convert_utf16le_to_utf8_with_errors( + const char16_t* input, size_t length, char* output) { + return to_ffi_result( + simdutf::convert_utf16le_to_utf8_with_errors(input, length, output)); +} + +size_t simdutf__convert_valid_utf16le_to_utf8(const char16_t* input, + size_t length, char* output) { + return simdutf::convert_valid_utf16le_to_utf8(input, length, output); +} + +// --- Conversion: UTF-8 <-> UTF-16BE --- + +size_t simdutf__convert_utf8_to_utf16be(const char* input, size_t length, + char16_t* output) { + return simdutf::convert_utf8_to_utf16be(input, length, output); +} + +size_t simdutf__convert_utf16be_to_utf8(const char16_t* input, size_t length, + char* output) { + return simdutf::convert_utf16be_to_utf8(input, length, output); +} + +// --- Conversion: UTF-8 <-> Latin-1 --- + +size_t simdutf__convert_utf8_to_latin1(const char* input, size_t length, + char* output) { + return simdutf::convert_utf8_to_latin1(input, length, output); +} + +simdutf__result simdutf__convert_utf8_to_latin1_with_errors(const char* input, + size_t length, + char* output) { + return to_ffi_result( + simdutf::convert_utf8_to_latin1_with_errors(input, length, output)); +} + +size_t simdutf__convert_valid_utf8_to_latin1(const char* input, size_t length, + char* output) { + return simdutf::convert_valid_utf8_to_latin1(input, length, output); +} + +size_t simdutf__convert_latin1_to_utf8(const char* input, size_t length, + char* output) { + return simdutf::convert_latin1_to_utf8(input, length, output); +} + +// --- Conversion: Latin-1 <-> UTF-16LE --- + +size_t simdutf__convert_latin1_to_utf16le(const char* input, size_t length, + char16_t* output) { + return simdutf::convert_latin1_to_utf16le(input, length, output); +} + +size_t simdutf__convert_utf16le_to_latin1(const char16_t* input, size_t length, + char* output) { + return simdutf::convert_utf16le_to_latin1(input, length, output); +} + +// --- Conversion: UTF-8 <-> UTF-32 --- + +size_t simdutf__convert_utf8_to_utf32(const char* input, size_t length, + char32_t* output) { + return simdutf::convert_utf8_to_utf32(input, length, output); +} + +size_t simdutf__convert_utf32_to_utf8(const char32_t* input, size_t length, + char* output) { + return simdutf::convert_utf32_to_utf8(input, length, output); +} + +// --- Length calculation --- + +size_t simdutf__utf8_length_from_utf16le(const char16_t* input, size_t length) { + return simdutf::utf8_length_from_utf16le(input, length); +} + +size_t simdutf__utf8_length_from_utf16be(const char16_t* input, size_t length) { + return simdutf::utf8_length_from_utf16be(input, length); +} + +size_t simdutf__utf16_length_from_utf8(const char* input, size_t length) { + return simdutf::utf16_length_from_utf8(input, length); +} + +size_t simdutf__utf8_length_from_latin1(const char* input, size_t length) { + return simdutf::utf8_length_from_latin1(input, length); +} + +size_t simdutf__latin1_length_from_utf8(const char* input, size_t length) { + return simdutf::latin1_length_from_utf8(input, length); +} + +size_t simdutf__utf32_length_from_utf8(const char* input, size_t length) { + return simdutf::utf32_length_from_utf8(input, length); +} + +size_t simdutf__utf8_length_from_utf32(const char32_t* input, size_t length) { + return simdutf::utf8_length_from_utf32(input, length); +} + +size_t simdutf__utf16_length_from_utf32(const char32_t* input, size_t length) { + return simdutf::utf16_length_from_utf32(input, length); +} + +size_t simdutf__utf32_length_from_utf16le(const char16_t* input, + size_t length) { + return simdutf::utf32_length_from_utf16le(input, length); +} + +// --- Counting --- + +size_t simdutf__count_utf8(const char* input, size_t length) { + return simdutf::count_utf8(input, length); +} + +size_t simdutf__count_utf16le(const char16_t* input, size_t length) { + return simdutf::count_utf16le(input, length); +} + +size_t simdutf__count_utf16be(const char16_t* input, size_t length) { + return simdutf::count_utf16be(input, length); +} + +// --- Encoding detection --- + +int simdutf__detect_encodings(const char* input, size_t length) { + return simdutf::detect_encodings(input, length); +} + +// --- Base64 --- + +size_t simdutf__maximal_binary_length_from_base64(const char* input, + size_t length) { + return simdutf::maximal_binary_length_from_base64(input, length); +} + +simdutf__result simdutf__base64_to_binary(const char* input, size_t length, + char* output, uint64_t options, + uint64_t last_chunk_options) { + return to_ffi_result(simdutf::base64_to_binary( + input, length, output, static_cast(options), + static_cast(last_chunk_options))); +} + +size_t simdutf__base64_length_from_binary(size_t length, uint64_t options) { + return simdutf::base64_length_from_binary( + length, static_cast(options)); +} + +size_t simdutf__binary_to_base64(const char* input, size_t length, char* output, + uint64_t options) { + return simdutf::binary_to_base64( + input, length, output, static_cast(options)); +} + +} // extern "C" diff --git a/crates/v8/src/binding.hpp b/crates/v8/src/binding.hpp new file mode 100644 index 0000000000..581c29ac4f --- /dev/null +++ b/crates/v8/src/binding.hpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include + +#include "support.h" + +/** + * Types defined here will be compiled with bindgen + * and made available in `crate::binding` in rust. + */ + +static size_t v8__ScriptOrigin_SIZE = sizeof(v8::ScriptOrigin); + +static size_t cppgc__Member_SIZE = sizeof(cppgc::Member); +static size_t cppgc__WeakMember_SIZE = sizeof(cppgc::WeakMember); + +static size_t v8__TracedReference_SIZE = sizeof(v8::TracedReference); + +static size_t v8__Eternal_SIZE = sizeof(v8::Eternal); + +static size_t v8__String__ValueView_SIZE = sizeof(v8::String::ValueView); + +static int v8__String__kMaxLength = v8::String::kMaxLength; + +static int v8__String__WriteFlags__kNullTerminate = + v8::String::WriteFlags::kNullTerminate; + +static int v8__String__WriteFlags__kReplaceInvalidUtf8 = + v8::String::WriteFlags::kReplaceInvalidUtf8; + +static size_t v8__TypedArray__kMaxByteLength = v8::TypedArray::kMaxByteLength; + +static size_t v8__TYPED_ARRAY_MAX_SIZE_IN_HEAP = + V8_TYPED_ARRAY_MAX_SIZE_IN_HEAP; + +#define TYPED_ARRAY_MAX_LENGTH(name) \ + static size_t v8__##name##__kMaxLength = v8::name::kMaxLength; +EACH_TYPED_ARRAY(TYPED_ARRAY_MAX_LENGTH) +#undef TYPED_ARRAY_MAX_LENGTH + +using v8__CFunction = v8::CFunction; +using v8__CFunctionInfo = v8::CFunctionInfo; +using v8__FastOneByteString = v8::FastOneByteString; +using v8__Isolate__UseCounterFeature = v8::Isolate::UseCounterFeature; +using v8__String__WriteFlags = v8::String::WriteFlags; +using v8__ModuleImportPhase = v8::ModuleImportPhase; +using v8__HeapStatistics = v8::HeapStatistics; +using v8__HeapSpaceStatistics = v8::HeapSpaceStatistics; +using v8__HeapCodeStatistics = v8::HeapCodeStatistics; +using v8__GCType = v8::GCType; +using v8__GCCallbackFlags = v8::GCCallbackFlags; +using v8__Intercepted = v8::Intercepted; + +static uint32_t v8__MAJOR_VERSION = V8_MAJOR_VERSION; +static uint32_t v8__MINOR_VERSION = V8_MINOR_VERSION; +static uint32_t v8__BUILD_NUMBER = V8_BUILD_NUMBER; +static uint32_t v8__PATCH_LEVEL = V8_PATCH_LEVEL; +static const char* v8__VERSION_STRING = V8_VERSION_STRING; + +// NOTE: This class is never used and only serves as a reference for +// the OneByteConst struct created on Rust-side. +class ExternalConstOneByteStringResource + : public v8::String::ExternalOneByteStringResource { + public: + ExternalConstOneByteStringResource(int length) : _length(length) { + static_assert(offsetof(ExternalConstOneByteStringResource, _length) == + sizeof(size_t) * 2, + "ExternalConstOneByteStringResource's length was not at " + "offset of sizeof(size_t) * 2"); + static_assert( + sizeof(ExternalConstOneByteStringResource) == sizeof(size_t) * 3, + "ExternalConstOneByteStringResource size was not sizeof(size_t) * 3"); + static_assert( + alignof(ExternalConstOneByteStringResource) == sizeof(size_t), + "ExternalConstOneByteStringResource align was not sizeof(size_t)"); + } + const char* data() const override { return nullptr; } + size_t length() const override { return _length; } + void Dispose() override {} + + private: + const int _length; +}; diff --git a/crates/v8/src/binding.rs b/crates/v8/src/binding.rs new file mode 100644 index 0000000000..4a1ff28269 --- /dev/null +++ b/crates/v8/src/binding.rs @@ -0,0 +1,6 @@ +#![allow(unused)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(unsafe_op_in_unsafe_fn)] +include!(env!("RUSTY_V8_SRC_BINDING_PATH")); diff --git a/crates/v8/src/context.rs b/crates/v8/src/context.rs new file mode 100644 index 0000000000..b564a53dda --- /dev/null +++ b/crates/v8/src/context.rs @@ -0,0 +1,651 @@ +use crate::isolate::RealIsolate; +use crate::scope::PinScope; +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. +use crate::Context; +use crate::Local; +use crate::MicrotaskQueue; +use crate::Object; +use crate::ObjectTemplate; +use crate::Value; +use crate::Weak; +use crate::handle::UnsafeRefHandle; +use crate::isolate::BuildTypeIdHasher; +use crate::isolate::RawSlot; +use crate::support::int; +use std::any::TypeId; +use std::collections::HashMap; +use std::ffi::c_void; +use std::ptr::NonNull; +use std::ptr::{null, null_mut}; +use std::rc::Rc; + +/// Callback for getting high resolution timestamps in Temporal, as a number of +/// nanoseconds since the Unix epoch. +/// +/// See [`Context::set_temporal_host_system_utc_epoch_nanoseconds_callback`]. +pub type TemporalHostSystemUTCEpochNanosecondsCallback = + unsafe extern "C" fn(context: Local) -> i64; + +unsafe extern "C" { + fn v8__Context__New( + isolate: *mut RealIsolate, + templ: *const ObjectTemplate, + global_object: *const Value, + microtask_queue: *mut MicrotaskQueue, + ) -> *const Context; + fn v8__Isolate__GetCurrent() -> *mut RealIsolate; + fn v8__Context__Global(this: *const Context) -> *const Object; + fn v8__Context__DetachGlobal(this: *const Context); + fn v8__Context__GetExtrasBindingObject(this: *const Context) + -> *const Object; + fn v8__Context__SetTemporalHostSystemUTCEpochNanosecondsCallback( + this: *const Context, + callback: TemporalHostSystemUTCEpochNanosecondsCallback, + ); + fn v8__Context__GetNumberOfEmbedderDataFields(this: *const Context) -> u32; + fn v8__Context__GetAlignedPointerFromEmbedderData( + this: *const Context, + index: int, + tag: u16, + ) -> *mut c_void; + fn v8__Context__SetAlignedPointerInEmbedderData( + this: *const Context, + index: int, + value: *mut c_void, + tag: u16, + ); + fn v8__Context__GetEmbedderData( + this: *const Context, + index: int, + ) -> *const Value; + fn v8__Context__SetEmbedderData( + this: *const Context, + index: int, + value: *const Value, + ); + fn v8__Context__FromSnapshot( + isolate: *mut RealIsolate, + context_snapshot_index: usize, + global_object: *const Value, + microtask_queue: *mut MicrotaskQueue, + ) -> *const Context; + pub(super) fn v8__Context__GetSecurityToken( + this: *const Context, + ) -> *const Value; + pub(super) fn v8__Context__SetSecurityToken( + this: *const Context, + value: *const Value, + ); + pub(super) fn v8__Context__UseDefaultSecurityToken(this: *const Context); + pub(super) fn v8__Context__AllowCodeGenerationFromStrings( + this: *const Context, + allow: bool, + ); + pub(super) fn v8__Context_IsCodeGenerationFromStringsAllowed( + this: *const Context, + ) -> bool; + fn v8__Context__GetMicrotaskQueue( + this: *const Context, + ) -> *const MicrotaskQueue; + fn v8__Context__SetMicrotaskQueue( + this: *const Context, + microtask_queue: *const MicrotaskQueue, + ); +} + +#[derive(Default)] +pub struct ContextOptions<'s> { + /// An optional object template from which the global object for the newly created context will + /// be created. + pub global_template: Option>, + /// An optional global object to be reused for the newly created context. This global object + /// must have been created by a previous call to Context::New with the same global template. + /// The state of the global object will be completely reset and only object identify will + /// remain. + pub global_object: Option>, + /// An optional microtask queue used to manage the microtasks created in this context. If not + /// set the per-isolate default microtask queue would be used. + pub microtask_queue: Option<*mut MicrotaskQueue>, +} + +impl Context { + const ANNEX_SLOT: int = 1; + const INTERNAL_SLOT_COUNT: int = 2; + + /// The tag used by the untagged embedder data accessors, matching V8's + /// `kEmbedderDataTypeTagDefault`. + /// + /// [`set_aligned_pointer_in_embedder_data_with_tag`]: + /// Context::set_aligned_pointer_in_embedder_data_with_tag + pub const EMBEDDER_DATA_TAG_DEFAULT: u16 = 0; + + /// The number of distinct embedder data tags V8 supports, matching V8's + /// `V8_EMBEDDER_DATA_TAG_COUNT`. Valid tags are + /// `0..EMBEDDER_DATA_TAG_COUNT`; V8 aborts on anything larger, so the + /// tagged accessors panic instead. A `static_assert` in `binding.cc` keeps + /// this in sync with V8. + pub const EMBEDDER_DATA_TAG_COUNT: u16 = 15; + + /// Creates a new context. + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_, ()>, + options: ContextOptions, + ) -> Local<'s, Context> { + // A null queue selects the isolate's default queue. + if let Some(queue) = options.microtask_queue.and_then(NonNull::new) { + unsafe { queue.as_ref() }.assert_belongs_to(scope); + } + unsafe { + scope.cast_local(|sd| { + v8__Context__New( + sd.get_isolate_ptr(), + options + .global_template + .map_or_else(null, |t| &*t as *const _), + options.global_object.map_or_else(null, |o| &*o as *const _), + options.microtask_queue.unwrap_or_else(null_mut), + ) + }) + } + .unwrap() + } + + #[inline(always)] + pub fn get_extras_binding_object<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + ) -> Local<'s, Object> { + unsafe { scope.cast_local(|_| v8__Context__GetExtrasBindingObject(self)) } + .unwrap() + } + + /// Returns the global proxy object. + /// + /// Global proxy object is a thin wrapper whose prototype points to actual + /// context's global object with the properties like Object, etc. This is done + /// that way for security reasons (for more details see + /// https://wiki.mozilla.org/Gecko:SplitWindow). + /// + /// Please note that changes to global proxy object prototype most probably + /// would break VM---v8 expects only global object as a prototype of global + /// proxy object. + #[inline(always)] + pub fn global<'s>(&self, scope: &PinScope<'s, '_, ()>) -> Local<'s, Object> { + unsafe { scope.cast_local(|_| v8__Context__Global(self)) }.unwrap() + } + + /// Detaches this context's global proxy so the global object can be reused + /// to create a new context through [`ContextOptions::global_object`]. The new + /// context must use the same global template as this context. Calling this + /// method more than once has no additional effect. + /// + /// Detaching resets the context's security token to its default unique state, + /// revoking cross-context access previously granted through a shared token. + /// + /// This also disassociates the context from its microtask queue. Pending + /// microtasks associated with this context will not run. + /// + /// A reference previously returned by [`Self::get_microtask_queue`] does not + /// keep the queue alive. With the default `v8_cppgc_microtask_queue=true`, + /// detaching removes this context's contribution to keeping the queue alive + /// after its [`crate::MicrotaskQueueHandle`] has been dropped. The handle or + /// another associated context must keep the queue alive while using any such + /// reference. + #[inline(always)] + pub fn detach_global(&self) { + unsafe { v8__Context__DetachGlobal(self) } + } + + /// Returns the microtask queue associated with this context. + /// + /// Returns `None` if the context has no microtask queue, which is the case + /// after its global object has been detached — detaching clears the native + /// context's microtask queue pointer. + /// + /// The returned reference does not own or root the queue. If the context is + /// later detached with the default `v8_cppgc_microtask_queue=true`, the + /// context no longer keeps the queue alive after its + /// [`crate::MicrotaskQueueHandle`] has been dropped. Do not use a reference + /// obtained before detachment unless the handle or another associated + /// context still keeps the queue alive. + #[inline(always)] + pub fn get_microtask_queue(&self) -> Option<&MicrotaskQueue> { + let queue = unsafe { v8__Context__GetMicrotaskQueue(self) }; + NonNull::new(queue.cast_mut()).map(|queue| unsafe { &*queue.as_ptr() }) + } + + /// Associates this context with the given microtask queue. + /// + /// # Panics + /// + /// Panics if this context has no microtask queue, i.e. if its global object + /// has been detached. V8 unconditionally dereferences the existing queue + /// when setting a new one, so a detached context cannot be re-associated + /// with a microtask queue. + #[inline(always)] + pub fn set_microtask_queue(&self, microtask_queue: &MicrotaskQueue) { + assert!( + self.get_microtask_queue().is_some(), + "cannot set a microtask queue on a context whose global object has been detached" + ); + if let Some(isolate) = crate::scope::scope_owner::current() { + let isolate = unsafe { crate::Isolate::from_non_null(isolate) }; + microtask_queue.assert_belongs_to(&isolate); + } + unsafe { + v8__Context__SetMicrotaskQueue(self, microtask_queue); + } + } + + #[inline] + #[allow(clippy::mut_from_ref)] + fn get_annex_mut( + &self, + create_if_not_present: bool, + ) -> Option<&mut ContextAnnex> { + let isolate = unsafe { v8__Isolate__GetCurrent() }; + let mut isolate = unsafe { crate::isolate::Isolate::from_raw_ptr(isolate) }; + + let num_data_fields = + unsafe { v8__Context__GetNumberOfEmbedderDataFields(self) } as int; + if num_data_fields > Self::ANNEX_SLOT { + let annex_ptr = unsafe { + v8__Context__GetAlignedPointerFromEmbedderData( + self, + Self::ANNEX_SLOT, + Self::EMBEDDER_DATA_TAG_DEFAULT, + ) + } as *mut ContextAnnex; + if !annex_ptr.is_null() { + // SAFETY: This reference doesn't outlive the Context, so it can't outlive + // the annex itself. Also, any mutations or accesses to the annex after + // its creation require a mutable reference to the context's isolate, but + // such a mutable reference is consumed by this reference during its + // lifetime. + return Some(unsafe { &mut *annex_ptr }); + } + } + + if !create_if_not_present { + return None; + } + + let annex = Box::new(ContextAnnex { + slots: Default::default(), + // Gets replaced later in the method. + self_weak: Weak::empty(&mut isolate), + }); + let annex_ptr = Box::into_raw(annex); + unsafe { + v8__Context__SetAlignedPointerInEmbedderData( + self, + Self::ANNEX_SLOT, + annex_ptr as *mut _, + Self::EMBEDDER_DATA_TAG_DEFAULT, + ); + }; + assert!( + unsafe { v8__Context__GetNumberOfEmbedderDataFields(self) } as int + > Self::ANNEX_SLOT + ); + + // Make sure to drop the annex after the context is dropped, by creating a + // weak handle with a finalizer that drops the annex, and storing the weak + // in the annex itself. + let weak = { + // SAFETY: `self` can only have been derived from a `Local` or `Global`, + // and assuming the caller is only using safe code, the `Local` or + // `Global` must still be alive, so `self_ref_handle` won't outlive it. + // We also check above that `isolate` is the context's isolate. + let self_ref_handle = unsafe { UnsafeRefHandle::new(self, &mut isolate) }; + + Weak::with_guaranteed_finalizer( + &mut isolate, + self_ref_handle, + Box::new(move || { + // SAFETY: The lifetimes of references to the annex returned by this + // method are always tied to the context, and because this is the + // context's finalizer, we know there are no living references to + // the annex. And since the finalizer is only called once, the annex + // can't have been dropped before. + let _ = unsafe { Box::from_raw(annex_ptr) }; + }), + ) + }; + + // SAFETY: This reference doesn't outlive the Context, so it can't outlive + // the annex itself. Also, any mutations or accesses to the annex after + // its creation require a mutable reference to the context's isolate, but + // such a mutable reference is consumed by this reference during its + // lifetime. + let annex_mut = unsafe { &mut *annex_ptr }; + annex_mut.self_weak = weak; + Some(annex_mut) + } + + /// Get a reference to embedder data added with [`Self::set_slot()`]. + #[inline(always)] + pub fn get_slot(&self) -> Option> { + if let Some(annex) = self.get_annex_mut(false) { + annex.slots.get(&TypeId::of::()).map(|slot| { + // SAFETY: `Self::set_slot` guarantees that only values of type T will be + // stored with T's TypeId as their key. + unsafe { slot.borrow::>().clone() } + }) + } else { + None + } + } + + /// Use with [`Context::get_slot`] and [`Context::get_slot_mut`] to associate + /// state with a Context. + /// + /// This method gives ownership of value to the Context. Exactly one object of + /// each type can be associated with a Context. If called more than once with + /// an object of the same type, the earlier version will be dropped and + /// replaced. + /// + /// Returns true if value was set without replacing an existing value. + /// + /// The value will be dropped when the context is garbage collected. + #[inline(always)] + pub fn set_slot(&self, value: Rc) -> Option> { + self + .get_annex_mut(true) + .unwrap() + .slots + .insert(TypeId::of::(), RawSlot::new(value)) + .map(|slot| { + // SAFETY: `Self::set_slot` guarantees that only values of type T will be + // stored with T's TypeId as their key. + unsafe { slot.into_inner::>() } + }) + } + + /// Removes the embedder data added with [`Self::set_slot()`] and returns it + /// if it exists. + #[inline(always)] + pub fn remove_slot(&self) -> Option> { + if let Some(annex) = self.get_annex_mut(false) { + annex.slots.remove(&TypeId::of::()).map(|slot| { + // SAFETY: `Self::set_slot` guarantees that only values of type T will be + // stored with T's TypeId as their key. + unsafe { slot.into_inner::>() } + }) + } else { + None + } + } + + /// Removes all embedder data added with [`Self::set_slot()`], and + /// deletes any internal state needed to keep track of such slots. + /// + /// This is needed to make a snapshot with + /// [`SnapshotCreator`](crate::SnapshotCreator), since the internal embedder + /// state uses [`Weak`] handles, which cannot be alive at the time of + /// snapshotting. + #[inline(always)] + pub fn clear_all_slots(&self) { + if let Some(annex_mut) = self.get_annex_mut(false) { + let annex_ptr = annex_mut as *mut ContextAnnex; + let _ = unsafe { Box::from_raw(annex_ptr) }; + unsafe { + v8__Context__SetAlignedPointerInEmbedderData( + self, + Self::ANNEX_SLOT, + null_mut(), + Self::EMBEDDER_DATA_TAG_DEFAULT, + ); + }; + } + } + + /// Sets the embedder data with the given index, growing the data as needed. + /// + /// Note that index 0 currently has a special meaning for Chrome's debugger. + #[inline(always)] + pub fn set_embedder_data(&self, slot: i32, data: Local<'_, Value>) { + // Initialize the annex when slot count > INTERNAL_SLOT_COUNT. + self.get_annex_mut(true); + + unsafe { + v8__Context__SetEmbedderData( + self, + slot + Self::INTERNAL_SLOT_COUNT, + &*data, + ); + } + } + + /// Gets the embedder data with the given index, which must have been set by + /// a previous call to SetEmbedderData with the same index. + #[inline(always)] + pub fn get_embedder_data<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + slot: i32, + ) -> Option> { + unsafe { + scope.cast_local(|_| { + v8__Context__GetEmbedderData(self, slot + Self::INTERNAL_SLOT_COUNT) + }) + } + } + + /// Sets a 2-byte-aligned native pointer in the embedder data with the given + /// index, growing the data as needed. + /// + /// This is equivalent to calling + /// [`set_aligned_pointer_in_embedder_data_with_tag`] with the default tag. + /// + /// # Safety + /// The pointer must be 2-byte aligned. + /// + /// [`set_aligned_pointer_in_embedder_data_with_tag`]: + /// Context::set_aligned_pointer_in_embedder_data_with_tag + #[inline(always)] + pub unsafe fn set_aligned_pointer_in_embedder_data( + &self, + slot: i32, + data: *mut c_void, + ) { + unsafe { + self.set_aligned_pointer_in_embedder_data_with_tag( + slot, + data, + Self::EMBEDDER_DATA_TAG_DEFAULT, + ); + } + } + + #[inline(always)] + fn assert_valid_embedder_data_tag(tag: u16) { + assert!( + tag < Self::EMBEDDER_DATA_TAG_COUNT, + "embedder data tag {tag} is out of range; must be less than {}", + Self::EMBEDDER_DATA_TAG_COUNT + ); + } + + /// Sets a 2-byte-aligned native pointer in the embedder data with the given + /// index, growing the data as needed. The `tag` distinguishes pointers of + /// different types stored in embedder data; the same tag must be passed to + /// [`get_aligned_pointer_from_embedder_data_with_tag`] when reading the + /// value back. + /// + /// Reading a slot with a tag other than the one it was written with does not + /// report an error, and what it does return depends on the build: with the + /// V8 sandbox enabled the mismatch is detected and the read returns null, + /// while with the sandbox disabled the tag is ignored entirely and the + /// stored pointer is returned regardless. Neither is a substitute for the + /// caller keeping the tags of a slot consistent. + /// + /// Use [`EMBEDDER_DATA_TAG_DEFAULT`] when no distinction between pointer + /// types is needed. + /// + /// # Panics + /// + /// Panics if `tag` is not less than [`EMBEDDER_DATA_TAG_COUNT`]. V8 aborts + /// the process for out-of-range tags, so this is checked here instead. + /// + /// # Safety + /// The pointer must be 2-byte aligned. + /// + /// [`get_aligned_pointer_from_embedder_data_with_tag`]: + /// Context::get_aligned_pointer_from_embedder_data_with_tag + /// [`EMBEDDER_DATA_TAG_DEFAULT`]: Context::EMBEDDER_DATA_TAG_DEFAULT + /// [`EMBEDDER_DATA_TAG_COUNT`]: Context::EMBEDDER_DATA_TAG_COUNT + #[inline(always)] + pub unsafe fn set_aligned_pointer_in_embedder_data_with_tag( + &self, + slot: i32, + data: *mut c_void, + tag: u16, + ) { + Self::assert_valid_embedder_data_tag(tag); + + // Initialize the annex when slot count > INTERNAL_SLOT_COUNT. + self.get_annex_mut(true); + + unsafe { + v8__Context__SetAlignedPointerInEmbedderData( + self, + slot + Self::INTERNAL_SLOT_COUNT, + data, + tag, + ); + } + } + + /// Gets a 2-byte-aligned native pointer from the embedder data with the + /// given index, which must have been set by a previous call to + /// [`set_aligned_pointer_in_embedder_data`] with the same index. + /// + /// [`set_aligned_pointer_in_embedder_data`]: + /// Context::set_aligned_pointer_in_embedder_data + #[inline(always)] + pub fn get_aligned_pointer_from_embedder_data( + &self, + slot: i32, + ) -> *mut c_void { + self.get_aligned_pointer_from_embedder_data_with_tag( + slot, + Self::EMBEDDER_DATA_TAG_DEFAULT, + ) + } + + /// Gets a 2-byte-aligned native pointer from the embedder data with the + /// given index, which must have been set by a previous call to + /// [`set_aligned_pointer_in_embedder_data_with_tag`] with the same index + /// and the same `tag`. + /// + /// If the tag does not match the one the slot was written with, this returns + /// null when the V8 sandbox is enabled and the stored pointer when it is + /// not; see [`set_aligned_pointer_in_embedder_data_with_tag`]. Do not + /// dereference the result unless the tags match. + /// + /// # Panics + /// + /// Panics if `tag` is not less than [`EMBEDDER_DATA_TAG_COUNT`]. + /// + /// [`set_aligned_pointer_in_embedder_data_with_tag`]: + /// Context::set_aligned_pointer_in_embedder_data_with_tag + /// [`EMBEDDER_DATA_TAG_COUNT`]: Context::EMBEDDER_DATA_TAG_COUNT + #[inline(always)] + pub fn get_aligned_pointer_from_embedder_data_with_tag( + &self, + slot: i32, + tag: u16, + ) -> *mut c_void { + Self::assert_valid_embedder_data_tag(tag); + + unsafe { + v8__Context__GetAlignedPointerFromEmbedderData( + self, + slot + Self::INTERNAL_SLOT_COUNT, + tag, + ) + } + } + + /// Create a new context from a (non-default) context snapshot. There + /// is no way to provide a global object template since we do not create + /// a new global object from template, but we can reuse a global object. + pub fn from_snapshot<'s>( + scope: &PinScope<'s, '_, ()>, + context_snapshot_index: usize, + options: ContextOptions, + ) -> Option> { + // A null queue selects the isolate's default queue. + if let Some(queue) = options.microtask_queue.and_then(NonNull::new) { + unsafe { queue.as_ref() }.assert_belongs_to(scope); + } + unsafe { + scope.cast_local(|sd| { + v8__Context__FromSnapshot( + sd.get_isolate_ptr(), + context_snapshot_index, + options.global_object.map_or_else(null, |o| &*o as *const _), + options.microtask_queue.unwrap_or_else(null_mut), + ) + }) + } + } + + #[inline(always)] + pub fn get_security_token<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + ) -> Local<'s, Value> { + unsafe { scope.cast_local(|_| v8__Context__GetSecurityToken(self)) } + .unwrap() + } + + #[inline(always)] + pub fn set_security_token(&self, token: Local) { + unsafe { + v8__Context__SetSecurityToken(self, &*token); + } + } + + /// Sets the callback used to obtain high resolution timestamps in Temporal. + /// + /// The callback is invoked with this context and returns the current time as + /// a number of nanoseconds since the Unix epoch. + #[inline(always)] + pub fn set_temporal_host_system_utc_epoch_nanoseconds_callback( + &self, + callback: TemporalHostSystemUTCEpochNanosecondsCallback, + ) { + unsafe { + v8__Context__SetTemporalHostSystemUTCEpochNanosecondsCallback( + self, callback, + ); + } + } + + #[inline(always)] + pub fn use_default_security_token(&self) { + unsafe { + v8__Context__UseDefaultSecurityToken(self); + } + } + + pub fn set_allow_generation_from_strings(&self, allow: bool) { + unsafe { + v8__Context__AllowCodeGenerationFromStrings(self, allow); + } + } + + pub fn is_code_generation_from_strings_allowed(&self) -> bool { + unsafe { v8__Context_IsCodeGenerationFromStringsAllowed(self) } + } +} + +struct ContextAnnex { + slots: HashMap, + // In order to run the finalizer that drops the ContextAnnex when the Context + // is GC'd, the corresponding Weak must be kept alive until that time. + self_weak: Weak, +} diff --git a/crates/v8/src/cppgc.rs b/crates/v8/src/cppgc.rs new file mode 100644 index 0000000000..671073d0ab --- /dev/null +++ b/crates/v8/src/cppgc.rs @@ -0,0 +1,771 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license + +use crate::Data; +use crate::TracedReference; +use crate::binding::RustObj; +use crate::platform::Platform; +use crate::support::Opaque; +use crate::support::SharedRef; +use crate::support::UniqueRef; +use crate::support::int; +use std::cell::UnsafeCell; +use std::ffi::CStr; +use std::ffi::c_char; +use std::marker::PhantomData; +use std::ptr::NonNull; + +unsafe extern "C" { + fn cppgc__initialize_process(platform: *mut Platform); + fn cppgc__shutdown_process(); + + fn v8__CppHeap__Create( + platform: *mut Platform, + marking_support: MarkingType, + sweeping_support: SweepingType, + ) -> *mut Heap; + fn v8__CppHeap__DELETE(heap: *mut Heap); + fn cppgc__make_garbage_collectable( + heap: *mut Heap, + size: usize, + alignment: usize, + ) -> *mut RustObj; + + fn cppgc__heap__enable_detached_garbage_collections_for_testing( + heap: *mut Heap, + ); + fn cppgc__heap__collect_garbage_for_testing( + heap: *mut Heap, + stack_state: EmbedderStackState, + ); + + fn cppgc__Visitor__Trace__Member( + visitor: *mut Visitor, + member: *const MemberInner, + ); + fn cppgc__Visitor__Trace__WeakMember( + visitor: *mut Visitor, + member: *const WeakMemberInner, + ); + fn cppgc__Visitor__Trace__TracedReference( + visitor: *mut Visitor, + reference: *const TracedReference, + ); + + fn cppgc__Member__CONSTRUCT(member: *mut MemberInner, obj: *mut RustObj); + fn cppgc__Member__DESTRUCT(member: *mut MemberInner); + fn cppgc__Member__Get(member: *const MemberInner) -> *mut RustObj; + fn cppgc__Member__Assign(member: *mut MemberInner, other: *mut RustObj); + + fn cppgc__WeakMember__CONSTRUCT( + member: *mut WeakMemberInner, + obj: *mut RustObj, + ); + fn cppgc__WeakMember__DESTRUCT(member: *mut WeakMemberInner); + fn cppgc__WeakMember__Get(member: *const WeakMemberInner) -> *mut RustObj; + fn cppgc__WeakMember__Assign( + member: *mut WeakMemberInner, + other: *mut RustObj, + ); + + fn cppgc__Persistent__CONSTRUCT(obj: *mut RustObj) -> *mut PersistentInner; + fn cppgc__Persistent__DESTRUCT(this: *mut PersistentInner); + fn cppgc__Persistent__Assign(this: *mut PersistentInner, ptr: *mut RustObj); + fn cppgc__Persistent__Get(this: *const PersistentInner) -> *mut RustObj; + + fn cppgc__WeakPersistent__CONSTRUCT( + obj: *mut RustObj, + ) -> *mut WeakPersistentInner; + fn cppgc__WeakPersistent__DESTRUCT(this: *mut WeakPersistentInner); + fn cppgc__WeakPersistent__Assign( + this: *mut WeakPersistentInner, + ptr: *mut RustObj, + ); + fn cppgc__WeakPersistent__Get( + this: *const WeakPersistentInner, + ) -> *mut RustObj; +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn rusty_v8_RustObj_trace( + obj: *const RustObj, + visitor: *mut Visitor, +) { + unsafe { + let r = get_rust_obj(obj); + r.trace(&mut *visitor); + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn rusty_v8_RustObj_get_name( + obj: *const RustObj, +) -> *const c_char { + let r = unsafe { get_rust_obj(obj) }; + r.get_name().as_ptr() +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn rusty_v8_RustObj_drop(obj: *mut RustObj) { + unsafe { + let r = get_rust_obj_mut(obj); + std::ptr::drop_in_place(r); + } +} + +/// Process-global initialization of the garbage collector. Must be called before +/// creating a Heap. +/// +/// Can be called multiple times when paired with `ShutdownProcess()`. +pub fn initialize_process(platform: SharedRef) { + unsafe { + cppgc__initialize_process(&*platform as *const Platform as *mut _); + } +} + +/// # Safety +/// +/// Must be called after destroying the last used heap. Some process-global +/// metadata may not be returned and reused upon a subsequent +/// `initialize_process()` call. +pub unsafe fn shutdown_process() { + unsafe { + cppgc__shutdown_process(); + } +} + +/// Visitor passed to trace methods. All managed pointers must have called the +/// Visitor's trace method on them. +/// +/// ``` +/// use v8::cppgc::{Member, Visitor, GarbageCollected}; +/// use std::ffi::CStr; +/// +/// struct Foo { foo: Member } +/// +/// unsafe impl GarbageCollected for Foo { +/// fn trace(&self, visitor: &mut Visitor) { +/// visitor.trace(&self.foo); +/// } +/// +/// fn get_name(&self) -> &'static std::ffi::CStr { +/// c"Foo" +/// } +/// } +/// ``` +#[repr(C)] +#[derive(Debug)] +pub struct Visitor(Opaque); + +impl Visitor { + /// Trace a managed object. + #[inline(always)] + pub fn trace(&mut self, member: &impl Traced) { + member.trace(self); + } +} + +/// Trait for inlined objects that are not allocated +/// themselves but otherwise follow managed heap layout. +pub trait Traced { + /// Called by the `Visitor` when tracing managed objects. + fn trace(&self, visitor: &mut Visitor); +} + +impl Traced for Option { + fn trace(&self, visitor: &mut Visitor) { + if let Some(value) = self { + visitor.trace(value); + } + } +} + +impl Traced for TracedReference { + fn trace(&self, visitor: &mut Visitor) { + unsafe { + cppgc__Visitor__Trace__TracedReference( + visitor, + self as *const TracedReference as *const TracedReference, + ); + } + } +} + +#[repr(C)] +pub enum EmbedderStackState { + /// Stack may contain interesting heap pointers. + MayContainHeapPointers, + /// Stack does not contain any interesting heap pointers. + NoHeapPointers, +} + +/// Specifies supported marking types. +#[repr(u8)] +pub enum MarkingType { + /// Atomic stop-the-world marking. This option does not require any write barriers but is the most intrusive in terms of jank. + Atomic, + /// Incremental marking interleaves marking with the rest of the application workload on the same thread. + Incremental, + /// Incremental and concurrent marking. + IncrementalAndConcurrent, +} + +/// Specifies supported sweeping types. +#[repr(u8)] +pub enum SweepingType { + /// Atomic stop-the-world sweeping. All of sweeping is performed at once. + Atomic, + /// Incremental sweeping interleaves sweeping with the rest of the application workload on the same thread. + Incremental, + /// Incremental and concurrent sweeping. Sweeping is split and interleaved with the rest of the application. + IncrementalAndConcurrent, +} + +pub type InternalFieldIndex = int; + +pub struct HeapCreateParams { + /// Specifies which kind of marking are supported by the heap. + pub marking_support: MarkingType, + /// Specifies which kind of sweeping are supported by the heap. + pub sweeping_support: SweepingType, +} + +impl Default for HeapCreateParams { + fn default() -> Self { + Self { + marking_support: MarkingType::IncrementalAndConcurrent, + sweeping_support: SweepingType::IncrementalAndConcurrent, + } + } +} + +/// A heap for allocating managed C++ objects. +/// +/// Similar to v8::Isolate, the heap may only be accessed from one thread at a +/// time. +#[repr(C)] +#[derive(Debug)] +pub struct Heap(Opaque); + +impl Drop for Heap { + fn drop(&mut self) { + unsafe { + v8__CppHeap__DELETE(self); + } + } +} + +impl Heap { + pub fn create( + platform: SharedRef, + params: HeapCreateParams, + ) -> UniqueRef { + unsafe { + UniqueRef::from_raw(v8__CppHeap__Create( + &*platform as *const Platform as *mut _, + params.marking_support, + params.sweeping_support, + )) + } + } + + pub unsafe fn collect_garbage_for_testing( + &self, + stack_state: EmbedderStackState, + ) { + unsafe { + cppgc__heap__collect_garbage_for_testing( + self as *const Heap as *mut _, + stack_state, + ); + } + } + + pub fn enable_detached_garbage_collections_for_testing(&self) { + unsafe { + cppgc__heap__enable_detached_garbage_collections_for_testing( + self as *const Heap as *mut _, + ); + } + } +} + +/// Base trait for objects supporting garbage collection. +/// +/// # Safety +/// +/// implementors must guarantee that the `trace()` +/// method correctly visits all [`Member`], [`WeakMember`], and +/// [`TraceReference`] pointers held by this object. Failing to do so will leave +/// dangling pointers in the heap as objects are garbage collected. +pub unsafe trait GarbageCollected { + /// `trace` must call [`Visitor::trace`] for each + /// [`Member`], [`WeakMember`], or [`TracedReference`] reachable + /// from `self`. + fn trace(&self, visitor: &mut Visitor); + + /// Specifies a name for the garbage-collected object. Such names will never + /// be hidden, as they are explicitly specified by the user of this API. + /// + /// V8 may call this function while generating a heap snapshot or at other + /// times. If V8 is currently generating a heap snapshot (according to + /// HeapProfiler::IsTakingSnapshot), then the returned string must stay alive + /// until the snapshot generation has completed. Otherwise, the returned string + /// must stay alive forever. If you need a place to store a temporary string + /// during snapshot generation, use HeapProfiler::CopyNameForHeapSnapshot. + fn get_name(&self) -> &'static CStr; +} + +#[repr(C)] +struct RustObjConcrete { + head: RustObj, + dynamic: *mut dyn GarbageCollected, + value: T, +} + +unsafe fn get_rust_obj<'s>(obj: *const RustObj) -> &'s dyn GarbageCollected { + unsafe { + let obj = &*(obj as *const RustObjConcrete<()>); + &*obj.dynamic + } +} + +unsafe fn get_rust_obj_mut<'s>( + obj: *mut RustObj, +) -> &'s mut dyn GarbageCollected { + unsafe { + let obj = &mut *(obj as *mut RustObjConcrete<()>); + &mut *obj.dynamic + } +} + +/// Constructs an instance of T, which is a garbage collected type. +/// +/// The object will be allocated on the heap and managed by cppgc. During +/// marking, the object will be traced by calling the `trace` method on it. +/// +/// During sweeping, the destructor will be called and the memory will be +/// freed. +/// +/// # Safety +/// +/// The caller must ensure that the returned pointer is always stored on +/// the stack, or is safely moved into one of the other cppgc pointer types. +pub unsafe fn make_garbage_collected( + heap: &Heap, + obj: T, +) -> UnsafePtr { + const { + // max alignment in cppgc is 16 + assert!(std::mem::align_of::() <= 16); + assert!( + std::mem::offset_of!(RustObjConcrete, dynamic) + == std::mem::offset_of!(RustObjConcrete<()>, dynamic) + ); + } + + let additional_bytes = + std::mem::size_of::>() - std::mem::size_of::(); + + let pointer = unsafe { + cppgc__make_garbage_collectable( + heap as *const Heap as *mut _, + additional_bytes, + std::mem::align_of::>(), + ) + }; + + assert!(!pointer.is_null()); + + unsafe { + let pointer = &mut *(pointer as *mut RustObjConcrete); + let value_ptr = std::ptr::addr_of_mut!(pointer.value); + value_ptr.write(obj); + std::ptr::addr_of_mut!(pointer.dynamic).write(value_ptr as _); + } + + UnsafePtr { + pointer: unsafe { NonNull::new_unchecked(pointer) }, + _phantom: PhantomData, + } +} + +/// # Safety +/// +/// T must be the correct type for this specific RustObj +unsafe fn get_value_from_rust_obj( + obj: *mut RustObj, +) -> *const T { + unsafe { + let obj = &mut *(obj as *mut RustObjConcrete); + std::ptr::addr_of_mut!(obj.value) + } +} + +/// UnsafePtr is used to refer to an on-heap object from the stack. +#[derive(Clone, Copy)] +pub struct UnsafePtr { + pointer: NonNull, + _phantom: PhantomData, +} + +impl UnsafePtr { + /// Create a new UnsafePtr. + /// + /// # Safety + /// + /// The caller must ensure that the returned pointer is always stored on + /// the stack, or is safely moved into one of the other cppgc pointer types. + pub unsafe fn new(value: &impl GetRustObj) -> Option> { + NonNull::new(value.get_rust_obj()).map(|pointer| UnsafePtr { + pointer, + _phantom: PhantomData, + }) + } + + pub unsafe fn as_ref(&self) -> &T { + unsafe { &*get_value_from_rust_obj(self.pointer.as_ptr()) } + } +} + +impl GetRustObj for UnsafePtr { + fn get_rust_obj(&self) -> *mut RustObj { + self.pointer.as_ptr() + } +} + +#[doc(hidden)] +pub trait GetRustObj { + fn get_rust_obj(&self) -> *mut RustObj; +} + +impl GetRustObj for *mut RustObj { + fn get_rust_obj(&self) -> *mut RustObj { + *self + } +} + +macro_rules! member { + ($( # $attr:tt )* $name:ident) => { + paste::paste! { + #[repr(transparent)] + struct [< $name Inner >]([u8; crate::binding:: [< cppgc__ $name _SIZE >]]); + + impl [< $name Inner >] { + fn new(ptr: *mut RustObj) -> Self { + let mut this = std::mem::MaybeUninit::uninit(); + unsafe { + [< cppgc__ $name __CONSTRUCT >](this.as_mut_ptr(), ptr); + this.assume_init() + } + } + + #[inline(always)] + fn get(&self) -> *mut RustObj { + // Member may be a compressed pointer, so just read it from C++ + unsafe { [< cppgc__ $name __Get >](self) } + } + + #[inline(always)] + fn assign(&mut self, ptr: *mut RustObj) { + // Assignment has write barriers in the GC, so call into C++ + unsafe { + [< cppgc__ $name __Assign >](self, ptr); + } + } + } + + impl Drop for [< $name Inner >] { + fn drop(&mut self) { + unsafe { + [< cppgc__ $name __DESTRUCT >](self); + } + } + } + + $( # $attr )* + #[repr(transparent)] + pub struct $name { + inner: [< $name Inner >], + _phantom: PhantomData, + } + + impl $name { + #[doc = "Create a new empty "] + #[doc = stringify!($name)] + #[doc = " which may be set later."] + pub fn empty() -> Self { + Self { + inner: [< $name Inner >]::new(std::ptr::null_mut()), + _phantom: PhantomData, + } + } + + #[doc = "Create a new "] + #[doc = stringify!($name)] + #[doc = " and initialize it with an object."] + pub fn new(other: &impl GetRustObj) -> Self { + Self { + inner: [< $name Inner >]::new(other.get_rust_obj()), + _phantom: PhantomData, + } + } + + #[doc = "Set the object pointed to by this "] + #[doc = stringify!($name)] + #[doc = "."] + pub fn set(&mut self, other: &impl GetRustObj) { + let ptr = other.get_rust_obj(); + self.inner.assign(ptr); + } + + #[doc = "Get the object pointed to by this "] + #[doc = stringify!($name)] + #[doc = ", returning `None` if the pointer is empty or has been garbage-collected."] + #[doc = ""] + #[doc = "# Safety"] + #[doc = ""] + #[doc = "The caller must ensure that this pointer is being traced correctly by appearing in the [`trace`](GarbageCollected::trace)"] + #[doc = "implementation of the object that owns the pointer. Between initializing the pointer and calling `get()`, the pointer must be reachable by the garbage collector."] + pub unsafe fn get(&self) -> Option<&T> { + let ptr = self.inner.get(); + if ptr.is_null() { + None + } else { + // SAFETY: Either this is a strong reference and the pointer is valid according + // to the safety contract of this method, or this is a weak reference and the + // ptr will be null if it was collected. + Some(unsafe { &*get_value_from_rust_obj(ptr) }) + } + } + } + + impl GetRustObj for $name { + fn get_rust_obj(&self) -> *mut RustObj { + self.inner.get() + } + } + + impl Traced for $name { + fn trace(&self, visitor: &mut Visitor) { + unsafe { [< cppgc__Visitor__Trace__ $name >](visitor, &self.inner) } + } + } + + impl std::fmt::Debug for $name { + fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { + fmt.debug_struct(stringify!($name)).finish() + } + } + } + } +} + +member! { + /// Members are used in classes to contain strong pointers to other garbage + /// collected objects. All Member fields of a class must be traced in the class' + /// trace method. + Member +} + +member! { + /// WeakMember is similar to Member in that it is used to point to other garbage + /// collected objects. However instead of creating a strong pointer to the + /// object, the WeakMember creates a weak pointer, which does not keep the + /// pointee alive. Hence if all pointers to to a heap allocated object are weak + /// the object will be garbage collected. At the time of GC the weak pointers + /// will automatically be set to null. + WeakMember +} + +macro_rules! persistent { + ($( # $attr:tt )* $name:ident) => { + paste::paste! { + // PersistentBase is extremely particular about move and copy semantics, + // so we allocate it on the heap and only interact with it via calls to C++. + #[repr(C)] + struct [< $name Inner >](Opaque); + + $( # $attr )* + pub struct $name { + inner: *mut [< $name Inner >], + _phantom: PhantomData, + } + + impl $name { + #[doc = "Create a new empty "] + #[doc = stringify!($name)] + #[doc = " which may be set later."] + pub fn empty() -> Self { + let this = unsafe { [< cppgc__ $name __CONSTRUCT >](std::ptr::null_mut()) }; + Self { + inner: this, + _phantom: PhantomData, + } + } + + #[doc = "Create a new "] + #[doc = stringify!($name)] + #[doc = " and initialize it with an object."] + pub fn new(other: &impl GetRustObj) -> Self { + let this = unsafe { [< cppgc__ $name __CONSTRUCT >](other.get_rust_obj()) }; + Self { + inner: this, + _phantom: PhantomData, + } + } + + #[doc = "Set the object pointed to by this "] + #[doc = stringify!($name)] + #[doc = "."] + pub fn set(&mut self, other: &impl GetRustObj) { + let ptr = other.get_rust_obj(); + self.assign(ptr); + } + + #[doc = "Borrow the object pointed to by this "] + #[doc = stringify!($name)] + #[doc = "."] + pub fn get(&self) -> Option<&T> { + let ptr = self.get_rust_obj(); + if ptr.is_null() { + None + } else { + // SAFETY: Either this is a strong reference and the pointer is always valid + // or this is a weak reference and the ptr will be null if it was collected. + Some(unsafe { &*get_value_from_rust_obj(ptr) }) + } + } + + #[inline(always)] + fn assign(&mut self, ptr: *mut RustObj) { + unsafe { + [< cppgc__ $name __Assign >](self.inner, ptr); + } + } + } + + impl Drop for $name { + fn drop(&mut self) { + unsafe { + [< cppgc__ $name __DESTRUCT >](self.inner); + } + } + } + + impl std::fmt::Debug for $name { + fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { + fmt.debug_struct(stringify!($name)).finish() + } + } + + impl GetRustObj for $name { + fn get_rust_obj(&self) -> *mut RustObj { + unsafe { + [< cppgc__ $name __Get >](self.inner) + } + } + } + } + }; +} + +persistent! { + /// Persistent is a way to create a strong pointer from an off-heap object to + /// another on-heap object. As long as the Persistent handle is alive the GC will + /// keep the object pointed to alive. The Persistent handle is always a GC root + /// from the point of view of the GC. Persistent must be constructed and + /// destructed in the same thread. + Persistent +} + +persistent! { + /// WeakPersistent is a way to create a weak pointer from an off-heap object to + /// an on-heap object. The pointer is automatically cleared when the pointee gets + /// collected. WeakPersistent must be constructed and destructed in the same + /// thread. + WeakPersistent +} + +/// `GcCell` is a zero-overhead memory cell that provides interior mutability +/// for garbage collected types. +/// +/// Mutable access to the value inside the `GcCell` is granted by delivering +/// proof that the caller has mutable access to the V8 Isolate (such as through +/// a [`HandleScope`](crate::HandleScope)). This statically guarantees that +/// access patterns follow the rules of both the garbage collector and the Rust +/// borrow checker. +/// +/// `GcCell` also implements [`Traced`], which means that it can hold other +/// garbage collected references, like [`Member`] or [`WeakMember`]. If the +/// `GcCell` holds other garbage collected objects, those objects cannot be +/// accessed while the `GcCell` is borrowed, and the caller must construct +/// temporary pointers ([`UnsafePtr`]) on the stack in order to access nested +/// objects in the object graph. +pub struct GcCell { + // Contents guarded by access to the `Isolate`. + value: UnsafeCell, +} + +impl std::fmt::Debug for GcCell { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("GcCell").finish() + } +} + +unsafe impl Send for GcCell {} +unsafe impl Sync for GcCell {} + +impl GcCell { + pub fn new(value: T) -> Self { + Self { + value: UnsafeCell::new(value), + } + } + + pub fn set(&self, isolate: &mut crate::Isolate, value: T) { + _ = isolate; + unsafe { + // SAFETY: The `isolate` argument is proof that we have mutable access to + // the value. + *self.value.get() = value; + } + } +} + +impl GcCell { + pub fn get<'a>(&'a self, isolate: &'a crate::Isolate) -> &'a T { + _ = isolate; + unsafe { + // SAFETY: The `isolate` argument is proof that we have access to the + // value, and the returned reference binds the isolate's lifetime. + &*self.value.get() + } + } + + pub fn get_mut<'a>(&'a self, isolate: &'a mut crate::Isolate) -> &'a mut T { + _ = isolate; + unsafe { + // SAFETY: The `isolate` argument is proof that we have mutable access to + // the value, and the returned reference binds the isolate's lifetime. + &mut *self.value.get() + } + } + + pub fn with<'a, 's, R>( + &'a self, + scope: &'a mut crate::HandleScope<'s>, + f: impl FnOnce(&'a mut crate::HandleScope<'s>, &'a mut T) -> R, + ) -> R { + f(scope, unsafe { &mut *self.value.get() }) + } +} + +impl Traced for GcCell { + fn trace(&self, visitor: &mut Visitor) { + unsafe { + // SAFETY: This is invoked by the GC, so access is guaranteed to follow + // its rules. + visitor.trace(&(*self.value.get())); + } + } +} diff --git a/crates/v8/src/crdtp.rs b/crates/v8/src/crdtp.rs new file mode 100644 index 0000000000..53d849b328 --- /dev/null +++ b/crates/v8/src/crdtp.rs @@ -0,0 +1,832 @@ +// Copyright 2024 the Deno authors. All rights reserved. MIT license. + +use crate::support::CxxVTable; +use crate::support::Opaque; +use std::cell::UnsafeCell; +use std::ffi::CString; +use std::ffi::c_void; +use std::mem::MaybeUninit; +use std::ops::Deref; +use std::ops::DerefMut; +use std::pin::Pin; +use std::ptr::NonNull; + +unsafe extern "C" { + fn crdtp__FrontendChannel__BASE__CONSTRUCT( + buf: *mut MaybeUninit, + ); + + fn crdtp__Serializable__DELETE(this: *mut RawSerializable); + fn crdtp__Serializable__AppendSerialized( + this: *const RawSerializable, + out: *mut CppVecU8, + ); + + fn crdtp__Dispatchable__new( + data: *const u8, + len: usize, + associated_data: *const u8, + associated_data_len: usize, + fallthrough_callback: *mut c_void, + ) -> *mut Dispatchable; + fn crdtp__Dispatchable__DELETE(this: *mut Dispatchable); + fn crdtp__Dispatchable__ok(this: *const Dispatchable) -> bool; + fn crdtp__Dispatchable__callId(this: *const Dispatchable) -> i32; + fn crdtp__Dispatchable__hasCallId(this: *const Dispatchable) -> bool; + fn crdtp__Dispatchable__methodLen(this: *const Dispatchable) -> usize; + fn crdtp__Dispatchable__methodCopy(this: *const Dispatchable, out: *mut u8); + fn crdtp__Dispatchable__sessionIdLen(this: *const Dispatchable) -> usize; + fn crdtp__Dispatchable__sessionIdCopy( + this: *const Dispatchable, + out: *mut u8, + ); + fn crdtp__Dispatchable__paramsLen(this: *const Dispatchable) -> usize; + fn crdtp__Dispatchable__paramsCopy(this: *const Dispatchable, out: *mut u8); + fn crdtp__Dispatchable__associatedDataLen(this: *const Dispatchable) + -> usize; + fn crdtp__Dispatchable__associatedDataCopy( + this: *const Dispatchable, + out: *mut u8, + ); + + fn crdtp__DispatchResponse__Success() -> *mut DispatchResponseWrapper; + fn crdtp__DispatchResponse__FallThrough() -> *mut DispatchResponseWrapper; + fn crdtp__DispatchResponse__ParseError( + msg: *const u8, + len: usize, + ) -> *mut DispatchResponseWrapper; + fn crdtp__DispatchResponse__InvalidRequest( + msg: *const u8, + len: usize, + ) -> *mut DispatchResponseWrapper; + fn crdtp__DispatchResponse__MethodNotFound( + msg: *const u8, + len: usize, + ) -> *mut DispatchResponseWrapper; + fn crdtp__DispatchResponse__InvalidParams( + msg: *const u8, + len: usize, + ) -> *mut DispatchResponseWrapper; + fn crdtp__DispatchResponse__ServerError( + msg: *const u8, + len: usize, + ) -> *mut DispatchResponseWrapper; + fn crdtp__DispatchResponse__DELETE(this: *mut DispatchResponseWrapper); + fn crdtp__DispatchResponse__isSuccess( + this: *const DispatchResponseWrapper, + ) -> bool; + fn crdtp__DispatchResponse__isError( + this: *const DispatchResponseWrapper, + ) -> bool; + fn crdtp__DispatchResponse__isFallThrough( + this: *const DispatchResponseWrapper, + ) -> bool; + fn crdtp__DispatchResponse__code(this: *const DispatchResponseWrapper) + -> i32; + fn crdtp__DispatchResponse__messageLen( + this: *const DispatchResponseWrapper, + ) -> usize; + fn crdtp__DispatchResponse__messageCopy( + this: *const DispatchResponseWrapper, + out: *mut u8, + ); + + fn crdtp__UberDispatcher__new( + channel: *mut RawFrontendChannel, + ) -> *mut UberDispatcher; + fn crdtp__UberDispatcher__DELETE(this: *mut UberDispatcher); + fn crdtp__UberDispatcher__channel( + this: *mut UberDispatcher, + ) -> *mut RawFrontendChannel; + fn crdtp__UberDispatcher__Dispatch( + this: *mut UberDispatcher, + dispatchable: *mut Dispatchable, + ); + + fn crdtp__vec_u8__new() -> *mut CppVecU8; + fn crdtp__vec_u8__DELETE(this: *mut CppVecU8); + fn crdtp__vec_u8__size(this: *const CppVecU8) -> usize; + fn crdtp__vec_u8__copy(this: *const CppVecU8, out: *mut u8); + + fn crdtp__json__ConvertJSONToCBOR( + json_data: *const u8, + json_len: usize, + cbor_out: *mut CppVecU8, + ) -> bool; + fn crdtp__json__ConvertCBORToJSON( + cbor_data: *const u8, + cbor_len: usize, + json_out: *mut CppVecU8, + ) -> bool; + + fn crdtp__CreateErrorResponse( + call_id: i32, + response: *mut DispatchResponseWrapper, + ) -> *mut RawSerializable; + fn crdtp__CreateResponse( + call_id: i32, + params: *mut RawSerializable, + ) -> *mut RawSerializable; + fn crdtp__CreateNotification( + method: *const std::ffi::c_char, + params: *mut RawSerializable, + ) -> *mut RawSerializable; + fn crdtp__CreateErrorNotification( + response: *mut DispatchResponseWrapper, + ) -> *mut RawSerializable; + + fn crdtp__DomainDispatcher__new( + channel: *mut RawFrontendChannel, + rust_dispatcher: *mut std::ffi::c_void, + ) -> *mut RawDomainDispatcher; + fn crdtp__DomainDispatcher__sendResponse( + this: *mut RawDomainDispatcher, + call_id: i32, + response: *mut DispatchResponseWrapper, + result: *mut RawSerializable, + ); + fn crdtp__UberDispatcher__WireBackend( + uber: *mut UberDispatcher, + domain_data: *const u8, + domain_len: usize, + dispatcher: *mut RawDomainDispatcher, + ); +} + +#[repr(C)] +pub struct Dispatchable(Opaque); + +/// An owning handle to a [`Dispatchable`]. +/// +/// The serialized message and associated data are borrowed by the underlying +/// C++ object, so this handle keeps both buffers alive for as long as the +/// dispatchable can be accessed. +pub struct OwnedDispatchable { + ptr: NonNull, + _cbor_data: Box<[u8]>, + _associated_data: Box<[u8]>, +} + +impl Deref for OwnedDispatchable { + type Target = Dispatchable; + + fn deref(&self) -> &Self::Target { + unsafe { self.ptr.as_ref() } + } +} + +impl DerefMut for OwnedDispatchable { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { self.ptr.as_mut() } + } +} + +impl AsRef for OwnedDispatchable { + fn as_ref(&self) -> &Dispatchable { + self + } +} + +impl AsMut for OwnedDispatchable { + fn as_mut(&mut self) -> &mut Dispatchable { + self + } +} + +impl Drop for OwnedDispatchable { + fn drop(&mut self) { + unsafe { + crdtp__Dispatchable__DELETE(self.ptr.as_ptr()); + } + } +} + +#[repr(C)] +struct DispatchResponseWrapper(Opaque); + +#[repr(C)] +pub struct UberDispatcher(Opaque); + +#[repr(C)] +pub(crate) struct CppVecU8(Opaque); + +impl CppVecU8 { + pub(crate) unsafe fn take_from_raw(ptr: *mut Self) -> Vec { + unsafe { + let len = crdtp__vec_u8__size(ptr); + let mut result = vec![0u8; len]; + crdtp__vec_u8__copy(ptr, result.as_mut_ptr()); + crdtp__vec_u8__DELETE(ptr); + result + } + } +} + +#[repr(C)] +struct RawSerializable(Opaque); + +#[repr(C)] +struct RawDomainDispatcher(Opaque); + +pub struct Serializable { + ptr: *mut RawSerializable, +} + +impl Serializable { + pub fn to_bytes(&self) -> Vec { + unsafe { + let vec = crdtp__vec_u8__new(); + crdtp__Serializable__AppendSerialized(self.ptr, vec); + CppVecU8::take_from_raw(vec) + } + } + + fn into_raw(self) -> *mut RawSerializable { + let ptr = self.ptr; + std::mem::forget(self); + ptr + } +} + +impl Drop for Serializable { + fn drop(&mut self) { + unsafe { + crdtp__Serializable__DELETE(self.ptr); + } + } +} + +impl Dispatchable { + #[allow(clippy::new_ret_no_self)] + pub fn new(cbor_data: &[u8]) -> OwnedDispatchable { + Self::new_inner(cbor_data, &[], std::ptr::null_mut()) + } + + /// Creates a dispatchable with per-message associated data and a callback + /// that receives commands which fall through the dispatcher. + /// + /// The callback may outlive this `Dispatchable` when an asynchronous command + /// takes ownership of it. + pub fn new_with_fallthrough( + cbor_data: &[u8], + associated_data: &[u8], + fallthrough_callback: impl FnMut(i32, &[u8], &[u8], &[u8]) + 'static, + ) -> OwnedDispatchable { + let callback = Box::new(FallthroughCallbackData { + callback: Box::new(fallthrough_callback), + }); + let callback = Box::into_raw(callback).cast::(); + Self::new_inner(cbor_data, associated_data, callback) + } + + fn new_inner( + cbor_data: &[u8], + associated_data: &[u8], + fallthrough_callback: *mut c_void, + ) -> OwnedDispatchable { + let cbor_data: Box<[u8]> = cbor_data.into(); + let associated_data: Box<[u8]> = associated_data.into(); + unsafe { + let ptr = crdtp__Dispatchable__new( + cbor_data.as_ptr(), + cbor_data.len(), + associated_data.as_ptr(), + associated_data.len(), + fallthrough_callback, + ); + OwnedDispatchable { + ptr: NonNull::new(ptr).unwrap(), + _cbor_data: cbor_data, + _associated_data: associated_data, + } + } + } + + pub fn ok(&self) -> bool { + unsafe { crdtp__Dispatchable__ok(self) } + } + + pub fn call_id(&self) -> i32 { + unsafe { crdtp__Dispatchable__callId(self) } + } + + pub fn has_call_id(&self) -> bool { + unsafe { crdtp__Dispatchable__hasCallId(self) } + } + + pub fn method(&self) -> Vec { + unsafe { + let len = crdtp__Dispatchable__methodLen(self); + let mut buf = vec![0u8; len]; + crdtp__Dispatchable__methodCopy(self, buf.as_mut_ptr()); + buf + } + } + + pub fn method_str(&self) -> String { + String::from_utf8_lossy(&self.method()).into_owned() + } + + pub fn session_id(&self) -> Vec { + unsafe { + let len = crdtp__Dispatchable__sessionIdLen(self); + let mut buf = vec![0u8; len]; + crdtp__Dispatchable__sessionIdCopy(self, buf.as_mut_ptr()); + buf + } + } + + pub fn params(&self) -> Vec { + unsafe { + let len = crdtp__Dispatchable__paramsLen(self); + let mut buf = vec![0u8; len]; + crdtp__Dispatchable__paramsCopy(self, buf.as_mut_ptr()); + buf + } + } + + pub fn associated_data(&self) -> Vec { + unsafe { + let len = crdtp__Dispatchable__associatedDataLen(self); + let mut buf = vec![0u8; len]; + crdtp__Dispatchable__associatedDataCopy(self, buf.as_mut_ptr()); + buf + } + } +} + +type FallthroughCallback = Box; + +struct FallthroughCallbackData { + callback: FallthroughCallback, +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn crdtp__FallthroughCallback__Run( + callback: *mut c_void, + call_id: i32, + method_data: *const u8, + method_len: usize, + message_data: *const u8, + message_len: usize, + associated_data: *const u8, + associated_data_len: usize, +) { + unsafe { + unsafe fn as_slice<'a>(data: *const u8, len: usize) -> &'a [u8] { + if len == 0 { + &[] + } else { + unsafe { std::slice::from_raw_parts(data, len) } + } + } + + let callback = &mut *(callback as *mut FallthroughCallbackData); + (callback.callback)( + call_id, + as_slice(method_data, method_len), + as_slice(message_data, message_len), + as_slice(associated_data, associated_data_len), + ); + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn crdtp__FallthroughCallback__Drop(callback: *mut c_void) { + unsafe { + drop(Box::from_raw(callback as *mut FallthroughCallbackData)); + } +} + +pub struct DispatchResponse { + ptr: *mut DispatchResponseWrapper, +} + +impl DispatchResponse { + pub fn success() -> Self { + unsafe { + Self { + ptr: crdtp__DispatchResponse__Success(), + } + } + } + + pub fn fall_through() -> Self { + unsafe { + Self { + ptr: crdtp__DispatchResponse__FallThrough(), + } + } + } + + pub fn parse_error(message: &str) -> Self { + unsafe { + Self { + ptr: crdtp__DispatchResponse__ParseError( + message.as_ptr(), + message.len(), + ), + } + } + } + + pub fn invalid_request(message: &str) -> Self { + unsafe { + Self { + ptr: crdtp__DispatchResponse__InvalidRequest( + message.as_ptr(), + message.len(), + ), + } + } + } + + pub fn method_not_found(message: &str) -> Self { + unsafe { + Self { + ptr: crdtp__DispatchResponse__MethodNotFound( + message.as_ptr(), + message.len(), + ), + } + } + } + + pub fn invalid_params(message: &str) -> Self { + unsafe { + Self { + ptr: crdtp__DispatchResponse__InvalidParams( + message.as_ptr(), + message.len(), + ), + } + } + } + + pub fn server_error(message: &str) -> Self { + unsafe { + Self { + ptr: crdtp__DispatchResponse__ServerError( + message.as_ptr(), + message.len(), + ), + } + } + } + + pub fn is_success(&self) -> bool { + unsafe { crdtp__DispatchResponse__isSuccess(self.ptr) } + } + + pub fn is_error(&self) -> bool { + unsafe { crdtp__DispatchResponse__isError(self.ptr) } + } + + /// Returns true if this is a fall-through response. + pub fn is_fall_through(&self) -> bool { + unsafe { crdtp__DispatchResponse__isFallThrough(self.ptr) } + } + + /// Get the error code. + pub fn code(&self) -> i32 { + unsafe { crdtp__DispatchResponse__code(self.ptr) } + } + + /// Get the error message. + pub fn message(&self) -> String { + unsafe { + let len = crdtp__DispatchResponse__messageLen(self.ptr); + let mut buf = vec![0u8; len]; + crdtp__DispatchResponse__messageCopy(self.ptr, buf.as_mut_ptr()); + String::from_utf8_lossy(&buf).into_owned() + } + } + + fn into_raw(self) -> *mut DispatchResponseWrapper { + let ptr = self.ptr; + std::mem::forget(self); + ptr + } +} + +impl Drop for DispatchResponse { + fn drop(&mut self) { + unsafe { + crdtp__DispatchResponse__DELETE(self.ptr); + } + } +} + +/// Trait for sending protocol responses and notifications to clients. +pub trait FrontendChannelImpl { + /// Send a response to a protocol request. + fn send_protocol_response(&mut self, call_id: i32, message: Serializable); + /// Send a notification (no call_id). + fn send_protocol_notification(&mut self, message: Serializable); + /// Flush any queued notifications. + fn flush_protocol_notifications(&mut self); +} + +#[repr(C)] +struct RawFrontendChannel { + _cxx_vtable: CxxVTable, +} + +/// Wraps a Rust `FrontendChannelImpl` for use with the C++ dispatcher. +pub struct FrontendChannel { + raw: UnsafeCell, + imp: Box, +} + +impl FrontendChannel { + /// Create a new FrontendChannel wrapping the given implementation. + pub fn new(imp: Box) -> Pin> { + let channel = Box::new(Self { + raw: UnsafeCell::new(unsafe { MaybeUninit::zeroed().assume_init() }), + imp, + }); + unsafe { + crdtp__FrontendChannel__BASE__CONSTRUCT( + channel.raw.get() as *mut _ as *mut MaybeUninit + ); + } + Box::into_pin(channel) + } + + fn raw_ptr(&self) -> *mut RawFrontendChannel { + self.raw.get() + } + + unsafe fn from_raw<'a>(ptr: *mut RawFrontendChannel) -> &'a mut Self { + unsafe { + let channel_ptr = ptr as *mut u8; + let offset = std::mem::offset_of!(FrontendChannel, raw); + let self_ptr = channel_ptr.sub(offset) as *mut Self; + &mut *self_ptr + } + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn crdtp__FrontendChannel__BASE__sendProtocolResponse( + this: *mut RawFrontendChannel, + call_id: i32, + message: *mut RawSerializable, +) { + unsafe { + let channel = FrontendChannel::from_raw(this); + let msg = Serializable { ptr: message }; + channel.imp.send_protocol_response(call_id, msg); + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn crdtp__FrontendChannel__BASE__sendProtocolNotification( + this: *mut RawFrontendChannel, + message: *mut RawSerializable, +) { + unsafe { + let channel = FrontendChannel::from_raw(this); + let msg = Serializable { ptr: message }; + channel.imp.send_protocol_notification(msg); + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn crdtp__FrontendChannel__BASE__flushProtocolNotifications( + this: *mut RawFrontendChannel, +) { + unsafe { + let channel = FrontendChannel::from_raw(this); + channel.imp.flush_protocol_notifications(); + } +} + +impl UberDispatcher { + /// Create a new UberDispatcher with the given frontend channel. + pub fn new(channel: &Pin>) -> Box { + unsafe { + let ptr = crdtp__UberDispatcher__new(channel.raw_ptr()); + Box::from_raw(ptr) + } + } + + /// Dispatch a protocol message immediately. + pub fn dispatch(&mut self, dispatchable: &mut Dispatchable) { + unsafe { + crdtp__UberDispatcher__Dispatch(self, dispatchable); + } + } +} + +impl Drop for UberDispatcher { + fn drop(&mut self) { + unsafe { + crdtp__UberDispatcher__DELETE(self); + } + } +} + +/// Convert JSON bytes to CBOR bytes. +pub fn json_to_cbor(json: &[u8]) -> Option> { + unsafe { + let vec = crdtp__vec_u8__new(); + let ok = crdtp__json__ConvertJSONToCBOR(json.as_ptr(), json.len(), vec); + if ok { + Some(CppVecU8::take_from_raw(vec)) + } else { + crdtp__vec_u8__DELETE(vec); + None + } + } +} + +/// Convert CBOR bytes to JSON bytes. +pub fn cbor_to_json(cbor: &[u8]) -> Option> { + unsafe { + let vec = crdtp__vec_u8__new(); + let ok = crdtp__json__ConvertCBORToJSON(cbor.as_ptr(), cbor.len(), vec); + if ok { + Some(CppVecU8::take_from_raw(vec)) + } else { + crdtp__vec_u8__DELETE(vec); + None + } + } +} + +/// Create an error response message. +pub fn create_error_response( + call_id: i32, + response: DispatchResponse, +) -> Serializable { + unsafe { + let ptr = crdtp__CreateErrorResponse(call_id, response.into_raw()); + Serializable { ptr } + } +} + +/// Create an error notification message. +pub fn create_error_notification(response: DispatchResponse) -> Serializable { + unsafe { + let ptr = crdtp__CreateErrorNotification(response.into_raw()); + Serializable { ptr } + } +} + +/// Create a success response message with optional result params. +pub fn create_response( + call_id: i32, + params: Option, +) -> Serializable { + unsafe { + let params_ptr = match params { + Some(p) => p.into_raw(), + None => std::ptr::null_mut(), + }; + let ptr = crdtp__CreateResponse(call_id, params_ptr); + Serializable { ptr } + } +} + +/// Create a notification message with a method name and optional params. +/// +/// # Panics +/// Panics if `method` contains interior null bytes. +pub fn create_notification( + method: &str, + params: Option, +) -> Serializable { + unsafe { + let method_cstr = + CString::new(method).expect("method name must not contain null bytes"); + let params_ptr = match params { + Some(p) => p.into_raw(), + None => std::ptr::null_mut(), + }; + let ptr = crdtp__CreateNotification(method_cstr.as_ptr(), params_ptr); + Serializable { ptr } + } +} + +/// Trait for implementing a domain-specific protocol dispatcher. +/// +/// The `dispatch` method executes the command immediately and returns whether +/// the command was handled. +pub trait DomainDispatcherImpl { + fn dispatch( + &mut self, + command: &[u8], + dispatchable: &Dispatchable, + handle: &DomainDispatcherHandle, + ) -> bool; +} + +/// Handle to the C++ DomainDispatcher, used to send responses. +pub struct DomainDispatcherHandle { + ptr: *mut RawDomainDispatcher, +} + +impl DomainDispatcherHandle { + /// Send a response for a dispatched command. + pub fn send_response( + &self, + call_id: i32, + response: DispatchResponse, + result: Option, + ) { + unsafe { + let result_ptr = match result { + Some(r) => r.into_raw(), + None => std::ptr::null_mut(), + }; + crdtp__DomainDispatcher__sendResponse( + self.ptr, + call_id, + response.into_raw(), + result_ptr, + ); + } + } +} + +/// A domain dispatcher that delegates to a Rust `DomainDispatcherImpl`. +/// +/// Ownership model: the Rust `DomainDispatcher` is heap-allocated and its +/// pointer is stored in the C++ `crdtp__DomainDispatcher__BASE`. When the +/// C++ side is destroyed (by `UberDispatcher`'s destructor), it calls back +/// into Rust via `crdtp__DomainDispatcher__BASE__Drop` to free the Rust +/// allocation. +struct DomainDispatcherData { + ptr: *mut RawDomainDispatcher, + imp: Box, + domain_bytes: Vec, +} + +pub struct DomainDispatcher; + +impl DomainDispatcher { + /// Wire a Rust domain dispatcher implementation to an `UberDispatcher`. + /// + /// The implementation will handle commands for the given `domain` name. + /// Ownership of `imp` is transferred to the C++ `UberDispatcher`; it + /// will be dropped when the `UberDispatcher` is destroyed. + pub fn wire( + uber: &mut UberDispatcher, + domain: &str, + imp: Box, + ) { + // Keep domain bytes alive as long as the DomainDispatcherData, since + // UberDispatcher stores domain as a span (pointer + length). + let domain_bytes = domain.as_bytes().to_vec(); + + let mut dd = Box::new(DomainDispatcherData { + ptr: std::ptr::null_mut(), + imp, + domain_bytes, + }); + + unsafe { + let rust_ptr = + &mut *dd as *mut DomainDispatcherData as *mut std::ffi::c_void; + let channel = crdtp__UberDispatcher__channel(uber); + let raw = crdtp__DomainDispatcher__new(channel, rust_ptr); + dd.ptr = raw; + + crdtp__UberDispatcher__WireBackend( + uber, + dd.domain_bytes.as_ptr(), + dd.domain_bytes.len(), + raw, + ); + } + + // Transfer ownership to the C++ side. The C++ destructor will call + // crdtp__DomainDispatcher__BASE__Drop to reclaim this allocation. + let _ = Box::into_raw(dd); + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn crdtp__DomainDispatcher__BASE__Dispatch( + rust_dispatcher: *mut std::ffi::c_void, + command_data: *const u8, + command_len: usize, + dispatchable: &Dispatchable, +) -> bool { + unsafe { + let dd = &mut *(rust_dispatcher as *mut DomainDispatcherData); + let command = std::slice::from_raw_parts(command_data, command_len); + let handle = DomainDispatcherHandle { ptr: dd.ptr }; + dd.imp.dispatch(command, dispatchable, &handle) + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn crdtp__DomainDispatcher__BASE__Drop( + rust_dispatcher: *mut std::ffi::c_void, +) { + unsafe { + drop(Box::from_raw(rust_dispatcher as *mut DomainDispatcherData)); + } +} diff --git a/crates/v8/src/crdtp_binding.cc b/crates/v8/src/crdtp_binding.cc new file mode 100644 index 0000000000..6b506f6e34 --- /dev/null +++ b/crates/v8/src/crdtp_binding.cc @@ -0,0 +1,384 @@ +// Copyright 2024 the Deno authors. All rights reserved. MIT license. + +#include + +#include "support.h" +#include "v8-inspector-protocol.h" +#include "v8/third_party/inspector_protocol/crdtp/cbor.h" +#include "v8/third_party/inspector_protocol/crdtp/dispatch.h" +#include "v8/third_party/inspector_protocol/crdtp/frontend_channel.h" +#include "v8/third_party/inspector_protocol/crdtp/json.h" +#include "v8/third_party/inspector_protocol/crdtp/parser_handler.h" + +using namespace support; +using namespace v8_crdtp; + +extern "C" { +void crdtp__FrontendChannel__BASE__sendProtocolResponse(FrontendChannel* self, + int call_id, + Serializable* message); +void crdtp__FrontendChannel__BASE__sendProtocolNotification( + FrontendChannel* self, Serializable* message); +void crdtp__FrontendChannel__BASE__flushProtocolNotifications( + FrontendChannel* self); +void crdtp__FallthroughCallback__Run( + void* callback, int call_id, const uint8_t* method_data, size_t method_len, + const uint8_t* message_data, size_t message_len, + const uint8_t* associated_data, size_t associated_data_len); +void crdtp__FallthroughCallback__Drop(void* callback); +} // extern "C" + +struct crdtp__FrontendChannel__BASE : public FrontendChannel { + void SendProtocolResponse(int call_id, + std::unique_ptr message) override { + crdtp__FrontendChannel__BASE__sendProtocolResponse(this, call_id, + message.release()); + } + void SendProtocolNotification( + std::unique_ptr message) override { + crdtp__FrontendChannel__BASE__sendProtocolNotification(this, + message.release()); + } + void FlushProtocolNotifications() override { + crdtp__FrontendChannel__BASE__flushProtocolNotifications(this); + } +}; + +static_assert(sizeof(crdtp__FrontendChannel__BASE) <= sizeof(void*), + "FrontendChannel BASE must fit in a single vtable pointer"); + +extern "C" { + +void crdtp__FrontendChannel__BASE__CONSTRUCT( + uninit_t* buf) { + construct_in_place(buf); +} + +void crdtp__Serializable__DELETE(Serializable* self) { delete self; } + +// Serialize once into a caller-provided CppVecU8 to avoid double-serialization. +void crdtp__Serializable__AppendSerialized(const Serializable* self, + std::vector* out) { + self->AppendSerialized(out); +} + +Dispatchable* crdtp__Dispatchable__new(const uint8_t* data, size_t len, + const uint8_t* associated_data, + size_t associated_data_len, + void* rust_callback) { + FallthroughCallback fallthrough_callback; + if (rust_callback) { + auto callback = + std::shared_ptr(rust_callback, crdtp__FallthroughCallback__Drop); + fallthrough_callback = [callback]( + int call_id, span method, + span serialized_message, + std::string_view fallthrough_associated_data) { + crdtp__FallthroughCallback__Run( + callback.get(), call_id, method.data(), method.size(), + serialized_message.data(), serialized_message.size(), + reinterpret_cast(fallthrough_associated_data.data()), + fallthrough_associated_data.size()); + }; + } + return new Dispatchable( + span(data, len), + std::string_view(reinterpret_cast(associated_data), + associated_data_len), + std::move(fallthrough_callback)); +} + +void crdtp__Dispatchable__DELETE(Dispatchable* self) { delete self; } + +bool crdtp__Dispatchable__ok(const Dispatchable* self) { return self->ok(); } + +int32_t crdtp__Dispatchable__callId(const Dispatchable* self) { + return self->CallId(); +} + +bool crdtp__Dispatchable__hasCallId(const Dispatchable* self) { + return self->HasCallId(); +} + +size_t crdtp__Dispatchable__methodLen(const Dispatchable* self) { + return self->Method().size(); +} + +void crdtp__Dispatchable__methodCopy(const Dispatchable* self, uint8_t* out) { + span method = self->Method(); + memcpy(out, method.data(), method.size()); +} + +size_t crdtp__Dispatchable__sessionIdLen(const Dispatchable* self) { + return self->SessionId().size(); +} + +void crdtp__Dispatchable__sessionIdCopy(const Dispatchable* self, + uint8_t* out) { + span session_id = self->SessionId(); + memcpy(out, session_id.data(), session_id.size()); +} + +size_t crdtp__Dispatchable__paramsLen(const Dispatchable* self) { + return self->Params().size(); +} + +void crdtp__Dispatchable__paramsCopy(const Dispatchable* self, uint8_t* out) { + span params = self->Params(); + memcpy(out, params.data(), params.size()); +} + +size_t crdtp__Dispatchable__associatedDataLen(const Dispatchable* self) { + return self->AssociatedData().size(); +} + +void crdtp__Dispatchable__associatedDataCopy(const Dispatchable* self, + uint8_t* out) { + std::string_view associated_data = self->AssociatedData(); + memcpy(out, associated_data.data(), associated_data.size()); +} + +struct DispatchResponseWrapper { + DispatchResponse inner; + + explicit DispatchResponseWrapper(DispatchResponse&& r) + : inner(std::move(r)) {} +}; + +DispatchResponseWrapper* crdtp__DispatchResponse__Success() { + return new DispatchResponseWrapper(DispatchResponse::Success()); +} + +DispatchResponseWrapper* crdtp__DispatchResponse__FallThrough() { + return new DispatchResponseWrapper(DispatchResponse::FallThrough()); +} + +DispatchResponseWrapper* crdtp__DispatchResponse__ParseError(const char* msg, + size_t len) { + return new DispatchResponseWrapper( + DispatchResponse::ParseError(std::string(msg, len))); +} + +DispatchResponseWrapper* crdtp__DispatchResponse__InvalidRequest( + const char* msg, size_t len) { + return new DispatchResponseWrapper( + DispatchResponse::InvalidRequest(std::string(msg, len))); +} + +DispatchResponseWrapper* crdtp__DispatchResponse__MethodNotFound( + const char* msg, size_t len) { + return new DispatchResponseWrapper( + DispatchResponse::MethodNotFound(std::string(msg, len))); +} + +DispatchResponseWrapper* crdtp__DispatchResponse__InvalidParams(const char* msg, + size_t len) { + return new DispatchResponseWrapper( + DispatchResponse::InvalidParams(std::string(msg, len))); +} + +DispatchResponseWrapper* crdtp__DispatchResponse__ServerError(const char* msg, + size_t len) { + return new DispatchResponseWrapper( + DispatchResponse::ServerError(std::string(msg, len))); +} + +void crdtp__DispatchResponse__DELETE(DispatchResponseWrapper* self) { + delete self; +} + +bool crdtp__DispatchResponse__isSuccess(const DispatchResponseWrapper* self) { + return self->inner.IsSuccess(); +} + +bool crdtp__DispatchResponse__isError(const DispatchResponseWrapper* self) { + return self->inner.IsError(); +} + +bool crdtp__DispatchResponse__isFallThrough( + const DispatchResponseWrapper* self) { + return self->inner.IsFallThrough(); +} + +int crdtp__DispatchResponse__code(const DispatchResponseWrapper* self) { + return static_cast(self->inner.Code()); +} + +size_t crdtp__DispatchResponse__messageLen( + const DispatchResponseWrapper* self) { + return self->inner.Message().size(); +} + +void crdtp__DispatchResponse__messageCopy(const DispatchResponseWrapper* self, + char* out) { + const std::string& msg = self->inner.Message(); + memcpy(out, msg.data(), msg.size()); +} + +UberDispatcher* crdtp__UberDispatcher__new(FrontendChannel* channel) { + return new UberDispatcher(channel); +} + +void crdtp__UberDispatcher__DELETE(UberDispatcher* self) { delete self; } + +FrontendChannel* crdtp__UberDispatcher__channel(UberDispatcher* self) { + return self->channel(); +} + +void crdtp__UberDispatcher__Dispatch(UberDispatcher* self, + Dispatchable* dispatchable) { + self->Dispatch(*dispatchable); +} + +// Convert JSON to CBOR +bool crdtp__json__ConvertJSONToCBOR(const uint8_t* json_data, size_t json_len, + std::vector* cbor_out) { + json::ConvertJSONToCBOR(span(json_data, json_len), cbor_out); + return !cbor_out->empty(); +} + +// Convert CBOR to JSON +bool crdtp__json__ConvertCBORToJSON(const uint8_t* cbor_data, size_t cbor_len, + std::vector* json_out) { + std::string json_str; + Status status = + json::ConvertCBORToJSON(span(cbor_data, cbor_len), &json_str); + if (!status.ok()) { + return false; + } + json_out->assign(json_str.begin(), json_str.end()); + return true; +} + +std::vector* crdtp__vec_u8__new() { + return new std::vector(); +} + +std::vector* v8_inspector__RemoteObject__toBytes( + const v8_inspector::protocol::Runtime::API::RemoteObject* self) { + auto* bytes = crdtp__vec_u8__new(); + self->AppendSerialized(bytes); + return bytes; +} + +void crdtp__vec_u8__DELETE(std::vector* self) { delete self; } + +size_t crdtp__vec_u8__size(const std::vector* self) { + return self->size(); +} + +void crdtp__vec_u8__copy(const std::vector* self, uint8_t* out) { + memcpy(out, self->data(), self->size()); +} + +Serializable* crdtp__CreateErrorResponse(int call_id, + DispatchResponseWrapper* response) { + return CreateErrorResponse(call_id, std::move(response->inner)).release(); +} + +Serializable* crdtp__CreateResponse(int call_id, Serializable* params) { + std::unique_ptr params_ptr(params); + return CreateResponse(call_id, std::move(params_ptr)).release(); +} + +// Owns a copy of the method string, since upstream Notification stores only +// a raw const char* pointer which leads to use-after-free when the Rust +// CString is dropped before AppendSerialized is called. +class OwnedNotification : public Serializable { + public: + OwnedNotification(const char* method, std::unique_ptr params) + : method_(method), params_(std::move(params)) {} + + void AppendSerialized(std::vector* out) const override { + Status status; + std::unique_ptr encoder = cbor::NewCBOREncoder(out, &status); + encoder->HandleMapBegin(); + encoder->HandleString8(SpanFrom("method")); + encoder->HandleString8(SpanFrom(method_)); + encoder->HandleString8(SpanFrom("params")); + if (params_) { + params_->AppendSerialized(out); + } else { + encoder->HandleMapBegin(); + encoder->HandleMapEnd(); + } + encoder->HandleMapEnd(); + assert(status.ok()); + } + + private: + std::string method_; + std::unique_ptr params_; +}; + +Serializable* crdtp__CreateNotification(const char* method, + Serializable* params) { + std::unique_ptr params_ptr(params); + return new OwnedNotification(method, std::move(params_ptr)); +} + +Serializable* crdtp__CreateErrorNotification( + DispatchResponseWrapper* response) { + return CreateErrorNotification(std::move(response->inner)).release(); +} + +} // extern "C" + +// DomainDispatcher binding - allows Rust to implement domain dispatchers. + +extern "C" { +// Rust callback: given a domain dispatcher pointer and a command name, +// executes the command and returns whether it was handled. +bool crdtp__DomainDispatcher__BASE__Dispatch(void* rust_dispatcher, + const uint8_t* command_data, + size_t command_len, + const Dispatchable& dispatchable); +// Rust callback: destroy the Rust DomainDispatcher when C++ side is destroyed. +void crdtp__DomainDispatcher__BASE__Drop(void* rust_dispatcher); +} + +struct crdtp__DomainDispatcher__BASE : public DomainDispatcher { + void* rust_dispatcher_; + + crdtp__DomainDispatcher__BASE(FrontendChannel* channel, void* rust_dispatcher) + : DomainDispatcher(channel), rust_dispatcher_(rust_dispatcher) {} + + ~crdtp__DomainDispatcher__BASE() override { + crdtp__DomainDispatcher__BASE__Drop(rust_dispatcher_); + } + + bool Dispatch(span command_name, + Dispatchable& dispatchable) override { + return crdtp__DomainDispatcher__BASE__Dispatch( + rust_dispatcher_, command_name.data(), command_name.size(), + dispatchable); + } +}; + +extern "C" { + +crdtp__DomainDispatcher__BASE* crdtp__DomainDispatcher__new( + FrontendChannel* channel, void* rust_dispatcher) { + return new crdtp__DomainDispatcher__BASE(channel, rust_dispatcher); +} + +void crdtp__DomainDispatcher__sendResponse(crdtp__DomainDispatcher__BASE* self, + int call_id, + DispatchResponseWrapper* response, + Serializable* result) { + std::unique_ptr result_ptr(result); + self->sendResponse(call_id, std::move(response->inner), + std::move(result_ptr)); + delete response; +} + +void crdtp__UberDispatcher__WireBackend( + UberDispatcher* uber, const uint8_t* domain_data, size_t domain_len, + crdtp__DomainDispatcher__BASE* dispatcher) { + std::unique_ptr dispatcher_ptr(dispatcher); + uber->WireBackend(span(domain_data, domain_len), + std::vector, span>>(), + std::move(dispatcher_ptr)); +} + +} // extern "C" diff --git a/crates/v8/src/data.rs b/crates/v8/src/data.rs new file mode 100644 index 0000000000..c4fc90867b --- /dev/null +++ b/crates/v8/src/data.rs @@ -0,0 +1,1743 @@ +// Copyright 2019-2022 the Deno authors. All rights reserved. MIT license. + +use std::any::type_name; +use std::cell::Cell; +use std::convert::From; +use std::convert::TryFrom; +use std::error::Error; +use std::fmt; +use std::fmt::Display; +use std::fmt::Formatter; +use std::hash::Hash; +use std::hash::Hasher; +use std::marker::PhantomData; +use std::mem::transmute; +use std::ops::Deref; + +use crate::Local; + +// V8 heap objects may only be accessed on their isolate's thread (or on the +// thread currently holding its Locker). Keep the opaque payload `Send` but not +// `Sync`, so a shared reference obtained from a Local cannot cross threads. +#[repr(C)] +#[derive(Clone, Copy, Debug)] +struct Opaque { + _ffi: [u8; 0], + _not_sync: PhantomData>, +} + +unsafe extern "C" { + fn v8__Data__EQ(this: *const Data, other: *const Data) -> bool; + fn v8__Data__IsBigInt(this: *const Data) -> bool; + fn v8__Data__IsBoolean(this: *const Data) -> bool; + fn v8__Data__IsContext(this: *const Data) -> bool; + fn v8__Data__IsDictionaryTemplate(this: *const Data) -> bool; + fn v8__Data__IsFixedArray(this: *const Data) -> bool; + fn v8__Data__IsFunctionTemplate(this: *const Data) -> bool; + fn v8__Data__IsModule(this: *const Data) -> bool; + fn v8__Data__IsModuleRequest(this: *const Data) -> bool; + fn v8__Data__IsName(this: *const Data) -> bool; + fn v8__Data__IsNumber(this: *const Data) -> bool; + fn v8__Data__IsObjectTemplate(this: *const Data) -> bool; + fn v8__Data__IsPrimitive(this: *const Data) -> bool; + fn v8__Data__IsPrivate(this: *const Data) -> bool; + fn v8__Data__IsString(this: *const Data) -> bool; + fn v8__Data__IsSymbol(this: *const Data) -> bool; + fn v8__Data__IsValue(this: *const Data) -> bool; + + fn v8__Value__SameValue(this: *const Value, other: *const Value) -> bool; + fn v8__Value__StrictEquals(this: *const Value, other: *const Value) -> bool; +} + +impl Data { + /// Returns true if this data is a `BigInt`. + #[inline(always)] + pub fn is_big_int(&self) -> bool { + unsafe { v8__Data__IsBigInt(self) } + } + + /// Returns true if this data is a `Boolean`. + #[inline(always)] + pub fn is_boolean(&self) -> bool { + unsafe { v8__Data__IsBoolean(self) } + } + + /// Returns true if this data is a `Context`. + #[inline(always)] + pub fn is_context(&self) -> bool { + unsafe { v8__Data__IsContext(self) } + } + + /// Returns true if this data is a `DictionaryTemplate`. + #[inline(always)] + pub fn is_dictionary_template(&self) -> bool { + unsafe { v8__Data__IsDictionaryTemplate(self) } + } + + /// Returns true if this data is a `FixedArray`. + #[inline(always)] + pub fn is_fixed_array(&self) -> bool { + unsafe { v8__Data__IsFixedArray(self) } + } + + /// Returns true if this data is a `FunctionTemplate`. + #[inline(always)] + pub fn is_function_template(&self) -> bool { + unsafe { v8__Data__IsFunctionTemplate(self) } + } + + /// Returns true if this data is a `Module`. + #[inline(always)] + pub fn is_module(&self) -> bool { + unsafe { v8__Data__IsModule(self) } + } + + /// Returns true if this data is a `ModuleRequest`. + #[inline(always)] + pub fn is_module_request(&self) -> bool { + unsafe { v8__Data__IsModuleRequest(self) } + } + + /// Returns true if this data is a `Name`. + #[inline(always)] + pub fn is_name(&self) -> bool { + unsafe { v8__Data__IsName(self) } + } + + /// Returns true if this data is a `Number`. + #[inline(always)] + pub fn is_number(&self) -> bool { + unsafe { v8__Data__IsNumber(self) } + } + + /// Returns true if this data is a `ObjectTemplate`. + #[inline(always)] + pub fn is_object_template(&self) -> bool { + unsafe { v8__Data__IsObjectTemplate(self) } + } + + /// Returns true if this data is a `Primitive`. + #[inline(always)] + pub fn is_primitive(&self) -> bool { + unsafe { v8__Data__IsPrimitive(self) } + } + + /// Returns true if this data is a `Private`. + #[inline(always)] + pub fn is_private(&self) -> bool { + unsafe { v8__Data__IsPrivate(self) } + } + + /// Returns true if this data is a `String`. + #[inline(always)] + pub fn is_string(&self) -> bool { + unsafe { v8__Data__IsString(self) } + } + + /// Returns true if this data is a `Symbol`. + #[inline(always)] + pub fn is_symbol(&self) -> bool { + unsafe { v8__Data__IsSymbol(self) } + } + + /// Returns true if this data is a `Value`. + #[inline(always)] + pub fn is_value(&self) -> bool { + unsafe { v8__Data__IsValue(self) } + } +} + +macro_rules! impl_deref { + { $target:ident for $type:ident } => { + impl Deref for $type { + type Target = $target; + #[inline] + fn deref(&self) -> &Self::Target { + unsafe { &*(self as *const _ as *const Self::Target) } + } + } + }; +} + +macro_rules! impl_from { + { $source:ident for $type:ident } => { + impl<'s> From> for Local<'s, $type> { + #[inline] + fn from(l: Local<'s, $source>) -> Self { + unsafe { transmute(l) } + } + } + }; +} + +macro_rules! impl_try_from { + { $source:ident for $target:ident if $value:pat => $check:expr } => { + impl<'s> TryFrom> for Local<'s, $target> { + type Error = DataError; + #[inline] + fn try_from(l: Local<'s, $source>) -> Result { + // Not dead: `cast()` is sometimes used in the $check expression. + #[allow(dead_code)] + #[inline(always)] + fn cast(l: Local<$source>) -> Local { + unsafe { transmute::, Local>(l) } + } + match l { + $value if $check => Ok(unsafe { + transmute::, Local<'s, $target>>(l) + }), + _ => Err(DataError::bad_type::<$target, $source>()) + } + } + } + }; +} + +macro_rules! impl_eq { + { for $type:ident } => { + impl Eq for $type {} + }; +} + +macro_rules! impl_hash { + { for $type:ident use $method:ident } => { + impl Hash for $type { + fn hash(&self, state: &mut H) { + self.$method().hash(state) + } + } + }; +} + +macro_rules! impl_partial_eq { + { $rhs:ident for $type:ident use identity } => { + impl<'s> PartialEq<$rhs> for $type { + #[inline] + fn eq(&self, other: &$rhs) -> bool { + let a = self as *const _ as *const Data; + let b = other as *const _ as *const Data; + unsafe { v8__Data__EQ(a, b) } + } + } + }; + { $rhs:ident for $type:ident use strict_equals } => { + impl<'s> PartialEq<$rhs> for $type { + #[inline] + fn eq(&self, other: &$rhs) -> bool { + let a = self as *const _ as *const Value; + let b = other as *const _ as *const Value; + unsafe { v8__Value__StrictEquals(a, b) } + } + } + }; + + { $rhs:ident for $type:ident use same_value_zero } => { + impl<'s> PartialEq<$rhs> for $type { + #[inline] + fn eq(&self, other: &$rhs) -> bool { + let a = self as *const _ as *const Value; + let b = other as *const _ as *const Value; + unsafe { + v8__Value__SameValue(a, b) || { + let zero = &*Local::::from(Integer::zero()); + v8__Value__StrictEquals(a, zero) && v8__Value__StrictEquals(b, zero) + } + } + } + } + }; +} + +#[derive(Clone, Copy, Debug)] +pub enum DataError { + BadType { + actual: &'static str, + expected: &'static str, + }, + NoData { + expected: &'static str, + }, +} + +impl DataError { + pub(crate) fn bad_type() -> Self { + Self::BadType { + expected: type_name::(), + actual: type_name::(), + } + } + + #[allow(dead_code)] + pub(crate) fn no_data() -> Self { + Self::NoData { + expected: type_name::(), + } + } +} + +impl Display for DataError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + Self::BadType { expected, actual } => { + write!(f, "expected type `{expected}`, got `{actual}`") + } + Self::NoData { expected } => { + write!(f, "expected `Some({expected})`, found `None`") + } + } + } +} + +impl Error for DataError {} + +/// The superclass of objects that can reside on V8's heap. +#[repr(C)] +#[derive(Debug)] +pub struct Data(Opaque); + +impl_from! { AccessorSignature for Data } +impl_from! { Context for Data } +impl_from! { FixedArray for Data } +impl_from! { Module for Data } +impl_from! { ModuleRequest for Data } +impl_from! { PrimitiveArray for Data } +impl_from! { Private for Data } +impl_from! { Signature for Data } +impl_from! { Template for Data } +impl_from! { FunctionTemplate for Data } +impl_from! { ObjectTemplate for Data } +impl_from! { DictionaryTemplate for Data } +impl_from! { UnboundModuleScript for Data } +impl_from! { UnboundScript for Data } +impl_from! { Value for Data } +impl_from! { External for Data } +impl_from! { Object for Data } +impl_from! { Array for Data } +impl_from! { ArrayBuffer for Data } +impl_from! { ArrayBufferView for Data } +impl_from! { DataView for Data } +impl_from! { TypedArray for Data } +impl_from! { BigInt64Array for Data } +impl_from! { BigUint64Array for Data } +impl_from! { Float32Array for Data } +impl_from! { Float64Array for Data } +impl_from! { Int16Array for Data } +impl_from! { Int32Array for Data } +impl_from! { Int8Array for Data } +impl_from! { Uint16Array for Data } +impl_from! { Uint32Array for Data } +impl_from! { Uint8Array for Data } +impl_from! { Uint8ClampedArray for Data } +impl_from! { BigIntObject for Data } +impl_from! { BooleanObject for Data } +impl_from! { Date for Data } +impl_from! { Function for Data } +impl_from! { Map for Data } +impl_from! { NumberObject for Data } +impl_from! { Promise for Data } +impl_from! { PromiseResolver for Data } +impl_from! { Proxy for Data } +impl_from! { RegExp for Data } +impl_from! { Set for Data } +impl_from! { SharedArrayBuffer for Data } +impl_from! { StringObject for Data } +impl_from! { SymbolObject for Data } +impl_from! { WasmMemoryObject for Data } +impl_from! { WasmModuleObject for Data } +impl_from! { Primitive for Data } +impl_from! { BigInt for Data } +impl_from! { Boolean for Data } +impl_from! { Name for Data } +impl_from! { String for Data } +impl_from! { Symbol for Data } +impl_from! { Number for Data } +impl_from! { Integer for Data } +impl_from! { Int32 for Data } +impl_from! { Uint32 for Data } +impl_partial_eq! { AccessorSignature for Data use identity } +impl_partial_eq! { Context for Data use identity } +impl_partial_eq! { FixedArray for Data use identity } +impl_partial_eq! { Module for Data use identity } +impl_partial_eq! { ModuleRequest for Data use identity } +impl_partial_eq! { PrimitiveArray for Data use identity } +impl_partial_eq! { Private for Data use identity } +impl_partial_eq! { Signature for Data use identity } +impl_partial_eq! { Template for Data use identity } +impl_partial_eq! { FunctionTemplate for Data use identity } +impl_partial_eq! { ObjectTemplate for Data use identity } +impl_partial_eq! { DictionaryTemplate for Data use identity } +impl_partial_eq! { UnboundModuleScript for Data use identity } +impl_partial_eq! { External for Data use identity } +impl_partial_eq! { Object for Data use identity } +impl_partial_eq! { Array for Data use identity } +impl_partial_eq! { ArrayBuffer for Data use identity } +impl_partial_eq! { ArrayBufferView for Data use identity } +impl_partial_eq! { DataView for Data use identity } +impl_partial_eq! { TypedArray for Data use identity } +impl_partial_eq! { BigInt64Array for Data use identity } +impl_partial_eq! { BigUint64Array for Data use identity } +impl_partial_eq! { Float32Array for Data use identity } +impl_partial_eq! { Float64Array for Data use identity } +impl_partial_eq! { Int16Array for Data use identity } +impl_partial_eq! { Int32Array for Data use identity } +impl_partial_eq! { Int8Array for Data use identity } +impl_partial_eq! { Uint16Array for Data use identity } +impl_partial_eq! { Uint32Array for Data use identity } +impl_partial_eq! { Uint8Array for Data use identity } +impl_partial_eq! { Uint8ClampedArray for Data use identity } +impl_partial_eq! { BigIntObject for Data use identity } +impl_partial_eq! { BooleanObject for Data use identity } +impl_partial_eq! { Date for Data use identity } +impl_partial_eq! { Function for Data use identity } +impl_partial_eq! { Map for Data use identity } +impl_partial_eq! { NumberObject for Data use identity } +impl_partial_eq! { Promise for Data use identity } +impl_partial_eq! { PromiseResolver for Data use identity } +impl_partial_eq! { Proxy for Data use identity } +impl_partial_eq! { RegExp for Data use identity } +impl_partial_eq! { Set for Data use identity } +impl_partial_eq! { SharedArrayBuffer for Data use identity } +impl_partial_eq! { StringObject for Data use identity } +impl_partial_eq! { SymbolObject for Data use identity } +impl_partial_eq! { WasmMemoryObject for Data use identity } +impl_partial_eq! { WasmModuleObject for Data use identity } +impl_partial_eq! { Boolean for Data use identity } +impl_partial_eq! { Symbol for Data use identity } + +/// An AccessorSignature specifies which receivers are valid parameters +/// to an accessor callback. +#[repr(C)] +#[derive(Debug)] +pub struct AccessorSignature(Opaque); + +impl_deref! { Data for AccessorSignature } +impl_eq! { for AccessorSignature } +impl_partial_eq! { Data for AccessorSignature use identity } +impl_partial_eq! { AccessorSignature for AccessorSignature use identity } + +/// A sandboxed execution context with its own set of built-in objects +/// and functions. +#[repr(C)] +#[derive(Debug)] +pub struct Context(Opaque); + +impl_deref! { Data for Context } +impl_try_from! { Data for Context if v => v.is_context() } +impl_eq! { for Context } +impl_partial_eq! { Data for Context use identity } +impl_partial_eq! { Context for Context use identity } + +/// A fixed-sized array with elements of type Data. +#[repr(C)] +#[derive(Debug)] +pub struct FixedArray(Opaque); + +impl_deref! { Data for FixedArray } +impl_try_from! { Data for FixedArray if v => v.is_fixed_array() } +impl_eq! { for FixedArray } +impl_partial_eq! { Data for FixedArray use identity } +impl_partial_eq! { FixedArray for FixedArray use identity } + +/// An error message. +#[repr(C)] +#[derive(Debug)] +pub struct Message(Opaque); + +impl_eq! { for Message } +impl_partial_eq! { Message for Message use identity } + +/// A compiled JavaScript module. +#[repr(C)] +#[derive(Debug)] +pub struct Module(Opaque); + +impl_deref! { Data for Module } +impl_try_from! { Data for Module if v => v.is_module() } +impl_eq! { for Module } +impl_hash! { for Module use get_identity_hash } +impl_partial_eq! { Data for Module use identity } +impl_partial_eq! { Module for Module use identity } + +#[repr(C)] +#[derive(Debug)] +pub struct ModuleRequest(Opaque); + +impl_deref! { Data for ModuleRequest } +impl_try_from! { Data for ModuleRequest if v => v.is_module_request() } +impl_eq! { for ModuleRequest } +impl_partial_eq! { Data for ModuleRequest use identity } +impl_partial_eq! { ModuleRequest for ModuleRequest use identity } + +/// An array to hold Primitive values. This is used by the embedder to +/// pass host defined options to the ScriptOptions during compilation. +/// +/// This is passed back to the embedder as part of +/// HostImportModuleDynamicallyCallback for module loading. +#[repr(C)] +#[derive(Debug)] +pub struct PrimitiveArray(Opaque); + +impl_deref! { Data for PrimitiveArray } +impl_eq! { for PrimitiveArray } +impl_partial_eq! { Data for PrimitiveArray use identity } +impl_partial_eq! { PrimitiveArray for PrimitiveArray use identity } + +/// A private symbol +/// +/// This is an experimental feature. Use at your own risk. +#[repr(C)] +#[derive(Debug)] +pub struct Private(Opaque); + +impl_deref! { Data for Private } +impl_try_from! { Data for Private if v => v.is_private() } +impl_eq! { for Private } +impl_partial_eq! { Data for Private use identity } +impl_partial_eq! { Private for Private use identity } + +/// A compiled JavaScript script, tied to a Context which was active when the +/// script was compiled. +#[repr(C)] +#[derive(Debug)] +pub struct Script(Opaque); + +impl_eq! { for Script } +impl_partial_eq! { Script for Script use identity } + +/// A container type that holds relevant metadata for module loading. +/// +/// This is passed back to the embedder as part of +/// HostImportModuleDynamicallyCallback for module loading. +#[repr(C)] +#[derive(Debug)] +pub struct ScriptOrModule(Opaque); + +impl_eq! { for ScriptOrModule } +impl_partial_eq! { ScriptOrModule for ScriptOrModule use identity } + +/// A Signature specifies which receiver is valid for a function. +/// +/// A receiver matches a given signature if the receiver (or any of its +/// hidden prototypes) was created from the signature's FunctionTemplate, or +/// from a FunctionTemplate that inherits directly or indirectly from the +/// signature's FunctionTemplate. +#[repr(C)] +#[derive(Debug)] +pub struct Signature(Opaque); + +impl_deref! { Data for Signature } +impl_eq! { for Signature } +impl_partial_eq! { Data for Signature use identity } +impl_partial_eq! { Signature for Signature use identity } + +/// A single JavaScript stack frame. +#[repr(C)] +#[derive(Debug)] +pub struct StackFrame(Opaque); + +impl_eq! { for StackFrame } +impl_partial_eq! { StackFrame for StackFrame use identity } + +/// Representation of a JavaScript stack trace. The information collected is a +/// snapshot of the execution stack and the information remains valid after +/// execution continues. +#[repr(C)] +#[derive(Debug)] +pub struct StackTrace(Opaque); + +impl_eq! { for StackTrace } +impl_partial_eq! { StackTrace for StackTrace use identity } + +/// The superclass of object and function templates. +#[repr(C)] +#[derive(Debug)] +pub struct Template(Opaque); + +impl_deref! { Data for Template } +impl_try_from! { Data for Template if v => v.is_function_template() || v.is_object_template() } +impl_from! { FunctionTemplate for Template } +impl_from! { ObjectTemplate for Template } +impl_eq! { for Template } +impl_partial_eq! { Data for Template use identity } +impl_partial_eq! { Template for Template use identity } +impl_partial_eq! { FunctionTemplate for Template use identity } +impl_partial_eq! { ObjectTemplate for Template use identity } + +/// A FunctionTemplate is used to create functions at runtime. There +/// can only be one function created from a FunctionTemplate in a +/// context. The lifetime of the created function is equal to the +/// lifetime of the context. So in case the embedder needs to create +/// temporary functions that can be collected using Scripts is +/// preferred. +/// +/// Any modification of a FunctionTemplate after first instantiation will trigger +/// a crash. +/// +/// A FunctionTemplate can have properties, these properties are added to the +/// function object when it is created. +/// +/// A FunctionTemplate has a corresponding instance template which is +/// used to create object instances when the function is used as a +/// constructor. Properties added to the instance template are added to +/// each object instance. +/// +/// A FunctionTemplate can have a prototype template. The prototype template +/// is used to create the prototype object of the function. +/// +/// The following example shows how to use a FunctionTemplate: +/// +/// ```ignore +/// v8::Local t = v8::FunctionTemplate::New(isolate); +/// t->Set(isolate, "func_property", v8::Number::New(isolate, 1)); +/// +/// v8::Local proto_t = t->PrototypeTemplate(); +/// proto_t->Set(isolate, +/// "proto_method", +/// v8::FunctionTemplate::New(isolate, InvokeCallback)); +/// proto_t->Set(isolate, "proto_const", v8::Number::New(isolate, 2)); +/// +/// v8::Local instance_t = t->InstanceTemplate(); +/// instance_t->SetAccessor( +/// String::NewFromUtf8Literal(isolate, "instance_accessor"), +/// InstanceAccessorCallback); +/// instance_t->SetHandler( +/// NamedPropertyHandlerConfiguration(PropertyHandlerCallback)); +/// instance_t->Set(String::NewFromUtf8Literal(isolate, "instance_property"), +/// Number::New(isolate, 3)); +/// +/// v8::Local function = t->GetFunction(); +/// v8::Local instance = function->NewInstance(); +/// ``` +/// +/// Let's use "function" as the JS variable name of the function object +/// and "instance" for the instance object created above. The function +/// and the instance will have the following properties: +/// +/// ```ignore +/// func_property in function == true; +/// function.func_property == 1; +/// +/// function.prototype.proto_method() invokes 'InvokeCallback' +/// function.prototype.proto_const == 2; +/// +/// instance instanceof function == true; +/// instance.instance_accessor calls 'InstanceAccessorCallback' +/// instance.instance_property == 3; +/// ``` +/// +/// A FunctionTemplate can inherit from another one by calling the +/// FunctionTemplate::Inherit method. The following graph illustrates +/// the semantics of inheritance: +/// +/// ```ignore +/// FunctionTemplate Parent -> Parent() . prototype -> { } +/// ^ ^ +/// | Inherit(Parent) | .__proto__ +/// | | +/// FunctionTemplate Child -> Child() . prototype -> { } +/// ``` +/// +/// A FunctionTemplate 'Child' inherits from 'Parent', the prototype +/// object of the Child() function has __proto__ pointing to the +/// Parent() function's prototype object. An instance of the Child +/// function has all properties on Parent's instance templates. +/// +/// Let Parent be the FunctionTemplate initialized in the previous +/// section and create a Child FunctionTemplate by: +/// +/// ```ignore +/// Local parent = t; +/// Local child = FunctionTemplate::New(); +/// child->Inherit(parent); +/// +/// Local child_function = child->GetFunction(); +/// Local child_instance = child_function->NewInstance(); +/// ``` +/// +/// The Child function and Child instance will have the following +/// properties: +/// +/// ```ignore +/// child_func.prototype.__proto__ == function.prototype; +/// child_instance.instance_accessor calls 'InstanceAccessorCallback' +/// child_instance.instance_property == 3; +/// ``` +/// +/// The additional 'c_function' parameter refers to a fast API call, which +/// must not trigger GC or JavaScript execution, or call into V8 in other +/// ways. For more information how to define them, see +/// include/v8-fast-api-calls.h. Please note that this feature is still +/// experimental. +#[repr(C)] +#[derive(Debug)] +pub struct FunctionTemplate(Opaque); + +impl_deref! { Template for FunctionTemplate } +impl_try_from! { Data for FunctionTemplate if v => v.is_function_template() } +impl_try_from! { Template for FunctionTemplate if v => v.is_function_template() } +impl_eq! { for FunctionTemplate } +impl_partial_eq! { Data for FunctionTemplate use identity } +impl_partial_eq! { Template for FunctionTemplate use identity } +impl_partial_eq! { FunctionTemplate for FunctionTemplate use identity } + +/// An ObjectTemplate is used to create objects at runtime. +/// +/// Properties added to an ObjectTemplate are added to each object +/// created from the ObjectTemplate. +#[repr(C)] +#[derive(Debug)] +pub struct ObjectTemplate(Opaque); + +impl_deref! { Template for ObjectTemplate } +impl_try_from! { Data for ObjectTemplate if v => v.is_object_template() } +impl_try_from! { Template for ObjectTemplate if v => v.is_object_template() } +impl_eq! { for ObjectTemplate } +impl_partial_eq! { Data for ObjectTemplate use identity } +impl_partial_eq! { Template for ObjectTemplate use identity } +impl_partial_eq! { ObjectTemplate for ObjectTemplate use identity } + +/// A DictionaryTemplate is used to create dictionary-mode objects with a +/// fixed, immutable set of property keys that is declared once, on +/// construction of the template. The values for those properties are supplied +/// separately, each time an instance is created. +#[repr(C)] +#[derive(Debug)] +pub struct DictionaryTemplate(Opaque); + +impl_deref! { Data for DictionaryTemplate } +impl_try_from! { Data for DictionaryTemplate if v => v.is_dictionary_template() } +impl_eq! { for DictionaryTemplate } +impl_partial_eq! { Data for DictionaryTemplate use identity } +impl_partial_eq! { DictionaryTemplate for DictionaryTemplate use identity } + +/// A compiled JavaScript module, not yet tied to a Context. +#[repr(C)] +#[derive(Debug)] +pub struct UnboundModuleScript(Opaque); + +impl_deref! { Data for UnboundModuleScript } +impl_eq! { for UnboundModuleScript } +impl_partial_eq! { Data for UnboundModuleScript use identity } +impl_partial_eq! { UnboundModuleScript for UnboundModuleScript use identity } + +/// A compiled JavaScript script, not yet tied to a Context. +#[repr(C)] +#[derive(Debug)] +pub struct UnboundScript(Opaque); + +impl_eq! { for UnboundScript } +impl_partial_eq! { UnboundScript for UnboundScript use identity } + +/// The superclass of all JavaScript values and objects. +#[repr(C)] +#[derive(Debug, Clone, Copy)] +pub struct Value(Opaque); + +impl_deref! { Data for Value } +impl_try_from! { Data for Value if v => v.is_value() } +impl_from! { External for Value } +impl_from! { Object for Value } +impl_from! { Array for Value } +impl_from! { ArrayBuffer for Value } +impl_from! { ArrayBufferView for Value } +impl_from! { DataView for Value } +impl_from! { TypedArray for Value } +impl_from! { BigInt64Array for Value } +impl_from! { BigUint64Array for Value } +impl_from! { Float32Array for Value } +impl_from! { Float64Array for Value } +impl_from! { Int16Array for Value } +impl_from! { Int32Array for Value } +impl_from! { Int8Array for Value } +impl_from! { Uint16Array for Value } +impl_from! { Uint32Array for Value } +impl_from! { Uint8Array for Value } +impl_from! { Uint8ClampedArray for Value } +impl_from! { BigIntObject for Value } +impl_from! { BooleanObject for Value } +impl_from! { Date for Value } +impl_from! { Function for Value } +impl_from! { Map for Value } +impl_from! { NumberObject for Value } +impl_from! { Promise for Value } +impl_from! { PromiseResolver for Value } +impl_from! { Proxy for Value } +impl_from! { RegExp for Value } +impl_from! { Set for Value } +impl_from! { SharedArrayBuffer for Value } +impl_from! { StringObject for Value } +impl_from! { SymbolObject for Value } +impl_from! { WasmMemoryObject for Value } +impl_from! { WasmModuleObject for Value } +impl_from! { Primitive for Value } +impl_from! { BigInt for Value } +impl_from! { Boolean for Value } +impl_from! { Name for Value } +impl_from! { String for Value } +impl_from! { Symbol for Value } +impl_from! { Number for Value } +impl_from! { Integer for Value } +impl_from! { Int32 for Value } +impl_from! { Uint32 for Value } +impl_eq! { for Value } +impl_hash! { for Value use get_hash } +impl_partial_eq! { Value for Value use same_value_zero } +impl_partial_eq! { External for Value use identity } +impl_partial_eq! { Object for Value use identity } +impl_partial_eq! { Array for Value use identity } +impl_partial_eq! { ArrayBuffer for Value use identity } +impl_partial_eq! { ArrayBufferView for Value use identity } +impl_partial_eq! { DataView for Value use identity } +impl_partial_eq! { TypedArray for Value use identity } +impl_partial_eq! { BigInt64Array for Value use identity } +impl_partial_eq! { BigUint64Array for Value use identity } +impl_partial_eq! { Float32Array for Value use identity } +impl_partial_eq! { Float64Array for Value use identity } +impl_partial_eq! { Int16Array for Value use identity } +impl_partial_eq! { Int32Array for Value use identity } +impl_partial_eq! { Int8Array for Value use identity } +impl_partial_eq! { Uint16Array for Value use identity } +impl_partial_eq! { Uint32Array for Value use identity } +impl_partial_eq! { Uint8Array for Value use identity } +impl_partial_eq! { Uint8ClampedArray for Value use identity } +impl_partial_eq! { BigIntObject for Value use identity } +impl_partial_eq! { BooleanObject for Value use identity } +impl_partial_eq! { Date for Value use identity } +impl_partial_eq! { Function for Value use identity } +impl_partial_eq! { Map for Value use identity } +impl_partial_eq! { NumberObject for Value use identity } +impl_partial_eq! { Promise for Value use identity } +impl_partial_eq! { PromiseResolver for Value use identity } +impl_partial_eq! { Proxy for Value use identity } +impl_partial_eq! { RegExp for Value use identity } +impl_partial_eq! { Set for Value use identity } +impl_partial_eq! { SharedArrayBuffer for Value use identity } +impl_partial_eq! { StringObject for Value use identity } +impl_partial_eq! { SymbolObject for Value use identity } +impl_partial_eq! { WasmMemoryObject for Value use identity } +impl_partial_eq! { WasmModuleObject for Value use identity } +impl_partial_eq! { Primitive for Value use same_value_zero } +impl_partial_eq! { BigInt for Value use same_value_zero } +impl_partial_eq! { Boolean for Value use identity } +impl_partial_eq! { Name for Value use same_value_zero } +impl_partial_eq! { String for Value use same_value_zero } +impl_partial_eq! { Symbol for Value use identity } +impl_partial_eq! { Number for Value use same_value_zero } +impl_partial_eq! { Integer for Value use same_value_zero } +impl_partial_eq! { Int32 for Value use same_value_zero } +impl_partial_eq! { Uint32 for Value use same_value_zero } + +/// A JavaScript value that wraps a C++ void*. This type of value is mainly used +/// to associate C++ data structures with JavaScript objects. +#[repr(C)] +#[derive(Debug)] +pub struct External(Opaque); + +impl_deref! { Value for External } +impl_try_from! { Data for External if v => v.is_value() && cast::(v).is_external() } +impl_try_from! { Value for External if v => v.is_external() } +impl_eq! { for External } +impl_hash! { for External use get_hash } +impl_partial_eq! { Data for External use identity } +impl_partial_eq! { Value for External use identity } +impl_partial_eq! { External for External use identity } + +/// A JavaScript object (ECMA-262, 4.3.3) +#[repr(C)] +#[derive(Debug)] +pub struct Object(Opaque); + +impl_deref! { Value for Object } +impl_try_from! { Data for Object if v => v.is_value() && cast::(v).is_object() } +impl_try_from! { Value for Object if v => v.is_object() } +impl_from! { Array for Object } +impl_from! { ArrayBuffer for Object } +impl_from! { ArrayBufferView for Object } +impl_from! { DataView for Object } +impl_from! { TypedArray for Object } +impl_from! { BigInt64Array for Object } +impl_from! { BigUint64Array for Object } +impl_from! { Float32Array for Object } +impl_from! { Float64Array for Object } +impl_from! { Int16Array for Object } +impl_from! { Int32Array for Object } +impl_from! { Int8Array for Object } +impl_from! { Uint16Array for Object } +impl_from! { Uint32Array for Object } +impl_from! { Uint8Array for Object } +impl_from! { Uint8ClampedArray for Object } +impl_from! { BigIntObject for Object } +impl_from! { BooleanObject for Object } +impl_from! { Date for Object } +impl_from! { Function for Object } +impl_from! { Map for Object } +impl_from! { NumberObject for Object } +impl_from! { Promise for Object } +impl_from! { PromiseResolver for Object } +impl_from! { Proxy for Object } +impl_from! { RegExp for Object } +impl_from! { Set for Object } +impl_from! { SharedArrayBuffer for Object } +impl_from! { StringObject for Object } +impl_from! { SymbolObject for Object } +impl_from! { WasmMemoryObject for Object } +impl_from! { WasmModuleObject for Object } +impl_eq! { for Object } +impl_hash! { for Object use get_identity_hash } +impl_partial_eq! { Data for Object use identity } +impl_partial_eq! { Value for Object use identity } +impl_partial_eq! { Object for Object use identity } +impl_partial_eq! { Array for Object use identity } +impl_partial_eq! { ArrayBuffer for Object use identity } +impl_partial_eq! { ArrayBufferView for Object use identity } +impl_partial_eq! { DataView for Object use identity } +impl_partial_eq! { TypedArray for Object use identity } +impl_partial_eq! { BigInt64Array for Object use identity } +impl_partial_eq! { BigUint64Array for Object use identity } +impl_partial_eq! { Float32Array for Object use identity } +impl_partial_eq! { Float64Array for Object use identity } +impl_partial_eq! { Int16Array for Object use identity } +impl_partial_eq! { Int32Array for Object use identity } +impl_partial_eq! { Int8Array for Object use identity } +impl_partial_eq! { Uint16Array for Object use identity } +impl_partial_eq! { Uint32Array for Object use identity } +impl_partial_eq! { Uint8Array for Object use identity } +impl_partial_eq! { Uint8ClampedArray for Object use identity } +impl_partial_eq! { BigIntObject for Object use identity } +impl_partial_eq! { BooleanObject for Object use identity } +impl_partial_eq! { Date for Object use identity } +impl_partial_eq! { Function for Object use identity } +impl_partial_eq! { Map for Object use identity } +impl_partial_eq! { NumberObject for Object use identity } +impl_partial_eq! { Promise for Object use identity } +impl_partial_eq! { PromiseResolver for Object use identity } +impl_partial_eq! { Proxy for Object use identity } +impl_partial_eq! { RegExp for Object use identity } +impl_partial_eq! { Set for Object use identity } +impl_partial_eq! { SharedArrayBuffer for Object use identity } +impl_partial_eq! { StringObject for Object use identity } +impl_partial_eq! { SymbolObject for Object use identity } +impl_partial_eq! { WasmMemoryObject for Object use identity } +impl_partial_eq! { WasmModuleObject for Object use identity } + +/// An instance of the built-in array constructor (ECMA-262, 15.4.2). +#[repr(C)] +#[derive(Debug)] +pub struct Array(Opaque); + +impl_deref! { Object for Array } +impl_try_from! { Data for Array if v => v.is_value() && cast::(v).is_array() } +impl_try_from! { Value for Array if v => v.is_array() } +impl_try_from! { Object for Array if v => v.is_array() } +impl_eq! { for Array } +impl_hash! { for Array use get_identity_hash } +impl_partial_eq! { Data for Array use identity } +impl_partial_eq! { Value for Array use identity } +impl_partial_eq! { Object for Array use identity } +impl_partial_eq! { Array for Array use identity } + +/// An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5). +#[repr(C)] +#[derive(Debug)] +pub struct ArrayBuffer(Opaque); + +impl_deref! { Object for ArrayBuffer } +impl_try_from! { Data for ArrayBuffer if v => v.is_value() && cast::(v).is_array_buffer() } +impl_try_from! { Value for ArrayBuffer if v => v.is_array_buffer() } +impl_try_from! { Object for ArrayBuffer if v => v.is_array_buffer() } +impl_eq! { for ArrayBuffer } +impl_hash! { for ArrayBuffer use get_identity_hash } +impl_partial_eq! { Data for ArrayBuffer use identity } +impl_partial_eq! { Value for ArrayBuffer use identity } +impl_partial_eq! { Object for ArrayBuffer use identity } +impl_partial_eq! { ArrayBuffer for ArrayBuffer use identity } + +/// A base class for an instance of one of "views" over ArrayBuffer, +/// including TypedArrays and DataView (ES6 draft 15.13). +#[repr(C)] +#[derive(Debug)] +pub struct ArrayBufferView(Opaque); + +impl_deref! { Object for ArrayBufferView } +impl_try_from! { Data for ArrayBufferView if v => v.is_value() && cast::(v).is_array_buffer_view() } +impl_try_from! { Value for ArrayBufferView if v => v.is_array_buffer_view() } +impl_try_from! { Object for ArrayBufferView if v => v.is_array_buffer_view() } +impl_from! { DataView for ArrayBufferView } +impl_from! { TypedArray for ArrayBufferView } +impl_from! { BigInt64Array for ArrayBufferView } +impl_from! { BigUint64Array for ArrayBufferView } +impl_from! { Float32Array for ArrayBufferView } +impl_from! { Float64Array for ArrayBufferView } +impl_from! { Int16Array for ArrayBufferView } +impl_from! { Int32Array for ArrayBufferView } +impl_from! { Int8Array for ArrayBufferView } +impl_from! { Uint16Array for ArrayBufferView } +impl_from! { Uint32Array for ArrayBufferView } +impl_from! { Uint8Array for ArrayBufferView } +impl_from! { Uint8ClampedArray for ArrayBufferView } +impl_eq! { for ArrayBufferView } +impl_hash! { for ArrayBufferView use get_identity_hash } +impl_partial_eq! { Data for ArrayBufferView use identity } +impl_partial_eq! { Value for ArrayBufferView use identity } +impl_partial_eq! { Object for ArrayBufferView use identity } +impl_partial_eq! { ArrayBufferView for ArrayBufferView use identity } +impl_partial_eq! { DataView for ArrayBufferView use identity } +impl_partial_eq! { TypedArray for ArrayBufferView use identity } +impl_partial_eq! { BigInt64Array for ArrayBufferView use identity } +impl_partial_eq! { BigUint64Array for ArrayBufferView use identity } +impl_partial_eq! { Float32Array for ArrayBufferView use identity } +impl_partial_eq! { Float64Array for ArrayBufferView use identity } +impl_partial_eq! { Int16Array for ArrayBufferView use identity } +impl_partial_eq! { Int32Array for ArrayBufferView use identity } +impl_partial_eq! { Int8Array for ArrayBufferView use identity } +impl_partial_eq! { Uint16Array for ArrayBufferView use identity } +impl_partial_eq! { Uint32Array for ArrayBufferView use identity } +impl_partial_eq! { Uint8Array for ArrayBufferView use identity } +impl_partial_eq! { Uint8ClampedArray for ArrayBufferView use identity } + +/// An instance of DataView constructor (ES6 draft 15.13.7). +#[repr(C)] +#[derive(Debug)] +pub struct DataView(Opaque); + +impl_deref! { ArrayBufferView for DataView } +impl_try_from! { Data for DataView if v => v.is_value() && cast::(v).is_data_view() } +impl_try_from! { Value for DataView if v => v.is_data_view() } +impl_try_from! { Object for DataView if v => v.is_data_view() } +impl_try_from! { ArrayBufferView for DataView if v => v.is_data_view() } +impl_eq! { for DataView } +impl_hash! { for DataView use get_identity_hash } +impl_partial_eq! { Data for DataView use identity } +impl_partial_eq! { Value for DataView use identity } +impl_partial_eq! { Object for DataView use identity } +impl_partial_eq! { ArrayBufferView for DataView use identity } +impl_partial_eq! { DataView for DataView use identity } + +/// A base class for an instance of TypedArray series of constructors +/// (ES6 draft 15.13.6). +#[repr(C)] +#[derive(Debug)] +pub struct TypedArray(Opaque); + +impl_deref! { ArrayBufferView for TypedArray } +impl_try_from! { Data for TypedArray if v => v.is_value() && cast::(v).is_typed_array() } +impl_try_from! { Value for TypedArray if v => v.is_typed_array() } +impl_try_from! { Object for TypedArray if v => v.is_typed_array() } +impl_try_from! { ArrayBufferView for TypedArray if v => v.is_typed_array() } +impl_from! { BigInt64Array for TypedArray } +impl_from! { BigUint64Array for TypedArray } +impl_from! { Float32Array for TypedArray } +impl_from! { Float64Array for TypedArray } +impl_from! { Int16Array for TypedArray } +impl_from! { Int32Array for TypedArray } +impl_from! { Int8Array for TypedArray } +impl_from! { Uint16Array for TypedArray } +impl_from! { Uint32Array for TypedArray } +impl_from! { Uint8Array for TypedArray } +impl_from! { Uint8ClampedArray for TypedArray } +impl_eq! { for TypedArray } +impl_hash! { for TypedArray use get_identity_hash } +impl_partial_eq! { Data for TypedArray use identity } +impl_partial_eq! { Value for TypedArray use identity } +impl_partial_eq! { Object for TypedArray use identity } +impl_partial_eq! { ArrayBufferView for TypedArray use identity } +impl_partial_eq! { TypedArray for TypedArray use identity } +impl_partial_eq! { BigInt64Array for TypedArray use identity } +impl_partial_eq! { BigUint64Array for TypedArray use identity } +impl_partial_eq! { Float32Array for TypedArray use identity } +impl_partial_eq! { Float64Array for TypedArray use identity } +impl_partial_eq! { Int16Array for TypedArray use identity } +impl_partial_eq! { Int32Array for TypedArray use identity } +impl_partial_eq! { Int8Array for TypedArray use identity } +impl_partial_eq! { Uint16Array for TypedArray use identity } +impl_partial_eq! { Uint32Array for TypedArray use identity } +impl_partial_eq! { Uint8Array for TypedArray use identity } +impl_partial_eq! { Uint8ClampedArray for TypedArray use identity } + +/// An instance of BigInt64Array constructor. +#[repr(C)] +#[derive(Debug)] +pub struct BigInt64Array(Opaque); + +impl_deref! { TypedArray for BigInt64Array } +impl_try_from! { Data for BigInt64Array if v => v.is_value() && cast::(v).is_big_int64_array() } +impl_try_from! { Value for BigInt64Array if v => v.is_big_int64_array() } +impl_try_from! { Object for BigInt64Array if v => v.is_big_int64_array() } +impl_try_from! { ArrayBufferView for BigInt64Array if v => v.is_big_int64_array() } +impl_try_from! { TypedArray for BigInt64Array if v => v.is_big_int64_array() } +impl_eq! { for BigInt64Array } +impl_hash! { for BigInt64Array use get_identity_hash } +impl_partial_eq! { Data for BigInt64Array use identity } +impl_partial_eq! { Value for BigInt64Array use identity } +impl_partial_eq! { Object for BigInt64Array use identity } +impl_partial_eq! { ArrayBufferView for BigInt64Array use identity } +impl_partial_eq! { TypedArray for BigInt64Array use identity } +impl_partial_eq! { BigInt64Array for BigInt64Array use identity } + +/// An instance of BigUint64Array constructor. +#[repr(C)] +#[derive(Debug)] +pub struct BigUint64Array(Opaque); + +impl_deref! { TypedArray for BigUint64Array } +impl_try_from! { Data for BigUint64Array if v => v.is_value() && cast::(v).is_big_uint64_array() } +impl_try_from! { Value for BigUint64Array if v => v.is_big_uint64_array() } +impl_try_from! { Object for BigUint64Array if v => v.is_big_uint64_array() } +impl_try_from! { ArrayBufferView for BigUint64Array if v => v.is_big_uint64_array() } +impl_try_from! { TypedArray for BigUint64Array if v => v.is_big_uint64_array() } +impl_eq! { for BigUint64Array } +impl_hash! { for BigUint64Array use get_identity_hash } +impl_partial_eq! { Data for BigUint64Array use identity } +impl_partial_eq! { Value for BigUint64Array use identity } +impl_partial_eq! { Object for BigUint64Array use identity } +impl_partial_eq! { ArrayBufferView for BigUint64Array use identity } +impl_partial_eq! { TypedArray for BigUint64Array use identity } +impl_partial_eq! { BigUint64Array for BigUint64Array use identity } + +/// An instance of Float16Array constructor. +#[repr(C)] +#[derive(Debug)] +pub struct Float16Array(Opaque); + +impl_deref! { TypedArray for Float16Array } +impl_try_from! { Data for Float16Array if v => v.is_value() && cast::(v).is_float16_array() } +impl_try_from! { Value for Float16Array if v => v.is_float16_array() } +impl_try_from! { Object for Float16Array if v => v.is_float16_array() } +impl_try_from! { ArrayBufferView for Float16Array if v => v.is_float16_array() } +impl_try_from! { TypedArray for Float16Array if v => v.is_float16_array() } +impl_eq! { for Float16Array } +impl_hash! { for Float16Array use get_identity_hash } +impl_partial_eq! { Data for Float16Array use identity } +impl_partial_eq! { Value for Float16Array use identity } +impl_partial_eq! { Object for Float16Array use identity } +impl_partial_eq! { ArrayBufferView for Float16Array use identity } +impl_partial_eq! { TypedArray for Float16Array use identity } +impl_partial_eq! { Float16Array for Float16Array use identity } + +/// An instance of Float32Array constructor (ES6 draft 15.13.6). +#[repr(C)] +#[derive(Debug)] +pub struct Float32Array(Opaque); + +impl_deref! { TypedArray for Float32Array } +impl_try_from! { Data for Float32Array if v => v.is_value() && cast::(v).is_float32_array() } +impl_try_from! { Value for Float32Array if v => v.is_float32_array() } +impl_try_from! { Object for Float32Array if v => v.is_float32_array() } +impl_try_from! { ArrayBufferView for Float32Array if v => v.is_float32_array() } +impl_try_from! { TypedArray for Float32Array if v => v.is_float32_array() } +impl_eq! { for Float32Array } +impl_hash! { for Float32Array use get_identity_hash } +impl_partial_eq! { Data for Float32Array use identity } +impl_partial_eq! { Value for Float32Array use identity } +impl_partial_eq! { Object for Float32Array use identity } +impl_partial_eq! { ArrayBufferView for Float32Array use identity } +impl_partial_eq! { TypedArray for Float32Array use identity } +impl_partial_eq! { Float32Array for Float32Array use identity } + +/// An instance of Float64Array constructor (ES6 draft 15.13.6). +#[repr(C)] +#[derive(Debug)] +pub struct Float64Array(Opaque); + +impl_deref! { TypedArray for Float64Array } +impl_try_from! { Data for Float64Array if v => v.is_value() && cast::(v).is_float64_array() } +impl_try_from! { Value for Float64Array if v => v.is_float64_array() } +impl_try_from! { Object for Float64Array if v => v.is_float64_array() } +impl_try_from! { ArrayBufferView for Float64Array if v => v.is_float64_array() } +impl_try_from! { TypedArray for Float64Array if v => v.is_float64_array() } +impl_eq! { for Float64Array } +impl_hash! { for Float64Array use get_identity_hash } +impl_partial_eq! { Data for Float64Array use identity } +impl_partial_eq! { Value for Float64Array use identity } +impl_partial_eq! { Object for Float64Array use identity } +impl_partial_eq! { ArrayBufferView for Float64Array use identity } +impl_partial_eq! { TypedArray for Float64Array use identity } +impl_partial_eq! { Float64Array for Float64Array use identity } + +/// An instance of Int16Array constructor (ES6 draft 15.13.6). +#[repr(C)] +#[derive(Debug)] +pub struct Int16Array(Opaque); + +impl_deref! { TypedArray for Int16Array } +impl_try_from! { Data for Int16Array if v => v.is_value() && cast::(v).is_int16_array() } +impl_try_from! { Value for Int16Array if v => v.is_int16_array() } +impl_try_from! { Object for Int16Array if v => v.is_int16_array() } +impl_try_from! { ArrayBufferView for Int16Array if v => v.is_int16_array() } +impl_try_from! { TypedArray for Int16Array if v => v.is_int16_array() } +impl_eq! { for Int16Array } +impl_hash! { for Int16Array use get_identity_hash } +impl_partial_eq! { Data for Int16Array use identity } +impl_partial_eq! { Value for Int16Array use identity } +impl_partial_eq! { Object for Int16Array use identity } +impl_partial_eq! { ArrayBufferView for Int16Array use identity } +impl_partial_eq! { TypedArray for Int16Array use identity } +impl_partial_eq! { Int16Array for Int16Array use identity } + +/// An instance of Int32Array constructor (ES6 draft 15.13.6). +#[repr(C)] +#[derive(Debug)] +pub struct Int32Array(Opaque); + +impl_deref! { TypedArray for Int32Array } +impl_try_from! { Data for Int32Array if v => v.is_value() && cast::(v).is_int32_array() } +impl_try_from! { Value for Int32Array if v => v.is_int32_array() } +impl_try_from! { Object for Int32Array if v => v.is_int32_array() } +impl_try_from! { ArrayBufferView for Int32Array if v => v.is_int32_array() } +impl_try_from! { TypedArray for Int32Array if v => v.is_int32_array() } +impl_eq! { for Int32Array } +impl_hash! { for Int32Array use get_identity_hash } +impl_partial_eq! { Data for Int32Array use identity } +impl_partial_eq! { Value for Int32Array use identity } +impl_partial_eq! { Object for Int32Array use identity } +impl_partial_eq! { ArrayBufferView for Int32Array use identity } +impl_partial_eq! { TypedArray for Int32Array use identity } +impl_partial_eq! { Int32Array for Int32Array use identity } + +/// An instance of Int8Array constructor (ES6 draft 15.13.6). +#[repr(C)] +#[derive(Debug)] +pub struct Int8Array(Opaque); + +impl_deref! { TypedArray for Int8Array } +impl_try_from! { Data for Int8Array if v => v.is_value() && cast::(v).is_int8_array() } +impl_try_from! { Value for Int8Array if v => v.is_int8_array() } +impl_try_from! { Object for Int8Array if v => v.is_int8_array() } +impl_try_from! { ArrayBufferView for Int8Array if v => v.is_int8_array() } +impl_try_from! { TypedArray for Int8Array if v => v.is_int8_array() } +impl_eq! { for Int8Array } +impl_hash! { for Int8Array use get_identity_hash } +impl_partial_eq! { Data for Int8Array use identity } +impl_partial_eq! { Value for Int8Array use identity } +impl_partial_eq! { Object for Int8Array use identity } +impl_partial_eq! { ArrayBufferView for Int8Array use identity } +impl_partial_eq! { TypedArray for Int8Array use identity } +impl_partial_eq! { Int8Array for Int8Array use identity } + +/// An instance of Uint16Array constructor (ES6 draft 15.13.6). +#[repr(C)] +#[derive(Debug)] +pub struct Uint16Array(Opaque); + +impl_deref! { TypedArray for Uint16Array } +impl_try_from! { Data for Uint16Array if v => v.is_value() && cast::(v).is_uint16_array() } +impl_try_from! { Value for Uint16Array if v => v.is_uint16_array() } +impl_try_from! { Object for Uint16Array if v => v.is_uint16_array() } +impl_try_from! { ArrayBufferView for Uint16Array if v => v.is_uint16_array() } +impl_try_from! { TypedArray for Uint16Array if v => v.is_uint16_array() } +impl_eq! { for Uint16Array } +impl_hash! { for Uint16Array use get_identity_hash } +impl_partial_eq! { Data for Uint16Array use identity } +impl_partial_eq! { Value for Uint16Array use identity } +impl_partial_eq! { Object for Uint16Array use identity } +impl_partial_eq! { ArrayBufferView for Uint16Array use identity } +impl_partial_eq! { TypedArray for Uint16Array use identity } +impl_partial_eq! { Uint16Array for Uint16Array use identity } + +/// An instance of Uint32Array constructor (ES6 draft 15.13.6). +#[repr(C)] +#[derive(Debug)] +pub struct Uint32Array(Opaque); + +impl_deref! { TypedArray for Uint32Array } +impl_try_from! { Data for Uint32Array if v => v.is_value() && cast::(v).is_uint32_array() } +impl_try_from! { Value for Uint32Array if v => v.is_uint32_array() } +impl_try_from! { Object for Uint32Array if v => v.is_uint32_array() } +impl_try_from! { ArrayBufferView for Uint32Array if v => v.is_uint32_array() } +impl_try_from! { TypedArray for Uint32Array if v => v.is_uint32_array() } +impl_eq! { for Uint32Array } +impl_hash! { for Uint32Array use get_identity_hash } +impl_partial_eq! { Data for Uint32Array use identity } +impl_partial_eq! { Value for Uint32Array use identity } +impl_partial_eq! { Object for Uint32Array use identity } +impl_partial_eq! { ArrayBufferView for Uint32Array use identity } +impl_partial_eq! { TypedArray for Uint32Array use identity } +impl_partial_eq! { Uint32Array for Uint32Array use identity } + +/// An instance of Uint8Array constructor (ES6 draft 15.13.6). +#[repr(C)] +#[derive(Debug)] +pub struct Uint8Array(Opaque); + +impl_deref! { TypedArray for Uint8Array } +impl_try_from! { Data for Uint8Array if v => v.is_value() && cast::(v).is_uint8_array() } +impl_try_from! { Value for Uint8Array if v => v.is_uint8_array() } +impl_try_from! { Object for Uint8Array if v => v.is_uint8_array() } +impl_try_from! { ArrayBufferView for Uint8Array if v => v.is_uint8_array() } +impl_try_from! { TypedArray for Uint8Array if v => v.is_uint8_array() } +impl_eq! { for Uint8Array } +impl_hash! { for Uint8Array use get_identity_hash } +impl_partial_eq! { Data for Uint8Array use identity } +impl_partial_eq! { Value for Uint8Array use identity } +impl_partial_eq! { Object for Uint8Array use identity } +impl_partial_eq! { ArrayBufferView for Uint8Array use identity } +impl_partial_eq! { TypedArray for Uint8Array use identity } +impl_partial_eq! { Uint8Array for Uint8Array use identity } + +/// An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6). +#[repr(C)] +#[derive(Debug)] +pub struct Uint8ClampedArray(Opaque); + +impl_deref! { TypedArray for Uint8ClampedArray } +impl_try_from! { Data for Uint8ClampedArray if v => v.is_value() && cast::(v).is_uint8_clamped_array() } +impl_try_from! { Value for Uint8ClampedArray if v => v.is_uint8_clamped_array() } +impl_try_from! { Object for Uint8ClampedArray if v => v.is_uint8_clamped_array() } +impl_try_from! { ArrayBufferView for Uint8ClampedArray if v => v.is_uint8_clamped_array() } +impl_try_from! { TypedArray for Uint8ClampedArray if v => v.is_uint8_clamped_array() } +impl_eq! { for Uint8ClampedArray } +impl_hash! { for Uint8ClampedArray use get_identity_hash } +impl_partial_eq! { Data for Uint8ClampedArray use identity } +impl_partial_eq! { Value for Uint8ClampedArray use identity } +impl_partial_eq! { Object for Uint8ClampedArray use identity } +impl_partial_eq! { ArrayBufferView for Uint8ClampedArray use identity } +impl_partial_eq! { TypedArray for Uint8ClampedArray use identity } +impl_partial_eq! { Uint8ClampedArray for Uint8ClampedArray use identity } + +/// A BigInt object (https://tc39.github.io/proposal-bigint) +#[repr(C)] +#[derive(Debug)] +pub struct BigIntObject(Opaque); + +impl_deref! { Object for BigIntObject } +impl_try_from! { Data for BigIntObject if v => v.is_value() && cast::(v).is_big_int_object() } +impl_try_from! { Value for BigIntObject if v => v.is_big_int_object() } +impl_try_from! { Object for BigIntObject if v => v.is_big_int_object() } +impl_eq! { for BigIntObject } +impl_hash! { for BigIntObject use get_identity_hash } +impl_partial_eq! { Data for BigIntObject use identity } +impl_partial_eq! { Value for BigIntObject use identity } +impl_partial_eq! { Object for BigIntObject use identity } +impl_partial_eq! { BigIntObject for BigIntObject use identity } + +/// A Boolean object (ECMA-262, 4.3.15). +#[repr(C)] +#[derive(Debug)] +pub struct BooleanObject(Opaque); + +impl_deref! { Object for BooleanObject } +impl_try_from! { Data for BooleanObject if v => v.is_value() && cast::(v).is_boolean_object() } +impl_try_from! { Value for BooleanObject if v => v.is_boolean_object() } +impl_try_from! { Object for BooleanObject if v => v.is_boolean_object() } +impl_eq! { for BooleanObject } +impl_hash! { for BooleanObject use get_identity_hash } +impl_partial_eq! { Data for BooleanObject use identity } +impl_partial_eq! { Value for BooleanObject use identity } +impl_partial_eq! { Object for BooleanObject use identity } +impl_partial_eq! { BooleanObject for BooleanObject use identity } + +/// An instance of the built-in Date constructor (ECMA-262, 15.9). +#[repr(C)] +#[derive(Debug)] +pub struct Date(Opaque); + +impl_deref! { Object for Date } +impl_try_from! { Data for Date if v => v.is_value() && cast::(v).is_date() } +impl_try_from! { Value for Date if v => v.is_date() } +impl_try_from! { Object for Date if v => v.is_date() } +impl_eq! { for Date } +impl_hash! { for Date use get_identity_hash } +impl_partial_eq! { Data for Date use identity } +impl_partial_eq! { Value for Date use identity } +impl_partial_eq! { Object for Date use identity } +impl_partial_eq! { Date for Date use identity } + +/// A JavaScript function object (ECMA-262, 15.3). +#[repr(C)] +#[derive(Debug)] +pub struct Function(Opaque); + +impl_deref! { Object for Function } +impl_try_from! { Data for Function if v => v.is_value() && cast::(v).is_function() } +impl_try_from! { Value for Function if v => v.is_function() } +impl_try_from! { Object for Function if v => v.is_function() } +impl_eq! { for Function } +impl_hash! { for Function use get_identity_hash } +impl_partial_eq! { Data for Function use identity } +impl_partial_eq! { Value for Function use identity } +impl_partial_eq! { Object for Function use identity } +impl_partial_eq! { Function for Function use identity } + +/// An instance of the built-in Map constructor (ECMA-262, 6th Edition, 23.1.1). +#[repr(C)] +#[derive(Debug)] +pub struct Map(Opaque); + +impl_deref! { Object for Map } +impl_try_from! { Data for Map if v => v.is_value() && cast::(v).is_map() } +impl_try_from! { Value for Map if v => v.is_map() } +impl_try_from! { Object for Map if v => v.is_map() } +impl_eq! { for Map } +impl_hash! { for Map use get_identity_hash } +impl_partial_eq! { Data for Map use identity } +impl_partial_eq! { Value for Map use identity } +impl_partial_eq! { Object for Map use identity } +impl_partial_eq! { Map for Map use identity } + +/// A Number object (ECMA-262, 4.3.21). +#[repr(C)] +#[derive(Debug)] +pub struct NumberObject(Opaque); + +impl_deref! { Object for NumberObject } +impl_try_from! { Data for NumberObject if v => v.is_value() && cast::(v).is_number_object() } +impl_try_from! { Value for NumberObject if v => v.is_number_object() } +impl_try_from! { Object for NumberObject if v => v.is_number_object() } +impl_eq! { for NumberObject } +impl_hash! { for NumberObject use get_identity_hash } +impl_partial_eq! { Data for NumberObject use identity } +impl_partial_eq! { Value for NumberObject use identity } +impl_partial_eq! { Object for NumberObject use identity } +impl_partial_eq! { NumberObject for NumberObject use identity } + +/// An instance of the built-in Promise constructor (ES6 draft). +#[repr(C)] +#[derive(Debug)] +pub struct Promise(Opaque); + +impl_deref! { Object for Promise } +impl_try_from! { Data for Promise if v => v.is_value() && cast::(v).is_promise() } +impl_try_from! { Value for Promise if v => v.is_promise() } +impl_try_from! { Object for Promise if v => v.is_promise() } +impl_eq! { for Promise } +impl_hash! { for Promise use get_identity_hash } +impl_partial_eq! { Data for Promise use identity } +impl_partial_eq! { Value for Promise use identity } +impl_partial_eq! { Object for Promise use identity } +impl_partial_eq! { Promise for Promise use identity } + +#[repr(C)] +#[derive(Debug)] +pub struct PromiseResolver(Opaque); + +impl_deref! { Object for PromiseResolver } +impl_eq! { for PromiseResolver } +impl_hash! { for PromiseResolver use get_identity_hash } +impl_partial_eq! { Data for PromiseResolver use identity } +impl_partial_eq! { Value for PromiseResolver use identity } +impl_partial_eq! { Object for PromiseResolver use identity } +impl_partial_eq! { PromiseResolver for PromiseResolver use identity } + +/// An instance of the built-in Proxy constructor (ECMA-262, 6th Edition, +/// 26.2.1). +#[repr(C)] +#[derive(Debug)] +pub struct Proxy(Opaque); + +impl_deref! { Object for Proxy } +impl_try_from! { Data for Proxy if v => v.is_value() && cast::(v).is_proxy() } +impl_try_from! { Value for Proxy if v => v.is_proxy() } +impl_try_from! { Object for Proxy if v => v.is_proxy() } +impl_eq! { for Proxy } +impl_hash! { for Proxy use get_identity_hash } +impl_partial_eq! { Data for Proxy use identity } +impl_partial_eq! { Value for Proxy use identity } +impl_partial_eq! { Object for Proxy use identity } +impl_partial_eq! { Proxy for Proxy use identity } + +/// An instance of the built-in RegExp constructor (ECMA-262, 15.10). +#[repr(C)] +#[derive(Debug)] +pub struct RegExp(Opaque); + +impl_deref! { Object for RegExp } +impl_try_from! { Data for RegExp if v => v.is_value() && cast::(v).is_reg_exp() } +impl_try_from! { Value for RegExp if v => v.is_reg_exp() } +impl_try_from! { Object for RegExp if v => v.is_reg_exp() } +impl_eq! { for RegExp } +impl_hash! { for RegExp use get_identity_hash } +impl_partial_eq! { Data for RegExp use identity } +impl_partial_eq! { Value for RegExp use identity } +impl_partial_eq! { Object for RegExp use identity } +impl_partial_eq! { RegExp for RegExp use identity } + +/// An instance of the built-in Set constructor (ECMA-262, 6th Edition, 23.2.1). +#[repr(C)] +#[derive(Debug)] +pub struct Set(Opaque); + +impl_deref! { Object for Set } +impl_try_from! { Data for Set if v => v.is_value() && cast::(v).is_set() } +impl_try_from! { Value for Set if v => v.is_set() } +impl_try_from! { Object for Set if v => v.is_set() } +impl_eq! { for Set } +impl_hash! { for Set use get_identity_hash } +impl_partial_eq! { Data for Set use identity } +impl_partial_eq! { Value for Set use identity } +impl_partial_eq! { Object for Set use identity } +impl_partial_eq! { Set for Set use identity } + +/// An instance of the built-in SharedArrayBuffer constructor. +#[repr(C)] +#[derive(Debug)] +pub struct SharedArrayBuffer(Opaque); + +impl_deref! { Object for SharedArrayBuffer } +impl_try_from! { Data for SharedArrayBuffer if v => v.is_value() && cast::(v).is_shared_array_buffer() } +impl_try_from! { Value for SharedArrayBuffer if v => v.is_shared_array_buffer() } +impl_try_from! { Object for SharedArrayBuffer if v => v.is_shared_array_buffer() } +impl_eq! { for SharedArrayBuffer } +impl_hash! { for SharedArrayBuffer use get_identity_hash } +impl_partial_eq! { Data for SharedArrayBuffer use identity } +impl_partial_eq! { Value for SharedArrayBuffer use identity } +impl_partial_eq! { Object for SharedArrayBuffer use identity } +impl_partial_eq! { SharedArrayBuffer for SharedArrayBuffer use identity } + +/// A String object (ECMA-262, 4.3.18). +#[repr(C)] +#[derive(Debug)] +pub struct StringObject(Opaque); + +impl_deref! { Object for StringObject } +impl_try_from! { Data for StringObject if v => v.is_value() && cast::(v).is_string_object() } +impl_try_from! { Value for StringObject if v => v.is_string_object() } +impl_try_from! { Object for StringObject if v => v.is_string_object() } +impl_eq! { for StringObject } +impl_hash! { for StringObject use get_identity_hash } +impl_partial_eq! { Data for StringObject use identity } +impl_partial_eq! { Value for StringObject use identity } +impl_partial_eq! { Object for StringObject use identity } +impl_partial_eq! { StringObject for StringObject use identity } + +/// A Symbol object (ECMA-262 edition 6). +#[repr(C)] +#[derive(Debug)] +pub struct SymbolObject(Opaque); + +impl_deref! { Object for SymbolObject } +impl_try_from! { Data for SymbolObject if v => v.is_value() && cast::(v).is_symbol_object() } +impl_try_from! { Value for SymbolObject if v => v.is_symbol_object() } +impl_try_from! { Object for SymbolObject if v => v.is_symbol_object() } +impl_eq! { for SymbolObject } +impl_hash! { for SymbolObject use get_identity_hash } +impl_partial_eq! { Data for SymbolObject use identity } +impl_partial_eq! { Value for SymbolObject use identity } +impl_partial_eq! { Object for SymbolObject use identity } +impl_partial_eq! { SymbolObject for SymbolObject use identity } + +#[repr(C)] +#[derive(Debug)] +pub struct WasmMemoryObject(Opaque); + +impl_deref! { Object for WasmMemoryObject } +impl_try_from! { Data for WasmMemoryObject if v => v.is_value() && cast::(v).is_wasm_memory_object() } +impl_try_from! { Value for WasmMemoryObject if v => v.is_wasm_memory_object() } +impl_try_from! { Object for WasmMemoryObject if v => v.is_wasm_memory_object() } +impl_eq! { for WasmMemoryObject } +impl_hash! { for WasmMemoryObject use get_identity_hash } +impl_partial_eq! { Data for WasmMemoryObject use identity } +impl_partial_eq! { Value for WasmMemoryObject use identity } +impl_partial_eq! { Object for WasmMemoryObject use identity } +impl_partial_eq! { WasmMemoryObject for WasmMemoryObject use identity } + +#[repr(C)] +#[derive(Debug)] +pub struct WasmModuleObject(Opaque); + +impl_deref! { Object for WasmModuleObject } +impl_try_from! { Data for WasmModuleObject if v => v.is_value() && cast::(v).is_wasm_module_object() } +impl_try_from! { Value for WasmModuleObject if v => v.is_wasm_module_object() } +impl_try_from! { Object for WasmModuleObject if v => v.is_wasm_module_object() } +impl_eq! { for WasmModuleObject } +impl_hash! { for WasmModuleObject use get_identity_hash } +impl_partial_eq! { Data for WasmModuleObject use identity } +impl_partial_eq! { Value for WasmModuleObject use identity } +impl_partial_eq! { Object for WasmModuleObject use identity } +impl_partial_eq! { WasmModuleObject for WasmModuleObject use identity } + +/// The superclass of primitive values. See ECMA-262 4.3.2. +#[repr(C)] +#[derive(Debug)] +pub struct Primitive(Opaque); + +impl_deref! { Value for Primitive } +impl_try_from! { Data for Primitive if v => v.is_primitive() } +impl_try_from! { Value for Primitive if v => v.is_primitive() } +impl_from! { BigInt for Primitive } +impl_from! { Boolean for Primitive } +impl_from! { Name for Primitive } +impl_from! { String for Primitive } +impl_from! { Symbol for Primitive } +impl_from! { Number for Primitive } +impl_from! { Integer for Primitive } +impl_from! { Int32 for Primitive } +impl_from! { Uint32 for Primitive } +impl_eq! { for Primitive } +impl_hash! { for Primitive use get_hash } +impl_partial_eq! { Value for Primitive use same_value_zero } +impl_partial_eq! { Primitive for Primitive use same_value_zero } +impl_partial_eq! { BigInt for Primitive use same_value_zero } +impl_partial_eq! { Boolean for Primitive use identity } +impl_partial_eq! { Name for Primitive use same_value_zero } +impl_partial_eq! { String for Primitive use same_value_zero } +impl_partial_eq! { Symbol for Primitive use identity } +impl_partial_eq! { Number for Primitive use same_value_zero } +impl_partial_eq! { Integer for Primitive use same_value_zero } +impl_partial_eq! { Int32 for Primitive use same_value_zero } +impl_partial_eq! { Uint32 for Primitive use same_value_zero } + +/// A JavaScript BigInt value (https://tc39.github.io/proposal-bigint) +#[repr(C)] +#[derive(Debug)] +pub struct BigInt(Opaque); + +impl_deref! { Primitive for BigInt } +impl_try_from! { Data for BigInt if v => v.is_big_int() } +impl_try_from! { Value for BigInt if v => v.is_big_int() } +impl_try_from! { Primitive for BigInt if v => v.is_big_int() } +impl_eq! { for BigInt } +impl_hash! { for BigInt use get_hash } +impl_partial_eq! { Value for BigInt use same_value_zero } +impl_partial_eq! { Primitive for BigInt use same_value_zero } +impl_partial_eq! { BigInt for BigInt use strict_equals } + +/// A primitive boolean value (ECMA-262, 4.3.14). Either the true +/// or false value. +#[repr(C)] +#[derive(Debug)] +pub struct Boolean(Opaque); + +impl_deref! { Primitive for Boolean } +impl_try_from! { Data for Boolean if v => v.is_boolean() } +impl_try_from! { Value for Boolean if v => v.is_boolean() } +impl_try_from! { Primitive for Boolean if v => v.is_boolean() } +impl_eq! { for Boolean } +impl_hash! { for Boolean use get_hash } +impl_partial_eq! { Data for Boolean use identity } +impl_partial_eq! { Value for Boolean use identity } +impl_partial_eq! { Primitive for Boolean use identity } +impl_partial_eq! { Boolean for Boolean use identity } + +/// A superclass for symbols and strings. +#[repr(C)] +#[derive(Debug)] +pub struct Name(Opaque); + +impl_deref! { Primitive for Name } +impl_try_from! { Data for Name if v => v.is_name() } +impl_try_from! { Value for Name if v => v.is_name() } +impl_try_from! { Primitive for Name if v => v.is_name() } +impl_from! { String for Name } +impl_from! { Symbol for Name } +impl_eq! { for Name } +impl_hash! { for Name use get_identity_hash } +impl_partial_eq! { Value for Name use same_value_zero } +impl_partial_eq! { Primitive for Name use same_value_zero } +impl_partial_eq! { Name for Name use strict_equals } +impl_partial_eq! { String for Name use strict_equals } +impl_partial_eq! { Symbol for Name use identity } + +/// A JavaScript string value (ECMA-262, 4.3.17). +#[repr(C)] +#[derive(Debug)] +pub struct String(Opaque); + +impl_deref! { Name for String } +impl_try_from! { Data for String if v => v.is_string() } +impl_try_from! { Value for String if v => v.is_string() } +impl_try_from! { Primitive for String if v => v.is_string() } +impl_try_from! { Name for String if v => v.is_string() } +impl_eq! { for String } +impl_hash! { for String use get_identity_hash } +impl_partial_eq! { Value for String use same_value_zero } +impl_partial_eq! { Primitive for String use same_value_zero } +impl_partial_eq! { Name for String use strict_equals } +impl_partial_eq! { String for String use strict_equals } + +/// A JavaScript symbol (ECMA-262 edition 6) +#[repr(C)] +#[derive(Debug)] +pub struct Symbol(Opaque); + +impl_deref! { Name for Symbol } +impl_try_from! { Data for Symbol if v => v.is_symbol() } +impl_try_from! { Value for Symbol if v => v.is_symbol() } +impl_try_from! { Primitive for Symbol if v => v.is_symbol() } +impl_try_from! { Name for Symbol if v => v.is_symbol() } +impl_eq! { for Symbol } +impl_hash! { for Symbol use get_identity_hash } +impl_partial_eq! { Data for Symbol use identity } +impl_partial_eq! { Value for Symbol use identity } +impl_partial_eq! { Primitive for Symbol use identity } +impl_partial_eq! { Name for Symbol use identity } +impl_partial_eq! { Symbol for Symbol use identity } + +/// A JavaScript number value (ECMA-262, 4.3.20) +#[repr(C)] +#[derive(Debug)] +pub struct Number(Opaque); + +impl_deref! { Primitive for Number } +impl_try_from! { Data for Number if v => v.is_number() } +impl_try_from! { Value for Number if v => v.is_number() } +impl_try_from! { Primitive for Number if v => v.is_number() } +impl_from! { Integer for Number } +impl_from! { Int32 for Number } +impl_from! { Uint32 for Number } +impl_eq! { for Number } +impl_hash! { for Number use get_hash } +impl_partial_eq! { Value for Number use same_value_zero } +impl_partial_eq! { Primitive for Number use same_value_zero } +impl_partial_eq! { Number for Number use same_value_zero } +impl_partial_eq! { Integer for Number use same_value_zero } +impl_partial_eq! { Int32 for Number use same_value_zero } +impl_partial_eq! { Uint32 for Number use same_value_zero } + +/// A JavaScript value representing a signed integer. +#[repr(C)] +#[derive(Debug)] +pub struct Integer(Opaque); + +impl_deref! { Number for Integer } +impl_try_from! { Data for Integer if v => v.is_number() && (cast::(v).is_int32() || cast::(v).is_uint32()) } +impl_try_from! { Value for Integer if v => v.is_int32() || v.is_uint32() } +impl_try_from! { Primitive for Integer if v => v.is_int32() || v.is_uint32() } +impl_try_from! { Number for Integer if v => v.is_int32() || v.is_uint32() } +impl_from! { Int32 for Integer } +impl_from! { Uint32 for Integer } +impl_eq! { for Integer } +impl_hash! { for Integer use get_hash } +impl_partial_eq! { Value for Integer use same_value_zero } +impl_partial_eq! { Primitive for Integer use same_value_zero } +impl_partial_eq! { Number for Integer use same_value_zero } +impl_partial_eq! { Integer for Integer use strict_equals } +impl_partial_eq! { Int32 for Integer use strict_equals } +impl_partial_eq! { Uint32 for Integer use strict_equals } + +/// A JavaScript value representing a 32-bit signed integer. +#[repr(C)] +#[derive(Debug)] +pub struct Int32(Opaque); + +impl_deref! { Integer for Int32 } +impl_try_from! { Data for Int32 if v => v.is_number() && cast::(v).is_int32() } +impl_try_from! { Value for Int32 if v => v.is_int32() } +impl_try_from! { Primitive for Int32 if v => v.is_int32() } +impl_try_from! { Number for Int32 if v => v.is_int32() } +impl_try_from! { Integer for Int32 if v => v.is_int32() } +impl_eq! { for Int32 } +impl_hash! { for Int32 use get_hash } +impl_partial_eq! { Value for Int32 use same_value_zero } +impl_partial_eq! { Primitive for Int32 use same_value_zero } +impl_partial_eq! { Number for Int32 use same_value_zero } +impl_partial_eq! { Integer for Int32 use strict_equals } +impl_partial_eq! { Int32 for Int32 use strict_equals } + +/// A JavaScript value representing a 32-bit unsigned integer. +#[repr(C)] +#[derive(Debug)] +pub struct Uint32(Opaque); + +impl_deref! { Integer for Uint32 } +impl_try_from! { Data for Uint32 if v => v.is_number() && cast::(v).is_uint32() } +impl_try_from! { Value for Uint32 if v => v.is_uint32() } +impl_try_from! { Primitive for Uint32 if v => v.is_uint32() } +impl_try_from! { Number for Uint32 if v => v.is_uint32() } +impl_try_from! { Integer for Uint32 if v => v.is_uint32() } +impl_eq! { for Uint32 } +impl_hash! { for Uint32 use get_hash } +impl_partial_eq! { Value for Uint32 use same_value_zero } +impl_partial_eq! { Primitive for Uint32 use same_value_zero } +impl_partial_eq! { Number for Uint32 use same_value_zero } +impl_partial_eq! { Integer for Uint32 use strict_equals } +impl_partial_eq! { Uint32 for Uint32 use strict_equals } diff --git a/crates/v8/src/date.rs b/crates/v8/src/date.rs new file mode 100644 index 0000000000..002a768741 --- /dev/null +++ b/crates/v8/src/date.rs @@ -0,0 +1,85 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +use crate::Context; +use crate::Date; +use crate::Local; +use crate::String; +use crate::scope::PinScope; + +unsafe extern "C" { + fn v8__Date__New(context: *const Context, value: f64) -> *const Date; + fn v8__Date__Parse( + context: *const Context, + date_string: *const String, + ) -> *const Date; + fn v8__Date__ValueOf(this: *const Date) -> f64; + fn v8__Date__ToISOString(this: *const Date) -> *const String; + fn v8__Date__ToUTCString(this: *const Date) -> *const String; +} + +/// An instance of the built-in Date constructor (ECMA-262, 15.9). +impl Date { + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_>, + value: f64, + ) -> Option> { + unsafe { + scope.cast_local(|sd| v8__Date__New(sd.get_current_context(), value)) + } + } + + /// Parses the given string as a date, as `Date.parse` does, and returns the + /// resulting Date object. + /// + /// Returns `None` if an exception was thrown; note that a string which is + /// not a valid date does not throw, but instead yields a Date whose + /// [`value_of()`] is `NaN`. + /// + /// [`value_of()`]: Date::value_of + #[inline(always)] + pub fn parse<'s>( + scope: &PinScope<'s, '_>, + date_string: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Date__Parse(sd.get_current_context(), &*date_string) + }) + } + } + + /// A specialization of Value::NumberValue that is more efficient + /// because we know the structure of this object. + #[inline(always)] + pub fn value_of(&self) -> f64 { + unsafe { v8__Date__ValueOf(self) } + } + + /// Generates ISO string representation. + /// + /// Unlike JavaScript's `Date.prototype.toISOString`, this does not throw + /// for a Date whose [`value_of()`] is `NaN`; it returns `"Invalid Date"`. + /// + /// [`value_of()`]: Date::value_of + #[inline(always)] + pub fn to_iso_string<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Local<'s, String> { + unsafe { scope.cast_local(|_| v8__Date__ToISOString(self)) }.unwrap() + } + + /// Generates UTC string representation. + /// + /// Returns `"Invalid Date"` for a Date whose [`value_of()`] is `NaN`. + /// + /// [`value_of()`]: Date::value_of + #[inline(always)] + pub fn to_utc_string<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Local<'s, String> { + unsafe { scope.cast_local(|_| v8__Date__ToUTCString(self)) }.unwrap() + } +} diff --git a/crates/v8/src/deno_inspector/BUILD.gn b/crates/v8/src/deno_inspector/BUILD.gn new file mode 100644 index 0000000000..69896310aa --- /dev/null +++ b/crates/v8/src/deno_inspector/BUILD.gn @@ -0,0 +1,110 @@ +# Copyright 2024 the Deno authors. All rights reserved. MIT license. + +import("//v8/gni/v8.gni") + +_inspector_protocol = "//v8/third_party/inspector_protocol" +import("$_inspector_protocol/inspector_protocol.gni") + +# Generated protocol sources for custom domains +_protocol_generated = [ + "protocol/Forward.h", + "protocol/Protocol.cpp", + "protocol/Protocol.h", + "protocol/IO.cpp", + "protocol/IO.h", + "protocol/Network.cpp", + "protocol/Network.h", + "protocol/NodeRuntime.cpp", + "protocol/NodeRuntime.h", + "protocol/NodeTracing.cpp", + "protocol/NodeTracing.h", + "protocol/NodeWorker.cpp", + "protocol/NodeWorker.h", + "protocol/Target.cpp", + "protocol/Target.h", +] + +# Custom inspector domain PDL files +_custom_pdl_files = [ + "domain_io.pdl", + "domain_network.pdl", + "domain_node_runtime.pdl", + "domain_node_tracing.pdl", + "domain_node_worker.pdl", + "domain_target.pdl", +] + +# Convert node_protocol.pdl (which includes the domain files) to JSON +action("convert_protocol_to_json") { + script = "$_inspector_protocol/convert_protocol_to_json.py" + inputs = [ "node_protocol.pdl" ] + _custom_pdl_files + outputs = [ "$target_gen_dir/node_protocol.json" ] + args = [ + rebase_path("node_protocol.pdl", root_build_dir), + rebase_path(outputs[0], root_build_dir), + ] +} + +# Generate C++ protocol code from the PDL +inspector_protocol_generate("protocol_generated_sources") { + deps = [ ":convert_protocol_to_json" ] + + inspector_protocol_dir = _inspector_protocol + out_dir = target_gen_dir + _protocol_path = rebase_path("$target_gen_dir/node_protocol.json", root_build_dir) + config_values = [ "protocol.path=$_protocol_path" ] + config_file = "inspector_protocol_config.json" + inputs = [ + "$target_gen_dir/node_protocol.json", + "inspector_protocol_config.json", + ] + outputs = _protocol_generated +} + +# Concatenate V8's protocol with our custom protocol for full schema +action("concatenate_protocols") { + script = "$_inspector_protocol/concatenate_protocols.py" + deps = [ ":convert_protocol_to_json" ] + inputs = [ + v8_inspector_js_protocol, + "$target_gen_dir/node_protocol.json", + ] + outputs = [ "$target_gen_dir/concatenated_protocol.json" ] + args = rebase_path(inputs + outputs, root_build_dir) +} + +config("inspector_protocol_config") { + include_dirs = [ + "$target_gen_dir", + "//v8/third_party/inspector_protocol", + "//v8", + ] +} + +# Source set for the generated protocol code +source_set("deno_inspector_protocol") { + sources = rebase_path(_protocol_generated, ".", target_gen_dir) + sources += [ + "//src/deno_inspector/string_util.cc", + "//src/deno_inspector/string_util.h", + ] + + public_deps = [ + ":protocol_generated_sources", + "//v8/third_party/inspector_protocol:crdtp", + "//v8/src/inspector:inspector", + ] + + deps = [ + "//v8:v8_libbase", + ] + + public_configs = [ ":inspector_protocol_config" ] + + include_dirs = [ + "//", + "//v8", + "//v8/include", + "$root_gen_dir/v8/include/inspector", + ] +} diff --git a/crates/v8/src/deno_inspector/domain_io.pdl b/crates/v8/src/deno_inspector/domain_io.pdl new file mode 100644 index 0000000000..2500db1c43 --- /dev/null +++ b/crates/v8/src/deno_inspector/domain_io.pdl @@ -0,0 +1,24 @@ +# Partial support for IO domain of ChromeDevTools Protocol. +# https://github.com/ChromeDevTools/devtools-protocol/blob/master/pdl/domains/IO.pdl + +domain IO + type StreamHandle extends string + # Read a chunk of the stream + command read + parameters + # Handle of the stream to read. + StreamHandle handle + # Seek to the specified offset before reading (if not specified, proceed with offset + # following the last read). Some types of streams may only support sequential reads. + optional integer offset + # Maximum number of bytes to read (left upon the agent discretion if not specified). + optional integer size + returns + # Data that were read. + string data + # Set if the end-of-file condition occurred while reading. + boolean eof + command close + parameters + # Handle of the stream to close. + StreamHandle handle diff --git a/crates/v8/src/deno_inspector/domain_network.pdl b/crates/v8/src/deno_inspector/domain_network.pdl new file mode 100644 index 0000000000..fdfd9ebe1f --- /dev/null +++ b/crates/v8/src/deno_inspector/domain_network.pdl @@ -0,0 +1,228 @@ +# Partial support for Network domain of ChromeDevTools Protocol. +# https://github.com/ChromeDevTools/devtools-protocol/blob/master/pdl/domains/Network.pdl + +experimental domain Network + depends on Runtime + + # Resource type as it was perceived by the rendering engine. + type ResourceType extends string + enum + Document + Stylesheet + Image + Media + Font + Script + TextTrack + XHR + Fetch + Prefetch + EventSource + WebSocket + Manifest + SignedExchange + Ping + CSPViolationReport + Preflight + Other + + # Unique request identifier. + type RequestId extends string + + # UTC time in seconds, counted from January 1, 1970. + type TimeSinceEpoch extends number + + # Monotonically increasing time in seconds since an arbitrary point in the past. + type MonotonicTime extends number + + # Information about the request initiator. + type Initiator extends object + properties + # Type of this initiator. + enum type + parser + script + preload + SignedExchange + preflight + other + # Initiator JavaScript stack trace, set for Script only. + # Requires the Debugger domain to be enabled. + optional Runtime.StackTrace stack + # Initiator URL, set for Parser type or for Script type (when script is importing module) or for SignedExchange type. + optional string url + # Initiator line number, set for Parser type or for Script type (when script is importing + # module) (0-based). + optional number lineNumber + # Initiator column number, set for Parser type or for Script type (when script is importing + # module) (0-based). + optional number columnNumber + # Set if another request triggered this request (e.g. preflight). + optional RequestId requestId + + # HTTP request data. + type Request extends object + properties + string url + string method + Headers headers + boolean hasPostData + + # HTTP response data. + type Response extends object + properties + string url + integer status + string statusText + Headers headers + string mimeType + string charset + + # Request / response headers as keys / values of JSON object. + type Headers extends object + + type LoadNetworkResourcePageResult extends object + properties + boolean success + optional IO.StreamHandle stream + + # WebSocket response data. + type WebSocketResponse extends object + properties + # HTTP response status code. + integer status + # HTTP response status text. + string statusText + # HTTP response headers. + Headers headers + + # Disables network tracking, prevents network events from being sent to the client. + command disable + + # Enables network tracking, network events will now be delivered to the client. + command enable + parameters + # Buffer size in bytes to use when preserving network payloads (XHRs, etc). + experimental optional integer maxTotalBufferSize + # Per-resource buffer size in bytes to use when preserving network payloads (XHRs, etc). + experimental optional integer maxResourceBufferSize + + # Returns post data sent with the request. Returns an error when no data was sent with the request. + command getRequestPostData + parameters + # Identifier of the network request to get content for. + RequestId requestId + returns + # Request body string, omitting files from multipart requests + string postData + + # Returns content served for the given request. + command getResponseBody + parameters + # Identifier of the network request to get content for. + RequestId requestId + returns + # Response body. + string body + # True, if content was sent as base64. + boolean base64Encoded + + # Enables streaming of the response for the given requestId. + # If enabled, the dataReceived event contains the data that was received during streaming. + experimental command streamResourceContent + parameters + # Identifier of the request to stream. + RequestId requestId + returns + # Data that has been buffered until streaming is enabled. + binary bufferedData + # Fetches the resource and returns the content. + command loadNetworkResource + parameters + # URL of the resource to get content for. + string url + returns + LoadNetworkResourcePageResult resource + + # Fired when page is about to send HTTP request. + event requestWillBeSent + parameters + # Request identifier. + RequestId requestId + # Request data. + Request request + # Request initiator. + Initiator initiator + # Timestamp. + MonotonicTime timestamp + # Timestamp. + TimeSinceEpoch wallTime + + # Fired when HTTP response is available. + event responseReceived + parameters + # Request identifier. + RequestId requestId + # Timestamp. + MonotonicTime timestamp + # Resource type. + ResourceType type + # Response data. + Response response + + event loadingFailed + parameters + # Request identifier. + RequestId requestId + # Timestamp. + MonotonicTime timestamp + # Resource type. + ResourceType type + # Error message. + string errorText + + event loadingFinished + parameters + # Request identifier. + RequestId requestId + # Timestamp. + MonotonicTime timestamp + + # Fired when data chunk was received over the network. + event dataReceived + parameters + # Request identifier. + RequestId requestId + # Timestamp. + MonotonicTime timestamp + # Data chunk length. + integer dataLength + # Actual bytes received (might be less than dataLength for compressed encodings). + integer encodedDataLength + # Data that was received. + experimental optional binary data + # Fired upon WebSocket creation. + event webSocketCreated + parameters + # Request identifier. + RequestId requestId + # WebSocket request URL. + string url + # Request initiator. + Initiator initiator + # Fired when WebSocket is closed. + event webSocketClosed + parameters + # Request identifier. + RequestId requestId + # Timestamp. + MonotonicTime timestamp + # Fired when WebSocket handshake response becomes available. + event webSocketHandshakeResponseReceived + parameters + # Request identifier. + RequestId requestId + # Timestamp. + MonotonicTime timestamp + # WebSocket response data. + WebSocketResponse response diff --git a/crates/v8/src/deno_inspector/domain_node_runtime.pdl b/crates/v8/src/deno_inspector/domain_node_runtime.pdl new file mode 100644 index 0000000000..6d3e812a64 --- /dev/null +++ b/crates/v8/src/deno_inspector/domain_node_runtime.pdl @@ -0,0 +1,22 @@ +# Support for inspecting node process state. +experimental domain NodeRuntime + # Enable the NodeRuntime events except by `NodeRuntime.waitingForDisconnect`. + command enable + + # Disable NodeRuntime events + command disable + + # Enable the `NodeRuntime.waitingForDisconnect`. + command notifyWhenWaitingForDisconnect + parameters + boolean enabled + + # This event is fired instead of `Runtime.executionContextDestroyed` when + # enabled. + # It is fired when the Node process finished all code execution and is + # waiting for all frontends to disconnect. + event waitingForDisconnect + + # This event is fired when the runtime is waiting for the debugger. For + # example, when inspector.waitingForDebugger is called + event waitingForDebugger diff --git a/crates/v8/src/deno_inspector/domain_node_tracing.pdl b/crates/v8/src/deno_inspector/domain_node_tracing.pdl new file mode 100644 index 0000000000..3da8514b67 --- /dev/null +++ b/crates/v8/src/deno_inspector/domain_node_tracing.pdl @@ -0,0 +1,34 @@ +experimental domain NodeTracing + type TraceConfig extends object + properties + # Controls how the trace buffer stores data. + optional enum recordMode + recordUntilFull + recordContinuously + recordAsMuchAsPossible + # Included category filters. + array of string includedCategories + + # Gets supported tracing categories. + command getCategories + returns + # A list of supported tracing categories. + array of string categories + + # Start trace events collection. + command start + parameters + TraceConfig traceConfig + + # Stop trace events collection. Remaining collected events will be sent as a sequence of + # dataCollected events followed by tracingComplete event. + command stop + + # Contains an bucket of collected trace events. + event dataCollected + parameters + array of object value + + # Signals that tracing is stopped and there is no trace buffers pending flush, all data were + # delivered via dataCollected events. + event tracingComplete diff --git a/crates/v8/src/deno_inspector/domain_node_worker.pdl b/crates/v8/src/deno_inspector/domain_node_worker.pdl new file mode 100644 index 0000000000..785ed714a1 --- /dev/null +++ b/crates/v8/src/deno_inspector/domain_node_worker.pdl @@ -0,0 +1,59 @@ +# Support for sending messages to Node worker Inspector instances. +experimental domain NodeWorker + + type WorkerID extends string + + # Unique identifier of attached debugging session. + type SessionID extends string + + type WorkerInfo extends object + properties + WorkerID workerId + string type + string title + string url + + # Sends protocol message over session with given id. + command sendMessageToWorker + parameters + string message + # Identifier of the session. + SessionID sessionId + + # Instructs the inspector to attach to running workers. Will also attach to new workers + # as they start + command enable + parameters + # Whether to new workers should be paused until the frontend sends `Runtime.runIfWaitingForDebugger` + # message to run them. + boolean waitForDebuggerOnStart + + # Detaches from all running workers and disables attaching to new workers as they are started. + command disable + + # Detached from the worker with given sessionId. + command detach + parameters + SessionID sessionId + + # Issued when attached to a worker. + event attachedToWorker + parameters + # Identifier assigned to the session used to send/receive messages. + SessionID sessionId + WorkerInfo workerInfo + boolean waitingForDebugger + + # Issued when detached from the worker. + event detachedFromWorker + parameters + # Detached session identifier. + SessionID sessionId + + # Notifies about a new protocol message received from the session + # (session ID is provided in attachedToWorker notification). + event receivedMessageFromWorker + parameters + # Identifier of a session which sends a message. + SessionID sessionId + string message diff --git a/crates/v8/src/deno_inspector/domain_target.pdl b/crates/v8/src/deno_inspector/domain_target.pdl new file mode 100644 index 0000000000..3195b3a441 --- /dev/null +++ b/crates/v8/src/deno_inspector/domain_target.pdl @@ -0,0 +1,26 @@ +# Partial support for IO domain of ChromeDevTools Protocol. +# https://github.com/ChromeDevTools/devtools-protocol/blob/master/pdl/domains/Target.pdl + +experimental domain Target + type SessionID extends string + type TargetID extends string + type TargetInfo extends object + properties + TargetID targetId + string type + string title + string url + boolean attached + boolean canAccessOpener + event targetCreated + parameters + TargetInfo targetInfo + event attachedToTarget + parameters + SessionID sessionId + TargetInfo targetInfo + boolean waitingForDebugger + command setAutoAttach + parameters + boolean autoAttach + boolean waitForDebuggerOnStart diff --git a/crates/v8/src/deno_inspector/inspector_protocol_config.json b/crates/v8/src/deno_inspector/inspector_protocol_config.json new file mode 100644 index 0000000000..5371f13627 --- /dev/null +++ b/crates/v8/src/deno_inspector/inspector_protocol_config.json @@ -0,0 +1,26 @@ +{ + "protocol": { + "package": "src/deno_inspector/protocol", + "output": "protocol", + "namespace": ["deno_inspector", "protocol"] + }, + "imported": { + "path": "../../v8/include/js_protocol.pdl", + "header": "", + "namespace": ["v8_inspector", "protocol"], + "options": [ + { + "domain": "Runtime", + "imported": ["StackTrace", "RemoteObject", "ExecutionContextId", "StackTraceId"] + } + ] + }, + "lib": { + "package": "src/deno_inspector/protocol", + "output": "protocol", + "string_header": "src/deno_inspector/string_util.h" + }, + "crdtp": { + "namespace": "v8_crdtp" + } +} diff --git a/crates/v8/src/deno_inspector/node_protocol.pdl b/crates/v8/src/deno_inspector/node_protocol.pdl new file mode 100644 index 0000000000..87b3d93d39 --- /dev/null +++ b/crates/v8/src/deno_inspector/node_protocol.pdl @@ -0,0 +1,11 @@ +# Please notify @nodejs/v8-inspector and @nodejs/trace-events before modifying this file +version + major 1 + minor 0 + +include domain_io.pdl +include domain_network.pdl +include domain_node_runtime.pdl +include domain_node_tracing.pdl +include domain_node_worker.pdl +include domain_target.pdl diff --git a/crates/v8/src/deno_inspector/string_util.cc b/crates/v8/src/deno_inspector/string_util.cc new file mode 100644 index 0000000000..09b5118601 --- /dev/null +++ b/crates/v8/src/deno_inspector/string_util.cc @@ -0,0 +1,197 @@ +// String utilities for Deno inspector protocol + +#include "src/deno_inspector/string_util.h" + +#include "v8/third_party/inspector_protocol/crdtp/json.h" + +namespace v8_crdtp { + +bool ProtocolTypeTraits::Deserialize(DeserializerState* state, + std::string* value) { + if (state->tokenizer()->TokenTag() == cbor::CBORTokenTag::STRING8) { + span cbor_span = state->tokenizer()->GetString8(); + value->assign(reinterpret_cast(cbor_span.data()), + cbor_span.size()); + return true; + } + if (state->tokenizer()->TokenTag() == cbor::CBORTokenTag::STRING16) { + span utf16le = state->tokenizer()->GetString16WireRep(); + *value = deno_inspector::protocol::StringUtil::fromUTF16LE( + reinterpret_cast(utf16le.data()), + utf16le.size() / sizeof(uint16_t)); + return true; + } + return false; +} + +void ProtocolTypeTraits::Serialize(const std::string& value, + std::vector* bytes) { + cbor::EncodeString8(SpanFrom(value), bytes); +} + +bool ProtocolTypeTraits::Deserialize( + DeserializerState* state, deno_inspector::protocol::Binary* value) { + if (state->tokenizer()->TokenTag() != cbor::CBORTokenTag::BINARY) { + return false; + } + span cbor_span = state->tokenizer()->GetBinary(); + *value = deno_inspector::protocol::Binary::fromSpan(cbor_span.data(), + cbor_span.size()); + return true; +} + +void ProtocolTypeTraits::Serialize( + const deno_inspector::protocol::Binary& value, + std::vector* bytes) { + cbor::EncodeString8(SpanFrom(value.toBase64()), bytes); +} + +} // namespace v8_crdtp + +namespace deno_inspector { +namespace protocol { + +String StringUtil::StringViewToUtf8(v8_inspector::StringView view) { + if (view.length() == 0) return ""; + if (view.is8Bit()) { + return std::string(reinterpret_cast(view.characters8()), + view.length()); + } + return fromUTF16(view.characters16(), view.length()); +} + +String StringUtil::fromUTF16(const uint16_t* data, size_t length) { + // Simple UTF-16 to UTF-8 conversion + std::string result; + result.reserve(length * 3); // Worst case + for (size_t i = 0; i < length; i++) { + uint32_t code_point = data[i]; + // Handle surrogate pairs + if (code_point >= 0xD800 && code_point <= 0xDBFF && i + 1 < length) { + uint32_t low = data[i + 1]; + if (low >= 0xDC00 && low <= 0xDFFF) { + code_point = 0x10000 + ((code_point - 0xD800) << 10) + (low - 0xDC00); + i++; + } + } + // Encode to UTF-8 + if (code_point < 0x80) { + result.push_back(static_cast(code_point)); + } else if (code_point < 0x800) { + result.push_back(static_cast(0xC0 | (code_point >> 6))); + result.push_back(static_cast(0x80 | (code_point & 0x3F))); + } else if (code_point < 0x10000) { + result.push_back(static_cast(0xE0 | (code_point >> 12))); + result.push_back(static_cast(0x80 | ((code_point >> 6) & 0x3F))); + result.push_back(static_cast(0x80 | (code_point & 0x3F))); + } else { + result.push_back(static_cast(0xF0 | (code_point >> 18))); + result.push_back(static_cast(0x80 | ((code_point >> 12) & 0x3F))); + result.push_back(static_cast(0x80 | ((code_point >> 6) & 0x3F))); + result.push_back(static_cast(0x80 | (code_point & 0x3F))); + } + } + return result; +} + +String StringUtil::fromUTF8(const uint8_t* data, size_t length) { + return std::string(reinterpret_cast(data), length); +} + +String StringUtil::fromUTF16LE(const uint16_t* data, size_t length) { + return fromUTF16(data, length); // Assuming host is little-endian +} + +const uint8_t* StringUtil::CharactersUTF8(const String& s) { + return reinterpret_cast(s.data()); +} + +size_t StringUtil::CharacterCount(const String& s) { return s.length(); } + +// Base64 encoding table +static const char kBase64Chars[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +String Binary::toBase64() const { + std::string result; + size_t len = bytes_->size(); + const uint8_t* data = bytes_->data(); + result.reserve(((len + 2) / 3) * 4); + + for (size_t i = 0; i < len; i += 3) { + uint32_t n = static_cast(data[i]) << 16; + if (i + 1 < len) n |= static_cast(data[i + 1]) << 8; + if (i + 2 < len) n |= static_cast(data[i + 2]); + + result.push_back(kBase64Chars[(n >> 18) & 0x3F]); + result.push_back(kBase64Chars[(n >> 12) & 0x3F]); + result.push_back(i + 1 < len ? kBase64Chars[(n >> 6) & 0x3F] : '='); + result.push_back(i + 2 < len ? kBase64Chars[n & 0x3F] : '='); + } + return result; +} + +Binary Binary::concat(const std::vector& binaries) { + size_t total_size = 0; + for (const auto& binary : binaries) { + total_size += binary.size(); + } + auto bytes = std::make_shared>(total_size); + uint8_t* data_ptr = bytes->data(); + for (const auto& binary : binaries) { + memcpy(data_ptr, binary.data(), binary.size()); + data_ptr += binary.size(); + } + return Binary(bytes); +} + +// Base64 decoding table +static const uint8_t kBase64DecodeTable[256] = { + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, + 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, + 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 255, 255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33, + 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, +}; + +Binary Binary::fromBase64(const String& base64, bool* success) { + *success = true; + auto bytes = std::make_shared>(); + bytes->reserve((base64.size() * 3) / 4); + + uint32_t buffer = 0; + int bits_collected = 0; + + for (char c : base64) { + if (c == '=') break; + uint8_t val = kBase64DecodeTable[static_cast(c)]; + if (val == 255) { + *success = false; + return Binary(); + } + buffer = (buffer << 6) | val; + bits_collected += 6; + if (bits_collected >= 8) { + bits_collected -= 8; + bytes->push_back(static_cast((buffer >> bits_collected) & 0xFF)); + } + } + + return Binary(bytes); +} + +} // namespace protocol +} // namespace deno_inspector diff --git a/crates/v8/src/deno_inspector/string_util.h b/crates/v8/src/deno_inspector/string_util.h new file mode 100644 index 0000000000..02a55b92a8 --- /dev/null +++ b/crates/v8/src/deno_inspector/string_util.h @@ -0,0 +1,99 @@ +// Based on Node's node_string.h +#ifndef SRC_DENO_INSPECTOR_STRING_UTIL_H_ +#define SRC_DENO_INSPECTOR_STRING_UTIL_H_ + +#include +#include +#include +#include +#include +#include + +#include "v8-inspector.h" +#include "v8/third_party/inspector_protocol/crdtp/protocol_core.h" +#include "v8/third_party/inspector_protocol/crdtp/span.h" + +// Provide DCHECK macros that the generated protocol code expects +#ifndef DCHECK +#define DCHECK(condition) assert(condition) +#endif +#ifndef DCHECK_LT +#define DCHECK_LT(a, b) assert((a) < (b)) +#endif + +namespace deno_inspector::protocol { +class Binary; +} + +namespace v8_crdtp { + +template <> +struct ProtocolTypeTraits { + static bool Deserialize(DeserializerState* state, std::string* value); + static void Serialize(const std::string& value, std::vector* bytes); +}; + +template <> +struct ProtocolTypeTraits { + static bool Deserialize(DeserializerState* state, + deno_inspector::protocol::Binary* value); + static void Serialize(const deno_inspector::protocol::Binary& value, + std::vector* bytes); +}; + +} // namespace v8_crdtp + +namespace deno_inspector { +namespace protocol { + +class Value; + +using String = std::string; +using StringBuilder = std::ostringstream; +using ProtocolMessage = std::string; + +struct StringUtil { + static String StringViewToUtf8(v8_inspector::StringView view); + static String fromUTF16(const uint16_t* data, size_t length); + static String fromUTF8(const uint8_t* data, size_t length); + static String fromUTF16LE(const uint16_t* data, size_t length); + static const uint8_t* CharactersUTF8(const String& s); + static size_t CharacterCount(const String& s); + + inline static uint8_t* CharactersLatin1(const String& s) { return nullptr; } + inline static const uint16_t* CharactersUTF16(const String& s) { + return nullptr; + } +}; + +// A read-only sequence of uninterpreted bytes with reference-counted storage. +class Binary { + public: + Binary() : bytes_(std::make_shared>()) {} + + const uint8_t* data() const { return bytes_->data(); } + size_t size() const { return bytes_->size(); } + + String toBase64() const; + + static Binary concat(const std::vector& binaries); + static Binary fromBase64(const String& base64, bool* success); + static Binary fromSpan(const uint8_t* data, size_t size) { + return Binary(std::make_shared>(data, data + size)); + } + // Overload for v8_crdtp::span used by generated protocol code + static Binary fromSpan(v8_crdtp::span bytes) { + return fromSpan(bytes.data(), bytes.size()); + } + + private: + std::shared_ptr> bytes_; + + explicit Binary(std::shared_ptr> bytes) + : bytes_(bytes) {} +}; + +} // namespace protocol +} // namespace deno_inspector + +#endif // SRC_DENO_INSPECTOR_STRING_UTIL_H_ diff --git a/crates/v8/src/exception.rs b/crates/v8/src/exception.rs new file mode 100644 index 0000000000..72868fa946 --- /dev/null +++ b/crates/v8/src/exception.rs @@ -0,0 +1,452 @@ +#![allow(non_snake_case)] + +use std::convert::TryInto; + +use crate::Context; +use crate::Local; +use crate::Message; +use crate::Object; +use crate::StackFrame; +use crate::StackTrace; +use crate::String; +use crate::Value; +use crate::isolate::RealIsolate; +use crate::scope::PinScope; +use crate::support::MaybeBool; +use crate::support::int; + +unsafe extern "C" { + fn v8__Message__Get(this: *const Message) -> *const String; + fn v8__Message__GetSourceLine( + this: *const Message, + context: *const Context, + ) -> *const String; + fn v8__Message__GetScriptResourceName(this: *const Message) -> *const Value; + fn v8__Message__GetLineNumber( + this: *const Message, + context: *const Context, + ) -> int; + fn v8__Message__GetStartPosition(this: *const Message) -> int; + fn v8__Message__GetEndPosition(this: *const Message) -> int; + fn v8__Message__GetWasmFunctionIndex(this: *const Message) -> int; + fn v8__Message__ErrorLevel(this: *const Message) -> int; + fn v8__Message__GetStartColumn(this: *const Message) -> int; + fn v8__Message__GetEndColumn(this: *const Message) -> int; + fn v8__Message__IsSharedCrossOrigin(this: *const Message) -> bool; + fn v8__Message__IsOpaque(this: *const Message) -> bool; + fn v8__Message__GetStackTrace(this: *const Message) -> *const StackTrace; + + fn v8__StackTrace__CurrentStackTrace( + isolate: *mut RealIsolate, + frame_limit: int, + ) -> *const StackTrace; + fn v8__StackTrace__CurrentScriptNameOrSourceURL( + isolate: *mut RealIsolate, + ) -> *const String; + fn v8__StackTrace__GetFrameCount(this: *const StackTrace) -> int; + fn v8__StackTrace__GetFrame( + this: *const StackTrace, + isolate: *mut RealIsolate, + index: u32, + ) -> *const StackFrame; + + fn v8__StackFrame__GetLineNumber(this: *const StackFrame) -> int; + fn v8__StackFrame__GetColumn(this: *const StackFrame) -> int; + fn v8__StackFrame__GetScriptId(this: *const StackFrame) -> int; + fn v8__StackFrame__GetScriptName(this: *const StackFrame) -> *const String; + fn v8__StackFrame__GetScriptNameOrSourceURL( + this: *const StackFrame, + ) -> *const String; + fn v8__StackFrame__GetScriptSource(this: *const StackFrame) -> *const String; + fn v8__StackFrame__GetScriptSourceMappingURL( + this: *const StackFrame, + ) -> *const String; + fn v8__StackFrame__GetFunctionName(this: *const StackFrame) -> *const String; + fn v8__StackFrame__IsEval(this: *const StackFrame) -> bool; + fn v8__StackFrame__IsConstructor(this: *const StackFrame) -> bool; + fn v8__StackFrame__IsWasm(this: *const StackFrame) -> bool; + fn v8__StackFrame__IsUserJavaScript(this: *const StackFrame) -> bool; + + fn v8__Exception__Error(message: *const String) -> *const Value; + fn v8__Exception__RangeError(message: *const String) -> *const Value; + fn v8__Exception__ReferenceError(message: *const String) -> *const Value; + fn v8__Exception__SyntaxError(message: *const String) -> *const Value; + fn v8__Exception__TypeError(message: *const String) -> *const Value; + + fn v8__Exception__CreateMessage( + isolate: *mut RealIsolate, + exception: *const Value, + ) -> *const Message; + fn v8__Exception__GetStackTrace(exception: *const Value) + -> *const StackTrace; + fn v8__Exception__CaptureStackTrace( + context: *const Context, + object: *const Object, + ) -> MaybeBool; +} + +impl StackTrace { + /// Grab a snapshot of the current JavaScript execution stack. + #[inline(always)] + pub fn current_stack_trace<'s>( + scope: &PinScope<'s, '_>, + frame_limit: usize, + ) -> Option> { + let frame_limit = frame_limit.try_into().ok()?; + unsafe { + scope.cast_local(|sd| { + v8__StackTrace__CurrentStackTrace(sd.get_isolate_ptr(), frame_limit) + }) + } + } + + /// Returns the first valid script name or source URL starting at the top of + /// the JS stack. The returned string is either an empty handle if no script + /// name/url was found or a non-zero-length string. + /// + /// This method is equivalent to calling StackTrace::CurrentStackTrace and + /// walking the resulting frames from the beginning until a non-empty script + /// name/url is found. The difference is that this method won't allocate + /// a stack trace. + /// + #[inline(always)] + pub fn current_script_name_or_source_url<'s>( + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__StackTrace__CurrentScriptNameOrSourceURL(sd.get_isolate_ptr()) + }) + } + } + + /// Returns the number of StackFrames. + #[inline(always)] + pub fn get_frame_count(&self) -> usize { + unsafe { v8__StackTrace__GetFrameCount(self) as usize } + } + + /// Returns a StackFrame at a particular index. + #[inline(always)] + pub fn get_frame<'s>( + &self, + scope: &PinScope<'s, '_>, + index: usize, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__StackTrace__GetFrame(self, sd.get_isolate_ptr(), index as u32) + }) + } + } +} + +impl StackFrame { + /// Returns the number, 1-based, of the line for the associated function call. + /// This method will return Message::kNoLineNumberInfo if it is unable to + /// retrieve the line number, or if kLineNumber was not passed as an option + /// when capturing the StackTrace. + #[inline(always)] + pub fn get_line_number(&self) -> usize { + unsafe { v8__StackFrame__GetLineNumber(self) as usize } + } + + /// Returns the 1-based column offset on the line for the associated function + /// call. + /// This method will return Message::kNoColumnInfo if it is unable to retrieve + /// the column number, or if kColumnOffset was not passed as an option when + /// capturing the StackTrace. + #[inline(always)] + pub fn get_column(&self) -> usize { + unsafe { v8__StackFrame__GetColumn(self) as usize } + } + + /// Returns the id of the script for the function for this StackFrame. + /// This method will return Message::kNoScriptIdInfo if it is unable to + /// retrieve the script id, or if kScriptId was not passed as an option when + /// capturing the StackTrace. + #[inline(always)] + pub fn get_script_id(&self) -> usize { + unsafe { v8__StackFrame__GetScriptId(self) as usize } + } + + /// Returns the name of the resource that contains the script for the + /// function for this StackFrame. + #[inline(always)] + pub fn get_script_name<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { scope.cast_local(|_| v8__StackFrame__GetScriptName(self)) } + } + + /// Returns the name of the resource that contains the script for the + /// function for this StackFrame or sourceURL value if the script name + /// is undefined and its source ends with //# sourceURL=... string or + /// deprecated //@ sourceURL=... string. + #[inline(always)] + pub fn get_script_name_or_source_url<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope.cast_local(|_| v8__StackFrame__GetScriptNameOrSourceURL(self)) + } + } + + /// Returns the source of the script for the function for this StackFrame. + #[inline(always)] + pub fn get_script_source<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { scope.cast_local(|_| v8__StackFrame__GetScriptSource(self)) } + } + + /// Returns the source mapping URL (if one is present) of the script for + /// the function for this StackFrame. + #[inline(always)] + pub fn get_script_source_mapping_url<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope.cast_local(|_| v8__StackFrame__GetScriptSourceMappingURL(self)) + } + } + + /// Returns the name of the function associated with this stack frame. + #[inline(always)] + pub fn get_function_name<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { scope.cast_local(|_| v8__StackFrame__GetFunctionName(self)) } + } + + /// Returns whether or not the associated function is compiled via a call to + /// eval(). + #[inline(always)] + pub fn is_eval(&self) -> bool { + unsafe { v8__StackFrame__IsEval(self) } + } + + /// Returns whether or not the associated function is called as a + /// constructor via "new". + #[inline(always)] + pub fn is_constructor(&self) -> bool { + unsafe { v8__StackFrame__IsConstructor(self) } + } + + /// Returns whether or not the associated functions is defined in wasm. + #[inline(always)] + pub fn is_wasm(&self) -> bool { + unsafe { v8__StackFrame__IsWasm(self) } + } + + /// Returns whether or not the associated function is defined by the user. + #[inline(always)] + pub fn is_user_javascript(&self) -> bool { + unsafe { v8__StackFrame__IsUserJavaScript(self) } + } +} + +impl Message { + #[inline(always)] + pub fn get<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, String> { + unsafe { scope.cast_local(|_| v8__Message__Get(self)) }.unwrap() + } + + /// Exception stack trace. By default stack traces are not captured for + /// uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows + /// to change this option. + #[inline(always)] + pub fn get_stack_trace<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { scope.cast_local(|_| v8__Message__GetStackTrace(self)) } + } + + #[inline(always)] + pub fn get_source_line<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Message__GetSourceLine(self, sd.get_current_context()) + }) + } + } + + /// Returns the resource name for the script from where the function causing + /// the error originates. + #[inline(always)] + pub fn get_script_resource_name<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { scope.cast_local(|_| v8__Message__GetScriptResourceName(self)) } + } + + /// Returns the number, 1-based, of the line where the error occurred. + #[inline(always)] + pub fn get_line_number(&self, scope: &PinScope<'_, '_>) -> Option { + let i = unsafe { + v8__Message__GetLineNumber(self, &*scope.get_current_context()) + }; + if i < 0 { None } else { Some(i as usize) } + } + + /// Returns the index within the script of the first character where + /// the error occurred. + #[inline(always)] + pub fn get_start_position(&self) -> int { + unsafe { v8__Message__GetStartPosition(self) } + } + + /// Returns the index within the script of the last character where + /// the error occurred. + #[inline(always)] + pub fn get_end_position(&self) -> int { + unsafe { v8__Message__GetEndPosition(self) } + } + + /// Returns the Wasm function index where the error occurred. Returns -1 if + /// message is not from a Wasm script. + #[inline(always)] + pub fn get_wasm_function_index(&self) -> int { + unsafe { v8__Message__GetWasmFunctionIndex(self) } + } + + /// Returns the error level of the message. + #[inline(always)] + pub fn error_level(&self) -> int { + unsafe { v8__Message__ErrorLevel(self) } + } + + /// Returns the index within the line of the first character where + /// the error occurred. + #[inline(always)] + pub fn get_start_column(&self) -> usize { + unsafe { v8__Message__GetStartColumn(self) as usize } + } + + /// Returns the index within the line of the last character where + /// the error occurred. + #[inline(always)] + pub fn get_end_column(&self) -> usize { + unsafe { v8__Message__GetEndColumn(self) as usize } + } + + /// Passes on the value set by the embedder when it fed the script from which + /// this Message was generated to V8. + #[inline(always)] + pub fn is_shared_cross_origin(&self) -> bool { + unsafe { v8__Message__IsSharedCrossOrigin(self) } + } + + #[inline(always)] + pub fn is_opaque(&self) -> bool { + unsafe { v8__Message__IsOpaque(self) } + } +} + +/// Create new error objects by calling the corresponding error object +/// constructor with the message. +#[derive(Debug)] +pub struct Exception; + +impl Exception { + #[inline(always)] + pub fn error<'s>( + scope: &PinScope<'s, '_>, + message: Local, + ) -> Local<'s, Value> { + Self::new_error_with(scope, message, v8__Exception__Error) + } + + #[inline(always)] + pub fn range_error<'s>( + scope: &PinScope<'s, '_>, + message: Local, + ) -> Local<'s, Value> { + Self::new_error_with(scope, message, v8__Exception__RangeError) + } + + #[inline(always)] + pub fn reference_error<'s>( + scope: &PinScope<'s, '_>, + message: Local, + ) -> Local<'s, Value> { + Self::new_error_with(scope, message, v8__Exception__ReferenceError) + } + + #[inline(always)] + pub fn syntax_error<'s>( + scope: &PinScope<'s, '_>, + message: Local, + ) -> Local<'s, Value> { + Self::new_error_with(scope, message, v8__Exception__SyntaxError) + } + + #[inline(always)] + pub fn type_error<'s>( + scope: &PinScope<'s, '_>, + message: Local, + ) -> Local<'s, Value> { + Self::new_error_with(scope, message, v8__Exception__TypeError) + } + + /// Internal helper to make the above error constructors less repetitive. + #[inline(always)] + fn new_error_with<'s>( + scope: &PinScope<'s, '_>, + message: Local, + contructor: unsafe extern "C" fn(*const String) -> *const Value, + ) -> Local<'s, Value> { + unsafe { + scope.enter(); + let error = scope.cast_local(|_| (contructor)(&*message)).unwrap(); + scope.exit(); + error + } + } + + /// Creates an error message for the given exception. + /// Will try to reconstruct the original stack trace from the exception value, + /// or capture the current stack trace if not available. + #[inline(always)] + pub fn create_message<'s>( + scope: &PinScope<'s, '_>, + exception: Local, + ) -> Local<'s, Message> { + unsafe { + scope.cast_local(|sd| { + v8__Exception__CreateMessage(sd.get_isolate_ptr(), &*exception) + }) + } + .unwrap() + } + + /// Returns the original stack trace that was captured at the creation time + /// of a given exception, or an empty handle if not available. + #[inline(always)] + pub fn get_stack_trace<'s>( + scope: &PinScope<'s, '_>, + exception: Local, + ) -> Option> { + unsafe { scope.cast_local(|_| v8__Exception__GetStackTrace(&*exception)) } + } + + /// Captures the current stack trace and attaches it to the given object in the + /// form of `stack` property. + #[inline(always)] + pub fn capture_stack_trace( + context: Local, + object: Local, + ) -> Option { + unsafe { v8__Exception__CaptureStackTrace(&*context, &*object).into() } + } +} diff --git a/crates/v8/src/external.rs b/crates/v8/src/external.rs new file mode 100644 index 0000000000..6bcee30a1a --- /dev/null +++ b/crates/v8/src/external.rs @@ -0,0 +1,35 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +use std::ffi::c_void; + +use crate::External; +use crate::Local; +use crate::isolate::RealIsolate; +use crate::scope::PinScope; + +unsafe extern "C" { + fn v8__External__New( + isolate: *mut RealIsolate, + value: *mut c_void, + ) -> *const External; + fn v8__External__Value(this: *const External) -> *mut c_void; +} + +impl External { + #[inline(always)] + #[allow(clippy::not_unsafe_ptr_arg_deref)] + pub fn new<'s>( + scope: &PinScope<'s, '_, ()>, + value: *mut c_void, + ) -> Local<'s, Self> { + unsafe { + scope.cast_local(|sd| v8__External__New(sd.get_isolate_ptr(), value)) + } + .unwrap() + } + + #[inline(always)] + pub fn value(&self) -> *mut c_void { + unsafe { v8__External__Value(self) } + } +} diff --git a/crates/v8/src/external_references.rs b/crates/v8/src/external_references.rs new file mode 100644 index 0000000000..9913fe46d5 --- /dev/null +++ b/crates/v8/src/external_references.rs @@ -0,0 +1,50 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +use crate::FunctionCallback; +use crate::IndexedDefinerCallback; +use crate::IndexedDeleterCallback; +use crate::IndexedGetterCallback; +use crate::IndexedQueryCallback; +use crate::IndexedSetterCallback; +use crate::MessageCallback; +use crate::NamedDefinerCallback; +use crate::NamedDeleterCallback; +use crate::NamedGetterCallback; +use crate::NamedQueryCallback; +use crate::NamedSetterCallback; +use crate::PropertyEnumeratorCallback; +use crate::fast_api::CFunctionInfo; +use std::ffi::c_void; +use std::fmt::Debug; + +#[derive(Clone, Copy)] +pub union ExternalReference { + pub function: FunctionCallback, + pub named_getter: NamedGetterCallback, + pub named_setter: NamedSetterCallback, + pub named_definer: NamedDefinerCallback, + pub named_deleter: NamedDeleterCallback, + pub named_query: NamedQueryCallback, + pub indexed_getter: IndexedGetterCallback, + pub indexed_setter: IndexedSetterCallback, + pub indexed_definer: IndexedDefinerCallback, + pub indexed_deleter: IndexedDeleterCallback, + pub indexed_query: IndexedQueryCallback, + pub enumerator: PropertyEnumeratorCallback, + pub message: MessageCallback, + pub pointer: *mut c_void, + pub type_info: *const CFunctionInfo, +} + +impl Debug for ExternalReference { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // SAFETY: All union fields are the same size + unsafe { (self.pointer).fmt(f) } + } +} + +impl PartialEq for ExternalReference { + fn eq(&self, other: &Self) -> bool { + unsafe { self.pointer.eq(&other.pointer) } + } +} diff --git a/crates/v8/src/fast_api.rs b/crates/v8/src/fast_api.rs new file mode 100644 index 0000000000..e4831b1a07 --- /dev/null +++ b/crates/v8/src/fast_api.rs @@ -0,0 +1,202 @@ +use crate::Isolate; +use crate::Local; +use crate::Value; +use crate::binding::*; +use crate::isolate::RealIsolate; +use std::ffi::c_void; +use std::ptr::NonNull; + +#[derive(Clone, Copy)] +#[repr(transparent)] +pub struct CFunction(v8__CFunction); + +impl CFunction { + /// Construct a `CFunction` from a function address and its type info. + /// + /// `type_info` is borrowed for `'static` because the resulting `CFunction` + /// stores the raw `v8::CFunctionInfo*` pointer and, once handed to V8 via + /// [`FunctionBuilder::build_fast`], that pointer is retained for the + /// lifetime of the `FunctionTemplate` (and copied into each fast-call + /// dispatch site). Anything other than `'static` storage for the type info + /// would let the pointer dangle. + /// + /// In practice this is achieved by constructing the `CFunctionInfo` inside + /// a `const` initializer (where reference-to-temporary rvalues are promoted + /// to `'static`), exactly as the existing test/bench patterns do: + /// + /// ```ignore + /// const FAST_CALL: v8::fast_api::CFunction = v8::fast_api::CFunction::new( + /// my_fast_fn as _, + /// &v8::fast_api::CFunctionInfo::new( + /// v8::fast_api::Type::Void.as_info(), + /// &[v8::fast_api::Type::V8Value.as_info()], + /// v8::fast_api::Int64Representation::Number, + /// ), + /// ); + /// ``` + /// + /// [`FunctionBuilder::build_fast`]: crate::FunctionBuilder::build_fast + pub const fn new( + address: *const c_void, + type_info: &'static CFunctionInfo, + ) -> Self { + Self(v8__CFunction { + address_: address, + type_info_: &type_info.0, + }) + } + + pub const fn address(&self) -> *const c_void { + self.0.address_ + } + + pub const fn type_info(&self) -> &CFunctionInfo { + // SAFETY: We initialize this field with a reference. and + // the layout of CFunctionInfo is identical to v8_CFunctionInfo. + unsafe { &*(self.0.type_info_ as *const CFunctionInfo) } + } +} + +#[repr(transparent)] +pub struct CFunctionInfo(v8__CFunctionInfo); + +impl CFunctionInfo { + /// Construct a struct to hold a CFunction's type information. + /// |return_info| describes the function's return type. + /// |arg_info| is an array of |arg_count| CTypeInfos describing the + /// arguments. Only the last argument may be of the special type + /// CTypeInfo::kCallbackOptionsType. + /// + /// `arg_info` is borrowed for `'static` because the resulting + /// `CFunctionInfo` stores the raw `arg_info.as_ptr()` pointer and V8 reads + /// it on every fast-call invocation through the `CFunction` chain. As with + /// [`CFunction::new`], `const` initializers make this trivial: rvalues + /// such as `&[Type::V8Value.as_info()]` are promoted to `'static` storage. + pub const fn new( + return_info: CTypeInfo, + arg_info: &'static [CTypeInfo], + repr: Int64Representation, + ) -> Self { + Self(v8__CFunctionInfo { + arg_count_: arg_info.len() as _, + arg_info_: arg_info.as_ptr() as _, + repr_: repr as _, + return_info_: return_info.0, + }) + } +} + +#[derive(Clone, Copy)] +#[repr(u8)] +pub enum Int64Representation { + /// Use numbers to represent 64 bit integers. + Number = v8_CFunctionInfo_Int64Representation_kNumber, + /// Use BigInts to represent 64 bit integers. + BigInt = v8_CFunctionInfo_Int64Representation_kBigInt, +} + +#[derive(Clone, Copy)] +#[repr(transparent)] +pub struct CTypeInfo(v8_CTypeInfo); + +impl CTypeInfo { + pub const fn new(r#type: Type, flags: Flags) -> Self { + Self(v8_CTypeInfo { + type_: r#type as _, + flags_: flags.bits(), + }) + } +} + +#[derive(Clone, Copy)] +#[repr(u8)] +pub enum Type { + Void = v8_CTypeInfo_Type_kVoid, + Bool = v8_CTypeInfo_Type_kBool, + Uint8 = v8_CTypeInfo_Type_kUint8, + Int32 = v8_CTypeInfo_Type_kInt32, + Uint32 = v8_CTypeInfo_Type_kUint32, + Int64 = v8_CTypeInfo_Type_kInt64, + Uint64 = v8_CTypeInfo_Type_kUint64, + Float32 = v8_CTypeInfo_Type_kFloat32, + Float64 = v8_CTypeInfo_Type_kFloat64, + Pointer = v8_CTypeInfo_Type_kPointer, + V8Value = v8_CTypeInfo_Type_kV8Value, + SeqOneByteString = v8_CTypeInfo_Type_kSeqOneByteString, + ApiObject = v8_CTypeInfo_Type_kApiObject, + Any = v8_CTypeInfo_Type_kAny, + CallbackOptions = 255, +} + +impl Type { + // const fn since From is not const + pub const fn as_info(self) -> CTypeInfo { + CTypeInfo::new(self, Flags::empty()) + } +} + +impl From for CTypeInfo { + fn from(t: Type) -> Self { + Self::new(t, Flags::empty()) + } +} + +bitflags::bitflags! { + pub struct Flags: u8 { + /// Must be an ArrayBuffer or TypedArray + const AllowShared = v8_CTypeInfo_Flags_kAllowSharedBit; + /// T must be integral + const EnforceRange = v8_CTypeInfo_Flags_kEnforceRangeBit; + /// T must be integral + const Clamp = v8_CTypeInfo_Flags_kClampBit; + /// T must be float or double + const IsRestricted = v8_CTypeInfo_Flags_kIsRestrictedBit; + } +} + +/// A struct which may be passed to a fast call callback, like so +/// ```c +/// void FastMethodWithOptions(int param, FastApiCallbackOptions& options); +/// ``` +#[repr(C)] +pub struct FastApiCallbackOptions<'a> { + pub(crate) isolate: *mut RealIsolate, + /// The `data` passed to the FunctionTemplate constructor, or `undefined`. + pub data: Local<'a, Value>, +} + +impl<'a> FastApiCallbackOptions<'a> { + pub unsafe fn isolate_unchecked(&self) -> &'a Isolate { + unsafe { + Isolate::from_raw_ref(std::mem::transmute::< + &*mut RealIsolate, + &NonNull, + >(&self.isolate)) + } + } + + pub unsafe fn isolate_unchecked_mut(&mut self) -> &mut Isolate { + unsafe { + Isolate::from_raw_ref_mut(std::mem::transmute::< + &mut *mut RealIsolate, + &mut NonNull, + >(&mut self.isolate)) + } + } +} + +pub type FastApiOneByteString = v8__FastOneByteString; + +impl FastApiOneByteString { + #[inline(always)] + pub fn as_bytes(&self) -> &[u8] { + // Ensure that we never create a null-ptr slice (even a zero-length null-ptr slice + // is invalid because of Rust's niche packing). + if self.data.is_null() { + return &mut []; + } + + // SAFETY: The data is guaranteed to be valid for the length of the string. + unsafe { std::slice::from_raw_parts(self.data as _, self.length as usize) } + } +} diff --git a/crates/v8/src/fixed_array.rs b/crates/v8/src/fixed_array.rs new file mode 100644 index 0000000000..d42e2931bf --- /dev/null +++ b/crates/v8/src/fixed_array.rs @@ -0,0 +1,33 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +use crate::Data; +use crate::FixedArray; +use crate::Local; +use crate::scope::PinScope; +use crate::support::int; + +unsafe extern "C" { + fn v8__FixedArray__Length(this: *const FixedArray) -> int; + + fn v8__FixedArray__Get(this: *const FixedArray, index: int) -> *const Data; +} + +impl FixedArray { + #[inline(always)] + pub fn length(&self) -> usize { + unsafe { v8__FixedArray__Length(self) as usize } + } + + #[inline(always)] + pub fn get<'s>( + &self, + scope: &PinScope<'s, '_>, + index: usize, + ) -> Option> { + if index >= self.length() { + return None; + } + + unsafe { scope.cast_local(|_| v8__FixedArray__Get(self, index as int)) } + } +} diff --git a/crates/v8/src/function.rs b/crates/v8/src/function.rs new file mode 100644 index 0000000000..e9143a893b --- /dev/null +++ b/crates/v8/src/function.rs @@ -0,0 +1,1160 @@ +use std::convert::TryFrom; +use std::marker::PhantomData; +use std::mem::MaybeUninit; +use std::ptr::NonNull; +use std::ptr::null; + +use crate::Array; +use crate::Boolean; +use crate::CallbackScope; +use crate::Context; +use crate::Function; +use crate::Integer; +use crate::Isolate; +use crate::Local; +use crate::Name; +use crate::Object; +use crate::PropertyDescriptor; +use crate::ScriptOrigin; +use crate::SealedLocal; +use crate::Signature; +use crate::String; +use crate::UniqueRef; +use crate::UnsafeRawIsolatePtr; +use crate::Value; +use crate::isolate::RealIsolate; +use crate::scope::PinScope; +use crate::scope::callback_scope; +use crate::script_compiler::CachedData; +use crate::support::MapFnFrom; +use crate::support::MapFnTo; +use crate::support::ToCFn; +use crate::support::UnitType; +use crate::support::{Opaque, int}; +use crate::template::Intercepted; + +unsafe extern "C" { + fn v8__Function__New( + context: *const Context, + callback: FunctionCallback, + data_or_null: *const Value, + length: i32, + constructor_behavior: ConstructorBehavior, + side_effect_type: SideEffectType, + ) -> *const Function; + fn v8__Function__Call( + this: *const Function, + context: *const Context, + recv: *const Value, + argc: int, + argv: *const *const Value, + ) -> *const Value; + fn v8__Function__NewInstance( + this: *const Function, + context: *const Context, + argc: int, + argv: *const *const Value, + ) -> *const Object; + fn v8__Function__GetName(this: *const Function) -> *const String; + fn v8__Function__SetName(this: *const Function, name: *const String); + fn v8__Function__GetScriptColumnNumber(this: *const Function) -> int; + fn v8__Function__GetScriptLineNumber(this: *const Function) -> int; + fn v8__Function__ScriptId(this: *const Function) -> int; + fn v8__Function__GetScriptOrigin<'a>( + this: *const Function, + out: *mut MaybeUninit>, + ); + + fn v8__Function__CreateCodeCache( + script: *const Function, + ) -> *mut CachedData<'static>; + + fn v8__FunctionCallbackInfo__GetIsolate( + this: *const FunctionCallbackInfo, + ) -> *mut RealIsolate; + fn v8__FunctionCallbackInfo__GetParts( + this: *const FunctionCallbackInfo, + ) -> RawFunctionCallbackInfoParts; + fn v8__FunctionCallbackInfo__Data( + this: *const FunctionCallbackInfo, + ) -> *const Value; + fn v8__FunctionCallbackInfo__This( + this: *const FunctionCallbackInfo, + ) -> *const Object; + fn v8__FunctionCallbackInfo__NewTarget( + this: *const FunctionCallbackInfo, + ) -> *const Value; + fn v8__FunctionCallbackInfo__IsConstructCall( + this: *const FunctionCallbackInfo, + ) -> bool; + fn v8__FunctionCallbackInfo__Get( + this: *const FunctionCallbackInfo, + index: int, + ) -> *const Value; + fn v8__FunctionCallbackInfo__Length(this: *const FunctionCallbackInfo) + -> int; + fn v8__FunctionCallbackInfo__GetReturnValue( + this: *const FunctionCallbackInfo, + ) -> usize; + + fn v8__PropertyCallbackInfo__GetIsolate( + this: *const RawPropertyCallbackInfo, + ) -> *mut RealIsolate; + fn v8__PropertyCallbackInfo__Data( + this: *const RawPropertyCallbackInfo, + ) -> *const Value; + fn v8__PropertyCallbackInfo__Holder( + this: *const RawPropertyCallbackInfo, + ) -> *const Object; + fn v8__PropertyCallbackInfo__GetReturnValue( + this: *const RawPropertyCallbackInfo, + ) -> usize; + fn v8__PropertyCallbackInfo__ShouldThrowOnError( + this: *const RawPropertyCallbackInfo, + ) -> bool; + + fn v8__ReturnValue__Value__Set( + this: *mut RawReturnValue, + value: *const Value, + ); + fn v8__ReturnValue__Value__Set__Bool(this: *mut RawReturnValue, value: bool); + fn v8__ReturnValue__Value__Set__Int32(this: *mut RawReturnValue, value: i32); + fn v8__ReturnValue__Value__Set__Uint32(this: *mut RawReturnValue, value: u32); + fn v8__ReturnValue__Value__Set__Double(this: *mut RawReturnValue, value: f64); + fn v8__ReturnValue__Value__SetNull(this: *mut RawReturnValue); + fn v8__ReturnValue__Value__SetUndefined(this: *mut RawReturnValue); + fn v8__ReturnValue__Value__SetEmptyString(this: *mut RawReturnValue); + fn v8__ReturnValue__Value__Get(this: *const RawReturnValue) -> *const Value; +} + +// Ad-libbed - V8 does not document ConstructorBehavior. +/// ConstructorBehavior::Allow creates a regular API function. +/// +/// ConstructorBehavior::Throw creates a "concise" API function, a function +/// without a ".prototype" property, that is somewhat faster to create and has +/// a smaller footprint. Functionally equivalent to ConstructorBehavior::Allow +/// followed by a call to FunctionTemplate::RemovePrototype(). +#[repr(C)] +pub enum ConstructorBehavior { + Throw, + Allow, +} + +/// Options for marking whether callbacks may trigger JS-observable side +/// effects. Side-effect-free callbacks are allowlisted during debug evaluation +/// with throwOnSideEffect. It applies when calling a Function, +/// FunctionTemplate, or an Accessor callback. For Interceptors, please see +/// PropertyHandlerFlags's kHasNoSideEffect. +/// Callbacks that only cause side effects to the receiver are allowlisted if +/// invoked on receiver objects that are created within the same debug-evaluate +/// call, as these objects are temporary and the side effect does not escape. +#[repr(C)] +pub enum SideEffectType { + HasSideEffect, + HasNoSideEffect, + HasSideEffectToReceiver, +} + +#[repr(C)] +#[derive(Clone, Copy, Debug)] +struct RawReturnValue(usize); + +#[repr(C)] +#[derive(Debug)] +struct RawFunctionCallbackInfoParts { + isolate: *mut RealIsolate, + return_value: usize, + data: *const Value, + length: int, +} + +// Note: the 'cb lifetime is required because the ReturnValue object must not +// outlive the FunctionCallbackInfo/PropertyCallbackInfo object from which it +// is derived. +#[derive(Debug)] +pub struct ReturnValue<'cb, T = Value>(RawReturnValue, PhantomData<&'cb T>); + +impl Clone for ReturnValue<'_, T> { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for ReturnValue<'_, T> {} + +impl<'cb, T> ReturnValue<'cb, T> { + #[inline(always)] + fn from_raw(raw: RawReturnValue) -> Self { + Self(raw, PhantomData) + } + + #[inline(always)] + pub fn from_property_callback_info( + info: &'cb PropertyCallbackInfo, + ) -> Self { + Self( + unsafe { + RawReturnValue(v8__PropertyCallbackInfo__GetReturnValue(&info.0)) + }, + PhantomData, + ) + } +} + +impl<'cb> ReturnValue<'cb, Value> { + #[inline(always)] + pub fn from_function_callback_info(info: &'cb FunctionCallbackInfo) -> Self { + Self( + unsafe { RawReturnValue(v8__FunctionCallbackInfo__GetReturnValue(info)) }, + PhantomData, + ) + } +} + +impl ReturnValue<'_, ()> { + #[inline(always)] + pub fn set_bool(&mut self, value: bool) { + unsafe { v8__ReturnValue__Value__Set__Bool(&mut self.0, value) } + } +} + +impl ReturnValue<'_, T> +where + for<'s> Local<'s, T>: Into>, +{ + #[inline(always)] + pub fn set(&mut self, value: Local) { + unsafe { v8__ReturnValue__Value__Set(&mut self.0, &*value.into()) } + } + + #[inline(always)] + pub fn set_bool(&mut self, value: bool) { + unsafe { v8__ReturnValue__Value__Set__Bool(&mut self.0, value) } + } + + #[inline(always)] + pub fn set_int32(&mut self, value: i32) { + unsafe { v8__ReturnValue__Value__Set__Int32(&mut self.0, value) } + } + + #[inline(always)] + pub fn set_uint32(&mut self, value: u32) { + unsafe { v8__ReturnValue__Value__Set__Uint32(&mut self.0, value) } + } + + #[inline(always)] + pub fn set_double(&mut self, value: f64) { + unsafe { v8__ReturnValue__Value__Set__Double(&mut self.0, value) } + } + + #[inline(always)] + pub fn set_null(&mut self) { + unsafe { v8__ReturnValue__Value__SetNull(&mut self.0) } + } + + #[inline(always)] + pub fn set_undefined(&mut self) { + unsafe { v8__ReturnValue__Value__SetUndefined(&mut self.0) } + } + + #[inline(always)] + pub fn set_empty_string(&mut self) { + unsafe { v8__ReturnValue__Value__SetEmptyString(&mut self.0) } + } + + /// Getter. Creates a new Local<> so it comes with a certain performance + /// hit. If the ReturnValue was not yet set, this will return the undefined + /// value. + #[inline(always)] + pub fn get<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, Value> { + unsafe { scope.cast_local(|_| v8__ReturnValue__Value__Get(&self.0)) } + .unwrap() + } +} + +/// The argument information given to function call callbacks. This +/// class provides access to information about the context of the call, +/// including the receiver, the number and values of arguments, and +/// the holder of the function. +#[repr(C)] +#[derive(Debug)] +pub struct FunctionCallbackInfo(*mut Opaque); + +/// Values commonly needed at the start of a function callback. +/// +/// This lets raw callbacks fetch the isolate, return value, callback data, and +/// argument length with one FFI call. The returned parts can also seed +/// [`FunctionCallbackArguments`] and [`CallbackScope`](crate::CallbackScope) +/// without re-reading those values from V8. +#[repr(C)] +#[derive(Debug)] +pub struct FunctionCallbackInfoParts<'cb> { + pub isolate: UnsafeRawIsolatePtr, + pub return_value: ReturnValue<'cb>, + pub data: Local<'cb, Value>, + pub length: int, +} + +impl FunctionCallbackInfo { + #[inline(always)] + pub(crate) fn get_isolate_ptr(&self) -> *mut RealIsolate { + unsafe { v8__FunctionCallbackInfo__GetIsolate(self) } + } + + /// Returns the common callback entry values with a single C++ shim call. + #[inline(always)] + pub fn get_parts(&self) -> FunctionCallbackInfoParts<'_> { + let raw = unsafe { v8__FunctionCallbackInfo__GetParts(self) }; + FunctionCallbackInfoParts { + isolate: UnsafeRawIsolatePtr::from_real_ptr(raw.isolate), + return_value: ReturnValue::from_raw(RawReturnValue(raw.return_value)), + data: unsafe { + Local::from_non_null(NonNull::new_unchecked(raw.data as *mut Value)) + }, + length: raw.length, + } + } + + #[inline(always)] + pub(crate) fn new_target(&self) -> Local<'_, Value> { + unsafe { + let ptr = v8__FunctionCallbackInfo__NewTarget(self); + let nn = NonNull::new_unchecked(ptr as *mut _); + Local::from_non_null(nn) + } + } + + #[inline(always)] + pub(crate) fn this(&self) -> Local<'_, Object> { + unsafe { + let ptr = v8__FunctionCallbackInfo__This(self); + let nn = NonNull::new_unchecked(ptr as *mut _); + Local::from_non_null(nn) + } + } + + #[inline] + pub fn is_construct_call(&self) -> bool { + unsafe { v8__FunctionCallbackInfo__IsConstructCall(self) } + } + + #[inline(always)] + pub(crate) fn data(&self) -> Local<'_, Value> { + unsafe { + let ptr = v8__FunctionCallbackInfo__Data(self); + let nn = NonNull::new_unchecked(ptr as *mut Value); + Local::from_non_null(nn) + } + } + + #[inline(always)] + pub(crate) fn length(&self) -> int { + unsafe { v8__FunctionCallbackInfo__Length(self) } + } + + #[inline(always)] + pub(crate) fn get(&self, index: int) -> Local<'_, Value> { + unsafe { + let ptr = v8__FunctionCallbackInfo__Get(self, index); + let nn = NonNull::new_unchecked(ptr as *mut Value); + Local::from_non_null(nn) + } + } +} + +#[repr(C)] +#[derive(Debug)] +struct RawPropertyCallbackInfo(*mut Opaque); + +/// The information passed to a property callback about the context +/// of the property access. +#[repr(C)] +#[derive(Debug)] +pub struct PropertyCallbackInfo(RawPropertyCallbackInfo, PhantomData); + +impl PropertyCallbackInfo { + #[inline(always)] + pub(crate) fn get_isolate_ptr(&self) -> *mut RealIsolate { + unsafe { v8__PropertyCallbackInfo__GetIsolate(&self.0) } + } +} + +#[derive(Debug)] +pub struct FunctionCallbackArguments<'s> { + info: &'s FunctionCallbackInfo, + data: Option>, + length: Option, +} + +impl<'s> FunctionCallbackArguments<'s> { + #[inline(always)] + pub fn from_function_callback_info(info: &'s FunctionCallbackInfo) -> Self { + Self { + info, + data: None, + length: None, + } + } + + /// Creates callback arguments from [`FunctionCallbackInfoParts`], reusing the + /// already-loaded callback data and argument length. + #[inline(always)] + pub fn from_function_callback_info_parts( + info: &'s FunctionCallbackInfo, + parts: &FunctionCallbackInfoParts<'s>, + ) -> Self { + Self { + info, + data: Some(parts.data), + length: Some(parts.length), + } + } + + /// SAFETY: caller must guarantee that no other references to the isolate are + /// accessible. Specifically, if an open CallbackScope or HandleScope exists + /// in the current function, `FunctionCallbackArguments::get_isolate()` should + /// not be called. + #[inline(always)] + pub unsafe fn get_isolate(&mut self) -> &mut Isolate { + unsafe { + &mut *(self.info.get_isolate_ptr() as *mut crate::isolate::Isolate) + } + } + + /// For construct calls, this returns the "new.target" value. + #[inline(always)] + pub fn new_target(&self) -> Local<'s, Value> { + self.info.new_target() + } + + /// Returns true if this is a construct call, i.e., if the function was + /// called with the `new` operator. + #[inline] + pub fn is_construct_call(&self) -> bool { + self.info.is_construct_call() + } + + /// Returns the receiver. This corresponds to the "this" value. + #[inline(always)] + pub fn this(&self) -> Local<'s, Object> { + self.info.this() + } + + /// Returns the data argument specified when creating the callback. + #[inline(always)] + pub fn data(&self) -> Local<'s, Value> { + self.data.unwrap_or_else(|| self.info.data()) + } + + /// The number of available arguments. + #[inline(always)] + pub fn length(&self) -> int { + self.length.unwrap_or_else(|| self.info.length()) + } + + /// Accessor for the available arguments. Returns `undefined` if the index is + /// out of bounds. + #[inline(always)] + pub fn get(&self, i: int) -> Local<'s, Value> { + self.info.get(i) + } +} + +#[derive(Debug)] +pub struct PropertyCallbackArguments<'s>(&'s RawPropertyCallbackInfo); + +impl<'s> PropertyCallbackArguments<'s> { + #[inline(always)] + pub(crate) fn from_property_callback_info( + info: &'s PropertyCallbackInfo, + ) -> Self { + Self(&info.0) + } + + /// Returns the object in the prototype chain of the receiver that has the + /// interceptor. Suppose you have `x` and its prototype is `y`, and `y` + /// has an interceptor. Then `info.This()` is `x` and `info.Holder()` is `y`. + /// In case the property is installed on the global object the Holder() + /// would return the global proxy. + #[inline(always)] + pub fn holder(&self) -> Local<'s, Object> { + unsafe { + Local::from_raw(v8__PropertyCallbackInfo__Holder(self.0)) + .unwrap_unchecked() + } + } + + /// Returns the data set in the configuration, i.e., in + /// `NamedPropertyHandlerConfiguration` or + /// `IndexedPropertyHandlerConfiguration.` + #[inline(always)] + pub fn data(&self) -> Local<'s, Value> { + unsafe { + Local::from_raw(v8__PropertyCallbackInfo__Data(self.0)).unwrap_unchecked() + } + } + + /// Returns `true` if the intercepted function should throw if an error + /// occurs. Usually, `true` corresponds to `'use strict'`. + /// + /// Always `false` when intercepting `Reflect.set()` independent of the + /// language mode. + #[inline(always)] + pub fn should_throw_on_error(&self) -> bool { + unsafe { v8__PropertyCallbackInfo__ShouldThrowOnError(self.0) } + } +} + +pub type FunctionCallback = unsafe extern "C" fn(*const FunctionCallbackInfo); + +impl MapFnFrom for FunctionCallback +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + FunctionCallbackArguments<'s>, + ReturnValue<'s, Value>, + ), +{ + fn mapping() -> Self { + let f = |info: *const FunctionCallbackInfo| { + let info = unsafe { &*info }; + let scope = std::pin::pin!(unsafe { CallbackScope::new(info) }); + let mut scope = scope.init(); + let args = FunctionCallbackArguments::from_function_callback_info(info); + let rv = ReturnValue::from_function_callback_info(info); + (F::get())(&mut scope, args, rv); + }; + f.to_c_fn() + } +} + +pub(crate) type NamedGetterCallbackForAccessor = + unsafe extern "C" fn(SealedLocal, *const PropertyCallbackInfo); + +impl MapFnFrom for NamedGetterCallbackForAccessor +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + Local<'s, Name>, + PropertyCallbackArguments<'s>, + ReturnValue, + ), +{ + fn mapping() -> Self { + let f = |key: SealedLocal, + info: *const PropertyCallbackInfo| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let key = unsafe { scope.unseal(key) }; + let args = PropertyCallbackArguments::from_property_callback_info(info); + let rv = ReturnValue::from_property_callback_info(info); + (F::get())(scope, key, args, rv); + }; + f.to_c_fn() + } +} + +pub(crate) type NamedGetterCallback = unsafe extern "C" fn( + SealedLocal, + *const PropertyCallbackInfo, +) -> Intercepted; + +impl MapFnFrom for NamedGetterCallback +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + Local<'s, Name>, + PropertyCallbackArguments<'s>, + ReturnValue, + ) -> Intercepted, +{ + fn mapping() -> Self { + let f = |key: SealedLocal, + info: *const PropertyCallbackInfo| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let key = unsafe { scope.unseal(key) }; + let args = PropertyCallbackArguments::from_property_callback_info(info); + let rv = ReturnValue::from_property_callback_info(info); + (F::get())(scope, key, args, rv) + }; + f.to_c_fn() + } +} + +pub(crate) type NamedQueryCallback = unsafe extern "C" fn( + SealedLocal, + *const PropertyCallbackInfo, +) -> Intercepted; + +impl MapFnFrom for NamedQueryCallback +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + Local<'s, Name>, + PropertyCallbackArguments<'s>, + ReturnValue, + ) -> Intercepted, +{ + fn mapping() -> Self { + let f = |key: SealedLocal, + info: *const PropertyCallbackInfo| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let key = unsafe { scope.unseal(key) }; + let args = PropertyCallbackArguments::from_property_callback_info(info); + let rv = ReturnValue::from_property_callback_info(info); + (F::get())(scope, key, args, rv) + }; + f.to_c_fn() + } +} + +pub(crate) type NamedSetterCallbackForAccessor = unsafe extern "C" fn( + SealedLocal, + SealedLocal, + *const PropertyCallbackInfo<()>, +); + +impl MapFnFrom for NamedSetterCallbackForAccessor +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + Local<'s, Name>, + Local<'s, Value>, + PropertyCallbackArguments<'s>, + ReturnValue<()>, + ), +{ + fn mapping() -> Self { + let f = |key: SealedLocal, + value: SealedLocal, + info: *const PropertyCallbackInfo<()>| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let key = unsafe { scope.unseal(key) }; + let value = unsafe { scope.unseal(value) }; + let args = PropertyCallbackArguments::from_property_callback_info(info); + let rv = ReturnValue::from_property_callback_info(info); + (F::get())(scope, key, value, args, rv); + }; + f.to_c_fn() + } +} + +pub(crate) type NamedSetterCallback = unsafe extern "C" fn( + SealedLocal, + SealedLocal, + *const PropertyCallbackInfo, +) -> Intercepted; + +impl MapFnFrom for NamedSetterCallback +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + Local<'s, Name>, + Local<'s, Value>, + PropertyCallbackArguments<'s>, + ReturnValue, + ) -> Intercepted, +{ + fn mapping() -> Self { + let f = |key: SealedLocal, + value: SealedLocal, + info: *const PropertyCallbackInfo| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let key = unsafe { scope.unseal(key) }; + let value = unsafe { scope.unseal(value) }; + let args = PropertyCallbackArguments::from_property_callback_info(info); + let rv = ReturnValue::from_property_callback_info(info); + (F::get())(scope, key, value, args, rv) + }; + f.to_c_fn() + } +} + +// Should return an Array in Return Value +pub(crate) type PropertyEnumeratorCallback = + unsafe extern "C" fn(*const PropertyCallbackInfo); + +impl MapFnFrom for PropertyEnumeratorCallback +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + PropertyCallbackArguments<'s>, + ReturnValue, + ), +{ + fn mapping() -> Self { + let f = |info: *const PropertyCallbackInfo| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let args = PropertyCallbackArguments::from_property_callback_info(info); + let rv = ReturnValue::from_property_callback_info(info); + (F::get())(scope, args, rv); + }; + f.to_c_fn() + } +} + +pub(crate) type NamedDefinerCallback = unsafe extern "C" fn( + SealedLocal, + *const PropertyDescriptor, + *const PropertyCallbackInfo, +) -> Intercepted; + +impl MapFnFrom for NamedDefinerCallback +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + Local<'s, Name>, + &PropertyDescriptor, + PropertyCallbackArguments<'s>, + ReturnValue, + ) -> Intercepted, +{ + fn mapping() -> Self { + let f = |key: SealedLocal, + desc: *const PropertyDescriptor, + info: *const PropertyCallbackInfo| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let key = unsafe { scope.unseal(key) }; + let args = PropertyCallbackArguments::from_property_callback_info(info); + let desc = unsafe { &*desc }; + let rv = ReturnValue::from_property_callback_info(info); + (F::get())(scope, key, desc, args, rv) + }; + f.to_c_fn() + } +} + +pub(crate) type NamedDeleterCallback = unsafe extern "C" fn( + SealedLocal, + *const PropertyCallbackInfo, +) -> Intercepted; + +impl MapFnFrom for NamedDeleterCallback +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + Local<'s, Name>, + PropertyCallbackArguments<'s>, + ReturnValue, + ) -> Intercepted, +{ + fn mapping() -> Self { + let f = |key: SealedLocal, + info: *const PropertyCallbackInfo| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let key = unsafe { scope.unseal(key) }; + let args = PropertyCallbackArguments::from_property_callback_info(info); + let rv = ReturnValue::from_property_callback_info(info); + (F::get())(scope, key, args, rv) + }; + f.to_c_fn() + } +} + +pub(crate) type IndexedGetterCallback = + unsafe extern "C" fn(u32, *const PropertyCallbackInfo) -> Intercepted; + +impl MapFnFrom for IndexedGetterCallback +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + u32, + PropertyCallbackArguments<'s>, + ReturnValue, + ) -> Intercepted, +{ + fn mapping() -> Self { + let f = |index: u32, info: *const PropertyCallbackInfo| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let args = PropertyCallbackArguments::from_property_callback_info(info); + let rv = ReturnValue::from_property_callback_info(info); + (F::get())(scope, index, args, rv) + }; + f.to_c_fn() + } +} + +pub(crate) type IndexedQueryCallback = unsafe extern "C" fn( + u32, + *const PropertyCallbackInfo, +) -> Intercepted; + +impl MapFnFrom for IndexedQueryCallback +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + u32, + PropertyCallbackArguments<'s>, + ReturnValue, + ) -> Intercepted, +{ + fn mapping() -> Self { + let f = |key: u32, info: *const PropertyCallbackInfo| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let args = PropertyCallbackArguments::from_property_callback_info(info); + let rv = ReturnValue::from_property_callback_info(info); + (F::get())(scope, key, args, rv) + }; + f.to_c_fn() + } +} + +pub(crate) type IndexedSetterCallback = unsafe extern "C" fn( + u32, + SealedLocal, + *const PropertyCallbackInfo, +) -> Intercepted; + +impl MapFnFrom for IndexedSetterCallback +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + u32, + Local<'s, Value>, + PropertyCallbackArguments<'s>, + ReturnValue, + ) -> Intercepted, +{ + fn mapping() -> Self { + let f = |index: u32, + value: SealedLocal, + info: *const PropertyCallbackInfo| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let value = unsafe { scope.unseal(value) }; + let args = PropertyCallbackArguments::from_property_callback_info(info); + let rv = ReturnValue::from_property_callback_info(info); + (F::get())(scope, index, value, args, rv) + }; + f.to_c_fn() + } +} + +pub(crate) type IndexedDefinerCallback = unsafe extern "C" fn( + u32, + *const PropertyDescriptor, + *const PropertyCallbackInfo, +) -> Intercepted; + +impl MapFnFrom for IndexedDefinerCallback +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + u32, + &PropertyDescriptor, + PropertyCallbackArguments<'s>, + ReturnValue, + ) -> Intercepted, +{ + fn mapping() -> Self { + let f = |index: u32, + desc: *const PropertyDescriptor, + info: *const PropertyCallbackInfo| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let args = PropertyCallbackArguments::from_property_callback_info(info); + let rv = ReturnValue::from_property_callback_info(info); + let desc = unsafe { &*desc }; + (F::get())(scope, index, desc, args, rv) + }; + f.to_c_fn() + } +} + +pub(crate) type IndexedDeleterCallback = unsafe extern "C" fn( + u32, + *const PropertyCallbackInfo, +) -> Intercepted; + +impl MapFnFrom for IndexedDeleterCallback +where + F: UnitType + + for<'s, 'i> Fn( + &mut PinScope<'s, 'i>, + u32, + PropertyCallbackArguments<'s>, + ReturnValue, + ) -> Intercepted, +{ + fn mapping() -> Self { + let f = |index: u32, info: *const PropertyCallbackInfo| { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let args = PropertyCallbackArguments::from_property_callback_info(info); + let rv = ReturnValue::from_property_callback_info(info); + (F::get())(scope, index, args, rv) + }; + f.to_c_fn() + } +} + +/// A builder to construct the properties of a Function or FunctionTemplate. +pub struct FunctionBuilder<'s, T> { + pub(crate) callback: Option, + pub(crate) data: Option>, + pub(crate) signature: Option>, + pub(crate) length: i32, + pub(crate) constructor_behavior: ConstructorBehavior, + pub(crate) side_effect_type: SideEffectType, + phantom: PhantomData, +} + +impl<'s, T> FunctionBuilder<'s, T> { + /// Create a new FunctionBuilder. + #[inline(always)] + pub fn new(callback: impl MapFnTo) -> Self { + Self::new_raw(callback.map_fn_to()) + } + + #[inline(always)] + pub fn new_raw(callback: FunctionCallback) -> Self { + Self::new_raw_optional(Some(callback)) + } + + /// Create a new FunctionBuilder, optionally without a callback. V8 only + /// accepts a missing callback when building a `FunctionTemplate`. + #[inline(always)] + pub(crate) fn new_raw_optional(callback: Option) -> Self { + Self { + callback, + data: None, + signature: None, + length: 0, + constructor_behavior: ConstructorBehavior::Allow, + side_effect_type: SideEffectType::HasSideEffect, + phantom: PhantomData, + } + } + + /// Set the associated data. The default is no associated data. + #[inline(always)] + pub fn data(mut self, data: Local<'s, Value>) -> Self { + self.data = Some(data); + self + } + + /// Set the function length. The default is 0. + #[inline(always)] + pub fn length(mut self, length: i32) -> Self { + self.length = length; + self + } + + /// Set the constructor behavior. The default is ConstructorBehavior::Allow. + #[inline(always)] + pub fn constructor_behavior( + mut self, + constructor_behavior: ConstructorBehavior, + ) -> Self { + self.constructor_behavior = constructor_behavior; + self + } + + /// Set the side effect type. The default is SideEffectType::HasSideEffect. + #[inline(always)] + pub fn side_effect_type(mut self, side_effect_type: SideEffectType) -> Self { + self.side_effect_type = side_effect_type; + self + } +} + +impl<'s> FunctionBuilder<'s, Function> { + /// Create the function in the current execution context. + #[inline(always)] + pub fn build<'i>( + self, + scope: &PinScope<'s, 'i>, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Function__New( + sd.get_current_context(), + self.callback.unwrap(), + self.data.map_or_else(null, |p| &*p), + self.length, + self.constructor_behavior, + self.side_effect_type, + ) + }) + } + } +} + +impl Function { + /// Create a FunctionBuilder to configure a Function. + /// This is the same as FunctionBuilder::::new(). + #[inline(always)] + pub fn builder<'s>( + callback: impl MapFnTo, + ) -> FunctionBuilder<'s, Self> { + FunctionBuilder::new(callback) + } + + #[inline(always)] + pub fn builder_raw<'s>( + callback: FunctionCallback, + ) -> FunctionBuilder<'s, Self> { + FunctionBuilder::new_raw(callback) + } + + /// Create a function in the current execution context + /// for a given FunctionCallback. + #[inline(always)] + pub fn new<'s>( + scope: &mut PinScope<'s, '_>, + callback: impl MapFnTo, + ) -> Option> { + Self::builder(callback).build(scope) + } + + #[inline(always)] + pub fn new_raw<'s>( + scope: &mut PinScope<'s, '_>, + callback: FunctionCallback, + ) -> Option> { + Self::builder_raw(callback).build(scope) + } + + /// Call a function in a context scope. + #[inline] + pub fn call<'s>( + &self, + scope: &PinScope<'s, '_>, + recv: Local, + args: &[Local], + ) -> Option> { + let args = Local::slice_into_raw(args); + let argc = int::try_from(args.len()).unwrap(); + let argv = args.as_ptr(); + unsafe { + scope.cast_local(|sd| { + v8__Function__Call(self, sd.get_current_context(), &*recv, argc, argv) + }) + } + } + + /// Call a function in a given context. + #[inline] + pub fn call_with_context<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + context: Local, + recv: Local, + args: &[Local], + ) -> Option> { + let args = Local::slice_into_raw(args); + let argc = int::try_from(args.len()).unwrap(); + let argv = args.as_ptr(); + unsafe { + let ret = v8__Function__Call( + self, + context.as_non_null().as_ptr(), + &*recv, + argc, + argv, + ); + if ret.is_null() { + None + } else { + scope.cast_local(|_| ret) + } + } + } + + #[inline(always)] + pub fn new_instance<'s>( + &self, + scope: &PinScope<'s, '_>, + args: &[Local], + ) -> Option> { + let args = Local::slice_into_raw(args); + let argc = int::try_from(args.len()).unwrap(); + let argv = args.as_ptr(); + unsafe { + scope.cast_local(|sd| { + v8__Function__NewInstance(self, sd.get_current_context(), argc, argv) + }) + } + } + + #[inline(always)] + pub fn get_name<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, String> { + unsafe { scope.cast_local(|_| v8__Function__GetName(self)).unwrap() } + } + + #[inline(always)] + pub fn set_name(&self, name: Local) { + unsafe { v8__Function__SetName(self, &*name) } + } + + /// Get the (zero-indexed) column number of the function's definition, if available. + #[inline(always)] + pub fn get_script_column_number(&self) -> Option { + let ret = unsafe { v8__Function__GetScriptColumnNumber(self) }; + (ret >= 0).then_some(ret as u32) + } + + /// Get the (zero-indexed) line number of the function's definition, if available. + #[inline(always)] + pub fn get_script_line_number(&self) -> Option { + let ret = unsafe { v8__Function__GetScriptLineNumber(self) }; + (ret >= 0).then_some(ret as u32) + } + + #[inline(always)] + pub fn get_script_origin<'s>( + &self, + _scope: &PinScope<'s, '_>, + ) -> ScriptOrigin<'s> { + unsafe { + let mut script_origin: MaybeUninit> = + MaybeUninit::uninit(); + v8__Function__GetScriptOrigin(self, &mut script_origin); + script_origin.assume_init() + } + } + + /// Returns scriptId. + #[inline(always)] + pub fn script_id(&self) -> i32 { + unsafe { v8__Function__ScriptId(self) } + } + + /// Creates and returns code cache for the specified unbound_script. + /// This will return nullptr if the script cannot be serialized. The + /// CachedData returned by this function should be owned by the caller. + #[inline(always)] + pub fn create_code_cache(&self) -> Option>> { + let code_cache = + unsafe { UniqueRef::try_from_raw(v8__Function__CreateCodeCache(self)) }; + if let Some(code_cache) = &code_cache { + debug_assert_eq!( + code_cache.buffer_policy(), + crate::script_compiler::BufferPolicy::BufferOwned + ); + } + code_cache + } +} diff --git a/crates/v8/src/gc.rs b/crates/v8/src/gc.rs new file mode 100644 index 0000000000..62824de8b0 --- /dev/null +++ b/crates/v8/src/gc.rs @@ -0,0 +1,2 @@ +pub use crate::binding::v8__GCCallbackFlags as GCCallbackFlags; +pub use crate::binding::v8__GCType as GCType; diff --git a/crates/v8/src/get_property_names_args_builder.rs b/crates/v8/src/get_property_names_args_builder.rs new file mode 100644 index 0000000000..dcd26d3237 --- /dev/null +++ b/crates/v8/src/get_property_names_args_builder.rs @@ -0,0 +1,126 @@ +use crate::PropertyFilter; + +#[derive(Debug, Clone, Copy)] +#[repr(C)] +pub enum KeyConversionMode { + /// kConvertToString will convert integer indices to strings. + ConvertToString, + /// kKeepNumbers will return numbers for integer indices. + KeepNumbers, + NoNumbers, +} + +/// Keys/Properties filter enums: +/// +/// KeyCollectionMode limits the range of collected properties. kOwnOnly limits +/// the collected properties to the given Object only. kIncludesPrototypes will +/// include all keys of the objects's prototype chain as well. +#[derive(Debug, Clone, Copy)] +#[repr(C)] +pub enum KeyCollectionMode { + /// OwnOnly limits the collected properties to the given Object only. + OwnOnly, + /// kIncludesPrototypes will include all keys of the objects's prototype chain + /// as well. + IncludePrototypes, +} + +#[derive(Debug, Clone, Copy)] +#[repr(C)] +pub enum IndexFilter { + /// kIncludesIndices allows for integer indices to be collected. + IncludeIndices, + /// kSkipIndices will exclude integer indices from being collected. + SkipIndices, +} + +pub struct GetPropertyNamesArgs { + pub mode: KeyCollectionMode, + pub property_filter: PropertyFilter, + pub index_filter: IndexFilter, + pub key_conversion: KeyConversionMode, +} + +impl Default for GetPropertyNamesArgs { + fn default() -> Self { + GetPropertyNamesArgs { + mode: KeyCollectionMode::IncludePrototypes, + property_filter: PropertyFilter::ONLY_ENUMERABLE + | PropertyFilter::SKIP_SYMBOLS, + index_filter: IndexFilter::IncludeIndices, + key_conversion: KeyConversionMode::KeepNumbers, + } + } +} + +pub struct GetPropertyNamesArgsBuilder { + mode: KeyCollectionMode, + property_filter: PropertyFilter, + index_filter: IndexFilter, + key_conversion: KeyConversionMode, +} + +impl Default for GetPropertyNamesArgsBuilder { + fn default() -> Self { + Self::new() + } +} + +impl GetPropertyNamesArgsBuilder { + #[inline(always)] + pub fn new() -> Self { + Self { + mode: KeyCollectionMode::IncludePrototypes, + property_filter: PropertyFilter::ONLY_ENUMERABLE + | PropertyFilter::SKIP_SYMBOLS, + index_filter: IndexFilter::IncludeIndices, + key_conversion: KeyConversionMode::KeepNumbers, + } + } + + #[inline(always)] + pub fn build(&self) -> GetPropertyNamesArgs { + GetPropertyNamesArgs { + mode: self.mode, + property_filter: self.property_filter, + index_filter: self.index_filter, + key_conversion: self.key_conversion, + } + } + + #[inline(always)] + pub fn mode( + &mut self, + mode: KeyCollectionMode, + ) -> &mut GetPropertyNamesArgsBuilder { + self.mode = mode; + self + } + + #[inline(always)] + pub fn property_filter( + &mut self, + property_filter: PropertyFilter, + ) -> &mut GetPropertyNamesArgsBuilder { + self.property_filter = property_filter; + self + } + + #[inline(always)] + pub fn index_filter( + &mut self, + index_filter: IndexFilter, + ) -> &mut GetPropertyNamesArgsBuilder { + self.index_filter = index_filter; + self + } + + #[inline(always)] + pub fn key_conversion( + &mut self, + key_conversion: KeyConversionMode, + ) -> &mut Self { + self.key_conversion = key_conversion; + self + } +} diff --git a/crates/v8/src/handle.rs b/crates/v8/src/handle.rs new file mode 100644 index 0000000000..5eacbedb15 --- /dev/null +++ b/crates/v8/src/handle.rs @@ -0,0 +1,1478 @@ +use std::borrow::Borrow; +use std::cell::Cell; +use std::ffi::c_void; +use std::hash::Hash; +use std::hash::Hasher; +use std::marker::PhantomData; +use std::mem::forget; +use std::mem::transmute; +use std::ops::Deref; +use std::ptr::NonNull; +use std::sync::atomic::AtomicUsize; +use std::sync::atomic::Ordering; + +use crate::Data; +use crate::Isolate; +use crate::IsolateHandle; +use crate::isolate::IsolateLiveness; +use crate::isolate::RealIsolate; +use crate::scope::GetIsolate; +use crate::scope::PinScope; +use crate::support::Opaque; + +unsafe extern "C" { + fn v8__Local__New( + isolate: *mut RealIsolate, + other: *const Data, + ) -> *const Data; + fn v8__Global__New( + isolate: *mut RealIsolate, + data: *const Data, + ) -> *const Data; + fn v8__Global__NewWeak( + isolate: *mut RealIsolate, + data: *const Data, + parameter: *const c_void, + callback: unsafe extern "C" fn(*const WeakCallbackInfo), + ) -> *const Data; + fn v8__Global__Reset(data: *const Data); + fn v8__WeakCallbackInfo__GetIsolate( + this: *const WeakCallbackInfo, + ) -> *mut RealIsolate; + fn v8__WeakCallbackInfo__GetParameter( + this: *const WeakCallbackInfo, + ) -> *mut c_void; + fn v8__WeakCallbackInfo__SetSecondPassCallback( + this: *const WeakCallbackInfo, + callback: unsafe extern "C" fn(*const WeakCallbackInfo), + ); + + fn v8__TracedReference__CONSTRUCT(this: *mut TracedReference); + fn v8__TracedReference__DESTRUCT(this: *mut TracedReference); + fn v8__TracedReference__Reset( + this: *mut TracedReference, + isolate: *mut RealIsolate, + data: *mut Data, + ); + fn v8__TracedReference__Get( + this: *const TracedReference, + isolate: *mut RealIsolate, + ) -> *const Data; + + fn v8__Eternal__CONSTRUCT(this: *mut Eternal); + fn v8__Eternal__DESTRUCT(this: *mut Eternal); + fn v8__Eternal__Clear(this: *mut Eternal); + fn v8__Eternal__Get( + this: *const Eternal, + isolate: *mut RealIsolate, + ) -> *const Data; + fn v8__Eternal__Set( + this: *mut Eternal, + isolate: *mut RealIsolate, + data: *mut Data, + ); + fn v8__Eternal__IsEmpty(this: *const Eternal) -> bool; +} + +/// An object reference managed by the v8 garbage collector. +/// +/// All objects returned from v8 have to be tracked by the garbage +/// collector so that it knows that the objects are still alive. Also, +/// because the garbage collector may move objects, it is unsafe to +/// point directly to an object. Instead, all objects are stored in +/// handles which are known by the garbage collector and updated +/// whenever an object moves. Handles should always be passed by value +/// (except in cases like out-parameters) and they should never be +/// allocated on the heap. +/// +/// There are two types of handles: local and persistent handles. +/// +/// Local handles are light-weight and transient and typically used in +/// local operations. They are managed by HandleScopes. That means that a +/// HandleScope must exist on the stack when they are created and that they are +/// only valid inside of the `HandleScope` active during their creation. +/// For passing a local handle to an outer `HandleScope`, an +/// `EscapableHandleScope` and its `Escape()` method must be used. +/// +/// Persistent handles can be used when storing objects across several +/// independent operations and have to be explicitly deallocated when they're no +/// longer used. +/// +/// It is safe to extract the object stored in the handle by +/// dereferencing the handle (for instance, to extract the `*Object` from +/// a `Local`); the value will still be governed by a handle +/// behind the scenes and the same rules apply to these values as to +/// their handles. +/// +/// Note: Local handles in Rusty V8 differ from the V8 C++ API in that they are +/// never empty. In situations where empty handles are needed, use +/// `Option`. +#[repr(C)] +#[derive(Debug)] +pub struct Local<'s, T>(NonNull, PhantomData<&'s ()>); + +mod sealed { + pub trait Sealed {} +} + +// this trait exists to allow you to specify the output lifetime for `Local::extend_lifetime_unchecked`. +// so you can do something like `unsafe { Local::extend_lifetime_unchecked::>(local) }`. +// if it were just a lifetime parameter, it would be "late bound" and you could not explicitly specify the output lifetime. +pub trait ExtendLifetime<'s, T>: sealed::Sealed { + type Input; + unsafe fn extend_lifetime_unchecked_from(value: Self::Input) -> Self; +} + +impl sealed::Sealed for Local<'_, T> {} + +impl<'s, T> ExtendLifetime<'s, T> for Local<'_, T> { + type Input = Local<'s, T>; + unsafe fn extend_lifetime_unchecked_from(value: Self::Input) -> Self { + unsafe { Local::from_non_null(value.as_non_null()) } + } +} + +impl<'s, T> Local<'s, T> { + /// Construct a new Local from an existing Handle. + #[inline(always)] + pub fn new<'i>( + scope: &PinScope<'s, 'i, ()>, + handle: impl Handle, + ) -> Local<'s, T> { + let HandleInfo { data, host } = handle.get_handle_info(); + host.assert_match_isolate(scope); + unsafe { + scope.cast_local(|sd| { + v8__Local__New(sd.get_isolate_ptr(), data.cast().as_ptr()) as *const T + }) + } + .unwrap() + } + + /// Create a local handle by downcasting from one of its super types. + /// This function is unsafe because the cast is unchecked. + #[inline(always)] + pub unsafe fn cast_unchecked(other: Local<'s, A>) -> Self + where + Local<'s, A>: TryFrom, + { + unsafe { transmute(other) } + } + /// Extend the lifetime of a `Local` handle to a longer lifetime. + /// + /// # Safety + /// + /// The caller is responsible for ensuring that the `Local` handle is valid + /// for the longer lifetime. Incorrect usage can lead to the usage of invalid + /// handles + /// + /// # Example + /// + /// ```ignore + /// let isolate = unsafe { Isolate::from_raw_isolate_ptr(isolate_ptr) }; + /// callback_scope!(unsafe scope, &mut isolate); + /// // the lifetime of the local handle will be tied to the lifetime of `&mut isolate`, + /// // which, because we've created it from a raw pointer, is only as long as the current function. + /// // the real lifetime at runtime is + /// // actually the lifetime of the parent scope. if we can guarantee that the parent scope lives at least as long as + /// // `'o`, it is valid to extend the lifetime of the local handle to `'o` by using `extend_lifetime_unchecked`. + /// let context = Local::new(scope, context_global_handle); + /// + /// let local_longer_lifetime = unsafe { local.extend_lifetime_unchecked::>() }; + /// ``` + #[inline(always)] + pub unsafe fn extend_lifetime_unchecked<'o, O>(self) -> O + where + O: ExtendLifetime<'s, T, Input = Self>, + { + unsafe { O::extend_lifetime_unchecked_from(self) } + } + + #[inline(always)] + pub(crate) unsafe fn from_raw(ptr: *const T) -> Option { + NonNull::new(ptr as *mut _).map(|nn| unsafe { Self::from_non_null(nn) }) + } + + #[inline(always)] + pub(crate) unsafe fn from_raw_unchecked(ptr: *const T) -> Self { + Self( + unsafe { NonNull::new_unchecked(ptr as *mut _) }, + PhantomData, + ) + } + + #[inline(always)] + pub(crate) unsafe fn from_non_null(nn: NonNull) -> Self { + Self(nn, PhantomData) + } + + #[inline(always)] + pub(crate) fn as_non_null(self) -> NonNull { + #[cfg(debug_assertions)] + crate::scope::scope_owner::check_handle(self.0.cast()); + self.0 + } + + #[inline(always)] + pub(crate) fn slice_into_raw(slice: &[Self]) -> &[*const T] { + #[cfg(debug_assertions)] + for local in slice { + crate::scope::scope_owner::check_handle(local.0.cast()); + } + unsafe { &*(slice as *const [Self] as *const [*const T]) } + } +} + +impl Copy for Local<'_, T> {} + +impl Clone for Local<'_, T> { + fn clone(&self) -> Self { + *self + } +} + +impl Deref for Local<'_, T> { + type Target = T; + fn deref(&self) -> &T { + #[cfg(debug_assertions)] + crate::scope::scope_owner::check_handle(self.0.cast()); + unsafe { self.0.as_ref() } + } +} + +impl<'s, T> Local<'s, T> { + /// Attempts to cast the contained type to another, + /// returning an error if the conversion fails. + /// + /// # Examples + /// + /// ```ignore + /// let value: Local<'_, Value> = get_v8_value(); + /// + /// if let Ok(func) = value.try_cast:: { + /// // + /// } + /// ``` + #[inline(always)] + pub fn try_cast( + self, + ) -> Result, >>::Error> + where + Self: TryInto>, + { + self.try_into() + } + + /// Attempts to cast the contained type to another, + /// panicking if the conversion fails. + /// + /// # Example + /// + /// ```ignore + /// let value: Local<'_, Value> = get_v8_value(); + /// + /// let func = value.cast::(); + /// ``` + #[inline(always)] + pub fn cast(self) -> Local<'s, A> + where + Self: TryInto, Error: std::fmt::Debug>, + { + self.try_into().unwrap() + } +} + +/// An object reference that is independent of any handle scope. Where +/// a Local handle only lives as long as the HandleScope in which it was +/// allocated, a global handle remains valid until it is dropped. +/// +/// A global handle contains a reference to a storage cell within +/// the V8 engine which holds an object value and which is updated by +/// the garbage collector whenever the object is moved. +/// +/// You can create a `v8::Local` out of `v8::Global` using +/// `v8::Local::new(scope, global_handle)`. +/// +/// Dropping a `Global` belonging to a [`crate::SharedIsolate`] without holding +/// that isolate's [`crate::Locker`] defers resetting its V8 storage cell until +/// the next lock boundary or isolate teardown. Until then the handle remains a +/// GC root and may keep its JavaScript object graph alive. +/// +/// # Thread safety +/// +/// `Global` is [`Send`] and [`Sync`], so the handle may be moved or shared +/// between threads. This does not make the V8 heap object itself concurrently +/// accessible. Cloning, hashing, creating a [`Local`], and comparisons that +/// may involve the same isolate touch V8: for a non-shared isolate they require +/// its home thread, and for a [`crate::SharedIsolate`] they require holding its +/// [`crate::Locker`] on the current thread. These operations panic when that +/// requirement is not met or when the host isolate has been disposed. Two +/// handles known to belong to different live isolates compare unequal without +/// accessing either isolate. Dropping a `Global` is allowed on any thread and +/// may defer resetting its V8 storage cell as described above. +/// +/// `Global` deliberately does not implement [`std::borrow::Borrow`]. +/// Such an impl could return a reference that outlives the isolate or the +/// `Locker` proving access to a shared isolate. Use [`Local::new`] under a +/// handle scope instead. As a consequence, a `HashMap, _>` cannot be +/// queried by `&T`; callers needing allocation-free borrowed lookup should use +/// an embedder-owned stable key rather than the V8 object reference. +/// +/// Opening a `Global` into a plain reference is unsafe because the reference +/// could outlive its isolate or cross threads. Prefer [`Local::new`] under a +/// handle scope instead. +#[derive(Debug)] +pub struct Global { + data: NonNull, + isolate_liveness: NonNull, +} + +impl Global { + #[inline(always)] + fn assert_access_allowed(&self) { + unsafe { + self.isolate_liveness.as_ref().assert_access_allowed(); + } + } + + /// Construct a new Global from an existing Handle. + #[inline(always)] + pub fn new(isolate: &Isolate, handle: impl Handle) -> Self { + let HandleInfo { data, host } = handle.get_handle_info(); + host.assert_match_isolate(isolate); + unsafe { Self::new_raw(isolate as *const Isolate as *mut Isolate, data) } + } + + /// Implementation helper function that contains the code that can be shared + /// between `Global::new()` and `Global::clone()`. + #[inline(always)] + unsafe fn new_raw(isolate: *mut Isolate, data: NonNull) -> Self { + let data = data.cast().as_ptr(); + unsafe { + let isolate_liveness = (*isolate).global_liveness(); + // Cheap checkpoint (a relaxed load when empty) so cells dropped by + // threads that couldn't touch the isolate don't pin their JS + // objects indefinitely on isolates that are never locked. + isolate_liveness + .as_ref() + .maybe_drain_deferred_global_resets(); + let data = v8__Global__New((*isolate).as_real_ptr(), data) as *const T; + let data = NonNull::new_unchecked(data as *mut _); + Self { + data, + isolate_liveness, + } + } + } + + /// Consume this `Global` and return the underlying raw pointer. + /// + /// The returned raw pointer must be converted back into a `Global` by using + /// [`Global::from_raw`], otherwise the V8 value referenced by this global + /// handle will be pinned on the V8 heap permanently and never get garbage + /// collected. + #[inline(always)] + pub fn into_raw(self) -> NonNull { + let data = self.data; + forget(self); + data + } + + /// Converts a raw pointer created with [`Global::into_raw()`] back to its + /// original `Global`. + #[inline(always)] + pub unsafe fn from_raw(isolate: &mut Isolate, data: NonNull) -> Self { + let isolate_liveness = isolate.global_liveness(); + Self { + data, + isolate_liveness, + } + } + + /// Returns a reference to the V8 heap object represented by this handle. + /// The handle is not cloned or converted to a [`Local`]. + /// + /// Prefer [`Local::new`] whenever possible. Unlike this function, a + /// [`Local`]'s lifetime is tied to its handle scope. + /// + /// # Safety + /// + /// For the entire lifetime of the returned reference, `isolate` must remain + /// alive and the current thread must remain permitted to access it. If the + /// isolate is shared, its [`crate::Locker`] must remain held on the current + /// thread. The reference must never be sent to or accessed from another + /// thread. + /// + /// # Panics + /// + /// This function panics if the handle is not hosted by `isolate`, if the + /// isolate has been disposed, or if the current thread is not permitted to + /// access the isolate. + #[inline(always)] + pub unsafe fn open<'a>(&'a self, isolate: &mut Isolate) -> &'a T { + self.assert_access_allowed(); + self.get_handle_host().assert_match_isolate(isolate); + unsafe { &*self.data.as_ptr() } + } + + #[inline(always)] + fn get_handle_host(&self) -> HandleHost { + let isolate = unsafe { self.isolate_liveness.as_ref().get_isolate_ptr() }; + NonNull::new(isolate) + .map_or(HandleHost::DisposedIsolate, HandleHost::Isolate) + } + + /// Panics unless the `Local`s of the scopes open on this thread belong to + /// the `Isolate` that hosts this handle. + pub(crate) fn assert_locals_in_same_isolate(&self) { + HandleHost::Scope.assert_match_host(self.get_handle_host(), None); + } +} + +/// Panics unless the `Local`s of the scopes open on this thread belong to +/// `isolate`. APIs that take an `Isolate` and `Local`s as separate arguments +/// call this, because the scope rule doesn't tie the two together. +pub(crate) fn assert_locals_in_isolate(isolate: &Isolate) { + HandleHost::Scope.assert_match_isolate(isolate); +} + +/// Like [`assert_locals_in_isolate`], for an `Isolate` that may have been +/// disposed. +pub(crate) fn assert_locals_in_isolate_of(isolate_handle: &IsolateHandle) { + HandleHost::Scope.assert_match_host(isolate_handle.into(), None); +} + +// A `Global` only touches V8 through methods that either take a scope or +// `&Isolate` argument (obtainable only on the isolate's thread or under +// its Locker), or that are guarded through `IsolateLiveness`: `clone`, +// `eq` and `hash` assert the current thread may touch the isolate, and +// `drop` releases the cell immediately when it may, deferring to the +// liveness queue otherwise. +unsafe impl Send for Global {} +unsafe impl Sync for Global {} + +impl Clone for Global { + fn clone(&self) -> Self { + self.assert_access_allowed(); + let HandleInfo { data, host } = self.get_handle_info(); + let mut isolate = unsafe { Isolate::from_non_null(host.get_isolate()) }; + unsafe { Self::new_raw(isolate.as_mut(), data) } + } +} + +impl Drop for Global { + fn drop(&mut self) { + unsafe { + let liveness = self.isolate_liveness.as_ref(); + if liveness.get_isolate_ptr().is_null() { + // This `Global` handle is associated with an `Isolate` that has already + // been disposed. + } else if !liveness.is_shared() && liveness.on_home_thread() { + // Destroy the storage cell that contains the contents of this Global. + v8__Global__Reset(self.data.cast().as_ptr()); + liveness.maybe_drain_deferred_global_resets(); + } else { + // Another thread may own the isolate right now; release the cell + // immediately if we may touch it, otherwise defer to the next + // lock acquisition or isolate teardown. + liveness.reset_or_defer_global(self.data.cast()); + } + } + } +} + +/// An implementation of [`Handle`] that can be constructed unsafely from a +/// reference. +pub(crate) struct UnsafeRefHandle<'a, T> { + reference: &'a T, + isolate_handle: IsolateHandle, +} +impl<'a, T> UnsafeRefHandle<'a, T> { + /// Constructs an `UnsafeRefHandle`. + /// + /// # Safety + /// + /// `reference` must be derived from a [`Local`] or [`Global`] handle, and its + /// lifetime must not outlive that handle. Furthermore, `isolate` must be the + /// isolate associated with the handle (for [`Local`], the current isolate; + /// for [`Global`], the isolate you would pass to the unsafe + /// [`Global::open()`] method). + #[inline(always)] + pub unsafe fn new(reference: &'a T, isolate: &mut Isolate) -> Self { + UnsafeRefHandle { + reference, + isolate_handle: isolate.thread_safe_handle(), + } + } +} + +pub trait Handle: Sized { + type Data; + + #[doc(hidden)] + fn get_handle_info(&self) -> HandleInfo; + + #[doc(hidden)] + fn assert_safe_to_access(&self) {} + + /// Reads the inner value contained in this handle, _without_ verifying that + /// the this handle is hosted by the currently active `Isolate`. + /// + /// # Safety + /// + /// Using a V8 heap object with another `Isolate` than the `Isolate` that + /// hosts it is not permitted under any circumstance. Doing so leads to + /// undefined behavior, likely a crash. + /// + /// For the entire lifetime of the returned reference, its handle and host + /// isolate must remain alive and the current thread must remain permitted to + /// access that isolate. If this is a [`Global`] belonging to a shared isolate, + /// its [`crate::Locker`] must remain held on the current thread. The reference + /// must never be sent to or accessed from another thread. + /// + /// # Panics + /// + /// This function panics if the `Isolate` that hosts the handle has been + /// disposed or, for a [`Global`], if the current thread is not permitted to + /// access it. + unsafe fn get_unchecked(&self) -> &Self::Data { + self.assert_safe_to_access(); + let HandleInfo { data, host } = self.get_handle_info(); + if let HandleHost::DisposedIsolate = host { + panic!("attempt to access Handle hosted by disposed Isolate"); + } + unsafe { &*data.as_ptr() } + } +} + +impl Handle for Local<'_, T> { + type Data = T; + fn get_handle_info(&self) -> HandleInfo { + HandleInfo::new(self.as_non_null(), HandleHost::Scope) + } +} + +impl<'a, 's: 'a, T> Handle for &'a Local<'s, T> { + type Data = T; + fn get_handle_info(&self) -> HandleInfo { + HandleInfo::new(self.as_non_null(), HandleHost::Scope) + } +} + +impl Handle for Global { + type Data = T; + fn get_handle_info(&self) -> HandleInfo { + HandleInfo::new(self.data, self.get_handle_host()) + } + fn assert_safe_to_access(&self) { + self.assert_access_allowed(); + } +} + +impl Handle for &Global { + type Data = T; + fn get_handle_info(&self) -> HandleInfo { + HandleInfo::new(self.data, self.get_handle_host()) + } + fn assert_safe_to_access(&self) { + self.assert_access_allowed(); + } +} + +impl Handle for UnsafeRefHandle<'_, T> { + type Data = T; + fn get_handle_info(&self) -> HandleInfo { + HandleInfo::new( + NonNull::from(self.reference), + (&self.isolate_handle).into(), + ) + } +} + +impl Handle for &UnsafeRefHandle<'_, T> { + type Data = T; + fn get_handle_info(&self) -> HandleInfo { + HandleInfo::new( + NonNull::from(self.reference), + (&self.isolate_handle).into(), + ) + } +} + +impl Borrow for Local<'_, T> { + fn borrow(&self) -> &T { + self + } +} + +// `Borrow for Global` is deliberately absent. `fn borrow(&self) -> &T` +// has nowhere to take proof that the caller may touch the isolate, and +// nowhere to tie the returned reference's lifetime to that proof: any check +// it made would expire while the `&T` it handed out stayed alive. Heap-object +// wrappers are `!Sync`, which prevents moving that reference to another +// thread, but does not prevent it from outliving a Locker or the isolate. Use +// `Local::new(scope, &global)` instead — a `Local` is bound to a scope, which +// is bound to the isolate. + +impl Eq for Local<'_, T> where T: Eq {} +impl Eq for Global where T: Eq {} + +impl Hash for Local<'_, T> { + fn hash(&self, state: &mut H) { + (**self).hash(state); + } +} + +impl Hash for Global { + fn hash(&self, state: &mut H) { + // Hashing may call into V8 (e.g. `Object::GetIdentityHash`, which can + // mutate the object), so it needs the same gate as any other access. + if unsafe { self.isolate_liveness.as_ref().get_isolate_ptr().is_null() } { + panic!("can't hash Global after its host Isolate has been disposed"); + } + self.assert_access_allowed(); + unsafe { self.data.as_ref().hash(state) } + } +} + +impl PartialEq for Local<'_, T> +where + T: PartialEq, +{ + fn eq(&self, other: &Rhs) -> bool { + let i1 = self.get_handle_info(); + let i2 = other.get_handle_info(); + if i1.host.are_different_live_isolates(i2.host) { + return false; + } + self.assert_safe_to_access(); + other.assert_safe_to_access(); + i1.host.match_host(i2.host, None) + && unsafe { i1.data.as_ref() == i2.data.as_ref() } + } +} + +impl PartialEq for Global +where + T: PartialEq, +{ + fn eq(&self, other: &Rhs) -> bool { + let i1 = self.get_handle_info(); + let i2 = other.get_handle_info(); + // Distinct live isolates cannot contain the same V8 object. This check + // does not touch either isolate, so preserve the historical `false` + // result even if one Global is currently off its home/Locker thread. + if i1.host.are_different_live_isolates(i2.host) { + return false; + } + self.assert_safe_to_access(); + other.assert_safe_to_access(); + if !i1.host.match_host(i2.host, None) { + return false; + } + // Comparison calls into V8 (e.g. `Value::SameValue`); both operands + // were gated by `assert_safe_to_access` above. + unsafe { i1.data.as_ref() == i2.data.as_ref() } + } +} + +#[derive(Copy, Debug, Clone)] +pub struct HandleInfo { + data: NonNull, + host: HandleHost, +} + +impl HandleInfo { + fn new(data: NonNull, host: HandleHost) -> Self { + Self { data, host } + } +} + +#[derive(Copy, Debug, Clone)] +enum HandleHost { + // Note: the `HandleHost::Scope` variant does not indicate that the handle + // it applies to is not associated with an `Isolate`. It only means that + // the handle is a `Local` handle that was unable to provide a pointer to + // the `Isolate` that hosts it (the handle) and the currently entered + // scope. + Scope, + Isolate(NonNull), + DisposedIsolate, +} + +impl From<&'_ Isolate> for HandleHost { + fn from(isolate: &'_ Isolate) -> Self { + Self::Isolate(unsafe { NonNull::new_unchecked(isolate.as_real_ptr()) }) + } +} + +impl From<&'_ IsolateHandle> for HandleHost { + fn from(isolate_handle: &IsolateHandle) -> Self { + NonNull::new(unsafe { isolate_handle.get_isolate_ptr() }) + .map_or(Self::DisposedIsolate, Self::Isolate) + } +} + +impl HandleHost { + fn are_different_live_isolates(self, other: Self) -> bool { + matches!( + (self, other), + (Self::Isolate(left), Self::Isolate(right)) if left != right + ) + } + + /// Compares two `HandleHost` values, returning `true` if they refer to the + /// same `Isolate`, or `false` if they refer to different isolates. + /// + /// A `Scope` host refers to the `Isolate` that owns the scopes open on this + /// thread (see "Isolates" in the `scope` module documentation). When no + /// scope is open, the caller can say which `Isolate` the handle's scope + /// belongs to via the second argument (`scope_isolate_opt`). + /// + /// # Panics + /// + /// This function panics if one of the `HandleHost` values refers to an + /// `Isolate` that has been disposed. + /// + /// # Safety / Bugs + /// + /// If no scope is open and the caller does not provide a value for the + /// `scope_isolate_opt` argument, the `Isolate` of a `Scope` host is unknown, + /// and this function returns `true`. A `Local` can only exist outside of a + /// scope through `unsafe` code. + fn match_host( + self, + other: Self, + scope_isolate_opt: Option<&Isolate>, + ) -> bool { + let scope_isolate_opt_nn = + crate::scope::scope_owner::current().or_else(|| { + scope_isolate_opt.map(|isolate| unsafe { + NonNull::new_unchecked(isolate.as_real_ptr()) + }) + }); + match (self, other, scope_isolate_opt_nn) { + (Self::Scope, Self::Scope, _) => true, + (Self::Isolate(ile1), Self::Isolate(ile2), _) => ile1 == ile2, + (Self::Scope, Self::Isolate(ile1), Some(ile2)) => ile1 == ile2, + (Self::Isolate(ile1), Self::Scope, Some(ile2)) => ile1 == ile2, + (Self::Scope, Self::Isolate(_), None) => true, + (Self::Isolate(_), Self::Scope, None) => true, + // Handles hosted in an Isolate that has been disposed aren't good for + // anything, even if a pair of handles used to to be hosted in the same + // now-disposed solate. + (Self::DisposedIsolate, ..) | (_, Self::DisposedIsolate, _) => { + panic!("attempt to access Handle hosted by disposed Isolate") + } + } + } + + fn assert_match_host(self, other: Self, scope_opt: Option<&Isolate>) { + assert!( + self.match_host(other, scope_opt), + "attempt to use Handle in an Isolate that is not its host" + ); + } + + #[allow(dead_code)] + fn match_isolate(self, isolate: &Isolate) -> bool { + self.match_host(isolate.into(), Some(isolate)) + } + + fn assert_match_isolate(self, isolate: &Isolate) { + self.assert_match_host(isolate.into(), Some(isolate)); + } + + fn get_isolate(self) -> NonNull { + match self { + Self::Scope => panic!("host Isolate for Handle not available"), + Self::Isolate(ile) => ile, + Self::DisposedIsolate => panic!("attempt to access disposed Isolate"), + } + } + + #[allow(dead_code)] + fn get_isolate_handle(self) -> IsolateHandle { + let isolate = unsafe { Isolate::from_non_null(self.get_isolate()) }; + isolate.thread_safe_handle() + } +} + +/// An object reference that does not prevent garbage collection for the object, +/// and which allows installing finalization callbacks which will be called +/// after the object has been GC'd. +/// +/// Note that finalization callbacks are tied to the lifetime of a `Weak`, +/// and will not be called after the `Weak` is dropped. +/// +/// # `Clone` +/// +/// Since finalization callbacks are specific to a `Weak` instance, cloning +/// will create a new object reference without a finalizer, as if created by +/// [`Self::new`]. You can use [`Self::clone_with_finalizer`] to attach a +/// finalization callback to the clone. +#[derive(Debug)] +pub struct Weak { + data: Option>>, + isolate_handle: IsolateHandle, +} + +impl Weak { + pub fn new(isolate: &mut Isolate, handle: impl Handle) -> Self { + let HandleInfo { data, host } = handle.get_handle_info(); + host.assert_match_isolate(isolate); + Self::new_raw(isolate, data, None) + } + + /// Create a weak handle with a finalization callback installed. + /// + /// There is no guarantee as to *when* or even *if* the finalization callback + /// will be invoked. The invocation is performed solely on a best effort + /// basis. GC-based finalization should *not* be relied upon for any critical + /// form of resource management! Consider using + /// [`Self::with_guaranteed_finalizer`] instead. + /// + /// The callback does not have access to the inner value, because it has + /// already been collected by the time it runs. + pub fn with_finalizer( + isolate: &mut Isolate, + handle: impl Handle, + finalizer: Box, + ) -> Self { + let HandleInfo { data, host } = handle.get_handle_info(); + host.assert_match_isolate(isolate); + Self::assert_supported(isolate); + let finalizer_id = isolate + .get_finalizer_map_mut() + .add(FinalizerCallback::Regular(finalizer)); + Self::new_raw(isolate, data, Some(finalizer_id)) + } + + /// Create a weak handle with a finalization callback installed, which is + /// guaranteed to run at some point. + /// + /// Unlike [`Self::with_finalizer`], whose finalization callbacks are not + /// guaranteed to run, this method is guaranteed to be called before the + /// isolate is destroyed. It can therefore be used for critical resource + /// management. Note that other than that, there is still no guarantee as to + /// *when* the callback will be called. + /// + /// Unlike regular finalizers, guaranteed finalizers aren't passed a mutable + /// [`Isolate`] reference, since they might be called when the isolate is + /// being destroyed, at which point it might be no longer valid to use. + /// Accessing the isolate (with unsafe code) from the finalizer callback is + /// therefore unsound, unless you prove the isolate is not being destroyed. + pub fn with_guaranteed_finalizer( + isolate: &mut Isolate, + handle: impl Handle, + finalizer: Box, + ) -> Self { + let HandleInfo { data, host } = handle.get_handle_info(); + host.assert_match_isolate(isolate); + Self::assert_supported(isolate); + let finalizer_id = isolate + .get_finalizer_map_mut() + .add(FinalizerCallback::Guaranteed(finalizer)); + Self::new_raw(isolate, data, Some(finalizer_id)) + } + + fn new_raw( + isolate: *mut Isolate, + data: NonNull, + finalizer_id: Option, + ) -> Self { + Self::assert_supported(isolate); + unsafe { *(*isolate).live_weak_count_mut() += 1 }; + let weak_data = Box::new(WeakData { + pointer: Default::default(), + finalizer_id, + weak_dropped: Cell::new(false), + }); + let data = data.cast().as_ptr(); + let data = unsafe { + v8__Global__NewWeak( + (*isolate).as_real_ptr(), + data, + weak_data.deref() as *const _ as *const c_void, + Self::first_pass_callback, + ) + }; + weak_data + .pointer + .set(Some(unsafe { NonNull::new_unchecked(data as *mut _) })); + Self { + data: Some(weak_data), + isolate_handle: unsafe { (*isolate).thread_safe_handle() }, + } + } + + fn assert_supported(isolate: *mut Isolate) { + // Weak callbacks fire during GC on whichever thread holds a shared + // isolate's lock, racing the `WeakData` owned by this (non-Send) + // handle on its home thread. + assert!( + !unsafe { (*isolate).global_liveness().as_ref() }.is_shared(), + "v8::Weak is not supported on shared isolates" + ); + } + + /// Creates a new empty handle, identical to one for an object that has + /// already been GC'd. + pub fn empty(isolate: &mut Isolate) -> Self { + Weak { + data: None, + isolate_handle: isolate.thread_safe_handle(), + } + } + + /// Clones this handle and installs a finalizer callback on the clone, as if + /// by calling [`Self::with_finalizer`]. + /// + /// Note that if this handle is empty (its value has already been GC'd), the + /// finalization callback will never run. + pub fn clone_with_finalizer( + &self, + finalizer: Box, + ) -> Self { + self.clone_raw(Some(FinalizerCallback::Regular(finalizer))) + } + + /// Clones this handle and installs a guaranteed finalizer callback on the + /// clone, as if by calling [`Self::with_guaranteed_finalizer`]. + /// + /// Note that if this handle is empty (its value has already been GC'd), the + /// finalization callback will never run. + pub fn clone_with_guaranteed_finalizer( + &self, + finalizer: Box, + ) -> Self { + self.clone_raw(Some(FinalizerCallback::Guaranteed(finalizer))) + } + + fn clone_raw(&self, finalizer: Option) -> Self { + if let Some(data) = self.get_pointer() { + // SAFETY: We're in the isolate's thread, because Weak isn't Send or + // Sync. + let isolate_ptr = unsafe { self.isolate_handle.get_isolate_ptr() }; + if isolate_ptr.is_null() { + unreachable!("Isolate was dropped but weak handle wasn't reset."); + } + let mut isolate = unsafe { Isolate::from_raw_ptr(isolate_ptr) }; + Self::assert_supported(&mut isolate); + let finalizer_id = finalizer + .map(|finalizer| isolate.get_finalizer_map_mut().add(finalizer)); + Self::new_raw(&mut isolate, data, finalizer_id) + } else { + Weak { + data: None, + isolate_handle: self.isolate_handle.clone(), + } + } + } + + /// Converts an optional raw pointer created with [`Weak::into_raw()`] back to + /// its original `Weak`. + /// + /// This method is called with `Some`, the pointer is invalidated and it + /// cannot be used with this method again. Additionally, it is unsound to call + /// this method with an isolate other than that in which the original `Weak` + /// was created. + /// + /// # Panics + /// + /// Panics if called with `Some` for a shared isolate. + pub unsafe fn from_raw( + isolate: &mut Isolate, + data: Option>>, + ) -> Self { + if data.is_some() { + Self::assert_supported(isolate); + } + Weak { + data: data.map(|raw| unsafe { Box::from_raw(raw.cast().as_ptr()) }), + isolate_handle: isolate.thread_safe_handle(), + } + } + + /// Consume this `Weak` handle and return the underlying raw pointer, or + /// `None` if the value has been GC'd. + /// + /// The return value can be converted back into a `Weak` by using + /// [`Weak::from_raw`]. Note that `Weak` allocates some memory, and if this + /// method returns `Some`, the pointer must be converted back into a `Weak` + /// for it to be freed. + /// + /// Note that this method might return `Some` even after the V8 value has been + /// GC'd. + pub fn into_raw(mut self) -> Option>> { + if let Some(data) = self.data.take() { + let has_finalizer = if let Some(finalizer_id) = data.finalizer_id { + // SAFETY: We're in the isolate's thread because Weak isn't Send or Sync + let isolate_ptr = unsafe { self.isolate_handle.get_isolate_ptr() }; + if isolate_ptr.is_null() { + // Disposed isolates have no finalizers. + false + } else { + let isolate = unsafe { Isolate::from_raw_ptr(isolate_ptr) }; + isolate.get_finalizer_map().map.contains_key(&finalizer_id) + } + } else { + false + }; + + if data.pointer.get().is_none() && !has_finalizer { + // If the pointer is None and we're not waiting for the second pass, + // drop the box and release its count. A `Some(raw)` return keeps the + // count until `from_raw` re-adopts the box and the resulting `Weak` + // is dropped; leaking the raw pointer conservatively keeps sharing + // disabled. + // SAFETY: we're in the isolate's thread because `Weak` isn't Send or + // Sync. + let isolate_ptr = unsafe { self.isolate_handle.get_isolate_ptr() }; + if !isolate_ptr.is_null() { + let mut isolate = unsafe { Isolate::from_raw_ptr(isolate_ptr) }; + isolate.release_live_weak(); + } + None + } else { + assert!(!data.weak_dropped.get()); + Some(unsafe { NonNull::new_unchecked(Box::into_raw(data)) }) + } + } else { + None + } + } + + fn get_pointer(&self) -> Option> { + if let Some(data) = &self.data { + // It seems like when the isolate is dropped, even the first pass callback + // might not be called. + if unsafe { self.isolate_handle.get_isolate_ptr() }.is_null() { + None + } else { + data.pointer.get() + } + } else { + None + } + } + + pub fn is_empty(&self) -> bool { + self.get_pointer().is_none() + } + + pub fn to_global(&self, isolate: &mut Isolate) -> Option> { + if let Some(data) = self.get_pointer() { + let handle_host: HandleHost = (&self.isolate_handle).into(); + handle_host.assert_match_isolate(isolate); + Some(unsafe { Global::new_raw(isolate, data) }) + } else { + None + } + } + + pub fn to_local<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + ) -> Option> { + if let Some(data) = self.get_pointer() { + let handle_host: HandleHost = (&self.isolate_handle).into(); + handle_host.assert_match_isolate(scope); + let local = unsafe { + scope.cast_local(|sd| { + v8__Local__New(sd.get_isolate_ptr(), data.cast().as_ptr()) as *const T + }) + }; + Some(local.unwrap()) + } else { + None + } + } + + // Finalization callbacks. + + unsafe extern "C" fn first_pass_callback(wci: *const WeakCallbackInfo) { + // SAFETY: If this callback is called, then the weak handle hasn't been + // reset, which means the `Weak` instance which owns the pinned box that the + // parameter points to hasn't been dropped. + let weak_data = unsafe { + let ptr = v8__WeakCallbackInfo__GetParameter(wci); + &*(ptr as *mut WeakData) + }; + + let data = weak_data.pointer.take().unwrap(); + unsafe { + v8__Global__Reset(data.cast().as_ptr()); + } + + // Only set the second pass callback if there could be a finalizer. + if weak_data.finalizer_id.is_some() { + unsafe { + v8__WeakCallbackInfo__SetSecondPassCallback( + wci, + Self::second_pass_callback, + ); + }; + } + } + + unsafe extern "C" fn second_pass_callback(wci: *const WeakCallbackInfo) { + // SAFETY: This callback is guaranteed by V8 to be called in the isolate's + // thread before the isolate is disposed. + let isolate = unsafe { v8__WeakCallbackInfo__GetIsolate(wci) }; + + // SAFETY: This callback might be called well after the first pass callback, + // which means the corresponding Weak might have been dropped. In Weak's + // Drop impl we make sure that if the second pass callback hasn't yet run, the + // Box> is leaked, so it will still be alive by the time this + // callback is called. + let weak_data = unsafe { + let ptr = v8__WeakCallbackInfo__GetParameter(wci); + &*(ptr as *mut WeakData) + }; + + let mut isolate = unsafe { Isolate::from_raw_ptr(isolate) }; + let finalizer: Option = { + let finalizer_id = weak_data.finalizer_id.unwrap(); + isolate.get_finalizer_map_mut().map.remove(&finalizer_id) + }; + + if weak_data.weak_dropped.get() { + // SAFETY: If weak_dropped is true, the corresponding Weak has been dropped, + // so it's safe to take ownership of the Box> and drop it. + let _ = unsafe { + Box::from_raw(weak_data as *const WeakData as *mut WeakData) + }; + } + + match finalizer { + Some(FinalizerCallback::Regular(finalizer)) => finalizer(&mut isolate), + Some(FinalizerCallback::Guaranteed(finalizer)) => finalizer(), + None => {} + } + } +} + +impl Clone for Weak { + fn clone(&self) -> Self { + self.clone_raw(None) + } +} + +impl Drop for Weak { + fn drop(&mut self) { + // `data` is `Some` iff this handle was created through `new_raw` and + // thus counted. SAFETY: we're in the isolate's thread because `Weak` + // isn't Send or Sync. + if self.data.is_some() { + let isolate_ptr = unsafe { self.isolate_handle.get_isolate_ptr() }; + if !isolate_ptr.is_null() { + let mut isolate = unsafe { Isolate::from_raw_ptr(isolate_ptr) }; + isolate.release_live_weak(); + } + } + + // Returns whether the finalizer existed. + let remove_finalizer = |finalizer_id: Option| -> bool { + if let Some(finalizer_id) = finalizer_id { + // SAFETY: We're in the isolate's thread because `Weak` isn't Send or Sync. + let isolate_ptr = unsafe { self.isolate_handle.get_isolate_ptr() }; + if !isolate_ptr.is_null() { + let mut isolate = unsafe { Isolate::from_raw_ptr(isolate_ptr) }; + let finalizer = + isolate.get_finalizer_map_mut().map.remove(&finalizer_id); + return finalizer.is_some(); + } + } + false + }; + + if let Some(data) = self.get_pointer() { + // If the pointer is not None, the first pass callback hasn't been + // called yet, and resetting will prevent it from being called. + unsafe { v8__Global__Reset(data.cast().as_ptr()) }; + remove_finalizer(self.data.as_ref().unwrap().finalizer_id); + } else if let Some(weak_data) = self.data.take() { + // The second pass callback removes the finalizer, so if there is one, + // the second pass hasn't yet run, and WeakData will have to be alive. + // In that case we leak the WeakData but remove the finalizer. + if remove_finalizer(weak_data.finalizer_id) { + weak_data.weak_dropped.set(true); + Box::leak(weak_data); + } + } + } +} + +impl Eq for Weak where T: Eq {} + +impl PartialEq for Weak +where + T: PartialEq, +{ + fn eq(&self, other: &Rhs) -> bool { + let HandleInfo { + data: other_data, + host: other_host, + } = other.get_handle_info(); + let self_host: HandleHost = (&self.isolate_handle).into(); + if !self_host.match_host(other_host, None) { + false + } else if let Some(self_data) = self.get_pointer() { + unsafe { self_data.as_ref() == other_data.as_ref() } + } else { + false + } + } +} + +impl PartialEq> for Weak +where + T: PartialEq, +{ + fn eq(&self, other: &Weak) -> bool { + let self_host: HandleHost = (&self.isolate_handle).into(); + let other_host: HandleHost = (&other.isolate_handle).into(); + if !self_host.match_host(other_host, None) { + return false; + } + match (self.get_pointer(), other.get_pointer()) { + (Some(self_data), Some(other_data)) => unsafe { + self_data.as_ref() == other_data.as_ref() + }, + (None, None) => true, + _ => false, + } + } +} + +/// The inner mechanism behind [`Weak`] and finalizations. +/// +/// This struct is heap-allocated and will not move until it's dropped, so it +/// can be accessed by the finalization callbacks by creating a shared reference +/// from a pointer. The fields are wrapped in [`Cell`] so they are modifiable by +/// both the [`Weak`] and the finalization callbacks. +pub struct WeakData { + pointer: Cell>>, + finalizer_id: Option, + weak_dropped: Cell, +} + +impl std::fmt::Debug for WeakData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("WeakData") + .field("pointer", &self.pointer) + .finish_non_exhaustive() + } +} + +#[repr(C)] +struct WeakCallbackInfo(Opaque); + +type FinalizerId = usize; + +pub(crate) enum FinalizerCallback { + Regular(Box), + Guaranteed(Box), +} + +#[derive(Default)] +pub(crate) struct FinalizerMap { + map: std::collections::HashMap, + next_id: FinalizerId, +} + +impl FinalizerMap { + pub(crate) fn is_empty(&self) -> bool { + self.map.is_empty() + } + + fn add(&mut self, finalizer: FinalizerCallback) -> FinalizerId { + let id = self.next_id; + // TODO: Overflow. + self.next_id += 1; + self.map.insert(id, finalizer); + id + } + + pub(crate) fn drain( + &mut self, + ) -> impl Iterator + '_ { + self.map.drain().map(|(_, finalizer)| finalizer) + } +} + +/// Panics unless the `TracedReference` or `Eternal` whose isolate is `set_in` +/// is empty or was set in `isolate`. +fn assert_set_in_isolate(set_in: &AtomicUsize, isolate: &Isolate) { + let set_in = set_in.load(Ordering::Relaxed); + assert!( + set_in == 0 || set_in == isolate.global_liveness().as_ptr() as usize, + "attempt to get a handle in an Isolate other than the one it was set in" + ); +} + +/// A traced handle without destructor that clears the handle. The embedder needs +/// to ensure that the handle is not accessed once the V8 object has been +/// reclaimed. For more details see BasicTracedReference. +#[repr(C)] +pub struct TracedReference { + data: [u8; crate::binding::v8__TracedReference_SIZE], + // The address of the `IsolateLiveness` of the isolate the reference was set + // in, or 0 if it is empty. Unlike isolate addresses, these are never + // reused. + isolate: AtomicUsize, + _phantom: PhantomData, +} + +impl TracedReference { + /// An empty TracedReference without storage cell. + pub fn empty() -> Self { + let mut this = std::mem::MaybeUninit::::uninit(); + unsafe { + v8__TracedReference__CONSTRUCT(this.as_mut_ptr() as _); + std::ptr::addr_of_mut!((*this.as_mut_ptr()).isolate) + .write(AtomicUsize::new(0)); + this.assume_init() + } + } + + /// Construct a TracedReference from a Local. + /// + /// A new storage cell is created pointing to the same object. + pub fn new<'s>(scope: &PinScope<'s, '_, ()>, data: Local<'s, T>) -> Self { + let mut this = Self::empty(); + this.reset(scope, Some(data)); + this + } + + /// # Panics + /// + /// Panics if the reference was set in another `Isolate`. + pub fn get<'s>(&self, scope: &PinScope<'s, '_, ()>) -> Option> { + assert_set_in_isolate(&self.isolate, scope); + unsafe { + scope.cast_local(|sd| { + v8__TracedReference__Get( + self as *const Self as *const TracedReference, + sd.get_isolate_ptr(), + ) as *const T + }) + } + } + + /// Always resets the reference. Creates a new reference from `other` if it is + /// non-empty. + /// + /// # Panics + /// + /// Panics if the reference was set in another `Isolate`. + pub fn reset<'s>( + &mut self, + scope: &PinScope<'s, '_, ()>, + data: Option>, + ) { + assert_set_in_isolate(&self.isolate, scope); + let isolate = match data { + Some(_) => scope.global_liveness().as_ptr() as usize, + None => 0, + }; + self.isolate.store(isolate, Ordering::Relaxed); + unsafe { + v8__TracedReference__Reset( + self as *mut Self as *mut TracedReference, + scope.get_isolate_ptr(), + data + .map_or(std::ptr::null_mut(), |h| h.as_non_null().as_ptr()) + .cast(), + ); + } + } +} + +impl Drop for TracedReference { + fn drop(&mut self) { + unsafe { + v8__TracedReference__DESTRUCT( + self as *mut Self as *mut TracedReference, + ); + } + } +} + +/// Eternal handles are set-once handles that live for the lifetime of the isolate. +#[repr(C)] +pub struct Eternal { + data: [u8; crate::binding::v8__Eternal_SIZE], + // The address of the `IsolateLiveness` of the isolate the handle was set + // in, or 0 if it is empty. An eternal handle is an index into its isolate's + // table of eternal handles, which means nothing in another isolate. + isolate: AtomicUsize, + _phantom: PhantomData, +} + +impl Eternal { + pub fn empty() -> Self { + let mut this = std::mem::MaybeUninit::::uninit(); + unsafe { + v8__Eternal__CONSTRUCT(this.as_mut_ptr() as _); + std::ptr::addr_of_mut!((*this.as_mut_ptr()).isolate) + .write(AtomicUsize::new(0)); + this.assume_init() + } + } + + pub fn clear(&self) { + self.isolate.store(0, Ordering::Relaxed); + unsafe { + v8__Eternal__Clear(self as *const Self as *mut Eternal); + } + } + + pub fn set<'s>(&self, scope: &PinScope<'s, '_, ()>, data: Local<'s, T>) { + self + .isolate + .store(scope.global_liveness().as_ptr() as usize, Ordering::Relaxed); + unsafe { + v8__Eternal__Set( + self as *const Self as *mut Eternal, + scope.get_isolate_ptr(), + data.as_non_null().as_ptr().cast(), + ); + } + } + + /// # Panics + /// + /// Panics if the handle was set in another `Isolate`. + pub fn get<'s>(&self, scope: &PinScope<'s, '_, ()>) -> Option> { + assert_set_in_isolate(&self.isolate, scope); + unsafe { + scope.cast_local(|sd| { + v8__Eternal__Get( + self as *const Self as *const Eternal, + sd.get_isolate_ptr(), + ) as *const T + }) + } + } + + pub fn is_empty(&self) -> bool { + unsafe { v8__Eternal__IsEmpty(self as *const Self as *const Eternal) } + } +} + +impl Drop for Eternal { + fn drop(&mut self) { + unsafe { + v8__Eternal__DESTRUCT(self as *mut Self as *mut Eternal); + } + } +} + +/// A Local passed from V8 without an inherent scope. +/// The value must be "unsealed" with Scope::unseal to bind +/// it to a lifetime. +#[derive(Debug)] +#[repr(transparent)] +pub struct SealedLocal(pub(crate) NonNull); diff --git a/crates/v8/src/icu.rs b/crates/v8/src/icu.rs new file mode 100644 index 0000000000..cb76ce0cf4 --- /dev/null +++ b/crates/v8/src/icu.rs @@ -0,0 +1,114 @@ +use crate::support::char; + +use std::ffi::CString; + +unsafe extern "C" { + fn icu_get_default_locale(output: *mut char, output_len: usize) -> usize; + fn icu_set_default_locale(locale: *const char); + fn icu_get_default_time_zone(output: *mut char, output_len: usize) -> usize; + fn icu_set_default_time_zone(time_zone_id: *const char) -> bool; + fn udata_setCommonData_78(this: *const u8, error_code: *mut i32); +} + +/// This function bypasses the normal ICU data loading process and allows you to force ICU's system +/// data to come out of a user-specified area in memory. +/// +/// ICU data must be at least 8-aligned, and should be 16-aligned. See +/// https://unicode-org.github.io/icu/userguide/icu_data/ +/// +/// The format of this data is that of the icu common data file, as is generated by the pkgdata +/// tool with mode=common or mode=dll. You can read in a whole common mode file and pass the +/// address to the start of the data, or (with the appropriate link options) pass in the pointer to +/// the data that has been loaded from a dll by the operating system, as shown in this code: +/// +/// ```c++ +/// extern const char U_IMPORT U_ICUDATA_ENTRY_POINT []; +/// // U_ICUDATA_ENTRY_POINT is same as entry point specified to pkgdata tool +/// UErrorCode status = U_ZERO_ERROR; +/// +/// udata_setCommonData(&U_ICUDATA_ENTRY_POINT, &status); +/// ``` +/// +/// It is important that the declaration be as above. The entry point must not be declared as an +/// extern void*. +/// +/// Starting with ICU 4.4, it is possible to set several data packages, one per call to this +/// function. udata_open() will look for data in the multiple data packages in the order in which +/// they were set. The position of the linked-in or default-name ICU .data package in the search +/// list depends on when the first data item is loaded that is not contained in the already +/// explicitly set packages. If data was loaded implicitly before the first call to this function +/// (for example, via opening a converter, constructing a UnicodeString from default-codepage data, +/// using formatting or collation APIs, etc.), then the default data will be first in the list. +/// +/// This function has no effect on application (non ICU) data. See udata_setAppData() for similar +/// functionality for application data. +// TODO(ry) Map error code to something useful. +#[inline(always)] +pub fn set_common_data_78(data: &'static [u8]) -> Result<(), i32> { + let mut error_code = 0i32; + unsafe { + udata_setCommonData_78(data.as_ptr(), &mut error_code); + } + if error_code == 0 { + Ok(()) + } else { + Err(error_code) + } +} + +/// Returns BCP47 language tag. +pub fn get_language_tag() -> String { + let mut output = [0u8; 1024]; + let len = unsafe { + icu_get_default_locale(output.as_mut_ptr() as *mut char, output.len()) + }; + std::str::from_utf8(&output[..len]).unwrap().to_owned() +} + +pub fn set_default_locale(locale: &str) { + unsafe { + let c_str = CString::new(locale).expect("Invalid locale"); + icu_set_default_locale(c_str.as_ptr()); + } +} + +/// Returns the id of ICU's current default time zone, usually an IANA id such +/// as `"America/New_York"`. It can also be a custom offset id like +/// `"GMT+05:00"`, either because one was installed with +/// [`set_default_time_zone`] or because the host reported its zone that way, +/// so don't assume the result resolves against the tz database. +/// +/// If the host time zone could not be determined, ICU reports the special +/// id `"Etc/Unknown"`, which behaves as GMT. +pub fn get_default_time_zone() -> String { + let mut output = [0u8; 1024]; + let len = unsafe { + icu_get_default_time_zone(output.as_mut_ptr() as *mut char, output.len()) + }; + std::str::from_utf8(&output[..len]).unwrap().to_owned() +} + +/// Sets ICU's default time zone from a time zone id (e.g. `"UTC"`, +/// `"Asia/Manila"`). This makes `Date` resolve the given zone on every +/// platform, including Windows, where ICU otherwise reads the host time +/// zone from the OS and ignores the `TZ` environment variable. +/// +/// Returns `false` — leaving the current default untouched — if `time_zone_id` +/// is not a time zone id ICU recognizes. Note that ICU's own "unknown zone" +/// id, `"Etc/Unknown"`, is rejected as well. Besides IANA ids, ICU accepts +/// custom offset ids such as `"GMT+05:00"`. +/// +/// This mutates process wide state and is not synchronized with isolates +/// running on other threads, so it should be called before those isolates +/// evaluate any code observing the time zone. Afterwards, notify each isolate +/// via [`crate::Isolate::date_time_configuration_change_notification`] with +/// [`crate::TimeZoneDetection::Skip`] so cached values are refreshed without +/// re-detecting (and overwriting) the zone just set here. +#[must_use] +pub fn set_default_time_zone(time_zone_id: &str) -> bool { + let Ok(c_str) = CString::new(time_zone_id) else { + // An interior nul byte can't be a valid time zone id. + return false; + }; + unsafe { icu_set_default_time_zone(c_str.as_ptr()) } +} diff --git a/crates/v8/src/inspector.rs b/crates/v8/src/inspector.rs new file mode 100644 index 0000000000..8d0efff147 --- /dev/null +++ b/crates/v8/src/inspector.rs @@ -0,0 +1,1463 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +//! Bindings to the V8 Inspector API. Documentation for the V8 inspector API is +//! very sparse, so here are a few references for the next sorry soul who has to +//! dig into it. +//! +//! https://medium.com/@hyperandroid/v8-inspector-from-an-embedder-standpoint-7f9c0472e2b7 +//! https://v8.dev/docs/inspector +//! https://chromedevtools.github.io/debugger-protocol-viewer/tot/ +//! https://cs.chromium.org/chromium/src/v8/include/v8-inspector.h +//! https://github.com/nodejs/node/blob/v13.7.0/src/inspector_agent.cc +//! https://github.com/nodejs/node/blob/v13.7.0/src/inspector_agent.h +//! https://github.com/nodejs/node/tree/v13.7.0/src/inspector +//! https://github.com/denoland/deno/blob/v0.38.0/cli/inspector.rs + +use crate::CallbackScope; +use crate::Context; +use crate::Isolate; +use crate::IsolateHandle; +use crate::Local; +use crate::PinScope; +use crate::StackTrace; +use crate::Value; +use crate::crdtp::CppVecU8; +use crate::isolate::RealIsolate; +use crate::support::CxxVTable; +use crate::support::Opaque; +use crate::support::UniquePtr; +use crate::support::UniqueRef; +use crate::support::int; +use std::cell::UnsafeCell; +use std::ffi::c_void; +use std::fmt::{self, Debug, Formatter}; +use std::pin::pin; + +unsafe extern "C" { + fn v8_inspector__V8Inspector__Channel__BASE__CONSTRUCT( + buf: *mut MaybeUninit, + ); + + fn v8_inspector__V8Inspector__Channel__sendResponse( + this: *mut RawChannel, + call_id: int, + message: UniquePtr, + ); + fn v8_inspector__V8Inspector__Channel__sendNotification( + this: *mut RawChannel, + message: UniquePtr, + ); + fn v8_inspector__V8Inspector__Channel__flushProtocolNotifications( + this: *mut RawChannel, + ); + + fn v8_inspector__V8InspectorClient__BASE__CONSTRUCT( + buf: *mut MaybeUninit, + ); + + fn v8_inspector__V8InspectorClient__generateUniqueId( + this: *mut RawV8InspectorClient, + ) -> i64; + fn v8_inspector__V8InspectorClient__runMessageLoopOnPause( + this: *mut RawV8InspectorClient, + context_group_id: int, + ); + fn v8_inspector__V8InspectorClient__quitMessageLoopOnPause( + this: *mut RawV8InspectorClient, + ); + fn v8_inspector__V8InspectorClient__runIfWaitingForDebugger( + this: *mut RawV8InspectorClient, + context_group_id: int, + ); + fn v8_inspector__V8InspectorClient__consoleAPIMessage( + this: *mut RawV8InspectorClient, + context_group_id: int, + level: int, + message: &StringView, + url: &StringView, + line_number: u32, + column_number: u32, + stack_trace: &mut V8StackTrace, + ); + + fn v8_inspector__V8InspectorSession__DELETE(this: *mut RawV8InspectorSession); + fn v8_inspector__V8InspectorSession__dispatchProtocolMessage( + session: *mut RawV8InspectorSession, + message: StringView, + ); + fn v8_inspector__V8InspectorSession__releaseObjectGroup( + session: *mut RawV8InspectorSession, + object_group: StringView, + ); + fn v8_inspector__V8InspectorSession__wrapObject( + session: *mut RawV8InspectorSession, + context: *const Context, + value: *const Value, + object_group: StringView, + generate_preview: bool, + ) -> *mut RawRemoteObject; + fn v8_inspector__V8InspectorSession__unwrapObject( + session: *mut RawV8InspectorSession, + error: *mut *mut StringBuffer, + object_id: StringView, + value: *mut *const Value, + context: *mut *const Context, + object_group: *mut *mut StringBuffer, + ) -> bool; + fn v8_inspector__RemoteObject__DELETE(this: *mut RawRemoteObject); + fn v8_inspector__RemoteObject__toBytes( + this: *const RawRemoteObject, + ) -> *mut CppVecU8; + fn v8_inspector__V8InspectorSession__schedulePauseOnNextStatement( + session: *mut RawV8InspectorSession, + break_reason: StringView, + break_details: StringView, + ); + fn v8_inspector__V8InspectorSession__cancelPauseOnNextStatement( + session: *mut RawV8InspectorSession, + ); + fn v8_inspector__V8InspectorSession__canDispatchMethod( + method: StringView, + ) -> bool; + fn v8_inspector__V8InspectorSession__Inspectable__NEW( + rust_impl: *mut c_void, + ) -> *mut RawInspectable; + fn v8_inspector__V8InspectorSession__Inspectable__DELETE( + inspectable: *mut RawInspectable, + ); + fn v8_inspector__V8InspectorSession__addInspectedObject( + session: *mut RawV8InspectorSession, + inspectable: *mut RawInspectable, + ); + + fn v8_inspector__StringBuffer__DELETE(this: *mut StringBuffer); + fn v8_inspector__StringBuffer__string(this: &StringBuffer) -> StringView<'_>; + fn v8_inspector__StringBuffer__create( + source: StringView, + ) -> UniquePtr; + + fn v8_inspector__V8Inspector__DELETE(this: *mut RawV8Inspector); + fn v8_inspector__V8Inspector__create( + isolate: *mut RealIsolate, + client: *mut RawV8InspectorClient, + ) -> *mut RawV8Inspector; + fn v8_inspector__V8Inspector__connect( + inspector: *mut RawV8Inspector, + context_group_id: int, + channel: *mut RawChannel, + state: StringView, + client_trust_level: V8InspectorClientTrustLevel, + ) -> *mut RawV8InspectorSession; + fn v8_inspector__V8Inspector__contextCreated( + this: *mut RawV8Inspector, + context: *const Context, + contextGroupId: int, + humanReadableName: StringView, + auxData: StringView, + ); + fn v8_inspector__V8Inspector__contextDestroyed( + this: *mut RawV8Inspector, + context: *const Context, + ); + fn v8_inspector__V8Inspector__idleStarted(this: *mut RawV8Inspector); + fn v8_inspector__V8Inspector__idleFinished(this: *mut RawV8Inspector); + fn v8_inspector__V8Inspector__asyncTaskScheduled( + this: *mut RawV8Inspector, + task_name: StringView, + task: *const c_void, + recurring: bool, + ); + fn v8_inspector__V8Inspector__asyncTaskCanceled( + this: *mut RawV8Inspector, + task: *const c_void, + ); + fn v8_inspector__V8Inspector__asyncTaskStarted( + this: *mut RawV8Inspector, + task: *const c_void, + ); + fn v8_inspector__V8Inspector__asyncTaskFinished( + this: *mut RawV8Inspector, + task: *const c_void, + ); + fn v8_inspector__V8Inspector__allAsyncTasksCanceled( + this: *mut RawV8Inspector, + ); + fn v8_inspector__V8Inspector__exceptionThrown( + this: *mut RawV8Inspector, + context: *const Context, + message: StringView, + exception: *const Value, + detailed_message: StringView, + url: StringView, + line_number: u32, + column_number: u32, + stack_trace: *mut V8StackTrace, + script_id: int, + ) -> u32; + fn v8_inspector__V8Inspector__createStackTrace( + this: *mut RawV8Inspector, + stack_trace: *const StackTrace, + ) -> *mut V8StackTrace; + fn v8_inspector__V8StackTrace__DELETE(this: *mut V8StackTrace); +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8Inspector__Channel__BASE__sendResponse( + this: *mut RawChannel, + call_id: int, + message: UniquePtr, +) { + unsafe { + let channel = ChannelHeap::from_raw(this); + channel.imp.send_response(call_id, message); + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8Inspector__Channel__BASE__sendNotification( + this: *mut RawChannel, + message: UniquePtr, +) { + unsafe { + ChannelHeap::from_raw(this).imp.send_notification(message); + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8Inspector__Channel__BASE__flushProtocolNotifications( + this: *mut RawChannel, +) { + unsafe { + ChannelHeap::from_raw(this) + .imp + .flush_protocol_notifications(); + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8InspectorClient__BASE__generateUniqueId( + this: *mut RawV8InspectorClient, +) -> i64 { + unsafe { + V8InspectorClientHeap::from_raw(this) + .imp + .generate_unique_id() + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8InspectorClient__BASE__runMessageLoopOnPause( + this: *mut RawV8InspectorClient, + context_group_id: int, +) { + unsafe { + V8InspectorClientHeap::from_raw(this) + .imp + .run_message_loop_on_pause(context_group_id); + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8InspectorClient__BASE__quitMessageLoopOnPause( + this: *mut RawV8InspectorClient, +) { + unsafe { + V8InspectorClientHeap::from_raw(this) + .imp + .quit_message_loop_on_pause(); + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8InspectorClient__BASE__runIfWaitingForDebugger( + this: *mut RawV8InspectorClient, + context_group_id: int, +) { + unsafe { + V8InspectorClientHeap::from_raw(this) + .imp + .run_if_waiting_for_debugger(context_group_id); + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8InspectorClient__BASE__consoleAPIMessage( + this: *mut RawV8InspectorClient, + context_group_id: int, + level: int, + message: &StringView, + url: &StringView, + line_number: u32, + column_number: u32, + stack_trace: &mut V8StackTrace, +) { + unsafe { + V8InspectorClientHeap::from_raw(this) + .imp + .console_api_message( + context_group_id, + level, + message, + url, + line_number, + column_number, + stack_trace, + ); + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8InspectorClient__BASE__valueSubtype( + this: *mut RawV8InspectorClient, + context: Local, + value: Local, +) -> *mut StringBuffer { + let scope = pin!(unsafe { CallbackScope::new(context) }); + let mut scope = scope.init(); + unsafe { + V8InspectorClientHeap::from_raw(this) + .imp + .value_subtype(&mut scope, value) + .and_then(|mut v| v.take()) + .map(|r| r.into_raw()) + .unwrap_or(std::ptr::null_mut()) + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8InspectorClient__BASE__descriptionForValueSubtype( + this: *mut RawV8InspectorClient, + context: Local, + value: Local, +) -> *mut StringBuffer { + let scope = pin!(unsafe { CallbackScope::new(context) }); + let mut scope = scope.init(); + unsafe { + V8InspectorClientHeap::from_raw(this) + .imp + .description_for_value_subtype(&mut scope, value) + .and_then(|mut v| v.take()) + .map(|r| r.into_raw()) + .unwrap_or(std::ptr::null_mut()) + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8InspectorClient__BASE__ensureDefaultContextInGroup( + this: *mut RawV8InspectorClient, + context_group_id: int, +) -> *const Context { + unsafe { + match V8InspectorClientHeap::from_raw(this) + .imp + .ensure_default_context_in_group(context_group_id) + { + Some(h) => &*h, + None => std::ptr::null_mut(), + } + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8InspectorClient__BASE__resourceNameToUrl( + this: *mut RawV8InspectorClient, + resource_name: &StringView, +) -> *mut StringBuffer { + unsafe { + V8InspectorClientHeap::from_raw(this) + .imp + .resource_name_to_url(resource_name) + .and_then(|mut v| v.take()) + .map(|r| r.into_raw()) + .unwrap_or(std::ptr::null_mut()) + } +} + +#[repr(C)] +#[derive(Debug)] +struct RawChannel { + _cxx_vtable: CxxVTable, +} + +#[repr(C)] +pub struct Channel { + heap: Pin>, +} + +impl std::fmt::Debug for Channel { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Channel").finish() + } +} + +#[repr(C)] +struct ChannelHeap { + raw: UnsafeCell, + imp: Box, + _pinned: PhantomPinned, +} + +impl ChannelHeap { + unsafe fn from_raw<'b>(this: *const RawChannel) -> &'b ChannelHeap { + unsafe { &(*this.cast::()) } + } +} + +impl Channel { + pub fn new(imp: Box) -> Self { + let heap = Box::into_raw(Box::new(MaybeUninit::::uninit())) + .cast::(); + + unsafe { + let raw = &raw mut (*heap).raw; + v8_inspector__V8Inspector__Channel__BASE__CONSTRUCT(raw.cast()); + let imp_ptr = &raw mut (*heap).imp; + imp_ptr.write(imp); + } + + Self { + heap: unsafe { Box::into_pin(Box::from_raw(heap.cast::())) }, + } + } + + fn raw(&self) -> *mut RawChannel { + self.heap.raw.get() + } + + pub fn send_response(&self, call_id: i32, message: UniquePtr) { + unsafe { + v8_inspector__V8Inspector__Channel__sendResponse( + self.raw(), + call_id, + message, + ); + } + } + pub fn send_notification(&self, message: UniquePtr) { + unsafe { + v8_inspector__V8Inspector__Channel__sendNotification(self.raw(), message); + } + } + pub fn flush_protocol_notifications(&self) { + unsafe { + v8_inspector__V8Inspector__Channel__flushProtocolNotifications( + self.raw(), + ); + } + } +} + +pub trait ChannelImpl { + fn send_response(&self, call_id: i32, message: UniquePtr); + fn send_notification(&self, message: UniquePtr); + fn flush_protocol_notifications(&self); +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::support::UniquePtr; + use std::sync::atomic::AtomicUsize; + use std::sync::atomic::Ordering::SeqCst; + + static MESSAGE: &[u8] = b"Hello Pluto!"; + static CALL_COUNT: AtomicUsize = AtomicUsize::new(0); + + // Using repr(C) to preserve field ordering and test that everything works + // when the ChannelBase field is not the first element of the struct. + #[repr(C)] + #[derive(Debug)] + pub struct TestChannel { + field1: i32, + field2: u64, + } + + impl ChannelImpl for TestChannel { + fn send_response( + &self, + call_id: i32, + mut message: UniquePtr, + ) { + assert_eq!(call_id, 999); + assert_eq!(message.as_mut().unwrap().string().len(), MESSAGE.len()); + self.log_call(); + } + fn send_notification(&self, mut message: UniquePtr) { + assert_eq!(message.as_mut().unwrap().string().len(), MESSAGE.len()); + self.log_call(); + } + fn flush_protocol_notifications(&self) { + self.log_call(); + } + } + + impl TestChannel { + pub fn new() -> Self { + Self { + field1: -42, + field2: 420, + } + } + + fn log_call(&self) { + assert_eq!(self.field1, -42); + assert_eq!(self.field2, 420); + CALL_COUNT.fetch_add(1, SeqCst); + } + } + + #[test] + fn test_channel() { + let channel = TestChannel::new(); + let msg_view = StringView::from(MESSAGE); + channel.send_response(999, StringBuffer::create(msg_view)); + assert_eq!(CALL_COUNT.swap(0, SeqCst), 1); + channel.send_notification(StringBuffer::create(msg_view)); + assert_eq!(CALL_COUNT.swap(0, SeqCst), 1); + channel.flush_protocol_notifications(); + assert_eq!(CALL_COUNT.swap(0, SeqCst), 1); + } +} + +#[repr(C)] +#[derive(Debug)] +pub struct RawV8InspectorClient { + _cxx_vtable: CxxVTable, +} + +impl V8InspectorClient { + pub fn run_message_loop_on_pause(&self, context_group_id: i32) { + unsafe { + v8_inspector__V8InspectorClient__runMessageLoopOnPause( + self.raw(), + context_group_id, + ); + } + } + + pub fn quit_message_loop_on_pause(&self) { + unsafe { + v8_inspector__V8InspectorClient__quitMessageLoopOnPause(self.raw()) + } + } + + pub fn run_if_waiting_for_debugger(&self, context_group_id: i32) { + unsafe { + v8_inspector__V8InspectorClient__runIfWaitingForDebugger( + self.raw(), + context_group_id, + ); + } + } + + #[allow(clippy::too_many_arguments)] + pub fn console_api_message( + &self, + context_group_id: i32, + level: i32, + message: &StringView, + url: &StringView, + line_number: u32, + column_number: u32, + stack_trace: &mut V8StackTrace, + ) { + unsafe { + v8_inspector__V8InspectorClient__consoleAPIMessage( + self.raw(), + context_group_id, + level, + message, + url, + line_number, + column_number, + stack_trace, + ); + } + } + + pub fn generate_unique_id(&self) -> i64 { + unsafe { v8_inspector__V8InspectorClient__generateUniqueId(self.raw()) } + } +} + +#[allow(unused_variables)] +pub trait V8InspectorClientImpl { + fn run_message_loop_on_pause(&self, context_group_id: i32) {} + fn quit_message_loop_on_pause(&self) {} + fn run_if_waiting_for_debugger(&self, context_group_id: i32) {} + + fn generate_unique_id(&self) -> i64 { + 0 // 0 = let V8 pick a unique id itself + } + + #[allow(clippy::too_many_arguments)] + fn console_api_message( + &self, + context_group_id: i32, + level: i32, + message: &StringView, + url: &StringView, + line_number: u32, + column_number: u32, + stack_trace: &mut V8StackTrace, + ) { + } + + /// Returns a custom Chrome DevTools Protocol `Runtime.RemoteObject` subtype + /// for `value`. Use one of the protocol's defined subtype enum values. + /// Returning `Some` causes V8 to call + /// [`Self::description_for_value_subtype`]. + /// + /// The callback scope uses the isolate's current context as a best-effort + /// context; it is not necessarily the context in which `value` originated. + /// If the isolate has no current context, V8 skips this callback entirely. + /// This callback runs while the inspector is constructing a value mirror. + /// Operations such as property access can execute JavaScript through getters + /// or proxies, so wrap them in a [`crate::TryCatch`] to avoid leaving a + /// pending exception in the inspector's mirror-building path. + fn value_subtype<'s>( + &self, + scope: &mut PinScope<'s, '_>, + value: Local<'s, Value>, + ) -> Option> { + None + } + + /// Returns the description for a value whose custom subtype was returned by + /// [`Self::value_subtype`]. Returning `None` makes V8 fall back to the default + /// object mirror, which also discards the custom subtype unless it is + /// `"error"` or `"array"`. + /// + /// Like [`Self::value_subtype`], this callback runs while the inspector is + /// constructing a value mirror. Wrap operations that can execute JavaScript + /// in a [`crate::TryCatch`] so an exception does not remain pending. + fn description_for_value_subtype<'s>( + &self, + scope: &mut PinScope<'s, '_>, + value: Local<'s, Value>, + ) -> Option> { + None + } + + fn ensure_default_context_in_group( + &self, + context_group_id: i32, + ) -> Option> { + None + } + + fn resource_name_to_url( + &self, + resource_name: &StringView, + ) -> Option> { + None + } +} + +// V8 will hold onto a raw pointer to the RawV8InspectorClient, so we need to +// make sure it stays pinned. +#[repr(C)] +struct V8InspectorClientHeap { + raw: UnsafeCell, + // this doesn't need to be pinned, but it's convenient to keep it here + // so we can access it from a pointer to the RawV8InspectorClient + imp: Box, + _pinned: PhantomPinned, +} + +impl V8InspectorClientHeap { + unsafe fn from_raw<'b>( + this: *const RawV8InspectorClient, + ) -> &'b V8InspectorClientHeap { + unsafe { &(*this.cast::()) } + } +} + +pub struct V8InspectorClient { + heap: Pin>, +} + +impl V8InspectorClient { + pub fn new(imp: Box) -> V8InspectorClient { + let heap = unsafe { + let heap = + Box::into_raw(Box::new(MaybeUninit::::uninit())); + let raw = &raw mut (*heap.cast::()).raw; + v8_inspector__V8InspectorClient__BASE__CONSTRUCT( + raw.cast::>(), + ); + let imp_ptr = &raw mut (*heap.cast::()).imp; + imp_ptr.write(imp); + Box::into_pin(Box::from_raw(heap.cast::())) + }; + + Self { heap } + } + + fn raw(&self) -> *mut RawV8InspectorClient { + self.heap.raw.get() + } +} + +impl Debug for V8InspectorClient { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.debug_struct("V8InspectorClient").finish() + } +} + +#[repr(C)] +#[derive(Debug)] +struct RawInspectable(Opaque); + +impl Drop for RawInspectable { + fn drop(&mut self) { + unsafe { + v8_inspector__V8InspectorSession__Inspectable__DELETE(self); + } + } +} + +// A trait object is a fat pointer, so box it once more before passing it through +// the FFI as a thin `*mut c_void`. +struct InspectableData { + imp: Box, +} + +/// Supplies the value of an object added to the inspector's `$0` through `$4` +/// history. +pub trait InspectableImpl { + /// Called by the inspector from within a V8 callback when the console + /// dereferences one of `$0` through `$4`. + /// + /// There is no way to return an empty handle; if no value is available, + /// return `undefined`. + fn get<'s>( + &self, + scope: &mut PinScope<'s, '_>, + context: Local<'s, Context>, + ) -> Local<'s, Value>; +} + +/// An object that can be added to an inspector session's `$0` through `$4` +/// history. +pub struct Inspectable { + raw: UniqueRef, +} + +impl Inspectable { + pub fn new(imp: Box) -> Self { + let data = Box::into_raw(Box::new(InspectableData { imp })).cast(); + let raw = unsafe { + UniqueRef::from_raw(v8_inspector__V8InspectorSession__Inspectable__NEW( + data, + )) + }; + Self { raw } + } +} + +impl Debug for Inspectable { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.debug_struct("Inspectable").finish() + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8InspectorSession__Inspectable__BASE__get( + rust_impl: *mut c_void, + context: Local, +) -> *const Value { + let data = unsafe { &*rust_impl.cast::() }; + // SAFETY: `CallbackScope::new(context)` must not open its own HandleScope. + // `NewCallbackScope for Local` has `NEEDS_SCOPE == false`, and + // `make_new_callback_scope` constructs it with `needs_scope == false`. The + // handle returned here is allocated in the EscapableHandleScope opened by + // the C++ shim and escaped there. If Rust opens its own HandleScope, that + // scope is destroyed before C++ escapes the handle, causing a use-after-free. + let scope = pin!(unsafe { CallbackScope::new(context) }); + let mut scope = scope.init(); + data.imp.get(&mut scope, context).as_non_null().as_ptr() +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8_inspector__V8InspectorSession__Inspectable__BASE__DROP( + rust_impl: *mut c_void, +) { + unsafe { + drop(Box::from_raw(rust_impl.cast::())); + } +} + +#[repr(C)] +#[derive(Debug)] +pub struct RawV8InspectorSession(Opaque); + +pub struct V8InspectorSession { + raw: UniqueRef, + // this isn't actually used, but it needs to live + // as long as the session + _channel: Channel, + isolate: IsolateHandle, +} + +impl V8InspectorSession { + pub fn can_dispatch_method(method: StringView) -> bool { + unsafe { v8_inspector__V8InspectorSession__canDispatchMethod(method) } + } + + pub fn dispatch_protocol_message(&self, message: StringView) { + unsafe { + v8_inspector__V8InspectorSession__dispatchProtocolMessage( + self.raw.as_ptr(), + message, + ); + } + } + + pub fn release_object_group(&self, object_group: StringView) { + unsafe { + v8_inspector__V8InspectorSession__releaseObjectGroup( + self.raw.as_ptr(), + object_group, + ); + } + } + + /// Wraps a V8 value in an Inspector `Runtime.RemoteObject`. + /// + /// With `generate_preview == false`, V8 uses `kIdOnly`: object results still + /// include metadata such as `className` and `description`, but no property + /// preview. With `true`, V8 uses `kPreview`, which additionally includes a + /// property preview. + /// + /// `context` is used only to obtain an execution-context ID and look up the + /// corresponding `InspectedContext` in this session's context group. The + /// selected `InjectedScript` wraps `value` in that inspected context's stored + /// V8 context. Returns `None` if no matching inspected context is registered, + /// for example before `V8Inspector::context_created`. + /// + /// `_scope` is intentionally unused by Rust; borrowing it keeps the caller's + /// V8 `HandleScope` alive while `ValueMirror::create` allocates local handles. + pub fn wrap_object<'s>( + &self, + _scope: &mut PinScope<'s, '_>, + context: Local<'s, Context>, + value: Local<'s, Value>, + object_group: StringView, + generate_preview: bool, + ) -> Option { + crate::handle::assert_locals_in_isolate_of(&self.isolate); + unsafe { + UniqueRef::try_from_raw(v8_inspector__V8InspectorSession__wrapObject( + self.raw.as_ptr(), + &*context, + &*value, + object_group, + generate_preview, + )) + .map(|raw| RemoteObject { raw }) + } + } + + /// Resolves an Inspector-generated object ID to its V8 value and context. + /// + /// On success, the returned object group contains the group name supplied + /// when the object was wrapped. An empty group name is returned as a + /// non-null buffer containing an empty string. On failure, the error buffer + /// is non-null and contains the Inspector's error message. This includes an + /// invalid ID or an ID made stale by releasing its object group. + /// + /// `_scope` is intentionally unused by Rust; borrowing it keeps the caller's + /// V8 `HandleScope` alive and ties the returned local handles to that scope. + #[allow(clippy::type_complexity)] + pub fn unwrap_object<'s>( + &self, + _scope: &mut PinScope<'s, '_>, + object_id: StringView, + ) -> Result< + ( + Local<'s, Value>, + Local<'s, Context>, + UniquePtr, + ), + UniquePtr, + > { + crate::handle::assert_locals_in_isolate_of(&self.isolate); + let mut error = std::ptr::null_mut(); + let mut value = std::ptr::null(); + let mut context = std::ptr::null(); + let mut object_group = std::ptr::null_mut(); + let success = unsafe { + v8_inspector__V8InspectorSession__unwrapObject( + self.raw.as_ptr(), + &mut error, + object_id, + &mut value, + &mut context, + &mut object_group, + ) + }; + + if !success { + return Err(unsafe { UniquePtr::from_raw(error) }); + } + + Ok(( + unsafe { Local::from_raw(value).unwrap() }, + unsafe { Local::from_raw(context).unwrap() }, + unsafe { UniquePtr::from_raw(object_group) }, + )) + } + + pub fn schedule_pause_on_next_statement( + &self, + reason: StringView, + detail: StringView, + ) { + unsafe { + v8_inspector__V8InspectorSession__schedulePauseOnNextStatement( + self.raw.as_ptr(), + reason, + detail, + ); + } + } + + /// Cancel a pause previously scheduled by + /// [`Self::schedule_pause_on_next_statement`] if it hasn't fired yet. + pub fn cancel_pause_on_next_statement(&self) { + unsafe { + v8_inspector__V8InspectorSession__cancelPauseOnNextStatement( + self.raw.as_ptr(), + ); + } + } + + pub fn add_inspected_object(&self, inspectable: Inspectable) { + unsafe { + v8_inspector__V8InspectorSession__addInspectedObject( + self.raw.as_ptr(), + inspectable.raw.into_raw(), + ); + } + } +} + +impl Drop for V8InspectorSession { + fn drop(&mut self) { + unsafe { v8_inspector__V8InspectorSession__DELETE(self.raw.as_ptr()) }; + } +} + +/// An opaque, owned Inspector `Runtime.RemoteObject`. +pub struct RemoteObject { + raw: UniqueRef, +} + +#[repr(C)] +#[derive(Debug)] +struct RawRemoteObject(Opaque); + +impl RemoteObject { + /// Serializes this remote object to its CRDTP/CBOR representation. + pub fn to_bytes(&self) -> Vec { + unsafe { + CppVecU8::take_from_raw(v8_inspector__RemoteObject__toBytes( + self.raw.as_ptr(), + )) + } + } +} + +impl Debug for RemoteObject { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.debug_struct("RemoteObject").finish() + } +} + +impl Drop for RawRemoteObject { + fn drop(&mut self) { + unsafe { v8_inspector__RemoteObject__DELETE(self) } + } +} + +// TODO: in C++, this class is intended to be user-extensible, just like +// like `Task`, `Client`, `Channel`. In Rust this would ideally also be the +// case, but currently to obtain a `UniquePtr` is by making a +// copy using `StringBuffer::create()`. +#[repr(C)] +#[derive(Debug)] +pub struct StringBuffer { + _cxx_vtable: CxxVTable, +} + +// TODO: make it possible to obtain a `UniquePtr` directly from +// an owned `Vec` or `Vec`, +impl StringBuffer { + // The C++ class definition does not declare `string()` to be a const method, + // therefore we declare self as mutable here. + // TODO: figure out whether it'd be safe to assume a const receiver here. + // That would make it possible to implement `Deref`. + pub fn string(&self) -> StringView<'_> { + unsafe { v8_inspector__StringBuffer__string(self) } + } + + /// This method copies contents. + pub fn create(source: StringView) -> UniquePtr { + unsafe { v8_inspector__StringBuffer__create(source) } + } +} + +impl Drop for StringBuffer { + fn drop(&mut self) { + unsafe { v8_inspector__StringBuffer__DELETE(self) } + } +} + +unsafe impl Send for StringBuffer {} +use std::iter::ExactSizeIterator; +use std::iter::IntoIterator; +use std::marker::PhantomData; +use std::marker::PhantomPinned; +use std::mem::MaybeUninit; +use std::ops::Deref; +use std::pin::Pin; +use std::ptr::NonNull; +use std::ptr::null; +use std::slice; +use std::string; + +// Notes: +// * This class is ported, not wrapped using bindings. +// * Since Rust `repr(bool)` is not allowed, we're assuming that `bool` and +// `u8` have the same size. This is assumption is checked in 'support.h'. +// TODO: find/open upstream issue to allow #[repr(bool)] support. + +#[derive(Clone, Debug, Copy)] +#[repr(u8)] +pub enum StringView<'a> { + // Do not reorder! + U16(CharacterArray<'a, u16>), + U8(CharacterArray<'a, u8>), +} + +impl StringView<'static> { + pub fn empty() -> Self { + Self::U8(CharacterArray::<'static, u8>::empty()) + } +} + +impl fmt::Display for StringView<'_> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Self::U16(v) => write!(f, "{v}"), + Self::U8(v) => write!(f, "{v}"), + } + } +} + +impl<'a> From<&'a [u8]> for StringView<'a> { + fn from(v: &'a [u8]) -> Self { + Self::U8(CharacterArray::<'a, u8>::from(v)) + } +} + +impl<'a> From<&'a [u16]> for StringView<'a> { + fn from(v: &'a [u16]) -> Self { + Self::U16(CharacterArray::<'a, u16>::from(v)) + } +} + +impl StringView<'_> { + pub fn is_8bit(&self) -> bool { + match self { + Self::U16(..) => false, + Self::U8(..) => true, + } + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn len(&self) -> usize { + match self { + Self::U16(v) => v.len(), + Self::U8(v) => v.len(), + } + } + + pub fn characters8(&self) -> Option<&[u8]> { + match self { + Self::U16(..) => None, + Self::U8(v) => Some(v), + } + } + + pub fn characters16(&self) -> Option<&[u16]> { + match self { + Self::U16(v) => Some(v), + Self::U8(..) => None, + } + } +} + +impl<'a> IntoIterator for StringView<'a> { + type IntoIter = StringViewIterator<'a>; + type Item = u16; + + fn into_iter(self) -> Self::IntoIter { + StringViewIterator { view: self, pos: 0 } + } +} + +#[repr(C)] +#[derive(Copy, Clone, Debug)] +pub struct CharacterArray<'a, T> { + m_length: usize, + m_characters: *const T, + _phantom: PhantomData<&'a T>, +} + +impl CharacterArray<'static, u8> { + pub fn empty() -> Self { + Self { + m_length: 0, + m_characters: null(), + _phantom: PhantomData, + } + } +} + +impl CharacterArray<'_, T> +where + T: Copy, +{ + #[inline(always)] + fn len(&self) -> usize { + self.m_length + } + + #[inline(always)] + fn get_at(&self, index: usize) -> Option { + if index < self.m_length { + Some(unsafe { *self.m_characters.add(index) }) + } else { + None + } + } +} + +unsafe impl Send for CharacterArray<'_, T> where T: Copy {} +unsafe impl Sync for CharacterArray<'_, T> where T: Sync {} + +impl fmt::Display for CharacterArray<'_, u8> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str( + self + .iter() + .cloned() + .map(char::from) + .collect::() + .as_str(), + ) + } +} + +impl fmt::Display for CharacterArray<'_, u16> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str(&string::String::from_utf16_lossy(self)) + } +} + +impl<'a, T> From<&'a [T]> for CharacterArray<'a, T> { + fn from(v: &'a [T]) -> Self { + Self { + m_length: v.len(), + m_characters: v.as_ptr(), + _phantom: PhantomData, + } + } +} + +impl Deref for CharacterArray<'_, T> { + type Target = [T]; + + fn deref(&self) -> &[T] { + let Self { + m_length, + mut m_characters, + .. + } = *self; + if m_characters.is_null() { + assert_eq!(m_length, 0); + m_characters = NonNull::dangling().as_ptr(); + }; + unsafe { slice::from_raw_parts(m_characters, m_length) } + } +} + +#[derive(Copy, Clone, Debug)] +pub struct StringViewIterator<'a> { + view: StringView<'a>, + pos: usize, +} + +impl Iterator for StringViewIterator<'_> { + type Item = u16; + + fn next(&mut self) -> Option { + let result = Some(match self.view { + StringView::U16(v) => v.get_at(self.pos)?, + StringView::U8(v) => u16::from(v.get_at(self.pos)?), + }); + self.pos += 1; + result + } +} + +impl ExactSizeIterator for StringViewIterator<'_> { + fn len(&self) -> usize { + self.view.len() + } +} + +#[test] +fn string_view_display() { + let ok: [u16; 2] = [111, 107]; + assert_eq!("ok", format!("{}", StringView::from(&ok[..]))); + assert_eq!("ok", format!("{}", StringView::from(&b"ok"[..]))); + assert_eq!("ØÞ", format!("{}", StringView::from(&[216u8, 222u8][..]))); +} + +#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)] +#[repr(C)] +pub enum V8InspectorClientTrustLevel { + Untrusted = 0, + FullyTrusted = 1, +} + +#[repr(C)] +#[derive(Debug)] +pub struct RawV8Inspector(Opaque); + +pub struct V8Inspector { + raw: UniqueRef, + _client: V8InspectorClient, + isolate: IsolateHandle, +} + +impl V8Inspector { + pub fn create( + isolate: &mut Isolate, + client: V8InspectorClient, + ) -> V8Inspector { + let raw = unsafe { + UniqueRef::from_raw(v8_inspector__V8Inspector__create( + isolate.as_real_ptr(), + client.raw(), + )) + }; + V8Inspector { + raw, + _client: client, + isolate: isolate.thread_safe_handle(), + } + } + + // note: in theory v8 could mutate through this pointer. + // this is fine, though, because we never create a rust reference + // to the actual RawV8Inspector, we only use raw pointers which + // don't enforce the immutability guarantee + fn raw(&self) -> *mut RawV8Inspector { + self.raw.as_ptr() + } + + pub fn connect( + &self, + context_group_id: i32, + channel: Channel, + state: StringView, + client_trust_level: V8InspectorClientTrustLevel, + ) -> V8InspectorSession { + let raw = unsafe { + UniqueRef::from_raw(v8_inspector__V8Inspector__connect( + self.raw(), + context_group_id, + channel.raw(), + state, + client_trust_level, + )) + }; + + V8InspectorSession { + raw, + _channel: channel, + isolate: self.isolate.clone(), + } + } + + /// Note: this method deviates from the C++ API here because it's a lot of + /// work to bind the V8ContextInfo, which is not used elsewhere. + pub fn context_created( + &self, + context: Local, + context_group_id: i32, + human_readable_name: StringView, + aux_data: StringView, + ) { + crate::handle::assert_locals_in_isolate_of(&self.isolate); + unsafe { + v8_inspector__V8Inspector__contextCreated( + self.raw(), + &*context, + context_group_id, + human_readable_name, + aux_data, + ); + } + } + + pub fn context_destroyed(&self, context: Local) { + crate::handle::assert_locals_in_isolate_of(&self.isolate); + unsafe { + v8_inspector__V8Inspector__contextDestroyed(self.raw(), &*context) + } + } + + /// Tell the inspector the runtime entered an idle period. Pairs with + /// [`Self::idle_finished`]. + pub fn idle_started(&self) { + unsafe { v8_inspector__V8Inspector__idleStarted(self.raw()) } + } + + /// Tell the inspector the runtime left an idle period. + pub fn idle_finished(&self) { + unsafe { v8_inspector__V8Inspector__idleFinished(self.raw()) } + } + + /// Notify the inspector that an async task has been scheduled — used to + /// build async stack traces. `task` is an opaque identity pointer that + /// must match the one later passed to + /// [`Self::async_task_started`]/[`Self::async_task_finished`]/ + /// [`Self::async_task_canceled`]. Set `recurring` to `true` for + /// repeating tasks such as `setInterval` where the same identity fires + /// multiple times. + /// + /// # Safety + /// `task` must be a stable pointer for the lifetime of the scheduled + /// async task; the inspector stores it as an opaque key. + pub unsafe fn async_task_scheduled( + &self, + task_name: StringView, + task: *const c_void, + recurring: bool, + ) { + unsafe { + v8_inspector__V8Inspector__asyncTaskScheduled( + self.raw(), + task_name, + task, + recurring, + ) + } + } + + /// Notify the inspector that a previously scheduled async task was + /// cancelled and will not run. `task` must match the pointer used in + /// the corresponding [`Self::async_task_scheduled`] call. + /// + /// # Safety + /// See [`Self::async_task_scheduled`]. + pub unsafe fn async_task_canceled(&self, task: *const c_void) { + unsafe { v8_inspector__V8Inspector__asyncTaskCanceled(self.raw(), task) } + } + + /// Notify the inspector that an async task has begun executing. Must be + /// paired with [`Self::async_task_finished`] before the JS callback + /// returns to V8. + /// + /// # Safety + /// See [`Self::async_task_scheduled`]. + pub unsafe fn async_task_started(&self, task: *const c_void) { + unsafe { v8_inspector__V8Inspector__asyncTaskStarted(self.raw(), task) } + } + + /// Notify the inspector that an async task has finished executing. + /// + /// # Safety + /// See [`Self::async_task_scheduled`]. + pub unsafe fn async_task_finished(&self, task: *const c_void) { + unsafe { v8_inspector__V8Inspector__asyncTaskFinished(self.raw(), task) } + } + + /// Notify the inspector that every outstanding async task is being + /// cancelled, e.g. during runtime shutdown. + pub fn all_async_tasks_canceled(&self) { + unsafe { v8_inspector__V8Inspector__allAsyncTasksCanceled(self.raw()) } + } + + #[allow(clippy::too_many_arguments)] + pub fn exception_thrown( + &self, + context: Local, + message: StringView, + exception: Local, + detailed_message: StringView, + url: StringView, + line_number: u32, + column_number: u32, + stack_trace: UniquePtr, + script_id: i32, + ) -> u32 { + crate::handle::assert_locals_in_isolate_of(&self.isolate); + unsafe { + v8_inspector__V8Inspector__exceptionThrown( + self.raw(), + &*context, + message, + &*exception, + detailed_message, + url, + line_number, + column_number, + stack_trace.into_raw(), + script_id, + ) + } + } + + pub fn create_stack_trace( + &self, + stack_trace: Option>, + ) -> UniquePtr { + crate::handle::assert_locals_in_isolate_of(&self.isolate); + unsafe { + UniquePtr::from_raw(v8_inspector__V8Inspector__createStackTrace( + self.raw(), + stack_trace.map_or(null(), |v| &*v), + )) + } + } +} + +impl Drop for V8Inspector { + fn drop(&mut self) { + unsafe { v8_inspector__V8Inspector__DELETE(self.raw()) }; + } +} + +#[repr(C)] +#[derive(Debug)] +pub struct V8StackTrace { + _cxx_vtable: CxxVTable, +} + +impl Drop for V8StackTrace { + fn drop(&mut self) { + unsafe { v8_inspector__V8StackTrace__DELETE(self) }; + } +} + +// TODO(bnoordhuis) This needs to be fleshed out more but that can wait +// until it's actually needed. diff --git a/crates/v8/src/isolate.rs b/crates/v8/src/isolate.rs new file mode 100644 index 0000000000..53273d5a62 --- /dev/null +++ b/crates/v8/src/isolate.rs @@ -0,0 +1,3362 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. +use crate::Array; +use crate::CallbackScope; +use crate::Context; +use crate::Data; +use crate::FixedArray; +use crate::Function; +use crate::FunctionCodeHandling; +use crate::IsolateGroup; +use crate::Local; +use crate::Message; +use crate::Module; +use crate::Object; +use crate::PinScope; +use crate::Platform; +use crate::Promise; +use crate::PromiseResolver; +use crate::StartupData; +use crate::String; +use crate::V8::get_current_platform; +use crate::Value; +use crate::binding::v8__HeapCodeStatistics; +use crate::binding::v8__HeapSpaceStatistics; +use crate::binding::v8__HeapStatistics; +use crate::binding::v8__Isolate__UseCounterFeature; +pub use crate::binding::v8__ModuleImportPhase as ModuleImportPhase; +use crate::cppgc::Heap; +use crate::external_references::ExternalReference; +use crate::function::FunctionCallbackInfo; +use crate::gc::GCCallbackFlags; +use crate::gc::GCType; +use crate::handle::FinalizerCallback; +use crate::handle::FinalizerMap; +use crate::isolate_create_params::CreateParams; +use crate::isolate_create_params::raw; +use crate::promise::PromiseRejectMessage; +use crate::snapshot::SnapshotCreator; +use crate::support::MapFnFrom; +use crate::support::MapFnTo; +use crate::support::Opaque; +use crate::support::ToCFn; +use crate::support::UnitType; +use crate::support::char; +use crate::support::int; +use crate::support::size_t; +use crate::wasm::WasmStreaming; +use crate::wasm::trampoline; +use std::cell::UnsafeCell; +use std::ffi::CStr; + +use std::any::Any; +use std::any::TypeId; +use std::borrow::Cow; +use std::collections::HashMap; +use std::ffi::c_void; +use std::fmt::{self, Debug, Formatter}; +use std::hash::BuildHasher; +use std::hash::Hasher; +use std::mem::MaybeUninit; +use std::mem::align_of; +use std::mem::forget; +use std::mem::needs_drop; +use std::mem::size_of; +use std::ops::Deref; +use std::ops::DerefMut; +use std::pin::pin; +use std::ptr; +use std::ptr::NonNull; +use std::ptr::addr_of_mut; +use std::ptr::drop_in_place; +use std::ptr::null_mut; +use std::sync::Arc; +use std::sync::Mutex; +use std::sync::atomic::AtomicPtr; +use std::sync::atomic::AtomicUsize; + +/// Policy for running microtasks: +/// - explicit: microtasks are invoked with the +/// Isolate::PerformMicrotaskCheckpoint() method; +/// - auto: microtasks are invoked when the script call depth decrements +/// to zero. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(C)] +pub enum MicrotasksPolicy { + Explicit = 0, + // Scoped = 1 (RAII) is omitted for now, doesn't quite map to idiomatic Rust. + Auto = 2, +} + +/// Signal for dependants of contexts. Useful for +/// [`Isolate::context_disposed_notification`] to implement different +/// strategies. +#[repr(C)] +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ContextDependants { + /// Context has no dependants. These are usually top-level contexts. + NoDependants, + /// Context has some dependants, i.e., it may depend on other contexts. This + /// is usually the case for inner contexts. + SomeDependants, +} + +/// Memory pressure level for the MemoryPressureNotification. +/// None hints V8 that there is no memory pressure. +/// Moderate hints V8 to speed up incremental garbage collection at the cost +/// of higher latency due to garbage collection pauses. +/// Critical hints V8 to free memory as soon as possible. Garbage collection +/// pauses at this level will be large. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(C)] +pub enum MemoryPressureLevel { + None = 0, + Moderate = 1, + Critical = 2, +} + +/// Time zone redetection indicator for +/// DateTimeConfigurationChangeNotification. +/// +/// kSkip indicates V8 that the notification should not trigger redetecting +/// host time zone. kRedetect indicates V8 that host time zone should be +/// redetected, and used to set the default time zone. +/// +/// The host time zone detection may require file system access or similar +/// operations unlikely to be available inside a sandbox. If v8 is run inside a +/// sandbox, the host time zone has to be detected outside the sandbox before +/// calling DateTimeConfigurationChangeNotification function. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(C)] +pub enum TimeZoneDetection { + Skip = 0, + Redetect = 1, +} + +/// PromiseHook with type Init is called when a new promise is +/// created. When a new promise is created as part of the chain in the +/// case of Promise.then or in the intermediate promises created by +/// Promise.{race, all}/AsyncFunctionAwait, we pass the parent promise +/// otherwise we pass undefined. +/// +/// PromiseHook with type Resolve is called at the beginning of +/// resolve or reject function defined by CreateResolvingFunctions. +/// +/// PromiseHook with type Before is called at the beginning of the +/// PromiseReactionJob. +/// +/// PromiseHook with type After is called right at the end of the +/// PromiseReactionJob. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(C)] +pub enum PromiseHookType { + Init, + Resolve, + Before, + After, +} + +/// Types of garbage collections that can be requested via +/// [`Isolate::request_garbage_collection_for_testing`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(C)] +pub enum GarbageCollectionType { + Full, + Minor, +} + +pub type MessageCallback = unsafe extern "C" fn(Local, Local); + +bitflags! { + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + #[repr(transparent)] + pub struct MessageErrorLevel: int { + const LOG = 1 << 0; + const DEBUG = 1 << 1; + const INFO = 1 << 2; + const ERROR = 1 << 3; + const WARNING = 1 << 4; + const ALL = (1 << 5) - 1; + } +} + +pub type PromiseHook = + unsafe extern "C" fn(PromiseHookType, Local, Local); + +pub type PromiseRejectCallback = unsafe extern "C" fn(PromiseRejectMessage); + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(C)] +pub enum WasmAsyncSuccess { + Success, + Fail, +} +pub type WasmAsyncResolvePromiseCallback = unsafe extern "C" fn( + UnsafeRawIsolatePtr, + Local, + Local, + Local, + WasmAsyncSuccess, +); + +pub type AllowWasmCodeGenerationCallback = + unsafe extern "C" fn(Local, Local) -> bool; + +/// HostInitializeImportMetaObjectCallback is called the first time import.meta +/// is accessed for a module. Subsequent access will reuse the same value. +/// +/// The method combines two implementation-defined abstract operations into one: +/// HostGetImportMetaProperties and HostFinalizeImportMeta. +/// +/// The embedder should use v8::Object::CreateDataProperty to add properties on +/// the meta object. +pub type HostInitializeImportMetaObjectCallback = + unsafe extern "C" fn(Local, Local, Local); + +/// HostImportModuleDynamicallyCallback is called when we require the embedder +/// to load a module. This is used as part of the dynamic import syntax. +/// +/// The host_defined_options are metadata provided by the host environment, which may be used +/// to customize or further specify how the module should be imported. +/// +/// The resource_name is the identifier or path for the module or script making the import request. +/// +/// The specifier is the name of the module that should be imported. +/// +/// The import_attributes are import assertions for this request in the form: +/// [key1, value1, key2, value2, ...] where the keys and values are of type +/// v8::String. Note, unlike the FixedArray passed to ResolveModuleCallback and +/// returned from ModuleRequest::GetImportAssertions(), this array does not +/// contain the source Locations of the assertions. +/// +/// The embedder must compile, instantiate, evaluate the Module, and obtain its +/// namespace object. +/// +/// The Promise returned from this function is forwarded to userland JavaScript. +/// The embedder must resolve this promise with the module namespace object. In +/// case of an exception, the embedder must reject this promise with the +/// exception. If the promise creation itself fails (e.g. due to stack +/// overflow), the embedder must propagate that exception by returning an empty +/// MaybeLocal. +/// +/// # Example +/// +/// ``` +/// fn host_import_module_dynamically_callback_example<'s>( +/// scope: &mut v8::HandleScope<'s>, +/// host_defined_options: v8::Local<'s, v8::Data>, +/// resource_name: v8::Local<'s, v8::Value>, +/// specifier: v8::Local<'s, v8::String>, +/// import_attributes: v8::Local<'s, v8::FixedArray>, +/// ) -> Option> { +/// todo!() +/// } +/// ``` +pub trait HostImportModuleDynamicallyCallback: + UnitType + + for<'s, 'i> FnOnce( + &mut PinScope<'s, 'i>, + Local<'s, Data>, + Local<'s, Value>, + Local<'s, String>, + Local<'s, FixedArray>, + ) -> Option> +{ + fn to_c_fn(self) -> RawHostImportModuleDynamicallyCallback; +} + +#[cfg(target_family = "unix")] +pub(crate) type RawHostImportModuleDynamicallyCallback = + for<'s> unsafe extern "C" fn( + Local<'s, Context>, + Local<'s, Data>, + Local<'s, Value>, + Local<'s, String>, + Local<'s, FixedArray>, + ) -> *mut Promise; + +#[cfg(all( + target_family = "windows", + any(target_arch = "x86_64", target_arch = "aarch64") +))] +pub type RawHostImportModuleDynamicallyCallback = + for<'s> unsafe extern "C" fn( + *mut *mut Promise, + Local<'s, Context>, + Local<'s, Data>, + Local<'s, Value>, + Local<'s, String>, + Local<'s, FixedArray>, + ) -> *mut *mut Promise; + +impl HostImportModuleDynamicallyCallback for F +where + F: UnitType + + for<'s, 'i> FnOnce( + &mut PinScope<'s, 'i>, + Local<'s, Data>, + Local<'s, Value>, + Local<'s, String>, + Local<'s, FixedArray>, + ) -> Option>, +{ + #[inline(always)] + fn to_c_fn(self) -> RawHostImportModuleDynamicallyCallback { + #[allow(unused_variables)] + #[inline(always)] + fn scope_adapter<'s, 'i: 's, F: HostImportModuleDynamicallyCallback>( + context: Local<'s, Context>, + host_defined_options: Local<'s, Data>, + resource_name: Local<'s, Value>, + specifier: Local<'s, String>, + import_attributes: Local<'s, FixedArray>, + ) -> Option> { + let scope = pin!(unsafe { CallbackScope::new(context) }); + let mut scope = scope.init(); + (F::get())( + &mut scope, + host_defined_options, + resource_name, + specifier, + import_attributes, + ) + } + + #[cfg(target_family = "unix")] + #[inline(always)] + unsafe extern "C" fn abi_adapter< + 's, + F: HostImportModuleDynamicallyCallback, + >( + context: Local<'s, Context>, + host_defined_options: Local<'s, Data>, + resource_name: Local<'s, Value>, + specifier: Local<'s, String>, + import_attributes: Local<'s, FixedArray>, + ) -> *mut Promise { + scope_adapter::( + context, + host_defined_options, + resource_name, + specifier, + import_attributes, + ) + .map_or_else(null_mut, |return_value| return_value.as_non_null().as_ptr()) + } + + #[cfg(all( + target_family = "windows", + any(target_arch = "x86_64", target_arch = "aarch64") + ))] + #[inline(always)] + unsafe extern "C" fn abi_adapter< + 's, + F: HostImportModuleDynamicallyCallback, + >( + return_value: *mut *mut Promise, + context: Local<'s, Context>, + host_defined_options: Local<'s, Data>, + resource_name: Local<'s, Value>, + specifier: Local<'s, String>, + import_attributes: Local<'s, FixedArray>, + ) -> *mut *mut Promise { + unsafe { + std::ptr::write( + return_value, + scope_adapter::( + context, + host_defined_options, + resource_name, + specifier, + import_attributes, + ) + .map(|return_value| return_value.as_non_null().as_ptr()) + .unwrap_or_else(null_mut), + ); + return_value + } + } + + abi_adapter:: + } +} + +/// HostImportModuleWithPhaseDynamicallyCallback is called when we +/// require the embedder to load a module with a specific phase. This is used +/// as part of the dynamic import syntax. +/// +/// The referrer contains metadata about the script/module that calls +/// import. +/// +/// The specifier is the name of the module that should be imported. +/// +/// The phase is the phase of the import requested. +/// +/// The import_attributes are import attributes for this request in the form: +/// [key1, value1, key2, value2, ...] where the keys and values are of type +/// v8::String. Note, unlike the FixedArray passed to ResolveModuleCallback and +/// returned from ModuleRequest::GetImportAttributes(), this array does not +/// contain the source Locations of the attributes. +/// +/// The Promise returned from this function is forwarded to userland +/// JavaScript. The embedder must resolve this promise according to the phase +/// requested: +/// - For ModuleImportPhase::kSource, the promise must be resolved with a +/// compiled ModuleSource object, or rejected with a SyntaxError if the +/// module does not support source representation. +/// - For ModuleImportPhase::kEvaluation, the promise must be resolved with a +/// ModuleNamespace object of a module that has been compiled, instantiated, +/// and evaluated. +/// +/// In case of an exception, the embedder must reject this promise with the +/// exception. If the promise creation itself fails (e.g. due to stack +/// overflow), the embedder must propagate that exception by returning an empty +/// MaybeLocal. +/// +/// This callback is still experimental and is only invoked for source phase +/// imports. +pub trait HostImportModuleWithPhaseDynamicallyCallback: + UnitType + + for<'s, 'i> FnOnce( + &mut PinScope<'s, 'i>, + Local<'s, Data>, + Local<'s, Value>, + Local<'s, String>, + ModuleImportPhase, + Local<'s, FixedArray>, + ) -> Option> +{ + fn to_c_fn(self) -> RawHostImportModuleWithPhaseDynamicallyCallback; +} + +#[cfg(target_family = "unix")] +pub(crate) type RawHostImportModuleWithPhaseDynamicallyCallback = + for<'s> unsafe extern "C" fn( + Local<'s, Context>, + Local<'s, Data>, + Local<'s, Value>, + Local<'s, String>, + ModuleImportPhase, + Local<'s, FixedArray>, + ) -> *mut Promise; + +#[cfg(all( + target_family = "windows", + any(target_arch = "x86_64", target_arch = "aarch64") +))] +pub type RawHostImportModuleWithPhaseDynamicallyCallback = + for<'s> unsafe extern "C" fn( + *mut *mut Promise, + Local<'s, Context>, + Local<'s, Data>, + Local<'s, Value>, + Local<'s, String>, + ModuleImportPhase, + Local<'s, FixedArray>, + ) -> *mut *mut Promise; + +impl HostImportModuleWithPhaseDynamicallyCallback for F +where + F: UnitType + + for<'s, 'i> FnOnce( + &mut PinScope<'s, 'i>, + Local<'s, Data>, + Local<'s, Value>, + Local<'s, String>, + ModuleImportPhase, + Local<'s, FixedArray>, + ) -> Option>, +{ + #[inline(always)] + fn to_c_fn(self) -> RawHostImportModuleWithPhaseDynamicallyCallback { + #[allow(unused_variables)] + #[inline(always)] + fn scope_adapter<'s, F: HostImportModuleWithPhaseDynamicallyCallback>( + context: Local<'s, Context>, + host_defined_options: Local<'s, Data>, + resource_name: Local<'s, Value>, + specifier: Local<'s, String>, + import_phase: ModuleImportPhase, + import_attributes: Local<'s, FixedArray>, + ) -> Option> { + let scope = pin!(unsafe { CallbackScope::new(context) }); + let mut scope = scope.init(); + (F::get())( + &mut scope, + host_defined_options, + resource_name, + specifier, + import_phase, + import_attributes, + ) + } + + #[cfg(target_family = "unix")] + #[inline(always)] + unsafe extern "C" fn abi_adapter< + 's, + F: HostImportModuleWithPhaseDynamicallyCallback, + >( + context: Local<'s, Context>, + host_defined_options: Local<'s, Data>, + resource_name: Local<'s, Value>, + specifier: Local<'s, String>, + import_phase: ModuleImportPhase, + import_attributes: Local<'s, FixedArray>, + ) -> *mut Promise { + scope_adapter::( + context, + host_defined_options, + resource_name, + specifier, + import_phase, + import_attributes, + ) + .map_or_else(null_mut, |return_value| return_value.as_non_null().as_ptr()) + } + + #[cfg(all( + target_family = "windows", + any(target_arch = "x86_64", target_arch = "aarch64") + ))] + #[inline(always)] + unsafe extern "C" fn abi_adapter< + 's, + F: HostImportModuleWithPhaseDynamicallyCallback, + >( + return_value: *mut *mut Promise, + context: Local<'s, Context>, + host_defined_options: Local<'s, Data>, + resource_name: Local<'s, Value>, + specifier: Local<'s, String>, + import_phase: ModuleImportPhase, + import_attributes: Local<'s, FixedArray>, + ) -> *mut *mut Promise { + unsafe { + std::ptr::write( + return_value, + scope_adapter::( + context, + host_defined_options, + resource_name, + specifier, + import_phase, + import_attributes, + ) + .map(|return_value| return_value.as_non_null().as_ptr()) + .unwrap_or_else(null_mut), + ); + return_value + } + } + + abi_adapter:: + } +} + +/// `HostCreateShadowRealmContextCallback` is called each time a `ShadowRealm` +/// is being constructed. You can use [`HandleScope::get_current_context`] to +/// get the [`Context`] in which the constructor is being run. +/// +/// The method combines [`Context`] creation and the implementation-defined +/// abstract operation `HostInitializeShadowRealm` into one. +/// +/// The embedder should use [`Context::new`] to create a new context. If the +/// creation fails, the embedder must propagate that exception by returning +/// [`None`]. +pub type HostCreateShadowRealmContextCallback = + for<'s, 'i> fn(scope: &mut PinScope<'s, 'i>) -> Option>; + +pub type GcCallbackWithData = unsafe extern "C" fn( + isolate: UnsafeRawIsolatePtr, + r#type: GCType, + flags: GCCallbackFlags, + data: *mut c_void, +); + +pub type InterruptCallback = + unsafe extern "C" fn(isolate: UnsafeRawIsolatePtr, data: *mut c_void); + +pub type NearHeapLimitCallback = unsafe extern "C" fn( + data: *mut c_void, + current_heap_limit: usize, + initial_heap_limit: usize, +) -> usize; + +#[repr(C)] +pub struct OomDetails { + pub is_heap_oom: bool, + pub detail: *const char, +} + +pub type OomErrorCallback = + unsafe extern "C" fn(location: *const char, details: &OomDetails); + +/// The outcome of a [`ModifyCodeGenerationFromStringsCallback`]. +/// +/// Note that this is not the type V8 sees; it is translated into an internal +/// `#[repr(C)]` equivalent before crossing the FFI boundary, so its layout +/// carries no ABI guarantees. +pub struct ModifyCodeGenerationFromStringsResult<'s> { + /// If true, proceed with the code generation. Otherwise, block it. + pub codegen_allowed: bool, + /// Compile this source instead of the original one, if present. This field + /// is only considered when `codegen_allowed` is true. + pub modified_source: Option>, +} + +/// Called when JavaScript dynamically generates code, i.e. through `eval` or +/// the `Function` constructor. The callback decides whether the code generation +/// is allowed, and may substitute the source that is about to be compiled. +/// +/// V8 skips this callback only when the context allows code generation from +/// strings *and* the source is already a string. So it is consulted either +/// when the context has code generation disabled (see +/// [`Context::set_allow_generation_from_strings`]) or when the source is +/// something other than a string, whichever comes first. +/// +/// `source` is the value that was handed to `eval`/`Function`; it is not +/// necessarily a string. When it isn't, returning `codegen_allowed: true` +/// without a `modified_source` leaves V8 with nothing it knows how to compile, +/// and the source object is handed back to the caller unchanged; supply a +/// `modified_source` to have it compiled instead. +/// +/// `is_code_like` is true when that value is an object that the host marked as +/// "code like" via `v8::ObjectTemplate::SetCodeLike()`, which rusty_v8 does not +/// currently expose. +pub type ModifyCodeGenerationFromStringsCallback = + for<'s, 'i> fn( + scope: &mut PinScope<'s, 'i>, + source: Local<'s, Value>, + is_code_like: bool, + ) -> ModifyCodeGenerationFromStringsResult<'s>; + +// The layout that V8 actually sees. It matches +// `v8::ModifyCodeGenerationFromStringsResult`, with the `MaybeLocal` +// spelled as a nullable raw pointer so that the `improper_ctypes` lints — which +// don't recognize the niche in `Option>` — are satisfied. +#[repr(C)] +struct RawModifyCodeGenerationFromStringsResult { + codegen_allowed: bool, + modified_source: *mut String, +} + +// Unlike `PrepareStackTraceCallback` and the shadow realm callback below, this +// one needs no `#[cfg(target_os = "windows")]` sret shim. Those return a bare +// `MaybeLocal`, which is 8 bytes: MSVC returns it through a hidden pointer +// because it has user-declared constructors, while Rust returns it in a +// register, so the two disagree. `ModifyCodeGenerationFromStringsResult` is 16 +// bytes, which is over the register-return threshold on every ABI we target, so +// MSVC and rustc independently agree on the hidden-pointer form. On System V +// and AArch64 it is trivially copyable and fits in two registers, and both +// sides agree there too. +type RawModifyCodeGenerationFromStringsCallback = + for<'s> unsafe extern "C" fn( + context: Local<'s, Context>, + source: Local<'s, Value>, + is_code_like: bool, + ) -> RawModifyCodeGenerationFromStringsResult; + +// Windows x64 ABI: MaybeLocal returned on the stack. +#[cfg(target_os = "windows")] +pub type PrepareStackTraceCallback<'s> = + unsafe extern "C" fn( + *mut *const Value, + Local<'s, Context>, + Local<'s, Value>, + Local<'s, Array>, + ) -> *mut *const Value; + +#[cfg(not(target_os = "windows"))] +pub type PrepareStackTraceCallback<'s> = + unsafe extern "C" fn( + Local<'s, Context>, + Local<'s, Value>, + Local<'s, Array>, + ) -> PrepareStackTraceCallbackRet; + +// System V ABI: MaybeLocal returned in a register. +// System V i386 ABI: Local returned in hidden pointer (struct). +#[cfg(not(target_os = "windows"))] +#[repr(C)] +pub struct PrepareStackTraceCallbackRet(*const Value); + +pub type UseCounterFeature = v8__Isolate__UseCounterFeature; +pub type UseCounterCallback = + unsafe extern "C" fn(&mut Isolate, UseCounterFeature); + +unsafe extern "C" { + fn v8__Isolate__New(params: *const raw::CreateParams) -> *mut RealIsolate; + fn v8__Isolate__NewInGroup( + group: *const IsolateGroup, + params: *const raw::CreateParams, + ) -> *mut RealIsolate; + fn v8__Isolate__Dispose(this: *mut RealIsolate); + fn v8__Isolate__GetNumberOfDataSlots(this: *const RealIsolate) -> u32; + fn v8__Isolate__GetData( + isolate: *const RealIsolate, + slot: u32, + ) -> *mut c_void; + fn v8__Isolate__SetData( + isolate: *const RealIsolate, + slot: u32, + data: *mut c_void, + ); + fn v8__Isolate__Enter(this: *mut RealIsolate); + fn v8__Isolate__Exit(this: *mut RealIsolate); + fn v8__Global__Reset(data: *const Data); + fn v8__Isolate__GetCurrent() -> *mut RealIsolate; + fn v8__Isolate__MemoryPressureNotification(this: *mut RealIsolate, level: u8); + fn v8__Isolate__ClearKeptObjects(isolate: *mut RealIsolate); + fn v8__Isolate__LowMemoryNotification(isolate: *mut RealIsolate); + fn v8__Isolate__ContextDisposedNotification( + isolate: *mut RealIsolate, + dependants: ContextDependants, + ); + fn v8__Isolate__SetIdle(isolate: *mut RealIsolate, is_idle: bool); + fn v8__CpuProfiler__CollectSample( + isolate: *mut RealIsolate, + trace_id: *const u64, + ); + fn v8__CpuProfiler__UseDetailedSourcePositionsForProfiling( + isolate: *mut RealIsolate, + ); + fn v8__Isolate__GetHeapStatistics( + this: *mut RealIsolate, + s: *mut v8__HeapStatistics, + ); + fn v8__Isolate__SetCaptureStackTraceForUncaughtExceptions( + this: *mut RealIsolate, + capture: bool, + frame_limit: i32, + ); + fn v8__Isolate__AddMessageListener( + isolate: *mut RealIsolate, + callback: MessageCallback, + ) -> bool; + fn v8__Isolate__AddMessageListenerWithErrorLevel( + isolate: *mut RealIsolate, + callback: MessageCallback, + message_levels: MessageErrorLevel, + ) -> bool; + fn v8__Isolate__AddGCPrologueCallback( + isolate: *mut RealIsolate, + callback: GcCallbackWithData, + data: *mut c_void, + gc_type_filter: GCType, + ); + fn v8__Isolate__RemoveGCPrologueCallback( + isolate: *mut RealIsolate, + callback: GcCallbackWithData, + data: *mut c_void, + ); + fn v8__Isolate__AddGCEpilogueCallback( + isolate: *mut RealIsolate, + callback: GcCallbackWithData, + data: *mut c_void, + gc_type_filter: GCType, + ); + fn v8__Isolate__RemoveGCEpilogueCallback( + isolate: *mut RealIsolate, + callback: GcCallbackWithData, + data: *mut c_void, + ); + fn v8__Isolate__NumberOfHeapSpaces(isolate: *mut RealIsolate) -> size_t; + fn v8__Isolate__GetHeapSpaceStatistics( + isolate: *mut RealIsolate, + space_statistics: *mut v8__HeapSpaceStatistics, + index: size_t, + ) -> bool; + fn v8__Isolate__GetHeapCodeAndMetadataStatistics( + isolate: *mut RealIsolate, + code_statistics: *mut v8__HeapCodeStatistics, + ) -> bool; + fn v8__Isolate__AddNearHeapLimitCallback( + isolate: *mut RealIsolate, + callback: NearHeapLimitCallback, + data: *mut c_void, + ); + fn v8__Isolate__RemoveNearHeapLimitCallback( + isolate: *mut RealIsolate, + callback: NearHeapLimitCallback, + heap_limit: usize, + ); + fn v8__Isolate__SetOOMErrorHandler( + isolate: *mut RealIsolate, + callback: OomErrorCallback, + ); + fn v8__Isolate__AdjustAmountOfExternalAllocatedMemory( + isolate: *mut RealIsolate, + change_in_bytes: i64, + ) -> i64; + fn v8__Isolate__GetCppHeap(isolate: *mut RealIsolate) -> *mut Heap; + fn v8__Isolate__SetPrepareStackTraceCallback( + isolate: *mut RealIsolate, + callback: PrepareStackTraceCallback, + ); + fn v8__Isolate__SetPromiseHook(isolate: *mut RealIsolate, hook: PromiseHook); + fn v8__Isolate__SetPromiseRejectCallback( + isolate: *mut RealIsolate, + callback: PromiseRejectCallback, + ); + fn v8__Isolate__SetWasmAsyncResolvePromiseCallback( + isolate: *mut RealIsolate, + callback: WasmAsyncResolvePromiseCallback, + ); + fn v8__Isolate__SetAllowWasmCodeGenerationCallback( + isolate: *mut RealIsolate, + callback: AllowWasmCodeGenerationCallback, + ); + fn v8__Isolate__SetHostInitializeImportMetaObjectCallback( + isolate: *mut RealIsolate, + callback: HostInitializeImportMetaObjectCallback, + ); + fn v8__Isolate__SetHostImportModuleDynamicallyCallback( + isolate: *mut RealIsolate, + callback: RawHostImportModuleDynamicallyCallback, + ); + fn v8__Isolate__SetHostImportModuleWithPhaseDynamicallyCallback( + isolate: *mut RealIsolate, + callback: RawHostImportModuleWithPhaseDynamicallyCallback, + ); + #[cfg(not(target_os = "windows"))] + fn v8__Isolate__SetHostCreateShadowRealmContextCallback( + isolate: *mut RealIsolate, + callback: unsafe extern "C" fn( + initiator_context: Local, + ) -> *mut Context, + ); + #[cfg(target_os = "windows")] + fn v8__Isolate__SetHostCreateShadowRealmContextCallback( + isolate: *mut RealIsolate, + callback: unsafe extern "C" fn( + rv: *mut *mut Context, + initiator_context: Local, + ) -> *mut *mut Context, + ); + fn v8__Isolate__SetUseCounterCallback( + isolate: *mut RealIsolate, + callback: UseCounterCallback, + ); + fn v8__Isolate__SetModifyCodeGenerationFromStringsCallback( + isolate: *mut RealIsolate, + callback: RawModifyCodeGenerationFromStringsCallback, + ); + fn v8__Isolate__RequestInterrupt( + isolate: *const RealIsolate, + callback: InterruptCallback, + data: *mut c_void, + ); + fn v8__Isolate__TerminateExecution(isolate: *const RealIsolate); + fn v8__Isolate__IsExecutionTerminating(isolate: *const RealIsolate) -> bool; + fn v8__Isolate__CancelTerminateExecution(isolate: *const RealIsolate); + fn v8__Isolate__GetMicrotasksPolicy( + isolate: *const RealIsolate, + ) -> MicrotasksPolicy; + fn v8__Isolate__SetMicrotasksPolicy( + isolate: *mut RealIsolate, + policy: MicrotasksPolicy, + ); + fn v8__Isolate__PerformMicrotaskCheckpoint(isolate: *mut RealIsolate); + fn v8__Isolate__EnqueueMicrotask( + isolate: *mut RealIsolate, + function: *const Function, + ); + fn v8__Isolate__SetAllowAtomicsWait(isolate: *mut RealIsolate, allow: bool); + fn v8__Isolate__SetWasmStreamingCallback( + isolate: *mut RealIsolate, + callback: unsafe extern "C" fn(*const FunctionCallbackInfo), + ); + fn v8__Isolate__DateTimeConfigurationChangeNotification( + isolate: *mut RealIsolate, + time_zone_detection: TimeZoneDetection, + ); + fn v8__Isolate__HasPendingBackgroundTasks( + isolate: *const RealIsolate, + ) -> bool; + fn v8__Isolate__RequestGarbageCollectionForTesting( + isolate: *mut RealIsolate, + r#type: usize, + ); + + fn v8__HeapProfiler__TakeHeapSnapshot( + isolate: *mut RealIsolate, + callback: unsafe extern "C" fn(*mut c_void, *const u8, usize) -> bool, + arg: *mut c_void, + ); +} + +/// Isolate represents an isolated instance of the V8 engine. V8 isolates have +/// completely separate states. Objects from one isolate must not be used in +/// other isolates. The embedder can create multiple isolates and use them in +/// parallel in multiple threads. An isolate can be entered by at most one +/// thread at any given time. The Locker/Unlocker API must be used to +/// synchronize. +/// +/// rusty_v8 note: Unlike in the C++ API, the Isolate is entered when it is +/// constructed and exited when dropped. Because of that v8::OwnedIsolate +/// instances must be dropped in the reverse order of creation +#[repr(transparent)] +#[derive(Debug)] +pub struct Isolate(NonNull); + +#[repr(transparent)] +#[derive(Debug, Clone, Copy)] +pub struct UnsafeRawIsolatePtr(*mut RealIsolate); + +impl UnsafeRawIsolatePtr { + #[inline] + pub(crate) fn from_real_ptr(ptr: *mut RealIsolate) -> Self { + Self(ptr) + } + + pub fn null() -> Self { + Self(std::ptr::null_mut()) + } + + pub fn is_null(&self) -> bool { + self.0.is_null() + } + + #[inline] + pub(crate) fn as_real_ptr(&self) -> *mut RealIsolate { + self.0 + } +} + +#[repr(C)] +pub struct RealIsolate(Opaque); + +impl Isolate { + pub(crate) fn as_real_ptr(&self) -> *mut RealIsolate { + self.0.as_ptr() + } + + pub unsafe fn as_raw_isolate_ptr(&self) -> UnsafeRawIsolatePtr { + UnsafeRawIsolatePtr(self.0.as_ptr()) + } + + #[inline] + pub unsafe fn from_raw_isolate_ptr(ptr: UnsafeRawIsolatePtr) -> Self { + Self(NonNull::new(ptr.0).unwrap()) + } + + #[inline] + pub unsafe fn from_raw_isolate_ptr_unchecked( + ptr: UnsafeRawIsolatePtr, + ) -> Self { + Self(unsafe { NonNull::new_unchecked(ptr.0) }) + } + + pub unsafe fn from_raw_ptr_unchecked(ptr: *mut RealIsolate) -> Self { + Self(unsafe { NonNull::new_unchecked(ptr) }) + } + + pub unsafe fn from_raw_ptr(ptr: *mut RealIsolate) -> Self { + Self(NonNull::new(ptr).unwrap()) + } + + #[inline] + pub unsafe fn ref_from_raw_isolate_ptr(ptr: &UnsafeRawIsolatePtr) -> &Self { + if ptr.is_null() { + panic!("UnsafeRawIsolatePtr is null"); + } + unsafe { &*(ptr as *const UnsafeRawIsolatePtr as *const Isolate) } + } + + #[inline] + pub unsafe fn ref_from_raw_isolate_ptr_unchecked( + ptr: &UnsafeRawIsolatePtr, + ) -> &Self { + unsafe { &*(ptr as *const UnsafeRawIsolatePtr as *const Isolate) } + } + + #[inline] + pub unsafe fn ref_from_raw_isolate_ptr_mut( + ptr: &mut UnsafeRawIsolatePtr, + ) -> &mut Self { + if ptr.is_null() { + panic!("UnsafeRawIsolatePtr is null"); + } + unsafe { &mut *(ptr as *mut UnsafeRawIsolatePtr as *mut Isolate) } + } + + #[inline] + pub unsafe fn ref_from_raw_isolate_ptr_mut_unchecked( + ptr: &mut UnsafeRawIsolatePtr, + ) -> &mut Self { + unsafe { &mut *(ptr as *mut UnsafeRawIsolatePtr as *mut Isolate) } + } + + #[inline] + pub(crate) unsafe fn from_non_null(ptr: NonNull) -> Self { + Self(ptr) + } + + #[inline] + pub(crate) unsafe fn from_raw_ref(ptr: &NonNull) -> &Self { + // SAFETY: Isolate is a repr(transparent) wrapper around NonNull + unsafe { &*(ptr as *const NonNull as *const Isolate) } + } + + #[inline] + pub(crate) unsafe fn from_raw_ref_mut( + ptr: &mut NonNull, + ) -> &mut Self { + // SAFETY: Isolate is a repr(transparent) wrapper around NonNull + unsafe { &mut *(ptr as *mut NonNull as *mut Isolate) } + } + + // Isolate data slots used internally by rusty_v8. + const ANNEX_SLOT: u32 = 0; + const INTERNAL_DATA_SLOT_COUNT: u32 = 1; + + #[inline(always)] + fn assert_embedder_data_slot_count_and_offset_correct(&self) { + assert!( + unsafe { v8__Isolate__GetNumberOfDataSlots(self.as_real_ptr()) } + >= Self::INTERNAL_DATA_SLOT_COUNT + ) + } + + fn new_impl( + group: Option<&IsolateGroup>, + params: CreateParams, + ) -> *mut RealIsolate { + crate::V8::assert_initialized(); + let (raw_create_params, create_param_allocations) = params.finalize(); + let has_embedder_cpp_heap = !raw_create_params.cpp_heap.is_null(); + let cxx_isolate = match group { + // Calls the group-less V8 overload, which is itself defined as passing + // the default group, so this only saves materializing an `IsolateGroup` + // handle on the Rust side. Keeping it means `Isolate::new` reaches V8 by + // exactly the path it always has. + None => unsafe { v8__Isolate__New(&raw_create_params) }, + Some(group) => unsafe { + v8__Isolate__NewInGroup(group, &raw_create_params) + }, + }; + let mut isolate = unsafe { Isolate::from_raw_ptr(cxx_isolate) }; + isolate.initialize(create_param_allocations); + isolate.get_annex_mut().has_embedder_cpp_heap = has_embedder_cpp_heap; + cxx_isolate + } + + pub(crate) fn initialize(&mut self, create_param_allocations: Box) { + self.assert_embedder_data_slot_count_and_offset_correct(); + self.create_annex(create_param_allocations); + } + + /// Creates a new isolate. Does not change the currently entered + /// isolate. + /// + /// When an isolate is no longer used its resources should be freed + /// by calling V8::dispose(). Using the delete operator is not allowed. + /// + /// V8::initialize() must have run prior to this. + #[allow(clippy::new_ret_no_self)] + pub fn new(params: CreateParams) -> OwnedIsolate { + OwnedIsolate::new(Self::new_impl(None, params)) + } + + /// Creates a new isolate belonging to the given [`IsolateGroup`]. Does not + /// change the currently entered isolate. + /// + /// Isolates in different groups have separate pointer-compression cages, so + /// this is how an embedder gets past the 4 GB per-group heap ceiling that + /// pointer compression otherwise imposes. Pass the same group to several + /// calls to place those isolates together. + /// + /// When an isolate is no longer used its resources should be freed + /// by calling V8::dispose(). Using the delete operator is not allowed. + /// + /// V8::initialize() must have run prior to this. + #[allow(clippy::new_ret_no_self)] + pub fn new_with_group( + group: &IsolateGroup, + params: CreateParams, + ) -> OwnedIsolate { + OwnedIsolate::new(Self::new_impl(Some(group), params)) + } + + #[allow(clippy::new_ret_no_self)] + pub fn snapshot_creator( + external_references: Option>, + params: Option, + ) -> OwnedIsolate { + SnapshotCreator::new(external_references, params) + } + + #[allow(clippy::new_ret_no_self)] + pub fn snapshot_creator_from_existing_snapshot( + existing_snapshot_blob: StartupData, + external_references: Option>, + params: Option, + ) -> OwnedIsolate { + SnapshotCreator::from_existing_snapshot( + existing_snapshot_blob, + external_references, + params, + ) + } + + /// Initial configuration parameters for a new Isolate. + #[inline(always)] + pub fn create_params() -> CreateParams { + CreateParams::default() + } + + #[inline(always)] + pub fn thread_safe_handle(&self) -> IsolateHandle { + self.get_annex().isolate_handle.clone() + } + + #[inline(always)] + pub(crate) fn global_liveness(&self) -> NonNull { + self.get_annex().global_liveness + } + + /// See [`IsolateHandle::terminate_execution`] + #[inline(always)] + pub fn terminate_execution(&self) -> bool { + unsafe { v8__Isolate__TerminateExecution(self.as_real_ptr()) }; + true + } + + /// See [`IsolateHandle::cancel_terminate_execution`] + #[inline(always)] + pub fn cancel_terminate_execution(&self) -> bool { + unsafe { v8__Isolate__CancelTerminateExecution(self.as_real_ptr()) }; + true + } + + /// See [`IsolateHandle::is_execution_terminating`] + #[inline(always)] + pub fn is_execution_terminating(&self) -> bool { + unsafe { v8__Isolate__IsExecutionTerminating(self.as_real_ptr()) } + } + + pub(crate) fn create_annex( + &mut self, + create_param_allocations: Box, + ) { + let annex_box = Box::new(IsolateAnnex::new(self, create_param_allocations)); + let annex_ptr = Box::into_raw(annex_box); + assert!(self.get_data_internal(Self::ANNEX_SLOT).is_null()); + self.set_data_internal(Self::ANNEX_SLOT, annex_ptr as *mut _); + } + + /// Prepare annex teardown while keeping `ANNEX_SLOT` pointing at the annex. + /// + /// Nulls the `IsolateHandle`'s inner pointer, reclaims + /// `create_param_allocations`, and drops the slot storage. The annex + /// allocation itself stays alive and `ANNEX_SLOT` keeps pointing at it, + /// so code that runs during the subsequent V8 teardown GC (weak + /// callbacks, guaranteed finalizers, embedder slot drops) can still + /// resolve the annex through `get_annex()` / `get_annex_mut()`. + /// + /// The returned pointer must be passed exactly once to + /// [`Self::finish_annex_dispose`] (or, on the snapshot path, dropped by + /// [`Self::dispose_annex`]). + /// + /// # Safety + /// + /// Called once per isolate, from teardown paths only. + pub(crate) unsafe fn prepare_annex_for_dispose( + &mut self, + ) -> (*mut IsolateAnnex, Box) { + let annex_ptr = + self.get_data_internal(Self::ANNEX_SLOT) as *mut IsolateAnnex; + assert!(!annex_ptr.is_null()); + + // Each step below operates through the raw pointer rather than a + // long-lived `&mut IsolateAnnex`. The borrows we form here are + // narrowly-scoped expressions that end before any user-controlled + // Drop runs. This matters because slot Drops, weak callbacks, and + // guaranteed finalizers may re-enter the isolate (e.g. via + // `Isolate::thread_safe_handle()`) and resolve the annex through + // `get_annex()`. An outer `&mut IsolateAnnex` held across that + // re-entry would alias the shared borrow they obtain. + + // SAFETY: `annex_ptr` is non-null and points at a live `IsolateAnnex` + // (ANNEX_SLOT is only cleared by code further down this teardown + // path). + unsafe { + // Release Globals dropped by threads that could not touch the + // isolate, then null the `IsolateHandle` so handles outliving the + // isolate see a disposed state. + (*annex_ptr) + .global_liveness() + .close_deferred_global_resets(); + (*annex_ptr).global_liveness().dispose(); + (*annex_ptr).isolate_handle.dispose(); + } + + // Reclaim `create_param_allocations` so the caller can keep it alive + // for as long as V8 needs (during snapshot blob creation, V8 reads + // external references out of it). + let create_param_allocations = + unsafe { (*annex_ptr).create_param_allocations.take().unwrap() }; + + // Move slots out before dropping them. A user Drop may re-enter the + // annex; holding `&mut (*annex_ptr).slots` across that would alias + // any `&IsolateAnnex` the re-entry obtains. + let slots = unsafe { std::mem::take(&mut (*annex_ptr).slots) }; + drop(slots); + + (annex_ptr, create_param_allocations) + } + + /// Drain `finalizer_map` and invoke any guaranteed finalizers. + /// + /// New finalizers registered by the running callbacks land in the + /// annex's now-empty `finalizer_map` and will be picked up by the next + /// call (currently the second drain in [`OwnedIsolate::drop`] after V8 + /// finishes its teardown GC). + /// + /// # Safety + /// + /// `annex_ptr` must point at a live `IsolateAnnex` with `ANNEX_SLOT` + /// still referencing it, so re-entrant callbacks resolve the annex + /// through normal accessors. + pub(crate) unsafe fn run_remaining_guaranteed_finalizers( + annex_ptr: *mut IsolateAnnex, + ) { + // Take the map out under a narrow borrow so the for-loop below + // borrows a local instead of `(*annex_ptr).finalizer_map`. Callbacks + // re-entering the annex via `get_annex_mut()` would otherwise alias + // an in-flight `&mut`. + let mut map = unsafe { std::mem::take(&mut (*annex_ptr).finalizer_map) }; + for finalizer in map.drain() { + if let FinalizerCallback::Guaranteed(callback) = finalizer { + callback(); + } + } + } + + /// Free the annex allocation after V8's final teardown. + /// + /// Drains any guaranteed finalizers V8's teardown GC may have + /// registered, then drops the annex box. `ANNEX_SLOT` is not cleared + /// because the isolate is gone — its embedder data storage no longer + /// exists. + /// + /// # Safety + /// + /// `annex_ptr` must be the pointer returned from a matching + /// [`Self::prepare_annex_for_dispose`] call, and the V8 isolate must + /// already be fully disposed (so no further callbacks can fire). + pub(crate) unsafe fn finish_annex_dispose(annex_ptr: *mut IsolateAnnex) { + unsafe { Self::run_remaining_guaranteed_finalizers(annex_ptr) }; + unsafe { drop(Box::from_raw(annex_ptr)) }; + } + + /// Snapshot-path teardown. + /// + /// Used by [`OwnedIsolate::create_blob`], which consumes the isolate + /// before V8 has run its final dispose. Cleans up the annex synchronously + /// (no weak-callback re-entry to worry about here) and nulls `ANNEX_SLOT` + /// so the snapshot creator's later isolate-dispose sees a clean slot. + unsafe fn dispose_annex(&mut self) -> Box { + let (annex_ptr, create_param_allocations) = + unsafe { self.prepare_annex_for_dispose() }; + unsafe { Self::run_remaining_guaranteed_finalizers(annex_ptr) }; + let taken_annex = + self.take_data_internal(Self::ANNEX_SLOT) as *mut IsolateAnnex; + debug_assert_eq!(taken_annex, annex_ptr); + unsafe { drop(Box::from_raw(annex_ptr)) }; + create_param_allocations + } + + #[inline(always)] + fn get_annex(&self) -> &IsolateAnnex { + let annex_ptr = + self.get_data_internal(Self::ANNEX_SLOT) as *const IsolateAnnex; + assert!(!annex_ptr.is_null()); + unsafe { &*annex_ptr } + } + + #[inline(always)] + fn get_annex_mut(&mut self) -> &mut IsolateAnnex { + let annex_ptr = + self.get_data_internal(Self::ANNEX_SLOT) as *mut IsolateAnnex; + assert!(!annex_ptr.is_null()); + unsafe { &mut *annex_ptr } + } + + /// Returns a non-null pointer to the isolate's annex data. + /// This is used by scopes to cache the annex pointer and avoid + /// repeated FFI calls to `v8__Isolate__GetData`. + #[inline(always)] + pub(crate) fn get_annex_ptr(&self) -> NonNull { + let annex_ptr = + self.get_data_internal(Self::ANNEX_SLOT) as *mut IsolateAnnex; + debug_assert!(!annex_ptr.is_null()); + unsafe { NonNull::new_unchecked(annex_ptr) } + } + + pub(crate) fn set_snapshot_creator( + &mut self, + snapshot_creator: SnapshotCreator, + ) { + let prev = self + .get_annex_mut() + .maybe_snapshot_creator + .replace(snapshot_creator); + assert!(prev.is_none()); + } + + pub(crate) fn get_finalizer_map(&self) -> &FinalizerMap { + &self.get_annex().finalizer_map + } + + pub(crate) fn get_finalizer_map_mut(&mut self) -> &mut FinalizerMap { + &mut self.get_annex_mut().finalizer_map + } + + pub(crate) fn live_weak_count_mut(&mut self) -> &mut usize { + &mut self.get_annex_mut().live_weak_count + } + + /// Release one live-`Weak` count. Saturating, so that a mispaired + /// `Weak::from_raw` (which is `unsafe` and could hand the same raw + /// pointer out twice) can't wrap the counter and leave `try_into_shared` + /// panicking forever about weak handles that don't exist. + pub(crate) fn release_live_weak(&mut self) { + let count = self.live_weak_count_mut(); + debug_assert!(*count > 0, "live weak count underflow"); + *count = count.saturating_sub(1); + } + + /// Retrieve embedder-specific data from the isolate. + /// Returns NULL if SetData has never been called for the given `slot`. + pub fn get_data(&self, slot: u32) -> *mut c_void { + self.get_data_internal(Self::INTERNAL_DATA_SLOT_COUNT + slot) + } + + /// Associate embedder-specific data with the isolate. `slot` has to be + /// between 0 and `Isolate::get_number_of_data_slots()`. + #[inline(always)] + pub fn set_data(&mut self, slot: u32, data: *mut c_void) { + self.set_data_internal(Self::INTERNAL_DATA_SLOT_COUNT + slot, data); + } + + /// Returns the maximum number of available embedder data slots. Valid slots + /// are in the range of `0 <= n < Isolate::get_number_of_data_slots()`. + pub fn get_number_of_data_slots(&self) -> u32 { + let n = unsafe { v8__Isolate__GetNumberOfDataSlots(self.as_real_ptr()) }; + n - Self::INTERNAL_DATA_SLOT_COUNT + } + + #[inline(always)] + pub(crate) fn get_data_internal(&self, slot: u32) -> *mut c_void { + unsafe { v8__Isolate__GetData(self.as_real_ptr(), slot) } + } + + #[inline(always)] + pub(crate) fn set_data_internal(&mut self, slot: u32, data: *mut c_void) { + unsafe { v8__Isolate__SetData(self.as_real_ptr(), slot, data) } + } + + /// Get the value of the slot and replace it with a null pointer. + #[inline(always)] + fn take_data_internal(&mut self, slot: u32) -> *mut c_void { + let ptr = self.get_data_internal(slot); + self.set_data_internal(slot, null_mut()); + ptr + } + + // pub(crate) fn init_scope_root(&mut self) { + // ScopeData::new_root(self); + // } + + // pub(crate) fn dispose_scope_root(&mut self) { + // ScopeData::drop_root(self); + // } + + // /// Returns a pointer to the `ScopeData` struct for the current scope. + // #[inline(always)] + // pub(crate) fn get_current_scope_data(&self) -> Option> { + // let scope_data_ptr = self.get_data_internal(Self::CURRENT_SCOPE_DATA_SLOT); + // NonNull::new(scope_data_ptr).map(NonNull::cast) + // } + + // /// Updates the slot that stores a `ScopeData` pointer for the current scope. + // #[inline(always)] + // pub(crate) fn set_current_scope_data( + // &mut self, + // scope_data: Option>, + // ) { + // let scope_data_ptr = scope_data + // .map(NonNull::cast) + // .map_or_else(null_mut, NonNull::as_ptr); + // self.set_data_internal(Self::CURRENT_SCOPE_DATA_SLOT, scope_data_ptr); + // } + + /// Get a reference to embedder data added with `set_slot()`. + #[inline(always)] + pub fn get_slot(&self) -> Option<&T> { + self.get_annex().get_slot::() + } + + /// Get a mutable reference to embedder data added with `set_slot()`. + #[inline(always)] + pub fn get_slot_mut(&mut self) -> Option<&mut T> { + self.get_annex_mut().get_slot_mut::() + } + + /// Use with Isolate::get_slot and Isolate::get_slot_mut to associate state + /// with an Isolate. + /// + /// This method gives ownership of value to the Isolate. Exactly one object of + /// each type can be associated with an Isolate. If called more than once with + /// an object of the same type, the earlier version will be dropped and + /// replaced. + /// + /// Returns true if value was set without replacing an existing value. + /// + /// The value will be dropped when the isolate is dropped. + #[inline(always)] + pub fn set_slot(&mut self, value: T) -> bool { + self.get_annex_mut().set_slot(value) + } + + /// Removes the embedder data added with `set_slot()` and returns it if it exists. + #[inline(always)] + pub fn remove_slot(&mut self) -> Option { + self.get_annex_mut().remove_slot::() + } + + /// Sets this isolate as the entered one for the current thread. + /// Saves the previously entered one (if any), so that it can be + /// restored when exiting. Re-entering an isolate is allowed. + /// + /// rusty_v8 note: Unlike in the C++ API, the isolate is entered when it is + /// constructed and exited when dropped. + #[inline(always)] + pub unsafe fn enter(&self) { + unsafe { + v8__Isolate__Enter(self.as_real_ptr()); + } + } + + /// Exits this isolate by restoring the previously entered one in the + /// current thread. The isolate may still stay the same, if it was + /// entered more than once. + /// + /// Requires: self == Isolate::GetCurrent(). + /// + /// rusty_v8 note: Unlike in the C++ API, the isolate is entered when it is + /// constructed and exited when dropped. + #[inline(always)] + pub unsafe fn exit(&self) { + unsafe { + v8__Isolate__Exit(self.as_real_ptr()); + } + } + + /// Optional notification that the system is running low on memory. + /// V8 uses these notifications to guide heuristics. + /// It is allowed to call this function from another thread while + /// the isolate is executing long running JavaScript code. + #[inline(always)] + pub fn memory_pressure_notification(&mut self, level: MemoryPressureLevel) { + unsafe { + v8__Isolate__MemoryPressureNotification(self.as_real_ptr(), level as u8) + } + } + + /// Optional notification that a context has been disposed. V8 uses these + /// notifications to guide heuristics on e.g. GC or compilers. + /// + /// `dependants` signals whether the disposed context possibly had any + /// dependants. + #[inline(always)] + pub fn context_disposed_notification( + &mut self, + dependants: ContextDependants, + ) { + unsafe { + v8__Isolate__ContextDisposedNotification(self.as_real_ptr(), dependants); + } + } + + /// Clears the set of objects held strongly by the heap. This set of + /// objects are originally built when a WeakRef is created or + /// successfully dereferenced. + /// + /// This is invoked automatically after microtasks are run. See + /// MicrotasksPolicy for when microtasks are run. + /// + /// This needs to be manually invoked only if the embedder is manually + /// running microtasks via a custom MicrotaskQueue class's PerformCheckpoint. + /// In that case, it is the embedder's responsibility to make this call at a + /// time which does not interrupt synchronous ECMAScript code execution. + #[inline(always)] + pub fn clear_kept_objects(&mut self) { + unsafe { v8__Isolate__ClearKeptObjects(self.as_real_ptr()) } + } + + /// Optional notification that the system is running low on memory. + /// V8 uses these notifications to attempt to free memory. + #[inline(always)] + pub fn low_memory_notification(&mut self) { + unsafe { v8__Isolate__LowMemoryNotification(self.as_real_ptr()) } + } + + /// Tells the VM whether the embedder is currently idle or not. + /// + /// This is consulted by V8's CPU profiler: samples taken while the embedder + /// is idle (for instance, blocked waiting for I/O in the event loop) are + /// attributed to the "(idle)" node instead of being counted as running code. + /// Embedders that don't call this end up reporting ~100% CPU usage in tools + /// like Chrome DevTools even when the program is doing nothing. + /// + /// Must be called on the isolate's own thread while no JavaScript is + /// executing (e.g. right before parking the event loop, and again with + /// `false` once it resumes). + #[inline(always)] + pub fn set_idle(&mut self, is_idle: bool) { + unsafe { v8__Isolate__SetIdle(self.as_real_ptr(), is_idle) } + } + + /// Synchronously collect a CPU profiling sample in all CPU profilers + /// attached to this isolate. This does not affect the number of ticks + /// recorded for the current top node. + /// + /// When `trace_id` is `Some`, the sample is tagged with that identifier, + /// which is useful to associate the sample with a trace event. + #[inline(always)] + pub fn collect_cpu_profiler_sample(&mut self, trace_id: Option) { + let trace_id_ptr = match &trace_id { + Some(id) => id as *const u64, + None => std::ptr::null(), + }; + unsafe { v8__CpuProfiler__CollectSample(self.as_real_ptr(), trace_id_ptr) } + } + + /// Generate more detailed source positions for code objects. This results in + /// better accuracy when mapping CPU profiling samples back to script source, + /// at the cost of some additional memory and CPU overhead. + #[inline(always)] + pub fn use_detailed_source_positions_for_profiling(&mut self) { + unsafe { + v8__CpuProfiler__UseDetailedSourcePositionsForProfiling( + self.as_real_ptr(), + ) + } + } + + /// Get statistics about the heap memory usage. + #[inline(always)] + pub fn get_heap_statistics(&mut self) -> HeapStatistics { + let inner = unsafe { + let mut s = MaybeUninit::zeroed(); + v8__Isolate__GetHeapStatistics(self.as_real_ptr(), s.as_mut_ptr()); + s.assume_init() + }; + HeapStatistics(inner) + } + + /// Returns the number of spaces in the heap. + #[inline(always)] + pub fn number_of_heap_spaces(&mut self) -> usize { + unsafe { v8__Isolate__NumberOfHeapSpaces(self.as_real_ptr()) } + } + + /// Get the memory usage of a space in the heap. + /// + /// \param space_statistics The HeapSpaceStatistics object to fill in + /// statistics. + /// \param index The index of the space to get statistics from, which ranges + /// from 0 to NumberOfHeapSpaces() - 1. + /// \returns true on success. + #[inline(always)] + pub fn get_heap_space_statistics( + &mut self, + index: usize, + ) -> Option { + let inner = unsafe { + let mut s = MaybeUninit::zeroed(); + if !v8__Isolate__GetHeapSpaceStatistics( + self.as_real_ptr(), + s.as_mut_ptr(), + index, + ) { + return None; + } + s.assume_init() + }; + Some(HeapSpaceStatistics(inner)) + } + + /// Get code and metadata statistics for the heap. + /// + /// \returns true on success. + #[inline(always)] + pub fn get_heap_code_and_metadata_statistics( + &mut self, + ) -> Option { + let inner = unsafe { + let mut s = MaybeUninit::zeroed(); + if !v8__Isolate__GetHeapCodeAndMetadataStatistics( + self.as_real_ptr(), + s.as_mut_ptr(), + ) { + return None; + } + s.assume_init() + }; + Some(HeapCodeStatistics(inner)) + } + + /// Tells V8 to capture current stack trace when uncaught exception occurs + /// and report it to the message listeners. The option is off by default. + #[inline(always)] + pub fn set_capture_stack_trace_for_uncaught_exceptions( + &mut self, + capture: bool, + frame_limit: i32, + ) { + unsafe { + v8__Isolate__SetCaptureStackTraceForUncaughtExceptions( + self.as_real_ptr(), + capture, + frame_limit, + ); + } + } + + /// Adds a message listener (errors only). + /// + /// The same message listener can be added more than once and in that + /// case it will be called more than once for each message. + /// + /// The exception object will be passed to the callback. + #[inline(always)] + pub fn add_message_listener(&mut self, callback: MessageCallback) -> bool { + unsafe { v8__Isolate__AddMessageListener(self.as_real_ptr(), callback) } + } + + /// Adds a message listener for the specified message levels. + #[inline(always)] + pub fn add_message_listener_with_error_level( + &mut self, + callback: MessageCallback, + message_levels: MessageErrorLevel, + ) -> bool { + unsafe { + v8__Isolate__AddMessageListenerWithErrorLevel( + self.as_real_ptr(), + callback, + message_levels, + ) + } + } + + /// This specifies the callback called when the stack property of Error + /// is accessed. + /// + /// PrepareStackTraceCallback is called when the stack property of an error is + /// first accessed. The return value will be used as the stack value. If this + /// callback is registed, the |Error.prepareStackTrace| API will be disabled. + /// |sites| is an array of call sites, specified in + /// https://v8.dev/docs/stack-trace-api + #[inline(always)] + pub fn set_prepare_stack_trace_callback<'s>( + &mut self, + callback: impl MapFnTo>, + ) { + // Note: the C++ API returns a MaybeLocal but V8 asserts at runtime when + // it's empty. That is, you can't return None and that's why the Rust API + // expects Local instead of Option>. + unsafe { + v8__Isolate__SetPrepareStackTraceCallback( + self.as_real_ptr(), + callback.map_fn_to(), + ); + }; + } + + /// Set the PromiseHook callback for various promise lifecycle + /// events. + #[inline(always)] + pub fn set_promise_hook(&mut self, hook: PromiseHook) { + unsafe { v8__Isolate__SetPromiseHook(self.as_real_ptr(), hook) } + } + + /// Set callback to notify about promise reject with no handler, or + /// revocation of such a previous notification once the handler is added. + #[inline(always)] + pub fn set_promise_reject_callback( + &mut self, + callback: PromiseRejectCallback, + ) { + unsafe { + v8__Isolate__SetPromiseRejectCallback(self.as_real_ptr(), callback) + } + } + + #[inline(always)] + pub fn set_wasm_async_resolve_promise_callback( + &mut self, + callback: WasmAsyncResolvePromiseCallback, + ) { + unsafe { + v8__Isolate__SetWasmAsyncResolvePromiseCallback( + self.as_real_ptr(), + callback, + ) + } + } + + #[inline(always)] + pub fn set_allow_wasm_code_generation_callback( + &mut self, + callback: AllowWasmCodeGenerationCallback, + ) { + unsafe { + v8__Isolate__SetAllowWasmCodeGenerationCallback( + self.as_real_ptr(), + callback, + ); + } + } + + #[inline(always)] + /// This specifies the callback called by the upcoming importa.meta + /// language feature to retrieve host-defined meta data for a module. + pub fn set_host_initialize_import_meta_object_callback( + &mut self, + callback: HostInitializeImportMetaObjectCallback, + ) { + unsafe { + v8__Isolate__SetHostInitializeImportMetaObjectCallback( + self.as_real_ptr(), + callback, + ); + } + } + + /// This specifies the callback called by the upcoming dynamic + /// import() language feature to load modules. + #[inline(always)] + pub fn set_host_import_module_dynamically_callback( + &mut self, + callback: impl HostImportModuleDynamicallyCallback, + ) { + unsafe { + v8__Isolate__SetHostImportModuleDynamicallyCallback( + self.as_real_ptr(), + callback.to_c_fn(), + ); + } + } + + /// This specifies the callback called by the upcoming dynamic + /// import() and import.source() language feature to load modules. + /// + /// This API is experimental and is expected to be changed or removed in the + /// future. The callback is currently only called when for source-phase + /// imports. Evaluation-phase imports use the existing + /// HostImportModuleDynamicallyCallback callback. + #[inline(always)] + pub fn set_host_import_module_with_phase_dynamically_callback( + &mut self, + callback: impl HostImportModuleWithPhaseDynamicallyCallback, + ) { + unsafe { + v8__Isolate__SetHostImportModuleWithPhaseDynamicallyCallback( + self.as_real_ptr(), + callback.to_c_fn(), + ); + } + } + + /// This specifies the callback called by the upcoming `ShadowRealm` + /// construction language feature to retrieve host created globals. + pub fn set_host_create_shadow_realm_context_callback( + &mut self, + callback: HostCreateShadowRealmContextCallback, + ) { + #[inline] + unsafe extern "C" fn rust_shadow_realm_callback( + initiator_context: Local, + ) -> *mut Context { + let scope = pin!(unsafe { CallbackScope::new(initiator_context) }); + let mut scope = scope.init(); + let isolate = scope.as_ref(); + let callback = isolate + .get_slot::() + .unwrap(); + let context = callback(&mut scope); + context.map_or_else(null_mut, |l| l.as_non_null().as_ptr()) + } + + // Windows x64 ABI: MaybeLocal must be returned on the stack. + #[cfg(target_os = "windows")] + unsafe extern "C" fn rust_shadow_realm_callback_windows( + rv: *mut *mut Context, + initiator_context: Local, + ) -> *mut *mut Context { + unsafe { + let ret = rust_shadow_realm_callback(initiator_context); + rv.write(ret); + } + rv + } + + let slot_didnt_exist_before = self.set_slot(callback); + if slot_didnt_exist_before { + unsafe { + #[cfg(target_os = "windows")] + v8__Isolate__SetHostCreateShadowRealmContextCallback( + self.as_real_ptr(), + rust_shadow_realm_callback_windows, + ); + #[cfg(not(target_os = "windows"))] + v8__Isolate__SetHostCreateShadowRealmContextCallback( + self.as_real_ptr(), + rust_shadow_realm_callback, + ); + } + } + } + + /// Sets a callback for counting the number of times a feature of V8 is used. + #[inline(always)] + pub fn set_use_counter_callback(&mut self, callback: UseCounterCallback) { + unsafe { + v8__Isolate__SetUseCounterCallback(self.as_real_ptr(), callback); + } + } + + /// Enables the host application to receive a notification before a + /// garbage collection. Allocations are allowed in the callback function, + /// but the callback is not re-entrant: if the allocation inside it will + /// trigger the garbage collection, the callback won't be called again. + /// It is possible to specify the GCType filter for your callback. But it is + /// not possible to register the same callback function two times with + /// different GCType filters. + #[allow(clippy::not_unsafe_ptr_arg_deref)] // False positive. + #[inline(always)] + pub fn add_gc_prologue_callback( + &mut self, + callback: GcCallbackWithData, + data: *mut c_void, + gc_type_filter: GCType, + ) { + unsafe { + v8__Isolate__AddGCPrologueCallback( + self.as_real_ptr(), + callback, + data, + gc_type_filter, + ); + } + } + + /// This function removes callback which was installed by + /// AddGCPrologueCallback function. + #[allow(clippy::not_unsafe_ptr_arg_deref)] // False positive. + #[inline(always)] + pub fn remove_gc_prologue_callback( + &mut self, + callback: GcCallbackWithData, + data: *mut c_void, + ) { + unsafe { + v8__Isolate__RemoveGCPrologueCallback(self.as_real_ptr(), callback, data) + } + } + + /// Enables the host application to receive a notification after a + /// garbage collection. + #[allow(clippy::not_unsafe_ptr_arg_deref)] // False positive. + #[inline(always)] + pub fn add_gc_epilogue_callback( + &mut self, + callback: GcCallbackWithData, + data: *mut c_void, + gc_type_filter: GCType, + ) { + unsafe { + v8__Isolate__AddGCEpilogueCallback( + self.as_real_ptr(), + callback, + data, + gc_type_filter, + ); + } + } + + /// This function removes a callback which was added by + /// `AddGCEpilogueCallback`. + #[allow(clippy::not_unsafe_ptr_arg_deref)] // False positive. + #[inline(always)] + pub fn remove_gc_epilogue_callback( + &mut self, + callback: GcCallbackWithData, + data: *mut c_void, + ) { + unsafe { + v8__Isolate__RemoveGCEpilogueCallback(self.as_real_ptr(), callback, data) + } + } + + /// This specifies the callback called by V8 when JS is trying to dynamically + /// execute code using `eval` or the `Function` constructor. + /// + /// The callback can decide whether to allow code generation and, if so, + /// modify the source code beforehand. V8 skips it only when the context + /// allows code generation from strings (see + /// [`Context::set_allow_generation_from_strings`]) *and* the source is + /// already a string; see [`ModifyCodeGenerationFromStringsCallback`] for the + /// exact conditions. + /// + /// Calling this again replaces the previously installed callback. + pub fn set_modify_code_generation_from_strings_callback( + &mut self, + callback: ModifyCodeGenerationFromStringsCallback, + ) { + unsafe extern "C" fn rust_modify_code_generation_callback<'s>( + context: Local<'s, Context>, + source: Local<'s, Value>, + is_code_like: bool, + ) -> RawModifyCodeGenerationFromStringsResult { + let scope = pin!(unsafe { CallbackScope::new(context) }); + let mut scope = scope.init(); + let callback = *scope + .as_ref() + .get_slot::() + .unwrap(); + let result = callback(&mut scope, source, is_code_like); + // `modified_source` outlives `scope` even though the raw handle slot is + // read here and only handed to V8 once `scope` has been dropped. A + // `CallbackScope` built from a `Local` has `NEEDS_SCOPE == + // false`, so it opens no `HandleScope` of its own and handles created by + // the callback belong to whichever scope V8 had active when it called + // us. If that ever changes, this needs an `EscapableHandleScope`. + RawModifyCodeGenerationFromStringsResult { + codegen_allowed: result.codegen_allowed, + modified_source: result + .modified_source + .map_or_else(null_mut, |s| s.as_non_null().as_ptr()), + } + } + + let slot_didnt_exist_before = self.set_slot(callback); + if slot_didnt_exist_before { + unsafe { + v8__Isolate__SetModifyCodeGenerationFromStringsCallback( + self.as_real_ptr(), + rust_modify_code_generation_callback, + ); + } + } + } + + /// Add a callback to invoke in case the heap size is close to the heap limit. + /// If multiple callbacks are added, only the most recently added callback is + /// invoked. + #[allow(clippy::not_unsafe_ptr_arg_deref)] // False positive. + #[inline(always)] + pub fn add_near_heap_limit_callback( + &mut self, + callback: NearHeapLimitCallback, + data: *mut c_void, + ) { + unsafe { + v8__Isolate__AddNearHeapLimitCallback(self.as_real_ptr(), callback, data) + }; + } + + /// Remove the given callback and restore the heap limit to the given limit. + /// If the given limit is zero, then it is ignored. If the current heap size + /// is greater than the given limit, then the heap limit is restored to the + /// minimal limit that is possible for the current heap size. + #[inline(always)] + pub fn remove_near_heap_limit_callback( + &mut self, + callback: NearHeapLimitCallback, + heap_limit: usize, + ) { + unsafe { + v8__Isolate__RemoveNearHeapLimitCallback( + self.as_real_ptr(), + callback, + heap_limit, + ); + }; + } + + /// Adjusts the amount of registered external memory. Used to give V8 an + /// indication of the amount of externally allocated memory that is kept + /// alive by JavaScript objects. V8 uses this to decide when to perform + /// global garbage collections. Registering externally allocated memory + /// will trigger global garbage collections more often than it would + /// otherwise in an attempt to garbage collect the JavaScript objects + /// that keep the externally allocated memory alive. + #[inline(always)] + pub fn adjust_amount_of_external_allocated_memory( + &mut self, + change_in_bytes: i64, + ) -> i64 { + unsafe { + v8__Isolate__AdjustAmountOfExternalAllocatedMemory( + self.as_real_ptr(), + change_in_bytes, + ) + } + } + + #[inline(always)] + pub fn get_cpp_heap(&mut self) -> Option<&Heap> { + unsafe { v8__Isolate__GetCppHeap(self.as_real_ptr()).as_ref() } + } + + #[inline(always)] + pub fn set_oom_error_handler(&mut self, callback: OomErrorCallback) { + unsafe { v8__Isolate__SetOOMErrorHandler(self.as_real_ptr(), callback) }; + } + + /// Returns the policy controlling how Microtasks are invoked. + #[inline(always)] + pub fn get_microtasks_policy(&self) -> MicrotasksPolicy { + unsafe { v8__Isolate__GetMicrotasksPolicy(self.as_real_ptr()) } + } + + /// Returns the policy controlling how Microtasks are invoked. + #[inline(always)] + pub fn set_microtasks_policy(&mut self, policy: MicrotasksPolicy) { + unsafe { v8__Isolate__SetMicrotasksPolicy(self.as_real_ptr(), policy) } + } + + /// Runs the default MicrotaskQueue until it gets empty and perform other + /// microtask checkpoint steps, such as calling ClearKeptObjects. Asserts that + /// the MicrotasksPolicy is not kScoped. Any exceptions thrown by microtask + /// callbacks are swallowed. + #[inline(always)] + pub fn perform_microtask_checkpoint(&mut self) { + unsafe { v8__Isolate__PerformMicrotaskCheckpoint(self.as_real_ptr()) } + } + + /// Enqueues the callback to the default MicrotaskQueue + #[inline(always)] + pub fn enqueue_microtask(&mut self, microtask: Local) { + crate::handle::assert_locals_in_isolate(self); + unsafe { v8__Isolate__EnqueueMicrotask(self.as_real_ptr(), &*microtask) } + } + + /// Set whether calling Atomics.wait (a function that may block) is allowed in + /// this isolate. This can also be configured via + /// CreateParams::allow_atomics_wait. + #[inline(always)] + pub fn set_allow_atomics_wait(&mut self, allow: bool) { + unsafe { v8__Isolate__SetAllowAtomicsWait(self.as_real_ptr(), allow) } + } + + /// Embedder injection point for `WebAssembly.compileStreaming(source)`. + /// The expectation is that the embedder sets it at most once. + /// + /// The callback receives the source argument (string, Promise, etc.) + /// and an instance of [WasmStreaming]. The [WasmStreaming] instance + /// can outlive the callback and is used to feed data chunks to V8 + /// asynchronously. + #[inline(always)] + pub fn set_wasm_streaming_callback(&mut self, _: F) + where + F: UnitType + + for<'a, 'b, 'c> Fn( + &'c mut PinScope<'a, 'b>, + Local<'a, Value>, + WasmStreaming, + ), + { + unsafe { + v8__Isolate__SetWasmStreamingCallback( + self.as_real_ptr(), + trampoline::(), + ) + } + } + + /// Notification that the embedder has changed the time zone, daylight savings + /// time or other date / time configuration parameters. V8 keeps a cache of + /// various values used for date / time computation. This notification will + /// reset those cached values for the current context so that date / time + /// configuration changes would be reflected. + /// + /// This API should not be called more than needed as it will negatively impact + /// the performance of date operations. + #[inline(always)] + pub fn date_time_configuration_change_notification( + &mut self, + time_zone_detection: TimeZoneDetection, + ) { + unsafe { + v8__Isolate__DateTimeConfigurationChangeNotification( + self.as_real_ptr(), + time_zone_detection, + ); + } + } + + /// Returns true if there is ongoing background work within V8 that will + /// eventually post a foreground task, like asynchronous WebAssembly + /// compilation. + #[inline(always)] + pub fn has_pending_background_tasks(&self) -> bool { + unsafe { v8__Isolate__HasPendingBackgroundTasks(self.as_real_ptr()) } + } + + /// Request garbage collection with a specific embedderstack state in this + /// Isolate. It is only valid to call this function if --expose_gc was + /// specified. + /// + /// This should only be used for testing purposes and not to enforce a garbage + /// collection schedule. It has strong negative impact on the garbage + /// collection performance. Use IdleNotificationDeadline() or + /// LowMemoryNotification() instead to influence the garbage collection + /// schedule. + #[inline(always)] + pub fn request_garbage_collection_for_testing( + &mut self, + r#type: GarbageCollectionType, + ) { + unsafe { + v8__Isolate__RequestGarbageCollectionForTesting( + self.as_real_ptr(), + match r#type { + GarbageCollectionType::Full => 0, + GarbageCollectionType::Minor => 1, + }, + ); + } + } + + /// Disposes the isolate. The isolate must not be entered by any + /// thread to be disposable. + pub(crate) unsafe fn dispose(&mut self) { + // No test case in rusty_v8 show this, but there have been situations in + // deno where dropping Annex before the states causes a segfault. + unsafe { + v8__Isolate__Dispose(self.as_real_ptr()); + } + } + + /// Take a heap snapshot. The callback is invoked one or more times + /// with byte slices containing the snapshot serialized as JSON. + /// It's the callback's responsibility to reassemble them into + /// a single document, e.g., by writing them to a file. + /// Note that Chrome DevTools refuses to load snapshots without + /// a .heapsnapshot suffix. + pub fn take_heap_snapshot(&mut self, mut callback: F) + where + F: FnMut(&[u8]) -> bool, + { + unsafe extern "C" fn trampoline( + arg: *mut c_void, + data: *const u8, + size: usize, + ) -> bool + where + F: FnMut(&[u8]) -> bool, + { + unsafe { + let mut callback = NonNull::::new_unchecked(arg as _); + if size > 0 { + (callback.as_mut())(std::slice::from_raw_parts(data, size)) + } else { + (callback.as_mut())(&[]) + } + } + } + + let arg = addr_of_mut!(callback); + unsafe { + v8__HeapProfiler__TakeHeapSnapshot( + self.as_real_ptr(), + trampoline::, + arg as _, + ); + } + } + + /// Set the default context to be included in the snapshot blob. + /// The snapshot will not contain the global proxy, and we expect one or a + /// global object template to create one, to be provided upon deserialization. + /// + /// # Panics + /// + /// Panics if the isolate was not created using [`Isolate::snapshot_creator`] + #[inline(always)] + pub fn set_default_context(&mut self, context: Local) { + crate::handle::assert_locals_in_isolate(self); + let snapshot_creator = self + .get_annex_mut() + .maybe_snapshot_creator + .as_mut() + .unwrap(); + snapshot_creator.set_default_context(context); + } + + /// Add additional context to be included in the snapshot blob. + /// The snapshot will include the global proxy. + /// + /// Returns the index of the context in the snapshot blob. + /// + /// # Panics + /// + /// Panics if the isolate was not created using [`Isolate::snapshot_creator`] + #[inline(always)] + pub fn add_context(&mut self, context: Local) -> usize { + crate::handle::assert_locals_in_isolate(self); + let snapshot_creator = self + .get_annex_mut() + .maybe_snapshot_creator + .as_mut() + .unwrap(); + snapshot_creator.add_context(context) + } + + /// Attach arbitrary `v8::Data` to the isolate snapshot, which can be + /// retrieved via `HandleScope::get_context_data_from_snapshot_once()` after + /// deserialization. This data does not survive when a new snapshot is created + /// from an existing snapshot. + /// + /// # Panics + /// + /// Panics if the isolate was not created using [`Isolate::snapshot_creator`] + #[inline(always)] + pub fn add_isolate_data(&mut self, data: Local) -> usize + where + for<'l> Local<'l, T>: Into>, + { + crate::handle::assert_locals_in_isolate(self); + let snapshot_creator = self + .get_annex_mut() + .maybe_snapshot_creator + .as_mut() + .unwrap(); + snapshot_creator.add_isolate_data(data) + } + + /// Attach arbitrary `v8::Data` to the context snapshot, which can be + /// retrieved via `HandleScope::get_context_data_from_snapshot_once()` after + /// deserialization. This data does not survive when a new snapshot is + /// created from an existing snapshot. + /// + /// # Panics + /// + /// Panics if the isolate was not created using [`Isolate::snapshot_creator`] + #[inline(always)] + pub fn add_context_data( + &mut self, + context: Local, + data: Local, + ) -> usize + where + for<'l> Local<'l, T>: Into>, + { + crate::handle::assert_locals_in_isolate(self); + let snapshot_creator = self + .get_annex_mut() + .maybe_snapshot_creator + .as_mut() + .unwrap(); + snapshot_creator.add_context_data(context, data) + } +} + +pub(crate) struct IsolateAnnex { + // Wrapped in `Option` so teardown can `take()` it through a `&mut + // IsolateAnnex` without having to move ownership of the whole annex out + // of `ANNEX_SLOT`. Only `prepare_annex_for_dispose` consumes it; that + // function runs at most once per annex, so the `unwrap()` there can + // never observe a `None`. + create_param_allocations: Option>, + slots: HashMap, + finalizer_map: FinalizerMap, + maybe_snapshot_creator: Option, + isolate_handle: IsolateHandle, + global_liveness: NonNull, + /// Number of live `Weak` handles, maintained so `try_into_shared()` can + /// reject isolates with outstanding weaks (their GC callbacks are not + /// thread-safe against the owning `Weak`). + live_weak_count: usize, + /// Whether an embedder cppgc heap was passed via `CreateParams`. V8 + /// attaches a default cppgc heap of its own, so `GetCppHeap()` can't + /// distinguish; `try_into_shared()` rejects only embedder heaps. + has_embedder_cpp_heap: bool, +} + +impl IsolateAnnex { + fn new(isolate: &Isolate, create_param_allocations: Box) -> Self { + // Globals may be dropped after their host isolate is disposed. Keep this + // tiny liveness cell valid so those late drops can observe the null isolate + // pointer without retaining an Arc per Global. + let global_liveness = Box::leak(Box::new(IsolateLiveness::new(isolate))); + Self { + create_param_allocations: Some(create_param_allocations), + slots: HashMap::default(), + finalizer_map: FinalizerMap::default(), + maybe_snapshot_creator: None, + isolate_handle: IsolateHandle::new(isolate), + global_liveness: NonNull::from(global_liveness), + live_weak_count: 0, + has_embedder_cpp_heap: false, + } + } + + #[inline(always)] + fn global_liveness(&self) -> &IsolateLiveness { + unsafe { self.global_liveness.as_ref() } + } + + #[inline(always)] + pub(crate) fn get_slot(&self) -> Option<&T> { + self + .slots + .get(&TypeId::of::()) + .map(|slot| unsafe { slot.borrow::() }) + } + + #[inline(always)] + pub(crate) fn get_slot_mut(&mut self) -> Option<&mut T> { + self + .slots + .get_mut(&TypeId::of::()) + .map(|slot| unsafe { slot.borrow_mut::() }) + } + + #[inline(always)] + pub(crate) fn set_slot(&mut self, value: T) -> bool { + self + .slots + .insert(TypeId::of::(), RawSlot::new(value)) + .is_none() + } + + #[inline(always)] + pub(crate) fn remove_slot(&mut self) -> Option { + self + .slots + .remove(&TypeId::of::()) + .map(|slot| unsafe { slot.into_inner::() }) + } +} + +/// A unique obligation to reset one V8 global-handle cell. +/// +/// The cell is stable until it is reset or its isolate is disposed. Moving the +/// pointer between threads is safe, but resetting it is only safe while the +/// destination thread holds the isolate's `v8::Locker`. +struct DeferredGlobalReset(NonNull); + +// SAFETY: This token is only created by `Global::drop`; that `Global` uniquely +// owns the cell and cannot use it again. The token is moved through a +// mutex-protected queue and consumed only by a thread holding the V8 lock. +unsafe impl Send for DeferredGlobalReset {} + +impl DeferredGlobalReset { + fn reset(self) { + unsafe { v8__Global__Reset(self.0.as_ptr()) }; + } +} + +// Zero is reserved for a thread whose ID has not been requested yet. IDs are +// never reused; abort before wrapping rather than risk confusing two threads. +static NEXT_THREAD_ID: AtomicUsize = AtomicUsize::new(1); + +thread_local! { + // Const-initialized and destructor-free, so this remains readable while + // other TLS values are being destroyed. `Global::clone`/`drop` hit this on + // every call. + static CURRENT_THREAD_ID: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +#[inline(always)] +fn current_thread_id() -> usize { + CURRENT_THREAD_ID.with(|current| { + let id = current.get(); + if id != 0 { + return id; + } + + // This cold path deliberately avoids `std::thread::current()`: it may run + // from another TLS destructor, where panicking would abort the process. + // A relaxed global counter is sufficient because IDs are only compared. + let id = NEXT_THREAD_ID + .fetch_update( + std::sync::atomic::Ordering::Relaxed, + std::sync::atomic::Ordering::Relaxed, + |next| next.checked_add(1), + ) + .unwrap_or_else(|_| std::process::abort()); + current.set(id); + id + }) +} + +pub(crate) struct IsolateLiveness { + isolate: AtomicPtr, + /// The thread the isolate was created on. Meaningful only while the + /// isolate is not shared: a non-shared isolate never leaves it. + home_thread: usize, + shared: std::sync::atomic::AtomicBool, + /// V8 cells whose Rust `Global` owners were dropped by threads that could + /// not touch the isolate. Drained at the next checkpoint: lock and unlock + /// boundaries for shared isolates, and `Global` creation or home-thread + /// `Global` drops for all isolates. `None` after teardown's final drain. + deferred_global_resets: Mutex>>, + /// Length hint for `deferred_global_resets`, so drain checkpoints are a + /// relaxed load in the common (empty) case instead of a mutex + /// acquisition. Updated under the mutex. + deferred_len: std::sync::atomic::AtomicUsize, +} + +impl IsolateLiveness { + #[inline(always)] + fn new(isolate: &Isolate) -> Self { + Self { + isolate: AtomicPtr::new(isolate.as_real_ptr()), + home_thread: current_thread_id(), + shared: std::sync::atomic::AtomicBool::new(false), + deferred_global_resets: Mutex::new(Some(Vec::new())), + deferred_len: std::sync::atomic::AtomicUsize::new(0), + } + } + + #[inline(always)] + fn dispose(&self) { + self + .isolate + .store(null_mut(), std::sync::atomic::Ordering::Relaxed); + } + + #[inline(always)] + pub(crate) fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.isolate.load(std::sync::atomic::Ordering::Relaxed) + } + + pub(crate) fn mark_shared(&self) { + assert!( + self.on_home_thread(), + "an isolate must be converted to SharedIsolate on its home thread" + ); + // Release/Acquire so a thread that observes `shared == false` (and + // takes the immediate-reset path in `Global::drop`) cannot do so + // after the isolate has started migrating between threads. + self + .shared + .store(true, std::sync::atomic::Ordering::Release); + } + + #[inline(always)] + pub(crate) fn is_shared(&self) -> bool { + self.shared.load(std::sync::atomic::Ordering::Acquire) + } + + /// The one gate for touching a `Global`'s V8 cell. Covers both cases: + /// a shared isolate needs its `Locker` held here, a non-shared one + /// needs the home thread. It has to be checked for non-shared isolates + /// too — `Global` is `Send`, so a plain `OwnedIsolate` can stay on + /// thread A while one of its `Global`s is used on thread B, racing A's + /// GC. + pub(crate) fn assert_access_allowed(&self) { + if !self.is_shared() { + // A non-shared OwnedIsolate cannot leave its home thread through safe + // Rust. Being on that thread therefore also excludes racing teardown, + // so the hot path needs no mutex. + assert!( + !self.get_isolate_ptr().is_null(), + "attempt to access Handle hosted by disposed Isolate" + ); + assert!( + self.on_home_thread(), + "accessing a Global requires being on its isolate's thread" + ); + return; + } + + // Sample liveness while holding the reset queue: teardown closes the + // queue under this mutex before disposing. If this thread holds the V8 + // lock, that lock prevents teardown after the mutex is released and for + // the duration of the caller's access. + let q = self.deferred_global_resets.lock().unwrap(); + let isolate_is_live = q.is_some() && !self.get_isolate_ptr().is_null(); + let allowed = isolate_is_live && self.on_isolate_thread(); + drop(q); + assert!( + isolate_is_live, + "attempt to access Handle hosted by disposed Isolate" + ); + assert!( + allowed, + "accessing a Global belonging to a shared isolate requires holding \ + its Locker on the current thread" + ); + } + + /// True when the current thread may touch the isolate's handle + /// storage: it holds the isolate's `v8::Locker` (shared), or it is the + /// isolate's home thread (non-shared). + #[inline(always)] + pub(crate) fn on_home_thread(&self) -> bool { + current_thread_id() == self.home_thread + } + + pub(crate) fn on_isolate_thread(&self) -> bool { + if self.is_shared() { + let isolate = self.get_isolate_ptr(); + // A disposed isolate cannot be locked by anyone. + !isolate.is_null() && crate::locker::thread_holds_lock(isolate) + } else { + self.on_home_thread() + } + } + + /// Release a `Global`'s cell now if the current thread may touch the + /// isolate; otherwise queue it for the next lock acquisition (shared) + /// or isolate teardown (non-shared). + /// + /// While the queue is `Some`, teardown has not started: its final drain + /// closes the queue under this mutex before the isolate is disposed, so + /// the isolate pointer read below stays valid for the duration of this + /// call. The null check is belt and braces on top of that invariant: a + /// disposed isolate means the cell is already gone. + pub(crate) fn reset_or_defer_global(&self, data: NonNull) { + let mut q = self.deferred_global_resets.lock().unwrap(); + let Some(v) = q.as_mut() else { return }; + if self.get_isolate_ptr().is_null() { + return; + } + if self.on_isolate_thread() { + drop(q); + DeferredGlobalReset(data).reset(); + } else { + v.push(DeferredGlobalReset(data)); + self + .deferred_len + .store(v.len(), std::sync::atomic::Ordering::Relaxed); + } + } + + /// Cheap drain checkpoint: a relaxed load when the queue is empty. + /// The caller must be allowed to touch the isolate (home thread, or + /// holding the lock of a shared isolate). + #[inline(always)] + pub(crate) fn maybe_drain_deferred_global_resets(&self) { + if self.deferred_len.load(std::sync::atomic::Ordering::Relaxed) > 0 { + self.drain_deferred_global_resets(); + } + } + + /// Called while allowed to touch the isolate. + pub(crate) fn drain_deferred_global_resets(&self) { + let drained = { + let mut q = self.deferred_global_resets.lock().unwrap(); + self + .deferred_len + .store(0, std::sync::atomic::Ordering::Relaxed); + match q.as_mut() { + Some(v) => std::mem::take(v), + None => return, + } + }; + for reset in drained { + reset.reset(); + } + } + + /// Final drain during teardown, with the isolate's lock held. Late + /// droppers that arrive after this see a closed queue and do nothing; + /// their cells are freed with the isolate. + pub(crate) fn close_deferred_global_resets(&self) { + let drained = { + let mut q = self.deferred_global_resets.lock().unwrap(); + self + .deferred_len + .store(0, std::sync::atomic::Ordering::Relaxed); + q.take() + }; + for reset in drained.into_iter().flatten() { + reset.reset(); + } + } +} + +impl Debug for IsolateAnnex { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.debug_struct("IsolateAnnex") + .field("isolate_handle", &self.isolate_handle) + .finish() + } +} + +pub(crate) struct IsolateHandleInner { + /// Safety invariants: + /// - The 'main thread' must lock the mutex and reset `isolate` to null just + /// before the isolate is disposed. + /// - Any other thread must lock the mutex while it's reading/using the + /// `isolate` pointer. + // These two fields can be replaced with a `Mutex<*mut RealIsolate>` once + // `Mutex::data_ptr()` is stabilized. + isolate: UnsafeCell<*mut RealIsolate>, + isolate_mutex: Mutex<()>, +} + +unsafe impl Send for IsolateHandleInner {} +unsafe impl Sync for IsolateHandleInner {} + +/// IsolateHandle is a thread-safe reference to an Isolate. Its main use is to +/// terminate execution of a running isolate from another thread. +/// +/// It is created with [`Isolate::thread_safe_handle()`]. +/// +/// IsolateHandle is Cloneable, Send, and Sync. +#[derive(Clone)] +pub struct IsolateHandle(Arc); + +impl fmt::Debug for IsolateHandle { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + if let Ok(_lock) = self.0.isolate_mutex.try_lock() { + // SAFETY: mutex lock is held + let ptr = unsafe { *self.0.isolate.get() }; + f.debug_struct("IsolateHandle") + .field("isolate_ptr", &ptr) + .finish() + } else { + f.debug_struct("IsolateHandle").finish_non_exhaustive() + } + } +} + +impl IsolateHandle { + #[inline(always)] + fn new(isolate: &Isolate) -> Self { + let inner = Arc::new(IsolateHandleInner { + isolate: UnsafeCell::new(isolate.as_real_ptr()), + isolate_mutex: Mutex::new(()), + }); + Self(inner) + } + + /// Set the inner isolate pointer to null. + fn dispose(&self) { + let _lock = self.0.isolate_mutex.lock().unwrap(); + // SAFETY: mutex lock is held + unsafe { *self.0.isolate.get() = null_mut() } + } + + /// Access the isolate, if it hasn't yet been disposed of. + /// + /// A lock is taken on the pointer and held for the scope of `f`, which + /// means the isolate can't be dropped until after `f` returns. If you + /// do something with the isolate afterwards, that needs to be verified + /// to be safe separately. + pub(crate) fn with_isolate_ptr( + &self, + f: impl FnOnce(NonNull) -> R, + ) -> Option { + let _lock = self.0.isolate_mutex.lock().unwrap(); + // SAFETY: mutex lock is held + let ptr = unsafe { *self.0.isolate.get() }; + NonNull::new(ptr).map(f) + } + + /// Access the pointer for this isolate - it may be null. + /// + /// # Safety + /// This function must only be called from the main thread associated with + /// the V8 isolate. + // TODO: have this return an `Option>` + pub(crate) unsafe fn get_isolate_ptr(&self) -> *mut RealIsolate { + // SAFETY: this function must only be called from the main thread of the + // isolate. On that thread, the caller cannot race with teardown code that + // sets this pointer to null. + unsafe { *self.0.isolate.get() } + } + + /// Forcefully terminate the current thread of JavaScript execution + /// in the given isolate. + /// + /// This method can be used by any thread even if that thread has not + /// acquired the V8 lock with a Locker object. + /// + /// Returns false if Isolate was already destroyed. + #[inline(always)] + pub fn terminate_execution(&self) -> bool { + self + .with_isolate_ptr(|isolate| unsafe { + v8__Isolate__TerminateExecution(isolate.as_ptr()) + }) + .is_some() + } + + /// Resume execution capability in the given isolate, whose execution + /// was previously forcefully terminated using TerminateExecution(). + /// + /// When execution is forcefully terminated using TerminateExecution(), + /// the isolate can not resume execution until all JavaScript frames + /// have propagated the uncatchable exception which is generated. This + /// method allows the program embedding the engine to handle the + /// termination event and resume execution capability, even if + /// JavaScript frames remain on the stack. + /// + /// This method can be used by any thread even if that thread has not + /// acquired the V8 lock with a Locker object. + /// + /// Returns false if Isolate was already destroyed. + #[inline(always)] + pub fn cancel_terminate_execution(&self) -> bool { + self + .with_isolate_ptr(|isolate| unsafe { + v8__Isolate__CancelTerminateExecution(isolate.as_ptr()) + }) + .is_some() + } + + /// Is V8 terminating JavaScript execution. + /// + /// Returns true if JavaScript execution is currently terminating + /// because of a call to TerminateExecution. In that case there are + /// still JavaScript frames on the stack and the termination + /// exception is still active. + /// + /// Returns false if Isolate was already destroyed. + #[inline(always)] + pub fn is_execution_terminating(&self) -> bool { + self + .with_isolate_ptr(|isolate| unsafe { + v8__Isolate__IsExecutionTerminating(isolate.as_ptr()) + }) + .unwrap_or(false) + } + + /// Request V8 to interrupt long running JavaScript code and invoke + /// the given |callback| passing the given |data| to it. After |callback| + /// returns control will be returned to the JavaScript code. + /// There may be a number of interrupt requests in flight. + /// Can be called from another thread without acquiring a |Locker|. + /// Registered |callback| must not reenter interrupted Isolate. + /// + /// Returns false if Isolate was already destroyed. + // Clippy warns that this method is dereferencing a raw pointer, but it is + // not: https://github.com/rust-lang/rust-clippy/issues/3045 + #[allow(clippy::not_unsafe_ptr_arg_deref)] + #[inline(always)] + pub fn request_interrupt( + &self, + callback: InterruptCallback, + data: *mut c_void, + ) -> bool { + self + .with_isolate_ptr(|isolate| unsafe { + v8__Isolate__RequestInterrupt(isolate.as_ptr(), callback, data) + }) + .is_some() + } +} + +/// Same as Isolate but gets disposed when it goes out of scope. +#[derive(Debug)] +pub struct OwnedIsolate { + cxx_isolate: NonNull, +} + +/// The reason an [`OwnedIsolate`] could not be converted into a +/// [`crate::SharedIsolate`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] +pub enum IntoSharedErrorKind { + SnapshotCreator, + LiveWeakHandlesOrPendingFinalizers, + EmbedderCppHeap, + AnotherIsolateEntered, +} + +impl fmt::Display for IntoSharedErrorKind { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let message = match self { + Self::SnapshotCreator => "snapshot-creator isolates cannot be shared", + Self::LiveWeakHandlesOrPendingFinalizers => { + "isolates with live v8::Weak handles or pending finalizers cannot be shared" + } + Self::EmbedderCppHeap => { + "isolates with an embedder cppgc heap cannot be shared" + } + Self::AnotherIsolateEntered => { + "try_into_shared() must be called with no other isolate entered on top of this one" + } + }; + f.write_str(message) + } +} + +/// A failed shared-isolate conversion. +/// +/// The original isolate is retained so the rejected state can be cleaned up +/// and conversion retried, or a snapshot creator can still produce its blob. +/// Call [`Self::into_isolate`] to recover it. Dropping this error without +/// recovering the isolate leaks it, because some rejected states (notably a +/// snapshot creator or an isolate below another entered isolate) cannot run +/// [`OwnedIsolate::drop`] safely at that point. +#[derive(Debug)] +pub struct IntoSharedError { + kind: IntoSharedErrorKind, + isolate: Option, +} + +impl IntoSharedError { + pub fn kind(&self) -> IntoSharedErrorKind { + self.kind + } + + pub fn into_isolate(mut self) -> OwnedIsolate { + self.isolate.take().unwrap() + } +} + +impl Drop for IntoSharedError { + fn drop(&mut self) { + if let Some(isolate) = self.isolate.take() { + // `OwnedIsolate::drop` may itself panic for the state represented by this + // error. Leaking is the only generally safe default; callers that can fix + // the rejected state recover ownership with `into_isolate()`. + forget(isolate); + } + } +} + +impl fmt::Display for IntoSharedError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.kind, f) + } +} + +impl std::error::Error for IntoSharedError {} + +impl OwnedIsolate { + pub(crate) fn new(cxx_isolate: *mut RealIsolate) -> Self { + let isolate = Self::new_already_entered(cxx_isolate); + unsafe { + isolate.enter(); + } + isolate + } + + pub(crate) fn new_already_entered(cxx_isolate: *mut RealIsolate) -> Self { + let cxx_isolate = NonNull::new(cxx_isolate).unwrap(); + let owned_isolate: OwnedIsolate = Self { cxx_isolate }; + // owned_isolate.init_scope_root(); + owned_isolate + } + + /// Try to convert this isolate into a [`crate::SharedIsolate`] that can be + /// locked and used from any thread. The isolate is exited on the + /// current thread; all further access goes through + /// [`crate::SharedIsolate::lock`]. + /// + /// # Safety + /// + /// A shared isolate migrates between threads together with everything + /// attached to it, without `Send` bounds the type system can check. + /// The caller must ensure that all embedder state hanging off the + /// isolate is `Send`: isolate slot values ([`Isolate::set_slot`]), + /// embedder data ([`Isolate::set_data`]), callbacks and their captured + /// state, and the allocations referenced by its `CreateParams`. All of + /// it may be accessed and eventually dropped on whichever thread holds + /// the lock or drops the `SharedIsolate`. + /// + /// This obligation covers the isolate's whole remaining lifetime, not + /// just the state present at this call: anything attached later + /// through a [`crate::Locker`] (e.g. `set_slot` under the lock) must + /// be `Send` too — nothing checks it at insertion time. + /// + /// The `OwnedIsolate` must still be on the thread where it was created. + /// This is guaranteed by its `!Send` type in safe Rust; callers that + /// reconstruct or transfer one through unsafe code must preserve that + /// invariant. + /// + /// No reference obtained through [`crate::Global::open`] may still be live + /// when this method is called. After sharing, opening a Global is unsafe and + /// its reference must not outlive the [`crate::Locker`] under which it was + /// opened or cross threads. Prefer creating a [`crate::Local`] under a handle + /// scope. Other access to a Global belonging to this isolate, including + /// cloning, hashing, and comparing, requires holding its Locker. Dropping one + /// is the sole exception and may happen on any thread. + /// + /// # Errors + /// + /// Returns the original isolate together with the rejection reason if this is + /// a snapshot-creator isolate, if it has live + /// [`crate::Weak`] handles or pending finalizers, if it has a cppgc + /// heap attached, or if another isolate is entered on top of this one + /// on the current thread. + /// + /// # Panics + /// + /// Panics if the isolate is no longer on the thread where it was created. + pub unsafe fn try_into_shared( + self, + ) -> Result { + let error_kind = if unsafe { + !std::ptr::eq(self.cxx_isolate.as_ptr(), v8__Isolate__GetCurrent()) + } { + Some(IntoSharedErrorKind::AnotherIsolateEntered) + } else { + let annex = self.get_annex(); + if annex.maybe_snapshot_creator.is_some() { + Some(IntoSharedErrorKind::SnapshotCreator) + } else if annex.live_weak_count != 0 || !annex.finalizer_map.is_empty() { + Some(IntoSharedErrorKind::LiveWeakHandlesOrPendingFinalizers) + } else if annex.has_embedder_cpp_heap { + Some(IntoSharedErrorKind::EmbedderCppHeap) + } else { + None + } + }; + + if let Some(kind) = error_kind { + return Err(IntoSharedError { + kind, + isolate: Some(self), + }); + } + + unsafe { + let isolate_handle = self.thread_safe_handle(); + self.global_liveness().as_ref().mark_shared(); + self.exit(); + let cxx_isolate = self.cxx_isolate; + forget(self); + Ok(crate::SharedIsolate::new(cxx_isolate, isolate_handle)) + } + } +} + +impl Drop for OwnedIsolate { + fn drop(&mut self) { + unsafe { + let snapshot_creator = self.get_annex_mut().maybe_snapshot_creator.take(); + assert!( + snapshot_creator.is_none(), + "If isolate was created using v8::Isolate::snapshot_creator, you should use v8::OwnedIsolate::create_blob before dropping an isolate." + ); + // Safety: We need to check `this == Isolate::GetCurrent()` before calling exit() + assert!( + std::ptr::eq(self.cxx_isolate.as_mut(), v8__Isolate__GetCurrent()), + "v8::OwnedIsolate instances must be dropped in the reverse order of creation. They are entered upon creation and exited upon being dropped." + ); + // self.dispose_scope_root(); + self.exit(); + let (annex_ptr, _create_param_allocations) = + self.prepare_annex_for_dispose(); + // Drain finalizers registered up to this point, before V8's final + // teardown GC has a chance to fire weak callbacks that need the + // annex. + Isolate::run_remaining_guaranteed_finalizers(annex_ptr); + Platform::notify_isolate_shutdown(&get_current_platform(), self); + // V8's final teardown runs here. `ANNEX_SLOT` still references the + // (drained) annex, so any re-entrant access from weak callbacks or + // embedder code resolves normally instead of panicking on a null + // slot. + self.dispose(); + // Drain finalizers V8 may have registered during teardown, then free + // the annex allocation. V8 has fully disposed the isolate, so its + // embedder data storage no longer exists and `ANNEX_SLOT` needs no + // explicit clearing. + Isolate::finish_annex_dispose(annex_ptr); + } + } +} + +impl OwnedIsolate { + /// Creates a snapshot data blob. + /// This must not be called from within a handle scope. + /// + /// # Panics + /// + /// Panics if the isolate was not created using [`Isolate::snapshot_creator`] + #[inline(always)] + pub fn create_blob( + mut self, + function_code_handling: FunctionCodeHandling, + ) -> Option { + let mut snapshot_creator = + self.get_annex_mut().maybe_snapshot_creator.take().unwrap(); + + // create_param_allocations is needed during CreateBlob + // so v8 can read external references + let _create_param_allocations = unsafe { + // self.dispose_scope_root(); + self.dispose_annex() + }; + + // The isolate is owned by the snapshot creator; we need to forget it + // here as the snapshot creator will drop it when running the destructor. + std::mem::forget(self); + snapshot_creator.create_blob(function_code_handling) + } +} + +impl Deref for OwnedIsolate { + type Target = Isolate; + fn deref(&self) -> &Self::Target { + unsafe { + std::mem::transmute::<&NonNull, &Isolate>(&self.cxx_isolate) + } + } +} + +impl DerefMut for OwnedIsolate { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { + std::mem::transmute::<&mut NonNull, &mut Isolate>( + &mut self.cxx_isolate, + ) + } + } +} + +impl AsMut for OwnedIsolate { + fn as_mut(&mut self) -> &mut Isolate { + self + } +} + +impl AsMut for Isolate { + fn as_mut(&mut self) -> &mut Isolate { + self + } +} + +/// Collection of V8 heap information. +/// +/// Instances of this class can be passed to v8::Isolate::GetHeapStatistics to +/// get heap statistics from V8. +pub struct HeapStatistics(v8__HeapStatistics); + +impl HeapStatistics { + #[inline(always)] + pub fn total_heap_size(&self) -> usize { + self.0.total_heap_size_ + } + + #[inline(always)] + pub fn total_heap_size_executable(&self) -> usize { + self.0.total_heap_size_executable_ + } + + #[inline(always)] + pub fn total_physical_size(&self) -> usize { + self.0.total_physical_size_ + } + + #[inline(always)] + pub fn total_available_size(&self) -> usize { + self.0.total_available_size_ + } + + #[inline(always)] + pub fn total_global_handles_size(&self) -> usize { + self.0.total_global_handles_size_ + } + + #[inline(always)] + pub fn used_global_handles_size(&self) -> usize { + self.0.used_global_handles_size_ + } + + #[inline(always)] + pub fn used_heap_size(&self) -> usize { + self.0.used_heap_size_ + } + + #[inline(always)] + pub fn heap_size_limit(&self) -> usize { + self.0.heap_size_limit_ + } + + #[inline(always)] + pub fn malloced_memory(&self) -> usize { + self.0.malloced_memory_ + } + + #[inline(always)] + pub fn external_memory(&self) -> usize { + self.0.external_memory_ + } + + #[inline(always)] + pub fn peak_malloced_memory(&self) -> usize { + self.0.peak_malloced_memory_ + } + + #[inline(always)] + pub fn number_of_native_contexts(&self) -> usize { + self.0.number_of_native_contexts_ + } + + #[inline(always)] + pub fn number_of_detached_contexts(&self) -> usize { + self.0.number_of_detached_contexts_ + } + + /// Returns the total number of bytes allocated since the Isolate was created. + /// This includes all heap objects allocated in any space (new, old, code, + /// etc.). + #[inline(always)] + pub fn total_allocated_bytes(&self) -> u64 { + self.0.total_allocated_bytes_ + } + + /// Returns a 0/1 boolean, which signifies whether the V8 overwrite heap + /// garbage with a bit pattern. + #[inline(always)] + pub fn does_zap_garbage(&self) -> bool { + self.0.does_zap_garbage_ + } +} + +pub struct HeapSpaceStatistics(v8__HeapSpaceStatistics); + +impl HeapSpaceStatistics { + pub fn space_name(&self) -> &'static CStr { + unsafe { CStr::from_ptr(self.0.space_name_) } + } + + pub fn space_size(&self) -> usize { + self.0.space_size_ + } + + pub fn space_used_size(&self) -> usize { + self.0.space_used_size_ + } + + pub fn space_available_size(&self) -> usize { + self.0.space_available_size_ + } + + pub fn physical_space_size(&self) -> usize { + self.0.physical_space_size_ + } +} + +pub struct HeapCodeStatistics(v8__HeapCodeStatistics); + +impl HeapCodeStatistics { + pub fn code_and_metadata_size(&self) -> usize { + self.0.code_and_metadata_size_ + } + + pub fn bytecode_and_metadata_size(&self) -> usize { + self.0.bytecode_and_metadata_size_ + } + + pub fn external_script_source_size(&self) -> usize { + self.0.external_script_source_size_ + } + + pub fn cpu_profiler_metadata_size(&self) -> usize { + self.0.cpu_profiler_metadata_size_ + } +} + +impl<'s, F> MapFnFrom for PrepareStackTraceCallback<'s> +where + F: UnitType + + for<'a> Fn( + &mut PinScope<'s, 'a>, + Local<'s, Value>, + Local<'s, Array>, + ) -> Local<'s, Value>, +{ + // Windows x64 ABI: MaybeLocal returned on the stack. + #[cfg(target_os = "windows")] + fn mapping() -> Self { + let f = |ret_ptr, context, error, sites| { + let scope = pin!(unsafe { CallbackScope::new(context) }); + let mut scope: crate::PinnedRef = scope.init(); + let r = (F::get())(&mut scope, error, sites); + unsafe { std::ptr::write(ret_ptr, &*r as *const _) }; + ret_ptr + }; + f.to_c_fn() + } + + // System V ABI + #[cfg(not(target_os = "windows"))] + fn mapping() -> Self { + let f = |context, error, sites| { + let scope = pin!(unsafe { CallbackScope::new(context) }); + let mut scope: crate::PinnedRef = scope.init(); + + let r = (F::get())(&mut scope, error, sites); + PrepareStackTraceCallbackRet(&*r as *const _) + }; + f.to_c_fn() + } +} + +/// A special hasher that is optimized for hashing `std::any::TypeId` values. +/// `TypeId` values are actually 64-bit values which themselves come out of some +/// hash function, so it's unnecessary to shuffle their bits any further. +#[derive(Clone, Default)] +pub(crate) struct TypeIdHasher { + state: Option, +} + +impl Hasher for TypeIdHasher { + fn write(&mut self, _bytes: &[u8]) { + panic!("TypeIdHasher::write() called unexpectedly"); + } + + #[inline] + fn write_u64(&mut self, value: u64) { + // The internal hash function of TypeId only takes the bottom 64-bits, even on versions + // of Rust that use a 128-bit TypeId. + let prev_state = self.state.replace(value); + debug_assert_eq!(prev_state, None); + } + + #[inline] + fn finish(&self) -> u64 { + self.state.unwrap() + } +} + +/// Factory for instances of `TypeIdHasher`. This is the type that one would +/// pass to the constructor of some map/set type in order to make it use +/// `TypeIdHasher` instead of the default hasher implementation. +#[derive(Copy, Clone, Default)] +pub(crate) struct BuildTypeIdHasher; + +impl BuildHasher for BuildTypeIdHasher { + type Hasher = TypeIdHasher; + + #[inline] + fn build_hasher(&self) -> Self::Hasher { + Default::default() + } +} + +const _: () = { + assert!( + size_of::() == size_of::() + || size_of::() == size_of::() + ); + assert!( + align_of::() == align_of::() + || align_of::() == align_of::() + ); +}; + +pub(crate) struct RawSlot { + data: RawSlotData, + dtor: Option, +} + +type RawSlotData = MaybeUninit; +type RawSlotDtor = unsafe fn(&mut RawSlotData) -> (); + +impl RawSlot { + #[inline] + pub fn new(value: T) -> Self { + if Self::needs_box::() { + Self::new_internal(Box::new(value)) + } else { + Self::new_internal(value) + } + } + + // SAFETY: a valid value of type `T` must haven been stored in the slot + // earlier. There is no verification that the type param provided by the + // caller is correct. + #[inline] + pub unsafe fn borrow(&self) -> &T { + unsafe { + if Self::needs_box::() { + &*(self.data.as_ptr() as *const Box) + } else { + &*(self.data.as_ptr() as *const T) + } + } + } + + // Safety: see [`RawSlot::borrow`]. + #[inline] + pub unsafe fn borrow_mut(&mut self) -> &mut T { + unsafe { + if Self::needs_box::() { + &mut *(self.data.as_mut_ptr() as *mut Box) + } else { + &mut *(self.data.as_mut_ptr() as *mut T) + } + } + } + + // Safety: see [`RawSlot::borrow`]. + #[inline] + pub unsafe fn into_inner(self) -> T { + unsafe { + let value = if Self::needs_box::() { + *std::ptr::read(self.data.as_ptr() as *mut Box) + } else { + std::ptr::read(self.data.as_ptr() as *mut T) + }; + forget(self); + value + } + } + + const fn needs_box() -> bool { + size_of::() > size_of::() + || align_of::() > align_of::() + } + + #[inline] + fn new_internal(value: B) -> Self { + assert!(!Self::needs_box::()); + let mut self_ = Self { + data: RawSlotData::zeroed(), + dtor: None, + }; + unsafe { + ptr::write(self_.data.as_mut_ptr() as *mut B, value); + } + if needs_drop::() { + self_.dtor.replace(Self::drop_internal::); + }; + self_ + } + + // SAFETY: a valid value of type `T` or `Box` must be stored in the slot. + unsafe fn drop_internal(data: &mut RawSlotData) { + assert!(!Self::needs_box::()); + unsafe { + drop_in_place(data.as_mut_ptr() as *mut B); + } + } +} + +impl Drop for RawSlot { + fn drop(&mut self) { + if let Some(dtor) = self.dtor { + unsafe { dtor(&mut self.data) }; + } + } +} + +impl AsRef for OwnedIsolate { + fn as_ref(&self) -> &Isolate { + unsafe { Isolate::from_raw_ref(&self.cxx_isolate) } + } +} +impl AsRef for Isolate { + fn as_ref(&self) -> &Isolate { + self + } +} diff --git a/crates/v8/src/isolate_create_params.rs b/crates/v8/src/isolate_create_params.rs new file mode 100644 index 0000000000..fa13251d87 --- /dev/null +++ b/crates/v8/src/isolate_create_params.rs @@ -0,0 +1,529 @@ +use crate::ExternalReference; +use crate::array_buffer; +use crate::array_buffer::Allocator as ArrayBufferAllocator; +use crate::cppgc::Heap; +use crate::snapshot::RawStartupData; +use crate::snapshot::StartupData; +use crate::support::Opaque; +use crate::support::SharedPtr; +use crate::support::UniqueRef; +use crate::support::char; +use crate::support::intptr_t; + +use std::any::Any; +use std::borrow::Cow; +use std::iter::once; +use std::mem::MaybeUninit; +use std::mem::size_of; +use std::ptr::null; + +/// Should return a pointer to memory that persists for the lifetime of the +/// isolate. +pub type CounterLookupCallback = + unsafe extern "C" fn(name: *const char) -> *mut i32; + +/// Initial configuration parameters for a new Isolate. +#[must_use] +#[derive(Debug, Default)] +pub struct CreateParams { + raw: raw::CreateParams, + allocations: CreateParamAllocations, +} + +impl CreateParams { + /// Enables the host application to provide a mechanism for recording + /// statistics counters. + pub fn counter_lookup_callback( + mut self, + callback: CounterLookupCallback, + ) -> Self { + self.raw.counter_lookup_callback = Some(callback); + self + } + + /// Explicitly specify a startup snapshot blob. + pub fn snapshot_blob(mut self, data: StartupData) -> Self { + let header = Box::new(RawStartupData { + data: data.as_ptr() as _, + raw_size: data.len() as _, + }); + self.raw.snapshot_blob = &*header; + self.allocations.snapshot_blob_data = Some(data); + self.allocations.snapshot_blob_header = Some(header); + self + } + + /// The ArrayBuffer::ArrayBufferAllocator to use for allocating and freeing + /// the backing store of ArrayBuffers. + pub fn array_buffer_allocator( + mut self, + array_buffer_allocator: impl Into>, + ) -> Self { + self.raw.array_buffer_allocator_shared = array_buffer_allocator.into(); + self + } + + /// Check if `array_buffer_allocator` has already been called. Useful to some + /// embedders that might want to set an allocator but not overwrite if one + /// was already set by a user. + pub fn has_set_array_buffer_allocator(&self) -> bool { + !self.raw.array_buffer_allocator_shared.is_null() + } + + /// Specifies an optional nullptr-terminated array of raw addresses in the + /// embedder that V8 can match against during serialization and use for + /// deserialization. This array and its content must stay valid for the + /// entire lifetime of the isolate. + pub fn external_references( + mut self, + ext_refs: Cow<'static, [ExternalReference]>, + ) -> Self { + let ext_refs = if ext_refs.last() + == Some(&ExternalReference { + pointer: std::ptr::null_mut(), + }) { + ext_refs + } else { + Cow::from( + ext_refs + .into_owned() + .into_iter() + .chain(once(ExternalReference { + pointer: std::ptr::null_mut(), + })) + .collect::>(), + ) + }; + + self.allocations.external_references = Some(ext_refs); + self.raw.external_references = self + .allocations + .external_references + .as_ref() + .map(|c| c.as_ptr() as _) + .unwrap_or_else(null); + + self + } + + /// Whether calling Atomics.wait (a function that may block) is allowed in + /// this isolate. This can also be configured via SetAllowAtomicsWait. + pub fn allow_atomics_wait(mut self, value: bool) -> Self { + self.raw.allow_atomics_wait = value; + self + } + + /// Configures the constraints with reasonable default values based on the + /// provided lower and upper bounds. + /// + /// By default V8 starts with a small heap and dynamically grows it to match + /// the set of live objects. This may lead to ineffective garbage collections + /// at startup if the live set is large. Setting the initial heap size avoids + /// such garbage collections. Note that this does not affect young generation + /// garbage collections. + /// + /// When the heap size approaches `max`, V8 will perform series of + /// garbage collections and invoke the + /// [NearHeapLimitCallback](struct.Isolate.html#method.add_near_heap_limit_callback). + /// If the garbage collections do not help and the callback does not + /// increase the limit, then V8 will crash with V8::FatalProcessOutOfMemory. + /// + /// The heap size includes both the young and the old generation. + /// + /// # Arguments + /// + /// * `initial` - The initial heap size or zero in bytes + /// * `max` - The hard limit for the heap size in bytes + pub fn heap_limits(mut self, initial: usize, max: usize) -> Self { + self + .raw + .constraints + .configure_defaults_from_heap_size(initial, max); + self + } + + /// Configures the constraints with reasonable default values based on the capabilities + /// of the current device the VM is running on. + /// + /// By default V8 starts with a small heap and dynamically grows it to match + /// the set of live objects. This may lead to ineffective garbage collections + /// at startup if the live set is large. Setting the initial heap size avoids + /// such garbage collections. Note that this does not affect young generation + /// garbage collections. + /// + /// When the heap size approaches its maximum, V8 will perform series of + /// garbage collections and invoke the + /// [NearHeapLimitCallback](struct.Isolate.html#method.add_near_heap_limit_callback). + /// If the garbage collections do not help and the callback does not + /// increase the limit, then V8 will crash with V8::FatalProcessOutOfMemory. + /// + /// # Arguments + /// + /// * `physical_memory` - The total amount of physical memory on the current device, in bytes. + /// * `virtual_memory_limit` - The amount of virtual memory on the current device, in bytes, or zero, if there is no limit. + pub fn heap_limits_from_system_memory( + mut self, + physical_memory: u64, + virtual_memory_limit: u64, + ) -> Self { + self + .raw + .constraints + .configure_defaults(physical_memory, virtual_memory_limit); + self + } + + /// Returns the maximum size of the old generation in bytes. + pub fn max_old_generation_size_in_bytes(&self) -> usize { + self.raw.constraints.max_old_generation_size_in_bytes() + } + + /// Sets the maximum size of the old generation in bytes. When the old + /// generation approaches this limit, V8 will perform series of garbage + /// collections and invoke the NearHeapLimitCallback. + pub fn set_max_old_generation_size_in_bytes(mut self, limit: usize) -> Self { + self + .raw + .constraints + .set_max_old_generation_size_in_bytes(limit); + self + } + + /// Returns the maximum size of the young generation in bytes. + pub fn max_young_generation_size_in_bytes(&self) -> usize { + self.raw.constraints.max_young_generation_size_in_bytes() + } + + /// Sets the maximum size of the young generation in bytes. The young + /// generation consists of two semi-spaces and a large object space. This + /// affects frequency of Scavenge garbage collections. + pub fn set_max_young_generation_size_in_bytes( + mut self, + limit: usize, + ) -> Self { + self + .raw + .constraints + .set_max_young_generation_size_in_bytes(limit); + self + } + + /// Returns the code range size in bytes. + pub fn code_range_size_in_bytes(&self) -> usize { + self.raw.constraints.code_range_size_in_bytes() + } + + /// Sets the amount of virtual memory reserved for generated code in bytes. + /// This is relevant for 64-bit architectures that rely on code range for + /// calls in code. + pub fn set_code_range_size_in_bytes(mut self, limit: usize) -> Self { + self.raw.constraints.set_code_range_size_in_bytes(limit); + self + } + + /// Returns the stack limit (the address beyond which the VM's stack may + /// not grow), or null if not set. + pub fn stack_limit(&self) -> *mut u32 { + self.raw.constraints.stack_limit() + } + + /// Sets the address beyond which the VM's stack may not grow. + /// + /// # Safety + /// + /// The caller must ensure that the pointer remains valid for the lifetime + /// of the isolate, and points to a valid stack boundary. + pub unsafe fn set_stack_limit(mut self, value: *mut u32) -> Self { + self.raw.constraints.set_stack_limit(value); + self + } + + /// Returns the initial size of the old generation in bytes. + pub fn initial_old_generation_size_in_bytes(&self) -> usize { + self.raw.constraints.initial_old_generation_size_in_bytes() + } + + /// Sets the initial size of the old generation in bytes. Setting the + /// initial size avoids ineffective garbage collections at startup if the + /// live set is large. + pub fn set_initial_old_generation_size_in_bytes( + mut self, + initial_size: usize, + ) -> Self { + self + .raw + .constraints + .set_initial_old_generation_size_in_bytes(initial_size); + self + } + + /// Returns the initial size of the young generation in bytes. + pub fn initial_young_generation_size_in_bytes(&self) -> usize { + self + .raw + .constraints + .initial_young_generation_size_in_bytes() + } + + /// Sets the initial size of the young generation in bytes. + pub fn set_initial_young_generation_size_in_bytes( + mut self, + initial_size: usize, + ) -> Self { + self + .raw + .constraints + .set_initial_young_generation_size_in_bytes(initial_size); + self + } + + /// A CppHeap used to construct the Isolate. V8 takes ownership of the + /// CppHeap passed this way. + pub fn cpp_heap(mut self, heap: UniqueRef) -> Self { + self.raw.cpp_heap = heap.into_raw(); + self + } + + pub(crate) fn finalize(mut self) -> (raw::CreateParams, Box) { + if self.raw.array_buffer_allocator_shared.is_null() { + self = self.array_buffer_allocator(array_buffer::new_default_allocator()); + } + let Self { raw, allocations } = self; + (raw, Box::new(allocations)) + } +} + +#[derive(Debug, Default)] +struct CreateParamAllocations { + // Owner of the snapshot data buffer itself. + snapshot_blob_data: Option, + // Owns `struct StartupData` which contains just the (ptr, len) tuple in V8's + // preferred format. We have to heap allocate this because we need to put a + // stable pointer to it in `CreateParams`. + snapshot_blob_header: Option>, + external_references: Option>, +} + +#[test] +fn create_param_defaults() { + let params = CreateParams::default(); + assert!(params.raw.allow_atomics_wait); +} + +pub(crate) mod raw { + use super::*; + + #[repr(C)] + #[derive(Debug)] + pub(crate) struct CreateParams { + pub code_event_handler: *const Opaque, // JitCodeEventHandler + pub constraints: ResourceConstraints, + pub snapshot_blob: *const RawStartupData, + pub counter_lookup_callback: Option, + pub create_histogram_callback: *const Opaque, // CreateHistogramCallback + pub add_histogram_sample_callback: *const Opaque, // AddHistogramSampleCallback + pub array_buffer_allocator: *mut ArrayBufferAllocator, + pub array_buffer_allocator_shared: SharedPtr, + pub external_references: *const intptr_t, + pub allow_atomics_wait: bool, + _fatal_error_handler: *const Opaque, // FatalErrorCallback + _oom_error_handler: *const Opaque, // OOMErrorCallback + pub cpp_heap: *const Heap, + } + + unsafe extern "C" { + fn v8__Isolate__CreateParams__CONSTRUCT( + buf: *mut MaybeUninit, + ); + fn v8__Isolate__CreateParams__SIZEOF() -> usize; + } + + impl Default for CreateParams { + fn default() -> Self { + let size = unsafe { v8__Isolate__CreateParams__SIZEOF() }; + assert_eq!(size, size_of::()); + let mut buf = MaybeUninit::::uninit(); + unsafe { v8__Isolate__CreateParams__CONSTRUCT(&mut buf) }; + unsafe { buf.assume_init() } + } + } + + #[repr(C)] + #[derive(Debug)] + pub(crate) struct ResourceConstraints { + code_range_size_: usize, + max_old_generation_size_: usize, + max_young_generation_size_: usize, + initial_old_generation_size_: usize, + initial_young_generation_size_: usize, + physical_memory_size_: u64, + stack_limit_: *mut u32, + } + + unsafe extern "C" { + fn v8__ResourceConstraints__ConfigureDefaultsFromHeapSize( + constraints: *mut ResourceConstraints, + initial_heap_size_in_bytes: usize, + maximum_heap_size_in_bytes: usize, + ); + fn v8__ResourceConstraints__ConfigureDefaults( + constraints: *mut ResourceConstraints, + physical_memory: u64, + virtual_memory_limit: u64, + ); + fn v8__ResourceConstraints__max_old_generation_size_in_bytes( + constraints: *const ResourceConstraints, + ) -> usize; + fn v8__ResourceConstraints__set_max_old_generation_size_in_bytes( + constraints: *mut ResourceConstraints, + limit: usize, + ); + fn v8__ResourceConstraints__max_young_generation_size_in_bytes( + constraints: *const ResourceConstraints, + ) -> usize; + fn v8__ResourceConstraints__set_max_young_generation_size_in_bytes( + constraints: *mut ResourceConstraints, + limit: usize, + ); + fn v8__ResourceConstraints__code_range_size_in_bytes( + constraints: *const ResourceConstraints, + ) -> usize; + fn v8__ResourceConstraints__set_code_range_size_in_bytes( + constraints: *mut ResourceConstraints, + limit: usize, + ); + fn v8__ResourceConstraints__stack_limit( + constraints: *const ResourceConstraints, + ) -> *mut u32; + fn v8__ResourceConstraints__set_stack_limit( + constraints: *mut ResourceConstraints, + value: *mut u32, + ); + fn v8__ResourceConstraints__initial_old_generation_size_in_bytes( + constraints: *const ResourceConstraints, + ) -> usize; + fn v8__ResourceConstraints__set_initial_old_generation_size_in_bytes( + constraints: *mut ResourceConstraints, + initial_size: usize, + ); + fn v8__ResourceConstraints__initial_young_generation_size_in_bytes( + constraints: *const ResourceConstraints, + ) -> usize; + fn v8__ResourceConstraints__set_initial_young_generation_size_in_bytes( + constraints: *mut ResourceConstraints, + initial_size: usize, + ); + } + + impl ResourceConstraints { + pub fn configure_defaults_from_heap_size( + &mut self, + initial_heap_size_in_bytes: usize, + maximum_heap_size_in_bytes: usize, + ) { + unsafe { + v8__ResourceConstraints__ConfigureDefaultsFromHeapSize( + self, + initial_heap_size_in_bytes, + maximum_heap_size_in_bytes, + ); + }; + } + + pub fn configure_defaults( + &mut self, + physical_memory: u64, + virtual_memory_limit: u64, + ) { + unsafe { + v8__ResourceConstraints__ConfigureDefaults( + self, + physical_memory, + virtual_memory_limit, + ); + } + } + + pub fn max_old_generation_size_in_bytes(&self) -> usize { + unsafe { v8__ResourceConstraints__max_old_generation_size_in_bytes(self) } + } + + pub fn set_max_old_generation_size_in_bytes(&mut self, limit: usize) { + unsafe { + v8__ResourceConstraints__set_max_old_generation_size_in_bytes( + self, limit, + ); + } + } + + pub fn max_young_generation_size_in_bytes(&self) -> usize { + unsafe { + v8__ResourceConstraints__max_young_generation_size_in_bytes(self) + } + } + + pub fn set_max_young_generation_size_in_bytes(&mut self, limit: usize) { + unsafe { + v8__ResourceConstraints__set_max_young_generation_size_in_bytes( + self, limit, + ); + } + } + + pub fn code_range_size_in_bytes(&self) -> usize { + unsafe { v8__ResourceConstraints__code_range_size_in_bytes(self) } + } + + pub fn set_code_range_size_in_bytes(&mut self, limit: usize) { + unsafe { + v8__ResourceConstraints__set_code_range_size_in_bytes(self, limit); + } + } + + pub fn stack_limit(&self) -> *mut u32 { + unsafe { v8__ResourceConstraints__stack_limit(self) } + } + + pub fn set_stack_limit(&mut self, value: *mut u32) { + unsafe { + v8__ResourceConstraints__set_stack_limit(self, value); + } + } + + pub fn initial_old_generation_size_in_bytes(&self) -> usize { + unsafe { + v8__ResourceConstraints__initial_old_generation_size_in_bytes(self) + } + } + + pub fn set_initial_old_generation_size_in_bytes( + &mut self, + initial_size: usize, + ) { + unsafe { + v8__ResourceConstraints__set_initial_old_generation_size_in_bytes( + self, + initial_size, + ); + } + } + + pub fn initial_young_generation_size_in_bytes(&self) -> usize { + unsafe { + v8__ResourceConstraints__initial_young_generation_size_in_bytes(self) + } + } + + pub fn set_initial_young_generation_size_in_bytes( + &mut self, + initial_size: usize, + ) { + unsafe { + v8__ResourceConstraints__set_initial_young_generation_size_in_bytes( + self, + initial_size, + ); + } + } + } +} diff --git a/crates/v8/src/isolate_group.rs b/crates/v8/src/isolate_group.rs new file mode 100644 index 0000000000..118dec03f3 --- /dev/null +++ b/crates/v8/src/isolate_group.rs @@ -0,0 +1,178 @@ +// Copyright 2018-2025 the Deno authors. All rights reserved. MIT license. + +use crate::Isolate; +use crate::isolate::RealIsolate; +use crate::support::Opaque; +use std::fmt; + +#[repr(C)] +struct InternalIsolateGroup(Opaque); + +unsafe extern "C" { + fn v8__IsolateGroup__GetDefault() -> *const InternalIsolateGroup; + fn v8__IsolateGroup__CanCreateNewGroups() -> bool; + fn v8__IsolateGroup__Create() -> *const InternalIsolateGroup; + fn v8__Isolate__GetGroup( + isolate: *const RealIsolate, + ) -> *const InternalIsolateGroup; + fn v8__IsolateGroup__CLONE( + this: *const IsolateGroup, + ) -> *const InternalIsolateGroup; + fn v8__IsolateGroup__DESTRUCT(this: *mut IsolateGroup); + fn v8__IsolateGroup__EQ( + this: *const IsolateGroup, + other: *const IsolateGroup, + ) -> bool; +} + +/// The set of V8 isolates in a process is partitioned into groups. Each group +/// has its own sandbox (if V8 was configured with support for the sandbox) and +/// pointer-compression cage (if configured with pointer compression). +/// +/// By default, all isolates are placed in the same group. This is the most +/// efficient configuration in terms of speed and memory use. However, with +/// pointer compression enabled, total heap usage of isolates in a group cannot +/// exceed 4 GB, not counting array buffers and other off-heap storage. Using +/// multiple isolate groups can allow embedders to allocate more than 4GB of +/// objects with pointer compression enabled, if the embedder's use case can +/// span multiple isolates. +/// +/// Creating an isolate group reserves a range of virtual memory addresses. A +/// group's memory mapping will be released when the last isolate in the group +/// is disposed, and there are no more live `IsolateGroup` objects that refer to +/// it. +/// +/// Note that isolate groups are reference counted, and this type is a reference +/// to one. Cloning it produces another reference to the same group; comparing +/// two values tells you whether they refer to the same group. +/// +/// Note that it's not going to be possible to pass shared JS objects across an +/// `IsolateGroup` boundary. +/// +/// # Lifetime +/// +/// An isolate holds its own reference to the group it was created in, so the +/// group outlives its isolates whether or not you keep this handle around. +/// +/// Do not, however, hold one of these across [`crate::V8::dispose`]. Tearing +/// down V8 asserts that the only remaining reference to the default group is +/// V8's own, and a live handle here makes that assertion fail, aborting the +/// process. Dropping a handle *after* `dispose` is equally unsupported, since +/// releasing the last reference reaches for a page allocator that is gone by +/// then. Drop every `IsolateGroup` before shutting V8 down. +/// +/// # Build configuration +/// +/// Creating groups beyond the default one requires V8 to be built with pointer +/// compression in multi-cage mode, i.e. `v8_enable_pointer_compression=true` +/// together with `v8_enable_pointer_compression_shared_cage=false`. That +/// configuration is mutually exclusive with the V8 sandbox. Use +/// [`IsolateGroup::can_create_new_groups`] to find out whether the V8 this +/// crate was linked against supports it. +#[repr(C)] +pub struct IsolateGroup(*const InternalIsolateGroup); + +// SAFETY: The group's reference count is a `std::atomic`, so acquiring and +// releasing references (which is all cloning and dropping do) is thread safe, +// and V8 supports isolates belonging to one group living on different threads. +unsafe impl Send for IsolateGroup {} +// SAFETY: `&IsolateGroup` only ever reads the pointer, either to compare it or +// to hand it to V8 when creating an isolate. +unsafe impl Sync for IsolateGroup {} + +impl IsolateGroup { + /// Returns true if new isolate groups can be created at run-time, or false if + /// all isolates must be in the same group. + /// + /// This is a property of how V8 was built, not of the running process, so the + /// answer never changes over the lifetime of the program. + pub fn can_create_new_groups() -> bool { + unsafe { v8__IsolateGroup__CanCreateNewGroups() } + } + + /// Gets the default isolate group. If this V8's build configuration only + /// supports a single group, this is a reference to that single group. + /// Otherwise this is a group like any other, distinguished only in that it is + /// the first group. + /// + /// # Panics + /// + /// Panics if V8 has not been initialized. V8 only creates the default group + /// during initialization, and would dereference a null pointer before then. + pub fn get_default() -> Self { + crate::V8::assert_initialized(); + IsolateGroup(unsafe { v8__IsolateGroup__GetDefault() }) + } + + /// Creates a new isolate group. + /// + /// # Panics + /// + /// Panics if this V8's build configuration only supports a single group. + /// Check [`IsolateGroup::can_create_new_groups`] first, or use + /// [`IsolateGroup::try_create`], which returns `None` instead. Also panics + /// if V8 has not been initialized. + pub fn create() -> Self { + Self::try_create().expect( + "V8 was not built with support for multiple isolate groups; this \ + requires v8_enable_pointer_compression=true together with \ + v8_enable_pointer_compression_shared_cage=false", + ) + } + + /// Creates a new isolate group, or returns `None` if this V8's build + /// configuration only supports a single group. + /// + /// # Panics + /// + /// Panics if V8 has not been initialized; creating a group needs the + /// platform's virtual address space. + pub fn try_create() -> Option { + crate::V8::assert_initialized(); + // V8 aborts the process rather than failing gracefully, so the check has to + // happen before the call rather than on its result. + if !Self::can_create_new_groups() { + return None; + } + Some(IsolateGroup(unsafe { v8__IsolateGroup__Create() })) + } +} + +impl Isolate { + /// Returns the [`IsolateGroup`] this isolate belongs to. + pub fn get_group(&self) -> IsolateGroup { + IsolateGroup(unsafe { v8__Isolate__GetGroup(self.as_real_ptr()) }) + } +} + +impl Default for IsolateGroup { + fn default() -> Self { + IsolateGroup::get_default() + } +} + +impl Clone for IsolateGroup { + fn clone(&self) -> Self { + IsolateGroup(unsafe { v8__IsolateGroup__CLONE(self) }) + } +} + +impl Drop for IsolateGroup { + fn drop(&mut self) { + unsafe { v8__IsolateGroup__DESTRUCT(self) } + } +} + +impl Eq for IsolateGroup {} + +impl PartialEq for IsolateGroup { + fn eq(&self, other: &Self) -> bool { + unsafe { v8__IsolateGroup__EQ(self, other) } + } +} + +impl fmt::Debug for IsolateGroup { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("IsolateGroup").field(&self.0).finish() + } +} diff --git a/crates/v8/src/json.rs b/crates/v8/src/json.rs new file mode 100644 index 0000000000..cc3870433d --- /dev/null +++ b/crates/v8/src/json.rs @@ -0,0 +1,46 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. +//! A JSON Parser and Stringifier. + +use crate::Context; +use crate::Local; +use crate::String; +use crate::Value; +use crate::scope::PinScope; + +unsafe extern "C" { + fn v8__JSON__Parse( + context: *const Context, + json_string: *const String, + ) -> *const Value; + fn v8__JSON__Stringify( + context: *const Context, + json_object: *const Value, + ) -> *const String; +} + +/// Tries to parse the string `json_string` and returns it as value if +/// successful. +#[inline(always)] +pub fn parse<'s>( + scope: &PinScope<'s, '_>, + json_string: Local<'_, String>, +) -> Option> { + unsafe { + scope + .cast_local(|sd| v8__JSON__Parse(sd.get_current_context(), &*json_string)) + } +} + +/// Tries to stringify the JSON-serializable object `json_object` and returns +/// it as string if successful. +#[inline(always)] +pub fn stringify<'s>( + scope: &PinScope<'s, '_>, + json_object: Local<'_, Value>, +) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__JSON__Stringify(sd.get_current_context(), &*json_object) + }) + } +} diff --git a/crates/v8/src/lib.rs b/crates/v8/src/lib.rs new file mode 100644 index 0000000000..ecec93b079 --- /dev/null +++ b/crates/v8/src/lib.rs @@ -0,0 +1,243 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +//! # Example +//! +//! ```rust +//! let platform = v8::new_default_platform(0, false).make_shared(); +//! v8::V8::initialize_platform(platform); +//! v8::V8::initialize(); +//! +//! let isolate = &mut v8::Isolate::new(Default::default()); +//! +//! let scope = std::pin::pin!(v8::HandleScope::new(isolate)); +//! let scope = &mut scope.init(); +//! let context = v8::Context::new(scope, Default::default()); +//! let scope = &mut v8::ContextScope::new(scope, context); +//! +//! let code = v8::String::new(scope, "'Hello' + ' World!'").unwrap(); +//! println!("javascript code: {}", code.to_rust_string_lossy(scope)); +//! +//! let script = v8::Script::compile(scope, code, None).unwrap(); +//! let result = script.run(scope).unwrap(); +//! let result = result.to_string(scope).unwrap(); +//! println!("result: {}", result.to_rust_string_lossy(scope)); +//! ``` + +#![allow(clippy::missing_safety_doc)] + +#[macro_use] +extern crate bitflags; +extern crate temporal_capi; + +mod array_buffer; +mod array_buffer_view; +mod bigint; +mod binding; +mod context; +pub use context::ContextOptions; +pub mod cppgc; +mod data; +mod date; +mod exception; +mod external; +mod external_references; +pub mod fast_api; +mod fixed_array; +mod function; +mod gc; +mod get_property_names_args_builder; +mod handle; +pub mod icu; +mod isolate; +mod isolate_create_params; +mod isolate_group; +mod locker; +mod microtask; +mod module; +mod name; +mod number; +mod object; +mod platform; +mod primitive_array; +mod primitives; +mod private; +mod promise; +mod property_attribute; +mod property_descriptor; +mod property_filter; +mod property_handler_flags; +mod proxy; +mod regexp; +mod scope; +mod script; +mod script_or_module; +mod shared_array_buffer; +mod snapshot; +mod string; +mod support; +mod symbol; +mod template; +mod typed_array; +mod unbound_module_script; +mod unbound_script; +mod value; +mod value_deserializer; +mod value_serializer; +mod wasm; + +pub mod crdtp; +pub mod inspector; +pub mod json; +pub mod script_compiler; +pub mod simdutf; +// This module is intentionally named "V8" rather than "v8" to match the +// C++ namespace "v8::V8". +#[allow(non_snake_case)] +pub mod V8; + +pub use array_buffer::*; +pub use data::*; +pub use exception::*; +pub use external_references::ExternalReference; +pub use function::*; +pub use gc::*; +pub use get_property_names_args_builder::*; +pub use handle::Eternal; +pub use handle::Global; +pub use handle::Handle; +pub use handle::Local; +pub use handle::SealedLocal; +pub use handle::TracedReference; +pub use handle::Weak; +pub use isolate::ContextDependants; +pub use isolate::GarbageCollectionType; +pub use isolate::HeapCodeStatistics; +pub use isolate::HeapSpaceStatistics; +pub use isolate::HeapStatistics; +pub use isolate::HostCreateShadowRealmContextCallback; +pub use isolate::HostImportModuleDynamicallyCallback; +pub use isolate::HostImportModuleWithPhaseDynamicallyCallback; +pub use isolate::HostInitializeImportMetaObjectCallback; +pub use isolate::IntoSharedError; +pub use isolate::IntoSharedErrorKind; +pub use isolate::Isolate; +pub use isolate::IsolateHandle; +pub use isolate::MemoryPressureLevel; +pub use isolate::MessageCallback; +pub use isolate::MessageErrorLevel; +pub use isolate::MicrotasksPolicy; +pub use isolate::ModifyCodeGenerationFromStringsCallback; +pub use isolate::ModifyCodeGenerationFromStringsResult; +pub use isolate::ModuleImportPhase; +pub use isolate::NearHeapLimitCallback; +pub use isolate::OomDetails; +pub use isolate::OomErrorCallback; +pub use isolate::OwnedIsolate; +pub use isolate::PromiseHook; +pub use isolate::PromiseHookType; +pub use isolate::PromiseRejectCallback; +pub use isolate::RealIsolate; +pub use isolate::TimeZoneDetection; +pub use isolate::UseCounterCallback; +pub use isolate::UseCounterFeature; +pub use isolate::WasmAsyncSuccess; +pub use isolate_create_params::CreateParams; +pub use isolate_group::IsolateGroup; +pub use locker::Locker; +pub use locker::SharedIsolate; +pub use microtask::MicrotaskQueue; +pub use microtask::MicrotaskQueueHandle; +pub use module::*; +pub use object::*; +pub use platform::IdleTask; +pub use platform::Platform; +pub use platform::PlatformImpl; +pub use platform::Task; +pub use platform::new_custom_platform; +pub use platform::new_default_platform; +pub use platform::new_single_threaded_default_platform; +pub use platform::new_unprotected_default_platform; +pub use primitives::*; +pub use promise::{PromiseRejectEvent, PromiseRejectMessage, PromiseState}; +pub use property_attribute::*; +pub use property_descriptor::*; +pub use property_filter::*; +pub use property_handler_flags::*; +pub use regexp::RegExpCreationFlags; +pub use scope::AllowJavascriptExecutionScope; +// pub use scope::CallbackScope; +pub use scope::CallbackScope; +pub use scope::ContextScope; +pub use scope::DisallowJavascriptExecutionScope; +pub use scope::EscapableHandleScope; +pub use scope::PinCallbackScope; +pub use scope::PinScope; +pub use scope::PinnedRef; +pub use scope::ScopeStorage; +// pub use scope::HandleScope; +pub use isolate::UnsafeRawIsolatePtr; +pub use scope::HandleScope; +pub use scope::OnFailure; +pub use scope::TryCatch; +pub use scope::allow_scopes_of_another_isolate; +pub use script::ScriptOrigin; +pub use script_compiler::CachedData; +pub use snapshot::FunctionCodeHandling; +pub use snapshot::StartupData; +pub use string::Encoding; +pub use string::NewStringType; +pub use string::OneByteConst; +pub use string::ValueView; +pub use string::ValueViewData; +pub use string::WriteFlags; +pub use string::WriteOptions; +pub use string::latin1_to_utf8; +pub use support::SharedPtr; +pub use support::SharedRef; +pub use support::UniquePtr; +pub use support::UniqueRef; +pub use template::*; +pub use value_deserializer::ValueDeserializer; +pub use value_deserializer::ValueDeserializerHelper; +pub use value_deserializer::ValueDeserializerImpl; +pub use value_serializer::ValueSerializer; +pub use value_serializer::ValueSerializerHelper; +pub use value_serializer::ValueSerializerImpl; +pub use wasm::CompiledWasmModule; +pub use wasm::ModuleCachingInterface; +pub use wasm::WasmModuleCompilation; +pub use wasm::WasmStreaming; + +/// https://v8.dev/docs/version-numbers +pub const MAJOR_VERSION: u32 = binding::v8__MAJOR_VERSION; +/// https://v8.dev/docs/version-numbers +pub const MINOR_VERSION: u32 = binding::v8__MINOR_VERSION; +/// https://v8.dev/docs/version-numbers +pub const BUILD_NUMBER: u32 = binding::v8__BUILD_NUMBER; +/// https://v8.dev/docs/version-numbers +pub const PATCH_LEVEL: u32 = binding::v8__PATCH_LEVEL; +/// https://v8.dev/docs/version-numbers +pub const VERSION_STRING: &str = + // TODO: cleanup when Result::unwrap is const stable. + match binding::v8__VERSION_STRING.to_str() { + Ok(v) => v, + Err(_) => panic!("Unable to convert CStr to &str??"), + }; + +// TODO(piscisaureus): Ideally this trait would not be exported. +pub use support::MapFnTo; + +pub const TYPED_ARRAY_MAX_SIZE_IN_HEAP: usize = + binding::v8__TYPED_ARRAY_MAX_SIZE_IN_HEAP as _; + +#[cfg(test)] +#[allow(unused)] +pub(crate) fn initialize_v8() { + use std::sync::Once; + + static INIT: Once = Once::new(); + INIT.call_once(|| { + V8::initialize_platform(new_default_platform(0, false).make_shared()); + V8::initialize(); + }); +} diff --git a/crates/v8/src/locker.rs b/crates/v8/src/locker.rs new file mode 100644 index 0000000000..f283e54f1e --- /dev/null +++ b/crates/v8/src/locker.rs @@ -0,0 +1,370 @@ +// Copyright 2019-2026 the Deno authors. All rights reserved. MIT license. + +//! Support for sharing an isolate between threads, one thread at a time, +//! via the `v8::Locker` API. + +use std::marker::PhantomData; +use std::mem::ManuallyDrop; +use std::ops::Deref; +use std::ops::DerefMut; +use std::ptr::NonNull; +use std::sync::Arc; + +use crate::Isolate; +use crate::IsolateHandle; +use crate::isolate::RealIsolate; + +unsafe extern "C" { + fn v8__Locker__CONSTRUCT(buf: *mut RawLocker, isolate: *mut RealIsolate); + fn v8__Locker__DESTRUCT(this: *mut RawLocker); + pub(crate) fn v8__Locker__IsLocked(isolate: *const RealIsolate) -> bool; + fn v8__Unlocker__CONSTRUCT(buf: *mut RawUnlocker, isolate: *mut RealIsolate); + fn v8__Unlocker__DESTRUCT(this: *mut RawUnlocker); + fn v8__Isolate__Enter(isolate: *mut RealIsolate); + fn v8__Isolate__Exit(isolate: *mut RealIsolate); + fn v8__Isolate__TryGetCurrent() -> *mut RealIsolate; +} + +/// Whether the current thread holds the `v8::Locker` for `isolate`. +/// +/// This asks V8 rather than shadowing the lock state in a thread-local. +/// A shadow would be a little cheaper on the `Global` clone/drop/eq/hash +/// path, but it has to stay correct in two places it is hard to +/// guarantee: it must be updated across [`Locker::unlock`] windows, and +/// any thread-local holding it is destructible, so a `Locker` or shared +/// `Global` living in a thread-local that was initialized *earlier* would +/// touch it from its destructor after it had already been torn down — +/// `LocalKey::with` panics there, and a panic in a TLS destructor aborts. +/// A false positive here would let `Global::drop` reset a cell without +/// the lock, so correctness wins over the FFI call. +pub(crate) fn thread_holds_lock(isolate: *mut RealIsolate) -> bool { + unsafe { v8__Locker__IsLocked(isolate) } +} + +/// Raw storage for a `v8::Locker`. Its size is checked by a static_assert +/// in binding.cc. It is not address-sensitive (it holds two flags and an +/// isolate pointer), but it lives in a `Box` anyway so its address stays +/// stable for the C++ destructor no matter how the Rust guard moves. +#[repr(C)] +pub(crate) struct RawLocker([usize; 2]); + +/// Raw storage for a `v8::Unlocker`. Size checked by a static_assert in +/// binding.cc. Boxed for the same reason as [`RawLocker`]. +#[repr(C)] +pub(crate) struct RawUnlocker([usize; 1]); + +/// An isolate that can be used from multiple threads, one at a time. +/// +/// Created with [`crate::OwnedIsolate::try_into_shared`]. All access goes +/// through [`SharedIsolate::lock`], which acquires the isolate's +/// `v8::Locker`, enters the isolate on the current thread, and yields a +/// [`Locker`] guard that dereferences to [`Isolate`]. +/// +/// Limitations: +/// - [`crate::Weak`] handles are not supported on shared isolates, and an +/// isolate with live weaks or pending finalizers is rejected by conversion. +/// - Snapshot-creator isolates and isolates with a cppgc heap attached +/// are rejected by conversion. +/// - Opening a [`crate::Global`] into a plain reference is unsafe, and that +/// reference must remain on the current thread and not outlive the lock. +/// Prefer converting it to a [`crate::Local`] under a handle scope. Other +/// access, such as cloning, hashing, or comparing, also requires holding the +/// lock on the current thread. +/// +/// [`crate::Global`]s are `Send` and may be dropped on any thread at any time: +/// if the +/// dropping thread holds the lock its V8 cell is reset immediately. Otherwise +/// the reset is deferred until the next lock acquisition, [`Locker::unlock`], +/// [`Locker`] drop, or isolate teardown. Until then it remains a GC root and +/// may retain the JavaScript object graph it references. +/// +/// # Blocking under the lock +/// +/// The lock is held for as long as its [`Locker`] guard lives, and +/// [`SharedIsolate::lock`] has no timeout and no `try_lock`. Anything that +/// blocks while holding it — a Rust callback doing I/O, a long computation, +/// JS that parks — blocks *every* other thread that wants this isolate, and +/// a thread that blocks forever wedges the isolate permanently. +/// +/// Use [`Locker::unlock`] to release the lock around such work so other +/// threads can make progress in the meantime. +/// +/// # Cost +/// +/// [`SharedIsolate::lock`] is not uniformly cheap: its cost scales with how +/// often the *entering thread changes*. V8's `ThreadManager` archives an +/// isolate's per-thread state when a different thread takes the lock and +/// restores it on the way back in, so a sequence of locks from one thread is +/// far cheaper than the same sequence alternating between two. +/// +/// This matters for work-stealing executors, which are free to run each of +/// an isolate's turns on a different worker and so maximise the migration. +/// A measured case: an embedder serving a trivial request per lock lost +/// about 9% of its throughput moving from one worker thread to twelve, with +/// the *same* number of locks in both — the loss was migration alone. If an +/// embedder can keep consecutive locks of one isolate on one thread, or run +/// fewer workers, it is worth doing. +/// +/// # Initialize V8 before creating the threads that will lock +/// +/// Every thread that calls [`SharedIsolate::lock`] must have been created +/// *after* [`V8::initialize`](crate::V8::initialize). On hardware with +/// memory protection keys, V8 guards its pointer tables with a key whose +/// access lives in the per-thread `PKRU` register, and a thread inherits +/// that access only at creation time. A worker created before V8 was +/// initialized will lock successfully and then take `SIGSEGV` with +/// `si_code == SEGV_PKUERR` the first time it runs JavaScript. +/// +/// The failure is easy to misread: it presents as a fault on a mapped, +/// readable page, and no debug build, heap verifier or handle check +/// reports anything, because nothing is corrupt. See +/// [`V8::initialize`](crate::V8::initialize) for the full description. +#[derive(Debug)] +pub struct SharedIsolate { + inner: Arc, +} + +#[derive(Debug)] +struct SharedIsolateInner { + cxx_isolate: NonNull, + isolate_handle: IsolateHandle, +} + +// SAFETY: V8 access is serialized by `v8::Locker`; the only operation exposed +// without it is cloning the separately synchronized `IsolateHandle`. +unsafe impl Send for SharedIsolateInner {} +unsafe impl Sync for SharedIsolateInner {} + +impl SharedIsolate { + pub(crate) fn new( + cxx_isolate: NonNull, + isolate_handle: IsolateHandle, + ) -> Self { + Self { + inner: Arc::new(SharedIsolateInner { + cxx_isolate, + isolate_handle, + }), + } + } + + pub(crate) fn as_real_ptr(&self) -> *mut RealIsolate { + self.inner.cxx_isolate.as_ptr() + } + + /// Acquire the isolate's lock and enter it on the current thread, + /// blocking until any other thread holding the lock releases it. + /// + /// # Panics + /// + /// Panics if the current thread already holds the lock. `v8::Locker` is + /// recursive, but two live guards would hand out aliasing `&mut Isolate` + /// references, so recursive locking is forbidden here. Also panics if a + /// different isolate is currently entered: permitting nested [`Locker`] + /// guards would make it possible to drop them out of order, which V8 cannot + /// recover from. + pub fn lock(&self) -> Locker<'_> { + Locker::new(self) + } + + /// A handle usable from any thread without holding the lock, for + /// [`IsolateHandle::terminate_execution`] and + /// [`IsolateHandle::request_interrupt`]. + /// + /// Obtaining one through a [`Locker`] would require the very lock the + /// runaway thread is holding, so take it from here instead. + pub fn thread_safe_handle(&self) -> IsolateHandle { + self.inner.isolate_handle.clone() + } +} + +impl Drop for SharedIsolateInner { + fn drop(&mut self) { + // Every Locker owns an Arc to this allocation. Forgetting a Locker therefore + // leaks the allocation and its still-entered V8 isolate instead of allowing + // this destructor to dispose an isolate that V8 still considers in use. + // With no Lockers left, other threads may still be dropping Globals: + // drain the deferred queue and close it under the lock, then tear down the + // same way `OwnedIsolate::drop` does. + unsafe { + let mut isolate = Isolate::from_non_null(self.cxx_isolate); + let ptr = self.cxx_isolate.as_ptr(); + let mut raw = Box::new(RawLocker([0; 2])); + v8__Locker__CONSTRUCT(&mut *raw, ptr); + isolate + .global_liveness() + .as_ref() + .close_deferred_global_resets(); + v8__Locker__DESTRUCT(&mut *raw); + let (annex_ptr, _create_param_allocations) = + isolate.prepare_annex_for_dispose(); + Isolate::run_remaining_guaranteed_finalizers(annex_ptr); + crate::Platform::notify_isolate_shutdown( + &crate::V8::get_current_platform(), + &isolate, + ); + isolate.dispose(); + Isolate::finish_annex_dispose(annex_ptr); + } + } +} + +/// A guard that holds the `v8::Locker` for a [`SharedIsolate`] and keeps +/// the isolate entered on the current thread. Dereferences to [`Isolate`]; +/// construct scopes with e.g. `HandleScope::new(&mut *locker)`. +pub struct Locker<'s> { + raw: Box, + cxx_isolate: NonNull, + // Dropped manually only after the C++ Locker is successfully destroyed. If + // cleanup panics first, retaining this Arc leaks the isolate rather than + // allowing its owner to dispose an isolate whose V8 lock is still held. + inner: ManuallyDrop>, + _shared: PhantomData<&'s SharedIsolate>, +} + +impl<'s> Locker<'s> { + fn new(shared: &'s SharedIsolate) -> Self { + let ptr = shared.as_real_ptr(); + unsafe { + assert!( + !thread_holds_lock(ptr), + "attempted to lock an isolate that is already locked by this thread" + ); + assert!( + v8__Isolate__TryGetCurrent().is_null(), + "attempted to lock a shared isolate while another isolate is entered" + ); + let mut raw = Box::new(RawLocker([0; 2])); + v8__Locker__CONSTRUCT(&mut *raw, ptr); + v8__Isolate__Enter(ptr); + let locker = Self { + raw, + cxx_isolate: shared.inner.cxx_isolate, + inner: ManuallyDrop::new(Arc::clone(&shared.inner)), + _shared: PhantomData, + }; + // Release Globals that were dropped by threads not holding the lock. + locker + .global_liveness() + .as_ref() + .maybe_drain_deferred_global_resets(); + locker + } + } + + /// Release the isolate's lock for the duration of `f` so other threads + /// can lock and use the isolate, then reacquire it before returning. + /// + /// Wrap anything that blocks or runs long without needing the isolate — + /// I/O in a callback, waiting on a channel, a heavy pure-Rust + /// computation — so it doesn't hold every other thread off the isolate. + /// + /// The isolate must not be touched inside `f`. The `&mut self` borrow + /// enforces that for anything reached through this guard: scopes and + /// `&Isolate` references derived from it are already dead by the time + /// `unlock` can be called, and handle operations that would need the + /// lock (e.g. cloning a [`crate::Global`]) panic while it is released. + /// + /// Handles created before the call stay valid — `v8::Unlocker` archives + /// this thread's isolate state and restores it on the way back in — but + /// another thread may run JS and collect garbage in the window, so + /// nothing observed beforehand can be assumed unchanged afterwards. + /// + /// # Panics + /// + /// Panics if another isolate has been entered on top of this one, since + /// unlocking would then release the wrong isolate's hold on this thread, or + /// if `f` returns while an isolate it entered is still current. + pub fn unlock(&mut self, f: impl FnOnce() -> R) -> R { + let ptr = self.cxx_isolate.as_ptr(); + unsafe { + assert!( + std::ptr::eq(ptr, v8__Isolate__TryGetCurrent()), + "Locker::unlock called while another isolate was entered on top of \ + this one" + ); + // Release what other threads queued while we held the lock; they + // can't do it themselves, and we're about to stop being able to. + self + .global_liveness() + .as_ref() + .maybe_drain_deferred_global_resets(); + } + unsafe { v8__Isolate__Exit(ptr) }; + let mut raw = Box::new(RawUnlocker([0; 1])); + // V8 requires the isolate to be exited before constructing an Unlocker. + // The guard destroys the Unlocker (reacquiring the lock) and then + // re-enters the isolate, including when `f` unwinds. + unsafe { v8__Unlocker__CONSTRUCT(&mut *raw, ptr) }; + let _relock = RelockGuard { + raw, + cxx_isolate: self.cxx_isolate, + }; + let result = f(); + // `result` was created after `_relock`, so if this assertion unwinds it is + // dropped first. That gives a returned OwnedIsolate or Locker a chance to + // exit cleanly before `_relock` restores this isolate. + assert!( + unsafe { v8__Isolate__TryGetCurrent().is_null() }, + "Locker::unlock closure returned while an isolate was still entered" + ); + result + } +} + +/// Reacquires the lock and re-enters the isolate when the unlock window +/// ends, on the normal path and while unwinding alike. +struct RelockGuard { + raw: Box, + cxx_isolate: NonNull, +} + +impl Drop for RelockGuard { + fn drop(&mut self) { + unsafe { + v8__Unlocker__DESTRUCT(&mut *self.raw); + v8__Isolate__Enter(self.cxx_isolate.as_ptr()); + // Globals may have been dropped while the lock was released. This is a + // lock acquisition boundary just like `SharedIsolate::lock()`. + Isolate::from_non_null(self.cxx_isolate) + .global_liveness() + .as_ref() + .maybe_drain_deferred_global_resets(); + } + } +} + +impl Drop for Locker<'_> { + fn drop(&mut self) { + unsafe { + // Final drain while we still hold the lock, so cells dropped by + // other threads during this lock don't sit in the queue (keeping + // their JS objects alive) until the next acquisition. + self + .global_liveness() + .as_ref() + .maybe_drain_deferred_global_resets(); + assert!( + std::ptr::eq(self.cxx_isolate.as_ptr(), v8__Isolate__TryGetCurrent()), + "Locker dropped while its isolate was not the entered one; lockers \ + must be dropped in reverse order of creation" + ); + v8__Isolate__Exit(self.cxx_isolate.as_ptr()); + v8__Locker__DESTRUCT(&mut *self.raw); + ManuallyDrop::drop(&mut self.inner); + } + } +} + +impl Deref for Locker<'_> { + type Target = Isolate; + fn deref(&self) -> &Isolate { + unsafe { Isolate::from_raw_ref(&self.cxx_isolate) } + } +} + +impl DerefMut for Locker<'_> { + fn deref_mut(&mut self) -> &mut Isolate { + unsafe { Isolate::from_raw_ref_mut(&mut self.cxx_isolate) } + } +} diff --git a/crates/v8/src/microtask.rs b/crates/v8/src/microtask.rs new file mode 100644 index 0000000000..95d067ff6f --- /dev/null +++ b/crates/v8/src/microtask.rs @@ -0,0 +1,157 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +use crate::Function; +use crate::Isolate; +use crate::Local; +use crate::MicrotasksPolicy; +use crate::isolate::RealIsolate; +use crate::support::Opaque; +use crate::support::int; +use std::ops::Deref; +use std::ptr::NonNull; + +#[repr(C)] +struct MicrotaskQueueHandleRaw(Opaque); + +unsafe extern "C" { + fn v8__MicrotaskQueueHandle__New( + isolate: *mut RealIsolate, + policy: MicrotasksPolicy, + ) -> *mut MicrotaskQueueHandleRaw; + fn v8__MicrotaskQueueHandle__DELETE(handle: *mut MicrotaskQueueHandleRaw); + fn v8__MicrotaskQueueHandle__Get( + handle: *const MicrotaskQueueHandleRaw, + ) -> *mut MicrotaskQueue; + fn v8__MicrotaskQueue__PerformCheckpoint( + isolate: *mut RealIsolate, + queue: *const MicrotaskQueue, + ); + fn v8__MicrotaskQueue__BelongsTo( + queue: *const MicrotaskQueue, + isolate: *mut RealIsolate, + ) -> bool; + fn v8__MicrotaskQueue__IsRunningMicrotasks( + queue: *const MicrotaskQueue, + ) -> bool; + fn v8__MicrotaskQueue__GetMicrotasksScopeDepth( + queue: *const MicrotaskQueue, + ) -> int; + fn v8__MicrotaskQueue__EnqueueMicrotask( + isolate: *mut RealIsolate, + queue: *const MicrotaskQueue, + microtask: *const Function, + ); +} + +/// Represents the microtask queue, where microtasks are stored and processed. +/// https://html.spec.whatwg.org/multipage/webappapis.html#microtask-queue +/// https://html.spec.whatwg.org/multipage/webappapis.html#enqueuejob(queuename,-job,-arguments) +/// https://html.spec.whatwg.org/multipage/webappapis.html#perform-a-microtask-checkpoint +/// +/// A MicrotaskQueue instance may be associated to multiple Contexts by passing +/// it to Context::New(), and they can be detached by Context::DetachGlobal(). +/// +/// Use the same instance of MicrotaskQueue for all Contexts that may access each +/// other synchronously. E.g. for Web embedding, use the same instance for all +/// origins that share the same URL scheme and eTLD+1. +#[repr(C)] +#[derive(Debug)] +pub struct MicrotaskQueue(Opaque); + +impl MicrotaskQueue { + #[allow(clippy::new_ret_no_self)] + pub fn new( + isolate: &mut Isolate, + policy: MicrotasksPolicy, + ) -> MicrotaskQueueHandle { + let handle = + unsafe { v8__MicrotaskQueueHandle__New(isolate.as_real_ptr(), policy) }; + MicrotaskQueueHandle(NonNull::new(handle).unwrap()) + } + + pub fn enqueue_microtask( + &self, + isolate: &mut Isolate, + microtask: Local, + ) { + self.assert_belongs_to(isolate); + crate::handle::assert_locals_in_isolate(isolate); + unsafe { + v8__MicrotaskQueue__EnqueueMicrotask( + isolate.as_real_ptr(), + self, + &*microtask, + ) + } + } + + /// Adds a callback to notify the embedder after microtasks were run. The + /// callback is triggered by explicit RunMicrotasks call or automatic + /// microtasks execution (see Isolate::SetMicrotasksPolicy). + /// + /// Callback will trigger even if microtasks were attempted to run, + /// but the microtasks queue was empty and no single microtask was actually + /// executed. + /// + /// Executing scripts inside the callback will not re-trigger microtasks and + /// the callback. + pub fn perform_checkpoint(&self, isolate: &mut Isolate) { + self.assert_belongs_to(isolate); + unsafe { + v8__MicrotaskQueue__PerformCheckpoint(isolate.as_real_ptr(), self); + } + } + + /// Panics unless this queue belongs to `isolate`. + /// + /// This takes time proportional to the number of microtask queues of + /// `isolate`. + pub(crate) fn assert_belongs_to(&self, isolate: &Isolate) { + assert!( + unsafe { v8__MicrotaskQueue__BelongsTo(self, isolate.as_real_ptr()) }, + "attempt to use a MicrotaskQueue in an Isolate other than its own" + ); + } + + /// Removes callback that was installed by AddMicrotasksCompletedCallback. + pub fn is_running_microtasks(&self) -> bool { + unsafe { v8__MicrotaskQueue__IsRunningMicrotasks(self) } + } + + /// Returns the current depth of nested MicrotasksScope that has kRunMicrotasks. + pub fn get_microtasks_scope_depth(&self) -> i32 { + unsafe { v8__MicrotaskQueue__GetMicrotasksScopeDepth(self) } + } +} + +/// A rooted handle to a [`MicrotaskQueue`]. +/// +/// This handle must be dropped before its isolate is disposed because dropping +/// it releases a root through the isolate's heap. +/// +/// With the default `v8_cppgc_microtask_queue` GN setting, each associated +/// context keeps the queue alive after this handle is dropped until it is +/// detached. If that setting is disabled through `EXTRA_GN_ARGS`, this handle +/// owns the queue and must outlive every associated context. +#[derive(Debug)] +pub struct MicrotaskQueueHandle(NonNull); + +impl Deref for MicrotaskQueueHandle { + type Target = MicrotaskQueue; + + fn deref(&self) -> &Self::Target { + unsafe { &*v8__MicrotaskQueueHandle__Get(self.0.as_ptr()) } + } +} + +impl AsRef for MicrotaskQueueHandle { + fn as_ref(&self) -> &MicrotaskQueue { + self + } +} + +impl Drop for MicrotaskQueueHandle { + fn drop(&mut self) { + unsafe { v8__MicrotaskQueueHandle__DELETE(self.0.as_ptr()) } + } +} diff --git a/crates/v8/src/module.rs b/crates/v8/src/module.rs new file mode 100644 index 0000000000..cc141cd6ed --- /dev/null +++ b/crates/v8/src/module.rs @@ -0,0 +1,667 @@ +use std::mem::MaybeUninit; +use std::num::NonZeroI32; +use std::ptr::null; + +use crate::Context; +use crate::FixedArray; +use crate::Local; +use crate::Message; +use crate::Module; +use crate::ModuleRequest; +use crate::Object; +use crate::String; +use crate::UnboundModuleScript; +use crate::Value; +use crate::isolate::ModuleImportPhase; +use crate::isolate::RealIsolate; +use crate::scope::GetIsolate; +use crate::scope::PinScope; +use crate::support::MapFnFrom; +use crate::support::MapFnTo; +use crate::support::MaybeBool; +use crate::support::ToCFn; +use crate::support::UnitType; +use crate::support::int; + +/// Called during Module::instantiate_module. Provided with arguments: +/// (context, specifier, import_attributes, referrer). Return None on error. +/// +/// Note: this callback has an unusual signature due to ABI incompatibilities +/// between Rust and C++. However end users can implement the callback as +/// follows; it'll be automatically converted. +/// +/// ```rust,ignore +/// fn my_resolve_callback<'a>( +/// context: v8::Local<'s, v8::Context>, +/// specifier: v8::Local<'s, v8::String>, +/// import_attributes: v8::Local<'s, v8::FixedArray>, +/// referrer: v8::Local<'s, v8::Module>, +/// ) -> Option> { +/// // ... +/// Some(resolved_module) +/// } +/// ``` +#[cfg(not(target_os = "windows"))] +#[repr(C)] +// System V ABI +pub struct ResolveModuleCallbackRet(*const Module); + +#[cfg(not(target_os = "windows"))] +pub type ResolveModuleCallback<'s> = + unsafe extern "C" fn( + Local<'s, Context>, + Local<'s, String>, + Local<'s, FixedArray>, + Local<'s, Module>, + ) -> ResolveModuleCallbackRet; + +// Windows x64 ABI: Local returned on the stack. +#[cfg(target_os = "windows")] +pub type ResolveModuleCallback<'s> = unsafe extern "C" fn( + *mut *const Module, + Local<'s, Context>, + Local<'s, String>, + Local<'s, FixedArray>, + Local<'s, Module>, +) + -> *mut *const Module; + +impl<'s, F> MapFnFrom for ResolveModuleCallback<'s> +where + F: UnitType + + Fn( + Local<'s, Context>, + Local<'s, String>, + Local<'s, FixedArray>, + Local<'s, Module>, + ) -> Option>, +{ + #[cfg(not(target_os = "windows"))] + fn mapping() -> Self { + let f = |context, specifier, import_attributes, referrer| { + ResolveModuleCallbackRet( + (F::get())(context, specifier, import_attributes, referrer) + .map(|r| -> *const Module { &*r }) + .unwrap_or(null()), + ) + }; + f.to_c_fn() + } + + #[cfg(target_os = "windows")] + fn mapping() -> Self { + let f = |ret_ptr, context, specifier, import_attributes, referrer| { + let r = (F::get())(context, specifier, import_attributes, referrer) + .map(|r| -> *const Module { &*r }) + .unwrap_or(null()); + unsafe { std::ptr::write(ret_ptr, r) }; // Write result to stack. + ret_ptr // Return stack pointer to the return value. + }; + f.to_c_fn() + } +} + +// System V ABI. +#[cfg(not(target_os = "windows"))] +#[repr(C)] +pub struct SyntheticModuleEvaluationStepsRet(*const Value); + +#[cfg(not(target_os = "windows"))] +pub type SyntheticModuleEvaluationSteps<'s> = + unsafe extern "C" fn( + Local<'s, Context>, + Local<'s, Module>, + ) -> SyntheticModuleEvaluationStepsRet; + +// Windows x64 ABI: Local returned on the stack. +#[cfg(target_os = "windows")] +pub type SyntheticModuleEvaluationSteps<'s> = + unsafe extern "C" fn( + *mut *const Value, + Local<'s, Context>, + Local<'s, Module>, + ) -> *mut *const Value; + +impl<'s, F> MapFnFrom for SyntheticModuleEvaluationSteps<'s> +where + F: UnitType + + Fn(Local<'s, Context>, Local<'s, Module>) -> Option>, +{ + #[cfg(not(target_os = "windows"))] + fn mapping() -> Self { + let f = |context, module| { + SyntheticModuleEvaluationStepsRet( + (F::get())(context, module).map_or(null(), |r| -> *const Value { &*r }), + ) + }; + f.to_c_fn() + } + + #[cfg(target_os = "windows")] + fn mapping() -> Self { + let f = |ret_ptr, context, module| { + let r = (F::get())(context, module) + .map(|r| -> *const Value { &*r }) + .unwrap_or(null()); + unsafe { std::ptr::write(ret_ptr, r) }; // Write result to stack. + ret_ptr // Return stack pointer to the return value. + }; + f.to_c_fn() + } +} + +// System V ABI +#[cfg(not(target_os = "windows"))] +#[repr(C)] +pub struct ResolveSourceCallbackRet(*const Object); + +#[cfg(not(target_os = "windows"))] +pub type ResolveSourceCallback<'s> = + unsafe extern "C" fn( + Local<'s, Context>, + Local<'s, String>, + Local<'s, FixedArray>, + Local<'s, Module>, + ) -> ResolveSourceCallbackRet; + +// Windows x64 ABI: Local returned on the stack. +#[cfg(target_os = "windows")] +pub type ResolveSourceCallback<'s> = unsafe extern "C" fn( + *mut *const Object, + Local<'s, Context>, + Local<'s, String>, + Local<'s, FixedArray>, + Local<'s, Module>, +) + -> *mut *const Object; + +impl<'s, F> MapFnFrom for ResolveSourceCallback<'s> +where + F: UnitType + + Fn( + Local<'s, Context>, + Local<'s, String>, + Local<'s, FixedArray>, + Local<'s, Module>, + ) -> Option>, +{ + #[cfg(not(target_os = "windows"))] + fn mapping() -> Self { + let f = |context, specifier, import_attributes, referrer| { + ResolveSourceCallbackRet( + (F::get())(context, specifier, import_attributes, referrer) + .map(|r| -> *const Object { &*r }) + .unwrap_or(null()), + ) + }; + f.to_c_fn() + } + + #[cfg(target_os = "windows")] + fn mapping() -> Self { + let f = |ret_ptr, context, specifier, import_attributes, referrer| { + let r = (F::get())(context, specifier, import_attributes, referrer) + .map(|r| -> *const Object { &*r }) + .unwrap_or(null()); + unsafe { std::ptr::write(ret_ptr, r) }; // Write result to stack. + ret_ptr // Return stack pointer to the return value. + }; + f.to_c_fn() + } +} + +unsafe extern "C" { + fn v8__Module__GetStatus(this: *const Module) -> ModuleStatus; + fn v8__Module__GetException(this: *const Module) -> *const Value; + fn v8__Module__GetModuleRequests(this: *const Module) -> *const FixedArray; + fn v8__Module__SourceOffsetToLocation( + this: *const Module, + offset: int, + out: *mut Location, + ); + fn v8__Module__GetModuleNamespace(this: *const Module) -> *const Value; + fn v8__Module__GetModuleNamespace2( + this: *const Module, + phase: ModuleImportPhase, + ) -> *const Value; + fn v8__Module__EvaluateForImportDefer( + this: *const Module, + context: *const Context, + ) -> *const Value; + fn v8__Module__GetIdentityHash(this: *const Module) -> int; + fn v8__Module__ScriptId(this: *const Module) -> int; + fn v8__Module__InstantiateModule( + this: *const Module, + context: *const Context, + cb: ResolveModuleCallback, + source_callback: Option, + ) -> MaybeBool; + fn v8__Module__Evaluate( + this: *const Module, + context: *const Context, + ) -> *const Value; + fn v8__Module__IsGraphAsync(this: *const Module) -> bool; + fn v8__Module__HasTopLevelAwait(this: *const Module) -> bool; + fn v8__Module__IsSourceTextModule(this: *const Module) -> bool; + fn v8__Module__IsSyntheticModule(this: *const Module) -> bool; + fn v8__Module__CreateSyntheticModule( + isolate: *const RealIsolate, + module_name: *const String, + export_names_len: usize, + export_names_raw: *const *const String, + evaluation_steps: SyntheticModuleEvaluationSteps, + ) -> *const Module; + fn v8__Module__SetSyntheticModuleExport( + this: *const Module, + isolate: *const RealIsolate, + export_name: *const String, + export_value: *const Value, + ) -> MaybeBool; + fn v8__Module__GetUnboundModuleScript( + this: *const Module, + ) -> *const UnboundModuleScript; + fn v8__Location__GetLineNumber(this: *const Location) -> int; + fn v8__Location__GetColumnNumber(this: *const Location) -> int; + fn v8__ModuleRequest__GetSpecifier( + this: *const ModuleRequest, + ) -> *const String; + fn v8__ModuleRequest__GetPhase( + this: *const ModuleRequest, + ) -> ModuleImportPhase; + fn v8__ModuleRequest__GetSourceOffset(this: *const ModuleRequest) -> int; + fn v8__ModuleRequest__GetImportAttributes( + this: *const ModuleRequest, + ) -> *const FixedArray; + fn v8__Module__GetStalledTopLevelAwaitMessage( + this: *const Module, + isolate: *const RealIsolate, + out_vec: *mut StalledTopLevelAwaitMessage, + vec_len: usize, + ) -> usize; +} + +#[repr(C)] +pub struct StalledTopLevelAwaitMessage { + pub module: *const Module, + pub message: *const Message, +} + +/// A location in JavaScript source. +#[repr(C)] +#[derive(Debug)] +pub struct Location([i32; 2]); + +impl Location { + pub fn get_line_number(&self) -> int { + unsafe { v8__Location__GetLineNumber(self) } + } + + pub fn get_column_number(&self) -> int { + unsafe { v8__Location__GetColumnNumber(self) } + } +} + +/// The different states a module can be in. +/// +/// This corresponds to the states used in ECMAScript except that "evaluated" +/// is split into kEvaluated and kErrored, indicating success and failure, +/// respectively. +#[derive(Debug, PartialEq, Eq)] +#[repr(C)] +pub enum ModuleStatus { + Uninstantiated, + Instantiating, + Instantiated, + Evaluating, + Evaluated, + Errored, +} + +impl Module { + /// Returns the module's current status. + #[inline(always)] + pub fn get_status(&self) -> ModuleStatus { + unsafe { v8__Module__GetStatus(self) } + } + + /// For a module in kErrored status, this returns the corresponding exception. + #[inline(always)] + pub fn get_exception<'o>(&self) -> Local<'o, Value> { + // Note: the returned value is not actually stored in a HandleScope, + // therefore we don't need a scope object here. + unsafe { Local::from_raw(v8__Module__GetException(self)) }.unwrap() + } + + /// Returns the ModuleRequests for this module. + #[inline(always)] + pub fn get_module_requests<'o>(&self) -> Local<'o, FixedArray> { + unsafe { Local::from_raw(v8__Module__GetModuleRequests(self)) }.unwrap() + } + + /// For the given source text offset in this module, returns the corresponding + /// Location with line and column numbers. + #[inline(always)] + pub fn source_offset_to_location(&self, offset: int) -> Location { + let mut out = MaybeUninit::::uninit(); + unsafe { + v8__Module__SourceOffsetToLocation(self, offset, out.as_mut_ptr()); + out.assume_init() + } + } + + /// Returns the V8 hash value for this value. The current implementation + /// uses a hidden property to store the identity hash. + /// + /// The return value will never be 0. Also, it is not guaranteed to be + /// unique. + #[inline(always)] + pub fn get_identity_hash(&self) -> NonZeroI32 { + unsafe { NonZeroI32::new_unchecked(v8__Module__GetIdentityHash(self)) } + } + + /// Returns the underlying script's id. + /// + /// The module must be a SourceTextModule and must not have an Errored status. + #[inline(always)] + pub fn script_id(&self) -> Option { + if !self.is_source_text_module() { + return None; + } + if self.get_status() == ModuleStatus::Errored { + return None; + } + Some(unsafe { v8__Module__ScriptId(self) }) + } + + /// Returns the namespace object of this module. + /// + /// The module's status must be at least kInstantiated. + #[inline(always)] + pub fn get_module_namespace<'o>(&self) -> Local<'o, Value> { + // Note: the returned value is not actually stored in a HandleScope, + // therefore we don't need a scope object here. + unsafe { Local::from_raw(v8__Module__GetModuleNamespace(self)).unwrap() } + } + + /// Returns the namespace object of this module for the given import phase. + /// + /// For `ModuleImportPhase::kDefer`, returns a deferred namespace object that + /// triggers module evaluation on first property access. + /// + /// The module's status must be at least kInstantiated. + #[inline(always)] + pub fn get_module_namespace_with_phase<'o>( + &self, + phase: ModuleImportPhase, + ) -> Local<'o, Value> { + unsafe { + Local::from_raw(v8__Module__GetModuleNamespace2(self, phase)).unwrap() + } + } + + /// Evaluates the module for deferred import, gathering async transitive + /// dependencies eagerly so that deferred namespace access can be synchronous. + /// + /// Returns a Promise that resolves when all async transitive dependencies + /// have been evaluated. The module itself is NOT evaluated until its + /// deferred namespace is first accessed. + /// + /// This is used to implement `import.defer()` dynamic imports. + #[must_use] + #[inline(always)] + pub fn evaluate_for_import_defer<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + Local::from_raw(v8__Module__EvaluateForImportDefer( + self, + &*scope.get_current_context(), + )) + } + } + + /// Instantiates the module and its dependencies. + /// + /// Returns an empty Maybe if an exception occurred during + /// instantiation. (In the case where the callback throws an exception, that + /// exception is propagated.) + #[must_use] + #[inline(always)] + pub fn instantiate_module<'s, 'i>( + &self, + scope: &PinScope<'s, 'i>, + callback: impl MapFnTo>, + ) -> Option { + unsafe { + v8__Module__InstantiateModule( + self, + &*scope.get_current_context(), + callback.map_fn_to(), + None, + ) + } + .into() + } + + /// Instantiates the module and its dependencies. + /// + /// Returns an empty Maybe if an exception occurred during + /// instantiation. (In the case where the callback throws an exception, that + /// exception is propagated.) + #[must_use] + #[inline(always)] + pub fn instantiate_module2<'s, 'i>( + &self, + scope: &PinScope<'s, 'i>, + callback: impl MapFnTo>, + source_callback: impl MapFnTo>, + ) -> Option { + unsafe { + v8__Module__InstantiateModule( + self, + &*scope.get_current_context(), + callback.map_fn_to(), + Some(source_callback.map_fn_to()), + ) + } + .into() + } + + /// Evaluates the module and its dependencies. + /// + /// If status is kInstantiated, run the module's code. On success, set status + /// to kEvaluated and return the completion value; on failure, set status to + /// kErrored and propagate the thrown exception (which is then also available + /// via |GetException|). + #[must_use] + #[inline(always)] + pub fn evaluate<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope + .cast_local(|sd| v8__Module__Evaluate(self, sd.get_current_context())) + } + } + + /// Returns whether this module or any of its requested modules is async, + /// i.e. contains top-level await. + /// + /// The module's status must be at least kInstantiated. + #[inline(always)] + pub fn is_graph_async(&self) -> bool { + unsafe { v8__Module__IsGraphAsync(self) } + } + + /// Returns whether this module is individually asynchronous (for example, + /// if it's a Source Text Module Record containing a top-level await). + /// See [[HasTLA]] in + /// + /// Unlike [`Self::is_graph_async`], this says nothing about the module's + /// dependencies and imposes no requirement on the module's status, so it can + /// be called on a module that has only been compiled. + #[inline(always)] + pub fn has_top_level_await(&self) -> bool { + unsafe { v8__Module__HasTopLevelAwait(self) } + } + + /// Returns whether the module is a SourceTextModule. + #[inline(always)] + pub fn is_source_text_module(&self) -> bool { + unsafe { v8__Module__IsSourceTextModule(self) } + } + + /// Returns whether the module is a SyntheticModule. + #[inline(always)] + pub fn is_synthetic_module(&self) -> bool { + unsafe { v8__Module__IsSyntheticModule(self) } + } + + /// Creates a new SyntheticModule with the specified export names, where + /// evaluation_steps will be executed upon module evaluation. + /// export_names must not contain duplicates. + /// module_name is used solely for logging/debugging and doesn't affect module + /// behavior. + /// + /// evaluation_steps must return a resolved `Promise` on success, or `None` + /// if an exception was thrown. Since V8 15.8, V8 aborts the process if the + /// returned value is not a promise. + #[inline(always)] + pub fn create_synthetic_module<'s, 'i>( + scope: &PinScope<'s, 'i>, + module_name: Local, + export_names: &[Local], + evaluation_steps: impl MapFnTo>, + ) -> Local<'s, Module> { + let export_names = Local::slice_into_raw(export_names); + let export_names_len = export_names.len(); + let export_names = export_names.as_ptr(); + unsafe { + scope + .cast_local(|sd| { + v8__Module__CreateSyntheticModule( + sd.get_isolate_ptr(), + &*module_name, + export_names_len, + export_names, + evaluation_steps.map_fn_to(), + ) + }) + .unwrap() + } + } + + /// Set this module's exported value for the name export_name to the specified + /// export_value. This method must be called only on Modules created via + /// create_synthetic_module. An error will be thrown if export_name is not one + /// of the export_names that were passed in that create_synthetic_module call. + /// Returns Some(true) on success, None if an error was thrown. + #[must_use] + #[inline(always)] + pub fn set_synthetic_module_export<'s>( + &self, + scope: &mut PinScope<'s, '_>, + export_name: Local<'s, String>, + export_value: Local<'s, Value>, + ) -> Option { + unsafe { + v8__Module__SetSyntheticModuleExport( + self, + scope.get_isolate_ptr(), + &*export_name, + &*export_value, + ) + } + .into() + } + + #[inline(always)] + pub fn get_unbound_module_script<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Local<'s, UnboundModuleScript> { + unsafe { + scope + .cast_local(|_| v8__Module__GetUnboundModuleScript(self)) + .unwrap() + } + } + + /// Search the modules requested directly or indirectly by the module for + /// any top-level await that has not yet resolved. If there is any, the + /// returned vector contains a tuple of the unresolved module and a message + /// with the pending top-level await. + /// An embedder may call this before exiting to improve error messages. + pub fn get_stalled_top_level_await_message<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + ) -> Vec<(Local<'s, Module>, Local<'s, Message>)> { + let mut out_vec: Vec = Vec::with_capacity(16); + for _i in 0..16 { + out_vec.push(StalledTopLevelAwaitMessage { + module: std::ptr::null(), + message: std::ptr::null(), + }); + } + + let returned_len = unsafe { + v8__Module__GetStalledTopLevelAwaitMessage( + self, + scope.get_isolate_ptr(), + out_vec.as_mut_ptr(), + out_vec.len(), + ) + }; + + let mut ret_vec = Vec::with_capacity(returned_len); + for item in out_vec.iter().take(returned_len) { + unsafe { + ret_vec.push(( + Local::from_raw(item.module).unwrap(), + Local::from_raw(item.message).unwrap(), + )); + } + } + ret_vec + } +} + +impl ModuleRequest { + /// Returns the module specifier for this ModuleRequest. + #[inline(always)] + pub fn get_specifier<'o>(&self) -> Local<'o, String> { + unsafe { Local::from_raw(v8__ModuleRequest__GetSpecifier(self)) }.unwrap() + } + + /// Returns the module import phase for this ModuleRequest. + #[inline(always)] + pub fn get_phase(&self) -> ModuleImportPhase { + unsafe { v8__ModuleRequest__GetPhase(self) } + } + /// Returns the source code offset of this module request. + /// Use Module::source_offset_to_location to convert this to line/column numbers. + #[inline(always)] + pub fn get_source_offset(&self) -> int { + unsafe { v8__ModuleRequest__GetSourceOffset(self) } + } + + /// Contains the import attributes for this request in the form: + /// [key1, value1, source_offset1, key2, value2, source_offset2, ...]. + /// The keys and values are of type v8::String, and the source offsets are of + /// type Int32. Use Module::source_offset_to_location to convert the source + /// offsets to Locations with line/column numbers. + /// + /// All assertions present in the module request will be supplied in this + /// list, regardless of whether they are supported by the host. Per + /// https://tc39.es/proposal-import-assertions/#sec-hostgetsupportedimportassertions, + /// hosts are expected to ignore assertions that they do not support (as + /// opposed to, for example, triggering an error if an unsupported assertion is + /// present). + #[inline(always)] + pub fn get_import_attributes<'o>(&self) -> Local<'o, FixedArray> { + unsafe { Local::from_raw(v8__ModuleRequest__GetImportAttributes(self)) } + .unwrap() + } +} diff --git a/crates/v8/src/name.rs b/crates/v8/src/name.rs new file mode 100644 index 0000000000..d4d8f02510 --- /dev/null +++ b/crates/v8/src/name.rs @@ -0,0 +1,22 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +use std::num::NonZeroI32; + +use crate::Name; +use crate::support::int; + +unsafe extern "C" { + fn v8__Name__GetIdentityHash(this: *const Name) -> int; +} + +impl Name { + /// Returns the V8 hash value for this value. The current implementation + /// uses a hidden property to store the identity hash. + /// + /// The return value will never be 0. Also, it is not guaranteed to be + /// unique. + #[inline(always)] + pub fn get_identity_hash(&self) -> NonZeroI32 { + unsafe { NonZeroI32::new_unchecked(v8__Name__GetIdentityHash(self)) } + } +} diff --git a/crates/v8/src/number.rs b/crates/v8/src/number.rs new file mode 100644 index 0000000000..1211a3b7e5 --- /dev/null +++ b/crates/v8/src/number.rs @@ -0,0 +1,104 @@ +use std::alloc::Layout; +use std::ptr::NonNull; + +use crate::Int32; +use crate::Integer; +use crate::Local; +use crate::Number; +use crate::Uint32; +use crate::isolate::RealIsolate; +use crate::scope::PinScope; + +unsafe extern "C" { + fn v8__Number__New(isolate: *mut RealIsolate, value: f64) -> *const Number; + fn v8__Number__Value(this: *const Number) -> f64; + fn v8__Integer__New(isolate: *mut RealIsolate, value: i32) -> *const Integer; + fn v8__Integer__NewFromUnsigned( + isolate: *mut RealIsolate, + value: u32, + ) -> *const Integer; + fn v8__Integer__Value(this: *const Integer) -> i64; + fn v8__Uint32__Value(this: *const Uint32) -> u32; + fn v8__Int32__Value(this: *const Int32) -> i32; +} + +impl Number { + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_, ()>, + value: f64, + ) -> Local<'s, Number> { + unsafe { + scope.cast_local(|sd| v8__Number__New(sd.get_isolate_ptr(), value)) + } + .unwrap() + } + + #[inline(always)] + pub fn value(&self) -> f64 { + unsafe { v8__Number__Value(self) } + } +} + +impl Integer { + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_, ()>, + value: i32, + ) -> Local<'s, Integer> { + unsafe { + scope.cast_local(|sd| v8__Integer__New(sd.get_isolate_ptr(), value)) + } + .unwrap() + } + + #[inline(always)] + pub fn new_from_unsigned<'s>( + scope: &PinScope<'s, '_, ()>, + value: u32, + ) -> Local<'s, Integer> { + unsafe { + scope.cast_local(|sd| { + v8__Integer__NewFromUnsigned(sd.get_isolate_ptr(), value) + }) + } + .unwrap() + } + + #[inline(always)] + pub fn value(&self) -> i64 { + unsafe { v8__Integer__Value(self) } + } + + /// Internal helper function to produce a handle containing a SMI zero value, + /// without the need for the caller to provide (or have entered) a + /// `HandleScope`. + #[inline(always)] + pub(crate) fn zero<'s>() -> Local<'s, Integer> { + // The SMI representation of zero is also zero. In debug builds, double + // check this, so in the unlikely event that V8 changes its internal + // representation of SMIs such that this invariant no longer holds, we'd + // catch it. + static ZERO_SMI: usize = 0; + let zero_raw = &ZERO_SMI as *const _ as *mut Self; + let zero_nn = unsafe { NonNull::new_unchecked(zero_raw) }; + let zero_local = unsafe { Local::from_non_null(zero_nn) }; + debug_assert_eq!(Layout::new::(), Layout::new::>()); + debug_assert_eq!(zero_local.value(), 0); + zero_local + } +} + +impl Uint32 { + #[inline(always)] + pub fn value(&self) -> u32 { + unsafe { v8__Uint32__Value(self) } + } +} + +impl Int32 { + #[inline(always)] + pub fn value(&self) -> i32 { + unsafe { v8__Int32__Value(self) } + } +} diff --git a/crates/v8/src/object.rs b/crates/v8/src/object.rs new file mode 100644 index 0000000000..5e3545a6df --- /dev/null +++ b/crates/v8/src/object.rs @@ -0,0 +1,1557 @@ +use crate::AccessorConfiguration; +use crate::AccessorNameGetterCallback; +use crate::AccessorNameSetterCallback; +use crate::Array; +use crate::Context; +use crate::Data; +use crate::FunctionTemplate; +use crate::GetPropertyNamesArgs; +use crate::IndexFilter; +use crate::KeyCollectionMode; +use crate::KeyConversionMode; +use crate::Local; +use crate::Map; +use crate::Name; +use crate::Object; +use crate::Private; +use crate::PropertyAttribute; +use crate::PropertyDescriptor; +use crate::PropertyFilter; +use crate::Set; +use crate::SideEffectType; +use crate::String; +use crate::Value; +use crate::binding::RustObj; +use crate::cppgc::GarbageCollected; +use crate::cppgc::GetRustObj; +use crate::cppgc::UnsafePtr; +use crate::isolate::Isolate; +use crate::isolate::RealIsolate; +use crate::scope::PinScope; +use crate::support::MapFnTo; +use crate::support::Maybe; +use crate::support::MaybeBool; +use crate::support::int; +use std::convert::TryFrom; +use std::ffi::c_void; +use std::mem::MaybeUninit; +use std::num::NonZeroI32; +use std::ptr::null; + +unsafe extern "C" { + fn v8__Object__New(isolate: *mut RealIsolate) -> *const Object; + fn v8__Object__New__with_prototype_and_properties( + isolate: *mut RealIsolate, + prototype_or_null: *const Value, + names: *const *const Name, + values: *const *const Value, + length: usize, + ) -> *const Object; + fn v8__Object__SetAccessor( + this: *const Object, + context: *const Context, + key: *const Name, + getter: AccessorNameGetterCallback, + setter: Option, + data_or_null: *const Value, + attr: PropertyAttribute, + ) -> MaybeBool; + #[allow(clippy::too_many_arguments)] + fn v8__Object__SetLazyDataProperty( + this: *const Object, + context: *const Context, + key: *const Name, + getter: AccessorNameGetterCallback, + data_or_null: *const Value, + attr: PropertyAttribute, + getter_side_effect_type: SideEffectType, + setter_side_effect_type: SideEffectType, + ) -> MaybeBool; + fn v8__Object__Get( + this: *const Object, + context: *const Context, + key: *const Value, + ) -> *const Value; + fn v8__Object__GetWithReceiver( + this: *const Object, + context: *const Context, + key: *const Value, + receiver: *const Object, + ) -> *const Value; + fn v8__Object__GetIndex( + this: *const Object, + context: *const Context, + index: u32, + ) -> *const Value; + fn v8__Object__GetPrototype(this: *const Object) -> *const Value; + fn v8__Object__FindInstanceInPrototypeChain( + this: *const Object, + tmpl: *const FunctionTemplate, + ) -> *const Object; + fn v8__Object__Set( + this: *const Object, + context: *const Context, + key: *const Value, + value: *const Value, + ) -> MaybeBool; + fn v8__Object__SetWithReceiver( + this: *const Object, + context: *const Context, + key: *const Value, + value: *const Value, + receiver: *const Object, + ) -> MaybeBool; + fn v8__Object__SetIndex( + this: *const Object, + context: *const Context, + index: u32, + value: *const Value, + ) -> MaybeBool; + fn v8__Object__SetPrototype( + this: *const Object, + context: *const Context, + prototype: *const Value, + ) -> MaybeBool; + fn v8__Object__GetConstructorName(this: *const Object) -> *const String; + fn v8__Object__CreateDataProperty( + this: *const Object, + context: *const Context, + key: *const Name, + value: *const Value, + ) -> MaybeBool; + fn v8__Object__DefineOwnProperty( + this: *const Object, + context: *const Context, + key: *const Name, + value: *const Value, + attr: PropertyAttribute, + ) -> MaybeBool; + fn v8__Object__DefineProperty( + this: *const Object, + context: *const Context, + key: *const Name, + desc: *const PropertyDescriptor, + ) -> MaybeBool; + fn v8__Object__GetIdentityHash(this: *const Object) -> int; + fn v8__Object__GetCreationContext(this: *const Object) -> *const Context; + fn v8__Object__GetOwnPropertyNames( + this: *const Object, + context: *const Context, + filter: PropertyFilter, + key_conversion: KeyConversionMode, + ) -> *const Array; + fn v8__Object__GetPropertyNames( + this: *const Object, + context: *const Context, + mode: KeyCollectionMode, + property_filter: PropertyFilter, + index_filter: IndexFilter, + key_conversion: KeyConversionMode, + ) -> *const Array; + fn v8__Object__Has( + this: *const Object, + context: *const Context, + key: *const Value, + ) -> MaybeBool; + fn v8__Object__HasIndex( + this: *const Object, + context: *const Context, + index: u32, + ) -> MaybeBool; + fn v8__Object__HasOwnProperty( + this: *const Object, + context: *const Context, + key: *const Name, + ) -> MaybeBool; + fn v8__Object__Delete( + this: *const Object, + context: *const Context, + key: *const Value, + ) -> MaybeBool; + fn v8__Object__DeleteIndex( + this: *const Object, + context: *const Context, + index: u32, + ) -> MaybeBool; + fn v8__Object__InternalFieldCount(this: *const Object) -> int; + fn v8__Object__GetInternalField( + this: *const Object, + index: int, + ) -> *const Data; + fn v8__Object__GetAlignedPointerFromInternalField( + this: *const Object, + index: int, + tag: u16, + ) -> *const c_void; + fn v8__Object__SetAlignedPointerInInternalField( + this: *const Object, + index: int, + value: *const c_void, + tag: u16, + ); + fn v8__Object__SetIntegrityLevel( + this: *const Object, + context: *const Context, + level: IntegrityLevel, + ) -> MaybeBool; + fn v8__Object__SetInternalField( + this: *const Object, + index: int, + data: *const Data, + ); + fn v8__Object__GetPrivate( + this: *const Object, + context: *const Context, + key: *const Private, + ) -> *const Value; + fn v8__Object__SetPrivate( + this: *const Object, + context: *const Context, + key: *const Private, + value: *const Value, + ) -> MaybeBool; + fn v8__Object__DeletePrivate( + this: *const Object, + context: *const Context, + key: *const Private, + ) -> MaybeBool; + fn v8__Object__HasPrivate( + this: *const Object, + context: *const Context, + key: *const Private, + ) -> MaybeBool; + fn v8__Object__GetPropertyAttributes( + this: *const Object, + context: *const Context, + key: *const Value, + out: *mut Maybe, + ); + fn v8__Object__GetOwnPropertyDescriptor( + this: *const Object, + context: *const Context, + key: *const Name, + ) -> *const Value; + fn v8__Object__PreviewEntries( + this: *const Object, + is_key_value: *mut bool, + ) -> *const Array; + fn v8__Object__GetRealNamedProperty( + this: *const Object, + context: *const Context, + key: *const Name, + ) -> *const Value; + fn v8__Object__HasRealNamedProperty( + this: *const Object, + context: *const Context, + key: *const Name, + ) -> MaybeBool; + fn v8__Object__GetRealNamedPropertyAttributes( + this: *const Object, + context: *const Context, + key: *const Name, + out: *mut Maybe, + ); + fn v8__Object__Wrap( + isolate: *const RealIsolate, + wrapper: *const Object, + value: *const RustObj, + tag: u16, + ); + fn v8__Object__Unwrap( + isolate: *const RealIsolate, + wrapper: *const Object, + tag: u16, + ) -> *mut RustObj; + fn v8__Object__IsApiWrapper(this: *const Object) -> bool; + fn v8__Object__IsCallable(this: *const Object) -> bool; + fn v8__Object__IsConstructor(this: *const Object) -> bool; + fn v8__Object__CallAsFunction( + this: *const Object, + context: *const Context, + recv: *const Value, + argc: int, + argv: *const *const Value, + ) -> *const Value; + fn v8__Object__CallAsConstructor( + this: *const Object, + context: *const Context, + argc: int, + argv: *const *const Value, + ) -> *const Value; + + fn v8__Array__New(isolate: *mut RealIsolate, length: int) -> *const Array; + fn v8__Array__New_with_elements( + isolate: *mut RealIsolate, + elements: *const *const Value, + length: usize, + ) -> *const Array; + fn v8__Array__Length(array: *const Array) -> u32; + fn v8__Array__Iterate( + array: *const Array, + context: *const Context, + callback: unsafe extern "C" fn( + index: u32, + element: *const Value, + data: *mut c_void, + ) -> ArrayIterationResult, + callback_data: *mut c_void, + ) -> bool; + fn v8__Map__New(isolate: *mut RealIsolate) -> *const Map; + fn v8__Map__Clear(this: *const Map); + fn v8__Map__Get( + this: *const Map, + context: *const Context, + key: *const Value, + ) -> *const Value; + fn v8__Map__Set( + this: *const Map, + context: *const Context, + key: *const Value, + value: *const Value, + ) -> *const Map; + fn v8__Map__Has( + this: *const Map, + context: *const Context, + key: *const Value, + ) -> MaybeBool; + fn v8__Map__Delete( + this: *const Map, + context: *const Context, + key: *const Value, + ) -> MaybeBool; + fn v8__Map__Size(map: *const Map) -> usize; + fn v8__Map__As__Array(this: *const Map) -> *const Array; + fn v8__Set__New(isolate: *mut RealIsolate) -> *const Set; + fn v8__Set__Clear(this: *const Set); + fn v8__Set__Add( + this: *const Set, + context: *const Context, + key: *const Value, + ) -> *const Set; + fn v8__Set__Has( + this: *const Set, + context: *const Context, + key: *const Value, + ) -> MaybeBool; + fn v8__Set__Delete( + this: *const Set, + context: *const Context, + key: *const Value, + ) -> MaybeBool; + fn v8__Set__Size(map: *const Set) -> usize; + fn v8__Set__As__Array(this: *const Set) -> *const Array; +} + +const LAST_TAG: u16 = 0x7fff; + +impl Object { + /// Creates an empty object. + #[inline(always)] + pub fn new<'s>(scope: &PinScope<'s, '_>) -> Local<'s, Object> { + unsafe { scope.cast_local(|sd| v8__Object__New(sd.get_isolate_ptr())) } + .unwrap() + } + + /// Creates a JavaScript object with the given properties, and the given + /// prototype_or_null (which can be any JavaScript value, and if it's null, + /// the newly created object won't have a prototype at all). This is similar + /// to Object.create(). All properties will be created as enumerable, + /// configurable and writable properties. + #[inline(always)] + pub fn with_prototype_and_properties<'s>( + scope: &PinScope<'s, '_>, + prototype_or_null: Local<'s, Value>, + names: &[Local], + values: &[Local], + ) -> Local<'s, Object> { + assert_eq!(names.len(), values.len()); + let names = Local::slice_into_raw(names); + let values = Local::slice_into_raw(values); + unsafe { + scope.cast_local(|sd| { + v8__Object__New__with_prototype_and_properties( + sd.get_isolate_ptr(), + &*prototype_or_null, + names.as_ptr(), + values.as_ptr(), + names.len(), + ) + }) + } + .unwrap() + } + + /// Set only return Just(true) or Empty(), so if it should never fail, use + /// result.Check(). + #[inline(always)] + pub fn set( + &self, + scope: &PinScope<'_, '_>, + key: Local, + value: Local, + ) -> Option { + unsafe { + v8__Object__Set(self, &*scope.get_current_context(), &*key, &*value) + } + .into() + } + + /// SetWithReceiver only return Just(true) or Empty(), so if it should never fail, use + /// result.Check(). + #[inline(always)] + pub fn set_with_receiver( + &self, + scope: &PinScope<'_, '_>, + key: Local, + value: Local, + receiver: Local, + ) -> Option { + unsafe { + v8__Object__SetWithReceiver( + self, + &*scope.get_current_context(), + &*key, + &*value, + &*receiver, + ) + } + .into() + } + + /// Set only return Just(true) or Empty(), so if it should never fail, use + /// result.Check(). + #[inline(always)] + pub fn set_index( + &self, + scope: &PinScope<'_, '_>, + index: u32, + value: Local, + ) -> Option { + unsafe { + v8__Object__SetIndex(self, &*scope.get_current_context(), index, &*value) + } + .into() + } + + /// Set the prototype object. This does not skip objects marked to be + /// skipped by proto and it does not consult the security handler. + #[inline(always)] + pub fn set_prototype( + &self, + scope: &PinScope<'_, '_>, + prototype: Local, + ) -> Option { + unsafe { + v8__Object__SetPrototype(self, &*scope.get_current_context(), &*prototype) + } + .into() + } + + /// Returns the name of the function invoked as a constructor for this object. + #[inline(always)] + pub fn get_constructor_name(&self) -> Local<'_, String> { + unsafe { Local::from_raw(v8__Object__GetConstructorName(self)) }.unwrap() + } + + /// Implements CreateDataProperty (ECMA-262, 7.3.4). + /// + /// Defines a configurable, writable, enumerable property with the given value + /// on the object unless the property already exists and is not configurable + /// or the object is not extensible. + /// + /// Returns true on success. + #[inline(always)] + pub fn create_data_property( + &self, + scope: &PinScope<'_, '_>, + key: Local, + value: Local, + ) -> Option { + unsafe { + v8__Object__CreateDataProperty( + self, + &*scope.get_current_context(), + &*key, + &*value, + ) + } + .into() + } + + /// Implements DefineOwnProperty. + /// + /// In general, CreateDataProperty will be faster, however, does not allow + /// for specifying attributes. + /// + /// Returns true on success. + #[inline(always)] + pub fn define_own_property( + &self, + scope: &PinScope<'_, '_>, + key: Local, + value: Local, + attr: PropertyAttribute, + ) -> Option { + unsafe { + v8__Object__DefineOwnProperty( + self, + &*scope.get_current_context(), + &*key, + &*value, + attr, + ) + } + .into() + } + + #[inline(always)] + pub fn define_property( + &self, + scope: &PinScope<'_, '_>, + key: Local, + descriptor: &PropertyDescriptor, + ) -> Option { + unsafe { + v8__Object__DefineProperty( + self, + &*scope.get_current_context(), + &*key, + descriptor, + ) + .into() + } + } + + #[inline(always)] + pub fn get<'s>( + &self, + scope: &PinScope<'s, '_>, + key: Local, + ) -> Option> { + unsafe { + scope + .cast_local(|sd| v8__Object__Get(self, sd.get_current_context(), &*key)) + } + } + + #[inline(always)] + pub fn get_with_receiver<'s>( + &self, + scope: &PinScope<'s, '_>, + key: Local, + receiver: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Object__GetWithReceiver( + self, + sd.get_current_context(), + &*key, + &*receiver, + ) + }) + } + } + + #[inline(always)] + pub fn get_index<'s>( + &self, + scope: &PinScope<'s, '_>, + index: u32, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Object__GetIndex(self, sd.get_current_context(), index) + }) + } + } + + /// Get the prototype object. This does not skip objects marked to be + /// skipped by proto and it does not consult the security handler. + #[inline(always)] + pub fn get_prototype<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { scope.cast_local(|_| v8__Object__GetPrototype(self)) } + } + + /// Finds an instance of the given function template in the prototype chain. + /// + /// Returns `None` if no instance was found. + #[inline(always)] + pub fn find_instance_in_prototype_chain<'s>( + &self, + scope: &PinScope<'s, '_>, + tmpl: Local, + ) -> Option> { + unsafe { + scope + .cast_local(|_| v8__Object__FindInstanceInPrototypeChain(self, &*tmpl)) + } + } + + /// Note: SideEffectType affects the getter only, not the setter. + #[inline(always)] + pub fn set_accessor( + &self, + scope: &PinScope<'_, '_>, + name: Local, + getter: impl MapFnTo, + ) -> Option { + self.set_accessor_with_configuration( + scope, + name, + AccessorConfiguration::new(getter), + ) + } + + #[inline(always)] + pub fn set_accessor_with_setter( + &self, + scope: &PinScope<'_, '_>, + name: Local, + getter: impl MapFnTo, + setter: impl MapFnTo, + ) -> Option { + self.set_accessor_with_configuration( + scope, + name, + AccessorConfiguration::new(getter).setter(setter), + ) + } + #[inline(always)] + pub fn set_accessor_with_configuration( + &self, + scope: &PinScope<'_, '_>, + name: Local, + configuration: AccessorConfiguration, + ) -> Option { + unsafe { + v8__Object__SetAccessor( + self, + &*scope.get_current_context(), + &*name, + configuration.getter, + configuration.setter, + configuration.data.map_or_else(null, |p| &*p), + configuration.property_attribute, + ) + } + .into() + } + + /// Sets a lazy data property on this object. The getter is invoked the first + /// time the property is read, and then the property is replaced with an + /// ordinary data property containing the value returned by the getter. + #[inline(always)] + pub fn set_lazy_data_property( + &self, + scope: &PinScope<'_, '_>, + name: Local, + getter: impl MapFnTo, + ) -> Option { + unsafe { + v8__Object__SetLazyDataProperty( + self, + &*scope.get_current_context(), + &*name, + getter.map_fn_to(), + null(), + PropertyAttribute::NONE, + SideEffectType::HasSideEffect, + SideEffectType::HasSideEffect, + ) + } + .into() + } + + /// Sets a lazy data property with data, attributes, and side effect types + /// on this object. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub fn set_lazy_data_property_with_data( + &self, + scope: &PinScope<'_, '_>, + name: Local, + getter: impl MapFnTo, + data: Local, + attr: PropertyAttribute, + getter_side_effect_type: SideEffectType, + setter_side_effect_type: SideEffectType, + ) -> Option { + unsafe { + v8__Object__SetLazyDataProperty( + self, + &*scope.get_current_context(), + &*name, + getter.map_fn_to(), + &*data, + attr, + getter_side_effect_type, + setter_side_effect_type, + ) + } + .into() + } + + /// Returns the V8 hash value for this value. The current implementation + /// uses a hidden property to store the identity hash. + /// + /// The return value will never be 0. Also, it is not guaranteed to be + /// unique. + #[inline(always)] + pub fn get_identity_hash(&self) -> NonZeroI32 { + unsafe { NonZeroI32::new_unchecked(v8__Object__GetIdentityHash(self)) } + } + + /// Returns the context in which the object was created. + #[inline(always)] + pub fn get_creation_context<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { scope.cast_local(|_| v8__Object__GetCreationContext(self)) } + } + + /// This function has the same functionality as GetPropertyNames but the + /// returned array doesn't contain the names of properties from prototype + /// objects. + #[inline(always)] + pub fn get_own_property_names<'s>( + &self, + scope: &PinScope<'s, '_>, + args: GetPropertyNamesArgs, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Object__GetOwnPropertyNames( + self, + sd.get_current_context(), + args.property_filter, + args.key_conversion, + ) + }) + } + } + + /// Returns an array containing the names of the filtered properties of this + /// object, including properties from prototype objects. The array returned by + /// this method contains the same values as would be enumerated by a for-in + /// statement over this object. + #[inline(always)] + pub fn get_property_names<'s>( + &self, + scope: &PinScope<'s, '_>, + args: GetPropertyNamesArgs, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Object__GetPropertyNames( + self, + sd.get_current_context(), + args.mode, + args.property_filter, + args.index_filter, + args.key_conversion, + ) + }) + } + } + + // Calls the abstract operation HasProperty(O, P) described in ECMA-262, + // 7.3.10. Returns true, if the object has the property, either own or on the + // prototype chain. Interceptors, i.e., PropertyQueryCallbacks, are called if + // present. + // + // This function has the same side effects as JavaScript's variable in object. + // For example, calling this on a revoked proxy will throw an exception. + // + // Note: This function converts the key to a name, which possibly calls back + // into JavaScript. + #[inline(always)] + pub fn has( + &self, + scope: &PinScope<'_, '_>, + key: Local, + ) -> Option { + unsafe { v8__Object__Has(self, &*scope.get_current_context(), &*key) } + .into() + } + + #[inline(always)] + pub fn has_index( + &self, + scope: &PinScope<'_, '_>, + index: u32, + ) -> Option { + unsafe { v8__Object__HasIndex(self, &*scope.get_current_context(), index) } + .into() + } + + /// HasOwnProperty() is like JavaScript's Object.prototype.hasOwnProperty(). + #[inline(always)] + pub fn has_own_property( + &self, + scope: &PinScope<'_, '_>, + key: Local, + ) -> Option { + unsafe { + v8__Object__HasOwnProperty(self, &*scope.get_current_context(), &*key) + } + .into() + } + + #[inline(always)] + pub fn delete( + &self, + scope: &PinScope<'_, '_>, + key: Local, + ) -> Option { + unsafe { v8__Object__Delete(self, &*scope.get_current_context(), &*key) } + .into() + } + + #[inline] + pub fn delete_index( + &self, + scope: &PinScope<'_, '_>, + index: u32, + ) -> Option { + unsafe { + v8__Object__DeleteIndex(self, &*scope.get_current_context(), index) + } + .into() + } + + /// Gets the number of internal fields for this Object. + #[inline(always)] + pub fn internal_field_count(&self) -> usize { + let count = unsafe { v8__Object__InternalFieldCount(self) }; + usize::try_from(count).expect("bad internal field count") // Can't happen. + } + + /// Gets the data from an internal field. + #[inline(always)] + pub fn get_internal_field<'s>( + &self, + scope: &PinScope<'s, '_>, + index: usize, + ) -> Option> { + // Trying to access out-of-bounds internal fields makes V8 abort + // in debug mode and access out-of-bounds memory in release mode. + // The C++ API takes an i32 but doesn't check for indexes < 0, which + // results in an out-of-bounds access in both debug and release mode. + if index < self.internal_field_count() + && let Ok(index) = int::try_from(index) + { + return unsafe { + scope.cast_local(|_| v8__Object__GetInternalField(self, index)) + }; + } + None + } + + /// Gets a 2-byte-aligned native pointer from an internal field. + /// + /// # Safety + /// This field must have been set by SetAlignedPointerInInternalField, everything else leads to undefined behavior. + #[inline(always)] + pub unsafe fn get_aligned_pointer_from_internal_field( + &self, + index: i32, + tag: u16, + ) -> *const c_void { + unsafe { v8__Object__GetAlignedPointerFromInternalField(self, index, tag) } + } + + /// Sets a 2-byte-aligned native pointer in an internal field. + /// To retrieve such a field, GetAlignedPointerFromInternalField must be used. + #[allow(clippy::not_unsafe_ptr_arg_deref)] + #[inline(always)] + pub fn set_aligned_pointer_in_internal_field( + &self, + index: i32, + value: *const c_void, + tag: u16, + ) { + unsafe { + v8__Object__SetAlignedPointerInInternalField(self, index, value, tag) + } + } + + /// Wraps a JS wrapper with a C++ instance. + /// + /// # Safety + /// + /// The `TAG` must be unique to the caller within the heap. + #[inline(always)] + pub unsafe fn wrap( + isolate: &mut Isolate, + wrapper: Local, + value: &impl GetRustObj, + ) { + const { + assert!(TAG < LAST_TAG); + } + let ptr = value.get_rust_obj(); + unsafe { v8__Object__Wrap(isolate.as_real_ptr(), &*wrapper, ptr, TAG) } + } + + /// Unwraps a JS wrapper object. + /// + /// # Safety + /// + /// The caller must ensure that the returned pointer is always stored on + /// the stack, or is safely moved into one of the other cppgc pointer types. + #[inline(always)] + pub unsafe fn unwrap( + isolate: &mut Isolate, + wrapper: Local, + ) -> Option> { + const { + assert!(TAG < LAST_TAG); + } + let ptr = + unsafe { v8__Object__Unwrap(isolate.as_real_ptr(), &*wrapper, TAG) }; + unsafe { UnsafePtr::new(&ptr) } + } + + /// Returns true if this object can be generally used to wrap object objects. + /// This means that the object either follows the convention of using embedder + /// fields to denote type/instance pointers or is using the Wrap()/Unwrap() + /// APIs for the same purpose. Returns false otherwise. + /// + /// Note that there may be other objects that use embedder fields but are not + /// used as API wrapper objects. E.g., v8::Promise may in certain configuration + /// use embedder fields but promises are not generally supported as API + /// wrappers. The method will return false in those cases. + #[inline(always)] + pub fn is_api_wrapper(&self) -> bool { + unsafe { v8__Object__IsApiWrapper(self) } + } + + /// Sets the integrity level of the object. + #[inline(always)] + pub fn set_integrity_level( + &self, + scope: &PinScope<'_, '_>, + level: IntegrityLevel, + ) -> Option { + unsafe { + v8__Object__SetIntegrityLevel(self, &*scope.get_current_context(), level) + } + .into() + } + + /// Sets the data in an internal field. Returns false when the index + /// is out of bounds, true otherwise. + #[inline(always)] + pub fn set_internal_field(&self, index: usize, data: Local) -> bool { + // Trying to access out-of-bounds internal fields makes V8 abort + // in debug mode and access out-of-bounds memory in release mode. + // The C++ API takes an i32 but doesn't check for indexes < 0, which + // results in an out-of-bounds access in both debug and release mode. + if index < self.internal_field_count() + && let Ok(index) = int::try_from(index) + { + unsafe { v8__Object__SetInternalField(self, index, &*data) }; + return true; + } + false + } + + /// Functionality for private properties. + /// This is an experimental feature, use at your own risk. + /// Note: Private properties are not inherited. Do not rely on this, since it + /// may change. + #[inline(always)] + pub fn get_private<'s>( + &self, + scope: &PinScope<'s, '_>, + key: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Object__GetPrivate(self, sd.get_current_context(), &*key) + }) + } + } + + /// Functionality for private properties. + /// This is an experimental feature, use at your own risk. + /// Note: Private properties are not inherited. Do not rely on this, since it + /// may change. + #[inline(always)] + pub fn set_private( + &self, + scope: &PinScope<'_, '_>, + key: Local, + value: Local, + ) -> Option { + unsafe { + v8__Object__SetPrivate( + self, + &*scope.get_current_context(), + &*key, + &*value, + ) + } + .into() + } + + /// Functionality for private properties. + /// This is an experimental feature, use at your own risk. + /// Note: Private properties are not inherited. Do not rely on this, since it + /// may change. + #[inline(always)] + pub fn delete_private( + &self, + scope: &PinScope<'_, '_>, + key: Local, + ) -> Option { + unsafe { + v8__Object__DeletePrivate(self, &*scope.get_current_context(), &*key) + } + .into() + } + + /// Functionality for private properties. + /// This is an experimental feature, use at your own risk. + /// Note: Private properties are not inherited. Do not rely on this, since it + /// may change. + #[inline(always)] + pub fn has_private( + &self, + scope: &PinScope<'_, '_>, + key: Local, + ) -> Option { + unsafe { + v8__Object__HasPrivate(self, &*scope.get_current_context(), &*key) + } + .into() + } + + /// Gets the property attributes of a property which can be + /// [PropertyAttribute::NONE] or any combination of + /// [PropertyAttribute::READ_ONLY], [PropertyAttribute::DONT_ENUM] and + /// [PropertyAttribute::DONT_DELETE]. + /// Returns [PropertyAttribute::NONE] when the property doesn't exist. + pub fn get_property_attributes( + &self, + scope: &PinScope<'_, '_>, + key: Local, + ) -> Option { + let mut out = Maybe::::default(); + unsafe { + v8__Object__GetPropertyAttributes( + self, + &*scope.get_current_context(), + &*key, + &mut out, + ); + }; + out.into() + } + + /// Implements Object.getOwnPropertyDescriptor(O, P), see + /// https://tc39.es/ecma262/#sec-object.getownpropertydescriptor. + #[inline] + pub fn get_own_property_descriptor<'s>( + &self, + scope: &PinScope<'s, '_>, + key: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Object__GetOwnPropertyDescriptor( + self, + sd.get_current_context(), + &*key, + ) + }) + } + } + + /// If this object is a Set, Map, WeakSet or WeakMap, this returns a + /// representation of the elements of this object as an array. + /// If this object is a SetIterator or MapIterator, this returns all elements + /// of the underlying collection, starting at the iterator's current position. + /// + /// Also returns a boolean, indicating whether the returned array contains + /// key & values (for example when the value is Set.entries()). + #[inline] + pub fn preview_entries<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> (Option>, bool) { + let mut is_key_value = MaybeUninit::uninit(); + unsafe { + let val = scope.cast_local(|_| { + v8__Object__PreviewEntries(self, is_key_value.as_mut_ptr()) + }); + let is_key_value = is_key_value.assume_init(); + + (val, is_key_value) + } + } + + /// If result.IsEmpty() no real property was located on the object or + /// in the prototype chain. + /// This means interceptors in the prototype chain are not called. + #[inline(always)] + pub fn get_real_named_property<'s>( + &self, + scope: &PinScope<'s, '_>, + key: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Object__GetRealNamedProperty(self, sd.get_current_context(), &*key) + }) + } + } + + #[inline(always)] + pub fn has_real_named_property( + &self, + scope: &PinScope<'_, '_>, + key: Local, + ) -> Option { + unsafe { + v8__Object__HasRealNamedProperty( + self, + &*scope.get_current_context(), + &*key, + ) + } + .into() + } + + /// Gets the property attributes of a real property which can be + /// None or any combination of ReadOnly, DontEnum and DontDelete. + /// Interceptors in the prototype chain are not called. + #[inline(always)] + pub fn get_real_named_property_attributes( + &self, + scope: &PinScope<'_, '_>, + key: Local, + ) -> Option { + let mut out = Maybe::::default(); + unsafe { + v8__Object__GetRealNamedPropertyAttributes( + self, + &*scope.get_current_context(), + &*key, + &mut out, + ); + } + out.into() + } + + /// When this Object is callable, this method returns `true`. + /// + /// Certain exotic objects may be callable despite not being + /// [`Function`](crate::Function)s, such as [`Proxy`](crate::Proxy), or other + /// objects with a `[[Call]]` internal method. Callable objects can be created + /// via + /// [`ObjectTemplate::set_call_as_function_handler`](crate::ObjectTemplate::set_call_as_function_handler). + #[must_use = "this is a pure method"] + #[inline(always)] + pub fn is_callable(&self) -> bool { + unsafe { v8__Object__IsCallable(self) } + } + + /// When this Object can be called as a constructor, this method returns + /// `true`. + #[must_use = "this is a pure method"] + #[inline(always)] + pub fn is_constructor(&self) -> bool { + unsafe { v8__Object__IsConstructor(self) } + } + + /// Calls the [`Object`] as a function with the provided `this` argument and + /// arguments. + /// + /// For more information, see [`Object::is_callable()`]. + #[inline] + pub fn call_as_function<'s>( + &self, + scope: &PinScope<'s, '_>, + recv: Local<'_, Value>, + args: &[Local<'_, Value>], + ) -> Option> { + let args = Local::slice_into_raw(args); + let argc = int::try_from(args.len()).unwrap(); + let argv = args.as_ptr(); + unsafe { + scope.cast_local(|sd| { + v8__Object__CallAsFunction( + self, + sd.get_current_context(), + &*recv, + argc, + argv, + ) + }) + } + } + + /// Calls the [`Object`] as a function in a given context. + /// + /// For more information, see [`Object::is_callable()`]. + #[inline] + pub fn call_as_function_with_context<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + context: Local<'_, Context>, + recv: Local<'_, Value>, + args: &[Local<'_, Value>], + ) -> Option> { + let args = Local::slice_into_raw(args); + let argc = int::try_from(args.len()).unwrap(); + let argv = args.as_ptr(); + unsafe { + let ret = v8__Object__CallAsFunction( + self, + context.as_non_null().as_ptr(), + &*recv, + argc, + argv, + ); + if ret.is_null() { + None + } else { + scope.cast_local(|_| ret) + } + } + } + + /// Calls the [`Object`] as a constructor with the provided arguments. + /// This is similar to + /// [`Function::new_instance()`](crate::Function::new_instance). + /// + /// For more information, see [`Object::is_constructor()`]. + #[inline] + pub fn call_as_constructor<'s>( + &self, + scope: &PinScope<'s, '_>, + args: &[Local<'_, Value>], + ) -> Option> { + let args = Local::slice_into_raw(args); + let argc = int::try_from(args.len()).unwrap(); + let argv = args.as_ptr(); + unsafe { + scope.cast_local(|sd| { + v8__Object__CallAsConstructor( + self, + sd.get_current_context(), + argc, + argv, + ) + }) + } + } + + /// Calls the [`Object`] as a constructor in a given context. + /// + /// For more information, see [`Object::is_constructor()`]. + #[inline] + pub fn call_as_constructor_with_context<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + context: Local<'_, Context>, + args: &[Local<'_, Value>], + ) -> Option> { + let args = Local::slice_into_raw(args); + let argc = int::try_from(args.len()).unwrap(); + let argv = args.as_ptr(); + unsafe { + let ret = v8__Object__CallAsConstructor( + self, + context.as_non_null().as_ptr(), + argc, + argv, + ); + if ret.is_null() { + None + } else { + scope.cast_local(|_| ret) + } + } + } +} + +/// Object integrity levels can be used to restrict what can be done to an +/// object's properties. +#[derive(Debug)] +#[repr(C)] +pub enum IntegrityLevel { + /// Frozen objects are like Sealed objects, except all existing properties are + /// also made non-writable. + Frozen, + /// Sealed objects prevent addition of any new property on the object, makes + /// all existing properties non-configurable, meaning they cannot be deleted, + /// have their enumerability, configurability, or writability changed. + Sealed, +} + +impl Array { + /// Creates a JavaScript array with the given length. If the length + /// is negative the returned array will have length 0. + #[inline(always)] + pub fn new<'s>(scope: &PinScope<'s, '_>, length: i32) -> Local<'s, Array> { + unsafe { + scope.cast_local(|sd| v8__Array__New(sd.get_isolate_ptr(), length)) + } + .unwrap() + } + + /// Creates a JavaScript array out of a Local array with a known + /// length. + #[inline(always)] + pub fn new_with_elements<'s>( + scope: &PinScope<'s, '_>, + elements: &[Local], + ) -> Local<'s, Array> { + if elements.is_empty() { + return Self::new(scope, 0); + } + let elements = Local::slice_into_raw(elements); + unsafe { + scope.cast_local(|sd| { + v8__Array__New_with_elements( + sd.get_isolate_ptr(), + elements.as_ptr(), + elements.len(), + ) + }) + } + .unwrap() + } + + #[inline(always)] + pub fn length(&self) -> u32 { + unsafe { v8__Array__Length(self) } + } + + /// Calls `callback` for every element of this array. + /// + /// This function will typically be faster than calling [`get_index()`] + /// repeatedly. + /// + /// Returns `None` on exception; use a [`TryCatch`] to catch and handle the + /// exception. When the callback returns [`ArrayIterationResult::Exception`], + /// iteration is terminated immediately, returning `None`. By returning + /// [`ArrayIterationResult::Break`], the callback can request non-exceptional + /// early termination of the iteration. + /// + /// # Safety + /// + /// This entry point is optimized for low overhead: while iterating, V8 holds + /// a raw pointer to the array's backing store that is not visible to the + /// garbage collector, and hands the callback a handle that does not point + /// into a handle scope. V8 deliberately does not enforce the rules below, so + /// violating any of them is undefined behaviour rather than a panic or a + /// failed assertion. The callback: + /// + /// - Must not allocate any V8 objects and then continue iterating. It may + /// allocate (e.g. an error message/object) and then immediately terminate + /// the iteration by returning [`ArrayIterationResult::Exception`] or + /// [`ArrayIterationResult::Break`]. Allocating and continuing can trigger + /// a garbage collection that moves the backing store, after which the + /// remaining elements are read from freed memory. + /// - Must not modify the array being iterated. V8 only checks this with a + /// `DCHECK`, so in release builds a mutation silently reads a stale + /// backing store. + /// - Must not call back into V8, unless it can guarantee that such a call + /// violates neither of the above, which is difficult. Note that the + /// callback can reach the surrounding scope, so this is not prevented by + /// the signature. + /// - Must not let the element handle "escape", i.e. must not assign it to + /// any other `Local`. Creating a `Global` from it is safe. + /// - Must not panic. The callback is invoked across an `extern "C"` + /// boundary, so a panic aborts the process rather than unwinding. + /// + /// [`get_index()`]: Object::get_index + /// [`TryCatch`]: crate::TryCatch + #[inline(always)] + pub unsafe fn iterate( + &self, + scope: &PinScope<'_, '_>, + mut callback: F, + ) -> Option<()> + where + F: for<'a> FnMut(u32, Local<'a, Value>) -> ArrayIterationResult, + { + unsafe extern "C" fn trampoline( + index: u32, + element: *const Value, + data: *mut c_void, + ) -> ArrayIterationResult + where + F: for<'a> FnMut(u32, Local<'a, Value>) -> ArrayIterationResult, + { + let callback = unsafe { &mut *data.cast::() }; + // V8 never passes an empty handle here: holes are replaced with + // `undefined` and the slow path uses a checked conversion. Use the + // checked constructor anyway so a future change upstream turns into a + // panic rather than a null dereference. + let element = unsafe { Local::from_raw(element) } + .expect("v8::Array::Iterate passed an empty element handle"); + callback(index, element) + } + + let ok = unsafe { + v8__Array__Iterate( + self, + &*scope.get_current_context(), + trampoline::, + std::ptr::from_mut(&mut callback).cast::(), + ) + }; + + if ok { Some(()) } else { None } + } +} + +/// The result of an [`Array::iterate()`] callback, controlling whether +/// iteration continues. +/// +/// Mirrors `v8::Array::CallbackResult`; the variants are returned directly to +/// V8, so both their order and the enum's width must match. `#[repr(i32)]` +/// pins the width to that of a C++ scoped enum with no fixed underlying type; +/// `binding.cc` static_asserts both properties. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(i32)] +pub enum ArrayIterationResult { + /// Terminate iteration immediately; an exception has been thrown. + Exception, + /// Terminate iteration early without throwing. + Break, + /// Continue with the next element. + Continue, +} + +impl Map { + #[inline(always)] + pub fn new<'s>(scope: &PinScope<'s, '_>) -> Local<'s, Map> { + unsafe { scope.cast_local(|sd| v8__Map__New(sd.get_isolate_ptr())) } + .unwrap() + } + + #[inline(always)] + pub fn size(&self) -> usize { + unsafe { v8__Map__Size(self) } + } + + #[inline(always)] + pub fn clear(&self) { + unsafe { v8__Map__Clear(self) } + } + + #[inline(always)] + pub fn get<'s>( + &self, + scope: &PinScope<'s, '_>, + key: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| v8__Map__Get(self, sd.get_current_context(), &*key)) + } + } + + #[inline(always)] + pub fn set<'s>( + &self, + scope: &PinScope<'s, '_>, + key: Local, + value: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Map__Set(self, sd.get_current_context(), &*key, &*value) + }) + } + } + + #[inline(always)] + pub fn has( + &self, + scope: &PinScope<'_, '_>, + key: Local, + ) -> Option { + unsafe { v8__Map__Has(self, &*scope.get_current_context(), &*key) }.into() + } + + #[inline(always)] + pub fn delete( + &self, + scope: &PinScope<'_, '_>, + key: Local, + ) -> Option { + unsafe { v8__Map__Delete(self, &*scope.get_current_context(), &*key) } + .into() + } + + /// Returns an array of length size() * 2, where index N is the Nth key and + /// index N + 1 is the Nth value. + #[inline(always)] + pub fn as_array<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, Array> { + unsafe { scope.cast_local(|_| v8__Map__As__Array(self)) }.unwrap() + } +} + +impl Set { + #[inline(always)] + pub fn new<'s>(scope: &PinScope<'s, '_>) -> Local<'s, Set> { + unsafe { scope.cast_local(|sd| v8__Set__New(sd.get_isolate_ptr())) } + .unwrap() + } + + #[inline(always)] + pub fn size(&self) -> usize { + unsafe { v8__Set__Size(self) } + } + + #[inline(always)] + pub fn clear(&self) { + unsafe { v8__Set__Clear(self) } + } + + #[inline(always)] + pub fn add<'s>( + &self, + scope: &PinScope<'s, '_>, + key: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| v8__Set__Add(self, sd.get_current_context(), &*key)) + } + } + + #[inline(always)] + pub fn has( + &self, + scope: &PinScope<'_, '_>, + key: Local, + ) -> Option { + unsafe { v8__Set__Has(self, &*scope.get_current_context(), &*key) }.into() + } + + #[inline(always)] + pub fn delete( + &self, + scope: &PinScope<'_, '_>, + key: Local, + ) -> Option { + unsafe { v8__Set__Delete(self, &*scope.get_current_context(), &*key) } + .into() + } + + /// Returns an array of the keys in this Set. + #[inline(always)] + pub fn as_array<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, Array> { + unsafe { scope.cast_local(|_| v8__Set__As__Array(self)) }.unwrap() + } +} diff --git a/crates/v8/src/platform.rs b/crates/v8/src/platform.rs new file mode 100644 index 0000000000..d501f8702c --- /dev/null +++ b/crates/v8/src/platform.rs @@ -0,0 +1,558 @@ +use crate::Isolate; +use crate::isolate::RealIsolate; +use crate::support::int; + +use crate::support::Opaque; +use crate::support::Shared; +use crate::support::SharedPtrBase; +use crate::support::SharedRef; +use crate::support::UniquePtr; +use crate::support::UniqueRef; +use crate::support::long; + +unsafe extern "C" { + fn v8__Platform__NewDefaultPlatform( + thread_pool_size: int, + idle_task_support: bool, + ) -> *mut Platform; + fn v8__Platform__NewUnprotectedDefaultPlatform( + thread_pool_size: int, + idle_task_support: bool, + ) -> *mut Platform; + fn v8__Platform__NewSingleThreadedDefaultPlatform( + idle_task_support: bool, + ) -> *mut Platform; + fn v8__Platform__NewCustomPlatform( + thread_pool_size: int, + idle_task_support: bool, + unprotected: bool, + context: *mut std::ffi::c_void, + ) -> *mut Platform; + fn v8__Platform__DELETE(this: *mut Platform); + + fn v8__Platform__PumpMessageLoop( + platform: *mut Platform, + isolate: *mut RealIsolate, + wait_for_work: bool, + ) -> bool; + + fn v8__Platform__RunIdleTasks( + platform: *mut Platform, + isolate: *mut RealIsolate, + idle_time_in_seconds: f64, + ); + + fn v8__Platform__NotifyIsolateShutdown( + platform: *mut Platform, + isolate: *mut RealIsolate, + ); + + fn v8__Task__Run(task: *mut std::ffi::c_void); + fn v8__Task__DELETE(task: *mut std::ffi::c_void); + fn v8__IdleTask__Run(task: *mut std::ffi::c_void, deadline_in_seconds: f64); + fn v8__IdleTask__DELETE(task: *mut std::ffi::c_void); + + fn std__shared_ptr__v8__Platform__CONVERT__std__unique_ptr( + unique_ptr: UniquePtr, + ) -> SharedPtrBase; + fn std__shared_ptr__v8__Platform__get( + ptr: *const SharedPtrBase, + ) -> *mut Platform; + fn std__shared_ptr__v8__Platform__COPY( + ptr: *const SharedPtrBase, + ) -> SharedPtrBase; + fn std__shared_ptr__v8__Platform__reset(ptr: *mut SharedPtrBase); + fn std__shared_ptr__v8__Platform__use_count( + ptr: *const SharedPtrBase, + ) -> long; +} + +#[repr(C)] +#[derive(Debug)] +pub struct Platform(Opaque); + +/// A V8 foreground task. Ownership is transferred from C++ to Rust when +/// V8 posts a task via [`PlatformImpl`] trait methods. +/// +/// The embedder is responsible for scheduling the task and calling +/// [`run()`](Task::run). For example, in an async runtime like tokio: +/// +/// ```ignore +/// tokio::spawn(async move { task.run() }); +/// ``` +/// +/// If dropped without calling `run()`, the task is destroyed without +/// executing. +pub struct Task(*mut std::ffi::c_void); + +// SAFETY: V8 tasks are designed to be posted from background threads and +// run on the isolate's foreground thread. The unique_ptr transfer is safe +// across thread boundaries. +unsafe impl Send for Task {} + +impl Task { + /// Run the task. Consumes self to prevent double execution. + pub fn run(self) { + let ptr = self.0; + // Prevent Drop from deleting — we'll delete after Run. + std::mem::forget(self); + unsafe { + v8__Task__Run(ptr); + v8__Task__DELETE(ptr); + } + } +} + +impl Drop for Task { + fn drop(&mut self) { + unsafe { v8__Task__DELETE(self.0) }; + } +} + +/// A V8 idle task. Similar to [`Task`] but accepts a deadline parameter +/// when run. +/// +/// If dropped without calling `run()`, the task is destroyed without +/// executing. +pub struct IdleTask(*mut std::ffi::c_void); + +// SAFETY: Same as Task — safe to transfer across threads. +unsafe impl Send for IdleTask {} + +impl IdleTask { + /// Run the idle task with the given deadline. Consumes self. + /// + /// `deadline_in_seconds` is the absolute time (in seconds since some + /// epoch) by which the idle task should complete. + pub fn run(self, deadline_in_seconds: f64) { + let ptr = self.0; + std::mem::forget(self); + unsafe { + v8__IdleTask__Run(ptr, deadline_in_seconds); + v8__IdleTask__DELETE(ptr); + } + } +} + +impl Drop for IdleTask { + fn drop(&mut self) { + unsafe { v8__IdleTask__DELETE(self.0) }; + } +} + +/// Trait for customizing platform behavior, following the same pattern as +/// [`V8InspectorClientImpl`](crate::inspector::V8InspectorClientImpl). +/// +/// Implement this trait to receive V8 foreground tasks and schedule them +/// on your event loop. The C++ `CustomPlatform` wraps each isolate's +/// `TaskRunner` so that every `PostTask` / `PostDelayedTask` / etc. call +/// transfers task ownership to Rust through the corresponding trait method. +/// +/// **The embedder is responsible for calling [`Task::run()`] on the +/// isolate's thread.** For example, using tokio: +/// +/// ```ignore +/// fn post_task(&self, isolate_ptr: *mut c_void, task: Task) { +/// tokio::spawn(async move { task.run() }); +/// } +/// +/// fn post_delayed_task(&self, isolate_ptr: *mut c_void, task: Task, delay: f64) { +/// tokio::spawn(async move { +/// tokio::time::sleep(Duration::from_secs_f64(delay)).await; +/// task.run(); +/// }); +/// } +/// ``` +/// +/// All methods have default implementations that run the task immediately +/// (synchronously). Override to integrate with your event loop. +/// +/// Implementations must be `Send + Sync` as callbacks may fire from any +/// thread. +#[allow(unused_variables)] +pub trait PlatformImpl: Send + Sync { + /// Called when `TaskRunner::PostTask` is invoked for the given isolate. + /// + /// The [`Task`] must be run on the isolate's foreground thread by calling + /// [`Task::run()`]. + /// + /// May be called from ANY thread (V8 background threads, etc.). + fn post_task(&self, isolate_ptr: *mut std::ffi::c_void, task: Task) { + task.run(); + } + + /// Called when `TaskRunner::PostNonNestableTask` is invoked. + /// + /// Same semantics as [`post_task`](Self::post_task), but the task must + /// not be run within a nested `PumpMessageLoop`. + fn post_non_nestable_task( + &self, + isolate_ptr: *mut std::ffi::c_void, + task: Task, + ) { + task.run(); + } + + /// Called when `TaskRunner::PostDelayedTask` is invoked. + /// + /// The task should be run after `delay_in_seconds` has elapsed. + /// For example, using `tokio::time::sleep` or a timer wheel. + /// + /// May be called from ANY thread. + fn post_delayed_task( + &self, + isolate_ptr: *mut std::ffi::c_void, + task: Task, + delay_in_seconds: f64, + ) { + task.run(); + } + + /// Called when `TaskRunner::PostNonNestableDelayedTask` is invoked. + /// + /// Same semantics as [`post_delayed_task`](Self::post_delayed_task). + fn post_non_nestable_delayed_task( + &self, + isolate_ptr: *mut std::ffi::c_void, + task: Task, + delay_in_seconds: f64, + ) { + task.run(); + } + + /// Called when `TaskRunner::PostIdleTask` is invoked. + /// + /// The [`IdleTask`] should be run when the embedder has idle time, + /// passing the deadline via [`IdleTask::run(deadline)`](IdleTask::run). + fn post_idle_task(&self, isolate_ptr: *mut std::ffi::c_void, task: IdleTask) { + task.run(0.0); + } +} + +// FFI callbacks called from C++ CustomPlatform/CustomTaskRunner. +// `context` is a raw pointer to a `Box`. +// Task pointers are owned — Rust is responsible for running and deleting them. + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostTask( + context: *mut std::ffi::c_void, + isolate: *mut std::ffi::c_void, + task: *mut std::ffi::c_void, +) { + let imp = unsafe { &*(context as *const Box) }; + imp.post_task(isolate, Task(task)); +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostNonNestableTask( + context: *mut std::ffi::c_void, + isolate: *mut std::ffi::c_void, + task: *mut std::ffi::c_void, +) { + let imp = unsafe { &*(context as *const Box) }; + imp.post_non_nestable_task(isolate, Task(task)); +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostDelayedTask( + context: *mut std::ffi::c_void, + isolate: *mut std::ffi::c_void, + task: *mut std::ffi::c_void, + delay_in_seconds: f64, +) { + let imp = unsafe { &*(context as *const Box) }; + imp.post_delayed_task(isolate, Task(task), delay_in_seconds); +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostNonNestableDelayedTask( + context: *mut std::ffi::c_void, + isolate: *mut std::ffi::c_void, + task: *mut std::ffi::c_void, + delay_in_seconds: f64, +) { + let imp = unsafe { &*(context as *const Box) }; + imp.post_non_nestable_delayed_task(isolate, Task(task), delay_in_seconds); +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostIdleTask( + context: *mut std::ffi::c_void, + isolate: *mut std::ffi::c_void, + task: *mut std::ffi::c_void, +) { + let imp = unsafe { &*(context as *const Box) }; + imp.post_idle_task(isolate, IdleTask(task)); +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__DROP( + context: *mut std::ffi::c_void, +) { + unsafe { + let _ = Box::from_raw(context as *mut Box); + } +} + +/// Returns a new instance of the default v8::Platform implementation. +/// +/// |thread_pool_size| is the number of worker threads to allocate for +/// background jobs. If a value of zero is passed, a suitable default +/// based on the current number of processors online will be chosen. +/// If |idle_task_support| is enabled then the platform will accept idle +/// tasks (IdleTasksEnabled will return true) and will rely on the embedder +/// calling v8::platform::RunIdleTasks to process the idle tasks. +/// +/// The default platform for v8 may include restrictions and caveats on thread +/// creation and initialization. This platform should only be used in cases +/// where v8 can be reliably initialized on the application's main thread, or +/// the parent thread to all threads in the system that will use v8. +/// +/// One example of a restriction is the use of Memory Protection Keys (pkeys) on +/// modern Linux systems using modern Intel/AMD processors. This particular +/// technology requires that all threads using v8 are created as descendent +/// threads of the thread that called `v8::Initialize`. +#[inline(always)] +pub fn new_default_platform( + thread_pool_size: u32, + idle_task_support: bool, +) -> UniqueRef { + Platform::new(thread_pool_size, idle_task_support) +} + +/// Creates a platform that is identical to the default platform, but does not +/// enforce thread-isolated allocations. This may reduce security in some cases, +/// so this method should be used with caution in cases where the threading +/// guarantees of `new_default_platform` cannot be upheld (generally for tests). +#[inline(always)] +pub fn new_unprotected_default_platform( + thread_pool_size: u32, + idle_task_support: bool, +) -> UniqueRef { + Platform::new_unprotected(thread_pool_size, idle_task_support) +} + +/// The same as new_default_platform() but disables the worker thread pool. +/// It must be used with the --single-threaded V8 flag. +/// +/// If |idle_task_support| is enabled then the platform will accept idle +/// tasks (IdleTasksEnabled will return true) and will rely on the embedder +/// calling v8::platform::RunIdleTasks to process the idle tasks. +#[inline(always)] +pub fn new_single_threaded_default_platform( + idle_task_support: bool, +) -> UniqueRef { + Platform::new_single_threaded(idle_task_support) +} + +/// Creates a custom platform backed by `DefaultPlatform` that transfers +/// foreground task ownership to the provided [`PlatformImpl`] trait object. +/// +/// Unlike the default platform, foreground tasks are NOT queued internally. +/// Instead, each `PostTask` / `PostDelayedTask` / etc. call transfers the +/// [`Task`] to Rust via the trait. The embedder is responsible for +/// scheduling and calling [`Task::run()`] on the isolate's thread. +/// +/// Background tasks (thread pool) are still handled by `DefaultPlatform`. +/// +/// When `unprotected` is true, thread-isolated allocations are disabled +/// (same as `new_unprotected_default_platform`). This is required when +/// isolates may be created on threads other than the one that called +/// `V8::initialize`. +#[inline(always)] +pub fn new_custom_platform( + thread_pool_size: u32, + idle_task_support: bool, + unprotected: bool, + platform_impl: impl PlatformImpl + 'static, +) -> UniqueRef { + Platform::new_custom( + thread_pool_size, + idle_task_support, + unprotected, + platform_impl, + ) +} + +impl Platform { + /// Returns a new instance of the default v8::Platform implementation. + /// + /// |thread_pool_size| is the number of worker threads to allocate for + /// background jobs. If a value of zero is passed, a suitable default + /// based on the current number of processors online will be chosen. + /// If |idle_task_support| is enabled then the platform will accept idle + /// tasks (IdleTasksEnabled will return true) and will rely on the embedder + /// calling v8::platform::RunIdleTasks to process the idle tasks. + /// + /// The default platform for v8 may include restrictions and caveats on thread + /// creation and initialization. This platform should only be used in cases + /// where v8 can be reliably initialized on the application's main thread, or + /// the parent thread to all threads in the system that will use v8. + /// + /// One example of a restriction is the use of Memory Protection Keys (pkeys) + /// on modern Linux systems using modern Intel/AMD processors. This particular + /// technology requires that all threads using v8 are created as descendent + /// threads of the thread that called `v8::Initialize`. + #[inline(always)] + pub fn new( + thread_pool_size: u32, + idle_task_support: bool, + ) -> UniqueRef { + unsafe { + UniqueRef::from_raw(v8__Platform__NewDefaultPlatform( + thread_pool_size.min(16) as i32, + idle_task_support, + )) + } + } + + /// Creates a platform that is identical to the default platform, but does not + /// enforce thread-isolated allocations. This may reduce security in some + /// cases, so this method should be used with caution in cases where the + /// threading guarantees of `new_default_platform` cannot be upheld (generally + /// for tests). + #[inline(always)] + pub fn new_unprotected( + thread_pool_size: u32, + idle_task_support: bool, + ) -> UniqueRef { + unsafe { + UniqueRef::from_raw(v8__Platform__NewUnprotectedDefaultPlatform( + thread_pool_size.min(16) as i32, + idle_task_support, + )) + } + } + + /// The same as new() but disables the worker thread pool. + /// It must be used with the --single-threaded V8 flag. + /// + /// If |idle_task_support| is enabled then the platform will accept idle + /// tasks (IdleTasksEnabled will return true) and will rely on the embedder + /// calling v8::platform::RunIdleTasks to process the idle tasks. + #[inline(always)] + pub fn new_single_threaded(idle_task_support: bool) -> UniqueRef { + unsafe { + UniqueRef::from_raw(v8__Platform__NewSingleThreadedDefaultPlatform( + idle_task_support, + )) + } + } + + /// Creates a custom platform that transfers foreground task ownership to + /// the provided [`PlatformImpl`] trait object. + /// + /// See [`new_custom_platform`] for details. + /// + /// The trait object is owned by the platform and will be dropped when the + /// platform is destroyed. + #[inline(always)] + pub fn new_custom( + thread_pool_size: u32, + idle_task_support: bool, + unprotected: bool, + platform_impl: impl PlatformImpl + 'static, + ) -> UniqueRef { + // Double-box: inner Box is a fat pointer, outer Box gives us a + // thin pointer we can pass through C++ void*. + let boxed: Box = Box::new(platform_impl); + let context = Box::into_raw(Box::new(boxed)) as *mut std::ffi::c_void; + // thread_pool_size clamping (0 → hardware_concurrency, max 16) is + // handled on the C++ side in v8__Platform__NewCustomPlatform. + unsafe { + UniqueRef::from_raw(v8__Platform__NewCustomPlatform( + thread_pool_size as i32, + idle_task_support, + unprotected, + context, + )) + } + } +} + +impl Platform { + /// Pumps the message loop for the given isolate. + /// + /// The caller has to make sure that this is called from the right thread. + /// Returns true if a task was executed, and false otherwise. If the call to + /// PumpMessageLoop is nested within another call to PumpMessageLoop, only + /// nestable tasks may run. Otherwise, any task may run. Unless requested through + /// the |wait_for_work| parameter, this call does not block if no task is pending. + #[inline(always)] + pub fn pump_message_loop( + platform: &SharedRef, + isolate: &Isolate, + wait_for_work: bool, + ) -> bool { + unsafe { + v8__Platform__PumpMessageLoop( + &**platform as *const Self as *mut _, + isolate.as_real_ptr(), + wait_for_work, + ) + } + } + + /// Runs pending idle tasks for at most |idle_time_in_seconds| seconds. + /// + /// The caller has to make sure that this is called from the right thread. + /// This call does not block if no task is pending. + #[inline(always)] + pub fn run_idle_tasks( + platform: &SharedRef, + isolate: &Isolate, + idle_time_in_seconds: f64, + ) { + unsafe { + v8__Platform__RunIdleTasks( + &**platform as *const Self as *mut _, + isolate.as_real_ptr(), + idle_time_in_seconds, + ); + } + } + + /// Notifies the given platform about the Isolate getting deleted soon. Has to + /// be called for all Isolates which are deleted - unless we're shutting down + /// the platform. + /// + /// The |platform| has to be created using |NewDefaultPlatform|. + #[inline(always)] + pub(crate) unsafe fn notify_isolate_shutdown( + platform: &SharedRef, + isolate: &Isolate, + ) { + unsafe { + v8__Platform__NotifyIsolateShutdown( + &**platform as *const Self as *mut _, + isolate.as_real_ptr(), + ); + } + } +} + +impl Shared for Platform { + fn from_unique_ptr(unique_ptr: UniquePtr) -> SharedPtrBase { + unsafe { + std__shared_ptr__v8__Platform__CONVERT__std__unique_ptr(unique_ptr) + } + } + fn get(ptr: &SharedPtrBase) -> *const Self { + unsafe { std__shared_ptr__v8__Platform__get(ptr) } + } + fn clone(ptr: &SharedPtrBase) -> SharedPtrBase { + unsafe { std__shared_ptr__v8__Platform__COPY(ptr) } + } + fn reset(ptr: &mut SharedPtrBase) { + unsafe { std__shared_ptr__v8__Platform__reset(ptr) } + } + fn use_count(ptr: &SharedPtrBase) -> long { + unsafe { std__shared_ptr__v8__Platform__use_count(ptr) } + } +} + +impl Drop for Platform { + fn drop(&mut self) { + unsafe { v8__Platform__DELETE(self) }; + } +} diff --git a/crates/v8/src/primitive_array.rs b/crates/v8/src/primitive_array.rs new file mode 100644 index 0000000000..121e1d8905 --- /dev/null +++ b/crates/v8/src/primitive_array.rs @@ -0,0 +1,82 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +use crate::Local; +use crate::Primitive; +use crate::PrimitiveArray; +use crate::isolate::RealIsolate; +use crate::scope::GetIsolate; +use crate::scope::PinScope; +use crate::support::int; + +unsafe extern "C" { + fn v8__PrimitiveArray__New( + isolate: *mut RealIsolate, + length: int, + ) -> *const PrimitiveArray; + + fn v8__PrimitiveArray__Length(this: *const PrimitiveArray) -> int; + + fn v8__PrimitiveArray__Set( + this: *const PrimitiveArray, + isolate: *mut RealIsolate, + index: int, + item: *const Primitive, + ); + + fn v8__PrimitiveArray__Get( + this: *const PrimitiveArray, + isolate: *mut RealIsolate, + index: int, + ) -> *const Primitive; +} + +impl PrimitiveArray { + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_>, + length: usize, + ) -> Local<'s, PrimitiveArray> { + unsafe { + scope.cast_local(|sd| { + v8__PrimitiveArray__New(sd.get_isolate_ptr(), length as int) + }) + } + .unwrap() + } + + #[inline(always)] + pub fn length(&self) -> usize { + unsafe { v8__PrimitiveArray__Length(self) as usize } + } + + #[inline(always)] + pub fn set( + &self, + scope: &PinScope<'_, '_>, + index: usize, + item: Local<'_, Primitive>, + ) { + unsafe { + v8__PrimitiveArray__Set( + self, + scope.get_isolate_ptr(), + index as int, + &*item, + ); + } + } + + #[inline(always)] + pub fn get<'s>( + &self, + scope: &PinScope<'s, '_>, + index: usize, + ) -> Local<'s, Primitive> { + unsafe { + scope.cast_local(|sd| { + v8__PrimitiveArray__Get(self, sd.get_isolate_ptr(), index as int) + }) + } + .unwrap() + } +} diff --git a/crates/v8/src/primitives.rs b/crates/v8/src/primitives.rs new file mode 100644 index 0000000000..fe144e5d76 --- /dev/null +++ b/crates/v8/src/primitives.rs @@ -0,0 +1,53 @@ +use crate::Boolean; +use crate::Local; +use crate::Primitive; +use crate::isolate::Isolate; +use crate::isolate::RealIsolate; + +unsafe extern "C" { + fn v8__Null(isolate: *mut RealIsolate) -> *const Primitive; + fn v8__Undefined(isolate: *mut RealIsolate) -> *const Primitive; + + fn v8__Boolean__New(isolate: *mut RealIsolate, value: bool) + -> *const Boolean; + fn v8__Boolean__Value(this: *const Boolean) -> bool; +} + +#[inline(always)] +pub fn null<'s, R>(scope: &R) -> Local<'s, Primitive> +where + R: AsRef, +{ + unsafe { Local::from_raw_unchecked(v8__Null(scope.as_ref().as_real_ptr())) } +} + +#[inline(always)] +pub fn undefined<'s, R>(scope: &R) -> Local<'s, Primitive> +where + R: AsRef, +{ + unsafe { + Local::from_raw_unchecked(v8__Undefined(scope.as_ref().as_real_ptr())) + } +} + +impl Boolean { + #[inline(always)] + pub fn new<'s, R>(scope: &R, value: bool) -> Local<'s, Boolean> + where + R: AsRef, + { + unsafe { + Local::from_raw_unchecked(v8__Boolean__New( + scope.as_ref().as_real_ptr(), + value, + )) + } + } + + /// Returns the value of this boolean. + #[inline(always)] + pub fn value(&self) -> bool { + unsafe { v8__Boolean__Value(self) } + } +} diff --git a/crates/v8/src/private.rs b/crates/v8/src/private.rs new file mode 100644 index 0000000000..0293fa3396 --- /dev/null +++ b/crates/v8/src/private.rs @@ -0,0 +1,66 @@ +use crate::Local; +use crate::Private; +use crate::String; +use crate::Value; +use crate::isolate::RealIsolate; +use crate::scope::PinScope; + +unsafe extern "C" { + fn v8__Private__New( + isolate: *mut RealIsolate, + name: *const String, + ) -> *const Private; + fn v8__Private__ForApi( + isolate: *mut RealIsolate, + name: *const String, + ) -> *const Private; + fn v8__Private__Name(this: *const Private) -> *const Value; +} + +impl Private { + /// Create a private symbol. If name is not empty, it will be the description. + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_, ()>, + name: Option>, + ) -> Local<'s, Private> { + unsafe { + scope.cast_local(|sd| { + v8__Private__New( + sd.get_isolate_ptr(), + name.map_or_else(std::ptr::null, |v| &*v), + ) + }) + } + .unwrap() + } + + /// Retrieve a global private symbol. If a symbol with this name has not + /// been retrieved in the same isolate before, it is created. + /// Note that private symbols created this way are never collected, so + /// they should only be used for statically fixed properties. + /// Also, there is only one global name space for the names used as keys. + /// To minimize the potential for clashes, use qualified names as keys, + /// e.g., "Class#property". + #[inline(always)] + pub fn for_api<'s>( + scope: &PinScope<'s, '_, ()>, + name: Option>, + ) -> Local<'s, Private> { + unsafe { + scope.cast_local(|sd| { + v8__Private__ForApi( + sd.get_isolate_ptr(), + name.map_or_else(std::ptr::null, |v| &*v), + ) + }) + } + .unwrap() + } + + /// Returns the print name string of the private symbol, or undefined if none. + #[inline(always)] + pub fn name<'s>(&self, scope: &PinScope<'s, '_, ()>) -> Local<'s, Value> { + unsafe { scope.cast_local(|_| v8__Private__Name(self)) }.unwrap() + } +} diff --git a/crates/v8/src/promise.rs b/crates/v8/src/promise.rs new file mode 100644 index 0000000000..383b57e5e9 --- /dev/null +++ b/crates/v8/src/promise.rs @@ -0,0 +1,240 @@ +use std::marker::PhantomData; + +use crate::Context; +use crate::Function; +use crate::Local; +use crate::Promise; +use crate::PromiseResolver; +use crate::Value; +use crate::scope::PinScope; +use crate::support::MaybeBool; + +unsafe extern "C" { + fn v8__Promise__Resolver__New( + context: *const Context, + ) -> *const PromiseResolver; + fn v8__Promise__Resolver__GetPromise( + this: *const PromiseResolver, + ) -> *const Promise; + fn v8__Promise__Resolver__Resolve( + this: *const PromiseResolver, + context: *const Context, + value: *const Value, + ) -> MaybeBool; + fn v8__Promise__Resolver__Reject( + this: *const PromiseResolver, + context: *const Context, + value: *const Value, + ) -> MaybeBool; + fn v8__Promise__State(this: *const Promise) -> PromiseState; + fn v8__Promise__HasHandler(this: *const Promise) -> bool; + fn v8__Promise__MarkAsHandled(this: *const Promise); + fn v8__Promise__Result(this: *const Promise) -> *const Value; + fn v8__Promise__Catch( + this: *const Promise, + context: *const Context, + handler: *const Function, + ) -> *const Promise; + fn v8__Promise__Then( + this: *const Promise, + context: *const Context, + handler: *const Function, + ) -> *const Promise; + fn v8__Promise__Then2( + this: *const Promise, + context: *const Context, + on_fulfilled: *const Function, + on_rejected: *const Function, + ) -> *const Promise; + + fn v8__PromiseRejectMessage__GetPromise( + this: *const PromiseRejectMessage, + ) -> *const Promise; + fn v8__PromiseRejectMessage__GetValue( + this: *const PromiseRejectMessage, + ) -> *const Value; + fn v8__PromiseRejectMessage__GetEvent( + this: *const PromiseRejectMessage, + ) -> PromiseRejectEvent; +} + +#[derive(Debug, PartialEq, Eq)] +#[repr(C)] +pub enum PromiseState { + Pending, + Fulfilled, + Rejected, +} + +impl Promise { + /// Returns the value of the [[PromiseState]] field. + #[inline(always)] + pub fn state(&self) -> PromiseState { + unsafe { v8__Promise__State(self) } + } + + /// Returns true if the promise has at least one derived promise, and + /// therefore resolve/reject handlers (including default handler). + #[inline(always)] + pub fn has_handler(&self) -> bool { + unsafe { v8__Promise__HasHandler(self) } + } + + /// Marks this promise as handled to avoid reporting unhandled rejections. + #[inline(always)] + pub fn mark_as_handled(&self) { + unsafe { v8__Promise__MarkAsHandled(self) } + } + + /// Returns the content of the [[PromiseResult]] field. The Promise must not + /// be pending. + #[inline(always)] + pub fn result<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, Value> { + unsafe { scope.cast_local(|_| v8__Promise__Result(self)) }.unwrap() + } + + /// Register a rejection handler with a promise. + /// + /// See `Self::then2`. + #[inline(always)] + pub fn catch<'s>( + &self, + scope: &PinScope<'s, '_>, + handler: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Promise__Catch(self, sd.get_current_context(), &*handler) + }) + } + } + + /// Register a resolution handler with a promise. + /// + /// See `Self::then2`. + #[inline(always)] + pub fn then<'s>( + &self, + scope: &PinScope<'s, '_>, + handler: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Promise__Then(self, sd.get_current_context(), &*handler) + }) + } + } + + /// Register a resolution/rejection handler with a promise. + /// The handler is given the respective resolution/rejection value as + /// an argument. If the promise is already resolved/rejected, the handler is + /// invoked at the end of turn. + #[inline(always)] + pub fn then2<'s>( + &self, + scope: &PinScope<'s, '_>, + on_fulfilled: Local, + on_rejected: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Promise__Then2( + self, + sd.get_current_context(), + &*on_fulfilled, + &*on_rejected, + ) + }) + } + } +} + +impl PromiseResolver { + /// Create a new resolver, along with an associated promise in pending state. + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope + .cast_local(|sd| v8__Promise__Resolver__New(sd.get_current_context())) + } + } + + /// Extract the associated promise. + #[inline(always)] + pub fn get_promise<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Local<'s, Promise> { + unsafe { scope.cast_local(|_| v8__Promise__Resolver__GetPromise(self)) } + .unwrap() + } + + /// Resolve the associated promise with a given value. + /// Ignored if the promise is no longer pending. + #[inline(always)] + pub fn resolve( + &self, + scope: &PinScope<'_, '_>, + value: Local<'_, Value>, + ) -> Option { + unsafe { + v8__Promise__Resolver__Resolve( + self, + &*scope.get_current_context(), + &*value, + ) + .into() + } + } + + /// Reject the associated promise with a given value. + /// Ignored if the promise is no longer pending. + #[inline(always)] + pub fn reject( + &self, + scope: &PinScope<'_, '_>, + value: Local<'_, Value>, + ) -> Option { + unsafe { + v8__Promise__Resolver__Reject( + self, + &*scope.get_current_context(), + &*value, + ) + .into() + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(C)] +pub enum PromiseRejectEvent { + PromiseRejectWithNoHandler, + PromiseHandlerAddedAfterReject, + PromiseRejectAfterResolved, + PromiseResolveAfterResolved, +} + +#[derive(Clone, Copy, Debug)] +#[repr(C)] +pub struct PromiseRejectMessage<'msg>([usize; 3], PhantomData<&'msg ()>); + +impl<'msg> PromiseRejectMessage<'msg> { + #[inline(always)] + pub fn get_promise(&self) -> Local<'msg, Promise> { + unsafe { Local::from_raw(v8__PromiseRejectMessage__GetPromise(self)) } + .unwrap() + } + + #[inline(always)] + pub fn get_event(&self) -> PromiseRejectEvent { + unsafe { v8__PromiseRejectMessage__GetEvent(self) } + } + + #[inline(always)] + pub fn get_value(&self) -> Option> { + unsafe { Local::from_raw(v8__PromiseRejectMessage__GetValue(self)) } + } +} diff --git a/crates/v8/src/property_attribute.rs b/crates/v8/src/property_attribute.rs new file mode 100644 index 0000000000..c980d658ac --- /dev/null +++ b/crates/v8/src/property_attribute.rs @@ -0,0 +1,115 @@ +#[repr(C)] +#[derive(Debug, Eq, PartialEq)] +pub struct PropertyAttribute(u32); + +impl PropertyAttribute { + /// No property attributes. + pub const NONE: Self = Self(0); + + /// Not writable. Corresponds to + /// `Object.defineProperty(o, "p", { writable: false })`. + pub const READ_ONLY: Self = Self(1 << 0); + + /// Not enumerable. Corresponds to + /// `Object.defineProperty(o, "p", { enumerable: false })`. + pub const DONT_ENUM: Self = Self(1 << 1); + + /// Not configurable. Corresponds to + /// `Object.defineProperty(o, "p", { configurable: false })`. + pub const DONT_DELETE: Self = Self(1 << 2); + + /// Test if no property attributes are set. + #[inline(always)] + pub fn is_none(&self) -> bool { + *self == PropertyAttribute::NONE + } + + /// Test if the read-only property attribute is set. + #[inline(always)] + pub fn is_read_only(&self) -> bool { + self.has(Self::READ_ONLY) + } + + /// Test if the non-enumerable property attribute is set. + #[inline(always)] + pub fn is_dont_enum(&self) -> bool { + self.has(Self::DONT_ENUM) + } + + /// Test if the non-configurable property attribute is set. + #[inline(always)] + pub fn is_dont_delete(&self) -> bool { + self.has(Self::DONT_DELETE) + } + + #[inline(always)] + fn has(&self, that: Self) -> bool { + let Self(lhs) = self; + let Self(rhs) = that; + 0 != lhs & rhs + } + + pub fn as_u32(&self) -> u32 { + let Self(bits) = self; + *bits + } +} + +// Identical to #[derive(Default)] but arguably clearer when made explicit. +impl Default for PropertyAttribute { + fn default() -> Self { + Self::NONE + } +} + +impl std::ops::BitOr for PropertyAttribute { + type Output = Self; + + fn bitor(self, Self(rhs): Self) -> Self { + let Self(lhs) = self; + Self(lhs | rhs) + } +} + +#[test] +fn test_attr() { + assert!(PropertyAttribute::NONE.is_none()); + assert!(!PropertyAttribute::NONE.is_read_only()); + assert!(!PropertyAttribute::NONE.is_dont_enum()); + assert!(!PropertyAttribute::NONE.is_dont_delete()); + + assert!(!PropertyAttribute::READ_ONLY.is_none()); + assert!(PropertyAttribute::READ_ONLY.is_read_only()); + assert!(!PropertyAttribute::READ_ONLY.is_dont_enum()); + assert!(!PropertyAttribute::READ_ONLY.is_dont_delete()); + + assert!(!PropertyAttribute::DONT_ENUM.is_none()); + assert!(!PropertyAttribute::DONT_ENUM.is_read_only()); + assert!(PropertyAttribute::DONT_ENUM.is_dont_enum()); + assert!(!PropertyAttribute::DONT_ENUM.is_dont_delete()); + + assert!(!PropertyAttribute::DONT_DELETE.is_none()); + assert!(!PropertyAttribute::DONT_DELETE.is_read_only()); + assert!(!PropertyAttribute::DONT_DELETE.is_dont_enum()); + assert!(PropertyAttribute::DONT_DELETE.is_dont_delete()); + + assert_eq!(PropertyAttribute::NONE, Default::default()); + assert_eq!( + PropertyAttribute::READ_ONLY, + PropertyAttribute::NONE | PropertyAttribute::READ_ONLY + ); + + let attr = PropertyAttribute::READ_ONLY | PropertyAttribute::DONT_ENUM; + assert!(!attr.is_none()); + assert!(attr.is_read_only()); + assert!(attr.is_dont_enum()); + assert!(!attr.is_dont_delete()); + + let attr = PropertyAttribute::READ_ONLY + | PropertyAttribute::READ_ONLY + | PropertyAttribute::DONT_ENUM; + assert!(!attr.is_none()); + assert!(attr.is_read_only()); + assert!(attr.is_dont_enum()); + assert!(!attr.is_dont_delete()); +} diff --git a/crates/v8/src/property_descriptor.rs b/crates/v8/src/property_descriptor.rs new file mode 100644 index 0000000000..0b0ef93725 --- /dev/null +++ b/crates/v8/src/property_descriptor.rs @@ -0,0 +1,181 @@ +use std::mem::MaybeUninit; +use std::mem::size_of; + +use crate::Local; +use crate::Value; + +unsafe extern "C" { + fn v8__PropertyDescriptor__CONSTRUCT(out: *mut PropertyDescriptor); + fn v8__PropertyDescriptor__CONSTRUCT__Value( + this: *const PropertyDescriptor, + value: *const Value, + ); + fn v8__PropertyDescriptor__CONSTRUCT__Value_Writable( + this: *const PropertyDescriptor, + value: *const Value, + writable: bool, + ); + fn v8__PropertyDescriptor__CONSTRUCT__Get_Set( + this: *const PropertyDescriptor, + get: *const Value, + set: *const Value, + ); + fn v8__PropertyDescriptor__DESTRUCT(this: *mut PropertyDescriptor); + fn v8__PropertyDescriptor__configurable( + this: *const PropertyDescriptor, + ) -> bool; + fn v8__PropertyDescriptor__enumerable( + this: *const PropertyDescriptor, + ) -> bool; + fn v8__PropertyDescriptor__writable(this: *const PropertyDescriptor) -> bool; + fn v8__PropertyDescriptor__value( + this: *const PropertyDescriptor, + ) -> *const Value; + fn v8__PropertyDescriptor__get( + this: *const PropertyDescriptor, + ) -> *const Value; + fn v8__PropertyDescriptor__set( + this: *const PropertyDescriptor, + ) -> *const Value; + fn v8__PropertyDescriptor__has_configurable( + this: *const PropertyDescriptor, + ) -> bool; + fn v8__PropertyDescriptor__has_enumerable( + this: *const PropertyDescriptor, + ) -> bool; + fn v8__PropertyDescriptor__has_writable( + this: *const PropertyDescriptor, + ) -> bool; + fn v8__PropertyDescriptor__has_value(this: *const PropertyDescriptor) + -> bool; + fn v8__PropertyDescriptor__has_get(this: *const PropertyDescriptor) -> bool; + fn v8__PropertyDescriptor__has_set(this: *const PropertyDescriptor) -> bool; + fn v8__PropertyDescriptor__set_enumerable( + this: *mut PropertyDescriptor, + enumerable: bool, + ); + fn v8__PropertyDescriptor__set_configurable( + this: *mut PropertyDescriptor, + configurable: bool, + ); +} + +#[repr(transparent)] +pub struct PropertyDescriptor([usize; 1]); + +const _: () = { + assert!( + size_of::() == size_of::(), + "PropertyDescriptor size is not 1 usize" + ); +}; + +impl Default for PropertyDescriptor { + fn default() -> Self { + Self::new() + } +} + +impl PropertyDescriptor { + pub fn new() -> Self { + let mut this = MaybeUninit::::uninit(); + unsafe { + v8__PropertyDescriptor__CONSTRUCT(this.as_mut_ptr()); + this.assume_init() + } + } + + pub fn new_from_value(value: Local) -> Self { + let mut this = MaybeUninit::::uninit(); + unsafe { + v8__PropertyDescriptor__CONSTRUCT__Value(this.as_mut_ptr(), &*value); + this.assume_init() + } + } + + pub fn new_from_value_writable(value: Local, writable: bool) -> Self { + let mut this = MaybeUninit::::uninit(); + unsafe { + v8__PropertyDescriptor__CONSTRUCT__Value_Writable( + this.as_mut_ptr(), + &*value, + writable, + ); + this.assume_init() + } + } + + pub fn new_from_get_set(get: Local, set: Local) -> Self { + let mut this = MaybeUninit::::uninit(); + unsafe { + v8__PropertyDescriptor__CONSTRUCT__Get_Set( + this.as_mut_ptr(), + &*get, + &*set, + ); + this.assume_init() + } + } + + pub fn configurable(&self) -> bool { + unsafe { v8__PropertyDescriptor__configurable(self) } + } + + pub fn enumerable(&self) -> bool { + unsafe { v8__PropertyDescriptor__enumerable(self) } + } + + pub fn writable(&self) -> bool { + unsafe { v8__PropertyDescriptor__writable(self) } + } + + pub fn value(&self) -> Local<'_, Value> { + unsafe { Local::from_raw(v8__PropertyDescriptor__value(self)) }.unwrap() + } + + pub fn get(&self) -> Local<'_, Value> { + unsafe { Local::from_raw(v8__PropertyDescriptor__get(self)) }.unwrap() + } + + pub fn set(&self) -> Local<'_, Value> { + unsafe { Local::from_raw(v8__PropertyDescriptor__set(self)) }.unwrap() + } + + pub fn has_configurable(&self) -> bool { + unsafe { v8__PropertyDescriptor__has_configurable(self) } + } + + pub fn has_enumerable(&self) -> bool { + unsafe { v8__PropertyDescriptor__has_enumerable(self) } + } + + pub fn has_writable(&self) -> bool { + unsafe { v8__PropertyDescriptor__has_writable(self) } + } + + pub fn has_value(&self) -> bool { + unsafe { v8__PropertyDescriptor__has_value(self) } + } + + pub fn has_get(&self) -> bool { + unsafe { v8__PropertyDescriptor__has_get(self) } + } + + pub fn has_set(&self) -> bool { + unsafe { v8__PropertyDescriptor__has_set(self) } + } + + pub fn set_enumerable(&mut self, enumerable: bool) { + unsafe { v8__PropertyDescriptor__set_enumerable(self, enumerable) } + } + + pub fn set_configurable(&mut self, configurable: bool) { + unsafe { v8__PropertyDescriptor__set_configurable(self, configurable) } + } +} + +impl Drop for PropertyDescriptor { + fn drop(&mut self) { + unsafe { v8__PropertyDescriptor__DESTRUCT(self) } + } +} diff --git a/crates/v8/src/property_filter.rs b/crates/v8/src/property_filter.rs new file mode 100644 index 0000000000..0268a69d97 --- /dev/null +++ b/crates/v8/src/property_filter.rs @@ -0,0 +1,137 @@ +#[repr(C)] +#[derive(Debug, Eq, PartialEq, Clone, Copy)] +pub struct PropertyFilter(u32); + +impl PropertyFilter { + pub const ALL_PROPERTIES: PropertyFilter = PropertyFilter(0); + + pub const ONLY_WRITABLE: PropertyFilter = PropertyFilter(1 << 0); + + pub const ONLY_ENUMERABLE: PropertyFilter = PropertyFilter(1 << 1); + + pub const ONLY_CONFIGURABLE: PropertyFilter = PropertyFilter(1 << 2); + + pub const SKIP_STRINGS: PropertyFilter = PropertyFilter(1 << 3); + + pub const SKIP_SYMBOLS: PropertyFilter = PropertyFilter(1 << 4); + + /// Test if all property filters are set. + #[inline(always)] + pub fn is_all_properties(&self) -> bool { + *self == Self::ALL_PROPERTIES + } + + /// Test if the only-writable property filter is set. + #[inline(always)] + pub fn is_only_writable(&self) -> bool { + self.has(Self::ONLY_WRITABLE) + } + + /// Test if the only-enumerable property filter is set. + #[inline(always)] + pub fn is_only_enumerable(&self) -> bool { + self.has(Self::ONLY_ENUMERABLE) + } + + /// Test if the only-configurable property filter is set. + #[inline(always)] + pub fn is_only_configurable(&self) -> bool { + self.has(Self::ONLY_CONFIGURABLE) + } + + /// Test if the skip-strings property filter is set. + #[inline(always)] + pub fn is_skip_strings(&self) -> bool { + self.has(Self::SKIP_STRINGS) + } + + /// Test if the skip-symbols property filter is set. + #[inline(always)] + pub fn is_skip_symbols(&self) -> bool { + self.has(Self::SKIP_SYMBOLS) + } + + #[inline(always)] + fn has(&self, that: Self) -> bool { + let Self(lhs) = self; + let Self(rhs) = that; + 0 != lhs & rhs + } +} + +// Identical to #[derive(Default)] but arguably clearer when made explicit. +impl Default for PropertyFilter { + fn default() -> Self { + Self::ALL_PROPERTIES + } +} + +impl std::ops::BitOr for PropertyFilter { + type Output = Self; + + fn bitor(self, Self(rhs): Self) -> Self { + let Self(lhs) = self; + Self(lhs | rhs) + } +} + +#[test] +fn test_attr() { + assert!(PropertyFilter::ALL_PROPERTIES.is_all_properties()); + assert!(!PropertyFilter::ALL_PROPERTIES.is_only_writable()); + assert!(!PropertyFilter::ALL_PROPERTIES.is_only_enumerable()); + assert!(!PropertyFilter::ALL_PROPERTIES.is_only_configurable()); + assert!(!PropertyFilter::ALL_PROPERTIES.is_skip_strings()); + assert!(!PropertyFilter::ALL_PROPERTIES.is_skip_symbols()); + + assert!(!PropertyFilter::ONLY_WRITABLE.is_all_properties()); + assert!(PropertyFilter::ONLY_WRITABLE.is_only_writable()); + assert!(!PropertyFilter::ONLY_WRITABLE.is_only_enumerable()); + assert!(!PropertyFilter::ONLY_WRITABLE.is_only_configurable()); + assert!(!PropertyFilter::ONLY_WRITABLE.is_skip_strings()); + assert!(!PropertyFilter::ONLY_WRITABLE.is_skip_symbols()); + + assert!(!PropertyFilter::ONLY_ENUMERABLE.is_all_properties()); + assert!(!PropertyFilter::ONLY_ENUMERABLE.is_only_writable()); + assert!(PropertyFilter::ONLY_ENUMERABLE.is_only_enumerable()); + assert!(!PropertyFilter::ONLY_ENUMERABLE.is_only_configurable()); + assert!(!PropertyFilter::ONLY_ENUMERABLE.is_skip_strings()); + assert!(!PropertyFilter::ONLY_ENUMERABLE.is_skip_symbols()); + + assert!(!PropertyFilter::ONLY_CONFIGURABLE.is_all_properties()); + assert!(!PropertyFilter::ONLY_CONFIGURABLE.is_only_writable()); + assert!(!PropertyFilter::ONLY_CONFIGURABLE.is_only_enumerable()); + assert!(PropertyFilter::ONLY_CONFIGURABLE.is_only_configurable()); + assert!(!PropertyFilter::ONLY_CONFIGURABLE.is_skip_strings()); + assert!(!PropertyFilter::ONLY_CONFIGURABLE.is_skip_symbols()); + + assert!(!PropertyFilter::SKIP_STRINGS.is_all_properties()); + assert!(!PropertyFilter::SKIP_STRINGS.is_only_writable()); + assert!(!PropertyFilter::SKIP_STRINGS.is_only_enumerable()); + assert!(!PropertyFilter::SKIP_STRINGS.is_only_configurable()); + assert!(PropertyFilter::SKIP_STRINGS.is_skip_strings()); + assert!(!PropertyFilter::SKIP_STRINGS.is_skip_symbols()); + + assert!(!PropertyFilter::SKIP_SYMBOLS.is_all_properties()); + assert!(!PropertyFilter::SKIP_SYMBOLS.is_only_writable()); + assert!(!PropertyFilter::SKIP_SYMBOLS.is_only_enumerable()); + assert!(!PropertyFilter::SKIP_SYMBOLS.is_only_configurable()); + assert!(!PropertyFilter::SKIP_SYMBOLS.is_skip_strings()); + assert!(PropertyFilter::SKIP_SYMBOLS.is_skip_symbols()); + + assert_eq!(PropertyFilter::ALL_PROPERTIES, Default::default()); + assert_eq!( + PropertyFilter::ONLY_WRITABLE, + PropertyFilter::ALL_PROPERTIES | PropertyFilter::ONLY_WRITABLE + ); + + let attr = PropertyFilter::ONLY_WRITABLE + | PropertyFilter::ONLY_WRITABLE + | PropertyFilter::SKIP_STRINGS; + assert!(!attr.is_all_properties()); + assert!(attr.is_only_writable()); + assert!(!attr.is_only_enumerable()); + assert!(!attr.is_only_configurable()); + assert!(attr.is_skip_strings()); + assert!(!attr.is_skip_symbols()); +} diff --git a/crates/v8/src/property_handler_flags.rs b/crates/v8/src/property_handler_flags.rs new file mode 100644 index 0000000000..f0ae28dc8d --- /dev/null +++ b/crates/v8/src/property_handler_flags.rs @@ -0,0 +1,108 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +#[repr(C)] +#[derive(Debug, Eq, PartialEq)] +pub struct PropertyHandlerFlags(u32); + +impl PropertyHandlerFlags { + /// None. + pub const NONE: Self = Self(0); + + /// Will not call into interceptor for properties on the receiver or prototype + /// chain, i.e., only call into interceptor for properties that do not exist. + /// Currently only valid for named interceptors. + pub const NON_MASKING: Self = Self(1 << 0); + + /// Will not call into interceptor for symbol lookup. Only meaningful for + /// named interceptors. + pub const ONLY_INTERCEPT_STRINGS: Self = Self(1 << 1); + + /// The getter, query, enumerator callbacks do not produce side effects. + pub const HAS_NO_SIDE_EFFECT: Self = Self(1 << 2); + + /// Test if no property handler flags are set. + #[inline(always)] + pub fn is_none(&self) -> bool { + *self == Self::NONE + } + + /// Test if the non-masking property handler flag is set. + #[inline(always)] + pub fn is_non_masking(&self) -> bool { + self.has(Self::NON_MASKING) + } + + /// Test if the only-intercept-strings property handler flag is set. + #[inline(always)] + pub fn is_only_intercept_strings(&self) -> bool { + self.has(Self::ONLY_INTERCEPT_STRINGS) + } + + /// Test if the has-no-side-effect property handler flag is set. + #[inline(always)] + pub fn is_has_no_side_effect(&self) -> bool { + self.has(Self::HAS_NO_SIDE_EFFECT) + } + + #[inline(always)] + fn has(&self, that: Self) -> bool { + let Self(lhs) = self; + let Self(rhs) = that; + 0 != lhs & rhs + } +} + +// Identical to #[derive(Default)] but arguably clearer when made explicit. +impl Default for PropertyHandlerFlags { + fn default() -> Self { + Self::NONE + } +} + +impl std::ops::BitOr for PropertyHandlerFlags { + type Output = Self; + + fn bitor(self, Self(rhs): Self) -> Self { + let Self(lhs) = self; + Self(lhs | rhs) + } +} + +#[test] +fn test_attr() { + assert!(PropertyHandlerFlags::NONE.is_none()); + assert!(!PropertyHandlerFlags::NONE.is_non_masking()); + assert!(!PropertyHandlerFlags::NONE.is_only_intercept_strings()); + assert!(!PropertyHandlerFlags::NONE.is_has_no_side_effect()); + + assert!(!PropertyHandlerFlags::NON_MASKING.is_none()); + assert!(PropertyHandlerFlags::NON_MASKING.is_non_masking()); + assert!(!PropertyHandlerFlags::NON_MASKING.is_only_intercept_strings()); + assert!(!PropertyHandlerFlags::NON_MASKING.is_has_no_side_effect()); + + assert!(!PropertyHandlerFlags::ONLY_INTERCEPT_STRINGS.is_none()); + assert!(!PropertyHandlerFlags::ONLY_INTERCEPT_STRINGS.is_non_masking()); + assert!( + PropertyHandlerFlags::ONLY_INTERCEPT_STRINGS.is_only_intercept_strings() + ); + assert!( + !PropertyHandlerFlags::ONLY_INTERCEPT_STRINGS.is_has_no_side_effect() + ); + + assert!(!PropertyHandlerFlags::HAS_NO_SIDE_EFFECT.is_none()); + assert!(!PropertyHandlerFlags::HAS_NO_SIDE_EFFECT.is_non_masking()); + assert!( + !PropertyHandlerFlags::HAS_NO_SIDE_EFFECT.is_only_intercept_strings() + ); + assert!(PropertyHandlerFlags::HAS_NO_SIDE_EFFECT.is_has_no_side_effect()); + + assert_eq!(PropertyHandlerFlags::NONE, Default::default()); + + let attr = PropertyHandlerFlags::ONLY_INTERCEPT_STRINGS + | PropertyHandlerFlags::HAS_NO_SIDE_EFFECT + | PropertyHandlerFlags::NON_MASKING; + assert!(!attr.is_none()); + assert!(attr.is_non_masking()); + assert!(attr.is_only_intercept_strings()); + assert!(attr.is_has_no_side_effect()); +} diff --git a/crates/v8/src/proxy.rs b/crates/v8/src/proxy.rs new file mode 100644 index 0000000000..2de7889ef3 --- /dev/null +++ b/crates/v8/src/proxy.rs @@ -0,0 +1,53 @@ +use crate::Context; +use crate::Local; +use crate::Object; +use crate::Proxy; +use crate::Value; +use crate::scope::PinScope; + +unsafe extern "C" { + fn v8__Proxy__New( + context: *const Context, + target: *const Object, + handler: *const Object, + ) -> *const Proxy; + fn v8__Proxy__GetHandler(this: *const Proxy) -> *const Value; + fn v8__Proxy__GetTarget(this: *const Proxy) -> *const Value; + fn v8__Proxy__IsRevoked(this: *const Proxy) -> bool; + fn v8__Proxy__Revoke(this: *const Proxy); +} + +impl Proxy { + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_>, + target: Local, + handler: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Proxy__New(sd.get_current_context(), &*target, &*handler) + }) + } + } + + #[inline(always)] + pub fn get_handler<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, Value> { + unsafe { scope.cast_local(|_| v8__Proxy__GetHandler(self)) }.unwrap() + } + + #[inline(always)] + pub fn get_target<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, Value> { + unsafe { scope.cast_local(|_| v8__Proxy__GetTarget(self)) }.unwrap() + } + + #[inline(always)] + pub fn is_revoked(&self) -> bool { + unsafe { v8__Proxy__IsRevoked(self) } + } + + #[inline(always)] + pub fn revoke(&self) { + unsafe { v8__Proxy__Revoke(self) }; + } +} diff --git a/crates/v8/src/regexp.rs b/crates/v8/src/regexp.rs new file mode 100644 index 0000000000..df8c92f322 --- /dev/null +++ b/crates/v8/src/regexp.rs @@ -0,0 +1,131 @@ +use crate::Context; +use crate::Local; +use crate::Object; +use crate::RegExp; +use crate::String; +use crate::scope::PinScope; +use crate::support::int; + +bitflags! { + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + #[repr(transparent)] + pub struct RegExpCreationFlags: int { + const GLOBAL = 1 << 0; + const IGNORE_CASE = 1 << 1; + const MULTILINE = 1 << 2; + const STICKY = 1 << 3; + const UNICODE = 1 << 4; + const DOT_ALL = 1 << 5; + const LINEAR = 1 << 6; + const HAS_INDICES = 1 << 7; + const UNICODE_SETS = 1 << 8; + } +} + +unsafe extern "C" { + fn v8__RegExp__New( + context: *const Context, + pattern: *const String, + flags: RegExpCreationFlags, + ) -> *const RegExp; + fn v8__RegExp__NewWithBacktrackLimit( + context: *const Context, + pattern: *const String, + flags: RegExpCreationFlags, + backtrack_limit: u32, + ) -> *const RegExp; + fn v8__RegExp__MAX_BACKTRACK_LIMIT() -> u32; + fn v8__RegExp__Exec( + this: *const RegExp, + context: *const Context, + subject: *const String, + ) -> *const Object; + fn v8__RegExp__GetSource(this: *const RegExp) -> *const String; +} + +impl RegExp { + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_>, + pattern: Local, + flags: RegExpCreationFlags, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__RegExp__New(sd.get_current_context(), &*pattern, flags) + }) + } + } + + /// The largest backtrack limit [`new_with_backtrack_limit`] accepts. + /// + /// V8 requires the limit to be a valid Smi, so this is `2^30 - 1` on + /// pointer-compressed builds and `2^31 - 1` otherwise. + /// + /// [`new_with_backtrack_limit`]: RegExp::new_with_backtrack_limit + #[inline(always)] + pub fn max_backtrack_limit() -> u32 { + unsafe { v8__RegExp__MAX_BACKTRACK_LIMIT() } + } + + /// Like [`new`], but additionally specifies a backtrack limit. If the number + /// of backtracks done in one [`exec`] call hits the limit, a match failure is + /// immediately returned. + /// + /// # Panics + /// + /// Panics if `backtrack_limit` is zero, which V8 reserves to mean "no + /// limit", or if it exceeds [`max_backtrack_limit()`]. V8 itself responds to + /// both by aborting the process even in release builds, so these are checked + /// here to fail with a usable message instead. + /// + /// [`new`]: RegExp::new + /// [`exec`]: RegExp::exec + /// [`max_backtrack_limit()`]: RegExp::max_backtrack_limit + #[inline(always)] + pub fn new_with_backtrack_limit<'s>( + scope: &PinScope<'s, '_>, + pattern: Local, + flags: RegExpCreationFlags, + backtrack_limit: u32, + ) -> Option> { + assert!( + backtrack_limit != 0, + "backtrack_limit must not be zero; zero is V8's \"no limit\" sentinel, \ + use RegExp::new() instead" + ); + let max = Self::max_backtrack_limit(); + assert!( + backtrack_limit <= max, + "backtrack_limit {backtrack_limit} exceeds the maximum of {max}" + ); + unsafe { + scope.cast_local(|sd| { + v8__RegExp__NewWithBacktrackLimit( + sd.get_current_context(), + &*pattern, + flags, + backtrack_limit, + ) + }) + } + } + + #[inline(always)] + pub fn exec<'s>( + &self, + scope: &PinScope<'s, '_>, + subject: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__RegExp__Exec(self, sd.get_current_context(), &*subject) + }) + } + } + + #[inline(always)] + pub fn get_source<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, String> { + unsafe { scope.cast_local(|_| v8__RegExp__GetSource(self)) }.unwrap() + } +} diff --git a/crates/v8/src/scope.rs b/crates/v8/src/scope.rs new file mode 100644 index 0000000000..8da79f7fba --- /dev/null +++ b/crates/v8/src/scope.rs @@ -0,0 +1,2609 @@ +//! This module contains the rust wrappers for V8's scope types. +//! +//! There are two main types of scopes, with the other types being derived from them: +//! - `HandleScope` - a scope to create and access `Local` handles +//! - `TryCatch` - a scope to catch exceptions thrown from javascript +//! +//! There are a few important properties that make v8 scopes challenging to model in rust. +//! - `HandleScope`s can (and almost certainly will be) nested, but handles are only +//! bound to the innermost `HandleScope` +//! - Importantly, this means that the Handle lifetimes are determined by the innermost `HandleScope` +//! - Both `HandleScope` and `TryCatch` cannot be moved, because V8 holds direct pointers to them +//! - The C++ API relies heavily on inheritance, which is a bit awkward to model in rust +//! +//! # Example +//! +//! ```rust +//! use v8::{Context, ContextScope, HandleScope, Isolate, Object}; +//! # let platform = v8::new_default_platform(0, false).make_shared(); +//! # v8::V8::initialize_platform(platform); +//! # v8::V8::initialize(); +//! +//! let isolate = &mut Isolate::new(Default::default()); +//! let scope = HandleScope::new(isolate); +//! let scope = std::pin::pin!(scope); +//! let mut scope = scope.init(); +//! let context = Context::new(&scope, Default::default()); +//! +//! let context_scope = ContextScope::new(&mut scope, context); +//! let object = Object::new(&context_scope); +//! ``` +//! +//! ## Explanation +//! The first thing you'll notice is that creating a `HandleScope` requires a few different steps. You'll see this pattern +//! across all scope types that are address-sensitive (all except for `ContextScope`): +//! +//! 1. Allocate the storage for the scope. At this point, the scope is not yet address-sensitive, and so it can be safely moved. +//! 2. Pin the storage to the stack. This is necessary because once we initialize the scope, it must not be moved. +//! 3. Initialize the scope. This is where the scope is actually initialized, and our `Pin` ensures that the scope cannot be moved. +//! +//! This is a bit verbose, so you can collapse it into two lines, +//! ```rust +//! # use v8::{HandleScope, Isolate}; +//! # let platform = v8::new_default_platform(0, false).make_shared(); +//! # v8::V8::initialize_platform(platform); +//! # v8::V8::initialize(); +//! # let isolate = &mut Isolate::new(Default::default()); +//! let scope = std::pin::pin!(HandleScope::new(isolate)); +//! let mut scope = scope.init(); +//! ``` +//! +//! or use the provided macros: +//! ```rust +//! # use v8::Isolate; +//! # let platform = v8::new_default_platform(0, false).make_shared(); +//! # v8::V8::initialize_platform(platform); +//! # v8::V8::initialize(); +//! # let isolate = &mut Isolate::new(Default::default()); +//! // note that this expands into statements, introducing a new variable `scope` into the current +//! // block. Using it as an expression (`let scope = v8::scope!(let scope, isolate);`) will not work +//! v8::scope!(let scope, isolate); +//! ``` +//! +//! # Scopes as function args +//! In a function that takes a scope, you'll typically want to take a `PinScope`, like +//! ```rust +//! fn foo<'s, 'i>(scope: &mut v8::PinScope<'s, 'i>) { +//! let local = v8::Number::new(scope, 42.0); +//! } +//! ``` +//! +//! `PinScope` is just a shorthand for `PinnedRef<'s, HandleScope<'i>>`, which you can use if you really prefer. +//! +//! The lifetimes can sometimes be elided, but if you are taking or returning a `Local`, you'll need to specify at least the first one. +//! ``` +//! fn foo<'s>(scope: &mut v8::PinScope<'s, '_>, arg: v8::Local<'s, v8::Number>) -> v8::Local<'s, v8::Number> { +//! v8::Number::new(scope, arg.value() + 42.0) +//! } +//! ``` +//! +//! # Deref/DerefMut +//! +//! Scopes implement `Deref` and `DerefMut` to allow for sort of "inheritance" of behavior. This is useful because +//! it allows most methods (as mentioned above) to just take a `PinScope`, and other scopes will deref to `PinScope`. +//! +//! That lets you seamlessly pass, for instance, a `ContextScope` to a function that takes a `PinScope`. +//! Note that pinned scopes do not implement `Deref` or `DerefMut`, themselves, rather `PinnedRef` does. +//! +//! The deref implementations are: +//! +//! PinnedRef<'_, HandleScope<'_, ()>> -> Isolate +//! PinnedRef<'_, HandleScope<'_>> -> PinnedRef<'_, HandleScope<'_, ()>> +//! PinnedRef<'_, ContextScope<'_, '_>> -> PinnedRef<'_, HandleScope<'_>> +//! PinnedRef<'_, CallbackScope<'_, '_>> -> PinnedRef<'_, HandleScope<'_, ()>> +//! +//! +//! +//! # Internals +//! +//! The initialization process uses the typestate pattern. The storage for the scope is a `ScopeStorage` struct, which is +//! then transititions to a `Pin<&mut ScopeStorage>`, and then `init` transitions to a `PinnedRef<'s, T>`. +//! +//! The `ScopeStorage` struct tracks initialization state, and is responsible for calling the destructor when the storage is dropped +//! (iff the scope was initialized). +//! +//! The `PinnedRef` type, returned from `init`, is a transparent wrapper around a `Pin<&mut T>`. The reason it is a newtype is so +//! that it can have specialized Deref/DerefMut implementations for the different scope types. `Pin` has a blanket implementation +//! that doesn't have the behavior we want. +//! +//! ## Lifetimes +//! +//! The trickiest part of the scopes here are the lifetimes. In general, the lifetimes exist for a few reasons: +//! - ensure that a scope can't outlive the thing it's made from (e.g. an isolate, or another scope) +//! - ensure that a scope higher up the stack can't be used until the scope below it has dropped +//! - ensure that the `Handle`s bound to the scope do not outlive the scope +//! +//! These lifetimes do not need to be exact, and in some cases I'm sure they are shorter than they could be, +//! as long as everything lives long enough. In other words, the lifetimes just need to be a safe approximation. +//! +//! +//! ### HandleScope +//! `HandleScope` itself has only one lifetime, `'i` which is the lifetime of the thing that the scope was created from +//! (e.g. an isolate). +//! +//! The lifetime for handles bound to the scope is really the lifetime of the `HandleScope` itself. In our case, +//! since we've pinned it to the stack, that is the lifetime of the pinned reference. So in +//! `PinnedRef<'s, HandleScope<'i>>`, `'s` is the lifetime of the pinned reference, and therefore +//! the handles, and 'i is the lifetime of the isolate. +//! +//! ### ContextScope +//! ContextScope is really just a wrapper around another scope, with a `Context` added to it. +//! It wraps a scope, and so it is not actually address-sensitive, and can be moved around freely. +//! +//! ContextScope has two lifetimes, `'b` and `'s`. `'b` is the lifetime of the borrow of the scope +//! it's wrapping, and `'s` is the lifetime of the scope. +//! +//! Effectively you have `&'b PinnedRef<'s, T>`. +//! +//! The lifetime for handles bound to the scope is the lifetime of the scope that it was created from. +//! So in `ContextScope<'b, 's>`, `'b` is the lifetime of the borrow of the inner scope, and `'s` is the lifetime of the inner scope (and therefore the handles). +//! +//! ### Isolates +//! A handle's lifetime ties it to a scope, but not to the `Isolate` that scope belongs to, so the +//! type system alone would allow a handle of one `Isolate` to be passed to an API call on another +//! one, which corrupts both heaps. To rule that out, all scopes that are open on a thread at the +//! same time must belong to the same `Isolate`: opening a `HandleScope`, `EscapableHandleScope` or +//! `CallbackScope` while a scope of another `Isolate` is open on the same thread panics. Since a +//! handle cannot outlive its scope, no handle of another `Isolate` can then be alive. Handles that +//! outlive scopes (`Global`, `Weak`, `TracedReference` and `Eternal`) remember their `Isolate`, and +//! panic when created from or turned into a `Local` in a scope of another `Isolate`. Debug builds +//! additionally check, whenever a `Local` is dereferenced, that its value can be used in the +//! `Isolate` that owns the open scopes: it lives in that `Isolate`'s heap, it is a value that all +//! `Isolate`s of its `IsolateGroup` share (such as `undefined`), or it is a Smi. +//! +//! Code that has to drive another `Isolate` while scopes of the first are open can do so inside +//! the `unsafe` [`allow_scopes_of_another_isolate`]. A scope that is leaked (for example with +//! `std::mem::forget`) counts as open for the rest of the thread's life, so scopes of other +//! `Isolate`s can no longer be opened on that thread. +use crate::{ + Context, Data, DataError, Function, FunctionCallbackInfo, Isolate, Local, + Message, Object, OwnedIsolate, PromiseRejectMessage, PropertyCallbackInfo, + SealedLocal, Value, + fast_api::FastApiCallbackOptions, + function::FunctionCallbackInfoParts, + isolate::{IsolateAnnex, RealIsolate}, + support::assert_layout_subset, +}; +use std::{ + any::type_name, + cell::Cell, + marker::{PhantomData, PhantomPinned}, + mem::ManuallyDrop, + ops::{Deref, DerefMut}, + pin::Pin, + ptr::NonNull, +}; +pub(crate) mod raw; + +/// Tracks which `Isolate` owns the scopes that are open on the current thread +/// (see "Isolates" in the module documentation). +pub(crate) mod scope_owner { + use std::cell::Cell; + use std::ptr::NonNull; + + use crate::isolate::RealIsolate; + + thread_local! { + // The `Isolate` that owns the open scopes, and how many scopes are open. + static OWNER: Cell<(*const RealIsolate, usize)> = + const { Cell::new((std::ptr::null(), 0)) }; + } + + /// Called when a scope of `isolate` is opened. + /// + /// # Panics + /// + /// Panics if a scope of another `Isolate` is open on this thread. + #[inline] + pub(super) fn enter(isolate: NonNull) { + let (owner, depth) = OWNER.get(); + if depth != 0 && owner != isolate.as_ptr().cast_const() { + panic!( + "cannot open a scope on an Isolate while a scope of another Isolate \ + is open on the same thread" + ); + } + OWNER.set((isolate.as_ptr(), depth + 1)); + } + + /// Returns the `Isolate` that owns the scopes open on this thread, if any. + #[inline] + pub(crate) fn current() -> Option> { + let (owner, depth) = OWNER.get(); + if depth == 0 { + None + } else { + NonNull::new(owner.cast_mut()) + } + } + + /// Runs `f` with no scopes counted as open on this thread, and restores + /// the count when `f` returns or unwinds. + pub(super) fn suspend(f: impl FnOnce() -> R) -> R { + struct Restore((*const RealIsolate, usize)); + impl Drop for Restore { + fn drop(&mut self) { + debug_assert_eq!(OWNER.get().1, 0, "a scope was left open"); + OWNER.set(self.0); + } + } + let _restore = Restore(OWNER.replace((std::ptr::null(), 0))); + f() + } + + /// Called when a scope opened with `enter()` is closed. + #[inline] + pub(super) fn exit() { + let (owner, depth) = OWNER.get(); + debug_assert!(depth > 0); + if depth == 1 { + OWNER.set((std::ptr::null(), 0)); + } else { + OWNER.set((owner, depth - 1)); + } + } + + #[cfg(debug_assertions)] + unsafe extern "C" { + fn v8__Data__IsCompatibleWithIsolate( + this: *const crate::Data, + isolate: *const RealIsolate, + ) -> bool; + } + + /// Checks that the value of a dereferenced `Local` can be used in the + /// `Isolate` that owns the open scopes. + /// + /// # Panics + /// + /// Panics if it can't. + #[cfg(debug_assertions)] + pub(crate) fn check_handle(data: NonNull) { + let (owner, depth) = OWNER.get(); + if depth == 0 { + return; + } + // SAFETY: `data` points to a live handle and `owner` to a live isolate. + if !unsafe { v8__Data__IsCompatibleWithIsolate(data.as_ptr(), owner) } { + panic!( + "a handle of one Isolate was used while a scope of another Isolate \ + is open" + ); + } + } +} + +/// Runs `f` with the scopes that are open on this thread set aside, so that +/// `f` can open scopes of an `Isolate` other than the one they belong to (see +/// "Isolates" in the module documentation). They count as open again when `f` +/// returns. +/// +/// # Safety +/// +/// While `f` runs, handles of the `Isolate`s whose scopes were set aside must +/// not be used with any other `Isolate`, and handles that `f` creates must not +/// be used once it returns. `f` must close every scope it opens. +pub unsafe fn allow_scopes_of_another_isolate(f: impl FnOnce() -> R) -> R { + scope_owner::suspend(f) +} + +pub type PinScope<'s, 'i, C = Context> = PinnedRef<'s, HandleScope<'i, C>>; +pub type PinCallbackScope<'s, 'i, C = Context> = + PinnedRef<'s, CallbackScope<'i, C>>; + +/// Storage for a scope. +/// +/// Tracks the initialization state of the scope, and holds the scope itself. +#[repr(C)] +pub struct ScopeStorage { + inited: bool, + scope: ManuallyDrop, + _pinned: PhantomPinned, +} + +impl ScopeStorage { + pub(crate) fn projected(self: Pin<&mut Self>) -> Pin<&mut T> { + // SAFETY: we are just projecting to a field, so the scope remains pinned + unsafe { + let self_mut = self.get_unchecked_mut(); + Pin::new_unchecked(&mut self_mut.scope) + } + } + + pub fn new(scope: T) -> Self { + Self { + inited: false, + scope: ManuallyDrop::new(scope), + _pinned: PhantomPinned, + } + } + + pub fn init(mut self: Pin<&mut Self>) -> PinnedRef<'_, T> { + if self.inited { + // free old, going to reuse this storage + unsafe { + let self_mut = self.as_mut().get_unchecked_mut(); + self_mut.drop_inner(); + self_mut.inited = false; + } + } + + // hold onto a pointer so we can set this after initialization. we can't use a normal + // mutable reference because the borrow checker will see overlapping borrows. this is + // safe, however, because we lose our mutable reference to the storage in `init_stack` + // as it gets projected to the inner type + let inited_ptr = + unsafe { &raw mut self.as_mut().get_unchecked_mut().inited }; + let ret = T::init_stack(self); + unsafe { inited_ptr.write(true) }; + PinnedRef(ret) + } + + /// SAFEFTY: `self.inited` must be true, and therefore must be pinned + unsafe fn drop_inner(&mut self) { + unsafe { + T::deinit(&mut self.scope); + } + self.inited = false; + } +} + +impl Drop for ScopeStorage { + fn drop(&mut self) { + if self.inited { + unsafe { + self.drop_inner(); + } + } + } +} + +pub trait Scope: Sized + sealed::Sealed + ScopeInit {} + +mod sealed { + pub trait Sealed {} +} + +pub trait ScopeInit: Sized { + fn init_stack(storage: Pin<&mut ScopeStorage>) -> Pin<&mut Self>; + + unsafe fn deinit(me: &mut Self); +} + +impl ScopeInit for HandleScope<'_, C> { + fn init_stack(storage: Pin<&mut ScopeStorage>) -> Pin<&mut Self> { + // SAFETY: no moving the scope from this point on + let storage_mut = unsafe { storage.get_unchecked_mut() }; + scope_owner::enter(storage_mut.scope.isolate); + unsafe { + let isolate = storage_mut.scope.isolate; + raw::HandleScope::init(&mut storage_mut.scope.raw_handle_scope, isolate) + }; + + let projected = &mut storage_mut.scope; + + // SAFETY: scope is still pinned + unsafe { Pin::new_unchecked(projected) } + } + + unsafe fn deinit(me: &mut Self) { + unsafe { raw::v8__HandleScope__DESTRUCT(&mut me.raw_handle_scope) }; + scope_owner::exit(); + } +} + +/// A stack-allocated class that governs a number of local handles. +/// After a handle scope has been created, all local handles will be +/// allocated within that handle scope until either the handle scope is +/// deleted or another handle scope is created. If there is already a +/// handle scope and a new one is created, all allocations will take +/// place in the new handle scope until it is deleted. After that, +/// new handles will again be allocated in the original handle scope. +/// +/// After the handle scope of a local handle has been deleted the +/// garbage collector will no longer track the object stored in the +/// handle and may deallocate it. The behavior of accessing a handle +/// for which the handle scope has been deleted is undefined. +#[repr(C)] +#[derive(Debug)] +pub struct HandleScope<'s, C = Context> { + raw_handle_scope: raw::HandleScope, + isolate: NonNull, + annex: NonNull, + context: Cell>>, + _phantom: PhantomData<&'s mut C>, + _pinned: PhantomPinned, +} + +impl sealed::Sealed for HandleScope<'_, C> {} +impl Scope for HandleScope<'_, C> {} + +mod get_isolate { + use crate::RealIsolate; + pub trait GetIsolate { + fn get_isolate_ptr(&self) -> *mut RealIsolate; + } +} +pub(crate) use get_isolate::GetIsolate; + +mod get_isolate_impls { + use crate::{Promise, PromiseRejectMessage}; + + use super::*; + impl GetIsolate for Isolate { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.as_real_ptr() + } + } + + impl GetIsolate for OwnedIsolate { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.as_real_ptr() + } + } + + impl GetIsolate for FunctionCallbackInfo { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.get_isolate_ptr() + } + } + + impl GetIsolate for FunctionCallbackInfoParts<'_> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.isolate.as_real_ptr() + } + } + + impl GetIsolate for PropertyCallbackInfo { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.get_isolate_ptr() + } + } + + impl GetIsolate for FastApiCallbackOptions<'_> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.isolate + } + } + + impl GetIsolate for Local<'_, Context> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + unsafe { raw::v8__Isolate__GetCurrent() } + } + } + + impl GetIsolate for Local<'_, Message> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + unsafe { raw::v8__Isolate__GetCurrent() } + } + } + + impl GetIsolate for Local<'_, Promise> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + unsafe { raw::v8__Isolate__GetCurrent() } + } + } + + impl GetIsolate for PromiseRejectMessage<'_> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + unsafe { raw::v8__Isolate__GetCurrent() } + } + } + + impl GetIsolate for HandleScope<'_, C> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.isolate.as_ptr() + } + } + + impl GetIsolate + for ContextScope<'_, '_, P> + { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.scope.get_isolate_ptr() + } + } + + impl GetIsolate for &mut P { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + P::get_isolate_ptr(self) + } + } + + impl GetIsolate for &P { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + P::get_isolate_ptr(self) + } + } + + impl GetIsolate for TryCatch<'_, '_, P> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.scope.get_isolate_ptr() + } + } + + impl GetIsolate for CallbackScope<'_, C> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.isolate.as_ptr() + } + } + + impl GetIsolate for EscapableHandleScope<'_, '_, C> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.isolate.as_ptr() + } + } + + impl GetIsolate for AllowJavascriptExecutionScope<'_, '_, P> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.scope.get_isolate_ptr() + } + } + + impl GetIsolate for DisallowJavascriptExecutionScope<'_, '_, P> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.scope.get_isolate_ptr() + } + } +} + +pub trait NewHandleScope<'s> { + type NewScope: Scope; + + fn make_new_scope(me: &'s mut Self) -> Self::NewScope; +} + +impl<'s, 'p: 's, C> NewHandleScope<'s> for PinnedRef<'_, HandleScope<'p, C>> { + type NewScope = HandleScope<'s, C>; + + fn make_new_scope(me: &'s mut Self) -> Self::NewScope { + HandleScope { + raw_handle_scope: unsafe { raw::HandleScope::uninit() }, + isolate: me.0.isolate, + annex: me.0.annex, + context: Cell::new(me.0.context.get()), + _phantom: PhantomData, + _pinned: PhantomPinned, + } + } +} + +impl<'s> NewHandleScope<'s> for Isolate { + type NewScope = HandleScope<'s, ()>; + + #[inline(always)] + fn make_new_scope(me: &'s mut Self) -> Self::NewScope { + HandleScope { + raw_handle_scope: unsafe { raw::HandleScope::uninit() }, + isolate: unsafe { NonNull::new_unchecked(me.as_real_ptr()) }, + annex: me.get_annex_ptr(), + context: Cell::new(None), + _phantom: PhantomData, + _pinned: PhantomPinned, + } + } +} + +impl<'s> NewHandleScope<'s> for OwnedIsolate { + type NewScope = HandleScope<'s, ()>; + + fn make_new_scope(me: &'s mut Self) -> Self::NewScope { + let isolate: &Isolate = me; + HandleScope { + raw_handle_scope: unsafe { raw::HandleScope::uninit() }, + isolate: unsafe { NonNull::new_unchecked(me.get_isolate_ptr()) }, + annex: isolate.get_annex_ptr(), + context: Cell::new(None), + _phantom: PhantomData, + _pinned: PhantomPinned, + } + } +} + +impl<'s, 'p: 's, 'i, C> NewHandleScope<'s> + for PinnedRef<'p, CallbackScope<'i, C>> +{ + type NewScope = HandleScope<'i, C>; + + fn make_new_scope(me: &'s mut Self) -> Self::NewScope { + HandleScope { + raw_handle_scope: unsafe { raw::HandleScope::uninit() }, + isolate: me.0.isolate, + annex: me.0.annex, + context: Cell::new(me.0.context.get()), + _phantom: PhantomData, + _pinned: PhantomPinned, + } + } +} + +impl<'a, 'i> NewHandleScope<'a> for ContextScope<'_, '_, HandleScope<'i>> { + type NewScope = HandleScope<'i>; + fn make_new_scope(me: &'a mut Self) -> Self::NewScope { + HandleScope { + raw_handle_scope: unsafe { raw::HandleScope::uninit() }, + isolate: unsafe { NonNull::new_unchecked(me.scope.get_isolate_ptr()) }, + annex: me.scope.0.annex, + context: Cell::new(Some(me.raw_handle_scope.entered_context)), + _phantom: PhantomData, + _pinned: PhantomPinned, + } + } +} + +pub(crate) struct ScopeData { + isolate: NonNull, + context: Cell>>, +} + +impl ScopeData { + #[inline(always)] + pub(crate) fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.isolate.as_ptr() + } + + pub(crate) fn get_current_context(&self) -> *mut Context { + if let Some(context) = self.context.get() { + context.as_ptr() + } else { + let isolate = self.get_isolate_ptr(); + let context = + unsafe { raw::v8__Isolate__GetCurrentContext(isolate) }.cast_mut(); + self + .context + .set(Some(unsafe { NonNull::new_unchecked(context) })); + context + } + } +} + +impl<'s> HandleScope<'s> { + #[allow(clippy::new_ret_no_self)] + pub fn new>( + scope: &'s mut P, + ) -> ScopeStorage { + ScopeStorage::new(P::make_new_scope(scope)) + } +} + +impl<'s> PinnedRef<'s, HandleScope<'_>> { + /// Returns the context of the currently running JavaScript, or the context + /// on the top of the stack if no JavaScript is running + pub fn get_current_context(&self) -> Local<'s, Context> { + if let Some(context) = self.0.context.get() { + unsafe { Local::from_non_null(context) } + } else { + let isolate = self.0.isolate; + let context = + unsafe { raw::v8__Isolate__GetCurrentContext(isolate.as_ptr()) } + .cast_mut(); + unsafe { + self.0.context.set(Some(NonNull::new_unchecked(context))); + Local::from_raw_unchecked(context) + } + } + } + + /// Returns either the last context entered through V8's C++ API, or the + /// context of the currently running microtask while processing microtasks. + /// If a context is entered while executing a microtask, that context is + /// returned. + pub fn get_entered_or_microtask_context(&self) -> Local<'_, Context> { + let context_ptr = unsafe { + raw::v8__Isolate__GetEnteredOrMicrotaskContext(self.0.isolate.as_ptr()) + }; + unsafe { Local::from_raw_unchecked(context_ptr) } + } + + /// Return data that was previously attached to the isolate snapshot via + /// SnapshotCreator, and removes the reference to it. If called again with + /// same `index` argument, this function returns `DataError::NoData`. + /// + /// The value that was stored in the snapshot must either match or be + /// convertible to type parameter `T`, otherwise `DataError::BadType` is + /// returned. + pub fn get_isolate_data_from_snapshot_once( + &self, + index: usize, + ) -> Result, DataError> + where + T: 'static, + for<'l> as TryInto>>::Error: + get_data_sealed::ToDataError, + for<'l> Local<'l, Data>: TryInto>, + { + unsafe { + let Some(res) = self.cast_local(|sd| { + raw::v8__Isolate__GetDataFromSnapshotOnce(sd.get_isolate_ptr(), index) + }) else { + return Err(DataError::no_data::()); + }; + use get_data_sealed::ToDataError; + match res.try_into() { + Ok(x) => Ok(x), + Err(e) => Err(e.to_data_error()), + } + } + } + + /// Return data that was previously attached to the context snapshot via + /// SnapshotCreator, and removes the reference to it. If called again with + /// same `index` argument, this function returns `DataError::NoData`. + /// + /// The value that was stored in the snapshot must either match or be + /// convertible to type parameter `T`, otherwise `DataError::BadType` is + /// returned. + pub fn get_context_data_from_snapshot_once( + &self, + index: usize, + ) -> Result, DataError> + where + T: 'static, + for<'l> as TryInto>>::Error: + get_data_sealed::ToDataError, + for<'l> Local<'l, Data>: TryInto>, + { + unsafe { + let Some(res) = self.cast_local(|sd| { + raw::v8__Context__GetDataFromSnapshotOnce( + sd.get_current_context(), + index, + ) + }) else { + return Err(DataError::no_data::()); + }; + use get_data_sealed::ToDataError; + match res.try_into() { + Ok(x) => Ok(x), + Err(e) => Err(e.to_data_error()), + } + } + } + + #[inline(always)] + pub fn set_promise_hooks( + &self, + init_hook: Option>, + before_hook: Option>, + after_hook: Option>, + resolve_hook: Option>, + ) { + unsafe { + let context = self.get_current_context(); + raw::v8__Context__SetPromiseHooks( + context.as_non_null().as_ptr(), + init_hook.map_or_else(std::ptr::null, |v| &*v), + before_hook.map_or_else(std::ptr::null, |v| &*v), + after_hook.map_or_else(std::ptr::null, |v| &*v), + resolve_hook.map_or_else(std::ptr::null, |v| &*v), + ); + } + } + + #[inline(always)] + pub fn set_continuation_preserved_embedder_data(&self, data: Local) { + unsafe { + let isolate = self.0.isolate; + raw::v8__Context__SetContinuationPreservedEmbedderData( + isolate.as_ptr(), + &*data, + ); + } + } + + #[inline(always)] + pub fn get_continuation_preserved_embedder_data(&self) -> Local<'s, Value> { + unsafe { + self + .cast_local(|sd| { + raw::v8__Context__GetContinuationPreservedEmbedderData( + sd.get_isolate_ptr(), + ) + }) + .unwrap() + } + } + + /// Returns the host defined options set for currently running script or + /// module, if available. + #[inline(always)] + pub fn get_current_host_defined_options(&self) -> Option> { + let isolate_ptr = self.0.isolate.as_ptr(); + unsafe { + Local::from_raw(raw::v8__Isolate__GetCurrentHostDefinedOptions( + isolate_ptr, + )) + } + } +} + +// for<'l> DataError: From< as TryInto>>::Error>, +mod get_data_sealed { + use crate::DataError; + use std::convert::Infallible; + + pub trait ToDataError { + fn to_data_error(self) -> DataError; + } + impl ToDataError for DataError { + fn to_data_error(self) -> DataError { + self + } + } + impl ToDataError for Infallible { + fn to_data_error(self) -> DataError { + unreachable!() + } + } +} + +impl<'p> PinnedRef<'p, HandleScope<'_, ()>> { + #[inline(always)] + pub(crate) unsafe fn cast_local( + &self, + _f: impl FnOnce(&mut ScopeData) -> *const T, + ) -> Option> { + let mut data: ScopeData = ScopeData { + context: Cell::new(self.0.context.get()), + isolate: self.0.isolate, + }; + let ptr = _f(&mut data); + unsafe { Local::from_raw(ptr) } + } + + /// Schedules an exception to be thrown when returning to JavaScript. When + /// an exception has been scheduled it is illegal to invoke any + /// JavaScript operation; the caller must return immediately and only + /// after the exception has been handled does it become legal to invoke + /// JavaScript operations. + /// + /// This function always returns the `undefined` value. + pub fn throw_exception( + &self, + exception: Local<'p, Value>, + ) -> Local<'p, Value> { + unsafe { + self.cast_local(|sd| { + raw::v8__Isolate__ThrowException(sd.get_isolate_ptr(), &*exception) + }) + } + .unwrap() + } + + /// Open a handle passed from V8 in the current scope. + /// + /// # Safety + /// + /// The handle must be rooted in this scope. + #[inline(always)] + pub unsafe fn unseal<'a, T>(&self, v: SealedLocal) -> Local<'a, T> { + unsafe { Local::from_non_null(v.0) } + } +} + +impl HandleScope<'_, C> {} + +impl GetIsolate for Pin<&mut HandleScope<'_, C>> { + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.isolate.as_ptr() + } +} + +/// Stack-allocated class which sets the execution context for all operations +/// executed within a local scope. After entering a context, all code compiled +/// and run is compiled and run in this context. +#[repr(C)] +pub struct ContextScope<'borrow, 'scope, P: ClearCachedContext> { + raw_handle_scope: raw::ContextScope, + scope: &'borrow mut PinnedRef<'scope, P>, +} + +impl ScopeInit for ContextScope<'_, '_, P> { + fn init_stack(storage: Pin<&mut ScopeStorage>) -> Pin<&mut Self> { + storage.projected() + } + unsafe fn deinit(_me: &mut Self) {} +} + +impl<'p, P: ClearCachedContext> Deref for ContextScope<'_, 'p, P> { + type Target = PinnedRef<'p, P>; + fn deref(&self) -> &Self::Target { + self.scope + } +} + +impl DerefMut for ContextScope<'_, '_, P> { + fn deref_mut(&mut self) -> &mut Self::Target { + self.scope + } +} + +impl sealed::Sealed for ContextScope<'_, '_, P> {} +impl Scope for ContextScope<'_, '_, P> {} + +mod new_context_scope { + + use super::{GetIsolate, Scope}; + use crate::{Context, Local}; + + pub trait NewContextScope<'s, 'c>: GetIsolate { + type NewScope: Scope; + + fn make_new_scope( + me: &'s mut Self, + context: Local<'c, Context>, + ) -> Self::NewScope; + } +} +use new_context_scope::NewContextScope; + +mod clear_cached_context { + pub trait ClearCachedContext { + fn clear_cached_context(&self); + } +} +use clear_cached_context::ClearCachedContext; + +impl ClearCachedContext for HandleScope<'_, C> { + fn clear_cached_context(&self) { + self.context.set(None); + } +} + +impl ClearCachedContext for CallbackScope<'_, C> { + fn clear_cached_context(&self) {} +} + +impl ClearCachedContext for PinnedRef<'_, P> { + fn clear_cached_context(&self) { + self.0.clear_cached_context(); + } +} + +impl ClearCachedContext for EscapableHandleScope<'_, '_, C> { + fn clear_cached_context(&self) { + self.context.set(None); + } +} + +impl

Drop for ContextScope<'_, '_, P> +where + P: ClearCachedContext, +{ + fn drop(&mut self) { + self.scope.0.clear_cached_context(); + } +} + +impl<'scope, 'obj: 'scope, 'ct, P: Scope + GetIsolate> + NewContextScope<'scope, 'ct> for ContextScope<'_, 'obj, P> +where + P: ClearCachedContext, +{ + type NewScope = ContextScope<'scope, 'obj, P>; + + fn make_new_scope( + me: &'scope mut Self, + context: Local<'ct, Context>, + ) -> Self::NewScope { + ContextScope { + raw_handle_scope: raw::ContextScope::new(context), + scope: me.scope, + } + } +} + +impl<'scope, 'obj: 'scope, 'ct, 'i, C> NewContextScope<'scope, 'ct> + for PinnedRef<'obj, HandleScope<'i, C>> +where + 'ct: 'scope, +{ + type NewScope = ContextScope<'scope, 'obj, HandleScope<'i>>; + + fn make_new_scope( + me: &'scope mut Self, + context: Local<'ct, Context>, + ) -> Self::NewScope { + me.0.context.set(None); + ContextScope { + raw_handle_scope: raw::ContextScope::new(context), + scope: unsafe { + // SAFETY: we are adding the context, so we can mark that it now has a context. + // the types are the same aside from the type parameter, which is only used in a ZST + cast_pinned_ref_mut::, HandleScope<'i, Context>>(me) + }, + } + } +} + +impl<'scope, 'obj: 'scope, 'i, 'ct, C> NewContextScope<'scope, 'ct> + for PinnedRef<'obj, CallbackScope<'i, C>> +{ + type NewScope = ContextScope<'scope, 'obj, HandleScope<'i>>; + + fn make_new_scope( + me: &'scope mut Self, + context: Local<'ct, Context>, + ) -> Self::NewScope { + ContextScope { + raw_handle_scope: raw::ContextScope::new(context), + scope: unsafe { + // we are adding the context, so we can mark that it now has a context. + // SAFETY: CallbackScope is a superset of HandleScope, so giving a "view" of + // the CallbackScope as a HandleScope is valid, and we won't ever move out of the transmuted + // value + cast_pinned_ref_mut::, HandleScope<'i, Context>>( + me, + ) + }, + } + } +} + +// these lifetimes are crazy. basically we have +// - 'borrow: the borrow of the scope +// - 'scope: the lifetime of the scope. this must be longer than 'borrow. this is the lifetime of the handles created from the scope. +// - 'i: the lifetime of the inner the `EscapableHandleScope` is made from +// - 'esc: the lifetime of the slot that the `EscapableHandleScope` will eventually escape to. this must be longer than 'i +// - 'ct: the lifetime of the context (this is _not_ the same as 'scope, it can be longer or shorter) +impl<'borrow, 'scope: 'borrow, 'i, 'esc: 'i, 'ct, C> + NewContextScope<'borrow, 'ct> + for PinnedRef<'scope, EscapableHandleScope<'i, 'esc, C>> +{ + type NewScope = ContextScope<'borrow, 'scope, EscapableHandleScope<'i, 'esc>>; + + fn make_new_scope( + me: &'borrow mut Self, + context: Local<'ct, Context>, + ) -> Self::NewScope { + ContextScope { + raw_handle_scope: raw::ContextScope::new(context), + scope: unsafe { + // SAFETY: layouts are the same aside from the type parameter, which is only used in a ZST + std::mem::transmute::< + &'borrow mut PinnedRef<'scope, EscapableHandleScope<'i, 'esc, C>>, + &'borrow mut PinnedRef< + 'scope, + EscapableHandleScope<'i, 'esc, Context>, + >, + >(me) + }, + } + } +} + +impl ClearCachedContext for ContextScope<'_, '_, P> { + fn clear_cached_context(&self) { + self.scope.0.clear_cached_context(); + } +} + +impl< + 'scope, + 'obj: 'scope, + 'ct, + P: NewContextScope<'scope, 'ct> + ClearCachedContext, +> ContextScope<'scope, 'obj, P> +{ + #[allow(clippy::new_ret_no_self)] + pub fn new( + param: &'scope mut P, + context: Local<'ct, Context>, + ) -> P::NewScope { + if param.get_isolate_ptr() != unsafe { raw::v8__Isolate__GetCurrent() } { + panic!( + "{} and Context do not belong to the same Isolate", + type_name::

() + ) + } + param.clear_cached_context(); + P::make_new_scope(param, context) + } +} + +/// A `CallbackScope` can be used to bootstrap a `HandleScope` and +/// `ContextScope` inside a callback function that gets called by V8. +/// Bootstrapping a scope inside a callback is the only valid use case of this +/// type; using it in other places leads to undefined behavior, which is also +/// the reason `CallbackScope::new()` is marked as being an unsafe function. +/// +/// For some callback types, rusty_v8 internally creates a scope and passes it +/// as an argument to to embedder callback. Eventually we intend to wrap all +/// callbacks in this fashion, so the embedder would never needs to construct +/// a CallbackScope. +/// +/// A `CallbackScope<()>`, without context, can be created from: +/// - `&mut Isolate` +/// - `&mut OwnedIsolate` +/// +/// A `CallbackScope`, with context, can be created from: +/// - `Local` +/// - `Local` +/// - `Local` +/// - `Local` +/// - `Local` +/// - `&FunctionCallbackInfo` +/// - `&PropertyCallbackInfo` +/// - `&PromiseRejectMessage` +/// - `&FastApiCallbackOptions` +#[repr(C)] +#[derive(Debug)] +pub struct CallbackScope<'s, C = Context> { + raw_handle_scope: raw::HandleScope, + isolate: NonNull, + annex: NonNull, + context: Cell>>, + _phantom: PhantomData<&'s C>, + _pinned: PhantomPinned, + needs_scope: bool, +} + +assert_layout_subset!(HandleScope<'static, ()>, CallbackScope<'static, ()> { raw_handle_scope, isolate, annex, context, _phantom, _pinned }); + +impl<'s> CallbackScope<'s> { + #[allow(clippy::new_ret_no_self)] + pub unsafe fn new>( + param: P, + ) -> ScopeStorage { + ScopeStorage::new(P::make_new_scope(param)) + } +} + +impl AsRef for CallbackScope<'_, C> { + fn as_ref(&self) -> &Isolate { + unsafe { Isolate::from_raw_ref(&self.isolate) } + } +} + +impl ScopeInit for CallbackScope<'_, C> { + fn init_stack(storage: Pin<&mut ScopeStorage>) -> Pin<&mut Self> { + let storage_mut = unsafe { storage.get_unchecked_mut() }; + let isolate = storage_mut.scope.isolate; + scope_owner::enter(isolate); + if storage_mut.scope.needs_scope { + unsafe { + raw::HandleScope::init( + &mut storage_mut.scope.raw_handle_scope, + isolate, + ); + } + } + + let projected = &mut storage_mut.scope; + unsafe { Pin::new_unchecked(projected) } + } + + unsafe fn deinit(me: &mut Self) { + if me.needs_scope { + unsafe { raw::v8__HandleScope__DESTRUCT(&mut me.raw_handle_scope) }; + } + scope_owner::exit(); + } +} + +impl sealed::Sealed for CallbackScope<'_, C> {} +impl Scope for CallbackScope<'_, C> {} + +pub trait NewCallbackScope<'s>: Sized + GetIsolate { + type NewScope: Scope; + const NEEDS_SCOPE: bool = false; + + #[inline] + fn get_context(&self) -> Option> { + None + } + + fn make_new_scope(me: Self) -> Self::NewScope; +} + +fn make_new_callback_scope<'a, C>( + isolate: impl GetIsolate, + context: Option>, +) -> CallbackScope<'a, C> { + let isolate_ptr = + unsafe { NonNull::new_unchecked(isolate.get_isolate_ptr()) }; + let isolate_ref = unsafe { Isolate::from_raw_ref(&isolate_ptr) }; + CallbackScope { + raw_handle_scope: unsafe { raw::HandleScope::uninit() }, + isolate: isolate_ptr, + annex: isolate_ref.get_annex_ptr(), + context: Cell::new(context), + _phantom: PhantomData, + _pinned: PhantomPinned, + needs_scope: false, + } +} + +impl<'s> NewCallbackScope<'s> for &'s mut Isolate { + type NewScope = CallbackScope<'s, ()>; + + fn make_new_scope(me: Self) -> Self::NewScope { + make_new_callback_scope(&*me, None) + } +} + +impl<'s> NewCallbackScope<'s> for &'s mut OwnedIsolate { + type NewScope = CallbackScope<'s, ()>; + + fn make_new_scope(me: Self) -> Self::NewScope { + make_new_callback_scope(&*me, None) + } +} + +impl<'s> NewCallbackScope<'s> for &'s FunctionCallbackInfo { + type NewScope = CallbackScope<'s>; + + fn make_new_scope(me: Self) -> Self::NewScope { + make_new_callback_scope(me, None) + } +} + +impl<'s> NewCallbackScope<'s> for &'s FunctionCallbackInfoParts<'s> { + type NewScope = CallbackScope<'s>; + + fn make_new_scope(me: Self) -> Self::NewScope { + make_new_callback_scope(me, None) + } +} + +impl<'s, T> NewCallbackScope<'s> for &'s PropertyCallbackInfo { + type NewScope = CallbackScope<'s>; + + fn make_new_scope(me: Self) -> Self::NewScope { + make_new_callback_scope(me, None) + } +} + +impl<'s> NewCallbackScope<'s> for &'s FastApiCallbackOptions<'s> { + type NewScope = CallbackScope<'s>; + const NEEDS_SCOPE: bool = true; + + fn make_new_scope(me: Self) -> Self::NewScope { + let isolate_ptr = + unsafe { NonNull::new_unchecked((*me).get_isolate_ptr()) }; + let isolate_ref = unsafe { Isolate::from_raw_ref(&isolate_ptr) }; + CallbackScope { + raw_handle_scope: unsafe { raw::HandleScope::uninit() }, + isolate: isolate_ptr, + annex: isolate_ref.get_annex_ptr(), + context: Cell::new(me.get_context().map(|c| c.as_non_null())), + _phantom: PhantomData, + _pinned: PhantomPinned, + needs_scope: Self::NEEDS_SCOPE, + } + } +} + +impl<'s> NewCallbackScope<'s> for Local<'s, Context> { + type NewScope = CallbackScope<'s>; + + #[inline] + fn get_context(&self) -> Option> { + Some(*self) + } + + fn make_new_scope(me: Self) -> Self::NewScope { + make_new_callback_scope(me, Some(me.as_non_null())) + } +} + +impl<'s> NewCallbackScope<'s> for Local<'s, Message> { + type NewScope = CallbackScope<'s>; + + fn make_new_scope(me: Self) -> Self::NewScope { + make_new_callback_scope(me, None) + } +} + +impl<'s, T: Into> + GetIsolate> NewCallbackScope<'s> for T { + type NewScope = CallbackScope<'s>; + + fn make_new_scope(me: Self) -> Self::NewScope { + make_new_callback_scope(me, None) + } +} + +impl<'s> NewCallbackScope<'s> for &'s PromiseRejectMessage<'s> { + type NewScope = CallbackScope<'s>; + + fn make_new_scope(me: Self) -> Self::NewScope { + make_new_callback_scope(me, None) + } +} + +impl<'s> AsRef>> for CallbackScope<'s, ()> { + fn as_ref(&self) -> &Pin<&'s mut HandleScope<'s, ()>> { + unsafe { std::mem::transmute(self) } + } +} + +/// An external exception handler. +#[repr(C)] +pub struct TryCatch<'scope, 'obj, P> { + raw_try_catch: raw::TryCatch, + scope: &'scope mut PinnedRef<'obj, P>, + _pinned: PhantomPinned, +} + +impl<'scope, P: NewTryCatch<'scope>> TryCatch<'scope, '_, P> { + #[allow(clippy::new_ret_no_self)] + pub fn new(param: &'scope mut P) -> ScopeStorage { + ScopeStorage::new(P::make_new_scope(param)) + } +} + +impl ScopeInit for TryCatch<'_, '_, P> { + fn init_stack(storage: Pin<&mut ScopeStorage>) -> Pin<&mut Self> { + let storage_mut = unsafe { storage.get_unchecked_mut() }; + let isolate = unsafe { + NonNull::new_unchecked(storage_mut.scope.scope.get_isolate_ptr()) + }; + unsafe { + raw::TryCatch::init(&mut storage_mut.scope.raw_try_catch, isolate); + } + let projected = &mut storage_mut.scope; + unsafe { Pin::new_unchecked(projected) } + } + + unsafe fn deinit(me: &mut Self) { + unsafe { raw::v8__TryCatch__DESTRUCT(&mut me.raw_try_catch) }; + } +} + +impl<'scope, 'obj: 'scope, 'iso: 'obj, P: GetIsolate> + PinnedRef<'_, TryCatch<'scope, 'obj, P>> +where + PinnedRef<'obj, P>: AsRef>>, +{ + /// Returns true if an exception has been caught by this try/catch block. + #[inline(always)] + pub fn has_caught(&self) -> bool { + unsafe { raw::v8__TryCatch__HasCaught(self.get_raw()) } + } + + /// For certain types of exceptions, it makes no sense to continue execution. + /// + /// If CanContinue returns false, the correct action is to perform any C++ + /// cleanup needed and then return. If CanContinue returns false and + /// HasTerminated returns true, it is possible to call + /// CancelTerminateExecution in order to continue calling into the engine. + #[inline(always)] + pub fn can_continue(&self) -> bool { + unsafe { raw::v8__TryCatch__CanContinue(self.get_raw()) } + } + + /// Returns true if an exception has been caught due to script execution + /// being terminated. + /// + /// There is no JavaScript representation of an execution termination + /// exception. Such exceptions are thrown when the TerminateExecution + /// methods are called to terminate a long-running script. + /// + /// If such an exception has been thrown, HasTerminated will return true, + /// indicating that it is possible to call CancelTerminateExecution in order + /// to continue calling into the engine. + #[inline(always)] + pub fn has_terminated(&self) -> bool { + unsafe { raw::v8__TryCatch__HasTerminated(self.get_raw()) } + } + + /// Returns true if verbosity is enabled. + #[inline(always)] + pub fn is_verbose(&self) -> bool { + unsafe { raw::v8__TryCatch__IsVerbose(self.get_raw()) } + } + + /// Set verbosity of the external exception handler. + /// + /// By default, exceptions that are caught by an external exception + /// handler are not reported. Call SetVerbose with true on an + /// external exception handler to have exceptions caught by the + /// handler reported as if they were not caught. + #[inline(always)] + pub fn set_verbose(&mut self, value: bool) { + unsafe { raw::v8__TryCatch__SetVerbose(self.get_raw_mut(), value) }; + } + + /// Set whether or not this TryCatch should capture a Message object + /// which holds source information about where the exception + /// occurred. True by default. + #[inline(always)] + pub fn set_capture_message(&mut self, value: bool) { + unsafe { raw::v8__TryCatch__SetCaptureMessage(self.get_raw_mut(), value) }; + } + + /// Clears any exceptions that may have been caught by this try/catch block. + /// After this method has been called, HasCaught() will return false. Cancels + /// the scheduled exception if it is caught and ReThrow() is not called + /// before. + /// + /// It is not necessary to clear a try/catch block before using it again; if + /// another exception is thrown the previously caught exception will just be + /// overwritten. However, it is often a good idea since it makes it easier + /// to determine which operation threw a given exception. + #[inline(always)] + pub fn reset(&mut self) { + unsafe { raw::v8__TryCatch__Reset(self.get_raw_mut()) }; + } + + #[inline(always)] + fn get_raw(&self) -> &raw::TryCatch { + &self.0.raw_try_catch + } + + #[inline(always)] + unsafe fn get_raw_mut(&mut self) -> &mut raw::TryCatch { + unsafe { &mut self.0.as_mut().get_unchecked_mut().raw_try_catch } + } + + pub fn exception(&self) -> Option> { + unsafe { + self + .0 + .scope + .as_ref() + .cast_local(|_data| raw::v8__TryCatch__Exception(self.get_raw())) + } + } + + pub fn message(&self) -> Option> { + unsafe { + self + .0 + .scope + .as_ref() + .cast_local(|_data| raw::v8__TryCatch__Message(self.get_raw())) + } + } + + pub fn rethrow(&mut self) -> Option> { + let raw_mut = unsafe { self.get_raw_mut() as *mut raw::TryCatch }; + unsafe { + self + .0 + .scope + .as_ref() + .cast_local(|_data| raw::v8__TryCatch__ReThrow(raw_mut)) + } + } + + pub fn stack_trace(&self) -> Option> { + unsafe { + self.0.scope.as_ref().cast_local(|_data| { + raw::v8__TryCatch__StackTrace( + self.get_raw(), + _data.get_current_context(), + ) + }) + } + } +} + +impl

sealed::Sealed for TryCatch<'_, '_, P> {} +impl Scope for TryCatch<'_, '_, P> {} + +pub trait NewTryCatch<'scope>: GetIsolate { + type NewScope: Scope; + fn make_new_scope(me: &'scope mut Self) -> Self::NewScope; +} + +impl<'scope, 'obj: 'scope, 'i, C> NewTryCatch<'scope> + for PinnedRef<'obj, HandleScope<'i, C>> +{ + type NewScope = TryCatch<'scope, 'obj, HandleScope<'i, C>>; + fn make_new_scope(me: &'scope mut Self) -> Self::NewScope { + TryCatch { + scope: me, + raw_try_catch: unsafe { raw::TryCatch::uninit() }, + _pinned: PhantomPinned, + } + } +} + +// the lifetimes here are: +// - 'borrow: the lifetime of the borrow of the scope +// - 'scope: the lifetime of the escapable handle scope +// - 'obj: the lifetime of the handles created from the escapable handle scope +// - 'esc: the lifetime of the slot that the escapable handle scope will eventually escape to. this must be longer than 'obj +impl<'borrow, 'scope: 'borrow, 'obj: 'borrow, 'esc: 'obj, C> + NewTryCatch<'borrow> + for PinnedRef<'scope, EscapableHandleScope<'obj, 'esc, C>> +{ + type NewScope = + TryCatch<'borrow, 'scope, EscapableHandleScope<'obj, 'esc, C>>; + + fn make_new_scope(me: &'borrow mut Self) -> Self::NewScope { + TryCatch { + scope: me, + raw_try_catch: unsafe { raw::TryCatch::uninit() }, + _pinned: PhantomPinned, + } + } +} + +impl<'scope, 'obj: 'scope, T: GetIsolate + Scope + ClearCachedContext> + NewTryCatch<'scope> for ContextScope<'_, 'obj, T> +{ + type NewScope = TryCatch<'scope, 'obj, T>; + fn make_new_scope(me: &'scope mut Self) -> Self::NewScope { + TryCatch { + scope: me, + raw_try_catch: unsafe { raw::TryCatch::uninit() }, + _pinned: PhantomPinned, + } + } +} + +impl<'scope, 'obj: 'scope, 'i, C> NewTryCatch<'scope> + for PinnedRef<'obj, CallbackScope<'i, C>> +{ + type NewScope = TryCatch<'scope, 'i, HandleScope<'i, C>>; + fn make_new_scope(me: &'scope mut Self) -> Self::NewScope { + TryCatch { + scope: unsafe { + std::mem::transmute::< + &mut PinnedRef<'_, CallbackScope<'_, C>>, + &mut PinnedRef<'_, HandleScope<'_, C>>, + >(me) + }, + raw_try_catch: unsafe { raw::TryCatch::uninit() }, + _pinned: PhantomPinned, + } + } +} + +impl<'scope, 'obj: 'scope, 'obj_outer: 'obj, 'iso, C> NewTryCatch<'scope> + for PinnedRef<'obj, TryCatch<'_, 'obj_outer, HandleScope<'iso, C>>> +{ + type NewScope = TryCatch<'scope, 'obj_outer, HandleScope<'iso, C>>; + fn make_new_scope(me: &'scope mut Self) -> Self::NewScope { + TryCatch { + scope: unsafe { me.as_mut_ref().0.get_unchecked_mut().scope }, + raw_try_catch: unsafe { raw::TryCatch::uninit() }, + _pinned: PhantomPinned, + } + } +} + +/// A HandleScope which first allocates a handle in the current scope +/// which will be later filled with the escape value. +#[repr(C)] +pub struct EscapableHandleScope<'s, 'esc: 's, C = Context> { + raw_handle_scope: raw::HandleScope, + isolate: NonNull, + annex: NonNull, + context: Cell>>, + _phantom: + PhantomData<(&'s mut raw::HandleScope, &'esc mut raw::EscapeSlot, &'s C)>, + _pinned: PhantomPinned, + raw_escape_slot: Option, +} + +assert_layout_subset!(HandleScope<'static, ()>, EscapableHandleScope<'static, 'static, ()> { + raw_handle_scope, + isolate, + annex, + context, + _phantom, + _pinned, +}); + +impl<'s, 'esc: 's, C> ScopeInit for EscapableHandleScope<'s, 'esc, C> { + fn init_stack(storage: Pin<&mut ScopeStorage>) -> Pin<&mut Self> { + let storage_mut = unsafe { storage.get_unchecked_mut() }; + scope_owner::enter(storage_mut.scope.isolate); + unsafe { + let isolate = storage_mut.scope.isolate; + raw::HandleScope::init(&mut storage_mut.scope.raw_handle_scope, isolate); + } + let projected = &mut storage_mut.scope; + + unsafe { Pin::new_unchecked(projected) } + } + + unsafe fn deinit(me: &mut Self) { + unsafe { raw::v8__HandleScope__DESTRUCT(&raw mut me.raw_handle_scope) }; + scope_owner::exit(); + } +} + +impl<'s, 'esc: 's> EscapableHandleScope<'s, 'esc> { + #[allow(clippy::new_ret_no_self)] + pub fn new>( + scope: &'s mut P, + ) -> ScopeStorage { + ScopeStorage::new(P::make_new_scope(scope)) + } +} + +impl<'s, 'esc: 's, C> PinnedRef<'_, EscapableHandleScope<'s, 'esc, C>> { + /// Pushes the value into the previous scope and returns a handle to it. + /// Cannot be called twice. + pub fn escape<'a, T>(&mut self, value: Local<'a, T>) -> Local<'esc, T> + where + for<'l> Local<'l, T>: Into>, + { + let escape_slot = unsafe { self.0.as_mut().get_unchecked_mut() } + .raw_escape_slot + .take() + .expect("EscapableHandleScope::escape() called twice"); + escape_slot.escape(value) + } +} + +impl<'p, 's, 'esc: 's, C> Deref + for PinnedRef<'p, EscapableHandleScope<'s, 'esc, C>> +{ + type Target = PinnedRef<'p, HandleScope<'s, C>>; + fn deref(&self) -> &Self::Target { + unsafe { std::mem::transmute(self) } + } +} + +impl<'s, 'esc: 's, C> DerefMut + for PinnedRef<'_, EscapableHandleScope<'s, 'esc, C>> +{ + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { std::mem::transmute(self) } + } +} + +pub trait NewEscapableHandleScope<'s> { + type NewScope: Scope; + fn make_new_scope(me: &'s mut Self) -> Self::NewScope; +} + +impl<'s> NewEscapableHandleScope<'s> for Isolate { + type NewScope = EscapableHandleScope<'s, 's, ()>; + + fn make_new_scope(me: &'s mut Self) -> Self::NewScope { + let isolate = unsafe { NonNull::new_unchecked(me.as_real_ptr()) }; + let annex = me.get_annex_ptr(); + let raw_escape_slot = raw::EscapeSlot::new(isolate); + let raw_handle_scope = unsafe { raw::HandleScope::uninit() }; + + EscapableHandleScope { + isolate, + annex, + context: Cell::new(None), + raw_escape_slot: Some(raw_escape_slot), + raw_handle_scope, + _phantom: PhantomData, + _pinned: PhantomPinned, + } + } +} + +impl<'s, 'obj: 's, C> NewEscapableHandleScope<'s> + for PinnedRef<'obj, HandleScope<'_, C>> +{ + type NewScope = EscapableHandleScope<'s, 'obj, C>; + fn make_new_scope(me: &'s mut Self) -> Self::NewScope { + // Note: the `raw_escape_slot` field must be initialized _before_ the + // `raw_handle_scope` field, otherwise the escaped local handle ends up + // inside the `EscapableHandleScope` that's being constructed here, + // rather than escaping from it. + let isolate = me.0.isolate; + let annex = me.0.annex; + let raw_escape_slot = raw::EscapeSlot::new(isolate); + let raw_handle_scope = unsafe { raw::HandleScope::uninit() }; + + EscapableHandleScope { + isolate, + annex, + context: Cell::new(me.0.context.get()), + raw_escape_slot: Some(raw_escape_slot), + raw_handle_scope, + _phantom: PhantomData, + _pinned: PhantomPinned, + } + } +} + +impl<'borrow, 'obj: 'borrow, C> NewEscapableHandleScope<'borrow> + for ContextScope<'_, 'obj, HandleScope<'_, C>> +{ + type NewScope = EscapableHandleScope<'borrow, 'obj, C>; + fn make_new_scope(me: &'borrow mut Self) -> Self::NewScope { + NewEscapableHandleScope::make_new_scope(me.scope) + } +} + +impl<'borrow, 's: 'borrow, 'esc: 'borrow, C> NewEscapableHandleScope<'borrow> + for PinnedRef<'_, EscapableHandleScope<'s, 'esc, C>> +{ + type NewScope = EscapableHandleScope<'borrow, 's, C>; + fn make_new_scope(me: &'borrow mut Self) -> Self::NewScope { + // Note: the `raw_escape_slot` field must be initialized _before_ the + // `raw_handle_scope` field, otherwise the escaped local handle ends up + // inside the `EscapableHandleScope` that's being constructed here, + // rather than escaping from it. + let isolate = me.0.isolate; + let annex = me.0.annex; + let raw_escape_slot = raw::EscapeSlot::new(isolate); + let raw_handle_scope = unsafe { raw::HandleScope::uninit() }; + EscapableHandleScope { + isolate, + annex, + context: Cell::new(me.0.context.get()), + raw_escape_slot: Some(raw_escape_slot), + raw_handle_scope, + _phantom: PhantomData, + _pinned: PhantomPinned, + } + } +} + +impl<'s, 'esc: 's, C> sealed::Sealed for EscapableHandleScope<'s, 'esc, C> {} +impl<'s, 'esc: 's, C> Scope for EscapableHandleScope<'s, 'esc, C> {} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(C)] +pub enum OnFailure { + CrashOnFailure, + ThrowOnFailure, + DumpOnFailure, +} + +#[repr(C)] +pub struct DisallowJavascriptExecutionScope<'scope, 'obj, P> { + raw: raw::DisallowJavascriptExecutionScope, + scope: &'scope mut PinnedRef<'obj, P>, + on_failure: OnFailure, + _pinned: PhantomPinned, +} + +impl ScopeInit for DisallowJavascriptExecutionScope<'_, '_, P> { + fn init_stack(storage: Pin<&mut ScopeStorage>) -> Pin<&mut Self> { + // SAFETY: we aren't going to move the raw scope out + let storage_mut = unsafe { storage.get_unchecked_mut() }; + let isolate = storage_mut.scope.scope.get_isolate_ptr(); + let on_failure = storage_mut.scope.on_failure; + // SAFETY: calling the raw function, isolate is valid, and the raw scope won't be moved + unsafe { + raw::DisallowJavascriptExecutionScope::init( + &mut storage_mut.scope.raw, + NonNull::new_unchecked(isolate), + on_failure, + ); + Pin::new_unchecked(&mut storage_mut.scope) + } + } + + unsafe fn deinit(me: &mut Self) { + unsafe { raw::v8__DisallowJavascriptExecutionScope__DESTRUCT(&mut me.raw) }; + } +} + +impl sealed::Sealed + for DisallowJavascriptExecutionScope<'_, '_, P> +{ +} +impl Scope + for DisallowJavascriptExecutionScope<'_, '_, P> +{ +} + +impl<'scope, P: NewDisallowJavascriptExecutionScope<'scope>> + DisallowJavascriptExecutionScope<'scope, '_, P> +{ + #[allow(clippy::new_ret_no_self)] + pub fn new( + param: &'scope mut P, + on_failure: OnFailure, + ) -> ScopeStorage { + ScopeStorage::new(P::make_new_scope(param, on_failure)) + } +} + +pub trait NewDisallowJavascriptExecutionScope<'scope> { + type NewScope: Scope; + fn make_new_scope( + me: &'scope mut Self, + on_failure: OnFailure, + ) -> Self::NewScope; +} + +impl<'scope, 'obj, P> NewDisallowJavascriptExecutionScope<'scope> + for ContextScope<'_, 'obj, P> +where + P: ClearCachedContext, + PinnedRef<'obj, P>: NewDisallowJavascriptExecutionScope<'scope>, +{ + type NewScope = as NewDisallowJavascriptExecutionScope< + 'scope, + >>::NewScope; + fn make_new_scope( + me: &'scope mut Self, + on_failure: OnFailure, + ) -> Self::NewScope { + PinnedRef::<'obj, P>::make_new_scope(me.scope, on_failure) + } +} + +impl<'scope, 'obj: 'scope, P: Scope + GetIsolate> + NewDisallowJavascriptExecutionScope<'scope> for PinnedRef<'obj, P> +{ + type NewScope = DisallowJavascriptExecutionScope<'scope, 'obj, P>; + + fn make_new_scope( + me: &'scope mut Self, + on_failure: OnFailure, + ) -> Self::NewScope { + DisallowJavascriptExecutionScope { + raw: unsafe { raw::DisallowJavascriptExecutionScope::uninit() }, + scope: me, + on_failure, + _pinned: PhantomPinned, + } + } +} + +#[repr(C)] +pub struct AllowJavascriptExecutionScope<'scope, 'obj, P> { + raw: raw::AllowJavascriptExecutionScope, + scope: &'scope mut PinnedRef<'obj, P>, + _pinned: PhantomPinned, +} + +impl ScopeInit for AllowJavascriptExecutionScope<'_, '_, P> { + fn init_stack(storage: Pin<&mut ScopeStorage>) -> Pin<&mut Self> { + let storage_mut = unsafe { storage.get_unchecked_mut() }; + let isolate = unsafe { + NonNull::new_unchecked(storage_mut.scope.scope.get_isolate_ptr()) + }; + unsafe { + raw::AllowJavascriptExecutionScope::init( + &mut storage_mut.scope.raw, + isolate, + ); + } + let projected = &mut storage_mut.scope; + unsafe { Pin::new_unchecked(projected) } + } + + unsafe fn deinit(me: &mut Self) { + unsafe { raw::v8__AllowJavascriptExecutionScope__DESTRUCT(&mut me.raw) }; + } +} + +impl sealed::Sealed + for AllowJavascriptExecutionScope<'_, '_, P> +{ +} +impl Scope for AllowJavascriptExecutionScope<'_, '_, P> {} + +impl<'scope, P: NewAllowJavascriptExecutionScope<'scope>> + AllowJavascriptExecutionScope<'scope, '_, P> +{ + #[allow(clippy::new_ret_no_self)] + pub fn new(param: &'scope mut P) -> ScopeStorage { + ScopeStorage::new(P::make_new_scope(param)) + } +} + +pub trait NewAllowJavascriptExecutionScope<'scope> { + type NewScope: Scope; + fn make_new_scope(me: &'scope mut Self) -> Self::NewScope; +} + +impl<'scope, 'obj: 'scope, P: Scope + GetIsolate> + NewAllowJavascriptExecutionScope<'scope> for PinnedRef<'obj, P> +{ + type NewScope = AllowJavascriptExecutionScope<'scope, 'obj, P>; + fn make_new_scope(me: &'scope mut Self) -> Self::NewScope { + AllowJavascriptExecutionScope { + raw: unsafe { raw::AllowJavascriptExecutionScope::uninit() }, + scope: me, + _pinned: PhantomPinned, + } + } +} + +// Note: the macros below _do not_ use std::pin::pin! because +// it leads to worse compiler errors when the scope doesn't live long enough. +// Instead, we do the same thing as std::pin::pin! but without the additional temporary scope. + +#[allow(unused_macros, clippy::macro_metavars_in_unsafe)] +#[macro_export] +macro_rules! callback_scope { + (unsafe $scope: ident, $param: expr $(,)?) => { + #[allow(clippy::macro_metavars_in_unsafe)] + let mut $scope = { + // force the caller to put a separate unsafe block around the param expr + let param = $param; + unsafe { $crate::CallbackScope::new(param) } + }; + // SAFETY: we are shadowing the original binding, so it can't be accessed + // ever again + let mut $scope = { + let scope_pinned = unsafe { std::pin::Pin::new_unchecked(&mut $scope) }; + scope_pinned.init() + }; + let $scope = &mut $scope; + }; + (unsafe let $scope: ident, $param: expr $(,)?) => { + $crate::callback_scope!(unsafe $scope, $param); + } +} + +#[allow(unused_imports)] +pub(crate) use callback_scope; + +/// Creates a pinned `HandleScope` and binds `&mut PinScope` to `$scope`. +/// +/// ```rust +/// # let platform = v8::new_default_platform(0, false).make_shared(); +/// # v8::V8::initialize_platform(platform); +/// # v8::V8::initialize(); +/// # let isolate = &mut v8::Isolate::new(Default::default()); +/// v8::scope!(let scope, isolate); +/// ``` +#[allow(unused_macros)] +#[macro_export] +macro_rules! scope { + ($scope: ident, $param: expr $(,)?) => { + let mut $scope = $crate::HandleScope::new($param); + // SAFETY: we are shadowing the original binding, so it can't be accessed + // ever again + let mut $scope = { + let scope_pinned = unsafe { std::pin::Pin::new_unchecked(&mut $scope) }; + scope_pinned.init() + }; + let $scope = &mut $scope; + }; + (let $scope: ident, $param: expr $(,)?) => { + $crate::scope!($scope, $param); + }; +} + +#[allow(unused_imports)] +pub(crate) use scope; + +#[allow(unused_macros)] +#[macro_export] +macro_rules! scope_with_context { + ($scope: ident, $param: expr, $context: expr $(,)?) => { + let mut $scope = $crate::HandleScope::new($param); + // SAFETY: we are shadowing the original binding, so it can't be accessed + // ever again + let mut $scope = { + let scope_pinned = unsafe { std::pin::Pin::new_unchecked(&mut $scope) }; + scope_pinned.init() + }; + let $scope = &mut $scope; + let context = v8::Local::new($scope, $context); + let $scope = &mut $crate::ContextScope::new($scope, context); + }; + (let $scope: ident, $param: expr, $context: expr $(,)?) => { + $crate::scope_with_context!($scope, $param, $context); + }; +} + +#[allow(unused_imports)] +pub(crate) use scope_with_context; + +#[allow(unused_macros)] +#[macro_export] +macro_rules! tc_scope { + ($scope: ident, $param: expr $(,)?) => { + let mut $scope = $crate::TryCatch::new($param); + // SAFETY: we are shadowing the original binding, so it can't be accessed + // ever again + let mut $scope = { + let scope_pinned = unsafe { std::pin::Pin::new_unchecked(&mut $scope) }; + scope_pinned.init() + }; + let $scope = &mut $scope; + }; + (let $scope: ident, $param: expr $(,)?) => { + $crate::tc_scope!($scope, $param); + }; +} + +#[macro_export] +macro_rules! disallow_javascript_execution_scope { + ($scope: ident, $param: expr, $on_failure: expr $(,)?) => { + let mut $scope = + $crate::DisallowJavascriptExecutionScope::new($param, $on_failure); + // SAFETY: we are shadowing the original binding, so it can't be accessed + // ever again + let mut $scope = { + let scope_pinned = unsafe { std::pin::Pin::new_unchecked(&mut $scope) }; + scope_pinned.init() + }; + let $scope = &mut $scope; + }; + (let $scope: ident, $param: expr, $on_failure: expr $(,)?) => { + $crate::disallow_javascript_execution_scope!($scope, $param, $on_failure); + }; +} + +#[allow(unused_imports)] +pub(crate) use disallow_javascript_execution_scope; + +#[macro_export] +macro_rules! allow_javascript_execution_scope { + ($scope: ident, $param: expr $(,)?) => { + let mut $scope = $crate::AllowJavascriptExecutionScope::new($param); + let mut $scope = { + let scope_pinned = unsafe { std::pin::Pin::new_unchecked(&mut $scope) }; + scope_pinned.init() + }; + let $scope = &mut $scope; + }; + (let $scope: ident, $param: expr $(,)?) => { + $crate::allow_javascript_execution_scope!($scope, $param); + }; +} + +#[allow(unused_imports)] +pub(crate) use allow_javascript_execution_scope; + +#[macro_export] +macro_rules! escapable_handle_scope { + ($scope: ident, $param: expr $(,)?) => { + let mut $scope = $crate::EscapableHandleScope::new($param); + let mut $scope = { + let scope_pinned = unsafe { std::pin::Pin::new_unchecked(&mut $scope) }; + scope_pinned.init() + }; + let $scope = &mut $scope; + }; + (let $scope: ident, $param: expr $(,)?) => { + $crate::escapable_handle_scope!($scope, $param); + }; +} + +#[allow(unused_imports)] +pub(crate) use escapable_handle_scope; + +#[repr(transparent)] +pub struct PinnedRef<'p, T>(Pin<&'p mut T>); + +impl<'p, T> From> for PinnedRef<'p, T> { + fn from(value: Pin<&'p mut T>) -> Self { + PinnedRef(value) + } +} + +impl PinnedRef<'_, T> { + pub fn as_mut_ref(&mut self) -> PinnedRef<'_, T> { + PinnedRef(self.0.as_mut()) + } +} + +unsafe fn cast_pinned_ref<'b, 'o, I, O>( + pinned: &PinnedRef<'_, I>, +) -> &'b PinnedRef<'o, O> { + unsafe { std::mem::transmute(pinned) } +} + +unsafe fn cast_pinned_ref_mut<'b, 'o, I, O>( + pinned: &mut PinnedRef<'_, I>, +) -> &'b mut PinnedRef<'o, O> { + unsafe { std::mem::transmute(pinned) } +} + +impl<'p, 'i> Deref for PinnedRef<'p, HandleScope<'i>> { + type Target = PinnedRef<'p, HandleScope<'i, ()>>; + fn deref(&self) -> &Self::Target { + unsafe { cast_pinned_ref::, HandleScope<'i, ()>>(self) } + } +} + +impl DerefMut for PinnedRef<'_, HandleScope<'_>> { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { cast_pinned_ref_mut::, HandleScope<'_, ()>>(self) } + } +} + +impl Deref for PinnedRef<'_, HandleScope<'_, ()>> { + type Target = Isolate; + #[inline(always)] + fn deref(&self) -> &Self::Target { + unsafe { Isolate::from_raw_ref(&self.0.isolate) } + } +} + +impl DerefMut for PinnedRef<'_, HandleScope<'_, ()>> { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { + Isolate::from_raw_ref_mut( + &mut self.0.as_mut().get_unchecked_mut().isolate, + ) + } + } +} + +impl<'i> Deref for PinnedRef<'_, CallbackScope<'i>> { + // You may notice the output lifetime is a little bit weird here. + // Basically, we're saying that any Handles created from this `CallbackScope` + // will live as long as the thing that we made the `CallbackScope` from. + // In practice, this means that the caller of `CallbackScope::new` needs to + // be careful to ensure that the input lifetime is a safe approximation. + type Target = PinnedRef<'i, HandleScope<'i>>; + fn deref(&self) -> &Self::Target { + unsafe { cast_pinned_ref::, HandleScope<'i>>(self) } + } +} + +impl DerefMut for PinnedRef<'_, CallbackScope<'_>> { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { cast_pinned_ref_mut::, HandleScope<'_>>(self) } + } +} + +impl<'i> Deref for PinnedRef<'_, CallbackScope<'i, ()>> { + type Target = PinnedRef<'i, HandleScope<'i, ()>>; + fn deref(&self) -> &Self::Target { + unsafe { + cast_pinned_ref::, HandleScope<'i, ()>>(self) + } + } +} + +impl DerefMut for PinnedRef<'_, CallbackScope<'_, ()>> { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { + cast_pinned_ref_mut::, HandleScope<'_, ()>>(self) + } + } +} + +impl<'obj, 'iso, C> Deref + for PinnedRef<'_, TryCatch<'_, 'obj, HandleScope<'iso, C>>> +{ + type Target = PinnedRef<'obj, HandleScope<'iso, C>>; + fn deref(&self) -> &Self::Target { + self.0.scope + } +} + +impl DerefMut for PinnedRef<'_, TryCatch<'_, '_, HandleScope<'_, C>>> { + fn deref_mut(&mut self) -> &mut Self::Target { + // SAFETY: we're just projecting the pinned reference, it still can't be moved + unsafe { self.as_mut_ref().0.get_unchecked_mut().scope } + } +} + +impl<'borrow, 'scope: 'borrow, 'obj: 'borrow, 'esc: 'obj, C> Deref + for PinnedRef< + '_, + TryCatch<'borrow, 'scope, EscapableHandleScope<'obj, 'esc, C>>, + > +{ + type Target = PinnedRef<'scope, EscapableHandleScope<'obj, 'esc, C>>; + + fn deref(&self) -> &Self::Target { + self.0.scope + } +} + +impl<'borrow, 'scope: 'borrow, 'obj: 'borrow, 'esc: 'obj, C> DerefMut + for PinnedRef< + '_, + TryCatch<'borrow, 'scope, EscapableHandleScope<'obj, 'esc, C>>, + > +{ + fn deref_mut(&mut self) -> &mut Self::Target { + // SAFETY: we're just projecting the pinned reference, it still can't be moved + unsafe { self.0.as_mut().get_unchecked_mut().scope } + } +} + +impl<'obj, P> Deref + for PinnedRef<'_, DisallowJavascriptExecutionScope<'_, 'obj, P>> +{ + type Target = PinnedRef<'obj, P>; + fn deref(&self) -> &Self::Target { + self.0.scope + } +} + +impl

DerefMut + for PinnedRef<'_, DisallowJavascriptExecutionScope<'_, '_, P>> +{ + fn deref_mut(&mut self) -> &mut Self::Target { + // SAFETY: we're just projecting the pinned reference, it still can't be moved + unsafe { self.0.as_mut().get_unchecked_mut().scope } + } +} + +impl<'obj, P> Deref + for PinnedRef<'_, AllowJavascriptExecutionScope<'_, 'obj, P>> +{ + type Target = PinnedRef<'obj, P>; + fn deref(&self) -> &Self::Target { + self.0.scope + } +} +impl

DerefMut for PinnedRef<'_, AllowJavascriptExecutionScope<'_, '_, P>> { + fn deref_mut(&mut self) -> &mut Self::Target { + // SAFETY: we're just projecting the pinned reference, it still can't be moved + unsafe { self.0.as_mut().get_unchecked_mut().scope } + } +} + +impl

GetIsolate for PinnedRef<'_, P> +where + P: GetIsolate, +{ + fn get_isolate_ptr(&self) -> *mut RealIsolate { + self.0.get_isolate_ptr() + } +} + +impl AsRef for PinnedRef<'_, HandleScope<'_, C>> { + fn as_ref(&self) -> &Isolate { + unsafe { Isolate::from_raw_ref(&self.0.isolate) } + } +} + +impl AsMut for PinnedRef<'_, HandleScope<'_, C>> { + fn as_mut(&mut self) -> &mut Isolate { + unsafe { + Isolate::from_raw_ref_mut( + &mut self.0.as_mut().get_unchecked_mut().isolate, + ) + } + } +} + +// Cached slot accessors that use the annex pointer stored in the scope, +// avoiding an FFI call to v8__Isolate__GetData on every access. +// These shadow the Isolate::get_slot/set_slot/etc. methods in the Deref chain. +impl PinnedRef<'_, HandleScope<'_, C>> { + /// Get a reference to embedder data added with `set_slot()`. + #[inline(always)] + pub fn get_slot(&self) -> Option<&T> { + let annex = unsafe { self.0.annex.as_ref() }; + annex.get_slot::() + } + + /// Get a mutable reference to embedder data added with `set_slot()`. + #[inline(always)] + pub fn get_slot_mut(&mut self) -> Option<&mut T> { + let annex = unsafe { &mut *self.0.annex.as_ptr() }; + annex.get_slot_mut::() + } + + /// Use with `get_slot` and `get_slot_mut` to associate state with an Isolate. + /// + /// This method gives ownership of value to the Isolate. Exactly one object of + /// each type can be associated with an Isolate. If called more than once with + /// an object of the same type, the earlier version will be dropped and + /// replaced. + /// + /// Returns true if value was set without replacing an existing value. + /// + /// The value will be dropped when the isolate is dropped. + #[inline(always)] + pub fn set_slot(&mut self, value: T) -> bool { + let annex = unsafe { &mut *self.0.annex.as_ptr() }; + annex.set_slot(value) + } + + /// Removes the embedder data added with `set_slot()` and returns it if it + /// exists. + #[inline(always)] + pub fn remove_slot(&mut self) -> Option { + let annex = unsafe { &mut *self.0.annex.as_ptr() }; + annex.remove_slot::() + } +} + +impl AsRef for PinnedRef<'_, CallbackScope<'_, C>> { + fn as_ref(&self) -> &Isolate { + unsafe { Isolate::from_raw_ref(&self.0.isolate) } + } +} + +impl AsMut for PinnedRef<'_, CallbackScope<'_, C>> { + fn as_mut(&mut self) -> &mut Isolate { + unsafe { + Isolate::from_raw_ref_mut( + &mut self.0.as_mut().get_unchecked_mut().isolate, + ) + } + } +} + +impl AsRef for PinnedRef<'_, TryCatch<'_, '_, HandleScope<'_, C>>> { + fn as_ref(&self) -> &Isolate { + unsafe { Isolate::from_raw_ref(&self.0.scope.0.isolate) } + } +} + +impl AsMut for PinnedRef<'_, TryCatch<'_, '_, HandleScope<'_, C>>> { + fn as_mut(&mut self) -> &mut Isolate { + unsafe { + Isolate::from_raw_ref_mut( + &mut self + .0 + .as_mut() + .get_unchecked_mut() + .scope + .0 + .as_mut() + .get_unchecked_mut() + .isolate, + ) + } + } +} + +impl AsRef + for PinnedRef< + '_, + DisallowJavascriptExecutionScope<'_, '_, HandleScope<'_, C>>, + > +{ + fn as_ref(&self) -> &Isolate { + unsafe { Isolate::from_raw_ref(&self.0.scope.0.isolate) } + } +} + +impl AsRef + for PinnedRef<'_, AllowJavascriptExecutionScope<'_, '_, HandleScope<'_, C>>> +{ + fn as_ref(&self) -> &Isolate { + unsafe { Isolate::from_raw_ref(&self.0.scope.0.isolate) } + } +} + +impl AsRef for PinnedRef<'_, EscapableHandleScope<'_, '_>> { + fn as_ref(&self) -> &Isolate { + unsafe { Isolate::from_raw_ref(&self.0.isolate) } + } +} + +impl AsRef for ContextScope<'_, '_, HandleScope<'_, C>> { + fn as_ref(&self) -> &Isolate { + unsafe { Isolate::from_raw_ref(&self.scope.0.isolate) } + } +} +impl AsRef for ContextScope<'_, '_, CallbackScope<'_, C>> { + fn as_ref(&self) -> &Isolate { + unsafe { Isolate::from_raw_ref(&self.scope.0.isolate) } + } +} + +impl<'pin, 's, 'esc: 's, C> AsRef>> + for PinnedRef<'pin, EscapableHandleScope<'s, 'esc, C>> +{ + fn as_ref(&self) -> &PinnedRef<'pin, HandleScope<'s, C>> { + unsafe { + cast_pinned_ref::, HandleScope<'s, C>>( + self, + ) + } + } +} + +impl<'obj, 'inner, C> AsRef>> + for PinnedRef<'obj, HandleScope<'inner, C>> +{ + fn as_ref(&self) -> &PinnedRef<'obj, HandleScope<'inner, C>> { + self + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::ContextOptions; + use std::any::type_name; + use std::pin::pin; + + trait SameType {} + impl SameType for (A, A) {} + + /// `AssertTypeOf` facilitates comparing types. The important difference with + /// assigning a value to a variable with an explicitly stated type is that the + /// latter allows coercions and dereferencing to change the type, whereas + /// `AssertTypeOf` requires the compared types to match exactly. + struct AssertTypeOf<'a, T>(#[allow(dead_code)] &'a T); + impl AssertTypeOf<'_, T> { + pub fn is(self) + where + (A, T): SameType, + { + assert_eq!(type_name::(), type_name::()); + } + } + + #[test] + fn deref_types() { + crate::initialize_v8(); + let isolate = &mut Isolate::new(Default::default()); + AssertTypeOf(isolate).is::(); + let l1_hs = pin!(HandleScope::new(isolate)); + let l1_hs = &mut l1_hs.init(); + AssertTypeOf(l1_hs).is::>>(); + let context = Context::new(l1_hs, ContextOptions::default()); + { + let l2_cxs = &mut ContextScope::new(l1_hs, context); + AssertTypeOf(l2_cxs).is::>(); + { + let d = l2_cxs.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + } + { + tc_scope!(let l3_tc, &mut **l2_cxs); + AssertTypeOf(l3_tc).is::>>(); + let d = l3_tc.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + } + { + disallow_javascript_execution_scope!(let l3_djses, l2_cxs, OnFailure::CrashOnFailure); + AssertTypeOf(l3_djses) + .is::>>(); + let d = l3_djses.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + { + allow_javascript_execution_scope!(let l4_ajses, l3_djses); + AssertTypeOf(l4_ajses).is::, + >, + >>(); + let d = l4_ajses.deref_mut(); + AssertTypeOf(d) + .is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + } + } + { + escapable_handle_scope!(let l3_ehs, l2_cxs); + AssertTypeOf(l3_ehs).is::>(); + { + let l4_cxs = &mut ContextScope::new(l3_ehs, context); + AssertTypeOf(l4_cxs).is::>(); + let d = l4_cxs.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + } + { + tc_scope!(let l4_tc, l3_ehs); + AssertTypeOf(l4_tc).is::>>(); + let d = l4_tc.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + } + { + disallow_javascript_execution_scope!(let l4_djses, l3_ehs, OnFailure::CrashOnFailure); + AssertTypeOf(l4_djses) + .is::>>(); + let d = l4_djses.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + { + allow_javascript_execution_scope!(let l5_ajses, l4_djses); + AssertTypeOf(l5_ajses).is::, + >, + >>(); + let d = l5_ajses.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + } + } + } + } + { + tc_scope!(let l2_tc, l1_hs); + AssertTypeOf(l2_tc).is::>>>(); + let d = l2_tc.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + { + disallow_javascript_execution_scope!(let l3_djses, l2_tc, OnFailure::CrashOnFailure); + AssertTypeOf(l3_djses).is::>>, + >>(); + let d = l3_djses.deref_mut(); + AssertTypeOf(d).is::>>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + { + allow_javascript_execution_scope!(let l4_ajses, l3_djses); + AssertTypeOf(l4_ajses).is::>>, + >, + >>(); + let d = l4_ajses.deref_mut(); + AssertTypeOf(d).is::>>, + >>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + } + } + } + { + escapable_handle_scope!(let l2_ehs, l1_hs); + AssertTypeOf(l2_ehs).is::>>(); + tc_scope!(let l3_tc, l2_ehs); + AssertTypeOf(l3_tc).is::>>>(); + let d = l3_tc.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + } + { + // `CallbackScope` is meant to be used inside V8 API callback functions + // only. It assumes that a `HandleScope` already exists on the stack, and + // that a context has been entered. Push a `ContextScope` onto the stack + // to also meet the second expectation. + let _ = ContextScope::new(l1_hs, context); + callback_scope!(unsafe l2_cbs, context); + AssertTypeOf(l2_cbs).is::>(); + let d = l2_cbs.deref_mut(); + AssertTypeOf(d).is::>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + } + { + let isolate: &mut Isolate = l1_hs.as_mut(); + callback_scope!(unsafe l2_cbs, isolate); + AssertTypeOf(l2_cbs).is::>>(); + let d = l2_cbs.deref_mut(); + AssertTypeOf(d).is::>>(); + let d = d.deref_mut(); + AssertTypeOf(d).is::(); + } + } +} diff --git a/crates/v8/src/scope/raw.rs b/crates/v8/src/scope/raw.rs new file mode 100644 index 0000000000..4e443c2704 --- /dev/null +++ b/crates/v8/src/scope/raw.rs @@ -0,0 +1,314 @@ +/// The `raw` module contains prototypes for all the `extern C` functions that +/// are used in the scope module, as well as definitions for the types they operate on. +use crate::{ + Context, Data, Function, Local, Message, OnFailure, Primitive, Value, + isolate::RealIsolate, +}; +use std::num::NonZeroUsize; +use std::{ + mem::MaybeUninit, + ptr::{self, NonNull}, +}; + +#[derive(Clone, Copy, Debug)] +#[repr(transparent)] +pub(super) struct Address(NonZeroUsize); + +#[derive(Debug)] +#[repr(transparent)] +pub(super) struct ContextScope { + pub(super) entered_context: NonNull, +} + +impl ContextScope { + pub fn new(context: Local) -> Self { + unsafe { v8__Context__Enter(&*context) }; + Self { + entered_context: context.as_non_null(), + } + } +} + +impl Drop for ContextScope { + #[inline(always)] + fn drop(&mut self) { + unsafe { v8__Context__Exit(self.entered_context.as_ptr()) }; + } +} + +#[cfg(feature = "v8_enable_v8_checks")] +pub const HANDLE_SCOPE_SIZE: usize = 4; +#[cfg(not(feature = "v8_enable_v8_checks"))] +pub const HANDLE_SCOPE_SIZE: usize = 3; + +#[repr(C)] +#[derive(Debug)] +pub(super) struct HandleScope([MaybeUninit; HANDLE_SCOPE_SIZE]); + +impl HandleScope { + /// Creates an uninitialized `HandleScope`. + /// + /// This function is marked unsafe because the caller must ensure that the + /// returned value isn't dropped before `init()` has been called. + #[inline(always)] + pub unsafe fn uninit() -> Self { + unsafe { MaybeUninit::uninit().assume_init() } + } + + /// This function is marked unsafe because `init()` must be called exactly + /// once, no more and no less, after creating a `HandleScope` value with + /// `HandleScope::uninit()`. + #[inline(always)] + pub unsafe fn init(&mut self, isolate: NonNull) { + let buf = NonNull::from(self).as_ptr().cast(); + unsafe { + v8__HandleScope__CONSTRUCT(buf, isolate.as_ptr()); + } + } +} + +impl Drop for HandleScope { + #[inline(always)] + fn drop(&mut self) { + unsafe { v8__HandleScope__DESTRUCT(self) }; + } +} + +#[repr(transparent)] +#[derive(Debug)] +pub(super) struct EscapeSlot(NonNull

); + +impl EscapeSlot { + pub fn new(isolate: NonNull) -> Self { + unsafe { + let undefined = v8__Undefined(isolate.as_ptr()) as *const _; + let local = v8__Local__New(isolate.as_ptr(), undefined); + let slot_address_ptr = local as *const Address as *mut _; + let slot_address_nn = NonNull::new_unchecked(slot_address_ptr); + Self(slot_address_nn) + } + } + + pub fn escape<'e, T>(self, value: Local<'_, T>) -> Local<'e, T> + where + for<'l> Local<'l, T>: Into>, + { + const { + assert!(size_of::() == size_of::>()); + assert!(align_of::() == align_of::>()); + } + unsafe { + let undefined = Local::::from_non_null(self.0.cast()); + debug_assert!(undefined.is_undefined()); + let value_address = *(&*value as *const T as *const Address); + ptr::write(self.0.as_ptr(), value_address); + Local::from_non_null(self.0.cast()) + } + } +} + +#[repr(C)] +#[derive(Debug)] +pub(super) struct TryCatch([MaybeUninit; 6]); + +impl TryCatch { + /// Creates an uninitialized `TryCatch`. + /// + /// This function is marked unsafe because the caller must ensure that the + /// returned value isn't dropped before `init()` has been called. + pub unsafe fn uninit() -> Self { + // SAFETY: All bit patterns are valid, since the struct is made up of MaybeUninit. + Self(unsafe { MaybeUninit::uninit().assume_init() }) + } + + /// This function is marked unsafe because `init()` must be called exactly + /// once, no more and no less, after creating a `TryCatch` value with + /// `TryCatch::uninit()`. + pub unsafe fn init(&mut self, isolate: NonNull) { + let buf = NonNull::from(self).cast(); + unsafe { + v8__TryCatch__CONSTRUCT(buf.as_ptr(), isolate.as_ptr()); + } + } +} + +impl Drop for TryCatch { + #[inline(always)] + fn drop(&mut self) { + unsafe { v8__TryCatch__DESTRUCT(self) }; + } +} + +#[repr(C)] +#[derive(Debug)] +pub(super) struct DisallowJavascriptExecutionScope([MaybeUninit; 2]); + +impl DisallowJavascriptExecutionScope { + /// Creates an uninitialized `DisallowJavascriptExecutionScope`. + /// + /// This function is marked unsafe because the caller must ensure that the + /// returned value isn't dropped before `init()` has been called. + #[inline] + pub unsafe fn uninit() -> Self { + // SAFETY: All bit patterns are valid, since the struct is made up of MaybeUninit. + Self(unsafe { MaybeUninit::uninit().assume_init() }) + } + + /// This function is marked unsafe because `init()` must be called exactly + /// once, no more and no less, after creating a + /// `DisallowJavascriptExecutionScope` value with + /// `DisallowJavascriptExecutionScope::uninit()`. + #[inline] + pub unsafe fn init( + &mut self, + isolate: NonNull, + on_failure: OnFailure, + ) { + let buf = NonNull::from(self).cast(); + unsafe { + v8__DisallowJavascriptExecutionScope__CONSTRUCT( + buf.as_ptr(), + isolate.as_ptr(), + on_failure, + ); + } + } +} + +impl Drop for DisallowJavascriptExecutionScope { + #[inline(always)] + fn drop(&mut self) { + unsafe { v8__DisallowJavascriptExecutionScope__DESTRUCT(self) }; + } +} + +#[repr(C)] +#[derive(Debug)] +pub(super) struct AllowJavascriptExecutionScope([MaybeUninit; 2]); + +impl AllowJavascriptExecutionScope { + /// Creates an uninitialized `AllowJavascriptExecutionScope`. + /// + /// This function is marked unsafe because the caller must ensure that the + /// returned value isn't dropped before `init()` has been called. + #[inline] + pub unsafe fn uninit() -> Self { + Self(unsafe { MaybeUninit::uninit().assume_init() }) + } + + /// This function is marked unsafe because `init()` must be called exactly + /// once, no more and no less, after creating an + /// `AllowJavascriptExecutionScope` value with + /// `AllowJavascriptExecutionScope::uninit()`. + #[inline] + pub unsafe fn init(&mut self, isolate: NonNull) { + unsafe { + let buf = NonNull::from(self).cast(); + v8__AllowJavascriptExecutionScope__CONSTRUCT( + buf.as_ptr(), + isolate.as_ptr(), + ); + } + } +} + +impl Drop for AllowJavascriptExecutionScope { + #[inline(always)] + fn drop(&mut self) { + unsafe { v8__AllowJavascriptExecutionScope__DESTRUCT(self) }; + } +} + +unsafe extern "C" { + pub(super) fn v8__Isolate__GetCurrent() -> *mut RealIsolate; + pub(super) fn v8__Isolate__GetCurrentContext( + isolate: *mut RealIsolate, + ) -> *const Context; + pub(super) fn v8__Isolate__GetEnteredOrMicrotaskContext( + isolate: *mut RealIsolate, + ) -> *const Context; + pub(super) fn v8__Isolate__ThrowException( + isolate: *mut RealIsolate, + exception: *const Value, + ) -> *const Value; + pub(super) fn v8__Isolate__GetDataFromSnapshotOnce( + this: *mut RealIsolate, + index: usize, + ) -> *const Data; + pub(super) fn v8__Isolate__GetCurrentHostDefinedOptions( + this: *mut RealIsolate, + ) -> *const Data; + + pub(super) fn v8__Context__Enter(this: *const Context); + pub(super) fn v8__Context__Exit(this: *const Context); + pub(super) fn v8__Context__GetDataFromSnapshotOnce( + this: *const Context, + index: usize, + ) -> *const Data; + pub(super) fn v8__Context__SetPromiseHooks( + this: *const Context, + init_hook: *const Function, + before_hook: *const Function, + after_hook: *const Function, + resolve_hook: *const Function, + ); + pub(super) fn v8__Context__SetContinuationPreservedEmbedderData( + this: *mut RealIsolate, + value: *const Value, + ); + pub(super) fn v8__Context__GetContinuationPreservedEmbedderData( + this: *mut RealIsolate, + ) -> *const Value; + + pub(super) fn v8__HandleScope__CONSTRUCT( + buf: *mut MaybeUninit, + isolate: *mut RealIsolate, + ); + pub(super) fn v8__HandleScope__DESTRUCT(this: *mut HandleScope); + + pub(super) fn v8__Local__New( + isolate: *mut RealIsolate, + other: *const Data, + ) -> *const Data; + pub(super) fn v8__Undefined(isolate: *mut RealIsolate) -> *const Primitive; + + pub(super) fn v8__TryCatch__CONSTRUCT( + buf: *mut MaybeUninit, + isolate: *mut RealIsolate, + ); + pub(super) fn v8__TryCatch__DESTRUCT(this: *mut TryCatch); + pub(super) fn v8__TryCatch__HasCaught(this: *const TryCatch) -> bool; + pub(super) fn v8__TryCatch__CanContinue(this: *const TryCatch) -> bool; + pub(super) fn v8__TryCatch__HasTerminated(this: *const TryCatch) -> bool; + pub(super) fn v8__TryCatch__IsVerbose(this: *const TryCatch) -> bool; + pub(super) fn v8__TryCatch__SetVerbose(this: *mut TryCatch, value: bool); + pub(super) fn v8__TryCatch__SetCaptureMessage( + this: *mut TryCatch, + value: bool, + ); + pub(super) fn v8__TryCatch__Reset(this: *mut TryCatch); + pub(super) fn v8__TryCatch__Exception(this: *const TryCatch) -> *const Value; + pub(super) fn v8__TryCatch__StackTrace( + this: *const TryCatch, + context: *const Context, + ) -> *const Value; + pub(super) fn v8__TryCatch__Message(this: *const TryCatch) -> *const Message; + pub(super) fn v8__TryCatch__ReThrow(this: *mut TryCatch) -> *const Value; + + pub(super) fn v8__DisallowJavascriptExecutionScope__CONSTRUCT( + buf: *mut MaybeUninit, + isolate: *mut RealIsolate, + on_failure: OnFailure, + ); + pub(super) fn v8__DisallowJavascriptExecutionScope__DESTRUCT( + this: *mut DisallowJavascriptExecutionScope, + ); + + pub(super) fn v8__AllowJavascriptExecutionScope__CONSTRUCT( + buf: *mut MaybeUninit, + isolate: *mut RealIsolate, + ); + pub(super) fn v8__AllowJavascriptExecutionScope__DESTRUCT( + this: *mut AllowJavascriptExecutionScope, + ); +} diff --git a/crates/v8/src/script.rs b/crates/v8/src/script.rs new file mode 100644 index 0000000000..2f28653bf8 --- /dev/null +++ b/crates/v8/src/script.rs @@ -0,0 +1,165 @@ +use std::mem::MaybeUninit; +use std::ptr::null; + +use crate::Context; +use crate::Data; +use crate::Local; +use crate::Script; +use crate::String; +use crate::UnboundScript; +use crate::Value; +use crate::scope::PinScope; + +/// The origin, within a file, of a script. +#[repr(C)] +#[derive(Debug)] +pub struct ScriptOrigin<'s>( + [u8; crate::binding::v8__ScriptOrigin_SIZE], + // require pointer alignment + [&'s (); 0], +); + +unsafe extern "C" { + fn v8__Script__Compile( + context: *const Context, + source: *const String, + origin: *const ScriptOrigin, + ) -> *const Script; + fn v8__Script__GetUnboundScript( + script: *const Script, + ) -> *const UnboundScript; + fn v8__Script__ScriptId(script: *const Script) -> i32; + fn v8__Script__Run( + script: *const Script, + context: *const Context, + ) -> *const Value; + + fn v8__ScriptOrigin__CONSTRUCT( + buf: *mut MaybeUninit, + resource_name: *const Value, + resource_line_offset: i32, + resource_column_offset: i32, + resource_is_shared_cross_origin: bool, + script_id: i32, + source_map_url: *const Value, + resource_is_opaque: bool, + is_wasm: bool, + is_module: bool, + host_defined_options: *const Data, + ); + fn v8__ScriptOrigin__ScriptId(origin: *const ScriptOrigin) -> i32; + fn v8__ScriptOrigin__ResourceName( + origin: *const ScriptOrigin, + ) -> *const Value; + fn v8__ScriptOrigin__SourceMapUrl( + origin: *const ScriptOrigin, + ) -> *const Value; +} + +impl Script { + /// A shorthand for ScriptCompiler::Compile(). + #[inline(always)] + pub fn compile<'s>( + scope: &PinScope<'s, '_>, + source: Local, + origin: Option<&ScriptOrigin>, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Script__Compile( + sd.get_current_context(), + &*source, + origin.map_or_else(null, |r| r as *const _), + ) + }) + } + } + + /// Returns the corresponding context-unbound script. + #[inline(always)] + pub fn get_unbound_script<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Local<'s, UnboundScript> { + unsafe { + scope + .cast_local(|_| v8__Script__GetUnboundScript(self)) + .unwrap() + } + } + + /// Returns the ID assigned to the underlying script by V8. + #[inline(always)] + pub fn script_id(&self) -> i32 { + unsafe { v8__Script__ScriptId(self) } + } + + /// Runs the script returning the resulting value. It will be run in the + /// context in which it was created (ScriptCompiler::CompileBound or + /// UnboundScript::BindToCurrentContext()). + #[inline] + pub fn run<'s>(&self, scope: &PinScope<'s, '_>) -> Option> { + unsafe { + scope.cast_local(|sd| v8__Script__Run(self, sd.get_current_context())) + } + } +} + +/// The origin, within a file, of a script. +impl<'s> ScriptOrigin<'s> { + #[allow(clippy::too_many_arguments)] + #[inline(always)] + pub fn new<'i>( + // TODO(littledivy): remove + _scope: &PinScope<'s, 'i>, + resource_name: Local<'s, Value>, + resource_line_offset: i32, + resource_column_offset: i32, + resource_is_shared_cross_origin: bool, + script_id: i32, + source_map_url: Option>, + resource_is_opaque: bool, + is_wasm: bool, + is_module: bool, + host_defined_options: Option>, + ) -> Self { + unsafe { + let mut buf = std::mem::MaybeUninit::::uninit(); + v8__ScriptOrigin__CONSTRUCT( + &mut buf, + &*resource_name, + resource_line_offset, + resource_column_offset, + resource_is_shared_cross_origin, + script_id, + source_map_url.map_or_else(null, |l| &*l as *const Value), + resource_is_opaque, + is_wasm, + is_module, + host_defined_options.map_or_else(null, |l| &*l as *const Data), + ); + buf.assume_init() + } + } + + #[inline(always)] + pub fn script_id(&self) -> i32 { + unsafe { v8__ScriptOrigin__ScriptId(self as *const _) } + } + + #[inline(always)] + pub fn resource_name(&self) -> Option> { + unsafe { + let ptr = v8__ScriptOrigin__ResourceName(self); + Local::from_raw(ptr) + } + } + + #[inline(always)] + pub fn source_map_url(&self) -> Option> { + unsafe { + let ptr = v8__ScriptOrigin__SourceMapUrl(self); + Local::from_raw(ptr) + } + } +} diff --git a/crates/v8/src/script_compiler.rs b/crates/v8/src/script_compiler.rs new file mode 100644 index 0000000000..2203a60c7c --- /dev/null +++ b/crates/v8/src/script_compiler.rs @@ -0,0 +1,429 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. +use std::{marker::PhantomData, mem::MaybeUninit}; + +use crate::Function; +use crate::Local; +use crate::Module; +use crate::Object; +use crate::ScriptOrigin; +use crate::String; +use crate::UniqueRef; +use crate::isolate::RealIsolate; +use crate::scope::GetIsolate; +use crate::scope::PinScope; +use crate::support::int; +use crate::{Context, Script, UnboundScript}; + +unsafe extern "C" { + fn v8__ScriptCompiler__Source__CONSTRUCT( + buf: *mut MaybeUninit, + source_string: *const String, + origin: *const ScriptOrigin, + cached_data: *mut CachedData, + ); + fn v8__ScriptCompiler__Source__DESTRUCT(this: *mut Source); + fn v8__ScriptCompiler__Source__GetCachedData<'a>( + this: *const Source, + ) -> *const CachedData<'a>; + fn v8__ScriptCompiler__CachedData__NEW<'a>( + data: *const u8, + length: i32, + ) -> *mut CachedData<'a>; + fn v8__ScriptCompiler__CachedData__DELETE<'a>(this: *mut CachedData<'a>); + fn v8__ScriptCompiler__CachedData__CompatibilityCheck<'a>( + this: *mut CachedData<'a>, + isolate: *mut RealIsolate, + ) -> int; + fn v8__ScriptCompiler__CompileOptionsIsValid(options: int) -> bool; + fn v8__ScriptCompiler__CompileModule( + isolate: *mut RealIsolate, + source: *mut Source, + options: CompileOptions, + no_cache_reason: NoCacheReason, + ) -> *const Module; + fn v8__ScriptCompiler__Compile( + context: *const Context, + source: *mut Source, + options: CompileOptions, + no_cache_reason: NoCacheReason, + ) -> *const Script; + fn v8__ScriptCompiler__CompileFunction( + context: *const Context, + source: *mut Source, + arguments_count: usize, + arguments: *const *const String, + context_extensions_count: usize, + context_extensions: *const *const Object, + options: CompileOptions, + no_cache_reason: NoCacheReason, + ) -> *const Function; + fn v8__ScriptCompiler__CompileUnboundScript( + isolate: *mut RealIsolate, + source: *mut Source, + options: CompileOptions, + no_cache_reason: NoCacheReason, + ) -> *const UnboundScript; + + fn v8__ScriptCompiler__CachedDataVersionTag() -> u32; +} + +/// Source code which can then be compiled to a UnboundScript or Script. +#[repr(C)] +#[derive(Debug)] +pub struct Source { + _source_string: usize, + _resource_name: usize, + _resource_line_offset: int, + _resource_column_offset: int, + _resource_options: int, + _source_map_url: usize, + _host_defined_options: usize, + _cached_data: usize, + _consume_cache_task: usize, + _compile_hint_callback: usize, + _compile_hint_callback_data: usize, + _compilation_details: [usize; 3], +} + +/// Compilation data that the embedder can cache and pass back to speed up future +/// compilations. The data is produced if the CompilerOptions passed to the compilation +/// functions in ScriptCompiler contains produce_data_to_cache = true. The data to cache +/// can then can be retrieved from UnboundScript. +#[repr(C)] +#[derive(Debug)] +pub struct CachedData<'a> { + data: *const u8, + length: i32, + rejected: bool, + buffer_policy: BufferPolicy, + _phantom: PhantomData<&'a ()>, +} + +impl Drop for CachedData<'_> { + fn drop(&mut self) { + unsafe { + v8__ScriptCompiler__CachedData__DELETE(self); + } + } +} + +impl<'a> CachedData<'a> { + pub fn new(data: &'a [u8]) -> UniqueRef { + let cached_data = unsafe { + UniqueRef::from_raw(v8__ScriptCompiler__CachedData__NEW( + data.as_ptr(), + data.len() as i32, + )) + }; + debug_assert_eq!( + cached_data.buffer_policy(), + crate::script_compiler::BufferPolicy::BufferNotOwned + ); + cached_data + } + + #[inline(always)] + pub(crate) fn buffer_policy(&self) -> BufferPolicy { + self.buffer_policy + } + + #[inline(always)] + pub fn rejected(&self) -> bool { + self.rejected + } + + /// Checks whether this cached data can be loaded in the given isolate, and + /// reports why not if it cannot. + #[inline(always)] + pub fn compatibility_check( + &mut self, + scope: &PinScope<'_, '_, ()>, + ) -> CompatibilityCheckResult { + let result = unsafe { + v8__ScriptCompiler__CachedData__CompatibilityCheck( + self, + scope.get_isolate_ptr(), + ) + }; + CompatibilityCheckResult::from_int(result) + } +} + +/// The outcome of [`CachedData::compatibility_check`]. +#[repr(C)] +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum CompatibilityCheckResult { + /// The cached data can be used. + Success = 0, + /// The data does not start with V8's code cache magic number. + MagicNumberMismatch = 1, + /// The data was produced by a different version of V8. + VersionMismatch = 2, + /// The data was produced from a different source string. + SourceMismatch = 3, + /// The data was produced with a different set of V8 flags. + FlagsMismatch = 5, + /// The data's payload checksum does not match. + ChecksumMismatch = 6, + /// The data's header is malformed. + InvalidHeader = 7, + /// The data's recorded length does not match its actual length. + LengthMismatch = 8, + /// The data was produced against a different read-only snapshot. + ReadOnlySnapshotChecksumMismatch = 9, +} + +impl CompatibilityCheckResult { + fn from_int(value: int) -> Self { + match value { + 0 => Self::Success, + 1 => Self::MagicNumberMismatch, + 2 => Self::VersionMismatch, + 3 => Self::SourceMismatch, + 5 => Self::FlagsMismatch, + 6 => Self::ChecksumMismatch, + 7 => Self::InvalidHeader, + 8 => Self::LengthMismatch, + 9 => Self::ReadOnlySnapshotChecksumMismatch, + other => panic!("unknown CompatibilityCheckResult: {other}"), + } + } +} + +impl std::ops::Deref for CachedData<'_> { + type Target = [u8]; + fn deref(&self) -> &Self::Target { + unsafe { std::slice::from_raw_parts(self.data, self.length as usize) } + } +} + +#[allow(dead_code)] +#[repr(C)] +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) enum BufferPolicy { + BufferNotOwned = 0, + BufferOwned, +} + +impl Source { + #[inline(always)] + pub fn new( + source_string: Local, + origin: Option<&ScriptOrigin>, + ) -> Self { + let mut buf = MaybeUninit::::uninit(); + unsafe { + v8__ScriptCompiler__Source__CONSTRUCT( + &mut buf, + &*source_string, + origin.map_or(std::ptr::null(), |x| x as *const _), + std::ptr::null_mut(), + ); + buf.assume_init() + } + } + + #[inline(always)] + pub fn new_with_cached_data( + source_string: Local, + origin: Option<&ScriptOrigin>, + cached_data: UniqueRef, + ) -> Self { + let mut buf = MaybeUninit::::uninit(); + unsafe { + v8__ScriptCompiler__Source__CONSTRUCT( + &mut buf, + &*source_string, + origin.map_or(std::ptr::null(), |x| x as *const _), + cached_data.into_raw(), // Source constructor takes ownership. + ); + buf.assume_init() + } + } + + #[inline(always)] + pub fn get_cached_data(&self) -> Option<&CachedData<'_>> { + unsafe { + let cached_data = v8__ScriptCompiler__Source__GetCachedData(self); + if cached_data.is_null() { + None + } else { + Some(&*cached_data) + } + } + } +} + +impl Drop for Source { + fn drop(&mut self) { + unsafe { v8__ScriptCompiler__Source__DESTRUCT(self) } + } +} + +bitflags! { + #[derive(Debug, Clone, Copy, PartialEq)] + #[repr(transparent)] + pub struct CompileOptions: int { + const NoCompileOptions = 0; + const ConsumeCodeCache = 1 << 0; + const EagerCompile = 1 << 1; + const ProduceCompileHints = 1 << 2; + const ConsumeCompileHints = 1 << 3; + const FollowCompileHintsMagicComment = 1 << 4; + const FollowCompileHintsPerFunctionMagicComment = 1 << 5; + } +} + +impl CompileOptions { + /// Returns true if this combination of compile options is one that V8 + /// accepts: producing and consuming a code cache are mutually exclusive, as + /// are producing and consuming compile hints. + #[inline(always)] + pub fn is_valid(self) -> bool { + unsafe { v8__ScriptCompiler__CompileOptionsIsValid(self.bits()) } + } +} + +/// The reason for which we are not requesting or providing a code cache. +#[repr(C)] +#[derive(Debug)] +pub enum NoCacheReason { + NoReason = 0, + BecauseCachingDisabled, + BecauseNoResource, + BecauseInlineScript, + BecauseModule, + BecauseStreamingSource, + BecauseInspector, + BecauseScriptTooSmall, + BecauseCacheTooCold, + BecauseV8Extension, + BecauseExtensionModule, + BecausePacScript, + BecauseInDocumentWrite, + BecauseResourceWithNoCacheHandler, + BecauseDeferredProduceCodeCache, +} + +/// Compile an ES module, returning a Module that encapsulates the compiled +/// code. +/// +/// Corresponds to the ParseModule abstract operation in the ECMAScript +/// specification. +#[inline(always)] +pub fn compile_module<'s>( + scope: &PinScope<'s, '_>, + source: &mut Source, +) -> Option> { + compile_module2( + scope, + source, + CompileOptions::NoCompileOptions, + NoCacheReason::NoReason, + ) +} + +/// Same as compile_module with more options. +#[inline(always)] +pub fn compile_module2<'s>( + scope: &PinScope<'s, '_>, + source: &mut Source, + options: CompileOptions, + no_cache_reason: NoCacheReason, +) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__ScriptCompiler__CompileModule( + sd.get_isolate_ptr(), + source, + options, + no_cache_reason, + ) + }) + } +} + +#[inline(always)] +pub fn compile<'s>( + scope: &PinScope<'s, '_>, + source: &mut Source, + options: CompileOptions, + no_cache_reason: NoCacheReason, +) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__ScriptCompiler__Compile( + &*sd.get_current_context(), + source, + options, + no_cache_reason, + ) + }) + } +} + +#[inline(always)] +pub fn compile_function<'s>( + scope: &PinScope<'s, '_>, + source: &mut Source, + arguments: &[Local], + context_extensions: &[Local], + options: CompileOptions, + no_cache_reason: NoCacheReason, +) -> Option> { + let arguments = Local::slice_into_raw(arguments); + let context_extensions = Local::slice_into_raw(context_extensions); + unsafe { + scope.cast_local(|sd| { + v8__ScriptCompiler__CompileFunction( + &*sd.get_current_context(), + source, + arguments.len(), + arguments.as_ptr(), + context_extensions.len(), + context_extensions.as_ptr(), + options, + no_cache_reason, + ) + }) + } +} + +#[inline(always)] +pub fn compile_unbound_script<'s>( + scope: &PinScope<'s, '_>, + source: &mut Source, + options: CompileOptions, + no_cache_reason: NoCacheReason, +) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__ScriptCompiler__CompileUnboundScript( + sd.get_isolate_ptr(), + source, + options, + no_cache_reason, + ) + }) + } +} + +/// Return a version tag for CachedData for the current V8 version & flags. +/// +/// This value is meant only for determining whether a previously generated +/// CachedData instance is still valid; the tag has no other meaing. +/// +/// Background: The data carried by CachedData may depend on the exact V8 +/// version number or current compiler flags. This means that when persisting +/// CachedData, the embedder must take care to not pass in data from another V8 +/// version, or the same version with different features enabled. +/// +/// The easiest way to do so is to clear the embedder's cache on any such +/// change. +/// +/// Alternatively, this tag can be stored alongside the cached data and compared +/// when it is being used. +#[inline(always)] +pub fn cached_data_version_tag() -> u32 { + unsafe { v8__ScriptCompiler__CachedDataVersionTag() } +} diff --git a/crates/v8/src/script_or_module.rs b/crates/v8/src/script_or_module.rs new file mode 100644 index 0000000000..48be50e09e --- /dev/null +++ b/crates/v8/src/script_or_module.rs @@ -0,0 +1,43 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. +use crate::Data; +use crate::Local; +use crate::ScriptOrModule; +use crate::Value; + +unsafe extern "C" { + fn v8__ScriptOrModule__GetResourceName( + this: *const ScriptOrModule, + ) -> *const Value; + + fn v8__ScriptOrModule__HostDefinedOptions( + this: *const ScriptOrModule, + ) -> *const Data; +} + +impl ScriptOrModule { + /// The name that was passed by the embedder as ResourceName to the + /// ScriptOrigin. This can be either a v8::String or v8::Undefined. + #[inline(always)] + pub fn get_resource_name(&self) -> Local<'_, Value> { + // Note: the C++ `v8::ScriptOrModule::GetResourceName()` does not actually + // return a local handle, but rather a handle whose lifetime is bound to + // the related `ScriptOrModule` object. + unsafe { + let ptr = v8__ScriptOrModule__GetResourceName(self); + Local::from_raw(ptr).unwrap() + } + } + + /// The options that were passed by the embedder as HostDefinedOptions to the + /// ScriptOrigin. + #[inline(always)] + pub fn host_defined_options(&self) -> Local<'_, Data> { + // Note: the C++ `v8::ScriptOrModule::HostDefinedOptions()` does not + // actually return a local handle, but rather a handle whose lifetime is + // bound to the related `ScriptOrModule` object. + unsafe { + let ptr = v8__ScriptOrModule__HostDefinedOptions(self); + Local::from_raw(ptr).unwrap() + } + } +} diff --git a/crates/v8/src/shared_array_buffer.rs b/crates/v8/src/shared_array_buffer.rs new file mode 100644 index 0000000000..123cbd491c --- /dev/null +++ b/crates/v8/src/shared_array_buffer.rs @@ -0,0 +1,330 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +use std::ffi::c_void; +use std::ptr::NonNull; + +use crate::BackingStore; +use crate::BackingStoreDeleterCallback; +use crate::BackingStoreInitializationMode; +use crate::BackingStoreOnFailureMode; +use crate::Local; +use crate::SharedArrayBuffer; +use crate::isolate::RealIsolate; +use crate::scope::GetIsolate; +use crate::scope::PinScope; +use crate::support::SharedRef; +use crate::support::UniqueRef; + +unsafe extern "C" { + fn v8__SharedArrayBuffer__New__with_byte_length( + isolate: *mut RealIsolate, + byte_length: usize, + ) -> *const SharedArrayBuffer; + fn v8__SharedArrayBuffer__New__with_backing_store( + isolate: *mut RealIsolate, + backing_store: *const SharedRef, + ) -> *const SharedArrayBuffer; + fn v8__SharedArrayBuffer__ByteLength(this: *const SharedArrayBuffer) + -> usize; + fn v8__SharedArrayBuffer__GetBackingStore( + this: *const SharedArrayBuffer, + ) -> SharedRef; + fn v8__SharedArrayBuffer__NewBackingStore__with_byte_length( + isolate: *mut RealIsolate, + byte_length: usize, + ) -> *mut BackingStore; + fn v8__SharedArrayBuffer__NewBackingStore__with_mode( + isolate: *mut RealIsolate, + byte_length: usize, + initialization_mode: BackingStoreInitializationMode, + on_failure: BackingStoreOnFailureMode, + ) -> *mut BackingStore; + fn v8__SharedArrayBuffer__Data(this: *const SharedArrayBuffer) + -> *mut c_void; + fn v8__SharedArrayBuffer__NewBackingStore__with_data( + data: *mut c_void, + byte_length: usize, + deleter: BackingStoreDeleterCallback, + deleter_data: *mut c_void, + ) -> *mut BackingStore; +} + +impl SharedArrayBuffer { + /// Create a new SharedArrayBuffer. Allocate |byte_length| bytes. + /// Allocated memory will be owned by a created SharedArrayBuffer and + /// will be deallocated when it is garbage-collected, + /// unless the object is externalized. + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_>, + byte_length: usize, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__SharedArrayBuffer__New__with_byte_length( + sd.get_isolate_ptr(), + byte_length, + ) + }) + } + } + + #[inline(always)] + pub fn with_backing_store<'s>( + scope: &PinScope<'s, '_>, + backing_store: &SharedRef, + ) -> Local<'s, SharedArrayBuffer> { + unsafe { + scope.cast_local(|sd| { + v8__SharedArrayBuffer__New__with_backing_store( + sd.get_isolate_ptr(), + backing_store, + ) + }) + } + .unwrap() + } + + /// Data length in bytes. + #[inline(always)] + pub fn byte_length(&self) -> usize { + unsafe { v8__SharedArrayBuffer__ByteLength(self) } + } + + /// More efficient shortcut for `get_backing_store().data()`. + /// The returned pointer is valid as long as the SharedArrayBuffer is alive. + #[inline(always)] + pub fn data(&self) -> Option> { + let raw_ptr = unsafe { v8__SharedArrayBuffer__Data(self) }; + NonNull::new(raw_ptr) + } + + /// Get a shared pointer to the backing store of this array buffer. This + /// pointer coordinates the lifetime management of the internal storage + /// with any live ArrayBuffers on the heap, even across isolates. The embedder + /// should not attempt to manage lifetime of the storage through other means. + #[inline(always)] + pub fn get_backing_store(&self) -> SharedRef { + unsafe { v8__SharedArrayBuffer__GetBackingStore(self) } + } + + /// Returns a new standalone BackingStore that is allocated using the array + /// buffer allocator of the isolate. The result can be later passed to + /// ArrayBuffer::New. + /// + /// If the allocator returns nullptr, then the function may cause GCs in the + /// given isolate and re-try the allocation. If GCs do not help, then the + /// function will crash with an out-of-memory error. + #[inline(always)] + pub fn new_backing_store( + scope: &PinScope<'_, '_>, + byte_length: usize, + ) -> UniqueRef { + unsafe { + UniqueRef::from_raw( + v8__SharedArrayBuffer__NewBackingStore__with_byte_length( + scope.get_isolate_ptr(), + byte_length, + ), + ) + } + } + + /// Returns a new zero-initialized standalone BackingStore that is allocated + /// using the array buffer allocator of the isolate, with control over what + /// happens when the allocation fails. + /// + /// If the allocator returns null, then the function may cause GCs in the + /// given isolate and re-try the allocation. + /// + /// If `on_failure` is [`BackingStoreOnFailureMode::OutOfMemory`] and GCs do + /// not help, then the process crashes with an out-of-memory error. If it is + /// [`BackingStoreOnFailureMode::ReturnNull`], `None` is returned instead. + #[inline(always)] + pub fn new_backing_store_with_mode( + scope: &PinScope<'_, '_>, + byte_length: usize, + on_failure: BackingStoreOnFailureMode, + ) -> Option> { + unsafe { + Self::new_backing_store_with_modes( + scope, + byte_length, + BackingStoreInitializationMode::ZeroInitialized, + on_failure, + ) + } + } + + /// Like [`SharedArrayBuffer::new_backing_store_with_mode`], but the + /// allocated memory is left uninitialized, which is faster but leaves + /// whatever the allocator handed out visible in the backing store. + /// + /// # Safety + /// + /// The returned backing store contains uninitialized memory, which its safe + /// `Deref]>` would otherwise let anyone read. The caller + /// must overwrite every byte before the backing store is read, either from + /// Rust or through a SharedArrayBuffer handed to JavaScript. Reading it + /// beforehand is undefined behaviour and may disclose unrelated process + /// memory. + #[inline(always)] + pub unsafe fn new_backing_store_uninitialized( + scope: &PinScope<'_, '_>, + byte_length: usize, + on_failure: BackingStoreOnFailureMode, + ) -> Option> { + unsafe { + Self::new_backing_store_with_modes( + scope, + byte_length, + BackingStoreInitializationMode::Uninitialized, + on_failure, + ) + } + } + + /// # Safety + /// + /// See [`SharedArrayBuffer::new_backing_store_uninitialized`]: passing + /// [`BackingStoreInitializationMode::Uninitialized`] yields a backing store + /// whose contents must be fully written before they are read or exposed. + #[inline(always)] + unsafe fn new_backing_store_with_modes( + scope: &PinScope<'_, '_>, + byte_length: usize, + initialization_mode: BackingStoreInitializationMode, + on_failure: BackingStoreOnFailureMode, + ) -> Option> { + let ptr = unsafe { + v8__SharedArrayBuffer__NewBackingStore__with_mode( + scope.get_isolate_ptr(), + byte_length, + initialization_mode, + on_failure, + ) + }; + if ptr.is_null() { + None + } else { + Some(unsafe { UniqueRef::from_raw(ptr) }) + } + } + + /// Returns a new standalone BackingStore that takes over the ownership of + /// the given buffer. + /// + /// The destructor of the BackingStore frees owned buffer memory. + /// + /// The result can be later passed to SharedArrayBuffer::New. The raw pointer + /// to the buffer must not be passed again to any V8 API function. + /// + /// Not available in Sandbox Mode, see new_backing_store_from_bytes for a potential alternative + #[inline(always)] + #[cfg(not(feature = "v8_enable_sandbox"))] + pub fn new_backing_store_from_boxed_slice( + data: Box<[u8]>, + ) -> UniqueRef { + Self::new_backing_store_from_bytes(data) + } + + /// Returns a new standalone BackingStore that takes over the ownership of + /// the given buffer. + /// + /// The destructor of the BackingStore frees owned buffer memory. + /// + /// The result can be later passed to SharedArrayBuffer::New. The raw pointer + /// to the buffer must not be passed again to any V8 API function. + /// + /// Not available in Sandbox Mode, see new_backing_store_from_bytes for a potential alternative + #[inline(always)] + #[cfg(not(feature = "v8_enable_sandbox"))] + pub fn new_backing_store_from_vec(data: Vec) -> UniqueRef { + Self::new_backing_store_from_bytes(data) + } + + /// Returns a new standalone BackingStore backed by a container that dereferences + /// to a mutable slice of bytes. The object is dereferenced once, and the resulting slice's + /// memory is used for the lifetime of the buffer. + /// + /// This method may be called with most single-ownership containers that implement `AsMut<[u8]>`, including + /// `Box<[u8]>`, and `Vec`. This will also support most other mutable bytes containers (including `bytes::BytesMut`), + /// though these buffers will need to be boxed to manage ownership of memory. + /// + /// Not available in sandbox mode. Sandbox mode requires data to be allocated + /// within the sandbox's address space. Within sandbox mode, consider the below alternatives: + /// + /// 1. consider using new_backing_store and BackingStore::data() followed by doing a std::ptr::copy to copy the data into a BackingStore. + /// 2. If you truly do have data that is allocated inside the sandbox address space, consider using the unsafe new_backing_store_from_ptr API + /// + /// ``` + /// // Vector of bytes + /// let backing_store = v8::ArrayBuffer::new_backing_store_from_bytes(vec![1, 2, 3]); + /// // Boxes slice of bytes + /// let boxed_slice: Box<[u8]> = vec![1, 2, 3].into_boxed_slice(); + /// let backing_store = v8::ArrayBuffer::new_backing_store_from_bytes(boxed_slice); + /// // BytesMut from bytes crate + /// let backing_store = v8::ArrayBuffer::new_backing_store_from_bytes(Box::new(bytes::BytesMut::new())); + /// ``` + #[cfg(not(feature = "v8_enable_sandbox"))] + #[inline(always)] + pub fn new_backing_store_from_bytes( + mut bytes: T, + ) -> UniqueRef + where + T: crate::array_buffer::sealed::Rawable, + { + let len = bytes.byte_len(); + + let (ptr, slice) = T::into_raw(bytes); + + unsafe extern "C" fn drop_rawable< + T: crate::array_buffer::sealed::Rawable, + >( + _ptr: *mut c_void, + len: usize, + data: *mut c_void, + ) { + // SAFETY: We know that data is a raw T from above + unsafe { T::drop_raw(data as _, len) } + } + + // SAFETY: We are extending the lifetime of a slice, but we're locking away the box that we + // derefed from so there's no way to get another mutable reference. + unsafe { + Self::new_backing_store_from_ptr( + slice as _, + len, + drop_rawable::, + ptr as _, + ) + } + } + + /// Returns a new standalone BackingStore backed by given ptr. + /// + /// SAFETY: This API consumes raw pointers so is inherently + /// unsafe. Usually you should use new_backing_store_from_boxed_slice. + /// + /// WARNING: Using sandbox mode has extra limitations that may cause crashes + /// or memory safety violations if this API is used incorrectly: + /// + /// 1. Sandbox mode requires data to be allocated within the sandbox's address space. + /// 2. It is very easy to cause memory safety errors when using this API with sandbox mode + #[inline(always)] + pub unsafe fn new_backing_store_from_ptr( + data_ptr: *mut c_void, + byte_length: usize, + deleter_callback: BackingStoreDeleterCallback, + deleter_data: *mut c_void, + ) -> UniqueRef { + unsafe { + UniqueRef::from_raw(v8__SharedArrayBuffer__NewBackingStore__with_data( + data_ptr, + byte_length, + deleter_callback, + deleter_data, + )) + } + } +} diff --git a/crates/v8/src/simdutf.rs b/crates/v8/src/simdutf.rs new file mode 100644 index 0000000000..122cde3ebe --- /dev/null +++ b/crates/v8/src/simdutf.rs @@ -0,0 +1,869 @@ +//! SIMD-accelerated Unicode validation, transcoding, and base64. +//! +//! These bindings expose the [simdutf](https://github.com/simdutf/simdutf) +//! library that is already bundled with V8, avoiding C++ symbol clashes +//! that occur when linking a separate simdutf crate. + +// --------------------------------------------------------------------------- +// FFI declarations +// --------------------------------------------------------------------------- + +#[repr(C)] +struct FfiResult { + error: i32, + count: usize, +} + +unsafe extern "C" { + // Validation + fn simdutf__validate_utf8(buf: *const u8, len: usize) -> bool; + fn simdutf__validate_utf8_with_errors( + buf: *const u8, + len: usize, + ) -> FfiResult; + fn simdutf__validate_ascii(buf: *const u8, len: usize) -> bool; + fn simdutf__validate_ascii_with_errors( + buf: *const u8, + len: usize, + ) -> FfiResult; + fn simdutf__validate_utf16le(buf: *const u16, len: usize) -> bool; + fn simdutf__validate_utf16le_with_errors( + buf: *const u16, + len: usize, + ) -> FfiResult; + fn simdutf__validate_utf16be(buf: *const u16, len: usize) -> bool; + fn simdutf__validate_utf16be_with_errors( + buf: *const u16, + len: usize, + ) -> FfiResult; + fn simdutf__validate_utf32(buf: *const u32, len: usize) -> bool; + fn simdutf__validate_utf32_with_errors( + buf: *const u32, + len: usize, + ) -> FfiResult; + + // Conversion: UTF-8 <-> UTF-16LE + fn simdutf__convert_utf8_to_utf16le( + input: *const u8, + length: usize, + output: *mut u16, + ) -> usize; + fn simdutf__convert_utf8_to_utf16le_with_errors( + input: *const u8, + length: usize, + output: *mut u16, + ) -> FfiResult; + fn simdutf__convert_valid_utf8_to_utf16le( + input: *const u8, + length: usize, + output: *mut u16, + ) -> usize; + fn simdutf__convert_utf16le_to_utf8( + input: *const u16, + length: usize, + output: *mut u8, + ) -> usize; + fn simdutf__convert_utf16le_to_utf8_with_errors( + input: *const u16, + length: usize, + output: *mut u8, + ) -> FfiResult; + fn simdutf__convert_valid_utf16le_to_utf8( + input: *const u16, + length: usize, + output: *mut u8, + ) -> usize; + + // Conversion: UTF-8 <-> UTF-16BE + fn simdutf__convert_utf8_to_utf16be( + input: *const u8, + length: usize, + output: *mut u16, + ) -> usize; + fn simdutf__convert_utf16be_to_utf8( + input: *const u16, + length: usize, + output: *mut u8, + ) -> usize; + + // Conversion: UTF-8 <-> Latin-1 + fn simdutf__convert_utf8_to_latin1( + input: *const u8, + length: usize, + output: *mut u8, + ) -> usize; + fn simdutf__convert_utf8_to_latin1_with_errors( + input: *const u8, + length: usize, + output: *mut u8, + ) -> FfiResult; + fn simdutf__convert_valid_utf8_to_latin1( + input: *const u8, + length: usize, + output: *mut u8, + ) -> usize; + fn simdutf__convert_latin1_to_utf8( + input: *const u8, + length: usize, + output: *mut u8, + ) -> usize; + + // Conversion: Latin-1 <-> UTF-16LE + fn simdutf__convert_latin1_to_utf16le( + input: *const u8, + length: usize, + output: *mut u16, + ) -> usize; + fn simdutf__convert_utf16le_to_latin1( + input: *const u16, + length: usize, + output: *mut u8, + ) -> usize; + + // Conversion: UTF-8 <-> UTF-32 + fn simdutf__convert_utf8_to_utf32( + input: *const u8, + length: usize, + output: *mut u32, + ) -> usize; + fn simdutf__convert_utf32_to_utf8( + input: *const u32, + length: usize, + output: *mut u8, + ) -> usize; + + // Length calculation + fn simdutf__utf8_length_from_utf16le( + input: *const u16, + length: usize, + ) -> usize; + fn simdutf__utf8_length_from_utf16be( + input: *const u16, + length: usize, + ) -> usize; + fn simdutf__utf16_length_from_utf8(input: *const u8, length: usize) -> usize; + fn simdutf__utf8_length_from_latin1(input: *const u8, length: usize) + -> usize; + fn simdutf__latin1_length_from_utf8(input: *const u8, length: usize) + -> usize; + fn simdutf__utf32_length_from_utf8(input: *const u8, length: usize) -> usize; + fn simdutf__utf8_length_from_utf32(input: *const u32, length: usize) + -> usize; + fn simdutf__utf16_length_from_utf32( + input: *const u32, + length: usize, + ) -> usize; + fn simdutf__utf32_length_from_utf16le( + input: *const u16, + length: usize, + ) -> usize; + + // Counting + fn simdutf__count_utf8(input: *const u8, length: usize) -> usize; + fn simdutf__count_utf16le(input: *const u16, length: usize) -> usize; + fn simdutf__count_utf16be(input: *const u16, length: usize) -> usize; + + // Encoding detection + fn simdutf__detect_encodings(input: *const u8, length: usize) -> i32; + + // Base64 + fn simdutf__maximal_binary_length_from_base64( + input: *const u8, + length: usize, + ) -> usize; + fn simdutf__base64_to_binary( + input: *const u8, + length: usize, + output: *mut u8, + options: u64, + last_chunk_options: u64, + ) -> FfiResult; + fn simdutf__base64_length_from_binary(length: usize, options: u64) -> usize; + fn simdutf__binary_to_base64( + input: *const u8, + length: usize, + output: *mut u8, + options: u64, + ) -> usize; +} + +// --------------------------------------------------------------------------- +// Public types +// --------------------------------------------------------------------------- + +/// Error codes returned by simdutf operations. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(i32)] +pub enum ErrorCode { + Success = 0, + HeaderBits = 1, + TooShort = 2, + TooLong = 3, + Overlong = 4, + TooLarge = 5, + Surrogate = 6, + InvalidBase64Character = 7, + Base64InputRemainder = 8, + Base64ExtraBits = 9, + OutputBufferTooSmall = 10, + Other = 11, +} + +impl ErrorCode { + fn from_i32(v: i32) -> Self { + match v { + 0 => Self::Success, + 1 => Self::HeaderBits, + 2 => Self::TooShort, + 3 => Self::TooLong, + 4 => Self::Overlong, + 5 => Self::TooLarge, + 6 => Self::Surrogate, + 7 => Self::InvalidBase64Character, + 8 => Self::Base64InputRemainder, + 9 => Self::Base64ExtraBits, + 10 => Self::OutputBufferTooSmall, + _ => Self::Other, + } + } +} + +/// Result of a simdutf operation. +/// +/// On success, `count` is the number of code units written/validated. +/// On error, `count` is the position of the error in the input. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct SimdUtfResult { + pub error: ErrorCode, + pub count: usize, +} + +impl SimdUtfResult { + fn from_ffi(r: FfiResult) -> Self { + Self { + error: ErrorCode::from_i32(r.error), + count: r.count, + } + } + + /// Returns `true` if the operation succeeded. + #[inline] + pub fn is_ok(&self) -> bool { + self.error == ErrorCode::Success + } +} + +/// Encoding types detected by [`detect_encodings`]. +/// +/// The returned value is a bitmask — multiple encodings may be possible. +pub mod encoding { + pub const UTF8: i32 = 1; + pub const UTF16_LE: i32 = 2; + pub const UTF16_BE: i32 = 4; + pub const UTF32_LE: i32 = 8; + pub const UTF32_BE: i32 = 16; + pub const LATIN1: i32 = 32; +} + +/// Base64 encoding/decoding options. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u64)] +pub enum Base64Options { + /// Standard base64 with padding. + Default = 0, + /// URL-safe base64 without padding. + Url = 1, + /// Standard base64 without padding. + DefaultNoPadding = 2, + /// URL-safe base64 with padding. + UrlWithPadding = 3, +} + +/// Last chunk handling for base64 decoding. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u64)] +pub enum LastChunkHandling { + /// Forgiving: decode partial final chunk. + Loose = 0, + /// Error on partial/unpadded last chunk. + Strict = 1, + /// Ignore partial last chunk (no error). + StopBeforePartial = 2, + /// Only decode full 4-character blocks. + OnlyFullChunks = 3, +} + +// --------------------------------------------------------------------------- +// Validation +// --------------------------------------------------------------------------- + +/// Validates that the input is valid UTF-8. +#[inline] +pub fn validate_utf8(input: &[u8]) -> bool { + unsafe { simdutf__validate_utf8(input.as_ptr(), input.len()) } +} + +/// Validates UTF-8 and returns error position on failure. +#[inline] +pub fn validate_utf8_with_errors(input: &[u8]) -> SimdUtfResult { + SimdUtfResult::from_ffi(unsafe { + simdutf__validate_utf8_with_errors(input.as_ptr(), input.len()) + }) +} + +/// Validates that the input is valid ASCII. +#[inline] +pub fn validate_ascii(input: &[u8]) -> bool { + unsafe { simdutf__validate_ascii(input.as_ptr(), input.len()) } +} + +/// Validates ASCII and returns error position on failure. +#[inline] +pub fn validate_ascii_with_errors(input: &[u8]) -> SimdUtfResult { + SimdUtfResult::from_ffi(unsafe { + simdutf__validate_ascii_with_errors(input.as_ptr(), input.len()) + }) +} + +/// Validates that the input is valid UTF-16LE. +#[inline] +pub fn validate_utf16le(input: &[u16]) -> bool { + unsafe { simdutf__validate_utf16le(input.as_ptr(), input.len()) } +} + +/// Validates UTF-16LE and returns error position on failure. +#[inline] +pub fn validate_utf16le_with_errors(input: &[u16]) -> SimdUtfResult { + SimdUtfResult::from_ffi(unsafe { + simdutf__validate_utf16le_with_errors(input.as_ptr(), input.len()) + }) +} + +/// Validates that the input is valid UTF-16BE. +#[inline] +pub fn validate_utf16be(input: &[u16]) -> bool { + unsafe { simdutf__validate_utf16be(input.as_ptr(), input.len()) } +} + +/// Validates UTF-16BE and returns error position on failure. +#[inline] +pub fn validate_utf16be_with_errors(input: &[u16]) -> SimdUtfResult { + SimdUtfResult::from_ffi(unsafe { + simdutf__validate_utf16be_with_errors(input.as_ptr(), input.len()) + }) +} + +/// Validates that the input is valid UTF-32 (as native-endian `u32`). +#[inline] +pub fn validate_utf32(input: &[u32]) -> bool { + unsafe { simdutf__validate_utf32(input.as_ptr(), input.len()) } +} + +/// Validates UTF-32 and returns error position on failure. +#[inline] +pub fn validate_utf32_with_errors(input: &[u32]) -> SimdUtfResult { + SimdUtfResult::from_ffi(unsafe { + simdutf__validate_utf32_with_errors(input.as_ptr(), input.len()) + }) +} + +// --------------------------------------------------------------------------- +// Conversion: UTF-8 <-> UTF-16LE +// --------------------------------------------------------------------------- + +/// Converts UTF-8 to UTF-16LE. Returns 0 if the input is not valid UTF-8. +/// +/// # Safety +/// +/// `output` must have at least `input.len()` elements of capacity. +/// Use [`utf16_length_from_utf8`] for an exact count. +#[inline] +pub unsafe fn convert_utf8_to_utf16le( + input: &[u8], + output: &mut [u16], +) -> usize { + unsafe { + simdutf__convert_utf8_to_utf16le( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +/// Converts UTF-8 to UTF-16LE with error reporting. +/// +/// # Safety +/// +/// `output` must have at least `input.len()` elements of capacity. +#[inline] +pub unsafe fn convert_utf8_to_utf16le_with_errors( + input: &[u8], + output: &mut [u16], +) -> SimdUtfResult { + SimdUtfResult::from_ffi(unsafe { + simdutf__convert_utf8_to_utf16le_with_errors( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + }) +} + +/// Converts valid UTF-8 to UTF-16LE. Input MUST be valid UTF-8. +/// +/// # Safety +/// +/// - The input must be valid UTF-8. +/// - `output` must have at least `input.len()` elements of capacity. +#[inline] +pub unsafe fn convert_valid_utf8_to_utf16le( + input: &[u8], + output: &mut [u16], +) -> usize { + unsafe { + simdutf__convert_valid_utf8_to_utf16le( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +/// Converts UTF-16LE to UTF-8. Returns 0 if the input is not valid UTF-16LE. +/// +/// # Safety +/// +/// `output` must have at least `input.len() * 3` bytes of capacity. +/// Use [`utf8_length_from_utf16le`] for an exact count. +#[inline] +pub unsafe fn convert_utf16le_to_utf8( + input: &[u16], + output: &mut [u8], +) -> usize { + unsafe { + simdutf__convert_utf16le_to_utf8( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +/// Converts UTF-16LE to UTF-8 with error reporting. +/// +/// # Safety +/// +/// `output` must have at least `input.len() * 3` bytes of capacity. +#[inline] +pub unsafe fn convert_utf16le_to_utf8_with_errors( + input: &[u16], + output: &mut [u8], +) -> SimdUtfResult { + SimdUtfResult::from_ffi(unsafe { + simdutf__convert_utf16le_to_utf8_with_errors( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + }) +} + +/// Converts valid UTF-16LE to UTF-8. Input MUST be valid UTF-16LE. +/// +/// # Safety +/// +/// - The input must be valid UTF-16LE. +/// - `output` must have at least `input.len() * 3` bytes of capacity. +#[inline] +pub unsafe fn convert_valid_utf16le_to_utf8( + input: &[u16], + output: &mut [u8], +) -> usize { + unsafe { + simdutf__convert_valid_utf16le_to_utf8( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +// --------------------------------------------------------------------------- +// Conversion: UTF-8 <-> UTF-16BE +// --------------------------------------------------------------------------- + +/// Converts UTF-8 to UTF-16BE. Returns 0 on invalid input. +/// +/// # Safety +/// +/// `output` must have at least `input.len()` elements of capacity. +#[inline] +pub unsafe fn convert_utf8_to_utf16be( + input: &[u8], + output: &mut [u16], +) -> usize { + unsafe { + simdutf__convert_utf8_to_utf16be( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +/// Converts UTF-16BE to UTF-8. Returns 0 on invalid input. +/// +/// # Safety +/// +/// `output` must have at least `input.len() * 3` bytes of capacity. +#[inline] +pub unsafe fn convert_utf16be_to_utf8( + input: &[u16], + output: &mut [u8], +) -> usize { + unsafe { + simdutf__convert_utf16be_to_utf8( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +// --------------------------------------------------------------------------- +// Conversion: UTF-8 <-> Latin-1 +// --------------------------------------------------------------------------- + +/// Converts UTF-8 to Latin-1. Returns 0 on invalid input or if +/// characters are outside the Latin-1 range. +/// +/// # Safety +/// +/// `output` must have at least `input.len()` bytes of capacity. +/// Use [`latin1_length_from_utf8`] for an exact count. +#[inline] +pub unsafe fn convert_utf8_to_latin1(input: &[u8], output: &mut [u8]) -> usize { + unsafe { + simdutf__convert_utf8_to_latin1( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +/// Converts UTF-8 to Latin-1 with error reporting. +/// +/// # Safety +/// +/// `output` must have at least `input.len()` bytes of capacity. +#[inline] +pub unsafe fn convert_utf8_to_latin1_with_errors( + input: &[u8], + output: &mut [u8], +) -> SimdUtfResult { + SimdUtfResult::from_ffi(unsafe { + simdutf__convert_utf8_to_latin1_with_errors( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + }) +} + +/// Converts valid UTF-8 to Latin-1. Input MUST be valid UTF-8 containing +/// only codepoints in the Latin-1 range (U+0000..U+00FF). +/// +/// # Safety +/// +/// - The input must be valid UTF-8 with only Latin-1 codepoints. +/// - `output` must have at least `input.len()` bytes of capacity. +#[inline] +pub unsafe fn convert_valid_utf8_to_latin1( + input: &[u8], + output: &mut [u8], +) -> usize { + unsafe { + simdutf__convert_valid_utf8_to_latin1( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +/// Converts Latin-1 to UTF-8. +/// +/// # Safety +/// +/// `output` must have at least `input.len() * 2` bytes of capacity. +/// Use [`utf8_length_from_latin1`] for an exact count. +#[inline] +pub unsafe fn convert_latin1_to_utf8(input: &[u8], output: &mut [u8]) -> usize { + unsafe { + simdutf__convert_latin1_to_utf8( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +// --------------------------------------------------------------------------- +// Conversion: Latin-1 <-> UTF-16LE +// --------------------------------------------------------------------------- + +/// Converts Latin-1 to UTF-16LE. +/// +/// # Safety +/// +/// `output` must have at least `input.len()` elements of capacity. +#[inline] +pub unsafe fn convert_latin1_to_utf16le( + input: &[u8], + output: &mut [u16], +) -> usize { + unsafe { + simdutf__convert_latin1_to_utf16le( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +/// Converts UTF-16LE to Latin-1. The input must only contain codepoints +/// in the Latin-1 range. +/// +/// # Safety +/// +/// `output` must have at least `input.len()` bytes of capacity. +#[inline] +pub unsafe fn convert_utf16le_to_latin1( + input: &[u16], + output: &mut [u8], +) -> usize { + unsafe { + simdutf__convert_utf16le_to_latin1( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +// --------------------------------------------------------------------------- +// Conversion: UTF-8 <-> UTF-32 +// --------------------------------------------------------------------------- + +/// Converts UTF-8 to UTF-32. Returns 0 on invalid input. +/// +/// # Safety +/// +/// `output` must have at least `input.len()` elements of capacity. +#[inline] +pub unsafe fn convert_utf8_to_utf32(input: &[u8], output: &mut [u32]) -> usize { + unsafe { + simdutf__convert_utf8_to_utf32( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +/// Converts UTF-32 to UTF-8. Returns 0 on invalid input. +/// +/// # Safety +/// +/// `output` must have at least `input.len() * 4` bytes of capacity. +#[inline] +pub unsafe fn convert_utf32_to_utf8(input: &[u32], output: &mut [u8]) -> usize { + unsafe { + simdutf__convert_utf32_to_utf8( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + ) + } +} + +// --------------------------------------------------------------------------- +// Length calculation +// --------------------------------------------------------------------------- + +/// Returns the number of UTF-8 bytes needed to encode the given UTF-16LE +/// input. +#[inline] +pub fn utf8_length_from_utf16le(input: &[u16]) -> usize { + unsafe { simdutf__utf8_length_from_utf16le(input.as_ptr(), input.len()) } +} + +/// Returns the number of UTF-8 bytes needed to encode the given UTF-16BE +/// input. +#[inline] +pub fn utf8_length_from_utf16be(input: &[u16]) -> usize { + unsafe { simdutf__utf8_length_from_utf16be(input.as_ptr(), input.len()) } +} + +/// Returns the number of UTF-16 code units needed to encode the given +/// UTF-8 input. +#[inline] +pub fn utf16_length_from_utf8(input: &[u8]) -> usize { + unsafe { simdutf__utf16_length_from_utf8(input.as_ptr(), input.len()) } +} + +/// Returns the number of UTF-8 bytes needed to encode the given Latin-1 data. +#[inline] +pub fn utf8_length_from_latin1(input: &[u8]) -> usize { + unsafe { simdutf__utf8_length_from_latin1(input.as_ptr(), input.len()) } +} + +/// Returns the number of Latin-1 bytes that the given UTF-8 input would +/// produce. The input must contain only codepoints in the Latin-1 range. +#[inline] +pub fn latin1_length_from_utf8(input: &[u8]) -> usize { + unsafe { simdutf__latin1_length_from_utf8(input.as_ptr(), input.len()) } +} + +/// Returns the number of UTF-32 code units needed for the given UTF-8 +/// input. +#[inline] +pub fn utf32_length_from_utf8(input: &[u8]) -> usize { + unsafe { simdutf__utf32_length_from_utf8(input.as_ptr(), input.len()) } +} + +/// Returns the number of UTF-8 bytes needed for the given UTF-32 input. +#[inline] +pub fn utf8_length_from_utf32(input: &[u32]) -> usize { + unsafe { simdutf__utf8_length_from_utf32(input.as_ptr(), input.len()) } +} + +/// Returns the number of UTF-16 code units needed for the given UTF-32 +/// input. +#[inline] +pub fn utf16_length_from_utf32(input: &[u32]) -> usize { + unsafe { simdutf__utf16_length_from_utf32(input.as_ptr(), input.len()) } +} + +/// Returns the number of UTF-32 code units needed for the given +/// UTF-16LE input. +#[inline] +pub fn utf32_length_from_utf16le(input: &[u16]) -> usize { + unsafe { simdutf__utf32_length_from_utf16le(input.as_ptr(), input.len()) } +} + +// --------------------------------------------------------------------------- +// Counting +// --------------------------------------------------------------------------- + +/// Counts the number of Unicode codepoints in the UTF-8 input. +/// The input must be valid UTF-8. +#[inline] +pub fn count_utf8(input: &[u8]) -> usize { + unsafe { simdutf__count_utf8(input.as_ptr(), input.len()) } +} + +/// Counts the number of Unicode codepoints in the UTF-16LE input. +/// The input must be valid UTF-16LE. +#[inline] +pub fn count_utf16le(input: &[u16]) -> usize { + unsafe { simdutf__count_utf16le(input.as_ptr(), input.len()) } +} + +/// Counts the number of Unicode codepoints in the UTF-16BE input. +/// The input must be valid UTF-16BE. +#[inline] +pub fn count_utf16be(input: &[u16]) -> usize { + unsafe { simdutf__count_utf16be(input.as_ptr(), input.len()) } +} + +// --------------------------------------------------------------------------- +// Encoding detection +// --------------------------------------------------------------------------- + +/// Detects which encodings the input could be. +/// +/// Returns a bitmask of possible encodings. Use the constants in +/// [`encoding`] to test the result: +/// +/// ```ignore +/// let mask = v8::simdutf::detect_encodings(data); +/// if mask & v8::simdutf::encoding::UTF8 != 0 { +/// // Could be UTF-8 +/// } +/// ``` +#[inline] +pub fn detect_encodings(input: &[u8]) -> i32 { + unsafe { simdutf__detect_encodings(input.as_ptr(), input.len()) } +} + +// --------------------------------------------------------------------------- +// Base64 +// --------------------------------------------------------------------------- + +/// Returns the maximum number of binary bytes that could result from +/// decoding the given base64 input. +#[inline] +pub fn maximal_binary_length_from_base64(input: &[u8]) -> usize { + unsafe { + simdutf__maximal_binary_length_from_base64(input.as_ptr(), input.len()) + } +} + +/// Decodes base64 input to binary. +/// +/// # Safety +/// +/// `output` must have at least +/// [`maximal_binary_length_from_base64`]`(input)` bytes of capacity. +#[inline] +pub unsafe fn base64_to_binary( + input: &[u8], + output: &mut [u8], + options: Base64Options, + last_chunk: LastChunkHandling, +) -> SimdUtfResult { + SimdUtfResult::from_ffi(unsafe { + simdutf__base64_to_binary( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + options as u64, + last_chunk as u64, + ) + }) +} + +/// Returns the number of base64 characters needed to encode the given +/// number of binary bytes. +#[inline] +pub fn base64_length_from_binary( + length: usize, + options: Base64Options, +) -> usize { + unsafe { simdutf__base64_length_from_binary(length, options as u64) } +} + +/// Encodes binary data to base64. +/// +/// # Safety +/// +/// `output` must have at least +/// [`base64_length_from_binary`]`(input.len(), options)` bytes of capacity. +#[inline] +pub unsafe fn binary_to_base64( + input: &[u8], + output: &mut [u8], + options: Base64Options, +) -> usize { + unsafe { + simdutf__binary_to_base64( + input.as_ptr(), + input.len(), + output.as_mut_ptr(), + options as u64, + ) + } +} diff --git a/crates/v8/src/snapshot.rs b/crates/v8/src/snapshot.rs new file mode 100644 index 0000000000..c493c65f18 --- /dev/null +++ b/crates/v8/src/snapshot.rs @@ -0,0 +1,281 @@ +use crate::Context; +use crate::Data; +use crate::Local; +use crate::OwnedIsolate; +use crate::external_references::ExternalReference; +use crate::isolate::RealIsolate; +use crate::isolate_create_params::raw; +use crate::support::char; +use crate::support::int; + +use std::borrow::Cow; +use std::mem::MaybeUninit; + +unsafe extern "C" { + fn v8__SnapshotCreator__CONSTRUCT( + buf: *mut MaybeUninit, + params: *const raw::CreateParams, + ); + fn v8__SnapshotCreator__DESTRUCT(this: *mut SnapshotCreator); + fn v8__SnapshotCreator__GetIsolate( + this: *const SnapshotCreator, + ) -> *mut RealIsolate; + fn v8__SnapshotCreator__CreateBlob( + this: *mut SnapshotCreator, + function_code_handling: FunctionCodeHandling, + ) -> RawStartupData; + fn v8__SnapshotCreator__SetDefaultContext( + this: *mut SnapshotCreator, + context: *const Context, + ); + fn v8__SnapshotCreator__AddContext( + this: *mut SnapshotCreator, + context: *const Context, + ) -> usize; + fn v8__SnapshotCreator__AddData_to_isolate( + this: *mut SnapshotCreator, + data: *const Data, + ) -> usize; + fn v8__SnapshotCreator__AddData_to_context( + this: *mut SnapshotCreator, + context: *const Context, + data: *const Data, + ) -> usize; + fn v8__StartupData__CanBeRehashed(this: *const RawStartupData) -> bool; + fn v8__StartupData__IsValid(this: *const RawStartupData) -> bool; + fn v8__StartupData__data__DELETE(this: *const char); +} + +#[repr(C)] +#[derive(Debug, Clone, Copy)] +pub(crate) struct RawStartupData { + pub(crate) data: *const char, + pub(crate) raw_size: int, +} + +#[derive(Debug, Clone)] +pub struct StartupData(StartupDataInner); + +#[derive(Debug)] +enum StartupDataInner { + Cpp(RawStartupData), + Cow(Cow<'static, [u8]>), +} + +impl StartupData { + /// Whether the data created can be rehashed and and the hash seed can be + /// recomputed when deserialized. + /// Only valid for StartupData returned by SnapshotCreator::CreateBlob(). + pub fn can_be_rehashed(&self) -> bool { + let tmp = match &self.0 { + StartupDataInner::Cpp(t) => *t, + StartupDataInner::Cow(c) => RawStartupData { + data: c.as_ptr() as _, + raw_size: c.len() as _, + }, + }; + unsafe { v8__StartupData__CanBeRehashed(&tmp) } + } + + /// Allows embedders to verify whether the data is valid for the current + /// V8 instance. + pub fn is_valid(&self) -> bool { + let tmp = match &self.0 { + StartupDataInner::Cpp(t) => *t, + StartupDataInner::Cow(c) => RawStartupData { + data: c.as_ptr() as _, + raw_size: c.len() as _, + }, + }; + unsafe { v8__StartupData__IsValid(&tmp) } + } +} + +impl std::ops::Deref for StartupData { + type Target = [u8]; + + fn deref(&self) -> &Self::Target { + match &self.0 { + StartupDataInner::Cpp(t) => unsafe { + std::slice::from_raw_parts(t.data as _, t.raw_size as _) + }, + StartupDataInner::Cow(c) => c, + } + } +} + +impl From for StartupData +where + T: Into>, +{ + fn from(value: T) -> Self { + Self(StartupDataInner::Cow(value.into())) + } +} + +impl Drop for StartupData { + fn drop(&mut self) { + if let StartupDataInner::Cpp(raw) = self.0 { + unsafe { + v8__StartupData__data__DELETE(raw.data); + } + } + } +} + +impl Clone for StartupDataInner { + fn clone(&self) -> Self { + match self { + // Cpp -> Cow to maintain unique ownership of the underlying pointer + Self::Cpp(r) => Self::Cow( + unsafe { std::slice::from_raw_parts(r.data as _, r.raw_size as _) } + .to_vec() + .into(), + ), + Self::Cow(c) => Self::Cow(c.clone()), + } + } +} + +#[repr(C)] +#[derive(Debug)] +pub enum FunctionCodeHandling { + Clear, + Keep, +} + +/// Helper class to create a snapshot data blob. +#[repr(C)] +#[derive(Debug)] +pub(crate) struct SnapshotCreator([usize; 1]); + +impl SnapshotCreator { + /// Create an isolate, and set it up for serialization. + /// The isolate is created from scratch. + #[inline(always)] + #[allow(clippy::new_ret_no_self)] + pub(crate) fn new( + external_references: Option>, + params: Option, + ) -> OwnedIsolate { + Self::new_impl(external_references, None, params) + } + + /// Create an isolate, and set it up for serialization. + /// The isolate is created from scratch. + #[inline(always)] + #[allow(clippy::new_ret_no_self)] + pub(crate) fn from_existing_snapshot( + existing_snapshot_blob: StartupData, + external_references: Option>, + params: Option, + ) -> OwnedIsolate { + Self::new_impl(external_references, Some(existing_snapshot_blob), params) + } + + /// Create and enter an isolate, and set it up for serialization. + /// The isolate is created from scratch. + #[inline(always)] + #[allow(clippy::new_ret_no_self)] + fn new_impl( + external_references: Option>, + existing_snapshot_blob: Option, + params: Option, + ) -> OwnedIsolate { + let mut snapshot_creator: MaybeUninit = MaybeUninit::uninit(); + + let mut params = params.unwrap_or_default(); + if let Some(external_refs) = external_references { + params = params.external_references(external_refs); + } + if let Some(snapshot_blob) = existing_snapshot_blob { + params = params.snapshot_blob(snapshot_blob); + } + let (raw_create_params, create_param_allocations) = params.finalize(); + + let snapshot_creator = unsafe { + v8__SnapshotCreator__CONSTRUCT(&mut snapshot_creator, &raw_create_params); + snapshot_creator.assume_init() + }; + + let isolate_ptr = + unsafe { v8__SnapshotCreator__GetIsolate(&snapshot_creator) }; + let mut owned_isolate = OwnedIsolate::new_already_entered(isolate_ptr); + owned_isolate.initialize(create_param_allocations); + owned_isolate.set_snapshot_creator(snapshot_creator); + owned_isolate + } +} + +impl Drop for SnapshotCreator { + fn drop(&mut self) { + unsafe { v8__SnapshotCreator__DESTRUCT(self) }; + } +} + +impl SnapshotCreator { + /// Set the default context to be included in the snapshot blob. + /// The snapshot will not contain the global proxy, and we expect one or a + /// global object template to create one, to be provided upon deserialization. + #[inline(always)] + pub(crate) fn set_default_context(&mut self, context: Local) { + unsafe { v8__SnapshotCreator__SetDefaultContext(self, &*context) }; + } + + /// Add additional context to be included in the snapshot blob. + /// The snapshot will include the global proxy. + /// + /// Returns the index of the context in the snapshot blob. + #[inline(always)] + pub(crate) fn add_context(&mut self, context: Local) -> usize { + unsafe { v8__SnapshotCreator__AddContext(self, &*context) } + } + + /// Attach arbitrary `v8::Data` to the isolate snapshot, which can be + /// retrieved via `HandleScope::get_context_data_from_snapshot_once()` after + /// deserialization. This data does not survive when a new snapshot is created + /// from an existing snapshot. + #[inline(always)] + pub(crate) fn add_isolate_data(&mut self, data: Local) -> usize + where + for<'l> Local<'l, T>: Into>, + { + unsafe { v8__SnapshotCreator__AddData_to_isolate(self, &*data.into()) } + } + + /// Attach arbitrary `v8::Data` to the context snapshot, which can be + /// retrieved via `HandleScope::get_context_data_from_snapshot_once()` after + /// deserialization. This data does not survive when a new snapshot is + /// created from an existing snapshot. + #[inline(always)] + pub(crate) fn add_context_data( + &mut self, + context: Local, + data: Local, + ) -> usize + where + for<'l> Local<'l, T>: Into>, + { + unsafe { + v8__SnapshotCreator__AddData_to_context(self, &*context, &*data.into()) + } + } + + /// Creates a snapshot data blob. + /// This must not be called from within a handle scope. + #[inline(always)] + pub(crate) fn create_blob( + &mut self, + function_code_handling: FunctionCodeHandling, + ) -> Option { + let blob = + unsafe { v8__SnapshotCreator__CreateBlob(self, function_code_handling) }; + if blob.data.is_null() { + debug_assert!(blob.raw_size == 0); + None + } else { + debug_assert!(blob.raw_size > 0); + Some(StartupData(StartupDataInner::Cpp(blob))) + } + } +} diff --git a/crates/v8/src/string.rs b/crates/v8/src/string.rs new file mode 100644 index 0000000000..70a0014bf4 --- /dev/null +++ b/crates/v8/src/string.rs @@ -0,0 +1,1697 @@ +use crate::Isolate; +use crate::Local; +use crate::String; +use crate::binding::v8__String__kMaxLength; +use crate::isolate::RealIsolate; +use crate::scope::PinScope; +use crate::support::Opaque; +use crate::support::char; +use crate::support::int; +use crate::support::size_t; +use std::borrow::Cow; +use std::convert::TryInto; +use std::default::Default; +use std::marker::PhantomData; +use std::mem::MaybeUninit; +use std::ptr::NonNull; +use std::slice; + +/// Converts Latin-1 encoded bytes to UTF-8, writing into the output buffer. +/// +/// The output buffer must have at least `2 * input_length` bytes of capacity, +/// since each Latin-1 byte can expand to at most 2 UTF-8 bytes. +/// +/// Returns the number of bytes written to the output buffer. +/// +/// # Safety +/// +/// - `inbuf` must point to at least `input_length` readable bytes. +/// - `outbuf` must point to at least `2 * input_length` writable bytes. +#[inline(always)] +pub unsafe fn latin1_to_utf8( + input_length: usize, + inbuf: *const u8, + outbuf: *mut u8, +) -> usize { + unsafe { + let mut output = 0; + let mut input = 0; + + // Process 8 bytes at a time: check if all are ASCII with a single AND + while input + 8 <= input_length { + let chunk = (inbuf.add(input) as *const u64).read_unaligned(); + if chunk & 0x8080_8080_8080_8080 == 0 { + // All 8 bytes are ASCII, copy in bulk + (outbuf.add(output) as *mut u64).write_unaligned(chunk); + input += 8; + output += 8; + } else { + // At least one non-ASCII byte, process individually + let end = input + 8; + while input < end { + let byte = *(inbuf.add(input)); + if byte < 0x80 { + *(outbuf.add(output)) = byte; + output += 1; + } else { + // Latin-1 byte to two-byte UTF-8 sequence + *(outbuf.add(output)) = (byte >> 6) | 0b1100_0000; + *(outbuf.add(output + 1)) = (byte & 0b0011_1111) | 0b1000_0000; + output += 2; + } + input += 1; + } + } + } + + // Handle remaining bytes + while input < input_length { + let byte = *(inbuf.add(input)); + if byte < 0x80 { + *(outbuf.add(output)) = byte; + output += 1; + } else { + *(outbuf.add(output)) = (byte >> 6) | 0b1100_0000; + *(outbuf.add(output + 1)) = (byte & 0b0011_1111) | 0b1000_0000; + output += 2; + } + input += 1; + } + output + } +} + +/// Minimum non-ASCII UTF-8 byte length before `new_from_utf8` decodes with +/// simdutf instead of V8's decoder. Below this the two potential simdutf FFI +/// calls cost more than V8 handling the tiny string itself. +const NONASCII_ENCODE_SIMD_THRESHOLD: usize = 16; + +unsafe extern "C" { + fn v8__String__Empty(isolate: *mut RealIsolate) -> *const String; + + fn v8__String__NewFromUtf8( + isolate: *mut RealIsolate, + data: *const char, + new_type: NewStringType, + length: int, + ) -> *const String; + + fn v8__String__NewFromOneByte( + isolate: *mut RealIsolate, + data: *const u8, + new_type: NewStringType, + length: int, + ) -> *const String; + + fn v8__String__NewFromTwoByte( + isolate: *mut RealIsolate, + data: *const u16, + new_type: NewStringType, + length: int, + ) -> *const String; + + fn v8__String__Concat( + isolate: *mut RealIsolate, + left: *const String, + right: *const String, + ) -> *const String; + + fn v8__String__Length(this: *const String) -> int; + + fn v8__String__Utf8Length( + this: *const String, + isolate: *mut RealIsolate, + ) -> int; + + fn v8__String__Write_v2( + this: *const String, + isolate: *mut RealIsolate, + offset: u32, + length: u32, + buffer: *mut u16, + flags: int, + ); + + fn v8__String__WriteOneByte_v2( + this: *const String, + isolate: *mut RealIsolate, + offset: u32, + length: u32, + buffer: *mut u8, + flags: int, + ); + + fn v8__String__WriteUtf8_v2( + this: *const String, + isolate: *mut RealIsolate, + buffer: *mut char, + capacity: size_t, + flags: int, + processed_characters_return: *mut size_t, + ) -> int; + + fn v8__String__GetExternalStringResource( + this: *const String, + ) -> *mut ExternalStringResource; + fn v8__String__GetExternalStringResourceBase( + this: *const String, + encoding: *mut Encoding, + ) -> *mut ExternalStringResourceBase; + + fn v8__String__NewExternalOneByteConst( + isolate: *mut RealIsolate, + onebyte_const: *const OneByteConst, + ) -> *const String; + + fn v8__String__NewExternalOneByteStatic( + isolate: *mut RealIsolate, + buffer: *const char, + length: int, + ) -> *const String; + + fn v8__String__NewExternalOneByte( + isolate: *mut RealIsolate, + buffer: *mut char, + length: size_t, + free: unsafe extern "C" fn(*mut char, size_t), + ) -> *const String; + + fn v8__String__NewExternalTwoByteStatic( + isolate: *mut RealIsolate, + buffer: *const u16, + length: int, + ) -> *const String; + + fn v8__String__NewExternalTwoByte( + isolate: *mut RealIsolate, + buffer: *mut u16, + length: size_t, + free: unsafe extern "C" fn(*mut u16, size_t), + ) -> *const String; + + #[allow(dead_code)] + fn v8__String__IsExternal(this: *const String) -> bool; + fn v8__String__IsExternalOneByte(this: *const String) -> bool; + fn v8__String__IsExternalTwoByte(this: *const String) -> bool; + #[allow(dead_code)] + fn v8__String__IsOneByte(this: *const String) -> bool; + fn v8__String__ContainsOnlyOneByte(this: *const String) -> bool; + fn v8__String__StringEquals(this: *const String, that: *const String) + -> bool; + fn v8__String__InternalizeString( + this: *const String, + isolate: *mut RealIsolate, + ) -> *const String; + fn v8__ExternalOneByteStringResource__data( + this: *const ExternalOneByteStringResource, + ) -> *const char; + fn v8__ExternalOneByteStringResource__length( + this: *const ExternalOneByteStringResource, + ) -> size_t; + + fn v8__String__ValueView__CONSTRUCT( + buf: *mut ValueView, + isolate: *mut RealIsolate, + string: *const String, + ); + fn v8__String__ValueView__DESTRUCT(this: *mut ValueView); +} + +#[derive(PartialEq, Debug)] +#[repr(C)] +pub enum Encoding { + Unknown = 0x1, + TwoByte = 0x2, + OneByte = 0x8, +} + +#[repr(C)] +pub struct ExternalStringResource(Opaque); + +#[repr(C)] +pub struct ExternalStringResourceBase(Opaque); + +#[repr(C)] +/// An external, one-byte string resource. +/// This corresponds with `v8::String::ExternalOneByteStringResource`. +/// +/// Note: The data contained in a one-byte string resource is guaranteed to be +/// Latin-1 data. It is not safe to assume that it is valid UTF-8, as Latin-1 +/// only has commonality with UTF-8 in the ASCII range and differs beyond that. +pub struct ExternalOneByteStringResource(Opaque); + +impl ExternalOneByteStringResource { + /// Returns a pointer to the data owned by this resource. + /// This pointer is valid as long as the resource is alive. + /// The data is guaranteed to be Latin-1. + #[inline] + pub fn data(&self) -> *const char { + unsafe { v8__ExternalOneByteStringResource__data(self) } + } + + /// Returns the length of the data owned by this resource. + #[inline] + pub fn length(&self) -> usize { + unsafe { v8__ExternalOneByteStringResource__length(self) } + } + + /// Returns the data owned by this resource as a string slice. + /// The data is guaranteed to be Latin-1. + #[inline] + pub fn as_bytes(&self) -> &[u8] { + let len = self.length(); + if len == 0 { + &[] + } else { + // SAFETY: We know this is Latin-1 + unsafe { std::slice::from_raw_parts(self.data().cast(), len) } + } + } +} + +/// A static ASCII string resource for usage in V8, created at build time. +#[repr(C)] +#[derive(Copy, Clone, Debug)] +pub struct OneByteConst { + vtable: *const OneByteConstNoOp, + cached_data: *const char, + length: usize, +} + +impl OneByteConst { + /// `const` function that returns this string as a string reference. + #[inline(always)] + pub const fn as_str(&self) -> &str { + if self.length == 0 { + "" + } else { + // SAFETY: We know this is ASCII and length > 0 + unsafe { + std::str::from_utf8_unchecked(std::slice::from_raw_parts( + self.cached_data as _, + self.length, + )) + } + } + } +} + +impl AsRef for OneByteConst { + #[inline(always)] + fn as_ref(&self) -> &str { + self.as_str() + } +} + +impl AsRef<[u8]> for OneByteConst { + #[inline(always)] + fn as_ref(&self) -> &[u8] { + self.as_str().as_bytes() + } +} + +impl std::ops::Deref for OneByteConst { + type Target = str; + #[inline(always)] + fn deref(&self) -> &Self::Target { + self.as_ref() + } +} + +// SAFETY: The vtable for OneByteConst is an immutable static and all +// of the included functions are thread-safe, the cached_data pointer +// is never changed and points to a static ASCII string, and the +// length is likewise never changed. Thus, it is safe to share the +// OneByteConst across threads. This means that multiple isolates +// can use the same OneByteConst statics simultaneously. +unsafe impl Sync for OneByteConst {} + +unsafe extern "C" fn one_byte_const_no_op(_this: *const OneByteConst) {} +unsafe extern "C" fn one_byte_const_is_cacheable( + _this: *const OneByteConst, +) -> bool { + true +} +unsafe extern "C" fn one_byte_const_data( + this: *const OneByteConst, +) -> *const char { + // SAFETY: Only called from C++ with a valid OneByteConst pointer. + unsafe { (*this).cached_data } +} +unsafe extern "C" fn one_byte_const_length(this: *const OneByteConst) -> usize { + // SAFETY: Only called from C++ with a valid OneByteConst pointer. + unsafe { (*this).length } +} +unsafe extern "C" fn one_byte_const_unaccount( + _this: *const OneByteConst, + _isolate: *mut RealIsolate, +) { +} +unsafe extern "C" fn one_byte_const_estimate_memory_usage( + _this: *const OneByteConst, +) -> size_t { + usize::MAX // ExternalStringResource::kDefaultMemoryEstimate +} +unsafe extern "C" fn one_byte_const_estimate_shared_memory_usage( + _this: *const OneByteConst, + _recorder: *mut (), +) { +} + +type OneByteConstNoOp = unsafe extern "C" fn(*const OneByteConst); +type OneByteConstIsCacheable = + unsafe extern "C" fn(*const OneByteConst) -> bool; +type OneByteConstData = + unsafe extern "C" fn(*const OneByteConst) -> *const char; +type OneByteConstLength = unsafe extern "C" fn(*const OneByteConst) -> usize; +type OneByteConstUnaccount = + unsafe extern "C" fn(*const OneByteConst, *mut RealIsolate); +type OneByteConstEstimateMemoryUsage = + unsafe extern "C" fn(*const OneByteConst) -> size_t; +type OneByteConstEstimateSharedMemoryUsage = + unsafe extern "C" fn(*const OneByteConst, *mut ()); + +#[repr(C)] +struct OneByteConstVtable { + #[cfg(target_family = "windows")] + // In SysV / Itanium ABI -0x10 offset of the vtable + // tells how many bytes the vtable pointer pointing to + // this vtable is offset from the base class. For + // single inheritance this is always 0. + _offset_to_top: usize, + // In Itanium ABI the -0x08 offset contains the type_info + // pointer, and in MSVC it contains the RTTI Complete Object + // Locator pointer. V8 is normally compiled with `-fno-rtti` + // meaning that this pointer is a nullptr on both + // Itanium and MSVC. + _typeinfo: *const (), + // After the metadata fields come the virtual function + // pointers. The vtable pointer in a class instance points + // to the first virtual function pointer, making this + // the 0x00 offset of the table. + // The order of the virtual function pointers is determined + // by their order of declaration in the classes. + delete1: OneByteConstNoOp, + // In SysV / Itanium ABI, a class vtable includes the + // deleting destructor and the compete object destructor. + // In MSVC, it only includes the deleting destructor. + #[cfg(not(target_family = "windows"))] + delete2: OneByteConstNoOp, + is_cacheable: OneByteConstIsCacheable, + unaccount: OneByteConstUnaccount, + estimate_memory_usage: OneByteConstEstimateMemoryUsage, + estimate_shared_memory_usage: OneByteConstEstimateSharedMemoryUsage, + dispose: OneByteConstNoOp, + lock: OneByteConstNoOp, + unlock: OneByteConstNoOp, + data: OneByteConstData, + length: OneByteConstLength, +} + +const ONE_BYTE_CONST_VTABLE: OneByteConstVtable = OneByteConstVtable { + #[cfg(target_family = "windows")] + _offset_to_top: 0, + _typeinfo: std::ptr::null(), + delete1: one_byte_const_no_op, + #[cfg(not(target_family = "windows"))] + delete2: one_byte_const_no_op, + is_cacheable: one_byte_const_is_cacheable, + unaccount: one_byte_const_unaccount, + estimate_memory_usage: one_byte_const_estimate_memory_usage, + estimate_shared_memory_usage: one_byte_const_estimate_shared_memory_usage, + dispose: one_byte_const_no_op, + lock: one_byte_const_no_op, + unlock: one_byte_const_no_op, + data: one_byte_const_data, + length: one_byte_const_length, +}; + +#[repr(C)] +#[derive(Debug, Default)] +pub enum NewStringType { + #[default] + Normal, + Internalized, +} + +bitflags! { + #[derive(Clone, Copy, Default)] + #[repr(transparent)] + pub struct WriteOptions: int { + const NO_OPTIONS = 0; + const HINT_MANY_WRITES_EXPECTED = 1; + const NO_NULL_TERMINATION = 2; + const PRESERVE_ONE_BYTE_NULL = 4; + // Used by WriteUtf8 to replace orphan surrogate code units with the + // unicode replacement character. Needs to be set to guarantee valid UTF-8 + // output. + const REPLACE_INVALID_UTF8 = 8; + } +} + +bitflags! { + #[derive(Clone, Copy, Default)] + #[repr(transparent)] + pub struct WriteFlags: int { + const kNullTerminate = + crate::binding::v8__String__WriteFlags__kNullTerminate as _; + const kReplaceInvalidUtf8 = + crate::binding::v8__String__WriteFlags__kReplaceInvalidUtf8 as _; + } +} + +impl String { + /// The maximum length (in bytes) of a buffer that a v8::String can be built + /// from. Attempting to create a v8::String from a larger buffer will result + /// in None being returned. + pub const MAX_LENGTH: usize = v8__String__kMaxLength as _; + + #[inline(always)] + pub fn empty<'s>(scope: &PinScope<'s, '_, ()>) -> Local<'s, String> { + // FIXME(bnoordhuis) v8__String__Empty() is infallible so there + // is no need to box up the result, only to unwrap it again. + unsafe { scope.cast_local(|sd| v8__String__Empty(sd.get_isolate_ptr())) } + .unwrap() + } + + /// Allocates a new string from UTF-8 data. Only returns an empty value when + /// length > kMaxLength + #[inline(always)] + pub fn new_from_utf8<'s>( + scope: &PinScope<'s, '_, ()>, + buffer: &[u8], + new_type: NewStringType, + ) -> Option> { + if buffer.is_empty() { + return Some(Self::empty(scope)); + } + // V8's `NewFromUtf8` runs a scalar UTF-8 decoder (twice: once to compute + // the width/length, once to write), which is very slow for non-ASCII. When + // simdutf is available we decode the input ourselves and hand V8 a + // pre-decoded one-byte (Latin-1) or two-byte (UTF-16) buffer — which it can + // just memcpy. + // `NewFromUtf8` rejects inputs whose *byte* length exceeds the maximum + // string length (conservatively, before decoding). Our decode paths would + // otherwise accept some of those (the decoded string is shorter), which + // would change behavior, so only take them when the byte length is in + // range and let V8 reject the rest. + if buffer.len() <= Self::MAX_LENGTH { + // Pure ASCII (the common case): the bytes are already valid one-byte + // (Latin-1) data. `onebyte_is_ascii` uses a wide simdutf scan for long + // inputs (where it beats std's SWAR `is_ascii`) and the inline scan for + // short ones — matching what the read paths already do. + if onebyte_is_ascii(buffer) { + return Self::new_from_one_byte(scope, buffer, new_type); + } + // Non-ASCII: transcode with simdutf only above a small threshold. For + // tiny strings the two potential simdutf FFI calls (Latin-1 attempt then + // UTF-16) cost more than V8's decoder, which is only slow at scale. + if buffer.len() >= NONASCII_ENCODE_SIMD_THRESHOLD { + return Self::new_from_utf8_transcode(scope, buffer, new_type); + } + } + let buffer_len = buffer.len().try_into().ok()?; + unsafe { + scope.cast_local(|sd| { + v8__String__NewFromUtf8( + sd.get_isolate_ptr(), + buffer.as_ptr() as *const char, + new_type, + buffer_len, + ) + }) + } + } + + /// Decodes non-ASCII, non-empty valid UTF-8 into one-byte (Latin-1) or + /// two-byte (UTF-16) data with simdutf and hands it to V8. Falls back to + /// V8's lossy `NewFromUtf8` when the input isn't valid UTF-8. + fn new_from_utf8_transcode<'s>( + scope: &PinScope<'s, '_, ()>, + buffer: &[u8], + new_type: NewStringType, + ) -> Option> { + { + // Try Latin-1 first (more compact). The conversion errors if any code + // point exceeds U+00FF or the input isn't valid UTF-8; a Latin-1 result + // is never longer than the UTF-8 input. + let mut latin1: Vec = Vec::with_capacity(buffer.len()); + // SAFETY: `latin1` has `buffer.len()` bytes of spare capacity, an upper + // bound on the Latin-1 length; simdutf only writes, never reads it. + let r = unsafe { + let out = + std::slice::from_raw_parts_mut(latin1.as_mut_ptr(), buffer.len()); + crate::simdutf::convert_utf8_to_latin1_with_errors(buffer, out) + }; + if r.is_ok() { + // SAFETY: simdutf wrote `r.count` valid Latin-1 bytes. + unsafe { latin1.set_len(r.count) }; + return Self::new_from_one_byte(scope, &latin1, new_type); + } + // Not Latin-1 representable (or invalid UTF-8): try UTF-16. A UTF-16 + // result is never more code units than the UTF-8 input has bytes. + let mut utf16: Vec = Vec::with_capacity(buffer.len()); + // SAFETY: `utf16` has `buffer.len()` units of spare capacity, an upper + // bound on the UTF-16 length. + let r = unsafe { + let out = + std::slice::from_raw_parts_mut(utf16.as_mut_ptr(), buffer.len()); + crate::simdutf::convert_utf8_to_utf16le_with_errors(buffer, out) + }; + if r.is_ok() { + // SAFETY: simdutf wrote `r.count` valid UTF-16 code units. + unsafe { utf16.set_len(r.count) }; + return Self::new_from_two_byte(scope, &utf16, new_type); + } + // Invalid UTF-8: fall through to V8's lossy `NewFromUtf8`. + } + let buffer_len = buffer.len().try_into().ok()?; + unsafe { + scope.cast_local(|sd| { + v8__String__NewFromUtf8( + sd.get_isolate_ptr(), + buffer.as_ptr() as *const char, + new_type, + buffer_len, + ) + }) + } + } + + /// Allocates a new string from Latin-1 data. Only returns an empty value when + /// length > kMaxLength. + #[inline(always)] + pub fn new_from_one_byte<'s>( + scope: &PinScope<'s, '_, ()>, + buffer: &[u8], + new_type: NewStringType, + ) -> Option> { + let buffer_len = buffer.len().try_into().ok()?; + unsafe { + scope.cast_local(|sd| { + v8__String__NewFromOneByte( + sd.get_isolate_ptr(), + buffer.as_ptr(), + new_type, + buffer_len, + ) + }) + } + } + + /// Allocates a new string from UTF-16 data. Only returns an empty value when + /// length > kMaxLength. + #[inline(always)] + pub fn new_from_two_byte<'s>( + scope: &PinScope<'s, '_, ()>, + buffer: &[u16], + new_type: NewStringType, + ) -> Option> { + let buffer_len = buffer.len().try_into().ok()?; + unsafe { + scope.cast_local(|sd| { + v8__String__NewFromTwoByte( + sd.get_isolate_ptr(), + buffer.as_ptr(), + new_type, + buffer_len, + ) + }) + } + } + + /// Returns the number of characters (UTF-16 code units) in this string. + #[inline(always)] + pub fn length(&self) -> usize { + unsafe { v8__String__Length(self) as usize } + } + + /// Returns the number of bytes in the UTF-8 encoded representation of this + /// string. + #[inline(always)] + pub fn utf8_length(&self, scope: &Isolate) -> usize { + crate::handle::assert_locals_in_isolate(scope); + unsafe { v8__String__Utf8Length(self, scope.as_real_ptr()) as usize } + } + + /// Writes the contents of the string to an external buffer, as 16-bit + /// (UTF-16) character codes. + #[inline(always)] + pub fn write_v2( + &self, + scope: &Isolate, + offset: u32, + buffer: &mut [u16], + flags: WriteFlags, + ) { + crate::handle::assert_locals_in_isolate(scope); + unsafe { + v8__String__Write_v2( + self, + scope.as_real_ptr(), + offset, + self.length().min(buffer.len()) as _, + buffer.as_mut_ptr(), + flags.bits(), + ) + } + } + + /// Writes the contents of the string to an external buffer, as one-byte + /// (Latin-1) characters. + #[inline(always)] + pub fn write_one_byte_v2( + &self, + scope: &Isolate, + offset: u32, + buffer: &mut [u8], + flags: WriteFlags, + ) { + crate::handle::assert_locals_in_isolate(scope); + unsafe { + v8__String__WriteOneByte_v2( + self, + scope.as_real_ptr(), + offset, + self.length().min(buffer.len()) as _, + buffer.as_mut_ptr(), + flags.bits(), + ) + } + } + + /// Writes the contents of the string to an external [`MaybeUninit`] buffer, as one-byte + /// (Latin-1) characters. + #[inline(always)] + pub fn write_one_byte_uninit_v2( + &self, + scope: &Isolate, + offset: u32, + buffer: &mut [MaybeUninit], + flags: WriteFlags, + ) { + crate::handle::assert_locals_in_isolate(scope); + unsafe { + v8__String__WriteOneByte_v2( + self, + scope.as_real_ptr(), + offset, + self.length().min(buffer.len()) as _, + buffer.as_mut_ptr() as _, + flags.bits(), + ) + } + } + + /// Writes the contents of the string to an external buffer, as UTF-8. + #[inline(always)] + pub fn write_utf8_v2( + &self, + scope: &Isolate, + buffer: &mut [u8], + flags: WriteFlags, + processed_characters_return: Option<&mut usize>, + ) -> usize { + crate::handle::assert_locals_in_isolate(scope); + unsafe { + // SAFETY: + // We assume that v8 will overwrite the buffer without de-initializing any byte in it. + // So the type casting of the buffer is safe. + + let buffer = { + let len = buffer.len(); + let data = buffer.as_mut_ptr().cast(); + slice::from_raw_parts_mut(data, len) + }; + self.write_utf8_uninit_v2( + scope, + buffer, + flags, + processed_characters_return, + ) + } + } + + /// Writes the contents of the string to an external [`MaybeUninit`] buffer, as UTF-8. + pub fn write_utf8_uninit_v2( + &self, + scope: &Isolate, + buffer: &mut [MaybeUninit], + flags: WriteFlags, + processed_characters_return: Option<&mut usize>, + ) -> usize { + crate::handle::assert_locals_in_isolate(scope); + let bytes = unsafe { + v8__String__WriteUtf8_v2( + self, + scope.as_real_ptr(), + buffer.as_mut_ptr() as _, + buffer.len(), + flags.bits(), + processed_characters_return + .map(|p| p as *mut _) + .unwrap_or(std::ptr::null_mut()), + ) + }; + bytes as usize + } + + // Convenience function not present in the original V8 API. + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_, ()>, + value: &str, + ) -> Option> { + Self::new_from_utf8(scope, value.as_ref(), NewStringType::Normal) + } + + /// Creates a new string by concatenating `left` and `right`. + /// Returns `None` if the resulting string would exceed + /// `v8::String::kMaxLength`. + #[inline(always)] + pub fn concat<'s>( + scope: &PinScope<'s, '_, ()>, + left: Local, + right: Local, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__String__Concat(sd.get_isolate_ptr(), &*left, &*right) + }) + } + } + + /// Compile-time function to create an external string resource. + /// The buffer is checked to contain only ASCII characters. + #[inline(always)] + pub const fn create_external_onebyte_const( + buffer: &'static [u8], + ) -> OneByteConst { + // Assert that the buffer contains only ASCII, and that the + // length is less or equal to (64-bit) v8::String::kMaxLength. + assert!(buffer.is_ascii() && buffer.len() <= ((1 << 29) - 24)); + OneByteConst { + vtable: &ONE_BYTE_CONST_VTABLE.delete1, + cached_data: buffer.as_ptr() as *const char, + length: buffer.len(), + } + } + + /// Compile-time function to create an external string resource which + /// skips the ASCII and length checks. + /// + /// ## Safety + /// + /// The passed in buffer must contain only ASCII data. Note that while V8 + /// allows OneByte string resources to contain Latin-1 data, the OneByteConst + /// struct does not allow it. + #[inline(always)] + pub const unsafe fn create_external_onebyte_const_unchecked( + buffer: &'static [u8], + ) -> OneByteConst { + OneByteConst { + vtable: &ONE_BYTE_CONST_VTABLE.delete1, + cached_data: buffer.as_ptr() as *const char, + length: buffer.len(), + } + } + + /// Creates a v8::String from a `&'static OneByteConst` + /// which is guaranteed to be ASCII. + /// + /// Note that OneByteConst guarantees ASCII even though V8 would allow + /// OneByte string resources to contain Latin-1. + #[inline(always)] + pub fn new_from_onebyte_const<'s>( + scope: &PinScope<'s, '_, ()>, + onebyte_const: &'static OneByteConst, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__String__NewExternalOneByteConst(sd.get_isolate_ptr(), onebyte_const) + }) + } + } + + /// Creates a v8::String from a `&'static [u8]`, + /// must be Latin-1 or ASCII, not UTF-8! + #[inline(always)] + pub fn new_external_onebyte_static<'s>( + scope: &PinScope<'s, '_, ()>, + buffer: &'static [u8], + ) -> Option> { + let buffer_len = buffer.len().try_into().ok()?; + unsafe { + scope.cast_local(|sd| { + v8__String__NewExternalOneByteStatic( + sd.get_isolate_ptr(), + buffer.as_ptr() as *const char, + buffer_len, + ) + }) + } + } + + /// Creates a `v8::String` from owned bytes. + /// The bytes must be Latin-1 or ASCII. + /// V8 will take ownership of the buffer and free it when the string is garbage collected. + #[inline(always)] + pub fn new_external_onebyte<'s>( + scope: &PinScope<'s, '_, ()>, + buffer: Box<[u8]>, + ) -> Option> { + let buffer_len = buffer.len(); + unsafe { + scope.cast_local(|sd| { + v8__String__NewExternalOneByte( + sd.get_isolate_ptr(), + Box::into_raw(buffer).cast::(), + buffer_len, + free_rust_external_onebyte, + ) + }) + } + } + + /// Creates a `v8::String` from owned bytes, length, and a custom destructor. + /// The bytes must be Latin-1 or ASCII. + /// V8 will take ownership of the buffer and free it when the string is garbage collected. + /// + /// SAFETY: `buffer` must be owned (valid for the lifetime of the string), and + /// `destructor` must be a valid function pointer that can free the buffer. + /// The destructor will be called with the buffer and length when the string is garbage collected. + #[inline(always)] + pub unsafe fn new_external_onebyte_raw<'s>( + scope: &PinScope<'s, '_, ()>, + buffer: *mut char, + buffer_len: usize, + destructor: unsafe extern "C" fn(*mut char, usize), + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__String__NewExternalOneByte( + sd.get_isolate_ptr(), + buffer, + buffer_len, + destructor, + ) + }) + } + } + + /// Creates a v8::String from a `&'static [u16]`. + #[inline(always)] + pub fn new_external_twobyte_static<'s>( + scope: &PinScope<'s, '_, ()>, + buffer: &'static [u16], + ) -> Option> { + let buffer_len = buffer.len().try_into().ok()?; + unsafe { + scope.cast_local(|sd| { + v8__String__NewExternalTwoByteStatic( + sd.get_isolate_ptr(), + buffer.as_ptr(), + buffer_len, + ) + }) + } + } + + /// Creates a `v8::String` from owned two-byte (UTF-16) data. + /// V8 will take ownership of the buffer and free it when the string + /// is garbage collected. + #[inline(always)] + pub fn new_external_twobyte<'s>( + scope: &PinScope<'s, '_, ()>, + buffer: Box<[u16]>, + ) -> Option> { + let buffer_len = buffer.len(); + unsafe { + scope.cast_local(|sd| { + v8__String__NewExternalTwoByte( + sd.get_isolate_ptr(), + Box::into_raw(buffer).cast::(), + buffer_len, + free_rust_external_twobyte, + ) + }) + } + } + + /// Creates a `v8::String` from owned two-byte (UTF-16) data, length, + /// and a custom destructor. + /// V8 will take ownership of the buffer and call the destructor when + /// the string is garbage collected. + /// + /// # Safety + /// + /// `buffer` must be owned (valid for the lifetime of the string), and + /// `destructor` must be a valid function pointer that can free the + /// buffer. The destructor will be called with the buffer and length + /// when the string is garbage collected. + #[inline(always)] + pub unsafe fn new_external_twobyte_raw<'s>( + scope: &PinScope<'s, '_, ()>, + buffer: *mut u16, + buffer_len: usize, + destructor: unsafe extern "C" fn(*mut u16, usize), + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__String__NewExternalTwoByte( + sd.get_isolate_ptr(), + buffer, + buffer_len, + destructor, + ) + }) + } + } + + /// Get the ExternalStringResource for an external string. + /// + /// Returns None if is_external() doesn't return true. + #[inline] + pub fn get_external_string_resource( + &self, + ) -> Option> { + NonNull::new(unsafe { v8__String__GetExternalStringResource(self) }) + } + + /// Get the ExternalOneByteStringResource for an external one-byte string. + /// + /// Returns None if is_external_onebyte() doesn't return true. + #[inline] + pub fn get_external_onebyte_string_resource( + &self, + ) -> Option> { + let (base, encoding) = self.get_external_string_resource_base(); + let base = base?; + if encoding != Encoding::OneByte { + return None; + } + + Some(base.cast()) + } + + /// Get the ExternalStringResourceBase for an external string. + /// Note this is just the base class, and isn't very useful on its own. + /// You'll want to downcast to one of its subclasses, for instance + /// with `get_external_onebyte_string_resource`. + pub fn get_external_string_resource_base( + &self, + ) -> (Option>, Encoding) { + let mut encoding = Encoding::Unknown; + ( + NonNull::new(unsafe { + v8__String__GetExternalStringResourceBase(self, &mut encoding) + }), + encoding, + ) + } + + /// True if string is external + #[inline(always)] + pub fn is_external(&self) -> bool { + // TODO: re-enable on next v8-release + // Right now it fallbacks to Value::IsExternal, which is incorrect + // See: https://source.chromium.org/chromium/_/chromium/v8/v8.git/+/1dd8624b524d14076160c1743f7da0b20fbe68e0 + // unsafe { v8__String__IsExternal(self) } + + // Fallback for now (though functionally identical) + self.is_external_onebyte() || self.is_external_twobyte() + } + + /// True if string is external & one-byte + /// (e.g: created with new_external_onebyte_static) + #[inline(always)] + pub fn is_external_onebyte(&self) -> bool { + unsafe { v8__String__IsExternalOneByte(self) } + } + + /// True if string is external & two-byte + /// (e.g: created with new_external_twobyte_static) + #[inline(always)] + pub fn is_external_twobyte(&self) -> bool { + unsafe { v8__String__IsExternalTwoByte(self) } + } + + /// Will return true if and only if string is known for certain to contain only one-byte data, + /// ie: Latin-1, a.k.a. ISO-8859-1 code points. Doesn't read the string so can return false + /// negatives, and a return value of false does not mean this string is not one-byte data. + /// + /// For a method that will not return false negatives at the cost of + /// potentially reading the entire string, use [`contains_only_onebyte()`]. + /// + /// [`contains_only_onebyte()`]: String::contains_only_onebyte + #[inline(always)] + pub fn is_onebyte(&self) -> bool { + unsafe { v8__String__IsOneByte(self) } + } + + /// True if the string contains only one-byte data. + /// Will read the entire string in some cases. + #[inline(always)] + pub fn contains_only_onebyte(&self) -> bool { + unsafe { v8__String__ContainsOnlyOneByte(self) } + } + + /// Returns true if the string is identical to `other`, comparing by + /// contents rather than by identity. + #[inline(always)] + pub fn string_equals(&self, other: Local) -> bool { + unsafe { v8__String__StringEquals(self, &*other) } + } + + /// Converts this string to an internalized string, which is guaranteed to + /// be unique for its contents within the isolate: two internalized strings + /// with equal contents are the same object. + #[inline(always)] + pub fn internalize_string<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + ) -> Local<'s, String> { + unsafe { + scope.cast_local(|sd| { + v8__String__InternalizeString(self, sd.get_isolate_ptr()) + }) + } + .unwrap() + } + + /// Creates a copy of a [`crate::String`] in a [`std::string::String`]. + /// Convenience function not present in the original V8 API. + /// + /// Uses [`ValueView`] internally for single-pass access to the string + /// data, with SIMD-accelerated transcoding for Latin-1 and two-byte + /// strings. + pub fn to_rust_string_lossy(&self, scope: &Isolate) -> std::string::String { + crate::handle::assert_locals_in_isolate(scope); + // No preliminary `self.length()` FFI call: the `ValueView` reports the + // length, and `data()` yields an empty slice for empty strings, which the + // ASCII arm below turns into an empty `String`. + // SAFETY: `self` is a valid V8 string reachable from a handle scope. + let view = unsafe { ValueView::new_from_ref(scope, self) }; + + match view.data() { + ValueViewData::OneByte(bytes) => onebyte_to_string(bytes), + ValueViewData::TwoByte(units) => wtf16_to_string(units), + } + } + + /// Writes the UTF-8 representation of this string into an existing + /// [`std::string::String`], reusing its allocation. + /// + /// The buffer is cleared first, then filled with the string's UTF-8 + /// contents. This avoids repeated heap allocation when converting + /// many V8 strings — callers can keep a single `String` and reuse it. + /// + /// Uses [`ValueView`] internally for single-pass access, avoiding + /// the extra `utf8_length` FFI call. + pub fn write_utf8_into( + &self, + scope: &mut Isolate, + buf: &mut std::string::String, + ) { + crate::handle::assert_locals_in_isolate(scope); + buf.clear(); + // No preliminary `self.length()` FFI call; an empty string yields an empty + // `data()` slice and leaves `buf` cleared. + // SAFETY: `self` is a valid V8 string reachable from a handle scope. + // The ValueView is dropped before we return. + let view = unsafe { ValueView::new_from_ref(scope, self) }; + + match view.data() { + ValueViewData::OneByte(bytes) => { + if onebyte_is_ascii(bytes) { + // ASCII: direct copy, already valid UTF-8. + buf.reserve(bytes.len()); + unsafe { + let vec = buf.as_mut_vec(); + std::ptr::copy_nonoverlapping( + bytes.as_ptr(), + vec.as_mut_ptr(), + bytes.len(), + ); + vec.set_len(bytes.len()); + } + } else { + // Latin-1: each byte can expand to at most 2 UTF-8 bytes. + let max_utf8_len = bytes.len() * 2; + buf.reserve(max_utf8_len); + unsafe { + let vec = buf.as_mut_vec(); + let written = + latin1_to_utf8(bytes.len(), bytes.as_ptr(), vec.as_mut_ptr()); + vec.set_len(written); + } + } + } + ValueViewData::TwoByte(units) => { + wtf16_into_string(units, buf); + } + } + } + + /// Converts a [`crate::String`] to either an owned [`std::string::String`], + /// or a borrowed [`str`], depending on whether it fits into the provided + /// buffer. + /// + /// Uses [`ValueView`] internally for direct access to the string's + /// contents, eliminating the `utf8_length` pre-scan that the previous + /// implementation required. + pub fn to_rust_cow_lossy<'a, const N: usize>( + &self, + scope: &mut Isolate, + buffer: &'a mut [MaybeUninit; N], + ) -> Cow<'a, str> { + crate::handle::assert_locals_in_isolate(scope); + // No preliminary `self.length()` FFI call; an empty string yields an empty + // `data()` slice, which the ASCII arm borrows as an empty `&str`. + // SAFETY: `self` is a valid V8 string reachable from a handle scope. + // The ValueView is dropped before we return, so the + // DisallowGarbageCollection scope it holds is properly scoped. + let view = unsafe { ValueView::new_from_ref(scope, self) }; + + match view.data() { + ValueViewData::OneByte(bytes) => { + // Fused single pass: `convert_latin1_to_utf8` transcodes Latin-1 and, + // for the common pure-ASCII case, is just a copy (`written == len`). + // This removes the separate `onebyte_is_ascii` pre-scan the previous + // code ran before the memcpy. Only taken when the worst-case 2x + // expansion fits the borrow buffer (so the convert can't overflow) and + // the string is long enough for the simdutf FFI call to pay off. + if bytes.len() >= ONEBYTE_SIMD_THRESHOLD + && bytes.len().saturating_mul(2) <= N + { + // SAFETY: `buffer` is valid for `N` writes and `N >= bytes.len() * 2` + // (guarded above), so it fits the full UTF-8 expansion. + let written = unsafe { + transcode_latin1_to_utf8(bytes, buffer.as_mut_ptr() as *mut u8, N) + }; + // SAFETY: simdutf wrote `written` valid UTF-8 bytes into `buffer`. + return unsafe { + let buf = &mut buffer[..written]; + let buf = &mut *(buf as *mut [_] as *mut [u8]); + Cow::Borrowed(std::str::from_utf8_unchecked(buf)) + }; + } + if onebyte_is_ascii(bytes) { + // ASCII: direct memcpy, no transcoding needed. + if bytes.len() <= N { + unsafe { + std::ptr::copy_nonoverlapping( + bytes.as_ptr(), + buffer.as_mut_ptr() as *mut u8, + bytes.len(), + ); + let buf = &mut buffer[..bytes.len()]; + let buf = &mut *(buf as *mut [_] as *mut [u8]); + Cow::Borrowed(std::str::from_utf8_unchecked(buf)) + } + } else { + // SAFETY: ASCII bytes are valid UTF-8. + unsafe { + Cow::Owned(std::string::String::from_utf8_unchecked( + bytes.to_vec(), + )) + } + } + } else { + latin1_to_cow_str(bytes, buffer) + } + } + ValueViewData::TwoByte(units) => wtf16_to_cow_str(units, buffer), + } + } +} + +#[inline] +pub unsafe extern "C" fn free_rust_external_onebyte(s: *mut char, len: usize) { + unsafe { + let slice = std::slice::from_raw_parts_mut(s, len); + + // Drop the slice + drop(Box::from_raw(slice)); + } +} + +#[inline] +pub unsafe extern "C" fn free_rust_external_twobyte(s: *mut u16, len: usize) { + unsafe { + let slice = std::slice::from_raw_parts_mut(s, len); + drop(Box::from_raw(slice)); + } +} + +#[derive(Debug, PartialEq)] +pub enum ValueViewData<'s> { + OneByte(&'s [u8]), + TwoByte(&'s [u16]), +} + +/// Returns a view onto a string's contents. +/// +/// WARNING: This does not copy the string's contents, and will therefore be +/// invalidated if the GC can move the string while the ValueView is alive. It +/// is therefore required that no GC or allocation can happen while there is an +/// active ValueView. This requirement may be relaxed in the future. +/// +/// V8 strings are either encoded as one-byte or two-bytes per character. +#[repr(C)] +pub struct ValueView<'s>( + [u8; crate::binding::v8__String__ValueView_SIZE], + PhantomData<&'s ()>, +); + +impl<'s> ValueView<'s> { + #[inline(always)] + pub fn new(isolate: &mut Isolate, string: Local<'s, String>) -> Self { + crate::handle::assert_locals_in_isolate(isolate); + // SAFETY: Local<'s, String> guarantees the V8 string is rooted in a + // HandleScope that lives for at least 's. Deref on Local erases the + // scope lifetime, so we recover it via pointer cast. + let string_ref: &'s String = unsafe { &*((&*string) as *const String) }; + unsafe { Self::new_from_ref(isolate, string_ref) } + } + + /// Constructs a `ValueView` from a raw string reference. + /// + /// # Safety + /// + /// The caller must ensure that `string` is a valid V8 string that + /// remains alive for at least `'s`. In practice this means the + /// string must be reachable from a handle scope that outlives the + /// returned `ValueView`. + #[inline(always)] + pub(crate) unsafe fn new_from_ref( + isolate: &Isolate, + string: &'s String, + ) -> Self { + let mut v = std::mem::MaybeUninit::uninit(); + unsafe { + v8__String__ValueView__CONSTRUCT( + v.as_mut_ptr(), + isolate.as_real_ptr(), + string, + ); + v.assume_init() + } + } + + #[inline(always)] + pub fn data(&self) -> ValueViewData<'_> { + // Read the `v8::String::ValueView` fields directly out of the byte buffer + // that `CONSTRUCT` filled, instead of crossing FFI for each one-line + // accessor thunk. The layout is fixed by the public header + // (v8/include/v8-primitive.h): + // offset 0: Local flat_str_ (1 pointer) + // offset size_of::<*>(): union { data8_; data16_ } (1 pointer) + // + size_of::<*>(): uint32_t length_ + // + size_of::(): bool is_one_byte_ + // The offsets are verified at runtime against the FFI accessors in + // `tests/test_api.rs` (`value_view_field_layout`). + const PTR: usize = std::mem::size_of::<*const u8>(); + const DATA_OFFSET: usize = PTR; + const LENGTH_OFFSET: usize = PTR + PTR; + const IS_ONE_BYTE_OFFSET: usize = PTR + PTR + std::mem::size_of::(); + unsafe { + let base = self.0.as_ptr(); + let length = + base.add(LENGTH_OFFSET).cast::().read_unaligned() as usize; + let is_one_byte = *base.add(IS_ONE_BYTE_OFFSET) != 0; + if length == 0 { + // Empty strings may carry a null `data8_`/`data16_` pointer, so return + // an empty slice with a valid (dangling) pointer rather than passing a + // possibly-null pointer to `from_raw_parts`. Still report the actual + // encoding so `data()`'s contract holds for empty two-byte strings. + return if is_one_byte { + ValueViewData::OneByte(&[]) + } else { + ValueViewData::TwoByte(&[]) + }; + } + let data = base.add(DATA_OFFSET).cast::<*const u8>().read_unaligned(); + if is_one_byte { + ValueViewData::OneByte(std::slice::from_raw_parts(data, length)) + } else { + ValueViewData::TwoByte(std::slice::from_raw_parts( + data.cast::(), + length, + )) + } + } + } + + /// Returns a zero-copy `&str` if the string is one-byte and pure ASCII. + /// + /// This is the fastest way to access a V8 string's contents as a Rust + /// `&str` — no allocation, no copy, no transcoding. Returns `None` for + /// strings that contain non-ASCII Latin-1 bytes or are two-byte encoded. + /// + /// The returned reference is valid as long as this `ValueView` is alive. + #[inline(always)] + pub fn as_str(&self) -> Option<&str> { + match self.data() { + ValueViewData::OneByte(bytes) => { + if bytes.is_ascii() { + // SAFETY: ASCII bytes are valid UTF-8. + Some(unsafe { std::str::from_utf8_unchecked(bytes) }) + } else { + None + } + } + ValueViewData::TwoByte(_) => None, + } + } + + /// Returns the string contents as a `Cow`. + /// + /// - **One-byte ASCII**: returns `Cow::Borrowed(&str)` — true zero-copy. + /// - **One-byte Latin-1** (non-ASCII): transcodes to UTF-8, returns + /// `Cow::Owned`. + /// - **Two-byte** (UTF-16/WTF-16): transcodes to UTF-8, returns + /// `Cow::Owned`, using SIMD-accelerated conversion for valid UTF-16 + /// strings above a threshold size. + #[inline(always)] + pub fn to_cow_lossy(&self) -> Cow<'_, str> { + match self.data() { + ValueViewData::OneByte(bytes) => { + if bytes.is_ascii() { + // SAFETY: ASCII bytes are valid UTF-8. + Cow::Borrowed(unsafe { std::str::from_utf8_unchecked(bytes) }) + } else { + Cow::Owned(latin1_to_string(bytes)) + } + } + ValueViewData::TwoByte(units) => Cow::Owned(wtf16_to_string(units)), + } + } +} + +// --------------------------------------------------------------------------- +// String conversion helpers. +// Hot paths dispatch to SIMD-accelerated routines in `crate::simdutf`. +// --------------------------------------------------------------------------- + +/// The minimum number of UTF-16 code units before we try the SIMD path. +/// With the single-pass `convert_utf16le_to_utf8_with_errors` conversion the +/// crossover against the scalar `decode_utf16` loop is low; measured wins start +/// around 16 units. +const WTF16_SIMD_THRESHOLD: usize = 16; + +/// Minimum one-byte string length before the simdutf `utf8_length_from_latin1` +/// path beats std's inline `is_ascii` SWAR scan (the simdutf FFI call has fixed +/// overhead that only pays off once the scan is long enough). +const ONEBYTE_SIMD_THRESHOLD: usize = 128; + +/// Transcodes Latin-1 `bytes` into the caller-provided output region, returning +/// the number of UTF-8 bytes written (the UTF-8 length of `bytes`). +/// +/// Callers write into uninitialized memory — a fresh `Vec`'s spare capacity or +/// a `MaybeUninit` borrow buffer — so the destination is passed as a raw +/// pointer + length rather than an already-initialized `&mut [u8]`. Centralizes +/// the one unsafe `simdutf` FFI call shared by the one-byte read paths. +/// +/// # Safety +/// `out_ptr` must be valid for writes of `out_len` bytes, and `out_len` must be +/// at least the UTF-8 length of `bytes` (which never exceeds `bytes.len() * 2`). +#[inline(always)] +unsafe fn transcode_latin1_to_utf8( + bytes: &[u8], + out_ptr: *mut u8, + out_len: usize, +) -> usize { + // SAFETY: the caller guarantees `out_ptr` is valid for `out_len` writes. + let out = unsafe { std::slice::from_raw_parts_mut(out_ptr, out_len) }; + // SAFETY: `out` covers the full UTF-8 expansion, so simdutf's write stays in + // bounds; it always produces valid UTF-8 from Latin-1 input. + unsafe { crate::simdutf::convert_latin1_to_utf8(bytes, out) } +} + +/// Whether one-byte string data is pure ASCII. Uses simdutf's wide SIMD scan +/// for long strings (where it beats std's SWAR `is_ascii`) and the inline +/// `is_ascii` for short ones (avoiding the simdutf FFI-call overhead). Shared +/// by the one-byte read paths that only need the ASCII/Latin-1 decision. +#[inline(always)] +fn onebyte_is_ascii(bytes: &[u8]) -> bool { + if bytes.len() >= ONEBYTE_SIMD_THRESHOLD { + // simdutf's `validate_ascii` scans the *whole* buffer even when the very + // first byte is non-ASCII, whereas std's `is_ascii` short-circuits. Do a + // cheap inline early-reject on the head first so Latin-1 / non-ASCII text + // (which typically has a high byte early) doesn't pay for a full SIMD scan + // just to be rejected. Pure ASCII passes the head and then gets simdutf's + // fast wide scan over the rest. + let head = bytes.len().min(32); + if !bytes[..head].is_ascii() { + return false; + } + // The head is already confirmed ASCII; scan only the remainder (ASCII-ness + // is per-byte, so this is equivalent to validating the whole buffer). + return crate::simdutf::validate_ascii(&bytes[head..]); + } + bytes.is_ascii() +} + +/// Converts one-byte (Latin-1) string data to an owned +/// [`std::string::String`]. +/// +/// With `simdutf`, a single `utf8_length_from_latin1` SIMD pass both detects +/// pure ASCII (result == input length) and yields the exact UTF-8 length for +/// the Latin-1 case, so an ASCII string is one SIMD scan + a memcpy and a +/// Latin-1 string is one SIMD scan + one SIMD transcode (down from the previous +/// `is_ascii` scan + separate length scan + transcode). +#[inline(always)] +fn onebyte_to_string(bytes: &[u8]) -> std::string::String { + { + // For long strings, one `utf8_length_from_latin1` SIMD pass both detects + // ASCII and sizes the Latin-1 transcode. For short strings the simdutf FFI + // call costs more than std's inline `is_ascii` SWAR loop, so keep the + // inline path there (crossover measured near ~128 bytes). + // Large strings: fuse detect+transcode into a single `convert_latin1_to_utf8` + // pass, over-allocating the 2x worst case up front. Dropping the separate + // `utf8_length_from_latin1` pre-scan is a net win only once the input is + // large enough to amortize the extra allocation; below this the exact-length + // path is cheaper (measured: fusing at ~256 bytes regresses from the 2x + // alloc, but wins clearly by a few KB). + const ONEBYTE_FUSE_THRESHOLD: usize = 4096; + if bytes.len() >= ONEBYTE_FUSE_THRESHOLD { + // `saturating_mul` mirrors the `to_rust_cow_lossy` guard; the product is + // the max UTF-8 length of Latin-1 input (2 bytes/code point). + let cap = bytes.len().saturating_mul(2); + let mut buf: Vec = Vec::with_capacity(cap); + // SAFETY: `buf` reserved `cap` bytes == max UTF-8 length of Latin-1 input; + // the transcode writes `written` <= `cap` valid UTF-8 bytes. + unsafe { + let written = transcode_latin1_to_utf8(bytes, buf.as_mut_ptr(), cap); + buf.set_len(written); + } + // TRADEOFF: the returned `String` keeps `capacity == cap == 2 * len` for + // its lifetime even though `written` can be as low as `len` (pure ASCII, + // the common case). We deliberately do NOT `shrink_to_fit` here: the + // realloc + full memcpy it would cost outweighs the single + // `utf8_length_from_latin1` pre-scan pass this fused path exists to + // avoid, erasing the win. So large one-byte strings trade up to 2x + // retained heap for the throughput gain (measured +18% ASCII at >=4 KB). + // SAFETY: simdutf produced valid UTF-8. + return unsafe { std::string::String::from_utf8_unchecked(buf) }; + } + if bytes.len() >= ONEBYTE_SIMD_THRESHOLD { + let utf8_len = crate::simdutf::utf8_length_from_latin1(bytes); + if utf8_len == bytes.len() { + // Pure ASCII: already valid UTF-8. SAFETY: ASCII is valid UTF-8. + return unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned(); + } + let mut buf: Vec = Vec::with_capacity(utf8_len); + // SAFETY: `buf` has capacity `utf8_len`, exactly what the transcode writes. + unsafe { + let written = + transcode_latin1_to_utf8(bytes, buf.as_mut_ptr(), utf8_len); + debug_assert_eq!(written, utf8_len); + buf.set_len(written); + return std::string::String::from_utf8_unchecked(buf); + } + } + } + if bytes.is_ascii() { + // SAFETY: ASCII is valid UTF-8. + unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned() + } else { + latin1_to_string(bytes) + } +} + +/// Converts Latin-1 bytes to an owned [`std::string::String`]. +#[inline(always)] +fn latin1_to_string(bytes: &[u8]) -> std::string::String { + debug_assert!(!bytes.is_ascii()); + let utf8_len = crate::simdutf::utf8_length_from_latin1(bytes); + let mut buf: Vec = Vec::with_capacity(utf8_len); + // SAFETY: `buf` has capacity `utf8_len`, exactly what the transcode writes. + unsafe { + let written = transcode_latin1_to_utf8(bytes, buf.as_mut_ptr(), utf8_len); + debug_assert_eq!(written, utf8_len); + buf.set_len(written); + std::string::String::from_utf8_unchecked(buf) + } +} + +/// Converts (potentially ill-formed) UTF-16LE / WTF-16 code units to an +/// owned [`std::string::String`], replacing unpaired surrogates with U+FFFD. +#[inline(always)] +fn wtf16_to_string(units: &[u16]) -> std::string::String { + { + // Single simdutf pass that validates *and* converts. Each UTF-16 code unit + // yields at most 3 UTF-8 bytes (surrogate pairs are 2 units -> 4 bytes), so + // `len * 3` is a safe upper bound. On a lone-surrogate error we fall + // through to the scalar WTF-16 loop below. + if units.len() >= WTF16_SIMD_THRESHOLD { + let cap = units.len() * 3; + let mut buf: Vec = Vec::with_capacity(cap); + // SAFETY: `buf` has `cap` bytes of spare capacity. + let result = unsafe { + let out = std::slice::from_raw_parts_mut(buf.as_mut_ptr(), cap); + crate::simdutf::convert_utf16le_to_utf8_with_errors(units, out) + }; + if result.is_ok() { + // SAFETY: simdutf wrote `result.count` valid UTF-8 bytes. + unsafe { + buf.set_len(result.count); + return std::string::String::from_utf8_unchecked(buf); + } + } + } + } + // Scalar fallback: handles short strings and strings with unpaired + // surrogates (WTF-16). + let mut buf = std::string::String::with_capacity(units.len() * 3); + for result in std::char::decode_utf16(units.iter().copied()) { + buf.push(result.unwrap_or('\u{FFFD}')); + } + buf +} + +/// Appends WTF-16 code units as UTF-8 into an existing string buffer. +#[inline(always)] +fn wtf16_into_string(units: &[u16], buf: &mut std::string::String) { + { + if units.len() >= WTF16_SIMD_THRESHOLD { + let cap = units.len() * 3; + buf.reserve(cap); + // SAFETY: appended bytes are valid UTF-8 (or we roll back on error). + let vec = unsafe { buf.as_mut_vec() }; + let start = vec.len(); + let result = unsafe { + let out = + std::slice::from_raw_parts_mut(vec.as_mut_ptr().add(start), cap); + crate::simdutf::convert_utf16le_to_utf8_with_errors(units, out) + }; + if result.is_ok() { + // SAFETY: simdutf wrote `result.count` valid UTF-8 bytes at `start`. + unsafe { vec.set_len(start + result.count) }; + return; + } + // Lone surrogate: `vec` len is unchanged (`start`); fall through to + // the scalar loop, which appends over the untouched spare capacity. + } + } + // Scalar fallback. + buf.reserve(units.len() * 3); + for result in std::char::decode_utf16(units.iter().copied()) { + buf.push(result.unwrap_or('\u{FFFD}')); + } +} + +/// Converts Latin-1 bytes to a `Cow`, borrowing from `buffer` when +/// the transcoded result fits. +#[inline(always)] +fn latin1_to_cow_str<'a, const N: usize>( + bytes: &[u8], + buffer: &'a mut [MaybeUninit; N], +) -> Cow<'a, str> { + let utf8_len = crate::simdutf::utf8_length_from_latin1(bytes); + + if utf8_len <= N { + // SAFETY: `buffer` is valid for `N >= utf8_len` writes (guarded above). + let written = unsafe { + transcode_latin1_to_utf8(bytes, buffer.as_mut_ptr() as *mut u8, utf8_len) + }; + + unsafe { + let buf = &mut buffer[..written]; + let buf = &mut *(buf as *mut [_] as *mut [u8]); + Cow::Borrowed(std::str::from_utf8_unchecked(buf)) + } + } else { + Cow::Owned(latin1_to_string(bytes)) + } +} + +/// Converts WTF-16 code units to a `Cow`, borrowing from `buffer` +/// when the transcoded result fits. +#[inline(always)] +fn wtf16_to_cow_str<'a, const N: usize>( + units: &[u16], + buffer: &'a mut [MaybeUninit; N], +) -> Cow<'a, str> { + { + if units.len() >= WTF16_SIMD_THRESHOLD { + // Each unit is at most 3 UTF-8 bytes, so if `len * 3` fits the stack + // buffer the single-pass conversion is guaranteed to fit; borrow it. + if units.len() * 3 <= N { + let result = unsafe { + let out = + std::slice::from_raw_parts_mut(buffer.as_mut_ptr() as *mut u8, N); + crate::simdutf::convert_utf16le_to_utf8_with_errors(units, out) + }; + if result.is_ok() { + return unsafe { + let buf = &mut buffer[..result.count]; + let buf = &mut *(buf as *mut [_] as *mut [u8]); + Cow::Borrowed(std::str::from_utf8_unchecked(buf)) + }; + } + // Lone surrogate: fall through to the scalar path below. + } else { + // The worst case may not fit the stack buffer — allocate. + return Cow::Owned(wtf16_to_string(units)); + } + } + } + + // Scalar fallback: try to fit into the buffer, otherwise allocate. + let mut pos = 0; + let mut tmp = [0u8; 4]; + let mut all_fit = true; + for result in std::char::decode_utf16(units.iter().copied()) { + let c = result.unwrap_or('\u{FFFD}'); + let encoded = c.encode_utf8(&mut tmp); + if pos + encoded.len() > N { + all_fit = false; + break; + } + unsafe { + std::ptr::copy_nonoverlapping( + encoded.as_ptr(), + (buffer.as_mut_ptr() as *mut u8).add(pos), + encoded.len(), + ); + } + pos += encoded.len(); + } + if all_fit { + unsafe { + let buf = &mut buffer[..pos]; + let buf = &mut *(buf as *mut [_] as *mut [u8]); + Cow::Borrowed(std::str::from_utf8_unchecked(buf)) + } + } else { + Cow::Owned(std::string::String::from_utf16_lossy(units)) + } +} + +impl Drop for ValueView<'_> { + fn drop(&mut self) { + unsafe { v8__String__ValueView__DESTRUCT(self) } + } +} diff --git a/crates/v8/src/support.h b/crates/v8/src/support.h new file mode 100644 index 0000000000..c1b9cc2dc1 --- /dev/null +++ b/crates/v8/src/support.h @@ -0,0 +1,209 @@ +#ifndef SUPPORT_H_ +#define SUPPORT_H_ + +#include +#include +#include +#include +#include +#include +#include + +#include "cppgc/name-provider.h" +#include "v8-cppgc.h" +#include "v8.h" + +// Check assumptions made in binding code. +static_assert(sizeof(bool) == sizeof(uint8_t), ""); +static_assert(sizeof(std::unique_ptr) == sizeof(void*), ""); + +namespace support { + +template +struct aligned_storage { + struct type { + alignas(Align) unsigned char data[Len]; + }; +}; + +template +using uninit_t = typename aligned_storage::type; + +template +class construct_in_place_helper { + public: + construct_in_place_helper(uninit_t* buf, Args... args) + : inner_(std::forward(args)...) {} + + private: + T inner_; +}; + +template +void construct_in_place(uninit_t* buf, Args... args) { + new (buf) + construct_in_place_helper(buf, std::forward(args)...); +} + +// Rust's FFI only supports returning normal C data structures (AKA plain old +// data). There are some situations in the V8 API where functions return non-POD +// data. We use make_pod to carefully adjust the return values so they can be +// passed into Rust. +// +// The destructor of V is never called. +// P is not allowed to have a destructor. +template +struct make_pod { + template + inline make_pod(V&& value) : pod_(helper(std::move(value))) {} + template + inline make_pod(const V& value) : pod_(helper(value)) {} + inline operator P() { return pod_; } + + private: + P pod_; + + // This helper exists to avoid calling the destructor. + // Using a union is a C++ trick to achieve this. + template + union helper { + static_assert(std::is_trivial

::value && + std::is_standard_layout

::value, + "type P must a pod type"); + static_assert(sizeof(V) == sizeof(P), "type P must be same size as type V"); + static_assert(alignof(V) == alignof(P), + "alignment of type P must be compatible with that of type V"); + + inline helper(V&& value) : value_(std::move(value)) {} + inline helper(const V& value) : value_(value) {} + inline ~helper() {} + + inline operator P() { + // Do a memcpy here avoid undefined behavior. + P result; + memcpy(&result, this, sizeof result); + return result; + } + + private: + V value_; + }; +}; + +// The C-ABI compatible equivalent of V8's Maybe. +enum class MaybeBool { JustFalse = 0, JustTrue = 1, Nothing = 2 }; + +inline static MaybeBool maybe_to_maybe_bool(v8::Maybe maybe) { + if (maybe.IsNothing()) { + return MaybeBool::Nothing; + } else if (maybe.FromJust()) { + return MaybeBool::JustTrue; + } else { + return MaybeBool::JustFalse; + } +} + +inline static v8::Maybe maybe_bool_to_maybe(MaybeBool maybe) { + switch (maybe) { + case MaybeBool::JustTrue: + case MaybeBool::JustFalse: + return v8::Just(maybe == MaybeBool::JustTrue); + default: + return v8::Nothing(); + } +} + +template +inline static T* local_to_ptr(v8::Local local) { + return *local; +} + +template +inline static const v8::Local ptr_to_local(const T* ptr) { + static_assert(sizeof(v8::Local) == sizeof(T*), ""); + auto local = *reinterpret_cast*>(&ptr); + assert(*local == ptr); + return local; +} + +template +inline static v8::Local* const_ptr_array_to_local_array( + const T* const ptr_array[]) { + static_assert(sizeof(v8::Local[42]) == sizeof(T* [42]), ""); + auto mut_ptr_array = const_cast(ptr_array); + auto mut_local_array = reinterpret_cast*>(mut_ptr_array); + return mut_local_array; +} + +template +inline static T* maybe_local_to_ptr(v8::MaybeLocal local) { + return *local.FromMaybe(v8::Local()); +} + +template +inline static const v8::MaybeLocal ptr_to_maybe_local(const T* ptr) { + static_assert(sizeof(v8::MaybeLocal) == sizeof(T*), ""); + return *reinterpret_cast*>(&ptr); +} + +template +inline static v8::Global ptr_to_global(const T* ptr) { + v8::Global global; + std::swap(ptr, *reinterpret_cast(&global)); + return global; +} + +// Because, for some reason, Clang complains that `std::array` is an +// incomplete type, incompatible with C linkage. +struct two_pointers_t { + void* a; + void* b; +}; + +struct three_pointers_t { + void* a; + void* b; + void* c; +}; + +struct function_callback_info_parts_t { + void* isolate; + void* return_value; + const void* data; + int length; +}; + +} // namespace support + +struct memory_span_t { + uint8_t* data; + size_t size; +}; + +struct const_memory_span_t { + const uint8_t* data; + size_t size; +}; + +#define EACH_TYPED_ARRAY(V) \ + V(Uint8Array) \ + V(Uint8ClampedArray) \ + V(Int8Array) \ + V(Uint16Array) \ + V(Int16Array) \ + V(Uint32Array) \ + V(Int32Array) \ + V(Float16Array) \ + V(Float32Array) \ + V(Float64Array) \ + V(BigUint64Array) \ + V(BigInt64Array) + +#endif // SUPPORT_H_ + +class RustObj : public v8::Object::Wrappable { + public: + ~RustObj(); + void Trace(cppgc::Visitor* visitor) const; + const char* GetHumanReadableName() const final; +}; diff --git a/crates/v8/src/support.rs b/crates/v8/src/support.rs new file mode 100644 index 0000000000..a28572695e --- /dev/null +++ b/crates/v8/src/support.rs @@ -0,0 +1,759 @@ +use std::any::type_name; +use std::borrow::Borrow; +use std::borrow::BorrowMut; +use std::convert::AsMut; +use std::convert::AsRef; +use std::convert::TryFrom; +use std::fmt::Debug; +use std::marker::PhantomData; +use std::mem::align_of; +use std::mem::forget; +use std::mem::size_of; +use std::mem::take; +use std::ops::Deref; +use std::ops::DerefMut; +use std::ptr::NonNull; +use std::ptr::drop_in_place; +use std::ptr::null_mut; +use std::thread::yield_now; +use std::time::Duration; +use std::time::Instant; + +// TODO use libc::intptr_t when stable. +// https://doc.rust-lang.org/1.7.0/libc/type.intptr_t.html +#[allow(non_camel_case_types)] +pub type intptr_t = isize; + +// TODO use libc::size_t when stable. +// https://doc.rust-lang.org/1.7.0/libc/type.size_t.html +#[allow(non_camel_case_types)] +pub type size_t = usize; + +pub use std::os::raw::c_char as char; +pub use std::os::raw::c_int as int; +pub use std::os::raw::c_long as long; + +pub type Opaque = [u8; 0]; + +/// Pointer to object allocated on the C++ heap. The pointer may be null. +#[repr(transparent)] +#[derive(Debug)] +pub struct UniquePtr(Option>); + +impl UniquePtr { + pub fn is_null(&self) -> bool { + self.0.is_none() + } + + pub fn as_ref(&self) -> Option<&UniqueRef> { + self.0.as_ref() + } + + pub fn as_mut(&mut self) -> Option<&mut UniqueRef> { + self.0.as_mut() + } + + pub fn take(&mut self) -> Option> { + take(&mut self.0) + } + + pub fn unwrap(self) -> UniqueRef { + self.0.unwrap() + } +} + +impl UniquePtr { + pub unsafe fn from_raw(ptr: *mut T) -> Self { + assert_unique_ptr_layout_compatible::(); + Self(unsafe { UniqueRef::try_from_raw(ptr) }) + } + + pub fn into_raw(self) -> *mut T { + self + .0 + .map_or_else(null_mut, |unique_ref| unique_ref.into_raw()) + } +} + +impl UniquePtr { + pub fn make_shared(self) -> SharedPtr { + self.into() + } +} + +impl Default for UniquePtr { + fn default() -> Self { + assert_unique_ptr_layout_compatible::(); + Self(None) + } +} + +impl From> for UniquePtr { + fn from(unique_ref: UniqueRef) -> Self { + assert_unique_ptr_layout_compatible::(); + Self(Some(unique_ref)) + } +} + +/// Pointer to object allocated on the C++ heap. The pointer may not be null. +#[repr(transparent)] +#[derive(Debug)] +pub struct UniqueRef(NonNull); + +impl UniqueRef { + pub(crate) unsafe fn try_from_raw(ptr: *mut T) -> Option { + assert_unique_ptr_layout_compatible::(); + NonNull::new(ptr).map(Self) + } + + pub(crate) unsafe fn from_raw(ptr: *mut T) -> Self { + assert_unique_ptr_layout_compatible::(); + unsafe { Self::try_from_raw(ptr).unwrap() } + } + + pub fn into_raw(self) -> *mut T { + let ptr = self.0.as_ptr(); + forget(self); + ptr + } + + pub(crate) fn as_ptr(&self) -> *mut T { + self.0.as_ptr() + } +} + +impl UniqueRef { + pub fn make_shared(self) -> SharedRef { + self.into() + } +} + +impl Drop for UniqueRef { + fn drop(&mut self) { + unsafe { drop_in_place(self.0.as_ptr()) } + } +} + +impl Deref for UniqueRef { + type Target = T; + fn deref(&self) -> &Self::Target { + unsafe { self.0.as_ref() } + } +} + +impl DerefMut for UniqueRef { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { self.0.as_mut() } + } +} + +impl AsRef for UniqueRef { + fn as_ref(&self) -> &T { + self + } +} + +impl AsMut for UniqueRef { + fn as_mut(&mut self) -> &mut T { + self + } +} + +impl Borrow for UniqueRef { + fn borrow(&self) -> &T { + self + } +} + +impl BorrowMut for UniqueRef { + fn borrow_mut(&mut self) -> &mut T { + self + } +} + +fn assert_unique_ptr_layout_compatible() { + // Assert that `U` (a `UniqueRef` or `UniquePtr`) has the same memory layout + // as a raw C pointer. + assert_eq!(size_of::(), size_of::<*mut T>()); + assert_eq!(align_of::(), align_of::<*mut T>()); +} + +pub trait Shared +where + Self: Sized, +{ + fn clone(shared_ptr: &SharedPtrBase) -> SharedPtrBase; + fn from_unique_ptr(unique_ptr: UniquePtr) -> SharedPtrBase; + fn get(shared_ptr: &SharedPtrBase) -> *const Self; + fn reset(shared_ptr: &mut SharedPtrBase); + fn use_count(shared_ptr: &SharedPtrBase) -> long; +} + +/// Private base type which is shared by the `SharedPtr` and `SharedRef` +/// implementations. +#[repr(C)] +#[derive(Eq, Debug, PartialEq)] +pub struct SharedPtrBase([usize; 2], PhantomData); + +unsafe impl Send for SharedPtrBase {} +unsafe impl Sync for SharedPtrBase {} + +impl Default for SharedPtrBase { + fn default() -> Self { + Self([0usize; 2], PhantomData) + } +} + +impl Drop for SharedPtrBase { + fn drop(&mut self) { + ::reset(self); + } +} + +/// Wrapper around a C++ shared_ptr. A shared_ptr may be be null. +#[repr(C)] +#[derive(Debug)] +pub struct SharedPtr(SharedPtrBase); + +impl SharedPtr { + /// Asserts that the number of references to the shared inner value is equal + /// to the `expected` count. + /// + /// This function relies on the C++ method `std::shared_ptr::use_count()`, + /// which usually performs a relaxed load. This function will repeatedly call + /// `use_count()` until it returns the expected value, for up to one second. + /// Therefore it should probably not be used in performance critical code. + #[track_caller] + pub fn assert_use_count_eq(&self, expected: usize) { + assert_shared_ptr_use_count_eq("SharedPtr", &self.0, expected); + } + + pub fn is_null(&self) -> bool { + ::get(&self.0).is_null() + } + + pub fn take(&mut self) -> Option> { + if self.is_null() { + None + } else { + let base = take(&mut self.0); + Some(SharedRef(base)) + } + } + + pub fn unwrap(self) -> SharedRef { + assert!(!self.is_null()); + SharedRef(self.0) + } +} + +impl Clone for SharedPtr { + fn clone(&self) -> Self { + Self(::clone(&self.0)) + } +} + +impl Default for SharedPtr { + fn default() -> Self { + Self(Default::default()) + } +} + +impl From for SharedPtr +where + T: Shared, + U: Into>, +{ + fn from(unique_ptr: U) -> Self { + let unique_ptr = unique_ptr.into(); + Self(::from_unique_ptr(unique_ptr)) + } +} + +impl From> for SharedPtr { + fn from(mut shared_ref: SharedRef) -> Self { + Self(take(&mut shared_ref.0)) + } +} + +/// Wrapper around a C++ shared_ptr. The shared_ptr is assumed to contain a +/// value and may not be null. +#[repr(C)] +#[derive(Debug)] +pub struct SharedRef(SharedPtrBase); + +impl SharedRef { + /// Asserts that the number of references to the shared inner value is equal + /// to the `expected` count. + /// + /// This function relies on the C++ method `std::shared_ptr::use_count()`, + /// which usually performs a relaxed load. This function will repeatedly call + /// `use_count()` until it returns the expected value, for up to one second. + /// Therefore it should probably not be used in performance critical code. + #[track_caller] + pub fn assert_use_count_eq(&self, expected: usize) { + assert_shared_ptr_use_count_eq("SharedRef", &self.0, expected); + } +} + +impl Clone for SharedRef { + fn clone(&self) -> Self { + Self(::clone(&self.0)) + } +} + +impl From> for SharedRef { + fn from(unique_ref: UniqueRef) -> Self { + SharedPtr::from(unique_ref).unwrap() + } +} + +impl Deref for SharedRef { + type Target = T; + fn deref(&self) -> &Self::Target { + unsafe { &*(::get(&self.0)) } + } +} + +impl AsRef for SharedRef { + fn as_ref(&self) -> &T { + self + } +} + +impl Borrow for SharedRef { + fn borrow(&self) -> &T { + self + } +} + +#[track_caller] +fn assert_shared_ptr_use_count_eq( + wrapper_type_name: &str, + shared_ptr: &SharedPtrBase, + expected: usize, +) { + let mut actual = T::use_count(shared_ptr); + let ok = match long::try_from(expected) { + Err(_) => false, // Non-`long` value can never match actual use count. + Ok(expected) if actual == expected => true, // Fast path. + Ok(expected) => { + pub const RETRY_TIMEOUT: Duration = Duration::from_secs(1); + let start = Instant::now(); + loop { + yield_now(); + actual = T::use_count(shared_ptr); + if actual == expected { + break true; + } else if start.elapsed() > RETRY_TIMEOUT { + break false; + } + } + } + }; + assert!( + ok, + "assertion failed: `{wrapper_type_name}<{}>` reference count does not match expectation\ + \n actual: {actual}\ + \n expected: {expected}", + type_name::(), + ); +} + +#[repr(C)] +#[derive(Debug, PartialEq, Eq)] +pub enum MaybeBool { + JustFalse = 0, + JustTrue = 1, + Nothing = 2, +} + +impl From for Option { + fn from(b: MaybeBool) -> Self { + match b { + MaybeBool::JustFalse => Some(false), + MaybeBool::JustTrue => Some(true), + MaybeBool::Nothing => None, + } + } +} + +impl From> for MaybeBool { + fn from(option: Option) -> Self { + match option { + Some(false) => MaybeBool::JustFalse, + Some(true) => MaybeBool::JustTrue, + None => MaybeBool::Nothing, + } + } +} + +#[derive(Copy, Clone, Debug)] +#[repr(transparent)] +pub struct CxxVTable(pub *const Opaque); + +#[allow(unused)] +#[derive(Copy, Clone, Debug)] +pub struct RustVTable(pub *const Opaque, pub PhantomData); + +#[derive(Debug)] +pub struct FieldOffset(usize, PhantomData); + +unsafe impl Send for FieldOffset where F: Send {} +unsafe impl Sync for FieldOffset where F: Sync {} + +impl Copy for FieldOffset {} + +impl Clone for FieldOffset { + fn clone(&self) -> Self { + *self + } +} + +impl FieldOffset { + pub fn from_ptrs(embedder_ptr: *const E, field_ptr: *const F) -> Self { + let embedder_addr = embedder_ptr as usize; + let field_addr = field_ptr as usize; + assert!(field_addr >= embedder_addr); + assert!((field_addr + size_of::()) <= (embedder_addr + size_of::())); + Self(field_addr - embedder_addr, PhantomData) + } + + #[allow(clippy::wrong_self_convention)] + pub unsafe fn to_embedder(self, field: &F) -> &E { + unsafe { + (((field as *const _ as usize) - self.0) as *const E) + .as_ref() + .unwrap() + } + } +} + +#[repr(C)] +#[derive(Debug, Default)] +pub struct Maybe { + has_value: bool, + value: T, +} + +impl From> for Option { + fn from(maybe: Maybe) -> Self { + if maybe.has_value { + Some(maybe.value) + } else { + None + } + } +} + +pub trait UnitType +where + Self: Copy + Sized, +{ + #[inline(always)] + fn get() -> Self { + UnitValue::::get() + } +} + +impl UnitType for T where T: Copy + Sized {} + +#[derive(Copy, Clone, Debug)] +struct UnitValue(PhantomData) +where + Self: Sized; + +impl UnitValue +where + Self: Copy + Sized, +{ + const SELF: Self = Self::new_checked(); + + const fn new_checked() -> Self { + // Statically assert that T is indeed a unit type. + let size_must_be_0 = size_of::(); + let s = Self(PhantomData::); + [s][size_must_be_0] + } + + #[inline(always)] + fn get_checked(self) -> T { + // This run-time check serves just as a backup for the compile-time + // check when Self::SELF is initialized. + assert_eq!(size_of::(), 0); + unsafe { std::mem::MaybeUninit::::zeroed().assume_init() } + } + + #[inline(always)] + pub fn get() -> T { + // Accessing the Self::SELF is necessary to make the compile-time type check + // work. + Self::SELF.get_checked() + } +} + +#[derive(Debug)] +pub struct DefaultTag; + +#[derive(Debug)] +pub struct IdenticalConversionTag; + +pub trait MapFnFrom +where + F: UnitType, + Self: Sized, +{ + fn mapping() -> Self; + + #[inline(always)] + fn map_fn_from(_: F) -> Self { + Self::mapping() + } +} + +impl MapFnFrom for F +where + Self: UnitType, +{ + #[inline(always)] + fn mapping() -> Self { + Self::get() + } +} + +pub trait MapFnTo +where + Self: UnitType, + T: Sized, +{ + fn mapping() -> T; + + #[inline(always)] + fn map_fn_to(self) -> T { + Self::mapping() + } +} + +impl MapFnTo for F +where + Self: UnitType, + T: MapFnFrom, +{ + #[inline(always)] + fn mapping() -> T { + T::map_fn_from(F::get()) + } +} + +pub trait CFnFrom +where + Self: Sized, + F: UnitType, +{ + fn mapping() -> Self; + + #[inline(always)] + fn c_fn_from(_: F) -> Self { + Self::mapping() + } +} + +macro_rules! impl_c_fn_from { + ($($arg:ident: $ty:ident),*) => { + impl CFnFrom for unsafe extern "C" fn($($ty),*) -> R + where + F: UnitType + Fn($($ty),*) -> R, + { + #[inline(always)] + fn mapping() -> Self { + unsafe extern "C" fn c_fn($($arg: $ty),*) -> R + where + F: UnitType + Fn($($ty),*) -> R, + { + (F::get())($($arg),*) + } + c_fn:: + } + } + }; +} + +impl_c_fn_from!(); +impl_c_fn_from!(a0: A0); +impl_c_fn_from!(a0: A0, a1: A1); +impl_c_fn_from!(a0: A0, a1: A1, a2: A2); +impl_c_fn_from!(a0: A0, a1: A1, a2: A2, a3: A3); +impl_c_fn_from!(a0: A0, a1: A1, a2: A2, a3: A3, a4: A4); +impl_c_fn_from!(a0: A0, a1: A1, a2: A2, a3: A3, a4: A4, a5: A5); +impl_c_fn_from!(a0: A0, a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6); + +pub trait ToCFn +where + Self: UnitType, + T: Sized, +{ + fn mapping() -> T; + + #[inline(always)] + fn to_c_fn(self) -> T { + Self::mapping() + } +} + +impl ToCFn for F +where + Self: UnitType, + T: CFnFrom, +{ + #[inline(always)] + fn mapping() -> T { + T::c_fn_from(F::get()) + } +} + +macro_rules! assert_layout_subset { + ($subset: ty, $superset: ty { $($field: ident),* $(,)? }) => { + const _: () = { + if !(std::mem::size_of::<$subset>() < std::mem::size_of::<$superset>()) { + panic!(concat!( + "assertion failed: ", + "size of `", + stringify!($subset), + "` is greater than `", + stringify!($superset), + "`" + )); + } + if !(std::mem::align_of::<$subset>() == std::mem::align_of::<$superset>()) { + panic!(concat!( + "assertion failed: `", + stringify!($subset), + "` and `", + stringify!($superset), + "` have different alignments" + )); + } + $( + if std::mem::offset_of!($subset, $field) != std::mem::offset_of!($superset, $field) { + panic!(concat!( + "assertion failed: `", + stringify!($subset), + "` and `", + stringify!($superset), + "` have different offsets for field `", + stringify!($field), + "`" + )); + } + )* + }; + }; +} + +pub(crate) use assert_layout_subset; + +#[cfg(test)] +mod tests { + use super::*; + use std::ptr::null; + + #[derive(Eq, PartialEq)] + struct MockSharedObj { + pub inner: u32, + } + + impl MockSharedObj { + const INSTANCE_A: Self = Self { inner: 11111 }; + const INSTANCE_B: Self = Self { inner: 22222 }; + + const SHARED_PTR_BASE_A: SharedPtrBase = + SharedPtrBase([1, 1], PhantomData); + const SHARED_PTR_BASE_B: SharedPtrBase = + SharedPtrBase([2, 2], PhantomData); + } + + impl Shared for MockSharedObj { + fn clone(_: &SharedPtrBase) -> SharedPtrBase { + unimplemented!() + } + + fn from_unique_ptr(_: UniquePtr) -> SharedPtrBase { + unimplemented!() + } + + fn get(p: &SharedPtrBase) -> *const Self { + match p { + &Self::SHARED_PTR_BASE_A => &Self::INSTANCE_A, + &Self::SHARED_PTR_BASE_B => &Self::INSTANCE_B, + p if p == &Default::default() => null(), + _ => unreachable!(), + } + } + + fn reset(p: &mut SharedPtrBase) { + forget(take(p)); + } + + fn use_count(p: &SharedPtrBase) -> long { + match p { + &Self::SHARED_PTR_BASE_A => 1, + &Self::SHARED_PTR_BASE_B => 2, + p if p == &Default::default() => 0, + _ => unreachable!(), + } + } + } + + #[test] + fn shared_ptr_and_shared_ref() { + let mut shared_ptr_a1 = SharedPtr(MockSharedObj::SHARED_PTR_BASE_A); + assert!(!shared_ptr_a1.is_null()); + shared_ptr_a1.assert_use_count_eq(1); + + let shared_ref_a: SharedRef<_> = shared_ptr_a1.take().unwrap(); + assert_eq!(shared_ref_a.inner, 11111); + shared_ref_a.assert_use_count_eq(1); + + assert!(shared_ptr_a1.is_null()); + shared_ptr_a1.assert_use_count_eq(0); + + let shared_ptr_a2: SharedPtr<_> = shared_ref_a.into(); + assert!(!shared_ptr_a2.is_null()); + shared_ptr_a2.assert_use_count_eq(1); + assert_eq!(shared_ptr_a2.unwrap().inner, 11111); + + let mut shared_ptr_b1 = SharedPtr(MockSharedObj::SHARED_PTR_BASE_B); + assert!(!shared_ptr_b1.is_null()); + shared_ptr_b1.assert_use_count_eq(2); + + let shared_ref_b: SharedRef<_> = shared_ptr_b1.take().unwrap(); + assert_eq!(shared_ref_b.inner, 22222); + shared_ref_b.assert_use_count_eq(2); + + assert!(shared_ptr_b1.is_null()); + shared_ptr_b1.assert_use_count_eq(0); + + let shared_ptr_b2: SharedPtr<_> = shared_ref_b.into(); + assert!(!shared_ptr_b2.is_null()); + shared_ptr_b2.assert_use_count_eq(2); + assert_eq!(shared_ptr_b2.unwrap().inner, 22222); + } + + #[test] + #[should_panic(expected = "assertion failed: \ + `SharedPtr` reference count \ + does not match expectation")] + fn shared_ptr_use_count_assertion_failed() { + let shared_ptr: SharedPtr = Default::default(); + shared_ptr.assert_use_count_eq(3); + } + + #[test] + #[should_panic(expected = "assertion failed: \ + `SharedRef` reference count \ + does not match expectation")] + fn shared_ref_use_count_assertion_failed() { + let shared_ref = SharedRef(MockSharedObj::SHARED_PTR_BASE_B); + shared_ref.assert_use_count_eq(7); + } +} diff --git a/crates/v8/src/symbol.rs b/crates/v8/src/symbol.rs new file mode 100644 index 0000000000..a5766ee495 --- /dev/null +++ b/crates/v8/src/symbol.rs @@ -0,0 +1,117 @@ +use crate::Local; +use crate::String; +use crate::Symbol; +use crate::Value; +use crate::isolate::RealIsolate; +use crate::scope::PinScope; + +unsafe extern "C" { + fn v8__Symbol__New( + isolate: *mut RealIsolate, + description: *const String, + ) -> *const Symbol; + fn v8__Symbol__For( + isolate: *mut RealIsolate, + description: *const String, + ) -> *const Symbol; + fn v8__Symbol__ForApi( + isolate: *mut RealIsolate, + description: *const String, + ) -> *const Symbol; + fn v8__Symbol__Description( + this: *const Symbol, + isolate: *mut RealIsolate, + ) -> *const Value; +} + +macro_rules! well_known { + ($name:ident, $binding:ident) => { + pub fn $name<'s>(scope: &PinScope<'s, '_, ()>) -> Local<'s, Symbol> { + unsafe extern "C" { + fn $binding(isolate: *mut RealIsolate) -> *const Symbol; + } + unsafe { scope.cast_local(|sd| $binding(sd.get_isolate_ptr())) }.unwrap() + } + }; +} + +impl Symbol { + /// Create a symbol. If description is not empty, it will be used as the + /// description. + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_, ()>, + description: Option>, + ) -> Local<'s, Symbol> { + unsafe { + scope.cast_local(|sd| { + v8__Symbol__New( + sd.get_isolate_ptr(), + description.map_or_else(std::ptr::null, |v| &*v), + ) + }) + } + .unwrap() + } + + /// Access global symbol registry. + /// Note that symbols created this way are never collected, so + /// they should only be used for statically fixed properties. + /// Also, there is only one global description space for the descriptions used as + /// keys. + /// To minimize the potential for clashes, use qualified descriptions as keys. + /// Corresponds to v8::Symbol::For() in C++. + #[inline(always)] + pub fn for_key<'s>( + scope: &PinScope<'s, '_, ()>, + description: Local, + ) -> Local<'s, Symbol> { + unsafe { + scope + .cast_local(|sd| v8__Symbol__For(sd.get_isolate_ptr(), &*description)) + } + .unwrap() + } + + /// Retrieve a global symbol. Similar to `for_key`, but using a separate + /// registry that is not accessible by (and cannot clash with) JavaScript code. + /// Corresponds to v8::Symbol::ForApi() in C++. + #[inline(always)] + pub fn for_api<'s>( + scope: &PinScope<'s, '_, ()>, + description: Local, + ) -> Local<'s, Symbol> { + unsafe { + scope.cast_local(|sd| { + v8__Symbol__ForApi(sd.get_isolate_ptr(), &*description) + }) + } + .unwrap() + } + + /// Returns the description string of the symbol, or undefined if none. + #[inline(always)] + pub fn description<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + ) -> Local<'s, Value> { + unsafe { + scope.cast_local(|sd| v8__Symbol__Description(self, sd.get_isolate_ptr())) + } + .unwrap() + } + + well_known!(get_async_iterator, v8__Symbol__GetAsyncIterator); + well_known!(get_has_instance, v8__Symbol__GetHasInstance); + well_known!(get_is_concat_spreadable, v8__Symbol__GetIsConcatSpreadable); + well_known!(get_iterator, v8__Symbol__GetIterator); + well_known!(get_match, v8__Symbol__GetMatch); + well_known!(get_replace, v8__Symbol__GetReplace); + well_known!(get_search, v8__Symbol__GetSearch); + well_known!(get_split, v8__Symbol__GetSplit); + well_known!(get_to_primitive, v8__Symbol__GetToPrimitive); + well_known!(get_to_string_tag, v8__Symbol__GetToStringTag); + well_known!(get_unscopables, v8__Symbol__GetUnscopables); + well_known!(get_dispose, v8__Symbol__GetDispose); + well_known!(get_async_dispose, v8__Symbol__GetAsyncDispose); +} diff --git a/crates/v8/src/template.rs b/crates/v8/src/template.rs new file mode 100644 index 0000000000..7cde06afb0 --- /dev/null +++ b/crates/v8/src/template.rs @@ -0,0 +1,1279 @@ +use crate::ConstructorBehavior; +use crate::Context; +use crate::Function; +use crate::FunctionBuilder; +use crate::FunctionCallback; +use crate::IndexedDefinerCallback; +use crate::IndexedDeleterCallback; +use crate::IndexedGetterCallback; +use crate::IndexedQueryCallback; +use crate::IndexedSetterCallback; +use crate::Local; +use crate::NamedDefinerCallback; +use crate::NamedDeleterCallback; +use crate::NamedGetterCallback; +use crate::NamedGetterCallbackForAccessor; +use crate::NamedQueryCallback; +use crate::NamedSetterCallback; +use crate::NamedSetterCallbackForAccessor; +use crate::Object; +use crate::PropertyAttribute; +use crate::PropertyEnumeratorCallback; +use crate::PropertyHandlerFlags; +use crate::SideEffectType; +use crate::Signature; +use crate::String; +use crate::Value; +pub use crate::binding::v8__Intercepted as Intercepted; +use crate::data::Data; +use crate::data::DictionaryTemplate; +use crate::data::FunctionTemplate; +use crate::data::Name; +use crate::data::ObjectTemplate; +use crate::data::Template; +use crate::fast_api::CFunction; +use crate::isolate::RealIsolate; +use crate::scope::PinScope; +use crate::support::MapFnTo; +use crate::support::int; +use std::convert::TryFrom; +use std::ffi::c_char; +use std::ptr::null; + +unsafe extern "C" { + fn v8__Template__Set( + this: *const Template, + key: *const Name, + value: *const Data, + attr: PropertyAttribute, + ); + fn v8__Template__SetIntrinsicDataProperty( + this: *const Template, + key: *const Name, + intrinsic: Intrinsic, + attr: PropertyAttribute, + ); + fn v8__Template__SetNativeDataProperty( + this: *const Template, + key: *const Name, + getter: AccessorNameGetterCallback, + setter: Option, + data_or_null: *const Value, + attr: PropertyAttribute, + ); + fn v8__Template__SetLazyDataProperty( + this: *const Template, + key: *const Name, + getter: AccessorNameGetterCallback, + data_or_null: *const Value, + attr: PropertyAttribute, + ); + + fn v8__Signature__New( + isolate: *mut RealIsolate, + templ: *const FunctionTemplate, + ) -> *const Signature; + fn v8__FunctionTemplate__New( + isolate: *mut RealIsolate, + callback: Option, + data_or_null: *const Value, + signature_or_null: *const Signature, + length: i32, + constructor_behavior: ConstructorBehavior, + side_effect_type: SideEffectType, + c_functions: *const CFunction, + c_functions_len: usize, + ) -> *const FunctionTemplate; + fn v8__FunctionTemplate__GetFunction( + this: *const FunctionTemplate, + context: *const Context, + ) -> *const Function; + fn v8__FunctionTemplate__PrototypeTemplate( + this: *const FunctionTemplate, + ) -> *const ObjectTemplate; + fn v8__FunctionTemplate__InstanceTemplate( + this: *const FunctionTemplate, + ) -> *const ObjectTemplate; + fn v8__FunctionTemplate__SetClassName( + this: *const FunctionTemplate, + name: *const String, + ); + fn v8__FunctionTemplate__SetAccessorProperty( + this: *const FunctionTemplate, + key: *const Name, + getter: *const FunctionTemplate, + setter: *const FunctionTemplate, + attr: PropertyAttribute, + ); + fn v8__FunctionTemplate__Inherit( + this: *const FunctionTemplate, + parent: *const FunctionTemplate, + ); + fn v8__FunctionTemplate__ReadOnlyPrototype(this: *const FunctionTemplate); + fn v8__FunctionTemplate__RemovePrototype(this: *const FunctionTemplate); + fn v8__FunctionTemplate__HasInstance( + this: *const FunctionTemplate, + object: *const Value, + ) -> bool; + + fn v8__DictionaryTemplate__New( + isolate: *mut RealIsolate, + names: *const *const c_char, + name_lengths: *const usize, + names_len: usize, + ) -> *const DictionaryTemplate; + fn v8__DictionaryTemplate__NewInstance( + this: *const DictionaryTemplate, + context: *const Context, + values: *const *const Value, + values_len: usize, + ) -> *const Object; + + fn v8__ObjectTemplate__New( + isolate: *mut RealIsolate, + templ: *const FunctionTemplate, + ) -> *const ObjectTemplate; + fn v8__ObjectTemplate__NewInstance( + this: *const ObjectTemplate, + context: *const Context, + ) -> *const Object; + fn v8__ObjectTemplate__InternalFieldCount(this: *const ObjectTemplate) + -> int; + fn v8__ObjectTemplate__SetInternalFieldCount( + this: *const ObjectTemplate, + value: int, + ); + + fn v8__ObjectTemplate__SetNativeDataProperty( + this: *const ObjectTemplate, + key: *const Name, + getter: AccessorNameGetterCallback, + setter: Option, + data_or_null: *const Value, + attr: PropertyAttribute, + ); + fn v8__ObjectTemplate__SetAccessorProperty( + this: *const ObjectTemplate, + key: *const Name, + getter: *const FunctionTemplate, + setter: *const FunctionTemplate, + attr: PropertyAttribute, + ); + + fn v8__ObjectTemplate__SetNamedPropertyHandler( + this: *const ObjectTemplate, + getter: Option, + setter: Option, + query: Option, + deleter: Option, + enumerator: Option, + definer: Option, + descriptor: Option, + data_or_null: *const Value, + flags: PropertyHandlerFlags, + ); + + fn v8__ObjectTemplate__SetIndexedPropertyHandler( + this: *const ObjectTemplate, + getter: Option, + setter: Option, + query: Option, + deleter: Option, + enumerator: Option, + definer: Option, + descriptor: Option, + data_or_null: *const Value, + flags: PropertyHandlerFlags, + ); + + fn v8__ObjectTemplate__SetImmutableProto(this: *const ObjectTemplate); + + fn v8__ObjectTemplate__SetCallAsFunctionHandler( + this: *const ObjectTemplate, + callback: FunctionCallback, + data_or_null: *const Value, + ); +} + +pub type AccessorNameGetterCallback = NamedGetterCallbackForAccessor; + +/// Note: [ReturnValue] is ignored for accessors. +pub type AccessorNameSetterCallback = NamedSetterCallbackForAccessor; + +/// Interceptor for get requests on an object. +/// +/// Use [ReturnValue] to set the return value of the intercepted get request. If +/// the property does not exist the callback should not set the result and must +/// not produce side effects. +/// +/// See also [ObjectTemplate::set_handler]. +pub type NamedPropertyGetterCallback = NamedGetterCallback; + +/// Interceptor for set requests on an object. +/// +/// Use [ReturnValue] to indicate whether the request was intercepted or not. If +/// the setter successfully intercepts the request, i.e., if the request should +/// not be further executed, call [ReturnValue::set]. If the setter did not +/// intercept the request, i.e., if the request should be handled as if no +/// interceptor is present, do not not call set() and do not produce side +/// effects. +/// +/// See also [ObjectTemplate::set_named_property_handler]. +pub type NamedPropertySetterCallback = NamedSetterCallback; + +/// Intercepts all requests that query the attributes of the property, e.g., +/// getOwnPropertyDescriptor(), propertyIsEnumerable(), and defineProperty(). +/// +/// Use [ReturnValue::set] to set the property attributes. The value is an +/// integer encoding a [PropertyAttribute]. If the property does not exist the +/// callback should not set the result and must not produce side effects. +/// +/// Note: Some functions query the property attributes internally, even though +/// they do not return the attributes. For example, hasOwnProperty() can trigger +/// this interceptor depending on the state of the object. +/// +/// See also [ObjectTemplate::set_named_property_handler]. +pub type NamedPropertyQueryCallback = NamedQueryCallback; + +/// Interceptor for delete requests on an object. +/// +/// Use [ReturnValue] to indicate whether the request was intercepted or not. If +/// the deleter successfully intercepts the request, i.e., if the request should +/// not be further executed, call [ReturnValue::set] with a boolean value. The +/// value is used as the return value of delete. If the deleter does not +/// intercept the request then it should not set the result and must not produce +/// side effects. +/// +/// Note: If you need to mimic the behavior of delete, i.e., throw in strict +/// mode instead of returning false, use +/// [PropertyCallbackArguments::should_throw_on_error] to determine if you are +/// in strict mode. +/// +/// See also [ObjectTemplate::set_named_property_handler]. +pub type NamedPropertyDeleterCallback = NamedDeleterCallback; + +/// Returns an array containing the names of the properties the named property getter intercepts. +/// +/// Note: The values in the array must be of type v8::Name. +/// +/// See also [ObjectTemplate::set_named_property_handler]. +pub type NamedPropertyEnumeratorCallback = PropertyEnumeratorCallback; + +/// Interceptor for defineProperty requests on an object. +/// +/// Use [ReturnValue] to indicate whether the request was intercepted or not. If +/// the definer successfully intercepts the request, i.e., if the request should +/// not be further executed, call [ReturnValue::set]. If the definer did not +/// intercept the request, i.e., if the request should be handled as if no +/// interceptor is present, do not not call set() and do not produce side +/// effects. +/// +/// See also [ObjectTemplate::set_named_property_handler]. +pub type NamedPropertyDefinerCallback = NamedDefinerCallback; + +/// Interceptor for getOwnPropertyDescriptor requests on an object. +/// +/// Use [ReturnValue::set] to set the return value of the intercepted request. +/// The return value must be an object that can be converted to a +/// [PropertyDescriptor], e.g., a [Value] returned from +/// `Object.getOwnPropertyDescriptor()`. +/// +/// Note: If GetOwnPropertyDescriptor is intercepted, it will always return +/// true, i.e., indicate that the property was found. +/// +/// See also [ObjectTemplate::set_named_property_handler]. +pub type NamedPropertyDescriptorCallback = NamedGetterCallback; + +/// See [GenericNamedPropertyGetterCallback]. +pub type IndexedPropertyGetterCallback = IndexedGetterCallback; + +/// See [GenericNamedPropertySetterCallback]. +pub type IndexedPropertySetterCallback = IndexedSetterCallback; + +/// See [GenericNamedPropertyQueryCallback]. +pub type IndexedPropertyQueryCallback = IndexedQueryCallback; + +/// See [GenericNamedPropertyDeleterCallback]. +pub type IndexedPropertyDeleterCallback = IndexedDeleterCallback; + +/// See [GenericNamedPropertyEnumeratorCallback]. +pub type IndexedPropertyEnumeratorCallback = PropertyEnumeratorCallback; + +/// See [GenericNamedPropertyDefinerCallback]. +pub type IndexedPropertyDefinerCallback = IndexedDefinerCallback; + +/// See [GenericNamedPropertyDescriptorCallback]. +pub type IndexedPropertyDescriptorCallback = IndexedGetterCallback; + +pub struct AccessorConfiguration<'s> { + pub(crate) getter: AccessorNameGetterCallback, + pub(crate) setter: Option, + pub(crate) data: Option>, + pub(crate) property_attribute: PropertyAttribute, +} + +impl<'s> AccessorConfiguration<'s> { + pub fn new(getter: impl MapFnTo) -> Self { + Self { + getter: getter.map_fn_to(), + setter: None, + data: None, + property_attribute: PropertyAttribute::NONE, + } + } + + pub fn setter( + mut self, + setter: impl MapFnTo, + ) -> Self { + self.setter = Some(setter.map_fn_to()); + self + } + + pub fn property_attribute( + mut self, + property_attribute: PropertyAttribute, + ) -> Self { + self.property_attribute = property_attribute; + self + } + + /// Set the associated data. The default is no associated data. + pub fn data(mut self, data: Local<'s, Value>) -> Self { + self.data = Some(data); + self + } +} + +#[derive(Default)] +pub struct NamedPropertyHandlerConfiguration<'s> { + pub(crate) getter: Option, + pub(crate) setter: Option, + pub(crate) query: Option, + pub(crate) deleter: Option, + pub(crate) enumerator: Option, + pub(crate) definer: Option, + pub(crate) descriptor: Option, + pub(crate) data: Option>, + pub(crate) flags: PropertyHandlerFlags, +} + +impl<'s> NamedPropertyHandlerConfiguration<'s> { + pub fn new() -> Self { + Self { + getter: None, + setter: None, + query: None, + deleter: None, + enumerator: None, + definer: None, + descriptor: None, + data: None, + flags: PropertyHandlerFlags::NONE, + } + } + + pub fn is_some(&self) -> bool { + self.getter.is_some() + || self.setter.is_some() + || self.query.is_some() + || self.deleter.is_some() + || self.enumerator.is_some() + || self.definer.is_some() + || self.descriptor.is_some() + || !self.flags.is_none() + } + + pub fn getter( + mut self, + getter: impl MapFnTo, + ) -> Self { + self.getter = Some(getter.map_fn_to()); + self + } + + pub fn getter_raw(mut self, getter: NamedPropertyGetterCallback) -> Self { + self.getter = Some(getter); + self + } + + pub fn setter( + mut self, + setter: impl MapFnTo, + ) -> Self { + self.setter = Some(setter.map_fn_to()); + self + } + + pub fn setter_raw(mut self, setter: NamedPropertySetterCallback) -> Self { + self.setter = Some(setter); + self + } + + pub fn query( + mut self, + query: impl MapFnTo, + ) -> Self { + self.query = Some(query.map_fn_to()); + self + } + + pub fn query_raw(mut self, query: NamedPropertyQueryCallback) -> Self { + self.query = Some(query); + self + } + + pub fn deleter( + mut self, + deleter: impl MapFnTo, + ) -> Self { + self.deleter = Some(deleter.map_fn_to()); + self + } + + pub fn deleter_raw(mut self, deleter: NamedPropertyDeleterCallback) -> Self { + self.deleter = Some(deleter); + self + } + + pub fn enumerator( + mut self, + enumerator: impl MapFnTo, + ) -> Self { + self.enumerator = Some(enumerator.map_fn_to()); + self + } + + pub fn enumerator_raw( + mut self, + enumerator: NamedPropertyEnumeratorCallback, + ) -> Self { + self.enumerator = Some(enumerator); + self + } + + pub fn definer( + mut self, + definer: impl MapFnTo, + ) -> Self { + self.definer = Some(definer.map_fn_to()); + self + } + + pub fn definer_raw(mut self, definer: NamedPropertyDefinerCallback) -> Self { + self.definer = Some(definer); + self + } + + pub fn descriptor( + mut self, + descriptor: impl MapFnTo, + ) -> Self { + self.descriptor = Some(descriptor.map_fn_to()); + self + } + + pub fn descriptor_raw( + mut self, + descriptor: NamedPropertyDescriptorCallback, + ) -> Self { + self.descriptor = Some(descriptor); + self + } + + /// Set the associated data. The default is no associated data. + pub fn data(mut self, data: Local<'s, Value>) -> Self { + self.data = Some(data); + self + } + + /// Set the property handler flags. The default is PropertyHandlerFlags::NONE. + pub fn flags(mut self, flags: PropertyHandlerFlags) -> Self { + self.flags = flags; + self + } +} + +#[derive(Default)] +pub struct IndexedPropertyHandlerConfiguration<'s> { + pub(crate) getter: Option, + pub(crate) setter: Option, + pub(crate) query: Option, + pub(crate) deleter: Option, + pub(crate) enumerator: Option, + pub(crate) definer: Option, + pub(crate) descriptor: Option, + pub(crate) data: Option>, + pub(crate) flags: PropertyHandlerFlags, +} + +impl<'s> IndexedPropertyHandlerConfiguration<'s> { + pub fn new() -> Self { + Self { + getter: None, + setter: None, + query: None, + deleter: None, + enumerator: None, + definer: None, + descriptor: None, + data: None, + flags: PropertyHandlerFlags::NONE, + } + } + + pub fn is_some(&self) -> bool { + self.getter.is_some() + || self.setter.is_some() + || self.query.is_some() + || self.deleter.is_some() + || self.enumerator.is_some() + || self.definer.is_some() + || self.descriptor.is_some() + || !self.flags.is_none() + } + + pub fn getter( + mut self, + getter: impl MapFnTo, + ) -> Self { + self.getter = Some(getter.map_fn_to()); + self + } + + pub fn getter_raw(mut self, getter: IndexedPropertyGetterCallback) -> Self { + self.getter = Some(getter); + self + } + + pub fn setter( + mut self, + setter: impl MapFnTo, + ) -> Self { + self.setter = Some(setter.map_fn_to()); + self + } + + pub fn setter_raw(mut self, setter: IndexedPropertySetterCallback) -> Self { + self.setter = Some(setter); + self + } + + pub fn query( + mut self, + query: impl MapFnTo, + ) -> Self { + self.query = Some(query.map_fn_to()); + self + } + + pub fn query_raw(mut self, query: IndexedPropertyQueryCallback) -> Self { + self.query = Some(query); + self + } + + pub fn deleter( + mut self, + deleter: impl MapFnTo, + ) -> Self { + self.deleter = Some(deleter.map_fn_to()); + self + } + + pub fn deleter_raw( + mut self, + deleter: IndexedPropertyDeleterCallback, + ) -> Self { + self.deleter = Some(deleter); + self + } + + pub fn enumerator( + mut self, + enumerator: impl MapFnTo, + ) -> Self { + self.enumerator = Some(enumerator.map_fn_to()); + self + } + + pub fn enumerator_raw( + mut self, + enumerator: IndexedPropertyEnumeratorCallback, + ) -> Self { + self.enumerator = Some(enumerator); + self + } + + pub fn definer( + mut self, + definer: impl MapFnTo, + ) -> Self { + self.definer = Some(definer.map_fn_to()); + self + } + + pub fn definer_raw( + mut self, + definer: IndexedPropertyDefinerCallback, + ) -> Self { + self.definer = Some(definer); + self + } + + pub fn descriptor( + mut self, + descriptor: impl MapFnTo, + ) -> Self { + self.descriptor = Some(descriptor.map_fn_to()); + self + } + + pub fn descriptor_raw( + mut self, + descriptor: IndexedPropertyDescriptorCallback, + ) -> Self { + self.descriptor = Some(descriptor); + self + } + + /// Set the associated data. The default is no associated data. + pub fn data(mut self, data: Local<'s, Value>) -> Self { + self.data = Some(data); + self + } + + /// Set the property handler flags. The default is PropertyHandlerFlags::NONE. + pub fn flags(mut self, flags: PropertyHandlerFlags) -> Self { + self.flags = flags; + self + } +} + +#[derive(Debug, Clone, Copy)] +#[repr(C)] +pub enum Intrinsic { + ArrayProtoEntries, + ArrayProtoForEach, + ArrayProtoKeys, + ArrayProtoValues, + ArrayPrototype, + AsyncIteratorPrototype, + ErrorPrototype, + IteratorPrototype, + MapIteratorPrototype, + ObjProtoValueOf, + SetIteratorPrototype, +} + +impl Template { + /// Adds a property to each instance created by this template. + #[inline(always)] + pub fn set(&self, key: Local, value: Local) { + self.set_with_attr(key, value, PropertyAttribute::NONE); + } + + /// Adds a property to each instance created by this template with + /// the specified property attributes. + #[inline(always)] + pub fn set_with_attr( + &self, + key: Local, + value: Local, + attr: PropertyAttribute, + ) { + unsafe { v8__Template__Set(self, &*key, &*value, attr) } + } + + /// Sets a native data property on every instance created by this template. + /// + /// The `getter` callback is invoked whenever the property is read, and the + /// optional `setter` callback whenever it is written. `data` is passed + /// through to both callbacks on every invocation. + /// + /// Unlike [`Template::set_lazy_data_property`], the callbacks stay installed + /// and are consulted on every access. + #[inline(always)] + pub fn set_native_data_property( + &self, + key: Local, + configuration: AccessorConfiguration, + ) { + unsafe { + v8__Template__SetNativeDataProperty( + self, + &*key, + configuration.getter, + configuration.setter, + configuration.data.map_or_else(null, |p| &*p), + configuration.property_attribute, + ); + } + } + + /// Like [`Template::set_native_data_property`], but V8 replaces the native + /// data property with a real data property holding the returned value on + /// first access, so the getter runs at most once per instance. + #[inline(always)] + pub fn set_lazy_data_property( + &self, + key: Local, + getter: impl MapFnTo, + data: Option>, + attr: PropertyAttribute, + ) { + unsafe { + v8__Template__SetLazyDataProperty( + self, + &*key, + getter.map_fn_to(), + data.map_or_else(null, |p| &*p), + attr, + ); + } + } + + /// During template instantiation, sets the value with the + /// intrinsic property from the correct context. + #[inline(always)] + pub fn set_intrinsic_data_property( + &self, + key: Local, + intrinsic: Intrinsic, + attr: PropertyAttribute, + ) { + unsafe { + v8__Template__SetIntrinsicDataProperty(self, &*key, intrinsic, attr); + } + } +} + +impl<'s> FunctionBuilder<'s, FunctionTemplate> { + /// Create a builder for a [`FunctionTemplate`] that has no call handler. + /// + /// This mirrors `v8::FunctionTemplate::New(isolate)` with a null callback, + /// and is useful when the template only serves to describe a class, i.e. + /// when only its instance template, prototype template or identity is of + /// interest. + #[inline(always)] + pub fn new_without_callback() -> Self { + FunctionBuilder::new_raw_optional(None) + } + + /// Set the function call signature. The default is no signature. + #[inline(always)] + pub fn signature(mut self, signature: Local<'s, Signature>) -> Self { + self.signature = Some(signature); + self + } + + /// Creates the function template. + #[inline(always)] + pub fn build<'i>( + self, + scope: &PinScope<'s, 'i, ()>, + ) -> Local<'s, FunctionTemplate> { + unsafe { + scope.cast_local(|sd| { + v8__FunctionTemplate__New( + sd.get_isolate_ptr(), + self.callback, + self.data.map_or_else(null, |p| &*p), + self.signature.map_or_else(null, |p| &*p), + self.length, + self.constructor_behavior, + self.side_effect_type, + null(), + 0, + ) + }) + } + .unwrap() + } + + /// It's not required to provide `CFunctionInfo` for the overloads - if they + /// are omitted, then they will be automatically created. In some cases it is + /// useful to pass them explicitly - eg. when you are snapshotting you'd provide + /// the overloads and `CFunctionInfo` that would be placed in the external + /// references array. + /// + /// # Lifetime invariant + /// + /// `overloads` is `&'static [CFunction]` because, since + /// [crrev.com/c/7828135], V8 stores the raw `v8::CFunction` pointers it + /// receives via `NewWithCFunctionOverloads` directly inside + /// `FunctionTemplateInfo` (rather than copying them into a managed heap + /// object). The pointed-to storage must therefore outlive every + /// `FunctionTemplate` that references it. A `FunctionTemplate` may live + /// until isolate disposal, so in practice this requires the slice (and its + /// elements, including the `CFunctionInfo`s they reference) to have + /// `'static` storage. Use a `const` slice such as + /// + /// ```ignore + /// const OVERLOADS: &[v8::fast_api::CFunction] = &[FAST_TEST]; + /// ``` + /// + /// or a `static` item; do **not** synthesize the slice from stack-local + /// data. + /// + /// [crrev.com/c/7828135]: https://chromium-review.googlesource.com/c/v8/v8/+/7828135 + pub fn build_fast<'i>( + self, + scope: &PinScope<'s, 'i>, + overloads: &'static [CFunction], + ) -> Local<'s, FunctionTemplate> { + unsafe { + scope.cast_local(|sd| { + v8__FunctionTemplate__New( + sd.get_isolate_ptr(), + self.callback, + self.data.map_or_else(null, |p| &*p), + self.signature.map_or_else(null, |p| &*p), + self.length, + ConstructorBehavior::Throw, + self.side_effect_type, + overloads.as_ptr(), + overloads.len(), + ) + }) + } + .unwrap() + } +} + +/// A Signature specifies which receiver is valid for a function. +/// +/// A receiver matches a given signature if the receiver (or any of its +/// hidden prototypes) was created from the signature's FunctionTemplate, or +/// from a FunctionTemplate that inherits directly or indirectly from the +/// signature's FunctionTemplate. +impl Signature { + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_, ()>, + templ: Local, + ) -> Local<'s, Self> { + unsafe { + scope.cast_local(|sd| v8__Signature__New(sd.get_isolate_ptr(), &*templ)) + } + .unwrap() + } +} + +impl FunctionTemplate { + /// Creates a function template without a call handler. + /// + /// See [`FunctionBuilder::new_without_callback`]. + #[inline(always)] + pub fn new_without_callback<'s>( + scope: &PinScope<'s, '_, ()>, + ) -> Local<'s, FunctionTemplate> { + FunctionBuilder::::new_without_callback().build(scope) + } + + /// Create a FunctionBuilder to configure a FunctionTemplate. + /// This is the same as FunctionBuilder::::new(). + #[inline(always)] + pub fn builder<'s>( + callback: impl MapFnTo, + ) -> FunctionBuilder<'s, Self> { + FunctionBuilder::new(callback) + } + + #[inline(always)] + pub fn builder_raw<'s>( + callback: FunctionCallback, + ) -> FunctionBuilder<'s, Self> { + FunctionBuilder::new_raw(callback) + } + + /// Creates a function template. + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_, ()>, + callback: impl MapFnTo, + ) -> Local<'s, FunctionTemplate> { + Self::builder(callback).build(scope) + } + + #[inline(always)] + pub fn new_raw<'s>( + scope: &PinScope<'s, '_, ()>, + callback: FunctionCallback, + ) -> Local<'s, FunctionTemplate> { + Self::builder_raw(callback).build(scope) + } + + /// Returns the unique function instance in the current execution context. + #[inline(always)] + pub fn get_function<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__FunctionTemplate__GetFunction(self, sd.get_current_context()) + }) + } + } + + /// Set the class name of the FunctionTemplate. This is used for + /// printing objects created with the function created from the + /// FunctionTemplate as its constructor. + #[inline(always)] + pub fn set_class_name(&self, name: Local) { + unsafe { v8__FunctionTemplate__SetClassName(self, &*name) }; + } + + /// Returns the ObjectTemplate that is used by this + /// FunctionTemplate as a PrototypeTemplate + #[inline(always)] + pub fn prototype_template<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + ) -> Local<'s, ObjectTemplate> { + unsafe { + scope.cast_local(|_sd| v8__FunctionTemplate__PrototypeTemplate(self)) + } + .unwrap() + } + + /// Returns the object template that is used for instances created when this function + /// template is called as a constructor. + #[inline(always)] + pub fn instance_template<'s>( + &self, + scope: &PinScope<'s, '_, ()>, + ) -> Local<'s, ObjectTemplate> { + unsafe { + scope.cast_local(|_sd| v8__FunctionTemplate__InstanceTemplate(self)) + } + .unwrap() + } + + /// Causes the function template to inherit from a parent function template. + /// This means the function's prototype.__proto__ is set to the parent function's prototype. + #[inline(always)] + pub fn inherit(&self, parent: Local) { + unsafe { v8__FunctionTemplate__Inherit(self, &*parent) }; + } + + /// Sets the ReadOnly flag in the attributes of the 'prototype' property + /// of functions created from this FunctionTemplate to true. + #[inline(always)] + pub fn read_only_prototype(&self) { + unsafe { v8__FunctionTemplate__ReadOnlyPrototype(self) }; + } + + /// Removes the prototype property from functions created from this FunctionTemplate. + #[inline(always)] + pub fn remove_prototype(&self) { + unsafe { v8__FunctionTemplate__RemovePrototype(self) }; + } + + /// Sets an [accessor property](https://tc39.es/ecma262/#sec-property-attributes) + /// on the function template (i.e. a static accessor on the constructor). + /// + /// # Panics + /// + /// Panics if both `getter` and `setter` are `None`. + #[inline(always)] + pub fn set_accessor_property( + &self, + key: Local, + getter: Option>, + setter: Option>, + attr: PropertyAttribute, + ) { + assert!(getter.is_some() || setter.is_some()); + + unsafe { + let getter = getter.map_or_else(std::ptr::null, |v| &*v); + let setter = setter.map_or_else(std::ptr::null, |v| &*v); + v8__FunctionTemplate__SetAccessorProperty( + self, &*key, getter, setter, attr, + ); + } + } + + /// Returns true if the given object is an instance of this function + /// template. + #[inline(always)] + pub fn has_instance(&self, object: Local) -> bool { + unsafe { v8__FunctionTemplate__HasInstance(self, &*object) } + } +} + +impl DictionaryTemplate { + /// Creates a new template, declaring the data properties that can be passed + /// on instantiation of the template. Properties can only be declared on + /// construction and are then immutable. Their values are passed when + /// creating an object via [`DictionaryTemplate::new_instance`]. + /// + /// # Panics + /// + /// Since V8 15.8 the declared names must be ASCII; V8 internalizes them + /// as one-byte strings and debug builds of V8 abort on any other byte. + /// This function panics on a name containing a non-ASCII character. + /// + /// V8 also rejects — by aborting the process — any name that is a canonical + /// array index, such as `"0"`. + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_, ()>, + names: &[&str], + ) -> Local<'s, DictionaryTemplate> { + for name in names { + if let Some(c) = name.chars().find(|c| !c.is_ascii()) { + panic!( + "DictionaryTemplate property name {name:?} contains U+{:04X}; \ + V8 only accepts ASCII names through this API", + u32::from(c) + ); + } + } + let bytes: Vec<&[u8]> = names.iter().map(|name| name.as_bytes()).collect(); + let ptrs: Vec<*const c_char> = + bytes.iter().map(|n| n.as_ptr().cast()).collect(); + let lengths: Vec = bytes.iter().map(|n| n.len()).collect(); + unsafe { + scope.cast_local(|sd| { + v8__DictionaryTemplate__New( + sd.get_isolate_ptr(), + ptrs.as_ptr(), + lengths.as_ptr(), + names.len(), + ) + }) + } + .unwrap() + } + + /// Creates a new instance of this template. + /// + /// `values` supplies the values of the properties declared when the template + /// was created, in declaration order. A `None` entry signals a non-existent + /// property, which is left out of the resulting object. + /// + /// `values` must have exactly one entry per declared property; V8 aborts the + /// process if it does not. + #[inline(always)] + pub fn new_instance<'s>( + &self, + scope: &PinScope<'s, '_>, + values: &[Option>], + ) -> Option> { + let values: Vec<*const Value> = values + .iter() + .map(|v| v.map_or_else(null, |v| &*v as _)) + .collect(); + unsafe { + scope.cast_local(|sd| { + v8__DictionaryTemplate__NewInstance( + self, + sd.get_current_context(), + values.as_ptr(), + values.len(), + ) + }) + } + } +} + +impl ObjectTemplate { + /// Creates an object template. + #[inline(always)] + pub fn new<'s>(scope: &PinScope<'s, '_, ()>) -> Local<'s, ObjectTemplate> { + unsafe { + scope.cast_local(|sd| { + v8__ObjectTemplate__New(sd.get_isolate_ptr(), std::ptr::null()) + }) + } + .unwrap() + } + + /// Creates an object template from a function template. + #[inline(always)] + pub fn new_from_template<'s>( + scope: &PinScope<'s, '_, ()>, + templ: Local, + ) -> Local<'s, ObjectTemplate> { + unsafe { + scope + .cast_local(|sd| v8__ObjectTemplate__New(sd.get_isolate_ptr(), &*templ)) + } + .unwrap() + } + + /// Creates a new instance of this object template. + #[inline(always)] + pub fn new_instance<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__ObjectTemplate__NewInstance(self, sd.get_current_context()) + }) + } + } + + /// Gets the number of internal fields for objects generated from + /// this template. + #[inline(always)] + pub fn internal_field_count(&self) -> usize { + let count = unsafe { v8__ObjectTemplate__InternalFieldCount(self) }; + usize::try_from(count).expect("bad internal field count") // Can't happen. + } + + /// Sets the number of internal fields for objects generated from + /// this template. + #[inline(always)] + pub fn set_internal_field_count(&self, value: usize) -> bool { + // The C++ API takes an i32 but trying to set a value < 0 + // results in unpredictable behavior, hence we disallow it. + match int::try_from(value) { + Err(_) => false, + Ok(value) => { + unsafe { v8__ObjectTemplate__SetInternalFieldCount(self, value) }; + true + } + } + } + + #[inline(always)] + pub fn set_accessor( + &self, + key: Local, + getter: impl MapFnTo, + ) { + self + .set_accessor_with_configuration(key, AccessorConfiguration::new(getter)); + } + + #[inline(always)] + pub fn set_accessor_with_setter( + &self, + key: Local, + getter: impl MapFnTo, + setter: impl MapFnTo, + ) { + self.set_accessor_with_configuration( + key, + AccessorConfiguration::new(getter).setter(setter), + ); + } + + #[inline(always)] + pub fn set_accessor_with_configuration( + &self, + key: Local, + configuration: AccessorConfiguration, + ) { + unsafe { + v8__ObjectTemplate__SetNativeDataProperty( + self, + &*key, + configuration.getter, + configuration.setter, + configuration.data.map_or_else(null, |p| &*p), + configuration.property_attribute, + ); + } + } + + //Re uses the AccessorNameGetterCallback to avoid implementation conflicts since the declaration for + //GenericNamedPropertyGetterCallback and AccessorNameGetterCallback are the same + pub fn set_named_property_handler( + &self, + configuration: NamedPropertyHandlerConfiguration, + ) { + assert!(configuration.is_some()); + unsafe { + v8__ObjectTemplate__SetNamedPropertyHandler( + self, + configuration.getter, + configuration.setter, + configuration.query, + configuration.deleter, + configuration.enumerator, + configuration.definer, + configuration.descriptor, + configuration.data.map_or_else(null, |p| &*p), + configuration.flags, + ); + } + } + + pub fn set_indexed_property_handler( + &self, + configuration: IndexedPropertyHandlerConfiguration, + ) { + assert!(configuration.is_some()); + unsafe { + v8__ObjectTemplate__SetIndexedPropertyHandler( + self, + configuration.getter, + configuration.setter, + configuration.query, + configuration.deleter, + configuration.enumerator, + configuration.definer, + configuration.descriptor, + configuration.data.map_or_else(null, |p| &*p), + configuration.flags, + ); + } + } + + /// Sets an [accessor property](https://tc39.es/ecma262/#sec-property-attributes) + /// on the object template. + /// + /// # Panics + /// + /// Panics if both `getter` and `setter` are `None`. + #[inline(always)] + pub fn set_accessor_property( + &self, + key: Local, + getter: Option>, + setter: Option>, + attr: PropertyAttribute, + ) { + assert!(getter.is_some() || setter.is_some()); + + unsafe { + let getter = getter.map_or_else(std::ptr::null, |v| &*v); + let setter = setter.map_or_else(std::ptr::null, |v| &*v); + v8__ObjectTemplate__SetAccessorProperty( + self, &*key, getter, setter, attr, + ); + } + } + + /// Makes the ObjectTemplate for an immutable prototype exotic object, + /// with an immutable proto. + #[inline(always)] + pub fn set_immutable_proto(&self) { + unsafe { v8__ObjectTemplate__SetImmutableProto(self) }; + } + + /// Sets the callback to be used when calling instances created from this + /// template as a function. + /// + /// If no callback is set, instances behave like normal [`Object`]s that + /// cannot be called like [`Function`]s. + #[inline(always)] + pub fn set_call_as_function_handler( + &self, + callback: impl MapFnTo, + data: Option>, + ) { + unsafe { + v8__ObjectTemplate__SetCallAsFunctionHandler( + self, + callback.map_fn_to(), + data.map_or_else(null, |p| &*p), + ); + } + } +} diff --git a/crates/v8/src/typed_array.rs b/crates/v8/src/typed_array.rs new file mode 100644 index 0000000000..5e07334323 --- /dev/null +++ b/crates/v8/src/typed_array.rs @@ -0,0 +1,90 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. +use crate::ArrayBuffer; +use crate::Local; +use crate::PinScope; +use crate::SharedArrayBuffer; +use crate::TypedArray; +use crate::binding::v8__TypedArray__kMaxByteLength; +use crate::support::size_t; +use paste::paste; + +unsafe extern "C" { + fn v8__TypedArray__Length(this: *const TypedArray) -> size_t; +} + +impl TypedArray { + /// The largest supported typed array byte size. Each subclass defines a + /// type-specific max_length for the maximum length that can be passed to new. + pub const MAX_BYTE_LENGTH: usize = v8__TypedArray__kMaxByteLength; + + /// Number of elements in this typed array + /// (e.g. for Int16Array, |ByteLength|/2). + #[inline(always)] + pub fn length(&self) -> usize { + unsafe { v8__TypedArray__Length(self) } + } +} + +macro_rules! typed_array { + ($name:ident) => { + paste! { + use crate::$name; + + unsafe extern "C" { + fn [< v8__ $name __New >]( + buf_ptr: *const ArrayBuffer, + byte_offset: usize, + length: usize, + ) -> *const $name; + fn [< v8__ $name __New__with_shared_buffer >]( + buf_ptr: *const SharedArrayBuffer, + byte_offset: usize, + length: usize, + ) -> *const $name; + } + + impl $name { + #[inline(always)] + pub fn new<'s>( + scope: &PinScope<'s, '_>, + buf: Local, + byte_offset: usize, + length: usize, + ) -> Option> { + unsafe { scope.cast_local(|_| [< v8__ $name __New >](&*buf, byte_offset, length)) } + } + + #[doc = concat!("Creates a ", stringify!($name), " over a `SharedArrayBuffer`.")] + #[inline(always)] + pub fn new_with_shared_buffer<'s>( + scope: &PinScope<'s, '_>, + buf: Local, + byte_offset: usize, + length: usize, + ) -> Option> { + unsafe { + scope.cast_local(|_| { + [< v8__ $name __New__with_shared_buffer >](&*buf, byte_offset, length) + }) + } + } + + #[doc = concat!("The largest ", stringify!($name), " size that can be constructed using `new`.")] + pub const MAX_LENGTH: usize = crate::binding::[< v8__ $name __kMaxLength >]; + } + } + }; +} + +typed_array!(Uint8Array); +typed_array!(Uint8ClampedArray); +typed_array!(Int8Array); +typed_array!(Uint16Array); +typed_array!(Int16Array); +typed_array!(Uint32Array); +typed_array!(Int32Array); +typed_array!(Float16Array); +typed_array!(Float32Array); +typed_array!(Float64Array); +typed_array!(BigUint64Array); +typed_array!(BigInt64Array); diff --git a/crates/v8/src/unbound_module_script.rs b/crates/v8/src/unbound_module_script.rs new file mode 100644 index 0000000000..a9da4bdef4 --- /dev/null +++ b/crates/v8/src/unbound_module_script.rs @@ -0,0 +1,61 @@ +use crate::CachedData; +use crate::Local; +use crate::PinScope; +use crate::UnboundModuleScript; +use crate::UniqueRef; +use crate::Value; + +unsafe extern "C" { + fn v8__UnboundModuleScript__CreateCodeCache( + script: *const UnboundModuleScript, + ) -> *mut CachedData<'static>; + + fn v8__UnboundModuleScript__GetSourceMappingURL( + script: *const UnboundModuleScript, + ) -> *const Value; + + fn v8__UnboundModuleScript__GetSourceURL( + script: *const UnboundModuleScript, + ) -> *const Value; +} + +impl UnboundModuleScript { + /// Creates and returns code cache for the specified unbound_module_script. + /// This will return nullptr if the script cannot be serialized. The + /// CachedData returned by this function should be owned by the caller. + #[inline(always)] + pub fn create_code_cache(&self) -> Option>> { + let code_cache = unsafe { + UniqueRef::try_from_raw(v8__UnboundModuleScript__CreateCodeCache(self)) + }; + if let Some(code_cache) = &code_cache { + debug_assert_eq!( + code_cache.buffer_policy(), + crate::script_compiler::BufferPolicy::BufferOwned + ); + } + code_cache + } + + pub fn get_source_mapping_url<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Local<'s, Value> { + unsafe { + scope + .cast_local(|_| v8__UnboundModuleScript__GetSourceMappingURL(self)) + .unwrap() + } + } + + pub fn get_source_url<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Local<'s, Value> { + unsafe { + scope + .cast_local(|_| v8__UnboundModuleScript__GetSourceURL(self)) + .unwrap() + } + } +} diff --git a/crates/v8/src/unbound_script.rs b/crates/v8/src/unbound_script.rs new file mode 100644 index 0000000000..599d8c66f7 --- /dev/null +++ b/crates/v8/src/unbound_script.rs @@ -0,0 +1,86 @@ +use crate::CachedData; +use crate::Local; +use crate::PinScope; +use crate::Script; +use crate::UnboundScript; +use crate::UniqueRef; +use crate::Value; + +unsafe extern "C" { + fn v8__UnboundScript__BindToCurrentContext( + script: *const UnboundScript, + ) -> *const Script; + fn v8__UnboundScript__ScriptId(script: *const UnboundScript) -> i32; + fn v8__UnboundScript__CreateCodeCache( + script: *const UnboundScript, + ) -> *mut CachedData<'static>; + + fn v8__UnboundScript__GetSourceMappingURL( + script: *const UnboundScript, + ) -> *const Value; + + fn v8__UnboundScript__GetSourceURL( + script: *const UnboundScript, + ) -> *const Value; +} + +impl UnboundScript { + /// Binds the script to the currently entered context. + #[inline(always)] + pub fn bind_to_current_context<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Local<'s, Script> { + unsafe { + scope.cast_local(|_| v8__UnboundScript__BindToCurrentContext(self)) + } + .unwrap() + } + + /// Returns the ID assigned to this context-unbound script by V8. + #[inline(always)] + pub fn script_id(&self) -> i32 { + unsafe { v8__UnboundScript__ScriptId(self) } + } + + /// Creates and returns code cache for the specified unbound_script. + /// This will return nullptr if the script cannot be serialized. The + /// CachedData returned by this function should be owned by the caller. + #[inline(always)] + pub fn create_code_cache(&self) -> Option>> { + let code_cache = unsafe { + UniqueRef::try_from_raw(v8__UnboundScript__CreateCodeCache(self)) + }; + if let Some(code_cache) = &code_cache { + debug_assert_eq!( + code_cache.buffer_policy(), + crate::script_compiler::BufferPolicy::BufferOwned + ); + } + code_cache + } + + #[inline(always)] + pub fn get_source_mapping_url<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Local<'s, Value> { + unsafe { + scope + .cast_local(|_| v8__UnboundScript__GetSourceMappingURL(self)) + .unwrap() + } + } + + #[inline(always)] + pub fn get_source_url<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Local<'s, Value> { + unsafe { + scope + .cast_local(|_| v8__UnboundScript__GetSourceURL(self)) + .unwrap() + } + } +} diff --git a/crates/v8/src/value.rs b/crates/v8/src/value.rs new file mode 100644 index 0000000000..c83b73d7df --- /dev/null +++ b/crates/v8/src/value.rs @@ -0,0 +1,829 @@ +use crate::BigInt; +use crate::Boolean; +use crate::Context; +use crate::Int32; +use crate::Integer; +use crate::Local; +use crate::Number; +use crate::Object; +use crate::PinScope; +use crate::String; +use crate::Uint32; +use crate::Value; +use crate::isolate::RealIsolate; +use crate::scope::GetIsolate; +use crate::support::Maybe; + +unsafe extern "C" { + fn v8__Value__IsUndefined(this: *const Value) -> bool; + fn v8__Value__IsNull(this: *const Value) -> bool; + fn v8__Value__IsNullOrUndefined(this: *const Value) -> bool; + fn v8__Value__IsTrue(this: *const Value) -> bool; + fn v8__Value__IsFalse(this: *const Value) -> bool; + fn v8__Value__IsName(this: *const Value) -> bool; + fn v8__Value__IsString(this: *const Value) -> bool; + fn v8__Value__IsSymbol(this: *const Value) -> bool; + fn v8__Value__IsFunction(this: *const Value) -> bool; + fn v8__Value__IsArray(this: *const Value) -> bool; + fn v8__Value__IsObject(this: *const Value) -> bool; + fn v8__Value__IsBigInt(this: *const Value) -> bool; + fn v8__Value__IsBoolean(this: *const Value) -> bool; + fn v8__Value__IsNumber(this: *const Value) -> bool; + fn v8__Value__IsExternal(this: *const Value) -> bool; + fn v8__Value__IsInt32(this: *const Value) -> bool; + fn v8__Value__IsUint32(this: *const Value) -> bool; + fn v8__Value__IsDate(this: *const Value) -> bool; + fn v8__Value__IsArgumentsObject(this: *const Value) -> bool; + fn v8__Value__IsBigIntObject(this: *const Value) -> bool; + fn v8__Value__IsBooleanObject(this: *const Value) -> bool; + fn v8__Value__IsNumberObject(this: *const Value) -> bool; + fn v8__Value__IsStringObject(this: *const Value) -> bool; + fn v8__Value__IsSymbolObject(this: *const Value) -> bool; + fn v8__Value__IsNativeError(this: *const Value) -> bool; + fn v8__Value__IsRegExp(this: *const Value) -> bool; + fn v8__Value__IsAsyncFunction(this: *const Value) -> bool; + fn v8__Value__IsGeneratorFunction(this: *const Value) -> bool; + #[allow(dead_code)] + fn v8__Value__IsGeneratorObject(this: *const Value) -> bool; + fn v8__Value__IsPromise(this: *const Value) -> bool; + fn v8__Value__IsMap(this: *const Value) -> bool; + fn v8__Value__IsSet(this: *const Value) -> bool; + fn v8__Value__IsMapIterator(this: *const Value) -> bool; + fn v8__Value__IsSetIterator(this: *const Value) -> bool; + fn v8__Value__IsSetGeneratorObject(this: *const Value) -> bool; + fn v8__Value__IsWeakMap(this: *const Value) -> bool; + fn v8__Value__IsWeakSet(this: *const Value) -> bool; + fn v8__Value__IsWeakRef(this: *const Value) -> bool; + fn v8__Value__IsWasmNull(this: *const Value) -> bool; + fn v8__Value__IsArrayBuffer(this: *const Value) -> bool; + fn v8__Value__IsArrayBufferView(this: *const Value) -> bool; + fn v8__Value__IsTypedArray(this: *const Value) -> bool; + fn v8__Value__IsUint8Array(this: *const Value) -> bool; + fn v8__Value__IsUint8ClampedArray(this: *const Value) -> bool; + fn v8__Value__IsInt8Array(this: *const Value) -> bool; + fn v8__Value__IsUint16Array(this: *const Value) -> bool; + fn v8__Value__IsInt16Array(this: *const Value) -> bool; + fn v8__Value__IsUint32Array(this: *const Value) -> bool; + fn v8__Value__IsInt32Array(this: *const Value) -> bool; + fn v8__Value__IsFloat16Array(this: *const Value) -> bool; + fn v8__Value__IsFloat32Array(this: *const Value) -> bool; + fn v8__Value__IsFloat64Array(this: *const Value) -> bool; + fn v8__Value__IsBigInt64Array(this: *const Value) -> bool; + fn v8__Value__IsBigUint64Array(this: *const Value) -> bool; + fn v8__Value__IsDataView(this: *const Value) -> bool; + fn v8__Value__IsSharedArrayBuffer(this: *const Value) -> bool; + fn v8__Value__IsProxy(this: *const Value) -> bool; + fn v8__Value__IsWasmMemoryObject(this: *const Value) -> bool; + fn v8__Value__IsWasmModuleObject(this: *const Value) -> bool; + fn v8__Value__IsModuleNamespaceObject(this: *const Value) -> bool; + fn v8__Value__StrictEquals(this: *const Value, that: *const Value) -> bool; + fn v8__Value__SameValue(this: *const Value, that: *const Value) -> bool; + fn v8__Value__InstanceOf( + this: *const Value, + context: *const Context, + object: *const Object, + out: *mut Maybe, + ); + fn v8__Value__ToBigInt( + this: *const Value, + context: *const Context, + ) -> *const BigInt; + fn v8__Value__ToNumber( + this: *const Value, + context: *const Context, + ) -> *const Number; + fn v8__Value__ToString( + this: *const Value, + context: *const Context, + ) -> *const String; + fn v8__Value__ToDetailString( + this: *const Value, + context: *const Context, + ) -> *const String; + fn v8__Value__ToObject( + this: *const Value, + context: *const Context, + ) -> *const Object; + fn v8__Value__ToInteger( + this: *const Value, + context: *const Context, + ) -> *const Integer; + fn v8__Value__ToUint32( + this: *const Value, + context: *const Context, + ) -> *const Uint32; + fn v8__Value__ToInt32( + this: *const Value, + context: *const Context, + ) -> *const Int32; + fn v8__Value__ToBoolean( + this: *const Value, + isolate: *mut RealIsolate, + ) -> *const Boolean; + + fn v8__Value__NumberValue( + this: *const Value, + context: *const Context, + out: *mut Maybe, + ); + fn v8__Value__IntegerValue( + this: *const Value, + context: *const Context, + out: *mut Maybe, + ); + fn v8__Value__Uint32Value( + this: *const Value, + context: *const Context, + out: *mut Maybe, + ); + fn v8__Value__Int32Value( + this: *const Value, + context: *const Context, + out: *mut Maybe, + ); + fn v8__Value__BooleanValue( + this: *const Value, + isolate: *mut RealIsolate, + ) -> bool; + fn v8__Value__GetHash(this: *const Value) -> u32; + fn v8__Value__TypeOf( + this: *const Value, + isolate: *mut RealIsolate, + ) -> *const String; +} + +impl Value { + /// Returns true if this value is the undefined value. See ECMA-262 4.3.10. + #[inline(always)] + pub fn is_undefined(&self) -> bool { + unsafe { v8__Value__IsUndefined(self) } + } + + /// Returns true if this value is the null value. See ECMA-262 4.3.11. + #[inline(always)] + pub fn is_null(&self) -> bool { + unsafe { v8__Value__IsNull(self) } + } + + /// Returns true if this value is either the null or the undefined value. + /// See ECMA-262 4.3.11. and 4.3.12 + #[inline(always)] + pub fn is_null_or_undefined(&self) -> bool { + unsafe { v8__Value__IsNullOrUndefined(self) } + } + + /// Returns true if this value is true. + /// This is not the same as `BooleanValue()`. The latter performs a + /// conversion to boolean, i.e. the result of `Boolean(value)` in JS, whereas + /// this checks `value === true`. + #[inline(always)] + pub fn is_true(&self) -> bool { + unsafe { v8__Value__IsTrue(self) } + } + + /// Returns true if this value is false. + /// This is not the same as `!BooleanValue()`. The latter performs a + /// conversion to boolean, i.e. the result of `!Boolean(value)` in JS, whereas + /// this checks `value === false`. + #[inline(always)] + pub fn is_false(&self) -> bool { + unsafe { v8__Value__IsFalse(self) } + } + + /// Returns true if this value is a symbol or a string. + /// This is equivalent to + /// `typeof value === 'string' || typeof value === 'symbol'` in JS. + #[inline(always)] + pub fn is_name(&self) -> bool { + unsafe { v8__Value__IsName(self) } + } + + /// Returns true if this value is an instance of the String type. + /// See ECMA-262 8.4. + #[inline(always)] + pub fn is_string(&self) -> bool { + unsafe { v8__Value__IsString(self) } + } + + /// Returns true if this value is a symbol. + /// This is equivalent to `typeof value === 'symbol'` in JS. + #[inline(always)] + pub fn is_symbol(&self) -> bool { + unsafe { v8__Value__IsSymbol(self) } + } + + /// Returns true if this value is a function. + #[inline(always)] + pub fn is_function(&self) -> bool { + unsafe { v8__Value__IsFunction(self) } + } + + /// Returns true if this value is an array. Note that it will return false for + /// an Proxy for an array. + #[inline(always)] + pub fn is_array(&self) -> bool { + unsafe { v8__Value__IsArray(self) } + } + + /// Returns true if this value is an object. + #[inline(always)] + pub fn is_object(&self) -> bool { + unsafe { v8__Value__IsObject(self) } + } + + /// Returns true if this value is a bigint. + /// This is equivalent to `typeof value === 'bigint'` in JS. + #[inline(always)] + pub fn is_big_int(&self) -> bool { + unsafe { v8__Value__IsBigInt(self) } + } + + /// Returns true if this value is boolean. + /// This is equivalent to `typeof value === 'boolean'` in JS. + #[inline(always)] + pub fn is_boolean(&self) -> bool { + unsafe { v8__Value__IsBoolean(self) } + } + + /// Returns true if this value is a number. + #[inline(always)] + pub fn is_number(&self) -> bool { + unsafe { v8__Value__IsNumber(self) } + } + + /// Returns true if this value is an `External` object. + #[inline(always)] + pub fn is_external(&self) -> bool { + unsafe { v8__Value__IsExternal(self) } + } + + /// Returns true if this value is a 32-bit signed integer. + #[inline(always)] + pub fn is_int32(&self) -> bool { + unsafe { v8__Value__IsInt32(self) } + } + + /// Returns true if this value is a 32-bit unsigned integer. + #[inline(always)] + pub fn is_uint32(&self) -> bool { + unsafe { v8__Value__IsUint32(self) } + } + + /// Returns true if this value is a Date. + #[inline(always)] + pub fn is_date(&self) -> bool { + unsafe { v8__Value__IsDate(self) } + } + + /// Returns true if this value is an Arguments object. + #[inline(always)] + pub fn is_arguments_object(&self) -> bool { + unsafe { v8__Value__IsArgumentsObject(self) } + } + + /// Returns true if this value is a BigInt object. + #[inline(always)] + pub fn is_big_int_object(&self) -> bool { + unsafe { v8__Value__IsBigIntObject(self) } + } + + /// Returns true if this value is a Boolean object. + #[inline(always)] + pub fn is_boolean_object(&self) -> bool { + unsafe { v8__Value__IsBooleanObject(self) } + } + + /// Returns true if this value is a Number object. + #[inline(always)] + pub fn is_number_object(&self) -> bool { + unsafe { v8__Value__IsNumberObject(self) } + } + + /// Returns true if this value is a String object. + #[inline(always)] + pub fn is_string_object(&self) -> bool { + unsafe { v8__Value__IsStringObject(self) } + } + + /// Returns true if this value is a Symbol object. + #[inline(always)] + pub fn is_symbol_object(&self) -> bool { + unsafe { v8__Value__IsSymbolObject(self) } + } + + /// Returns true if this value is a NativeError. + #[inline(always)] + pub fn is_native_error(&self) -> bool { + unsafe { v8__Value__IsNativeError(self) } + } + + /// Returns true if this value is a RegExp. + #[inline(always)] + pub fn is_reg_exp(&self) -> bool { + unsafe { v8__Value__IsRegExp(self) } + } + + /// Returns true if this value is an async function. + #[inline(always)] + pub fn is_async_function(&self) -> bool { + unsafe { v8__Value__IsAsyncFunction(self) } + } + + /// Returns true if this value is a Generator function. + #[inline(always)] + pub fn is_generator_function(&self) -> bool { + unsafe { v8__Value__IsGeneratorFunction(self) } + } + + /// Returns true if this value is a Promise. + #[inline(always)] + pub fn is_promise(&self) -> bool { + unsafe { v8__Value__IsPromise(self) } + } + + /// Returns true if this value is a Map. + #[inline(always)] + pub fn is_map(&self) -> bool { + unsafe { v8__Value__IsMap(self) } + } + + /// Returns true if this value is a Set. + #[inline(always)] + pub fn is_set(&self) -> bool { + unsafe { v8__Value__IsSet(self) } + } + + /// Returns true if this value is a Map Iterator. + #[inline(always)] + pub fn is_map_iterator(&self) -> bool { + unsafe { v8__Value__IsMapIterator(self) } + } + + /// Returns true if this value is a Set Iterator. + #[inline(always)] + pub fn is_set_iterator(&self) -> bool { + unsafe { v8__Value__IsSetIterator(self) } + } + + /// Returns true if this value is a Generator Object. + #[inline(always)] + pub fn is_generator_object(&self) -> bool { + unsafe { v8__Value__IsSetGeneratorObject(self) } + } + + /// Returns true if this value is a WeakMap. + #[inline(always)] + pub fn is_weak_map(&self) -> bool { + unsafe { v8__Value__IsWeakMap(self) } + } + + /// Returns true if this value is a WeakSet. + #[inline(always)] + pub fn is_weak_set(&self) -> bool { + unsafe { v8__Value__IsWeakSet(self) } + } + + /// Returns true if this value is a WeakRef. + #[inline(always)] + pub fn is_weak_ref(&self) -> bool { + unsafe { v8__Value__IsWeakRef(self) } + } + + /// Returns true if this value is the WasmNull object. + #[inline(always)] + pub fn is_wasm_null(&self) -> bool { + unsafe { v8__Value__IsWasmNull(self) } + } + + /// Returns true if this value is an ArrayBuffer. + #[inline(always)] + pub fn is_array_buffer(&self) -> bool { + unsafe { v8__Value__IsArrayBuffer(self) } + } + + /// Returns true if this value is an ArrayBufferView. + #[inline(always)] + pub fn is_array_buffer_view(&self) -> bool { + unsafe { v8__Value__IsArrayBufferView(self) } + } + + /// Returns true if this value is one of TypedArrays. + #[inline(always)] + pub fn is_typed_array(&self) -> bool { + unsafe { v8__Value__IsTypedArray(self) } + } + + /// Returns true if this value is an Uint8Array. + #[inline(always)] + pub fn is_uint8_array(&self) -> bool { + unsafe { v8__Value__IsUint8Array(self) } + } + + /// Returns true if this value is an Uint8ClampedArray. + #[inline(always)] + pub fn is_uint8_clamped_array(&self) -> bool { + unsafe { v8__Value__IsUint8ClampedArray(self) } + } + + /// Returns true if this value is an Int8Array. + #[inline(always)] + pub fn is_int8_array(&self) -> bool { + unsafe { v8__Value__IsInt8Array(self) } + } + + /// Returns true if this value is an Uint16Array. + #[inline(always)] + pub fn is_uint16_array(&self) -> bool { + unsafe { v8__Value__IsUint16Array(self) } + } + + /// Returns true if this value is an Int16Array. + #[inline(always)] + pub fn is_int16_array(&self) -> bool { + unsafe { v8__Value__IsInt16Array(self) } + } + + /// Returns true if this value is an Uint32Array. + #[inline(always)] + pub fn is_uint32_array(&self) -> bool { + unsafe { v8__Value__IsUint32Array(self) } + } + + /// Returns true if this value is an Int32Array. + #[inline(always)] + pub fn is_int32_array(&self) -> bool { + unsafe { v8__Value__IsInt32Array(self) } + } + + /// Returns true if this value is a Float16Array. + #[inline(always)] + pub fn is_float16_array(&self) -> bool { + unsafe { v8__Value__IsFloat16Array(self) } + } + + /// Returns true if this value is a Float32Array. + #[inline(always)] + pub fn is_float32_array(&self) -> bool { + unsafe { v8__Value__IsFloat32Array(self) } + } + + /// Returns true if this value is a Float64Array. + #[inline(always)] + pub fn is_float64_array(&self) -> bool { + unsafe { v8__Value__IsFloat64Array(self) } + } + + /// Returns true if this value is a BigInt64Array. + #[inline(always)] + pub fn is_big_int64_array(&self) -> bool { + unsafe { v8__Value__IsBigInt64Array(self) } + } + + /// Returns true if this value is a BigUint64Array. + #[inline(always)] + pub fn is_big_uint64_array(&self) -> bool { + unsafe { v8__Value__IsBigUint64Array(self) } + } + + /// Returns true if this value is a DataView. + #[inline(always)] + pub fn is_data_view(&self) -> bool { + unsafe { v8__Value__IsDataView(self) } + } + + /// Returns true if this value is a SharedArrayBuffer. + /// This is an experimental feature. + #[inline(always)] + pub fn is_shared_array_buffer(&self) -> bool { + unsafe { v8__Value__IsSharedArrayBuffer(self) } + } + + /// Returns true if this value is a JavaScript Proxy. + #[inline(always)] + pub fn is_proxy(&self) -> bool { + unsafe { v8__Value__IsProxy(self) } + } + + /// Returns true if this value is a WasmMemoryObject. + #[inline(always)] + pub fn is_wasm_memory_object(&self) -> bool { + unsafe { v8__Value__IsWasmMemoryObject(self) } + } + + /// Returns true if this value is a WasmModuleObject. + #[inline(always)] + pub fn is_wasm_module_object(&self) -> bool { + unsafe { v8__Value__IsWasmModuleObject(self) } + } + + /// Returns true if the value is a Module Namespace Object. + #[inline(always)] + pub fn is_module_namespace_object(&self) -> bool { + unsafe { v8__Value__IsModuleNamespaceObject(self) } + } + + #[inline(always)] + pub fn strict_equals(&self, that: Local) -> bool { + unsafe { v8__Value__StrictEquals(self, &*that) } + } + + #[inline(always)] + pub fn same_value(&self, that: Local) -> bool { + unsafe { v8__Value__SameValue(self, &*that) } + } + + /// Implements the the abstract operation `SameValueZero`, which is defined in + /// ECMA-262 6th edition § 7.2.10 + /// (http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero). + /// + /// This operation is used to compare values for the purpose of insertion into + /// a `Set`, or determining whether `Map` keys are equivalent. Its semantics + /// are almost the same as `strict_equals()` and `same_value()`, with the + /// following important distinctions: + /// - It considers `NaN` equal to `NaN` (unlike `strict_equals()`). + /// - It considers `-0` equal to `0` (unlike `same_value()`). + #[inline(always)] + pub fn same_value_zero(&self, that: Local) -> bool { + // The SMI representation of zero is also zero. In debug builds, double + // check this, so in the unlikely event that V8 changes its internal + // representation of SMIs such that this invariant no longer holds, we'd + // catch it. + self.same_value(that) || { + let zero = Integer::zero().into(); + self.strict_equals(zero) && that.strict_equals(zero) + } + } + + #[inline(always)] + pub fn to_big_int<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope + .cast_local(|sd| v8__Value__ToBigInt(self, &*sd.get_current_context())) + } + } + + #[inline(always)] + pub fn to_number<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope + .cast_local(|sd| v8__Value__ToNumber(self, &*sd.get_current_context())) + } + } + + #[inline(always)] + pub fn to_string<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope + .cast_local(|sd| v8__Value__ToString(self, &*sd.get_current_context())) + } + } + + /// Convenience function not present in the original V8 API. + #[inline(always)] + pub fn to_rust_string_lossy( + &self, + scope: &PinScope<'_, '_>, + ) -> std::string::String { + self + .to_string(scope) + .map_or_else(std::string::String::new, |s| s.to_rust_string_lossy(scope)) + } + + #[inline(always)] + pub fn to_detail_string<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__Value__ToDetailString(self, &*sd.get_current_context()) + }) + } + } + + #[inline(always)] + pub fn to_object<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope + .cast_local(|sd| v8__Value__ToObject(self, &*sd.get_current_context())) + } + } + + #[inline(always)] + pub fn to_integer<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope + .cast_local(|sd| v8__Value__ToInteger(self, &*sd.get_current_context())) + } + } + + #[inline(always)] + pub fn to_uint32<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope + .cast_local(|sd| v8__Value__ToUint32(self, &*sd.get_current_context())) + } + } + + #[inline(always)] + pub fn to_int32<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Option> { + unsafe { + scope + .cast_local(|sd| v8__Value__ToInt32(self, &*sd.get_current_context())) + } + } + + /// Perform the equivalent of Boolean(value) in JS. This can never fail. + #[inline(always)] + pub fn to_boolean<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, Boolean> { + unsafe { + scope.cast_local(|sd| v8__Value__ToBoolean(self, sd.get_isolate_ptr())) + } + .unwrap() + } + + #[inline(always)] + pub fn instance_of( + &self, + scope: &PinScope<'_, '_>, + object: Local, + ) -> Option { + let mut out = Maybe::::default(); + unsafe { + v8__Value__InstanceOf( + self, + &*scope.get_current_context(), + &*object, + &mut out, + ); + } + out.into() + } + + #[inline(always)] + pub fn number_value(&self, scope: &PinScope<'_, '_>) -> Option { + let mut out = Maybe::::default(); + unsafe { + v8__Value__NumberValue(self, &*scope.get_current_context(), &mut out); + }; + out.into() + } + + #[inline(always)] + pub fn integer_value(&self, scope: &PinScope<'_, '_>) -> Option { + let mut out = Maybe::::default(); + unsafe { + v8__Value__IntegerValue(self, &*scope.get_current_context(), &mut out); + }; + out.into() + } + + #[inline(always)] + pub fn uint32_value(&self, scope: &PinScope<'_, '_>) -> Option { + let mut out = Maybe::::default(); + unsafe { + v8__Value__Uint32Value(self, &*scope.get_current_context(), &mut out); + }; + out.into() + } + + #[inline(always)] + pub fn int32_value(&self, scope: &PinScope<'_, '_>) -> Option { + let mut out = Maybe::::default(); + unsafe { + v8__Value__Int32Value(self, &*scope.get_current_context(), &mut out); + }; + out.into() + } + + #[inline(always)] + pub fn boolean_value(&self, scope: &PinScope<'_, '_>) -> bool { + unsafe { v8__Value__BooleanValue(self, scope.get_isolate_ptr()) } + } + + /// Get the hash of this value. The hash is not guaranteed to be + /// unique. For |Object| and |Name| instances the result is equal to + /// |GetIdentityHash|. Hashes are not guaranteed to be stable across + /// different isolates or processes. + #[inline(always)] + pub fn get_hash(&self) -> u32 { + unsafe { v8__Value__GetHash(self) } + } + + #[inline(always)] + pub fn type_of<'s>(&self, scope: &PinScope<'s, '_>) -> Local<'s, String> { + unsafe { + scope.cast_local(|sd| v8__Value__TypeOf(self, sd.get_isolate_ptr())) + } + .unwrap() + } + + /// Utility method that returns human readable representation of the + /// underlying value. + pub fn type_repr(&self) -> &'static str { + if self.is_module_namespace_object() { + "Module" + } else if self.is_wasm_module_object() { + "WASM module" + } else if self.is_wasm_memory_object() { + "WASM memory object" + } else if self.is_proxy() { + "Proxy" + } else if self.is_shared_array_buffer() { + "SharedArrayBuffer" + } else if self.is_data_view() { + "DataView" + } else if self.is_big_uint64_array() { + "BigUint64Array" + } else if self.is_big_int64_array() { + "BigInt64Array" + } else if self.is_float64_array() { + "Float64Array" + } else if self.is_float32_array() { + "Float32Array" + } else if self.is_int32_array() { + "Int32Array" + } else if self.is_uint32_array() { + "Uint32Array" + } else if self.is_int16_array() { + "Int16Array" + } else if self.is_uint16_array() { + "Uint16Array" + } else if self.is_int8_array() { + "Int8Array" + } else if self.is_uint8_clamped_array() { + "Uint8ClampedArray" + } else if self.is_uint8_array() { + "Uint8Array" + } else if self.is_typed_array() { + "TypedArray" + } else if self.is_array_buffer_view() { + "ArrayBufferView" + } else if self.is_array_buffer() { + "ArrayBuffer" + } else if self.is_weak_set() { + "WeakSet" + } else if self.is_weak_map() { + "WeakMap" + } else if self.is_set_iterator() { + "Set Iterator" + } else if self.is_map_iterator() { + "Map Iterator" + } else if self.is_set() { + "Set" + } else if self.is_map() { + "Map" + } else if self.is_promise() { + "Promise" + } else if self.is_generator_function() { + "Generator function" + } else if self.is_async_function() { + "Async function" + } else if self.is_reg_exp() { + "RegExp" + } else if self.is_date() { + "Date" + } else if self.is_number() { + "Number" + } else if self.is_boolean() { + "Boolean" + } else if self.is_big_int() { + "bigint" + } else if self.is_array() { + "array" + } else if self.is_function() { + "function" + } else if self.is_symbol() { + "symbol" + } else if self.is_string() { + "string" + } else if self.is_null() { + "null" + } else if self.is_undefined() { + "undefined" + } else { + "unknown" + } + } +} diff --git a/crates/v8/src/value_deserializer.rs b/crates/v8/src/value_deserializer.rs new file mode 100644 index 0000000000..69437c62e9 --- /dev/null +++ b/crates/v8/src/value_deserializer.rs @@ -0,0 +1,521 @@ +use crate::ArrayBuffer; +use crate::Context; +use crate::Exception; +use crate::Global; +use crate::Isolate; +use crate::Local; +use crate::Object; +use crate::PinScope; +use crate::SharedArrayBuffer; +use crate::String; +use crate::Value; +use crate::WasmModuleObject; +use crate::isolate::RealIsolate; +use crate::scope::CallbackScope; +use crate::scope::ContextScope; +use crate::scope::GetIsolate; + +use crate::support::CxxVTable; +use crate::support::FieldOffset; +use crate::support::MaybeBool; + +use std::ffi::c_void; +use std::mem::MaybeUninit; +use std::pin::Pin; +use std::pin::pin; +use std::ptr::addr_of; + +// Must be == sizeof(v8::ValueDeserializer::Delegate), +// see v8__ValueDeserializer__Delegate__CONSTRUCT(). +#[repr(C)] +pub struct CxxValueDeserializerDelegate { + _cxx_vtable: CxxVTable, +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__ValueDeserializer__Delegate__ReadHostObject( + this: &CxxValueDeserializerDelegate, + isolate: *mut RealIsolate, +) -> *const Object { + let value_deserializer_heap = + unsafe { ValueDeserializerHeap::dispatch(this) }; + let mut isolate = unsafe { Isolate::from_raw_ptr(isolate) }; + let scope = unsafe { CallbackScope::new(&mut isolate) }; + let scope = pin!(scope); + let scope = &mut scope.init(); + let context = Local::new(scope, &value_deserializer_heap.context); + let mut scope = { ContextScope::new(scope, context) }; + + match value_deserializer_heap + .value_deserializer_impl + .read_host_object( + &mut scope, + &value_deserializer_heap.cxx_value_deserializer, + ) { + None => std::ptr::null(), + Some(x) => x.as_non_null().as_ptr(), + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__ValueDeserializer__Delegate__GetSharedArrayBufferFromId( + this: &CxxValueDeserializerDelegate, + isolate: *mut RealIsolate, + transfer_id: u32, +) -> *const SharedArrayBuffer { + let value_deserializer_heap = + unsafe { ValueDeserializerHeap::dispatch(this) }; + let mut isolate = unsafe { Isolate::from_raw_ptr(isolate) }; + let scope = unsafe { CallbackScope::new(&mut isolate) }; + let scope = pin!(scope); + let scope = &mut scope.init(); + // let hs = scope; + let context = Local::new(scope, &value_deserializer_heap.context); + let mut scope = { ContextScope::new(scope, context) }; + + match value_deserializer_heap + .value_deserializer_impl + .get_shared_array_buffer_from_id(&mut scope, transfer_id) + { + None => std::ptr::null(), + Some(x) => x.as_non_null().as_ptr(), + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__ValueDeserializer__Delegate__GetWasmModuleFromId( + this: &mut CxxValueDeserializerDelegate, + isolate: *mut RealIsolate, + clone_id: u32, +) -> *const WasmModuleObject { + let value_deserializer_heap = + unsafe { ValueDeserializerHeap::dispatch(this) }; + let mut isolate = unsafe { Isolate::from_raw_ptr(isolate) }; + let scope = unsafe { CallbackScope::new(&mut isolate) }; + let scope = pin!(scope); + let scope = &mut scope.init(); + let context = Local::new(scope, &value_deserializer_heap.context); + let mut scope = { ContextScope::new(scope, context) }; + + match value_deserializer_heap + .value_deserializer_impl + .get_wasm_module_from_id(&mut scope, clone_id) + { + None => std::ptr::null(), + Some(x) => x.as_non_null().as_ptr(), + } +} + +unsafe extern "C" { + fn v8__ValueDeserializer__Delegate__CONSTRUCT( + buf: *mut MaybeUninit, + ); +} + +// Must be == sizeof(v8::ValueDeserializer), +// see v8__ValueDeserializer__CONSTRUCT(). +#[repr(C)] +pub struct CxxValueDeserializer { + _cxx_vtable: CxxVTable, +} + +unsafe extern "C" { + fn v8__ValueDeserializer__CONSTRUCT( + buf: *mut MaybeUninit, + isolate: *mut RealIsolate, + data: *const u8, + size: usize, + delegate: *mut CxxValueDeserializerDelegate, + ); + + fn v8__ValueDeserializer__DESTRUCT(this: *mut CxxValueDeserializer); + + fn v8__ValueDeserializer__TransferArrayBuffer( + this: *mut CxxValueDeserializer, + transfer_id: u32, + array_buffer: Local, + ); + + fn v8__ValueDeserializer__TransferSharedArrayBuffer( + this: *mut CxxValueDeserializer, + transfer_id: u32, + array_buffer: Local, + ); + + fn v8__ValueDeserializer__SetSupportsLegacyWireFormat( + this: *mut CxxValueDeserializer, + supports_legacy_wire_format: bool, + ); + + fn v8__ValueDeserializer__ReadHeader( + this: *mut CxxValueDeserializer, + context: Local, + ) -> MaybeBool; + + fn v8__ValueDeserializer__ReadValue( + this: *mut CxxValueDeserializer, + context: Local, + ) -> *const Value; + + fn v8__ValueDeserializer__ReadUint32( + this: *mut CxxValueDeserializer, + value: *mut u32, + ) -> bool; + + fn v8__ValueDeserializer__ReadUint64( + this: *mut CxxValueDeserializer, + value: *mut u64, + ) -> bool; + + fn v8__ValueDeserializer__ReadDouble( + this: *mut CxxValueDeserializer, + value: *mut f64, + ) -> bool; + + fn v8__ValueDeserializer__ReadRawBytes( + this: *mut CxxValueDeserializer, + length: usize, + data: *mut *const c_void, + ) -> bool; + + fn v8__ValueDeserializer__GetWireFormatVersion( + this: *mut CxxValueDeserializer, + ) -> u32; +} + +/// The ValueDeserializerImpl trait allows for +/// custom callback functions used by v8. +pub trait ValueDeserializerImpl { + fn read_host_object<'s>( + &self, + scope: &mut PinScope<'s, '_>, + _value_deserializer: &dyn ValueDeserializerHelper, + ) -> Option> { + let msg = + String::new(scope, "Deno deserializer: read_host_object not implemented") + .unwrap(); + let exc = Exception::error(scope, msg); + scope.throw_exception(exc); + None + } + + fn get_shared_array_buffer_from_id<'s>( + &self, + scope: &mut PinScope<'s, '_>, + _transfer_id: u32, + ) -> Option> { + let msg = String::new( + scope, + "Deno deserializer: get_shared_array_buffer_from_id not implemented", + ) + .unwrap(); + let exc = Exception::error(scope, msg); + scope.throw_exception(exc); + None + } + + fn get_wasm_module_from_id<'s>( + &self, + scope: &mut PinScope<'s, '_>, + _clone_id: u32, + ) -> Option> { + let msg = String::new( + scope, + "Deno deserializer: get_wasm_module_from_id not implemented", + ) + .unwrap(); + let exc = Exception::error(scope, msg); + scope.throw_exception(exc); + None + } +} + +fn cast_to_ptr(t: &T) -> *mut T { + t as *const _ as *mut _ +} + +/// The ValueDeserializerHeap object contains all objects related to a +/// deserializer. This object has to be pinned to the heap because of the Cpp +/// pointers that have to remain valid. Moving this object would result in the +/// Cpp pointer to the delegate to become invalid and thus causing the delegate +/// callback to fail. Additionally the deserializer and implementation are also +/// pinned in memory because these have to be accessable from within the +/// delegate callback methods. +pub struct ValueDeserializerHeap<'a> { + value_deserializer_impl: Box, + cxx_value_deserializer: CxxValueDeserializer, + cxx_value_deserializer_delegate: CxxValueDeserializerDelegate, + context: Global, +} + +impl ValueDeserializerHeap<'_> { + fn get_cxx_value_deserializer_delegate_offset() + -> FieldOffset { + let buf = std::mem::MaybeUninit::::uninit(); + let delegate = + unsafe { addr_of!((*buf.as_ptr()).cxx_value_deserializer_delegate) }; + FieldOffset::from_ptrs(buf.as_ptr(), delegate) + } + + fn get_cxx_value_deserializer_offset() -> FieldOffset { + let buf = std::mem::MaybeUninit::::uninit(); + let deserializer = + unsafe { addr_of!((*buf.as_ptr()).cxx_value_deserializer) }; + FieldOffset::from_ptrs(buf.as_ptr(), deserializer) + } + + /// Panics unless the `Local`s of the scopes open on this thread belong to + /// the isolate of the deserializer. Every `CxxValueDeserializer` is embedded + /// in a `ValueDeserializerHeap`. + fn assert_locals_in_isolate_of( + cxx_value_deserializer: &CxxValueDeserializer, + ) { + let heap: &ValueDeserializerHeap = unsafe { + Self::get_cxx_value_deserializer_offset() + .to_embedder(cxx_value_deserializer) + }; + heap.context.assert_locals_in_same_isolate(); + } + + /// Starting from 'this' pointer a ValueDeserializerHeap ref can be created + pub unsafe fn dispatch( + value_serializer_delegate: &CxxValueDeserializerDelegate, + ) -> &Self { + unsafe { + Self::get_cxx_value_deserializer_delegate_offset() + .to_embedder::(value_serializer_delegate) + } + } +} + +impl Drop for ValueDeserializerHeap<'_> { + fn drop(&mut self) { + unsafe { + v8__ValueDeserializer__DESTRUCT(&mut self.cxx_value_deserializer); + }; + } +} + +/// Trait used for direct read from the deserialization buffer. +/// Mostly used by the read_host_object callback function in the +/// ValueDeserializerImpl trait to create custom deserialization logic. +pub trait ValueDeserializerHelper { + fn get_cxx_value_deserializer(&self) -> &CxxValueDeserializer; + + fn read_header(&self, context: Local) -> Option { + ValueDeserializerHeap::assert_locals_in_isolate_of( + self.get_cxx_value_deserializer(), + ); + unsafe { + v8__ValueDeserializer__ReadHeader( + cast_to_ptr(self.get_cxx_value_deserializer()), + context, + ) + } + .into() + } + + fn read_value<'s>( + &self, + context: Local<'s, Context>, + ) -> Option> { + ValueDeserializerHeap::assert_locals_in_isolate_of( + self.get_cxx_value_deserializer(), + ); + unsafe { + Local::from_raw(v8__ValueDeserializer__ReadValue( + cast_to_ptr(self.get_cxx_value_deserializer()), + context, + )) + } + } + + fn read_uint32(&self, value: &mut u32) -> bool { + unsafe { + v8__ValueDeserializer__ReadUint32( + cast_to_ptr(self.get_cxx_value_deserializer()), + value, + ) + } + } + + fn read_uint64(&self, value: &mut u64) -> bool { + unsafe { + v8__ValueDeserializer__ReadUint64( + cast_to_ptr(self.get_cxx_value_deserializer()), + value, + ) + } + } + + fn read_double(&self, value: &mut f64) -> bool { + unsafe { + v8__ValueDeserializer__ReadDouble( + cast_to_ptr(self.get_cxx_value_deserializer()), + value, + ) + } + } + + fn read_raw_bytes(&self, length: usize) -> Option<&[u8]> { + let mut data: *const c_void = std::ptr::null_mut(); + let ok = unsafe { + v8__ValueDeserializer__ReadRawBytes( + cast_to_ptr(self.get_cxx_value_deserializer()), + length, + &mut data, + ) + }; + if ok { + assert!(!data.is_null()); + unsafe { Some(std::slice::from_raw_parts(data as *const u8, length)) } + } else { + None + } + } + + fn transfer_array_buffer( + &self, + transfer_id: u32, + array_buffer: Local, + ) { + ValueDeserializerHeap::assert_locals_in_isolate_of( + self.get_cxx_value_deserializer(), + ); + unsafe { + v8__ValueDeserializer__TransferArrayBuffer( + cast_to_ptr(self.get_cxx_value_deserializer()), + transfer_id, + array_buffer, + ); + }; + } + + fn transfer_shared_array_buffer( + &self, + transfer_id: u32, + shared_array_buffer: Local, + ) { + ValueDeserializerHeap::assert_locals_in_isolate_of( + self.get_cxx_value_deserializer(), + ); + unsafe { + v8__ValueDeserializer__TransferSharedArrayBuffer( + cast_to_ptr(self.get_cxx_value_deserializer()), + transfer_id, + shared_array_buffer, + ); + }; + } + + fn get_wire_format_version(&self) -> u32 { + unsafe { + v8__ValueDeserializer__GetWireFormatVersion(cast_to_ptr( + self.get_cxx_value_deserializer(), + )) + } + } +} + +impl ValueDeserializerHelper for CxxValueDeserializer { + fn get_cxx_value_deserializer(&self) -> &CxxValueDeserializer { + self + } +} + +impl ValueDeserializerHelper for ValueDeserializerHeap<'_> { + fn get_cxx_value_deserializer(&self) -> &CxxValueDeserializer { + &self.cxx_value_deserializer + } +} + +impl ValueDeserializerHelper for ValueDeserializer<'_> { + fn get_cxx_value_deserializer(&self) -> &CxxValueDeserializer { + &self.value_deserializer_heap.cxx_value_deserializer + } +} + +/// ValueDeserializer is a stack object used as entry-point for an owned and +/// pinned heap object ValueDeserializerHeap. +/// The 'a lifetime is the lifetime of the ValueDeserializerImpl implementation. +/// The 's lifetime is the lifetime of the HandleScope which is used to retrieve +/// a Local<'s, Context> for the CallbackScopes +pub struct ValueDeserializer<'a> { + value_deserializer_heap: Pin>>, + // ValueDeserializerHeap is already !Send and !Sync + // but this is just making it explicit + _phantom: std::marker::PhantomData<*mut ()>, +} + +impl<'a> ValueDeserializer<'a> { + pub fn new<'s, 'i, D: ValueDeserializerImpl + 'a>( + scope: &PinScope<'s, 'i>, + value_deserializer_impl: Box, + data: &[u8], + ) -> Self { + let context = scope.get_current_context(); + // create dummy ValueDeserializerHeap and move to heap + pin to address + let value_deserializer_heap_ptr = + Box::into_raw(Box::new(ValueDeserializerHeap { + value_deserializer_impl, + cxx_value_deserializer: CxxValueDeserializer { + _cxx_vtable: CxxVTable(std::ptr::null()), + }, + cxx_value_deserializer_delegate: CxxValueDeserializerDelegate { + _cxx_vtable: CxxVTable(std::ptr::null()), + }, + context: Global::new(scope, context), + })); + + unsafe { + let delegate_ptr = std::ptr::addr_of_mut!( + (*value_deserializer_heap_ptr).cxx_value_deserializer_delegate + ); + let deserializer_ptr = std::ptr::addr_of_mut!( + (*value_deserializer_heap_ptr).cxx_value_deserializer + ); + v8__ValueDeserializer__Delegate__CONSTRUCT( + delegate_ptr + .cast::>(), + ); + + v8__ValueDeserializer__CONSTRUCT( + deserializer_ptr.cast::>(), + scope.get_isolate_ptr(), + data.as_ptr(), + data.len(), + delegate_ptr, + ); + }; + + // SAFETY: pointer from Box::into_raw is valid + let value_deserializer_heap = + Pin::new(unsafe { Box::from_raw(value_deserializer_heap_ptr) }); + + ValueDeserializer { + value_deserializer_heap, + _phantom: std::marker::PhantomData, + } + } +} + +impl ValueDeserializer<'_> { + pub fn set_supports_legacy_wire_format( + &self, + supports_legacy_wire_format: bool, + ) { + unsafe { + v8__ValueDeserializer__SetSupportsLegacyWireFormat( + cast_to_ptr(&self.value_deserializer_heap.cxx_value_deserializer), + supports_legacy_wire_format, + ); + } + } + + pub fn read_value<'t>( + &self, + context: Local<'t, Context>, + ) -> Option> { + self.value_deserializer_heap.read_value(context) + } +} diff --git a/crates/v8/src/value_serializer.rs b/crates/v8/src/value_serializer.rs new file mode 100644 index 0000000000..76c72d4349 --- /dev/null +++ b/crates/v8/src/value_serializer.rs @@ -0,0 +1,608 @@ +use crate::ArrayBuffer; +use crate::Context; +use crate::Exception; +use crate::Global; +use crate::Isolate; +use crate::Local; +use crate::Object; +use crate::PinScope; +use crate::SharedArrayBuffer; +use crate::String; +use crate::Value; +use crate::WasmModuleObject; +use crate::isolate::RealIsolate; +use crate::scope::CallbackScope; +use crate::scope::ContextScope; +use crate::scope::GetIsolate; + +use std::alloc::Layout; +use std::alloc::alloc; +use std::alloc::dealloc; +use std::alloc::realloc; +use std::mem::MaybeUninit; +use std::pin::pin; +use std::ptr::addr_of; +use std::sync::atomic::AtomicUsize; + +use crate::support::CxxVTable; +use crate::support::FieldOffset; +use crate::support::MaybeBool; + +use std::ffi::c_void; +use std::pin::Pin; + +// Must be == sizeof(v8::ValueSerializer::Delegate), +// see v8__ValueSerializer__Delegate__CONSTRUCT(). +#[repr(C)] +pub struct CxxValueSerializerDelegate { + _cxx_vtable: CxxVTable, +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__ValueSerializer__Delegate__ThrowDataCloneError( + this: &CxxValueSerializerDelegate, + message: Local, +) { + let value_serializer_heap = unsafe { ValueSerializerHeap::dispatch(this) }; + let mut isolate = + unsafe { Isolate::from_raw_ptr(value_serializer_heap.isolate_ptr) }; + let scope = unsafe { CallbackScope::new(&mut isolate) }; + let scope = pin!(scope); + let scope = &mut scope.init(); + let context = Local::new(scope, &value_serializer_heap.context); + let mut scope = ContextScope::new(scope, context); + value_serializer_heap + .value_serializer_impl + .throw_data_clone_error(&mut scope, message); +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__ValueSerializer__Delegate__HasCustomHostObject( + this: &CxxValueSerializerDelegate, + isolate: *mut RealIsolate, +) -> bool { + let value_serializer_heap = unsafe { ValueSerializerHeap::dispatch(this) }; + let isolate = unsafe { Isolate::from_raw_ptr(isolate) }; + value_serializer_heap + .value_serializer_impl + .has_custom_host_object(&isolate) +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__ValueSerializer__Delegate__IsHostObject( + this: &CxxValueSerializerDelegate, + isolate: *mut RealIsolate, + object: Local, +) -> MaybeBool { + let value_serializer_heap = unsafe { ValueSerializerHeap::dispatch(this) }; + let mut isolate = unsafe { Isolate::from_raw_ptr(isolate) }; + let scope = unsafe { CallbackScope::new(&mut isolate) }; + let scope = pin!(scope); + let scope = &mut scope.init(); + let context = Local::new(scope, &value_serializer_heap.context); + let mut scope = ContextScope::new(scope, context); + + MaybeBool::from( + value_serializer_heap + .value_serializer_impl + .is_host_object(&mut scope, object), + ) +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__ValueSerializer__Delegate__WriteHostObject( + this: &CxxValueSerializerDelegate, + isolate: *mut RealIsolate, + object: Local, +) -> MaybeBool { + let value_serializer_heap = unsafe { ValueSerializerHeap::dispatch(this) }; + let mut isolate = unsafe { Isolate::from_raw_ptr(isolate) }; + let scope = unsafe { CallbackScope::new(&mut isolate) }; + let scope = pin!(scope); + let scope = &mut scope.init(); + let context = Local::new(scope, &value_serializer_heap.context); + let mut scope = ContextScope::new(scope, context); + let value_serializer_impl = + value_serializer_heap.value_serializer_impl.as_ref(); + MaybeBool::from(value_serializer_impl.write_host_object( + &mut scope, + object, + &value_serializer_heap.cxx_value_serializer, + )) +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__ValueSerializer__Delegate__GetSharedArrayBufferId( + this: &CxxValueSerializerDelegate, + isolate: *mut RealIsolate, + shared_array_buffer: Local, + clone_id: *mut u32, +) -> bool { + let value_serializer_heap = unsafe { ValueSerializerHeap::dispatch(this) }; + let mut isolate = unsafe { Isolate::from_raw_ptr(isolate) }; + let scope = unsafe { CallbackScope::new(&mut isolate) }; + let scope = pin!(scope); + let scope = &mut scope.init(); + let context = Local::new(scope, &value_serializer_heap.context); + let mut scope = ContextScope::new(scope, context); + match value_serializer_heap + .value_serializer_impl + .get_shared_array_buffer_id(&mut scope, shared_array_buffer) + { + Some(x) => { + unsafe { + *clone_id = x; + } + true + } + None => false, + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__ValueSerializer__Delegate__GetWasmModuleTransferId( + this: &CxxValueSerializerDelegate, + isolate: *mut RealIsolate, + module: Local, + transfer_id: *mut u32, +) -> bool { + let value_serializer_heap = unsafe { ValueSerializerHeap::dispatch(this) }; + let mut isolate = unsafe { Isolate::from_raw_ptr(isolate) }; + let scope = unsafe { CallbackScope::new(&mut isolate) }; + let scope = pin!(scope); + let scope = &mut scope.init(); + let context = Local::new(scope, &value_serializer_heap.context); + let scope = &mut ContextScope::new(scope, context); + match value_serializer_heap + .value_serializer_impl + .get_wasm_module_transfer_id(scope, module) + { + Some(x) => { + unsafe { + *transfer_id = x; + } + true + } + None => false, + } +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__ValueSerializer__Delegate__ReallocateBufferMemory( + this: &CxxValueSerializerDelegate, + old_buffer: *mut c_void, + size: usize, + actual_size: *mut usize, +) -> *mut c_void { + let base = unsafe { ValueSerializerHeap::dispatch(this) }; + + let buffer_size = base + .buffer_size + .swap(size, std::sync::atomic::Ordering::Release); + let new_buffer = if old_buffer.is_null() { + let layout = Layout::from_size_align(size, 1).unwrap(); + unsafe { alloc(layout) } + } else { + let old_layout = Layout::from_size_align(buffer_size, 1).unwrap(); + unsafe { realloc(old_buffer as *mut _, old_layout, size) } + }; + + unsafe { + *actual_size = size; + } + new_buffer as *mut c_void +} + +#[unsafe(no_mangle)] +unsafe extern "C" fn v8__ValueSerializer__Delegate__FreeBufferMemory( + this: &mut CxxValueSerializerDelegate, + buffer: *mut c_void, +) { + let base = unsafe { ValueSerializerHeap::dispatch(this) }; + if !buffer.is_null() { + let layout = Layout::from_size_align( + base.buffer_size.load(std::sync::atomic::Ordering::Relaxed), + 1, + ) + .unwrap(); + unsafe { dealloc(buffer as *mut _, layout) }; + }; +} + +unsafe extern "C" { + fn v8__ValueSerializer__Delegate__CONSTRUCT( + buf: *mut MaybeUninit, + ); +} + +// Must be == sizeof(v8::ValueSerializer), see v8__ValueSerializer__CONSTRUCT(). +#[repr(C)] +pub struct CxxValueSerializer { + _cxx_vtable: CxxVTable, +} + +unsafe extern "C" { + fn v8__ValueSerializer__CONSTRUCT( + buf: *mut MaybeUninit, + isolate: *mut RealIsolate, + delegate: *mut CxxValueSerializerDelegate, + ); + + fn v8__ValueSerializer__DESTRUCT(this: *mut CxxValueSerializer); + + fn v8__ValueSerializer__Release( + this: *mut CxxValueSerializer, + ptr: *mut *mut u8, + size: *mut usize, + ); + + fn v8__ValueSerializer__TransferArrayBuffer( + this: *mut CxxValueSerializer, + transfer_id: u32, + array_buffer: Local, + ); + + fn v8__ValueSerializer__WriteHeader(this: *mut CxxValueSerializer); + fn v8__ValueSerializer__WriteValue( + this: *mut CxxValueSerializer, + context: Local, + value: Local, + ) -> MaybeBool; + fn v8__ValueSerializer__WriteUint32( + this: *mut CxxValueSerializer, + value: u32, + ); + fn v8__ValueSerializer__WriteUint64( + this: *mut CxxValueSerializer, + value: u64, + ); + fn v8__ValueSerializer__WriteDouble( + this: *mut CxxValueSerializer, + value: f64, + ); + fn v8__ValueSerializer__WriteRawBytes( + this: *mut CxxValueSerializer, + source: *const c_void, + length: usize, + ); + fn v8__ValueSerializer__SetTreatArrayBufferViewsAsHostObjects( + this: *mut CxxValueSerializer, + mode: bool, + ); +} + +/// The ValueSerializerImpl trait allows for +/// custom callback functions used by v8. +pub trait ValueSerializerImpl { + fn throw_data_clone_error<'s>( + &self, + scope: &mut PinScope<'s, '_>, + message: Local<'s, String>, + ); + + fn has_custom_host_object(&self, _isolate: &Isolate) -> bool { + false + } + + fn is_host_object<'s>( + &self, + scope: &mut PinScope<'s, '_>, + _object: Local<'s, Object>, + ) -> Option { + let msg = + String::new(scope, "Deno serializer: is_host_object not implemented") + .unwrap(); + let exc = Exception::error(scope, msg); + scope.throw_exception(exc); + None + } + + fn write_host_object<'s>( + &self, + scope: &mut PinScope<'s, '_>, + _object: Local<'s, Object>, + _value_serializer: &dyn ValueSerializerHelper, + ) -> Option { + let msg = + String::new(scope, "Deno serializer: write_host_object not implemented") + .unwrap(); + let exc = Exception::error(scope, msg); + scope.throw_exception(exc); + None + } + + fn get_shared_array_buffer_id<'s>( + &self, + _scope: &mut PinScope<'s, '_>, + _shared_array_buffer: Local<'s, SharedArrayBuffer>, + ) -> Option { + None + } + + fn get_wasm_module_transfer_id( + &self, + scope: &mut PinScope<'_, '_>, + _module: Local, + ) -> Option { + let msg = String::new( + scope, + "Deno serializer: get_wasm_module_transfer_id not implemented", + ) + .unwrap(); + let exc = Exception::error(scope, msg); + scope.throw_exception(exc); + None + } +} + +/// The ValueSerializerHeap object contains all objects related to serializer. +/// This object has to be pinned to the heap because of the Cpp pointers that +/// have to remain valid. Moving this object would result in the Cpp pointer +/// to the delegate to become invalid and thus causing the delegate callback +/// to fail. Additionally the serializer and implementation are also pinned +/// in memory because these have to be accessable from within the delegate +/// callback methods. +pub struct ValueSerializerHeap<'a> { + value_serializer_impl: Box, + cxx_value_serializer_delegate: CxxValueSerializerDelegate, + cxx_value_serializer: CxxValueSerializer, + buffer_size: AtomicUsize, + context: Global, + isolate_ptr: *mut RealIsolate, +} + +impl ValueSerializerHeap<'_> { + fn get_cxx_value_serializer_delegate_offset() + -> FieldOffset { + let buf = std::mem::MaybeUninit::::uninit(); + let delegate = + unsafe { addr_of!((*buf.as_ptr()).cxx_value_serializer_delegate) }; + FieldOffset::from_ptrs(buf.as_ptr(), delegate) + } + + fn get_cxx_value_serializer_offset() -> FieldOffset { + let buf = std::mem::MaybeUninit::::uninit(); + let serializer = unsafe { addr_of!((*buf.as_ptr()).cxx_value_serializer) }; + FieldOffset::from_ptrs(buf.as_ptr(), serializer) + } + + /// Panics unless the `Local`s of the scopes open on this thread belong to + /// the isolate of the serializer. Every `CxxValueSerializer` is embedded in + /// a `ValueSerializerHeap`. + fn assert_locals_in_isolate_of(cxx_value_serializer: &CxxValueSerializer) { + let heap: &ValueSerializerHeap = unsafe { + Self::get_cxx_value_serializer_offset().to_embedder(cxx_value_serializer) + }; + heap.context.assert_locals_in_same_isolate(); + } + + /// Starting from 'this' pointer a ValueSerializerHeap ref can be created + pub unsafe fn dispatch( + value_serializer_delegate: &CxxValueSerializerDelegate, + ) -> &Self { + unsafe { + Self::get_cxx_value_serializer_delegate_offset() + .to_embedder::(value_serializer_delegate) + } + } +} + +impl Drop for ValueSerializerHeap<'_> { + fn drop(&mut self) { + unsafe { v8__ValueSerializer__DESTRUCT(&mut self.cxx_value_serializer) }; + } +} + +fn cast_to_ptr(x: &T) -> *mut T { + x as *const T as *mut T +} + +/// Trait used for direct write to the serialization buffer. +/// Mostly used by the write_host_object callback function in the +/// ValueSerializerImpl trait to create custom serialization logic. +pub trait ValueSerializerHelper { + fn get_cxx_value_serializer(&self) -> &CxxValueSerializer; + + fn write_header(&self) { + unsafe { + v8__ValueSerializer__WriteHeader(cast_to_ptr( + self.get_cxx_value_serializer(), + )); + }; + } + + fn write_value( + &self, + context: Local, + value: Local, + ) -> Option { + ValueSerializerHeap::assert_locals_in_isolate_of( + self.get_cxx_value_serializer(), + ); + unsafe { + v8__ValueSerializer__WriteValue( + cast_to_ptr(self.get_cxx_value_serializer()), + context, + value, + ) + } + .into() + } + + fn write_uint32(&self, value: u32) { + unsafe { + v8__ValueSerializer__WriteUint32( + cast_to_ptr(self.get_cxx_value_serializer()), + value, + ); + }; + } + + fn write_uint64(&self, value: u64) { + unsafe { + v8__ValueSerializer__WriteUint64( + cast_to_ptr(self.get_cxx_value_serializer()), + value, + ); + }; + } + + fn write_double(&self, value: f64) { + unsafe { + v8__ValueSerializer__WriteDouble( + cast_to_ptr(self.get_cxx_value_serializer()), + value, + ); + }; + } + + fn write_raw_bytes(&self, source: &[u8]) { + unsafe { + v8__ValueSerializer__WriteRawBytes( + cast_to_ptr(self.get_cxx_value_serializer()), + source.as_ptr() as *const _, + source.len(), + ); + }; + } + + fn transfer_array_buffer( + &self, + transfer_id: u32, + array_buffer: Local, + ) { + ValueSerializerHeap::assert_locals_in_isolate_of( + self.get_cxx_value_serializer(), + ); + unsafe { + v8__ValueSerializer__TransferArrayBuffer( + cast_to_ptr(self.get_cxx_value_serializer()), + transfer_id, + array_buffer, + ); + }; + } + + fn set_treat_array_buffer_views_as_host_objects(&self, mode: bool) { + unsafe { + v8__ValueSerializer__SetTreatArrayBufferViewsAsHostObjects( + cast_to_ptr(self.get_cxx_value_serializer()), + mode, + ); + }; + } +} + +impl ValueSerializerHelper for CxxValueSerializer { + fn get_cxx_value_serializer(&self) -> &CxxValueSerializer { + self + } +} + +impl ValueSerializerHelper for ValueSerializerHeap<'_> { + fn get_cxx_value_serializer(&self) -> &CxxValueSerializer { + &self.cxx_value_serializer + } +} + +impl ValueSerializerHelper for ValueSerializer<'_> { + fn get_cxx_value_serializer(&self) -> &CxxValueSerializer { + &self.value_serializer_heap.cxx_value_serializer + } +} + +pub struct ValueSerializer<'a> { + value_serializer_heap: Pin>>, + // ValueSerializerHeap is already !Send and !Sync + // but this is just making it explicit + _phantom: std::marker::PhantomData<*mut ()>, +} + +/// ValueSerializer is a stack object used as entry-point for an owned and +/// pinned heap object ValueSerializerHeap. +/// The 'a lifetime is the lifetime of the ValueSerializerImpl implementation. +/// The 's lifetime is the lifetime of the HandleScope which is used to retrieve +/// a Local<'s, Context> for the CallbackScopes +impl<'a> ValueSerializer<'a> { + pub fn new<'s, 'i, D: ValueSerializerImpl + 'a>( + scope: &PinScope<'s, 'i>, + value_serializer_impl: Box, + ) -> Self { + let context = scope.get_current_context(); + // create dummy ValueSerializerHeap 'a, and move to heap + pin to address + let value_serializer_heap_ptr = + Box::into_raw(Box::new(ValueSerializerHeap { + value_serializer_impl, + cxx_value_serializer: CxxValueSerializer { + _cxx_vtable: CxxVTable(std::ptr::null()), + }, + cxx_value_serializer_delegate: CxxValueSerializerDelegate { + _cxx_vtable: CxxVTable(std::ptr::null()), + }, + buffer_size: AtomicUsize::new(0), + context: Global::new(scope, context), + isolate_ptr: scope.get_isolate_ptr(), + })); + + unsafe { + let delegate_ptr = std::ptr::addr_of_mut!( + (*value_serializer_heap_ptr).cxx_value_serializer_delegate + ); + let serializer_ptr = std::ptr::addr_of_mut!( + (*value_serializer_heap_ptr).cxx_value_serializer + ); + v8__ValueSerializer__Delegate__CONSTRUCT( + delegate_ptr + .cast::>(), + ); + + v8__ValueSerializer__CONSTRUCT( + serializer_ptr.cast::>(), + scope.get_isolate_ptr(), + delegate_ptr, + ); + }; + + // SAFETY: pointer from `Box::into_raw` is valid + let value_serializer_heap = + Pin::new(unsafe { Box::from_raw(value_serializer_heap_ptr) }); + + Self { + value_serializer_heap, + _phantom: std::marker::PhantomData, + } + } +} + +impl ValueSerializer<'_> { + pub fn release(&self) -> Vec { + unsafe { + let mut size: usize = 0; + let mut ptr: *mut u8 = &mut 0; + v8__ValueSerializer__Release( + cast_to_ptr(self.get_cxx_value_serializer()), + &mut ptr, + &mut size, + ); + let capacity = self + .value_serializer_heap + .buffer_size + .swap(0, std::sync::atomic::Ordering::Relaxed); + if ptr.is_null() { + return Vec::new(); + } + assert!(size <= capacity); + // SAFETY: ptr is non-null, was allocated by us in `v8__ValueSerializer__Delegate__ReallocateBufferMemory`, and + // the capacity is correctly updated during reallocation. Size is asserted to be valid above. + Vec::from_raw_parts(ptr, size, capacity) + } + } + + pub fn write_value( + &self, + context: Local, + value: Local, + ) -> Option { + self.value_serializer_heap.write_value(context, value) + } +} diff --git a/crates/v8/src/wasm.rs b/crates/v8/src/wasm.rs new file mode 100644 index 0000000000..4897797a79 --- /dev/null +++ b/crates/v8/src/wasm.rs @@ -0,0 +1,642 @@ +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. + +use std::ffi::c_void; +use std::ptr::null; +use std::ptr::null_mut; + +use crate::ArrayBuffer; +use crate::Isolate; +use crate::IsolateHandle; +use crate::Local; +use crate::PinScope; +use crate::Value; +use crate::WasmMemoryObject; +use crate::WasmModuleObject; +use crate::binding::const_memory_span_t; +use crate::function::FunctionCallbackArguments; +use crate::function::FunctionCallbackInfo; +use crate::isolate::RealIsolate; +use crate::scope::GetIsolate; +use crate::scope::callback_scope; +use crate::support::MapFnFrom; +use crate::support::MapFnTo; +use crate::support::Opaque; +use crate::support::ToCFn; +use crate::support::UnitType; +use crate::support::char; + +// Type-erased std::shared_ptr. Assumes it's safe +// to move around (no backlinks). Not generally true for shared_ptrs +// but it is in this case - other shared_ptrs that point to the same +// v8::WasmStreaming exist but are managed by V8 and don't leak out. +// +// We don't use crate::support::SharedPtr because it only allows +// immutable borrows and derefs to avoid aliasing but that's not +// a problem here, only a single instance outside V8 exists. +// +// Note: uses *mut u8 rather than e.g. usize to enforce !Send and !Sync. +#[repr(C)] +struct WasmStreamingSharedPtr([*mut u8; 2]); + +/// The V8 interface for WebAssembly streaming compilation. +/// When streaming compilation is initiated, V8 passes a [Self] +/// object to the embedder such that the embedder can pass the +/// input bytes for streaming compilation to V8. +#[repr(C)] +pub struct WasmStreaming( + WasmStreamingSharedPtr, + // The isolate that started the streaming compilation, whose promise + // `abort()` rejects. + IsolateHandle, +); + +impl + WasmStreaming +{ + /// Pass a new chunk of bytes to WebAssembly streaming compilation. + #[inline(always)] + pub fn on_bytes_received(&mut self, data: &[u8]) { + unsafe { + v8__WasmStreaming__OnBytesReceived( + &mut self.0, + data.as_ptr(), + data.len(), + ); + } + } + + /// Abort streaming compilation. If {exception} has a value, then the promise + /// associated with streaming compilation is rejected with that value. If + /// {exception} does not have value, the promise does not get rejected. + #[inline(always)] + /// + /// # Panics + /// + /// Panics if `exception` belongs to another isolate than the one that + /// started the streaming compilation. + pub fn abort(mut self, exception: Option>) { + if exception.is_some() { + crate::handle::assert_locals_in_isolate_of(&self.1); + } + let exception = exception.map_or(null(), |v| &*v as *const Value); + unsafe { v8__WasmStreaming__Abort(&mut self.0, exception) } + } + + /// Sets the UTF-8 encoded source URL for the `Script` object. This must be + /// called before [`Self::finish()`]. + #[inline(always)] + pub fn set_url(&mut self, url: &str) { + // Although not documented, V8 requires the url to be null terminated. + // See https://chromium-review.googlesource.com/c/v8/v8/+/3289148. + let null_terminated_url = format!("{url}\0"); + unsafe { + v8__WasmStreaming__SetUrl( + &mut self.0, + null_terminated_url.as_ptr() as *const char, + url.len(), + ); + } + } +} + +impl WasmStreaming { + /// {Finish} should be called after all received bytes where passed to + /// {OnBytesReceived} to tell V8 that there will be no more bytes. {Finish} + /// must not be called after {Abort} has been called already. + /// If {SetHasCompiledModuleBytes()} was called before, a {caching_callback} + /// can be passed which can inspect the full received wire bytes and set cached + /// module bytes which will be deserialized then. This callback will happen + /// synchronously within this call; the callback is not stored. + #[inline(always)] + pub fn finish(mut self) { + unsafe { v8__WasmStreaming__Finish(&mut self.0, None) } + } + + /// Mark that the embedder has (potentially) cached compiled module bytes (i.e. + /// a serialized {CompiledWasmModule}) that could match this streaming request. + /// This will cause V8 to skip streaming compilation. + /// The embedder should then pass a callback to the {Finish} method to pass the + /// serialized bytes, after potentially checking their validity against the + /// full received wire bytes. + #[inline(always)] + pub fn set_has_compiled_module_bytes(mut self) -> WasmStreaming { + unsafe { + v8__WasmStreaming__SetHasCompiledModuleBytes(&mut self.0); + std::mem::transmute(self) + } + } +} + +impl WasmStreaming { + /// {Finish} should be called after all received bytes where passed to + /// {OnBytesReceived} to tell V8 that there will be no more bytes. {Finish} + /// must not be called after {Abort} has been called already. + /// If {SetHasCompiledModuleBytes()} was called before, a {caching_callback} + /// can be passed which can inspect the full received wire bytes and set cached + /// module bytes which will be deserialized then. This callback will happen + /// synchronously within this call; the callback is not stored. + #[inline(always)] + pub fn finish(mut self, f: F) + where + F: MapFnTo, + { + unsafe { v8__WasmStreaming__Finish(&mut self.0, Some(f.map_fn_to())) } + } +} + +impl Drop + for WasmStreaming +{ + fn drop(&mut self) { + unsafe { v8__WasmStreaming__shared_ptr_DESTRUCT(&mut self.0) } + } +} + +impl WasmModuleObject { + /// Efficiently re-create a WasmModuleObject, without recompiling, from + /// a CompiledWasmModule. + #[inline(always)] + pub fn from_compiled_module<'s>( + scope: &PinScope<'s, '_>, + compiled_module: &CompiledWasmModule, + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__WasmModuleObject__FromCompiledModule( + sd.get_isolate_ptr(), + compiled_module.0, + ) + }) + } + } + + /// Get the compiled module for this module object. The compiled module can be + /// shared by several module objects. + #[inline(always)] + pub fn get_compiled_module(&self) -> CompiledWasmModule { + let ptr = unsafe { v8__WasmModuleObject__GetCompiledModule(self) }; + CompiledWasmModule(ptr) + } + + /// Compile a Wasm module from the provided uncompiled bytes. + #[inline(always)] + pub fn compile<'s>( + scope: &PinScope<'s, '_>, + wire_bytes: &[u8], + ) -> Option> { + unsafe { + scope.cast_local(|sd| { + v8__WasmModuleObject__Compile( + sd.get_isolate_ptr(), + wire_bytes.as_ptr(), + wire_bytes.len(), + ) + }) + } + } +} + +#[repr(C)] +pub struct ModuleCachingInterface(Opaque); + +impl ModuleCachingInterface { + /// Get the full wire bytes, to check against the cached version. + #[inline(always)] + pub fn get_wire_bytes(&self) -> &[u8] { + unsafe { + let span = v8__ModuleCachingInterface__GetWireBytes(self); + std::slice::from_raw_parts(span.data, span.size) + } + } + + /// Pass serialized (cached) compiled module bytes, to be deserialized and + /// used as the result of this streaming compilation. + /// The passed bytes will only be accessed inside this callback, i.e. + /// lifetime can end after the call. + /// The return value indicates whether V8 could use the passed bytes; {false} + /// would be returned on e.g. version mismatch. + /// This method can only be called once. + #[inline(always)] + pub fn set_cached_compiled_module_bytes(&mut self, bytes: &[u8]) -> bool { + unsafe { + v8__ModuleCachingInterface__SetCachedCompiledModuleBytes( + self, + const_memory_span_t { + data: bytes.as_ptr(), + size: bytes.len(), + }, + ) + } + } +} + +pub type ModuleCachingCallback = + unsafe extern "C" fn(*mut ModuleCachingInterface); + +impl MapFnFrom for ModuleCachingCallback +where + F: UnitType + Fn(&mut ModuleCachingInterface), +{ + fn mapping() -> Self { + let f = |mci: *mut ModuleCachingInterface| { + (F::get())(unsafe { &mut *mci }); + }; + f.to_c_fn() + } +} + +// Type-erased v8::CompiledWasmModule. We need this because the C++ +// v8::CompiledWasmModule must be destructed because its private fields hold +// pointers that must be freed, but v8::CompiledWasmModule itself doesn't have +// a destructor. Therefore, in order to avoid memory leaks, the Rust-side +// CompiledWasmModule must be a pointer to a C++ allocation of +// v8::CompiledWasmModule. +#[repr(C)] +struct InternalCompiledWasmModule(Opaque); + +/// Wrapper around a compiled WebAssembly module, which is potentially shared by +/// different WasmModuleObjects. +pub struct CompiledWasmModule(*mut InternalCompiledWasmModule); + +impl CompiledWasmModule { + /// Get the (wasm-encoded) wire bytes that were used to compile this module. + #[inline(always)] + pub fn get_wire_bytes_ref(&self) -> &[u8] { + let mut len = 0isize; + unsafe { + let ptr = v8__CompiledWasmModule__GetWireBytesRef(self.0, &mut len); + std::slice::from_raw_parts(ptr, len.try_into().unwrap()) + } + } + + #[inline(always)] + pub fn source_url(&self) -> &str { + let mut len = 0; + unsafe { + let ptr = v8__CompiledWasmModule__SourceUrl(self.0, &mut len); + let bytes = std::slice::from_raw_parts(ptr as _, len); + std::str::from_utf8_unchecked(bytes) + } + } +} + +// TODO(andreubotella): Safety??? +unsafe impl Send for CompiledWasmModule {} +unsafe impl Sync for CompiledWasmModule {} + +impl Drop for CompiledWasmModule { + fn drop(&mut self) { + unsafe { v8__CompiledWasmModule__DELETE(self.0) } + } +} + +// Type-erased v8::WasmModuleCompilation allocated on the C++ heap. +#[repr(C)] +struct InternalWasmModuleCompilation(Opaque); + +/// An interface for asynchronous WebAssembly module compilation, to be used +/// e.g. for implementing source phase imports. +/// +/// Note: This interface is experimental and can change or be removed without +/// notice. +pub struct WasmModuleCompilation(*mut InternalWasmModuleCompilation); + +// OnBytesReceived can be called from any thread per V8 documentation. +unsafe impl Send for WasmModuleCompilation {} + +impl WasmModuleCompilation { + /// Start an asynchronous module compilation. This can be called on any + /// thread. + #[inline(always)] + pub fn new() -> Self { + unsafe { WasmModuleCompilation(v8__WasmModuleCompilation__NEW()) } + } + + /// Pass a new chunk of bytes to WebAssembly compilation. The buffer is + /// owned by the caller and will not be accessed after this call returns. + /// Can be called from any thread. + #[inline(always)] + pub fn on_bytes_received(&mut self, data: &[u8]) { + unsafe { + v8__WasmModuleCompilation__OnBytesReceived( + self.0, + data.as_ptr(), + data.len(), + ); + } + } + + /// Finish compilation. Must be called on the main thread after all bytes + /// were passed to [`Self::on_bytes_received`]. + /// + /// The `resolution_callback` will eventually be called with either the + /// compiled module or a compilation error. The callback receives `&Isolate` + /// so that [`crate::Global`] handles can be created from the [`Local`] + /// handles to persist them beyond the callback. + /// + /// Must not be called after [`Self::abort`]. + #[inline(always)] + pub fn finish( + self, + scope: &mut PinScope, + caching_callback: Option, + resolution_callback: impl FnOnce( + &Isolate, + Result, Local<'_, Value>>, + ) + 'static, + ) { + // Capture the isolate pointer in the closure so it doesn't need to be + // threaded through C++. + let isolate_ptr = scope.get_isolate_ptr(); + let wrapped = move |module: *const WasmModuleObject, + error: *const Value| { + let isolate = unsafe { Isolate::from_raw_ptr(isolate_ptr) }; + if !module.is_null() { + resolution_callback( + &isolate, + Ok(unsafe { Local::from_raw(module) }.unwrap()), + ); + } else { + resolution_callback( + &isolate, + Err(unsafe { Local::from_raw(error) }.unwrap()), + ); + } + }; + + // Double-box with Option: the outer Box gives us a thin pointer suitable + // for void*. The Option allows the trampoline to .take() the closure + // (FnOnce semantics) without freeing the outer allocation, which is + // ref-counted by shared_ptr on the C++ side. + #[allow(clippy::type_complexity)] + let boxed: Box< + Option>, + > = Box::new(Some(Box::new(wrapped))); + let data = Box::into_raw(boxed) as *mut c_void; + + unsafe { + v8__WasmModuleCompilation__Finish( + self.0, + scope.get_isolate_ptr(), + caching_callback, + resolution_trampoline, + data, + drop_resolution_data, + ); + } + } + + /// Abort compilation. Can be called from any thread. + /// Must not be called repeatedly, or after [`Self::finish`]. + #[inline(always)] + pub fn abort(self) { + unsafe { v8__WasmModuleCompilation__Abort(self.0) } + } + + /// Mark that the embedder has (potentially) cached compiled module bytes + /// (i.e. a serialized [`CompiledWasmModule`]) that could match this + /// compilation request. This will cause V8 to skip streaming compilation. + /// The embedder should then pass a caching callback to [`Self::finish`]. + #[inline(always)] + pub fn set_has_compiled_module_bytes(&mut self) { + unsafe { + v8__WasmModuleCompilation__SetHasCompiledModuleBytes(self.0); + } + } + + /// Sets a callback which is called whenever a significant number of new + /// functions are ready for serialization. + #[inline(always)] + pub fn set_more_functions_can_be_serialized_callback( + &mut self, + callback: impl Fn(CompiledWasmModule) + Send + 'static, + ) { + let boxed: Box> = + Box::new(Box::new(callback)); + let data = Box::into_raw(boxed) as *mut c_void; + + unsafe { + v8__WasmModuleCompilation__SetMoreFunctionsCanBeSerializedCallback( + self.0, + serialization_trampoline, + data, + drop_serialization_data, + ); + } + } + + /// Sets the UTF-8 encoded source URL for the `Script` object. This must + /// be called before [`Self::finish`]. + #[inline(always)] + pub fn set_url(&mut self, url: &str) { + // V8 requires the url to be null terminated. + let null_terminated_url = format!("{url}\0"); + unsafe { + v8__WasmModuleCompilation__SetUrl( + self.0, + null_terminated_url.as_ptr() as *const char, + url.len(), + ); + } + } +} + +impl Default for WasmModuleCompilation { + fn default() -> Self { + Self::new() + } +} + +impl Drop for WasmModuleCompilation { + fn drop(&mut self) { + unsafe { v8__WasmModuleCompilation__DELETE(self.0) } + } +} + +unsafe extern "C" fn resolution_trampoline( + data: *mut c_void, + module: *const WasmModuleObject, + error: *const Value, +) { + // Take the closure out of the Option without freeing the outer Box. + // The outer Box is ref-counted by shared_ptr on the C++ side and will + // be freed via drop_resolution_data when the last copy is destroyed. + let slot = unsafe { + &mut *(data + as *mut Option>) + }; + let callback = slot.take().unwrap(); + callback(module, error); +} + +unsafe extern "C" fn drop_resolution_data(data: *mut c_void) { + let _ = unsafe { + Box::from_raw( + data + as *mut Option>, + ) + }; +} + +unsafe extern "C" fn serialization_trampoline( + data: *mut c_void, + compiled_module: *mut InternalCompiledWasmModule, +) { + let callback = + unsafe { &**(data as *const Box) }; + callback(CompiledWasmModule(compiled_module)); +} + +unsafe extern "C" fn drop_serialization_data(data: *mut c_void) { + let _ = unsafe { + Box::from_raw(data as *mut Box) + }; +} + +impl WasmMemoryObject { + /// Returns underlying ArrayBuffer. + #[inline(always)] + pub fn buffer(&self) -> Local<'_, ArrayBuffer> { + unsafe { Local::from_raw(v8__WasmMemoryObject__Buffer(self)) }.unwrap() + } +} + +pub(crate) fn trampoline() +-> unsafe extern "C" fn(*const FunctionCallbackInfo) +where + F: UnitType + + for<'a, 'b, 'c> Fn( + &'c mut PinScope<'a, 'b>, + Local<'a, Value>, + WasmStreaming, + ), +{ + unsafe extern "C" fn c_fn(info: *const FunctionCallbackInfo) + where + F: UnitType + + for<'a, 'b, 'c> Fn( + &'c mut PinScope<'a, 'b>, + Local<'a, Value>, + WasmStreaming, + ), + { + let info = unsafe { &*info }; + callback_scope!(unsafe scope, info); + let args = FunctionCallbackArguments::from_function_callback_info(info); + let data = args.data(); + let zero = null_mut(); + let mut that = WasmStreamingSharedPtr([zero, zero]); + unsafe { + v8__WasmStreaming__Unpack(scope.get_isolate_ptr(), &*data, &mut that); + }; + let source = args.get(0); + let isolate = scope.thread_safe_handle(); + (F::get())(scope, source, WasmStreaming(that, isolate)); + } + c_fn:: +} + +unsafe extern "C" { + fn v8__WasmStreaming__Unpack( + isolate: *mut RealIsolate, + value: *const Value, + that: *mut WasmStreamingSharedPtr, // Out parameter. + ); + fn v8__WasmStreaming__shared_ptr_DESTRUCT(this: *mut WasmStreamingSharedPtr); + fn v8__WasmStreaming__SetHasCompiledModuleBytes( + this: *mut WasmStreamingSharedPtr, + ); + fn v8__WasmStreaming__OnBytesReceived( + this: *mut WasmStreamingSharedPtr, + data: *const u8, + len: usize, + ); + fn v8__WasmStreaming__Finish( + this: *mut WasmStreamingSharedPtr, + callback: Option, + ); + fn v8__WasmStreaming__Abort( + this: *mut WasmStreamingSharedPtr, + exception: *const Value, + ); + fn v8__WasmStreaming__SetUrl( + this: *mut WasmStreamingSharedPtr, + url: *const char, + len: usize, + ); + + fn v8__ModuleCachingInterface__GetWireBytes( + interface: *const ModuleCachingInterface, + ) -> const_memory_span_t; + fn v8__ModuleCachingInterface__SetCachedCompiledModuleBytes( + interface: *mut ModuleCachingInterface, + bytes: const_memory_span_t, + ) -> bool; + + fn v8__WasmModuleObject__FromCompiledModule( + isolate: *mut RealIsolate, + compiled_module: *const InternalCompiledWasmModule, + ) -> *const WasmModuleObject; + fn v8__WasmModuleObject__GetCompiledModule( + this: *const WasmModuleObject, + ) -> *mut InternalCompiledWasmModule; + fn v8__WasmModuleObject__Compile( + isolate: *mut RealIsolate, + wire_bytes_data: *const u8, + length: usize, + ) -> *mut WasmModuleObject; + + fn v8__CompiledWasmModule__GetWireBytesRef( + this: *mut InternalCompiledWasmModule, + length: *mut isize, + ) -> *const u8; + fn v8__CompiledWasmModule__SourceUrl( + this: *mut InternalCompiledWasmModule, + length: *mut usize, + ) -> *const char; + fn v8__CompiledWasmModule__DELETE(this: *mut InternalCompiledWasmModule); + + fn v8__WasmMemoryObject__Buffer( + this: *const WasmMemoryObject, + ) -> *mut ArrayBuffer; + + fn v8__WasmModuleCompilation__NEW() -> *mut InternalWasmModuleCompilation; + fn v8__WasmModuleCompilation__DELETE( + this: *mut InternalWasmModuleCompilation, + ); + fn v8__WasmModuleCompilation__OnBytesReceived( + this: *mut InternalWasmModuleCompilation, + bytes: *const u8, + size: usize, + ); + fn v8__WasmModuleCompilation__Finish( + this: *mut InternalWasmModuleCompilation, + isolate: *mut RealIsolate, + caching_callback: Option, + resolution_callback: unsafe extern "C" fn( + *mut c_void, + *const WasmModuleObject, + *const Value, + ), + resolution_data: *mut c_void, + drop_resolution_data: unsafe extern "C" fn(*mut c_void), + ); + fn v8__WasmModuleCompilation__Abort(this: *mut InternalWasmModuleCompilation); + fn v8__WasmModuleCompilation__SetHasCompiledModuleBytes( + this: *mut InternalWasmModuleCompilation, + ); + fn v8__WasmModuleCompilation__SetMoreFunctionsCanBeSerializedCallback( + this: *mut InternalWasmModuleCompilation, + callback: unsafe extern "C" fn( + *mut c_void, + *mut InternalCompiledWasmModule, + ), + data: *mut c_void, + drop_data: unsafe extern "C" fn(*mut c_void), + ); + fn v8__WasmModuleCompilation__SetUrl( + this: *mut InternalWasmModuleCompilation, + url: *const char, + length: usize, + ); +} diff --git a/crates/v8/tests/linked.rs b/crates/v8/tests/linked.rs new file mode 100644 index 0000000000..4e682edf5d --- /dev/null +++ b/crates/v8/tests/linked.rs @@ -0,0 +1,21 @@ +#[test] +fn verified_archive_creates_and_disposes_an_isolate() { + let platform = v8::new_default_platform(4, false).make_shared(); + v8::V8::initialize_platform(platform); + v8::V8::initialize(); + assert_eq!(v8::V8::get_version(), "15.8.62.58-rusty"); + { + let isolate = &mut v8::Isolate::new(Default::default()); + v8::scope!(let scope, isolate); + let context = v8::Context::new(scope, Default::default()); + let scope = &mut v8::ContextScope::new(scope, context); + let source = v8::String::new(scope, "21 * 2").unwrap(); + let value = v8::Script::compile(scope, source, None) + .unwrap() + .run(scope) + .unwrap(); + assert_eq!(value.integer_value(scope), Some(42)); + } + unsafe { v8::V8::dispose() }; + v8::V8::dispose_platform(); +} diff --git a/docker/build/darwin.Dockerfile b/docker/build/darwin.Dockerfile index 65e78fd491..af3272dee8 100644 --- a/docker/build/darwin.Dockerfile +++ b/docker/build/darwin.Dockerfile @@ -19,6 +19,7 @@ ARG DARWIN_LLVM_VERSION=21 ENV SDK=/root/osxcross/target/SDK/MacOSX14.5.sdk \ SDKROOT=/root/osxcross/target/SDK/MacOSX14.5.sdk \ + OSXCROSS_SDKROOT=/root/osxcross/target/SDK/MacOSX14.5.sdk \ OSXCROSS_SDK=MacOSX14.5.sdk \ RUSTC_WRAPPER=sccache \ SCCACHE_DIR=/root/.cache/sccache \ @@ -76,8 +77,8 @@ RUN --mount=type=cache,id=cargo-registry-agentos-darwin,target=/usr/local/cargo/ tu=$(echo "$TARGET" | tr 'a-z-' 'A-Z_') && \ tl=$(echo "$TARGET" | tr - _) && \ export BINDGEN_EXTRA_CLANG_ARGS_${tl}="--sysroot=$SDK -isystem $SDK/usr/include" && \ - export CFLAGS_${tl}="-B/root/osxcross/target/bin" && \ - export CXXFLAGS_${tl}="-B/root/osxcross/target/bin" && \ + export CFLAGS_${tl}="-B/root/osxcross/target/bin -isysroot $SDK" && \ + export CXXFLAGS_${tl}="-B/root/osxcross/target/bin -isysroot $SDK -nostdinc++ -isystem $SDK/usr/include/c++/v1" && \ export CC_${tl}=${CLANG}-clang && \ export CXX_${tl}=${CLANG}-clang++ && \ export AR_${tl}=${CLANG}-ar && \ diff --git a/scripts/ci/v8/archives.json b/scripts/ci/v8/archives.json index 8576287c8c..54fdf1f44d 100644 --- a/scripts/ci/v8/archives.json +++ b/scripts/ci/v8/archives.json @@ -1,18 +1,6 @@ { - "aarch64-apple-darwin": { - "sha256": "07fcc9f69cfe977d8b3567c14e300f74ab5f842e0ee672c9121a32cf0b7f0cac", - "artifactId": 14679159204 - }, - "aarch64-unknown-linux-gnu": { - "sha256": "72a71c276d8c44440cb25e412ec99f72bb5b331a81a62998f86d6e9bf5b471f8", - "artifactId": 14679705551 - }, - "x86_64-apple-darwin": { - "sha256": "4bd58f1a1268448bd688eab504123570b6c73344faa73139ff1e17b728521b63", - "artifactId": 14680823472 - }, - "x86_64-unknown-linux-gnu": { - "artifactId": 14699571567, - "sha256": "6fea0688c054ab5046219ad231ec76ced6a35b188fce6e4282614fb1f1927389" - } + "aarch64-apple-darwin": "07fcc9f69cfe977d8b3567c14e300f74ab5f842e0ee672c9121a32cf0b7f0cac", + "aarch64-unknown-linux-gnu": "72a71c276d8c44440cb25e412ec99f72bb5b331a81a62998f86d6e9bf5b471f8", + "x86_64-apple-darwin": "4bd58f1a1268448bd688eab504123570b6c73344faa73139ff1e17b728521b63", + "x86_64-unknown-linux-gnu": "6fea0688c054ab5046219ad231ec76ced6a35b188fce6e4282614fb1f1927389" } diff --git a/scripts/ci/v8/engine.py b/scripts/ci/v8/engine.py index 9143d16f36..53048d5079 100644 --- a/scripts/ci/v8/engine.py +++ b/scripts/ci/v8/engine.py @@ -264,7 +264,8 @@ def reviewed_archive(target, dest, allow_actions): download_action_bundle(artifact_id, target, work) else: url = f"https://releases.rivet.dev/agent-os/v8/{SOURCE['v8Tree']}/{checksum}/{target}.tar.gz" - with urllib.request.urlopen(url, timeout=120) as response, (work / "bundle").open("wb") as out: + request = urllib.request.Request(url, headers={"User-Agent": "agentos-v8/" + SOURCE["version"]}) + with urllib.request.urlopen(request, timeout=120) as response, (work / "bundle").open("wb") as out: shutil.copyfileobj(response, out) require(digest(work / "bundle") == checksum, "V8 archive SHA-256 mismatch") yield work / "bundle", checksum diff --git a/scripts/ci/v8/test_engine.py b/scripts/ci/v8/test_engine.py index f5f8e78ed8..cafca8e833 100644 --- a/scripts/ci/v8/test_engine.py +++ b/scripts/ci/v8/test_engine.py @@ -14,6 +14,19 @@ import engine +class BindingProvenanceTests(unittest.TestCase): + def test_published_binding_keeps_reviewed_sources_and_verifier(self): + repo = Path(__file__).resolve().parents[3] + binding = repo / "crates/v8" + upstream = json.loads((binding / "UPSTREAM.json").read_text()) + self.assertEqual(upstream["revision"], engine.SOURCE["rustyV8"]) + actual = {p.relative_to(binding).as_posix(): engine.digest(p) + for p in (binding / "src").rglob("*") if p.is_file()} + self.assertEqual(actual, upstream["rustSources"]) + for name in ("engine.py", "source.json", "linux-x64-sysroot.json", "archives.json"): + self.assertEqual((binding / "provider" / name).read_bytes(), (engine.HERE / name).read_bytes()) + + class BundleTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() diff --git a/scripts/publish/src/lib/rust-crates.test.ts b/scripts/publish/src/lib/rust-crates.test.ts index 4ce034ee33..c1e563037e 100644 --- a/scripts/publish/src/lib/rust-crates.test.ts +++ b/scripts/publish/src/lib/rust-crates.test.ts @@ -54,6 +54,7 @@ test("Rust crate publish order satisfies internal dependencies", () => { assert(!RUST_CRATES.includes("agentos-vm-core" as never)); assertBefore("agentos-rivetkit-ars-client", "agentos-vm"); + assertBefore("agentos-v8", "agentos-executor-v8-runtime"); assertBefore("agentos-vm-host-interface", "agentos-executor-v8-runtime"); assertBefore("agentos-executor-contract", "agentos-executor-v8-runtime"); assertBefore("agentos-resource-accounting", "agentos-driver-tokio"); @@ -177,6 +178,22 @@ test("real crate releases require every locked V8 dependency to resolve through }); }); +test("owned V8 bindings require a real publishable crate and durable pins", () => { + withFixture((root) => { + write(root, "Cargo.lock", '[[package]]\nname = "agentos-v8"\nversion = "0.0.1"\n'); + assert.throws(() => assertPublishableV8Source(root), /outside the agentOS workspace/); + write(root, "crates/v8/Cargo.toml", '[package]\nname = "agentos-v8"\n'); + const pins = Object.fromEntries([ + "x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", + "x86_64-apple-darwin", "aarch64-apple-darwin", + ].map((target) => [target, "a".repeat(64)])); + write(root, "scripts/ci/v8/archives.json", JSON.stringify(pins)); + assert.doesNotThrow(() => assertPublishableV8Source(root)); + write(root, "Cargo.lock", '[[package]]\nname = "agentos-v8"\nversion = "0.0.1"\nsource = "git+https://example.org/binding"\n'); + assert.throws(() => assertPublishableV8Source(root), /outside the agentOS workspace/); + }); +}); + // cargo publish resolves every non-dev dependency from crates.io, optional // ones included, so each crate must come after all of them in the order. test("every crate publishes after the agentOS crates its manifest depends on", () => { diff --git a/scripts/publish/src/lib/rust-crates.ts b/scripts/publish/src/lib/rust-crates.ts index 08d9ccf7bc..a329652122 100644 --- a/scripts/publish/src/lib/rust-crates.ts +++ b/scripts/publish/src/lib/rust-crates.ts @@ -1,9 +1,10 @@ -import { readFileSync } from "node:fs"; +import { existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; // AgentOS-owned crates published to crates.io in dependency order. Crates with // `publish = false` stay out of this list. export const RUST_CRATE_ORDER = [ + "agentos-v8", "agentos-rivetkit-ars-client", "agentos-vm-host-interface", "agentos-executor-contract", @@ -45,9 +46,13 @@ export const STANDALONE_RUST_CRATE_MANIFESTS = { export function assertPublishableV8Source(repoRoot: string): void { const lock = readFileSync(join(repoRoot, "Cargo.lock"), "utf8"); const engines = lock.split(/^\[\[package\]\]$/m).filter((p) => /^name = "v8"$/m.test(p)); - if (engines.length === 0 || engines.some((p) => !/^source = "registry\+https:\/\/github\.com\/rust-lang\/crates\.io-index"$/m.test(p))) { + const owned = lock.split(/^\[\[package\]\]$/m).filter((p) => /^name = "agentos-v8"$/m.test(p)); + const ownedManifest = join(repoRoot, "crates/v8/Cargo.toml"); + const ownedSource = owned.length === 1 && !/^source = /m.test(owned[0]) && existsSync(ownedManifest) && + /^name = "agentos-v8"$/m.test(readFileSync(ownedManifest, "utf8")); + if ((!ownedSource && engines.length === 0) || engines.some((p) => !/^source = "registry\+https:\/\/github\.com\/rust-lang\/crates\.io-index"$/m.test(p))) { throw new Error( - "Rust publication requires a crates.io V8 dependency that works outside the agentOS workspace. Qualify the registry crate and its matching engine bundle before releasing.", + "Rust publication requires a packaged agentOS V8 binding or a crates.io V8 dependency that works outside the agentOS workspace. Qualify the crate and its matching engine bundle before releasing.", ); } const pins = JSON.parse(readFileSync(join(repoRoot, "scripts/ci/v8/archives.json"), "utf8")); From e0c4c6a0a6ad7ed13f689d355b5720b13ed42af2 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 17:28:02 -0800 Subject: [PATCH 15/19] fix: construct V8 scope in caller-owned stack storage --- .../src/v8_thread_support.cc | 4 ++- scripts/ci/v8/README.md | 34 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/crates/executor-v8-runtime/src/v8_thread_support.cc b/crates/executor-v8-runtime/src/v8_thread_support.cc index 047738f3bd..7fcc762476 100644 --- a/crates/executor-v8-runtime/src/v8_thread_support.cc +++ b/crates/executor-v8-runtime/src/v8_thread_support.cc @@ -48,7 +48,9 @@ extern "C" void agentos_v8_suppress_microtasks_begin(void* storage, v8::Isolate* // The Rust caller pins storage BEFORE placement construction. nullptr selects // the default queue; call depth suppresses implicit checkpoints for all queues, // while an owned queue's explicit checkpoints remain available. - new (storage) AgentOsSuppressMicrotasks(isolate, nullptr); + // Global placement new constructs in that existing Rust stack slot; Clang + // correctly deletes the V8 class allocation operators for stack-only scopes. + ::new (storage) AgentOsSuppressMicrotasks(isolate, nullptr); } extern "C" void agentos_v8_suppress_microtasks_end(void* storage) { diff --git a/scripts/ci/v8/README.md b/scripts/ci/v8/README.md index 72e758f81c..45b51f64d4 100644 --- a/scripts/ci/v8/README.md +++ b/scripts/ci/v8/README.md @@ -5,9 +5,10 @@ Normal PR, benchmark and sidecar release jobs use reviewed binary inputs. The and arm64. It has read-only repository permissions. Each output contains the static library, Rust bindings, public headers, generated GN configuration, ICU common data, license notices and the Rust crate files from the pinned Git commit. -CI installs those crate files using Cargo's directory source replacement and -keeps the original Git identity in `Cargo.lock`. Consumer jobs can use the -binary inputs without cloning Chromium's native submodules. Before upload, a +The lockstep `agentos-v8` crate includes the unchanged upstream Rust sources +and downloads the reviewed native archive for its target. Normal consumers need +no Git override or Chromium checkout. The producer retains the upstream crate +inside each archive for independent native-build qualification. Before upload, a separate Cargo build links the bundle and compares the native V8 version to the generated binding and the pinned source version. @@ -18,9 +19,9 @@ consumer creates and disposes an isolate/context, links against the release sysroot, passes the ELF glibc-floor check and runs on Ubuntu 18.04. A full sidecar release link and runtime checks are still required before release. -Pip-Boy's dependency comparison fetches only the Rust sources for each -checkout's locked revision. A registry-only base uses Cargo's normal registry -resolution. Its platform size jobs pass the reviewed bundle to BuildKit as a +Pip-Boy's dependency comparison supports older checkouts with Git-pinned Rust +sources. The current checkout uses `agentos-v8`; a registry-only base uses +Cargo's normal registry resolution. Its platform size jobs pass the reviewed bundle to BuildKit as a read-only named context and verify the digest inside the build; Actions credentials stay on the runner. @@ -63,9 +64,10 @@ DebugVisualizers are unused for these targets. upstream binary with the same crate version. No digest is credited until that target has actually been built and inspected. 4. Before a crates.io release, qualify an independent, clean Rust consumer - using the registry dependency and its default artifact resolution. Cargo - ignores this workspace's Git patch in downstream crates. The publish - workflow checks for this before any real release job can publish. + using the packaged `agentos-v8` dependency and its default archive resolution. + Its source digests must match `crates/v8/UPSTREAM.json`; its provider files + must match this directory. Publish the binding before its dependent executor + crates. The release rehearsal verifies every crate from its own tarball. ## Running locally @@ -95,18 +97,24 @@ python3 scripts/ci/v8/engine.py pack "$repo/target/rusty-v8-source" \ "$repo/target/v8-build" "$target" "$repo/target/$target.tar.gz" ``` -To use an approved archive from the durable origin: +Normal Cargo builds resolve the published archive automatically: + +```sh +cargo build --locked -p agentos-sidecar +``` + +To prefetch and explicitly select an approved archive: ```sh python3 scripts/ci/v8/engine.py install "$target" "$repo/target/v8-bundles" \ - --cargo-config "$repo/target/v8-source.toml" \ --shell-env "$repo/target/v8-build.env" . "$repo/target/v8-build.env" -cargo --config "$repo/target/v8-source.toml" build --locked -p agentos-sidecar +cargo build --locked -p agentos-sidecar ``` A draft checkout using temporary pins can add `--allow-actions` with an authenticated `gh` CLI that has read access to the artifact. A locally qualified build can use the same install command with `--bundle --sha256 `. The installer verifies the entire archive before extracting and checks all inputs against its manifest. The -activation file pins both Cargo's library source and the generated Rust binding. +activation file selects the verified library, generated binding and headers; +the packaged Rust sources remain pinned in `agentos-v8`. From 904b3ea81574595ca753f9c56c6121420921db52 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 17:59:37 -0800 Subject: [PATCH 16/19] fix: initialize V8 stack bounds on all native targets --- .github/workflows/pipboy.yml | 56 ++++++++++++++++--- crates/executor-v8-runtime/src/isolate.rs | 37 +++++++++++- .../src/v8_thread_support.cc | 22 ++++++-- .../core/tests/native-platform-smoke.test.ts | 48 ++++++++++++++++ 4 files changed, 151 insertions(+), 12 deletions(-) create mode 100644 packages/core/tests/native-platform-smoke.test.ts diff --git a/.github/workflows/pipboy.yml b/.github/workflows/pipboy.yml index 205c0e67c1..d4743ea0a0 100644 --- a/.github/workflows/pipboy.yml +++ b/.github/workflows/pipboy.yml @@ -49,14 +49,14 @@ jobs: # changes or the `ci: native-all` label, which is read on the next push. NATIVE_ALL: >- [ - {"platform":"linux-x64-gnu","target":"x86_64-unknown-linux-gnu","runner":"${{ github.event_name == 'pull_request' && 'depot-ubuntu-24.04-8' || 'ubuntu-latest' }}","dockerfile":"linux-gnu","clang":"","tier":"linux"}, - {"platform":"linux-arm64-gnu","target":"aarch64-unknown-linux-gnu","runner":"ubuntu-24.04-arm","dockerfile":"linux-gnu","clang":"","tier":"all"}, - {"platform":"darwin-x64","target":"x86_64-apple-darwin","runner":"${{ github.event_name == 'pull_request' && 'depot-ubuntu-24.04-8' || 'ubuntu-latest' }}","dockerfile":"darwin","clang":"x86_64-apple-darwin20.4","tier":"all"}, - {"platform":"darwin-arm64","target":"aarch64-apple-darwin","runner":"${{ github.event_name == 'pull_request' && 'depot-ubuntu-24.04-8' || 'ubuntu-latest' }}","dockerfile":"darwin","clang":"aarch64-apple-darwin20.4","tier":"all"} + {"platform":"linux-x64-gnu","target":"x86_64-unknown-linux-gnu","runner":"${{ github.event_name == 'pull_request' && 'depot-ubuntu-24.04-8' || 'ubuntu-latest' }}","dockerfile":"linux-gnu","clang":"","tier":"linux","smoke_runner":"ubuntu-24.04"}, + {"platform":"linux-arm64-gnu","target":"aarch64-unknown-linux-gnu","runner":"ubuntu-24.04-arm","dockerfile":"linux-gnu","clang":"","tier":"all","smoke_runner":"ubuntu-24.04-arm"}, + {"platform":"darwin-x64","target":"x86_64-apple-darwin","runner":"${{ github.event_name == 'pull_request' && 'depot-ubuntu-24.04-8' || 'ubuntu-latest' }}","dockerfile":"darwin","clang":"x86_64-apple-darwin20.4","tier":"all","smoke_runner":"macos-15"}, + {"platform":"darwin-arm64","target":"aarch64-apple-darwin","runner":"${{ github.event_name == 'pull_request' && 'depot-ubuntu-24.04-8' || 'ubuntu-latest' }}","dockerfile":"darwin","clang":"aarch64-apple-darwin20.4","tier":"all","smoke_runner":"macos-15"} ] run: | - RUST='(\.rs$|(^|/)Cargo\.(toml|lock)$|^deny\.toml$|^rust-toolchain\.toml$|^docker/build/|^scripts/ci/v8/(archives\.json|source\.json|engine\.py|.*\.patch)$|^scripts/pipboy/|^\.github/workflows/pipboy\.yml$)' - PLATFORM_SENSITIVE='((^|/)Cargo\.(toml|lock)$|(^|/)build\.rs$|^docker/build/|^scripts/ci/v8/(archives\.json|source\.json|engine\.py|.*\.patch)$|^rust-toolchain\.toml$)' + RUST='(\.(rs|cc|h)$|(^|/)Cargo\.(toml|lock)$|^deny\.toml$|^rust-toolchain\.toml$|^docker/build/|^scripts/ci/v8/(archives\.json|source\.json|engine\.py|.*\.patch)$|^scripts/pipboy/|^\.github/workflows/pipboy\.yml$)' + PLATFORM_SENSITIVE='((^|/)Cargo\.(toml|lock)$|(^|/)build\.rs$|\.(cc|h)$|^docker/build/|^scripts/ci/v8/(archives\.json|source\.json|engine\.py|.*\.patch)$|^rust-toolchain\.toml$)' matrix() { jq -c --arg tier "$1" 'map(select($tier == "all" or .tier == $tier))' <<< "$NATIVE_ALL"; } if [ "$EVENT" != "pull_request" ]; then @@ -318,6 +318,13 @@ jobs: cid="$(docker create pipboy-native)" docker cp "$cid:/artifacts/agentos-sidecar" "$RUNNER_TEMP/agentos-native-sidecar" docker rm "$cid" + # Transfer only the release binary, never Cargo or package caches. + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: smoke-sidecar-${{ matrix.platform }} + path: ${{ runner.temp }}/agentos-native-sidecar + retention-days: 1 + if-no-files-found: error - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 # No pnpm cache: this job runs fork code and doesn't need one. - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 @@ -337,9 +344,44 @@ jobs: path: ${{ runner.temp }}/reports/native-agentos-native-sidecar-${{ matrix.platform }}.json retention-days: 90 + native-smoke: + name: execute · ${{ matrix.platform }} + needs: [changes, native] + if: needs.changes.outputs.native != 'none' + runs-on: ${{ matrix.smoke_runner }} + timeout-minutes: 10 + permissions: + contents: read + strategy: + fail-fast: false + matrix: + include: ${{ fromJSON(needs.changes.outputs.native_matrix) }} + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 + with: + name: smoke-sidecar-${{ matrix.platform }} + path: ${{ runner.temp }}/native-smoke + - name: Enable x64 execution on the macOS arm64 host + if: matrix.platform == 'darwin-x64' + run: softwareupdate --install-rosetta --agree-to-license + - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 + with: + node-version: 24 + - run: pnpm install --frozen-lockfile --ignore-scripts --filter '@rivet-dev/agentos...' + - name: Execute the release sidecar through its matching public client + env: + AGENTOS_SIDECAR_BIN: ${{ runner.temp }}/native-smoke/agentos-native-sidecar + run: | + chmod +x "$AGENTOS_SIDECAR_BIN" + pnpm --dir packages/core exec vitest run tests/native-platform-smoke.test.ts --reporter=verbose + report: name: report - needs: [changes, footprint, hygiene, native] + needs: [changes, footprint, hygiene, native, native-smoke] # Not `always()`: a run cancelled by a newer push must not post its partial results. if: ${{ !cancelled() }} runs-on: ubuntu-latest diff --git a/crates/executor-v8-runtime/src/isolate.rs b/crates/executor-v8-runtime/src/isolate.rs index 6113816eff..04fd3cc384 100644 --- a/crates/executor-v8-runtime/src/isolate.rs +++ b/crates/executor-v8-runtime/src/isolate.rs @@ -58,7 +58,7 @@ pub(crate) fn plan_native_terminal_stack() -> Result= plan.stack_low + plan.guard_bytes + 64 * 1024); + }) + .unwrap() + .join() + .unwrap(); + thread::Builder::new() + .stack_size(256 * 1024) + .spawn(|| { + let error = plan_native_terminal_stack() + .err() + .expect("undersized stack rejected"); + assert!( + error.contains("ERR_AGENTOS_NATIVE_STACK_RESERVE"), + "{error}" + ); + }) + .unwrap() + .join() + .unwrap(); + } +} diff --git a/crates/executor-v8-runtime/src/v8_thread_support.cc b/crates/executor-v8-runtime/src/v8_thread_support.cc index 7fcc762476..cc7bacbb28 100644 --- a/crates/executor-v8-runtime/src/v8_thread_support.cc +++ b/crates/executor-v8-runtime/src/v8_thread_support.cc @@ -60,7 +60,7 @@ extern "C" void agentos_v8_suppress_microtasks_end(void* storage) { #include #include -#if defined(__linux__) +#if defined(__linux__) || defined(__APPLE__) #include #endif @@ -73,7 +73,7 @@ extern "C" void agentos_v8_suppress_microtasks_end(void* storage) { #define AGENTOS_TERMINAL_STACK_ASAN 1 #endif -// Finite Linux/x86-64 prototype. No V8 internals or native stack mappings change. +// Use the current thread's OS-reported bounds; never alter native stack mappings. struct AgentOsTerminalStackPlan { uintptr_t stack_low; uintptr_t stack_high; @@ -84,7 +84,11 @@ struct AgentOsTerminalStackPlan { }; extern "C" int agentos_v8_plan_terminal_stack(AgentOsTerminalStackPlan* out) { -#if defined(__linux__) && defined(__x86_64__) && !defined(AGENTOS_TERMINAL_STACK_ASAN) +#if (defined(__linux__) || defined(__APPLE__)) && \ + (defined(__x86_64__) || defined(__aarch64__)) && \ + !defined(AGENTOS_TERMINAL_STACK_ASAN) + if (!out) return EINVAL; +#if defined(__linux__) pthread_attr_t attributes; int result = pthread_getattr_np(pthread_self(), &attributes); if (result != 0) return result; @@ -99,9 +103,19 @@ extern "C" int agentos_v8_plan_terminal_stack(AgentOsTerminalStackPlan* out) { const uintptr_t low = reinterpret_cast(stack); if (size > std::numeric_limits::max() - low) return EOVERFLOW; const uintptr_t high = low + size; +#else + // Darwin reports the top of a downward-growing stack. libpthread computes + // its size as stackaddr - stackbottom; the guard is outside that usable range. + const uintptr_t high = reinterpret_cast( + pthread_get_stackaddr_np(pthread_self())); + const size_t size = pthread_get_stacksize_np(pthread_self()); + if (size == 0 || size > high) return ERANGE; + const uintptr_t low = high - size; + const size_t guard = 0; +#endif volatile unsigned char marker = 0; const uintptr_t sp = reinterpret_cast(&marker); - // Match the pinned, non-ASan x64 V8 default budget, but explicitly own its + // Match the pinned, non-ASan x64/arm64 V8 default budget, but explicitly own its // origin. Reserving below it changes recursion depth slightly from V8's own // deeper initialization frame; it does not increase the guest byte budget. constexpr uintptr_t guest_budget = 984 * 1024; diff --git a/packages/core/tests/native-platform-smoke.test.ts b/packages/core/tests/native-platform-smoke.test.ts new file mode 100644 index 0000000000..b6d4b30cfc --- /dev/null +++ b/packages/core/tests/native-platform-smoke.test.ts @@ -0,0 +1,48 @@ +import { expect, test } from "vitest"; +import { AgentOs } from "../src/index.js"; + +// Runs the actual release binary on every supported OS/CPU. Cross-compilation +// alone cannot detect unsupported thread-stack discovery or snapshot startup. +test("native release creates, interrupts and disposes VMs on a reused sidecar", async () => { + const sidecar = await AgentOs.createSidecar(); + try { + for (let iteration = 0; iteration < 3; iteration++) { + const vm = await AgentOs.create({ + sidecar: { kind: "explicit", handle: sidecar }, + defaultSoftware: false, + permissions: { network: "deny" }, + }); + try { + const result = await vm.javascript.evaluate("21 * 2"); + expect(result.outcome).toBe("succeeded"); + expect(result.value).toBe(42); + await vm.filesystem.writeFile("/home/agentos/platform.txt", "native\n"); + expect( + new TextDecoder().decode( + await vm.filesystem.readFile("/home/agentos/platform.txt"), + ), + ).toBe("native\n"); + const wasm = await vm.javascript.evaluate(`(() => { + const bytes = new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,127,3,2,1,0,7,10,1,6,97,110,115,119,101,114,0,0,10,6,1,4,0,65,42,11]); + return new WebAssembly.Instance(new WebAssembly.Module(bytes)).exports.answer() + })()`); + expect(wasm.outcome, JSON.stringify(wasm)).toBe("succeeded"); + expect(wasm.value).toBe(42); + const interrupted = await vm.javascript.evaluate( + "(() => { for (;;) {} })()", + { + timeoutMs: 250, + }, + ); + expect(interrupted.outcome).toBe("timed_out"); + const recovered = await vm.javascript.evaluate("6 * 7"); + expect(recovered.outcome).toBe("succeeded"); + expect(recovered.value).toBe(42); + } finally { + await vm.dispose(); + } + } + } finally { + await sidecar.dispose(); + } +}, 60_000); From 294fd97051dffbcafcae760f1e66abe1a1aa64dd Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 18:45:08 -0800 Subject: [PATCH 17/19] test: run WASM conformance against compiled executors --- crates/vm/tests/guest_identity.rs | 6 ++- crates/vm/tests/wasm_raw_abi.rs | 27 +++++++------ crates/vm/tests/wasm_software_parity.rs | 51 +++++++++++++++---------- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/crates/vm/tests/guest_identity.rs b/crates/vm/tests/guest_identity.rs index 7e75356f0a..3587f942a9 100644 --- a/crates/vm/tests/guest_identity.rs +++ b/crates/vm/tests/guest_identity.rs @@ -995,7 +995,11 @@ fn guest_identity_cases() { for case_name in GUEST_IDENTITY_CASES { let backends: &[Option<&str>] = if case_name.starts_with("wasm_") { - &[Some("v8"), Some("wasmtime")] + &[ + Some("v8"), + #[cfg(feature = "wasm-wasmtime")] + Some("wasmtime"), + ] } else { &[None] }; diff --git a/crates/vm/tests/wasm_raw_abi.rs b/crates/vm/tests/wasm_raw_abi.rs index afb7a451ca..3443d065d2 100644 --- a/crates/vm/tests/wasm_raw_abi.rs +++ b/crates/vm/tests/wasm_raw_abi.rs @@ -385,18 +385,21 @@ fn run_raw_module_with_metadata( metadata.clone(), StandaloneWasmBackend::V8, ); - let wasmtime = run_raw_module_for_backend( - &format!("{name}-wasmtime"), - module, - tier, - metadata, - StandaloneWasmBackend::Wasmtime, - ); - assert_eq!( - wasmtime, v8, - "Wasmtime and V8-WASM raw ABI outcomes diverged for {name}" - ); - wasmtime + #[cfg(feature = "wasm-wasmtime")] + { + let wasmtime = run_raw_module_for_backend( + &format!("{name}-wasmtime"), + module, + tier, + metadata, + StandaloneWasmBackend::Wasmtime, + ); + assert_eq!( + wasmtime, v8, + "Wasmtime and V8-WASM raw ABI outcomes diverged for {name}" + ); + } + v8 } fn run_raw_module_for_backend( diff --git a/crates/vm/tests/wasm_software_parity.rs b/crates/vm/tests/wasm_software_parity.rs index 2eff42dad7..5853b08173 100644 --- a/crates/vm/tests/wasm_software_parity.rs +++ b/crates/vm/tests/wasm_software_parity.rs @@ -257,14 +257,17 @@ fn assert_command_parity(command: &str, args: &[&str]) -> (String, String, i32) args, StandaloneWasmBackend::V8, ); - let wasmtime = run_command( - &format!("software-{command}-wasmtime"), - command, - args, - StandaloneWasmBackend::Wasmtime, - ); - assert_eq!(wasmtime, v8, "software command {command} diverged"); - wasmtime + #[cfg(feature = "wasm-wasmtime")] + { + let wasmtime = run_command( + &format!("software-{command}-wasmtime"), + command, + args, + StandaloneWasmBackend::Wasmtime, + ); + assert_eq!(wasmtime, v8, "software command {command} diverged"); + } + v8 } fn assert_command_parity_with_files( @@ -279,15 +282,18 @@ fn assert_command_parity_with_files( extra_commands, StandaloneWasmBackend::V8, ); - let wasmtime = run_command_with_files( - &format!("software-{command}-wasmtime"), - command, - args, - extra_commands, - StandaloneWasmBackend::Wasmtime, - ); - assert_eq!(wasmtime, v8, "software command {command} diverged"); - wasmtime + #[cfg(feature = "wasm-wasmtime")] + { + let wasmtime = run_command_with_files( + &format!("software-{command}-wasmtime"), + command, + args, + extra_commands, + StandaloneWasmBackend::Wasmtime, + ); + assert_eq!(wasmtime, v8, "software command {command} diverged"); + } + v8 } fn run_curl_http_backend(backend: StandaloneWasmBackend) -> (String, String, i32) { @@ -508,8 +514,11 @@ fn vim_batch_edit_matches_v8_wasm() { #[ignore = "requires the generated curl command artifact"] fn curl_network_path_matches_v8_wasm() { let v8 = run_curl_http_backend(StandaloneWasmBackend::V8); - let wasmtime = run_curl_http_backend(StandaloneWasmBackend::Wasmtime); - assert_eq!(wasmtime, v8, "curl HTTP behavior diverged"); - assert_eq!(wasmtime.2, 0, "stderr: {}", wasmtime.1); - assert_eq!(wasmtime.0, "agentos-network-path\n"); + #[cfg(feature = "wasm-wasmtime")] + { + let wasmtime = run_curl_http_backend(StandaloneWasmBackend::Wasmtime); + assert_eq!(wasmtime, v8, "curl HTTP behavior diverged"); + } + assert_eq!(v8.2, 0, "stderr: {}", v8.1); + assert_eq!(v8.0, "agentos-network-path\n"); } From 215bed071f5f58c14fcc36446c2c9e839b909b65 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 19:23:16 -0800 Subject: [PATCH 18/19] fix: preserve retained context input and retire completed I/O --- .github/workflows/ci.yml | 2 +- crates/vm/src/language_execution.rs | 13 ---------- crates/vm/tests/architecture_guards.rs | 2 +- .../build-tools/bridge-src/builtins/net.ts | 25 +++++++++++++------ 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d946d5740f..22f51e080b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -845,7 +845,7 @@ jobs: cargo test --release --locked -p agentos-actor --lib runtime::tests:: \ -- --ignored --nocapture --test-threads=1 - name: Run all sidecar tests through the selected VM backend - run: cargo test --release -p agentos-vm --features all-executors --tests -- --test-threads=1 + run: cargo test --release -p agentos-vm --features all-executors --tests --no-fail-fast -- --test-threads=1 - name: Run native-vs-WASM C conformance through the selected VM backend run: pnpm --dir packages/core exec vitest run --root ../.. toolchain/conformance/c-parity.test.ts --reporter=verbose --maxWorkers=1 --minWorkers=1 - name: Run core WASM and cross-runtime integration serially diff --git a/crates/vm/src/language_execution.rs b/crates/vm/src/language_execution.rs index 5e9c7f116f..ea49452d8a 100644 --- a/crates/vm/src/language_execution.rs +++ b/crates/vm/src/language_execution.rs @@ -1841,19 +1841,6 @@ where ) .await?; } - // Attached executions have no writeStdin API after admission. Their - // supplied bytes are the entire input stream, so publish EOF as soon - // as the initial write has been accepted. Spawned executions keep the - // pipe open for later writes through WriteExecutionStdin. - if !operation.background { - self.close_stdin( - request, - CloseStdinRequest { - process_id: process_id.clone(), - }, - ) - .await?; - } // An attached run outside a context returns no stdin handle, so the // `stdin` option is all its input: end it, as `< file` does on Linux. // Left open, a command that reads to end of input (`cat`, `tr`) runs diff --git a/crates/vm/tests/architecture_guards.rs b/crates/vm/tests/architecture_guards.rs index 4bc68bdb43..1126c2a893 100644 --- a/crates/vm/tests/architecture_guards.rs +++ b/crates/vm/tests/architecture_guards.rs @@ -774,7 +774,7 @@ fn maintained_wasm_surfaces_are_mechanically_gated_on_v8() { "AGENTOS_TEST_WASM_BACKEND: ${{ matrix.backend }}", "EXPECT_WASM_BACKEND_MATRIX:", "required WASM backend CI job did not succeed", - "cargo test --release -p agentos-vm --features all-executors --tests -- --test-threads=1", + "cargo test --release -p agentos-vm --features all-executors --tests --no-fail-fast -- --test-threads=1", "toolchain/conformance/c-parity.test.ts", "wasm-c-parity-fixtures", "packages/core exec vitest run", diff --git a/packages/build-tools/bridge-src/builtins/net.ts b/packages/build-tools/bridge-src/builtins/net.ts index ba9d375d2e..4c5bcb6f56 100644 --- a/packages/build-tools/bridge-src/builtins/net.ts +++ b/packages/build-tools/bridge-src/builtins/net.ts @@ -23,7 +23,7 @@ import { registerCapabilityReadiness, unregisterCapabilityReadiness, } from "./readiness.js"; -import { setImmediate } from "./timers.js"; +import { clearImmediate, setImmediate } from "./timers.js"; const CanonicalDuplex = streamStdlibModuleNs.Duplex ?? streamStdlibModuleNs.default?.Duplex; @@ -1669,12 +1669,20 @@ exposeCustomGlobal("__agentOSNetBridgeMetrics", { }, }); -function yieldBridgeMacrotask() { +function yieldBridgeMacrotask(socket = undefined) { return new Promise((resolve) => { - if (typeof setImmediate === "function") { - setImmediate(resolve); - } else { - setTimeout(resolve, 0); + const finish = () => { + if (socket) socket._cancelBridgeReadYield = null; + resolve(); + }; + const immediate = setImmediate(finish); + if (socket) { + // The yield belongs to this read pump. Closing its socket must both + // cancel the timer and settle the pump, without running unrelated work. + socket._cancelBridgeReadYield = () => { + clearImmediate(immediate); + finish(); + }; } }); } @@ -1838,6 +1846,7 @@ var NetSocket = class _NetSocket extends CanonicalDuplex { _keepAliveDelaySeconds = 0; _refed = true; _bridgeReadLoopRunning = false; + _cancelBridgeReadYield = null; _bridgeReadRpcInFlight = false; _bridgeReadPumpQueued = false; _pendingBridgeWake = false; @@ -2178,6 +2187,7 @@ var NetSocket = class _NetSocket extends CanonicalDuplex { this._maybeEmitSocketClose(); } _destroy(error, callback) { + this._cancelBridgeReadYield?.(); debugBridgeNetwork( "socket destroy", this._socketId, @@ -2442,6 +2452,7 @@ var NetSocket = class _NetSocket extends CanonicalDuplex { this._maybeEmitSocketClose(); } _releaseBridgeSocket() { + this._cancelBridgeReadYield?.(); if ( this._bridgeReleased || !this._socketId || @@ -2998,7 +3009,7 @@ var NetSocket = class _NetSocket extends CanonicalDuplex { if (deliveredPayloads > 0) { countNetBridgeMetric("readMacrotaskYields"); const yieldStartUs = traceMetrics ? netBridgeNowUs() : 0; - await yieldBridgeMacrotask(); + await yieldBridgeMacrotask(this); if (traceMetrics) { const yieldElapsedUs = netBridgeNowUs() - yieldStartUs; countNetBridgeMetric("readMacrotaskYieldElapsedUs", yieldElapsedUs); From 9c15cbc8dd63f357a9600c3123d8ada51679164c Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sat, 21 Nov 2026 20:11:48 -0800 Subject: [PATCH 19/19] fix: resolve expanded runtime qualification failures --- .github/workflows/ci.yml | 31 +++ crates/client/src/shell.rs | 14 +- crates/client/tests/common/mod.rs | 2 +- crates/client/tests/overload_e2e.rs | 21 +- crates/client/tests/process_e2e.rs | 44 +++- crates/client/tests/shell_e2e.rs | 34 +-- crates/client/tests/wasm_command_mount_e2e.rs | 21 +- .../src/backend/submission.rs | 5 + crates/executor-contract/src/host/mod.rs | 7 + crates/executor-contract/src/host/network.rs | 2 + crates/executor-python-v8-pyodide/src/lib.rs | 8 +- .../assets/runners/python-runner.mjs | 39 ++- crates/executor-wasm-v8/src/lib.rs | 6 + crates/vm-kernel/src/resource_accounting.rs | 2 +- crates/vm/src/execution/child_process.rs | 225 +++++++++++++++++- .../execution/host_dispatch/network_compat.rs | 18 ++ crates/vm/src/execution/network/managed.rs | 17 +- crates/vm/src/execution/owned_rpc.rs | 22 ++ crates/vm/src/execution/process.rs | 58 ++++- crates/vm/src/execution/process_events.rs | 116 +++++---- crates/vm/src/service.rs | 75 +++++- crates/vm/src/vm.rs | 4 +- .../vm/tests/fixtures/limits-inventory.json | 55 ++++- crates/vm/tests/python.rs | 93 ++++++-- crates/vm/tests/service.rs | 154 ++++++++++-- crates/vm/tests/xfstests_correctness.rs | 35 ++- .../build-tools/bridge-src/builtins/fs.ts | 7 +- .../build-tools/bridge-src/builtins/http.ts | 11 +- .../builtins/uint8array-encoding.ts | 11 +- packages/core/src/event-buffer.ts | 40 ++-- packages/core/src/kernel-proxy.ts | 4 +- packages/core/src/runtime-compat.ts | 2 +- packages/core/src/runtime.ts | 2 +- packages/core/src/sidecar/rpc-client.ts | 38 +-- packages/core/src/test-runtime.ts | 2 +- packages/core/tests/bridge-vm-budgets.test.ts | 4 +- .../core/tests/dispose-exit-status.test.ts | 8 +- packages/core/tests/event-buffer.test.ts | 70 ++++++ .../core/tests/fs-read-file-abort.test.ts | 42 +++- .../http-cancellation-parity.nightly.test.ts | 14 +- .../node-binary-behavior.nightly.test.ts | 11 +- packages/core/tests/leak-rpc-client.test.ts | 19 +- .../tests/node-fs-recursive.native.test.ts | 6 +- ...-uint8array-encoding-memory.native.test.ts | 10 +- .../tests/process-lifecycle.nightly.test.ts | 3 + .../core/tests/process-tree-identity.test.ts | 108 +++++++-- .../tests/pty-line-discipline.nightly.test.ts | 5 + packages/core/tests/request-overload.test.ts | 12 +- .../tests/sidecar-process.nightly.test.ts | 24 +- .../sidecar-protocol-capacity.native.test.ts | 12 +- .../tests/software-projection.nightly.test.ts | 19 +- .../tests/vim-interactive.nightly.test.ts | 4 +- .../core/tests/vim-provides.nightly.test.ts | 4 +- software/curl/test/curl.nightly.test.ts | 16 +- software/duckdb/test/duckdb.nightly.test.ts | 2 +- 55 files changed, 1300 insertions(+), 318 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 22f51e080b..1b4df745ec 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -768,6 +768,37 @@ jobs: with: key: wasm-backend-${{ matrix.backend }} - run: pnpm install --frozen-lockfile + - name: Install native protocol fixture prerequisites + run: | + sudo apt-get update + sudo apt-get install --yes --no-install-recommends g++-14 pkg-config openssh-server curl + sudo mkdir -p /run/sshd + /usr/bin/curl --version | grep -w sftp + - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + id: curl-protocol-fixtures + with: + path: ${{ runner.temp }}/agentos-curl-protocol-fixtures + key: native-curl-protocol-v1-${{ runner.os }}-${{ runner.arch }}-gcc14-${{ hashFiles('toolchain/c/scripts/build-native-http3-fixture.sh', 'toolchain/c/scripts/check-generation-limit.sh') }} + - name: Build pinned native HTTP/3 fixtures + if: steps.curl-protocol-fixtures.outputs.cache-hit != 'true' + timeout-minutes: 15 + env: + PREFIX: ${{ runner.temp }}/agentos-http3 + CACHE_DIR: ${{ runner.temp }}/agentos-http3-sources + JOBS: '4' + NATIVE_CC: gcc-14 + NATIVE_CXX: g++-14 + run: | + bash toolchain/c/scripts/build-native-http3-fixture.sh + mkdir -p "$RUNNER_TEMP/agentos-curl-protocol-fixtures" + cp -L "$PREFIX/bin/curl" "$PREFIX/bin/nghttpx" "$RUNNER_TEMP/agentos-curl-protocol-fixtures/" + - name: Select native protocol fixtures + run: | + fixture_dir="$RUNNER_TEMP/agentos-curl-protocol-fixtures" + "$fixture_dir/curl" --version | grep -w HTTP3 + "$fixture_dir/nghttpx" --version + printf 'AGENTOS_NATIVE_HTTP3_CURL=%s/curl\nAGENTOS_NGHTTPX_HTTP3_SERVER=%s/nghttpx\n' "$fixture_dir" "$fixture_dir" >> "$GITHUB_ENV" + printf 'SSHD=/usr/sbin/sshd\nAGENTOS_NATIVE_SSH_CURL=/usr/bin/curl\n' >> "$GITHUB_ENV" - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: wasm-commands diff --git a/crates/client/src/shell.rs b/crates/client/src/shell.rs index 4260075ea1..9798c153a4 100644 --- a/crates/client/src/shell.rs +++ b/crates/client/src/shell.rs @@ -11,9 +11,9 @@ //! the wire, while the public boundary keeps the synthetic `shell-N` id. //! //! Stream routing mirrors the TS PTY path: the public `data` stream (`on_shell_data`) carries stdout -//! and stderr in the order received from the sidecar. stderr is also delivered on an optional -//! channel-specific diagnostic tap (`on_shell_stderr` + [`OpenShellOptions::on_stderr`]); terminal -//! renderers consume only `data` so prompts and control sequences are neither reordered nor doubled. +//! and stderr merged by the PTY. The optional diagnostic tap (`on_shell_stderr` + +//! [`OpenShellOptions::on_stderr`]) receives only explicitly stderr-labelled sidecar events; +//! it cannot recover descriptor identity from merged PTY bytes. Renderers consume only `data`. use std::collections::{BTreeMap, VecDeque}; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -381,8 +381,8 @@ impl AgentOs { /// pending-shell-exit set so `dispose` can drain it (two-phase teardown). /// /// Stdout and stderr are fanned into the shell's ordered `data` broadcast (`on_shell_data`). - /// Stderr is also fanned into a dedicated diagnostic broadcast (`on_shell_stderr` and the - /// [`OpenShellOptions::on_stderr`] callback); terminal renderers should consume only `data`. + /// The PTY merges both descriptors. Only explicitly stderr-labelled sidecar events also reach + /// `on_shell_stderr` and [`OpenShellOptions::on_stderr`]; renderers should consume only `data`. pub fn open_shell(&self, mut options: OpenShellOptions) -> Result { let inner = self.inner(); if inner.disposed.load(Ordering::SeqCst) { @@ -556,7 +556,7 @@ impl AgentOs { // control sequences retain their original stdout/stderr order. let _ = data_tx.send(output.chunk.clone()); if output.channel == StreamChannel::Stderr { - // Channel identity remains available as an optional diagnostic tap. + // Forward only explicit diagnostic events, never guess from PTY bytes. let _ = stderr_tx.send(output.chunk); } } @@ -1073,7 +1073,7 @@ impl AgentOs { /// Subscribe to a shell's stderr. SYNC register; multi-handler; dropping the returned stream is /// the unsubscribe. This is the optional diagnostic channel backing the TS `onStderr` option; - /// stderr is also present once in ordered `on_shell_data`. Errors with + /// the PTY normally merges stderr into `on_shell_data`, leaving this tap empty. Errors with /// [`ClientError::ShellNotFound`]. pub fn on_shell_stderr( &self, diff --git a/crates/client/tests/common/mod.rs b/crates/client/tests/common/mod.rs index 15c7d1c6d4..acbf97debe 100644 --- a/crates/client/tests/common/mod.rs +++ b/crates/client/tests/common/mod.rs @@ -17,7 +17,7 @@ use agentos_client::AgentOs; static INIT: Once = Once::new(); -fn test_wasm_backend() -> Option { +pub fn test_wasm_backend() -> Option { match std::env::var("AGENTOS_TEST_WASM_BACKEND").as_deref() { Ok("v8") => Some(agentos_client::StandaloneWasmBackend::V8), Ok("wasmtime") => Some(agentos_client::StandaloneWasmBackend::Wasmtime), diff --git a/crates/client/tests/overload_e2e.rs b/crates/client/tests/overload_e2e.rs index f93b6dc527..1a8380e6b6 100644 --- a/crates/client/tests/overload_e2e.rs +++ b/crates/client/tests/overload_e2e.rs @@ -26,12 +26,14 @@ async fn request_spike_fails_only_with_typed_retryable_errors_and_dispose_still_ disposal.expect("dispose succeeds while a spike saturates the sidecar"); let mut succeeded = 0; - for result in results { + let mut rejected_paths = Vec::new(); + for (path, result) in paths.iter().zip(results) { match result { Ok(false) => succeeded += 1, Ok(true) => panic!("spike paths do not exist"), Err(error) => match error.downcast_ref::() { Some(ClientError::ResourceLimit { details, .. }) => { + rejected_paths.push(path); assert_eq!(details.retryable, Some(true)); assert_eq!(details.errno.as_deref(), Some("EAGAIN")); assert!(details @@ -43,10 +45,19 @@ async fn request_spike_fails_only_with_typed_retryable_errors_and_dispose_still_ }, } } - assert!( - succeeded >= SPIKE / 2, - "most of the spike must be served, got {succeeded}/{SPIKE}" - ); + // A burst larger than the bounded ingress and request queues has no + // guaranteed first-attempt success ratio: that depends on scheduling. + // Require actual progress, then prove every typed rejection is recoverable + // after the burst drains, without changing any queue limit. + assert!(succeeded > 0, "the burst must make progress"); + for path in rejected_paths { + assert!(!busy + .exists(path) + .await + .expect("retry after overload drains")); + succeeded += 1; + } + assert_eq!(succeeded, SPIKE, "every request must eventually succeed"); busy.shutdown().await.expect("dispose the busy VM"); } diff --git a/crates/client/tests/process_e2e.rs b/crates/client/tests/process_e2e.rs index 08e4aa8169..9e3db9bb6f 100644 --- a/crates/client/tests/process_e2e.rs +++ b/crates/client/tests/process_e2e.rs @@ -1011,8 +1011,8 @@ async fn stdin_written_right_after_spawn_arrives_in_order() { } /// A language process that exits while another process floods stdout reports its real exit status -/// when the VM is disposed. Its exit event queues behind the flood on the event lane, so only the -/// dispose response's list of finished executions can deliver it. +/// when the VM is disposed. Observe authoritative termination before disposal: a host sleep does +/// not prove the guest timer has run while another process floods the event lane. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn a_process_that_exits_behind_an_output_flood_reports_its_exit_status_on_shutdown() { const TEST: &str = @@ -1023,7 +1023,7 @@ async fn a_process_that_exits_behind_an_output_flood_reports_its_exit_status_on_ let os = common::new_vm().await; let flood = os .spawn_javascript( - "for (let i = 0; i < 50000; i++) console.log('flood ' + i); setInterval(() => {}, 1000)", + "process.on('SIGTERM', () => {}); require('node:fs').writeFileSync('/tmp/flood-ready', '1'); for (let i = 0; i < 50000; i++) console.log('flood ' + i); setInterval(() => {}, 1000)", agentos_client::LanguageSpawnOptions::default(), ) .await @@ -1032,6 +1032,17 @@ async fn a_process_that_exits_behind_an_output_flood_reports_its_exit_status_on_ let os = os.clone(); tokio::spawn(async move { os.wait_process(flood.pid).await }) }; + tokio::time::timeout(std::time::Duration::from_secs(10), async { + while !os + .exists("/tmp/flood-ready") + .await + .expect("flood readiness") + { + tokio::time::sleep(std::time::Duration::from_millis(20)).await; + } + }) + .await + .expect("flood installed its SIGTERM handler before disposal"); let exits = os .spawn_javascript( "setTimeout(() => process.exit(3), 100)", @@ -1043,8 +1054,29 @@ async fn a_process_that_exits_behind_an_output_flood_reports_its_exit_status_on_ let os = os.clone(); tokio::spawn(async move { os.wait_process(exits.pid).await }) }; - // Dispose once the exit is due, before the host drains the flood ahead of it. - tokio::time::sleep(std::time::Duration::from_millis(300)).await; + // Keep the flood running, but prove the other process really exited before + // initiating disposal. A fixed host sleep can kill a guest whose timer has + // not run yet and cannot establish the exit-status preservation contract. + tokio::time::timeout(std::time::Duration::from_secs(10), async { + loop { + if os + .all_processes() + .await + .expect("kernel process snapshot") + .iter() + .any(|process| { + process.pid == exits.pid + && process.status == agentos_client::ProcessStatus::Exited + && process.exit_code == Some(3) + }) + { + break; + } + tokio::time::sleep(std::time::Duration::from_millis(20)).await; + } + }) + .await + .expect("guest exit must be confirmed while the flood is still running"); tokio::time::timeout(std::time::Duration::from_secs(30), os.shutdown()) .await .expect("bounded VM cleanup") @@ -1057,7 +1089,7 @@ async fn a_process_that_exits_behind_an_output_flood_reports_its_exit_status_on_ matches!(exit, Ok(3)), "a process that exited with 3 just before dispose reported {exit:?}" ); - // The flood was still running, so the dispose killed it: SIGKILL is exit status 137. + // The flood explicitly ignores SIGTERM, so disposal must escalate to SIGKILL (137). let flood_exit = tokio::time::timeout(std::time::Duration::from_secs(10), flood_waiter) .await .expect("the flood's wait settles once the VM is disposed") diff --git a/crates/client/tests/shell_e2e.rs b/crates/client/tests/shell_e2e.rs index aa812c31cd..648adf2037 100644 --- a/crates/client/tests/shell_e2e.rs +++ b/crates/client/tests/shell_e2e.rs @@ -149,7 +149,7 @@ async fn shell_surface_open_write_data_resize_close() { let _ = data_tx.send(event.data); }) .expect("on_shell_data for live shell"); - // Stderr remains available as a channel-specific diagnostic tap. + // A PTY merges fd 1 and fd 2; no separate stderr event should be invented. let (stderr_tx, mut stderr_rx) = tokio::sync::mpsc::unbounded_channel(); let _stderr = os .on_shell_stderr(&shell.shell_id, move |event| { @@ -160,7 +160,7 @@ async fn shell_surface_open_write_data_resize_close() { // --- write_shell: prove execution and stdout/stderr ordering on the PTY stream ---------------- // Neither output marker occurs in the input, so PTY input echo alone cannot satisfy this // assertion. The process writes stdout before stderr, proving the combined stream retains wire - // order while the stderr bytes remain available through the diagnostic tap. + // order after the PTY merges both descriptors. os.write_shell( &shell.shell_id, StdinInput::Text("hello-shell\n".to_string()), @@ -192,22 +192,24 @@ async fn shell_surface_open_write_data_resize_close() { "combined PTY stream reordered stdout/stderr: {ordered_output:?}" ); - let diagnostic_saw_stderr = tokio::time::timeout(std::time::Duration::from_secs(10), async { - let mut acc = Vec::::new(); - while let Some(chunk) = stderr_rx.recv().await { - acc.extend_from_slice(&chunk); - if String::from_utf8_lossy(&acc).contains("ERR:hello-shell") { - return true; - } - } - false - }) - .await - .unwrap_or(false); assert!( - diagnostic_saw_stderr, - "stderr diagnostic stream should retain the executed stderr output" + matches!( + stderr_rx.try_recv(), + Err(tokio::sync::mpsc::error::TryRecvError::Empty) + ), + "a PTY must not duplicate merged output on the stderr tap" ); + // Pipe execution retains separate descriptors, unlike the PTY above. + let piped = os + .exec_process( + "printf pipe-out; printf pipe-err >&2", + agentos_client::ExecOptions::default(), + ) + .await + .expect("execute with separate pipes"); + assert_eq!(piped.exit_code, 0); + assert_eq!(piped.stdout, "pipe-out"); + assert_eq!(piped.stderr, "pipe-err"); // --- resize_shell: validates existence and forwards the native PTY resize ---------------------- os.resize_shell(&shell.shell_id, 120, 40) diff --git a/crates/client/tests/wasm_command_mount_e2e.rs b/crates/client/tests/wasm_command_mount_e2e.rs index 9f46315646..a8b121f440 100644 --- a/crates/client/tests/wasm_command_mount_e2e.rs +++ b/crates/client/tests/wasm_command_mount_e2e.rs @@ -45,22 +45,27 @@ async fn wasm_command_software_mounts_into_vm() { } #[tokio::test] -async fn every_public_wasm_backend_selector_executes_projected_commands() { - if !common::require_sidecar("every_public_wasm_backend_selector_executes_projected_commands") { +async fn configured_wasm_backend_selectors_execute_projected_commands() { + if !common::require_sidecar("configured_wasm_backend_selectors_execute_projected_commands") { return; } let Some(package_dir) = common::coreutils_package_dir() else { eprintln!( - "skipping every_public_wasm_backend_selector_executes_projected_commands: coreutils package artifacts absent" + "skipping configured_wasm_backend_selectors_execute_projected_commands: coreutils package artifacts absent" ); return; }; - for backend in [ - StandaloneWasmBackend::V8, - StandaloneWasmBackend::Wasmtime, - StandaloneWasmBackend::WasmtimeThreads, - ] { + // The normal sidecar contains V8 only. Optional Wasmtime qualification + // selects its separately built sidecar through the same suite environment. + let backends = match common::test_wasm_backend().unwrap_or(StandaloneWasmBackend::V8) { + StandaloneWasmBackend::V8 => vec![StandaloneWasmBackend::V8], + StandaloneWasmBackend::Wasmtime | StandaloneWasmBackend::WasmtimeThreads => vec![ + StandaloneWasmBackend::Wasmtime, + StandaloneWasmBackend::WasmtimeThreads, + ], + }; + for backend in backends { let os = AgentOs::create(AgentOsConfig { packages: vec![PackageRef { path: package_dir.to_string_lossy().into_owned(), diff --git a/crates/executor-contract/src/backend/submission.rs b/crates/executor-contract/src/backend/submission.rs index 51627bf6dc..81b6fb9a38 100644 --- a/crates/executor-contract/src/backend/submission.rs +++ b/crates/executor-contract/src/backend/submission.rs @@ -108,6 +108,11 @@ impl fmt::Debug for ExecutionEventSubmitHandle { } impl ExecutionEventSubmitHandle { + /// Fixed retained-byte ceiling for one admitted request, not current free capacity. + pub fn request_byte_limit(&self) -> usize { + self.queue.retained_bytes.limit.maximum() + } + pub fn process(&self) -> HostProcessContext { self.process } diff --git a/crates/executor-contract/src/host/mod.rs b/crates/executor-contract/src/host/mod.rs index 51d1c7a3db..02d8fe4a74 100644 --- a/crates/executor-contract/src/host/mod.rs +++ b/crates/executor-contract/src/host/mod.rs @@ -206,6 +206,13 @@ impl ProcessHostCapabilitySet { self.process } + /// Fixed request ceiling when this capability uses the common event lane. + pub fn request_byte_limit(&self) -> Option { + self.event_submission + .as_ref() + .map(ExecutionEventSubmitHandle::request_byte_limit) + } + pub fn admit_request( &self, retained_bytes: usize, diff --git a/crates/executor-contract/src/host/network.rs b/crates/executor-contract/src/host/network.rs index ee44f7ea37..d1d7feba5b 100644 --- a/crates/executor-contract/src/host/network.rs +++ b/crates/executor-contract/src/host/network.rs @@ -199,6 +199,8 @@ pub enum NetworkOperation { }, ManagedCloseListener { listener_id: BoundedString, + /// Node closes also remove the guest pathname; POSIX descriptor close does not. + unlink_path: bool, }, ManagedTlsUpgrade { socket_id: BoundedString, diff --git a/crates/executor-python-v8-pyodide/src/lib.rs b/crates/executor-python-v8-pyodide/src/lib.rs index a36383bc76..0c68390218 100644 --- a/crates/executor-python-v8-pyodide/src/lib.rs +++ b/crates/executor-python-v8-pyodide/src/lib.rs @@ -3487,9 +3487,11 @@ fn python_javascript_sync_rpc_action( let digest = format!("{:x}", Sha256::digest(&contents)); if bundled_pyodide_file_sha256(file_name) != Some(digest.as_str()) { return Ok(Some(PythonJavascriptSyncRpcAction::Error { - code: "EIO", + // Integrity failures are not POSIX I/O failures. Preserve + // their diagnostic through Node's filesystem error adapter. + code: "ERR_AGENTOS_PYODIDE_INTEGRITY", message: format!( - "EIO: {file_name} does not match the sha256 pinned in pyodide-lock.json, write '{path}'" + "ERR_AGENTOS_PYODIDE_INTEGRITY: {file_name} does not match the sha256 pinned in pyodide-lock.json, write '{path}'" ), })); } @@ -4436,7 +4438,7 @@ mod tests { assert!(matches!( python_javascript_sync_rpc_action(&PyodidePaths::for_dist(&pyodide), &mut PythonManagedHostFiles::default(), &request) .expect("route cache write"), - Some(PythonJavascriptSyncRpcAction::Error { code: "EIO", message }) + Some(PythonJavascriptSyncRpcAction::Error { code: "ERR_AGENTOS_PYODIDE_INTEGRITY", message }) if message.contains("sha256 pinned in pyodide-lock.json") )); assert!(!super::pyodide_cache_path(&pyodide).join(file_name).exists()); diff --git a/crates/executor-v8-runtime/assets/runners/python-runner.mjs b/crates/executor-v8-runtime/assets/runners/python-runner.mjs index a41e0c4c5a..330e0aeb61 100644 --- a/crates/executor-v8-runtime/assets/runners/python-runner.mjs +++ b/crates/executor-v8-runtime/assets/runners/python-runner.mjs @@ -461,6 +461,22 @@ function normalizePythonBridgeError(error) { return normalized; } +function serializePythonSubprocessResult(result) { + // CBOR bridge bytes arrive as views. JSON.stringify alone turns them into + // numeric-key objects, losing their byte identity at the Python boundary. + const output = (value) => ArrayBuffer.isView(value) + ? { + __agentOSType: 'bytes', + base64: Buffer.from(value.buffer, value.byteOffset, value.byteLength).toString('base64'), + } + : value; + return JSON.stringify({ + ...result, + stdout: output(result.stdout ?? ''), + stderr: output(result.stderr ?? ''), + }); +} + function createPythonBridgeRpcBridge() { if (!bridgePythonRpc) { return null; @@ -588,7 +604,7 @@ function createPythonBridgeRpcBridge() { } catch (error) { throw new Error(`invalid Python subprocessRun payload JSON: ${formatError(error)}`); } - return JSON.stringify(requestSync('subprocessRun', { + return serializePythonSubprocessResult(requestSync('subprocessRun', { command, args, argv0, @@ -826,7 +842,7 @@ function createPythonFdRpcBridge() { } catch (error) { throw new Error(`invalid Python subprocessRun payload JSON: ${formatError(error)}`); } - return JSON.stringify(requestSync('subprocessRun', { + return serializePythonSubprocessResult(requestSync('subprocessRun', { command, args, argv0, @@ -1417,7 +1433,14 @@ class _AgentOsCompletedProcess: self.stdout = stdout self.stderr = stderr -def _agentos_subprocess_run(args, *, capture_output=False, check=False, cwd=None, env=None, input=None, shell=False, executable=None, text=False, encoding="utf-8", errors="strict", stdout=None, stderr=None, timeout=None, **kwargs): +def _agentos_subprocess_bytes(value): + if isinstance(value, str): + return value.encode("utf-8") + if not isinstance(value, dict) or value.get("__agentOSType") != "bytes" or not isinstance(value.get("base64"), str): + raise RuntimeError("invalid subprocess output byte payload") + return _agentos_base64.b64decode(value["base64"], validate=True) + +def _agentos_subprocess_run(args, *, capture_output=False, check=False, cwd=None, env=None, input=None, shell=False, executable=None, text=False, encoding=None, errors=None, stdout=None, stderr=None, timeout=None, **kwargs): del kwargs, stdout, stderr, timeout if isinstance(args, (str, bytes)): original_command = args.decode("utf-8") if isinstance(args, bytes) else args @@ -1464,11 +1487,11 @@ def _agentos_subprocess_run(args, *, capture_output=False, check=False, cwd=None ) except Exception as error: _agentos_raise_from_error(_agentos_vm_host_interface_error(error)) - stdout_bytes = payload.get("stdout", "").encode("utf-8") - stderr_bytes = payload.get("stderr", "").encode("utf-8") - if text or encoding is not None: - stdout_value = stdout_bytes.decode(encoding or "utf-8", errors=errors) - stderr_value = stderr_bytes.decode(encoding or "utf-8", errors=errors) + stdout_bytes = _agentos_subprocess_bytes(payload.get("stdout", "")) + stderr_bytes = _agentos_subprocess_bytes(payload.get("stderr", "")) + if text or encoding is not None or errors is not None: + stdout_value = stdout_bytes.decode(encoding or "utf-8", errors=errors or "strict") + stderr_value = stderr_bytes.decode(encoding or "utf-8", errors=errors or "strict") else: stdout_value = stdout_bytes stderr_value = stderr_bytes diff --git a/crates/executor-wasm-v8/src/lib.rs b/crates/executor-wasm-v8/src/lib.rs index bd2a966ca6..66e61f1755 100644 --- a/crates/executor-wasm-v8/src/lib.rs +++ b/crates/executor-wasm-v8/src/lib.rs @@ -2916,6 +2916,12 @@ fn start_wasm_javascript_execution( guest_runtime.kernel_stdio = false; guest_runtime.kernel_ipc = false; } + // Restricted WASM processes cannot use process IPC. Do not probe that + // capability from the JavaScript host shim before the WASM entrypoint. + // The host dispatcher still enforces the permission tier on every call. + if request.permission_tier != WasmPermissionTier::Full { + guest_runtime.kernel_ipc = false; + } let inline_code = match snapshot_mode { WasmSnapshotRunnerMode::Off => { diff --git a/crates/vm-kernel/src/resource_accounting.rs b/crates/vm-kernel/src/resource_accounting.rs index e932a84eaa..f8a6631aeb 100644 --- a/crates/vm-kernel/src/resource_accounting.rs +++ b/crates/vm-kernel/src/resource_accounting.rs @@ -781,7 +781,7 @@ impl ResourceAccountant { if let Some(limit) = self.limits.max_recursive_fs_entries { if entries > limit { return Err(ResourceError::out_of_memory(format!( - "recursive filesystem operation with {entries} entries exceeds configured limit {limit}" + "recursive filesystem operation with {entries} entries exceeds configured limit {limit}; raise limits.resources.maxRecursiveFsEntries when creating the VM" ))); } } diff --git a/crates/vm/src/execution/child_process.rs b/crates/vm/src/execution/child_process.rs index 84328245e2..3253cc6b74 100644 --- a/crates/vm/src/execution/child_process.rs +++ b/crates/vm/src/execution/child_process.rs @@ -5280,6 +5280,9 @@ where continue; }; if child_path.is_empty() { + // Adopted children no longer pass through the ordinary root pump. + // Service their parked reads/polls on the same readiness wake. + self.recheck_root_deferred_operations(vm_id, &root_process_id, false)?; loop { if host_services .len() @@ -5354,14 +5357,15 @@ where normalize_child_public_event(process, event)? }; match event { - ActiveExecutionEvent::Stdout(chunk) => { + event @ (ActiveExecutionEvent::Stdout(_) + | ActiveExecutionEvent::RoutedStdout(_)) => { let envelope = ProcessEventEnvelope { connection_id, session_id, vm_id: vm_id.to_owned(), child_path: Vec::new(), process_id: detached_process_id.clone(), - event: ActiveExecutionEvent::Stdout(chunk), + event, }; if let ProcessEventOffer::Paused(envelope) = self.offer_pending_process_event(envelope)? @@ -5386,14 +5390,15 @@ where drop(reservation); emitted_any = true; } - ActiveExecutionEvent::Stderr(chunk) => { + event @ (ActiveExecutionEvent::Stderr(_) + | ActiveExecutionEvent::RoutedStderr(_)) => { let envelope = ProcessEventEnvelope { connection_id, session_id, vm_id: vm_id.to_owned(), child_path: Vec::new(), process_id: detached_process_id.clone(), - event: ActiveExecutionEvent::Stderr(chunk), + event, }; if let ProcessEventOffer::Paused(envelope) = self.offer_pending_process_event(envelope)? @@ -14200,6 +14205,218 @@ pub(super) mod child_event_claim_tests { .await; } + #[tokio::test(flavor = "current_thread")] + async fn adopted_detached_child_keeps_inflight_completion_locator() { + tokio::task::LocalSet::new() + .run_until(async { + let (mut sidecar, vm_id) = sidecar_with_test_vm(4).await; + let id = "root/detached".to_owned(); + let (connection_id, session_id) = { + let mut vm = sidecar.vms.get_mut(&vm_id).unwrap(); + let child = + host_function_process(&mut vm, "detached", None).with_detached(true); + vm.active_processes.insert(id.clone(), child); + vm.detached_child_processes.insert(id.clone()); + (vm.connection_id.clone(), vm.session_id.clone()) + }; + let envelope = ProcessEventEnvelope { + connection_id, + session_id, + vm_id: vm_id.clone(), + process_id: "root".into(), + child_path: vec!["detached".into()], + event: ActiveExecutionEvent::DeferredPosixPollWake, + }; + assert!(sidecar + .route_received_internal_process_event(envelope) + .unwrap() + .is_none()); + let mut vm = sidecar.vms.get_mut(&vm_id).unwrap(); + let child = vm.active_processes.get_mut(&id).unwrap(); + let queued = child + .lease_pending_execution_event() + .expect("completion follows adopted owner"); + assert!(matches!( + queued.event(), + ActiveExecutionEvent::DeferredPosixPollWake + )); + }) + .await; + } + + #[tokio::test(flavor = "current_thread")] + async fn detached_rpc_prepared_before_adoption_uses_current_owner() { + tokio::task::LocalSet::new() + .run_until(async { + let (mut sidecar, vm_id) = sidecar_with_test_vm(4).await; + let (generation, pid) = { + let mut vm = sidecar.vms.get_mut(&vm_id).unwrap(); + let mut parent = host_function_process(&mut vm, "parent", None); + let child = host_function_process(&mut vm, "detached", Some(parent.kernel_pid)) + .with_detached(true); + let identity = (vm.generation, child.kernel_pid); + parent.child_processes.insert("child".into(), child); + vm.active_processes.insert("parent".into(), parent); + identity + }; + let reply = DirectHostReplyHandle::new( + HostCallIdentity { + generation, + pid, + call_id: 1, + }, + Arc::new(TestReply), + 1024, + ) + .unwrap(); + let vm_handle = sidecar.vms.handle(&vm_id).unwrap(); + let work = sidecar.prepare_owned_host_rpc( + &vm_id, + "parent", + &["child".into()], + vm_handle, + ExecutionHostCall { + request: HostRpcRequest { + id: 1, + method: "fs.writeFileSync".into(), + args: vec![json!("/rpc-survived"), host_bytes_value(b"alive")], + raw_bytes_args: Default::default(), + }, + reply: reply.clone(), + }, + ); + // The service has admission but has not been polled when the parent exits. + { + let mut vm = sidecar.vms.get_mut(&vm_id).unwrap(); + let mut parent = vm.active_processes.remove("parent").unwrap(); + for (id, child) in VmManager::::adopt_detached_child_processes( + "parent", + &mut parent, + ) { + vm.detached_child_processes.insert(id.clone()); + vm.active_processes.insert(id, child); + } + } + work.await.unwrap(); + assert!(reply.is_terminal()); + let mut vm = sidecar.vms.get_mut(&vm_id).unwrap(); + assert_eq!( + vm.kernel + .read_file_for_process(EXECUTION_DRIVER_NAME, pid, "/rpc-survived") + .unwrap(), + b"alive" + ); + }) + .await; + } + + #[tokio::test(flavor = "current_thread")] + async fn output_envelope_chunks_preserve_bytes_and_backpressure() { + tokio::task::LocalSet::new() + .run_until(async { + let (mut sidecar, vm_id) = sidecar_with_test_vm(1).await; + let (connection_id, session_id) = { + let mut vm = sidecar.vms.get_mut(&vm_id).unwrap(); + vm.limits.process.pending_event_bytes = 1024; + (vm.connection_id.clone(), vm.session_id.clone()) + }; + let data = (0..4096).map(|i| (i % 251) as u8).collect::>(); + let mut next = Some(ProcessEventEnvelope { + connection_id, + session_id, + vm_id: vm_id.clone(), + process_id: "root".into(), + child_path: Vec::new(), + event: ActiveExecutionEvent::RoutedStdout(data.clone()), + }); + let mut received = Vec::new(); + while let Some(event) = next.take() { + next = match sidecar.offer_pending_process_event(event).unwrap() { + ProcessEventOffer::Queued => None, + ProcessEventOffer::Paused(tail) => Some(*tail), + }; + if let Some(tail) = next.take() { + next = match sidecar.offer_pending_process_event(tail).unwrap() { + ProcessEventOffer::Paused(tail) => Some(*tail), + ProcessEventOffer::Queued => { + panic!("full queue must retain the suffix") + } + }; + } + let event = sidecar.pending_process_events.take_next(|_| true).unwrap(); + assert!(event.retained_bytes() <= 1024); + match event.event { + ActiveExecutionEvent::RoutedStdout(bytes) => received.extend(bytes), + _ => panic!("output kind changed"), + } + sidecar.observe_pending_process_event_depth(); + } + assert_eq!(received, data); + }) + .await; + } + + #[tokio::test(flavor = "current_thread")] + async fn adopted_detached_output_stays_public_and_ordered_under_backpressure() { + tokio::task::LocalSet::new() + .run_until(async { + let (mut sidecar, vm_id) = sidecar_with_test_vm(1).await; + let id = "root/detached".to_owned(); + { + let mut vm = sidecar.vms.get_mut(&vm_id).unwrap(); + let mut child = + host_function_process(&mut vm, "detached", None).with_detached(true); + for event in [ + ActiveExecutionEvent::RoutedStdout(b"out".to_vec()), + ActiveExecutionEvent::RoutedStderr(b"err".to_vec()), + ActiveExecutionEvent::Exited(0), + ] { + child.queue_pending_execution_event(event).unwrap(); + } + vm.active_processes.insert(id.clone(), child); + vm.detached_child_processes.insert(id.clone()); + } + let mut services = Vec::new(); + let mut relays = Vec::new(); + for phase in 0..3 { + sidecar + .pump_detached_child_process_events_nowait( + &vm_id, + &mut services, + &mut relays, + 1, + ) + .unwrap(); + assert!( + services.is_empty(), + "public output must never become an internal host service" + ); + assert!(relays.is_empty()); + // With the one-slot public queue occupied, the next event stays at its source. + sidecar + .pump_detached_child_process_events_nowait( + &vm_id, + &mut services, + &mut relays, + 1, + ) + .unwrap(); + assert!(services.is_empty()); + assert_eq!(sidecar.pending_process_events.len(), 1); + let event = sidecar.pending_process_events.take_next(|_| true).unwrap(); + assert_eq!(event.process_id, id); + match (phase, event.event) { + (0, ActiveExecutionEvent::RoutedStdout(bytes)) => assert_eq!(bytes, b"out"), + (1, ActiveExecutionEvent::RoutedStderr(bytes)) => assert_eq!(bytes, b"err"), + (2, ActiveExecutionEvent::Exited(0)) => {} + (_, other) => panic!("unexpected detached event: {other:?}"), + } + sidecar.observe_pending_process_event_depth(); + } + }) + .await; + } + #[tokio::test(flavor = "current_thread")] async fn detached_descendant_exit_waits_for_public_event_capacity_before_cleanup() { tokio::task::LocalSet::new() diff --git a/crates/vm/src/execution/host_dispatch/network_compat.rs b/crates/vm/src/execution/host_dispatch/network_compat.rs index 224b86a7ca..10b8d4f774 100644 --- a/crates/vm/src/execution/host_dispatch/network_compat.rs +++ b/crates/vm/src/execution/host_dispatch/network_compat.rs @@ -1289,6 +1289,7 @@ fn retire_managed_description_routes( continue; } }, + unlink_path: false, }, ), ManagedHostNetRoute::UdpSocket(socket_id) => service_managed_udp_operation( @@ -1677,6 +1678,7 @@ pub(super) fn decode_managed( }, "net.server_close" => NetworkOperation::ManagedCloseListener { listener_id: id(0, "listener id")?, + unlink_path: optional_bool(request, 1, "unlink pathname")?, }, "net.socket_upgrade_tls" => { let options = javascript_sync_rpc_arg_str(&request.args, 1, "TLS options")?; @@ -5788,6 +5790,22 @@ mod managed_tests { )); } + #[test] + fn managed_listener_close_preserves_explicit_path_unlink() { + for flag in [None, Some(false), Some(true)] { + let mut close = request("net.server_close"); + if let Some(flag) = flag { + close.args.push(json!(flag)); + } + assert!(matches!(decode_managed(&close, 1024).unwrap(), + Some(NetworkOperation::ManagedCloseListener { unlink_path, .. }) + if unlink_path == flag.unwrap_or(false))); + } + let mut close = request("net.server_close"); + close.args.push(json!("true")); + assert!(decode_managed(&close, 1024).is_err()); + } + #[test] fn every_frozen_network_family_rpc_has_a_typed_operation_route() { use crate::execution::host_dispatch::inventory::{capability_family, HostCapabilityFamily}; diff --git a/crates/vm/src/execution/network/managed.rs b/crates/vm/src/execution/network/managed.rs index ddc842e317..6762edcdaa 100644 --- a/crates/vm/src/execution/network/managed.rs +++ b/crates/vm/src/execution/network/managed.rs @@ -75,9 +75,10 @@ pub(in crate::execution) fn service_managed_network_operation( socket_id, options_json, } => managed_tls_upgrade(context, socket_id.as_str(), options_json.as_str()), - crate::executor::host::NetworkOperation::ManagedCloseListener { listener_id } => { - managed_close_listener(context, listener_id.as_str()) - } + crate::executor::host::NetworkOperation::ManagedCloseListener { + listener_id, + unlink_path, + } => managed_close_listener(context, listener_id.as_str(), unlink_path), crate::executor::host::NetworkOperation::ManagedAccept { listener_id } => { managed_accept(context, listener_id.as_str()) } @@ -361,6 +362,7 @@ fn managed_tls_upgrade( fn managed_close_listener( context: ManagedNetworkServiceContext<'_>, listener_id: &str, + unlink_path: bool, ) -> Result { if let Some(listener) = context.process.tcp_listeners.remove(listener_id) { release_tcp_listener_handle( @@ -401,6 +403,15 @@ fn managed_close_listener( &context.socket_paths.unix_bound_addresses, &listener.registry_binding_id, )?; + if unlink_path { + if let Some(path) = listener.guest_node_path.as_deref() { + match context.kernel.remove_file(path) { + Ok(()) => {} + Err(error) if error.code() == "ENOENT" => {} + Err(error) => return Err(kernel_error(error)), + } + } + } let completion = listener.close(); let deadline = reactor_io_limits(&context.process.limits).operation_deadline; let listener_id = listener_id.to_owned(); diff --git a/crates/vm/src/execution/owned_rpc.rs b/crates/vm/src/execution/owned_rpc.rs index 63e7828275..7f562df163 100644 --- a/crates/vm/src/execution/owned_rpc.rs +++ b/crates/vm/src/execution/owned_rpc.rs @@ -172,6 +172,10 @@ where let vm_id = vm_id.to_owned(); let root_id = root_id.to_owned(); let path = path.to_vec(); + let target_label = Self::child_process_path_label( + &root_id, + &path.iter().map(String::as_str).collect::>(), + ); let notify = Arc::clone(&self.process_event_notify); let sender = self.process_event_sender.clone(); Box::pin(async move { @@ -182,6 +186,8 @@ where vm.resources.buffered_bytes_release_generation() })?; let response = vm.try_command("service admitted compatibility RPC", |vm| { + let (root_id, path) = Self::resolve_detached_child_process_path(vm, &target_label) + .ok_or_else(|| VmError::host("ESTALE", "compatibility RPC caller exited"))?; // No `fs.*` method reads the socket path context; skip copying // the mount table and scanning processes on every file call. let socket_paths = if call.request.method.starts_with("fs.") { @@ -210,6 +216,10 @@ where let root = active_processes.get_mut(&root_id).ok_or_else(|| missing_process_error(&vm_id, &root_id))?; let refs = path.iter().map(String::as_str).collect::>(); let process = Self::active_process_by_path_mut(root, &refs).ok_or_else(|| VmError::host("ESTALE", "compatibility RPC caller exited"))?; + let identity = call.reply.identity(); + if identity.generation != vm.generation || identity.pid != process.kernel_pid { + return Err(VmError::host("ESTALE", "compatibility RPC identity changed before dispatch")); + } let mut future = Box::pin(service_javascript_sync_rpc(JavascriptSyncRpcServiceRequest { bridge: &bridge, vm_id: &vm_id, dns: &dns, socket_paths: socket_paths.as_ref(), kernel, kernel_readiness, process, sync_request: &call.request, @@ -225,6 +235,9 @@ where }).and_then(|response| response); let response = if call.request.method == "net.write" { match vm.try_command("route socket write backpressure", |vm| { + let (root_id, path) = + Self::resolve_detached_child_process_path(vm, &target_label) + .ok_or_else(|| VmError::host("ESTALE", "socket write caller exited"))?; route_javascript_net_write_result( vm, sender.clone(), @@ -297,6 +310,15 @@ where // process-owned socket/capability before replying to the guest, // using the same settlement path as admitted managed calls. vm.try_command("settle compatibility RPC completion", |vm| { + let (root_id, path) = + Self::resolve_detached_child_process_path(vm, &target_label).ok_or_else( + || { + VmError::host( + "ESTALE", + "compatibility RPC caller exited before completion", + ) + }, + )?; let generation = vm.generation; let readiness = Arc::clone(&vm.kernel_socket_readiness); let unix_addresses = Arc::clone(&vm.unix_address_registry); diff --git a/crates/vm/src/execution/process.rs b/crates/vm/src/execution/process.rs index 1e1e813fb1..f37d255256 100644 --- a/crates/vm/src/execution/process.rs +++ b/crates/vm/src/execution/process.rs @@ -3461,6 +3461,35 @@ mod execution_backend_lifecycle_tests { fn assert_execution_backend() {} + #[test] + fn stdio_partial_write_fits_retained_request_budget_without_losing_order() { + let data = (0..65536).map(|i| (i % 251) as u8).collect::>(); + let mut request = HostRpcRequest { + id: 1, + method: "__kernel_stdio_write".to_owned(), + args: vec![json!(1), Value::Null], + raw_bytes_args: std::collections::HashMap::from([(1, data.clone())]), + }; + let limit = 16 * 1024; + fit_stdio_write_request(&mut request, limit); + let kept = &request.raw_bytes_args[&1]; + assert!(!kept.is_empty()); + assert!(kept.len() < data.len()); + assert_eq!(kept.as_slice(), &data[..kept.len()]); + assert!(std::mem::size_of::() + request.heap_bytes() <= limit); + assert!(std::mem::size_of::() + kept.len() <= limit); + let fitted = request.clone(); + fit_stdio_write_request(&mut request, limit); + assert_eq!(request, fitted); + // No partial success when even metadata cannot be admitted. + fit_stdio_write_request(&mut request, 1); + assert_eq!(request, fitted); + request.method = "other.atomic.write".to_owned(); + let atomic = request.clone(); + fit_stdio_write_request(&mut request, 1024); + assert_eq!(request, atomic); + } + #[test] fn host_function_and_active_execution_use_the_common_lifecycle_contract() { assert_execution_backend::(); @@ -3951,12 +3980,39 @@ fn map_wasm_execution_event_with_host( Ok(Some(event)) } +// Stdio is a partial-write operation. Fit its owned binary payload before common +// event admission; the kernel still checks the descriptor and returns the actual +// count. Other RPCs remain atomic and retain their ordinary typed limit failures. +fn fit_stdio_write_request(request: &mut HostRpcRequest, limit: usize) { + if request.method != "__kernel_stdio_write" { + return; + } + let Some(bytes) = request.raw_bytes_args.get(&1) else { + return; + }; + let overhead = std::mem::size_of::() + .saturating_add(request.heap_bytes()) + .saturating_sub(bytes.capacity()); + // The same bytes next enter the output event queue, whose enum overhead + // can be larger than the host request. Both queues obey the same limit. + let overhead = overhead.max(std::mem::size_of::()); + let available = limit.saturating_sub(overhead); + if available > 0 && bytes.capacity() > available { + let kept = bytes.len().min(available); + let partial = bytes[..kept].to_vec(); + request.raw_bytes_args.insert(1, partial); + } +} + fn route_compatibility_host_call( - call: ExecutionHostCall, + mut call: ExecutionHostCall, full_filesystem: bool, max_reply_bytes: usize, host: Option<&ProcessHostCapabilitySet>, ) -> Result, VmError> { + if let Some(limit) = host.and_then(ProcessHostCapabilitySet::request_byte_limit) { + fit_stdio_write_request(&mut call.request, limit); + } let reply = call.reply.clone(); let admission = match host .map(|host| { diff --git a/crates/vm/src/execution/process_events.rs b/crates/vm/src/execution/process_events.rs index 093caabda9..839c643082 100644 --- a/crates/vm/src/execution/process_events.rs +++ b/crates/vm/src/execution/process_events.rs @@ -944,6 +944,18 @@ where reservation, .. } = target; + // A queued claim can outlive its parent's exit. Resolve the unchanged + // process label in the current tree before dispatch; direct replies still + // enforce the original VM generation and kernel PID. + let label = Self::child_process_path_label( + &process_id, + &child_path.iter().map(String::as_str).collect::>(), + ); + let (process_id, child_path) = self + .vms + .get(&vm_id) + .and_then(|state| Self::resolve_detached_child_process_path(&state, &label)) + .unwrap_or((process_id, child_path)); let future: std::pin::Pin>>> = match event { ActiveExecutionEvent::Common(ExecutionEvent::HostCall { operation, reply }) => { @@ -1087,11 +1099,8 @@ where /// broker unchanged. pub(super) fn route_received_internal_process_event( &mut self, - envelope: ProcessEventEnvelope, + mut envelope: ProcessEventEnvelope, ) -> Result, VmError> { - if !Self::internal_execution_event(&envelope.event) { - return Ok(Some(envelope)); - } self.validate_process_event_envelope_locator(&envelope)?; let target_label = if envelope.child_path.is_empty() { envelope.process_id.clone() @@ -1099,10 +1108,11 @@ where format!("{}/{}", envelope.process_id, envelope.child_path.join("/")) }; let Some(mut vm) = self.vms.get_mut(&envelope.vm_id) else { - tracing::debug!( - vm_id = envelope.vm_id, - process_id = target_label, - "ERR_AGENTOS_STALE_PROCESS_EVENT: runtime completion targeted a disposed VM" + log_stale_process_event( + &self.bridge, + &envelope.vm_id, + &target_label, + "execution event dispatch", ); reject_internal_event( envelope.event, @@ -1112,7 +1122,7 @@ where }; if vm.connection_id != envelope.connection_id || vm.session_id != envelope.session_id { return Err(VmError::InvalidState(format!( - "ERR_AGENTOS_PROCESS_EVENT_SCOPE_MISMATCH: runtime completion for VM {} carried connection/session {}/{}, expected {}/{}", + "ERR_AGENTOS_PROCESS_EVENT_SCOPE_MISMATCH: runtime completion ownership mismatch for VM {} carried connection/session {}/{}, expected {}/{}", envelope.vm_id, envelope.connection_id, envelope.session_id, @@ -1120,6 +1130,18 @@ where vm.session_id ))); } + // Output already in flight at disposal is stale too. Validate its VM + // owner before admission, but allow trailing public events after the + // process itself has been reaped. + if !Self::internal_execution_event(&envelope.event) { + return Ok(Some(envelope)); + } + // Reparenting preserves the full label, PID and generation. Only its + // root/path split changes when a detached child is adopted. + if let Some((root, path)) = Self::resolve_detached_child_process_path(&vm, &target_label) { + envelope.process_id = root; + envelope.child_path = path; + } let Some(root) = vm.active_processes.get_mut(&envelope.process_id) else { tracing::debug!( vm_id = envelope.vm_id, @@ -1383,29 +1405,25 @@ where process_id, event, }; - if let Err(error) = self.check_pending_process_event_capacity(&envelope) { - let retry = self.global_process_event_pressure_can_retry(&envelope); - let Some(mut vm) = self.vms.get_mut(&vm_id) else { - return Err(error); - }; - let Some(process) = vm.active_processes.get_mut(&envelope.process_id) - else { - return Err(error); - }; - let polled = PolledExecutionEvent { + if let ProcessEventOffer::Paused(envelope) = + self.offer_pending_process_event(envelope)? + { + let mut vm = self + .vms + .get_mut(&vm_id) + .ok_or_else(|| missing_vm_error(&vm_id))?; + let process = vm + .active_processes + .get_mut(&envelope.process_id) + .ok_or_else(|| missing_process_error(&vm_id, &envelope.process_id))?; + process.restore_paused_execution_event(PolledExecutionEvent { event: envelope.event, reservation, - }; - if retry { - process.restore_paused_execution_event(polled)?; - self.public_process_event_capacity_waiter - .store(true, std::sync::atomic::Ordering::Release); - continue; - } - process.requeue_pending_execution_event(polled)?; - return Err(error); + })?; + self.public_process_event_capacity_waiter + .store(true, std::sync::atomic::Ordering::Release); + continue; } - self.queue_pending_process_event(envelope)?; root_source_remains |= source_remains; drop(reservation); emitted_any = true; @@ -1921,28 +1939,27 @@ where process_id: process_id.clone(), event, }; - if let Err(error) = self.check_pending_process_event_capacity(&envelope) { - let retry = self.global_process_event_pressure_can_retry(&envelope); - let Some(mut vm) = self.vms.get_mut(&vm_id) else { - return Err(error); - }; - let Some(process) = vm.active_processes.get_mut(&process_id) else { - return Err(error); - }; - let polled = PolledExecutionEvent { + if let ProcessEventOffer::Paused(envelope) = + self.offer_pending_process_event(envelope)? + { + let mut vm = self + .vms + .get_mut(&vm_id) + .ok_or_else(|| missing_vm_error(&vm_id))?; + let process = vm + .active_processes + .get_mut(&envelope.process_id) + .ok_or_else(|| { + missing_process_error(&vm_id, &envelope.process_id) + })?; + process.restore_paused_execution_event(PolledExecutionEvent { event: envelope.event, reservation, - }; - if retry { - process.restore_paused_execution_event(polled)?; - self.public_process_event_capacity_waiter - .store(true, std::sync::atomic::Ordering::Release); - continue; - } - process.requeue_pending_execution_event(polled)?; - return Err(error); + })?; + self.public_process_event_capacity_waiter + .store(true, std::sync::atomic::Ordering::Release); + continue; } - self.queue_pending_process_event(envelope)?; drop(reservation); } emitted_any = true; @@ -2405,6 +2422,9 @@ where vm: &VmState, detached_process_id: &str, ) -> Option<(String, Vec)> { + if vm.active_processes.contains_key(detached_process_id) { + return Some((detached_process_id.to_owned(), Vec::new())); + } let root_process_id = vm .active_processes .keys() diff --git a/crates/vm/src/service.rs b/crates/vm/src/service.rs index bc227b1183..4a3ba9497c 100644 --- a/crates/vm/src/service.rs +++ b/crates/vm/src/service.rs @@ -2371,8 +2371,81 @@ where pub(crate) fn offer_pending_process_event( &mut self, - envelope: ProcessEventEnvelope, + mut envelope: ProcessEventEnvelope, ) -> Result { + // Output may fit the execution queue but need smaller chunks once its + // routing identity is attached. Admit one prefix and retain the suffix + // at the producer, using the same backpressure path as a full queue. + self.validate_process_event_envelope_locator(&envelope)?; + let limit = self + .vms + .get(&envelope.vm_id) + .map(|vm| vm.limits.process.pending_event_bytes) + .unwrap_or(0); + let retained = envelope.retained_bytes(); + let bytes = match &mut envelope.event { + ActiveExecutionEvent::Stdout(bytes) + | ActiveExecutionEvent::Stderr(bytes) + | ActiveExecutionEvent::RoutedStdout(bytes) + | ActiveExecutionEvent::RoutedStderr(bytes) => Some(bytes), + _ => None, + }; + if let Some(bytes) = bytes { + let available = limit.saturating_sub(retained.saturating_sub(bytes.len())); + if available > 0 && bytes.len() > available { + let suffix = bytes[available..].to_vec(); + *bytes = bytes[..available].to_vec(); + let event = match &envelope.event { + ActiveExecutionEvent::Stdout(_) => ActiveExecutionEvent::Stdout(suffix), + ActiveExecutionEvent::Stderr(_) => ActiveExecutionEvent::Stderr(suffix), + ActiveExecutionEvent::RoutedStdout(_) => { + ActiveExecutionEvent::RoutedStdout(suffix) + } + ActiveExecutionEvent::RoutedStderr(_) => { + ActiveExecutionEvent::RoutedStderr(suffix) + } + _ => unreachable!("output prefix preserves its event kind"), + }; + let remainder = ProcessEventEnvelope { + connection_id: envelope.connection_id.clone(), + session_id: envelope.session_id.clone(), + vm_id: envelope.vm_id.clone(), + process_id: envelope.process_id.clone(), + child_path: envelope.child_path.clone(), + event, + }; + match self.try_queue_pending_process_event(envelope) { + Ok(()) => return Ok(ProcessEventOffer::Paused(Box::new(remainder))), + Err((error, mut prefix)) => { + let retry = self.global_process_event_pressure_can_retry(&prefix); + match (&mut prefix.event, remainder.event) { + ( + ActiveExecutionEvent::Stdout(bytes), + ActiveExecutionEvent::Stdout(tail), + ) + | ( + ActiveExecutionEvent::Stderr(bytes), + ActiveExecutionEvent::Stderr(tail), + ) + | ( + ActiveExecutionEvent::RoutedStdout(bytes), + ActiveExecutionEvent::RoutedStdout(tail), + ) + | ( + ActiveExecutionEvent::RoutedStderr(bytes), + ActiveExecutionEvent::RoutedStderr(tail), + ) => bytes.extend(tail), + _ => unreachable!("output suffix preserves its event kind"), + } + if retry { + self.pending_process_events.pause_producer(); + return Ok(ProcessEventOffer::Paused(Box::new(prefix))); + } + return Err(error); + } + } + } + } match self.try_queue_pending_process_event(envelope) { Ok(()) => Ok(ProcessEventOffer::Queued), Err((_, envelope)) if self.global_process_event_pressure_can_retry(&envelope) => { diff --git a/crates/vm/src/vm.rs b/crates/vm/src/vm.rs index d3770fb80f..d68ae1175c 100644 --- a/crates/vm/src/vm.rs +++ b/crates/vm/src/vm.rs @@ -7957,7 +7957,9 @@ mod tests { let close_started = Arc::new(AtomicBool::new(false)); let generation = { let mut vm = sidecar.vms.get_mut(&vm_id).expect("pending-close VM"); - vm.limits.reactor.shutdown_deadline_ms = 10; + // Reach the deliberately stalled database close after normal + // kernel teardown, including unoptimized test builds. + vm.limits.reactor.shutdown_deadline_ms = 1_000; vm.database = Some(Arc::new(PendingCloseDatabase { close_started: Arc::clone(&close_started), })); diff --git a/crates/vm/tests/fixtures/limits-inventory.json b/crates/vm/tests/fixtures/limits-inventory.json index 4172c6abf2..4adf4e426f 100644 --- a/crates/vm/tests/fixtures/limits-inventory.json +++ b/crates/vm/tests/fixtures/limits-inventory.json @@ -2345,12 +2345,6 @@ "class": "invariant", "rationale": "Node-compatible 512 KiB FileHandle.writeFile request granularity limits bridge calls for regular files while short writes still retry and yield for timer-driven cancellation." }, - { - "name": "MAX_SYMLINK_DEPTH", - "path": "packages/build-tools/bridge-src/builtins/fs.ts", - "class": "invariant", - "rationale": "Linux ELOOP mirror of the kernel invariant." - }, { "name": "NET_BRIDGE_MAX_RAW_WRITE_BYTES", "path": "packages/build-tools/bridge-src/builtins/net.ts", @@ -3503,5 +3497,54 @@ "path": "crates/executor-v8-runtime/src/snapshot.rs", "class": "policy-deferred", "rationale": "Caps the V8 heap admitted to a one-shot snapshot helper." + }, + { + "name": "DEFAULT_HOST_CAPTURE_LIMIT_BYTES", + "path": "packages/core/src/host-output-capture.ts", + "class": "policy", + "rationale": "Default byte ceiling for host-owned output capture, independently configurable from VM output policy.", + "wired": "maxCapturedOutputBytes" + }, + { + "name": "MAX_BRIDGE_CBOR_NESTING", + "path": "crates/executor-v8-runtime/src/adapter_runtime.rs", + "class": "invariant", + "rationale": "Fixed recursive bridge conversion depth protects the trusted native stack from nested guest values." + }, + { + "name": "MAX_HTTP_REQUEST_TIMERS", + "path": "packages/build-tools/bridge-src/builtins/timers.ts", + "class": "policy-deferred", + "rationale": "Existing legacy HTTP request continuation cap; bounded and warns near threshold, but per-request configuration is not yet exposed." + }, + { + "name": "MAX_BYTE_LENGTH", + "path": "crates/v8/src/array_buffer.rs", + "class": "invariant", + "rationale": "Upstream V8 ABI constant defining ArrayBuffer byte-length representability; not an agentOS resource-policy default." + }, + { + "name": "MAX_BYTE_LENGTH", + "path": "crates/v8/src/typed_array.rs", + "class": "invariant", + "rationale": "Upstream V8 ABI constant defining TypedArray byte-length representability; not an agentOS resource-policy default." + }, + { + "name": "MAX_LENGTH", + "path": "crates/v8/src/string.rs", + "class": "invariant", + "rationale": "Upstream V8 ABI constant defining String length representability; not an agentOS resource-policy default." + }, + { + "name": "MAX_LENGTH", + "path": "crates/v8/src/typed_array.rs", + "class": "invariant", + "rationale": "Upstream V8 ABI constant defining TypedArray element-count representability; not an agentOS resource-policy default." + }, + { + "name": "TYPED_ARRAY_MAX_SIZE_IN_HEAP", + "path": "crates/v8/src/lib.rs", + "class": "invariant", + "rationale": "Upstream V8 ABI constant defining TypedArray inline heap layout; not an agentOS resource-policy default." } ] diff --git a/crates/vm/tests/python.rs b/crates/vm/tests/python.rs index 27e7e7887d..4fdd2d6590 100644 --- a/crates/vm/tests/python.rs +++ b/crates/vm/tests/python.rs @@ -945,7 +945,8 @@ fn wait_for_stdout_chunk( vm_id: &str, process_id: &str, needle: &str, -) { +) -> String { + let mut stdout = Vec::new(); let ownership = wire_vm(connection_id, session_id, vm_id); let deadline = Instant::now() + Duration::from_secs(10); @@ -961,11 +962,12 @@ fn wait_for_stdout_chunk( match event.payload { EventPayload::ProcessOutputEvent(output) - if output.process_id == process_id - && output.channel == StreamChannel::Stdout - && chunk_contains(&output.chunk, needle) => + if output.process_id == process_id && output.channel == StreamChannel::Stdout => { - return; + append_stream_chunk(&mut stdout, &output.chunk, "python stdout"); + if chunk_contains(&stdout, needle) { + return String::from_utf8(stdout).expect("Python fixture stdout UTF-8"); + } } EventPayload::ProcessOutputEvent(_) => {} EventPayload::ProcessExitedEvent(exited) if exited.process_id == process_id => { @@ -2600,24 +2602,24 @@ print(f"tail:{sys.stdin.read()!r}") "alpha\n", ); - wait_for_stdout_chunk( + let first_output = wait_for_stdout_chunk( &mut sidecar, &connection_id, &session_id, &vm_id, "proc-python-stdin-interactive", - "first:alpha", + "prompt-2: ", ); - + assert!(first_output.contains("first:alpha"), "{first_output}"); assert!( sidecar .poll_event_wire_blocking( &wire_vm(&connection_id, &session_id, &vm_id), - Duration::from_millis(200) + Duration::from_millis(200), ) .expect("poll stalled python interactive stdin before second write") .is_none(), - "python interactive stdin execution should stay blocked for the second input" + "python must remain blocked after printing the second input prompt" ); write_process_stdin( @@ -3260,6 +3262,18 @@ import json import subprocess result = subprocess.run(["node", "./child.mjs"], capture_output=True, text=True, check=True) +raw = subprocess.run(["node", "-e", "process.stdout.write(Buffer.from([0, 255, 128])); process.stderr.write(Buffer.from([254]))"], capture_output=True, check=True) +assert isinstance(raw.stdout, bytes) and raw.stdout == bytes([0, 255, 128]) +assert isinstance(raw.stderr, bytes) and raw.stderr == bytes([254]) +latin = subprocess.run(["node", "-e", "process.stdout.write(Buffer.from([255]))"], capture_output=True, encoding="latin-1", check=True) +assert latin.stdout == chr(255) +replaced = subprocess.run(["node", "-e", "process.stdout.write(Buffer.from([255]))"], capture_output=True, errors="replace", check=True) +assert replaced.stdout == chr(0xfffd) +try: + subprocess.run(["node", "-e", "process.stderr.write(Buffer.from([255])); process.exit(3)"], capture_output=True, check=True) + raise AssertionError("failed child must raise CalledProcessError") +except subprocess.CalledProcessError as error: + assert error.returncode == 3 and error.stderr == bytes([255]) print(json.dumps({ "code": result.returncode, "stdout": result.stdout.strip(), @@ -4116,7 +4130,8 @@ fn python_reads_and_writes_arbitrary_vm_paths() { fn python_pip_installs_persist_across_invocations() { assert_node_available(); - let (port, server) = spawn_static_file_server(pyodide_asset_dir()); + let mirror = temp_dir("python-persistence-wheel"); + let (port, server) = spawn_static_file_server(mirror.clone()); let mut sidecar = new_sidecar("python-vfs-pip"); let cwd = temp_dir("python-vfs-pip-cwd"); let connection_id = authenticate_wire(&mut sidecar, "conn-python"); @@ -4135,8 +4150,49 @@ fn python_pip_installs_persist_across_invocations() { wire_permissions_allow_all(), ); - // Process 1: `pip install` a wheel — the shim copies it into the VFS-backed - // site-packages so it persists past this interpreter. + // A bundled wheel (such as click) is deliberately not copied into the VFS. + // Generate a genuinely unbundled fixture with the guest standard library. + execute_inline_python( + &mut sidecar, + 40, + &connection_id, + &session_id, + &vm_id, + "proc-build-persistence-wheel", + r#" +import io, json, zipfile +files = { + "agentos_persistence_probe/__init__.py": "VALUE = 'persisted'\n", + "agentos_persistence_probe-0.0.1.dist-info/METADATA": "Metadata-Version: 2.1\nName: agentos-persistence-probe\nVersion: 0.0.1\n", + "agentos_persistence_probe-0.0.1.dist-info/WHEEL": "Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: py3-none-any\n", +} +record = "agentos_persistence_probe-0.0.1.dist-info/RECORD" +files[record] = "".join(name + ",,\n" for name in [*files, record]) +archive = io.BytesIO() +with zipfile.ZipFile(archive, "w") as wheel: + for name, content in files.items(): + wheel.writestr(name, content) +print(json.dumps(list(archive.getvalue()))) +"#, + ); + let (wheel_output, wheel_stderr, wheel_exit) = collect_process_output( + &mut sidecar, + &connection_id, + &session_id, + &vm_id, + "proc-build-persistence-wheel", + ); + assert_eq!(wheel_exit, 0, "wheel fixture: {wheel_stderr}"); + let wheel_bytes: Vec = + serde_json::from_str(wheel_output.trim()).expect("fixture wheel bytes"); + fs::write( + mirror.join("agentos_persistence_probe-0.0.1-py3-none-any.whl"), + wheel_bytes, + ) + .expect("serve fixture wheel"); + + // Process 1 installs the unbundled wheel into the VFS-backed site-packages. + // A fresh interpreter below must import that persisted copy. execute_python_cli_with_env( &mut sidecar, 4, @@ -4147,7 +4203,7 @@ fn python_pip_installs_persist_across_invocations() { "pip", &[ "install", - &format!("http://127.0.0.1:{port}/click-8.3.1-py3-none-any.whl"), + &format!("http://127.0.0.1:{port}/agentos_persistence_probe-0.0.1-py3-none-any.whl"), ], HashMap::from([( String::from("AGENTOS_PYODIDE_PACKAGE_BASE_URL"), @@ -4173,7 +4229,7 @@ fn python_pip_installs_persist_across_invocations() { &connection_id, &session_id, &vm_id, - "/home/agentos/.agentos/site-packages/click/__init__.py", + "/home/agentos/.agentos/site-packages/agentos_persistence_probe/__init__.py", ); assert!( home_install_exists, @@ -4190,7 +4246,10 @@ fn python_pip_installs_persist_across_invocations() { &vm_id, "proc-pip-import", "python", - &["-c", "import click; print(click.__version__)"], + &[ + "-c", + "import agentos_persistence_probe; print(agentos_persistence_probe.VALUE)", + ], ); let (stdout2, stderr2, exit2) = collect_process_output_with_timeout( &mut sidecar, @@ -4204,7 +4263,7 @@ fn python_pip_installs_persist_across_invocations() { assert_eq!(exit2, 0, "stdout: {stdout2}\nstderr: {stderr2}"); assert_eq!( stdout2.trim(), - "8.3.1", + "persisted", "stdout: {stdout2}\nstderr: {stderr2}" ); } diff --git a/crates/vm/tests/service.rs b/crates/vm/tests/service.rs index 76d5c1cf50..32c9cafa1b 100644 --- a/crates/vm/tests/service.rs +++ b/crates/vm/tests/service.rs @@ -647,6 +647,33 @@ ykAheWCsAteSEWVc0w==\n\ } } + fn owned_test_process_events( + sidecar: &mut VmManager, + ) -> impl Fn(usize) -> ProcessEventEnvelope { + let (connection_id, session_id) = + authenticate_and_open_session(sidecar).expect("authenticate event owner"); + let vm_id = create_vm_with_metadata( + sidecar, + &connection_id, + &session_id, + PermissionsPolicy::allow_all(), + BTreeMap::from([( + String::from("limits.process.pendingEventCount"), + MAX_PROCESS_EVENT_QUEUE.to_string(), + )]), + ) + .expect("create event owner"); + move |index| ProcessEventEnvelope { + connection_id: connection_id.clone(), + session_id: session_id.clone(), + vm_id: vm_id.clone(), + // Exercise total capacity, including slots reserved for control + // events. Output-only admission has its own smaller pool. + event: ActiveExecutionEvent::Exited(0), + ..test_process_event(index) + } + } + fn process_event_target_label(envelope: &ProcessEventEnvelope) -> String { if envelope.child_path.is_empty() { envelope.process_id.clone() @@ -883,6 +910,7 @@ ykAheWCsAteSEWVc0w==\n\ ..agentos_driver_tokio::DriverProtocolConfig::default() }; let mut sidecar = create_test_sidecar_with_protocol_limits(protocol); + let test_process_event = owned_test_process_events(&mut sidecar); assert_eq!(sidecar.pending_process_events_gauge.capacity(), 2); assert_eq!(sidecar.outbound_sidecar_requests_gauge.capacity(), 2); @@ -960,6 +988,7 @@ ykAheWCsAteSEWVc0w==\n\ fn pending_process_events_are_bounded() { let mut sidecar = create_test_sidecar(); + let test_process_event = owned_test_process_events(&mut sidecar); for index in 0..MAX_PROCESS_EVENT_QUEUE { sidecar @@ -978,6 +1007,7 @@ ykAheWCsAteSEWVc0w==\n\ fn process_event_receiver_overflow_preserves_queued_event() { let mut sidecar = create_test_sidecar(); + let test_process_event = owned_test_process_events(&mut sidecar); for index in 0..MAX_PROCESS_EVENT_QUEUE { sidecar @@ -992,7 +1022,10 @@ ykAheWCsAteSEWVc0w==\n\ .expect("queue process event behind full pending queue"); let error = sidecar - .take_matching_process_event_envelope("vm-queue", &expected_process_id) + .take_matching_process_event_envelope( + &test_process_event(0).vm_id, + &expected_process_id, + ) .expect_err("receiver drain should reject overflow before consuming event"); assert!( error.to_string().contains("process event queue exceeded"), @@ -1775,6 +1808,7 @@ ykAheWCsAteSEWVc0w==\n\ ) .expect("create vm"); insert_host_function_process(&mut sidecar, &vm_id, "proc-exit"); + let test_process_event = owned_test_process_events(&mut sidecar); for index in 0..(MAX_PROCESS_EVENT_QUEUE - 1) { sidecar @@ -4335,7 +4369,18 @@ console.log(JSON.stringify({ status: "ok", summary })); let Some(event) = next_event else { panic!("process {process_id} disappeared before writing {needle:?}"); }; - if let ActiveExecutionEvent::Stdout(chunk) = &event { + let chunk = match &event { + ActiveExecutionEvent::Stdout(chunk) + | ActiveExecutionEvent::RoutedStdout(chunk) => Some(chunk.as_slice()), + ActiveExecutionEvent::Common( + agentos_vm::executor::backend::ExecutionEvent::Output { + stream: agentos_vm::executor::backend::OutputStream::Stdout, + bytes, + }, + ) => Some(bytes.as_slice()), + _ => None, + }; + if let Some(chunk) = chunk { append_process_stream_chunk(&mut stdout, chunk, process_id, "stdout"); if process_stream_to_string(&stdout).contains(needle) { block_on_sidecar!( @@ -4497,6 +4542,13 @@ console.log(JSON.stringify({ status: "ok", summary })); ) (then unreachable) ) + ;; Keep the descriptors alive until the host finishes inspecting them. + (i32.store (i32.const 48) (i32.const 72)) + (i32.store (i32.const 52) (i32.const 1)) + (if (i32.ne + (call $fd_read (i32.const 0) (i32.const 48) (i32.const 1) (i32.const 60)) + (i32.const 0)) (then unreachable)) + (if (i32.ne (i32.load (i32.const 60)) (i32.const 0)) (then unreachable)) ) ) "#, @@ -4727,6 +4779,8 @@ console.log(JSON.stringify({ status: "ok", summary })); }; let mut vm = sidecar.vms.get_mut(vm_id).expect("javascript vm"); + let stdin_budget = Arc::clone(&vm.pending_stdin_bytes_budget); + let event_budget = Arc::clone(&vm.pending_event_bytes_budget); vm.active_processes.insert( process_id.to_owned(), active_process_for_tests( @@ -4735,6 +4789,7 @@ console.log(JSON.stringify({ status: "ok", summary })); GuestRuntimeKind::JavaScript, ActiveExecution::HostFunction(HostFunctionExecution::default()), ) + .with_vm_pending_byte_budgets(stdin_budget, event_budget) .with_env(guest_env) .with_host_cwd(cwd.to_path_buf()), ); @@ -6160,12 +6215,16 @@ console.log(JSON.stringify({ status: "ok", summary })); drop(payload); assert!(tls_resources.is_zero()); + let datagram_metadata = agentos_vm_kernel::socket_table::DATAGRAM_METADATA_BYTES; let udp_resources = ResourceLedger::root( "udp-small-limit", [ ( ResourceClass::BufferedBytes, - ResourceLimit::new(8, "limits.resources.maxSocketBufferedBytes"), + ResourceLimit::new( + 8 + 2 * datagram_metadata, + "limits.resources.maxSocketBufferedBytes", + ), ), ( ResourceClass::Datagrams, @@ -6173,7 +6232,10 @@ console.log(JSON.stringify({ status: "ok", summary })); ), ( ResourceClass::UdpBytes, - ResourceLimit::new(4, "limits.udp.maxBufferedBytes"), + ResourceLimit::new( + 4 + 2 * datagram_metadata, + "limits.udp.maxBufferedBytes", + ), ), ( ResourceClass::UdpDatagrams, @@ -6186,9 +6248,15 @@ console.log(JSON.stringify({ status: "ok", summary })); let error = crate::execution::reserve_udp_receive_buffer(&udp_resources, 1) .expect_err("UDP datagram sublimit must apply backpressure"); assert!(error.to_string().contains("resource=udpDatagrams")); - assert_eq!(udp_resources.usage(ResourceClass::BufferedBytes).used, 3); + assert_eq!( + udp_resources.usage(ResourceClass::BufferedBytes).used, + 3 + datagram_metadata + ); assert_eq!(udp_resources.usage(ResourceClass::Datagrams).used, 1); - assert_eq!(udp_resources.usage(ResourceClass::UdpBytes).used, 3); + assert_eq!( + udp_resources.usage(ResourceClass::UdpBytes).used, + 3 + datagram_metadata + ); assert_eq!(udp_resources.usage(ResourceClass::UdpDatagrams).used, 1); let runtime_context = agentos_driver_tokio::TokioDriver::process( &agentos_driver_tokio::DriverConfig::default(), @@ -9960,7 +10028,7 @@ console.log(JSON.stringify({ status: "ok", summary })); let call_args: Value = serde_json::from_str(&call.args).expect("js bridge args json"); let path = call_args.get("path").and_then(Value::as_str); - if path == Some("/") { + if matches!(path, Some("/" | "/output.txt" | "/a.txt" | "/b.txt")) { return match call.operation.as_str() { "exists" => js_bridge_result(request, Some(Value::Bool(true)), None), "stat" | "lstat" => js_bridge_result( @@ -9971,7 +10039,7 @@ console.log(JSON.stringify({ status: "ok", summary })); blocks: 0, dev: 1, rdev: 0, - is_directory: true, + is_directory: path == Some("/"), is_symbolic_link: false, atime_ms: 0, atime_nsec: 0, @@ -9991,7 +10059,9 @@ console.log(JSON.stringify({ status: "ok", summary })); "readDirWithTypes" => { js_bridge_result(request, Some(Value::Array(Vec::new())), None) } - "realpath" => js_bridge_result(request, Some(json!("/")), None), + "realpath" => js_bridge_result(request, Some(json!(path)), None), + "writeFile" => js_bridge_result(request, None, Some("permission denied")), + "rename" => js_bridge_result(request, None, Some("already exists")), _ => js_bridge_result(request, None, None), }; } @@ -10061,7 +10131,7 @@ console.log(JSON.stringify({ status: "ok", summary })); .filesystem_mut() .write_file("/workspace/output.txt", b"blocked".to_vec()) .expect_err("write should fail"); - assert_eq!(write_error.code(), "EACCES"); + assert_eq!(write_error.code(), "EACCES", "{write_error}"); let rename_error = vm .kernel @@ -11096,6 +11166,21 @@ console.log(JSON.stringify({ status: "ok", summary })); }), )) .expect("start backend policy fixture"); + if expected_backend == agentos_vm::executor::StandaloneWasmBackend::Wasmtime + && !cfg!(feature = "wasm-wasmtime") + { + let ResponsePayload::Rejected(rejected) = started.response.payload else { + panic!("uncompiled Wasmtime must be rejected"); + }; + assert_eq!(rejected.code, "ERR_AGENTOS_EXECUTOR_UNAVAILABLE"); + assert!(!sidecar + .vms + .get(&vm_id) + .expect("VM") + .active_processes + .contains_key(process_id)); + continue; + } assert!(matches!( started.response.payload, ResponsePayload::ProcessStarted(_) @@ -11273,7 +11358,7 @@ console.log(JSON.stringify({ status: "ok", summary })); .filesystem_mut() .write_file("/blocked.txt", b"nope".to_vec()) .expect_err("write should be denied after failed rollback"); - assert_eq!(write_error.code(), "EACCES"); + assert_eq!(write_error.code(), "EACCES", "{write_error}"); let permission_check_count_after_write = sidecar .with_bridge_mut(|bridge| bridge.permission_checks.len()) .expect("read bridge permission checks"); @@ -12848,6 +12933,15 @@ console.log(JSON.stringify({ status: "ok", summary })); ); } + sidecar + .dispatch_blocking(request( + 7, + OwnershipScope::vm(&connection_id, &session_id, &vm_id), + RequestPayload::CloseStdin(CloseStdinRequest { + process_id: String::from("proc-managed-wasm-kernel-pipe"), + }), + )) + .expect("release the live descriptor inspection barrier"); let (stdout, stderr, exit_code) = drain_process_output(&mut sidecar, &vm_id, "proc-managed-wasm-kernel-pipe"); assert_eq!(exit_code, Some(0), "stdout: {stdout} stderr: {stderr}"); @@ -14320,7 +14414,7 @@ process.stdout.write(`${JSON.stringify(snapshot)}\n`); "posix-spawnp-parent", ); - let (nested_handle, nested_env) = { + let (nested_handle, nested_env, nested_stdin_budget, nested_event_budget) = { let mut vm = sidecar.vms.get_mut(&vm_id).expect("created vm"); let root_pid = vm .active_processes @@ -14340,7 +14434,12 @@ process.stdout.write(`${JSON.stringify(snapshot)}\n`); }, ) .expect("spawn nested kernel parent"); - (handle, vm.guest_env.clone()) + ( + handle, + vm.guest_env.clone(), + Arc::clone(&vm.pending_stdin_bytes_budget), + Arc::clone(&vm.pending_event_bytes_budget), + ) }; let nested_pid = nested_handle.pid(); sidecar @@ -14360,6 +14459,7 @@ process.stdout.write(`${JSON.stringify(snapshot)}\n`); ActiveExecution::HostFunction(HostFunctionExecution::default()), ) .with_guest_cwd(String::from("/")) + .with_vm_pending_byte_budgets(nested_stdin_budget, nested_event_budget) .with_env(nested_env) .with_host_cwd(host_cwd), ); @@ -18559,6 +18659,14 @@ await new Promise(() => {}); *observed_rpc_methods .entry(request.method.clone()) .or_default() += 1; + if request.method == "fs.readSync" && !saw_read_batch { + assert_eq!( + observed_rpc_methods.get("fs.openSync"), + Some(&10), + "all readFile calls must open before the first payload read" + ); + saw_read_batch = true; + } if !request.method.starts_with("fs.promises.") { block_on_sidecar!( sidecar, @@ -18574,13 +18682,11 @@ await new Promise(() => {}); pending_requests.push(request); - let expected_method = if !saw_write_batch { - "fs.promises.writeFile" - } else if !saw_read_batch { - "fs.promises.readFile" - } else { - panic!("received unexpected extra fs.promises request batch"); - }; + assert!( + !saw_write_batch, + "unexpected extra fs.promises request batch" + ); + let expected_method = "fs.promises.writeFile"; if pending_requests.len() == 10 { assert!( @@ -18606,11 +18712,7 @@ await new Promise(() => {}); .expect("handle batched javascript promises rpc event"); } - if !saw_write_batch { - saw_write_batch = true; - } else { - saw_read_batch = true; - } + saw_write_batch = true; } } ActiveExecutionEvent::Stdout(chunk) @@ -18688,7 +18790,7 @@ await new Promise(() => {}); ); assert!( saw_read_batch, - "expected Promise.all(readFile) to issue a full batch before the first response; pending methods={:?}, rpc_methods={observed_rpc_methods:?}, exit={held_exit:?}, stdout={observed_stdout:?}, stderr={observed_stderr:?}, other_events={observed_other_events:?}", + "expected Promise.all(readFile) to admit all files before reading bounded payloads; pending methods={:?}, rpc_methods={observed_rpc_methods:?}, exit={held_exit:?}, stdout={observed_stdout:?}, stderr={observed_stderr:?}, other_events={observed_other_events:?}", pending_requests .iter() .map(|request| request.method.as_str()) diff --git a/crates/vm/tests/xfstests_correctness.rs b/crates/vm/tests/xfstests_correctness.rs index 0e97043e80..a57718d584 100644 --- a/crates/vm/tests/xfstests_correctness.rs +++ b/crates/vm/tests/xfstests_correctness.rs @@ -1784,6 +1784,17 @@ fn xfstests_wasi_xfs_io_links_open_and_unnamed_files() { ); assert_eq!(exit_code, 0, "stdout: {stdout}\nstderr: {stderr}"); + // Unnamed inodes belong to the default root filesystem. Custom writable + // mounts intentionally reject O_TMPFILE instead of exposing a backing file. + guest_filesystem_call( + &mut sidecar, + 9, + &connection_id, + &session_id, + &vm_id, + filesystem_request(GuestFilesystemOperation::CreateDir, "/unnamed-probe"), + ); + let (stdout, stderr, exit_code) = execute_command( &mut sidecar, 6, @@ -1799,8 +1810,8 @@ fn xfstests_wasi_xfs_io_links_open_and_unnamed_files() { "-c", "pread 0 4096", "-c", - "flink /mnt/test/unnamed-link", - "/mnt/test", + "flink /unnamed-probe/unnamed-link", + "/unnamed-probe", ], ); assert_eq!(exit_code, 0, "stdout: {stdout}\nstderr: {stderr}"); @@ -1817,7 +1828,7 @@ fn xfstests_wasi_xfs_io_links_open_and_unnamed_files() { "sh", &[ "-c", - "test -f /mnt/test/named-link && test -f /mnt/test/unnamed-link && test $(wc -c < /mnt/test/unnamed-link) -eq 4096 && ! ls -A /mnt/test | grep '^.agentos-tmpfile-'", + "test -f /mnt/test/named-link && test -f /unnamed-probe/unnamed-link && test $(wc -c < /unnamed-probe/unnamed-link) -eq 4096 && ! ls -A /unnamed-probe | grep '^.agentos-tmpfile-'", ], ); assert_eq!(exit_code, 0, "stdout: {stdout}\nstderr: {stderr}"); @@ -1830,7 +1841,7 @@ fn xfstests_wasi_xfs_io_links_open_and_unnamed_files() { &vm_id, "xfstests-tmpfile-read-only", "xfs_io", - &["-Tr", "/mnt/test", "-c", "close"], + &["-Tr", "/unnamed-probe", "-c", "close"], ); assert_ne!(exit_code, 0, "read-only O_TMPFILE unexpectedly succeeded"); assert!( @@ -1838,6 +1849,22 @@ fn xfstests_wasi_xfs_io_links_open_and_unnamed_files() { "stdout: {stdout}\nstderr: {stderr}" ); + let (stdout, stderr, exit_code) = execute_command( + &mut sidecar, + 10, + &connection_id, + &session_id, + &vm_id, + "xfstests-tmpfile-custom-mount", + "xfs_io", + &["-T", "/mnt/test", "-c", "close"], + ); + assert_ne!(exit_code, 0, "custom mount unexpectedly accepted O_TMPFILE"); + assert!( + stderr.to_ascii_lowercase().contains("not supported"), + "stdout: {stdout}\nstderr: {stderr}" + ); + dispose_vm_and_close_session_wire(&mut sidecar, &connection_id, &session_id, &vm_id); drop(sidecar); drop(root); diff --git a/packages/build-tools/bridge-src/builtins/fs.ts b/packages/build-tools/bridge-src/builtins/fs.ts index f2bd8910a4..5e656e0aa7 100644 --- a/packages/build-tools/bridge-src/builtins/fs.ts +++ b/packages/build-tools/bridge-src/builtins/fs.ts @@ -554,6 +554,7 @@ var FileHandle = class _FileHandle { throwIfAborted(signal); const chunk = import_buffer.Buffer.alloc(FILE_HANDLE_READ_CHUNK_BYTES); const { bytesRead } = await handle.read(chunk, 0, chunk.byteLength, null); + throwIfAborted(signal); if (bytesRead === 0) { break; } @@ -2695,12 +2696,16 @@ function toNodeFsError(err, syscall, path) { const description = POSIX_ERROR_DESCRIPTIONS[code] ?? code.slice(1).toLowerCase(); const target = typeof path === "string" ? ` '${path}'` : ""; - return createFsError( + const error = createFsError( code, `${code}: ${description}, ${syscall}${target}`, syscall, typeof path === "string" ? path : undefined, ); + // Preserve the kernel diagnostic (including configured resource limits) + // while exposing Node-compatible errno, syscall and path fields. + error.cause = err; + return error; } // Like Node, `filter(src, dest)` runs before the pair is validated, and a false // result skips that path and everything under it. diff --git a/packages/build-tools/bridge-src/builtins/http.ts b/packages/build-tools/bridge-src/builtins/http.ts index 7925e92d89..cdeccf9a20 100644 --- a/packages/build-tools/bridge-src/builtins/http.ts +++ b/packages/build-tools/bridge-src/builtins/http.ts @@ -5280,6 +5280,12 @@ function attachHttpServerSocket(server, socket) { badBody(); return; } + // Node HTTP does not keep the writable half open after the peer's EOF. + // A response waiting on application work must not retain a dead client. + if (current?.bodyDone && !server.httpAllowHalfOpen) { + finishSocket(); + return; + } scheduleDispatch(); }; const onClose = () => { @@ -5417,7 +5423,10 @@ function attachHttpServerSocket(server, socket) { while (!detached && !socket.destroyed) { if (current) { feedBody(); - if (current.bodyDone && !current.responseDone) socket.pause(); + // Pause pipelined bytes, not an empty socket: an empty paused + // reader cannot observe cancellation while the response is held. + if (current.bodyDone && !current.responseDone && buffer.length > 0) + socket.pause(); if (!current.bodyDone || !current.responseDone || detached) return; current = null; if (ended && !buffer.length) { diff --git a/packages/build-tools/bridge-src/builtins/uint8array-encoding.ts b/packages/build-tools/bridge-src/builtins/uint8array-encoding.ts index 00d6ca79b2..d9ad6be554 100644 --- a/packages/build-tools/bridge-src/builtins/uint8array-encoding.ts +++ b/packages/build-tools/bridge-src/builtins/uint8array-encoding.ts @@ -3,11 +3,13 @@ import { createBridgeSyncFacade } from "./fs.js"; const nativeEncoding = createBridgeSyncFacade("uint8array.encode"); // `Uint8Array` hex and base64 methods from tc39/proposal-arraybuffer-base64. -// Keep native methods when available, except setFromBase64: V8 15.8 still +// Keep native decoders when available, except setFromBase64: V8 15.8 still // validates trailing input after filling the destination and overcounts trailing // whitespace. Retain our bounded decoder until native behavior matches // https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64. // +// Encoders retain the owning pre-allocation memory check even when V8 supplies +// native methods; native string allocation alone does not enforce that bound. // These act as built-ins, so they read the view through captured intrinsics. // A subclass's `length`, or a guest patch of `slice` or `Buffer`, cannot change // the bytes they see or the type they return. @@ -379,7 +381,12 @@ function decodeHexInto(target, string, maxLength) { function installEncodingMethods(target, methods) { for (const [name, value] of Object.entries(methods)) { - if (name !== "setFromBase64" && typeof target[name] === "function") + if ( + name !== "setFromBase64" && + name !== "toHex" && + name !== "toBase64" && + typeof target[name] === "function" + ) continue; Object.defineProperty(target, name, { configurable: true, diff --git a/packages/core/src/event-buffer.ts b/packages/core/src/event-buffer.ts index 9ec209e095..09a8a5d5e1 100644 --- a/packages/core/src/event-buffer.ts +++ b/packages/core/src/event-buffer.ts @@ -142,17 +142,31 @@ export class SidecarEventBuffer { } buffer(event: TEvent): SidecarEventBufferOverflow | null { - // `disposed` is the last event the sidecar emits for a VM. Nothing can - // claim that VM's unclaimed events afterwards, so release them instead of - // holding them for the sidecar's lifetime. if ( event.payload.type === "vm_lifecycle" && event.payload.state === "disposed" && event.ownership.scope === "vm" ) { - this.releaseOwner(event.ownership); - return null; + let hasPendingData = false; + for (const [id, record] of this.bufferedEvents) { + if ( + !ownershipMatchesSelector(event.ownership, record.event.ownership) + ) { + continue; + } + if (record.event.payload.type === "vm_lifecycle") { + this.remove(id); + } else { + hasPendingData = true; + } + } + // Unclaimed lifecycle-only history ends here. A pending data backlog + // still needs the terminal marker after its output/completion events. + if (!hasPendingData) return null; } + // A terminal event can arrive in the same transport turn as final output + // and exit. Keep the ordered backlog until its consumer drains it; receipt + // of `disposed` does not mean the consumer has observed those events. if (this.bufferedEvents.size >= this.capacity) { return new SidecarEventBufferOverflow({ capacity: this.capacity, @@ -198,22 +212,6 @@ export class SidecarEventBuffer { return null; } - private releaseOwner(ownership: LiveOwnershipScope): void { - const queue = this.bufferedEventQueues.get(ANY_BUFFERED_EVENT_KEY); - if (!queue) { - return; - } - for (const eventId of [...queue]) { - const record = this.bufferedEvents.get(eventId); - if ( - record && - ownershipMatchesSelector(ownership, record.event.ownership) - ) { - this.remove(eventId); - } - } - } - private takeFromKey(key: string): TEvent | null { const queue = this.bufferedEventQueues.get(key); if (!queue) { diff --git a/packages/core/src/kernel-proxy.ts b/packages/core/src/kernel-proxy.ts index bf88daf9d9..25d3d807e9 100644 --- a/packages/core/src/kernel-proxy.ts +++ b/packages/core/src/kernel-proxy.ts @@ -1162,8 +1162,8 @@ export class SidecarKernelProxy { } }, onStderr: (chunk) => { - // `onData` is the ordered PTY rendering stream. `onStderr` remains an - // optional channel-specific diagnostic tap and must not also be rendered. + // Guest fd 1 and fd 2 share the ordered PTY stream. Non-PTY runtime + // diagnostics can still arrive here; do not render the tap a second time. for (const handler of terminalHandlers) { handler(chunk); } diff --git a/packages/core/src/runtime-compat.ts b/packages/core/src/runtime-compat.ts index 33b4415810..413a660a7e 100644 --- a/packages/core/src/runtime-compat.ts +++ b/packages/core/src/runtime-compat.ts @@ -259,7 +259,7 @@ export interface OpenShellOptions { rows?: number; /** Engine affinity inherited by standalone WASM commands launched by the shell. */ wasmBackend?: "v8" | "wasmtime" | "wasmtime-threads"; - /** Optional stderr-only diagnostic tap; do not render it alongside `onData`. */ + /** Runtime diagnostics outside the PTY; guest stdout and stderr share `onData`. */ onStderr?: (data: Uint8Array) => void; } diff --git a/packages/core/src/runtime.ts b/packages/core/src/runtime.ts index 6651305916..d2854282c2 100644 --- a/packages/core/src/runtime.ts +++ b/packages/core/src/runtime.ts @@ -148,7 +148,7 @@ export interface OpenShellOptions { rows?: number; /** Engine affinity inherited by standalone WASM commands launched by the shell. */ wasmBackend?: "v8" | "wasmtime" | "wasmtime-threads"; - /** Optional stderr-only diagnostic tap; do not render it alongside `onData`. */ + /** Runtime diagnostics outside the PTY; guest stdout and stderr share `onData`. */ onStderr?: (data: Uint8Array) => void; } diff --git a/packages/core/src/sidecar/rpc-client.ts b/packages/core/src/sidecar/rpc-client.ts index 07928cdc1e..64e7a563b8 100644 --- a/packages/core/src/sidecar/rpc-client.ts +++ b/packages/core/src/sidecar/rpc-client.ts @@ -1114,8 +1114,8 @@ export class SidecarKernelProxy { } }, onStderr: (chunk) => { - // `onData` is the ordered PTY rendering stream. `onStderr` remains an - // optional channel-specific diagnostic tap and must not also be rendered. + // Guest fd 1 and fd 2 share the ordered PTY stream. Non-PTY runtime + // diagnostics can still arrive here; do not render the tap a second time. for (const handler of terminalHandlers) { handler(chunk); } @@ -2020,7 +2020,11 @@ export class SidecarKernelProxy { // a typed failure (pump, VM lifecycle, signal), or disposal wakes this. // No timer and no sidecar request is held while the process runs. entry.waitPromise = (async () => { - while (entry.exitCode === null && !entry.waitError && !this.disposed) { + while ( + entry.exitCode === null && + ((this.disposing && !entry.launchRejected) || + (!entry.waitError && !this.disposed)) + ) { await new Promise((resolve) => { entry.wakeWait = () => { entry.wakeWait = null; @@ -2029,31 +2033,9 @@ export class SidecarKernelProxy { }); } - if (entry.exitCode === null && this.disposing && !entry.launchRejected) { - // Disposal starts before TERM/KILL can publish an exit. Preserve a - // bounded opportunity for authoritative status instead of declaring - // failure at the moment disposal closes request admission. - await new Promise((resolve) => { - const finish = () => { - clearTimeout(timer); - if (entry.wakeWait === wake) entry.wakeWait = null; - resolve(); - }; - const wake = () => { - if ( - entry.exitCode !== null || - entry.launchRejected || - !this.disposing - ) - finish(); - }; - const timer = setTimeout( - finish, - DEFAULT_VM_DISPOSE_EXIT_CONFIRMATION_TIMEOUT_MS, - ); - entry.wakeWait = wake; - }); - } + // disposeInternal owns the bounded TERM/KILL and final event drain. + // Its terminal transition wakes this waiter; starting a second timer + // here could reject before the sidecar's authoritative exit arrives. if (entry.exitCode !== null) return entry.exitCode; throw ( diff --git a/packages/core/src/test-runtime.ts b/packages/core/src/test-runtime.ts index f3da9aac28..dd52c8e1bb 100644 --- a/packages/core/src/test-runtime.ts +++ b/packages/core/src/test-runtime.ts @@ -275,7 +275,7 @@ export interface OpenShellOptions { rows?: number; /** Engine affinity inherited by standalone WASM commands launched by the shell. */ wasmBackend?: "v8" | "wasmtime" | "wasmtime-threads"; - /** Optional stderr-only diagnostic tap; do not render it alongside `onData`. */ + /** Runtime diagnostics outside the PTY; guest stdout and stderr share `onData`. */ onStderr?: (data: Uint8Array) => void; } diff --git a/packages/core/tests/bridge-vm-budgets.test.ts b/packages/core/tests/bridge-vm-budgets.test.ts index de04c47eed..badac14add 100644 --- a/packages/core/tests/bridge-vm-budgets.test.ts +++ b/packages/core/tests/bridge-vm-budgets.test.ts @@ -217,6 +217,8 @@ test("actual paused HTTP response budget rejects finite overflow and raised poli return error; }, ); + // The parser first resumes transport after installing its listeners. + expect(o.socket.resumes).toBe(1); o.socket.emit( "data", Buffer.from("HTTP/1.1 200 OK\r\nContent-Length: 8\r\n\r\nA"), @@ -227,7 +229,7 @@ test("actual paused HTTP response budget rejects finite overflow and raised poli o.response().allow = true; o.response().resume(); await Promise.resolve(); - expect(o.socket.resumes).toBe(1); + expect(o.socket.resumes).toBe(2); o.socket.emit("data", Buffer.from("FGH")); expect(await done).toBeNull(); expect(Buffer.concat(o.response().chunks).toString()).toBe("ABCDEFGH"); diff --git a/packages/core/tests/dispose-exit-status.test.ts b/packages/core/tests/dispose-exit-status.test.ts index a47a5538b9..6ce3bc4dea 100644 --- a/packages/core/tests/dispose-exit-status.test.ts +++ b/packages/core/tests/dispose-exit-status.test.ts @@ -49,6 +49,12 @@ test("a process that exits while another process prints reports its real exit st expect(exits).toEqual([{ pid, outcome: "exited", exitCode: 3 }]); // The flood was still running, so the dispose killed it. expect(floodExits).toEqual([ - { pid: flood.pid, outcome: "signalled", signal: "SIGKILL" }, + { + pid: flood.pid, + outcome: "signalled", + signal: "SIGKILL", + exitCode: 137, + exitSignal: 9, + }, ]); }, 60_000); diff --git a/packages/core/tests/event-buffer.test.ts b/packages/core/tests/event-buffer.test.ts index 5224d26721..13fe1dcdab 100644 --- a/packages/core/tests/event-buffer.test.ts +++ b/packages/core/tests/event-buffer.test.ts @@ -27,6 +27,76 @@ const vmReadyEvent: LiveSidecarEventFrame = { }; describe("event buffer helpers", () => { + it("retires lifecycle-only history without removing another VM's output", () => { + const buffer = new SidecarEventBuffer(4); + const output: LiveSidecarEventFrame = { + ownership: { + scope: "vm", + connection_id: "conn", + session_id: "session", + vm_id: "other", + }, + payload: { + type: "process_output", + process_id: "proc-1", + channel: "stdout", + chunk: new Uint8Array([42]), + }, + }; + expect(buffer.buffer(output)).toBe(null); + for (let cycle = 0; cycle < 16; cycle++) { + expect(buffer.buffer(vmReadyEvent)).toBe(null); + expect( + buffer.buffer({ + ...vmReadyEvent, + payload: { type: "vm_lifecycle", state: "disposing" }, + }), + ).toBe(null); + expect( + buffer.buffer({ + ...vmReadyEvent, + payload: { type: "vm_lifecycle", state: "disposed" }, + }), + ).toBe(null); + expect(buffer.size).toBe(1); + } + expect(buffer.take(normalizeSidecarEventMatcher({ any: true }))).toBe( + output, + ); + }); + + it("preserves terminal output and exit until the consumer drains disposal", () => { + const buffer = new SidecarEventBuffer(4); + const events: LiveSidecarEventFrame[] = [ + { + ...vmReadyEvent, + payload: { + type: "process_output", + process_id: "proc-1", + channel: "stdout", + chunk: new Uint8Array([42]), + }, + }, + { + ...vmReadyEvent, + payload: { + type: "process_exited", + process_id: "proc-1", + exit_code: 143, + exit_signal: 15, + }, + }, + { ...vmReadyEvent, payload: { type: "vm_lifecycle", state: "disposed" } }, + ]; + for (const event of events) expect(buffer.buffer(event)).toBe(null); + const matcher = normalizeSidecarEventMatcher( + (event) => + event.ownership.scope === "vm" && event.ownership.vm_id === "vm", + ); + for (const event of events) expect(buffer.take(matcher)).toBe(event); + expect(buffer.size).toBe(0); + }); + it("reports bounded event buffer overflows", () => { const error = new SidecarEventBufferOverflow({ capacity: 4, diff --git a/packages/core/tests/fs-read-file-abort.test.ts b/packages/core/tests/fs-read-file-abort.test.ts index ee6d72e0f5..c7dae610b0 100644 --- a/packages/core/tests/fs-read-file-abort.test.ts +++ b/packages/core/tests/fs-read-file-abort.test.ts @@ -23,13 +23,34 @@ function readFileWithBridge(readFile: () => Promise) { if (!declaration) throw new Error(`Missing production function ${name}`); return declaration[0]; }); - return runInNewContext(`${functions.join("\n")}\nfsReadFileAsync`, { - validateEncodingOption() {}, - normalizePathLike: (path: string) => path, - _fsAsync: { readFile, readFileBinary: readFile }, - import_buffer: { Buffer }, - bridgeErrorCode: (error: { code?: string }) => error.code, - }) as ( + const method = source.slice( + source.indexOf("\tasync readFile(options) {"), + source.indexOf("\n\tasync writeFile(data, options) {"), + ); + return runInNewContext( + `${functions.join("\n")} + class FileHandle { + static _assertHandle(handle) { return handle; } + stat() { return Promise.resolve({size: 0}); } + read() { return readFile().then(() => ({bytesRead: 0})); } + close() { return Promise.resolve(); } + ${method} + } + const _FileHandle=FileHandle; + fsReadFileAsync`, + { + validateEncodingOption() {}, + normalizePathLike: (path: string) => path, + fs: { openSync: () => 3 }, + FILE_HANDLE_MAX_READ_BYTES: 1024, + FILE_HANDLE_READ_CHUNK_BYTES: 64, + waitForNextTick: async () => {}, + readFile, + setImmediate, + import_buffer: { Buffer }, + bridgeErrorCode: (error: { code?: string }) => error.code, + }, + ) as ( path: string, options: { encoding?: string; signal: AbortSignal }, ) => Promise; @@ -40,10 +61,15 @@ test.each([ undefined, ])("async path read (%s) rejects cancellation while awaiting host I/O", async (encoding) => { let finish!: (value: string) => void; + let markStarted!: () => void; + const started = new Promise((resolve) => { + markStarted = resolve; + }); const read = readFileWithBridge( () => new Promise((resolve) => { finish = resolve; + markStarted(); }), ); const controller = new AbortController(); @@ -54,6 +80,8 @@ test.each([ code: "ABORT_ERR", cause: reason, }); + // Wait until the production reader is awaiting its host read. + await started; controller.abort(reason); finish("contents must not be delivered after cancellation"); await rejected; diff --git a/packages/core/tests/http-cancellation-parity.nightly.test.ts b/packages/core/tests/http-cancellation-parity.nightly.test.ts index d06521a07a..867390235e 100644 --- a/packages/core/tests/http-cancellation-parity.nightly.test.ts +++ b/packages/core/tests/http-cancellation-parity.nightly.test.ts @@ -515,7 +515,10 @@ describe("guest HTTP cancellation and TLS lifecycle match native Node", () => { await vm?.dispose(); }); - async function compare(source: string): Promise> { + async function compare( + source: string, + expectedStderr: string | RegExp = "", + ): Promise> { const native = spawnSync(process.execPath, ["-e", source], { encoding: "utf8", timeout: 10_000, @@ -531,7 +534,9 @@ describe("guest HTTP cancellation and TLS lifecycle match native Node", () => { output: { capture: "all" }, }); expect(guest.exitCode, guest.stderr).toBe(0); - expect(guest.stderr).toBe(""); + if (typeof expectedStderr === "string") + expect(guest.stderr).toBe(expectedStderr); + else expect(guest.stderr).toMatch(expectedStderr); const nativeResult = JSON.parse(native.stdout.trim()) as Record< string, unknown @@ -706,7 +711,10 @@ describe("guest HTTP cancellation and TLS lifecycle match native Node", () => { }, 30_000); test("a 16 KiB chunk extension remains accepted", async () => { - const result = await compare(chunkExtensionLine(false, 16 * 1024)); + const result = await compare( + chunkExtensionLine(false, 16 * 1024), + /^limits\.http\.maxChunkSizeLineBytes approaching limit: observed=\d+ maximum=16416; raise limits\.http\.maxChunkSizeLineBytes when creating the VM\n$/, + ); expect(result.events).toContain("response-end"); expect(result.events).not.toContain("response-error"); }, 30_000); diff --git a/packages/core/tests/integration/node-binary-behavior.nightly.test.ts b/packages/core/tests/integration/node-binary-behavior.nightly.test.ts index 7c5c62241a..66aa636dc4 100644 --- a/packages/core/tests/integration/node-binary-behavior.nightly.test.ts +++ b/packages/core/tests/integration/node-binary-behavior.nightly.test.ts @@ -12,7 +12,14 @@ * Gracefully skipped when WASM binaries are not built. */ -import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; +import { + chmod, + mkdir, + mkdtemp, + rm, + symlink, + writeFile, +} from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import type { IntegrationKernelResult } from "@rivet-dev/agentos-test-harness"; @@ -272,6 +279,7 @@ describeIf(!skipReason, "node binary: exec child_process", () => { it("node -e spawnSync resolves shebang-backed node_modules/.bin commands through JavaScript runtime", async () => { tempDir = await mkdtemp(path.join(tmpdir(), "kernel-node-bin-")); + await chmod(tempDir, 0o755); await writeFile( path.join(tempDir, "package.json"), JSON.stringify({ name: "node-bin-repro", private: true }), @@ -285,6 +293,7 @@ describeIf(!skipReason, "node binary: exec child_process", () => { "#!/usr/bin/env node", "console.log(`hello ${process.argv.slice(2).join(' ')}`);", ].join("\n"), + { mode: 0o755 }, ); await mkdir(path.join(tempDir, "node_modules", ".bin"), { recursive: true, diff --git a/packages/core/tests/leak-rpc-client.test.ts b/packages/core/tests/leak-rpc-client.test.ts index 63a64d4711..78405f0d41 100644 --- a/packages/core/tests/leak-rpc-client.test.ts +++ b/packages/core/tests/leak-rpc-client.test.ts @@ -582,7 +582,7 @@ it("a sidecar-side VM disposal rejects wait instead of hanging or inventing an e } }); -it("disposal wakes wait before a stalled DisposeVm completes", async () => { +it("disposal preserves authoritative exit while DisposeVm acknowledgement is stalled", async () => { vi.useFakeTimers(); vi.spyOn(console, "error").mockImplementation(() => {}); const stub = createStubClient(); @@ -603,17 +603,22 @@ it("disposal wakes wait before a stalled DisposeVm completes", async () => { settled = true; }, ); - const assertion = expect(waiting).rejects.toThrow( - /^termination_failed:.*disposed/, - ); + const assertion = expect(waiting).resolves.toBe(137); await vi.advanceTimersByTimeAsync(100); const disposal = proxy.dispose(); try { - await vi.advanceTimersByTimeAsync(499); + await vi.advanceTimersByTimeAsync(1_000); expect(settled).toBe(false); - await vi.advanceTimersByTimeAsync(1); + stub.pushEvent({ + ownership: { scope: "vm", vm_id: vm.vmId }, + payload: { + type: "process_exited", + process_id: "proc-1000000", + exit_code: 137, + }, + }); await assertion; - expect(proc.exitCode).toBeNull(); + expect(proc.exitCode).toBe(137); } finally { acknowledgeDispose(); await vi.runAllTimersAsync(); diff --git a/packages/core/tests/node-fs-recursive.native.test.ts b/packages/core/tests/node-fs-recursive.native.test.ts index fad3391f37..23f1dec330 100644 --- a/packages/core/tests/node-fs-recursive.native.test.ts +++ b/packages/core/tests/node-fs-recursive.native.test.ts @@ -25,7 +25,7 @@ async function run(limits: Record = {}, denied = false) { }); let stdout = "", stderr = ""; - const source = `const fs=require('node:fs');(async()=>{const opts={recursive:true,withFileTypes:true};const rows=e=>e.filter(x=>x.isFile()).map(x=>x.parentPath.replace('/tmp/tree','')+'/'+x.name).sort();try{console.log(JSON.stringify({sync:rows(fs.readdirSync('/tmp/tree',opts)),async:rows(await fs.promises.readdir('/tmp/tree',opts)),names:fs.readdirSync('/tmp/tree',{recursive:true}).sort()}));}catch(e){console.log(JSON.stringify({code:e.code,message:e.message}));}})();`; + const source = `const fs=require('node:fs');(async()=>{const opts={recursive:true,withFileTypes:true};const rows=e=>e.filter(x=>x.isFile()).map(x=>x.parentPath.replace('/tmp/tree','')+'/'+x.name).sort();try{console.log(JSON.stringify({sync:rows(fs.readdirSync('/tmp/tree',opts)),async:rows(await fs.promises.readdir('/tmp/tree',opts)),names:fs.readdirSync('/tmp/tree',{recursive:true}).sort()}));}catch(e){console.log(JSON.stringify({code:e.code,message:e.message,cause:e.cause?.message}));}})();`; const p = await vm.process.spawn("node", ["-e", source], { timeoutMs: 10000, onStdout: (b) => (stdout += Buffer.from(b).toString()), @@ -46,7 +46,9 @@ test("recursive directory reads include nested names and correct Dirent parents }); }, 15000); test("recursive directory reads retain the configured traversal quota", async () => { - expect((await run({ maxRecursiveFsEntries: 2 })).message).toMatch( + const result = await run({ maxRecursiveFsEntries: 2 }); + expect(result.code).toBe("ENOMEM"); + expect(result.cause).toMatch( /maxRecursiveFsEntries|recursive.*entries|limit/i, ); }, 15000); diff --git a/packages/core/tests/node-uint8array-encoding-memory.native.test.ts b/packages/core/tests/node-uint8array-encoding-memory.native.test.ts index 2684d6238d..7f3d232a9d 100644 --- a/packages/core/tests/node-uint8array-encoding-memory.native.test.ts +++ b/packages/core/tests/node-uint8array-encoding-memory.native.test.ts @@ -28,13 +28,17 @@ async function execute(source: string, heapMb = 128) { } test("large Uint8Array encodings preserve offset/shared views and every base64 remainder", async () => { - const source = `const{createHash}=require('node:crypto');const observations=[],digest=s=>[s.length,createHash('sha256').update(s).digest('hex')];for(const shared of[false,true])for(const length of[65536,65537,65538,65539]){const buffer=shared?new SharedArrayBuffer(length+8):new ArrayBuffer(length+8),whole=new Uint8Array(buffer);for(let i=0;i[s.length,createHash('sha256').update(s).digest('hex')];for(const shared of[false,true])for(const length of[65536,65537,65538,65539]){const buffer=shared?new SharedArrayBuffer(length+8):new ArrayBuffer(length+8),whole=new Uint8Array(buffer);for(let i=0;iBuffer.from(v).toString('hex'),base64=(v,o={})=>{let s=Buffer.from(v).toString('base64');if(o.alphabet==='base64url')s=s.replaceAll('+','-').replaceAll('/','_');return o.omitPadding?s.replace(/=+$/,''):s;};`; + const guest = `const hex=v=>v.toHex(),base64=(v,o)=>v.toBase64(o);`; + const host = spawnSync(process.execPath, ["-e", reference + source], { encoding: "utf8", timeout: 15000, }); expect(host.status, host.stderr).toBe(0); - expect(await execute(source)).toEqual(JSON.parse(host.stdout)); + expect(await execute(guest + source)).toEqual(JSON.parse(host.stdout)); }, 25000); test("Uint8Array encoding refuses its external-memory budget before allocating and keeps the VM usable", async () => { diff --git a/packages/core/tests/process-lifecycle.nightly.test.ts b/packages/core/tests/process-lifecycle.nightly.test.ts index e972e5f3f0..9ade23f640 100644 --- a/packages/core/tests/process-lifecycle.nightly.test.ts +++ b/packages/core/tests/process-lifecycle.nightly.test.ts @@ -10,6 +10,9 @@ function isExpectedTeardownError(message: string): boolean { const normalized = message.toLowerCase(); return ( normalized.includes("err_agentos_request_admission_closed") || + normalized.includes("err_agentos_coordinator_not_found") || + normalized.includes("err_agentos_coordinator_closing") || + normalized.includes("err_agentos_coordinator_cancelled") || normalized.includes("unknown sidecar vm") || normalized.includes("already been disposed") || normalized.includes("sidecar disposed") || diff --git a/packages/core/tests/process-tree-identity.test.ts b/packages/core/tests/process-tree-identity.test.ts index e34adda00a..a4b760bb41 100644 --- a/packages/core/tests/process-tree-identity.test.ts +++ b/packages/core/tests/process-tree-identity.test.ts @@ -177,12 +177,14 @@ describe("process-tree identity", () => { client, session: {}, vm: {}, + processSnapshotRefresh: null, + sidecarProcessSnapshot: [], trackedProcesses: new Map([[tracked.pid, tracked]]), trackedProcessesById: new Map([[tracked.processId, tracked]]), observedProcessStartTimes: new Map(), processes: new Map(), }); - const records = await proxy.snapshotProcessTopology(); + const records = await proxy.currentProcessTopology(); expect(records).toHaveLength(2); expect(records.map((record) => record.info.pid)).toEqual([ 1_000_000, 1_000_000, @@ -259,31 +261,26 @@ it.each([ code: "ERR_AGENTOS_VM_LIFECYCLE_CONFLICT", retryable: true, errno: "EAGAIN", - retry: true, }, { code: "ERR_AGENTOS_VM_LIFECYCLE_CONFLICT", retryable: false, errno: "EAGAIN", - retry: false, }, { code: "ERR_AGENTOS_OTHER_FAILURE", retryable: true, errno: "EAGAIN", - retry: false, }, { code: "ERR_AGENTOS_VM_LIFECYCLE_CONFLICT", retryable: true, errno: "EIO", - retry: false, }, -])("process exit observation retries only explicit lifecycle backpressure ($code,$retryable,$errno)", async ({ +])("event-driven process wait preserves typed failures without polling ($code,$retryable,$errno)", async ({ code, retryable, errno, - retry, }) => { vi.useFakeTimers(); try { @@ -310,8 +307,8 @@ it.each([ started: true, exitCode: null as number | null, waitError: null as unknown, - waitWithFallbackPromise: null, - wakeWait: null, + waitPromise: null, + wakeWait: null as (() => void) | null, hostExitObservedAt: null, confirmExit(code: number) { this.exitCode = code; @@ -349,20 +346,87 @@ it.each([ (code: number) => ({ code }), (error: unknown) => ({ error }), ); - await vi.advanceTimersByTimeAsync(50); - if (!retry) { - expect(await outcome).toEqual({ error: blocked }); - expect(handleBackgroundProcessError).toHaveBeenCalledWith(entry, blocked); - expect(getProcessSnapshot).toHaveBeenCalledTimes(1); - expect(finishProcess).not.toHaveBeenCalled(); - return; - } + await vi.advanceTimersByTimeAsync(500); + expect(getProcessSnapshot).not.toHaveBeenCalled(); expect(entry.waitError).toBeNull(); - expect(handleBackgroundProcessError).not.toHaveBeenCalled(); - await vi.advanceTimersByTimeAsync(50); - expect(await outcome).toEqual({ code: 23 }); - expect(getProcessSnapshot).toHaveBeenCalledTimes(2); - expect(finishProcess).toHaveBeenCalledWith(entry, 23, null); + entry.waitError = blocked; + entry.wakeWait?.(); + expect(await outcome).toEqual({ error: blocked }); + expect(getProcessSnapshot).not.toHaveBeenCalled(); + expect(finishProcess).not.toHaveBeenCalled(); + } finally { + vi.useRealTimers(); + } +}); + +it("process wait coalesces until the ordered exit event without snapshot polling", async () => { + const entry = { + exitCode: null as number | null, + waitError: null, + waitPromise: null, + wakeWait: null as (() => void) | null, + }; + const getProcessSnapshot = vi.fn(); + const proxy = Object.assign(Object.create(SidecarKernelProxy.prototype), { + disposed: false, + disposing: false, + client: { getProcessSnapshot }, + }); + const first = proxy.waitForTrackedProcess(entry); + expect(proxy.waitForTrackedProcess(entry)).toBe(first); + let settled = false; + void first.then(() => { + settled = true; + }); + await Promise.resolve(); + expect(settled).toBe(false); + expect(getProcessSnapshot).not.toHaveBeenCalled(); + entry.exitCode = 23; + entry.wakeWait?.(); + expect(await first).toBe(23); +}); + +it.each([ + true, + false, +])("process wait lets bounded disposal finish before deciding exit proof (%s)", async (confirmed) => { + vi.useFakeTimers(); + try { + const entry = { + pid: 1, + exitCode: null as number | null, + waitError: null as Error | null, + waitPromise: null, + wakeWait: null as (() => void) | null, + }; + const proxy = Object.assign(Object.create(SidecarKernelProxy.prototype), { + disposed: false, + disposing: false, + }); + const wait = proxy.waitForTrackedProcess(entry); + let settled = false; + const result = wait.then( + (code: number) => { + settled = true; + return { code }; + }, + (error: Error) => { + settled = true; + return { error }; + }, + ); + proxy.disposed = true; + proxy.disposing = true; + entry.wakeWait?.(); + await vi.advanceTimersByTimeAsync(2000); + expect(settled).toBe(false); + proxy.disposing = false; + if (confirmed) entry.exitCode = 143; + else entry.waitError = new Error("termination unconfirmed"); + entry.wakeWait?.(); + expect(await result).toEqual( + confirmed ? { code: 143 } : { error: entry.waitError }, + ); } finally { vi.useRealTimers(); } diff --git a/packages/core/tests/pty-line-discipline.nightly.test.ts b/packages/core/tests/pty-line-discipline.nightly.test.ts index b245839d4b..0ff1b4d82f 100644 --- a/packages/core/tests/pty-line-discipline.nightly.test.ts +++ b/packages/core/tests/pty-line-discipline.nightly.test.ts @@ -374,6 +374,11 @@ const CASES: Case[] = [ await ctx.waitForScreen("#READY tag=sigint"); await ctx.writeShell("\x03"); const status = await ctx.waitShellStatus(); + // Process exit does not flush xterm's asynchronous write queue. + // Observe the existing final marker before capturing the screen. + if (ctx.runtime.name === "js-node") { + await ctx.waitForScreen("#DONE id=sigint"); + } ctx.snapshot("after"); // Correct: VINTR is consumed as a signal, so the byte is neither // echoed nor delivered. diff --git a/packages/core/tests/request-overload.test.ts b/packages/core/tests/request-overload.test.ts index acd8d91f07..d63cb9f566 100644 --- a/packages/core/tests/request-overload.test.ts +++ b/packages/core/tests/request-overload.test.ts @@ -169,7 +169,7 @@ test("dispose with a running process completes while held requests fill every in ]); // Settles during dispose, so capture the outcome before anything awaits it. const workerExit = target.process.wait(worker.pid).then( - () => null, + (exit) => exit, (error: unknown) => error, ); @@ -197,8 +197,14 @@ test("dispose with a running process completes while held requests fill every in vms.splice(vms.indexOf(target), 1); expect(disposedAt).toBeLessThan(firstHeldResponseAt); - // The worker's wait settles with the disposal instead of hanging. - expect(String(await workerExit)).toMatch(/VM disposed/); + // Disposal preserves the sidecar's observed terminal status, even while + // ordinary request slots are full; it must not invent a wait failure. + expect(await workerExit).toMatchObject({ + pid: worker.pid, + outcome: "signalled", + exitCode: 143, + exitSignal: 15, + }); const results = await held; expect(results.map((result) => result.status)).toEqual( results.map(() => "fulfilled"), diff --git a/packages/core/tests/sidecar-process.nightly.test.ts b/packages/core/tests/sidecar-process.nightly.test.ts index 3eafa22076..c86fe5b486 100644 --- a/packages/core/tests/sidecar-process.nightly.test.ts +++ b/packages/core/tests/sidecar-process.nightly.test.ts @@ -652,7 +652,7 @@ describe("sidecar process client", () => { driverPath, [ "import { Socket } from 'node:net';", - "const schema = { name: 'agentos-sidecar', version: 8 };", + `const schema = ${JSON.stringify(SIDECAR_PROTOCOL_SCHEMA)};`, "const control = new Socket({ fd: 3, readable: true, writable: true });", "let stdinBuffer = Buffer.alloc(0);", "const writeFrame = (frame) => {", @@ -677,6 +677,8 @@ describe("sidecar process client", () => { " respond(frame, {", " type: 'authenticated',", " connection_id: 'conn-1',", + " max_frame_bytes: 1048576,", + " max_write_files_entries: 1024,", " });", " break;", " case 'open_session':", @@ -990,9 +992,12 @@ describe("sidecar process client", () => { } }, 60_000); - test("NativeKernel wait() drains trailing stdout from short-lived processes", async () => { + test("NativeKernel wait() drains trailing stdout and stderr from short-lived processes", async () => { const projectRoot = mkdtempSync(join(tmpdir(), "agentos-fast-exit-")); cleanupPaths.push(projectRoot); + // This fixture is a guest-readable project root, not an owner-only host + // directory. Keep the normal guest UID and filesystem access checks. + chmodSync(projectRoot, 0o755); const kernel = createKernel({ filesystem: new NodeFileSystem({ root: projectRoot }), @@ -1011,6 +1016,7 @@ describe("sidecar process client", () => { "console.log('first');", "console.log('second');", "console.log('third');", + "console.error('diagnostic');", ].join(" "), ], { @@ -1025,7 +1031,7 @@ describe("sidecar process client", () => { const exitCode = await child.wait(); expect(exitCode).toBe(0); - expect(stderr).toBe(""); + expect(stderr).toBe("diagnostic\n"); expect(stdout).toBe("first\nsecond\nthird\n"); } finally { await kernel.dispose(); @@ -1878,7 +1884,7 @@ describe("sidecar process client", () => { } }, 60_000); - test("openShell preserves terminal order and exposes diagnostic stderr", async () => { + test("openShell preserves merged PTY output without duplicating guest stderr", async () => { const kernel = createKernel({ filesystem: createInMemoryFileSystem(), permissions: ALLOW_ALL_VM_PERMISSIONS, @@ -1919,9 +1925,7 @@ describe("sidecar process client", () => { value.includes("OUT:hello-shell") && value.includes("ERR:hello-shell"), }); - await waitFor(() => stderr, { - isReady: (value) => value.includes("ERR:hello-shell"), - }); + expect(await shell.wait()).toBe(0); expect(terminal).toContain("OUT:hello-shell"); expect(terminal).toContain("ERR:hello-shell"); @@ -1929,9 +1933,9 @@ describe("sidecar process client", () => { terminal.indexOf("ERR:hello-shell"), ); expect(terminal.match(/ERR:hello-shell/g)).toHaveLength(1); - expect(stderr).toContain("ERR:hello-shell"); - expect(stderr).not.toContain("OUT:hello-shell"); - expect(await shell.wait()).toBe(0); + // Like a Linux PTY, fd 1 and fd 2 share one terminal stream. Only + // non-PTY runtime diagnostics can reach the separate stderr tap. + expect(stderr).toBe(""); } finally { await kernel.dispose(); } diff --git a/packages/core/tests/sidecar-protocol-capacity.native.test.ts b/packages/core/tests/sidecar-protocol-capacity.native.test.ts index bb100aa86f..fc9375687e 100644 --- a/packages/core/tests/sidecar-protocol-capacity.native.test.ts +++ b/packages/core/tests/sidecar-protocol-capacity.native.test.ts @@ -2,7 +2,7 @@ import { expect, test } from "vitest"; import { AgentOs } from "../src/index.js"; for (const maximum of [2, 4]) - test(`protocol capacity ${maximum} remains enforced for real parked guest reads`, async () => { + test(`parked guest reads complete through bounded protocol admission at capacity ${maximum}`, async () => { const sidecar = await AgentOs.createSidecar({ runtime: { protocol: { @@ -52,12 +52,10 @@ for (const maximum of [2, 4]) { ok: true, body: "done" }, { ok: true, body: "done" }, ]); - if (maximum === 2) - expect(results[2]).toMatchObject({ - ok: false, - error: expect.stringMatching(/IN_FLIGHT_REQUEST_LIMIT/), - }); - else expect(results[2], stderr).toEqual({ ok: true, body: "done" }); + // A full active lane now queues the third request within the sidecar's + // bounded FIFO. Native transport controls separately prove queue overflow + // and deadline rejection; ordinary temporary contention is not an error. + expect(results[2], stderr).toEqual({ ok: true, body: "done" }); for (const connection of connections) await connection.close(); connections.length = 0; await vm.process.kill(proc.pid); diff --git a/packages/core/tests/software-projection.nightly.test.ts b/packages/core/tests/software-projection.nightly.test.ts index 7e7c9cd33d..126379c477 100644 --- a/packages/core/tests/software-projection.nightly.test.ts +++ b/packages/core/tests/software-projection.nightly.test.ts @@ -1,23 +1,6 @@ import { afterEach, describe, expect, test } from "vitest"; import { AgentOs } from "../src/agent-os.js"; -async function waitForExit( - vm: AgentOs, - pid: number, - timeoutMs = 30_000, -): Promise { - const deadline = Date.now() + timeoutMs; - while (Date.now() < deadline) { - const proc = vm.getProcess(pid); - if (!proc.running) { - return proc.exitCode ?? -1; - } - await new Promise((resolve) => setTimeout(resolve, 20)); - } - - throw new Error(`Timed out waiting for process ${pid} to exit`); -} - describe("software projection on the sidecar path", () => { let vm: AgentOs | undefined; @@ -55,7 +38,7 @@ describe("software projection on the sidecar path", () => { }, ); - const exitCode = await waitForExit(vm, pid); + const exitCode = (await vm.process.wait(pid)).exitCode; expect({ exitCode, stderr }).toEqual({ exitCode: 0, stderr: "" }); expect(stdout).not.toContain("write:unexpected-success"); expect(stdout).toMatch(/writeError (ERR_ACCESS_DENIED|EACCES|EPERM|EROFS)/); diff --git a/packages/core/tests/vim-interactive.nightly.test.ts b/packages/core/tests/vim-interactive.nightly.test.ts index 342e6e60a4..945ff706cb 100644 --- a/packages/core/tests/vim-interactive.nightly.test.ts +++ b/packages/core/tests/vim-interactive.nightly.test.ts @@ -255,7 +255,7 @@ describe.skipIf(!existsSync(VIM_BINARY))("interactive vim over VM PTY", () => { expect(fileContent).toBe("hello world\n"); offData(); - void __disposeAllSharedSidecarsForTesting().catch(() => {}); + await __disposeAllSharedSidecarsForTesting(); vm = undefined; }, TEST_TIMEOUT_MS, @@ -331,7 +331,7 @@ describe.skipIf(!existsSync(VIM_BINARY))("interactive vim over VM PTY", () => { await sleep(1_000); offData(); - void __disposeAllSharedSidecarsForTesting().catch(() => {}); + await __disposeAllSharedSidecarsForTesting(); vm = undefined; }, TEST_TIMEOUT_MS, diff --git a/packages/core/tests/vim-provides.nightly.test.ts b/packages/core/tests/vim-provides.nightly.test.ts index 79461e20e7..12ba8c9151 100644 --- a/packages/core/tests/vim-provides.nightly.test.ts +++ b/packages/core/tests/vim-provides.nightly.test.ts @@ -304,7 +304,7 @@ describe.skipIf(!existsSync(VIM_BINARY))( expect(fileContent).toBe("provides works\n"); offData(); - void __disposeAllSharedSidecarsForTesting().catch(() => {}); + await __disposeAllSharedSidecarsForTesting(); vm = undefined; }, TEST_TIMEOUT_MS, @@ -375,7 +375,7 @@ describe.skipIf(!existsSync(VIM_BINARY))( await settle(800); offData(); - void __disposeAllSharedSidecarsForTesting().catch(() => {}); + await __disposeAllSharedSidecarsForTesting(); vm = undefined; }, TEST_TIMEOUT_MS, diff --git a/software/curl/test/curl.nightly.test.ts b/software/curl/test/curl.nightly.test.ts index 7d9132040c..6dbeedb85c 100644 --- a/software/curl/test/curl.nightly.test.ts +++ b/software/curl/test/curl.nightly.test.ts @@ -57,6 +57,14 @@ import { brotliCompressSync, gzipSync, zstdCompressSync } from 'node:zlib'; // The Rust fallback in COMMANDS_DIR supports a smaller flag surface. const hasHttpGetTest = hasWasmBinaries && existsSync(resolve(C_BUILD_DIR, 'http_get_test')); const CURL_BIN_DIR = resolve(import.meta.dirname, '../bin'); +const toolchainMakefile = readFileSync(resolve(import.meta.dirname, '../../../toolchain/c/Makefile'), 'utf8'); +function pinnedToolVersion(name: string): string { + const version = toolchainMakefile.match(new RegExp(`^${name} := ([0-9.]+)$`, 'm'))?.[1]; + if (!version) throw new Error(`Missing toolchain version pin: ${name}`); + return version; +} +const CURL_RELEASE_VERSION = pinnedToolVersion('CURL_RELEASE_VERSION'); +const OPENSSL_VERSION = pinnedToolVersion('OPENSSL_VERSION'); const hasCurl = hasCWasmBinaries('curl') || existsSync(resolve(CURL_BIN_DIR, 'curl')); const runExternalNetwork = process.env.AGENTOS_E2E_NETWORK === '1'; const EXTERNAL_HOST = 'example.com'; @@ -651,7 +659,7 @@ describeIf(hasCurl || hasHttpGetTest, 'curl and socket layer', () => { await createKernelWithNet(); const result = await kernel.exec('curl --version'); expect(result.exitCode).toBe(0); - expect(result.stdout).toContain('curl 8.11.1'); + expect(result.stdout).toContain(`curl ${CURL_RELEASE_VERSION}`); expect(result.stdout).toMatch(/Protocols:/); }, CURL_TEST_TIMEOUT_MS); @@ -850,13 +858,13 @@ describeIf(hasCurl || hasHttpGetTest, 'curl and socket layer', () => { expect(Date.now() - startedAt).toBeLessThan(CURL_PROMPT_EXIT_BOUND_MS); }, CURL_TEST_TIMEOUT_MS); - itIf(hasCurl, 'curl --version reports the mbedTLS backend', async () => { + itIf(hasCurl, 'curl --version reports the pinned OpenSSL backend', async () => { await createKernelWithNet(); const result = await kernel.exec('curl --version'); expect(result.exitCode).toBe(0); - // Real in-guest TLS: the SSL version line must name mbedTLS, and the + // Real in-guest TLS: the SSL version line must name the pinned OpenSSL backend, and the // retired host shim string must be gone. - expect(result.stdout).toMatch(/mbedTLS/i); + expect(result.stdout).toContain(`OpenSSL/${OPENSSL_VERSION}`); expect(result.stdout).not.toMatch(/WASI-TLS|wasi-tls/i); expect(result.stdout).toMatch(/^Features:.*\bSSL\b/m); expect(result.stdout).toMatch(/^Features:.*\bbrotli\b/m); diff --git a/software/duckdb/test/duckdb.nightly.test.ts b/software/duckdb/test/duckdb.nightly.test.ts index e9af2e9997..1c1c123e69 100644 --- a/software/duckdb/test/duckdb.nightly.test.ts +++ b/software/duckdb/test/duckdb.nightly.test.ts @@ -31,7 +31,7 @@ import { existsSync } from 'node:fs'; import { resolve } from 'node:path'; import { setTimeout as sleep } from 'node:timers/promises'; -const hasWasmDuckDB = existsSync(resolve(C_BUILD_DIR, 'duckdb')); +const hasWasmDuckDB = [COMMANDS_DIR, C_BUILD_DIR].some(directory => existsSync(resolve(directory, 'duckdb'))); const hasWasmCurl = (existsSync(resolve(COMMANDS_DIR, 'curl')) || existsSync(resolve(C_BUILD_DIR, 'curl')));