Skip to main content

Web Assembly Virtual Machine

The @devicescript/vm package contains DeviceScript C virtual machine compiled to Web Assembly. It allows you to run bytecode in node.js and browsers.

This package is used in the CLI and in developer tools web page.

npm install --save @devicescript/vm

import

Loading the virtual machine is async and should typically be cached in a global variable.

Node.js

import type { DevsModule } from "@devicescript/vm"

const vmLoader = require("@devicescript/vm")
const vm: DevsModule = await vmLoader()
vm.devsInit()

Browser

import type { DevsModule } from "@devicescript/vm"
import vmLoader from "@devicescript/vm"

const vm: DevsModule = await vmLoader()
vm.devsInit()

or import https://microsoft.github.io/devicescript/dist/devicescript-vm.js for the latest build.

devsSetDeviceId

Specifies the device id of the virtual machine device. This method should be called before starting the virtual machine.

vm.devsSetDeviceId("1989f4eee0ebe206")

devsStart

Starts the virtual machine. Does nothing if already running.

const res = vm.devsStart()
if (res) console.error("failed to start", res)

devsStop

Stops the virtual machine. Does nothing if already stopped.

vm.devsStop()

devsIsRunning

Indicates if the virtual machine is started.

const running = vm.devsIsRunning()

devsInit

This method allocates data structures necessary for running the virtual machine. It is automatically called by other methods.

vm.devsInit()

devsVerify

Verifies that a buffer of bytecode is well-formed. Returns non-zero error codes when failing.

const res = vm.devsVerify()
if (res) console.error("failed to verify", res)

devsClearFlash

Clear persistent "flash" storage.

vm.devsClearFlash()