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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions packages/quickjs-emscripten-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,14 @@
return str
}

heapValueHandle(ptr: JSValuePointer): JSValue {
return new Lifetime(ptr, this.copyJSValue, this.freeJSValue, this.owner)
heapValueHandle(ptr: JSValuePointer, extraDispose?: () => void): JSValue {
const dispose: typeof this.freeJSValue = extraDispose
? (val) => {
extraDispose()
this.freeJSValue(val)
}
: this.freeJSValue
return new Lifetime(ptr, this.copyJSValue, dispose, this.owner)
}

/** Manage a heap pointer with the lifetime of the context */
Expand Down Expand Up @@ -599,9 +605,15 @@
* ```
*/
newFunction(name: string, fn: VmFunctionImplementation<QuickJSHandle>): QuickJSHandle {
const fnId = ++this.fnNextId
const fnId = this.fnIdFreelist.pop() ?? ++this.fnNextId
this.setFunction(fnId, fn)
return this.memory.heapValueHandle(this.ffi.QTS_NewFunction(this.ctx.value, fnId, name))
const dispose = () => {
this.freeFunction(fnId)
}
return this.memory.heapValueHandle(
this.ffi.QTS_NewFunction(this.ctx.value, fnId, name),
dispose,
)
}

newError(error: { name: string; message: string }): QuickJSHandle
Expand Down Expand Up @@ -1282,7 +1294,7 @@

if (cause && typeof cause === "object" && typeof cause.message === "string") {
const { message, name, stack, ...rest } = cause
const exception = new QuickJSUnwrapError(cause, context)

Check failure on line 1297 in packages/quickjs-emscripten-core/src/context.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

src/quickjs.test.ts > QuickJSContext > DEBUG sync module > .newPromise() > passes an end-to-end test

TypeError: cannot read property 'then' of undefined ❯ QuickJSContext.unwrapResult ../quickjs-emscripten-core/src/context.ts:1297:27 ❯ src/quickjs.test.ts:1044:10 ❯ src/quickjs.test.ts:87:17 ❯ Function.withScopeAsync ../quickjs-emscripten-core/src/lifetime.ts:314:20 ❯ src/quickjs.test.ts:86:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { context: { type: 'QuickJSContext', __vitest__: true, alive: true } } Caused by: TypeError: cannot read property 'then' of undefined

Check failure on line 1297 in packages/quickjs-emscripten-core/src/context.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

src/quickjs.test.ts > QuickJSContext > QuickJS.newContext > .newPromise() > passes an end-to-end test

TypeError: cannot read property 'then' of undefined ❯ QuickJSContext.unwrapResult ../quickjs-emscripten-core/src/context.ts:1297:27 ❯ src/quickjs.test.ts:1044:10 ❯ src/quickjs.test.ts:87:17 ❯ Function.withScopeAsync ../quickjs-emscripten-core/src/lifetime.ts:314:20 ❯ src/quickjs.test.ts:86:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { context: { type: 'QuickJSContext', __vitest__: true, alive: true } } Caused by: Caused by: TypeError: cannot read property 'then' of undefined

if (typeof name === "string") {
exception.name = cause.name
Expand Down Expand Up @@ -1316,6 +1328,7 @@
protected fnNextId = -32768 // min value of signed 16bit int used by Quickjs
/** @private */
protected fnMaps = new Map<number, Map<number, VmFunctionImplementation<QuickJSHandle>>>()
protected fnIdFreelist: number[] = []

/** @private */
protected getFunction(fn_id: number): VmFunctionImplementation<QuickJSHandle> | undefined {
Expand All @@ -1338,6 +1351,17 @@
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.

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.

return
}
if (fnMap.delete(fn_id)) {
this.fnIdFreelist.push(fn_id)
}
}

/**
* @hidden
*/
Expand Down
Loading