jdk/src/share/classes/com/sun/script/javascript/RhinoCompiledScript.java
changeset 17462 c1bfafc15e02
parent 17461 84860231159b
parent 17460 19eb5d62770a
child 17463 9392f1567896
equal deleted inserted replaced
17461:84860231159b 17462:c1bfafc15e02
     1 /*
       
     2  * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package com.sun.script.javascript;
       
    27 import javax.script.*;
       
    28 import sun.org.mozilla.javascript.internal.*;
       
    29 
       
    30 /**
       
    31  * Represents compiled JavaScript code.
       
    32  *
       
    33  * @author Mike Grogan
       
    34  * @since 1.6
       
    35  */
       
    36 final class RhinoCompiledScript extends CompiledScript {
       
    37 
       
    38     private RhinoScriptEngine engine;
       
    39     private Script script;
       
    40 
       
    41 
       
    42     RhinoCompiledScript(RhinoScriptEngine engine, Script script) {
       
    43         this.engine = engine;
       
    44         this.script = script;
       
    45     }
       
    46 
       
    47     public Object eval(ScriptContext context) throws ScriptException {
       
    48 
       
    49         Object result = null;
       
    50         Context cx = RhinoScriptEngine.enterContext();
       
    51         try {
       
    52 
       
    53             Scriptable scope = engine.getRuntimeScope(context);
       
    54             Object ret = script.exec(cx, scope);
       
    55             result = engine.unwrapReturnValue(ret);
       
    56         } catch (RhinoException re) {
       
    57             int line = (line = re.lineNumber()) == 0 ? -1 : line;
       
    58             String msg;
       
    59             if (re instanceof JavaScriptException) {
       
    60                 msg = String.valueOf(((JavaScriptException)re).getValue());
       
    61             } else {
       
    62                 msg = re.toString();
       
    63             }
       
    64             ScriptException se = new ScriptException(msg, re.sourceName(), line);
       
    65             se.initCause(re);
       
    66             throw se;
       
    67         } finally {
       
    68             Context.exit();
       
    69         }
       
    70 
       
    71         return result;
       
    72     }
       
    73 
       
    74     public ScriptEngine getEngine() {
       
    75         return engine;
       
    76     }
       
    77 
       
    78 }