UNPKG

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.

76 lines 4.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DebugSharedPointerChecks = void 0; const _debug_js_1 = require("../../debug/_debug.js"); const number_get_hex_string_js_1 = require("../../number/impl/number-get-hex-string.js"); const null_pointer_js_1 = require("../emscripten/null-pointer.js"); /** * @internal * @remarks * `ProtectedViews` will be invalidated any time a memory resize might occur with standard `_BUILD.DEBUG` set. * * On garbage collect of the javascript object, the associated WASM pointer is checked to see if it has been disposed of. */ class DebugSharedPointerChecks { /** * Register the shared object but also registers the associated cleanup on release too. */ static registerWithCleanup(wrapper, owner, metadata, protectedViewFactory) { const node = owner.resourceHandle; owner.resourceHandle.onFreeChannel.addListener({ onFree: () => DebugSharedPointerChecks.unregister(wrapper, node, metadata, protectedViewFactory) }); DebugSharedPointerChecks.register(wrapper, owner, metadata, protectedViewFactory); } static register(wrapper, instance, metadata, protectedViewFactory) { _debug_js_1._Debug.assert(instance.pointer != null_pointer_js_1.nullPtr, "tried to track nullptr"); if (protectedViewFactory != null) { const debugUtils = wrapper.debugUtils; debugUtils.protectedViews.setValue(instance.resourceHandle, protectedViewFactory); debugUtils.onAllocate.addListener(protectedViewFactory); } if (metadata.isOwning) { // stringifying the stack would be far too verbose, most debuggers allow expansion of objects... const allocationStack = { stack: _debug_js_1._Debug.getStackTrace() }; const pointer = instance.pointer; const address = (0, number_get_hex_string_js_1.numberGetHexString)(pointer); const message = `JS claimed ${metadata.instanceName} ${address}`; _debug_js_1._Debug.verboseLog(["WASM", "MEMORY", "ALLOCATIONS"], message, allocationStack); _debug_js_1._Debug.assert(pointer !== null_pointer_js_1.nullPtr && pointer != null, "expected pointer to object but got null pointer"); _debug_js_1._Debug.assert(!wrapper.debugUtils.uniquePointers.has(pointer), `expected pointer to be unique (${address})`); wrapper.debugUtils.uniquePointers.add(pointer); } else { const pointer = instance.pointer; const address = (0, number_get_hex_string_js_1.numberGetHexString)(pointer); const message = `JS points to (NON-OWNING) ${metadata.instanceName} ${address}`; _debug_js_1._Debug.verboseLog(["WASM", "MEMORY"], message); } } static unregister(wrapper, handle, metadata, protectedViewFactory) { _debug_js_1._Debug.assert(metadata.address != null_pointer_js_1.nullPtr, "tried to deallocate null"); if (protectedViewFactory) { const debug = wrapper.debugUtils; debug.protectedViews .getValue(handle) .invalidate(); debug.protectedViews.deleteValue(handle); debug.onAllocate.removeListener(protectedViewFactory); } if (metadata.isOwning) { const pointer = metadata.address; const address = (0, number_get_hex_string_js_1.numberGetHexString)(pointer); _debug_js_1._Debug.verboseLog(["WASM", "MEMORY", "DEALLOCATIONS"], `JS released ${metadata.instanceName} ${address}`); _debug_js_1._Debug.assert(wrapper.debugUtils.uniquePointers.has(pointer), `expected to find pointer (${address})`); wrapper.debugUtils.uniquePointers.delete(pointer); } else { const pointer = metadata.address; const address = (0, number_get_hex_string_js_1.numberGetHexString)(pointer); const message = `JS stops pointing to (NON-OWNING) ${metadata.instanceName} ${address}`; _debug_js_1._Debug.verboseLog(["WASM", "MEMORY"], message); } } } exports.DebugSharedPointerChecks = DebugSharedPointerChecks; //# sourceMappingURL=debug-shared-pointer-checks.js.map