Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/test/script/currently-failing/clone_ir.js
32281 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* clone_ir : Check that functionNode.clone copies all nodes and that they25* are not the same references26*27* @test28* @run29*/3031var js1 = "var tuple = { func : function f(x) { if (x) { print('true'); { print('block_under-true'); } } else { print('false'); } } }";3233var Parser = Java.type("jdk.nashorn.internal.parser.Parser");34var ASTWriter = Java.type("jdk.nashorn.internal.ir.debug.ASTWriter");35var Context = Java.type("jdk.nashorn.internal.runtime.Context");36var ScriptEnvironment = Java.type("jdk.nashorn.internal.runtime.ScriptEnvironment");37var Source = Java.type("jdk.nashorn.internal.runtime.Source");38var FunctionNode = Java.type("jdk.nashorn.internal.ir.FunctionNode");39var ThrowErrorManager = Java.type("jdk.nashorn.internal.runtime.Context$ThrowErrorManager");40var System = Java.type("java.lang.System");4142var toArrayMethod = ASTWriter.class.getMethod("toArray");43var parseMethod = Parser.class.getMethod("parse");4445function toString(obj) {46var output = "{ ";47for (property in obj) {48output += property + ': ' + obj[property]+'; ';49}50return output + '}'51}5253function flatten(func) {54var writer = new ASTWriter(func);55var funcList = toArrayMethod.invoke(writer);5657var res = [];58for each (x in funcList) {59res.push({ name: x.getClass().getName(), id: System.identityHashCode(x) });60}61return res;62}6364function check(contents) {65return check_src(new Source("<no name>", contents));66}6768function check_src(src) {69var parser = new Parser(Context.getContext().getEnv(), src, new ThrowErrorManager());7071var func = parseMethod.invoke(parser);72print(func);73var func2 = func.clone();7475var f1 = flatten(func);76var f2 = flatten(func2);7778print(f1.map(toString));79print(f2.map(toString));8081if (f1.length != f2.length) {82print("length difference between original and clone " + f1.length + " != " + f2.length);83return false;84}8586for (var i = 0; i < f1.length; i++) {87if (f1[i].name !== f2[i].name) {88print("name conflict at " + i + " " + f1[i].name + " != " + f2[i].name);89return false;90} else if (f1[i].id === f2[i].id) {91print("id problem at " + i + " " + toString(f1[i]) + " was not deep copied to " + toString(f2[i]) + " became " + f1[i].id + " != " + f2[i].id);92return false;93}94}9596return true;97}9899print(check(js1));100101102