aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/es6/tail-call-proxies.js
blob: 25f9fcfbe7047a4e934b57c9d37c4d41ca20e867 (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
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --allow-natives-syntax --harmony-tailcalls --harmony-proxies
"use strict";

Error.prepareStackTrace = (e,s) => s;

function CheckStackTrace(expected) {
  var stack = (new Error()).stack;
  assertEquals("CheckStackTrace", stack[0].getFunctionName());
  for (var i = 0; i < expected.length; i++) {
    assertEquals(expected[i].name, stack[i + 1].getFunctionName());
  }
}


// Tail call proxy function when caller does not have an arguments
// adaptor frame.
(function test() {
  // Caller and callee have same number of arguments.
  function f1(a) {
    CheckStackTrace([f1, test]);
    return 10 + a;
  }
  var p1 = new Proxy(f1, {});
  function g1(a) { return p1(2); }
  assertEquals(12, g1(1));

  // Caller has more arguments than callee.
  function f2(a) {
    CheckStackTrace([f2, test]);
    return 10 + a;
  }
  var p2 = new Proxy(f2, {});
  function g2(a, b, c) { return p2(2); }
  assertEquals(12, g2(1, 2, 3));

  // Caller has less arguments than callee.
  function f3(a, b, c) {
    CheckStackTrace([f3, test]);
    return 10 + a + b + c;
  }
  var p3 = new Proxy(f3, {});
  function g3(a) { return p3(2, 3, 4); }
  assertEquals(19, g3(1));

  // Callee has arguments adaptor frame.
  function f4(a, b, c) {
    CheckStackTrace([f4, test]);
    return 10 + a;
  }
  var p4 = new Proxy(f4, {});
  function g4(a) { return p4(2); }
  assertEquals(12, g4(1));
})();


// Tail call proxy function when caller has an arguments adaptor frame.
(function test() {
  // Caller and callee have same number of arguments.
  function f1(a) {
    CheckStackTrace([f1, test]);
    return 10 + a;
  }
  var p1 = new Proxy(f1, {});
  function g1(a) { return p1(2); }
  assertEquals(12, g1());

  // Caller has more arguments than callee.
  function f2(a) {
    CheckStackTrace([f2, test]);
    return 10 + a;
  }
  var p2 = new Proxy(f2, {});
  function g2(a, b, c) { return p2(2); }
  assertEquals(12, g2());

  // Caller has less arguments than callee.
  function f3(a, b, c) {
    CheckStackTrace([f3, test]);
    return 10 + a + b + c;
  }
  var p3 = new Proxy(f3, {});
  function g3(a) { return p3(2, 3, 4); }
  assertEquals(19, g3());

  // Callee has arguments adaptor frame.
  function f4(a, b, c) {
    CheckStackTrace([f4, test]);
    return 10 + a;
  }
  var p4 = new Proxy(f4, {});
  function g4(a) { return p4(2); }
  assertEquals(12, g4());
})();