wren

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2022 License: MIT Imports: 8 Imported by: 4

README

WrenGo

GoDoc GoReportCard Wren

WrenGo provides bindings for go to interact with the wren scripting language. Currently Mutex is not used so be careful with Goroutines. There probably should be a lot more tests as well, however there are some tests to ensure that basic functionality works.

Installation

go get github.com/crazyinfin8/WrenGo

Usage

A simple Hello world

package main

import (
	wren "github.com/crazyinfin8/WrenGo"
)

func main() {
	vm := wren.NewVM()
	// The VM should be freed when no longer needed
	defer vm.Free()
	vm.InterpretString("main", `System.print("Hello world from wren!")`)
}

Adding some configurating

package main

import (
	wren "github.com/crazyinfin8/WrenGo"
)

func main() {
	cfg := wren.NewConfig()
	cfg.LoadModuleFn = func(vm *wren.VM, name string) (string, bool) {
		if name == "WrenGo" {
			return `System.print("Hello from imported module")`, true // return true for successful import
		}
		return "", false // return false for unsuccessful import
	}
	vm := cfg.NewVM()
	defer vm.Free()
	vm.InterpretString("main", `import "WrenGo"`)
}

Calling Wren functions from Go

package main

import (
	wren "github.com/crazyinfin8/WrenGo"
)

func main() {
	vm := wren.NewVM()
	defer vm.Free()
	vm.InterpretString("main", 
	`class MyClass {
		static sayHello() {
			System.print("Hello from MyClass")
		}
	}`)
	value, _ := vm.GetVariable("main", "MyClass")
	MyClass, _ := value.(*wren.Handle)
	// Handles should be freed when no longer needed
	defer MyClass.Free()
	Fn, _ := MyClass.Func("sayHello()")
	defer Fn.Free()
	Fn.Call()
}

Calling Go functions from Wren

package main

import (
	wren "github.com/crazyinfin8/WrenGo"
)

func main() {
	vm := wren.NewVM()
	defer vm.Free()
	vm.SetModule("main", wren.NewModule(wren.ClassMap{
		"MyClass": wren.NewClass(nil, nil, wren.MethodMap{
			"static sayHello()": func(vm *wren.VM, parameters []interface{}) (interface{}, error) {
				println("Hello from MyClass but from Go")
				return nil, nil
			},
		}),
	}))
	vm.InterpretString("main", 
	`foreign class MyClass {
		foreign static sayHello()
	}

	MyClass.sayHello()`)
}

More complete example about using foreign class functions

package main

import (
	"errors"
	wren "github.com/crazyinfin8/WrenGo"
)

// Person is a single human
type Person struct {
	name string
}

// GetPerson Safely extract Person struct from interface{}
func GetPerson(i interface{}) (*Person, bool) {
	if foreign, ok := i.(*wren.ForeignHandle); ok {
		if val, err := foreign.Get(); err == nil {
			person, ok := val.(*Person)
			return person, ok
		}
	}
	return nil, false
}

