This commit is contained in:
Lana Steuck 2014-07-18 08:25:35 -07:00
commit 3bc1beed81
10 changed files with 369 additions and 19 deletions

View File

@ -392,6 +392,7 @@ grant codeBase "file:/${basedir}/test/script/markdown.js" {
verbose="${testng.verbose}" haltonfailure="true" useDefaultListeners="false" listeners="${testng.listeners}" workingDir="${basedir}">
<jvmarg line="${ext.class.path}"/>
<jvmarg line="${run.test.jvmargs} -Xmx${run.test.xmx}"/>
<sysproperty key="nashorn.jar" value="${dist.dir}/nashorn.jar"/>
<propertyset>
<propertyref prefix="nashorn."/>
</propertyset>

View File

@ -0,0 +1,103 @@
#// Usage: jjs javafoovars.js -- <directory>
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// This example demonstrates Java subclassing by Java.extend
// and javac Compiler and Tree API. This example counts number
// of variables called "foo" in the given java source files!
if (arguments.length == 0) {
print("Usage: jjs javafoovars.js -- <directory>");
exit(1);
}
// Java types used
var File = Java.type("java.io.File");
var Files = Java.type("java.nio.file.Files");
var FileVisitOption = Java.type("java.nio.file.FileVisitOption");
var StringArray = Java.type("java.lang.String[]");
var ToolProvider = Java.type("javax.tools.ToolProvider");
var Tree = Java.type("com.sun.source.tree.Tree");
var TreeScanner = Java.type("com.sun.source.util.TreeScanner");
var VariableTree = Java.type("com.sun.source.tree.VariableTree");
// count "foo"-s in the given .java files
function countFoo() {
// get the system compiler tool
var compiler = ToolProvider.systemJavaCompiler;
// get standard file manager
var fileMgr = compiler.getStandardFileManager(null, null, null);
// Using Java.to convert script array (arguments) to a Java String[]
var compUnits = fileMgr.getJavaFileObjects(
Java.to(arguments, StringArray));
// create a new compilation task
var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);
// subclass SimpleTreeVisitor - to count variables called "foo"
var FooCounterVisitor = Java.extend(TreeScanner);
var fooCount = 0;
var visitor = new FooCounterVisitor() {
visitVariable: function (node, p) {
if (node.name.toString() == "foo") {
fooCount++;
}
}
}
for each (var cu in task.parse()) {
cu.accept(visitor, null);
}
return fooCount;
}
// for each ".java" file in directory (recursively) count "foo".
function main(dir) {
var totalCount = 0;
Files.walk(dir.toPath(), FileVisitOption.FOLLOW_LINKS).
forEach(function(p) {
var name = p.toFile().absolutePath;
if (name.endsWith(".java")) {
var count = 0;
try {
count = countFoo(p.toFile().getAbsolutePath());
} catch (e) {
print(e);
}
if (count != 0) {
print(name + ": " + count);
}
totalCount += count;
}
});
print("Total foo count: " + totalCount);
}
main(new File(arguments[0]));

View File

@ -0,0 +1,73 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Flexible script object using AbstractJSObject subclass
var AbstractJSObject = Java.type("jdk.nashorn.api.scripting.AbstractJSObject");
// JSObject example that uses a map for properties and
// falls back to with methods on a java object (for missing
// properties
function makeJSObj(map, fallback) {
return new AbstractJSObject() {
getMember: function(name) {
if (map.containsKey(name)) {
return map.get(name);
}
var val = fallback[name];
if (typeof val == 'function') {
return function() {
var a = arguments;
switch (a.length) {
case 0: return fallback[name]();
case 1: return fallback[name](a[0]);
case 2: return fallback[name](a[0], a[1]);
case 3: return fallback[name](a[0], a[1], a[2]);
case 4: return fallback[name](a[0], a[1], a[2], a[3]);
}
}
}
}
}
}
var m = new java.util.HashMap();
m.put("foo", 42);
m.put("bar", 'hello');
var obj = makeJSObj(m, new java.io.File("."));
print(obj.foo);
print(obj.bar);
print(obj.getAbsolutePath());
print(obj.isDirectory());

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
if (arguments.length == 0) {
print("Usage: jjs ziplist <zip-file>");
exit(1);
}
// list the content details of a .zip or .jar file
var file = arguments[0];
// java classes used
var Attributes = Java.type("java.util.jar.Attributes");
var FileTime = Java.type("java.nio.file.attribute.FileTime");
var JarFile = Java.type("java.util.jar.JarFile");
var ZipEntry = Java.type("java.util.zip.ZipEntry");
var ZipFile = Java.type("java.util.zip.ZipFile");
var zf = file.endsWith(".jar")? new JarFile(file) : new ZipFile(file);
var entries = zf.entries();
// make overall output a valid JSON
var zfObj = {
name: zf.name,
comment: zf.comment,
size: zf.size(),
entries: []
};
while (entries.hasMoreElements()) {
zfObj.entries.push(entries.nextElement());
}
print(JSON.stringify(zfObj, function (key, value) {
if (value instanceof ZipEntry) {
return Object.bindProperties({}, value);
} else if (value instanceof FileTime) {
return value.toString();
} else if (value instanceof Attributes) {
var attrs = {};
var itr = value.entrySet().iterator();
while (itr.hasNext()) {
var n = itr.next();
attrs[n.key] = String(n.value);
}
return attrs;
}
return value;
}, ' '));
zf.close();

