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.16.tar.gz (46.1 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.13 Windows x86-64

stpyv8-13.0.245.16-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.16-cp313-cp313-macosx_14_0_arm64.whl (17.6 MB view details)

Uploaded CPython 3.13 macOS 14.0+ ARM64

stpyv8-13.0.245.16-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.16-cp312-cp312-win_amd64.whl (14.1 MB view details)

Uploaded CPython 3.12 Windows x86-64

stpyv8-13.0.245.16-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.16-cp312-cp312-macosx_14_0_arm64.whl (17.6 MB view details)

Uploaded CPython 3.12 macOS 14.0+ ARM64

stpyv8-13.0.245.16-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.16-cp311-cp311-win_amd64.whl (14.1 MB view details)

Uploaded CPython 3.11 Windows x86-64

stpyv8-13.0.245.16-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.16-cp311-cp311-macosx_14_0_arm64.whl (17.6 MB view details)

Uploaded CPython 3.11 macOS 14.0+ ARM64

stpyv8-13.0.245.16-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.16-cp310-cp310-win_amd64.whl (14.1 MB view details)

Uploaded CPython 3.10 Windows x86-64

stpyv8-13.0.245.16-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.16-cp310-cp310-macosx_14_0_arm64.whl (17.6 MB view details)

Uploaded CPython 3.10 macOS 14.0+ ARM64

stpyv8-13.0.245.16-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.16-cp39-cp39-win_amd64.whl (14.1 MB view details)

Uploaded CPython 3.9 Windows x86-64

stpyv8-13.0.245.16-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.16-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.16.tar.gz.

File metadata

  • Download URL: stpyv8-13.0.245.16.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.16.tar.gz
Algorithm Hash digest
SHA256 b49b04d35a3d680c11dc82239c452d2596569a3a825cef4078a0f06f55f41891
MD5 46b4266407498dc021d96ccb11d93e6c
BLAKE2b-256 5306ba881a6f43b00dc1f9021ad700f89659062e7bfcb4c8782bb30f635c107d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 95ce6c0f1f14c68e20b8b6c4ab1b5d766c05545973bba6c6ebd76547a87fb585
MD5 5a34ec679caa5647cd72e569ab9728f9
BLAKE2b-256 17a5b8d316803f9a862d9bc00578083bdaea3eb46902248194310278e3033a1f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp313-cp313-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 148c19c016bc753c8c0a4e52e8c35ddb252455ef48a1da9aad7a570b087bb216
MD5 351b069a5fcceb32269c478c49efacee
BLAKE2b-256 fb3dbdeea3c2edd9c643ea499bd63d5e5214805231968cf2aca41e462cf4eeb7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 d6ce86c50751c22e6e59ff94923dcf9684520267ce392d3c70ec4c72ff38fa0a
MD5 34f8f5057def5a41778e4acb29bc8838
BLAKE2b-256 0c341a0daf7318a13c212f7a9abe9428d1ec3f76487e435dad84e9fccec7cbe6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 8c2c16df01610cc87cae616993c69016aefb998d9370a517830e77bdc90f2f8b
MD5 42051fb315adb2e4b58caf8be686ce3a
BLAKE2b-256 0f5a1434f5fd1fcc83c60aa08083e03d58d664e1854acafbc5b3913c3d9771c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9e2f38343bd734be77efd4ce5a4442b4cef5e152232ea5f605c2265cbbb26241
MD5 a45207df78d31526c7749a19d01d030e
BLAKE2b-256 cbaf5fb09f5b02131274aa7c4c8f010c5ceac63e7c87d1fef76f10c3734e5501

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp312-cp312-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 56a3c15ec6ef1c80b0a84add428b512ef45afc71fde74634441abe3dff6d523b
MD5 2e5f38884cc51fde97d1918aee6c9099
BLAKE2b-256 6f57b546f7ca321e291edf9a0eeda20414d68ad365dae1d900297626a1d88ad1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 3cd811661c09bc298620c929b2a911e955bf537f78d3e2c6e298685857bfd530
MD5 eb7f4c7d20c6294a25c8a93455e76e58
BLAKE2b-256 12922af581de2bbf5cabe38b4423e95153a42b6aa143089654453e816a036945

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp312-cp312-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 41873d12906a06965d605b8d7efdd9f2c50bb92ea786b69e3e6767a62655bf3e
MD5 615952b3a99f8f42e45c698e6f9e0115
BLAKE2b-256 647bbe52912fdc32499f082250e808a68480b76681fbe9e23a9946ad5ccda41f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 858a2f5fa4672a0f3a0b172c9c1bb5c194388d71806daff7121182f97cad502b
MD5 5153def536e6821fc13e47e99b18c2ff
BLAKE2b-256 44438d5c75d0795de77f6cefb4748c79cecf532f4022bcab568e9ad5ab73d253

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp311-cp311-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 cabc8a55d7d2a7b5b5ad9d793ff3cbe042ce41b07cbdc140521731c855c00abf
MD5 bfad3a9eefadda9f49bd792094416055
BLAKE2b-256 6df84ce4a22f5fb9ceaa3529537dfdd6120ad551195bfbeac0701f68ed0406ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 1e58895494048835a6d59848bec565dd3d43f4aefd8f263a30cc7ac0c7b250db
MD5 2b9b3a8822559366361dc9e03bfdf079
BLAKE2b-256 f21b2d2c26b7b2c55ed91d0ea87c720bbe8e5ceef3b9463429ecf4f9c01376dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp311-cp311-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 9236b373557feb4364125042243608cbd2c29674bbb4eb6be543488be1563e6f
MD5 b24a8d27bce765accbb2b13f6a0b9e6c
BLAKE2b-256 8fa853d55e2903ba436d136396545e03feca3ad780fdf656e6c0b1c8b1816a2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3716a904ee015e2b1a3ab4a5ed32a9f36916c6af64ccaf4f49a03b3dd72f646c
MD5 e2b6895e8987aa6d56b735f839523195
BLAKE2b-256 ec1b73f121fd38e31c37a84336be457c3d9cce419ab388c06228cc6eaa025fba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp310-cp310-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 17600f9d3c25e67b0ab8779008ec363ea152a51ffb9decb9658b29808c35c361
MD5 81221f46798add711b27f18ec99b9061
BLAKE2b-256 02349d5086ff5d8219c4387c25c251a44a0edf01cadfa06376fb60620197caed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 0c2a286230449c70fcbbc9a9b380f6dfcf59056c911af0350f1bdabdff419634
MD5 e8147ac3e921796500d56b67a50459ac
BLAKE2b-256 eb6e2bb02e0bca7b680150225f9f9369c2fabcc587abb2aa17a68843ab54b30e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp310-cp310-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 d2af3db36de10ff0000cf65042ee4cd9dfaea47c2ac788238f7e09f45fc3916d
MD5 19129b034dc0e339cd4a9edbc10ea873
BLAKE2b-256 48ed4ddaeab95f6c7931762130fd0112699be5eea13f71e3a7e248518c8da88d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 702e439b77af550925617f4dda2e7b4a34737c8c2d9d6768e79cb54ed2ae2ea1
MD5 18c28468f09790ad376ab0c863e45d10
BLAKE2b-256 d286d1b224734ed477481f556492d03f0a56c68aa19ed282e920933e992ab8b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 ebc63571ac9fa1db6fb31f9858050867700068564730a3eb1d8be335be6eee99
MD5 4e667b0ea09bf0414b0511637dbf0d77
BLAKE2b-256 5436baf2a0887b7036e255bcd7c1fbb56997c5fad4434c78c64f1ed4311bff4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-13.0.245.16-cp39-cp39-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 ef81c1b13ba7d328c2259a9056b763c539aea16a35d7d52922baf767fe486650
MD5 9966d488448bba29d0022c61ef36559e
BLAKE2b-256 fb6769b42a5ac6792da0041804021b9c57fcbc685c04b88ca6c0ac5eacb809b8

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