Documentation ¶
Index ¶
- Variables
- type Config
- type ErrorType
- type Handle
- type InterpretResult
- type Type
- type VM
- func (vm VM) AbortError(err error)
- func (vm VM) AbortFiber(slot int)
- func (vm VM) Allocated() int
- func (vm VM) Call(handle Handle) InterpretResult
- func (vm VM) CollectGarbage()
- func (vm VM) EnsureSlots(numSlots int)
- func (vm VM) Exit()
- func (vm VM) Free()
- func (vm VM) GetBool(slot int) bool
- func (vm VM) GetBytes(slot int, writer io.Writer) error
- func (vm VM) GetForeign(slot int) any
- func (vm VM) GetHandle(slot int) Handle
- func (vm VM) GetListElement(listSlot, index, elementSlot int)
- func (vm VM) GetMapValue(mapSlot, keySlot, valueSlot int)
- func (vm VM) GetNum(slot int) float64
- func (vm VM) GetString(slot int) string
- func (vm VM) GetVariable(module, name string, slot int)
- func (vm VM) HasModule(module string) bool
- func (vm VM) HasVariable(module, name string) bool
- func (vm VM) InsertInList(listSlot, index, elementSlot int)
- func (vm VM) Interpret(module, source string) InterpretResult
- func (vm VM) ListCount(slot int) int
- func (vm VM) MapCount(slot int) int
- func (vm VM) MapHasKey(mapSlot, keySlot int) bool
- func (vm VM) NewCallHandle(signature string) Handle
- func (vm VM) ReleaseHandle(handle Handle)
- func (vm VM) RemoveMapValue(mapSlot, keySlot, valueSlot int)
- func (vm VM) SetBool(slot int, value bool)
- func (vm VM) SetBytes(slot int, data []byte)
- func (vm VM) SetHandle(slot int, handle Handle)
- func (vm VM) SetListElement(listSlot, index, elementSlot int)
- func (vm VM) SetMapValue(mapSlot, keySlot, valueSlot int)
- func (vm VM) SetNewForeign(slot, classSlot int, obj interface{})
- func (vm VM) SetNewList(slot int)
- func (vm VM) SetNewMap(slot int)
- func (vm VM) SetNull(slot int)
- func (vm VM) SetNum(slot int, value float64)
- func (vm VM) SetString(slot int, value string)
- func (vm VM) SetUserdata(data interface{})
- func (vm VM) SlotCount() int
- func (vm VM) SlotType(slot int) Type
- func (vm VM) UserData() any
- func (vm VM) VersionNumber() int
- func (vm VM) VersionString() string
- func (vm VM) VersionTuple() [3]int
- type WrenError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var StdErr io.Writer
StdErr is where error messages from any context is printed to. This may contain debug information from the incorrect use of wren.
var StdOut io.Writer
StdOut is where output from any context is printed to.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // ResolveModuleFn allows the host to canonicalize the imported name beofre // it is passed to LoadModuleFn. // // It takes the name of the module doing the import and the target module to // import. It should return the new name to be passed to LoadModuleFn as // well as if the import is ok. If an import cannot be resolved, this // function should return false. ResolveModuleFn func(vm VM, importer, name string) (newName string, ok bool) // LoadModuleFn load a modules source code. It takes the name of the module // to import and returns the wren source code as well as whether the module // was successfully loaded. LoadModuleFn func(vm VM, name string) (src string, ok bool) // BindForeignMethodFn binds a foreign function to a class method in wren. // // It is passed the module, class name, and call signature of the function // as well as whether the function is static. // // Returning nil signifies that such function does not exist, raising an // error in wren // // The call signature's format varies between the type of function it is but // usually begins with the function name and ends with the amount of // parameters indicated as underscores. // // - Regular functions enclose the parameters in parenthesis: // "function(_,_,_)" // - Getters do not have anything following the name: "function" // - Setters have an equal sign before the parameters surrounded by // parenthesis (and should only have one parameter): "function=(_)" // - Indexes have no names but parameters surrounded by brackets: // "[_,_,_]" // - Index setters also have no name but parameters surrounded by // brackets. It also has an equal sign and one parameter surrounded by // parenthesis: "[_,_,_]=(_)" // - Operators begin with the operation followed by one parameter // surrounded by parenthesis: "+(_)", ">=(_)", "<<(_)" // BindForeignMethodFn func(vm VM, module, class, signature string, static bool) func(VM) // BindForeignClassMethodFn binds the allocator and finalizer of a class. // // It takes the name of the class and it's module and should return two // functions: one called during the creation of a foreign object (when a // constructor is called) and one called when the foreign object is garbage // collected. Setting the allocator to nil signifies an error to wren but // finalizer is optional and can be set to nil to indicate no finalizer // function. BindForeignClassMethodFn func(vm VM, module, class string) (allocator func(VM), finalizer func(VM, interface{})) // WriteFn is called when "System.write", "System.print", and related // functions have been called in wren. WriteFn func(vm VM, message string) // ErrorFn is called whenever acompilation or runtime error has occurred in // wren. The error value should always be of type WrenError. ErrorFn func(vm VM, err error) // InitialHeapSize is the number of bytes wren will allocate before // triggering the first garbage collection. // // Set this to zero to use wren's default value InitialHeapSize uint // MinHeapSize is the minimum size threshold after a garbage collection has // occured. This ensures that the heap does not get to small. // // Set this to zero to use wren's default value MinHeapSize uint // HeapGrowthPercent determines the amount of additional memory the heap // will grow to after garbage collection. This is specified as a percent of // the current heap size with a value like 50 increasing the heap size by // 50% // // The smaller this value is means the less memory wasted but the garbage // collector may be triggered more often // // Set this to zero to use wren's default value HeapGrowthPercent int }
Example (Foreign) ¶
type ID struct { module, class, signature string bool } cfg := wren.Config{ WriteFn: func(vm wren.VM, message string) { println(message) }, ErrorFn: func(vm wren.VM, err error) { println(err.Error()) }, LoadModuleFn: func(vm wren.VM, name string) (src string, ok bool) { src, ok = map[ID]string{ {module: "foreign"}: ` foreign class Foreign { foreign static greet(name) construct new() { System.print("Hello world") } } `, }[ID{module: name}] return }, BindForeignMethodFn: func(vm wren.VM, module, class, signature string, static bool) func(wren.VM) { return map[ID]func(wren.VM){ {"foreign", "Foreign", "greet(_)", true}: func(v wren.VM) { if v.SlotCount() != 2 { // slot 0 is the "Foreign" class v.AbortError(errors.New("Expected 1 parameter")) } if v.SlotType(1) != wren.TypeString { v.AbortError(errors.New("Expected parameter 1 to be string")) } println("Hello", v.GetString(1)) }, }[ID{module, class, signature, static}] }, } vm := wren.NewVM(cfg) vm.Interpret("main", ` import "foreign" for Foreign Foreign.greet("wren") `)
Output:
type Handle ¶
type Handle struct {
// contains filtered or unexported fields
}
Handle is used to keep a reference to a value in the VM as to prevent it from being garbage collected and to refer to it later in foreign functions, but can also be used to call methods from the VM.
func (*Handle) Call ¶
func (h *Handle) Call() InterpretResult
Call calls this handle, using the receiver in slot 0 and arguments from slot 1 or greater.
The handle should have been created using NewCallHandle.
Note: that wren is not reentrant. You should not call this function from a foreign method or while the VM is currently running.
type InterpretResult ¶
type InterpretResult int32
InterpretResult represents the result of running code in the VM.
const ( // ResultSuccess represents no errors ResultSuccess InterpretResult = iota // ResultCompileError represents an error during the compilation of a module ResultCompileError // ResultRuntimeError represents an error during the running of a module. ResultRuntimeError )
type VM ¶
type VM struct {
// contains filtered or unexported fields
}
VM interacts with the wren virtual machine.
Example ¶
cfg := wren.Config{ WriteFn: func(vm wren.VM, message string) { fmt.Print(message) }, } vm := wren.NewVM(cfg) vm.Interpret("main", `System.print("Hello from wren!")`)
Output: Hello from wren!
func (VM) AbortError ¶
AbortError is a helper function that takes a Go error and uses it to abort wren.
func (VM) AbortFiber ¶
AbortFiber stops the current fiber using the value in slot as the error value.
func (VM) Allocated ¶
Allocated returns the amount of bytes allocated by the wren VM. This does not include foreign objects or functions as those are still handled by Go and it doesn't reflect the entire amount of memory needed to run this instance of VM but it is useful to compare against the original version of wren.
func (VM) Call ¶
func (vm VM) Call(handle Handle) InterpretResult
CallHandle calls the handle, using the receiver in slot 0 and arguments from slot 1 or greater.
The handle should have been created using NewCallHandle.
Note: that wren is not reentrant. You should not call this function from a foreign method or while the VM is currently running.
func (VM) CollectGarbage ¶
func (vm VM) CollectGarbage()
CollectGarbage runs the garbage collector for wren.
func (VM) EnsureSlots ¶
EnsureSlots ensures that the callstack has enough slots to accomadate the value passed.
Each slot coorosponds to one receiver or parameter
func (VM) Exit ¶
func (vm VM) Exit()
Exit attempts to interrupt wren while it is running and exit. The VM has not been tested to verify that it's state is usable afterwards so it is recommended to free it, though your mileage may vary.
func (VM) Free ¶
func (vm VM) Free()
Free disposes all data used by the VM. This isn't always necessary and won't immediately free it's data because the VM's memory is still managed by Go and must be garbage collected by Go. However, this function garbage collects any remaining foreign objects existing in the VM to ensure their finalizers are called.
This VM should no longer be used after calling this function.
func (VM) GetBytes ¶
GetBytes retrieves string data from wren and writes it into writer. Error values from writer are returned.
func (VM) GetForeign ¶
GetForeign returns the foreign object referenced in the current slot. It's value reflects what was passed from SetNewForeign.
func (VM) GetHandle ¶
GetSlotHandle creates a new handle from the value in the slot provided. Handles prevent a value from being garbage collected.
func (VM) GetListElement ¶
GetListElement places an element at the specified index from a list located in listSlot into elementSlot.
func (VM) GetMapValue ¶
GetMapValue puts the element at index of keySlot from the map in mapSlot into valueSlot.
func (VM) GetVariable ¶
GetVariable retreives a variable from the VM and stores it in the specified slot.
func (VM) HasModule ¶
HasModule returns true if the module has previously been imported or interpreted.
func (VM) HasVariable ¶
HasVariable returns true if the value exists in this module.
func (VM) InsertInList ¶
InsertInList inserts an element into the list at the specified index.
Negative values are supported to index an array from the end.
func (VM) Interpret ¶
func (vm VM) Interpret(module, source string) InterpretResult
Interpret runs the source code in a new fiber in the resolved module.
Note: that wren is not reentrant. You should not call this function from a foreign method or while the VM is currently running.
func (VM) NewCallHandle ¶
NewCallHandle creates a handle used to invoke methods in wren. When the handle is no longer needed, you should free the handle using ReleaseHandle.
The Handle is reusable and is not locked to a specific receiver. Instead the receiver must be added to the callstack by setting slot 0.
Note: that wren is not reentrant. You should not call this handle from a foreign method or while the VM is currently running.
The call signature's format varies between the type of function it is but usually begins with the function name and ends with the amount of parameters indicated as underscores.
- Regular functions enclose the parameters in parenthesis: "function(_,_,_)"
- Getters do not have anything following the name: "function"
- Setters have an equal sign before the parameters surrounded by parenthesis (and should only have one parameter): "function=(_)"
- Indexes have no names but parameters surrounded by brackets: "[_,_,_]"
- Index setters also have no name but parameters surrounded by brackets. It also has an equal sign and one parameter surrounded by parenthesis: "[_,_,_]=(_)"
- Operators begin with the operation followed by one parameter surrounded by parenthesis: "+(_)", ">=(_)", "<<(_)"
func (VM) ReleaseHandle ¶
ReleaseHandle releases and frees up this handle. Handles prevent values from being garbage collected so releasing a handle removes this referance to that value, allowing it to be garbage collected when all references are gone.
func (VM) RemoveMapValue ¶
RemoveMapValue removes an element from the map. If the element was found, it is put into valueSlot. If not, then valueSlot will be set to null.
func (VM) SetListElement ¶
SetListElement sets a lists element at the specified index from a list locates in listSLot to the value locates at elementSlot.
func (VM) SetMapValue ¶
SetMapValue sets the element at index of keySlot from the map in mapSlot into valueSlot.
func (VM) SetNewForeign ¶
SetNewForeign creates a new foreign object in wren.
func (VM) SetNewList ¶
SetNewList creates an empty list and sets it in the slot provided.
func (VM) SetUserdata ¶
func (vm VM) SetUserdata(data interface{})
SetUserdata sets the user data for this VM
func (VM) SlotCount ¶
SlotCount returns the numbers of slots currently available to the current foreign method.