clone_ir.js revision 136:e15806b9d716
1/*
2 * Copyright (c) 2010, 2013, 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 * clone_ir : Check that functionNode.clone copies all nodes and that they
26 * are not the same references
27 *
28 * @test
29 * @run
30 */
31
32var js1 = "var tuple = { func : function f(x) { if (x) { print('true'); { print('block_under-true'); } } else { print('false'); } } }";
33
34var Parser            = Java.type("jdk.nashorn.internal.parser.Parser");
35var ASTWriter         = Java.type("jdk.nashorn.internal.ir.debug.ASTWriter");
36var Context           = Java.type("jdk.nashorn.internal.runtime.Context");
37var ScriptEnvironment = Java.type("jdk.nashorn.internal.runtime.ScriptEnvironment");
38var Source            = Java.type("jdk.nashorn.internal.runtime.Source");
39var FunctionNode      = Java.type("jdk.nashorn.internal.ir.FunctionNode");
40var ThrowErrorManager = Java.type("jdk.nashorn.internal.runtime.Context$ThrowErrorManager");
41var System            = Java.type("java.lang.System");
42
43var toArrayMethod = ASTWriter.class.getMethod("toArray");
44var parseMethod  = Parser.class.getMethod("parse");
45
46function toString(obj) {
47    var output = "{ ";
48    for (property in obj) {
49	output += property + ': ' + obj[property]+'; ';
50    }
51    return output + '}'
52}
53
54function flatten(func) {
55    var writer   = new ASTWriter(func);
56    var funcList = toArrayMethod.invoke(writer);
57
58    var res = [];
59    for each (x in funcList) {
60	    res.push({ name: x.getClass().getName(), id: System.identityHashCode(x) });
61	}
62    return res;
63}
64
65function check(contents) {
66    return check_src(new Source("<no name>", contents));
67}
68
69function check_src(src) {
70    var parser  = new Parser(Context.getContext().getEnv(), src, new ThrowErrorManager());
71
72    var func = parseMethod.invoke(parser);
73    print(func);
74    var func2 = func.clone();
75
76    var f1 = flatten(func);
77    var f2 = flatten(func2);
78
79    print(f1.map(toString));
80    print(f2.map(toString));
81
82    if (f1.length != f2.length) {
83	print("length difference between original and clone " + f1.length + " != " + f2.length);
84	return false;
85    }
86
87    for (var i = 0; i < f1.length; i++) {
88	if (f1[i].name !== f2[i].name) {
89	    print("name conflict at " + i + " " + f1[i].name + " != " + f2[i].name);
90	    return false;
91	} else if (f1[i].id === f2[i].id) {
92	    print("id problem at " + i + " " + toString(f1[i]) + " was not deep copied to " + toString(f2[i]) + " became " + f1[i].id + " != " + f2[i].id);
93	    return false;
94	}
95    }
96
97    return true;
98}
99
100print(check(js1));
101