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

Uploaded Source

Built Distributions

stpyv8-12.9.202.27-cp313-cp313-win_amd64.whl (14.0 MB view details)

Uploaded CPython 3.13 Windows x86-64

stpyv8-12.9.202.27-cp313-cp313-manylinux_2_31_x86_64.whl (24.8 MB view details)

Uploaded CPython 3.13 manylinux: glibc 2.31+ x86-64

stpyv8-12.9.202.27-cp313-cp313-macosx_14_0_arm64.whl (17.5 MB view details)

Uploaded CPython 3.13 macOS 14.0+ ARM64

stpyv8-12.9.202.27-cp313-cp313-macosx_13_0_x86_64.whl (19.1 MB view details)

Uploaded CPython 3.13 macOS 13.0+ x86-64

stpyv8-12.9.202.27-cp312-cp312-win_amd64.whl (14.0 MB view details)

Uploaded CPython 3.12 Windows x86-64

stpyv8-12.9.202.27-cp312-cp312-manylinux_2_31_x86_64.whl (24.8 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.31+ x86-64

stpyv8-12.9.202.27-cp312-cp312-macosx_14_0_arm64.whl (17.5 MB view details)

Uploaded CPython 3.12 macOS 14.0+ ARM64

stpyv8-12.9.202.27-cp312-cp312-macosx_13_0_x86_64.whl (19.1 MB view details)

Uploaded CPython 3.12 macOS 13.0+ x86-64

stpyv8-12.9.202.27-cp311-cp311-win_amd64.whl (14.0 MB view details)

Uploaded CPython 3.11 Windows x86-64

stpyv8-12.9.202.27-cp311-cp311-manylinux_2_31_x86_64.whl (24.7 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.31+ x86-64

stpyv8-12.9.202.27-cp311-cp311-macosx_14_0_arm64.whl (17.5 MB view details)

Uploaded CPython 3.11 macOS 14.0+ ARM64

stpyv8-12.9.202.27-cp311-cp311-macosx_13_0_x86_64.whl (19.3 MB view details)

Uploaded CPython 3.11 macOS 13.0+ x86-64

stpyv8-12.9.202.27-cp310-cp310-win_amd64.whl (14.0 MB view details)

Uploaded CPython 3.10 Windows x86-64

stpyv8-12.9.202.27-cp310-cp310-manylinux_2_31_x86_64.whl (24.7 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.31+ x86-64

stpyv8-12.9.202.27-cp310-cp310-macosx_14_0_arm64.whl (17.5 MB view details)

Uploaded CPython 3.10 macOS 14.0+ ARM64

stpyv8-12.9.202.27-cp310-cp310-macosx_13_0_x86_64.whl (19.1 MB view details)

Uploaded CPython 3.10 macOS 13.0+ x86-64

stpyv8-12.9.202.27-cp39-cp39-win_amd64.whl (14.0 MB view details)

Uploaded CPython 3.9 Windows x86-64

stpyv8-12.9.202.27-cp39-cp39-manylinux_2_31_x86_64.whl (24.7 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.31+ x86-64

stpyv8-12.9.202.27-cp39-cp39-macosx_13_0_x86_64.whl (19.1 MB view details)

Uploaded CPython 3.9 macOS 13.0+ x86-64

File details

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

File metadata

  • Download URL: stpyv8-12.9.202.27.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-12.9.202.27.tar.gz
Algorithm Hash digest
SHA256 8d9f461ebca6a16fc0f2ad35e2583f886b868dfb2c2bffcc19d35f6449629a1b
MD5 306cb87f0738fc386a8e9ce089199b33
BLAKE2b-256 80f36ab66049a0ba51f8b131c643005731bbc9b26a07ff1cbc17e2d2839b3f2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 99be9b242ba37a8077a2eeaebb4e3a365fcb435db72bba36965212eef78bdd46
MD5 22b47a5b3950d02df5aaf99824132fd2
BLAKE2b-256 dd8c0394bd66937869aee65d4d4f3159d416908d5139b3901a69e2e1c03c6faa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp313-cp313-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 30f1e240498b390521f8ffe8d5d77641dd6667aea6ff91a8bc4a179229e03877
MD5 baada49b6194481cdde74e4860a518d9
BLAKE2b-256 35a553695e203a757cc2460c29852893637d4b0dcc030eb1dae243d40db7cfce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 bc48986cb7b7583cc6a604a9e5de59f670856985a9c5bc37ec8e0d4c5358c45a
MD5 e80e3b136ca771bb64c53013a6048da7
BLAKE2b-256 c40ce9e0937c480325a60c1f621c8769ccf5358eefdd38f5bf2f9d074d30f2bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 d23e25b0bc6d75867188f8d95d91b7a6192f0e6a9a891060e9e82e76b9522206
MD5 93d72c4292fe18febef7e4e55c931c17
BLAKE2b-256 67893ecec1e993d7e228caa5b0293fc9ca1bd683a41d83cef5b9ec2e3d958fb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6740ca9c63ef330c625cfaf9398047886473f9efd42db988e2b3fba721174dc3
MD5 820d1db01a7c0a2e30411edac889c5e7
BLAKE2b-256 97379b8defbf077286ae405c729a0fb8b4b13e5e5edd74f8f9fe1e10a010b3cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp312-cp312-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 2edc21b9574b57da8055612bd391695555bdc969171e54d64f4b1f4113f0ed0d
MD5 84f4cc6e69145ae0a77f79c3d181e568
BLAKE2b-256 52ceab6d3b46de19cc5b5e26a311291cbddcc2f727652c360d51be2e181ed938

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 0e246730d371a5e6a9143903d731bc8f977c586aeb3644362bf034e1314658fa
MD5 08dfbc9c24aaa463ff332ba2380a5d6f
BLAKE2b-256 f145ed19fcbc8e9c7e17ea6ffe16bc84f50913be5af502a210861bf5467bff59

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp312-cp312-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 018071df311464662b9ab206efbd597c750835f0bebdc5fa00989e91bf150ff9
MD5 1ec5703810dc002d47e2c4a9d5af9b7c
BLAKE2b-256 09b0a677818f1b4bb3b4e3438b9ff6c6c6b9b42326f8082434927bab40017753

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 581fe3da5fa430248c9c081f3a0bd67929838fd62d439a111e89e1f50ca0e7ac
MD5 6c1051e571d4bd0c99595f37869b293a
BLAKE2b-256 346afe0799df14f54f23e788259e3fa6f53cbb5236b26abb116ed3c8bd604c19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp311-cp311-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 524b5c597904aa35330c40e681a677eb2fdab0bce983c00d4d290717303b23e3
MD5 de8ab74ae07aec7e83a85bee1cc65142
BLAKE2b-256 dcab0514dd79e8c572d3e5cf98e715f579cd24d9851b15cea34ce5267d10aa73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 8127eaf3581fc6bc7105d170f87abe0fc7bd8bf89ff53345215625e572fc633c
MD5 43d817c6d50264dd6078b0573b7d320c
BLAKE2b-256 13979d27c841672eed5234c7ab91eb21cd59055b3e16ce87af6b868be60b7651

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp311-cp311-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 6a5f2ebf019d81632d8a716b1c2cd5748221c811e2ea825a706031b10a074560
MD5 334ee4859aa5b6bf839bf169e8d3877b
BLAKE2b-256 b71e6fb623c12ae4f2eec7626b2475ff7594a3e6b318e6805056707e56e39e25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6b1bad993134359b698bc2be9dabccc56f2a2430b2c3c4f083c690ec35a9035d
MD5 d70823c2a69e027ea3d379e06f2c2f9b
BLAKE2b-256 43e6ea72373cbe0dca080a4cdbf93a84edae8348596bc5618073786670a598f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp310-cp310-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 dec5005f75136895646593fc728f9e6a2b54ef37ccba209d0a3104a4bc08dacf
MD5 b99bd89c2d8d1966f2867076355bc5e7
BLAKE2b-256 636123bf79d22588b96f7947f8beb2538599924259492b1dc6883c6966f2e88e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 4a45d879c51724537c372a95d151b3e1360a20446543a61d6f32d4ee82910f4b
MD5 bb8ce42ad1e92dea4bcfefa30808238b
BLAKE2b-256 0163353164403d8ae0b3315088ed055efa7bb1cb6dd658b837b8f508c574b8ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp310-cp310-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 2c324073c5e2d8c713705ea806419187bf8998528a74eeef1f451c52bc1c5495
MD5 2e98e5fff4195d29e6e45c25d465b648
BLAKE2b-256 379741fcae4cea89658b3a7a2781c204629fd2d4647e40ce863efbc09d1bb790

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 493fa6981b4da77ec2d97300bfb69b235789cab7f6786f354a50f1decd49c574
MD5 22abf2b4d003f4da5c6dad1140230aee
BLAKE2b-256 1ebf2ccb0b8f467fe663e2c5cdd224421bbc5e4dbbf8a8350f4cd492a009ea3e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 1d37f175272454e0fcd990f10eaeca2034639c067fe97e2094e49ba00139af8d
MD5 fc675d86f61115b7a2011e2766d69e08
BLAKE2b-256 27358c293469dc953ec28b86e9f351df12d3db4927e188c16b866a7552782148

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stpyv8-12.9.202.27-cp39-cp39-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 d4d50b50e59845fd12b07ceb6682df4260519bc75fde7c17be583b1b6c10fbf0
MD5 dad0afa9528cdbba34e6dc2450d4b001
BLAKE2b-256 69acdef7313ecf2c2d9491608c061c6136ba041ef16d3eff87ee8c23f90a182f

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