|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 const ASM_OK_STRING = "successfully compiled asm.js code"; |
|
6 const ASM_TYPE_FAIL_STRING = "asm.js type error:"; |
|
7 const ASM_DIRECTIVE_FAIL_STRING = "\"use asm\" is only meaningful in the Directive Prologue of a function body"; |
|
8 |
|
9 const USE_ASM = '"use asm";'; |
|
10 const HEAP_IMPORTS = "const i8=new glob.Int8Array(b);var u8=new glob.Uint8Array(b);"+ |
|
11 "const i16=new glob.Int16Array(b);var u16=new glob.Uint16Array(b);"+ |
|
12 "const i32=new glob.Int32Array(b);var u32=new glob.Uint32Array(b);"+ |
|
13 "const f32=new glob.Float32Array(b);var f64=new glob.Float64Array(b);"; |
|
14 const BUF_64KB = new ArrayBuffer(64 * 1024); |
|
15 |
|
16 function asmCompile() |
|
17 { |
|
18 var f = Function.apply(null, arguments); |
|
19 assertEq(!isAsmJSCompilationAvailable() || isAsmJSModule(f), true); |
|
20 return f; |
|
21 } |
|
22 |
|
23 function asmCompileCached() |
|
24 { |
|
25 if (!isAsmJSCompilationAvailable()) |
|
26 return Function.apply(null, arguments); |
|
27 |
|
28 if (!isCachingEnabled()) { |
|
29 var f = Function.apply(null, arguments); |
|
30 assertEq(isAsmJSModule(f), true); |
|
31 return f; |
|
32 } |
|
33 |
|
34 var quotedArgs = []; |
|
35 for (var i = 0; i < arguments.length; i++) |
|
36 quotedArgs.push("'" + arguments[i] + "'"); |
|
37 var code = "setCachingEnabled(true); var f = new Function(" + quotedArgs.join(',') + ");assertEq(isAsmJSModule(f), true);"; |
|
38 nestedShell("--js-cache", "--execute=" + code); |
|
39 |
|
40 var f = Function.apply(null, arguments); |
|
41 assertEq(isAsmJSModuleLoadedFromCache(f), true); |
|
42 return f; |
|
43 } |
|
44 |
|
45 function assertAsmDirectiveFail(str) |
|
46 { |
|
47 if (!isAsmJSCompilationAvailable()) |
|
48 return; |
|
49 |
|
50 // Turn on warnings-as-errors |
|
51 var oldOpts = options("werror"); |
|
52 assertEq(oldOpts.indexOf("werror"), -1); |
|
53 |
|
54 // Verify an error is thrown |
|
55 var caught = false; |
|
56 try { |
|
57 eval(str); |
|
58 } catch (e) { |
|
59 if ((''+e).indexOf(ASM_DIRECTIVE_FAIL_STRING) == -1) |
|
60 throw new Error("Didn't catch the expected directive failure error; instead caught: " + e + "\nStack: " + new Error().stack); |
|
61 caught = true; |
|
62 } |
|
63 if (!caught) |
|
64 throw new Error("Didn't catch the directive failure error"); |
|
65 |
|
66 // Turn warnings-as-errors back off |
|
67 options("werror"); |
|
68 } |
|
69 |
|
70 function assertAsmTypeFail() |
|
71 { |
|
72 if (!isAsmJSCompilationAvailable()) |
|
73 return; |
|
74 |
|
75 // Verify no error is thrown with warnings off |
|
76 Function.apply(null, arguments); |
|
77 |
|
78 // Turn on warnings-as-errors |
|
79 var oldOpts = options("werror"); |
|
80 assertEq(oldOpts.indexOf("werror"), -1); |
|
81 |
|
82 // Verify an error is thrown |
|
83 var caught = false; |
|
84 try { |
|
85 Function.apply(null, arguments); |
|
86 } catch (e) { |
|
87 if ((''+e).indexOf(ASM_TYPE_FAIL_STRING) == -1) |
|
88 throw new Error("Didn't catch the expected type failure error; instead caught: " + e + "\nStack: " + new Error().stack); |
|
89 caught = true; |
|
90 } |
|
91 if (!caught) |
|
92 throw new Error("Didn't catch the type failure error"); |
|
93 |
|
94 // Turn warnings-as-errors back off |
|
95 options("werror"); |
|
96 } |
|
97 |
|
98 function assertAsmLinkFail(f) |
|
99 { |
|
100 if (!isAsmJSCompilationAvailable()) |
|
101 return; |
|
102 |
|
103 assertEq(isAsmJSModule(f), true); |
|
104 |
|
105 // Verify no error is thrown with warnings off |
|
106 var ret = f.apply(null, Array.slice(arguments, 1)); |
|
107 |
|
108 assertEq(isAsmJSFunction(ret), false); |
|
109 if (typeof ret === 'object') |
|
110 for (f of ret) |
|
111 assertEq(isAsmJSFunction(f), false); |
|
112 |
|
113 // Turn on warnings-as-errors |
|
114 var oldOpts = options("werror"); |
|
115 assertEq(oldOpts.indexOf("werror"), -1); |
|
116 |
|
117 // Verify an error is thrown |
|
118 var caught = false; |
|
119 try { |
|
120 f.apply(null, Array.slice(arguments, 1)); |
|
121 } catch (e) { |
|
122 // Arbitrary code an run in the GetProperty, so don't assert any |
|
123 // particular string |
|
124 caught = true; |
|
125 } |
|
126 if (!caught) |
|
127 throw new Error("Didn't catch the link failure error"); |
|
128 |
|
129 // Turn warnings-as-errors back off |
|
130 options("werror"); |
|
131 } |
|
132 |
|
133 // Linking should throw an exception even without warnings-as-errors |
|
134 function assertAsmLinkAlwaysFail(f) |
|
135 { |
|
136 var caught = false; |
|
137 try { |
|
138 f.apply(null, Array.slice(arguments, 1)); |
|
139 } catch (e) { |
|
140 caught = true; |
|
141 } |
|
142 if (!caught) |
|
143 throw new Error("Didn't catch the link failure error"); |
|
144 |
|
145 // Turn on warnings-as-errors |
|
146 var oldOpts = options("werror"); |
|
147 assertEq(oldOpts.indexOf("werror"), -1); |
|
148 |
|
149 // Verify an error is thrown |
|
150 var caught = false; |
|
151 try { |
|
152 f.apply(null, Array.slice(arguments, 1)); |
|
153 } catch (e) { |
|
154 caught = true; |
|
155 } |
|
156 if (!caught) |
|
157 throw new Error("Didn't catch the link failure error"); |
|
158 |
|
159 // Turn warnings-as-errors back off |
|
160 options("werror"); |
|
161 } |
|
162 |
|
163 // Linking should throw a warning-as-error but otherwise run fine |
|
164 function asmLink(f) |
|
165 { |
|
166 if (!isAsmJSCompilationAvailable()) |
|
167 return f.apply(null, Array.slice(arguments, 1)); |
|
168 |
|
169 // Turn on warnings-as-errors |
|
170 var oldOpts = options("werror"); |
|
171 assertEq(oldOpts.indexOf("werror"), -1); |
|
172 |
|
173 var ret = f.apply(null, Array.slice(arguments, 1)); |
|
174 |
|
175 // Turn warnings-as-errors back off |
|
176 options("werror"); |
|
177 |
|
178 return ret; |
|
179 } |