Skip to content

Let engines drop host refs with clearHostRefs() - #239

Merged
andreaTP merged 1 commit into
roastedroot:mainfrom
andreaTP:host-refs-reset
Dec 4, 2026
Merged

andreaTP merged 1 commit into
roastedroot:mainfrom
andreaTP:host-refs-reset

Conversation

@andreaTP

@andreaTP andreaTP commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Every object passed to JS as a HostRef (a guest function argument or a builtin's result) was added to a list on the Engine that only ever grew, so it stayed reachable until the engine was closed.

This adds Engine#clearHostRefs() and Runner#clearHostRefs() to drop them. Refs are not cleared automatically after each call, because guest code can keep pointers in globals across calls on the same engine.

  • Pointers are no longer list indexes: they come from a counter that is never reset, so a pointer that JS still holds after a clear can't point at a newer object.
  • A stale pointer resolves to null rather than throwing, because an exception thrown inside a host call leaves the engine unusable for every later call.

Every object passed to JS as a HostRef (guest function argument or
builtin result) was appended to a list that only grew, so an Engine
kept all of them alive until it was closed.

Add Engine#clearHostRefs() and Runner#clearHostRefs() to drop them.
Refs are not cleared after each call, since guest code can keep
pointers in globals across calls. Pointers now come from a counter
that is never reset, so a stale pointer can't alias a newer object;
it resolves to null instead of throwing, because an exception thrown
inside a host call leaves the engine unusable.

@andreaTP andreaTP left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@andreaTP
andreaTP merged commit 358e1d2 into roastedroot:main Dec 4, 2026
4 checks passed
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