func main() {
	vm := wren.NewVM()
	defer vm.Free()
	vm.SetModule("main", wren.NewModule(wren.ClassMap{
		"Person": wren.NewClass(
			// This function is called whenever the "Person" constructor is called
			func(vm *wren.VM, parameters []interface{}) (interface{}, error) {
				// parameters index start at 1. Index 0 is the receiver object in wren
				// (in this case it is the class "Person")
				if len(parameters) < 2 {
					return nil, errors.New("Expected at least 1 Parameter")
				}
				if name, ok := parameters[1].(string); ok {
					println("Created Person: " + name)
					return &Person{name}, nil
				}
				return nil, errors.New("Expected first parameter to be of type String")
			},
			// This function is called whenever the "Person" instance was Garbage collected
			func(vm *wren.VM, data interface{}) {
				if person, ok := data.(*Person); ok {
					println(person.name + " was garbage collected")
				} else {
					panic("Should not happen")
				}

			},
			wren.MethodMap{
				"sayHello()": func(vm *wren.VM, parameters []interface{}) (interface{}, error) {
					if len(parameters) > 0 {
						// Again index 0 is for the receiver. As this function is not static,
						// the receiver is the instance of this "Person" class.
						// Index is actually a ForeignHandle that can access the "Person" struct.
						// To extract the "Person" struct, I can use the helper function above,
						// "GetPerson".
						//
						// Note: you should normally call Free all handles, however handles that were
						// created by WrenGo (for parameters) are automatically freed.
						// If you want to save the Handle, you should call Copy on it (make sure to
						// free that handle as well)
						if person, ok := GetPerson(parameters[0]); ok {
							println("Hello, I am " + person.name)
						}
					} else {
						panic("Should not happen")
					}

					return nil, nil
				},
				// notice that this function has an underscore. The number of underscores should determine the length of "parameters", They should also be comma seperated
				"introduceTo(_)": func(vm *wren.VM, parameters []interface{}) (interface{}, error) {
					if len(parameters) < 2 {
						return nil, errors.New("Expected at least 1 Parameter")
					}
					var (
						person1, person2 *Person
						ok               bool
					)
					if person1, ok = GetPerson(parameters[0]); !ok {
						panic("Should not happen")
					}
					if person2, ok = GetPerson(parameters[1]); !ok {
						return nil, errors.New("Expected first parameter to be of type Person")
					}
					println(person1.name + ", I would like you to meet " + person2.name)
					return nil, nil
				},
				// getters do not use parenthesis
				"name": func(vm *wren.VM, parameters []interface{}) (interface{}, error) {
					if len(parameters) > 0 {
						// Again index 0 is for the receiver. As this function is not static,
						// the receiver is the instance of this "Person" class
						if person, ok := GetPerson(parameters[0]); ok {
							return person.name, nil
						}
					}
					panic("Should not happen")
				},
				// setters are like regular functions but with an equal sign after the function name and only one parameter
				"name=(_)": func(vm *wren.VM, parameters []interface{}) (interface{}, error) {
					if len(parameters) < 2 {
						return nil, errors.New("Expected at least 1 Parameter")
					}
					var (
						person *Person
						name   string
						ok     bool
					)
					if person, ok = GetPerson(parameters[0]); !ok {
						panic("Should not happen")
					}
					if name, ok = parameters[1].(string); !ok {
						return nil, errors.New("Expected first parameter to be of type String")
					}
					println(person.name + " changed their name to " + name)
					person.name = name
					return nil, nil
				},
				// operator overloading is also possible by changing name to the operator
				"+(_)": func(vm *wren.VM, parameters []interface{}) (interface{}, error) {
					if len(parameters) < 2 {
						return nil, errors.New("Expected at least 1 Parameter")
					}
					var (
						person1, person2 *Person
						ok               bool
					)
					if person1, ok = GetPerson(parameters[0]); !ok {
						panic("Should not happen")
					}
					if person2, ok = GetPerson(parameters[1]); !ok {
						return nil, errors.New("Expected other operand to be of type Person")
					}
					println(person1.name + " has married " + person2.name)
					return nil, nil
				},
				// overloading Indexing behavior is posible by using brackets 
				"[_]": func(vm *wren.VM, parameters []interface{}) (interface{}, error) {
					if len(parameters) < 2 {
						return nil, errors.New("Expected at least 1 Parameter")
					}
					var (
						person1, person2 *Person
						ok               bool
					)
					if person1, ok = GetPerson(parameters[0]); !ok {
						panic("Should not happen")
					}
					if person2, ok = GetPerson(parameters[1]); !ok {
						return nil, errors.New("Expected other operand to be of type Person")
					}
					println("indexing " + person1.name + " by " + person2.name + " (don't think to hard about that, it just works)")
					return nil, nil
				},
			}),
	}))
	vm.InterpretString("main",
		`foreign class Person {
			construct new(name) {}
			foreign sayHello()
			foreign introduceTo(person)
			foreign name
			foreign name=(name)
			foreign +(person)
			foreign [person]
		}

		var person1 = Person.new("John")
		var person2 = Person.new("Jane")
		System.print("person1 is %(person1.name)")
		person1.name = "David"
		person2.introduceTo(person1)
		person1.sayHello()
		person1 + person2
		person1[person2]`)
}

Documentation

Overview

