tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

testSource-2.js (7485B)


      1 // |jit-test| skip-if: !Function.prototype.toSource
      2 
      3 (function() {
      4 /*
      5 * NO ARGUMENT
      6 */
      7 
      8 function f0() {
      9    "use asm";
     10    function g() {}
     11    return g;
     12 
     13 }
     14 
     15 var bodyOnly = '"use asm";\n\
     16    function g() {}\n\
     17    return g;\n';
     18 
     19 var funcBody =  'function f0() {\n\
     20    "use asm";\n\
     21    function g() {}\n\
     22    return g;\n\n\
     23 }';
     24 
     25 assertEq(f0.toSource(), funcBody);
     26 
     27 var f0 = function() {
     28    "use asm";
     29    function g() {}
     30    return g;
     31 
     32 }
     33 
     34 funcBody1 = funcBody.replace('function f0','function');
     35 assertEq(f0.toSource(), '(' + funcBody1 + ')');
     36 
     37 var g = function g0() {
     38    "use asm";
     39    function g() {}
     40    return g;
     41 
     42 }
     43 
     44 funcBody2 = funcBody.replace('function f0', 'function g0');
     45 assertEq(g.toSource(), '(' + funcBody2 + ')');
     46 
     47 f0 = new Function(bodyOnly);
     48 assertEq(f0.toSource(), "(function anonymous(\n) {\n" + bodyOnly + "\n})");
     49 
     50 if (isAsmJSCompilationAvailable()) {
     51    var m = new Function(bodyOnly);
     52    assertEq(isAsmJSModule(m), true);
     53    assertEq(m.toSource(), "(function anonymous(\n) {\n" + bodyOnly + "\n})");
     54 }
     55 
     56 })();
     57 
     58 (function() {
     59 /*
     60 * ONE ARGUMENT
     61 */
     62 function f1(glob) {
     63    "use asm";
     64    function g() {}
     65    return g;
     66 
     67 }
     68 
     69 var bodyOnly = '"use asm";\n\
     70    function g() {}\n\
     71    return g;\n';
     72 
     73 var funcBody =  'function f1(glob) {\n\
     74    "use asm";\n\
     75    function g() {}\n\
     76    return g;\n\n\
     77 }';
     78 
     79 assertEq(f1.toSource(), funcBody);
     80 
     81 f1 = function(glob) {
     82    "use asm";
     83    function g() {}
     84    return g;
     85 
     86 }
     87 
     88 funcBody1 = funcBody.replace('function f1', 'function');
     89 assertEq(f1.toSource(), '(' + funcBody1 + ')');
     90 
     91 var g = function g0(glob) {
     92    "use asm";
     93    function g() {}
     94    return g;
     95 
     96 }
     97 
     98 funcBody2 = funcBody.replace('function f1', 'function g0');
     99 assertEq(g.toSource(), '(' + funcBody2 + ')');
    100 
    101 f1 = new Function('glob', bodyOnly);
    102 assertEq(f1.toSource(), "(function anonymous(glob\n) {\n" + bodyOnly + "\n})");
    103 
    104 if (isAsmJSCompilationAvailable()) {
    105    var m = new Function('glob', bodyOnly);
    106    assertEq(isAsmJSModule(m), true);
    107    assertEq(m.toSource(), "(function anonymous(glob\n) {\n" + bodyOnly + "\n})");
    108 }
    109 
    110 })();
    111 
    112 
    113 (function() {
    114 /*
    115 * TWO ARGUMENTS
    116 */
    117 function f2(glob, ffi) {
    118    "use asm";
    119    function g() {}
    120    return g;
    121 
    122 }
    123 
    124 var bodyOnly = '"use asm";\n\
    125    function g() {}\n\
    126    return g;\n';
    127 
    128 var funcBody =  'function f2(glob, ffi) {\n\
    129    "use asm";\n\
    130    function g() {}\n\
    131    return g;\n\n\
    132 }';
    133 
    134 assertEq(f2.toSource(), funcBody);
    135 
    136 f2 = function(glob, ffi) {
    137    "use asm";
    138    function g() {}
    139    return g;
    140 
    141 }
    142 
    143 funcBody1 = funcBody.replace('function f2', 'function');
    144 assertEq(f2.toSource(), '(' + funcBody1 + ')');
    145 
    146 var g = function g0(glob, ffi) {
    147    "use asm";
    148    function g() {}
    149    return g;
    150 
    151 }
    152 
    153 var funcBody2 = funcBody.replace('function f2', 'function g0');
    154 assertEq(g.toSource(), '(' + funcBody2 + ')');
    155 
    156 f2 = new Function('glob', 'ffi', bodyOnly);
    157 assertEq(f2.toSource(), "(function anonymous(glob,ffi\n) {\n" + bodyOnly + "\n})");
    158 
    159 if (isAsmJSCompilationAvailable()) {
    160    var m = new Function('glob', 'ffi', bodyOnly);
    161    assertEq(isAsmJSModule(m), true);
    162    assertEq(m.toSource(), "(function anonymous(glob,ffi\n) {\n" + bodyOnly + "\n})");
    163 }
    164 
    165 })();
    166 
    167 
    168 (function() {
    169 /*
    170 * THREE ARGUMENTS
    171 */
    172 function f3(glob, ffi, heap) {
    173    "use asm";
    174    function g() {}
    175    return g;
    176 
    177 }
    178 
    179 var bodyOnly = '"use asm";\n\
    180    function g() {}\n\
    181    return g;\n';
    182 
    183 var funcBody =  'function f3(glob, ffi, heap) {\n\
    184    "use asm";\n\
    185    function g() {}\n\
    186    return g;\n\n\
    187 }';
    188 
    189 assertEq(f3.toSource(), funcBody);
    190 
    191 f3 = function(glob, ffi, heap) {
    192    "use asm";
    193    function g() {}
    194    return g;
    195 
    196 }
    197 
    198 funcBody1 = funcBody.replace('function f3', 'function');
    199 assertEq(f3.toSource(), '(' + funcBody1 + ')');
    200 
    201 var g = function g0(glob, ffi, heap) {
    202    "use asm";
    203    function g() {}
    204    return g;
    205 
    206 }
    207 
    208 funcBody2 = funcBody.replace('function f3', 'function g0');
    209 assertEq(g.toSource(), '(' + funcBody2 + ')');
    210 
    211 f3 = new Function('glob', 'ffi', 'heap', bodyOnly);
    212 assertEq(f3.toSource(), "(function anonymous(glob,ffi,heap\n) {\n" + bodyOnly + "\n})");
    213 
    214 if (isAsmJSCompilationAvailable()) {
    215    var m = new Function('glob', 'ffi', 'heap', bodyOnly);
    216    assertEq(isAsmJSModule(m), true);
    217    assertEq(m.toSource(), "(function anonymous(glob,ffi,heap\n) {\n" + bodyOnly + "\n})");
    218 }
    219 
    220 })();
    221 
    222 /* Modules in "use strict" context */
    223 (function() {
    224 
    225 var funcSource =
    226    `function(glob, ffi, heap) {
    227        "use asm";
    228        function g() {}
    229        return g;
    230    }`;
    231 
    232 var f4 = eval("\"use strict\";\n(" + funcSource + ")");
    233 
    234 var expectedToString = funcSource;
    235 var expectedToSource = '(' + expectedToString + ')';
    236 
    237 assertEq(f4.toSource(), expectedToSource);
    238 
    239 if (isAsmJSCompilationAvailable()) {
    240    var f5 = eval("\"use strict\";\n(" + funcSource + ")");
    241    assertEq(isAsmJSModule(f5), true);
    242    assertEq(f5.toSource(), expectedToSource);
    243 }
    244 })();
    245 
    246 /* Functions */
    247 (function() {
    248 
    249 var noSrc = "function noArgument() {\n\
    250    return 42;\n\
    251 }"
    252 var oneSrc = "function oneArgument(x) {\n\
    253    x = x | 0;\n\
    254    return x + 1 | 0;\n\
    255 }";
    256 var twoSrc = "function twoArguments(x, y) {\n\
    257    x = x | 0;\n\
    258    y = y | 0;\n\
    259    return x + y | 0;\n\
    260 }";
    261 var threeSrc = "function threeArguments(a, b, c) {\n\
    262    a = +a;\n\
    263    b = +b;\n\
    264    c = +c;\n\
    265    return +(+(a * b) + c);\n\
    266 }";
    267 
    268 var funcBody = '\n\
    269    "use asm";\n'
    270    + noSrc + '\n'
    271    + oneSrc + '\n'
    272    + twoSrc + '\n'
    273    + threeSrc + '\n'
    274    + 'return {\n\
    275    no: noArgument,\n\
    276    one: oneArgument,\n\
    277    two: twoArguments,\n\
    278    three: threeArguments\n\
    279    }';
    280 
    281 var g = new Function(funcBody);
    282 var moduleG = g();
    283 
    284 function checkFuncSrc(m) {
    285    assertEq(m.no.toSource(), noSrc);
    286 
    287    assertEq(m.one.toSource(), oneSrc);
    288 
    289    assertEq(m.two.toSource(), twoSrc);
    290 
    291    assertEq(m.three.toSource(), threeSrc);
    292 }
    293 checkFuncSrc(moduleG);
    294 
    295 if (isAsmJSCompilationAvailable()) {
    296    var g2 = new Function(funcBody);
    297    assertEq(isAsmJSModule(g2), true);
    298    m = g2();
    299    checkFuncSrc(m);
    300 
    301    var moduleDecl = 'function g3() {' + funcBody + '}';
    302    eval(moduleDecl);
    303    m = g3();
    304    assertEq(isAsmJSModule(g3), true);
    305    checkFuncSrc(m);
    306 
    307    eval('var x = 42;' + moduleDecl);
    308    m = g3();
    309    assertEq(isAsmJSModule(g3), true);
    310    checkFuncSrc(m);
    311 }
    312 
    313 })();
    314 
    315 /* Functions in "use strict" context */
    316 (function () {
    317 
    318 var funcCode = 'function g(x) {\n\
    319    x=x|0;\n\
    320    return x + 1 | 0;}';
    321 var moduleCode = 'function () {\n\
    322    "use asm";\n' + funcCode + '\n\
    323    return g;\n\
    324    }',
    325    useStrict = '"use strict";';
    326 
    327 var f5 = eval(useStrict + ";\n(" + moduleCode + "())");
    328 
    329 var expectedToString = funcCode;
    330 var expectedToSource = expectedToString
    331 
    332 assertEq(f5.toSource(), expectedToSource);
    333 
    334 if (isAsmJSCompilationAvailable()) {
    335    var mf5 = eval("\"use strict\";\n(" + moduleCode + ")");
    336    assertEq(isAsmJSModule(mf5), true);
    337    var f5 = mf5();
    338    assertEq(f5.toSource(), expectedToSource);
    339 }
    340 
    341 })();
    342 
    343 /* Functions in "use strict" context with dynamic linking failure */
    344 (function () {
    345 
    346 var funcCode = 'function g(x) {\n\
    347    x=x|0;\n\
    348    return x + 1 | 0;}';
    349 var moduleCode = 'function (glob) {\n\
    350    "use asm";\n\
    351    var fround = glob.Math.fround;\n\
    352    ' + funcCode + '\n\
    353    return g;\n\
    354    }',
    355    useStrict = '"use strict";';
    356 
    357 var f6 = eval(useStrict + ";\n(" + moduleCode + "({Math:{}}))");
    358 
    359 assertEq(f6.toSource(), funcCode);
    360 
    361 if (isAsmJSCompilationAvailable()) {
    362    var mf6 = eval("\"use strict\";\n(" + moduleCode + ")");
    363    assertEq(isAsmJSModule(mf6), true);
    364    var f6 = mf6({Math:{}});
    365    assertEq(f6.toSource(), funcCode);
    366 }
    367 
    368 })();