1 /*
   2  * Copyright (c) 2010, 2014, 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-8046905: apply on apply is broken
  26  *
  27  * @test
  28  * @run
  29  */
  30 
  31 var apply = Function.prototype.apply;
  32 var call = Function.prototype.call;
  33 var sort = Array.prototype.sort;
  34 var join = Array.prototype.join;
  35 
  36 // Running three times so that we test an already linked call site too:
  37 // i==0: linking initially with assumed optimistic returned type int.
  38 // i==1: linking after deoptimization with returned type Object.
  39 // i==2: re-running code linked in previous iteration. This will
  40 //       properly exercise the guards too.
  41 print("1 level of apply")
  42 for(i = 0; i < 3; ++i) {
  43     print(sort.apply([4,3,2,1]))
  44 }
  45 print("2 levels of apply")
  46 for(i = 0; i < 3; ++i) {
  47     print(apply.apply(sort,[[4,3,2,1]]))
  48 }
  49 print("3 levels of apply")
  50 for(i = 0; i < 3; ++i) {
  51     print(apply.apply(apply,[sort,[[4,3,2,1]]]))
  52 }
  53 print("4 levels of apply")
  54 for(i = 0; i < 3; ++i) {
  55     print(apply.apply(apply,[apply,[sort,[[4,3,2,1]]]]))
  56 }
  57 print("5 levels of apply")
  58 for(i = 0; i < 3; ++i) {
  59     print(apply.apply(apply,[apply,[apply,[sort,[[4,3,2,1]]]]]))
  60 }
  61 print("Many levels of apply!")
  62 for(i = 0; i < 3; ++i) {
  63     print(apply.apply(apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[apply,[sort,[[4,3,2,1]]]]]]]]]]]]]]]]]]]]]]))
  64 }
  65 
  66 print("different invocations that'll trigger relinking")
  67 var invocation = [sort,[[4,3,2,1]]];
  68 for(i = 0; i < 4; ++i) {
  69     print(apply.apply(apply,[apply,invocation]))
  70     // First change after i==1, so it relinks an otherwise stable linkage
  71     if(i == 1) {
  72     invocation = [sort,[[8,7,6,5]]];
  73     } else if(i == 2) {
  74         invocation = [join,[[8,7,6,5],["-"]]];
  75     }
  76 }
  77 
  78 print("Many levels of call!")
  79 for(i = 0; i < 3; ++i) {
  80     print(call.call(call,call,call,call,call,call,call,call,call,call,call,call,call,call,call,call,call,call,call,call,sort,[4,3,2,1]))
  81 }
  82 
  83 print("call apply call apply call... a lot");
  84 for(i = 0; i < 3; ++i) {
  85     print(apply.call(call, apply, [call, apply, [call, apply, [call, apply, [call, apply, [call, apply, [sort, [4,3,2,1]]]]]]]))
  86 }
  87 
  88 print("apply call apply call apply... a lot");
  89 for(i = 0; i < 3; ++i) {
  90     print(call.apply(apply, [call, apply, [call, apply, [call, apply, [call, apply, [call, apply, [call, apply, [call, sort, [[4,3,2,1]]]]]]]]]))
  91 }