Documentation
¶
Index ¶
- Variables
- func BytePtrToString(p *byte) string
- func Init(libraryPath string) error
- func InitFromDirectory(libraryBaseDirectoryPath string) error
- func VersionNumber() (int, int, int)
- type CallHandle
- type Config
- type GoForeignFunction
- type SlotType
- type VM
- func (vm *VM) CallHandle(module, object, signature string) (*CallHandle, error)
- func (vm *VM) Free() error
- func (vm *VM) HasModule(moduleName string) bool
- func (vm *VM) HasVariable(module, objectName string) bool
- func (vm *VM) Run(moduleName, src string) error
- func (vm *VM) RunFile(fsys fs.FS, fpath string) error
- func (vm *VM) Variable(module, objectName string) any
Constants ¶
This section is empty.
Variables ¶
var ErrCompileTime = errors.New("error compiling wren script")
InterpretResultSuccess InterpretResult = iota
var ErrVMFreed = errors.New("error: virtual machine already freed")
Functions ¶
func BytePtrToString ¶
func Init ¶
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 ¶
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 ¶
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 (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.
type GoForeignFunction ¶
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 VM ¶
type VM struct {
// contains filtered or unexported fields
}
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) HasVariable ¶
HasVariable returns true if the VM has a variable of the given name in the module specified.
func (*VM) Run ¶
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.