Skip to main content

Python Wrapper for Google V8 Engine

Project description

STPyV8

STPyV8 allows interoperability between Python 3 and JavaScript running Google V8 engine. Use STPyV8 to embed JavaScript code directly into your Python project, or to call Python code from JavaScript.

STPyV8 is a fork of the original PyV8 project, with code changed to work with the latest Google V8 engine and Python 3. STPyV8 links with Google V8 built as a static library.

Currently the library builds on Linux, MacOS and Windows.

Usage Examples

Wrapping a JavaScript function in a Python function:

# simple.py
import STPyV8

with STPyV8.JSContext() as ctxt:
  upcase = ctxt.eval("""
    ( (lowerString) => {
        return lowerString.toUpperCase();
    })
  """)
  print(upcase("hello world!"))
$ python simple.py
HELLO WORLD!

Using Python in V8

STPyV8 allows you to use Python functions, classes, and objects from within V8.

Exporting a Python class into V8 and using it from JavaScript:

# meaning.py
import STPyV8

class MyClass(STPyV8.JSClass):
  def reallyComplexFunction(self, addme):
    return 10 * 3 + addme

my_class = MyClass()

with STPyV8.JSContext(my_class) as ctxt:
  meaning = ctxt.eval("this.reallyComplexFunction(2) + 10;")
  print("The meaning of life: " + str(meaning))
$ python meaning.py
The meaning of life: 42

Using JavaScript in Python

STPyV8 allows you to use JavaScript functions, classes, object from Python.

Calling methods on a JavaScript class from Python code:

# circle.py
import STPyV8

with STPyV8.JSContext() as ctxt:
  ctxt.eval("""
    class Circle {
      constructor(radius) {
        this.radius = radius;
      }
      get area() {
        return this.calcArea()
      }
      calcArea() {
        return 3.14 * this.radius * this.radius;
      }
  }
  """)
  circle = ctxt.eval("new Circle(10)")
  print("Area of the circle: " + str(circle.area))
$ python cicle.py
Area of the circle: 314

Find more in the tests directory.

Installing

STPyV8 is avaliable on PyPI (starting from release v12.0.267.16) and officially supports Python 3.9+

$ pip install stpyv8

Be aware that, starting from STPyV8 v12.0.267.14, installing boost-python and some other boost dependencies is not required anymore while it is still required if you're installing older versions (see later for details). Most Linux distributions and MacOS provide easy to install Boost packages and this is the suggested way to install the library in case you need it.

If you are planning to install a version older than v12.0.267.16 you should use one of the Python wheels provided at Releases. The wheels are automatically generated using Github Actions and multiple platforms and Python versions are supported. In such case, you need to download the zip file for the proper platform and Python version. Each zip file contains the ICU data file icudtl.dat and the wheel itself. First of all you should copy icudtl.data to the STPyV8 ICU data folder (Linux: /usr/share/stpyv8, MacOS: /Library/Application Support/STPyV8/) and then install/upgrade STPyV8 using pip.

Installing on MacOS

$ unzip stpyv8-macos-10.15-python-3.9.zip
Archive:  stpyv8-macos-10.15-python-3.9.zip
  inflating: stpyv8-macos-10.15-3.9/icudtl.dat
  inflating: stpyv8-macos-10.15-3.9/stpyv8-9.9.115.8-cp39-cp39-macosx_10_15_x86_64.whl
$ cd stpyv8-macos-10.15-3.9
$ sudo mv icudtl.dat /Library/Application\ Support/STPyV8
$ pip install --upgrade stpyv8-9.9.115.8-cp39-cp39-macosx_10_15_x86_64.whl
Processing ./stpyv8-9.9.115.8-cp39-cp39-macosx_10_15_x86_64.whl
Installing collected packages: stpyv8
Successfully installed stpyv8-9.9.115.8

If no wheels are provided for your platform and Python version you are required to build STPyV8.

Building

GCC/clang or equivalent and Python3 headers are needed to build the main STPyV8 source code, as well as boost-python and some other boost dependencies.

Build Examples

Ubuntu/Debian

Building on Ubuntu and Debian distros:

$ sudo apt install python3 python3-dev build-essential libboost-dev libboost-system-dev libboost-python-dev libboost-iostreams-dev
$ python setup.py build
$ sudo python setup.py install

Building on other Linux distributions requires appropriate use of their package managers for these external dependencies, and some gymnastics for the V8 build dependencies.

MacOS

Building on MacOS requires full XCode (not just the command line tools) to compile Google V8. The command line tools bundled with XCode are required (rather than the stand-alone command line tools, sometimes requiring drastic measures .)

