michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const ASM_OK_STRING = "successfully compiled asm.js code"; michael@0: const ASM_TYPE_FAIL_STRING = "asm.js type error:"; michael@0: const ASM_DIRECTIVE_FAIL_STRING = "\"use asm\" is only meaningful in the Directive Prologue of a function body"; michael@0: michael@0: const USE_ASM = '"use asm";'; michael@0: const HEAP_IMPORTS = "const i8=new glob.Int8Array(b);var u8=new glob.Uint8Array(b);"+ michael@0: "const i16=new glob.Int16Array(b);var u16=new glob.Uint16Array(b);"+ michael@0: "const i32=new glob.Int32Array(b);var u32=new glob.Uint32Array(b);"+ michael@0: "const f32=new glob.Float32Array(b);var f64=new glob.Float64Array(b);"; michael@0: const BUF_64KB = new ArrayBuffer(64 * 1024); michael@0: michael@0: function asmCompile() michael@0: { michael@0: var f = Function.apply(null, arguments); michael@0: assertEq(!isAsmJSCompilationAvailable() || isAsmJSModule(f), true); michael@0: return f; michael@0: } michael@0: michael@0: function asmCompileCached() michael@0: { michael@0: if (!isAsmJSCompilationAvailable()) michael@0: return Function.apply(null, arguments); michael@0: michael@0: if (!isCachingEnabled()) { michael@0: var f = Function.apply(null, arguments); michael@0: assertEq(isAsmJSModule(f), true); michael@0: return f; michael@0: } michael@0: michael@0: var quotedArgs = []; michael@0: for (var i = 0; i < arguments.length; i++) michael@0: quotedArgs.push("'" + arguments[i] + "'"); michael@0: var code = "setCachingEnabled(true); var f = new Function(" + quotedArgs.join(',') + ");assertEq(isAsmJSModule(f), true);"; michael@0: nestedShell("--js-cache", "--execute=" + code); michael@0: michael@0: var f = Function.apply(null, arguments); michael@0: assertEq(isAsmJSModuleLoadedFromCache(f), true); michael@0: return f; michael@0: } michael@0: michael@0: function assertAsmDirectiveFail(str) michael@0: { michael@0: if (!isAsmJSCompilationAvailable()) michael@0: return; michael@0: michael@0: // Turn on warnings-as-errors michael@0: var oldOpts = options("werror"); michael@0: assertEq(oldOpts.indexOf("werror"), -1); michael@0: michael@0: // Verify an error is thrown michael@0: var caught = false; michael@0: try { michael@0: eval(str); michael@0: } catch (e) { michael@0: if ((''+e).indexOf(ASM_DIRECTIVE_FAIL_STRING) == -1) michael@0: throw new Error("Didn't catch the expected directive failure error; instead caught: " + e + "\nStack: " + new Error().stack); michael@0: caught = true; michael@0: } michael@0: if (!caught) michael@0: throw new Error("Didn't catch the directive failure error"); michael@0: michael@0: // Turn warnings-as-errors back off michael@0: options("werror"); michael@0: } michael@0: michael@0: function assertAsmTypeFail() michael@0: { michael@0: if (!isAsmJSCompilationAvailable()) michael@0: return; michael@0: michael@0: // Verify no error is thrown with warnings off michael@0: Function.apply(null, arguments); michael@0: michael@0: // Turn on warnings-as-errors michael@0: var oldOpts = options("werror"); michael@0: assertEq(oldOpts.indexOf("werror"), -1); michael@0: michael@0: // Verify an error is thrown michael@0: var caught = false; michael@0: try { michael@0: Function.apply(null, arguments); michael@0: } catch (e) { michael@0: if ((''+e).indexOf(ASM_TYPE_FAIL_STRING) == -1) michael@0: throw new Error("Didn't catch the expected type failure error; instead caught: " + e + "\nStack: " + new Error().stack); michael@0: caught = true; michael@0: } michael@0: if (!caught) michael@0: throw new Error("Didn't catch the type failure error"); michael@0: michael@0: // Turn warnings-as-errors back off michael@0: options("werror"); michael@0: } michael@0: michael@0: function assertAsmLinkFail(f) michael@0: { michael@0: if (!isAsmJSCompilationAvailable()) michael@0: return; michael@0: michael@0: assertEq(isAsmJSModule(f), true); michael@0: michael@0: // Verify no error is thrown with warnings off michael@0: var ret = f.apply(null, Array.slice(arguments, 1)); michael@0: michael@0: assertEq(isAsmJSFunction(ret), false); michael@0: if (typeof ret === 'object') michael@0: for (f of ret) michael@0: assertEq(isAsmJSFunction(f), false); michael@0: michael@0: // Turn on warnings-as-errors michael@0: var oldOpts = options("werror"); michael@0: assertEq(oldOpts.indexOf("werror"), -1); michael@0: michael@0: // Verify an error is thrown michael@0: var caught = false; michael@0: try { michael@0: f.apply(null, Array.slice(arguments, 1)); michael@0: } catch (e) { michael@0: // Arbitrary code an run in the GetProperty, so don't assert any michael@0: // particular string michael@0: caught = true; michael@0: } michael@0: if (!caught) michael@0: throw new Error("Didn't catch the link failure error"); michael@0: michael@0: // Turn warnings-as-errors back off michael@0: options("werror"); michael@0: } michael@0: michael@0: // Linking should throw an exception even without warnings-as-errors michael@0: function assertAsmLinkAlwaysFail(f) michael@0: { michael@0: var caught = false; michael@0: try { michael@0: f.apply(null, Array.slice(arguments, 1)); michael@0: } catch (e) { michael@0: caught = true; michael@0: } michael@0: if (!caught) michael@0: throw new Error("Didn't catch the link failure error"); michael@0: michael@0: // Turn on warnings-as-errors michael@0: var oldOpts = options("werror"); michael@0: assertEq(oldOpts.indexOf("werror"), -1); michael@0: michael@0: // Verify an error is thrown michael@0: var caught = false; michael@0: try { michael@0: f.apply(null, Array.slice(arguments, 1)); michael@0: } catch (e) { michael@0: caught = true; michael@0: } michael@0: if (!caught) michael@0: throw new Error("Didn't catch the link failure error"); michael@0: michael@0: // Turn warnings-as-errors back off michael@0: options("werror"); michael@0: } michael@0: michael@0: // Linking should throw a warning-as-error but otherwise run fine michael@0: function asmLink(f) michael@0: { michael@0: if (!isAsmJSCompilationAvailable()) michael@0: return f.apply(null, Array.slice(arguments, 1)); michael@0: michael@0: // Turn on warnings-as-errors michael@0: var oldOpts = options("werror"); michael@0: assertEq(oldOpts.indexOf("werror"), -1); michael@0: michael@0: var ret = f.apply(null, Array.slice(arguments, 1)); michael@0: michael@0: // Turn warnings-as-errors back off michael@0: options("werror"); michael@0: michael@0: return ret; michael@0: }