-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_runtime.go
More file actions
94 lines (79 loc) · 3.16 KB
/
script_runtime.go
File metadata and controls
94 lines (79 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package runtime
import "context"
// ScriptRuntime defines the interface for external script execution engines.
// Implementations live in go-json-runtimes package (or any custom implementation).
// go-json core defines this interface but never imports implementations.
type ScriptRuntime interface {
// Name returns the runtime identifier (e.g., "python", "node", "goja").
Name() string
// Extensions returns file extensions this runtime can handle (e.g., [".py", ".pyw"]).
Extensions() []string
// CanHandle reports whether this runtime can execute scripts with the given extension.
CanHandle(extension string) bool
// Execute runs a script file with the given parameters and bridge.
// - ctx: execution context (for cancellation/timeout)
// - script: path to the script file (absolute or relative to working dir)
// - function: function name to call within the script (empty = execute entire script)
// - params: input parameters passed to the script as JSON-compatible map
// - bridge: host functions exposed to the script (map[string]any, not typed)
// Returns the script's return value (JSON-compatible) or error.
Execute(ctx context.Context, script string, function string, params map[string]any, bridge map[string]any) (any, error)
// Validate checks if the runtime is available (e.g., Python installed, correct version).
// Returns nil if ready, descriptive error if not.
Validate() error
// Close releases all resources held by this runtime (process pools, VMs, etc.).
Close() error
}
// ScriptRuntimeRegistry manages multiple ScriptRuntime implementations.
// It provides resolution by file extension or by name.
type ScriptRuntimeRegistry struct {
runtimes []ScriptRuntime
}
// NewScriptRuntimeRegistry creates an empty registry.
func NewScriptRuntimeRegistry() *ScriptRuntimeRegistry {
return &ScriptRuntimeRegistry{}
}
// Register adds a runtime to the registry.
// Runtimes are resolved in registration order — first match wins.
func (r *ScriptRuntimeRegistry) Register(rt ScriptRuntime) {
r.runtimes = append(r.runtimes, rt)
}
// Resolve finds a runtime that can handle the given file extension.
// Returns nil if no runtime can handle it.
func (r *ScriptRuntimeRegistry) Resolve(extension string) ScriptRuntime {
for _, rt := range r.runtimes {
if rt.CanHandle(extension) {
return rt
}
}
return nil
}
// ResolveByName finds a runtime by name.
// Returns nil if no runtime with that name is registered.
func (r *ScriptRuntimeRegistry) ResolveByName(name string) ScriptRuntime {
for _, rt := range r.runtimes {
if rt.Name() == name {
return rt
}
}
return nil
}
// All returns all registered runtimes.
func (r *ScriptRuntimeRegistry) All() []ScriptRuntime {
return r.runtimes
}
// Close closes all registered runtimes, releasing their resources.
// Returns the first error encountered (but still attempts to close all).
func (r *ScriptRuntimeRegistry) Close() error {
var firstErr error
for _, rt := range r.runtimes {
if err := rt.Close(); err != nil && firstErr == nil {
firstErr = err
}
}
return firstErr
}
// HasRuntimes reports whether any runtimes are registered.
func (r *ScriptRuntimeRegistry) HasRuntimes() bool {
return len(r.runtimes) > 0
}