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.
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
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
The easiest way to learn about extending Starlark with custom built-ins is to look into the tests directory.
Start by examining Examples
:
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
/** 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!")
If you want to read more about the specification, please visit the official Starlark specification
- Go to Project Structure.
- Under
Project Settings
, go toModules
. - Find
libstarlark
and click on thePaths
tab. - Under
Compiler Output
, selectUse module compile output path
. Replace thetarget/classes
in output path tosrc/main/java
.Replace thetarget/test-classes
in test output path tosrc/test/java
.
You can now run the tests from IntelliJ.