Skip to content

Latest commit

 

History

History
 
 

libstarlark

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Starlark

Starlark is a scripting language from Google that is mostly a subset of python. This package implements starlark as a library to allow embedded usage of Starlark in Very Good Security's Secure Compute environment.

Starlark is used as a transformation syntax and are about as close as one can get to the full power of a programming language for computing on data safely and securely. It allows Very Good Security to execute untrusted code, similar to how Serverless-like applications work, and controls language side effects.

Key Differences from Python

You can read more about the key differences here, but in short, the following is a general overview of differences from Python

  • No While Loops
  • No Recursion
  • Variables are frozen after mutation
  • Strings are not iterable

Differences between libstarlark and upstream bazelbuild

For all intents and purposes, any changes to libstarlark are kept to an absolute minimum.

This version contains a copy of a net.starlark.java from bazel's repository (revision 09c621e4cf5b968f4c6cdf905ab142d5961f9ddc).

The following is a complete list of changes applied to upstream. The list exists here to simplify sync with upstream:

  • 5f8103c: starlark: add 'bytes' data type, for binary strings
  • 396e243: Rename StarlarkByte => StarlarkBytes
  • 34abe6b: Introduced ByteStringModuleApi
  • d796c6f: Use StarlarkBytes and StarlarkByteArray instead of LarkyByte and LarkyByteArray
  • 1716e47: StarlarkBytes - General cleanup

Extending Starlark

The easiest way to learn about extending Starlark with custom built-ins is to look into the tests directory. Start by examining Examples:

py2star

We have developed a translating compiler (i.e. "transpiler") that can be used to migrate python libraries automatically to compensate for the differences between Python and Starlark. This should help migrate many external third-party python packages without too much effort.

py2star has a roadmap which includes a list of either completed or to-be-done tranforms.

More on py2star here: https://github.com/mahmoudimus/py2star More on migrating python to starlark/larky FAQs here: https://github.com/mahmoudimus/py2star/blob/main/MIGRATING.md

Extending Starlark from Java

/** This function shows how to construct a callable Starlark value from a Java method. */
import net.starlark.java.eval.Starlark;

ImmutableMap<String, Object> makeEnvironment(){
  ImmutableMap.Builder<String, Object> env=ImmutableMap.builder();
  env.put("zero",0);
  Starlark.addMethods(env,new MyFunctions(),StarlarkSemantics.DEFAULT); // adds 'square'
  return env.build();
  }

/**
 * The annotated methods of this class are added to the environment by {@link
 * Starlark#addMethods}.
 */
static final class MyFunctions {
  @StarlarkMethod(
    name = "square",
    parameters = {@Param(name = "x", type = int.class)},
    doc = "Returns the square of its integer argument.")
  public int square(int x) {
    return x * x;
  }
}

Then, you can:

if square(2) == 4:
    print("square function works!") 

Language Lawyers

If you want to read more about the specification, please visit the official Starlark specification

Contributing

IntelliJ Configuration

  1. Go to Project Structure.
  2. Under Project Settings, go to Modules.
  3. Find libstarlark and click on the Paths tab.
  4. Under Compiler Output, select Use module compile output path.
  5. Replace the target/classes in output path to src/main/java.
  6. Replace the target/test-classes in test output path to src/test/java.

You can now run the tests from IntelliJ.