-
Notifications
You must be signed in to change notification settings - Fork 116
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
|
@@ -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 | ||
|
@@ -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
|
||
|
||
if (typeof name === "string") { | ||
exception.name = cause.name | ||
|
@@ -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 { | ||
|
@@ -1338,6 +1351,17 @@ | |
return fnMap.set(fn_id, handle) | ||
} | ||
|
||
protected freeFunction(fn_id: number) { | ||
const map_id = fn_id >> 8 | ||
const fnMap = this.fnMaps.get(map_id) | ||
if (!fnMap) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After deleting the last function in a map, consider removing the empty Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
return | ||
} | ||
if (fnMap.delete(fn_id)) { | ||
this.fnIdFreelist.push(fn_id) | ||
} | ||
} | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
|
There was a problem hiding this comment.
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.Copilot uses AI. Check for mistakes.