aboutsummaryrefslogtreecommitdiff
path: root/test/script/currently-failing/clone_ir.js
blob: 14009506ca58d600c713ed56b20c9d87c7bdde3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * clone_ir : Check that functionNode.clone copies all nodes and that they
 * are not the same references
 *
 * @test
 * @run
 */

var js1 = "var tuple = { func : function f(x) { if (x) { print('true'); { print('block_under-true'); } } else { print('false'); } } }";

var Parser            = Java.type("jdk.nashorn.internal.parser.Parser");
var ASTWriter         = Java.type("jdk.nashorn.internal.ir.debug.ASTWriter");
var Context           = Java.type("jdk.nashorn.internal.runtime.Context");
var ScriptEnvironment = Java.type("jdk.nashorn.internal.runtime.ScriptEnvironment");
var Source            = Java.type("jdk.nashorn.internal.runtime.Source");
var FunctionNode      = Java.type("jdk.nashorn.internal.ir.FunctionNode");
var ThrowErrorManager = Java.type("jdk.nashorn.internal.runtime.Context$ThrowErrorManager");
var System            = Java.type("java.lang.System");

var toArrayMethod = ASTWriter.class.getMethod("toArray");
var parseMethod  = Parser.class.getMethod("parse");

function toString(obj) {
    var output = "{ ";
    for (property in obj) {
    output += property + ': ' + obj[property]+'; ';
    }
    return output + '}'
}

function flatten(func) {
    var writer   = new ASTWriter(func);
    var funcList = toArrayMethod.invoke(writer);

    var res = [];
    for each (x in funcList) {
        res.push({ name: x.getClass().getName(), id: System.identityHashCode(x) });
    }
    return res;
}

function check(contents) {
    return check_src(new Source("<no name>", contents));
}

function check_src(src) {
    var parser  = new Parser(Context.getContext().getEnv(), src, new ThrowErrorManager());

    var func = parseMethod.invoke(parser);
    print(func);
    var func2 = func.clone();

    var f1 = flatten(func);
    var f2 = flatten(func2);

    print(f1.map(toString));
    print(f2.map(toString));

    if (f1.length != f2.length) {
    print("length difference between original and clone " + f1.length + " != " + f2.length);
    return false;
    }

    for (var i = 0; i < f1.length; i++) {
    if (f1[i].name !== f2[i].name) {
        print("name conflict at " + i + " " + f1[i].name + " != " + f2[i].name);
        return false;
    } else if (f1[i].id === f2[i].id) {
        print("id problem at " + i + " " + toString(f1[i]) + " was not deep copied to " + toString(f2[i]) + " became " + f1[i].id + " != " + f2[i].id);
        return false;
    }
    }

    return true;
}

print(check(js1));