Package wren provides bindings for Go programs to utilize and interact with the Wren scripting langues http://wren.io

Index

Constants

View Source
const (
	// VersionString Wren's version as a string
	VersionString string = C.WREN_VERSION_STRING
	// VersionMajor Wren's major version number
	VersionMajor int = C.WREN_VERSION_MAJOR
	// VersionMinor Wren's minor version number
	VersionMinor int = C.WREN_VERSION_MINOR
	// VersionPatch Wren's patch version number
	VersionPatch int = C.WREN_VERSION_PATCH
)
View Source
const MAX_REGISTRATIONS = 512

Variables

View Source
var (

	// DefaultOutput is where Wren will print to if a VM's config doesn't specify its own output (Set this to nil to disable output)
	DefaultOutput io.Writer = os.Stdout
	// DefaultError is where Wren will send error messages to if a VM's config doesn't specify its own place for outputting errors (Set this to nil to disable output)
	DefaultError io.Writer = os.Stderr
	// DefaultModuleLoader allows Wren to import modules by loading files relative to the current directory (Set this to nil to disable importing or file access)
	DefaultModuleLoader LoadModuleFn = func(vm *VM, name string) (string, bool) {
		if data, err := ioutil.ReadFile(name); err == nil {
			return string(data), true
		}
		return "", false
	}
)

Functions

func VersionTuple

func VersionTuple() [3]int

VersionTuple returns Wren's version numbers as an array of 3 numbers

Types

type CallHandle

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

CallHandle is a handle to a wren function

func (*CallHandle) Call

func (h *CallHandle) Call(parameters ...interface{}) (interface{}, error)

Call tries to call the function on the handles that created the `CallHandle`. The amount of parameters should coorespond to the signature used to create this function. This function should not be called if the VM is already running.

func (*CallHandle) Free

func (h *CallHandle) Free()

Free releases the handle tied to it. The handle should be freed when no longer in use. The handle should not be used after it has been freed

type ClassMap

type ClassMap map[string]*ForeignClass

ClassMap is a map containing all foreign classes (or classes where objects are made in Go and not Wren) organized by class name

func (ClassMap) Clone

func (classes ClassMap) Clone() ClassMap

Clone creates a copy of all classes this `ClassMap` references

func (ClassMap) Merge

func (classes ClassMap) Merge(source ClassMap) ClassMap

Merge goes through all items in the source `ClassMap` and adds them if they are not nil

type CompileError

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

CompileError is sent by Wren to `ErrorFn` if Wren source code couldn't compile

func (*CompileError) Error

func (err *CompileError) Error() string

type Config

type Config struct {
	// Wren calls this function to print text
	WriteFn WriteFn
	// Wren calls this function to print errors
	ErrorFn ErrorFn
	// Wren calls this function before loading modules to resolve module names.
	ResolveModuleFn ResolveModuleFn
	// Wren calls this function to import modules (if you want to disable importing, this should be set to nil and the global value `DefaultModuleLoader` should also be set to nil)
	LoadModuleFn LoadModuleFn
	// If `WriteFn` is not set, wren will print text to here instead (if you want to disable all output, this should be set to nil and the global value `DefaultOutput` should also be set to nil)
	DefaultOutput io.Writer
	// If `ErrorFn` is not set, wren errors will be written to here instead (if you want to disable all output, this should be set to nil and the global value `DefaultError` should also be set to nil)
	DefaultError io.Writer
	// Custom data
	UserData interface{}
}

Config contains some settings to setup how VM will behave

func NewConfig

func NewConfig() *Config

NewConfig creates a new config and initializes it with default variables (mainly specifying where output should go)

func (Config) Clone

func (cfg Config) Clone() *Config

Clone returns a copy of a config

func (*Config) NewVM

func (cfg *Config) NewVM() *VM

NewVM creates a new instance of Wren's virtual machine by cloning the config passed to it

type ErrorFn

type ErrorFn func(vm *VM, err error)

ErrorFn is called by Wren whenever there is a runtime error, compile error, or stack trace. It should be of type `CompileError`, `RuntimeError`, or `StackTrace`

type ForeignClass

