deno_vm API reference

A Python 3 to Deno + vm binding, helps you execute JavaScript safely.

Also checkout the readme.

Functions

deno_vm.eval(code, **options)

A shortcut to eval JavaScript.

Parameters:
  • code (str) – The code to be run.

  • options – Additional options sent to VM.

This function will create a VM, run the code, and return the result.

Classes

class deno_vm.BaseVM(server=None, console='off')

BaseVM class, containing some common methods for VMs.

Parameters:

server (VMServer) – Optional. If provided, the VM will be created on the server. Otherwise, the VM will be created on a default server, which is started on the first creation of VMs.

__enter__()

This class can be used as a context manager, which automatically create() when entering the context.

__exit__(exc_type, exc_value, traceback)

See destroy()

create()

Create the VM.

destroy()

Destroy the VM.

class deno_vm.VM(code='', server=None, console='off', **options)

Create VM instance.

Parameters:
  • code (str) – Optional JavaScript code to run after creating the VM. Useful to define some functions.

  • server (VMServer) – Optional VMServer. See BaseVM for details.

  • console (str) – Optional. Can be “off”, “inherit”, “redirect”. If set to “redirect”, console events would be put into event_que.

  • options – Other options for VM.

event_que

A queue.Queue object containing console events.

An event is a dict and you can get the text value with:

event = self.event_que.get()
text = event.get("value")
run(code)

Execute JavaScript and return the result.

If the server responses an error, a VMError will be raised.

call(function_name, *args)

Call a function and return the result.

Parameters:
  • function_name (str) – The function to call.

  • args – Function arguments.

function_name may include “.” to call functions on an object.

class deno_vm.VMServer(command=None)

VMServer class, represent vm-server. See start() for details.

Parameters:

command (str) –

the command to spawn subprocess. If not set, it would use:

  1. Environment variable DENO_EXECUTABLE

  2. ”deno”

__enter__()

This class can be used as a context manager, which automatically start() the server.

server = VMServer()
server.start()
# create VMs on the server...
server.close()

vs.

with VMServer() as server:
    # create VMs on the server...
__exit__(exc_type, exc_value, traceback)

See close().

start()

Spawn a subprocess and run vm-server.

vm-server is a REPL server, which allows us to connect to it with stdios. You can find the script at deno_vm/vm-server (Github).

Communication using JSON:

> {"id": 1, "action": "create", "type": "VM"}
{"id": 1, "status": "success"}

> {"id": 2, "action": "run", "code": "var a = 0; a += 10; a"}
{"id": 2, "status": "success", "value": 10}

> {"id": 3, "action": "xxx"}
{"id": 3, "status": "error", "error": "Unknown action: xxx"}

A VMError will be thrown if the process cannot be spawned.

close()

Close the server. Once the server is closed, it can’t be re-open.