Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: generalize finalizer second pass callback (nodejs/node#44141)
- Loading branch information
1 parent
6231ddf
commit 0b985bf
Showing
17 changed files
with
322 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,25 @@ | ||
import type { Env } from './env' | ||
|
||
/** @internal */ | ||
export enum EnvReferenceMode { | ||
kNoEnvReference, | ||
kKeepEnvReference | ||
} | ||
|
||
/** @internal */ | ||
export class Finalizer { | ||
protected _hasEnvReference: boolean | ||
protected _finalizeRan: boolean | ||
|
||
public constructor ( | ||
protected envObject: Env, | ||
protected _finalizeCallback: napi_finalize = 0, | ||
protected _finalizeData: void_p = 0, | ||
protected _finalizeHint: void_p = 0, | ||
refmode: EnvReferenceMode = EnvReferenceMode.kNoEnvReference | ||
) { | ||
this._finalizeRan = false | ||
this._hasEnvReference = refmode === EnvReferenceMode.kKeepEnvReference | ||
if (this._hasEnvReference) { | ||
envObject.ref() | ||
} | ||
protected _finalizeHint: void_p = 0 | ||
) {} | ||
|
||
public callback (): napi_finalize { return this._finalizeCallback } | ||
public data (): void_p { return this._finalizeData } | ||
public hint (): void_p { return this._finalizeHint } | ||
|
||
public resetFinalizer (): void { | ||
this._finalizeCallback = 0 | ||
this._finalizeData = 0 | ||
this._finalizeHint = 0 | ||
} | ||
|
||
public dispose (): void { | ||
if (this._hasEnvReference) { | ||
this.envObject.unref() | ||
} | ||
this.envObject = undefined! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { supportFinalizer } from './util' | ||
|
||
/** @public */ | ||
export class Global<T extends object> { | ||
private _ref: T | WeakRef<T> | null | ||
private _param: any | ||
private _callback: ((param: any) => void) | undefined | ||
|
||
private static readonly _registry = supportFinalizer | ||
? new FinalizationRegistry((value: Global<any>) => { | ||
value._ref = null | ||
if (typeof value._callback === 'function') { | ||
value._callback(value._param) | ||
} | ||
}) | ||
: undefined! | ||
|
||
constructor (value: T) { | ||
this._ref = value | ||
} | ||
|
||
setWeak<P> (param: any, callback: (param: P) => void): void { | ||
if (this._ref === null) return | ||
if (!supportFinalizer) return | ||
if (this._ref instanceof WeakRef) return | ||
this._param = param | ||
this._callback = callback | ||
Global._registry.register(this._ref, this, this) | ||
this._ref = new WeakRef<T>(this._ref) | ||
} | ||
|
||
clearWeak (): void { | ||
if (this._ref === null) return | ||
if (!supportFinalizer) return | ||
if (this._ref instanceof WeakRef) { | ||
try { | ||
Global._registry.unregister(this) | ||
} catch (_) {} | ||
this._param = undefined | ||
this._callback = undefined | ||
this._ref = this._ref.deref() as T | ||
} | ||
} | ||
|
||
reset (other?: T | WeakRef<T>): void { | ||
if (supportFinalizer) { | ||
try { | ||
Global._registry.unregister(this) | ||
} catch (_) {} | ||
} | ||
this._param = undefined | ||
this._callback = undefined | ||
if (other) { | ||
this._ref = other | ||
} else { | ||
this._ref = null | ||
} | ||
} | ||
|
||
isEmpty (): boolean { | ||
return this._ref === null | ||
} | ||
|
||
deref (): T | undefined { | ||
if (!supportFinalizer) { | ||
return (this._ref as T | null) ?? undefined | ||
} | ||
|
||
if (this._ref === null) return undefined | ||
|
||
if (this._ref instanceof WeakRef) { | ||
return this._ref.deref() | ||
} | ||
|
||
return this._ref | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.