Help us shape the future of Go! Take the 2026 Go Developer Survey

script_engine

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 8 Imported by: 0

README

go-scripts

go-scripts 是一个面向 Go 应用的嵌入式脚本框架。根模块定义统一的引擎能力接口、生命周期管理、引擎池和脚本源抽象;各语言引擎以独立子模块提供,通过空白导入自动注册到工厂。

当前仓库包含 9 个引擎实现:

子模块 类型 底层实现 适合场景
lua LuaType gopher-lua Lua 5.1 脚本、宿主模块、沙箱
javascript JavaScriptType goja / goja_nodejs JavaScript 规则和 CommonJS 模块
gpython GPythonType gpython Python 3.4 子集脚本
yaegi YaegiType Yaegi 动态 Go 代码和插件
wazero WazeroType wazero WebAssembly 模块
cel CELType cel-go 安全策略和条件表达式
expr ExprType expr-lang/expr 业务表达式和数据筛选
starlark StarlarkType starlark-go 封闭式构建规则和配置脚本
tcl TclType modernc.org/tcl Tcl 8.6 兼容脚本

快速开始

以 Lua 为例,先安装根模块和所需引擎:

go get github.com/liujitcn/go-scripts
go get github.com/liujitcn/go-scripts/lua

通过空白导入触发引擎注册,然后按统一接口初始化和执行:

package main

import (
	"context"
	"fmt"
	"log"

	scriptEngine "github.com/liujitcn/go-scripts"
	_ "github.com/liujitcn/go-scripts/lua"
)

func main() {
	ctx := context.Background()
	eng, err := scriptEngine.NewScriptEngine(scriptEngine.LuaType)
	if err != nil {
		log.Fatal(err)
	}
	defer eng.Close()

	if err = eng.Init(ctx); err != nil {
		log.Fatal(err)
	}
	if err = eng.RegisterGlobal("name", "Go"); err != nil {
		log.Fatal(err)
	}

	result, err := eng.ExecuteString(ctx, "hello.lua", `return "Hello, " .. name`)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(result)
}

不同引擎只需要替换子模块空白导入和类型常量,例如 JavaScriptType、CELType 或 WazeroType。

核心接口

根模块位于 github.com/liujitcn/go-scripts,公共能力按接口拆分:

接口 作用
ScriptEngine GetType、Init、Close、状态和错误管理
ScriptLoader 绑定 source.Reader,加载单个或多个脚本
ScriptExecutor 执行已加载脚本、按 key 执行或直接执行字符串
GlobalAccessor 注册和读取脚本全局变量
FunctionRegistrar 注册 Go 宿主函数、调用脚本函数
ModuleRegistrar 注册脚本模块
ScriptWatcher 监听脚本源变化并触发热更新
SandboxConfigurator 配置 Lua 等引擎的标准库白名单
RuntimeHookRegistrar 在运行时创建后注入宿主模块和回调

轻量引擎不一定实现全部能力。调用方可以使用 AsLoader、AsExecutor、AsWatcher 等辅助函数检查能力,而不必强制断言完整的 Engine 接口。

生命周期管理

根模块提供三种管理方式:

  • Manager:按名称注册多个引擎,统一执行 InitAll、CloseAll、Remove 和默认实例查找。
  • EnginePool:固定数量的引擎实例,适合并发执行和稳定资源上限。
  • AutoGrowEnginePool:初始实例不足时按需扩容,直到配置的最大实例数。

池的包装方法每次只借出一个实例,因此 RegisterGlobal、RegisterFunction、RegisterModule 等有状态配置只作用于取得的实例。需要池内所有实例共享配置时,请在创建阶段配置每个实例,或显式遍历池中实例。

脚本源

source 包定义 Reader、Watcher 和 ReadWatcher,并提供以下实现:

  • 根模块:FileSource、FileSystemSource、MemSource、MultiSource、CachedSource、转换包装器。
  • 独立子模块:source/database、source/consul、source/etcd、source/git、source/http、source/redis、source/s3。

脚本源只负责读取和监听,脚本引擎不直接耦合文件、数据库或对象存储。CachedSource 可在远程源外增加内存缓存;MultiSource 可组合多个源并选择顺序回退或首个成功策略。

