2021-03-04 18:54:12 +00:00
|
|
|
import { Actor, Assertion, Turn } from './actor.js';
|
2021-03-02 11:41:06 +00:00
|
|
|
import { Dictionary, Record } from 'preserves';
|
|
|
|
import { Dataspace, Observe } from './dataspace.js';
|
2021-03-02 08:50:23 +00:00
|
|
|
import { Worker } from 'worker_threads';
|
|
|
|
import { Relay, spawnRelay } from './relay.js';
|
2021-03-02 11:41:06 +00:00
|
|
|
import { BoxState, SetBox } from './box-protocol.js';
|
2021-03-04 18:54:12 +00:00
|
|
|
import { attenuate, CRec, Lit, Pattern, PCompound, rfilter } from './rewrite.js';
|
2021-03-02 12:53:33 +00:00
|
|
|
import path from 'path';
|
2021-02-23 15:16:15 +00:00
|
|
|
|
2021-03-02 08:50:23 +00:00
|
|
|
const Instance = Record.makeConstructor<{moduleName: string, arg: Assertion}>()(
|
|
|
|
Symbol.for('Instance'), ['moduleName', 'arg']);
|
2021-02-22 18:37:47 +00:00
|
|
|
|
2021-03-02 08:50:23 +00:00
|
|
|
function spawnWorker(t: Turn, moduleName: string, arg: Assertion) {
|
2021-03-02 12:53:33 +00:00
|
|
|
const w = new Worker(path.join(__dirname, 'wload.js'));
|
2021-03-02 08:50:23 +00:00
|
|
|
spawnRelay(t, {
|
|
|
|
packetWriter: bs => w.postMessage(bs),
|
|
|
|
setup(t: Turn, r: Relay) {
|
|
|
|
w.on('message', bs => r.accept(bs));
|
|
|
|
w.on('error', err => Turn.for(t.actor, t => t.crash(err)));
|
|
|
|
w.on('exit', code => Turn.for(t.actor, t => {
|
|
|
|
if (code === 0) {
|
2021-02-22 18:37:47 +00:00
|
|
|
t.quit();
|
|
|
|
} else {
|
2021-03-02 08:50:23 +00:00
|
|
|
t.crash(new Error(`Worker crashed with code ${code}`));
|
2021-02-22 18:37:47 +00:00
|
|
|
}
|
2021-03-02 08:50:23 +00:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
initialOid: 0,
|
|
|
|
}).then(factory => Turn.for(new Actor(), t => {
|
|
|
|
t.assert(factory!, Instance(moduleName, arg));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function spawnModule(t: Turn, moduleName: string, arg: Assertion) {
|
2021-03-03 15:23:00 +00:00
|
|
|
import(moduleName).then(m => t.freshen(t => t.spawn(t => m.default(t, arg))));
|
2021-02-23 15:16:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 11:41:06 +00:00
|
|
|
// __setNextActorId(1000);
|
|
|
|
|
2021-02-23 15:16:15 +00:00
|
|
|
Turn.for(new Actor(), async (t: Turn) => {
|
2021-03-03 10:45:01 +00:00
|
|
|
const ds_unproxied = t.ref(new Dataspace());
|
|
|
|
|
|
|
|
const ds = ds_unproxied;
|
|
|
|
// const { proxy: ds, revoker } = forwarder(t, ds_unproxied);
|
|
|
|
// t.spawn(t => {
|
|
|
|
// t.assert(ds, Observe(SetBox.constructorInfo.label, t.ref({
|
|
|
|
// message(t: Turn, [newValue]: [number]): void {
|
|
|
|
// if (newValue === 30000) {
|
|
|
|
// console.log('BOOM!');
|
|
|
|
// t.message(revoker, true);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// })));
|
|
|
|
// });
|
2021-02-24 20:48:55 +00:00
|
|
|
|
2021-03-02 11:41:06 +00:00
|
|
|
const ds_for_box = attenuate(
|
|
|
|
ds,
|
|
|
|
rfilter(PCompound(CRec(BoxState.constructorInfo.label,
|
|
|
|
BoxState.constructorInfo.arity),
|
|
|
|
new Dictionary()),
|
|
|
|
PCompound(CRec(Observe.constructorInfo.label,
|
|
|
|
Observe.constructorInfo.arity),
|
2021-03-03 14:45:35 +00:00
|
|
|
new Dictionary<Pattern, never>([
|
2021-03-02 11:41:06 +00:00
|
|
|
[0, Lit(SetBox.constructorInfo.label)]]))));
|
|
|
|
|
|
|
|
const ds_for_client = attenuate(
|
2021-03-03 10:45:01 +00:00
|
|
|
ds_unproxied,
|
2021-03-02 11:41:06 +00:00
|
|
|
rfilter(PCompound(CRec(SetBox.constructorInfo.label,
|
|
|
|
SetBox.constructorInfo.arity),
|
|
|
|
new Dictionary()),
|
|
|
|
PCompound(CRec(Observe.constructorInfo.label,
|
|
|
|
Observe.constructorInfo.arity),
|
2021-03-03 14:45:35 +00:00
|
|
|
new Dictionary<Pattern, never>([
|
2021-03-02 11:41:06 +00:00
|
|
|
[0, Lit(BoxState.constructorInfo.label)]]))));
|
|
|
|
|
|
|
|
|
2021-03-02 12:53:33 +00:00
|
|
|
const boxpath = path.join(__dirname, 'box.js');
|
|
|
|
const clientpath = path.join(__dirname, 'client.js');
|
2021-03-02 11:41:06 +00:00
|
|
|
|
2021-03-03 14:45:35 +00:00
|
|
|
// spawnModule(t, boxpath, [ds_for_box, 500000, 25000]);
|
|
|
|
spawnWorker(t, boxpath, [ds_for_box, 50000, 2500]);
|
2021-02-24 20:48:55 +00:00
|
|
|
|
2021-03-02 12:53:33 +00:00
|
|
|
spawnModule(t, clientpath, ds_for_client);
|
|
|
|
// spawnWorker(t, clientpath, ds_for_client);
|
2021-02-22 09:12:10 +00:00
|
|
|
});
|