summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/es6/tail-call-megatest.js
blob: 3d2ecb8daa3884a02287bac12b2e25e53d289fdd (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// 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


Error.prepareStackTrace = (error,stack) => {
  error.strace = stack;
  return error.message + "\n    at " + stack.join("\n    at ");
}

var verbose = typeof(arguments) !== "undefined" && arguments.indexOf("-v") >= 0;

function checkStackTrace(expected) {
  var e = new Error();
  e.stack;  // prepare stack trace
  var stack = e.strace;
  assertEquals("checkStackTrace", stack[0].getFunctionName());
  for (var i = 0; i < expected.length; i++) {
    assertEquals(expected[i].name, stack[i + 1].getFunctionName());
  }
}


var CAN_INLINE_COMMENT  = "// Let it be inlined.";
var DONT_INLINE_COMMENT = (function() {
  var line = "// Don't inline. Don't inline. Don't inline. Don't inline.";
  for (var i = 0; i < 4; i++) {
    line += "\n  " + line;
  }
  return line;
})();


function ident_source(source, ident) {
  ident = " ".repeat(ident);
  return ident + source.replace(/\n/gi, "\n" + ident);
}

var SHARDS_COUNT = 10;

function run_tests(shard) {
  function inlinable_comment(inlinable) {
    return inlinable ? CAN_INLINE_COMMENT : DONT_INLINE_COMMENT;
  }

  // Check arguments manually to avoid bailing out with reason "bad value
  // context for arguments value".
  function check_arguments_template(expected_name) {
    var lines = [
      `  assertEquals_(${expected_name}.length, arguments.length);`,
      `  for (var i = 0; i < ${expected_name}.length; i++) {`,
      `    assertEquals_(${expected_name}[i], arguments[i]);`,
      `  }`,
    ];
    return lines.join("\n");
  }
  var check_arguments = check_arguments_template("expected_args");

  function deopt_template(deopt_mode) {
    switch(deopt_mode) {
      case "none":
        return "  // Don't deoptimize";
      case "f":
      case "g":
      case "test":
        return `  %DeoptimizeFunction(${deopt_mode});`;
      default:
        assertUnreachable();
    }
  }

  var f_cfg_sloppy = {
    func_name: 'f',
    source_template: function(cfg) {
      var receiver = cfg.f_receiver != undefined ? cfg.f_receiver
                                                 : "global";
      var do_checks = [
        `  assertEquals_(${receiver}, this);`,
        `  ${!cfg.check_new_target ? "// " : ""}assertEquals_(undefined, new.target);`,
        check_arguments,
        `  checkStackTrace_([f, test]);`,
      ].join("\n");

      var lines = [
        `function f(a) {`,
        `  ${inlinable_comment(cfg.f_inlinable)}`,
        `  counter++;`,
        `  var expected_args = [${cfg.f_args}];`,
        do_checks,
        deopt_template(cfg.deopt_mode),
        do_checks,
        `  return 42;`,
        `}`,
      ];
      return lines.join("\n");
    },
  };

  var f_cfg_strict = {
    func_name: 'f',
    source_template: function(cfg) {
      var receiver = cfg.f_receiver != undefined ? cfg.f_receiver
                                                 : "undefined";
      var do_checks = [
        `  assertEquals_(${receiver}, this);`,
        `  ${!cfg.check_new_target ? "// " : ""}assertEquals_(undefined, new.target);`,
        check_arguments,
        `  checkStackTrace_([f, test]);`,
      ].join("\n");

      var lines = [
        `function f(a) {`,
        `  "use strict";`,
        `  ${inlinable_comment(cfg.f_inlinable)}`,
        `  counter++;`,
        `  var expected_args = [${cfg.f_args}];`,
        do_checks,
        deopt_template(cfg.deopt_mode),
        do_checks,
        `  return 42;`,
        `}`,
      ];
      return lines.join("\n");
    },
  };

  var f_cfg_possibly_eval = {
    func_name: 'eval',
    source_template: function(cfg) {
      var receiver = cfg.f_receiver != undefined ? cfg.f_receiver
                                                 : "global";
      var do_checks = [
        `  assertEquals_(${receiver}, this);`,
        `  ${!cfg.check_new_target ? "// " : ""}assertEquals_(undefined, new.target);`,
        check_arguments,
        `  checkStackTrace_([f, test]);`,
      ].join("\n");

      var lines = [
        `function f(a) {`,
        `  ${inlinable_comment(cfg.f_inlinable)}`,
        `  counter++;`,
        `  var expected_args = [${cfg.f_args}];`,
        do_checks,
        deopt_template(cfg.deopt_mode),
        do_checks,
        `  return 42;`,
        `}`,
        `var eval = f;`,
      ];
      return lines.join("\n");
    },
  };

  var f_cfg_bound = {
    func_name: 'bound',
    source_template: function(cfg) {
      var do_checks = [
        `  assertEquals_(receiver, this);`,
        `  ${!cfg.check_new_target ? "// " : ""}assertEquals_(undefined, new.target);`,
        check_arguments,
        `  checkStackTrace_([f, test]);`,
      ].join("\n");

      var lines = [
        `function f(a) {`,
        `  "use strict";`,
        `  ${inlinable_comment(cfg.f_inlinable)}`,
        `  counter++;`,
        `  var expected_args = [${cfg.f_args}];`,
        do_checks,
        deopt_template(cfg.deopt_mode),
        do_checks,
        `  return 42;`,
        `}`,
        `var receiver = {a: 153};`,
        `var bound = f.bind(receiver);`,
      ];
      return lines.join("\n");
    },
  };

  var f_cfg_proxy = {
    func_name: 'p',
    source_template: function(cfg) {
      var receiver = cfg.f_receiver != undefined ? cfg.f_receiver
                                                 : "global";
      var do_checks = [
        `  assertEquals_(${receiver}, this);`,
        `  ${!cfg.check_new_target ? "// " : ""}assertEquals_(undefined, new.target);`,
        check_arguments,
        `  checkStackTrace_([f, test]);`,
      ].join("\n");

      var lines = [
        `function f(a) {`,
        `  ${inlinable_comment(cfg.f_inlinable)}`,
        `  counter++;`,
        `  var expected_args = [${cfg.f_args}];`,
        do_checks,
        deopt_template(cfg.deopt_mode),
        do_checks,
        `  return 42;`,
        `}`,
        `var p = new Proxy(f, {});`,
      ];
      return lines.join("\n");
    },
  };

  var g_cfg_normal = {
    receiver: undefined,
    source_template: function(cfg) {
      var lines = [
        `function g(a) {`,
        `  "use strict";`,
        `  ${inlinable_comment(cfg.g_inlinable)}`,
        `  var expected_args = [${cfg.g_args}];`,
        check_arguments,
        `  return ${cfg.f_name}(${cfg.f_args});`,
        `}`,
      ];
      return lines.join("\n");
    },
  };


  var g_cfg_reflect_apply = {
    receiver: "the_receiver",
    source_template: function(cfg) {
      var lines = [
        `function g(a) {`,
        `  "use strict";`,
        `  ${inlinable_comment(cfg.g_inlinable)}`,
        `  var expected_args = [${cfg.g_args}];`,
        check_arguments,
        `  return Reflect.apply(${cfg.f_name}, the_receiver, [${cfg.f_args}]);`,
        `}`,
      ];
      return lines.join("\n");
    },
  };


  var g_cfg_function_apply = {
    receiver: "the_receiver",
    source_template: function(cfg) {
      var lines = [
        `function g(a) {`,
        `  "use strict";`,
        `  ${inlinable_comment(cfg.g_inlinable)}`,
        `  var expected_args = [${cfg.g_args}];`,
        check_arguments,
        `  return ${cfg.f_name}.apply(the_receiver, [${cfg.f_args}]);`,
        `}`,
      ];
      return lines.join("\n");
    },
  };


  var g_cfg_function_apply_arguments_object = {
    receiver: "the_receiver",
    source_template: function(cfg) {
      cfg.f_args = cfg.g_args;
      var lines = [
        `function g(a) {`,
        `  "use strict";`,
        `  ${inlinable_comment(cfg.g_inlinable)}`,
        `  var expected_args = [${cfg.g_args}];`,
        check_arguments,
        `  return ${cfg.f_name}.apply(the_receiver, arguments);`,
        `}`,
      ];
      return lines.join("\n");
    },
  };


  var g_cfg_function_call = {
    receiver: "the_receiver",
    source_template: function(cfg) {
      var f_args = "the_receiver";
      if (cfg.f_args !== "") f_args += ", ";
      f_args += cfg.f_args;

      var lines = [
        `function g(a) {`,
        `  "use strict";`,
        `  ${inlinable_comment(cfg.g_inlinable)}`,
        `  var expected_args = [${cfg.g_args}];`,
        check_arguments,
        `  return ${cfg.f_name}.call(${f_args});`,
        `}`,
      ];
      return lines.join("\n");
    },
  };


  function test_template(cfg) {
    // Note: g_source_template modifies cfg.f_args in some cases.
    var g_source = cfg.g_source_template(cfg);
    g_source = ident_source(g_source, 2);

    var f_source = cfg.f_source_template(cfg);
    f_source = ident_source(f_source, 2);

    var lines = [
      `(function() {`,
      `  // Avoid bailing out because of "Reference to a variable which requires dynamic lookup".`,
      `  var assertEquals_ = assertEquals;`,
      `  var checkStackTrace_ = checkStackTrace;`,
      `  var undefined = void 0;`,
      `  var global = Function('return this')();`,
      `  var the_receiver = {receiver: 1};`,
      `  var counter = 0;`,
      ``,
      `  // Don't inline helper functions`,
      `  %NeverOptimizeFunction(assertEquals);`,
      `  %NeverOptimizeFunction(checkStackTrace);`,
      ``,
      f_source,
      g_source,
      `  function test() {`,
      `    "use strict";`,
      `    assertEquals_(42, g(${cfg.g_args}));`,
      `  }`,
      `  ${"test();".repeat(cfg.test_warmup_count)}`,
      `  ${cfg.f_inlinable ? "%SetForceInlineFlag(f)" : "%OptimizeFunctionOnNextCall(f)"};`,
      `  ${cfg.g_inlinable ? "%SetForceInlineFlag(g)" : "%OptimizeFunctionOnNextCall(g)"};`,
      `  %OptimizeFunctionOnNextCall(test);`,
      `  test();`,
      `  assertEquals(${1 + cfg.test_warmup_count}, counter);`,
      `})();`,
      ``,
    ];
    var source = lines.join("\n");
    return source;
  }

  var f_args_variants = [/*"", "1",*/ "1, 2"];
  var g_args_variants = [/*"", "10",*/ "10, 20"];
  var f_inlinable_variants = [true, false];
  var g_inlinable_variants = [true, false];
  // This is to avoid bailing out because of referencing new.target.
  var check_new_target_variants = [/*true,*/ false];
  var deopt_mode_variants = ["none", "f", "g", "test"];
  var f_variants = [
      f_cfg_sloppy,
      f_cfg_strict,
      f_cfg_bound,
      f_cfg_proxy,
//      f_cfg_possibly_eval,
  ];
  var g_variants = [
      g_cfg_normal,
//      g_cfg_reflect_apply,
      g_cfg_function_apply,
//      g_cfg_function_apply_arguments_object,
      g_cfg_function_call,
  ];
  var test_warmup_counts = [0, 1, 2];

  var iter = 0;
  var tests_executed = 0;
  if (verbose && shard !== undefined) {
    print("Running shard #" + shard);
  }
  f_variants.forEach((f_cfg) => {
    check_new_target_variants.forEach((check_new_target) => {
      deopt_mode_variants.forEach((deopt_mode) => {
        g_variants.forEach((g_cfg) => {
          f_args_variants.forEach((f_args) => {
            g_args_variants.forEach((g_args) => {
              f_inlinable_variants.forEach((f_inlinable) => {
                g_inlinable_variants.forEach((g_inlinable) => {
                  test_warmup_counts.forEach((test_warmup_count) => {
                    if (shard !== undefined && (iter++) % SHARDS_COUNT != shard) {
                      if (verbose) {
                        print("skipping...");
                      }
                      return;
                    }
                    tests_executed++;
                    var cfg = {
                      f_source_template: f_cfg.source_template,
                      f_inlinable,
                      f_args,
                      f_name: f_cfg.func_name,
                      f_receiver: g_cfg.receiver,
                      g_source_template: g_cfg.source_template,
                      g_inlinable,
                      g_args,
                      test_warmup_count,
                      check_new_target,
                      deopt_mode,
                    };
                    var source = test_template(cfg);
                    if (verbose) {
                      // print("====================");
                      // print(source);
                    }
                    eval(source);
                  });
                });
              });
            });
          });
        });
      });
    });
  });
  if (verbose) {
    print("Number of tests executed: " + tests_executed);
  }
}

// Uncomment to run all the tests at once or use shard runners.
//run_tests();