pass blockInfo to extension methods

Sometimes a single extension method needs to service several different
instances of the same opcode. This can happen with variables or custom
procedures, for example. This change allows the extension method to
inspect the `blockInfo` for instance data, including arbitrary
extension-specific properties if necessary.
This commit is contained in:
Christopher Willis-Ford 2019-04-08 15:20:14 -07:00
parent 9135780c55
commit 638062e982

View file

@ -391,23 +391,22 @@ class ExtensionManager {
} }
break; break;
default: default:
if (!blockInfo.opcode) { if (blockInfo.opcode) {
throw new Error('Missing opcode for block'); const funcName = blockInfo.func ? this._sanitizeID(blockInfo.func) : blockInfo.opcode;
}
blockInfo.func = blockInfo.func ? this._sanitizeID(blockInfo.func) : blockInfo.opcode; // Avoid promise latency unless necessary
// Avoid promise overhead if possible
if (dispatch._isRemoteService(serviceName)) { if (dispatch._isRemoteService(serviceName)) {
blockInfo.func = dispatch.call.bind(dispatch, serviceName, blockInfo.func); blockInfo.func = (args, util) => dispatch.call(serviceName, funcName, args, util, blockInfo);
} else { } else {
const serviceObject = dispatch.services[serviceName]; const serviceObject = dispatch.services[serviceName];
const func = serviceObject[blockInfo.func]; if (!serviceObject[funcName]) {
if (func) { // The function might show up later as a dynamic property of the service object
blockInfo.func = func.bind(serviceObject); log.warn(`Could not find extension block function called ${funcName}`);
} else {
throw new Error(`Could not find extension block function called ${blockInfo.func}`);
} }
blockInfo.func = (args, util) => serviceObject[funcName](args, util, blockInfo);
}
} else {
throw new Error('Missing opcode for block');
} }
break; break;
} }