aoqi@0: /* aoqi@0: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * clone_ir : Check that functionNode.clone copies all nodes and that they aoqi@0: * are not the same references aoqi@0: * aoqi@0: * @test aoqi@0: * @run aoqi@0: */ aoqi@0: aoqi@0: var js1 = "var tuple = { func : function f(x) { if (x) { print('true'); { print('block_under-true'); } } else { print('false'); } } }"; aoqi@0: aoqi@0: var Parser = Java.type("jdk.nashorn.internal.parser.Parser"); aoqi@0: var ASTWriter = Java.type("jdk.nashorn.internal.ir.debug.ASTWriter"); aoqi@0: var Context = Java.type("jdk.nashorn.internal.runtime.Context"); aoqi@0: var ScriptEnvironment = Java.type("jdk.nashorn.internal.runtime.ScriptEnvironment"); aoqi@0: var Source = Java.type("jdk.nashorn.internal.runtime.Source"); aoqi@0: var FunctionNode = Java.type("jdk.nashorn.internal.ir.FunctionNode"); aoqi@0: var ThrowErrorManager = Java.type("jdk.nashorn.internal.runtime.Context$ThrowErrorManager"); aoqi@0: var System = Java.type("java.lang.System"); aoqi@0: aoqi@0: var toArrayMethod = ASTWriter.class.getMethod("toArray"); aoqi@0: var parseMethod = Parser.class.getMethod("parse"); aoqi@0: aoqi@0: function toString(obj) { aoqi@0: var output = "{ "; aoqi@0: for (property in obj) { aoqi@0: output += property + ': ' + obj[property]+'; '; aoqi@0: } aoqi@0: return output + '}' aoqi@0: } aoqi@0: aoqi@0: function flatten(func) { aoqi@0: var writer = new ASTWriter(func); aoqi@0: var funcList = toArrayMethod.invoke(writer); aoqi@0: aoqi@0: var res = []; aoqi@0: for each (x in funcList) { aoqi@0: res.push({ name: x.getClass().getName(), id: System.identityHashCode(x) }); aoqi@0: } aoqi@0: return res; aoqi@0: } aoqi@0: aoqi@0: function check(contents) { aoqi@0: return check_src(new Source("", contents)); aoqi@0: } aoqi@0: aoqi@0: function check_src(src) { aoqi@0: var parser = new Parser(Context.getContext().getEnv(), src, new ThrowErrorManager()); aoqi@0: aoqi@0: var func = parseMethod.invoke(parser); aoqi@0: print(func); aoqi@0: var func2 = func.clone(); aoqi@0: aoqi@0: var f1 = flatten(func); aoqi@0: var f2 = flatten(func2); aoqi@0: aoqi@0: print(f1.map(toString)); aoqi@0: print(f2.map(toString)); aoqi@0: aoqi@0: if (f1.length != f2.length) { aoqi@0: print("length difference between original and clone " + f1.length + " != " + f2.length); aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: for (var i = 0; i < f1.length; i++) { aoqi@0: if (f1[i].name !== f2[i].name) { aoqi@0: print("name conflict at " + i + " " + f1[i].name + " != " + f2[i].name); aoqi@0: return false; aoqi@0: } else if (f1[i].id === f2[i].id) { aoqi@0: print("id problem at " + i + " " + toString(f1[i]) + " was not deep copied to " + toString(f2[i]) + " became " + f1[i].id + " != " + f2[i].id); aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: print(check(js1));