dukpy/README.rst

81 lines
2.5 KiB
ReStructuredText
Raw Normal View History

2015-03-09 11:53:47 +00:00
dukpy
=====
DukPy is a simple javascript interpreter for Python built on top of
2015-03-10 08:28:08 +00:00
duktape engine **without any external dependency**.
It comes with a builtin *CoffeeScript* compiler for
convenience and usage example.
2015-03-09 11:53:47 +00:00
Dukpy has been tested on **Python 2.7** and **Python 3.4**, dukpy
is currently not production ready and might actually crash your
program as it is mostly implemented in C.
2015-03-09 11:53:47 +00:00
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'
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)