func SetErrorWriter(w io.Writer) {
	errWriter = w
}
SetErrorWriter sets the writer to be used for script error output. If this method is never
called (or called with nil), it uses standard error.
package wren
import "C"
import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"reflect"
	"runtime"
	"strings"
	"unsafe"
)
var (
	vmMap     = make(map[*C.WrenVM]*VM)
	errWriter io.Writer
)
type VM struct {
	vm               *C.WrenVM
	classes, methods map[string]unsafe.Pointer
	userData         map[string]interface{}
	userDataPtr      unsafe.Pointer
	outWriter        io.Writer
}
func NewVM() *VM {
	var config C.WrenConfiguration
	C.wrenInitConfiguration(&config)
	config.writeFn = C.WrenWriteFn(C.write)
	config.bindForeignMethodFn = C.WrenBindForeignMethodFn(C.bindMethod)
	config.bindForeignClassFn = C.WrenBindForeignClassFn(C.bindClass)
	config.errorFn = C.WrenErrorFn(C.writeErr)
	config.loadModuleFn = C.WrenLoadModuleFn(C.loadModule)
	vm := VM{vm: C.wrenNewVM(&config)}
	vm.classes = make(map[string]unsafe.Pointer)
	vm.methods = make(map[string]unsafe.Pointer)
	vm.userData = make(map[string]interface{})
	vmMap[vm.vm] = &vm
	runtime.SetFinalizer(&vm, func(vm *VM) {
		C.wrenFreeVM(vm.vm)
		delete(vmMap, vm.vm)
	})
	return &vm
}
func (vm *VM) SetModulesDir(path string) {
	vm.setUserData("MODULES_DIR", path)
}
func (vm *VM) setUserData(key string, val interface{}) {
	vm.userData[key] = val
	if jval, e := json.Marshal(vm.userData); e == nil {
		if vm.userDataPtr != nil {
			C.free(vm.userDataPtr)
		}
		vm.userDataPtr = unsafe.Pointer(C.CString(string(jval)))
		C.wrenSetUserData(vm.vm, vm.userDataPtr)
	}
}
func (vm *VM) RegisterForeignMethod(fullName string, f interface{}) error {
	ptr, err := registerFunc(fullName, func() {
		if err := handleFunction(vm.vm, f); err != nil {
			panic(err)
		}
	})
	if err != nil {
		return err
	}
	vmMap[vm.vm].methods[fullName] = ptr
	return nil
}
func (vm *VM) RegisterForeignClass(className string, f func() interface{}) error {
	ptr, err := registerFunc(className, func() {
		newForeign(vm.vm, f())
	})
	if err != nil {
		return err
	}
	vmMap[vm.vm].classes[className] = ptr
	return nil
}
func (vm *VM) SetOutputWriter(w io.Writer) {
	vmMap[vm.vm].outWriter = w
}