Skip to content

E16N: move extension manager from VM to Runtime #2243

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

Open
wants to merge 2 commits into
base: e16n
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ArgumentType = require('../extension-support/argument-type');
const Blocks = require('./blocks');
const BlocksRuntimeCache = require('./blocks-runtime-cache');
const BlockType = require('../extension-support/block-type');
const ExtensionManager = require('../extension-support/extension-manager');
const Profiler = require('./profiler');
const Sequencer = require('./sequencer');
const execute = require('./execute.js');
Expand Down Expand Up @@ -367,6 +368,11 @@ class Runtime extends EventEmitter {
* @type {function}
*/
this.removeCloudVariable = this._initializeRemoveCloudVariable(newCloudDataManager);

/**
* The Extension Manager handles loading and registering extensions, their services, and their blocks.
*/
this.extensionManager = new ExtensionManager(this);
}

/**
Expand Down
25 changes: 15 additions & 10 deletions src/virtual-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const JSZip = require('jszip');

const Buffer = require('buffer').Buffer;
const centralDispatch = require('./dispatch/central-dispatch');
const ExtensionManager = require('./extension-support/extension-manager');
const log = require('./util/log');
const MathUtil = require('./util/math-util');
const Runtime = require('./engine/runtime');
Expand Down Expand Up @@ -129,7 +128,7 @@ class VirtualMachine extends EventEmitter {
this.emit(Runtime.BLOCK_UPDATE, blockId, blockInfo);
});
this.runtime.on(Runtime.TOOLBOX_EXTENSIONS_NEED_UPDATE, () => {
this.extensionManager.refreshBlocks();
this.runtime.extensionManager.refreshBlocks();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there's no longer to bubble this event all the way up to the VM anymore. We could either remove the event all together or just have the runtime listen and handle its own event since it also owns the extensionManager.

});
this.runtime.on(Runtime.PERIPHERAL_LIST_UPDATE, info => {
this.emit(Runtime.PERIPHERAL_LIST_UPDATE, info);
Expand Down Expand Up @@ -159,11 +158,9 @@ class VirtualMachine extends EventEmitter {
this.emit(Runtime.HAS_CLOUD_DATA_UPDATE, hasCloudData);
});

this.extensionManager = new ExtensionManager(this.runtime);

// Load core extensions
for (const id of CORE_EXTENSIONS) {
this.extensionManager.loadExtensionIdSync(id);
this.runtime.extensionManager.loadExtensionIdSync(id);
}

this.blockListener = this.blockListener.bind(this);
Expand All @@ -172,6 +169,14 @@ class VirtualMachine extends EventEmitter {
this.variableListener = this.variableListener.bind(this);
}

/**
* @returns {ExtensionManager} the extension manager, now owned by the runtime.
* @deprecated Please access the extension manager through the runtime instead.
Copy link
Contributor

Choose a reason for hiding this comment

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

Could also log this warning if it's accessed this way so we can find places we've missed in our own code. Although I wonder if we care/want that? Seems like accessing via VM from the outside could be seen as the right thing to do — the VM is the external interface. But internally we refer to the actual location?

*/
get extensionManager () {
return this.runtime.extensionManager;
}

