Skip to content

Instantly share code, notes, and snippets.

@dckc
Last active September 25, 2020 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dckc/cfff1056728f5ef7561a259076e69610 to your computer and use it in GitHub Desktop.
Save dckc/cfff1056728f5ef7561a259076e69610 to your computer and use it in GitHub Desktop.

Snapshot test sketch

see especially the // snapshot worker? comments in main.js.

tested on linux with moddable version 54d8e94710ed6f8c310f046c4fbc83c967ccd346

Run as usual

mkdir -p build
mcconfig -o build -d -m

Expected Results

bundleSource reading...
setBundle...
from vatHost: ok
deliver...
from vatHost: 0
from vatHost: 1
from vatHost: 2
from vatHost: 1
demo concludes.
vatHost handling: ["setBundle","(function buildRootObject(vatPowers) {\n  const { freeze } = Object;\n  const { testLog } = vatPowers;\n\n  let count = 0;\n\n  return freeze({\n    incr() {\n      return count++;\n    },\n    decr() {\n      return count--;\n    },\n  });\n});\n"]
vatHost handling: ["deliver","message","slot1",{"method":"incr","args":[]}]
vatHost handling: ["deliver","message","slot1",{"method":"incr","args":[]}]
vatHost handling: ["deliver","message","slot1",{"method":"decr","args":[]}]
vatHost handling: ["deliver","message","slot1",{"method":"incr","args":[]}]
(function buildRootObject(vatPowers) {
const { freeze } = Object;
const { testLog } = vatPowers;
let count = 0;
return freeze({
incr() {
return count++;
},
decr() {
return count--;
},
});
});
/*
* based on Moddable worker example
*/
import Worker from "worker";
import { File } from "file";
function traceln(txt) {
trace(txt);
trace("\n");
}
function bundleSource(vatSrc) {
traceln("bundleSource reading...", vatSrc);
return new File(vatSrc).read(String);
}
function makePromiseKit() {
let resolve, reject;
const promise = new Promise((win, lose) => {
resolve = win;
reject = lose;
});
return { promise, resolve, reject };
}
export async function main() {
let vatWorker = new Worker("vatHost", {
allocation: 6 * 1024,
stackCount: 64,
slotCount: 32,
});
let sync = makePromiseKit();
vatWorker.onmessage = function (message) {
trace("from vatHost: ");
traceln(message);
sync.resolve();
sync = makePromiseKit();
};
const post = (msg) => vatWorker.postMessage(msg);
const aliceVatSrc = bundleSource("./alice-vat.js");
traceln("setBundle...");
post(["setBundle", aliceVatSrc]);
await sync.promise;
// snapshot the vatWorker?
traceln("deliver...");
post(["deliver", "message", "slot1", { method: "incr", args: [] }]);
await sync.promise;
post(["deliver", "message", "slot1", { method: "incr", args: [] }]);
await sync.promise;
// snapshot the vatWorker?
post(["deliver", "message", "slot1", { method: "decr", args: [] }]);
await sync.promise;
post(["deliver", "message", "slot1", { method: "incr", args: [] }]);
await sync.promise;
traceln("demo concludes.");
}
main().catch((err) => traceln(err));
{
"include": [
"$(MODDABLE)/examples/manifest_base.json",
"$(MODDABLE)/modules/files/file/manifest.json",
"$(MODULES)/base/worker/manifest.json"
],
"creation": {
"static": 12288,
"chunk": {
"initial": 1536,
"incremental": 512
},
"heap": {
"initial": 64,
"incremental": 32
},
"stack": 128,
"keys": {
"available": 32,
"name": 53,
"symbol": 3
},
"main": "main"
},
"modules": {
"*": ["./main", "./vatHost"]
}
}
# tested on linux
# moddable version 54d8e94710ed6f8c310f046c4fbc83c967ccd346
mkdir -p build
mcconfig -o build -d -m
const { freeze } = Object; // cf. harden
function testLog(...args) {
trace(`TEST: ${JSON.stringify(args)}\n`);
}
function main(port) {
const clist = {};
let nextSlot = 1;
const vatCmp = new Compartment();
const handle = freeze(async ([type, ...margs]) => {
trace(`vatHost handling: ${JSON.stringify([type, ...margs])}\n`);
switch (type) {
case "setBundle":
{
const [bundle] = margs;
const buildRoot = vatCmp.evaluate(bundle);
clist[`slot${nextSlot++}`] = buildRoot({ testLog });
port.postMessage("ok");
}
break;
case "deliver":
{
const [dtype, targetRef, { method, args }] = margs;
const target = clist[targetRef];
const result = target[method](...args);
port.postMessage(result);
}
break;
default:
throw new Error(type);
}
});
port.onmessage = handle;
}
main(self);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment