Esempio n. 1
0
  /** A synchronized call to {@link DartCompiler#analyzeLibraries} */
  public static Map<URI, LibraryUnit> secureAnalyzeLibraries(
      LibrarySource librarySource,
      SelectiveCache selectiveCache,
      CompilerConfiguration config,
      DartArtifactProvider provider,
      DartCompilerListener listener,
      boolean resolveAllNewLibs)
      throws IOException {
    // All calls to DartC must be synchronized

    long start = System.currentTimeMillis();
    Map<URI, LibraryUnit> ret;

    synchronized (compilerLock) {
      ret =
          DartCompiler.analyzeLibraries(
              librarySource, selectiveCache, config, provider, listener, resolveAllNewLibs);
    }

    long elapsed = System.currentTimeMillis() - start;
    Instrumentation.metric("DartCompilerUtils-secureAnalyzeLibraries", elapsed).log();
    Instrumentation.operation("DartCompilerUtils-secureAnalyzeLibraries", elapsed)
        .with("librarySource.Name", librarySource.getName())
        .with("librarySource.LastModified", librarySource.getLastModified())
        .log();

    return ret;
  }
Esempio n. 2
0
  /**
   * A synchronized call to {@link DartCompiler#compileLib(LibrarySource, CompilerConfiguration,
   * DartArtifactProvider, DartCompilerListener)}
   */
  public static void secureCompileLib(
      LibrarySource libSource,
      CompilerConfiguration config,
      DartArtifactProvider provider,
      DartCompilerListener listener)
      throws IOException {

    long start = System.currentTimeMillis();

    if (!DartSdkManager.getManager().hasSdk()) {
      return;
    }

    List<LibrarySource> embeddedLibraries = new ArrayList<LibrarySource>();
    // All calls to DartC must be synchronized
    synchronized (compilerLock) {
      DartCompiler.compileLib(libSource, embeddedLibraries, config, provider, listener);
    }

    long elapsed = System.currentTimeMillis() - start;
    Instrumentation.metric("DartCompilerUtils-secureCompileLib", elapsed).log();

    Instrumentation.operation("DartCompilerUtils-secureCompileLib", elapsed)
        .with("librarySource.Name", libSource.getName())
        .log();
  }
 private void compile(LibrarySource lib, String srcName, Object... errors) {
   try {
     DartCompilerListener listener = new DartCompilerListenerTest(srcName, errors);
     DartCompiler.compileLib(lib, config, provider, listener);
   } catch (IOException e) {
     throw new AssertionFailedError("Unexpected IOException: " + e.getMessage());
   }
 }
Esempio n. 4
0
  /**
   * Resolve the specified library and any imported libraries that have not already been resolved.
   *
   * @return a map of newly resolved libraries
   */
  static Map<URI, LibraryUnit> resolve(
      AnalysisServer server,
      Library library,
      Map<URI, LibraryUnit> resolvedLibs,
      Map<URI, DartUnit> parsedUnits) {
    ErrorListener errorListener = new ErrorListener(server);

    File libraryFile = library.getFile();
    LibrarySource librarySource = library.getLibrarySource();
    provider.clearCachedArtifacts();

    Map<URI, LibraryUnit> newlyResolved = null;
    try {
      newlyResolved =
          DartCompiler.analyzeLibraries(
              librarySource, resolvedLibs, parsedUnits, config, provider, errorListener);
    } catch (IOException e) {
      errorListener.onError(newIoError(librarySource, e));
    } catch (Throwable e) {
      DartCore.logError("Exception while resolving " + libraryFile.getPath(), e);
      DartCompilationError error =
          new DartCompilationError(
              librarySource, AnalysisErrorCode.RESOLUTION_FAILURE, e.getMessage());
      error.setSource(librarySource);
      errorListener.onError(error);
    }

    if (newlyResolved != null) {
      notifyParsedDuringResolve(server, parsedUnits, newlyResolved.values(), errorListener);
      errorListener.notifyResolved(newlyResolved);
    } else {
      newlyResolved = new HashMap<URI, LibraryUnit>();
      newlyResolved.put(libraryFile.toURI(), new LibraryUnit(librarySource));
    }
    return newlyResolved;
  }
