rc-js-util
Version:
A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.
57 lines • 2.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DebugSharedObjectLifeCycleChecker = void 0;
const _debug_js_1 = require("./_debug.js");
const number_get_hex_string_js_1 = require("../number/impl/number-get-hex-string.js");
const array_insert_at_index_js_1 = require("../array/impl/array-insert-at-index.js");
/**
* @public
* Wrapper of {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry} for shared objects,
* useful for checking if the shared object was properly disposed. Available in debug contexts only.
*/
class DebugSharedObjectLifeCycleChecker {
constructor() {
this.finalizationRegistry = new FinalizationRegistry(([node, maybePtr]) => {
// the `sharedObject` has been destroyed, we get the tuple we associated in `registerFinalizationCheck`
if (!node.getIsDestroyed()) {
// the object has leaked...
if (maybePtr != null) {
_debug_js_1._Debug.error(["A shared object was leaked:", maybePtr.toString()].join("\n"));
}
else {
// RIP
_debug_js_1._Debug.error("A shared object was leaked, but we don't have any information about it...");
}
}
});
}
registerFinalizationCheck(sharedObject, metadata) {
const debugPointer = metadata == null
? null
: new DebugPointer(metadata.address, sharedObject.constructor);
// associate the debug information along with the shared object
this.finalizationRegistry.register(sharedObject, [sharedObject.resourceHandle, debugPointer]);
}
}
exports.DebugSharedObjectLifeCycleChecker = DebugSharedObjectLifeCycleChecker;
class DebugPointer {
constructor(address, objectConstructor, instanceLabel = _debug_js_1._Debug.label) {
this.address = address;
this.objectConstructor = objectConstructor;
this.instanceLabel = instanceLabel;
this.creationCallstack = _debug_js_1._Debug.getStackTrace();
}
toString() {
const lines = [
`WASM address: ${(0, number_get_hex_string_js_1.numberGetHexString)(this.address)}`,
`Js constructor name: ${this.objectConstructor.name}`,
"\nCall site stack trace follows:",
this.creationCallstack,
];
if (this.instanceLabel != null) {
(0, array_insert_at_index_js_1.arrayInsertAtIndex)(lines, `Instance label : ${this.instanceLabel}`, 2);
}
return lines.join("\n");
}
}
//# sourceMappingURL=debug-shared-object-life-cycle-checker.js.map