fetch method

dynamic fetch(
  1. String varName, {
  2. String? moduleName,
  3. String? sourceName,
})

Get a top level variable defined in a certain namespace.

Implementation

dynamic fetch(
  String varName, {
  String? moduleName,
  String? sourceName,
}) {
  try {
    final savedModuleName = _currentBytecodeModule.id;
    HTNamespace nsp = globalNamespace;
    if (moduleName != null) {
      if (_currentBytecodeModule.id != moduleName) {
        _currentBytecodeModule = cachedModules[moduleName]!;
      }
      if (sourceName != null) {
        if (nsp.fullName != sourceName) {
          assert(_currentBytecodeModule.namespaces.containsKey(sourceName));
          nsp = _currentBytecodeModule.namespaces[sourceName]!;
        }
      } else if (_currentBytecodeModule.namespaces.isNotEmpty) {
        nsp = _currentBytecodeModule.namespaces.values.last;
      }
    } else if (sourceName != null) {
      assert(_currentBytecodeModule.namespaces.containsKey(sourceName));
      nsp = _currentBytecodeModule.namespaces[sourceName]!;
    }
    final result = nsp.memberGet(varName, isRecursive: false);
    if (_currentBytecodeModule.id != savedModuleName) {
      _currentBytecodeModule = cachedModules[savedModuleName]!;
    }
    return result;
  } catch (error, stackTrace) {
    if (config.processError) {
      processError(error, stackTrace);
    } else {
      rethrow;
    }
  }
}