1// Copyright 2016 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --expose-wasm
6
7load("test/mjsunit/wasm/wasm-constants.js");
8load("test/mjsunit/wasm/wasm-module-builder.js");
9
10var builder = new WasmModuleBuilder();
11
12var last_func_index = builder.addFunction("exec_unreachable", kSig_v_v)
13  .addBody([kExprUnreachable])
14
15var illegal_func_name = [0xff];
16var func_names = [ "☠", illegal_func_name, "some math: (½)² = ¼", "" ];
17var expected_names = ["exec_unreachable", "☠", null,
18                      "some math: (½)² = ¼", "", "main"];
19
20for (var func_name of func_names) {
21  last_func_index = builder.addFunction(func_name, kSig_v_v)
22    .addBody([kExprCallFunction, kArity0, last_func_index]).index;
23}
24
25builder.addFunction("main", kSig_v_v)
26  .addBody([kExprCallFunction, kArity0, last_func_index])
27  .exportFunc();
28
29var module = builder.instantiate();
30
31(function testFunctionNamesAsString() {
32  var names = expected_names.concat(["testFunctionNamesAsString", null]);
33  try {
34    module.exports.main();
35    assertFalse("should throw");
36  } catch (e) {
37    var lines = e.stack.split(/\r?\n/);
38    lines.shift();
39    assertEquals(names.length, lines.length);
40    for (var i = 0; i < names.length; ++i) {
41      var line = lines[i].trim();
42      if (names[i] === null) continue;
43      var printed_name = names[i] === undefined ? "<WASM UNNAMED>" : names[i]
44      var expected_start = "at " + printed_name + " (";
45      assertTrue(line.startsWith(expected_start),
46          "should start with '" + expected_start + "': '" + line + "'");
47    }
48  }
49})();
50
51// For the remaining tests, collect the Callsite objects instead of just a
52// string:
53Error.prepareStackTrace = function(error, frames) {
54  return frames;
55};
56
57
58(function testFunctionNamesAsCallSites() {
59  var names = expected_names.concat(["testFunctionNamesAsCallSites", null]);
60  try {
61    module.exports.main();
62    assertFalse("should throw");
63  } catch (e) {
64    assertEquals(names.length, e.stack.length);
65    for (var i = 0; i < names.length; ++i) {
66      assertEquals(names[i], e.stack[i].getFunctionName());
67    }
68  }
69})();
70