quickjs-capi 0.17.30

C ABI compatibility layer: the QuickJS C API on top of the pure-Rust engine
Documentation
//! The shipped header is the reference's header.
//!
//! `include/quickjs.h` is what a C host compiles against, and it is a copy of the reference
//! interpreter's own header rather than a restatement of this crate's exports. That is the
//! only defensible choice for a compatibility layer — half of the reference header is
//! `static inline` code that expands in the *caller* and decodes a `JSValue` by hand, so a
//! header that spelled those differently would be a different ABI, not a compatible one.
//!
//! A copy rots the way any copy does, so this test is the mechanism that keeps it honest:
//! everything from `#ifndef QUICKJS_H` onward has to be byte-identical to the reference's
//! header. It skips when the oracle is not built on this machine, exactly as the symbol
//! table test does — the oracle is a *check*, not a build dependency.
//!
//! The preamble above the include guard is ours: the provenance, the MIT notice being the
//! reference's, and the count disclaimer. It is deliberately outside the compared region,
//! because it says things the reference's header has no reason to say.

use std::path::{Path, PathBuf};

/// Where the reference's header is, when this machine has one.
fn reference_header() -> Option<PathBuf> {
    let candidates = [
        "oracle/quickjs-master/quickjs.h",
        "../../oracle/quickjs-master/quickjs.h",
    ];
    candidates
        .iter()
        .map(PathBuf::from)
        .find(|path| path.is_file())
}

/// The two files from the include guard onward.
fn contract(path: &Path) -> String {
    let text = std::fs::read_to_string(path).expect("header is readable");
    // Line-anchored: the provenance preamble *mentions* `#ifndef QUICKJS_H` in prose, and a
    // substring search would cut the contract off inside that sentence.
    let start = text
        .find("\n#ifndef QUICKJS_H\n")
        .unwrap_or_else(|| panic!("{} has no include guard on its own line", path.display()))
        + 1;
    text[start..].to_string()
}

#[test]
fn the_shipped_header_matches_the_reference() {
    let Some(reference) = reference_header() else {
        eprintln!("skipping: no oracle/quickjs-master/quickjs.h on this machine");
        return;
    };
    let ours = contract(Path::new("include/quickjs.h"));
    let theirs = contract(&reference);
    assert_eq!(
        ours, theirs,
        "include/quickjs.h has drifted from the reference's header; re-copy everything from \
         `#ifndef QUICKJS_H` onward (keep the provenance preamble) and re-run the ABI tests"
    );
}

/// The preamble is the part we wrote, so it has to say the things a reader needs.
///
/// Checked rather than trusted: a provenance block that silently loses its version number is
/// the exact failure this whole exercise is about — a claim on a package page that nothing
/// verifies.
#[test]
fn the_preamble_states_provenance_and_the_verdict_counts() {
    let text = std::fs::read_to_string("include/quickjs.h").expect("header is readable");
    for needle in ["Provenance", "bellard/quickjs", "MIT", "194", "17", "12"] {
        assert!(
            text.contains(needle),
            "the header preamble no longer mentions {needle:?}"
        );
    }
    // The preamble has to stay above the guard: inside it, it would be part of the contract
    // compared against the reference above and the two tests would contradict each other.
    let guard = text
        .find("\n#ifndef QUICKJS_H\n")
        .expect("has a guard on its own line");
    assert!(
        text[..guard].contains("Provenance"),
        "the provenance block has slipped below the include guard"
    );
}