Skip to content

Commit

Permalink
Inspector config hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
laverdet committed Apr 11, 2018
1 parent 9edc33f commit 4adc911
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 55 deletions.
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ _.extend(config.engine, {
customIntentTypes: {},
historyChunkSize: 20,
useSigintTimeout: false,
reportMemoryUsageInterval: 0
reportMemoryUsageInterval: 0,
enableInspector: false,
});

config.engine.on('playerSandbox', (sandbox) => {
Expand Down Expand Up @@ -508,4 +509,4 @@ exports.queue = queue;

exports.constants = config.common.constants;

process.on('disconnect', () => process.exit());
process.on('disconnect', () => process.exit());
2 changes: 1 addition & 1 deletion lib/runtime/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ module.exports = function(userId, onlyInRoom) {
runtimeUserVm.clear(userId);
console.error('isolated-vm timeout', userId);
pubsub.publish(`user:${userId}/cpu`, JSON.stringify({cpu: 'error'}));
}, 5000);
}, Math.max(5000, config.engine.mainLoopResetInterval));
make(scope, userId, onlyInRoom).then(resolve).catch(reject);
})
.then(result => {
Expand Down
110 changes: 58 additions & 52 deletions lib/runtime/user-vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,77 +18,83 @@ exports.create = async function({userId, staticTerrainData, staticTerrainDataSiz
exports.clear(userId);
throw 'Script execution has been terminated: your isolate disposed unexpectedly, restarting virtual machine';
}
if(codeTimestamp > vms[userId].codeTimestamp) {
if(!vms[userId].ready) {
return vms[userId].promise;
} else if(codeTimestamp > vms[userId].codeTimestamp) {
exports.clear(userId);
}
}

if(!vms[userId]) {
let isolate = new ivm.Isolate({snapshot, memoryLimit: 256 + staticTerrainDataSize/1024/1024});
let context = await isolate.createContext();
let [ nativeModInstance, initScript, cleanupScript ] = await Promise.all([
nativeMod.create(context),
isolate.compileScript('_init();'),
isolate.compileScript('new ' + function () {
delete global._ivm;
delete global._isolate;
delete global._context;
delete global._init;
delete global._evalFn;
delete global._start;
delete global._setStaticTerrainData;
delete global._worldSize;
delete global._nativeMod;
}),
]);
await Promise.all([
context.global.set('global', context.global.derefInto()),
context.global.set('_ivm', ivm),
context.global.set('_isolate', isolate),
context.global.set('_context', context),
context.global.set('_worldSize', index.getWorldSize()),
context.global.set('_nativeMod', nativeModInstance.derefInto()),
initScript.run(context),
]);
let [ evalFn, start, setStaticTerrainData ] = await Promise.all([
context.global.get('_evalFn'),
context.global.get('_start'),
context.global.get('_setStaticTerrainData'),
]);
let inspector = config.engine.enableInspector;
let isolate = new ivm.Isolate({inspector, snapshot, memoryLimit: 256 + staticTerrainDataSize/1024/1024});
let vm = vms[userId] = {isolate, ready: false};
vm.promise = async function() {
let context = await isolate.createContext({inspector});
let [ nativeModInstance, initScript, cleanupScript ] = await Promise.all([
nativeMod.create(context),
isolate.compileScript('_init();'),
isolate.compileScript('new ' + function () {
delete global._ivm;
delete global._isolate;
delete global._context;
delete global._init;
delete global._evalFn;
delete global._start;
delete global._setStaticTerrainData;
delete global._worldSize;
delete global._nativeMod;
}),
]);
await Promise.all([
context.global.set('global', context.global.derefInto()),
context.global.set('_ivm', ivm),
context.global.set('_isolate', isolate),
context.global.set('_context', context),
context.global.set('_worldSize', index.getWorldSize()),
context.global.set('_nativeMod', nativeModInstance.derefInto()),
initScript.run(context),
]);
let [ evalFn, start, setStaticTerrainData ] = await Promise.all([
context.global.get('_evalFn'),
context.global.get('_start'),
context.global.get('_setStaticTerrainData'),
]);

await Promise.all([
setStaticTerrainData.apply(undefined, [
new ivm.ExternalCopy(staticTerrainData.buffer).copyInto({ release: true }),
new ivm.ExternalCopy(staticTerrainData.roomOffsets).copyInto({ release: true }),
]),
cleanupScript.run(context),
]);
await Promise.all([
setStaticTerrainData.apply(undefined, [
new ivm.ExternalCopy(staticTerrainData.buffer).copyInto({ release: true }),
new ivm.ExternalCopy(staticTerrainData.roomOffsets).copyInto({ release: true }),
]),
cleanupScript.run(context),
]);

vms[userId] = {
isolate,
context,
start,
evalFn,
nativeModInstance,
codeTimestamp
};
Object.assign(vm, {
ready: true,
context,
start,
evalFn,
nativeModInstance,
codeTimestamp
});
}();
await vm.promise;
}

vms[userId].lastUsed = Date.now();
};

exports.get = function(userId) {
userId = ""+userId;
return vms[userId];
let vm = vms[userId];
if (vm && vm.ready) {
return vm;
}
};
exports.clear = function(userId) {
userId = ""+userId;
if(vms[userId]) {
try {
vms[userId].start.dispose();
vms[userId].evalFn.dispose();
vms[userId].nativeModInstance.dispose();
vms[userId].context.release();
if(!vms[userId].isolate.isDisposed) {
vms[userId].isolate.dispose();
}
Expand Down

0 comments on commit 4adc911

Please sign in to comment.