Giter Site home page Giter Site logo

enuan / duktape-py Goto Github PK

View Code? Open in Web Editor NEW

This project forked from brosner/duktape-py

0.0 3.0 0.0 6.96 MB

Python wrapper for duktape (embeddable Javascript engine)

License: MIT License

Makefile 0.02% Python 0.87% C 94.83% Cython 1.70% C++ 2.57%

duktape-py's Introduction

duktape-py

This project was originally Abe Winter's, but he has transferred ownership over to me (Brian Rosner). I plan on fixing issues and implementing new features. I have already started supporting Python 3 and updated duktape to 1.3.1. More to come, so stay tuned!

Python wrapper for duktape, an embeddable Javascript engine

demo

>>> import duktape
>>> c=duktape.DukContext()
>>> c.eval_string("""function C(a,b){this.a=a; this.b=b};
... C.prototype.tot=function(){return this.a+this.b};
... new C(1,2);""")
>>> c.get()
{'a': 1.0, 'b': 2.0}
>>> c.call_prop('tot',())
>>> c.get()
3.0
>>> def pushget(x): c.push(x); return c.get()
...
>>> map(pushget,[1,2.,'three',[4,5],{'6':7},[[8]]])
[1.0, 2.0, 'three', [4.0, 5.0], {'6': 7.0}, [[8.0]]]
>>> c.get_global("C")
>>> c.construct(1,2)
>>> c.get()
{'a': 1.0, 'b': 2.0}

you can make python functions callable from javascript (though it leaks memory and you can't bind a 'this'):

>>> def add(a,b): return a+b
...
>>> c.push_func(add,2)
>>> c.call(1,2)
>>> c.get()
3.0

Tread lightly: not all errors are caught. In particular, errors in a constructor function aren't handled.

installation

pip install duktape

why?

  • pyv8 is fine if you want boost on your system
  • being able to run JS from python lets you run integration tests without standing up client-server infrastructure -- makes tests faster and more reliable

warnings

  • use this for testing only
  • note the version (0.0.2). this is not mature.
  • don't run in production unless you like SIGABRT
  • this isn't full-featured; most of duktape isn't exposed
  • this is almost as low-level as duktape so you'll have to interact with the stack

duktape-py's People

Contributors

mcella avatar brosner avatar abe-winter avatar paolo-losi avatar

Watchers

 avatar  avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.