宿主模块

hostmodule 提供语言无关的 ModuleDef,包含 log、crypto 和 util 模块。Lua 的 lua/host 在此基础上提供可 require 的模块:

  • kratos_logger:日志输出。
  • kratos_crypto:AES-GCM 字符串、JSON 和载荷加解密。
  • kratos_util:休眠、时间戳和日期格式化。
  • kratos_hook:脚本注册 Hook 和回调。
  • kratos_http:带域名白名单、重定向限制和响应体上限的 HTTP 请求。

加密实现使用 github.com/liujitcn/go-utils/crypto 原语;日志适配器使用 github.com/liujitcn/kratos-kit/logger。

目录结构

go-scripts/
├── engine.go                  # 能力接口、聚合接口和能力检测函数
├── factory.go                 # 引擎工厂注册表
├── manager.go                 # 多引擎生命周期管理
├── engine_pool.go             # 固定大小引擎池
├── engine_pool_autogrow.go    # 自动扩容引擎池
├── source/                    # 根脚本源实现
├── hostmodule/                # 语言无关宿主模块和适配器
├── lua/                      # Lua 引擎、状态池和 Lua 宿主模块
├── javascript/               # JavaScript 引擎和 Node.js 兼容层
├── gpython/ yaegi/ wazero/   # Python、Go、WebAssembly 引擎
├── cel/ expr/ starlark/ tcl/ # 表达式和 DSL 引擎
└── source/{database,...}/     # 数据库、配置中心、Git、HTTP、Redis、S3 源

每个子目录都是独立 Go 模块,拥有自己的 go.mod、测试和 README。根目录不包含 go.work,跨模块验证时请在各模块目录使用 GOWORK=off。

验证

在仓库根目录运行根模块测试:

GOWORK=off go test ./...

完整仓库验证需要对每个子模块分别运行:

GOWORK=off go test -mod=readonly ./...

项目采用 MIT License,详见 LICENSE。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCapabilityNotSupported = errors.New("script engine: capability not supported by this engine type")

ErrCapabilityNotSupported 表示引擎未实现调用方请求的可选能力。 调用方收到此错误后应按能力降级处理,而不是将其视为致命故障。

View Source
var ErrQuotaExceeded = errors.New("script engine: execution quota exceeded")

ErrQuotaExceeded 表示脚本超过配置的时间或指令预算并被中断。

Functions

func Register

func Register(typ Type, f FactoryFunc) error

Register 注册 typ 对应的工厂;typ 已注册时返回错误。

func SetLogger

func SetLogger(l Logger)

SetLogger 设置包级日志器;传入 nil 恢复静默实现。 此方法可与日志调用并发使用;修改日志器自身状态时仍需由调用方负责同步。

func Unregister

func Unregister(typ Type) bool

Unregister 删除 typ 对应的工厂,并返回是否成功删除。

Types

type AutoGrowEnginePool

type AutoGrowEnginePool struct {
	// contains filtered or unexported fields
}

AutoGrowEnginePool 管理可按需扩容的 Engine 实例。 优先复用空闲实例,没有空闲实例且未达到上限时创建新实例。

func NewAutoGrowEnginePool

func NewAutoGrowEnginePool(initialSize, maxSize int, typ Type) (*AutoGrowEnginePool, error)

NewAutoGrowEnginePool 使用 typ 对应的注册工厂创建自动扩容池。 工厂会自动执行每个实例的 Init。

func NewAutoGrowEnginePoolWithFactory

func NewAutoGrowEnginePoolWithFactory(initialSize, maxSize int, factory EngineFactoryFunc) (*AutoGrowEnginePool, error)

NewAutoGrowEnginePoolWithFactory 使用自定义工厂创建自动扩容池。 initialSize 是预创建数量,maxSize 是实例总数上限。

func (*AutoGrowEnginePool) Acquire

func (p *AutoGrowEnginePool) Acquire() (Engine, error)

Acquire 借出一个实例;达到上限且没有空闲实例时阻塞等待。

func (*AutoGrowEnginePool) CallFunction

func (p *AutoGrowEnginePool) CallFunction(ctx context.Context, name string, args ...any) (any, error)

