8157680: Callback parameter of any JS builtin implementation should accept any Callable
Reviewed-by: hannesw, mhaupt
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeArray.java Wed May 25 11:03:08 2016 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeArray.java Wed May 25 15:14:37 2016 +0530
@@ -238,7 +238,7 @@
@Override
public MethodHandle call() {
return Bootstrap.createDynamicCallInvoker(double.class,
- ScriptFunction.class, Object.class, Object.class, Object.class);
+ Object.class, Object.class, Object.class, Object.class);
}
});
}
@@ -1210,23 +1210,23 @@
return copy;
}
- private static ScriptFunction compareFunction(final Object comparefn) {
+ private static Object compareFunction(final Object comparefn) {
if (comparefn == ScriptRuntime.UNDEFINED) {
return null;
}
- if (! (comparefn instanceof ScriptFunction)) {
+ if (!Bootstrap.isCallable(comparefn)) {
throw typeError("not.a.function", ScriptRuntime.safeToString(comparefn));
}
- return (ScriptFunction)comparefn;
+ return comparefn;
}
private static Object[] sort(final Object[] array, final Object comparefn) {
- final ScriptFunction cmp = compareFunction(comparefn);
+ final Object cmp = compareFunction(comparefn);
final List<Object> list = Arrays.asList(array);
- final Object cmpThis = cmp == null || cmp.isStrict() ? ScriptRuntime.UNDEFINED : Global.instance();
+ final Object cmpThis = cmp == null || Bootstrap.isStrictCallable(cmp) ? ScriptRuntime.UNDEFINED : Global.instance();
try {
Collections.sort(list, new Comparator<Object>() {
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJSON.java Wed May 25 11:03:08 2016 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJSON.java Wed May 25 15:14:37 2016 +0530
@@ -47,7 +47,6 @@
import jdk.nashorn.internal.runtime.JSONFunctions;
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.PropertyMap;
-import jdk.nashorn.internal.runtime.ScriptFunction;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator;
import jdk.nashorn.internal.runtime.linker.Bootstrap;
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeRegExp.java Wed May 25 11:03:08 2016 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeRegExp.java Wed May 25 15:14:37 2016 +0530
@@ -45,7 +45,6 @@
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.ParserException;
import jdk.nashorn.internal.runtime.PropertyMap;
-import jdk.nashorn.internal.runtime.ScriptFunction;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.ScriptRuntime;
import jdk.nashorn.internal.runtime.linker.Bootstrap;
@@ -655,7 +654,7 @@
* @param replacement Replacement string.
* @return String with substitutions.
*/
- String replace(final String string, final String replacement, final ScriptFunction function) throws Throwable {
+ String replace(final String string, final String replacement, final Object function) throws Throwable {
final RegExpMatcher matcher = regexp.match(string);
if (matcher == null) {
@@ -671,7 +670,7 @@
sb.append(string, 0, matcher.start());
if (function != null) {
- final Object self = function.isStrict() ? UNDEFINED : Global.instance();
+ final Object self = Bootstrap.isStrictCallable(function) ? UNDEFINED : Global.instance();
sb.append(callReplaceValue(getReplaceValueInvoker(), function, self, matcher, string));
} else {
appendReplacement(matcher, string, replacement, sb);
@@ -691,7 +690,7 @@
final StringBuilder sb = new StringBuilder();
final MethodHandle invoker = function == null ? null : getReplaceValueInvoker();
- final Object self = function == null || function.isStrict() ? UNDEFINED : Global.instance();
+ final Object self = function == null || Bootstrap.isStrictCallable(function) ? UNDEFINED : Global.instance();
do {
sb.append(string, thisIndex, matcher.start());
@@ -807,12 +806,12 @@
new Callable<MethodHandle>() {
@Override
public MethodHandle call() {
- return Bootstrap.createDynamicCallInvoker(String.class, ScriptFunction.class, Object.class, Object[].class);
+ return Bootstrap.createDynamicCallInvoker(String.class, Object.class, Object.class, Object[].class);
}
});
}
- private String callReplaceValue(final MethodHandle invoker, final ScriptFunction function, final Object self, final RegExpMatcher matcher, final String string) throws Throwable {
+ private String callReplaceValue(final MethodHandle invoker, final Object function, final Object self, final RegExpMatcher matcher, final String string) throws Throwable {
final Object[] groups = groups(matcher);
final Object[] args = Arrays.copyOf(groups, groups.length + 2);
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeString.java Wed May 25 11:03:08 2016 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeString.java Wed May 25 15:14:37 2016 +0530
@@ -58,10 +58,10 @@
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.OptimisticBuiltins;
import jdk.nashorn.internal.runtime.PropertyMap;
-import jdk.nashorn.internal.runtime.ScriptFunction;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.ScriptRuntime;
import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
+import jdk.nashorn.internal.runtime.linker.Bootstrap;
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
import jdk.nashorn.internal.runtime.linker.NashornGuards;
import jdk.nashorn.internal.runtime.linker.PrimitiveLookup;
@@ -743,8 +743,8 @@
nativeRegExp = NativeRegExp.flatRegExp(JSType.toString(string));
}
- if (replacement instanceof ScriptFunction) {
- return nativeRegExp.replace(str, "", (ScriptFunction)replacement);
+ if (Bootstrap.isCallable(replacement)) {
+ return nativeRegExp.replace(str, "", replacement);
}
return nativeRegExp.replace(str, JSType.toString(replacement), null);
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeWeakMap.java Wed May 25 11:03:08 2016 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeWeakMap.java Wed May 25 15:14:37 2016 +0530
@@ -31,7 +31,6 @@
import jdk.nashorn.internal.objects.annotations.Constructor;
import jdk.nashorn.internal.objects.annotations.Function;
import jdk.nashorn.internal.objects.annotations.ScriptClass;
-import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.PropertyMap;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.ScriptRuntime;
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeWeakSet.java Wed May 25 11:03:08 2016 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeWeakSet.java Wed May 25 15:14:37 2016 +0530
@@ -31,7 +31,6 @@
import jdk.nashorn.internal.objects.annotations.Constructor;
import jdk.nashorn.internal.objects.annotations.Function;
import jdk.nashorn.internal.objects.annotations.ScriptClass;
-import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.PropertyMap;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.ScriptRuntime;
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/JSONFunctions.java Wed May 25 11:03:08 2016 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/JSONFunctions.java Wed May 25 15:14:37 2016 +0530
@@ -46,7 +46,7 @@
@Override
public MethodHandle call() {
return Bootstrap.createDynamicCallInvoker(Object.class,
- ScriptFunction.class, ScriptObject.class, String.class, Object.class);
+ Object.class, Object.class, String.class, Object.class);
}
});
}
@@ -90,16 +90,16 @@
// apply 'reviver' function if available
private static Object applyReviver(final Global global, final Object unfiltered, final Object reviver) {
- if (reviver instanceof ScriptFunction) {
+ if (Bootstrap.isCallable(reviver)) {
final ScriptObject root = global.newObject();
root.addOwnProperty("", Property.WRITABLE_ENUMERABLE_CONFIGURABLE, unfiltered);
- return walk(root, "", (ScriptFunction)reviver);
+ return walk(root, "", reviver);
}
return unfiltered;
}
// This is the abstract "Walk" operation from the spec.
- private static Object walk(final ScriptObject holder, final Object name, final ScriptFunction reviver) {
+ private static Object walk(final ScriptObject holder, final Object name, final Object reviver) {
final Object val = holder.get(name);
if (val instanceof ScriptObject) {
final ScriptObject valueObj = (ScriptObject)val;
@@ -131,7 +131,7 @@
try {
// Object.class, ScriptFunction.class, ScriptObject.class, String.class, Object.class);
- return getREVIVER_INVOKER().invokeExact(reviver, holder, JSType.toString(name), val);
+ return getREVIVER_INVOKER().invokeExact(reviver, (Object)holder, JSType.toString(name), val);
} catch(Error|RuntimeException t) {
throw t;
} catch(final Throwable t) {
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/Bootstrap.java Wed May 25 11:03:08 2016 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/Bootstrap.java Wed May 25 15:14:37 2016 +0530
@@ -171,7 +171,9 @@
return ((JSObject)callable).isStrictFunction();
} else if (callable instanceof BoundCallable) {
return isStrictCallable(((BoundCallable)callable).getCallable());
- } else if (BeansLinker.isDynamicMethod(callable) || callable instanceof StaticClass) {
+ } else if (BeansLinker.isDynamicMethod(callable) ||
+ callable instanceof StaticClass ||
+ isFunctionalInterfaceObject(callable)) {
return false;
}
throw notFunction(callable);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8157680.js Wed May 25 15:14:37 2016 +0530
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2016, 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-8157680: Callback parameter of any JS builtin implementation should accept any Callable
+ *
+ * @option -scripting
+ * @test
+ * @run
+ */
+
+var SM = Java.type("javax.script.ScriptEngineManager")
+var engine = new SM().getEngineByName("nashorn")
+
+engine.put("output", print);
+var reviver = engine.eval(<<EOF
+function(name, value) {
+ if (name == "") return value
+ output(name + " = " + value)
+ return value
+}
+EOF)
+
+// reviver function from mirror world!
+JSON.parse('{ "foo" : 44, "bar" : "hello" }', reviver)
+
+var AJO = Java.type("jdk.nashorn.api.scripting.AbstractJSObject")
+// reviver function as a JSObject function
+JSON.parse('{ "nashorn" : "hello" }', new AJO() {
+ isFunction: function() true,
+ call: function(thiz, args) {
+ var name = args[0], value = args[1]
+ if (name == "") return value
+ print(name + " -> " + value)
+ return value
+ }
+})
+
+// compare function from the mirror world
+var arr = [34,567,-3, 53].sort(engine.eval(<<EOF
+ function(x, y) x < y? -1 : ((x > y)? 1 : 0)
+EOF))
+print(arr)
+
+// compare function as a JSObject function
+arr = [34,57,-3, 53, 670, 33].sort(new AJO() {
+ isFunction: function() true,
+ call: function(thiz, args) {
+ var x = args[0], y = args[1]
+ return x < y? -1 : ((x > y)? 1 : 0)
+ }
+})
+print(arr)
+
+// replacer function from mirror world
+var str = "hello".replace(/l/g, engine.eval(<<EOF
+ function() "_"
+EOF))
+print(str)
+
+// replacer function as a JSObject function
+str = "hello".replace(/[el]/g, new AJO() {
+ isFunction: function() true,
+ call: function(thiz, args) {
+ var match = args[0]
+ return match.toUpperCase()
+ }
+})
+print(str)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8157680.js.EXPECTED Wed May 25 15:14:37 2016 +0530
@@ -0,0 +1,7 @@
+foo = 44
+bar = hello
+nashorn -> hello
+-3,34,53,567
+-3,33,34,53,57,670
+he__o
+hELLo