Thu, 16 Mar 2017 10:25:02 -0700
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
attila@962 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
attila@962 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
attila@962 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
attila@962 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /** |
aoqi@0 | 25 | * clone_ir : Check that functionNode.clone copies all nodes and that they |
aoqi@0 | 26 | * are not the same references |
aoqi@0 | 27 | * |
aoqi@0 | 28 | * @test |
aoqi@0 | 29 | * @run |
aoqi@0 | 30 | */ |
aoqi@0 | 31 | |
aoqi@0 | 32 | var js1 = "var tuple = { func : function f(x) { if (x) { print('true'); { print('block_under-true'); } } else { print('false'); } } }"; |
aoqi@0 | 33 | |
aoqi@0 | 34 | var Parser = Java.type("jdk.nashorn.internal.parser.Parser"); |
aoqi@0 | 35 | var ASTWriter = Java.type("jdk.nashorn.internal.ir.debug.ASTWriter"); |
aoqi@0 | 36 | var Context = Java.type("jdk.nashorn.internal.runtime.Context"); |
aoqi@0 | 37 | var ScriptEnvironment = Java.type("jdk.nashorn.internal.runtime.ScriptEnvironment"); |
aoqi@0 | 38 | var Source = Java.type("jdk.nashorn.internal.runtime.Source"); |
aoqi@0 | 39 | var FunctionNode = Java.type("jdk.nashorn.internal.ir.FunctionNode"); |
aoqi@0 | 40 | var ThrowErrorManager = Java.type("jdk.nashorn.internal.runtime.Context$ThrowErrorManager"); |
aoqi@0 | 41 | var System = Java.type("java.lang.System"); |
aoqi@0 | 42 | |
aoqi@0 | 43 | var toArrayMethod = ASTWriter.class.getMethod("toArray"); |
attila@962 | 44 | var parseMethod = Parser.class.getMethod("parse"); |
aoqi@0 | 45 | |
aoqi@0 | 46 | function toString(obj) { |
aoqi@0 | 47 | var output = "{ "; |
aoqi@0 | 48 | for (property in obj) { |
attila@962 | 49 | output += property + ': ' + obj[property]+'; '; |
aoqi@0 | 50 | } |
aoqi@0 | 51 | return output + '}' |
aoqi@0 | 52 | } |
aoqi@0 | 53 | |
aoqi@0 | 54 | function flatten(func) { |
aoqi@0 | 55 | var writer = new ASTWriter(func); |
aoqi@0 | 56 | var funcList = toArrayMethod.invoke(writer); |
attila@962 | 57 | |
aoqi@0 | 58 | var res = []; |
aoqi@0 | 59 | for each (x in funcList) { |
attila@962 | 60 | res.push({ name: x.getClass().getName(), id: System.identityHashCode(x) }); |
attila@962 | 61 | } |
aoqi@0 | 62 | return res; |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | function check(contents) { |
aoqi@0 | 66 | return check_src(new Source("<no name>", contents)); |
aoqi@0 | 67 | } |
aoqi@0 | 68 | |
aoqi@0 | 69 | function check_src(src) { |
aoqi@0 | 70 | var parser = new Parser(Context.getContext().getEnv(), src, new ThrowErrorManager()); |
aoqi@0 | 71 | |
aoqi@0 | 72 | var func = parseMethod.invoke(parser); |
aoqi@0 | 73 | print(func); |
aoqi@0 | 74 | var func2 = func.clone(); |
aoqi@0 | 75 | |
aoqi@0 | 76 | var f1 = flatten(func); |
aoqi@0 | 77 | var f2 = flatten(func2); |
aoqi@0 | 78 | |
aoqi@0 | 79 | print(f1.map(toString)); |
aoqi@0 | 80 | print(f2.map(toString)); |
aoqi@0 | 81 | |
aoqi@0 | 82 | if (f1.length != f2.length) { |
attila@962 | 83 | print("length difference between original and clone " + f1.length + " != " + f2.length); |
attila@962 | 84 | return false; |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | for (var i = 0; i < f1.length; i++) { |
attila@962 | 88 | if (f1[i].name !== f2[i].name) { |
attila@962 | 89 | print("name conflict at " + i + " " + f1[i].name + " != " + f2[i].name); |
attila@962 | 90 | return false; |
attila@962 | 91 | } else if (f1[i].id === f2[i].id) { |
attila@962 | 92 | print("id problem at " + i + " " + toString(f1[i]) + " was not deep copied to " + toString(f2[i]) + " became " + f1[i].id + " != " + f2[i].id); |
attila@962 | 93 | return false; |
aoqi@0 | 94 | } |
attila@962 | 95 | } |
attila@962 | 96 | |
aoqi@0 | 97 | return true; |
aoqi@0 | 98 | } |
aoqi@0 | 99 | |
aoqi@0 | 100 | print(check(js1)); |