summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/ion-error-trace.js
blob: cd9d9dd1ad9697b65149e0af9dae6f9d13932b43 (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
///////////////////////////////////////////////////////////////////////////////
// FIRST TEST /////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

(function() {

function debug() {
    throw new Error('gotcha');
}

var imports = {
    numCalls:0,
    main: {
        f() {
            debug();
        }
    }
};

var instance = new WebAssembly.Instance(new WebAssembly.Module(wasmTextToBinary(`(module
    (import "main" "f" (func))
    (func (export "add") (param i32) (param i32) (result i32)
     local.get 0
     local.get 1
     call 2
    )
    (func (param i32) (param i32) (result i32)
     local.get 0
     i32.const 5000
     i32.eq
     if
         call 0
     end

     local.get 0
     local.get 1
     i32.add
    )
)`)), imports).exports;

function loopBody(i) {
    var caught = null;
    try {
        assertEq(instance.add(i, i), 2 * i);
    } catch(e) {
        // TODO check stack trace
        print(e.stack);
        caught = e;
    }
    assertEq(!!caught, i === 5000);
}

function main() {
    for (var i = 0; i < 100000; i++) {
        loopBody(i);
    }
    assertEq(i, 100000);
}

main();

})();

///////////////////////////////////////////////////////////////////////////////
// SECOND TEST ////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

(function() {

function debug() {
    gc();
}

var imports = {
    numCalls:0,
    main: {
        f() {
            debug();
        }
    }
};

var instance = new WebAssembly.Instance(new WebAssembly.Module(wasmTextToBinary(`(module
    (import "main" "f" (func))
    (func (export "add") (param i32) (param i32) (result i32)
     local.get 0
     local.get 1
     call 2
    )
    (func (param i32) (param i32) (result i32)
     local.get 0
     i32.const 5000
     i32.eq
     if
         call 0
         unreachable
     end

     local.get 0
     local.get 1
     i32.add
    )
)`)), imports).exports;

function loopBody(i) {
    var caught = null;
    try {
        assertEq(instance.add(i, i), 2 * i);
    } catch(e) {
        // TODO check stack trace
        print(e.stack);
        caught = e;
    }
    assertEq(!!caught, i === 5000);
}

function main() {
    for (var i = 0; i < 100000; i++) {
        loopBody(i);
    }
    assertEq(i, 100000);
}

main();

})();