RegisterModule 注册脚本模块。

func (*AutoGrowEnginePool) ClearError

func (p *AutoGrowEnginePool) ClearError()

func (*AutoGrowEnginePool) Close

func (p *AutoGrowEnginePool) Close() error

Close 关闭池并释放所有空闲实例;借出中的实例在归还时关闭。

func (*AutoGrowEnginePool) Execute

func (p *AutoGrowEnginePool) Execute(ctx context.Context) (any, error)

ExecuteFromKey 加载并执行 key 对应脚本。

func (*AutoGrowEnginePool) ExecuteFromKey

func (p *AutoGrowEnginePool) ExecuteFromKey(ctx context.Context, key string) (any, error)

ExecuteFromKeys 批量加载并执行脚本。

func (*AutoGrowEnginePool) ExecuteFromKeys

func (p *AutoGrowEnginePool) ExecuteFromKeys(ctx context.Context, keys []string) ([]any, error)

ExecuteString 编译并执行内联脚本。

func (*AutoGrowEnginePool) ExecuteString

func (p *AutoGrowEnginePool) ExecuteString(ctx context.Context, name string, code string) (any, error)

RegisterGlobal 注册全局变量。

func (*AutoGrowEnginePool) GetGlobal

func (p *AutoGrowEnginePool) GetGlobal(name string) (any, error)

RegisterFunction 注册宿主函数。

func (*AutoGrowEnginePool) GetLastError

func (p *AutoGrowEnginePool) GetLastError() error

ClearError 清除最近一次错误。

func (*AutoGrowEnginePool) GetSource

func (p *AutoGrowEnginePool) GetSource() source.Reader

Load 从绑定源加载脚本。

func (*AutoGrowEnginePool) Load

func (p *AutoGrowEnginePool) Load(ctx context.Context, key string) error

LoadMulti 按顺序从绑定源加载多个脚本。

func (*AutoGrowEnginePool) LoadMulti

func (p *AutoGrowEnginePool) LoadMulti(ctx context.Context, keys []string) error

LoadString 编译不经过绑定源的内联脚本。

func (*AutoGrowEnginePool) LoadString

func (p *AutoGrowEnginePool) LoadString(ctx context.Context, name string, code string) error

Execute 执行借出实例中已加载的脚本。

func (*AutoGrowEnginePool) RegisterFunction

func (p *AutoGrowEnginePool) RegisterFunction(name string, fn any) error

CallFunction 调用脚本函数。

func (*AutoGrowEnginePool) RegisterGlobal

func (p *AutoGrowEnginePool) RegisterGlobal(name string, value any) error

GetGlobal 读取全局变量。

func (*AutoGrowEnginePool) RegisterModule

func (p *AutoGrowEnginePool) RegisterModule(name string, module any) error

StartWatch 开始监听脚本变化。

func (*AutoGrowEnginePool) Release

func (p *AutoGrowEnginePool) Release(e Engine)

Release 将实例归还池中;池已关闭或通道已满时关闭实例。

func (*AutoGrowEnginePool) SetSource

func (p *AutoGrowEnginePool) SetSource(source source.Reader)

GetSource 返回借出实例当前绑定的脚本源。

func (*AutoGrowEnginePool) StartWatch

func (p *AutoGrowEnginePool) StartWatch(ctx context.Context, key string) error

StopWatch 停止监听脚本变化。

func (*AutoGrowEnginePool) StopWatch

func (p *AutoGrowEnginePool) StopWatch(key string) error

GetLastError 返回最近一次错误。

type Engine

Engine 聚合所有完整引擎能力。调用方不应假设轻量引擎一定实现此接口, 应使用 AsLoader、AsExecutor 等辅助函数或类型断言检查具体能力。

func NewScriptEngine

func NewScriptEngine(typ Type) (Engine, error)

NewScriptEngine 使用 typ 对应的已注册工厂创建引擎实例。

type EngineFactoryFunc

type EngineFactoryFunc func(ctx context.Context) (Engine, error)

EngineFactoryFunc 创建一个已经完成初始化的 Engine 实例。 工厂可在 Init 前完成沙箱白名单、全局变量等实例级配置。

