jsengine


Namejsengine JSON
Version 1.0.7.post1 PyPI version JSON
download
home_pagehttps://github.com/SeaHOH/jsengine
SummaryJSEngine is a simple wrapper of Javascript engines.
upload_time2023-01-01 07:22:38
maintainer
docs_urlNone
authorSeaHOH
requires_python>=2.7
licenseMIT
keywords javascript js engine node chakra quickjs execjs
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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](https://github.com/PetterS/quickjs)  
    [PyChakra](https://github.com/zhengrenzhe/PyChakra)  
    [PyMiniRacer (V8)](https://github.com/sqreen/PyMiniRacer)
    (Caused by [a scope issue](https://github.com/sqreen/PyMiniRacer/issues/148),
    its work process is similar to external call now, to fix the scope issue,
    [Esprima](https://github.com/Kronuz/esprima-python) is needed)  

- Any installed external Javascript interpreters, e.g.

    SpiderMonkey, Node.js, QuickJS, etc.

JSEngine used to be part of [YKDL](https://github.com/SeaHOH/ykdl),
which created by [@coslyk](https://github.com/coslyk).


# Installation
Install from 
[![version](https://img.shields.io/pypi/v/jsengine)](https://pypi.org/project/jsengine/)
[![package format](https://img.shields.io/pypi/format/jsengine)](https://pypi.org/project/jsengine/#files)
[![monthly downloads](https://img.shields.io/pypi/dm/jsengine)](https://pypi.org/project/jsengine/#files)

    pip install jsengine

Or download and Install from source code

    python setup.py install

# Compatibility
- Python >= 2.7


# Usage

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

Use a JSEngine context.

```python
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.

```python
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.
```python
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](https://github.com/SeaHOH/jsengine/blob/master/LICENSE).



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/SeaHOH/jsengine",
    "name": "jsengine",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=2.7",
    "maintainer_email": "",
    "keywords": "javascript js engine node chakra quickjs execjs",
    "author": "SeaHOH",
    "author_email": "seahoh@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/bc/0a/1321515de90de02f9c98ac12dfa9763ae93d658ed662261758dc5e902986/jsengine-1.0.7.post1.tar.gz",
    "platform": "any",
    "description": "# JSEngine\r\n\r\nThis is a simple wrapper of Javascript engines, it wraps the Javascript\r\ninterpreter for Python use.\r\n\r\nThere are two ways to call interpreters, via dynamic library loading is internal\r\ncall which is faster than the other one, via subprocess is external call.\r\n\r\n- System's built-in Javascript interpreter:\r\n\r\n    **macOS**: JavascriptCore  \r\n    **Linux**: Gjs on Gnome, CJS on Cinnamon, etc.  \r\n    **Windows**: Chakra (internal call, but not applicable to Windows 7)  \r\n\r\n- Python bindings (Recommend, internal call):\r\n\r\n    [QuickJS](https://github.com/PetterS/quickjs)  \r\n    [PyChakra](https://github.com/zhengrenzhe/PyChakra)  \r\n    [PyMiniRacer (V8)](https://github.com/sqreen/PyMiniRacer)\r\n    (Caused by [a scope issue](https://github.com/sqreen/PyMiniRacer/issues/148),\r\n    its work process is similar to external call now, to fix the scope issue,\r\n    [Esprima](https://github.com/Kronuz/esprima-python) is needed)  \r\n\r\n- Any installed external Javascript interpreters, e.g.\r\n\r\n    SpiderMonkey, Node.js, QuickJS, etc.\r\n\r\nJSEngine used to be part of [YKDL](https://github.com/SeaHOH/ykdl),\r\nwhich created by [@coslyk](https://github.com/coslyk).\r\n\r\n\r\n# Installation\r\nInstall from \r\n[![version](https://img.shields.io/pypi/v/jsengine)](https://pypi.org/project/jsengine/)\r\n[![package format](https://img.shields.io/pypi/format/jsengine)](https://pypi.org/project/jsengine/#files)\r\n[![monthly downloads](https://img.shields.io/pypi/dm/jsengine)](https://pypi.org/project/jsengine/#files)\r\n\r\n    pip install jsengine\r\n\r\nOr download and Install from source code\r\n\r\n    python setup.py install\r\n\r\n# Compatibility\r\n- Python >= 2.7\r\n\r\n\r\n# Usage\r\n\r\n```python\r\nimport jsengine\r\njsengine.eval('\"Hello, world!\"')  # => 'Hello, world!'\r\n```\r\n\r\nUse a JSEngine context.\r\n\r\n```python\r\ntry:\r\n    ctx1 = jsengine.jsengine()\r\nexcept jsengine.RuntimeError:\r\n    ...  # do something if useless\r\n\r\nif jsengine.JSEngine is None:\r\n    ...  # do something if useless\r\nelse:\r\n    ctx2 = jsengine.JSEngine(\"\"\"\r\n            function add(x, y) {\r\n                return x + y;\r\n            }\r\n            \"\"\")\r\n\r\nctx1.eval('1 + 1')  # => 2\r\n\r\n# call funtion\r\nctx2.call(\"add\", 1, 2)  # => 3\r\n\r\n# append new script\r\nctx1.append(\"\"\"\r\n    function square(x) {\r\n        return x ** 2;\r\n    }\r\n    \"\"\")\r\nctx1.call(\"square\", 9)  # => 81\r\n```\r\n\r\nUse a specified external Javascript interpreter.\r\n\r\n```python\r\nbinary = binary_name or binary_path\r\nkwargs = {\r\n    'name': 'None or any string',  # see ExternalInterpreterNameAlias.keys()\r\n    'tempfile': True,              # use tempfile or not. Default is False, fallback is True\r\n    'evalstring': True,            # can run command string as Javascript or can not,\r\n                                   # just like '-e script_code'\r\n                                   # instead of True, supported argument can be passed,\r\n                                   # e.g. '--eval', '--execute'\r\n    'args': [args1, args2, ...]    # arguments used for interpreter\r\n}\r\n\r\n# case 1\r\ninterpreter = jsengine.ExternalInterpreter.get(binary, **kwargs)\r\nif interpreter:\r\n    # found\r\n    ctx = jsengine.ExternalJSEngine(interpreter)\r\n\r\n# case 2\r\nif jsengine.set_external_interpreter(binary, **kwargs):\r\n    # set default external interpreter OK\r\n    ctx = jsengine.ExternalJSEngine()\r\n\r\n# case 3, maybe get default fallback instead of your specified\r\ntry:\r\n    ctx = jsengine.ExternalJSEngine(interpreter=binary, **kwargs)\r\nexcept jsengine.RuntimeError:\r\n    ...  # do something if useless\r\n```\r\n\r\nUse threading lock. Javascript source itself always be ran in single threaded,\r\nthat just make the APIs can be used in multithreadeding.\r\n```python\r\njsengine.set_threading(True)   # MUST enable befor using, it's disabled by default\r\n\r\nctx_quickjs = jsengine.QuickJSEngine()\r\nctx_chakra = jsengine.ChakraJSEngine()   # internal chakra will create an extra thread per context\r\nctx_v8 = jsengine.V8JSEngine()\r\nctx_exter = jsengine.ExternalJSEngine()  # external interpreter will be called one by one with context\r\n\r\n...  # do multithreading\r\n\r\njsengine.set_threading(False)  # disable is not necessary\r\n```\r\n\r\n\r\n# Internal VS. External\r\n|                 | QuickJSEngine  | ChakraJSEngine | V8JSEngine (esprima) | V8JSEngine \\**       | ExternalJSEngine     |\r\n| --------------- | :------------: | :------------: | :------------------: | :------------------: | :------------------: |\r\n| Load backend on | import         | import or init | init                 | init                 | every fetch result   |\r\n| Loading speed   | fastest        | fast           | very slow with py3   | fast                 | very slow            |\r\n| Performance     |                | highest        | high                 | low, if much results | low, if much results |\r\n| Fetch result    | run the passed | run the passed | run the passed       | run all/full source  | run all/full source  |\r\n\r\n\\* Fetch results means call `eval()/call()`.  \r\n\\** V8JSEngine is now similar to ExternalJSEngine which caused by scope issue.  \r\n\r\n\r\n# License\r\nJSEngine is released under the [MIT License](https://github.com/SeaHOH/jsengine/blob/master/LICENSE).\r\n\r\n\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "JSEngine is a simple wrapper of Javascript engines.",
    "version": "1.0.7.post1",
    "split_keywords": [
        "javascript",
        "js",
        "engine",
        "node",
        "chakra",
        "quickjs",
        "execjs"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "beac44f0cc504eec27340747251b9d60",
                "sha256": "12253f6e04065cfdda4df75179c7585a0d1e3a76bb7e6dcaa6f0a0e1b92d5c89"
            },
            "downloads": -1,
            "filename": "jsengine-1.0.7.post1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "beac44f0cc504eec27340747251b9d60",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=2.7",
            "size": 17454,
            "upload_time": "2023-01-01T07:22:36",
            "upload_time_iso_8601": "2023-01-01T07:22:36.675804Z",
            "url": "https://files.pythonhosted.org/packages/db/34/ad9d9b2c303f949748740f7123a9ed209eba94824fc5bc66ae586daaa663/jsengine-1.0.7.post1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "c1efb449d5baa5efe76a2a0bb49733c2",
                "sha256": "2d0d0dcb46d5cb621f21ea1686bdc26a7dc4775607fc85818dd524ba95e0a0fd"
            },
            "downloads": -1,
            "filename": "jsengine-1.0.7.post1.tar.gz",
            "has_sig": false,
            "md5_digest": "c1efb449d5baa5efe76a2a0bb49733c2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=2.7",
            "size": 16248,
            "upload_time": "2023-01-01T07:22:38",
            "upload_time_iso_8601": "2023-01-01T07:22:38.351767Z",
            "url": "https://files.pythonhosted.org/packages/bc/0a/1321515de90de02f9c98ac12dfa9763ae93d658ed662261758dc5e902986/jsengine-1.0.7.post1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-01 07:22:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "SeaHOH",
    "github_project": "jsengine",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "jsengine"
}
        
Elapsed time: 0.04735s