wrengo

package module
v0.0.0-...-e5142ae Latest Latest
Warning

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

Go to latest
Published: May 15, 2025 License: MIT Imports: 9 Imported by: 0

README

Wren-Go Bindings

These are PureGo bindings for the Wren scripting language to be used with Go.

pkg.dev

What is Wren?

"Wren is a small, fast, class-based concurrent scripting language."

Other bindings exist, right?

Yes, but they:

  • May not use Purego. This hurts cross-compilation.
  • May be outdated.
  • May not be efficient.
  • May not be as Go-like as these bindings.

These bindings, while incomplete and imperfect, still fulfill the above restrictions.

The following platforms should work with these bindings:

  • Windows x86 / x86_64
  • Linux x86_64
  • Mac x86_64 / arm_64

Though only Linux has been tested.

How do I use the bindings?

  1. go get github.com/solarlune/wren-go
  2. Copy the lib directory to your project directory.

func main() {

	err := wrengo.InitFromDirectory("./lib")
	if err != nil {
		panic(err)
	}

	// Create a config.
	config := wrengo.NewConfig()

	// Create a VM with the config.
	vm := wrengo.NewVM(config)

	// Run Wren!
	vm.Run("main", `System.print("Hi from Wren!")`)

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCompileTime = errors.New("error compiling wren script")

InterpretResultSuccess InterpretResult = iota

View Source
var ErrVMFreed = errors.New("error: virtual machine already freed")

Functions

func BytePtrToString

func BytePtrToString(p *byte) string

func Init

func Init(libraryPath string) error

Init initializes the Wren-Go bindings using the passed shared library filepath. The path is, by default, relative to the executable, in the current working directory. The function automatically loads libraries using the OS and architecture hierarchy from the original DLL / library download.

func InitFromDirectory

func InitFromDirectory(libraryBaseDirectoryPath string) error

InitFromDirectory initializes the scripting bindings using the libraries in the given directory, automatically loading the correct one for the current platform if it's properly named.

func VersionNumber

func VersionNumber() (int, int, int)

Returns the version number of the scripting engine.

Types

type CallHandle

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

func (*CallHandle) Call

func (w *CallHandle) Call(args ...any) (any, error)

Call attempts to call the specified function on the object in the given module from the creation of the CallHandle. If it can't due to an error (wrong signature, runtime errors, etc.), the fiber running the VM will fail, meaning further attempts to run code on this VM will fail until the VM is restarted by re-evaluating / re-compiling code, and the function will return an error.

args is any arguments to supply to the function call. can be any primitive numeric or boolean type, a string, a byte slice, nil, a []any or a map[string]any. The following Go variable types are usable as arguments:

- bool - float32 / float64 / int / int32 / rune / int64 / uint / uint8 / uint16 / uint32 / uint64 - all transformed to Double - string - []byte - nil - []any (elements must be convertible, of course) - map[any]any (elements must be convertible, of course)

The function will return any values returned from the function in Wren, converted to Go types, and an error if the function couldn't be called.

func (*CallHandle) Release

func (w *CallHandle) Release()

type Config

type Config struct {
	SlotNum int // Number of slots for variables; by default, set to 100
	// contains filtered or unexported fields
}

func NewConfig

func NewConfig() Config

NewConfig creates a new configuration for creating a VM.

func (Config) WithForeignMethodResolver

func (cfg Config) WithForeignMethodResolver(resolver func(vm *VM, module, className, signature string, isStatic bool) GoForeignFunction) Config

WithForeignMethodResolver sets the configuration to use a foreign method resolver. This allows you to call Go from Wren by simply returning a GoForeignFunction - a function written in Go that does something and that can return a value, depending on the module, classname, signature, and staticness provided.

This can only be set once per Config.

func (Config) WithModuleLoaderFromFS

func (cfg Config) WithModuleLoaderFromFS(filesys fs.FS) Config

WithModuleLoaderFromFS sets the Wren VM to use a file system to load and import Wren modules.

This can only be set once per Config.

type GoForeignFunction

type GoForeignFunction func(vm *VM, args []any) any

GoForeignFunction represents a Go function that is called from Wren. args represents the arguments that were supplied from Wren through the method or function call, and the function should return a convertible value.

type SlotType

type SlotType int
const (
	SlotTypeBool SlotType = iota
	SlotTypeNumber
	SlotTypeForeign
	SlotTypeList
	SlotTypeMap
	SlotTypeNull
	SlotTypeString
	SlotTypeUnknown // Unrepresentable by C
)

type VM

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

func NewVM

func NewVM(config Config) *VM

NewVM creates a new VM using the provided configuration.

func (*VM) CallHandle

func (vm *VM) CallHandle(module, object, signature string) (*CallHandle, error)

CallHandle creates a call handle to the function or method with the name and function signature defined of the object designated in the module provided. The module is the filepath of the script file for VM.InterpretFilesystem() calls, or the explicit module name for VM.Interpret() calls.

This can be used in two ways.

1. Functions. For these, object should be the name of the function and signature ".call()".

2. Methods. For these, object should be the name of the class instance or class object and the signature the method to call. The method is designated by arity, with underscores being spaces for arguments. (So for a function named "Walk" that takes an argument on a Dog class, object = "Dog" and signature = "Walk(_)"). You can also use it on getters, setters, and static functions.

func (*VM) Free

func (vm *VM) Free() error

func (*VM) HasModule

func (vm *VM) HasModule(moduleName string) bool

HasModule returns true if the VM contains a module of the given name.

func (*VM) HasVariable

func (vm *VM) HasVariable(module, objectName string) bool

HasVariable returns true if the VM has a variable of the given name in the module specified.

func (*VM) Run

func (vm *VM) Run(moduleName, src string) error

Run compiles and evaluates the source text src and binds it to the given module name. Once run, module cannot be run again, as the VM's state is persistent.

func (*VM) RunFile

func (vm *VM) RunFile(fsys fs.FS, fpath string) error

RunFile evaluates a file, found at the provided filepath in the file system. Once run, the file cannot be run again, as the VM's state is persistent.

func (*VM) Variable

func (vm *VM) Variable(module, objectName string) any

Variable looks up the object name in the specified module; if it exists, it attempts to parse it to a Go object.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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