js/src/jit-test/tests/asm.js/testSource.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial