Git Product home page Git Product logo

deno_vm's Introduction

deno_vm

Documentation Status test

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

How it works

The module launches a Deno REPL server, which can be communicated with JSON. All JavaScript code are encoded in JSON and sent to the server. After the server executing the code in vm, the result is sent back to Python.

Install

Install Deno

Follow the instruction here: https://docs.deno.com/runtime/manual/getting_started/installation

Also make sure deno command works as expected: https://docs.deno.com/runtime/manual/getting_started/installation#testing-your-installation

Install deno_vm

pip install deno_vm

Usage

Most of the APIs are bound to worker-vm.

Simple eval:

from deno_vm import eval

print(eval("['foo', 'bar'].join()"))

Use VM:

from deno_vm import VM

with VM() as vm:
   vm.run("""
      var sum = 0, i;
      for (i = 0; i < 10; i++) sum += i;
   """)
   print(vm.run("sum"))

It is possible to do async task with Promise:

from datetime import datetime
from deno_vm import VM

js = """
function test() {
   return new Promise(resolve => {
      setTimeout(() => {
         resolve("hello")
      }, 3000);
   });
};
"""
with VM() as vm:
   vm.run(js)
   print(datetime.now())
   print(vm.call("test"))
   print(datetime.now())

API reference

http://deno_vm.readthedocs.io/

Changelog

  • 0.6.0 (Mar 4, 2024)
    • Change: use --unstable-worker-options instead of --unstable.
    • Change: vendor deno dependencies. Now deno_vm doesn't require network and filesystem write access.
    • Fix: suppres cleanup error.
    • Fix: improve uninitialized error message.
  • 0.5.1 (Oct 10, 2023)
    • Fix: unable to pass initial code to VM().
  • 0.5.0 (Oct 10, 2023)
    • Switch to deno_vm.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.