bazel syntax: disentangle StarlarkThread and Module

New interpreter API (if you don't use a helper function):

To call a function:

   try (Mutability mu = Mutability.create("myexpr")) {
      StarlarkThread thread = new StarlarkThread(mu, semantics);
      return Starlark.call(thread, fn, args, kwargs);
   } catch (EvalException ex) {
       ...
   }

To execute a file:

   StarlarkFile file = ...
   Module module = Module.create(); // default environment
   Resolver.resolve(file, module);
   try (Mutability mu = Mutability.create("myfile")) {
      StarlarkThread thread = new StarlarkThread(mu, semantics);
      Starlark.exec(file, thread, module);
   } catch (EvalException ex) {
       ...
   }
   // Inv: module contains globals

Overview of change:

- Eliminate the concept of "a Starlark thread's module".
  A module is rightly associated with a Starlark function, not a thread.
  Consider a thread used just to call an existing function value, for example.
  (Previously, a module would always have been created even if unused.)

- Modules are now created explicitly, from a predeclared environment
  and a semantics, which is used for filtering but not retained.
  Modules can now be created before threads---the logical order.
  This simplifies a number of clients.

- Flatten Module. It is no longer a linked list. It contains only
   (predeclared, globals, clientData),
  and exportedGlobals which will go away soon.

- Simplify processing of FlagGuardedValues. They are either unwrapped
  (if enabled by semantics) or left as is, if disabled.
  This means they are visible through Module.getPredeclared.

- Delete Module.mutability. It is inessential and raises
  questions of consistency with StarlarkThread.
  What really matters is whether a module's global values are mutable.

- Delete StarlarkThread.Builder. A simple constructor now suffices:
   new StarlarkThread(Mutability, StarlarkSemantics).

- EvaluationTestCase now exposes two hooks for Module and Thread creation
  so that tests can predeclare bindings, set client data, and insert
  thread local values.  Creation of Module and Thread is now fully lazy.
  A follow-up change will eliminate the regrettable use of inheritance.

Also:

- Move ModuleCodec into Module, so that we don't need to harm its API.

- Use separate UNIVERSE and predeclared buckets in Module.
  The UNIVERSE is always implicitly available.
  The API doesn't fully separate them yet (needs Resolver work),
  but this should reduce the amount of map copying and redundant
  specification.

- Add more pre-evaluated expressions to ParamDescriptor.evalDefault
  so that we can bootstrap all the @Param annotation's default values
  used by Starlark.UNIVERSE without JVM deadlock. This breaks a cyclic
  dependency between the evaluator and UNIVERSE.

- Use composition not inheritance of EvaluationTestCase in more tests.

This is my 6th attempt at this change in as many months.

This is a breaking API change for Copybara.

PiperOrigin-RevId: 312284294
44 files changed
tree: b32b1f0d9eb62b3952243c790af901fcb4ea2dad
  1. .bazelci/
  2. examples/
  3. scripts/
  4. site/
  5. src/
  6. third_party/
  7. tools/
  8. .bazelrc
  9. .gitattributes
  10. .gitignore
  11. AUTHORS
  12. BUILD
  13. CHANGELOG.md
  14. CODEOWNERS
  15. combine_distfiles.py
  16. combine_distfiles_to_tar.sh
  17. compile.sh
  18. CONTRIBUTING.md
  19. CONTRIBUTORS
  20. distdir.bzl
  21. ISSUE_TEMPLATE.md
  22. LICENSE
  23. README.md
  24. WORKSPACE
README.md

Bazel

{Fast, Correct} - Choose two

Build and test software of any size, quickly and reliably.

  • Speed up your builds and tests: Bazel rebuilds only what is necessary. With advanced local and distributed caching, optimized dependency analysis and parallel execution, you get fast and incremental builds.

  • One tool, multiple languages: Build and test Java, C++, Android, iOS, Go, and a wide variety of other language platforms. Bazel runs on Windows, macOS, and Linux.

  • Scalable: Bazel helps you scale your organization, codebase, and continuous integration solution. It handles codebases of any size, in multiple repositories or a huge monorepo.

  • Extensible to your needs: Easily add support for new languages and platforms with Bazel's familiar extension language. Share and re-use language rules written by the growing Bazel community.

Getting Started

Documentation

Contributing to Bazel

See CONTRIBUTING.md

Build status