@metamask/ocap-kernel
Version:
OCap kernel core components
1 lines • 3.85 kB
Source Map (JSON)
{"version":3,"file":"kernel-slots.cjs","sourceRoot":"","sources":["../../../src/store/utils/kernel-slots.ts"],"names":[],"mappings":";;AAoBA,0CAkBC;AAUD,wCAQC;AAUD,4CASC;AA3ED,sDAAqD;AAarD;;;;;;GAMG;AACH,SAAgB,eAAe,CAAC,IAAY;IAI1C,kBAAM,CAAC,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IAC9B,IAAI,IAAoB,CAAC;IACzB,IAAI,QAAgB,CAAC;IACrB,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QAC1B,IAAI,GAAG,QAAQ,CAAC;QAChB,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC3B,CAAC;SAAM,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QACjC,IAAI,GAAG,SAAS,CAAC;QACjB,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC3B,CAAC;SAAM,CAAC;QACN,MAAM,IAAA,gBAAI,EAAA,sBAAsB,IAAI,EAAE,CAAC;IACzC,CAAC;IACD,MAAM,EAAE,GAAG,QAAQ,CAAC;IACpB,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;AACtB,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,cAAc,CAAC,IAAoB,EAAE,EAAU;IAC7D,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;QACtB,OAAO,KAAK,EAAE,EAAE,CAAC;IACnB,CAAC;IACD,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACvB,OAAO,KAAK,EAAE,EAAE,CAAC;IACnB,CAAC;IACD,MAAM,IAAA,gBAAI,EAAA,gBAAgB,IAAI,EAAE,CAAC;AACnC,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,gBAAgB,CAC9B,IAAoB,EACpB,UAA8B;IAE9B,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;QAC7B,MAAM,IAAA,gBAAI,EAAA,yBAAyB,CAAC;IACtC,CAAC;IACD,IAAI,KAAK,eAAe,CAAC,UAAU,CAAC,CAAC,IAAI;QACvC,IAAA,gBAAI,EAAA,cAAc,UAAU,mBAAmB,IAAI,EAAE,CAAC;AAC1D,CAAC","sourcesContent":["import { assert, Fail } from '../../utils/assert.ts';\n\n// Object/promise references (in the kernel) contain a two-tuple of (type,\n// index). All object references point to entries in the kernel Object\n// Table, which records the vat that owns the actual object. In that vat,\n// the object reference will be expressed as 'o+NN', and the NN was\n// allocated by that vat when they first exported the reference into the\n// kernel. In all other vats, if/when they are given a reference to this\n// object, they will receive 'o-NN', with the NN allocated by the kernel\n// clist for the recipient vat.\n\ntype KernelSlotType = 'object' | 'promise';\n\n/**\n * Parse a kernel slot reference string into a kernel slot object.\n *\n * @param slot The string to be parsed, as described above.\n * @returns kernel slot object corresponding to the parameter.\n * @throws if the given string is syntactically incorrect.\n */\nexport function parseKernelSlot(slot: string): {\n type: KernelSlotType;\n id: string;\n} {\n assert.typeof(slot, 'string');\n let type: KernelSlotType;\n let idSuffix: string;\n if (slot.startsWith('ko')) {\n type = 'object';\n idSuffix = slot.slice(2);\n } else if (slot.startsWith('kp')) {\n type = 'promise';\n idSuffix = slot.slice(2);\n } else {\n throw Fail`invalid kernelSlot ${slot}`;\n }\n const id = idSuffix;\n return { type, id };\n}\n\n/**\n * Generate a kernel slot reference string given a type and id.\n *\n * @param type - The kernel slot type desired, a string.\n * @param id - The id, a number.\n * @returns the corresponding kernel slot reference string.\n * @throws if type is not one of the above known types.\n */\nexport function makeKernelSlot(type: KernelSlotType, id: string): string {\n if (type === 'object') {\n return `ko${id}`;\n }\n if (type === 'promise') {\n return `kp${id}`;\n }\n throw Fail`unknown type ${type}`;\n}\n\n/**\n * Assert function to ensure that a kernel slot reference string refers to a\n * slot of a given type.\n *\n * @param type - The kernel slot type desired, a string.\n * @param kernelSlot - The kernel slot reference string being tested\n * @throws if kernelSlot is not of the given type or is malformed.\n */\nexport function insistKernelType(\n type: KernelSlotType,\n kernelSlot: string | undefined,\n): asserts kernelSlot is KernelSlotType {\n if (kernelSlot === undefined) {\n throw Fail`kernelSlot is undefined`;\n }\n type === parseKernelSlot(kernelSlot).type ||\n Fail`kernelSlot ${kernelSlot} is not of type ${type}`;\n}\n"]}