Java Code Examples for com.google.devtools.build.lib.syntax.EvalUtils#exec()

The following examples show how to use com.google.devtools.build.lib.syntax.EvalUtils#exec() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThreadHandler.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Executes the Starlark statements code in the environment defined by the provided {@link
 * StarlarkThread}. If the last statement is an expression, doEvaluate returns its value,
 * otherwise it returns null.
 *
 * <p>The caller is responsible for ensuring that the associated Starlark thread isn't currently
 * running.
 */
private Object doEvaluate(StarlarkThread thread, String content)
    throws SyntaxError.Exception, EvalException, InterruptedException {
  try {
    servicingEvalRequest.set(true);

    // TODO(adonovan): opt: don't parse and resolve the expression every time we hit a breakpoint
    // (!).
    ParserInput input = ParserInput.create(content, "<debug eval>");
    // TODO(adonovan): the module or call frame should be a parameter.
    Module module = Module.ofInnermostEnclosingStarlarkFunction(thread);
    return EvalUtils.exec(input, FileOptions.DEFAULT, module, thread);
  } finally {
    servicingEvalRequest.set(false);
  }
}
 
Example 2
Source File: StarlarkDebugServerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and starts a worker thread parsing, resolving, and executing the given Starlark file to
 * populate the specified module, or if none is given, in a fresh module with a default
 * environment.
 */
private static Thread execInWorkerThread(ParserInput input, @Nullable Module module) {
  Thread javaThread =
      new Thread(
          () -> {
            try (Mutability mu = Mutability.create("test")) {
              StarlarkThread thread = new StarlarkThread(mu, StarlarkSemantics.DEFAULT);
              EvalUtils.exec(
                  input, FileOptions.DEFAULT, module != null ? module : Module.create(), thread);
            } catch (SyntaxError.Exception | EvalException | InterruptedException ex) {
              throw new AssertionError(ex);
            }
          });
  javaThread.start();
  return javaThread;
}
 
Example 3
Source File: SkydocMain.java    From bazel with Apache License 2.0 5 votes vote down vote up
/** Evaluates the AST from a single Starlark file, given the already-resolved imports. */
private static Module evalStarlarkBody(
    StarlarkSemantics semantics,
    StarlarkFile file,
    Map<String, Module> imports,
    List<RuleInfoWrapper> ruleInfoList,
    List<ProviderInfoWrapper> providerInfoList,
    List<AspectInfoWrapper> aspectInfoList)
    throws InterruptedException, StarlarkEvaluationException {

  Module module =
      Module.withPredeclared(
          semantics, getPredeclaredEnvironment(ruleInfoList, providerInfoList, aspectInfoList));

  Resolver.resolveFile(file, module);
  if (!file.ok()) {
    throw new StarlarkEvaluationException(file.errors().get(0).toString());
  }

  // execute
  try (Mutability mu = Mutability.create("Skydoc")) {
    StarlarkThread thread = new StarlarkThread(mu, semantics);
    // We use the default print handler, which writes to stderr.
    thread.setLoader(imports::get);

    EvalUtils.exec(file, module, thread);
  } catch (EvalException | InterruptedException ex) {
    // This exception class seems a bit unnecessary. Replace with EvalException?
    throw new StarlarkEvaluationException("Starlark evaluation error", ex);
  }
  return module;
}
 
Example 4
Source File: StarlarkRepositoryContextTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static Object execAndEval(String... lines) {
  try {
    return EvalUtils.exec(
        ParserInput.fromLines(lines), FileOptions.DEFAULT, Module.create(), thread);
  } catch (Exception ex) { // SyntaxError | EvalException | InterruptedException
    throw new AssertionError("exec failed", ex);
  }
}
 
Example 5
Source File: AllocationTrackerTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
private void exec(String... lines)
    throws SyntaxError.Exception, EvalException, InterruptedException {
  ParserInput input = ParserInput.create(Joiner.on("\n").join(lines), "a.star");
  Module module =
      Module.withPredeclared(
          StarlarkSemantics.DEFAULT,
          ImmutableMap.of(
              "sample", new SamplerValue(),
              "myrule", new MyRuleFunction()));
  try (Mutability mu = Mutability.create("test")) {
    StarlarkThread thread = new StarlarkThread(mu, StarlarkSemantics.DEFAULT);
    EvalUtils.exec(input, FileOptions.DEFAULT, module, thread);
  }
}
 
