jdk/src/share/classes/com/sun/script/javascript/RhinoScriptEngine.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8772 e088a9afb9cc
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8772
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.script.javascript;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import com.sun.script.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.script.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.org.mozilla.javascript.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Implementation of <code>ScriptEngine</code> using the Mozilla Rhino
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * interpreter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Mike Grogan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author A. Sundararajan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public final class RhinoScriptEngine extends AbstractScriptEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        implements  Invocable, Compilable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final boolean DEBUG = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* Scope where standard JavaScript objects and our
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * extensions to it are stored. Note that these are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * user defined engine level global variables. These are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * variables have to be there on all compliant ECMAScript
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * scopes. We put these standard objects in this top level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private RhinoTopLevel topLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /* map used to store indexed properties in engine scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * refer to comment on 'indexedProps' in ExternalScriptable.java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private Map<Object, Object> indexedProps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private ScriptEngineFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private InterfaceImplementor implementor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
7800
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    64
    private static final int languageVersion = getLanguageVersion();
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    65
    private static final int optimizationLevel = getOptimizationLevel();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        ContextFactory.initGlobal(new ContextFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            protected Context makeContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                Context cx = super.makeContext();
7800
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    70
                cx.setLanguageVersion(languageVersion);
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    71
                cx.setOptimizationLevel(optimizationLevel);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                cx.setClassShutter(RhinoClassShutter.getInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                cx.setWrapFactory(RhinoWrapFactory.getInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                return cx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
7800
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    79
    private static final String RHINO_JS_VERSION = "rhino.js.version";
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    80
    private static int getLanguageVersion() {
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    81
        int version;
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    82
        String tmp = java.security.AccessController.doPrivileged(
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    83
            new sun.security.action.GetPropertyAction(RHINO_JS_VERSION));
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    84
        if (tmp != null) {
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    85
            version = Integer.parseInt((String)tmp);
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    86
        } else {
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    87
            version = Context.VERSION_1_8;
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    88
        }
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    89
        return version;
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    90
    }
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    91
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    92
    private static final String RHINO_OPT_LEVEL = "rhino.opt.level";
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    93
    private static int getOptimizationLevel() {
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    94
        int optLevel = -1;
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    95
        // disable optimizer under security manager, for now.
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    96
        if (System.getSecurityManager() == null) {
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    97
            optLevel = Integer.getInteger(RHINO_OPT_LEVEL, -1);
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    98
        }
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    99
        return optLevel;
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
   100
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Creates a new instance of RhinoScriptEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public RhinoScriptEngine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Context cx = enterContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            topLevel = new RhinoTopLevel(cx, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            cx.exit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        indexedProps = new HashMap<Object, Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        //construct object used to implement getInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        implementor = new InterfaceImplementor(this) {
8772
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   118
                protected boolean isImplemented(Object thiz, Class<?> iface) {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   119
                    Context cx = enterContext();
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   120
                    try {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   121
                        if (thiz != null && !(thiz instanceof Scriptable)) {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   122
                            thiz = cx.toObject(thiz, topLevel);
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   123
                        }
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   124
                        Scriptable engineScope = getRuntimeScope(context);
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   125
                        Scriptable localScope = (thiz != null)? (Scriptable) thiz :
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   126
                                                    engineScope;
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   127
                        for (Method method : iface.getMethods()) {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   128
                            // ignore methods of java.lang.Object class
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   129
                            if (method.getDeclaringClass() == Object.class) {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   130
                                continue;
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   131
                            }
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   132
                            Object obj = ScriptableObject.getProperty(localScope, method.getName());
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   133
                            if (! (obj instanceof Function)) {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   134
                                return false;
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   135
                            }
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   136
                        }
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   137
                        return true;
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   138
                    } finally {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   139
                        cx.exit();
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   140
                    }
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   141
                }
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents: 7991
diff changeset
   142
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                protected Object convertResult(Method method, Object res)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                            throws ScriptException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    Class desiredType = method.getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    if (desiredType == Void.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        return Context.jsToJava(res, desiredType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public Object eval(Reader reader, ScriptContext ctxt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    throws ScriptException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        Object ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        Context cx = enterContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            Scriptable scope = getRuntimeScope(ctxt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            String filename = (String) get(ScriptEngine.FILENAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            filename = filename == null ? "<Unknown source>" : filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            ret = cx.evaluateReader(scope, reader, filename , 1,  null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } catch (RhinoException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (DEBUG) re.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            int line = (line = re.lineNumber()) == 0 ? -1 : line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            String msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (re instanceof JavaScriptException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                msg = String.valueOf(((JavaScriptException)re).getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                msg = re.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            ScriptException se = new ScriptException(msg, re.sourceName(), line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            se.initCause(re);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw new ScriptException(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            cx.exit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return unwrapReturnValue(ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public Object eval(String script, ScriptContext ctxt) throws ScriptException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (script == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new NullPointerException("null script");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return eval(new StringReader(script) , ctxt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public ScriptEngineFactory getFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return new RhinoScriptEngineFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public Bindings createBindings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return new SimpleBindings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    //Invocable methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public Object invokeFunction(String name, Object... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    throws ScriptException, NoSuchMethodException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return invoke(null, name, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public Object invokeMethod(Object thiz, String name, Object... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    throws ScriptException, NoSuchMethodException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (thiz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throw new IllegalArgumentException("script object can not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return invoke(thiz, name, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private Object invoke(Object thiz, String name, Object... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    throws ScriptException, NoSuchMethodException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        Context cx = enterContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                throw new NullPointerException("method name is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (thiz != null && !(thiz instanceof Scriptable)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                thiz = cx.toObject(thiz, topLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            Scriptable engineScope = getRuntimeScope(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            Scriptable localScope = (thiz != null)? (Scriptable) thiz :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                                    engineScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            Object obj = ScriptableObject.getProperty(localScope, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (! (obj instanceof Function)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                throw new NoSuchMethodException("no such method: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            Function func = (Function) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            Scriptable scope = func.getParentScope();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (scope == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                scope = engineScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            Object result = func.call(cx, scope, localScope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                      wrapArguments(args));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return unwrapReturnValue(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } catch (RhinoException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (DEBUG) re.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            int line = (line = re.lineNumber()) == 0 ? -1 : line;
7991
96cdf8806d96 6508943: small typo in the documentation
sundar
parents: 7800
diff changeset
   251
            ScriptException se = new ScriptException(re.toString(), re.sourceName(), line);
96cdf8806d96 6508943: small typo in the documentation
sundar
parents: 7800
diff changeset
   252
            se.initCause(re);
96cdf8806d96 6508943: small typo in the documentation
sundar
parents: 7800
diff changeset
   253
            throw se;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            cx.exit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public <T> T getInterface(Class<T> clasz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return implementor.getInterface(null, clasz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } catch (ScriptException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public <T> T getInterface(Object thiz, Class<T> clasz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (thiz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new IllegalArgumentException("script object can not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return implementor.getInterface(thiz, clasz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } catch (ScriptException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private static final String printSource =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            "function print(str, newline) {                \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            "    if (typeof(str) == 'undefined') {         \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            "        str = 'undefined';                    \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            "    } else if (str == null) {                 \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            "        str = 'null';                         \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            "    }                                         \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            "    var out = context.getWriter();            \n" +
7991
96cdf8806d96 6508943: small typo in the documentation
sundar
parents: 7800
diff changeset
   287
            "    if (!(out instanceof java.io.PrintWriter))\n" +
96cdf8806d96 6508943: small typo in the documentation
sundar
parents: 7800
diff changeset
   288
            "        out = new java.io.PrintWriter(out);   \n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            "    out.print(String(str));                   \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            "    if (newline) out.print('\\n');            \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            "    out.flush();                              \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            "}\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            "function println(str) {                       \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            "    print(str, true);                         \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            "}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    Scriptable getRuntimeScope(ScriptContext ctxt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (ctxt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            throw new NullPointerException("null script context");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // we create a scope for the given ScriptContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        Scriptable newScope = new ExternalScriptable(ctxt, indexedProps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // Set the prototype of newScope to be 'topLevel' so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // JavaScript standard objects are visible from the scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        newScope.setPrototype(topLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        // define "context" variable in the new scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        newScope.put("context", newScope, ctxt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // define "print", "println" functions in the new scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        Context cx = enterContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            cx.evaluateString(newScope, printSource, "print", 1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            cx.exit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return newScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    //Compilable methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public CompiledScript compile(String script) throws ScriptException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return compile(new StringReader(script));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public CompiledScript compile(java.io.Reader script) throws ScriptException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        CompiledScript ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        Context cx = enterContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            String fileName = (String) get(ScriptEngine.FILENAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (fileName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                fileName = "<Unknown Source>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            Scriptable scope = getRuntimeScope(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            Script scr = cx.compileReader(scope, script, fileName, 1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            ret = new RhinoCompiledScript(this, scr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (DEBUG) e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            throw new ScriptException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            cx.exit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    //package-private helpers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    static Context enterContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        // call this always so that initializer of this class runs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // and initializes custom wrap factory and class shutter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return Context.enter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    void setEngineFactory(ScriptEngineFactory fac) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        factory = fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    Object[] wrapArguments(Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (args == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return Context.emptyArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        Object[] res = new Object[args.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        for (int i = 0; i < res.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            res[i] = Context.javaToJS(args[i], topLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    Object unwrapReturnValue(Object result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (result instanceof Wrapper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            result = ( (Wrapper) result).unwrap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return result instanceof Undefined ? null : result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
7800
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
   382
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (args.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            System.out.println("No file specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        InputStreamReader r = new InputStreamReader(new FileInputStream(args[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        ScriptEngine engine = new RhinoScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        engine.put("x", "y");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        engine.put(ScriptEngine.FILENAME, args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        engine.eval(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        System.out.println(engine.get("x"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
7800
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
   397
    */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
}