This provides Starlark as a standalone java library, distinct from Bazel. There is no API stability guarantee, so do exercise caution if you decide to use this library.
This library is published to Github Packages, you can add it in your project fairly easily.
repositories {
maven {
url = uri("https://maven.pkg.github.com/cyberdelia/starlark")
credentials {
username = project.findProperty("gpr.user") as String? ?: System.getenv("GITHUB_USERNAME")
password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN")
}
}
}
dependencies {
implementation("com.lapanthere:starlark:5.3.2")
}
You can also refer to Github documentation for further details.
<dependencies>
<dependency>
<groupId>com.lapanthere</groupId>
<artifactId>starlark</artifactId>
<version>5.3.2</version>
</dependency>
</dependencies>
You can also refer to Github documentation for further details.
To embed and use the interpreter in your program:
StarlarkSemantics semantics = StarlarkSemantics.DEFAULT;
try (Mutability mu = Mutability.create("scope")) {
StarlarkThread thread = new StarlarkThread(mu, semantics);
Module module = Module.withPredeclared(semantics, new HashMap<>());
Program program = Program.compileExpr(Expression.parse(ParserInput.fromLines("print('hello')")), module, FileOptions.DEFAULT);
Starlark.execFileProgram(program, module, thread);
}
Mutability.create("scope").use { mu ->
val semantics = StarlarkSemantics.DEFAULT
val thread = StarlarkThread(mu, semantics)
val module = Module.withPredeclared(semantics, emptyMap())
val program =
Program.compileExpr(Expression.parse(ParserInput.fromLines("""print("hello")""")), module, FileOptions.DEFAULT)
Starlark.execFileProgram(program, module, thread)
}