Skip to content

Commit 5a043cd

Browse files
committed
refactor(SwingSet): Simplify conditions in mapVatSlotToKernelSlot
1 parent d8f6bce commit 5a043cd

File tree

1 file changed

+35
-42
lines changed

1 file changed

+35
-42
lines changed

packages/SwingSet/src/kernel/state/vatKeeper.js

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -369,54 +369,47 @@ export function makeVatKeeper(
369369
required = false,
370370
requireNew = false,
371371
} = options;
372-
assert(
373-
!(required && requireNew),
374-
"'required' and 'requireNew' are mutually exclusive",
375-
);
372+
!(required && requireNew) ||
373+
Fail`'required' and 'requireNew' are mutually exclusive`;
376374
typeof vatSlot === 'string' || Fail`non-string vatSlot: ${vatSlot}`;
377375
const { type, allocatedByVat } = parseVatSlot(vatSlot);
378376
const vatKey = `${vatID}.c.${vatSlot}`;
379-
if (!kvStore.has(vatKey)) {
377+
if (kvStore.has(vatKey)) {
378+
!requireNew || Fail`vref ${q(vatSlot)} is already allocated`;
379+
} else {
380380
!required || Fail`vref ${vatSlot} not in clist`;
381-
if (allocatedByVat) {
382-
let kernelSlot;
383-
if (type === 'object') {
384-
// this sets the initial refcount to reachable:0 recognizable:0
385-
kernelSlot = addKernelObject(vatID);
386-
} else if (type === 'device') {
387-
Fail`normal vats aren't allowed to export device nodes`;
388-
} else if (type === 'promise') {
389-
kernelSlot = addKernelPromiseForVat(vatID);
390-
} else {
391-
Fail`unknown type ${type}`;
392-
}
393-
// now increment the refcount with isExport=true and
394-
// onlyRecognizable=true, which will skip object exports (we only
395-
// count imports) and leave the reachability count at zero
396-
const incopts = { isExport: true, onlyRecognizable: true };
397-
incrementRefCount(kernelSlot, `${vatID}|vk|clist`, incopts);
398-
const kernelKey = `${vatID}.c.${kernelSlot}`;
399-
incStat('clistEntries');
400-
// we add the key as "unreachable" but "recognizable", and then rely
401-
// on setReachableFlag() at the end to both mark it reachable and to
402-
// update any necessary refcounts consistently
403-
kvStore.set(kernelKey, buildReachableAndVatSlot(false, vatSlot));
404-
kvStore.set(vatKey, kernelSlot);
405-
kernelSlog.changeCList(
406-
vatID,
407-
getCrankNumber(),
408-
'export',
409-
kernelSlot,
410-
vatSlot,
411-
);
412-
kdebug(`Add mapping v->k ${kernelKey}<=>${vatKey}`);
381+
allocatedByVat || Fail`unknown vatSlot ${q(vatSlot)}`;
382+
let kernelSlot;
383+
if (type === 'object') {
384+
// this sets the initial refcount to reachable:0 recognizable:0
385+
kernelSlot = addKernelObject(vatID);
386+
} else if (type === 'device') {
387+
Fail`normal vats aren't allowed to export device nodes`;
388+
} else if (type === 'promise') {
389+
kernelSlot = addKernelPromiseForVat(vatID);
413390
} else {
414-
// the vat didn't allocate it, and the kernel didn't allocate it
415-
// (else it would have been in the c-list), so it must be bogus
416-
Fail`unknown vatSlot ${q(vatSlot)}`;
391+
Fail`unknown type ${type}`;
417392
}
418-
} else if (requireNew) {
419-
Fail`vref ${q(vatSlot)} is already allocated`;
393+
// now increment the refcount with isExport=true and
394+
// onlyRecognizable=true, which will skip object exports (we only
395+
// count imports) and leave the reachability count at zero
396+
const incopts = { isExport: true, onlyRecognizable: true };
397+
incrementRefCount(kernelSlot, `${vatID}|vk|clist`, incopts);
398+
const kernelKey = `${vatID}.c.${kernelSlot}`;
399+
incStat('clistEntries');
400+
// we add the key as "unreachable" but "recognizable", and then rely
401+
// on setReachableFlag() at the end to both mark it reachable and to
402+
// update any necessary refcounts consistently
403+
kvStore.set(kernelKey, buildReachableAndVatSlot(false, vatSlot));
404+
kvStore.set(vatKey, kernelSlot);
405+
kernelSlog.changeCList(
406+
vatID,
407+
getCrankNumber(),
408+
'export',
409+
kernelSlot,
410+
vatSlot,
411+
);
412+
kdebug(`Add mapping v->k ${kernelKey}<=>${vatKey}`);
420413
}
421414
const kernelSlot = getRequired(vatKey);
422415

0 commit comments

Comments
 (0)