js/src/jit-test/lib/asm.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jit-test/lib/asm.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,179 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const ASM_OK_STRING = "successfully compiled asm.js code";
     1.9 +const ASM_TYPE_FAIL_STRING = "asm.js type error:";
    1.10 +const ASM_DIRECTIVE_FAIL_STRING = "\"use asm\" is only meaningful in the Directive Prologue of a function body";
    1.11 +
    1.12 +const USE_ASM = '"use asm";';
    1.13 +const HEAP_IMPORTS = "const i8=new glob.Int8Array(b);var u8=new glob.Uint8Array(b);"+
    1.14 +                     "const i16=new glob.Int16Array(b);var u16=new glob.Uint16Array(b);"+
    1.15 +                     "const i32=new glob.Int32Array(b);var u32=new glob.Uint32Array(b);"+
    1.16 +                     "const f32=new glob.Float32Array(b);var f64=new glob.Float64Array(b);";
    1.17 +const BUF_64KB = new ArrayBuffer(64 * 1024);
    1.18 +
    1.19 +function asmCompile()
    1.20 +{
    1.21 +    var f = Function.apply(null, arguments);
    1.22 +    assertEq(!isAsmJSCompilationAvailable() || isAsmJSModule(f), true);
    1.23 +    return f;
    1.24 +}
    1.25 +
    1.26 +function asmCompileCached()
    1.27 +{
    1.28 +    if (!isAsmJSCompilationAvailable())
    1.29 +        return Function.apply(null, arguments);
    1.30 +
    1.31 +    if (!isCachingEnabled()) {
    1.32 +        var f = Function.apply(null, arguments);
    1.33 +        assertEq(isAsmJSModule(f), true);
    1.34 +        return f;
    1.35 +    }
    1.36 +
    1.37 +    var quotedArgs = [];
    1.38 +    for (var i = 0; i < arguments.length; i++)
    1.39 +        quotedArgs.push("'" + arguments[i] + "'");
    1.40 +    var code = "setCachingEnabled(true); var f = new Function(" + quotedArgs.join(',') + ");assertEq(isAsmJSModule(f), true);";
    1.41 +    nestedShell("--js-cache", "--execute=" + code);
    1.42 +
    1.43 +    var f = Function.apply(null, arguments);
    1.44 +    assertEq(isAsmJSModuleLoadedFromCache(f), true);
    1.45 +    return f;
    1.46 +}
    1.47 +
    1.48 +function assertAsmDirectiveFail(str)
    1.49 +{
    1.50 +    if (!isAsmJSCompilationAvailable())
    1.51 +        return;
    1.52 +
    1.53 +    // Turn on warnings-as-errors
    1.54 +    var oldOpts = options("werror");
    1.55 +    assertEq(oldOpts.indexOf("werror"), -1);
    1.56 +
    1.57 +    // Verify an error is thrown
    1.58 +    var caught = false;
    1.59 +    try {
    1.60 +        eval(str);
    1.61 +    } catch (e) {
    1.62 +        if ((''+e).indexOf(ASM_DIRECTIVE_FAIL_STRING) == -1)
    1.63 +            throw new Error("Didn't catch the expected directive failure error; instead caught: " + e + "\nStack: " + new Error().stack);
    1.64 +        caught = true;
    1.65 +    }
    1.66 +    if (!caught)
    1.67 +        throw new Error("Didn't catch the directive failure error");
    1.68 +
    1.69 +    // Turn warnings-as-errors back off
    1.70 +    options("werror");
    1.71 +}
    1.72 +
    1.73 +function assertAsmTypeFail()
    1.74 +{
    1.75 +    if (!isAsmJSCompilationAvailable())
    1.76 +        return;
    1.77 +
    1.78 +    // Verify no error is thrown with warnings off
    1.79 +    Function.apply(null, arguments);
    1.80 +
    1.81 +    // Turn on warnings-as-errors
    1.82 +    var oldOpts = options("werror");
    1.83 +    assertEq(oldOpts.indexOf("werror"), -1);
    1.84 +
    1.85 +    // Verify an error is thrown
    1.86 +    var caught = false;
    1.87 +    try {
    1.88 +        Function.apply(null, arguments);
    1.89 +    } catch (e) {
    1.90 +        if ((''+e).indexOf(ASM_TYPE_FAIL_STRING) == -1)
    1.91 +            throw new Error("Didn't catch the expected type failure error; instead caught: " + e + "\nStack: " + new Error().stack);
    1.92 +        caught = true;
    1.93 +    }
    1.94 +    if (!caught)
    1.95 +        throw new Error("Didn't catch the type failure error");
    1.96 +
    1.97 +    // Turn warnings-as-errors back off
    1.98 +    options("werror");
    1.99 +}
   1.100 +
   1.101 +function assertAsmLinkFail(f)
   1.102 +{
   1.103 +    if (!isAsmJSCompilationAvailable())
   1.104 +        return;
   1.105 +
   1.106 +    assertEq(isAsmJSModule(f), true);
   1.107 +
   1.108 +    // Verify no error is thrown with warnings off
   1.109 +    var ret = f.apply(null, Array.slice(arguments, 1));
   1.110 +
   1.111 +    assertEq(isAsmJSFunction(ret), false);
   1.112 +    if (typeof ret === 'object')
   1.113 +        for (f of ret)
   1.114 +            assertEq(isAsmJSFunction(f), false);
   1.115 +
   1.116 +    // Turn on warnings-as-errors
   1.117 +    var oldOpts = options("werror");
   1.118 +    assertEq(oldOpts.indexOf("werror"), -1);
   1.119 +
   1.120 +    // Verify an error is thrown
   1.121 +    var caught = false;
   1.122 +    try {
   1.123 +        f.apply(null, Array.slice(arguments, 1));
   1.124 +    } catch (e) {
   1.125 +        // Arbitrary code an run in the GetProperty, so don't assert any
   1.126 +        // particular string
   1.127 +        caught = true;
   1.128 +    }
   1.129 +    if (!caught)
   1.130 +        throw new Error("Didn't catch the link failure error");
   1.131 +
   1.132 +    // Turn warnings-as-errors back off
   1.133 +    options("werror");
   1.134 +}
   1.135 +
   1.136 +// Linking should throw an exception even without warnings-as-errors
   1.137 +function assertAsmLinkAlwaysFail(f)
   1.138 +{
   1.139 +    var caught = false;
   1.140 +    try {
   1.141 +        f.apply(null, Array.slice(arguments, 1));
   1.142 +    } catch (e) {
   1.143 +        caught = true;
   1.144 +    }
   1.145 +    if (!caught)
   1.146 +        throw new Error("Didn't catch the link failure error");
   1.147 +
   1.148 +    // Turn on warnings-as-errors
   1.149 +    var oldOpts = options("werror");
   1.150 +    assertEq(oldOpts.indexOf("werror"), -1);
   1.151 +
   1.152 +    // Verify an error is thrown
   1.153 +    var caught = false;
   1.154 +    try {
   1.155 +        f.apply(null, Array.slice(arguments, 1));
   1.156 +    } catch (e) {
   1.157 +        caught = true;
   1.158 +    }
   1.159 +    if (!caught)
   1.160 +        throw new Error("Didn't catch the link failure error");
   1.161 +
   1.162 +    // Turn warnings-as-errors back off
   1.163 +    options("werror");
   1.164 +}
   1.165 +
   1.166 +// Linking should throw a warning-as-error but otherwise run fine
   1.167 +function asmLink(f)
   1.168 +{
   1.169 +    if (!isAsmJSCompilationAvailable())
   1.170 +        return f.apply(null, Array.slice(arguments, 1));
   1.171 +
   1.172 +    // Turn on warnings-as-errors
   1.173 +    var oldOpts = options("werror");
   1.174 +    assertEq(oldOpts.indexOf("werror"), -1);
   1.175 +
   1.176 +    var ret = f.apply(null, Array.slice(arguments, 1));
   1.177 +
   1.178 +    // Turn warnings-as-errors back off
   1.179 +    options("werror");
   1.180 +
   1.181 +    return ret;
   1.182 +}

mercurial