Using HomeBrew makes the boost-python and related dependencies easier for STPyV8:

$ brew install boost-python3
$ python setup.py build
$ sudo python setup.py install

More detailed build instructions are in the docs folder.

Windows

Please note that building STPyV8 on Windows may take a while and can be a pretty involved process. You're encouraged to download a precompiled binary from this repository.

Here are the prerequisites for building on Windows:

  • MSVC 14.20 (packaged with Visual Studio 2019)
  • Boost

The following environment variables must be set:

  • BOOST_ROOT - Boost installation directory (e.g. C:\local\boost_1_83_0)
  • Python_ROOT_DIR - Python installation directory (e.g. C:\python311)

Boost installation with precompiled binaries

If your Boost installation comes with precompiled binaries you'll have to make sure they can be used to build this project.

The binaries required are statically-linkable LIB files compiled with MSVC 14.20 and may look something like this (in a Boost 1.82 installation):

boost_python310-vc142-mt-s-x64-1_82.lib

If you were able to locate a similar file for your Python version you might not need to build Boost.

If the LIB file you found is not located in the directory $env:BOOST_DIR\stage\lib then you must add its containing directory path to the LIB environment variable.

For example, if you installed Boost through an official installer, the LIB file might be in the lib64-msvc-14.2 directory. In this case: $env:LIB = "$env:LIB;$env:BOOST_ROOT\lib64-msvc-14.2".

If you weren't able to located the correct file, or you encountered linking errors further down the build process, you'll have to build Boost. Here is an example of one such linking error:

LINK : fatal error LNK1104: cannot open file 'libboost_python310-vc142-mt-s-x32-1_74.lib'

Building Boost

To build the Boost.Python component of Boost with Powershell Developer Console:

cd $env:BOOST_ROOT
.\bootstrap.bat

Before building you must tell Boost which Python version you're building for. To do this, add the following line to the end of project-config.jam:

using python : : "C:\\python311" ;

NOTE: Use the actual path to your Python installation and ensure backslases are escaped. This directory should include python.exe, an include directory and a libs directory.

Back to Powershell:

.\b2.exe stage -j 8 link=static runtime-link=static --with-python --with-iostreams --with-date_time --with-thread

The boost binaries will be generated to $env:BOOST_ROOT\stage\lib.

Building STPyV8

Once you've got your Boost binaries you're ready to build STPyV8.

From Powershell, cd into the project root:

python -m pip install wheel
python setup.py bdist_wheel

Once the second command is done (may take quite a while) you'll have a wheel file ready to be installed.

How does this work?

STPyV8 is a Python C++ Extension Module that links to an embedded V8 library. Since PyV8 used the Boost.Python C++ library (as wells as some others) we kept it, but future work may include just using the C API exposed by Python and eliminating boost. Think of this as an Oreo cookie - Python and Google V8 crackers with C++ icing in the middle gluing them together.

Is STPyV8 fast?

STPyV8 needs to translate Python arguments (and JavaScript arguments) back and forth between function and method calls in the two languages. It does the minimum amount of work using native code, but if you are interested in the height of performance, make your interface between Python and JavaScript "chunky" ... i.e., make the minimum number of transitions between the two.

What can I use this for?

We use STPyV8 to simulate a browser, and then execute sketchy JavaScript in an instrumented container. Other kinds of JavaScript sandboxing (simulating and monitoring the external world to JavaScript code) are a natural fit.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stpyv8-13.0.245.18.tar.gz (46.1 kB view details)

Uploaded Source

Built Distributions

stpyv8-13.0.245.18-cp313-cp313-win_amd64.whl (14.1 MB view details)

Uploaded CPython 3.13 Windows x86-64

stpyv8-13.0.245.18-cp313-cp313-manylinux_2_31_x86_64.whl (25.0 MB view details)

Uploaded CPython 3.13 manylinux: glibc 2.31+ x86-64

stpyv8-13.0.245.18-cp313-cp313-macosx_14_0_arm64.whl (17.6 MB view details)

Uploaded CPython 3.13 macOS 14.0+ ARM64

stpyv8-13.0.245.18-cp313-cp313-macosx_13_0_x86_64.whl (19.2 MB view details)

Uploaded CPython 3.13 macOS 13.0+ x86-64

stpyv8-13.0.245.18-cp312-cp312-win_amd64.whl (14.1 MB view details)

Uploaded CPython 3.12 Windows x86-64

