Skip to content

Allow adding function with a context access#34

Open
jaenyf wants to merge 1 commit into
danfry1:mainfrom
jaenyf:methods-with-context
Open

Allow adding function with a context access#34
jaenyf wants to merge 1 commit into
danfry1:mainfrom
jaenyf:methods-with-context

Conversation

@jaenyf
Copy link
Copy Markdown

@jaenyf jaenyf commented Apr 19, 2026

This PR allows adding function with a shared context access

  • Context access is disabled by default
  • It has to be specified with related option when calling addFunction
  • It does not work with arrows functions

Summary

This allow sharing a context, when allowed, across functions added with addFunction.
The context is accessed from inside the function with this.context. Because of the this keyword, the function that access to this can't be an "arrow function" otherwise it will be undefined by default.

Test plan

I added 3 tests :

  • A test that ensure context is not shared by default (as it was before this PR)
  • A test that ensure context is shared when specified
  • A test that ensure context sharing is denied when specified

- Context access is disabled by default
- It has to be specified with related option when calling addFunction
- It does not work with arrows functions (because of bind)
@danfry1
Copy link
Copy Markdown
Owner

danfry1 commented May 13, 2026

Hi @jaenyf, thank you for raising #33 and contributing this PR. Really appreciate your patience while we worked through it.

I've opened #40 with an implementation for the feature. Your write-up in #33 was what made the use case concrete, and you're credited as co-author on the implementation commit and called out in the CHANGELOG.

A few specifics that might be worth your review:

  • addContextFunction(name, fn) registers a function that receives context as its first parameter
  • bonsai<TCtx>() propagates context typing through evaluate, evaluateSync, compile, and addContextFunction end-to-end, so the context shape is verified at every call site
  • Context is passed shallow-frozen and lazily cached per evaluation, so isolation is guaranteed and the hot path stays zero-overhead

Would value your eyes on #40 before I merge, especially anything that misses a corner of your original use case. Much better to know now than after release.

Either way, thanks again for kicking this off.

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.

2 participants