diff --git a/index.js b/index.js index b1e4919..215d4fd 100644 --- a/index.js +++ b/index.js @@ -293,7 +293,7 @@ module.exports = function createDLayer(options) { } async function make(typeID, args, existenceRequired) { - let type = loaded.types[typeID].func; + let type = loaded.types[typeID]; if (type == null) { if (existenceRequired === true) { @@ -302,7 +302,7 @@ module.exports = function createDLayer(options) { return; } } else { - let instance = await type(args); + let instance = await type.func(args, getContextForModule(type.__moduleID)); if (loaded.extensions[typeID] != null && instance !== InvalidObject) { for (let [ key, extension ] of Object.entries(loaded.extensions[typeID])) { diff --git a/load-modules.js b/load-modules.js index 356e5a2..03d7d09 100644 --- a/load-modules.js +++ b/load-modules.js @@ -32,12 +32,12 @@ function createTypeTracker() { throw new Error(`Type '${name}' already exists (from module '${module.name}', already defined by module '${existingEntry.source.name}')`); } else { typeFactories[name] = { + __moduleID: getModuleID(module), source: module, - // No context provided to type factory functions for now, since they are not allowed to be async for now anyway - // FIXME: Maybe add a warning if the user returns a Promise from a factory, asking them to file a bug if they really need it? // func: wrapModuleFunction(module, factory) - func: async function (... args) { - let instance = await factory(... args); + func: async function (args, context) { + // NOTE: We pass through the context here without modifying it; because a module-specific context will be injected from the main runtime + let instance = await factory(args, context); if (instance !== InvalidObject) { // We need to patch every created object, so that the correct context gets injected into its type-specific methods when they are called