type ForeignClass struct {
	// Wren will call this function as a constructor. Whatever it returns for `interface{}` will be the the foreign instance of this foreign class
	Initializer ForeignInitializer
	// Wren will call this function when Wren's garbage collector collects the forign class (not that maintaining handles will prevent the foreign object from being garbage collected)
	Finalizer ForeignFinalizer
	// A map containing `ForeignMethodFn`s organized by function signatures. see MethodMap for mor information on signatures syntax.
	MethodMap MethodMap
}

ForeignClass details a foreign class (or classes where objects are made in Go and not Wren) for Wren to use. Whenever Wren runs a constructor for this class, `Initializer` is called. Whatever the first value `Initializer` returns will be used as the instance for this foreign class.

func NewClass

func NewClass(initializer ForeignInitializer, finalizer ForeignFinalizer, methods MethodMap) *ForeignClass

NewClass creates a new `ForeignClass` with the given `ForeignInitializer` function, `ForeignFinalizer` function, and `MethodMap`

func (*ForeignClass) Clone

func (class *ForeignClass) Clone() *ForeignClass

Clone creates a copy of the current `ForeignClass`

type ForeignFinalizer

type ForeignFinalizer func(vm *VM, data interface{})

ForeignFinalizer is a function called when Wren garbage collects the forign object it is tied to (note that maintaining handles will prevent the foreign object from being garbage collected)

type ForeignHandle

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

ForeignHandle is a handle to a foreign object in Wren

func (*ForeignHandle) Copy

func (h *ForeignHandle) Copy() (*ForeignHandle, error)

Copy creates a new `ForeignHandle` tied to this foreign object, if the previous one is freed the new one should still persist

func (*ForeignHandle) Free

func (h *ForeignHandle) Free()

Free releases the handle tied to it. The handle should be freed when no longer in use. The handle should not be used after it has been freed

func (*ForeignHandle) Func

func (h *ForeignHandle) Func(signature string) (*CallHandle, error)

Func creates a callable handle from the Wren object tied to the current handle. There isn't currently a way to check if the function referenced from `signature` exists before calling it

func (*ForeignHandle) Get

func (h *ForeignHandle) Get() (interface{}, error)

Get tries to get the original value that this `ForeignHandle` set to

func (*ForeignHandle) Handle

func (h *ForeignHandle) Handle() *Handle

Handle returns the generic handle that this `ForeignHandle` is tied to

func (*ForeignHandle) VM

func (h *ForeignHandle) VM() *VM

VM returns the vm that this handle belongs to

type ForeignInitializer

type ForeignInitializer func(vm *VM, parameters []interface{}) (interface{}, error)

ForeignInitializer is a function used to initialize a foreign class instance. The value of parameter[0] will be the foreign class while anything after that are the parameters from the wren constructor. Whatever it returns for `interface{}` will be the the foreign instance of the foreign class

type ForeignMethodFn

type ForeignMethodFn func(vm *VM, parameters []interface{}) (interface{}, error)

ForeignMethodFn is a function that wren can import or call. The value of parameters[0] will be the foreign object itself while anything after that are the parameters from the wren function. if it returns an error, then it will call `vm.Abort`. Handles that originated from `parameters` are automatically freed by WrenGo. If you want to keep the handle, you need to call copy on it.

type Handle

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

Handle is a generic handle from wren

func (*Handle) Copy

func (h *Handle) Copy() (*Handle, error)

func (*Handle) Free

func (h *Handle) Free()

Free releases the handle tied to it. The handle should be freed when no longer in use. The handle should not be used after it has been freed

func (*Handle) Func

func (h *Handle) Func(signature string) (*CallHandle, error)

Func creates a callable handle from the wren object tied to the current handle. There isn't currently a way to check if the function referenced from `signature` exists before calling it

func (*Handle) Handle

func (h *Handle) Handle() *Handle

Handle returns the generic handle

func (*Handle) VM

func (h *Handle) VM() *VM

VM returns the vm that this handle belongs to

type InvalidKey

type InvalidKey struct {
	Map *MapHandle
	Key interface{}
}

InvalidKey is returned if there was an attempt to access a maps value with a key that is not of type number, boolean, string, or nil

func (*InvalidKey) Error

func (err *InvalidKey) Error() string

