Class StarlarkScript
StarlarkThread, and
functions cannot change global state. Calls are bounded by a step count, a wall-clock timeout
and an allocation budget (StarlarkScript.Limits).
Each call is measured: its interpreter steps, about how many bytes of values it created
(StarlarkThread.getAllocatedBytes(), an estimate of what was made rather than of what
stayed in memory) and its time. With metrics they go to
hooks.starlark.<script>.<hook>.*, tagged with the script's name and
the hook; they add up per exchange in FlowContext.scriptStats(); a ScriptCallObserver sees each call; and a call for an exchange whose trace is sampled gets a
span of its own, a child of the exchange's (starlark <hook>, see ScriptSpans).
A script may load() modules of the standard library ported from starlarky (Stdlib), e.g. load("@stdlib//urllib/parse", "parse"). Such a script runs with Python's
string semantics (Stdlib.withPythonStrings(org.microproxy.thirdparty.starlark.eval.StarlarkSemantics)), which the library relies on; a script without
load statements runs exactly as before.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordBounds on each call into a script (and on running its top level). -
Method Summary
Modifier and TypeMethodDescriptioncall(FlowContext flow, String function, org.microproxy.thirdparty.starlark.eval.Mutability mu, Object... args) Callsfunctionwithargsfor the exchangeflow.static StarlarkScriptcompile(String source, String name, StarlarkScript.Limits limits) Compiles and runssource;nameappears in error messages.static StarlarkScriptCompiles and runssourcewithconstantsas read-only globals (seeScriptedProxy.Builder.constants);nameappears in error messages.static StarlarkScriptcompile(String source, String name, StarlarkScript.Limits limits, Map<String, ?> constants, StatsRegistry stats) Compiles and runssourcewithconstantsas read-only globals, itsstatsmodule counting intostats(ScriptStats);nameappears in error messages.booleanWhether the script defines a functionfunction.The value of the globalname(frozen), ornullwhen the script sets none.static StarlarkScriptload(Path file, StarlarkScript.Limits limits) Loads and runs the script infile.static StarlarkScriptload(Path file, StarlarkScript.Limits limits, Map<String, ?> constants) Loads and runs the script infile, withconstantsas read-only globals (seeScriptedProxy.Builder.constants).static StarlarkScriptload(Path file, StarlarkScript.Limits limits, Map<String, ?> constants, StatsRegistry stats) Loads and runs the script infile, withconstantsas read-only globals and itsstatsmodule counting intostats.name()The name given at compile time (the file path for loaded scripts).Returns the protobuf schema the script decodes with, ornull.The name this script's stats are tagged with (microproxy_script): its file name without.star, or the one given towithStatsName(java.lang.String).toString()withObserver(ScriptCallObserver observer) This script withobserverseeing every call into it.withProtoSchema(ProtoSchema schema) This script with a protobuf schema for itsprotobufandgrpcmodules, so thatprotobuf.decode(data, type="pkg.Message")decodes with field names (andencodetakes them).withStatsName(String name) This script with its stats taggednamerather than after its file.
-
Method Details
-
statsName
The name this script's stats are tagged with (microproxy_script): its file name without.star, or the one given towithStatsName(java.lang.String).- Returns:
- the stats name
-
withStatsName
This script with its stats taggednamerather than after its file. The script itself is shared, not run again.- Parameters:
name- the name, made fit for a stat name asTagExtractor.sanitize(java.lang.String)does- Returns:
- the script with the name
-
withObserver
This script withobserverseeing every call into it. The script itself is shared, not run again.- Parameters:
observer- the observer, or null for none- Returns:
- the script with the observer
-
withProtoSchema
This script with a protobuf schema for itsprotobufandgrpcmodules, so thatprotobuf.decode(data, type="pkg.Message")decodes with field names (andencodetakes them). The script itself is shared, not run again.- Parameters:
schema- the schema, ornullfor none (the well-known types only)- Returns:
- the script with the schema
-
protoSchema
Returns the protobuf schema the script decodes with, ornull.- Returns:
- the protobuf schema the script decodes with, or
null
-
load
public static StarlarkScript load(Path file, StarlarkScript.Limits limits) throws IOException, ScriptException Loads and runs the script infile.- Parameters:
file- the UTF-8 script filelimits- the bounds for top-level execution and later calls- Returns:
- the compiled script with frozen globals
- Throws:
IOException- if the file cannot be readScriptException- if parsing, compilation or top-level execution fails
-
load
public static StarlarkScript load(Path file, StarlarkScript.Limits limits, Map<String, ?> constants) throws IOException, ScriptExceptionLoads and runs the script infile, withconstantsas read-only globals (seeScriptedProxy.Builder.constants).- Parameters:
file- the UTF-8 script filelimits- the bounds for top-level execution and later callsconstants- the globals to copy and freeze before compilation- Returns:
- the compiled script with frozen globals
- Throws:
IOException- if the file cannot be readScriptException- if parsing, compilation or top-level execution failsIllegalArgumentException- if a constant name or value is invalid
-
load
public static StarlarkScript load(Path file, StarlarkScript.Limits limits, Map<String, ?> constants, StatsRegistry stats) throws IOException, ScriptExceptionLoads and runs the script infile, withconstantsas read-only globals and itsstatsmodule counting intostats.- Parameters:
file- the UTF-8 script filelimits- the bounds for top-level execution and later callsconstants- the globals to copy and freeze before compilationstats- the registry of the script'sstats, ornullfor metrics that count nothing- Returns:
- the compiled script with frozen globals
- Throws:
IOException- if the file cannot be readScriptException- if parsing, compilation or top-level execution failsIllegalArgumentException- if a constant name or value is invalid
-
compile
public static StarlarkScript compile(String source, String name, StarlarkScript.Limits limits) throws ScriptException Compiles and runssource;nameappears in error messages.- Parameters:
source- the Starlark source to compilename- the script name used in diagnosticslimits- the bounds for top-level execution and later calls- Returns:
- the compiled script with frozen globals
- Throws:
ScriptException- if parsing, compilation or top-level execution fails
-
compile
public static StarlarkScript compile(String source, String name, StarlarkScript.Limits limits, Map<String, ?> constants) throws ScriptExceptionCompiles and runssourcewithconstantsas read-only globals (seeScriptedProxy.Builder.constants);nameappears in error messages.- Parameters:
source- the Starlark source to compilename- the script name used in diagnosticslimits- the bounds for top-level execution and later callsconstants- the globals to copy and freeze before compilation- Returns:
- the compiled script with frozen globals
- Throws:
ScriptException- if parsing, compilation or top-level execution failsIllegalArgumentException- for constants that are not valid (seeScriptedProxy.Builder.constants)
-
compile
public static StarlarkScript compile(String source, String name, StarlarkScript.Limits limits, Map<String, ?> constants, StatsRegistry stats) throws ScriptExceptionCompiles and runssourcewithconstantsas read-only globals, itsstatsmodule counting intostats(ScriptStats);nameappears in error messages.- Parameters:
source- the Starlark source to compilename- the script name used in diagnosticslimits- the bounds for top-level execution and later callsconstants- the globals to copy and freeze before compilationstats- the registry of the script'sstats, ornullfor metrics that count nothing- Returns:
- the compiled script with frozen globals
- Throws:
ScriptException- if parsing, compilation or top-level execution failsIllegalArgumentException- for constants that are not valid (seeScriptedProxy.Builder.constants)
-
name
The name given at compile time (the file path for loaded scripts).- Returns:
- the diagnostic name of this script
-
defines
Whether the script defines a functionfunction.- Parameters:
function- the global name to inspect- Returns:
- whether that global is callable
-
global
The value of the globalname(frozen), ornullwhen the script sets none.- Parameters:
name- the global name to look up- Returns:
- the frozen global value, or null if absent
-
call
public Object call(String function, org.microproxy.thirdparty.starlark.eval.Mutability mu, Object... args) throws org.microproxy.thirdparty.starlark.eval.EvalException, InterruptedException - Parameters:
function- the global function namemu- the owner of values created by this callargs- the positional Starlark arguments- Returns:
- the function's Starlark result
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if the function is absent, evaluation fails or an execution limit is reachedInterruptedException- if the calling thread is interrupted
-
call
public Object call(FlowContext flow, String function, org.microproxy.thirdparty.starlark.eval.Mutability mu, Object... args) throws org.microproxy.thirdparty.starlark.eval.EvalException, InterruptedException Callsfunctionwithargsfor the exchangeflow. Values the call creates belong tomu, so several calls for one request can share state through it. The call is measured (steps, bytes of values created, time): with metrics, inhooks.starlark.<script>.<function>.*; inflow's totals; and by the observer. Without any of them, and without a timeout, no clock is read.- Parameters:
flow- the exchange the call is for, or nullfunction- the global function namemu- the owner of values created by this callargs- the positional Starlark arguments- Returns:
- the function's Starlark result
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if the function is absent, evaluation fails or an execution limit is reachedInterruptedException- if the calling thread is interrupted
-
toString
-