Repository navigation
feat(capi): Lexbor's users had no C API to switch to - #78
Merged
Merged
Conversation
crates/parsec-capi builds libparsec as a shared and a static library over parsec-html's public API, with include/parsec.h generated by cbindgen and checked in. Handles are opaque, nodes are plain numbers checked against their document, strings come back either borrowed (pointer and length) or as copies freed with parsec_string_free, and every call is NULL-safe, returns a status code and runs inside catch_unwind. Selectors compile once and run on any document, from any thread. package.sh lays out a package with the header, the libraries, parsec.pc and a CMake config with parsec::parsec and parsec::parsec_static targets. tests/capi.c parses, selects, walks, edits and serializes, and tries every error path. cargo test builds and runs it on Unix; CI's capi job builds it through find_package on Linux, macOS (universal) and Windows, shared and static; capi-checks runs it under ASan, LSan and UBSan through pkg-config, checks the header matches cbindgen's output, and compiles the header as C++.
release-capi.yml builds the C package for Linux x86-64 and ARM64 (on 22.04, for glibc 2.35), macOS universal, and Windows x86-64 and ARM64, smoke-tests each with tests/capi.c through CMake, and archives it. On a v* tag it waits for the release release.yml makes once the tag's signature checks out, then attests the archives and attaches them with a SHA256SUMS file and the provenance bundle. Pull requests that touch the C API build and test all five packages.
is(f(&len), len, ...) leaves the order of its arguments to the compiler. Clang works them out left to right, GCC and MSVC right to left, so on Linux and Windows len was read before f wrote it. The helpers now take the length's address and read it inside.
With CARGO_TERM_COLOR=always, as CI sets it, rustc's native-static-libs note ends in an escape code, which package.sh copied onto the last library in parsec.pc and the CMake config (-lc on Linux, -lm on macOS), and the static link couldn't find it.
# Conflicts: # Cargo.lock # Cargo.toml # README.md # ROADMAP.md
parsed() and the selector's success path wrote through `out` relying on a NULL check made earlier, in another function; CodeQL flagged both. clear() now turns the checked pointer into a reference, and the writes go through that.
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Parsec could only be called from Rust. Lexbor's users, and the bindings built on it (PHP, Ruby, Elixir, Crystal, selectolax), call C, so there was nothing for them to switch to: no library, no header, no packages, and no guide from Lexbor's calls to Parsec's.
Closes #20
Closes #21
Closes #22
Change
crates/parsec-capi, buildinglibparsecas a shared and a static library (cdylib+staticlib) over parsec-html's public API. No change tocrates/parsec.include/parsec.his generated by cbindgen fromsrc/lib.rsand checked in; CI fails if it drifts. The rules every call keeps are at the top of the header.parsec_documentandparsec_selector, each with one free function. No struct layout is part of the API.parsec_node, auint32_t), valid for the document's life. Removing a node only detaches it, so nothing dangles, and every number is checked against the document before use.const char *plus length, no NUL, valid until the next change) or owned copies (char *, NUL-terminated, freed withparsec_string_free, which keeps its own size so it needs only the pointer).PARSEC_OK,PARSEC_ERR_NULL,..._NO_SUCH_NODE,..._CYCLEand the rest), each with a message fromparsec_status_message. Every call is NULL-safe and runs insidecatch_unwind, so a panic comes back asPARSEC_ERR_PANICinstead of unwinding into C.nodeTypenumbers, same as Lexbor's), names and values;matches;const parsec_document *and can run concurrently; a compiled selector can be shared; edits need the document to themselves. This rests on the compile-timeSend + Synccheck already incrates/parsec.package.sh OUT [TARGET]lays outinclude/,lib/,lib/pkgconfig/parsec.pcandlib/cmake/parsec/(parsec::parsecandparsec::parsec_static, with the system libraries rustc reports for a static link), plus LICENSE and NOTICE.universal-apple-darwinbuilds both macOS architectures and joins them withlipo. The macOS dylib's install name is@rpath/libparsec.dylib(build.rs), so a prebuilt copy works from wherever it's unpacked..github/workflows/release-capi.yml, separate fromrelease.yml:v*tag it packages Linux x86-64 and ARM64 (on 22.04, for glibc 2.35), macOS universal, and Windows x86-64 and ARM64.tests/capi.cagainst it through CMake, shared and static, then archived.release.ymlcreates once the tag's signature checks out. Only then does it attest the archives and attach them, withparsec-capi-SHA256SUMSand the provenance bundle, so nothing is published for a tag that fails the check.contents: readexcept the publish job, and every action is pinned by commit.ci.yml:capibuilds the package and runs the CMake test on Linux, macOS (universal) and Windows.capi-checkschecks the header against cbindgen 0.29.4, compiles it as C++, and runs the C test through pkg-config under ASan, LSan and UBSan.Docs:
C-APIreference, andMoving-from-Lexborrewritten with a C column and side-by-side C programs) go up after merge.I didn't add a drop-in
lxb_*shim, as the design doc decided.Tests
tests/capi.ccalls every function. It parses, selects, walks, edits and serializes, and it checks each error path: NULL handles, out-of-range node numbers, bad flags and namespaces, refused edits, a bad selector with its message, a budget timeout, and invalid UTF-8.cargo testbuilds and runs it on Unix (tests/c.rs). Locally on macOS, it passes as is and under-fsanitize=address,undefinedagainst the static library.package.shpackage, shared and static.2ae88a1, the onebench/builds) and print identical output.cargo fmt,cargo clippy --workspace --all-targets -D warnings,cargo test --workspace,cargo deny check,scripts/check-headers.sh,actionlintandshellcheckall pass.Not tried locally: the Linux and Windows packages and the ARM runners. CI and the
release-capirun on this pull request cover them.