JDK-8006529.js revision 427:798e3aa19718
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 * JDK-8006529 : Methods should not always get callee parameter, and they
26 * should not be too eager in creation of scopes.
27 *
28 * @test
29 * @run
30 */
31
32/*
33 * This test script depends on nashorn Compiler internals. It uses reflection
34 * to get access to private field and many public methods of Compiler and
35 * FunctionNode classes. Note that this is trusted code and access to such
36 * internal package classes and methods is okay. But, if you modify any
37 * Compiler or FunctionNode class, you may have to revisit this script.
38 * We cannot use direct Java class (via dynalink bean linker) to Compiler
39 * and FunctionNode because of package-access check and so reflective calls.
40 */
41
42var Parser            = Java.type("jdk.nashorn.internal.parser.Parser")
43var Compiler          = Java.type("jdk.nashorn.internal.codegen.Compiler")
44var Context           = Java.type("jdk.nashorn.internal.runtime.Context")
45var ScriptEnvironment = Java.type("jdk.nashorn.internal.runtime.ScriptEnvironment")
46var Source            = Java.type("jdk.nashorn.internal.runtime.Source")
47var FunctionNode      = Java.type("jdk.nashorn.internal.ir.FunctionNode")
48var Block             = Java.type("jdk.nashorn.internal.ir.Block")
49var VarNode           = Java.type("jdk.nashorn.internal.ir.VarNode")
50var ExecuteNode       = Java.type("jdk.nashorn.internal.ir.ExecuteNode")
51var UnaryNode         = Java.type("jdk.nashorn.internal.ir.UnaryNode")
52var BinaryNode        = Java.type("jdk.nashorn.internal.ir.BinaryNode")
53var ThrowErrorManager = Java.type("jdk.nashorn.internal.runtime.Context$ThrowErrorManager")
54var Debug             = Java.type("jdk.nashorn.internal.runtime.Debug")
55
56var parseMethod = Parser.class.getMethod("parse");
57var compileMethod = Compiler.class.getMethod("compile", FunctionNode.class);
58var getBodyMethod = FunctionNode.class.getMethod("getBody");
59var getStatementsMethod = Block.class.getMethod("getStatements");
60var getInitMethod = VarNode.class.getMethod("getInit");
61var getExpressionMethod = ExecuteNode.class.getMethod("getExpression")
62var rhsMethod = UnaryNode.class.getMethod("rhs")
63var lhsMethod = BinaryNode.class.getMethod("lhs")
64var binaryRhsMethod = BinaryNode.class.getMethod("rhs")
65var debugIdMethod = Debug.class.getMethod("id", java.lang.Object.class)
66
67// These are method names of methods in FunctionNode class
68var allAssertionList = ['isVarArg', 'needsParentScope', 'needsCallee', 'hasScopeBlock', 'needsSelfSymbol', 'isSplit', 'hasEval', 'allVarsInScope', 'isStrict']
69
70// corresponding Method objects of FunctionNode class
71var functionNodeMethods = {};
72// initialize FunctionNode methods
73(function() {
74    for (var f in allAssertionList) {
75        var method = allAssertionList[f];
76        functionNodeMethods[method] = FunctionNode.class.getMethod(method);
77    }
78})();
79
80// returns functionNode.getBody().getStatements().get(0)
81function getFirstFunction(functionNode) {
82    var f = findFunction(getBodyMethod.invoke(functionNode))
83    if (f == null) {
84        throw new Error();
85    }
86    return f;
87}
88
89function findFunction(node) {
90    if(node instanceof Block) {
91        var stmts = getStatementsMethod.invoke(node)
92        for(var i = 0; i < stmts.size(); ++i) {
93            var retval = findFunction(stmts.get(i))
94            if(retval != null) {
95                return retval;
96            }
97        }
98    } else if(node instanceof VarNode) {
99        return findFunction(getInitMethod.invoke(node))
100    } else if(node instanceof UnaryNode) {
101        return findFunction(rhsMethod.invoke(node))
102    } else if(node instanceof BinaryNode) {
103        return findFunction(lhsMethod.invoke(node)) || findFunction(binaryRhsMethod.invoke(node))
104	} else if(node instanceof ExecuteNode) {
105		return findFunction(getExpressionMethod.invoke(node))
106    } else if(node instanceof FunctionNode) {
107        return node
108    }
109}
110
111var getContextMethod = Context.class.getMethod("getContext")
112var getEnvMethod = Context.class.getMethod("getEnv")
113
114// compile(script) -- compiles a script specified as a string with its
115// source code, returns a jdk.nashorn.internal.ir.FunctionNode object
116// representing it.
117function compile(source) {
118    var source   = new Source("<no name>", source);
119
120    var env = getEnvMethod.invoke(getContextMethod.invoke(null))
121
122    var parser   = new Parser(env, source, new ThrowErrorManager());
123    var func     = parseMethod.invoke(parser);
124
125    var compiler = new Compiler(env);
126
127    return compileMethod.invoke(compiler, func);
128};
129
130var allAssertions = (function() {
131    var allAssertions = {}
132    for(var assertion in allAssertionList) {
133        allAssertions[allAssertionList[assertion]] = true
134    }
135    return allAssertions;
136})();
137
138
139// test(f[, assertions...]) tests whether all the specified assertions on the
140// passed function node are true.
141function test(f) {
142    var assertions = {}
143    for(var i = 1; i < arguments.length; ++i) {
144        var assertion = arguments[i]
145        if(!allAssertions[assertion]) {
146            throw "Unknown assertion " + assertion + " for " + f;
147        }
148        assertions[assertion] = true
149    }
150    for(var assertion in allAssertions) {
151        var expectedValue = !!assertions[assertion]
152        var actualValue = functionNodeMethods[assertion].invoke(f)
153        if(actualValue !== expectedValue) {
154            throw "Expected " + assertion + " === " + expectedValue + ", got " + actualValue + " for " + f + ":" + debugIdMethod.invoke(null, f);
155        }
156    }
157}
158
159// testFirstFn(script[, assertions...] tests whether all the specified
160// assertions are true in the first function in the given script; "script"
161// is a string with the source text of the script.
162function testFirstFn(script) {
163    arguments[0] = getFirstFunction(compile(script))
164    test.apply(null, arguments)
165}
166
167// ---------------------------------- ACTUAL TESTS START HERE --------------
168
169// The simplest possible functions have no attributes set
170testFirstFn("function f() { }")
171testFirstFn("function f(x) { x }")
172
173// A function referencing a global needs parent scope, and it needs callee
174// (because parent scope is passed through callee)
175testFirstFn("function f() { x }", 'needsCallee', 'needsParentScope')
176
177// A function referencing "arguments" will have to be vararg. It also needs
178// the callee, as it needs to fill out "arguments.callee".
179testFirstFn("function f() { arguments }", 'needsCallee', 'isVarArg')
180
181// A function referencing "arguments" will have to be vararg. If it is
182// strict, it will not have to have a callee, though.
183testFirstFn("function f() {'use strict'; arguments }", 'isVarArg', 'isStrict')
184
185// A function defining "arguments" as a parameter will not be vararg.
186testFirstFn("function f(arguments) { arguments }")
187
188// A function defining "arguments" as a nested function will not be vararg.
189testFirstFn("function f() { function arguments() {}; arguments; }")
190
191// A function defining "arguments" as a local variable will be vararg.
192testFirstFn("function f() { var arguments; arguments; }", 'isVarArg', 'needsCallee')
193
194// A self-referencing function defined as a statement doesn't need a self
195// symbol, as it'll rather obtain itself from the parent scope.
196testFirstFn("function f() { f() }", 'needsCallee', 'needsParentScope')
197
198// A self-referencing function defined as an expression needs a self symbol,
199// as it can't obtain itself from the parent scope.
200testFirstFn("(function f() { f() })", 'needsCallee', 'needsSelfSymbol')
201
202// A child function accessing parent's variable triggers the need for scope
203// in parent
204testFirstFn("(function f() { var x; function g() { x } })", 'hasScopeBlock')
205
206// A child function accessing parent's parameter triggers the need for scope
207// in parent
208testFirstFn("(function f(x) { function g() { x } })", 'hasScopeBlock')
209
210// A child function accessing a global variable triggers the need for parent
211// scope in parent
212testFirstFn("(function f() { function g() { x } })", 'needsParentScope', 'needsCallee')
213
214// A child function redefining a local variable from its parent should not
215// affect the parent function in any way
216testFirstFn("(function f() { var x; function g() { var x; x } })")
217
218// Using "with" on its own doesn't do much.
219testFirstFn("(function f() { var o; with(o) {} })")
220
221// "with" referencing a local variable triggers scoping.
222testFirstFn("(function f() { var x; var y; with(x) { y } })", 'hasScopeBlock')
223
224// "with" referencing a non-local variable triggers parent scope.
225testFirstFn("(function f() { var x; with(x) { y } })", 'needsCallee', 'needsParentScope')
226
227// Nested function using "with" is pretty much the same as the parent
228// function needing with.
229testFirstFn("(function f() { function g() { var o; with(o) {} } })")
230
231// Nested function using "with" referencing a local variable.
232testFirstFn("(function f() { var x; function g() { var o; with(o) { x } } })", 'hasScopeBlock')
233
234// Using "eval" triggers pretty much everything. The function even needs to be
235// vararg, 'cause we don't know if eval will be using "arguments".
236testFirstFn("(function f() { eval() })", 'needsParentScope', 'needsCallee', 'hasScopeBlock', 'hasEval', 'isVarArg', 'allVarsInScope')
237
238// Nested function using "eval" is almost the same as parent function using
239// eval, but at least the parent doesn't have to be vararg.
240testFirstFn("(function f() { function g() { eval() } })", 'needsParentScope', 'needsCallee', 'hasScopeBlock', 'allVarsInScope')
241
242// Function with 250 named parameters is ordinary
243testFirstFn("function f(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, p31, p32, p33, p34, p35, p36, p37, p38, p39, p40, p41, p42, p43, p44, p45, p46, p47, p48, p49, p50, p51, p52, p53, p54, p55, p56, p57, p58, p59, p60, p61, p62, p63, p64, p65, p66, p67, p68, p69, p70, p71, p72, p73, p74, p75, p76, p77, p78, p79, p80, p81, p82, p83, p84, p85, p86, p87, p88, p89, p90, p91, p92, p93, p94, p95, p96, p97, p98, p99, p100, p101, p102, p103, p104, p105, p106, p107, p108, p109, p110, p111, p112, p113, p114, p115, p116, p117, p118, p119, p120, p121, p122, p123, p124, p125, p126, p127, p128, p129, p130, p131, p132, p133, p134, p135, p136, p137, p138, p139, p140, p141, p142, p143, p144, p145, p146, p147, p148, p149, p150, p151, p152, p153, p154, p155, p156, p157, p158, p159, p160, p161, p162, p163, p164, p165, p166, p167, p168, p169, p170, p171, p172, p173, p174, p175, p176, p177, p178, p179, p180, p181, p182, p183, p184, p185, p186, p187, p188, p189, p190, p191, p192, p193, p194, p195, p196, p197, p198, p199, p200, p201, p202, p203, p204, p205, p206, p207, p208, p209, p210, p211, p212, p213, p214, p215, p216, p217, p218, p219, p220, p221, p222, p223, p224, p225, p226, p227, p228, p229, p230, p231, p232, p233, p234, p235, p236, p237, p238, p239, p240, p241, p242, p243, p244, p245, p246, p247, p248, p249, p250) { p250 = p249 }")
244
245// Function with 251 named parameters is variable arguments
246testFirstFn("function f(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, p31, p32, p33, p34, p35, p36, p37, p38, p39, p40, p41, p42, p43, p44, p45, p46, p47, p48, p49, p50, p51, p52, p53, p54, p55, p56, p57, p58, p59, p60, p61, p62, p63, p64, p65, p66, p67, p68, p69, p70, p71, p72, p73, p74, p75, p76, p77, p78, p79, p80, p81, p82, p83, p84, p85, p86, p87, p88, p89, p90, p91, p92, p93, p94, p95, p96, p97, p98, p99, p100, p101, p102, p103, p104, p105, p106, p107, p108, p109, p110, p111, p112, p113, p114, p115, p116, p117, p118, p119, p120, p121, p122, p123, p124, p125, p126, p127, p128, p129, p130, p131, p132, p133, p134, p135, p136, p137, p138, p139, p140, p141, p142, p143, p144, p145, p146, p147, p148, p149, p150, p151, p152, p153, p154, p155, p156, p157, p158, p159, p160, p161, p162, p163, p164, p165, p166, p167, p168, p169, p170, p171, p172, p173, p174, p175, p176, p177, p178, p179, p180, p181, p182, p183, p184, p185, p186, p187, p188, p189, p190, p191, p192, p193, p194, p195, p196, p197, p198, p199, p200, p201, p202, p203, p204, p205, p206, p207, p208, p209, p210, p211, p212, p213, p214, p215, p216, p217, p218, p219, p220, p221, p222, p223, p224, p225, p226, p227, p228, p229, p230, p231, p232, p233, p234, p235, p236, p237, p238, p239, p240, p241, p242, p243, p244, p245, p246, p247, p248, p249, p250, p251) { p250 = p251 }", 'isVarArg')
247