|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, 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. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * JDK-8157680: Callback parameter of any JS builtin implementation should accept any Callable |
| 26 | + * |
| 27 | + * @option -scripting |
| 28 | + * @test |
| 29 | + * @run |
| 30 | + */ |
| 31 | + |
| 32 | +var SM = Java.type("javax.script.ScriptEngineManager") |
| 33 | +var engine = new SM().getEngineByName("nashorn") |
| 34 | + |
| 35 | +engine.put("output", print); |
| 36 | +var reviver = engine.eval(<<EOF |
| 37 | +function(name, value) { |
| 38 | + if (name == "") return value |
| 39 | + output(name + " = " + value) |
| 40 | + return value |
| 41 | +} |
| 42 | +EOF) |
| 43 | + |
| 44 | +// reviver function from mirror world! |
| 45 | +JSON.parse('{ "foo" : 44, "bar" : "hello" }', reviver) |
| 46 | + |
| 47 | +var AJO = Java.type("jdk.nashorn.api.scripting.AbstractJSObject") |
| 48 | +// reviver function as a JSObject function |
| 49 | +JSON.parse('{ "nashorn" : "hello" }', new AJO() { |
| 50 | + isFunction: function() true, |
| 51 | + call: function(thiz, args) { |
| 52 | + var name = args[0], value = args[1] |
| 53 | + if (name == "") return value |
| 54 | + print(name + " -> " + value) |
| 55 | + return value |
| 56 | + } |
| 57 | +}) |
| 58 | + |
| 59 | +// compare function from the mirror world |
| 60 | +var arr = [34,567,-3, 53].sort(engine.eval(<<EOF |
| 61 | + function(x, y) x < y? -1 : ((x > y)? 1 : 0) |
| 62 | +EOF)) |
| 63 | +print(arr) |
| 64 | + |
| 65 | +// compare function as a JSObject function |
| 66 | +arr = [34,57,-3, 53, 670, 33].sort(new AJO() { |
| 67 | + isFunction: function() true, |
| 68 | + call: function(thiz, args) { |
| 69 | + var x = args[0], y = args[1] |
| 70 | + return x < y? -1 : ((x > y)? 1 : 0) |
| 71 | + } |
| 72 | +}) |
| 73 | +print(arr) |
| 74 | + |
| 75 | +// replacer function from mirror world |
| 76 | +var str = "hello".replace(/l/g, engine.eval(<<EOF |
| 77 | + function() "_" |
| 78 | +EOF)) |
| 79 | +print(str) |
| 80 | + |
| 81 | +// replacer function as a JSObject function |
| 82 | +str = "hello".replace(/[el]/g, new AJO() { |
| 83 | + isFunction: function() true, |
| 84 | + call: function(thiz, args) { |
| 85 | + var match = args[0] |
| 86 | + return match.toUpperCase() |
| 87 | + } |
| 88 | +}) |
| 89 | +print(str) |
0 commit comments