script_engine

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: MIT Imports: 6 Imported by: 2

README

Golang Embedded Script Engine

简介

这是一个用 Go 实现的脚本引擎,支持多种脚本语言(当前支持:Lua、JavaScript),旨在让宿主程序能够无缝加载并执行脚本以扩展行为或做快速原型开发。

特性

  • 支持 Lua 脚本执行
  • 支持 JavaScript 脚本执行
  • 可嵌入到 Go 应用中以扩展运行时行为
  • 提供单元测试与示例

需求

  • Go 1.20+(根据 go.mod 调整)
  • 可选:Lua runtime / JavaScript runtime 相关依赖(若使用 C 绑定或第三方引擎)

快速开始

1. 克隆仓库:
git clone https://github.com/tx7do/go-scripts.git
cd go-scripts
2. 安装依赖:
go mod tidy

使用JavaScript脚本引擎

import (
	_ "github.com/tx7do/go-scripts/javascript"
	"github.com/tx7do/go-scripts"
)

// 初始化支持JavaScript的自动扩容引擎池(初始2个实例,最大10个)
enginePool, err := script_engine.NewAutoGrowEnginePool(2, 10, script_engine.JavaScriptType)
if err != nil {
    // 处理初始化错误
}
defer enginePool.Close()

// 定义Go中的业务函数
func updateUserStatus(userId int64, status string) error {
    // 实际更新用户状态的业务逻辑
return nil
}

// 注册到脚本引擎,供JavaScript调用
err := enginePool.RegisterFunction("updateUserStatus", updateUserStatus)
if err != nil {
    // 处理注册错误
}

使用Lua脚本引擎

import (
	_ "github.com/tx7do/go-scripts/javascript"
	"github.com/tx7do/go-scripts"
)

// 初始化支持JavaScript的自动扩容引擎池(初始2个实例,最大10个)
enginePool, err := script_engine.NewAutoGrowEnginePool(2, 10, script_engine.LuaType)
if err != nil {
// 处理初始化错误
}
defer enginePool.Close()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(typ Type, f FactoryFunc) error

Register registers a FactoryFunc for a given Type.

func Unregister

func Unregister(typ Type) bool

Unregister removes a registered factory by Type. It returns true if a factory was removed.

Types

type AutoGrowEnginePool added in v0.0.2

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

AutoGrowEnginePool 是可按需扩展但有上限的引擎池。

func NewAutoGrowEnginePool added in v0.0.2

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

NewAutoGrowEnginePool 创建一个可自增长的池。 initialSize: 初始创建数量(>=0) maxSize: 池允许的最大实例数(必须 >= initialSize && >=1)

func (*AutoGrowEnginePool) Acquire added in v0.0.2

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

Acquire 获取一个 Engine:优先立即取空闲实例;若无且未到 max,则创建并返回新实例;否则阻塞等待。

func (*AutoGrowEnginePool) CallFunction added in v0.0.2

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

func (*AutoGrowEnginePool) ClearError added in v0.0.2

func (p *AutoGrowEnginePool) ClearError()

func (*AutoGrowEnginePool) Close added in v0.0.2

func (p *AutoGrowEnginePool) Close() error

Close 关闭池并销毁所有空闲实例。已借出的实例应由调用方关闭或归还后会被关闭。

func (*AutoGrowEnginePool) Execute added in v0.0.2

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

func (*AutoGrowEnginePool) ExecuteFile added in v0.0.2

func (p *AutoGrowEnginePool) ExecuteFile(ctx context.Context, filePath string) (any, error)

func (*AutoGrowEnginePool) ExecuteString added in v0.0.2

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

func (*AutoGrowEnginePool) GetGlobal added in v0.0.2

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

func (*AutoGrowEnginePool) GetLastError added in v0.0.2

func (p *AutoGrowEnginePool) GetLastError() error

func (*AutoGrowEnginePool) LoadFile added in v0.0.2

func (p *AutoGrowEnginePool) LoadFile(ctx context.Context, filePath string) error

func (*AutoGrowEnginePool) LoadReader added in v0.0.2

func (p *AutoGrowEnginePool) LoadReader(ctx context.Context, reader io.Reader, name string) error

func (*AutoGrowEnginePool) LoadString added in v0.0.2

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

func (*AutoGrowEnginePool) RegisterFunction added in v0.0.2

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

func (*AutoGrowEnginePool) RegisterGlobal added in v0.0.2

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

func (*AutoGrowEnginePool) RegisterModule added in v0.0.2

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

func (*AutoGrowEnginePool) Release added in v0.0.2

func (p *AutoGrowEnginePool) Release(e Engine)

Release 归还 Engine;若池已关闭或通道已满则关闭该实例。

type CallResult

type CallResult struct {
	Values []any
	Error  error
}

CallResult 函数调用结果

type Engine

