Created
September 24, 2010 12:26
-
-
Save svdgraaf/595273 to your computer and use it in GitHub Desktop.
Embedding V8 in Python is awesome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import PyV8, duh! | |
import PyV8 | |
# define a class which methods should be available in Javascript | |
class epicClass(PyV8.JSClass): | |
def ponies(self): | |
return "Ponies!" | |
def doubletime(self, what, amount): | |
for i in range(0, amount): | |
print what | |
# define a bit of javascript, or fetch it from disk/database/POST, whatever | |
javascript = """awesome = ponies(); | |
awesome = awesome + " and rainbows!"; | |
doubletime(awesome, 2);""" | |
# create a javascript context | |
context = PyV8.JSContext(epicClass()) | |
# enter the context, and run! | |
context.enter(); | |
context.eval(javascript); # outputs "Ponies! and rainbows!" Twice... Amazing! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment