wrenjs-plus

A feature complete emscripten "port" of the Wren virtual machine.

View the Project on GitHub kettek/wrenjs-plus

Table of Contents

WrenVM

WrenVM provides an object-oriented interface to the underlying Wren VM.

Parameters

Examples

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()
  })
})

interpret

Runs the VM interpreter in the given module space with the provided source.

Parameters

Examples

vm.interpret('main', 'System.print("Hello, xorld!")').then(result => {
  // Handle result.
})

Returns Promise

call

Calls the given handle. This presumes Wren slots have been set up appropriately.

Parameters

Examples

vm.ensureSlots(2)
vm.getVariable("my_module", "myClass", 0)
var handle = vm.makeCallHandle("classMethod()")
vm.call(handle)
vm.releaseHandle(handle)

free

Frees up the VM. You must call releaseHandle on all your handles first.

addForeignMethod

Adds a foreign method to the VM.

Parameters

Examples

vm.addForeignMethod("wren/ForeignClass", "ForeignClass", 0, "add(_,_)", function() {
  var a = vm.getSlotDouble(1)
  var b = vm.getSlotDouble(2)
  vm.setSlotDouble(0, a+b)
})

addForeignClassMethods

Adds a foreign class allocator and finalizer.

Parameters

Examples

vm.addForeignClassMethods("wren/ForeignClass", "ForeignClass", function() {
  // Allocator
  vm.setSlotNewForeign(0, 0, 0)
}, function() {
  // Finalizer
})

ensureSlots

Ensures that the VM has the defined amount of slots available for use.

Parameters

getSlotCount

Gets the amount of slots currently used.

Returns number

getSlotType

Returns the WrenType stored in a given slot.

Parameters

Returns number

getSlotDouble

Returns a floating point number stored in a given slot.

Parameters

Returns number

setSlotDouble

Sets the given slot to the provided floating point number.

Parameters

getSlotString

Returns a string stored in the given slot.

Parameters

Returns string

setSlotString

Sets the given slot to the provided string.

Parameters

getSlotBytes

Returns a Uint8Array of bytes stored in the given slot.

Parameters

Returns Uint8Array

setSlotBytes

Sets the given slot to the provided TypedArray. Provided data will be converted to a Uint8Array.

Parameters

getSlotBool

Returns the boolean value of a given slot.

Parameters

Returns boolean

setSlotBool

Sets the given slot to the provided boolean value.

Parameters

setSlotNull

Sets the given slot to null.

Parameters

getSlotForeign

Returns a foreign object from the provided slot.

Parameters

Returns number

setSlotNewForeign

Sets the given slot to a new foreign object with an optional size for extra bytes storage.

Parameters

Returns number pointer to the extra bytes.

setSlotNewList

Sets the given slot to a new list.

Parameters

getListCount

Returns the number of items in a list in the given slot.

Parameters

Returns number

getListElement

Moves an element from the given listSlot’s index to target elementSlot.

Parameters

insertInList

Inserts the provided element in elementSlot into the index position in the list at slot.

Parameters

getVariable

Gets a variable of the provided name in a module and sets it to the given slot.

Parameters

getSlotHandle

Returns the handle in the given slot.

Parameters

Returns number

setSlotHandle

Sets the given slot to the given handle.

Parameters

makeCallHandle

Creates and returns a handle that is usable in functions such as call(handle). You must call releaseHandle(handle) before you free the VM.

Parameters

Examples

vm.ensureSlots(2)
vm.getVariable("my_module", "myClass", 0)
var handle = vm.makeCallHandle("foreignMethod()")
vm.call(handle)
vm.releaseHandle(handle)

Returns number

releaseHandle

Release the given handle from the VM.

Parameters

collectGarbage

Calls the VM’s garbage collection routine.

abortFiber

Aborts the fiber.

Parameters

importFile

Loads 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.

Parameters

Examples

vm.importFile("wren/testImport.wren")
.then(result => {
  vm.interpret(...)
})

Returns Promise

importFiles

Calls importFile(…) on an array of strings.

Parameters

Examples

vm.importFiles(["wren/testImport.wren", "wren/testImport2.wren"])
.then(result => {
  vm.interpret(...)
})

Returns Promise

WrenJS

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.

newVM

Creates and returns a new WrenVM instance.

Parameters

Returns WrenVM

WrenInterpretResult

WrenInterpretResult is the resulting value of a call to WrenVM.interpret.

Type: number

Properties

WrenType

WrenType represents a WrenVM data type.

Type: number

Properties

WrenErrorType

WrenErrorType represents a given error type that will be passed to WrenVM.errorFn.

Type: number

Properties

LibraryWren

LibraryWren provides the JavaScript functions available to C.

WrenJS_errorFn

Provides the C to WrenJS to WrenVM interface for handling Wren’s error function.

Parameters

WrenJS_writeFn

Provides the C to WrenJS to WrenVM interface for handling Wren’s write function.

Parameters

WrenJS_getForeignMethod

Returns a VM’s foreign method to C.

Parameters

Returns function

WrenJS_getForeignClassAllocator

Returns a VM’s foreign class allocator to C.

Parameters

Returns function

WrenJS_getForeignClassFinalizer

Returns a VM’s foreign class finalizer(destructor) to C.

Parameters

Returns function

WrenJS_isFileAvailable

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.

Parameters

Returns boolean

WrenJS_importFileFromVM

Requests a VM-defined result for a Wren import statement. This is not used if WrenJS+ was built with DISABLE_JSVM_IMPORT.

Parameters