testHeapAccess.js (30778B)
1 load(libdir + "asm.js"); 2 3 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i32[0>>2] = 4.0; return i32[0>>2]|0; } return f'); 4 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f32[0>>2] = 4; return +f32[0>>2]; } return f'); 5 6 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { var x=0,y=0; return i8[x+y]|0 } return f'); 7 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { var x=0,y=0; return u8[x+y]|0 } return f'); 8 9 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return i32[0>>0]|0 }; return f'); 10 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return i32[0>>1]|0 }; return f'); 11 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return i32[0>>4]|0 }; return f'); 12 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return i32[0]|0 }; return f'), this, null, new ArrayBuffer(BUF_MIN))(), 0); 13 14 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i = i32[i>>2]|0; return i|0}; return f'); 15 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 16 assertEq(f(0), 0); 17 18 // In order to allow following tests work on both big-endian and little- 19 // endian architectures we need to define least significant byte (lsb) and 20 // least significant word (lsw). 21 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i32[0] = i; return i8[0]|0}; return f'); 22 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 23 var lsb=0; 24 var lsw=0 25 if (f(0x12345678) == 0x12) { 26 lsb=3; 27 lsw=1; 28 } 29 30 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i32[0] = i; return i8[' + lsb + ']|0}; return f'); 31 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 32 assertEq(f(0),0); 33 assertEq(f(0x7f),0x7f); 34 assertEq(f(0xff),-1); 35 assertEq(f(0x100),0); 36 37 { 38 var buf = new ArrayBuffer(BUF_MIN); 39 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + '/* not a clone */ function f(i) {i=i|0; i32[0] = i; return i8[' + lsb + ']|0}; return f'); 40 var f = asmLink(code, this, null, buf); 41 assertEq(f(0),0); 42 assertEq(f(0x7f),0x7f); 43 assertEq(f(0xff),-1); 44 assertEq(f(0x100),0); 45 46 // Bug 1088655 47 assertEq(asmLink(asmCompile('stdlib', 'foreign', 'heap', USE_ASM + 'var i32=new stdlib.Int32Array(heap); function f(i) {i=i|0;var j=0x10000;return (i32[j>>2] = i)|0 } return f'), this, null, buf)(1), 1); 48 } 49 50 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i32[0] = i; return u8[' + lsb + ']|0}; return f'); 51 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 52 assertEq(f(0),0); 53 assertEq(f(0x7f),0x7f); 54 assertEq(f(0xff),0xff); 55 assertEq(f(0x100),0); 56 57 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i32[0] = i; return i16[' + lsw + ']|0}; return f'); 58 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 59 assertEq(f(0),0); 60 assertEq(f(0x7fff),0x7fff); 61 assertEq(f(0xffff),-1); 62 assertEq(f(0x10000),0); 63 64 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i32[0] = i; return u16[' + lsw + ']|0}; return f'); 65 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 66 assertEq(f(0),0); 67 assertEq(f(0x7fff),0x7fff); 68 assertEq(f(0xffff),0xffff); 69 assertEq(f(0x10000),0); 70 71 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i32[0] = i; return i32[0]|0}; return f'); 72 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 73 assertEq(f(0),0); 74 assertEq(f(0x7fffffff),0x7fffffff); 75 assertEq(f(0xffffffff),-1); 76 assertEq(f(0x100000000),0); 77 78 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i32[0] = i; return u32[0]|0}; return f'); 79 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 80 assertEq(f(0),0); 81 assertEq(f(0x7fffffff),0x7fffffff); 82 assertEq(f(0xffffffff),-1); 83 assertEq(f(0x100000000),0); 84 85 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i32[0] = i; return i8[' + lsb + ']|0}; return f'); 86 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 87 assertEq(f(0),0); 88 assertEq(f(0x7f),0x7f); 89 assertEq(f(0xff),-1); 90 assertEq(f(0x100),0); 91 92 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i32[0] = i; return u8[' + lsb + ']|0}; return f'); 93 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 94 assertEq(f(0),0); 95 assertEq(f(0x7f),0x7f); 96 assertEq(f(0xff),0xff); 97 assertEq(f(0x100),0); 98 99 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i8[0] = i; return i8[0]|0}; return f'); 100 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 101 assertEq(f(0),0); 102 assertEq(f(0x7f),0x7f); 103 assertEq(f(0xff),-1); 104 assertEq(f(0x100),0); 105 106 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; i8[0] = i; return u8[0]|0}; return f'); 107 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 108 assertEq(f(0),0); 109 assertEq(f(0x7f),0x7f); 110 assertEq(f(0xff),0xff); 111 assertEq(f(0x100),0); 112 113 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i,j) {i=i|0;j=+j; f64[i>>3] = j; return (~~+f64[i>>3])|0}; return f'); 114 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 115 assertEq(f(0, 1.3), 1); 116 assertEq(f(BUF_MIN-8, 2.5), 2); 117 assertEq(f(BUF_MIN, 3.8), 0); 118 119 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i,j) {i=i|0;j=+j; f64[i>>3] = j; return (~~f64[i>>3])|0}; return f'); 120 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 121 assertEq(f(0, 1.3), 1); 122 assertEq(f(BUF_MIN-8, 2.5), 2); 123 assertEq(f(BUF_MIN, 3.8), 0); 124 125 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i,j) {i=i|0;j=+j; f64[i>>3] = j; return +f64[i>>3]}; return f'); 126 var f = asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 127 assertEq(f(0, 1.3), 1.3); 128 assertEq(f(BUF_MIN-8, 2.5), 2.5); 129 assertEq(f(BUF_MIN, 3.8), NaN); 130 131 var i32 = new Int32Array(BUF_MIN); 132 i32[0] = 42; 133 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i32[1] = i32[0] }; return f'), this, null, i32.buffer)(); 134 assertEq(i32[1], 42); 135 136 var f64 = new Float64Array(BUF_MIN); 137 f64[0] = 42; 138 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f64[1] = f64[0] }; return f'), this, null, f64.buffer)(); 139 assertEq(f64[1], 42); 140 141 var i32 = new Int32Array(BUF_MIN/4); 142 i32[0] = 13; 143 i32[1] = 0xfffeeee; 144 var f = asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; return i32[((i<<2)+1)>>2]|0 }; return f'), this, null, i32.buffer); 145 assertEq(f(0), 13); 146 assertEq(f(1), 0xfffeeee); 147 var f = asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; return i32[((i<<2)+2)>>2]|0 }; return f'), this, null, i32.buffer); 148 assertEq(f(0), 13); 149 assertEq(f(1), 0xfffeeee); 150 var f = asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; return i32[(i<<1)>>2]|0 }; return f'), this, null, i32.buffer); 151 assertEq(f(0), 13); 152 assertEq(f(1), 13); 153 assertEq(f(2), 0xfffeeee); 154 assertEq(f(3), 0xfffeeee); 155 156 var f = asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; return i32[(i<<2)>>2]|0 }; return f'), this, null, i32.buffer); 157 assertEq(f(0), 13); 158 assertEq(f(1), 0xfffeeee); 159 160 var f = asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) {i=i|0; return i32[((i<<2)+4)>>2]|0 }; return f'), this, null, i32.buffer); 161 assertEq(f(0), 0xfffeeee); 162 163 // For legacy compatibility, test Int8/Uint8 accesses with no shift. 164 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u8[7&0xffff] = 41 } return f'), this, null, BUF_64KB)(); 165 assertEq(new Uint8Array(BUF_64KB)[7], 41); 166 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i8[7&0xffff] = -41 } return f'), this, null, BUF_64KB)(); 167 assertEq(new Int8Array(BUF_64KB)[7], -41); 168 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u8[(7&0xffff)>>0] = 41 } return f'), this, null, BUF_64KB)(); 169 assertEq(new Uint8Array(BUF_64KB)[7], 41); 170 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i8[(7&0xffff)>>0] = -41 } return f'), this, null, BUF_64KB)(); 171 assertEq(new Int8Array(BUF_64KB)[7], -41); 172 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u16[(6&0xffff)>>1] = 0xabc } return f'), this, null, BUF_64KB)(); 173 assertEq(new Uint16Array(BUF_64KB)[3], 0xabc); 174 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i16[(6&0xffff)>>1] = -0xabc } return f'), this, null, BUF_64KB)(); 175 assertEq(new Int16Array(BUF_64KB)[3], -0xabc); 176 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u32[(4&0xffff)>>2] = 0xabcde } return f'), this, null, BUF_64KB)(); 177 assertEq(new Uint32Array(BUF_64KB)[1], 0xabcde); 178 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i32[(4&0xffff)>>2] = -0xabcde } return f'), this, null, BUF_64KB)(); 179 assertEq(new Int32Array(BUF_64KB)[1], -0xabcde); 180 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f32[(4&0xffff)>>2] = 1.0 } return f'), this, null, BUF_64KB)(); 181 assertEq(new Float32Array(BUF_64KB)[1], 1.0); 182 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f64[(8&0xffff)>>3] = 1.3 } return f'), this, null, BUF_64KB)(); 183 assertEq(new Float64Array(BUF_64KB)[1], 1.3); 184 185 new Float32Array(BUF_64KB)[1] = 1.0; 186 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f32[(4&0xffff)>>2] } return f'), this, null, BUF_64KB)(), 1.0); 187 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'var toFloat32 = glob.Math.fround; function f() { return toFloat32(f32[(4&0xffff)>>2]) } return f'), this, null, BUF_64KB)(), 1.0); 188 new Float64Array(BUF_64KB)[1] = 1.3; 189 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f64[(8&0xffff)>>3] } return f'), this, null, BUF_64KB)(), 1.3); 190 191 asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) { i=i|0; u8[255]; u8[i] } return f'); 192 // For legacy compatibility, test Int8/Uint8 accesses with no shift. 193 asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) { i=i|0; u8[i&0xff]; u8[255] } return f'); 194 asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) { i=i|0; u8[(i&0xff)>>0]; u8[255] } return f'); 195 asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) { i=i|0; u32[63]; u32[i>>2] } return f'); 196 asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) { i=i|0; u32[i>>2]; u32[63] } return f'); 197 198 var code = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) { i=i|0; u32[64] } return f'); 199 asmLink(code, this, null, new ArrayBuffer(BUF_MIN)); 200 201 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) { i=i|0; u32[12] = i } return f'), this, null, BUF_64KB)(11); 202 assertEq(new Int32Array(BUF_64KB)[12], 11); 203 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return u32[12]|0 } return f'), this, null, BUF_64KB)(), 11); 204 new Float64Array(BUF_64KB)[0] = 3.5; 205 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +-f64[0] } return f'), this, null, BUF_64KB)(), -3.5); 206 207 // Test constant loads. 208 var buf = new ArrayBuffer(BUF_MIN); 209 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u8[1] = -1 } return f'), this, null, buf)(); 210 assertEq(new Uint8Array(buf)[0], 0); 211 assertEq(new Uint8Array(buf)[1], 255); 212 assertEq(new Uint8Array(buf)[2], 0); 213 var buf = new ArrayBuffer(BUF_MIN); 214 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u8[4095] = -1 } return f'), this, null, buf)(); 215 assertEq(new Uint8Array(buf)[4094], 0); 216 assertEq(new Uint8Array(buf)[4095], 255); 217 assertEq(new Uint8Array(buf)[4096], 0); 218 var buf = new ArrayBuffer(BUF_MIN); 219 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u8[4096] = -1 } return f'), this, null, buf)(); 220 assertEq(new Uint8Array(buf)[4095], 0); 221 assertEq(new Uint8Array(buf)[4096], 255); 222 assertEq(new Uint8Array(buf)[4097], 0); 223 assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u8[' + BUF_MIN + '] = -1 } return f'), this, null, buf); 224 var buf = new ArrayBuffer(262144); 225 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u8[258048] = -1 } return f'), this, null, buf)(); 226 assertEq(new Uint8Array(buf)[258047], 0); 227 assertEq(new Uint8Array(buf)[258048], 255); 228 assertEq(new Uint8Array(buf)[258049], 0); 229 230 var buf = new ArrayBuffer(BUF_MIN); 231 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i8[1] = -1 } return f'), this, null, buf)(); 232 assertEq(new Int8Array(buf)[0], 0); 233 assertEq(new Int8Array(buf)[1], -1); 234 assertEq(new Int8Array(buf)[2], 0); 235 var buf = new ArrayBuffer(BUF_MIN); 236 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i8[4095] = -1 } return f'), this, null, buf)(); 237 assertEq(new Int8Array(buf)[4094], 0); 238 assertEq(new Int8Array(buf)[4095], -1); 239 assertEq(new Int8Array(buf)[4096], 0); 240 var buf = new ArrayBuffer(BUF_MIN); 241 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i8[4096] = -1 } return f'), this, null, buf)(); 242 assertEq(new Int8Array(buf)[4095], 0); 243 assertEq(new Int8Array(buf)[4096], -1); 244 assertEq(new Int8Array(buf)[4097], 0); 245 assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i8[' + BUF_MIN + '] = -1 } return f'), this, null, buf); 246 var buf = new ArrayBuffer(262144); 247 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i8[258048] = -1 } return f'), this, null, buf)(); 248 assertEq(new Int8Array(buf)[258047], 0); 249 assertEq(new Int8Array(buf)[258048], -1); 250 assertEq(new Int8Array(buf)[258049], 0); 251 252 var buf = new ArrayBuffer(BUF_MIN); 253 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u16[1] = -1 } return f'), this, null, buf)(); 254 assertEq(new Uint16Array(buf)[0], 0); 255 assertEq(new Uint16Array(buf)[1], 65535); 256 assertEq(new Uint16Array(buf)[2], 0); 257 var buf = new ArrayBuffer(BUF_MIN); 258 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u16[' + (BUF_MIN/2-1) + '] = -1 } return f'), this, null, buf)(); 259 assertEq(new Uint16Array(buf)[BUF_MIN/2-2], 0); 260 assertEq(new Uint16Array(buf)[BUF_MIN/2-1], 65535); 261 assertEq(new Uint16Array(buf)[BUF_MIN/2], undefined); 262 var buf = new ArrayBuffer(BUF_MIN); 263 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u16[' + (BUF_MIN/4) + '] = -1 } return f'), this, null, buf)(); 264 assertEq(new Uint16Array(buf)[BUF_MIN/4-1], 0); 265 assertEq(new Uint16Array(buf)[BUF_MIN/4], 65535); 266 assertEq(new Uint16Array(buf)[BUF_MIN/4+1], 0); 267 assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u16[' + (BUF_MIN/2) + '] = -1 } return f'), this, null, buf); 268 var buf = new ArrayBuffer(262144); 269 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u16[126976] = -1 } return f'), this, null, buf)(); 270 assertEq(new Uint16Array(buf)[126975], 0); 271 assertEq(new Uint16Array(buf)[126976], 65535); 272 assertEq(new Uint16Array(buf)[126977], 0); 273 274 var buf = new ArrayBuffer(BUF_MIN); 275 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i16[1] = -1 } return f'), this, null, buf)(); 276 assertEq(new Int16Array(buf)[0], 0); 277 assertEq(new Int16Array(buf)[1], -1); 278 assertEq(new Int16Array(buf)[2], 0); 279 var buf = new ArrayBuffer(BUF_MIN); 280 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i16[' + (BUF_MIN/2-1) + '] = -1 } return f'), this, null, buf)(); 281 assertEq(new Int16Array(buf)[BUF_MIN/2-2], 0); 282 assertEq(new Int16Array(buf)[BUF_MIN/2-1], -1); 283 assertEq(new Int16Array(buf)[BUF_MIN/2], undefined); 284 var buf = new ArrayBuffer(BUF_MIN); 285 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i16[' + (BUF_MIN/4) + '] = -1 } return f'), this, null, buf)(); 286 assertEq(new Int16Array(buf)[BUF_MIN/4-1], 0); 287 assertEq(new Int16Array(buf)[BUF_MIN/4], -1); 288 assertEq(new Int16Array(buf)[BUF_MIN/4+1], 0); 289 assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i16[' + (BUF_MIN/2) + '] = -1 } return f'), this, null, buf); 290 var buf = new ArrayBuffer(262144); 291 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i16[126976] = -1 } return f'), this, null, buf)(); 292 assertEq(new Int16Array(buf)[126975], 0); 293 assertEq(new Int16Array(buf)[126976], -1); 294 assertEq(new Int16Array(buf)[126977], 0); 295 296 var buf = new ArrayBuffer(BUF_MIN); 297 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u32[1] = -1 } return f'), this, null, buf)(); 298 assertEq(new Uint32Array(buf)[0], 0); 299 assertEq(new Uint32Array(buf)[1], 4294967295); 300 assertEq(new Uint32Array(buf)[2], 0); 301 var buf = new ArrayBuffer(BUF_MIN); 302 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u32[' + (BUF_MIN/4-1) + '] = -1 } return f'), this, null, buf)(); 303 assertEq(new Uint32Array(buf)[BUF_MIN/4-2], 0); 304 assertEq(new Uint32Array(buf)[BUF_MIN/4-1], 4294967295); 305 assertEq(new Uint32Array(buf)[BUF_MIN/4], undefined); 306 var buf = new ArrayBuffer(BUF_MIN); 307 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u32[' + (BUF_MIN/8) + '] = -1 } return f'), this, null, buf)(); 308 assertEq(new Uint32Array(buf)[BUF_MIN/8-1], 0); 309 assertEq(new Uint32Array(buf)[BUF_MIN/8], 4294967295); 310 assertEq(new Uint32Array(buf)[BUF_MIN/8+1], 0); 311 assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u32[' + (BUF_MIN/4) + '] = -1 } return f'), this, null, buf); 312 var buf = new ArrayBuffer(262144); 313 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { u32[61440] = -1 } return f'), this, null, buf)(); 314 assertEq(new Uint32Array(buf)[61439], 0); 315 assertEq(new Uint32Array(buf)[61440], 4294967295); 316 assertEq(new Uint32Array(buf)[61441], 0); 317 318 var buf = new ArrayBuffer(BUF_MIN); 319 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i32[1] = -1 } return f'), this, null, buf)(); 320 assertEq(new Int32Array(buf)[0], 0); 321 assertEq(new Int32Array(buf)[1], -1); 322 assertEq(new Int32Array(buf)[2], 0); 323 var buf = new ArrayBuffer(BUF_MIN); 324 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i32[' + (BUF_MIN/4-1) + '] = -1 } return f'), this, null, buf)(); 325 assertEq(new Int32Array(buf)[BUF_MIN/4-2], 0); 326 assertEq(new Int32Array(buf)[BUF_MIN/4-1], -1); 327 assertEq(new Int32Array(buf)[BUF_MIN/4], undefined); 328 var buf = new ArrayBuffer(BUF_MIN); 329 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i32[' + (BUF_MIN/8) + '] = -1 } return f'), this, null, buf)(); 330 assertEq(new Int32Array(buf)[BUF_MIN/8-1], 0); 331 assertEq(new Int32Array(buf)[BUF_MIN/8], -1); 332 assertEq(new Int32Array(buf)[BUF_MIN/8+1], 0); 333 assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i32[' + (BUF_MIN/4) + '] = -1 } return f'), this, null, buf); 334 var buf = new ArrayBuffer(262144); 335 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { i32[61440] = -1 } return f'), this, null, buf)(); 336 assertEq(new Int32Array(buf)[61439], 0); 337 assertEq(new Int32Array(buf)[61440], -1); 338 assertEq(new Int32Array(buf)[61441], 0); 339 340 var buf = new ArrayBuffer(BUF_MIN); 341 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f32[1] = -1.0 } return f'), this, null, buf)(); 342 assertEq(new Int32Array(buf)[0], 0); 343 assertEq(new Float32Array(buf)[1], -1.0); 344 assertEq(new Int32Array(buf)[2], 0); 345 var buf = new ArrayBuffer(BUF_MIN); 346 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f32[' + (BUF_MIN/4-1) + '] = -1.0 } return f'), this, null, buf)(); 347 assertEq(new Int32Array(buf)[BUF_MIN/4-2], 0); 348 assertEq(new Float32Array(buf)[BUF_MIN/4-1], -1.0); 349 assertEq(new Int32Array(buf)[BUF_MIN/4], undefined); 350 var buf = new ArrayBuffer(BUF_MIN); 351 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f32[' + (BUF_MIN/8) + '] = -1.0 } return f'), this, null, buf)(); 352 assertEq(new Int32Array(buf)[BUF_MIN/8-1], 0); 353 assertEq(new Float32Array(buf)[BUF_MIN/8], -1.0); 354 assertEq(new Int32Array(buf)[BUF_MIN/8+1], 0); 355 assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f32[' + (BUF_MIN/4) + '] = -1.0 } return f'), this, null, buf); 356 var buf = new ArrayBuffer(262144); 357 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f32[61440] = -1.0 } return f'), this, null, buf)(); 358 assertEq(new Float32Array(buf)[61439], 0.0); 359 assertEq(new Float32Array(buf)[61440], -1.0); 360 assertEq(new Float32Array(buf)[61441], 0.0); 361 362 var buf = new ArrayBuffer(BUF_MIN); 363 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f64[1] = -1.0 } return f'), this, null, buf)(); 364 assertEq(new Float64Array(buf)[0], 0.0); 365 assertEq(new Float64Array(buf)[1], -1.0); 366 assertEq(new Float64Array(buf)[2], 0.0); 367 var buf = new ArrayBuffer(BUF_MIN); 368 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f64[' + (BUF_MIN/8-1) + '] = -1.0 } return f'), this, null, buf)(); 369 assertEq(new Float64Array(buf)[BUF_MIN/8-2], 0.0); 370 assertEq(new Float64Array(buf)[BUF_MIN/8-1], -1.0); 371 assertEq(new Float64Array(buf)[BUF_MIN/8], undefined); 372 var buf = new ArrayBuffer(BUF_MIN); 373 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f64[' + (BUF_MIN/16) + '] = -1.0 } return f'), this, null, buf)(); 374 assertEq(new Float64Array(buf)[BUF_MIN/16-1], 0.0); 375 assertEq(new Float64Array(buf)[BUF_MIN/16], -1.0); 376 assertEq(new Float64Array(buf)[BUF_MIN/16+1], 0.0); 377 assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f64[' + (BUF_MIN/8) + '] = -1.0 } return f'), this, null, buf); 378 var buf = new ArrayBuffer(262144); 379 asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { f64[28672] = -1.0 } return f'), this, null, buf)(); 380 assertEq(new Float64Array(buf)[28671], 0.0); 381 assertEq(new Float64Array(buf)[28672], -1.0); 382 assertEq(new Float64Array(buf)[28673], 0.0); 383 384 385 var buf = new ArrayBuffer(BUF_MIN); 386 new Uint8Array(buf)[1] = -1; 387 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return u8[1]|0; } return f'), this, null, buf)(),255); 388 new Int8Array(buf)[1] = -1; 389 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return i8[1]|0; } return f'), this, null, buf)(),-1); 390 var buf = new ArrayBuffer(262144); 391 new Uint8Array(buf)[126976] = -1; 392 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return u8[126976]|0; } return f'), this, null, buf)(),255); 393 new Int8Array(buf)[126976] = -1; 394 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return i8[126976]|0; } return f'), this, null, buf)(),-1); 395 396 var buf = new ArrayBuffer(BUF_MIN); 397 new Uint16Array(buf)[1] = -1; 398 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return u16[1]|0; } return f'), this, null, buf)(),65535); 399 new Int16Array(buf)[1] = -1; 400 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return i16[1]|0; } return f'), this, null, buf)(),-1); 401 var buf = new ArrayBuffer(262144); 402 new Uint16Array(buf)[126976] = -1; 403 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return u16[126976]|0; } return f'), this, null, buf)(),65535); 404 new Int16Array(buf)[126976] = -1; 405 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return i16[126976]|0; } return f'), this, null, buf)(),-1); 406 407 var buf = new ArrayBuffer(BUF_MIN); 408 new Uint32Array(buf)[1] = -1; 409 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return u32[1]|0; } return f'), this, null, buf)(),-1); 410 new Int32Array(buf)[1] = -1; 411 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return i32[1]|0; } return f'), this, null, buf)(),-1); 412 var buf = new ArrayBuffer(262144); 413 new Int32Array(buf)[61440] = -1; 414 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return i32[61440]|0; } return f'), this, null, buf)(),-1); 415 416 var buf = new ArrayBuffer(BUF_MIN); 417 new Float32Array(buf)[1] = -1.0; 418 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f32[1]; } return f'), this, null, buf)(),-1.0); 419 new Float32Array(buf)[BUF_MIN/4-1] = -1.0; 420 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f32[' + (BUF_MIN/4-1) + ']; } return f'), this, null, buf)(),-1.0); 421 new Float32Array(buf)[BUF_MIN/8] = -1.0; 422 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f32[' + (BUF_MIN/8) + ']; } return f'), this, null, buf)(),-1.0); 423 assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f32[' + (BUF_MIN/4) + ']; } return f'), this, null, buf); 424 var buf = new ArrayBuffer(262144); 425 new Float32Array(buf)[61440] = -1.0; 426 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f32[61440]; } return f'), this, null, buf)(),-1.0); 427 428 var buf = new ArrayBuffer(BUF_MIN); 429 new Float64Array(buf)[1] = -1.0; 430 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f64[1]; } return f'), this, null, buf)(),-1.0); 431 new Float64Array(buf)[BUF_MIN/8-1] = -1.0; 432 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f64[' + (BUF_MIN/8-1) + ']; } return f'), this, null, buf)(),-1.0); 433 new Float64Array(buf)[BUF_MIN/16] = -1.0; 434 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f64[' + (BUF_MIN/16) + ']; } return f'), this, null, buf)(),-1.0); 435 assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f64[' + (BUF_MIN/8) + ']; } return f'), this, null, buf); 436 var buf = new ArrayBuffer(262144); 437 new Float64Array(buf)[28672] = -1.0; 438 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f64[28672]; } return f'), this, null, buf)(),-1.0); 439 440 // Bug 913867 441 var buf = new ArrayBuffer(BUF_MIN); 442 new Int32Array(buf)[0] = 0x55aa5a5a; 443 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f1() { i32[0] = 1; return 8; }; function f() { return i32[((f1()|0)&0)>>2]|0; } return f'), this, null, buf)(),1); 444 assertEq(new Int32Array(buf)[0], 1); 445 446 // Bug 882012 447 assertEq(asmLink(asmCompile('stdlib', 'foreign', 'heap', USE_ASM + "var id=foreign.id;var doubles=new stdlib.Float64Array(heap);function g(){doubles[0]=+id(2.0);return +doubles[0];}return g"), this, {id: function(x){return x;}}, BUF_64KB)(), 2.0); 448 449 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return u8[4294967295]|0; } return f'); 450 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'const i=4294967295; function f() { return u8[i]|0; } return f'); 451 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return u8[-1]|0; } return f'); 452 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'const i=-1; function f() { return u8[i]|0; } return f'); 453 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return u8[0x80000000]|0; } return f'); 454 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'const i=0x80000000; function f() { return u8[i]|0; } return f'); 455 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return u8[-2147483648]|0; } return f'); 456 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'const i=-2147483648; function f() { return u8[i]|0; } return f'); 457 458 // GVN checks 459 var buf = new ArrayBuffer(BUF_MIN); 460 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'var i=0; function f() { var x = 0, y = 0; u8[0] = 1; u8[1] = 2; x = 0|u8[i]; i = x; y = 0|u8[i]; return y|0;} return f'), this, null, buf)(),2); 461 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'var i=0; function f() { var x = 0, y = 0; u8[0] = 1; u8[1] = 2; x = 0|u8[i]; y = 0|u8[i]; return (x+y)|0;} return f'), this, null, buf)(),2); 462 463 // Heap length constraints 464 var m = asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f(i) { i=i|0; return u8[i]|0; } return f'); 465 assertAsmLinkAlwaysFail(m, this, null, new ArrayBuffer(0xffff)); 466 assertEq(asmLink(m, this, null, new ArrayBuffer(0x10000))(0),0); 467 assertAsmLinkFail(m, this, null, new ArrayBuffer(0x10010)); 468 assertEq(asmLink(m, this, null, new ArrayBuffer(0x20000))(0),0); 469 assertAsmLinkFail(m, this, null, new ArrayBuffer(0xfe0000)); 470 assertAsmLinkFail(m, this, null, new ArrayBuffer(0xfe0010)); 471 assertAsmLinkFail(m, this, null, new ArrayBuffer(0xfe0000)); 472 assertAsmLinkFail(m, this, null, new ArrayBuffer(0xff8000)); 473 var buf = new ArrayBuffer(0x100000); 474 new Uint8Array(buf)[0x4242] = 0xAA; 475 var f = asmLink(m, this, null, buf); 476 assertEq(f(0),0); 477 assertEq(f(0x4242),0xAA); 478 assertEq(f(0x100000),0); 479 assertAsmLinkFail(m, this, null, new ArrayBuffer(0x104000)); 480 assertEq(asmLink(m, this, null, new ArrayBuffer(0x200000))(0),0); 481 assertAsmLinkFail(m, this, null, new ArrayBuffer(0x3f8000)); 482 assertAsmLinkFail(m, this, null, new ArrayBuffer(0x3fe000)); 483 assertAsmLinkFail(m, this, null, new ArrayBuffer(0x3fc000)); 484 assertEq(asmLink(m, this, null, new ArrayBuffer(0x400000))(0),0); 485 assertAsmLinkFail(m, this, null, new ArrayBuffer(0x410000)); 486 assertEq(asmLink(m, this, null, new ArrayBuffer(0x800000))(0),0); 487 var buf = new ArrayBuffer(0x1000000); 488 new Uint8Array(buf)[0x424242] = 0xAA; 489 var f = asmLink(m, this, null, buf); 490 assertEq(f(0),0); 491 assertEq(f(0x424242),0xAA); 492 assertEq(f(0x1000000),0); 493 assertEq(asmLink(m, this, null, new ArrayBuffer(0x2000000))(0),0); 494 assertEq(asmLink(m, this, null, new ArrayBuffer(0x3000000))(0),0); 495 496 // Heap offsets 497 var asmMod = function test (glob, env, b) { 498 'use asm'; 499 var i8 = new glob.Int8Array(b); 500 function f(i) { 501 i = i | 0; 502 i = i & 1; 503 i = (i - 0x40000)|0; 504 i8[0x3ffff] = 0; 505 return i8[(i + 0x7fffe) >> 0] | 0; 506 } 507 return f; 508 }; 509 var buffer = new ArrayBuffer(0x40000); 510 var asm = asmMod(this, {}, buffer); 511 assertEq(asm(-1),0);