Repository navigation
Js globals reset - #227
Open
andreaTP wants to merge 3 commits into
Open
Js globals reset#227andreaTP wants to merge 3 commits into
andreaTP wants to merge 3 commits into
Conversation
The code appended to every guest library looked up the function and delivered its result through globalThis.quickjs4j_engine, globalThis.<module>, java_invoke and JSON, all read after the guest function returned. A guest that replaced any of them lost its result (null). The call now runs in a block that captures java_invoke and JSON.stringify before calling the guest and dispatches through a local table of the invokables. Generated output with the prelude and suffix is otherwise unchanged.
withJsGlobals(false) drops the prelude (globalThis.<module>.<fn> wrappers for builtins, including quickjs4j_engine and the *_set_result modules) and the suffix (globalThis exports of guest functions). Guest functions keep working through the self-contained call code, and builtins stay reachable through java_invoke. Embedders such as camel-quickjs build their own JavaScript API over java_invoke and today stub and restore these globals around every script.
andreaTP
force-pushed
the
js-globals-reset
branch
from
December 3, 2026 12:25
96fb6ce to
a5613f3
Compare
The Engine now tracks the call in progress and its result, instead of storing the result on the shared GuestFunction, and fails the call with a GuestException when no result arrives: - an async guest function that rejects reports its error, instead of returning null - a promise that never settles fails, instead of returning the previous call's result - a guest can't deliver the result of another function Host calls record these failures rather than throwing, since an exception unwinding through QuickJS leaves the engine unusable. Nested invocations fail fast with an IllegalStateException, unknown modules or functions with an IllegalArgumentException instead of a NullPointerException, and builtins sharing a module name with invokables are no longer dropped. GuestFunction no longer exposes getResult() and setResultHostFunction(). The generated epilogue is now a text block, emits names as JSON strings, also captures JSON.parse, and drops the no-op rejection handler; prelude, suffix and epilogue are final fields.
andreaTP
marked this pull request as ready for review
December 3, 2026 18:54
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.
Changes to the JavaScript the engine adds around guest code, and to how guest results reach Java:
java_invoke,JSON.stringifyandJSON.parsecaptured before the call. A guest that replacesjava_invoke,quickjs4j_engine,JSONor its own module global still returns its result.Engine.builder().withJsGlobals(false)skips the generated globals: theglobalThis.<module>.<function>wrappers for builtins,quickjs4j_engineincluded, and the exports of guest functions. Guest functions are still invoked from Java as usual, and builtins stay reachable throughjava_invoke.Enginetracks the call in progress and its result, instead of storing the result on the sharedGuestFunction, and fails with aGuestExceptionwhen no result arrives:nullIllegalArgumentExceptioninstead ofNullPointerExceptionIllegalStateException, instead of aRefCell already borrowedpanic in QuickJSEmbedders that build their own JavaScript API over
java_invoke, such as camel-quickjs, can usewithJsGlobals(false)instead of stubbing and restoring these globals around every script.Breaking:
GuestFunction.getResult()andGuestFunction.setResultHostFunction()are removed. Nothing in this repository used them.jsPreludeLineCount()grows by one, because of the newquickjs4j_engine.set_errorbuiltin.