Does Google App Engine support JavaScript Engine?

2019-03-01 14:50发布

I want evaluate dynamically JavaScript code inside the Google App Engine runtime.

Java have this feature but Want to know if this is supported by GAE too.

If you can provide a simple code will be very appreciate, and if you use it, please shares comments about it, thanks.

...

GAE support Scripting Languages but by default 'JavaScript' service is not register. So GAE out-of-the-box do not evaluate JavaScript.

2条回答
放我归山
2楼-- · 2019-03-01 15:25

Last time I tried, though ScriptEngine is whitelisted, it is not available in the production environment. I had to package the Rhino.jar along with my app.

For examples on general usage of scripting in Java, you can refer to the Java documentation itself.

Though, note that in the GAE/J environment you will need to invoke the Rhino APIs directly.

For example,

// 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));
查看更多
Lonely孤独者°
3楼-- · 2019-03-01 15:30

https://developers.google.com/appengine/docs/java/jrewhitelist includes javax.script.ScriptEngine in its whitelisted (allowed) APIs, so, yes.

查看更多
登录 后发表回答