You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* major reorg
* rename the EvalWithPrep to just Evaluator
* update docs and other references after big reorg
* relocate again, replace 'abstract' package with 'platform' and move evaluator to the root of platform
* add a workaround script to fix the benchmark, need to review the input_data prefix later
* add new benchmark result
* fix and relocate benchmarks
A Go package providing a unified interface for loading and running various scripting languages and WebAssembly modules in your Go applications.
8
+
A Go package providing a unified interface for loading and running various scripting languages and WASM in your app.
9
9
10
10
## Overview
11
11
12
-
go-polyscript provides a consistent API across different scripting engines, allowing for easy interchangeability and minimizing lock-in to a specific scripting language. This package provides low-overhead abstractions of "machines," "executables," and the final "result". The API for the input/output and runtime are all standardized, which simplifies combining or swapping scripting engines in your application.
13
-
14
-
Currently supported scripting engines "machines":
15
-
16
-
-**Risor**: A simple scripting language specifically designed for embedding in Go applications
17
-
-**Starlark**: Google's configuration language (a Python dialect) used in Bazel and many other tools
18
-
-**Extism**: WebAssembly runtime and plugin system for executing WASM modules
12
+
go-polyscript democratizes different scripting engines by abstracting the loading, data handling, runtime, and results handling, allowing for interchangeability of scripting languages. This package provides interfaces and implementations for "engines", "executables", "evaluators" and the final "result". There are several tiers of public APIs, each with increasing complexity and configurability. `polyscript.go` in the root exposes the most common use cases, but is also the most opiniated.
19
13
20
14
## Features
21
15
22
-
-**Unified API**: Common interfaces for all supported scripting languages
16
+
-**Unified API**: Common interfaces and implementations for several scripting languages
23
17
-**Flexible Engine Selection**: Easily switch between different script engines
24
18
-**Thread-safe Data Management**: Multiple ways to provide input data to scripts
25
19
-**Compilation and Evaluation Separation**: Compile once, run multiple times with different inputs
26
20
-**Data Preparation and Evaluation Separation**: Prepare data in one step/system, evaluate in another
27
-
-**Slog Logging**: Customizable structured logging with `slog`
21
+
22
+
## Engines Implemented
23
+
24
+
-**Risor**: A simple scripting language specifically designed for embedding in Go applications
25
+
-**Starlark**: Google's configuration language (a Python dialect) used in Bazel and many other tools
26
+
-**Extism**: Pure Go runtime and plugin system for executing WASM
28
27
29
28
## Installation
30
29
@@ -49,140 +48,110 @@ import (
49
48
)
50
49
51
50
funcmain() {
52
-
// Create a logger
53
-
handler:= slog.NewTextHandler(os.Stdout, nil)
54
-
logger:= slog.New(handler)
51
+
logHandler:= slog.NewTextHandler(os.Stdout, nil)
55
52
56
-
// Script content
57
-
scriptContent:=`
53
+
script:=`
58
54
// Script has access to ctx variable passed from Go
go-polyscript uses data providers to supply information to scripts during evaluation. Depending on your use case, you can choose from several built-in providers or combine them for more flexibility.
91
+
go-polyscript enables you to send input data using a system called "data providers". There are several built-in providers, and you can implement your own or stack multiple with the `CompositeProvider`.
99
92
100
93
### StaticProvider
101
94
102
-
The `StaticProvider` supplies fixed data for all evaluations. This is ideal for scenarios where the input data remains constant across evaluations:
95
+
The `FromRisorStringWithData` function uses a `StaticProvider` to send the static data map.
However, when using `StaticProvider`, each evaluation will always use the same input data. If you need to provide dynamic runtime data that varies per evaluation, you can use the `ContextProvider`.
116
103
117
104
### ContextProvider
118
105
119
-
The `ContextProvider` retrieves dynamic data from the context and makes it available to scripts. This is useful for scenarios where input data changes at runtime:
106
+
The `ContextProvider` retrieves dynamic data from the context object sent to Eval. This is useful when input data changes at runtime:
//In scripts, dynamic data is accessed via input_data:
137
-
// userId := ctx["input_data"]["userId"] // 123
115
+
//Execute with the "enriched" context containing the link to the input data
116
+
result, _:=evaluator.Eval(enrichedCtx)
138
117
```
139
118
140
-
### Combining Static and Dynamic Data
119
+
### Combining Static and Dynamic Runtime Data
141
120
142
-
go-polyscript makes it easy to combine static configuration data with dynamic request data. This is a common pattern where you want both fixed configuration values and per-request variable data to be available during evaluation:
121
+
This is a common pattern where you want both fixed configuration values and threadsafe per-request data to be available during evaluation:
This pattern ensures that your scripts have access to both constant configuration values and per-evaluation runtime data, making your evaluations more flexible and powerful.
173
-
174
144
## Architecture
175
145
176
146
go-polyscript is structured around a few key concepts:
177
147
178
-
1.**Loader**: Loads script content from various sources (files, strings, http, etc.)
148
+
1.**Loader**: Loads script content from various sources (disk, `io.Reader`, strings, http, etc.)
179
149
2.**Compiler**: Validates and compiles scripts into internal "bytecode"
180
-
3.**ExecutableUnit**: Represents a compiled script ready for execution
181
-
4.**Evaluator**: Executes compiled scripts with provided input data
182
-
5.**EvalDataPreparer**: Prepares data for evaluation (can be separated from evaluation)
183
-
6.**Provider**: Supplies data to scripts during evaluation
184
-
7.**Machine**: A specific implementation of a scripting engine (Risor, Starlark, Extism)
185
-
8.**EvaluatorResponse**: The response object returned from all **Machine**s
150
+
3.**ExecutableUnit**: Compiled script bundle, ready for execution
151
+
4.**Engine**: A specific implementation of a scripting engine (Risor, Starlark, Extism)
152
+
5.**Evaluator**: Executes compiled scripts with provided input data
153
+
6.**DataProvider**: Sends data to the VM prior to evaluation
154
+
7.**EvaluatorResponse**: The response object returned from all **Engine**s
186
155
187
156
### Note on Data Access Patterns
188
157
@@ -194,39 +163,12 @@ go-polyscript uses a unified `Provider` interface to supply data to scripts. The
194
163
195
164
See the [Data Providers](#working-with-data-providers) section for more details.
196
165
197
-
## Preparing Data Separately from Evaluation
166
+
## Other Engines
198
167
199
-
go-polyscript provides the `EvalDataPreparer` interface to separate data preparation from script evaluation, which is useful for distributed architectures and multi-step data processing:
168
+
### Starlark
169
+
Starlark syntax is a deterministic "python like" language designed for complex configuration, not so much for dynamic scripting. It's high performance, but the capabilities of the language are very limited. Read more about it here: [Starlark-Go](https://github.com/google/starlark-go)
200
170
201
171
```go
202
-
// Create an evaluator (implements EvaluatorWithPrep interface)
Extism uses the Wazero WASM runtime for providing WASI abstractions, and an easy input/output memory sharing data system. Read more about writing WASM plugins for the Extism/Wazero runtime using the Extism PDK here: [extism.org](https://extism.org/docs/concepts/pdk)
0 commit comments