sundar@964: /* sundar@964: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. sundar@964: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. sundar@964: * sundar@964: * This code is free software; you can redistribute it and/or modify it sundar@964: * under the terms of the GNU General Public License version 2 only, as sundar@964: * published by the Free Software Foundation. sundar@964: * sundar@964: * This code is distributed in the hope that it will be useful, but WITHOUT sundar@964: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or sundar@964: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License sundar@964: * version 2 for more details (a copy is included in the LICENSE file that sundar@964: * accompanied this code). sundar@964: * sundar@964: * You should have received a copy of the GNU General Public License version sundar@964: * 2 along with this work; if not, write to the Free Software Foundation, sundar@964: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. sundar@964: * sundar@964: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA sundar@964: * or visit www.oracle.com if you need additional information or have any sundar@964: * questions. sundar@964: */ sundar@964: sundar@964: /** sundar@964: * ClassFilter to filter out java classes in a script engine. sundar@964: * sundar@964: * @test sundar@964: * @run sundar@964: */ sundar@964: sundar@964: var NashornScriptEngineFactory = Java.type("jdk.nashorn.api.scripting.NashornScriptEngineFactory"); sundar@964: sundar@964: var fac = new NashornScriptEngineFactory(); sundar@964: // allow only "java.*" classes to be accessed sundar@964: var e = fac.getScriptEngine( sundar@964: function(name) name.startsWith("java.")); sundar@964: sundar@964: function evalIt(str) { sundar@964: print(str + " evalutes to " + e.eval(str)); sundar@964: } sundar@964: sundar@964: function evalExpectError(str) { sundar@964: try { sundar@964: print(e.eval(str)); sundar@964: fail("expected error for: " + str); sundar@964: } catch(exp) { sundar@964: print(str + " throws " + exp); sundar@964: } sundar@964: } sundar@964: sundar@964: evalIt("typeof javax.script.ScriptContext"); sundar@964: evalIt("typeof javax.script.ScriptEngine"); sundar@964: evalIt("typeof java.util.Vector"); sundar@964: evalIt("typeof java.util.Map"); sundar@964: evalIt("typeof java.util.HashMap"); sundar@964: // should be able to call methods, create objects of java.* classes sundar@964: evalIt("var m = new java.util.HashMap(); m.put('foo', 42); m"); sundar@964: evalIt("java.lang.System.out.println"); sundar@964: evalIt("java.lang.System.exit"); sundar@964: sundar@964: evalExpectError("new javax.script.SimpleBindings"); sundar@964: evalExpectError("Java.type('javax.script.ScriptContext')"); sundar@964: evalExpectError("java.lang.Class.forName('javax.script.ScriptContext')"); sundar@964: sundar@964: try { sundar@964: fac["getScriptEngine(ClassFilter)"](null); sundar@964: fail("should have thrown NPE"); sundar@964: } catch (e) { sundar@964: if (! (e instanceof java.lang.NullPointerException)) { sundar@964: fail("NPE expected, got " + e); sundar@964: } sundar@964: }