| 1 |
load(libdir + "asm.js"); |
| 2 |
load(libdir + "asserts.js"); |
| 3 |
|
| 4 |
assertAsmTypeFail(USE_ASM); |
| 5 |
assertAsmTypeFail(USE_ASM + 'return'); |
| 6 |
assertAsmTypeFail(USE_ASM + 'function f(){}'); |
| 7 |
assertAsmTypeFail(USE_ASM + 'function f(){} return 0'); |
| 8 |
assertAsmTypeFail(USE_ASM + 'function f(){} return g'); |
| 9 |
assertAsmTypeFail(USE_ASM + 'function f(){} function f(){} return f'); |
| 10 |
assertAsmTypeFail(USE_ASM + 'function f(){}; function g(){}; return {f, g}'); |
| 11 |
assertAsmTypeFail(USE_ASM + 'var f = f;'); |
| 12 |
assertAsmTypeFail(USE_ASM + 'var f=0; function f(){} return f'); |
| 13 |
assertAsmTypeFail(USE_ASM + 'var f=glob.Math.imul; return {}'); |
| 14 |
assertAsmTypeFail('glob', USE_ASM + 'var f=glob.Math.imul; function f(){} return f'); |
| 15 |
assertAsmTypeFail('glob','foreign', USE_ASM + 'var f=foreign.foo; function f(){} return f'); |
| 16 |
assertAsmTypeFail(USE_ASM + 'function f(){} var f=[f,f]; return f'); |
| 17 |
assertAsmTypeFail('"use strict";' + USE_ASM + 'function f() {} return f'); |
| 18 |
assertAsmTypeFail(USE_ASM + '"use strict"; function f() {} return f'); |
| 19 |
assertAsmTypeFail(USE_ASM + '"use foopy" + 1; function f() {} return f'); |
| 20 |
assertAsmTypeFail(USE_ASM + 'function f() { "use strict"; } return f'); |
| 21 |
assertEq(asmLink(asmCompile(USE_ASM + '"use asm"; function f() {} return f'))(), undefined); |
| 22 |
assertEq(asmLink(asmCompile(USE_ASM + '"use foopy"; function f() {} return f'))(), undefined); |
| 23 |
assertEq(asmLink(asmCompile(USE_ASM + '"use foopy"; "use blarg"; function f() {} return f'))(), undefined); |
| 24 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f() { "use asm"; } return f'))(), undefined); |
| 25 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f() { "use foopy"; } return f'))(), undefined); |
| 26 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f() { "use foopy"; "use blarg"; } return f'))(), undefined); |
| 27 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f(i) { "use foopy"; i=i|0; } return f'))(), undefined); |
| 28 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f(i) { "use foopy"; "use asm"; i=i|0; } return f'))(), undefined); |
| 29 |
assertEq(asmLink(asmCompile(USE_ASM + '"use warm"; function f(i) { "use cold"; i=i|0 } function g() { "use hot"; return 0 } return f'))(), undefined); |
| 30 |
assertEq(asmLink(asmCompile(USE_ASM + '"use warm"; function f(i) { "use cold"; i=i|0 } function g() { "use asm"; return 0 } return f'))(), undefined); |
| 31 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f(){} return f'))(), undefined); |
| 32 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f(){;} return f'))(), undefined); |
| 33 |
assertAsmTypeFail(USE_ASM + 'function f(i,j){;} return f'); |
| 34 |
assertEq(asmLink(asmCompile('"use asm";; function f(){};;; return f;;'))(), undefined); |
| 35 |
assertAsmTypeFail(USE_ASM + 'function f(x){} return f'); |
| 36 |
assertAsmTypeFail(USE_ASM + 'function f(){if (0) return; return 1} return f'); |
| 37 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f(x){x=x|0} return f'))(42), undefined); |
| 38 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f(x){x=x|0; return x|0} return f'))(42), 42); |
| 39 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f(x){x=x|0; return x|0;;;} return f'))(42), 42); |
| 40 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f(x,y){x=x|0;y=y|0; return (x+y)|0} return f'))(44, -2), 42); |
| 41 |
assertAsmTypeFail('a', USE_ASM + 'function a(){} return a'); |
| 42 |
assertAsmTypeFail('a','b','c', USE_ASM + 'var c'); |
| 43 |
assertAsmTypeFail('a','b','c', USE_ASM + 'var c=0'); |
| 44 |
assertAsmTypeFail(USE_ASM + 'c=0;return {}'); |
| 45 |
assertAsmTypeFail('x','x', USE_ASM + 'function a(){} return a'); |
| 46 |
assertAsmTypeFail('x','y','x', USE_ASM + 'function a(){} return a'); |
| 47 |
assertEq(asmLink(asmCompile('x', USE_ASM + 'function a(){} return a'))(), undefined); |
| 48 |
assertEq(asmLink(asmCompile('x','y', USE_ASM + 'function a(){} return a'))(), undefined); |
| 49 |
assertEq(asmLink(asmCompile('x','y','z', USE_ASM + 'function a(){} return a'))(), undefined); |
| 50 |
assertAsmTypeFail('x','y', USE_ASM + 'function y(){} return f'); |
| 51 |
assertEq(asmLink(asmCompile('x', USE_ASM + 'function f(){} return f'), 1, 2, 3)(), undefined); |
| 52 |
assertEq(asmLink(asmCompile('x', USE_ASM + 'function f(){} return f'), 1)(), undefined); |
| 53 |
assertEq(asmLink(asmCompile('x','y', USE_ASM + 'function f(){} return f'), 1, 2)(), undefined); |
| 54 |
|
| 55 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f(i) {i=i|0} return f'))(42), undefined); |
| 56 |
assertEq(asmLink(asmCompile(USE_ASM + 'function f() {var i=0;; var j=1;} return f'))(), undefined); // bug 877965 second part |
| 57 |
assertAsmTypeFail(USE_ASM + 'function f(i) {i=i|0;var i} return f'); |
| 58 |
assertAsmTypeFail(USE_ASM + 'function f(i) {i=i|0;var i=0} return f'); |
| 59 |
|
| 60 |
assertAsmTypeFail('glob', USE_ASM + 'var im=glob.imul; function f() {} return f'); |
| 61 |
assertAsmLinkAlwaysFail(asmCompile('glob', USE_ASM + 'var im=glob.Math.imul; function f(i,j) {i=i|0;j=j|0; return im(i,j)|0} return f'), null); |
| 62 |
assertAsmLinkAlwaysFail(asmCompile('glob', USE_ASM + 'var im=glob.Math.imul; function f(i,j) {i=i|0;j=j|0; return im(i,j)|0} return f'), {}); |
| 63 |
assertAsmLinkAlwaysFail(asmCompile('glob', USE_ASM + 'var im=glob.Math.imul; function f(i,j) {i=i|0;j=j|0; return im(i,j)|0} return f'), {imul:Math.imul}); |
| 64 |
assertEq(asmLink(asmCompile('glob', USE_ASM + 'var im=glob.Math.imul; function f(i,j) {i=i|0;j=j|0; return im(i,j)|0} return f'), {Math:{imul:Math.imul}})(2,3), 6); |
| 65 |
assertEq(asmLink(asmCompile('glob', USE_ASM + 'var im=glob.Math.imul; function f(i,j) {i=i|0;j=j|0; return im(i,j)|0} return f'), this)(8,4), 32); |
| 66 |
|
| 67 |
var module = asmCompile('glob','i','b', USE_ASM + 'var i32=new glob.Int32Array(b); function f(){} return f'); |
| 68 |
assertAsmLinkAlwaysFail(module, null, null); |
| 69 |
assertAsmLinkFail(module, this, null, null); |
| 70 |
assertAsmLinkFail(module, this, null, null); |
| 71 |
assertAsmLinkAlwaysFail(module, this, null, new ArrayBuffer(1)); |
| 72 |
assertAsmLinkFail(module, this, null, new ArrayBuffer(4)); |
| 73 |
assertAsmLinkFail(module, this, null, new ArrayBuffer(100)); |
| 74 |
assertAsmLinkFail(module, this, null, new ArrayBuffer(4092)); |
| 75 |
assertAsmLinkFail(module, this, null, new ArrayBuffer(64000)); |
| 76 |
assertAsmLinkFail(module, this, null, new ArrayBuffer(BUF_MIN+4)); |
| 77 |
assertAsmLinkDeprecated(module, this, null, new ArrayBuffer(4096)); |
| 78 |
assertAsmLinkDeprecated(module, this, null, new ArrayBuffer(2*4096)); |
| 79 |
assertAsmLinkDeprecated(module, this, null, new ArrayBuffer(4*4096)); |
| 80 |
assertAsmLinkDeprecated(module, this, null, new ArrayBuffer(32*1024)); |
| 81 |
assertEq(asmLink(module, this, null, new ArrayBuffer(BUF_MIN))(), undefined); |
| 82 |
|
| 83 |
assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i = i32[i]|0; return i|0}; return f'); |
| 84 |
assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i = i32[i>>1]|0; return i|0}; return f'); |
| 85 |
assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i = i32[i>>1]|0; return i|0}; return f'); |
| 86 |
assertAsmLinkAlwaysFail(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i = i32[i>>2]|0; return i|0}; return f'), this, null, new ArrayBuffer(BUF_MIN-1)); |
| 87 |
assertEq(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i = i32[i>>2]|0; return i|0}; return f')(this, null, new ArrayBuffer(BUF_MIN))(), 0); |
| 88 |
|
| 89 |
var exp = asmLink(asmCompile(USE_ASM + "return {}")); |
| 90 |
assertEq(Object.keys(exp).length, 0); |
| 91 |
|
| 92 |
var exp = asmLink(asmCompile(USE_ASM + "function f() { return 3 } return {f:f,f:f}")); |
| 93 |
assertEq(exp.f(), 3); |
| 94 |
assertEq(Object.keys(exp).join(), 'f'); |
| 95 |
|
| 96 |
var exp = asmLink(asmCompile(USE_ASM + "function f() { return 4 } return {f1:f,f2:f}")); |
| 97 |
assertEq(exp.f1(), 4); |
| 98 |
assertEq(exp.f2(), 4); |
| 99 |
assertEq(Object.keys(exp).sort().join(), 'f1,f2'); |
| 100 |
assertEq(exp.f1, exp.f2); |
| 101 |
|
| 102 |
assertAsmTypeFail(USE_ASM + "function f() { return 3 } return {1:f}"); |
| 103 |
assertAsmTypeFail(USE_ASM + "function f() { return 3 } return {__proto__:f}"); |
| 104 |
assertAsmTypeFail(USE_ASM + "function f() { return 3 } return {get x() {} }"); |
| 105 |
|
| 106 |
var exp = asmLink(asmCompile(USE_ASM + 'function internal() { return ((g()|0)+2)|0 } function f() { return 1 } function g() { return 2 } function h() { return internal()|0 } return {f:f,g1:g,h1:h}')); |
| 107 |
assertEq(exp.f(), 1); |
| 108 |
assertEq(exp.g1(), 2); |
| 109 |
assertEq(exp.h1(), 4); |
| 110 |
assertEq(Object.keys(exp).join(), 'f,g1,h1'); |
| 111 |
|
| 112 |
// can't test destructuring args with Function constructor |
| 113 |
function assertTypeFailInEval(str) |
| 114 |
{ |
| 115 |
if (!isAsmJSCompilationAvailable()) |
| 116 |
return; |
| 117 |
|
| 118 |
assertWarning(() => { |
| 119 |
eval(str); |
| 120 |
}, /asm.js type error:/) |
| 121 |
} |
| 122 |
assertTypeFailInEval('function f({}) { "use asm"; function g() {} return g }'); |
| 123 |
assertTypeFailInEval('function f({global}) { "use asm"; function g() {} return g }'); |
| 124 |
assertTypeFailInEval('function f(global, {imports}) { "use asm"; function g() {} return g }'); |
| 125 |
assertTypeFailInEval('function f(g = 2) { "use asm"; function g() {} return g }'); |
| 126 |
assertTypeFailInEval('function *f() { "use asm"; function g() {} return g }'); |
| 127 |
assertAsmTypeFail(USE_ASM + 'function *f(){}'); |
| 128 |
assertTypeFailInEval('f => { "use asm"; function g() {} return g }'); |
| 129 |
assertTypeFailInEval('var f = { method() {"use asm"; return {}} }'); |
| 130 |
assertAsmTypeFail(USE_ASM + 'return {m() {}};'); |
| 131 |
assertTypeFailInEval('var f = { get p() {"use asm"; return {}} }'); |
| 132 |
assertAsmTypeFail(USE_ASM + 'return {get p() {}};'); |
| 133 |
assertTypeFailInEval('var f = { set p(x) {"use asm"; return {}} }'); |
| 134 |
assertAsmTypeFail(USE_ASM + 'return {set p(x) {}};'); |
| 135 |
assertTypeFailInEval('class f { constructor() {"use asm"; return {}} }'); |
| 136 |
assertAsmTypeFail(USE_ASM + 'class c { constructor() {}}; return c;'); |
| 137 |
|
| 138 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'var)') }, SyntaxError); |
| 139 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'return)') }, SyntaxError); |
| 140 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'var z=-2w') }, SyntaxError); |
| 141 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'var z=-2w;') }, SyntaxError); |
| 142 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'function') }, SyntaxError); |
| 143 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'function f') }, SyntaxError); |
| 144 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'function f(') }, SyntaxError); |
| 145 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'function f()') }, SyntaxError); |
| 146 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'function f() {') }, SyntaxError); |
| 147 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'function f() {} var') }, SyntaxError); |
| 148 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'function f() {} var TBL=-2w; return f') }, SyntaxError); |
| 149 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'function f() {} var TBL=-2w return f') }, SyntaxError); |
| 150 |
assertThrowsInstanceOf(function() { new Function(USE_ASM + 'function () {}') }, SyntaxError); |
| 151 |
assertNoWarning(function() { parse("function f() { 'use asm'; function g() {} return g }") }); |
| 152 |
|
| 153 |
// Test asm.js->asm.js, wasm->asm.js, asm.js->wasm imports: |
| 154 |
|
| 155 |
var f = asmLink(asmCompile(USE_ASM + 'function f() {} return f')); |
| 156 |
var g = asmLink(asmCompile('glob', 'ffis', USE_ASM + 'var f = ffis.f; function g() { return f(1)|0; } return g'), null, {f}); |
| 157 |
assertEq(g(), 0); |
| 158 |
|
| 159 |
if (wasmIsSupported()) { |
| 160 |
var h = new WebAssembly.Instance(new WebAssembly.Module(wasmTextToBinary(`(module |
| 161 |
(import "imp" "f" (func $f (param i32) (result i32))) |
| 162 |
(func $h (result i32) (call $f (i32.const 1))) |
| 163 |
(export "h" (func $h)) |
| 164 |
)`)), {imp:{f}}).exports.h; |
| 165 |
assertEq(h(), 0); |
| 166 |
|
| 167 |
var i = new WebAssembly.Instance(new WebAssembly.Module(wasmTextToBinary(`(module (func $i) (export "i" (func $i)))`))).exports.i |
| 168 |
var j = asmLink(asmCompile('glob', 'ffis', USE_ASM + 'var i = ffis.i; function j() { return i(1)|0; } return j'), null, {i}); |
| 169 |
assertEq(j(), 0); |
| 170 |
} |
| 171 |
|
| 172 |
var exp = asmLink(asmCompile(USE_ASM + "function f() { return 0 } return {f:f}")); |
| 173 |
assertEq(Object.isFrozen(exp), false); |