deno_vm API reference¶
A Python 3 to Deno + vm binding, helps you execute JavaScript safely.
Also checkout the readme.
Functions¶
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.
- 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.
- 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:
Environment variable
DENO_EXECUTABLE
”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...
- 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.