type Engine interface {
	Init(ctx context.Context) error
	Close() error
	IsInitialized() bool

	LoadString(ctx context.Context, source string) error
	LoadFile(ctx context.Context, filePath string) error
	LoadReader(ctx context.Context, reader io.Reader, name string) error

	Execute(ctx context.Context) (any, error)
	ExecuteString(ctx context.Context, source string) (any, error)
	ExecuteFile(ctx context.Context, filePath string) (any, error)

	RegisterGlobal(name string, value any) error
	GetGlobal(name string) (any, error)

	RegisterFunction(name string, fn any) error
	CallFunction(ctx context.Context, name string, args ...any) (any, error)

	RegisterModule(name string, module any) error

	GetLastError() error
	ClearError()
}

Engine Define the interface for script engines

func NewScriptEngine added in v0.0.2

func NewScriptEngine(typ Type) (Engine, error)

NewScriptEngine 使用已注册的工厂函数创建一个 Engine 实例。

type EnginePool added in v0.0.2

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

EnginePool 管理多个独立 Engine 实例以支持并发执行。 NewEnginePool 需要提供一个 factory 用于创建单个 Engine 实例。

func NewEnginePool added in v0.0.2

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

NewEnginePool 创建并初始化一个包含 size 个 Engine 的池。 factory 用于创建单个 Engine(例如 newLuaEngine)。

func (*EnginePool) Acquire added in v0.0.2

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

Acquire 从池中获取一个 Engine(会阻塞直到有可用的)。

func (*EnginePool) CallFunction added in v0.0.2

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

func (*EnginePool) ClearError added in v0.0.2

func (p *EnginePool) ClearError()

func (*EnginePool) Close added in v0.0.2

func (p *EnginePool) Close() error

Close 关闭池并销毁所有子 Engine。

func (*EnginePool) Execute added in v0.0.2

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

func (*EnginePool) ExecuteFile added in v0.0.2

func (p *EnginePool) ExecuteFile(ctx context.Context, filePath string) (any, error)

func (*EnginePool) ExecuteString added in v0.0.2

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

func (*EnginePool) GetGlobal added in v0.0.2

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

func (*EnginePool) GetLastError added in v0.0.2

func (p *EnginePool) GetLastError() error

func (*EnginePool) InitAll added in v0.0.2

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

func (*EnginePool) IsClosed added in v0.0.2

func (p *EnginePool) IsClosed() bool

IsClosed 返回池是否已关闭。

func (*EnginePool) LoadFile added in v0.0.2

func (p *EnginePool) LoadFile(ctx context.Context, filePath string) error

func (*EnginePool) LoadReader added in v0.0.2

func (p *EnginePool) LoadReader(ctx context.Context, reader io.Reader, name string) error

func (*EnginePool) LoadString added in v0.0.2

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

func (*EnginePool) RegisterFunction added in v0.0.2

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

func (*EnginePool) RegisterGlobal added in v0.0.2

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

func (*EnginePool) RegisterModule added in v0.0.2

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

func (*EnginePool) Release added in v0.0.2

func (p *EnginePool) Release(e Engine)

Release 将 Engine 放回池中;若池已关闭则关闭该 Engine。

type ExecuteOptions

type ExecuteOptions struct {
	Timeout  time.Duration
	Globals  map[string]any
	MaxStack int
}

ExecuteOptions 执行选项

type FactoryFunc

type FactoryFunc func() (Engine, error)

FactoryFunc 是用于创建 Engine 实例的工厂函数类型。

func GetFactory

func GetFactory(typ Type) (FactoryFunc, bool)

GetFactory returns a registered FactoryFunc for a given Type and whether it existed.

type Manager added in v0.0.2

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

Manager 管理多个 Engine 实例的生命周期与访问。 - 适用于需要多个引擎实例、统一 Init/Close、或按 name 获取的场景。 - 若项目只需要单个全局 Engine,可不使用 Manager。

func NewManager added in v0.0.2

func NewManager() *Manager

NewManager 创建 Manager。

func (*Manager) CloseAll added in v0.0.2

func (m *Manager) CloseAll() error

CloseAll 关闭所有已注册引擎(并忽略单个 Close 错误,返回最后一个错误)。

func (*Manager) Get added in v0.0.2

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

Get 返回已注册的 Engine。

func (*Manager) GetDefault added in v0.0.2

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

GetDefault 获取默认引擎。

func (*Manager) InitAll added in v0.0.2

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

InitAll 对所有已注册引擎执行 Init。

func (*Manager) Register added in v0.0.2

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

Register 注册一个 Engine(不初始化)。 若 name 已存在返回错误。

func (*Manager) Remove added in v0.0.2

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

Remove 注销并可选择关闭该 Engine(若 closeIfExists 为 true)。

func (*Manager) SetDefault added in v0.0.2

func (m *Manager) SetDefault(name string)

SetDefault 设置默认引擎名,便于不指定 name 时使用。

type Type

type Type string
const (
	// LuaType Lua 脚本引擎类型
	LuaType Type = "lua"

	// JavaScriptType JavaScript 脚本引擎类型
	JavaScriptType Type = "javascript"
)

func ListFactories

func ListFactories() []Type

ListFactories returns a slice of currently registered Types.

Directories

Path Synopsis
javascript module

Jump to

Keyboard shortcuts

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