type EnginePool

type EnginePool struct {
	// contains filtered or unexported fields
}

EnginePool 管理固定数量的独立 Engine 实例,适合需要稳定并发容量的场景。

func NewEnginePool

func NewEnginePool(size int, typ Type) (*EnginePool, error)

NewEnginePool 创建并初始化包含 size 个实例的引擎池。

func (*EnginePool) Acquire

func (p *EnginePool) Acquire() (Engine, error)

Acquire 借出一个引擎;没有空闲实例时阻塞等待,池已关闭时返回错误。

func (*EnginePool) CallFunction

func (p *EnginePool) CallFunction(ctx context.Context, name string, args ...any) (any, error)

CallFunction 调用脚本函数。

func (*EnginePool) ClearError

func (p *EnginePool) ClearError()

ClearError 清除最近一次错误。

func (*EnginePool) Close

func (p *EnginePool) Close() error

Close 关闭引擎池并释放所有空闲实例。

func (*EnginePool) Execute

func (p *EnginePool) Execute(ctx context.Context) (any, error)

Execute 执行借出实例中已加载的脚本。

func (*EnginePool) ExecuteFromKey

func (p *EnginePool) ExecuteFromKey(ctx context.Context, key string) (any, error)

ExecuteFromKey 加载并立即执行 key 对应脚本。

func (*EnginePool) ExecuteFromKeys

func (p *EnginePool) ExecuteFromKeys(ctx context.Context, keys []string) ([]any, error)

ExecuteFromKeys 批量加载并执行多个脚本。

func (*EnginePool) ExecuteString

func (p *EnginePool) ExecuteString(ctx context.Context, name string, code string) (any, error)

ExecuteString 编译并立即执行内联脚本。

func (*EnginePool) GetGlobal

func (p *EnginePool) GetGlobal(name string) (any, error)

GetGlobal 读取全局变量。

func (*EnginePool) GetLastError

func (p *EnginePool) GetLastError() error

GetLastError 返回最近一次错误。

func (*EnginePool) GetSource

func (p *EnginePool) GetSource() source.Reader

GetSource 返回借出实例绑定的脚本源。

func (*EnginePool) InitAll

func (p *EnginePool) InitAll(ctx context.Context) error

InitAll 重新初始化池中所有实例。

func (*EnginePool) IsClosed

func (p *EnginePool) IsClosed() bool

IsClosed 判断引擎池是否已关闭。

func (*EnginePool) Load

func (p *EnginePool) Load(ctx context.Context, key string) error

Load 从绑定源加载一个脚本。

func (*EnginePool) LoadMulti

func (p *EnginePool) LoadMulti(ctx context.Context, keys []string) error

LoadMulti 从绑定源按顺序加载多个脚本。

func (*EnginePool) LoadString

func (p *EnginePool) LoadString(ctx context.Context, name string, code string) error

LoadString 编译一个不经过绑定源的内联脚本。

func (*EnginePool) RegisterFunction

func (p *EnginePool) RegisterFunction(name string, fn any) error

RegisterFunction 注册宿主函数。

func (*EnginePool) RegisterGlobal

func (p *EnginePool) RegisterGlobal(name string, value any) error

RegisterGlobal 注册全局变量。

func (*EnginePool) RegisterModule

func (p *EnginePool) RegisterModule(name string, module any) error

RegisterModule 注册脚本模块。

func (*EnginePool) Release

func (p *EnginePool) Release(e Engine)

Release 将引擎归还池中;池已关闭或通道已满时关闭该实例。

func (*EnginePool) SetSource

func (p *EnginePool) SetSource(source source.Reader)

SetSource 为借出的实例绑定脚本源。

func (*EnginePool) StartWatch

func (p *EnginePool) StartWatch(ctx context.Context, key string) error

StartWatch 开始监听脚本变化。

func (*EnginePool) StopWatch

func (p *EnginePool) StopWatch(key string) error

StopWatch 停止监听脚本变化。

type FactoryFunc

type FactoryFunc func() (Engine, error)

FactoryFunc 定义创建 Engine 实例的工厂函数签名。

func GetFactory

