Skip to main content

JSEngine

This is a simple wrapper of Javascript engines, it wraps the Javascript interpreter for Python use.

There are two ways to call interpreters, via dynamic library loading is internal call which is faster than the other one, via subprocess is external call.

  • System's built-in Javascript interpreter:

    macOS: JavascriptCore
    Linux: Gjs on Gnome, CJS on Cinnamon, etc.
    Windows: Chakra (internal call, but not applicable to Windows 7)

  • Python bindings (Recommend, internal call):

    QuickJS
    PyChakra
    PyMiniRacer (V8) (Caused by a scope issue, its work process is similar to external call now, to fix the scope issue, Esprima is needed)

  • Any installed external Javascript interpreters, e.g.

    SpiderMonkey, Node.js, QuickJS, etc.

JSEngine used to be part of YKDL, which created by @coslyk.

Installation

Install from version package format monthly downloads

pip install jsengine

Or download and Install from source code

python setup.py install

Compatibility

  • Python >= 2.7

Usage

import jsengine
jsengine.eval('"Hello, world!"')  # => 'Hello, world!'

Use a JSEngine context.

try:
    ctx1 = jsengine.jsengine()
except jsengine.RuntimeError:
    ...  # do something if useless

if jsengine.JSEngine is None:
    ...  # do something if useless
else:
    ctx2 = jsengine.JSEngine("""
            function add(x, y) {
                return x + y;
            }
            """)

ctx1.eval('1 + 1')  # => 2

# call funtion
ctx2.call("add", 1, 2)  # => 3

# append new script
ctx1.append("""
    function square(x) {
        return x ** 2;
    }
    """)
ctx1.call("square", 9)  # => 81

Use a specified external Javascript interpreter.

binary = binary_name or binary_path
kwargs = {
    'name': 'None or any string',  # see ExternalInterpreterNameAlias.keys()
    'tempfile': True,              # use tempfile or not. Default is False, fallback is True
    'evalstring': True,            # can run command string as Javascript or can not,
                                   # just like '-e script_code'
                                   # instead of True, supported argument can be passed,
                                   # e.g. '--eval', '--execute'
    'args': [args1, args2, ...]    # arguments used for interpreter
}

# case 1
interpreter = jsengine.ExternalInterpreter.get(binary, **kwargs)
if interpreter:
    # found
    ctx = jsengine.ExternalJSEngine(interpreter)

# case 2
if jsengine.set_external_interpreter(binary, **kwargs):
    # set default external interpreter OK
    ctx = jsengine.ExternalJSEngine()

# case 3, maybe get default fallback instead of your specified
try:
    ctx = jsengine.ExternalJSEngine(interpreter=binary, **kwargs)
except jsengine.RuntimeError:
    ...  # do something if useless

Use threading lock. Javascript source itself always be ran in single threaded, that just make the APIs can be used in multithreadeding.

jsengine.set_threading(True)   # MUST enable befor using, it's disabled by default

ctx_quickjs = jsengine.QuickJSEngine()
ctx_chakra = jsengine.ChakraJSEngine()   # internal chakra will create an extra thread per context
ctx_v8 = jsengine.V8JSEngine()
ctx_exter = jsengine.ExternalJSEngine()  # external interpreter will be called one by one with context

...  # do multithreading

jsengine.set_threading(False)  # disable is not necessary

Internal VS. External

QuickJSEngine ChakraJSEngine V8JSEngine (esprima) V8JSEngine ** ExternalJSEngine
Load backend on import import or init init init every fetch result
Loading speed fastest fast very slow with py3 fast very slow
Performance highest high low, if much results low, if much results
Fetch result run the passed run the passed run the passed run all/full source run all/full source

* Fetch results means call eval()/call().
** V8JSEngine is now similar to ExternalJSEngine which caused by scope issue.

License

JSEngine is released under the MIT License.

Release files for jsengine 1.0.7.post1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsengine 1.0.7.post1
File Size Uploaded
jsengine-1.0.7.post1.tar.gz 16.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsengine 1.0.7.post1
File Interpreter ABI Platform
jsengine-1.0.7.post1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 33.7 kB

Release files / jsengine-1.0.7.post1.tar.gz

Download URL jsengine-1.0.7.post1.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2d0d0dcb46d5cb621f21ea1686bdc26a7dc4775607fc85818dd524ba95e0a0fd
BLAKE2b-256 checksum
How to use checksums
bc0a1321515de90de02f9c98ac12dfa9763ae93d658ed662261758dc5e902986
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/60.5.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.9

Release files / jsengine-1.0.7.post1-py2.py3-none-any.whl

Download URL jsengine-1.0.7.post1-py2.py3-none-any.whl
Size 17.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
12253f6e04065cfdda4df75179c7585a0d1e3a76bb7e6dcaa6f0a0e1b92d5c89
BLAKE2b-256 checksum
How to use checksums
db34ad9d9b2c303f949748740f7123a9ed209eba94824fc5bc66ae586daaa663
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/60.5.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.9

Release history Release notifications | RSS feed

This release

1.0.7.post1 This release

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page