An example of python running JavaScript, that calls a python object method. Then adding a JavaScript method to the afore mentioned object (in JavaScript) and calling that method from python.
import PyV8
class DevilMagic(object):
def __init__(self):
pass
def summon(self, msg):
name = raw_input("enter your name: ")
self.name = name
return "Hello %s, %s" % (name, msg)
class PyContext(object):
def GetDevilMagic(self):
self.oneMagicPlease = DevilMagic()
return self.oneMagicPlease
def main():
msg = "is your mind blown?"
pc = PyContext()
jtx = PyV8.JSContext(pc)
jtx.enter()
res = jtx.eval("""
//This is JavaScript:
function foo() {
var oneMagicPlease = GetDevilMagic(),
summon_result = oneMagicPlease.summon("%s");
//Add a function to the object
oneMagicPlease.hi = function(){
return "This function was added to the object in javascript,";
};
return summon_result + "\\n ..."+ oneMagicPlease.name +"?";
}
var res = foo();
res;
""" % msg)
print res
print pc.oneMagicPlease.hi(), "but called from python."
if __name__ == "__main__":
main()