require method
Dynamically load a source into current bytecode.
Implementation
HTNamespace require(String path) {
final key = config.normalizeImportPath
? sourceContext.getAbsolutePath(key: path)
: path;
// Search in current module first
if (interpreter.currentBytecodeModule.namespaces.containsKey(key)) {
return interpreter.currentBytecodeModule.namespaces[key]!;
}
// If the source is not in current module, then try to search it in any loaded modules.
else {
for (final module in interpreter.cachedModules.values) {
for (final nsp in module.namespaces.values) {
if (nsp.fullName == key) {
return nsp;
}
}
}
}
// If the source has not been evaled at all, then we have to load the source dynamically.
final source = sourceContext.getResource(key);
final bytes = _compileSource(source);
final HTContext savedContext = interpreter.getContext();
interpreter.loadBytecode(bytes: bytes, module: key);
final nsp = interpreter.currentBytecodeModule.namespaces.values.last;
interpreter.setContext(savedContext);
return nsp;
}