func GetFactory(typ Type) (FactoryFunc, bool)

GetFactory 返回 typ 对应的工厂及其是否存在。

type FunctionRegistrar

type FunctionRegistrar interface {
	// RegisterFunction 注册脚本可调用的 Go 函数;函数签名由具体引擎校验。
	RegisterFunction(name string, fn any) error

	// CallFunction 调用脚本函数并返回结果;ctx 可用于取消或超时控制。
	CallFunction(ctx context.Context, name string, args ...any) (any, error)
}

FunctionRegistrar 提供宿主函数注册和脚本函数调用能力。

func AsFunctionRegistrar

func AsFunctionRegistrar(e any) FunctionRegistrar

AsFunctionRegistrar 返回 e 的 FunctionRegistrar 能力;不支持时返回 nil。

type GlobalAccessor

type GlobalAccessor interface {
	// RegisterGlobal 注册或覆盖脚本可见的全局变量。
	RegisterGlobal(name string, value any) error

	// GetGlobal 读取脚本全局变量;变量不存在时返回错误。
	GetGlobal(name string) (any, error)
}

GlobalAccessor 提供脚本全局变量的注册和读取能力。

func AsGlobalAccessor

func AsGlobalAccessor(e any) GlobalAccessor

AsGlobalAccessor 返回 e 的 GlobalAccessor 能力;不支持时返回 nil。

type Logger

type Logger interface {
	// Debug 记录 DEBUG 级别日志。
	Debug(ctx context.Context, msg string, args ...any)
	// Info 记录 INFO 级别日志。
	Info(ctx context.Context, msg string, args ...any)
	// Warn 记录 WARN 级别日志。
	Warn(ctx context.Context, msg string, args ...any)
	// Error 记录 ERROR 级别日志。
	Error(ctx context.Context, msg string, args ...any)

	// With 返回附带结构化字段的新日志器。
	With(args ...any) Logger
}

Logger 是框架内部使用的最小日志接口,便于适配 slog、zap、zerolog 等后端。

func GetLogger

func GetLogger() Logger

GetLogger 返回当前包级日志器。

func NewSlogLogger

func NewSlogLogger() Logger

NewSlogLogger 创建向 stderr 输出文本日志的默认 SlogLogger。

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager 按名称管理多个引擎实例,并统一控制初始化、关闭和默认实例。

func NewManager

func NewManager() *Manager

NewManager 创建空的 Manager。

func (*Manager) CloseAll

func (m *Manager) CloseAll() error

CloseAll 关闭所有已注册引擎并清空注册表,返回最后一个关闭错误。

func (*Manager) Get

func (m *Manager) Get(name string) (Engine, bool)

Get 返回 name 对应的引擎及其是否存在。

func (*Manager) GetDefault

func (m *Manager) GetDefault() (Engine, bool)

GetDefault 返回当前默认引擎。

func (*Manager) InitAll

func (m *Manager) InitAll(ctx context.Context) error

InitAll 初始化所有已注册引擎,遇到第一个错误时返回。

func (*Manager) Register

func (m *Manager) Register(name string, eng Engine) error

Register 注册未初始化的引擎;name 已存在或参数无效时返回错误。

func (*Manager) Remove

func (m *Manager) Remove(name string, closeIfExists bool)

Remove 删除 name 对应的引擎;closeIfExists 为 true 时同时关闭它。

func (*Manager) SetDefault

func (m *Manager) SetDefault(name string)

SetDefault 设置 GetDefault 使用的实例名称。

type ModuleRegistrar

type ModuleRegistrar interface {
	// RegisterModule 以 name 注册模块,供脚本导入或访问。
	RegisterModule(name string, module any) error
}

ModuleRegistrar 为支持模块系统的引擎注册模块,例如 Lua 的 require 模块。 module 的具体结构由引擎实现决定。

func AsModuleRegistrar

func AsModuleRegistrar(e any) ModuleRegistrar

AsModuleRegistrar 返回 e 的 ModuleRegistrar 能力;不支持时返回 nil。

type Quota

type Quota struct {
	// Timeout 定义墙上时钟超时时间。
	Timeout time.Duration

	// MaxInstructions 定义允许执行的最大指令数;零值表示不限制。
	MaxInstructions int
}

