@metamask/ocap-kernel
Version:
OCap kernel core components
1 lines • 9.83 kB
Source Map (JSON)
{"version":3,"file":"clist.mjs","sourceRoot":"","sources":["../../../src/store/methods/clist.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,mBAAkB;AAC3C,OAAO,EAAE,mBAAmB,EAAE,wBAAuB;AACrD,OAAO,EAAE,kBAAkB,EAAE,uBAAsB;AAGnD,OAAO,EAAE,QAAQ,EAAE,+BAA8B;AACjD,OAAO,EAAE,YAAY,EAAE,iCAAgC;AACvD,OAAO,EACL,wBAAwB,EACxB,wBAAwB,EACzB,+BAA8B;AAE/B;;;;;GAKG;AACH,4EAA4E;AAC5E,MAAM,UAAU,eAAe,CAAC,GAAiB;IAC/C,MAAM,EAAE,UAAU,EAAE,GAAG,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC9C,MAAM,EAAE,kBAAkB,EAAE,GAAG,mBAAmB,CAAC,GAAG,CAAC,CAAC;IACxD,MAAM,EAAE,iBAAiB,EAAE,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAC;IAEtD;;;;;;;;OAQG;IACH,SAAS,aAAa,CAAC,UAAsB,EAAE,IAAU,EAAE,IAAU;QACnE,GAAG,CAAC,EAAE,CAAC,GAAG,CACR,UAAU,CAAC,UAAU,EAAE,IAAI,CAAC,EAC5B,wBAAwB,CAAC,IAAI,EAAE,IAAI,CAAC,CACrC,CAAC;QACF,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC,UAAU,EAAE,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC;IACjD,CAAC;IAED;;;;;;OAMG;IACH,SAAS,aAAa,CAAC,UAAsB,EAAE,IAAY;QACzD,OAAO,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC,KAAK,SAAS,CAAC;IAChE,CAAC;IAED;;;;;;OAMG;IACH,SAAS,gBAAgB,CACvB,UAAsB,EACt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AAU;QACpD,MAAM,IAAI,GAAG,UAAU,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;QAC1C,IAAI,IAAI,EAAE,CAAC;YACT,gBAAgB,CAAC,UAAU,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAC3C,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACH,SAAS,UAAU,CAAC,UAAsB,EAAE,IAAU;QACpD,MAAM,IAAI,GAAG,UAAU,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;QAC1C,IAAI,IAAI,EAAE,CAAC;YACT,gBAAgB,CAAC,UAAU,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAC3C,CAAC;IACH,CAAC;IAED,OAAO;QACL,iBAAiB;QACjB,aAAa;QACb,aAAa;QACb,gBAAgB;QAChB,kBAAkB;QAClB,mBAAmB;QACnB,UAAU;QACV,UAAU;QACV,UAAU;QACV,UAAU;QACV,oBAAoB;KACrB,CAAC;AACJ,CAAC","sourcesContent":["import { getBaseMethods } from './base.ts';\nimport { getReachableMethods } from './reachable.ts';\nimport { getRefCountMethods } from './refcount.ts';\nimport type { EndpointId, KRef, ERef } from '../../types.ts';\nimport type { StoreContext } from '../types.ts';\nimport { parseRef } from '../utils/parse-ref.ts';\nimport { isPromiseRef } from '../utils/promise-ref.ts';\nimport {\n buildReachableAndVatSlot,\n parseReachableAndVatSlot,\n} from '../utils/reachable.ts';\n\n/**\n * Get the c-list methods that provide functionality for managing c-lists.\n *\n * @param ctx - The store context.\n * @returns The c-list store.\n */\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type\nexport function getCListMethods(ctx: StoreContext) {\n const { getSlotKey } = getBaseMethods(ctx.kv);\n const { clearReachableFlag } = getReachableMethods(ctx);\n const { decrementRefCount } = getRefCountMethods(ctx);\n\n /**\n * Add an entry to an endpoint's c-list, creating a new bidirectional mapping\n * between an ERef belonging to the endpoint and a KRef belonging to the\n * kernel.\n *\n * @param endpointId - The endpoint whose c-list is to be added to.\n * @param kref - The KRef.\n * @param eref - The ERef.\n */\n function addCListEntry(endpointId: EndpointId, kref: KRef, eref: ERef): void {\n ctx.kv.set(\n getSlotKey(endpointId, kref),\n buildReachableAndVatSlot(true, eref),\n );\n ctx.kv.set(getSlotKey(endpointId, eref), kref);\n }\n\n /**\n * Test if there's a c-list entry for some slot.\n *\n * @param endpointId - The endpoint of interest\n * @param slot - The slot of interest\n * @returns true iff this vat has a c-list entry mapping for `slot`.\n */\n function hasCListEntry(endpointId: EndpointId, slot: string): boolean {\n return ctx.kv.get(getSlotKey(endpointId, slot)) !== undefined;\n }\n\n /**\n * Remove an entry from an endpoint's c-list.\n *\n * @param endpointId - The endpoint whose c-list entry is to be removed.\n * @param kref - The KRef.\n * @param eref - The ERef.\n */\n function deleteCListEntry(\n endpointId: EndpointId,\n kref: KRef,\n eref: ERef,\n ): void {\n const kernelKey = getSlotKey(endpointId, kref);\n const vatKey = getSlotKey(endpointId, eref);\n assert(ctx.kv.get(kernelKey));\n clearReachableFlag(endpointId, kref);\n const { direction } = parseRef(eref);\n decrementRefCount(kref, 'delete|kref', {\n isExport: direction === 'export',\n onlyRecognizable: true,\n });\n ctx.kv.delete(kernelKey);\n ctx.kv.delete(vatKey);\n }\n\n /**\n * Generate a new eref for a kernel object or promise being imported into an\n * endpoint.\n *\n * @param endpointId - The endpoint the kref is being imported into.\n * @param kref - The kref for the kernel object or promise in question.\n *\n * @returns A new eref in the scope of the given endpoint for the given kernel entity.\n */\n function allocateErefForKref(endpointId: EndpointId, kref: KRef): ERef {\n let id;\n const refTag = endpointId.startsWith('v') ? '' : endpointId[0];\n let refType;\n if (isPromiseRef(kref)) {\n id = ctx.kv.get(`e.nextPromiseId.${endpointId}`);\n ctx.kv.set(`e.nextPromiseId.${endpointId}`, `${Number(id) + 1}`);\n refType = 'p';\n } else {\n id = ctx.kv.get(`e.nextObjectId.${endpointId}`);\n ctx.kv.set(`e.nextObjectId.${endpointId}`, `${Number(id) + 1}`);\n refType = 'o';\n }\n const eref = `${refTag}${refType}-${id}`;\n addCListEntry(endpointId, kref, eref);\n return eref;\n }\n\n /**\n * Look up the ERef that and endpoint's c-list maps a KRef to.\n *\n * @param endpointId - The endpoint in question.\n * @param eref - The ERef to look up.\n * @returns The KRef corresponding to `eref` in the given endpoints c-list, or undefined\n * if there is no such mapping.\n */\n function erefToKref(endpointId: EndpointId, eref: ERef): KRef | undefined {\n return ctx.kv.get(getSlotKey(endpointId, eref));\n }\n\n /**\n * Look up the KRef that and endpoint's c-list maps an ERef to.\n *\n * @param endpointId - The endpoint in question.\n * @param kref - The KRef to look up.\n * @returns The given endpoint's ERef corresponding to `kref`, or undefined if\n * there is no such mapping.\n */\n function krefToEref(endpointId: EndpointId, kref: KRef): ERef | undefined {\n const key = getSlotKey(endpointId, kref);\n const data = ctx.kv.get(key);\n if (!data) {\n return undefined;\n }\n const { vatSlot } = parseReachableAndVatSlot(data);\n return vatSlot;\n }\n\n /**\n * Look up the ERef that and endpoint's c-list maps a KRef to.\n *\n * @param endpointId - The endpoint in question.\n * @param krefs - The KRefs to look up.\n * @returns The given endpoint's ERefs corresponding to `krefs`\n */\n function krefsToExistingErefs(endpointId: EndpointId, krefs: KRef[]): ERef[] {\n return krefs\n .map((kref) => krefToEref(endpointId, kref))\n .filter((eref): eref is ERef => Boolean(eref));\n }\n\n /**\n * Remove an entry from an endpoint's c-list given an eref.\n *\n * @param endpointId - The endpoint whose c-list entry is to be removed.\n * @param eref - The ERef.\n */\n function forgetEref(endpointId: EndpointId, eref: ERef): void {\n const kref = erefToKref(endpointId, eref);\n if (kref) {\n deleteCListEntry(endpointId, kref, eref);\n }\n }\n\n /**\n * Remove an entry from an endpoint's c-list given a kref.\n *\n * @param endpointId - The endpoint whose c-list entry is to be removed.\n * @param kref - The Kref.\n */\n function forgetKref(endpointId: EndpointId, kref: KRef): void {\n const eref = krefToEref(endpointId, kref);\n if (eref) {\n deleteCListEntry(endpointId, kref, eref);\n }\n }\n\n return {\n // C-List entries\n addCListEntry,\n hasCListEntry,\n deleteCListEntry,\n // Eref allocation\n allocateErefForKref,\n erefToKref,\n krefToEref,\n forgetEref,\n forgetKref,\n krefsToExistingErefs,\n };\n}\n"]}