Skip to main content

Simple JavaScript interpreter for Python

Project description

https://travis-ci.org/lukegb/dukpy.png?branch=master https://coveralls.io/repos/lukegb/dukpy/badge.png?branch=master

DukPy is a simple javascript interpreter for Python built on top of duktape engine without any external dependency. It comes with a bunch of common transpilers built-in for convenience:

  • CoffeeScript

  • BabelJS

  • TypeScript

Dukpy has been tested on Python 3.4, dukpy is currently not production ready and might actually crash your program as it is mostly implemented in C.

dukpy-lukegb is a fork of Alessandro Molina’s dukpy: https://github.com/amol-/dukpy

CoffeeScript Compiler

Using the coffeescript compiler is as easy as running:

>>> import dukpy
>>> dukpy.coffee_compile('''
...     fill = (container, liquid = "coffee") ->
...         "Filling the #{container} with #{liquid}..."
... ''')
'(function() {\n  var fill;\n\n  fill = function*(container, liquid) {\n    if (liquid == null) {\n      liquid = "coffee";\n    }\n    return "Filling the " + container + " with " + liquid + "...";\n  };\n\n}).call(this);\n'

TypeScript Transpiler

The TypeScript compiler can be used through the dukpy.typescript_compile function:

>>> import dukpy
>>> dukpy.typescript_compile('''
... class Greeter {
...     constructor(public greeting: string) { }
...     greet() {
...         return "<h1>" + this.greeting + "</h1>";
...     }
... };
...
... var greeter = new Greeter("Hello, world!");
... ''')
'var Greeter = (function () {\n    function Greeter(greeting) {\n        this.greeting = greeting;\n    }\n    Greeter.prototype.greet = function () {\n        return "<h1>" + this.greeting + "</h1>";\n    };\n    return Greeter;\n})();\n;\nvar greeter = new Greeter("Hello, world!");\n'

Currently the compiler has built-in options and doesn’t accept additional ones,

EcmaScript6 BabelJS Transpiler

To compile ES6 code to ES5 for everyday usage you can use dukpy.babel_compile:

>>> import dukpy
>>> dukpy.babel_compile('''
... class Point {
...     constructor(x, y) {
...             this.x = x;
...         this.y = y;
...         }
...         toString() {
...             return '(' + this.x + ', ' + this.y + ')';
...         }
... }
... ''')
'"use strict";\n\nvar _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); };\n\nvar _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };\n\nvar Point = (function () {\n    function Point(x, y) {\n        _classCallCheck(this, Point);\n\n        this.x = x;\n        this.y = y;\n    }\n\n    _prototypeProperties(Point, null, {\n        toString: {\n            value: function toString() {\n                return "(" + this.x + ", " + this.y + ")";\n            },\n            writable: true,\n            configurable: true\n        }\n    });\n\n    return Point;\n})();\n'

