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