0N/A/*
3909N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.script.javascript;
0N/Aimport com.sun.script.util.*;
0N/Aimport javax.script.*;
0N/Aimport sun.org.mozilla.javascript.internal.*;
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.io.*;
4437N/Aimport java.security.*;
0N/Aimport java.util.*;
0N/A
0N/A
0N/A/**
0N/A * Implementation of <code>ScriptEngine</code> using the Mozilla Rhino
0N/A * interpreter.
0N/A *
0N/A * @author Mike Grogan
0N/A * @author A. Sundararajan
0N/A * @since 1.6
0N/A */
0N/Apublic final class RhinoScriptEngine extends AbstractScriptEngine
0N/A implements Invocable, Compilable {
0N/A
0N/A private static final boolean DEBUG = false;
0N/A
4437N/A private AccessControlContext accCtxt;
4437N/A
0N/A /* Scope where standard JavaScript objects and our
0N/A * extensions to it are stored. Note that these are not
0N/A * user defined engine level global variables. These are
0N/A * variables have to be there on all compliant ECMAScript
0N/A * scopes. We put these standard objects in this top level.
0N/A */
0N/A private RhinoTopLevel topLevel;
0N/A
0N/A /* map used to store indexed properties in engine scope
0N/A * refer to comment on 'indexedProps' in ExternalScriptable.java.
0N/A */
0N/A private Map<Object, Object> indexedProps;
0N/A
0N/A private ScriptEngineFactory factory;
0N/A private InterfaceImplementor implementor;
0N/A
3320N/A private static final int languageVersion = getLanguageVersion();
3320N/A private static final int optimizationLevel = getOptimizationLevel();
4437N/A
0N/A static {
0N/A ContextFactory.initGlobal(new ContextFactory() {
4437N/A /**
4437N/A * Create new Context instance to be associated with the current thread.
4437N/A */
4437N/A @Override
0N/A protected Context makeContext() {
0N/A Context cx = super.makeContext();
3320N/A cx.setLanguageVersion(languageVersion);
3320N/A cx.setOptimizationLevel(optimizationLevel);
0N/A cx.setClassShutter(RhinoClassShutter.getInstance());
0N/A cx.setWrapFactory(RhinoWrapFactory.getInstance());
0N/A return cx;
0N/A }
4437N/A
4437N/A /**
4437N/A * Execute top call to script or function. When the runtime is about to
4437N/A * execute a script or function that will create the first stack frame
4437N/A * with scriptable code, it calls this method to perform the real call.
4437N/A * In this way execution of any script happens inside this function.
4437N/A */
4437N/A @Override
4437N/A protected Object doTopCall(final Callable callable,
4437N/A final Context cx, final Scriptable scope,
4437N/A final Scriptable thisObj, final Object[] args) {
4437N/A AccessControlContext accCtxt = null;
4437N/A Scriptable global = ScriptableObject.getTopLevelScope(scope);
4437N/A Scriptable globalProto = global.getPrototype();
4437N/A if (globalProto instanceof RhinoTopLevel) {
4437N/A accCtxt = ((RhinoTopLevel)globalProto).getAccessContext();
4437N/A }
4437N/A
4437N/A if (accCtxt != null) {
4437N/A return AccessController.doPrivileged(new PrivilegedAction<Object>() {
4437N/A public Object run() {
4437N/A return superDoTopCall(callable, cx, scope, thisObj, args);
4437N/A }
4437N/A }, accCtxt);
4437N/A } else {
4437N/A return superDoTopCall(callable, cx, scope, thisObj, args);
4437N/A }
4437N/A }
4437N/A
4437N/A private Object superDoTopCall(Callable callable,
4437N/A Context cx, Scriptable scope,
4437N/A Scriptable thisObj, Object[] args) {
4437N/A return super.doTopCall(callable, cx, scope, thisObj, args);
4437N/A }
0N/A });
0N/A }
0N/A
3320N/A private static final String RHINO_JS_VERSION = "rhino.js.version";
3320N/A private static int getLanguageVersion() {
3320N/A int version;
3320N/A String tmp = java.security.AccessController.doPrivileged(
3320N/A new sun.security.action.GetPropertyAction(RHINO_JS_VERSION));
3320N/A if (tmp != null) {
3320N/A version = Integer.parseInt((String)tmp);
3320N/A } else {
3320N/A version = Context.VERSION_1_8;
3320N/A }
3320N/A return version;
3320N/A }
3320N/A
3320N/A private static final String RHINO_OPT_LEVEL = "rhino.opt.level";
3320N/A private static int getOptimizationLevel() {
3320N/A int optLevel = -1;
3320N/A // disable optimizer under security manager, for now.
3320N/A if (System.getSecurityManager() == null) {
3320N/A optLevel = Integer.getInteger(RHINO_OPT_LEVEL, -1);
3320N/A }
3320N/A return optLevel;
3320N/A }
0N/A
0N/A /**
0N/A * Creates a new instance of RhinoScriptEngine
0N/A */
0N/A public RhinoScriptEngine() {
4437N/A if (System.getSecurityManager() != null) {
5413N/A try {
5413N/A AccessController.checkPermission(new AllPermission());
5413N/A } catch (AccessControlException ace) {
5413N/A accCtxt = AccessController.getContext();
5413N/A }
4437N/A }
0N/A
0N/A Context cx = enterContext();
0N/A try {
0N/A topLevel = new RhinoTopLevel(cx, this);
0N/A } finally {
0N/A cx.exit();
0N/A }
0N/A
0N/A indexedProps = new HashMap<Object, Object>();
0N/A
0N/A //construct object used to implement getInterface
0N/A implementor = new InterfaceImplementor(this) {
3743N/A protected boolean isImplemented(Object thiz, Class<?> iface) {
3743N/A Context cx = enterContext();
3743N/A try {
3743N/A if (thiz != null && !(thiz instanceof Scriptable)) {
3743N/A thiz = cx.toObject(thiz, topLevel);
3743N/A }
3743N/A Scriptable engineScope = getRuntimeScope(context);
3743N/A Scriptable localScope = (thiz != null)? (Scriptable) thiz :
3743N/A engineScope;
3743N/A for (Method method : iface.getMethods()) {
3743N/A // ignore methods of java.lang.Object class
3743N/A if (method.getDeclaringClass() == Object.class) {
3743N/A continue;
3743N/A }
3743N/A Object obj = ScriptableObject.getProperty(localScope, method.getName());
3743N/A if (! (obj instanceof Function)) {
3743N/A return false;
3743N/A }
3743N/A }
3743N/A return true;
3743N/A } finally {
3743N/A cx.exit();
3743N/A }
3743N/A }
3743N/A
0N/A protected Object convertResult(Method method, Object res)
0N/A throws ScriptException {
0N/A Class desiredType = method.getReturnType();
0N/A if (desiredType == Void.TYPE) {
0N/A return null;
0N/A } else {
0N/A return Context.jsToJava(res, desiredType);
0N/A }
0N/A }
0N/A };
0N/A }
0N/A
0N/A public Object eval(Reader reader, ScriptContext ctxt)
0N/A throws ScriptException {
0N/A Object ret;
0N/A
0N/A Context cx = enterContext();
0N/A try {
0N/A Scriptable scope = getRuntimeScope(ctxt);
0N/A String filename = (String) get(ScriptEngine.FILENAME);
0N/A filename = filename == null ? "<Unknown source>" : filename;
0N/A
0N/A ret = cx.evaluateReader(scope, reader, filename , 1, null);
0N/A } catch (RhinoException re) {
0N/A if (DEBUG) re.printStackTrace();
0N/A int line = (line = re.lineNumber()) == 0 ? -1 : line;
0N/A String msg;
0N/A if (re instanceof JavaScriptException) {
0N/A msg = String.valueOf(((JavaScriptException)re).getValue());
0N/A } else {
0N/A msg = re.toString();
0N/A }
0N/A ScriptException se = new ScriptException(msg, re.sourceName(), line);
0N/A se.initCause(re);
0N/A throw se;
0N/A } catch (IOException ee) {
0N/A throw new ScriptException(ee);
0N/A } finally {
0N/A cx.exit();
0N/A }
0N/A
0N/A return unwrapReturnValue(ret);
0N/A }
0N/A
0N/A public Object eval(String script, ScriptContext ctxt) throws ScriptException {
0N/A if (script == null) {
0N/A throw new NullPointerException("null script");
0N/A }
0N/A return eval(new StringReader(script) , ctxt);
0N/A }
0N/A
0N/A public ScriptEngineFactory getFactory() {
0N/A if (factory != null) {
0N/A return factory;
0N/A } else {
0N/A return new RhinoScriptEngineFactory();
0N/A }
0N/A }
0N/A
0N/A public Bindings createBindings() {
0N/A return new SimpleBindings();
0N/A }
0N/A
0N/A //Invocable methods
0N/A public Object invokeFunction(String name, Object... args)
0N/A throws ScriptException, NoSuchMethodException {
0N/A return invoke(null, name, args);
0N/A }
0N/A
0N/A public Object invokeMethod(Object thiz, String name, Object... args)
0N/A throws ScriptException, NoSuchMethodException {
0N/A if (thiz == null) {
0N/A throw new IllegalArgumentException("script object can not be null");
0N/A }
0N/A return invoke(thiz, name, args);
0N/A }
0N/A
0N/A private Object invoke(Object thiz, String name, Object... args)
0N/A throws ScriptException, NoSuchMethodException {
0N/A Context cx = enterContext();
0N/A try {
0N/A if (name == null) {
0N/A throw new NullPointerException("method name is null");
0N/A }
0N/A
0N/A if (thiz != null && !(thiz instanceof Scriptable)) {
0N/A thiz = cx.toObject(thiz, topLevel);
0N/A }
0N/A
0N/A Scriptable engineScope = getRuntimeScope(context);
0N/A Scriptable localScope = (thiz != null)? (Scriptable) thiz :
0N/A engineScope;
0N/A Object obj = ScriptableObject.getProperty(localScope, name);
0N/A if (! (obj instanceof Function)) {
0N/A throw new NoSuchMethodException("no such method: " + name);
0N/A }
0N/A
0N/A Function func = (Function) obj;
0N/A Scriptable scope = func.getParentScope();
0N/A if (scope == null) {
0N/A scope = engineScope;
0N/A }
0N/A Object result = func.call(cx, scope, localScope,
0N/A wrapArguments(args));
0N/A return unwrapReturnValue(result);
0N/A } catch (RhinoException re) {
0N/A if (DEBUG) re.printStackTrace();
0N/A int line = (line = re.lineNumber()) == 0 ? -1 : line;
3402N/A ScriptException se = new ScriptException(re.toString(), re.sourceName(), line);
3402N/A se.initCause(re);
3402N/A throw se;
0N/A } finally {
0N/A cx.exit();
0N/A }
0N/A }
0N/A
0N/A public <T> T getInterface(Class<T> clasz) {
0N/A try {
0N/A return implementor.getInterface(null, clasz);
0N/A } catch (ScriptException e) {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public <T> T getInterface(Object thiz, Class<T> clasz) {
0N/A if (thiz == null) {
0N/A throw new IllegalArgumentException("script object can not be null");
0N/A }
0N/A
0N/A try {
0N/A return implementor.getInterface(thiz, clasz);
0N/A } catch (ScriptException e) {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A private static final String printSource =
0N/A "function print(str, newline) { \n" +
0N/A " if (typeof(str) == 'undefined') { \n" +
0N/A " str = 'undefined'; \n" +
0N/A " } else if (str == null) { \n" +
0N/A " str = 'null'; \n" +
0N/A " } \n" +
0N/A " var out = context.getWriter(); \n" +
3402N/A " if (!(out instanceof java.io.PrintWriter))\n" +
3402N/A " out = new java.io.PrintWriter(out); \n" +
0N/A " out.print(String(str)); \n" +
0N/A " if (newline) out.print('\\n'); \n" +
0N/A " out.flush(); \n" +
0N/A "}\n" +
0N/A "function println(str) { \n" +
0N/A " print(str, true); \n" +
0N/A "}";
0N/A
0N/A Scriptable getRuntimeScope(ScriptContext ctxt) {
0N/A if (ctxt == null) {
0N/A throw new NullPointerException("null script context");
0N/A }
0N/A
0N/A // we create a scope for the given ScriptContext
0N/A Scriptable newScope = new ExternalScriptable(ctxt, indexedProps);
0N/A
0N/A // Set the prototype of newScope to be 'topLevel' so that
0N/A // JavaScript standard objects are visible from the scope.
0N/A newScope.setPrototype(topLevel);
0N/A
0N/A // define "context" variable in the new scope
0N/A newScope.put("context", newScope, ctxt);
0N/A
0N/A // define "print", "println" functions in the new scope
0N/A Context cx = enterContext();
0N/A try {
0N/A cx.evaluateString(newScope, printSource, "print", 1, null);
0N/A } finally {
0N/A cx.exit();
0N/A }
0N/A return newScope;
0N/A }
0N/A
0N/A
0N/A //Compilable methods
0N/A public CompiledScript compile(String script) throws ScriptException {
0N/A return compile(new StringReader(script));
0N/A }
0N/A
0N/A public CompiledScript compile(java.io.Reader script) throws ScriptException {
0N/A CompiledScript ret = null;
0N/A Context cx = enterContext();
0N/A
0N/A try {
0N/A String fileName = (String) get(ScriptEngine.FILENAME);
0N/A if (fileName == null) {
0N/A fileName = "<Unknown Source>";
0N/A }
0N/A
0N/A Scriptable scope = getRuntimeScope(context);
0N/A Script scr = cx.compileReader(scope, script, fileName, 1, null);
0N/A ret = new RhinoCompiledScript(this, scr);
0N/A } catch (Exception e) {
0N/A if (DEBUG) e.printStackTrace();
0N/A throw new ScriptException(e);
0N/A } finally {
0N/A cx.exit();
0N/A }
0N/A return ret;
0N/A }
0N/A
0N/A
0N/A //package-private helpers
0N/A
0N/A static Context enterContext() {
0N/A // call this always so that initializer of this class runs
0N/A // and initializes custom wrap factory and class shutter.
0N/A return Context.enter();
0N/A }
0N/A
0N/A void setEngineFactory(ScriptEngineFactory fac) {
0N/A factory = fac;
0N/A }
0N/A
4437N/A AccessControlContext getAccessContext() {
4437N/A return accCtxt;
4437N/A }
4437N/A
0N/A Object[] wrapArguments(Object[] args) {
0N/A if (args == null) {
0N/A return Context.emptyArgs;
0N/A }
0N/A Object[] res = new Object[args.length];
0N/A for (int i = 0; i < res.length; i++) {
0N/A res[i] = Context.javaToJS(args[i], topLevel);
0N/A }
0N/A return res;
0N/A }
0N/A
0N/A Object unwrapReturnValue(Object result) {
0N/A if (result instanceof Wrapper) {
0N/A result = ( (Wrapper) result).unwrap();
0N/A }
0N/A
0N/A return result instanceof Undefined ? null : result;
0N/A }
0N/A
3320N/A /*
0N/A public static void main(String[] args) throws Exception {
0N/A if (args.length == 0) {
0N/A System.out.println("No file specified");
0N/A return;
0N/A }
0N/A
0N/A InputStreamReader r = new InputStreamReader(new FileInputStream(args[0]));
0N/A ScriptEngine engine = new RhinoScriptEngine();
0N/A
0N/A engine.put("x", "y");
0N/A engine.put(ScriptEngine.FILENAME, args[0]);
0N/A engine.eval(r);
0N/A System.out.println(engine.get("x"));
0N/A }
3320N/A */
0N/A}