@metamask/ocap-kernel
Version:
OCap kernel core components
75 lines • 3.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getReachableMethods = getReachableMethods;
const base_ts_1 = require("./base.cjs");
const object_ts_1 = require("./object.cjs");
const refcount_ts_1 = require("./refcount.cjs");
const parse_ref_ts_1 = require("../utils/parse-ref.cjs");
const reachable_ts_1 = require("../utils/reachable.cjs");
/**
* Get the reachable methods that provide functionality for managing reachable flags.
*
* @param ctx - The store context.
* @returns The reachable methods.
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function getReachableMethods(ctx) {
const { getSlotKey } = (0, base_ts_1.getBaseMethods)(ctx.kv);
const { getObjectRefCount, setObjectRefCount } = (0, object_ts_1.getObjectMethods)(ctx);
const { kernelRefExists } = (0, refcount_ts_1.getRefCountMethods)(ctx);
/**
* Check if a kernel object is reachable.
*
* @param endpointId - The endpoint for which the reachable flag is being checked.
* @param kref - The kref.
* @returns True if the kernel object is reachable, false otherwise.
*/
function getReachableFlag(endpointId, kref) {
const key = getSlotKey(endpointId, kref);
const data = ctx.kv.getRequired(key);
const { isReachable } = (0, reachable_ts_1.parseReachableAndVatSlot)(data);
return isReachable;
}
/**
* Get the reachable and vat slot for a given vat and kernel slot.
*
* @param endpointId - The vat ID.
* @param kref - The kernel slot.
* @returns The reachable and vat slot.
*/
function getReachableAndVatSlot(endpointId, kref) {
const key = getSlotKey(endpointId, kref);
const data = ctx.kv.getRequired(key);
return (0, reachable_ts_1.parseReachableAndVatSlot)(data);
}
/**
* Clear the reachable flag for a given endpoint and kref.
*
* @param endpointId - The endpoint for which the reachable flag is being cleared.
* @param kref - The kref.
*/
function clearReachableFlag(endpointId, kref) {
const key = getSlotKey(endpointId, kref);
const { isReachable, vatSlot } = getReachableAndVatSlot(endpointId, kref);
ctx.kv.set(key, (0, reachable_ts_1.buildReachableAndVatSlot)(false, vatSlot));
const { direction, isPromise } = (0, parse_ref_ts_1.parseRef)(vatSlot);
// decrement 'reachable' part of refcount, but only for object imports
if (isReachable &&
!isPromise &&
direction === 'import' &&
kernelRefExists(kref)) {
const counts = getObjectRefCount(kref);
counts.reachable -= 1;
setObjectRefCount(kref, counts);
if (counts.reachable === 0) {
ctx.maybeFreeKrefs.add(kref);
}
}
}
return {
getReachableFlag,
getReachableAndVatSlot,
clearReachableFlag,
};
}
//# sourceMappingURL=reachable.cjs.map