type InvalidValue

type InvalidValue struct {
	Value interface{}
}

InvalidValue is returned if there was an attempt to pass a value to Wren that WrenGo cannot process. Note that Go maps, lists, and slices (other than byte slices), may also send this error. `ListHandle`s and `MapHandle`s should be used instead of list and maps.

func (InvalidValue) Error

func (err InvalidValue) Error() string

type KeyNotExist

type KeyNotExist struct {
	Map *MapHandle
	Key interface{}
}

KeyNotExist is returned if there was an attempt to access a key value from a map that doesn't exist yet

func (*KeyNotExist) Error

func (err *KeyNotExist) Error() string

type ListHandle

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

ListHandle is a handle to a list object in Wren

func (*ListHandle) Copy

func (h *ListHandle) Copy() (*ListHandle, error)

Copy creates a new `ListHandle` tied to this Wren list, if the previous one is freed the new one should still persist

func (*ListHandle) Count

func (h *ListHandle) Count() (int, error)

Count counts how many elements are in the Wren list

func (*ListHandle) Free

func (h *ListHandle) Free()

Free releases the handle tied to it. The handle should be freed when no longer in use. The handle should not be used after it has been freed

func (*ListHandle) Func

func (h *ListHandle) Func(signature string) (*CallHandle, error)

Func creates a callable handle from the Wren object tied to the current handle. There isn't currently a way to check if the function referenced from `signature` exists before calling it

func (*ListHandle) Get

func (h *ListHandle) Get(index int) (interface{}, error)

Get tries to return the value in the Wren list at the index `index`

func (*ListHandle) Handle

func (h *ListHandle) Handle() *Handle

Handle returns the generic handle that this `ListHandle` is tied to

func (*ListHandle) Insert

func (h *ListHandle) Insert(value interface{}) error

Insert tries to insert an element into the wren list at the end

func (*ListHandle) InsertAt

func (h *ListHandle) InsertAt(index int, value interface{}) error

InsertAt tries to insert an element into the wren list at index `index`

func (*ListHandle) Set

func (h *ListHandle) Set(index int, value interface{}) error

Set tries to set the value in the Wren list at the index `index`

func (*ListHandle) VM

func (h *ListHandle) VM() *VM

VM returns the vm that this handle belongs to

type LoadModuleFn

type LoadModuleFn func(vm *VM, name string) (source string, ok bool)

LoadModuleFn is called by Wren whenever `import` is called. It takes the name of a module and returns the modules source code. If the module cannot be loaded, setting `ok` to false will send an error to the VM

type MapHandle

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

MapHandle is a handle to a map object in Wren

func (*MapHandle) Copy

func (h *MapHandle) Copy() (*MapHandle, error)

Copy creates a new `MapHandle` tied to this Wren map, if the previous one is freed the new one should still persist

func (*MapHandle) Count

func (h *MapHandle) Count() (int, error)

Count counts how many elements are in the Wren map

func (*MapHandle) Delete

func (h *MapHandle) Delete(key interface{}) (interface{}, error)

Delete removes a value from the Wren map with the key `key`

func (*MapHandle) Free

func (h *MapHandle) Free()

Free releases the handle tied to it. The handle should be freed when no longer in use. The handle should not be used after it has been freed

func (*MapHandle) Func

func (h *MapHandle) Func(signature string) (*CallHandle, error)

Func creates a callable handle from the Wren object tied to the current handle. There isn't currently a way to check if the function referenced from `signature` exists before calling it

func (*MapHandle) Get

func (h *MapHandle) Get(key interface{}) (interface{}, error)

Get tries to return the value in the Wren map with the key `key`

func (*MapHandle) Handle

func (h *MapHandle) Handle() *Handle

Handle returns the generic handle that this `MapHandle` is tied to

func (*MapHandle) Has

func (h *MapHandle) Has(key interface{}) (bool, error)

Has check if a wren map has a value with the key `key`

func (*MapHandle) Set

func (h *MapHandle) Set(key, value interface{}) error

Set tries to set the value in the Wren map with the key `key`

func (*MapHandle) VM

func (h *MapHandle) VM() *VM

VM returns the vm that this handle belongs to