Quota 定义一次同步执行的时间和指令数预算;两个字段都为零表示不限制。

type QuotaController

type QuotaController interface {
	// SetQuota 设置执行预算;传入零值会取消预算限制。
	SetQuota(q Quota)
}

QuotaController 为后续 ExecuteSync 调用设置执行预算。

func AsQuotaController

func AsQuotaController(e any) QuotaController

AsQuotaController 返回 e 的 QuotaController 能力;不支持时返回 nil。

type RuntimeHook

type RuntimeHook func(ctx context.Context) error

RuntimeHook 在运行时准备完成后执行。使用状态池的引擎会在每次取得状态时重放钩子。 它允许调用方向运行时注入业务模块、宿主函数和反向回调。使用运行时池的引擎必须在每次重新取得状态时重放所有钩子, 并先清理上一个实例注入的业务全局变量,以保证引擎实例之间相互隔离。

type RuntimeHookRegistrar

type RuntimeHookRegistrar interface {
	// AddRuntimeHook 注册一个运行时钩子。
	AddRuntimeHook(hook RuntimeHook) error
}

RuntimeHookRegistrar 是支持注册运行时钩子的可选接口。 Init 前注册的钩子延迟到初始化后执行,Init 后注册的钩子立即执行。

func AsRuntimeHookRegistrar

func AsRuntimeHookRegistrar(e any) RuntimeHookRegistrar

AsRuntimeHookRegistrar 返回 e 的 RuntimeHookRegistrar 能力;不支持时返回 nil。

type SandboxConfigurator

type SandboxConfigurator interface {
	// SetOpenLibs 设置允许打开的标准库;必须在 Init 前调用。
	SetOpenLibs(libs ...string)
}

SandboxConfigurator 配置脚本可使用的标准库白名单。 Lua 引擎实现此能力;不支持沙箱的引擎不会实现该接口。

func AsSandboxConfigurator

func AsSandboxConfigurator(e any) SandboxConfigurator

AsSandboxConfigurator 返回 e 的 SandboxConfigurator 能力;不支持时返回 nil。

type ScriptEngine

type ScriptEngine interface {
	// GetType 返回引擎类型标识。
	GetType() Type

	// Init 创建运行时并完成引擎初始化,必须在加载或执行脚本前调用。
	// 初始化失败或引擎已经初始化时返回错误。
	Init(ctx context.Context) error

	// Close 释放运行时、脚本状态和其他底层资源。
	// 关闭后再次使用前必须重新调用 Init。
	Close() error

	// IsInitialized 判断引擎是否已初始化且尚未关闭。
	IsInitialized() bool

	// GetLastError 返回引擎记录的最近一次错误;没有错误时返回 nil。
	GetLastError() error

	// ClearError 清除引擎记录的错误状态。
	ClearError()
}

ScriptEngine 是所有脚本引擎都必须实现的生命周期接口。 轻量引擎可以只实现部分能力接口;完整引擎再组合为 Engine。

type ScriptExecutor

type ScriptExecutor interface {
	// Execute 按加载顺序执行队列中的脚本,并返回最后一个结果。
	Execute(ctx context.Context) (any, error)

	// ExecuteFromKey 从脚本源加载 key 对应脚本并立即执行。
	ExecuteFromKey(ctx context.Context, key string) (any, error)

	// ExecuteFromKeys 批量加载并执行多个 key,结果顺序与 keys 保持一致。
	ExecuteFromKeys(ctx context.Context, keys []string) ([]any, error)

	// ExecuteString 编译并立即执行内联脚本,不经过绑定脚本源。
	ExecuteString(ctx context.Context, name string, code string) (any, error)
}

ScriptExecutor 提供脚本执行能力。

func AsExecutor

func AsExecutor(e any) ScriptExecutor

AsExecutor 返回 e 的 ScriptExecutor 能力;不支持时返回 nil。

type ScriptLoader

