A vm
library built on top of Deno's worker.
import { VM } from "https://deno.land/x/[email protected]/mod.ts";
const vm = new VM(); // create a new VM Worker
console.log(await vm.run("1 + 1")); // run code in the worker
await vm.run(`
function sum(a, b) {
return a + b;
} `); // define a function
console.log(await vm.call("sum", 2, 3)) // 5
vm.close(); // terminate the worker
--unstable
- permission options in worker is an unstable feature.--allow-read=path/to/worker.ts
- to launch the worker.
Although Deno is able to block disk/network access, we can't prevent the code from calling self.close()
. If this happens, the worker will be terminated. All calls to vm.run
will timeout.
Data must be cloneable to be passed to the worker:
VM.call
- function arguments are JSON-cloned.- The result of
VM.run
- throws an error if it is not cloneable. VM.on("console")
- function arguments are JSON-cloned.
- vm2 - discontinued.
- isolated-vm - a much more powerful vm that runs in node. However, it has to be built from source when installing.
-
0.2.0 (Oct 10, 2023)
- Add: capture console output.
-
0.1.2 (Oct 10, 2023)
- Update reamde.
-
0.1.0 (Oct 9, 2023)
- Initial release.