type MaxBindingsReached

type MaxBindingsReached struct {
	VM *VM
}

func (*MaxBindingsReached) Error

func (err *MaxBindingsReached) Error() string

type MethodMap

type MethodMap map[string]ForeignMethodFn

MethodMap is a map containing `ForeignMethodFn`s organized by signatures.

Signatures have specific syntax in order for wren to know which function to use.

- If the function is static then it will begin with the string "static " (note the space after it).

- The name of the function is required to match how it will look in Wren

- Next will be an open parenthesis("(")

- for the amount of expected parameters, add underscores separated by comma. do not add a trailing comma after the last underscore or any spaces.

- close everything up with a closing parenthesis (")")

For example:

- A static function called "foo" with 3 parameters will look like "static foo(_,_,_)"

- A function that isn't static called "bar" with no parameters will look like "static bar()"

func (MethodMap) Clone

func (methods MethodMap) Clone() MethodMap

Clone creates a copy of the current `MethodMap`

func (MethodMap) Merge

func (methods MethodMap) Merge(source MethodMap) MethodMap

Merge goes through all items in the source `MethodMap` and adds them if they are not nil

type Module

type Module struct {
	ClassMap ClassMap
}

Module contains a `ClassMap` which is a map containing foreign classes (or classes where objects are made in Go and not Wren) organized by class name

func NewModule

func NewModule(classes ClassMap) *Module

NewModule creates a new `Module` from the given `ClassMap`

func (*Module) Clone

func (module *Module) Clone() *Module

Clone creates a copy of all classes this `Module` references

type ModuleMap

type ModuleMap map[string]*Module

ModuleMap is a map containing Module organized by module names

func (ModuleMap) Clone

func (modules ModuleMap) Clone() ModuleMap

Clone creates a copy clone of all modules and classes this `ModuleMap` references

func (ModuleMap) Merge

func (modules ModuleMap) Merge(source ModuleMap) ModuleMap

Merge goes through all items in the source `ModuleMap` and adds them if they are not nil

type NilHandleError

type NilHandleError struct {
}

NilHandleError is returned if there was an attempt to use a `Handle` that was freed already

func (*NilHandleError) Error

func (err *NilHandleError) Error() string

type NilVMError

type NilVMError struct{}

NilVMError is returned if there was an attempt to use a VM that was freed already

func (*NilVMError) Error

func (err *NilVMError) Error() string

type NoSuchModule

type NoSuchModule struct {
	Module string
}

NoSuchModule is returned when `GetVariable` cannot find a module

func (*NoSuchModule) Error

func (err *NoSuchModule) Error() string

type NoSuchVariable

type NoSuchVariable struct {
	Module, Name string
}

NoSuchVariable is returned when `GetVariable` cannot get a variable from a module

func (*NoSuchVariable) Error

func (err *NoSuchVariable) Error() string

type NonMatchingVM

type NonMatchingVM struct{}

NonMatchingVM is returned if there was an attempt to use a handle in a VM that it did not originate from

func (*NonMatchingVM) Error

func (err *NonMatchingVM) Error() string

type OutOfBounds

type OutOfBounds struct {
	List  *ListHandle
	Index int
}

OutOfBounds is returned if there was an attempt to access a lists value at an index that hasn't been set yet

func (*OutOfBounds) Error

func (err *OutOfBounds) Error() string

type ResolveModuleFn

type ResolveModuleFn func(vm *VM, importer, name string) (newName string, ok bool)

ResolveModuleFn is called by wren whenever `import` is called but runs before LoadModuleFn. It takes the file that called the import as well as the name of the mofule to import and returns a string that will then be put into ResolveModule. If modules name cannot be resolved, setting `ok` to false will send an error to the VM

type ResultCompileError

type ResultCompileError struct{}

ResultCompileError is returned from `InterpretString` or `InterpretFile` if there were problems compiling the Wren source code

func (*ResultCompileError) Error

func (err *ResultCompileError) Error() string

type ResultRuntimeError

type ResultRuntimeError struct{}

ResultRuntimeError is returned from `InterpretString`, `InterpretFile`, or `Call` if there was a problem during script execution

func (*ResultRuntimeError) Error

