180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// Copyright 2012 the V8 project authors. All rights reserved.
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// Redistribution and use in source and binary forms, with or without
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// modification, are permitted provided that the following conditions are
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// met:
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//     * Redistributions of source code must retain the above copyright
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//       notice, this list of conditions and the following disclaimer.
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//     * Redistributions in binary form must reproduce the above
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//       copyright notice, this list of conditions and the following
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//       disclaimer in the documentation and/or other materials provided
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//       with the distribution.
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//     * Neither the name of Google Inc. nor the names of its
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//       contributors may be used to endorse or promote products derived
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//       from this software without specific prior written permission.
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// Flags: --allow-natives-syntax
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querufunction worker1(ignored) {
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  return 100;
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querufunction factory(worker) {
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  return function(call_depth) {
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (call_depth == 0) return 10;
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return 1 + worker(call_depth - 1);
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  }
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
40
41var f1 = factory(worker1);
42var f2 = factory(f1);
43assertEquals(11, f2(1));
44%OptimizeFunctionOnNextCall(f1);
45assertEquals(10, f1(0));
46%OptimizeFunctionOnNextCall(f2);
47assertEquals(102, f2(2));
48assertEquals(102, f2(2));
49