Google App Engine 是否支持 Java Script Engine

Does Google App Engine support Java Script Engine?

本文关键字:Engine Java Script 支持 是否 App Google      更新时间:2023-09-26

我想在Google App Engine运行时中动态评估JavaScript代码。

Java有这个功能,但想知道GAE是否也支持此功能。

如果您

能提供一个简单的代码将不胜感激,如果您使用它,请分享有关它的评论,谢谢。

GAE 支持脚本语言,但默认情况下"JavaScript"服务不注册。所以开箱即用的 GAE 不评估 JavaScript。

上次我尝试过,虽然 ScriptEngine 被列入白名单,但它在生产环境中不可用。我不得不将Rhino与我的应用程序一起打包.jar。

有关 Java 中脚本的一般用法的示例,您可以参考 Java 文档本身。

不过,请注意,在 GAE/J 环境中,您需要直接调用 Rhino API。

例如

// Import statements.
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
private Object executeUsingRhino(String script) throws Exception
{
    Context ctx = Context.enter();
    try
    {
        Scriptable scope = ctx.initStandardObjects();
        return ctx.evaluateString(scope, script, "<cmd>", 1, null);
    }
    finally
    {
        Context.exit();
    }
}

// Invoke a script that returns a string output using the following code snippet
String output = Context.toString(executeUsingRhino(script));

https://developers.google.com/appengine/docs/java/jrewhitelist 在其白名单(允许的)API中包含javax.script.ScriptEngine,所以,是的。