Repository navigation
feat: user functions from JavaScript (minigraf#180) - #20
Merged
Merged
Conversation
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
10 tasks done
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.
Part of project-minigraf/minigraf#180 (PR 9, plan step 24; spec §6.1, §6.2 Node).
What
db.registerPredicate(name, (value) => boolean)anddb.registerAggregate(name, { init, step, finish }). These have the same JS shapes asBrowserDb(minigraf#757): values arrive asFactValue({type, value}, with integers asbigint), andfinishreturns a plain value or typed form, read like a prepared bind value. A throw, a predicate result that is not a boolean, or afinishresult that is not a value isQRY-014, with the error'smessage(elseString(e)).MiniGrafDbmethod locksinneronly to clone theMinigraf.close()still takes theOption, and a running call keeps its clone.preparelists the slot underinnerafter preparing. A prepared query is held asArc<minigraf::PreparedQuery>, so a UDF can run the same prepared query again.test/udf.test.mjs(14 tests), plus 2 Rust tests. CI now runscargo teston Linux; the Rust tests had never run in CI before. The consumer contract gainsadult?,productand a throw →QRY-014. The README gains a "User functions" section.Deviations from the spec (§6.2 Node)
Send + Sync". Instead, each JS thread keeps one holder object, referenced once and stored in athread_local!. Every function and accumulator state is a property of that object, keyed by a globalAtomicU64id, and the registry holds only the id. This is the sameHelddesign as BrowserDb.1nor0could not be held. CI runs Node 20, andenginesis>=18.called off the JavaScript threadand never touches an env. The Rust test callstest,init,stepandfinishfrom a spawned thread.Helddropped later does nothing.nextBatch()on the cursor whose batch called it would alias napi-rs's&mut self.Cursornow uses&selfwith aRefCell, and that call fails withthe cursor is already reading a batch, so the query fails with QRY-014. Aclose()made during the read is deferred until the read returns. The spec does not cover this case.Codex review
One finding, fixed in 3rd commit: a thrown object whose
messagegetter throws (or afinishresult whosetypegetter throws) left that exception pending, failing later Node-API calls of the query. Now cleared; tests for a throwing getter, a thrownSymboland a null-prototype object (red before the fix).Verified
node --testpasses 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 warningsandcargo fmt --checkare clean.--expose-gc) is clean on all three Node versions.🤖 Generated with Claude Code
https://claude.ai/code/session_01Urda7FD9CYYoxdeGGJa11x