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