View File

@ -36,9 +36,9 @@ import java.net.URL;
import java.security.AccessController;
import java.security.MessageDigest;
import java.security.PrivilegedAction;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
import jdk.nashorn.internal.codegen.types.Type;

View File

@ -1763,6 +1763,8 @@ public final class Global extends ScriptObject implements Scope {
final int NOT_ENUMERABLE_NOT_CONFIG = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE;
scontextProperty = addOwnProperty("context", NOT_ENUMERABLE_NOT_CONFIG, null);
addOwnProperty("engine", NOT_ENUMERABLE_NOT_CONFIG, engine);
// default file name
addOwnProperty(ScriptEngine.FILENAME, Attribute.NOT_ENUMERABLE, null);
// __noSuchProperty__ hook for ScriptContext search of missing variables
final ScriptFunction noSuchProp = ScriptFunctionImpl.makeStrictFunction(NO_SUCH_PROPERTY_NAME, NO_SUCH_PROPERTY);
addOwnProperty(NO_SUCH_PROPERTY_NAME, Attribute.NOT_ENUMERABLE, noSuchProp);

View File

@ -36,24 +36,6 @@ e.eval(<<EOF
'use strict';
try {
context = 444;
print("FAILED!! context write should have thrown error");
} catch (e) {
if (! (e instanceof TypeError)) {
print("TypeError expected but got " + e);
}
}
try {
engine = "hello";
print("FAILED!! engine write should have thrown error");
} catch (e) {
if (! (e instanceof TypeError)) {
print("TypeError expected but got " + e);
}
}
try {
delete context;
print("FAILED!! context delete should have thrown error");

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* JDK-8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
*
* @test
* @run
*/
var ScriptEngine = javax.script.ScriptEngine;
var m = new javax.script.ScriptEngineManager();
var engine = m.getEngineByName("nashorn");
engine.put(ScriptEngine.FILENAME, "foo");
var desc = engine.eval("Object.getOwnPropertyDescriptor(this, '"
+ ScriptEngine.FILENAME + "')");
if (desc.enumerable) {
fail(ScriptEngine.FILENAME + " is enumerable");
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* JDK-8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
*
* Make sure that nashorn.jar has only 'compact1' dependency.
*
* @test
* @option -scripting
* @run
*/
// assume that this script is run with "nashorn.jar" System
// property set to relative path of nashorn.jar from the current
// directory of test execution.
if (typeof fail != 'function') {
fail = print;
}
var System = java.lang.System;
var File = java.io.File;
var nashornJar = new File(System.getProperty("nashorn.jar"));
if (! nashornJar.isAbsolute()) {
nashornJar = new File(".", nashornJar);
}
// run jdep on nashorn.jar - only summary but print profile info
`jdeps -s -P ${nashornJar.absolutePath}`
// check for "(compact1)" in output from jdep tool
if (! /(compact1)/.test($OUT)) {
fail("non-compact1 dependency: " + $OUT);
}

View File

@ -607,6 +607,20 @@ public class ScriptEngineTest {
assertEquals(res, "hello");
}
// @bug 8050432:javax.script.filename variable should not be enumerable
// with nashorn engine's ENGINE_SCOPE bindings
@Test
public void enumerableGlobalsTest() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
e.put(ScriptEngine.FILENAME, "test");
Object enumerable = e.eval(
"Object.getOwnPropertyDescriptor(this, " +
" 'javax.script.filename').enumerable");
assertEquals(enumerable, Boolean.FALSE);
}
private static void checkProperty(final ScriptEngine e, final String name)
throws ScriptException {
final String value = System.getProperty(name);