Skip to content

module.derive (import-with) doesn’t handle circular imports. #178

@mbostock

Description

@mbostock

module.derive creates a copy of the module:

runtime/src/module.js

Lines 61 to 88 in 0801ad4

function module_derive(injects, injectModule) {
var injectByAlias = new Map;
forEach.call(injects, function(inject) {
if (typeof inject !== "object") inject = {name: inject + ""};
if (inject.alias == null) inject.alias = inject.name;
injectByAlias.set(inject.alias, inject);
});
return this._copy(injectByAlias, injectModule, new Map);
}
function module_copy(injectByAlias, injectModule, map) {
var copy = new Module(this._runtime);
map.set(this, copy);
this._scope.forEach(function(source, name) {
var target = new Variable(source._type, copy), inject;
if (inject = injectByAlias.get(name)) {
target.import(inject.name, inject.alias, injectModule);
} else if (source._definition === identity) { // import!
var sourceInput = source._inputs[0],
sourceModule = sourceInput._module,
targetModule = map.get(sourceModule) || sourceModule._copy(none, null, map);
target.import(sourceInput._name, name, targetModule);
} else {
target.define(name, source._inputs.map(variable_name), source._definition);
}
});
return copy;
}

The problem is if you have circular imports, runtime.module will give you an empty module:

runtime/src/runtime.js

Lines 53 to 72 in 0801ad4

function runtime_module(define, observer = noop) {
let module;
if (define === undefined) {
if (module = this._init) {
this._init = null;
return module;
}
return new Module(this);
}
module = this._modules.get(define);
if (module) return module;
this._init = module = new Module(this);
this._modules.set(define, module);
try {
define(this, observer);
} finally {
this._init = null;
}
return module;
}

So, we need module.derive to somehow be lazy, rather than copying the variables greedily into the new module.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions