Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct oversights & editing errors in virtual object code #1967

Merged
merged 3 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions packages/SwingSet/src/kernel/virtualObjectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,24 +406,22 @@ export function makeVirtualObjectManager(
harden(target);
}

let representative;
let instanceKit;
if (initializing) {
innerSelf.wrapData = wrapData;
representative = instanceMaker(innerSelf.rawData);
instanceKit = harden(instanceMaker(innerSelf.rawData));
} else {
const activeData = {};
wrapData(activeData);
representative = instanceMaker(activeData);
delete representative.initialize;
harden(representative);
instanceKit = harden(instanceMaker(activeData));
}
cache.remember(innerSelf);
valToSlotTable.set(representative, innerSelf.vobjID);
return representative;
valToSlotTable.set(instanceKit.self, innerSelf.vobjID);
return instanceKit;
}

function reanimate(vobjID) {
return makeRepresentative(cache.lookup(vobjID), false);
return makeRepresentative(cache.lookup(vobjID), false).self;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it now makes an instance kit, how about?

Suggested change
return makeRepresentative(cache.lookup(vobjID), false).self;
return makeInstanceKit(cache.lookup(vobjID), false).self;

}
kindTable.set(kindID, reanimate);

Expand All @@ -434,12 +432,11 @@ export function makeVirtualObjectManager(
const initialData = {};
initializationsInProgress.add(initialData);
const innerSelf = { vobjID, rawData: initialData };
const initialRepresentative = makeRepresentative(innerSelf, true);
const initialize = initialRepresentative.initialize;
delete initialRepresentative.initialize;
harden(initialRepresentative);
if (initialize) {
initialize(...args);
// prettier-ignore
const { self: initialRepresentative, init } =
makeRepresentative(innerSelf, true);
if (init) {
init(...args);
}
initializationsInProgress.delete(initialData);
const rawData = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,52 +76,56 @@ function makeAllTheStuff(cacheSize) {

function makeThingInstance(state) {
return {
initialize(label = 'thing', counter = 0) {
init(label = 'thing', counter = 0) {
state.counter = counter;
state.label = label;
state.resetCounter = 0;
},
inc() {
state.counter += 1;
return state.counter;
},
reset(newStart) {
state.counter = newStart;
state.resetCounter += 1;
return state.resetCounter;
},
relabel(newLabel) {
state.label = newLabel;
},
get() {
return state.counter;
},
describe() {
return `${state.label} counter has been reset ${state.resetCounter} times and is now ${state.counter}`;
self: {
inc() {
state.counter += 1;
return state.counter;
},
reset(newStart) {
state.counter = newStart;
state.resetCounter += 1;
return state.resetCounter;
},
relabel(newLabel) {
state.label = newLabel;
},
get() {
return state.counter;
},
describe() {
return `${state.label} counter has been reset ${state.resetCounter} times and is now ${state.counter}`;
},
},
};
}

function makeZotInstance(state) {
return {
initialize(arbitrary = 47, name = 'Bob', tag = 'say what?') {
init(arbitrary = 47, name = 'Bob', tag = 'say what?') {
state.arbitrary = arbitrary;
state.name = name;
state.tag = tag;
state.count = 0;
},
sayHello(msg) {
state.count += 1;
return `${msg} ${state.name}`;
},
rename(newName) {
state.name = newName;
state.count += 1;
return state.name;
},
getInfo() {
state.count += 1;
return `zot ${state.name} tag=${state.tag} count=${state.count} arbitrary=${state.arbitrary}`;
self: {
sayHello(msg) {
state.count += 1;
return `${msg} ${state.name}`;
},
rename(newName) {
state.name = newName;
state.count += 1;
return state.name;
},
getInfo() {
state.count += 1;
return `zot ${state.name} tag=${state.tag} count=${state.count} arbitrary=${state.arbitrary}`;
},
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const stuff = makeWeakStore();
let initialSelf;

function makeThingInstance(state) {
function init(name) {
state.name = name;
// eslint-disable-next-line no-use-before-define
initialSelf = self;
}
const self = {
initialize(name) {
state.name = name;
initialSelf = self;
},
getName() {
return state.name;
},
Expand All @@ -19,32 +20,32 @@ function makeThingInstance(state) {
return self;
},
};
return self;
return { init, self };
}

const thingMaker = makeKind(makeThingInstance);

function makeZotInstance(state) {
function init(name, forceOverflow) {
state.name = name;
// enough instances to push me out of the cache
for (let i = 0; i < 5; i += 1) {
stuff.init(thingMaker(`${name}-subthing${i}`, 29));
}
if (forceOverflow) {
// eslint-disable-next-line no-use-before-define
zotMaker('recur', true);
}
}
const self = {
initialize(name, forceOverflow) {
state.name = name;
// enough instances to push me out of the cache
for (let i = 0; i < 5; i += 1) {
stuff.init(thingMaker(`${name}-subthing${i}`, 29));
}
if (forceOverflow) {
// eslint-disable-next-line no-use-before-define
zotMaker('recur', true);
}
},
getName() {
return state.name;
},
rename(newName) {
state.name = newName;
},
};
return self;
return { init, self };
}

const zotMaker = makeKind(makeZotInstance);
Expand Down
74 changes: 39 additions & 35 deletions packages/swingset-runner/demo/vatStore1/vat-bob.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,34 @@ const p = console.log;

function makeThingInstance(state) {
return {
initialize(label = 'thing', counter = 0) {
init(label = 'thing', counter = 0) {
p(`@@@ thing.initialize(${label}, ${counter})`);
state.counter = counter;
state.label = label;
state.resetCounter = 0;
},
inc() {
state.counter += 1;
p(`#thing# ${state.label} inc() counter now ${state.counter}`);
},
reset(newStart) {
p(`#thing# ${state.label} reset(${newStart})`);
state.counter = newStart;
state.resetCounter += 1;
},
relabel(newLabel) {
p(`#thing# ${state.label} relabel(${newLabel})`);
state.label = newLabel;
},
get() {
p(`#thing# ${state.label} get()=>${state.counter}`);
return state.counter;
},
describe() {
p(`#thing# ${state.label} describe()`);
return `${state.label} counter has been reset ${state.resetCounter} times and is now ${state.counter}`;
self: {
inc() {
state.counter += 1;
p(`#thing# ${state.label} inc() counter now ${state.counter}`);
},
reset(newStart) {
p(`#thing# ${state.label} reset(${newStart})`);
state.counter = newStart;
state.resetCounter += 1;
},
relabel(newLabel) {
p(`#thing# ${state.label} relabel(${newLabel})`);
state.label = newLabel;
},
get() {
p(`#thing# ${state.label} get()=>${state.counter}`);
return state.counter;
},
describe() {
p(`#thing# ${state.label} describe()`);
return `${state.label} counter has been reset ${state.resetCounter} times and is now ${state.counter}`;
},
},
};
}
Expand All @@ -37,26 +39,28 @@ const thingMaker = makeKind(makeThingInstance);

function makeZotInstance(state) {
return {
initialize(arbitrary = 47, name = 'Bob', tag = 'say what?') {
init(arbitrary = 47, name = 'Bob', tag = 'say what?') {
p(`@@@ zot.initialize(${arbitrary}, ${name}, ${tag})`);
state.arbitrary = arbitrary;
state.name = name;
state.tag = tag;
state.count = 0;
},
sayHello(msg) {
p(`#zot# ${msg} ${state.name}`);
state.count += 1;
},
rename(newName) {
p(`#zot# ${state.name} rename(${newName})`);
state.name = newName;
state.count += 1;
},
printInfo() {
// prettier-ignore
p(`#zot# ${state.name} tag=${state.tag} count=${state.count} arbitrary=${state.arbitrary}`);
state.count += 1;
self: {
sayHello(msg) {
p(`#zot# ${msg} ${state.name}`);
state.count += 1;
},
rename(newName) {
p(`#zot# ${state.name} rename(${newName})`);
state.name = newName;
state.count += 1;
},
printInfo() {
// prettier-ignore
p(`#zot# ${state.name} tag=${state.tag} count=${state.count} arbitrary=${state.arbitrary}`);
state.count += 1;
},
},
};
}
Expand Down
42 changes: 22 additions & 20 deletions packages/swingset-runner/demo/vatStore2/thingHolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,34 @@ const p = console.log;
function build(name) {
function makeThingInstance(state) {
return {
initialize(label, companion, companionName) {
init(label, companion, companionName) {
p(`${name}'s thing ${label}: initialize ${companionName}`);
state.label = label;
state.companion = companion;
state.companionName = companionName;
state.count = 0;
},
echo(message) {
state.count += 1;
E(state.companion).say(message);
},
async changePartner(newCompanion) {
state.count += 1;
state.companion = newCompanion;
const companionName = await E(newCompanion).getName();
state.companionName = companionName;
p(`${name}'s thing ${state.label}: changePartner ${companionName}`);
},
getLabel() {
const label = state.label;
p(`${name}'s thing ${label}: getLabel`);
state.count += 1;
return label;
},
report() {
p(`${name}'s thing ${state.label} invoked ${state.count} times`);
self: {
echo(message) {
state.count += 1;
E(state.companion).say(message);
},
async changePartner(newCompanion) {
state.count += 1;
state.companion = newCompanion;
const companionName = await E(newCompanion).getName();
state.companionName = companionName;
p(`${name}'s thing ${state.label}: changePartner ${companionName}`);
},
getLabel() {
const label = state.label;
p(`${name}'s thing ${label}: getLabel`);
state.count += 1;
return label;
},
report() {
p(`${name}'s thing ${state.label} invoked ${state.count} times`);
},
},
};
}
Expand Down