type ScriptLoader interface {
	// SetSource 绑定脚本源;传入 nil 会清除当前绑定。
	// 后续 Load 和 ExecuteFromKey 调用都会通过该脚本源读取内容。
	SetSource(source source.Reader)

	// GetSource 返回当前绑定的脚本源;未绑定时返回 nil。
	GetSource() source.Reader

	// Load 根据 key 从绑定脚本源加载一个脚本并加入执行队列。
	Load(ctx context.Context, key string) error

	// LoadMulti 按 keys 顺序加载多个脚本,遇到第一个错误时停止。
	LoadMulti(ctx context.Context, keys []string) error

	// LoadString 编译直接传入的脚本,不经过绑定脚本源;name 用于错误诊断。
	LoadString(ctx context.Context, name string, code string) error
}

ScriptLoader 提供与具体 IO 实现解耦的脚本加载能力。 脚本从绑定的 source.Reader 读取,支持文件、内存、远程服务和组合源。

func AsLoader

func AsLoader(e any) ScriptLoader

AsLoader 返回 e 的 ScriptLoader 能力;不支持时返回 nil。

type ScriptWatcher

type ScriptWatcher interface {
	// StartWatch 开始监听 key 对应脚本的变化并在变化后重新加载。
	StartWatch(ctx context.Context, key string) error

	// StopWatch 停止监听 key 对应脚本并释放监听协程。
	StopWatch(key string) error
}

ScriptWatcher 提供脚本热更新能力。 引擎会通过绑定脚本源的 Watch 方法接收变化通知并重新加载脚本。

func AsWatcher

func AsWatcher(e any) ScriptWatcher

AsWatcher 返回 e 的 ScriptWatcher 能力;不支持时返回 nil。

type SlogLogger

type SlogLogger struct {
	L *slog.Logger
}

func (SlogLogger) Debug

func (s SlogLogger) Debug(ctx context.Context, msg string, args ...any)

Debug 转发到 slog.Logger.DebugContext。

func (SlogLogger) Error

func (s SlogLogger) Error(ctx context.Context, msg string, args ...any)

Error 转发到 slog.Logger.ErrorContext。

func (SlogLogger) Info

func (s SlogLogger) Info(ctx context.Context, msg string, args ...any)

Info 转发到 slog.Logger.InfoContext。

func (SlogLogger) Warn

func (s SlogLogger) Warn(ctx context.Context, msg string, args ...any)

Warn 转发到 slog.Logger.WarnContext。

func (SlogLogger) With

func (s SlogLogger) With(args ...any) Logger

With 返回底层日志器附带字段的新 SlogLogger。

type SyncExecutor

type SyncExecutor interface {
	// ExecuteSync 同步执行最近加载的脚本,超出预算时返回 ErrQuotaExceeded。
	ExecuteSync(ctx context.Context) (any, error)
}

SyncExecutor 提供无额外协程和通道分配的同步执行路径,适合高频调用。

func AsSyncExecutor

func AsSyncExecutor(e any) SyncExecutor

AsSyncExecutor 返回 e 的 SyncExecutor 能力;不支持时返回 nil。

type Type

type Type string

Type 标识脚本引擎实现类型。

const (
	UnknownType Type = "unknown"

	// LuaType 表示 gopher-lua 引擎。
	LuaType Type = "lua"

	// JavaScriptType 表示 goja JavaScript 引擎。
	JavaScriptType Type = "javascript"

	// GPythonType 表示 gpython Python 引擎。
	GPythonType Type = "gpython"

	// YaegiType 表示 Yaegi Go 解释器引擎。
	YaegiType Type = "yaegi"

	// WazeroType 表示 wazero WebAssembly 引擎。
	WazeroType Type = "wazero"

	// CELType 表示 cel-go CEL 表达式引擎。
	CELType Type = "cel"

	// ExprType 表示 expr-lang/expr 表达式引擎。
	ExprType Type = "expr"

	// StarlarkType 表示 starlark-go 引擎。
	StarlarkType Type = "starlark"

	// TclType 表示 modernc.org/tcl 引擎。
	TclType Type = "tcl"
)

func ListFactories

func ListFactories() []Type

ListFactories 返回当前已注册的引擎类型快照。

Directories

Path Synopsis
cel module
expr module
gpython module
hostmodule module
javascript module
lua module
starlark module
tcl module
wazero module
yaegi module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL