UNPKG

@metamask/ocap-kernel

Version:
64 lines 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseKernelSlot = parseKernelSlot; exports.makeKernelSlot = makeKernelSlot; exports.insistKernelType = insistKernelType; const assert_ts_1 = require("../../utils/assert.cjs"); /** * Parse a kernel slot reference string into a kernel slot object. * * @param slot The string to be parsed, as described above. * @returns kernel slot object corresponding to the parameter. * @throws if the given string is syntactically incorrect. */ function parseKernelSlot(slot) { assert_ts_1.assert.typeof(slot, 'string'); let type; let idSuffix; if (slot.startsWith('ko')) { type = 'object'; idSuffix = slot.slice(2); } else if (slot.startsWith('kp')) { type = 'promise'; idSuffix = slot.slice(2); } else { throw (0, assert_ts_1.Fail) `invalid kernelSlot ${slot}`; } const id = idSuffix; return { type, id }; } /** * Generate a kernel slot reference string given a type and id. * * @param type - The kernel slot type desired, a string. * @param id - The id, a number. * @returns the corresponding kernel slot reference string. * @throws if type is not one of the above known types. */ function makeKernelSlot(type, id) { if (type === 'object') { return `ko${id}`; } if (type === 'promise') { return `kp${id}`; } throw (0, assert_ts_1.Fail) `unknown type ${type}`; } /** * Assert function to ensure that a kernel slot reference string refers to a * slot of a given type. * * @param type - The kernel slot type desired, a string. * @param kernelSlot - The kernel slot reference string being tested * @throws if kernelSlot is not of the given type or is malformed. */ function insistKernelType(type, kernelSlot) { if (kernelSlot === undefined) { throw (0, assert_ts_1.Fail) `kernelSlot is undefined`; } type === parseKernelSlot(kernelSlot).type || (0, assert_ts_1.Fail) `kernelSlot ${kernelSlot} is not of type ${type}`; } //# sourceMappingURL=kernel-slots.cjs.map