2

I'm using PyV8 to execute Javascript programs from Python. I had a problem executing "document.write", but found the solution here (using Mock Document): Executing Javascript from Python

Now,I'm facing an other problem. I want to execute a prompt command javascript from python. The effect of the result should be like a simple python raw_input. I give an example:

var a, b, c, d;
prompt(a);
c = a
prompt(b);
c = c + b
d = a * b
document.write(c)
document.write(d)

using PyV8, the evaluation of this script should evaluate the first line, stop at prompt(a) asking me to introduce the value of a (DOS line command), then resume the evaluation till next prompt, and so on. Thks in advance.

4

2 回答 2

2

将 Python 函数注入 JavaScript 上下文实际上非常简单——通过JSContext.locals对象将该函数分配给局部变量:

ctx = PyV8.JSContext()
ctx.enter()
ctx.locals.prompt = raw_input

ctx.eval('var a = prompt("js> ");')

突然之间,您可以像在 Python 中那样使用 JavaScript 中的 Python 函数。

我通常会链接到文档,但PyV8 文档似乎在任何地方都没有正确的 MIME 类型。

于 2012-06-12T13:34:14.187 回答
1

如果 PyV8 中没有函数,那么您可以像在您引用的示例中添加对象prompt()一样添加它。document

于 2012-06-05T19:16:15.953 回答