Esempio n. 5
0
  /** A synchronized call to {@link DartCompiler#analyzeLibrary} */
  public static LibraryUnit secureAnalyzeLibrary(
      LibrarySource librarySource,
      final Map<URI, DartUnit> parsedUnits,
      final CompilerConfiguration config,
      DartArtifactProvider provider,
      DartCompilerListener listener)
      throws IOException {

    long start = System.currentTimeMillis();

    if (!DartSdkManager.getManager().hasSdk()) {
      return null;
    }

    final PackageLibraryManager manager = PackageLibraryManagerProvider.getPackageLibraryManager();
    AnalysisServer server = PackageLibraryManagerProvider.getDefaultAnalysisServer();

    URI librarySourceUri = librarySource.getUri();

    if (parsedUnits == null && !(librarySource instanceof LibraryWithSuppliedSources)) {

      // Resolve dart:<libname> to file URI before calling AnalysisServer
      URI libraryFileUri = manager.resolveDartUri(librarySourceUri);
      File libraryFile = new File(libraryFileUri.getPath());

      LibraryUnit ret = server.getSavedContext().resolve(libraryFile, 30000);

      long elapsed = System.currentTimeMillis() - start;
      Instrumentation.metric("DartCompilerUtils-secureAnalyzeLibrary", elapsed).log();
      Instrumentation.operation("DartCompilerUtils-secureAnalyzeLibrary", elapsed)
          .with("librarySource.Name", librarySource.getName())
          .with("librarySource.LastModified", librarySource.getLastModified())
          .log();

      return ret;
    }

    // Resolve the specified library against all currently cached libraries
    final Map<URI, LibraryUnit> resolvedLibs = server.getSavedContext().getResolvedLibraries(50);
    resolvedLibs.remove(librarySourceUri);

    // Construct the selective cache
    SelectiveCache selectiveCache =
        new DartCompiler.SelectiveCache() {
          @Override
          public Map<URI, LibraryUnit> getResolvedLibraries() {
            return resolvedLibs;
          }

          @Override
          public DartUnit getUnresolvedDartUnit(DartSource dartSrc) {
            if (parsedUnits == null) {
              return null;
            }
            URI srcUri = dartSrc.getUri();
            DartUnit parsedUnit = parsedUnits.remove(srcUri);
            if (parsedUnit != null) {
              return parsedUnit;
            }
            URI fileUri = manager.resolveDartUri(srcUri);
            return parsedUnits.remove(fileUri);
          }
        };

    Map<URI, LibraryUnit> libMap;
    // All calls to DartC must be synchronized
    synchronized (compilerLock) {
      libMap =
          DartCompiler.analyzeLibraries(
              librarySource, selectiveCache, config, provider, listener, false);
    }

    long elapsed = System.currentTimeMillis() - start;
    Instrumentation.metric("DartCompilerUtils-secureAnalyzeLibrary", elapsed)
        .with("libMapHasResult", Boolean.toString(libMap != null))
        .log();

    Instrumentation.operation("DartCompilerUtils-secureAnalyzeLibrary", elapsed)
        .with("librarySource.Name", librarySource.getName())
        .with("libMapHasResult", Boolean.toString(libMap != null))
        .with("librarySource.LastModified", librarySource.getLastModified())
        .log();

    return libMap != null ? libMap.get(librarySourceUri) : null;
  }
Esempio n. 6
0
    @Override
    public void run() throws Exception {
      final PackageLibraryManager libraryManager =
          PackageLibraryManagerProvider.getPackageLibraryManager();
      final LibraryElement enclosingLibrary =
          cachedLibraries.get(librarySource.wrappedSource).getElement();

      // Try to find the core library in the enclosing set of libraries, otherwise the typeAnalyzer
      // will be void of core types.
      LibraryUnit coreUnit;
      // All calls to DartC must be synchronized
      synchronized (compilerLock) {
        coreUnit = DartCompiler.getCoreLib(enclosingLibrary.getLibraryUnit());
      }
      if (coreUnit == null) {
        throw new RuntimeException("Unable to locate core library");
      }
      LibraryElement coreLibrary = coreUnit.getElement();

      SourceDelta delta =
          new SourceDelta() {

            @Override
            public DartSource getSourceAfter() {
              return source;
            }

            @Override
            public Source getSourceBefore() {
              for (DartUnit u : enclosingLibrary.getLibraryUnit().getUnits()) {
                Source unitSource = u.getSourceInfo().getSource();
                if (unitSource.getUri().equals(unitUri)) {
                  return unitSource;
                }
              }
              return null;
            }

            @Override
            public DartUnit getUnitAfter() {
              return parsedUnit;
            }
          };
      final CompilerConfiguration config =
          new DefaultCompilerConfiguration(DartCompilerUtilities.COMPILER_OPTIONS, libraryManager) {

            @Override
            public boolean incremental() {
              return true;
            }

            @Override
            public boolean resolveDespiteParseErrors() {
              return true;
            }

            @Override
            public boolean typeErrorsAreFatal() {
              return false;
            }

            @Override
            public boolean warningsAreFatal() {
              return false;
            }
          };
      // All calls to DartC must be synchronized
      synchronized (compilerLock) {
        analyzedNode =
            DartCompiler.analyzeDelta(
                delta,
                enclosingLibrary,
                coreLibrary,
                completionNode,
                completionLocation,
                0,
                config,
                this);
      }
    }