UNPKG

@metamask/ocap-kernel

Version:
1 lines 7.83 kB
{"version":3,"file":"object.cjs","sourceRoot":"","sources":["../../../src/store/methods/object.ts"],"names":[],"mappings":";;AAgBA,4CAqIC;AArJD,yCAAoC;AAEpC,wCAA2C;AAC3C,8CAAkD;AAGlD,+DAA0D;AAE1D;;;;;;GAMG;AACH,4EAA4E;AAC5E,SAAgB,gBAAgB,CAAC,GAAiB;IAChD,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,GACvE,IAAA,wBAAc,EAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAEzB;;;;;;;;OAQG;IACH,SAAS,gBAAgB,CAAC,KAAiB;QACzC,MAAM,IAAI,GAAG,eAAe,EAAE,CAAC;QAC/B,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;QACrC,iBAAiB,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,CAAC,CAAC;QAC3D,kCAAkC;QAClC,sCAAsC;QACtC,OAAO,CAAC,KAAK,CAAC,kBAAkB,EAAE,IAAI,EAAE,KAAK,EAAE,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC;QACxE,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;;OAMG;IACH,SAAS,QAAQ,CAAC,IAAU,EAAE,cAAc,GAAG,IAAI;QACjD,MAAM,KAAK,GAAG,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;QAC5C,IAAI,cAAc,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YAC1C,MAAM,KAAK,CAAC,yBAAyB,IAAI,EAAE,CAAC,CAAC;QAC/C,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;;;OAKG;IACH,SAAS,aAAa,CAAC,KAAY;QACjC,OAAO,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;;OAOG;IACH,SAAS,iBAAiB,CACxB,IAAU,EACV,MAAmD;QAEnD,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,GAAG,MAAM,CAAC;QAC3C,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QACnC,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;QACtC,CAAC,SAAS,IAAI,CAAC,IAAI,YAAY,IAAI,CAAC,CAAC;YACnC,IAAA,aAAI,EAAA,GAAG,IAAI,cAAc,SAAS,IAAI,YAAY,EAAE,CAAC;QACvD,SAAS,IAAI,YAAY;YACvB,IAAA,aAAI,EAAA,oBAAoB,IAAI,IAAI,SAAS,IAAI,YAAY,EAAE,CAAC;QAC9D,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,GAAG,SAAS,IAAI,YAAY,EAAE,CAAC,CAAC;IAChE,CAAC;IAED,OAAO;QACL,gBAAgB;QAChB,QAAQ;QACR,aAAa;QACb,YAAY;QACZ,kBAAkB;QAClB,eAAe;QACf,iBAAiB;QACjB,iBAAiB;KAClB,CAAC;AACJ,CAAC","sourcesContent":["import { Fail } from '@endo/errors';\n\nimport { getBaseMethods } from './base.ts';\nimport { ROOT_OBJECT_VREF } from '../../types.ts';\nimport type { EndpointId, KRef, VatId } from '../../types.ts';\nimport type { StoreContext } from '../types.ts';\nimport { makeKernelSlot } from '../utils/kernel-slots.ts';\n\n/**\n * Create an object store object that provides functionality for managing kernel objects.\n *\n * @param ctx - The store context.\n * @returns An object store object that maps various persistent kernel data\n * structures onto `kv`.\n */\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type\nexport function getObjectMethods(ctx: StoreContext) {\n const { getSlotKey, incCounter, refCountKey, getOwnerKey, getRevokedKey } =\n getBaseMethods(ctx.kv);\n\n /**\n * Create a new kernel object. The new object will be born with reference and\n * recognizability counts of 1, on the assumption that the new object\n * corresponds to an object that has just been imported from somewhere. The\n * object is initially unrevoked.\n *\n * @param owner - The endpoint that is the owner of the new object.\n * @returns The new object's KRef.\n */\n function initKernelObject(owner: EndpointId): KRef {\n const koId = getNextObjectId();\n ctx.kv.set(getOwnerKey(koId), owner);\n setObjectRefCount(koId, { reachable: 1, recognizable: 1 });\n // TODO(#562): Use logger instead.\n // eslint-disable-next-line no-console\n console.debug('initKernelObject', koId, owner, getObjectRefCount(koId));\n return koId;\n }\n\n /**\n * Get a kernel object's owner.\n *\n * @param koId - The KRef of the kernel object of interest.\n * @param throwIfUnknown - Whether to throw an error if the owner is unknown.\n * @returns The identity of the vat or remote that owns the object.\n */\n function getOwner(koId: KRef, throwIfUnknown = true): EndpointId | undefined {\n const owner = ctx.kv.get(getOwnerKey(koId));\n if (throwIfUnknown && owner === undefined) {\n throw Error(`unknown kernel object ${koId}`);\n }\n return owner;\n }\n\n /**\n * Get the root object for a vat.\n *\n * @param vatId - The ID of the vat of interest.\n * @returns The root object for the vat.\n */\n function getRootObject(vatId: VatId): KRef | undefined {\n return ctx.kv.get(getSlotKey(vatId, ROOT_OBJECT_VREF));\n }\n\n /**\n * True if `kref` is the root object for `vatId`.\n *\n * Every vat exports its root as slot `o+0`, which gives a c‑list entry\n *\n * @param kref - The KRef of the object of interest.\n * @param vatId - The ID of the vat of interest.\n * @returns True if `kref` is the root object for `vatId`.\n */\n function isRootObject(kref: KRef, vatId: VatId): boolean {\n return getRootObject(vatId) === kref;\n }\n\n /**\n * Expunge a kernel object from the kernel's persistent state.\n *\n * @param koId - The KRef of the kernel object to delete.\n */\n function deleteKernelObject(koId: KRef): void {\n ctx.kv.delete(getOwnerKey(koId));\n ctx.kv.delete(refCountKey(koId));\n ctx.kv.delete(getRevokedKey(koId));\n }\n\n /**\n * Obtain a KRef for the next unallocated kernel object.\n *\n * @returns The next koId use.\n */\n function getNextObjectId(): KRef {\n return makeKernelSlot('object', incCounter(ctx.nextObjectId));\n }\n\n /**\n * Get the reference counts for a kernel object\n *\n * @param kref - The KRef of the object of interest.\n * @returns The reference counts for the object.\n */\n function getObjectRefCount(kref: KRef): {\n reachable: number;\n recognizable: number;\n } {\n const data = ctx.kv.get(refCountKey(kref));\n if (!data) {\n return { reachable: 0, recognizable: 0 };\n }\n const [reachable = 0, recognizable = 0] = data.split(',').map(Number);\n reachable <= recognizable ||\n Fail`refMismatch(get) ${kref} ${reachable},${recognizable}`;\n return { reachable, recognizable };\n }\n\n /**\n * Set the reference counts for a kernel object\n *\n * @param kref - The KRef of the object of interest.\n * @param counts - The reference counts to set.\n * @param counts.reachable - The reachable reference count.\n * @param counts.recognizable - The recognizable reference count.\n */\n function setObjectRefCount(\n kref: KRef,\n counts: { reachable: number; recognizable: number },\n ): void {\n const { reachable, recognizable } = counts;\n assert.typeof(reachable, 'number');\n assert.typeof(recognizable, 'number');\n (reachable >= 0 && recognizable >= 0) ||\n Fail`${kref} underflow ${reachable},${recognizable}`;\n reachable <= recognizable ||\n Fail`refMismatch(set) ${kref} ${reachable},${recognizable}`;\n ctx.kv.set(refCountKey(kref), `${reachable},${recognizable}`);\n }\n\n return {\n initKernelObject,\n getOwner,\n getRootObject,\n isRootObject,\n deleteKernelObject,\n getNextObjectId,\n getObjectRefCount,\n setObjectRefCount,\n };\n}\n"]}