Skip to content

Js globals reset - #227

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

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

Adds Engine.builder().withJsGlobals(false). It skips the generated globals:

  • the globalThis.<module>.<function> wrappers for builtins, quickjs4j_engine included;
  • the exports of guest functions.

Guest functions are still invoked from Java as usual, and builtins stay reachable through java_invoke.

To make that work, the code appended after the guest library now calls the guest function through a generated table and java_invoke. It no longer reads quickjs4j_engine or globalThis.<module>, so it works the same with the globals on or off.

Smaller fixes:

  • an unknown module or guest function throws IllegalArgumentException instead of NullPointerException
  • builtins sharing a module name with invokables are no longer dropped; a builtin whose name clashes with a guest function or its _set_result setter fails with IllegalArgumentException
  • execution and stdout/stderr flush errors keep the underlying exception as the cause

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.

@andreaTP
andreaTP marked this pull request as ready for review December 3, 2026 18:54
@andreaTP
andreaTP marked this pull request as draft December 4, 2026 17:07
withJsGlobals(false) skips the globalThis.<module>.<function> wrappers
for builtins (quickjs4j_engine included) and the exports of guest
functions. Builtins stay reachable through java_invoke.

The code appended after the guest library now calls the guest function
through a generated table and java_invoke, so it no longer depends on
those globals in either mode.

Smaller fixes:
- an unknown module or guest function throws IllegalArgumentException
  instead of NullPointerException
- invoking a guest function from inside a builtin fails with a clear
  IllegalStateException instead of a QuickJS panic
- builtins sharing a module name with invokables are no longer dropped
The guard only fired when the inner call's code was already cached, and
the engine was unusable afterwards anyway.

Builtins that share a module with invokables now fail fast when a name
clashes with a guest function or its _set_result setter, instead of one
silently replacing the other.

exec and guestOutput keep the underlying exception as the cause.
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