8000 feat: user functions from JavaScript (minigraf#180) by adityamukho · Pull Request #20 · project-minigraf/minigraf-node · GitHub
[go: up one dir, main page]

Skip to content

feat: user functions from JavaScript (minigraf#180) - #20

Merged
adityamukho merged 3 commits into
v3from
feat/180-udf-ffi
Oct 25, 2026
Merged

adityamukho merged 3 commits into
v3from
feat/180-udf-ffi

Conversation

@adityamukho
@adityamukho adityamukho commented Oct 25, 2026 •
Copy link
Copy Markdown
Contributor

Part of project-minigraf/minigraf#180 (PR 9, plan step 24; spec §6.1, §6.2 Node).

What

  • db.registerPredicate(name, (value) => boolean) and db.registerAggregate(name, { init, step, finish }). These have the same JS shapes as BrowserDb (minigraf#757): values arrive as FactValue ({type, value}, with integers as bigint), and finish returns a plain value or typed form, read like a prepared bind value. A throw, a predicate result that is not a boolean, or a finish result that is not a value is QRY-014, with the error's message (else String(e)).
  • Handle locks (§6.1). Every MiniGrafDb method locks inner only to clone the Minigraf. close() still takes the Option, and a running call keeps its clone. prepare lists the slot under inner after preparing. A prepared query is held as Arc<minigraf::PreparedQuery>, so a UDF can run the same prepared query again.
  • Pin 7c8c5540 → 02243f16. The old pin already had #181 and #316, and #306 needed nothing here.
  • Tests: test/udf.test.mjs (14 tests), plus 2 Rust tests. CI now runs cargo test on Linux; the Rust tests had never run in CI before. The consumer contract gains adult?, product and a throw → QRY-014. The README gains a "User functions" section.

Deviations from the spec (§6.2 Node)

  1. No napi reference per function. The spec says to store the function "as a napi reference wrapped in a type marked Send + Sync". Instead, each JS thread keeps one holder object, referenced once and stored in a thread_local!. Every function and accumulator state is a property of that object, keyed by a global AtomicU64 id, and the registry holds only the id. This is the same Held design as BrowserDb.
    • The reason: Node-API before version 10 (Node 22.14) cannot reference a primitive, so a functional accumulator like 1n or 0 could not be held. CI runs Node 20, and engines is >=18.
    • It also gives the off-thread check for free. A thread without a holder fails with called off the JavaScript thread and never touches an env. The Rust test calls test, init, step and finish from a spawned thread.
    • An env cleanup hook forgets the holder at teardown, so a Held dropped later does nothing.
  2. Same-cursor re-entry. A UDF that calls nextBatch() on the cursor whose batch called it would alias napi-rs's &mut self. Cursor now uses &self with a RefCell, and that call fails with the cursor is already reading a batch, so the query fails with QRY-014. A close() made during the read is deferred until the read returns. The spec does not cover this case.
  3. Each call into JS opens its own handle scope, so a long query does not pile up handles until the method returns.

Codex review

One finding, fixed in 3rd commit: a thrown object whose message getter throws (or a finish result whose type getter throws) left that exception pending, failing later Node-API calls of the query. Now cleared; tests for a throwing getter, a thrown Symbol and a null-prototype object (red before the fix).

Verified

  • node --test passes 40/40 on Node 18.20.8, 20.18.0 and 24.15.0, and red before the implementation (registerPredicate is not a function).
  • cargo test, cargo clippy --all-targets -D warnings and cargo fmt --check are clean.
  • A GC stress run (10,000 databases with UDFs, some closed and some left to GC, with --expose-gc) is clean on all three Node versions.
  • The consumer contract passes against the local build.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Urda7FD9CYYoxdeGGJa11x

adityamukho and others added 3 commits October 25, 2026 14:03
Pins minigraf to 02243f16, the v3 merge with the fallible user-function
API. The tests fail until registerPredicate/registerAggregate exist.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Urda7FD9CYYoxdeGGJa11x
A function or accumulator state lives in a per-thread holder object, keyed
by id, so the core's Send + Sync registry holds no JavaScript value and a
primitive accumulator works before Node-API 10. A call off the JavaScript
thread fails without touching an env. A throw is QRY-014.

Every MiniGrafDb method locks its handle only to clone the database, a
prepared query is shared through an Arc, and a cursor read from inside its
own batch fails the call instead of aliasing, so a user function may call
back into the same database, even close() it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Urda7FD9CYYoxdeGGJa11x
…odex review)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Urda7FD9CYYoxdeGGJa11x
@adityamukho
adityamukho merged commit 823cf3a into v3 Oct 25, 2026
14 checks passed
@adityamukho
adityamukho deleted the feat/180-udf-ffi branch October 25, 2026 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0