A feature complete emscripten "port" of the Wren virtual machine.
WrenVM provides an object-oriented interface to the underlying Wren VM.
id
number This is the emscripten numerical pointer to a WrenVM instance.config
object This optional object can provide a writeFn(string) or an errorFn(type, module, line, message) handler.WrenJS.addEventListener('ready', () => {
var vm = WrenJS.newVM({
writeFn: (text) => {
console.log("stdout: %s", text)
},
errorFn: (type, module, line, message) => {
console.log("stderr(%d): %s %d %s", type, module, line, message)
}
})
vm.interpret("main", 'System.print("Hello, xorld!")')
.then(result => {
switch(result) {
case WrenJS.RESULT_COMPILE_ERROR:
console.log("Compilation error!")
break
case WrenJS.RESULT_RUNTIME_ERROR:
console.log("Runtime error!")
break
case WrenJS.RESULT_SUCCESS:
console.log("Success!")
break
}
// vm.call(...), etc.
vm.free()
})
})
Runs the VM interpreter in the given module space with the provided source.
vm.interpret('main', 'System.print("Hello, xorld!")').then(result => {
// Handle result.
})
Returns Promise
Calls the given handle. This presumes Wren slots have been set up appropriately.
handle
numbervm.ensureSlots(2)
vm.getVariable("my_module", "myClass", 0)
var handle = vm.makeCallHandle("classMethod()")
vm.call(handle)
vm.releaseHandle(handle)
Frees up the VM. You must call releaseHandle
on all your handles first.
Adds a foreign method to the VM.
vm.addForeignMethod("wren/ForeignClass", "ForeignClass", 0, "add(_,_)", function() {
var a = vm.getSlotDouble(1)
var b = vm.getSlotDouble(2)
vm.setSlotDouble(0, a+b)
})
Adds a foreign class allocator and finalizer.
vm.addForeignClassMethods("wren/ForeignClass", "ForeignClass", function() {
// Allocator
vm.setSlotNewForeign(0, 0, 0)
}, function() {
// Finalizer
})
Ensures that the VM has the defined amount of slots available for use.
count
numberGets the amount of slots currently used.
Returns number
Returns the WrenType stored in a given slot.
slot
numberReturns number
Returns a floating point number stored in a given slot.
slot
numberReturns number
Sets the given slot to the provided floating point number.
Returns a string stored in the given slot.
slot
numberReturns string
Sets the given slot to the provided string.
Returns a Uint8Array of bytes stored in the given slot.
slot
numberReturns Uint8Array
Sets the given slot to the provided TypedArray. Provided data will be converted to a Uint8Array.
slot
numbertypedArray
TypedArrayReturns the boolean value of a given slot.
slot
numberReturns boolean
Sets the given slot to the provided boolean value.
Sets the given slot to null.
slot
numberReturns a foreign object from the provided slot.
slot
numberReturns number
Sets the given slot to a new foreign object with an optional size for extra bytes storage.
Returns number pointer to the extra bytes.
Sets the given slot to a new list.
slot
numberReturns the number of items in a list in the given slot.
slot
numberReturns number
Moves an element from the given listSlot’s index to target elementSlot.
Inserts the provided element in elementSlot into the index position in the list at slot.
Gets a variable of the provided name in a module and sets it to the given slot.
Returns the handle in the given slot.
slot
numberReturns number
Sets the given slot to the given handle.
Creates and returns a handle that is usable in functions such as call(handle)
.
You must call releaseHandle(handle)
before you free the VM.
signature
stringvm.ensureSlots(2)
vm.getVariable("my_module", "myClass", 0)
var handle = vm.makeCallHandle("foreignMethod()")
vm.call(handle)
vm.releaseHandle(handle)
Returns number
Release the given handle from the VM.
handle
numberCalls the VM’s garbage collection routine.
Aborts the fiber.
slot
numberLoads a given file via XHR and adds it to the importedFiles map. Must be called before interpreting. Does nothing if WrenJS+ was built with DISABLE_JSVM_IMPORT.
file
stringvm.importFile("wren/testImport.wren")
.then(result => {
vm.interpret(...)
})
Returns Promise
Calls importFile(…) on an array of strings.
vm.importFiles(["wren/testImport.wren", "wren/testImport2.wren"])
.then(result => {
vm.interpret(...)
})
Returns Promise
WrenJS is the interface through which Wren VMs are created. Be aware that this is actually an Emscripten Module. See preamble.js for additional information.
Creates and returns a new WrenVM instance.
config
object Provides a config object that can contain errorFn and/or writeFn.Returns WrenVM
WrenInterpretResult is the resulting value of a call to WrenVM.interpret.
Type: number
RESULT_COMPILE_ERROR
number? Compilation error.RESULT_RUNTIME_ERROR
number? Runtime error.RESULT_SUCCESS
number? Success.WrenType represents a WrenVM data type.
Type: number
TYPE_BOOL
number? Boolean type.TYPE_NUM
number? Numerical type.TYPE_STRING
number? String type.TYPE_LIST
number? List type.TYPE_FOREIGN
number? Foreign object type.TYPE_NULL
number? Null type.TYPE_UNKNOWN
number? Unknown type.WrenErrorType represents a given error type that will be passed to WrenVM.errorFn.
Type: number
ERROR_COMPILE
number? Compile-time error.ERROR_RUNTIME
number? Run-time error.ERROR_STACK_TRACE
number? Stack-trace for a given error.LibraryWren provides the JavaScript functions available to C.
Provides the C to WrenJS to WrenVM interface for handling Wren’s error function.
Provides the C to WrenJS to WrenVM interface for handling Wren’s write function.
Returns a VM’s foreign method to C.
Returns function
Returns a VM’s foreign class allocator to C.
Returns function
Returns a VM’s foreign class finalizer(destructor) to C.
Returns function
Returns whether or not a file has been defined as a wren script in the document’s head. This is not used if WrenJS+ was built with ALLOW_NONSCRIPT_FETCH.
file
stringReturns boolean
Requests a VM-defined result for a Wren import statement. This is not used if WrenJS+ was built with DISABLE_JSVM_IMPORT.