Repository navigation
Add JSON builder API to json_capi example; fix macOS-only .dylib assumptions - #1
Conversation
…mptions
- examples/json/json_capi: add #[diplomat::opaque_mut] and builder
constructors/mutators (new_object, new_array, new_string, new_number,
new_bool, set_string, set_number, set_bool, set_value, push) to
JsonValue, extending the existing parse/read-only API.
- Regenerate bindings/headers/shims (plain + mem-trace variants).
- Extend demo.clj and leak-check.clj to exercise and verify the new
builder API (0 bytes leaked over 20,000 iterations; negative control
confirms the leak detector itself still works).
- Fix two cross-platform bugs found while loading generated bindings on
Linux (AL2023) for the first time, both previously hardcoded to macOS's
.dylib extension:
- runtime/src/diplomat/runtime.clj: load! macro now picks .dylib/.so
via OS detection (mirrors the pattern already used in
jolt-lang/http-client's tls.clj.
- bind.clj: shim output filename uses the same OS detection.
- Verified: cargo test --release (11/11, backend, no regressions),
jolt run -m demo / -m leak-check still pass unchanged on macOS.
EOF
)
Addresses code review on the JSON builder API PR: - DiplomatStringView.len was measured with (count s) (a char count) instead of a UTF-8 byte count, silently truncating/corrupting any non-ASCII &str param. Now (alength (.getBytes s)). - A plain (non-Option) bool return was emitted unwrapped; 0 is truthy in Clojure/Jolt, so callers checking the return value couldn't see failure. Now wrapped with (not= 0 ...), in both the shimmed and the direct-defcfn emission paths. Fixes json's new set_string/set_number/ set_bool/set_value/push, and pre-existing semver::is_prerelease, semver::VersionReq::matches, and regex::is_match. - load!'s syntax-quote leaked a reference to the private native-lib-ext var into every call site's namespace; now unquoted so the literal value is spliced in at macro-expansion time. - Documented NaN/Infinity -> null substitution on new_number/set_number. - Fixed a stale repo-name reference in json_capi's lib.rs comment. - Added 4 backend unit tests covering both bugs above and the two new codegen shapes (no-arg ctor returning an owned opaque; &mut self with two &str params). Regenerated generated/ and generated-traced/ for every affected example crate (base64, chrono, json, markdown, regex, sdl3, semver, tantivy). callback needed no change (no string params, no bool returns). Rebuilt and re-ran the demo + leak-check for every crate except sdl3, which fails to link locally on a missing SDL3_ttf system library unrelated to this change; sdl3's regenerated bindings are diff-verified against the fixed generator but not execution-verified.
3595074 to
61e1e08
Compare
|
Pushed a fix for the review feedback in #1 (byte-length, HIGH). #2 (truthy-0, HIGH). #3. Left as bool-only, per your note that fixing #2 makes #4. Documented the NaN/Infinity -> #5 (private-var leak). #6 (test coverage). Added 4 backend unit tests: byte-length, both bool-wrap paths, the no-arg-ctor-returning-owned-opaque shape, and the #7. Fixed the stale repo-name comment. Regenerated |
|
@fricze Would love your view on the above. Thanks |
Closes #2
Summary
Two related additions, both born out of a real downstream use case (building an AWS Lambda custom runtime in Jolt —
lambda-mvp-rst— that needed a way to build JSON, which Jolt itself has no library for):examples/json/json_capi. The example previously only supported parsing/reading JSON (JsonValue::parse, field/array accessors). This adds the mirror-image write side: construct aserde_json::Valuefrom Jolt and mutate it, entirely through Diplomat-generated FFI.bind.cljandruntime/src/diplomat/runtime.clj, found while loading this project's generated bindings on Linux (Amazon Linux 2023) for the first time — both previously assumed.dylibunconditionally.1. JSON builder API
JsonValuegains:new_object,new_array,new_string,new_number,new_boolset_string,set_number,set_bool,set_value,pushSince these are
&mut selfmethods,JsonValuealso gains#[diplomat::opaque_mut](previously only#[diplomat::opaque]), following the same pattern already used by thesdl3example for mutable opaque types.set_value/pushclone the sourceserde_json::Value(mirroring the existingarray_getaccessor's ownership convention) — the caller's original handle remains independently valid and must still be closed separately.demo.cljandleak-check.cljare both extended to exercise the new API. Leak-check results: 0 bytes leaked over 20,000 iterations using the builder API, with the existing negative-control case (deliberately not freeing a handle) still correctly flagged as a leak — confirming the leak detector itself wasn't disabled or bypassed by the new code path.2. Portability fixes (
.dylib→ OS-detected)Both
bind.clj's shim-output filename andruntime/src/diplomat/runtime.clj'sload!macro hardcoded the.dylibextension, so generated bindings could only ever be loaded on macOS. Fixed both via OS detection:This mirrors the pattern already in use in
jolt-lang/http-client'stls.cljfor OpenSSL library selection, so it's a proven approach for this codebase specifically (confirmed to work under Chez Scheme despite not running on a JVM).Verification
cargo test --releaseinbackend/: 11/11 pass, no regressions.jolt run -m demo/jolt run -m leak-checkinexamples/json: still pass unchanged on macOS after the portability fixes (i.e. the fixes are additive, not behavior-changing, on the platform they previously assumed).lambda-mvp-rst(an AWS Lambda custom runtime written in Jolt), whose handler uses the new builder API to construct real JSON responses instead of hand-interpolating strings. That project's Docker/AL2023 build compiles this crate + runsbind.cljinside the container (exercising the Linux.sopath these portability fixes enable) and successfully produces a working Lambda deployment — verified with a realaws lambda deploy/invoke/teardowncycle (cold init ~295–305 ms, warm invoke ~1.7–2.0 ms, consistent with this example's existing performance characteristics).Compatibility
No breaking changes to the existing parse/read API surface — this is purely additive (new constructors/mutators) plus a portability fix that only changes behavior on non-macOS platforms (where bindings previously couldn't load at all).