test/script/trusted/JDK-8006529.js

Thu, 07 Feb 2013 17:17:29 +0530

author
sundar
date
Thu, 07 Feb 2013 17:17:29 +0530
changeset 77
d7e83be6e7aa
parent 62
test/script/basic/JDK-8006529.js@f7825c1a11d3
child 82
abea4ba28901
permissions
-rw-r--r--

8007715: Make sure that not all tests run with AllPermission
Reviewed-by: lagergren, attila

attila@62 1 /*
attila@62 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
attila@62 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
attila@62 4 *
attila@62 5 * This code is free software; you can redistribute it and/or modify it
attila@62 6 * under the terms of the GNU General Public License version 2 only, as
attila@62 7 * published by the Free Software Foundation.
attila@62 8 *
attila@62 9 * This code is distributed in the hope that it will be useful, but WITHOUT
attila@62 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
attila@62 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
attila@62 12 * version 2 for more details (a copy is included in the LICENSE file that
attila@62 13 * accompanied this code).
attila@62 14 *
attila@62 15 * You should have received a copy of the GNU General Public License version
attila@62 16 * 2 along with this work; if not, write to the Free Software Foundation,
attila@62 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
attila@62 18 *
attila@62 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
attila@62 20 * or visit www.oracle.com if you need additional information or have any
attila@62 21 * questions.
attila@62 22 */
attila@62 23
attila@62 24 /**
attila@62 25 * JDK-8006529 : Methods should not always get callee parameter, and they
attila@62 26 * should not be too eager in creation of scopes.
attila@62 27 *
attila@62 28 * @test
attila@62 29 * @run
attila@62 30 */
attila@62 31
attila@62 32 // compile(script) -- compiles a script specified as a string with its
attila@62 33 // source code, returns a jdk.nashorn.internal.ir.FunctionNode object
attila@62 34 // representing it.
attila@62 35 var compile = (function() {
attila@62 36 var Compiler = Java.type("jdk.nashorn.internal.codegen.Compiler")
attila@62 37 var Context = Java.type("jdk.nashorn.internal.runtime.Context")
attila@62 38 var Source = Java.type("jdk.nashorn.internal.runtime.Source")
attila@62 39 var CompilerAccess = Java.type("jdk.nashorn.internal.codegen.CompilerAccess")
attila@62 40 return function(source) {
attila@62 41 var compiler = Compiler.compiler(new Source("<no name>", source), Context.getContext())
attila@62 42 compiler.compile()
attila@62 43 return CompilerAccess.getScriptNode(compiler)
attila@62 44 }
attila@62 45 })();
attila@62 46
attila@62 47 var allAssertions = (function() {
attila@62 48 var allAssertionList = ['isVarArg', 'needsParentScope', 'needsCallee', 'needsScope', 'needsSelfSymbol', 'isSplit', 'hasEval', 'hasWith', 'hasDeepWithOrEval', 'varsInScope', 'isStrictMode']
attila@62 49 var allAssertions = {}
attila@62 50 for(var assertion in allAssertionList) {
attila@62 51 allAssertions[allAssertionList[assertion]] = true
attila@62 52 }
attila@62 53 return allAssertions;
attila@62 54 })();
attila@62 55
attila@62 56 // test(f[, assertions...]) tests whether all the specified assertions on the
attila@62 57 // passed function node are true.
attila@62 58 function test(f) {
attila@62 59 var assertions = {}
attila@62 60 for(var i = 1; i < arguments.length; ++i) {
attila@62 61 var assertion = arguments[i]
attila@62 62 if(!allAssertions[assertion]) {
attila@62 63 throw "Unknown assertion " + assertion + " for " + f;
attila@62 64 }
attila@62 65 assertions[assertion] = true
attila@62 66 }
attila@62 67 for(var assertion in allAssertions) {
attila@62 68 var expectedValue = !!assertions[assertion]
attila@62 69 if(f[assertion] == null) {
attila@62 70 throw "Can't find " + assertion + " on " + f;
attila@62 71 }
attila@62 72 if(f[assertion]() !== expectedValue) {
attila@62 73 throw "Expected " + assertion + " === " + expectedValue + " for " + f;
attila@62 74 }
attila@62 75 }
attila@62 76 }
attila@62 77
attila@62 78 // testFirstFn(script[, assertions...] tests whether all the specified
attila@62 79 // assertions are true in the first function in the given script; "script"
attila@62 80 // is a string with the source text of the script.
attila@62 81 function testFirstFn(script) {
attila@62 82 arguments[0] = compile(script).functions[0]
attila@62 83 test.apply(null, arguments)
attila@62 84 }
attila@62 85
attila@62 86 // ---------------------------------- ACTUAL TESTS START HERE --------------
attila@62 87
attila@62 88 // The simplest possible functions have no attributes set
attila@62 89 testFirstFn("function f() { }")
attila@62 90 testFirstFn("function f(x) { x }")
attila@62 91
attila@62 92 // A function referencing a global needs parent scope, and it needs callee
attila@62 93 // (because parent scope is passed through callee)
attila@62 94 testFirstFn("function f() { x }", 'needsCallee', 'needsParentScope')
attila@62 95
attila@62 96 // A function referencing "arguments" will have to be vararg. It also needs
attila@62 97 // the callee, as it needs to fill out "arguments.callee".
attila@62 98 testFirstFn("function f() { arguments }", 'needsCallee', 'isVarArg')
attila@62 99
attila@62 100 // A function referencing "arguments" will have to be vararg. If it is
attila@62 101 // strict, it will not have to have a callee, though.
attila@62 102 testFirstFn("function f() {'use strict'; arguments }", 'isVarArg', 'isStrictMode')
attila@62 103
attila@62 104 // A function defining "arguments" as a parameter will not be vararg.
attila@62 105 testFirstFn("function f(arguments) { arguments }")
attila@62 106
attila@62 107 // A function defining "arguments" as a nested function will not be vararg.
attila@62 108 testFirstFn("function f() { function arguments() {}; arguments; }")
attila@62 109
attila@62 110 // A function defining "arguments" as a local variable will be vararg.
attila@62 111 testFirstFn("function f() { var arguments; arguments; }", 'isVarArg', 'needsCallee')
attila@62 112
attila@62 113 // A self-referencing function defined as a statement doesn't need a self
attila@62 114 // symbol, as it'll rather obtain itself from the parent scope.
attila@62 115 testFirstFn("function f() { f() }", 'needsCallee', 'needsParentScope')
attila@62 116
attila@62 117 // A self-referencing function defined as an expression needs a self symbol,
attila@62 118 // as it can't obtain itself from the parent scope.
attila@62 119 testFirstFn("(function f() { f() })", 'needsCallee', 'needsSelfSymbol')
attila@62 120
attila@62 121 // A child function accessing parent's variable triggers the need for scope
attila@62 122 // in parent
attila@62 123 testFirstFn("(function f() { var x; function g() { x } })", 'needsScope')
attila@62 124
attila@62 125 // A child function accessing parent's parameter triggers the need for scope
attila@62 126 // in parent
attila@62 127 testFirstFn("(function f(x) { function g() { x } })", 'needsScope')
attila@62 128
attila@62 129 // A child function accessing a global variable triggers the need for parent
attila@62 130 // scope in parent
attila@62 131 testFirstFn("(function f() { function g() { x } })", 'needsParentScope', 'needsCallee')
attila@62 132
attila@62 133 // A child function redefining a local variable from its parent should not
attila@62 134 // affect the parent function in any way
attila@62 135 testFirstFn("(function f() { var x; function g() { var x; x } })")
attila@62 136
attila@62 137 // Using "with" unleashes a lot of needs: parent scope, callee, own scope,
attila@62 138 // and all variables in scope. Actually, we could make "with" less wasteful,
attila@62 139 // and only put those variables in scope that it actually references, similar
attila@62 140 // to what nested functions do with variables in their parents.
attila@62 141 testFirstFn("(function f() { var o; with(o) {} })", 'needsParentScope', 'needsCallee', 'needsScope', 'hasWith', 'hasDeepWithOrEval', 'varsInScope')
attila@62 142
attila@62 143 // Using "eval" is as bad as using "with" with the added requirement of
attila@62 144 // being vararg, 'cause we don't know if eval will be using "arguments".
attila@62 145 testFirstFn("(function f() { eval() })", 'needsParentScope', 'needsCallee', 'needsScope', 'hasEval', 'isVarArg', 'hasDeepWithOrEval', 'varsInScope')
attila@62 146
attila@62 147 // Nested function using "with" is pretty much the same as the parent
attila@62 148 // function needing with.
attila@62 149 testFirstFn("(function f() { function g() { var o; with(o) {} } })", 'needsParentScope', 'needsCallee', 'needsScope', 'hasDeepWithOrEval', 'varsInScope')
attila@62 150 // Nested function using "eval" is almost the same as parent function using
attila@62 151 // eval, but at least the parent doesn't have to be vararg.
attila@62 152 testFirstFn("(function f() { function g() { eval() } })", 'needsParentScope', 'needsCallee', 'needsScope', 'hasDeepWithOrEval', 'varsInScope')
attila@62 153
attila@62 154 // Function with 250 named parameters is ordinary
attila@62 155 testFirstFn("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 }")
attila@62 156
attila@62 157 // Function with 251 named parameters is variable arguments
attila@62 158 testFirstFn("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')

mercurial