Skip to content

Js globals reset - #227

Open
andreaTP wants to merge 3 commits into
roastedroot:mainfrom
andreaTP:js-globals-reset
Open

andreaTP wants to merge 3 commits into
roastedroot:mainfrom
andreaTP:js-globals-reset

Conversation

@andreaTP

@andreaTP andreaTP commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Changes to the JavaScript the engine adds around guest code, and to how guest results reach Java:

  • Guest functions are called through a local table, with java_invoke, JSON.stringify and JSON.parse captured before the call. A guest that replaces java_invoke, quickjs4j_engine, JSON or its own module global still returns its result.
  • Engine.builder().withJsGlobals(false) skips the generated globals: the globalThis.<module>.<function> wrappers for builtins, quickjs4j_engine included, and the exports of guest functions. Guest functions are still invoked from Java as usual, and builtins stay reachable through java_invoke.
  • The Engine tracks the call in progress and its result, instead of storing the result on the shared GuestFunction, and fails 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
  • Smaller fixes:
    • an unknown module or function throws IllegalArgumentException instead of NullPointerException
    • a nested invocation from inside a builtin fails fast with IllegalStateException, instead of a RefCell already borrowed panic in QuickJS
    • builtins sharing a module name with invokables are no longer dropped

Embedders that build their own JavaScript API over java_invoke, such as camel-quickjs, can use withJsGlobals(false) instead of stubbing and restoring these globals around every script.

Breaking: GuestFunction.getResult() and GuestFunction.setResultHostFunction() are removed. Nothing in this repository used them.

jsPreludeLineCount() grows by one, because of the new quickjs4j_engine.set_error builtin.

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.
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
andreaTP marked this pull request as ready for review December 3, 2026 18:54
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