stpyv8-13.0.245.18-cp312-cp312-manylinux_2_31_x86_64.whl (25.0 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.31+ x86-64

stpyv8-13.0.245.18-cp312-cp312-macosx_14_0_arm64.whl (17.6 MB view details)

Uploaded CPython 3.12 macOS 14.0+ ARM64

stpyv8-13.0.245.18-cp312-cp312-macosx_13_0_x86_64.whl (19.2 MB view details)

Uploaded CPython 3.12 macOS 13.0+ x86-64

stpyv8-13.0.245.18-cp311-cp311-win_amd64.whl (14.1 MB view details)

Uploaded CPython 3.11 Windows x86-64

stpyv8-13.0.245.18-cp311-cp311-manylinux_2_31_x86_64.whl (24.9 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.31+ x86-64

stpyv8-13.0.245.18-cp311-cp311-macosx_14_0_arm64.whl (17.6 MB view details)

Uploaded CPython 3.11 macOS 14.0+ ARM64

stpyv8-13.0.245.18-cp311-cp311-macosx_13_0_x86_64.whl (19.5 MB view details)

Uploaded CPython 3.11 macOS 13.0+ x86-64

stpyv8-13.0.245.18-cp310-cp310-win_amd64.whl (14.1 MB view details)

Uploaded CPython 3.10 Windows x86-64

stpyv8-13.0.245.18-cp310-cp310-manylinux_2_31_x86_64.whl (24.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.31+ x86-64

stpyv8-13.0.245.18-cp310-cp310-macosx_14_0_arm64.whl (17.6 MB view details)

Uploaded CPython 3.10 macOS 14.0+ ARM64

stpyv8-13.0.245.18-cp310-cp310-macosx_13_0_x86_64.whl (19.2 MB view details)

Uploaded CPython 3.10 macOS 13.0+ x86-64

stpyv8-13.0.245.18-cp39-cp39-win_amd64.whl (14.1 MB view details)

Uploaded CPython 3.9 Windows x86-64

stpyv8-13.0.245.18-cp39-cp39-manylinux_2_31_x86_64.whl (24.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.31+ x86-64

stpyv8-13.0.245.18-cp39-cp39-macosx_13_0_x86_64.whl (19.2 MB view details)

Uploaded CPython 3.9 macOS 13.0+ x86-64

File details

Details for the file stpyv8-13.0.245.18.tar.gz.

File metadata

  • Download URL: stpyv8-13.0.245.18.tar.gz
  • Upload date:
  • Size: 46.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for stpyv8-13.0.245.18.tar.gz
Algorithm Hash digest
SHA256 bc1c2b3fdaf4952933456d40b28d6faf474c6c0a3883317a0449882f5a3ad3ae
MD5 10caf0a60ba23ed12472f4c49cf0d91f
BLAKE2b-256 b82578e098784ee7e55e3733a51c4d7284320ac9659790b02bfbdc3017d50266

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 addbe27979ce97c76963f7336b9d90ba1dc85120376d186f5d0dfbb4190ab60f
MD5 2faf81922d7718559ba04a64334d38c4
BLAKE2b-256 ae1716fe42daa413cc59920f5ba41e5b3563cea131a8001f4605e40d0b64514a

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp313-cp313-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp313-cp313-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 f260bed7796f4df711ce9794715db92f54660927ae8545586e1175c958dffd83
MD5 1c9e8fdd0504f59ee4bcbe2a57a9265a
BLAKE2b-256 d84a47cf391ab896fe1798b930d5498116f7968877301b89592435a4cec7a8ec

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 0fae02f03dc7c6778b8b4d194b219742a835c34127e996caba8efd5e12f2be4f
MD5 cb6d37e5336b15e87440b5d2ae995a7f
BLAKE2b-256 2cb977769b1d2e123330b55db69b44dffc128d0c1eff61d7d7d67cb2efe65889

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp313-cp313-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 a8d04f487de404ff7940024b35a52aa5c187963f891c293002fe580c0dacea03
MD5 a5390ba9430b44f48846044107b5355d
BLAKE2b-256 a2ece78b8e1e0d81cfe5bcdc5b8ad735936f8e5d34447a93a66840d4c207b595

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6aa61be5c971b5d56487b07d27f41fc6fc8dadad7906a51da4ec5e129bb82e1e
MD5 f39ce1abf48eb09135142f320aac4a32
BLAKE2b-256 9ec62c6bca6c76768666b281433d19203b08f9d9188e47e27796fdf61a527bf8

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp312-cp312-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp312-cp312-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 1a1788b76266aa5b17c89998173396537077a5bc571c1fc284ea9bc0965922b3
MD5 063cd87dfa25e8987b77ffe7ad0a0892
BLAKE2b-256 ae63d4f204cc17b2aefa1dbe9b13b0caef18758fe2ae920dda65f36d730e688d

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 014e925291cbc4a8acd47f2b75033f49d4251ffa4ca583f65c4b8e43244a02d6
MD5 bdad017f0f8a6de52d7705276ddea9e4
BLAKE2b-256 5e58e8376d96ef71518ca75b097bad1ef9fea5f29bf534d1e484a81f15e40b81

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp312-cp312-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp312-cp312-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 9fe9f3963303358d071a45ad196e3828912a34847414a03d533655492143dbc5
MD5 61dac1c066fb1c863c49ca0763015d37
BLAKE2b-256 f052292e57e3720713bebfef8ef1b3a17e20e51fd774a19a9a60c8f6650f8313

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7a8701c537b468a07e87eb0bdc8b7365007dc70ea98d36ea13b9371bbeed1df3
MD5 632aad4f7c8093232160407220177a7f
BLAKE2b-256 5cfe4e53c993d7e88b32cb274996a1bdac04df3fa669eef43f305fa162e31f51

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp311-cp311-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp311-cp311-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 5baf12d23c5235ba9f69d85835ca0d9f13f5da2a5295bfd02c2d85e4ce702b6a
MD5 c85b2389dbd60a14f63d8eed89c67d66
BLAKE2b-256 8ce90390c2e4cf1dbec12a982fe22ebe596f3b5e08294478f6c94359d4c12d56

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 9d04e576efcfb0ba86c469c9cff5cfaa7c9985901424f080c22c2846c1f16e5a
MD5 4c395004d269b2af821401b6451a88c9
BLAKE2b-256 1ab652fcb24fb0f5a3fbda2f65ba36de81868c4f99f87ab488e682cda89260bf

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp311-cp311-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp311-cp311-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 f37635d186795f1c44d5b832bbf0b04e3e34d7c0bcc5e293cb393fb9553fe54d
MD5 98e030a955bc1e56a7fbf817b3f0fe95
BLAKE2b-256 79055184d1808723f5c124e08266aca7a2aa0edce2ca631cbdd4bd48b38bf979

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1492ce1171408e506526edbbd8ba1fb78bb948b545744d51f7632fa7abe97a31
MD5 80a85e008e44e72031da931ba10fbd81
BLAKE2b-256 9a64eceadabb8ef82b7086f74981ce20b1b50b1e4ef1322df8c573496fabab18

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp310-cp310-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp310-cp310-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 121246962508b3d37ba10e3a66409cfc0b400f08d873520df036d2724d8b80cb
MD5 67e5afdba30420807de7f177ea42929d
BLAKE2b-256 ba79cfe88e17c51ec7f101a7af55b25e04f91d0348e5ddebeacd031895c76446

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp310-cp310-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 2b50be930d393841e43a3551841bccb8fdcee183869cd7b57b880f59b207e5bf
MD5 1557eb95d17d150dfb79f0bcfa8dea9a
BLAKE2b-256 5709758d06a3df72235915435fa113200508c7e01a42a622a11a5638f6cd89d6

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp310-cp310-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp310-cp310-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 c1ff5142f10542ad0ac04e83da31b2b02f113538766f070a7c8a8ef7a5aed9cf
MD5 166f1f9f35e1a132e8a1c33737a938e0
BLAKE2b-256 6091936d202a62e823010a732f568f2976e4ece9d764ac523f1cf924975884c1

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 fbd0e414089fdb3de41e89c0034a27ba1764c878f0e5dee08c823f49c7a310d9
MD5 e1cfa607e528eeca3871cf287471c9e3
BLAKE2b-256 cafbaef2950c43001a05746637751ddcf2472782bdaad7c87e8ed7de44d0e700

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp39-cp39-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 5d894361a3d3d45bceaa38ee735f08d25ba7755a99035824d9441db04cd91df3
MD5 0dfac8ea46716b358238ab12f2bb3b57
BLAKE2b-256 6a6c7defd3e994e713dae912285cdd9c818624d92d9f86d99ece426c6dc7e640

See more details on using hashes here.

File details

Details for the file stpyv8-13.0.245.18-cp39-cp39-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for stpyv8-13.0.245.18-cp39-cp39-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 60dc91468cd8eba84ac1b6b9cc243f7ff0e4cb4c5117a7e87f504624faac10c8
MD5 5f2e3003e866b1dabd355a9c77ea0571
BLAKE2b-256 afdb4c9f020e7fb9b09de9d5e1ed8b339cc5737560b01ac2d1f7120fe1955200

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page