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 * 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 * 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 * 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. 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 0N/A * Implementation of <code>ScriptEngine</code> using the Mozilla Rhino 0N/A * @author Mike Grogan 0N/A * @author A. Sundararajan 0N/A private static final boolean DEBUG =
false;
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 /* map used to store indexed properties in engine scope 4437N/A * Create new Context instance to be associated with the current thread. 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. 3320N/A // disable optimizer under security manager, for now. 0N/A * Creates a new instance of RhinoScriptEngine 0N/A //construct object used to implement getInterface 3743N/A // ignore methods of java.lang.Object class 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 " 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 "function println(str) { \n" +
0N/A " print(str, true); \n" +
0N/A // we create a scope for the given ScriptContext 0N/A // Set the prototype of newScope to be 'topLevel' so that 0N/A // JavaScript standard objects are visible from the scope. 0N/A // define "context" variable in the new scope 0N/A // define "print", "println" functions in the new scope 0N/A //Compilable methods 0N/A //package-private helpers 0N/A // call this always so that initializer of this class runs 0N/A // and initializes custom wrap factory and class shutter. 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 InputStreamReader r = new InputStreamReader(new FileInputStream(args[0])); 0N/A ScriptEngine engine = new RhinoScriptEngine(); 0N/A engine.put("x", "y"); 0N/A engine.put(ScriptEngine.FILENAME, args[0]); 0N/A System.out.println(engine.get("x"));