Skip to content

Commit

Permalink
chore: incorporate fakeVirtualObjectManager for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
katelynsills committed Feb 27, 2021
1 parent 0133a3a commit f9cb176
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/ERTP/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
],
"require": [
"esm",
"@agoric/install-ses"
"@agoric/install-ses",
"./test/_setupFakeVirtualObjectManager.js"
]
},
"eslintConfig": {
Expand Down
7 changes: 7 additions & 0 deletions packages/ERTP/test/_setupFakeVirtualObjectManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { makeFakeVirtualObjectManager } from '@agoric/swingset-vat/tools/fakeVirtualObjectManager';

const { makeKind, makeWeakStore } = makeFakeVirtualObjectManager(3);

global.makeKind = makeKind;
global.makeWeakStore = makeWeakStore;
3 changes: 2 additions & 1 deletion packages/SwingSet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"files": [
"bin/vat",
"src/**/*.js",
"exported.js"
"exported.js",
"tools"
],
"repository": {
"type": "git",
Expand Down
71 changes: 71 additions & 0 deletions packages/SwingSet/tools/fakeVirtualObjectManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { makeMarshal } from '@agoric/marshal';
import { assert } from '@agoric/assert';
import { parseVatSlot } from '../src/parseVatSlots';

import { makeVirtualObjectManager } from '../src/kernel/virtualObjectManager';

export function makeFakeVirtualObjectManager(cacheSize = 100) {
const fakeStore = new Map();

function dumpStore() {
const result = [];
for (const entry of fakeStore.entries()) {
result.push(entry);
}
result.sort((e1, e2) => e1[0].localeCompare(e2[0]));
return result;
}

const fakeSyscall = {
vatstoreGet: key => fakeStore.get(key),
vatstoreSet: (key, value) => fakeStore.set(key, value),
vatstoreDelete: key => fakeStore.delete(key),
};

let nextExportID = 1;
function fakeAllocateExportID() {
const exportID = nextExportID;
nextExportID += 1;
return exportID;
}

const valToSlot = new WeakMap();

function fakeConvertValToSlot(val) {
return valToSlot.get(val);
}

function fakeConvertSlotToVal(slot) {
const { type, virtual } = parseVatSlot(slot);
assert(
virtual,
'fakeConvertSlotToVal only works with virtual object references',
);
assert.equal(type, 'object');
// eslint-disable-next-line no-use-before-define
return makeVirtualObjectRepresentative(slot);
}

// eslint-disable-next-line no-use-before-define
const fakeMarshal = makeMarshal(fakeConvertValToSlot, fakeConvertSlotToVal);

const {
makeVirtualObjectRepresentative,
makeWeakStore,
makeKind,
flushCache,
} = makeVirtualObjectManager(
fakeSyscall,
fakeAllocateExportID,
valToSlot,
fakeMarshal,
cacheSize,
);

return {
makeKind,
makeWeakStore,
flushCache,
dumpStore,
};
}

0 comments on commit f9cb176

Please sign in to comment.