Skip to content

Clean up functions when disposed #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

justjake
Copy link
Owner

When disposed, delete the function from the internal map and add the function id to a "freelist" that will be re-used for the next function created. Only increment ID if no freelist id is available.

Fixes #223

@justjake justjake requested a review from Copilot June 22, 2025 22:21
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR cleans up function entries upon disposal by removing them from the internal map and reusing freed function IDs via a freelist instead of always incrementing the counter.

  • Extended heapValueHandle to accept an extra disposal callback.
  • Updated newFunction to pull IDs from fnIdFreelist and schedule freeFunction on handle disposal.
  • Introduced fnIdFreelist storage and freeFunction method for recycling IDs.
Comments suppressed due to low confidence (2)

packages/quickjs-emscripten-core/src/context.ts:1331

  • Add a JSDoc comment for fnIdFreelist to explain its role in recycling function IDs upon disposal.
  protected fnIdFreelist: number[] = []

packages/quickjs-emscripten-core/src/context.ts:608

  • Add unit tests to verify that IDs from fnIdFreelist are correctly reused when disposing and creating new functions.
    const fnId = this.fnIdFreelist.pop() ?? ++this.fnNextId

@@ -1338,6 +1351,17 @@ export class QuickJSContext
return fnMap.set(fn_id, handle)
}

protected freeFunction(fn_id: number) {
const map_id = fn_id >> 8
Copy link
Preview

Copilot AI Jun 22, 2025

Choose a reason for hiding this comment

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

Extract the magic number 8 used in the bit shift into a named constant (e.g., ID_MAP_SHIFT_BITS) to clarify intent.

Suggested change
const map_id = fn_id >> 8
const map_id = fn_id >> QuickJSContext.ID_MAP_SHIFT_BITS

Copilot uses AI. Check for mistakes.

protected freeFunction(fn_id: number) {
const map_id = fn_id >> 8
const fnMap = this.fnMaps.get(map_id)
if (!fnMap) {
Copy link
Preview

Copilot AI Jun 22, 2025

Choose a reason for hiding this comment

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

After deleting the last function in a map, consider removing the empty fnMap entry from fnMaps to avoid retaining unused maps.

Copilot uses AI. Check for mistakes.

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.

Integer overflow in function IDs
1 participant