test/script/currently-failing/clone_ir.js

changeset 137
e15806b9d716
parent 0
b1a7da25b547
child 962
ac62e33a99b0
equal deleted inserted replaced
136:c54e218333be 137: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
32 var js1 = "var tuple = { func : function f(x) { if (x) { print('true'); { print('block_under-true'); } } else { print('false'); } } }";
33
34 var Parser = Java.type("jdk.nashorn.internal.parser.Parser");
35 var ASTWriter = Java.type("jdk.nashorn.internal.ir.debug.ASTWriter");
36 var Context = Java.type("jdk.nashorn.internal.runtime.Context");
37 var ScriptEnvironment = Java.type("jdk.nashorn.internal.runtime.ScriptEnvironment");
38 var Source = Java.type("jdk.nashorn.internal.runtime.Source");
39 var FunctionNode = Java.type("jdk.nashorn.internal.ir.FunctionNode");
40 var ThrowErrorManager = Java.type("jdk.nashorn.internal.runtime.Context$ThrowErrorManager");
41 var System = Java.type("java.lang.System");
42
43 var toArrayMethod = ASTWriter.class.getMethod("toArray");
44 var parseMethod = Parser.class.getMethod("parse");
45
46 function toString(obj) {
47 var output = "{ ";
48 for (property in obj) {
49 output += property + ': ' + obj[property]+'; ';
50 }
51 return output + '}'
52 }
53
54 function 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
65 function check(contents) {
66 return check_src(new Source("<no name>", contents));
67 }
68
69 function 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
100 print(check(js1));

mercurial