/**
* Start running the VM - do this before anything else.
*/
Expand Down Expand Up @@ -509,9 +514,9 @@ class VirtualMachine extends EventEmitter {
const extensionPromises = [];

extensions.extensionIDs.forEach(extensionID => {
if (!this.extensionManager.isExtensionLoaded(extensionID)) {
if (!this.runtime.extensionManager.isExtensionLoaded(extensionID)) {
const extensionURL = extensions.extensionURLs.get(extensionID) || extensionID;
extensionPromises.push(this.extensionManager.loadExtensionURL(extensionURL));
extensionPromises.push(this.runtime.extensionManager.loadExtensionURL(extensionURL));
}
});

Expand Down Expand Up @@ -1128,7 +1133,7 @@ class VirtualMachine extends EventEmitter {
if (locale !== formatMessage.setup().locale) {
formatMessage.setup({locale: locale, translations: {[locale]: messages}});
}
return this.extensionManager.refreshBlocks();
return this.runtime.extensionManager.refreshBlocks();
}

/**
Expand Down Expand Up @@ -1231,12 +1236,12 @@ class VirtualMachine extends EventEmitter {
const extensionIDs = new Set(copiedBlocks
.map(b => sb3.getExtensionIdForOpcode(b.opcode))
.filter(id => !!id) // Remove ids that do not exist
.filter(id => !this.extensionManager.isExtensionLoaded(id)) // and remove loaded extensions
.filter(id => !this.runtime.extensionManager.isExtensionLoaded(id)) // and remove loaded extensions
);

// Create an array promises for extensions to load
const extensionPromises = Array.from(extensionIDs,
id => this.extensionManager.loadExtensionURL(id)
id => this.runtime.extensionManager.loadExtensionURL(id)
);

return Promise.all(extensionPromises).then(() => {
Expand Down
6 changes: 3 additions & 3 deletions test/integration/internal-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test('internal extension', t => {
t.ok(extension.status.constructorCalled);

t.notOk(extension.status.getInfoCalled);
vm.extensionManager._registerInternalExtension(extension);
vm.runtime.extensionManager._registerInternalExtension(extension);
t.ok(extension.status.getInfoCalled);

const func = vm.runtime.getOpcodeFunction('testInternalExtension_go');
Expand Down Expand Up @@ -102,8 +102,8 @@ test('internal extension', t => {

test('load sync', t => {
const vm = new VirtualMachine();
vm.extensionManager.loadExtensionIdSync('coreExample');
t.ok(vm.extensionManager.isExtensionLoaded('coreExample'));
vm.runtime.extensionManager.loadExtensionIdSync('coreExample');
t.ok(vm.runtime.extensionManager.isExtensionLoaded('coreExample'));

t.equal(vm.runtime._blockInfo.length, 1);

Expand Down
4 changes: 2 additions & 2 deletions test/integration/load-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('Load external extensions', async t => {
await t.test('Confirm expected extension is installed in example sb2 and sb3 projects', extTest => {
vm.loadProject(project)
.then(() => {
extTest.ok(vm.extensionManager.isExtensionLoaded(ext));
extTest.ok(vm.runtime.extensionManager.isExtensionLoaded(ext));
extTest.end();
});
});
Expand Down Expand Up @@ -53,7 +53,7 @@ test('Load video sensing extension and video properties', async t => {

const stage = vm.runtime.getTargetForStage();

t.ok(vm.extensionManager.isExtensionLoaded('videoSensing'));
t.ok(vm.runtime.extensionManager.isExtensionLoaded('videoSensing'));

// Check that the stage target has the video state values we expect
// based on the test project files, then check that the video io device
Expand Down
4 changes: 2 additions & 2 deletions test/integration/monitors_sb3.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ test('importing sb3 project with monitors', t => {
t.equal(monitorRecord.visible, true);
t.equal(monitorRecord.spriteName, null);
t.equal(monitorRecord.targetId, null);
t.equal(vm.extensionManager.isExtensionLoaded('music'), true);
t.equal(vm.runtime.extensionManager.isExtensionLoaded('music'), true);

monitorId = 'ev3_getDistance';
monitorRecord = vm.runtime._monitorState.get(monitorId);
Expand All @@ -245,7 +245,7 @@ test('importing sb3 project with monitors', t => {
t.equal(monitorRecord.visible, true);
t.equal(monitorRecord.spriteName, null);
t.equal(monitorRecord.targetId, null);
t.equal(vm.extensionManager.isExtensionLoaded('ev3'), true);
t.equal(vm.runtime.extensionManager.isExtensionLoaded('ev3'), true);

t.end();
process.nextTick(process.exit);
Expand Down
8 changes: 4 additions & 4 deletions test/integration/sb2-import-extension-monitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('loading sb2 project with invisible video monitor should not load monitor o
vm.setCompatibilityMode(false);
vm.setTurboMode(false);
vm.loadProject(invisibleVideoMonitorProject).then(() => {
t.equal(vm.extensionManager.isExtensionLoaded('videoSensing'), false);
t.equal(vm.runtime.extensionManager.isExtensionLoaded('videoSensing'), false);
t.equal(vm.runtime._monitorState.size, 0);
t.end();
});
Expand All @@ -53,7 +53,7 @@ test('loading sb2 project with visible video monitor should not load extension',
vm.setCompatibilityMode(false);
vm.setTurboMode(false);
vm.loadProject(visibleVideoMonitorProject).then(() => {
t.equal(vm.extensionManager.isExtensionLoaded('videoSensing'), false);
t.equal(vm.runtime.extensionManager.isExtensionLoaded('videoSensing'), false);
t.equal(vm.runtime._monitorState.size, 0);
t.end();
});
Expand Down Expand Up @@ -84,7 +84,7 @@ test('sb2 project with invisible music monitor should not load monitor or extens
vm.setCompatibilityMode(false);
vm.setTurboMode(false);
vm.loadProject(invisibleTempoMonitorProject).then(() => {
t.equal(vm.extensionManager.isExtensionLoaded('music'), false);
t.equal(vm.runtime.extensionManager.isExtensionLoaded('music'), false);
t.equal(vm.runtime._monitorState.size, 0);
t.end();
});
Expand All @@ -100,7 +100,7 @@ test('sb2 project with visible music monitor should load monitor and extension',
vm.setCompatibilityMode(false);
vm.setTurboMode(false);
vm.loadProject(visibleTempoMonitorProject).then(() => {
t.equal(vm.extensionManager.isExtensionLoaded('music'), true);
t.equal(vm.runtime.extensionManager.isExtensionLoaded('music'), true);
t.equal(vm.runtime._monitorState.size, 1);
t.equal(vm.runtime._monitorState.has('music_getTempo'), true);
t.equal(vm.runtime._monitorState.get('music_getTempo').visible, true);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/virtual-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ test('shareBlocksToTarget loads extensions that have not yet been loaded', t =>

// Stub the extension manager
const loadedIds = [];
vm.extensionManager = {
vm.runtime.extensionManager = {
isExtensionLoaded: id => id === 'loaded',
loadExtensionURL: id => new Promise(resolve => {
loadedIds.push(id);
Expand Down