The DukPY based BabelJS compiler also provides a WebAssets ( http://webassets.readthedocs.org/en/latest/ ) filter to automatically compile ES6 code in your assets pipeline. You register this filter as babeljs within WebAssets using:

from webassets.filter import register_filter
from dukpy.webassets import BabelJS

register_filter(BabelJS)

Which makes the filter available with the babeljs name.

NOTE: When using the BabelJS compiler for code that needs to run in the browser, make sure to add https://cdnjs.cloudflare.com/ajax/libs/babel-core/4.6.6/browser-polyfill.js dependency.

Using the JavaScript Interpreter

Using dukpy is as simple as calling the dukpy.evaljs function with the javascript code:

>>> import dukpy
>>> dukpy.evaljs("var o = {'value': 5}; o['value'] += 3; o")
{'value': 8}

The evaljs function executes the javascript and returns the resulting value as far as it is possible to encode it in JSON.

If execution fails a dukpy.JSRuntimeError exception is raised with the failure reason.

Passing Arguments

Any argument passed to evaljs is available in JavaScript inside the dukpy object in javascript. It must be possible to encode the arguments using JSON for them to be available in Javascript:

>>> import dukpy
>>>
>>> def sum3(value):
...     return dukpy.evaljs("dukpy['value'] + 3", value=value)
...
>>> sum3(7)
10

Running Multiple Scripts

The evaljs function supports providing multiple source codes to be executed in the same context.

Multiple script can be passed in a list or tuple:

>>> import dukpy
>>> dukpy.evaljs(["var o = {'value': 5}",
...               "o['value'] += 3",
...               "o"])
{'value': 8}

This is useful when your code requires dependencies to work, as you can load the dependency and then your code.

This is actually how the coffeescript compiler is implemented by DukPy itself:

def coffee_compile(source):
    with open(COFFEE_COMPILER, 'r') as coffeescript_js:
        return evaljs((coffeescript_js.read(), 'CoffeeScript.compile(dukpy.coffeecode)'),
                      coffeecode=source)

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

dukpy-lukegb-0.2.1.tar.gz (1.6 MB view details)

Uploaded Source

Built Distributions

dukpy_lukegb-0.2.1-cp35-none-win32.whl (890.7 kB view details)

Uploaded CPython 3.5 Windows x86

dukpy_lukegb-0.2.1-cp27-none-win32.whl (892.0 kB view details)

Uploaded CPython 2.7 Windows x86

dukpy-lukegb-0.2.1.win32-py3.5.msi (974.8 kB view details)

Uploaded Source

dukpy-lukegb-0.2.1.win32-py2.7.msi (1.0 MB view details)

Uploaded Source

File details

Details for the file dukpy-lukegb-0.2.1.tar.gz.

File metadata

File hashes

Hashes for dukpy-lukegb-0.2.1.tar.gz
Algorithm Hash digest
SHA256 d0eaaccd1bff28fcd4f548aafae40722f53d85ec93f997e7acb65c4253968c99
MD5 15a732c29641292574b652abe12db986
BLAKE2b-256 45198b6411077565a686c111c68e94dcaf1758c0ac2eeadf6886611bc45bff72

See more details on using hashes here.

File details

Details for the file dukpy_lukegb-0.2.1-cp35-none-win32.whl.

File metadata

File hashes

Hashes for dukpy_lukegb-0.2.1-cp35-none-win32.whl
Algorithm Hash digest
SHA256 ae1076cf1db511ec00fd195ef96d90f2f756040a63a46859926365b870605e50
MD5 c888d145e071850a7692f00b64bfabab
BLAKE2b-256 cbba7079a3c65c30846fc1ae15eadc43d953a1ea4f5dc8775c3bc07e3cfd70e2

See more details on using hashes here.

File details

Details for the file dukpy_lukegb-0.2.1-cp27-none-win32.whl.

File metadata

File hashes

Hashes for dukpy_lukegb-0.2.1-cp27-none-win32.whl
Algorithm Hash digest
SHA256 9d8f89f085fed963fb8b6878fb2f21350ed62bea771cd2477ca7b52eb3eccf47
MD5 a0f56beb6e0957db5af769fe8b81c9a2
BLAKE2b-256 83a95968e07794230e9de8c42841e2db87f06353b8253be23b4fa3eb0dea1c39

See more details on using hashes here.

File details

Details for the file dukpy-lukegb-0.2.1.win32-py3.5.msi.

File metadata

File hashes

Hashes for dukpy-lukegb-0.2.1.win32-py3.5.msi
Algorithm Hash digest
SHA256 99b2a4ddfd2a09d3497e0b93b6107d5e4ed6d8be76f71637a7f56919ee67f3b2
MD5 b847ccef161142c9ce800f22dec8275f
BLAKE2b-256 3a01cddd278bc06397d976ef8901e06823d2813d72a8bad9cb0f7a4aad4a795e

See more details on using hashes here.

File details

Details for the file dukpy-lukegb-0.2.1.win32-py2.7.msi.

File metadata

File hashes

Hashes for dukpy-lukegb-0.2.1.win32-py2.7.msi
Algorithm Hash digest
SHA256 5447be83e2e3ad13bd4e455877aabefc2bd1fc8125a6b907751f66cd10eb679f
MD5 80e92720f872aefb06db57dad08f837c
BLAKE2b-256 9934b40271a8abaf4badeacd6fe062c0a936f5c9e7e94f375e74d36cf070c14f

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