plugin

package
v0.4.19 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

pkg/plugin/plugin.go Plugin system for loading WebAssembly plugins at runtime.

WASM plugins work on all platforms (Windows, Linux, macOS) without CGO. Plugins can be written in TinyGo, Rust, C/C++, Zig, AssemblyScript, etc.

Usage from xxlang:

import "plugin/myplugin"
myplugin.hello()

Building a WASM plugin:

tinygo build -o myplugin.wasm -target=wasi myplugin.go
# or with Rust:
cargo build --target wasm32-wasi --release

pkg/plugin/wasm_loader.go WebAssembly plugin loader using gowasm runtime. Works on all platforms including Windows without CGO.

pkg/plugin/wasm_plugin.go WebAssembly plugin wrapper that implements the Plugin interface.

Index

Constants

This section is empty.

Variables

View Source
var Registry = struct {
	sync.RWMutex
	plugins map[string]Plugin
}{
	// contains filtered or unexported fields
}

Registry tracks loaded plugins. It is safe for concurrent use.

Functions

func Has

func Has(name string) bool

Has checks if a plugin is registered.

func List

func List() []string

List returns all registered plugin names.

func Register

func Register(p Plugin)

Register registers a plugin with the registry. This is typically called from a plugin's init() function (for static plugins) or by the loader after loading a WASM plugin.

func ToModule

func ToModule(p Plugin) *objects.Module

ToModule converts a Plugin to an objects.Module.

Types

type Loader

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

Loader handles loading WASM plugin files.

func NewLoader

func NewLoader() *Loader

NewLoader creates a new plugin loader with default search paths.

func (*Loader) AddPath

func (l *Loader) AddPath(path string)

AddPath adds a search path for plugins.

func (*Loader) Load

func (l *Loader) Load(name string) (Plugin, error)

Load loads a plugin by name. It first checks the registry, then searches for a .wasm file. Returns the plugin and any error encountered.

func (*Loader) LoadPath added in v0.4.19

func (l *Loader) LoadPath(path string) (Plugin, error)

LoadPath loads a plugin from a specific file path. The path should point to a .wasm file. This is useful when you want to load a plugin from a specific location without setting up search paths.

Example:

loader := plugin.NewLoader()
p, err := loader.LoadPath("./my-plugins/fib.wasm")

func (*Loader) Paths

func (l *Loader) Paths() []string

Paths returns the current search paths.

func (*Loader) SetPaths

func (l *Loader) SetPaths(paths []string)

SetPaths sets the search paths for plugins.

type Plugin

type Plugin interface {
	// Name returns the plugin name (used as plugin/name in imports).
	Name() string

	// Exports returns the module's exported symbols.
	// The map keys are the names accessible from xxlang code.
	Exports() map[string]objects.Object
}

Plugin is the interface that WASM plugins must implement.

func Get

func Get(name string) (Plugin, bool)

Get retrieves a plugin from the registry. Returns the plugin and true if found, nil and false otherwise.

type WasmPlugin added in v0.4.19

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

WasmPlugin wraps a WASM module to implement the Plugin interface.

func NewWasmPlugin added in v0.4.19

func NewWasmPlugin(name string, module api.Module, rt interface{ Close(context.Context) error }) *WasmPlugin

NewWasmPlugin creates a new WasmPlugin from a loaded module.

func (*WasmPlugin) Close added in v0.4.19

func (p *WasmPlugin) Close(ctx context.Context) error

Close releases the WASM module resources.

func (*WasmPlugin) Exports added in v0.4.19

func (p *WasmPlugin) Exports() map[string]objects.Object

Exports returns the plugin's exported functions as Xxlang objects.

func (*WasmPlugin) Name added in v0.4.19

func (p *WasmPlugin) Name() string

Name returns the plugin name.

Jump to

Keyboard shortcuts

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