func (err *ResultRuntimeError) Error() string

type RunningVMError

type RunningVMError struct{}

func (*RunningVMError) Error

func (err *RunningVMError) Error() string

type RuntimeError

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

RuntimeError is sent by Wren to `ErrorFn` if the vm encountered an error during script execution

func (*RuntimeError) Error

func (err *RuntimeError) Error() string

type StackTrace

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

StackTrace is sent by Wren to `ErrorFn` after sending `RuntimeError` these help try to pinpoint how and where an error occurred

func (*StackTrace) Error

func (err *StackTrace) Error() string

type UnexpectedValue

type UnexpectedValue struct {
	Value interface{}
}

UnexpectedValue is returned if Wren did not create the correct type (probably might panic from being out of memory or something before that but just to be safe)

func (*UnexpectedValue) Error

func (err *UnexpectedValue) Error() string

type UnknownForeign

type UnknownForeign struct {
	Handle *ForeignHandle
}

UnknownForeign is returned if a foreign value was not set by WrenGo

func (*UnknownForeign) Error

func (err *UnknownForeign) Error() string

type VM

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

VM is an instance of Wren's virtual machine

func NewVM

func NewVM() *VM

NewVM creates a new instance of Wren's virtual machine with blank configurations

func (*VM) Abort

func (vm *VM) Abort(err error)

Abort stops the running Wren fiber and throws the error passed to it

func (*VM) Free

func (vm *VM) Free()

Free destroys the wren virtual machine and frees all handles tied to it. The VM should be freed when no longer in use. The VM should not be used after it has been freed

func (*VM) FreeAll

func (vm *VM) FreeAll(items ...interface{})

FreeAll can take any argument type. It filters through and calls free on any handles passed. It does not free anything else

func (*VM) GC

func (vm *VM) GC()

GC runs the garbage collector on the `VM`

func (*VM) GetVariable

func (vm *VM) GetVariable(module, name string) (interface{}, error)

GetVariable tries to get a variable from the Wren vm with the given module name and variable name. This function checks that `HasVariable` is true to prevent segfaults

func (*VM) GetVariableUnsafe

func (vm *VM) GetVariableUnsafe(module, name string) interface{}

GetVariableUnsafe is like `GetVariable` but does not perform any checks to ensure that things aren't null (This function will segfault if things don't exist)

func (*VM) HasModule

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

HasModule tries to check that a module has been imported or resolved before

func (*VM) HasVariable

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

HasVariable tries to check that a variable from the Wren vm with the given module name and variable name exists. This function checks that `HasModule` is true to prevent segfaults

func (*VM) InterpretFile

func (vm *VM) InterpretFile(fileName string) error

InterpretFile compiles and runs wren source code from the given file. the module name would be set to the `fileName`, This function should not be called if the VM is currently running.

func (*VM) InterpretString

func (vm *VM) InterpretString(module, source string) error

InterpretString compiles and runs wren source code from `source`. the module name of the source can be set with `module`. This function should not be called if the VM is currently running.

func (*VM) IsRunning

func (vm *VM) IsRunning() bool

IsRunning returns true if the current VM is running (Whether `InterpretString`, `InterpretFile`, and any `CallHandle`s have been called on this VM)

func (*VM) Merge

func (vm *VM) Merge(moduleMap ModuleMap)

Merge combine all non nil values from `moduleMap` to the vm's own module map (If a vm already imported classes and methods from any module already, changing it again won't set the previously imported values)

func (*VM) NewList

func (vm *VM) NewList() (*ListHandle, error)

NewList creates a new empty list object in wren and returns it's handle

func (*VM) NewMap

func (vm *VM) NewMap() (*MapHandle, error)

NewMap creates a new empty map object in wren and returns it's handle

func (*VM) SetModule

func (vm *VM) SetModule(name string, module *Module)

SetModule sets a foreign module for wren to import from (If a vm already imported classes and methods from this module already, changing it again won't set the previously imported values)

type WriteFn

type WriteFn func(vm *VM, text string)

WriteFn is called by wren whenever `System.write`, `System.print`, or `System.printAll` is called in a script

Jump to

Keyboard shortcuts

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