Skip to content

Fail guest calls that return no result - #243

Open
andreaTP wants to merge 3 commits into
roastedroot:mainfrom
andreaTP:guest-result-fix
Open

andreaTP wants to merge 3 commits into
roastedroot:mainfrom
andreaTP:guest-result-fix

Conversation

@andreaTP

@andreaTP andreaTP commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

Based on #227: only the last commit (Fail guest calls that return no result) belongs to this PR. I'll rebase once #227 is merged.

The result of a guest function was never cleared between calls. When <fn>_set_result wasn't reached, the caller silently got the previous call's result, or null on the first call:

  • an async guest function that rejects, for example async f(x) { if (x < 0) throw ...; return x; } called with 5 and then -1, returns 5 the second time;
  • a guest function that returns a promise that never settles.

The engine now clears the result before each call and throws a GuestException naming the function when no result arrives. A rejection's message and stack are written to stderr, so they show up in the exception message. The engine stays usable afterwards.

Behavior change: precompiled code that doesn't deliver a result, for example code not built with compilePortableGuestFunction, now throws instead of returning null or a stale value.

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.
The result of a guest function was never cleared between calls, so an
async guest function that rejected, or returned a promise that never
settled, silently returned the previous call's result (or null).

Clear the result before each call and throw a GuestException when none
arrives. A rejection's message and stack are written to stderr, so they
show up in the exception.
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