Example 6
Source File: WorkspaceFactory.java    From bazel with Apache License 2.0 4 votes vote down vote up
private void execute(
    StarlarkFile file,
    Map<String, Module> additionalLoadedModules,
    StoredEventHandler localReporter,
    WorkspaceFileValue.WorkspaceFileKey workspaceFileKey)
    throws InterruptedException {
  loadedModules.putAll(additionalLoadedModules);

  // set up predeclared environment
  HashMap<String, Object> predeclared = new HashMap<>();
  predeclared.putAll(getDefaultEnvironment());
  predeclared.putAll(bindings); // (may shadow bindings in default environment)
  Module module = Module.withPredeclared(starlarkSemantics, predeclared);

  // resolve
  Resolver.resolveFile(file, module);

  // create thread
  StarlarkThread thread = new StarlarkThread(mutability, starlarkSemantics);
  thread.setLoader(loadedModules::get);
  thread.setPrintHandler(Event.makeDebugPrintHandler(localReporter));
  thread.setThreadLocal(
      PackageFactory.PackageContext.class,
      new PackageFactory.PackageContext(builder, null, localReporter));

  // The workspace environment doesn't need the tools repository or the fragment map
  // because executing workspace rules happens before analysis and it doesn't need a
  // repository mapping because calls to the Label constructor in the WORKSPACE file
  // are, by definition, not in an external repository and so they don't need the mapping
  new BazelStarlarkContext(
          BazelStarlarkContext.Phase.WORKSPACE,
          /*toolsRepository=*/ null,
          /*fragmentNameToClass=*/ null,
          /*repoMapping=*/ ImmutableMap.of(),
          new SymbolGenerator<>(workspaceFileKey),
          /*analysisRuleLabel=*/ null)
      .storeInThread(thread);

  List<String> globs = new ArrayList<>(); // unused
  if (!file.ok()) {
    Event.replayEventsOn(localReporter, file.errors());
  } else if (PackageFactory.checkBuildSyntax(
      file, globs, globs, new HashMap<>(), localReporter)) {
    try {
      EvalUtils.exec(file, module, thread);
    } catch (EvalException ex) {
      localReporter.handle(Event.error(ex.getLocation(), ex.getMessage()));
    }
  }

  // Accumulate the global bindings created by this chunk of the WORKSPACE file,
  // for use in the next chunk. This set does not include the bindings
  // added by getDefaultEnvironment; but it does include bindings created by load,
  // so we will need to set the legacy load-binds-globally flag for this file in due course.
  this.bindings.putAll(module.getGlobals());

  builder.addPosts(localReporter.getPosts());
  builder.addEvents(localReporter.getEvents());
  if (localReporter.hasErrors()) {
    builder.setContainsErrors();
  }
  localReporter.clear();
}
 
Example 7
Source File: ResolvedFileFunction.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public SkyValue compute(SkyKey skyKey, Environment env)
    throws InterruptedException, SkyFunctionException {

  ResolvedFileKey key = (ResolvedFileKey) skyKey;
  StarlarkSemantics starlarkSemantics = PrecomputedValue.STARLARK_SEMANTICS.get(env);
  if (starlarkSemantics == null) {
    return null;
  }
  FileValue fileValue = (FileValue) env.getValue(FileValue.key(key.getPath()));
  if (fileValue == null) {
    return null;
  }
  try {
    if (!fileValue.exists()) {
      throw new ResolvedFileFunctionException(
          new NoSuchThingException("Specified resolved file '" + key.getPath() + "' not found."));
    } else {
      // read
      byte[] bytes =
          FileSystemUtils.readWithKnownFileSize(
              key.getPath().asPath(), key.getPath().asPath().getFileSize());

      // parse
      StarlarkFile file =
          StarlarkFile.parse(ParserInput.create(bytes, key.getPath().asPath().toString()));
      if (!file.ok()) {
        Event.replayEventsOn(env.getListener(), file.errors());
        throw resolvedValueError("Failed to parse resolved file " + key.getPath());
      }

      Module module = Module.create();

      // resolve
      Resolver.resolveFile(file, module);
      if (!file.ok()) {
        Event.replayEventsOn(env.getListener(), file.errors());
        throw resolvedValueError("Failed to validate resolved file " + key.getPath());
      }

      // execute
      try (Mutability mu = Mutability.create("resolved file", key.getPath())) {
        StarlarkThread thread = new StarlarkThread(mu, starlarkSemantics);
        EvalUtils.exec(file, module, thread);
      } catch (EvalException ex) {
        env.getListener().handle(Event.error(ex.getLocation(), ex.getMessage()));
        throw resolvedValueError("Failed to evaluate resolved file " + key.getPath());
      }
      Object resolved = module.getGlobal("resolved");
      if (resolved == null) {
        throw resolvedValueError(
            "Symbol 'resolved' not exported in resolved file " + key.getPath());
      }
      if (!(resolved instanceof List)) {
        throw resolvedValueError(
            "Symbol 'resolved' in resolved file " + key.getPath() + " not a list");
      }
      ImmutableList.Builder<Map<String, Object>> result
          = new ImmutableList.Builder<Map<String, Object>>();
      for (Object entry : (List) resolved) {
        if (!(entry instanceof Map)) {
          throw resolvedValueError(
              "Symbol 'resolved' in resolved file "
                  + key.getPath()
                  + " contains a non-map entry");
        }
        ImmutableMap.Builder<String, Object> entryBuilder
            = new ImmutableMap.Builder<String, Object>();
        for (Map.Entry<?, ?> keyValue : ((Map<?, ?>) entry).entrySet()) {
          Object attribute = keyValue.getKey();
          if (!(attribute instanceof String)) {
            throw resolvedValueError(
                "Symbol 'resolved' in resolved file "
                    + key.getPath()
                    + " contains a non-string key in one of its entries");
          }
          entryBuilder.put((String) attribute, keyValue.getValue());
        }
        result.add(entryBuilder.build());
      }
      return new ResolvedFileValue(result.build());
    }
  } catch (IOException e) {
    throw new ResolvedFileFunctionException(e);
  }
}
 
Example 8
Source File: EvaluationTestCase.java    From bazel with Apache License 2.0 4 votes vote down vote up
/** Joins the lines, parses them as a file, and executes it. */
public final void exec(String... lines)
    throws SyntaxError.Exception, EvalException, InterruptedException {
  ParserInput input = ParserInput.fromLines(lines);
  EvalUtils.exec(input, FileOptions.DEFAULT, getModule(), getStarlarkThread());
}