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: Dec 19, 2025 License: MIT Imports: 5 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
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(typ Type, f FactoryFunc) error

func Unregister

func Unregister(typ Type) bool

Types

type CallResult

type CallResult struct {
	Values []any
	Error  error
}

CallResult 函数调用结果

type Engine

type Engine interface {
	Init(ctx context.Context) error
	Destroy() 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

type ExecuteOptions

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

ExecuteOptions 执行选项

type FactoryFunc

type FactoryFunc func() (Engine, error)

func GetFactory

func GetFactory(typ Type) (FactoryFunc, bool)

type Type

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

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

func ListFactories

func ListFactories() []Type

Directories

Path Synopsis
javascript module

Jump to

Keyboard shortcuts

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