| 1 |
// |reftest| skip-if(!xulRuntime.shell) |
| 2 |
/* -*- Mode: js2; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 3 |
/* |
| 4 |
* Any copyright is dedicated to the Public Domain. |
| 5 |
* http://creativecommons.org/licenses/publicdomain/ |
| 6 |
* Contributor: |
| 7 |
* Vladimir Vukicevic <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
//----------------------------------------------------------------------------- |
| 11 |
var BUGNUMBER = 532774; |
| 12 |
var summary = 'js typed arrays (webgl arrays)'; |
| 13 |
var actual = ''; |
| 14 |
var expect = ''; |
| 15 |
|
| 16 |
//----------------------------------------------------------------------------- |
| 17 |
if (typeof gczeal !== 'undefined') |
| 18 |
gczeal(0) |
| 19 |
|
| 20 |
if (typeof numberToDouble !== 'function') { |
| 21 |
var numberToDouble = SpecialPowers.Cu.getJSTestingFunctions().numberToDouble; |
| 22 |
} |
| 23 |
|
| 24 |
test(); |
| 25 |
//----------------------------------------------------------------------------- |
| 26 |
|
| 27 |
function test() |
| 28 |
{ |
| 29 |
printBugNumber(BUGNUMBER); |
| 30 |
printStatus(summary); |
| 31 |
|
| 32 |
var TestPassCount = 0; |
| 33 |
var TestFailCount = 0; |
| 34 |
var TestTodoCount = 0; |
| 35 |
|
| 36 |
var TODO = 1; |
| 37 |
|
| 38 |
function check(fun, msg, todo) { |
| 39 |
var thrown = null; |
| 40 |
var success = false; |
| 41 |
try { |
| 42 |
success = fun(); |
| 43 |
} catch (x) { |
| 44 |
thrown = x; |
| 45 |
} |
| 46 |
|
| 47 |
if (thrown) |
| 48 |
success = false; |
| 49 |
|
| 50 |
if (todo) { |
| 51 |
TestTodoCount++; |
| 52 |
|
| 53 |
if (success) { |
| 54 |
var ex = new Error; |
| 55 |
print ("=== TODO but PASSED? ==="); |
| 56 |
print (ex.stack); |
| 57 |
print ("========================"); |
| 58 |
} |
| 59 |
|
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
if (success) { |
| 64 |
TestPassCount++; |
| 65 |
} else { |
| 66 |
TestFailCount++; |
| 67 |
|
| 68 |
var ex = new Error; |
| 69 |
print ("=== FAILED ==="); |
| 70 |
if (msg) |
| 71 |
print (msg); |
| 72 |
print (ex.stack); |
| 73 |
if (thrown) { |
| 74 |
print (" threw exception:"); |
| 75 |
print (thrown); |
| 76 |
} |
| 77 |
print ("=============="); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
function checkThrows(fun, type, todo) { |
| 82 |
var thrown = false; |
| 83 |
try { |
| 84 |
fun(); |
| 85 |
} catch (x) { |
| 86 |
thrown = x; |
| 87 |
} |
| 88 |
|
| 89 |
if (typeof(type) !== 'undefined') |
| 90 |
if (thrown) { |
| 91 |
check(() => thrown instanceof type, |
| 92 |
"expected " + type.name + " but saw " + thrown, |
| 93 |
todo); |
| 94 |
} else { |
| 95 |
check(() => thrown, "expected " + type.name + " but no exception thrown", todo); |
| 96 |
} |
| 97 |
else |
| 98 |
check(() => thrown, undefined, todo); |
| 99 |
} |
| 100 |
|
| 101 |
function checkThrowsTODO(fun, type) { |
| 102 |
checkThrows(fun, type, true); |
| 103 |
} |
| 104 |
|
| 105 |
function testBufferManagement() { |
| 106 |
// Single buffer |
| 107 |
var buffer = new ArrayBuffer(128); |
| 108 |
buffer = null; |
| 109 |
gc(); |
| 110 |
|
| 111 |
// Buffer with single view, kill the view first |
| 112 |
buffer = new ArrayBuffer(128); |
| 113 |
var v1 = new Uint8Array(buffer); |
| 114 |
gc(); |
| 115 |
v1 = null; |
| 116 |
gc(); |
| 117 |
buffer = null; |
| 118 |
gc(); |
| 119 |
|
| 120 |
// Buffer with single view, kill the buffer first |
| 121 |
buffer = new ArrayBuffer(128); |
| 122 |
v1 = new Uint8Array(buffer); |
| 123 |
gc(); |
| 124 |
buffer = null; |
| 125 |
gc(); |
| 126 |
v1 = null; |
| 127 |
gc(); |
| 128 |
|
| 129 |
// Buffer with multiple views, kill first view first |
| 130 |
buffer = new ArrayBuffer(128); |
| 131 |
v1 = new Uint8Array(buffer); |
| 132 |
v2 = new Uint8Array(buffer); |
| 133 |
gc(); |
| 134 |
v1 = null; |
| 135 |
gc(); |
| 136 |
v2 = null; |
| 137 |
gc(); |
| 138 |
|
| 139 |
// Buffer with multiple views, kill second view first |
| 140 |
buffer = new ArrayBuffer(128); |
| 141 |
v1 = new Uint8Array(buffer); |
| 142 |
v2 = new Uint8Array(buffer); |
| 143 |
gc(); |
| 144 |
v2 = null; |
| 145 |
gc(); |
| 146 |
v1 = null; |
| 147 |
gc(); |
| 148 |
|
| 149 |
// Buffer with multiple views, kill all possible subsets of views |
| 150 |
buffer = new ArrayBuffer(128); |
| 151 |
for (let order = 0; order < 16; order++) { |
| 152 |
var views = [ new Uint8Array(buffer), |
| 153 |
new Uint8Array(buffer), |
| 154 |
new Uint8Array(buffer), |
| 155 |
new Uint8Array(buffer) ]; |
| 156 |
gc(); |
| 157 |
|
| 158 |
// Kill views according to the bits set in 'order' |
| 159 |
for (let i = 0; i < 4; i++) { |
| 160 |
if (order & (1 << i)) |
| 161 |
views[i] = null; |
| 162 |
} |
| 163 |
|
| 164 |
gc(); |
| 165 |
|
| 166 |
views = null; |
| 167 |
gc(); |
| 168 |
} |
| 169 |
|
| 170 |
// Similar: multiple views, kill them one at a time in every possible order |
| 171 |
buffer = new ArrayBuffer(128); |
| 172 |
for (let order = 0; order < 4*3*2*1; order++) { |
| 173 |
var views = [ new Uint8Array(buffer), |
| 174 |
new Uint8Array(buffer), |
| 175 |
new Uint8Array(buffer), |
| 176 |
new Uint8Array(buffer) ]; |
| 177 |
gc(); |
| 178 |
|
| 179 |
var sequence = [ 0, 1, 2, 3 ]; |
| 180 |
let groupsize = 4*3*2*1; |
| 181 |
let o = order; |
| 182 |
for (let i = 4; i > 0; i--) { |
| 183 |
groupsize = groupsize / i; |
| 184 |
let which = Math.floor(o/groupsize); |
| 185 |
[ sequence[i-1], sequence[which] ] = [ sequence[which], sequence[i-1] ]; |
| 186 |
o = o % groupsize; |
| 187 |
} |
| 188 |
|
| 189 |
for (let i = 0; i < 4; i++) { |
| 190 |
views[i] = null; |
| 191 |
gc(); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
// Multiple buffers with multiple views |
| 196 |
var views = []; |
| 197 |
for (let numViews of [ 1, 2, 0, 3, 2, 1 ]) { |
| 198 |
buffer = new ArrayBuffer(128); |
| 199 |
for (let viewNum = 0; viewNum < numViews; viewNum++) { |
| 200 |
views.push(new Int8Array(buffer)); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if (typeof setMarkStackLimit === 'function') { |
| 205 |
setMarkStackLimit(200); |
| 206 |
} |
| 207 |
var forceOverflow = [ buffer ]; |
| 208 |
for (let i = 0; i < 1000; i++) { |
| 209 |
forceOverflow = [ forceOverflow ]; |
| 210 |
} |
| 211 |
gc(); |
| 212 |
buffer = null; |
| 213 |
views = null; |
| 214 |
gcslice(3); gcslice(3); gcslice(3); gcslice(3); gcslice(3); gcslice(3); gc(); |
| 215 |
} |
| 216 |
|
| 217 |
var buf, buf2; |
| 218 |
|
| 219 |
buf = new ArrayBuffer(100); |
| 220 |
check(() => buf); |
| 221 |
check(() => buf.byteLength == 100); |
| 222 |
|
| 223 |
buf.byteLength = 50; |
| 224 |
check(() => buf.byteLength == 100); |
| 225 |
|
| 226 |
var zerobuf = new ArrayBuffer(0); |
| 227 |
check(() => zerobuf); |
| 228 |
check(() => zerobuf.byteLength == 0); |
| 229 |
|
| 230 |
check(() => (new Int32Array(zerobuf)).length == 0); |
| 231 |
checkThrows(() => new Int32Array(zerobuf, 1)); |
| 232 |
|
| 233 |
var zerobuf2 = new ArrayBuffer(); |
| 234 |
check(() => zerobuf2.byteLength == 0); |
| 235 |
|
| 236 |
checkThrows(() => new ArrayBuffer(-100), RangeError); |
| 237 |
// this is using js_ValueToECMAUInt32, which is giving 0 for "abc" |
| 238 |
checkThrowsTODO(() => new ArrayBuffer("abc"), TypeError); |
| 239 |
|
| 240 |
var zeroarray = new Int32Array(0); |
| 241 |
check(() => zeroarray.length == 0); |
| 242 |
check(() => zeroarray.byteLength == 0); |
| 243 |
check(() => zeroarray.buffer); |
| 244 |
check(() => zeroarray.buffer.byteLength == 0); |
| 245 |
|
| 246 |
var zeroarray2 = new Int32Array(); |
| 247 |
check(() => zeroarray2.length == 0); |
| 248 |
check(() => zeroarray2.byteLength == 0); |
| 249 |
check(() => zeroarray2.buffer); |
| 250 |
check(() => zeroarray2.buffer.byteLength == 0); |
| 251 |
|
| 252 |
var a = new Int32Array(20); |
| 253 |
check(() => a); |
| 254 |
check(() => a.length == 20); |
| 255 |
check(() => a.byteLength == 80); |
| 256 |
check(() => a.byteOffset == 0); |
| 257 |
check(() => a.buffer); |
| 258 |
check(() => a.buffer.byteLength == 80); |
| 259 |
|
| 260 |
var b = new Uint8Array(a.buffer, 4, 4); |
| 261 |
check(() => b); |
| 262 |
check(() => b.length == 4); |
| 263 |
check(() => b.byteLength == 4); |
| 264 |
check(() => a.buffer == b.buffer); |
| 265 |
|
| 266 |
b[0] = 0xaa; |
| 267 |
b[1] = 0xbb; |
| 268 |
b[2] = 0xcc; |
| 269 |
b[3] = 0xdd; |
| 270 |
|
| 271 |
check(() => a[0] == 0); |
| 272 |
check(() => a[1] != 0); |
| 273 |
check(() => a[2] == 0); |
| 274 |
|
| 275 |
buf = new ArrayBuffer(4); |
| 276 |
check(() => (new Int8Array(buf)).length == 4); |
| 277 |
check(() => (new Uint8Array(buf)).length == 4); |
| 278 |
check(() => (new Int16Array(buf)).length == 2); |
| 279 |
check(() => (new Uint16Array(buf)).length == 2); |
| 280 |
check(() => (new Int32Array(buf)).length == 1); |
| 281 |
check(() => (new Uint32Array(buf)).length == 1); |
| 282 |
check(() => (new Float32Array(buf)).length == 1); |
| 283 |
checkThrows(() => (new Float64Array(buf))); |
| 284 |
buf2 = new ArrayBuffer(8); |
| 285 |
check(() => (new Float64Array(buf2)).length == 1); |
| 286 |
|
| 287 |
buf = new ArrayBuffer(5); |
| 288 |
check(() => buf); |
| 289 |
check(() => buf.byteLength == 5); |
| 290 |
|
| 291 |
check(() => new Int32Array(buf, 0, 1)); |
| 292 |
checkThrows(() => new Int32Array(buf, 0)); |
| 293 |
check(() => new Int8Array(buf, 0)); |
| 294 |
|
| 295 |
check(() => (new Int8Array(buf, 3)).byteLength == 2); |
| 296 |
checkThrows(() => new Int8Array(buf, 500)); |
| 297 |
checkThrows(() => new Int8Array(buf, 0, 50)); |
| 298 |
checkThrows(() => new Float32Array(buf, 500)); |
| 299 |
checkThrows(() => new Float32Array(buf, 0, 50)); |
| 300 |
|
| 301 |
var sl = a.subarray(5,10); |
| 302 |
check(() => sl.length == 5); |
| 303 |
check(() => sl.buffer == a.buffer); |
| 304 |
check(() => sl.byteLength == 20); |
| 305 |
check(() => sl.byteOffset == 20); |
| 306 |
|
| 307 |
check(() => a.subarray(5,5).length == 0); |
| 308 |
check(() => a.subarray(-5).length == 5); |
| 309 |
check(() => a.subarray(-100).length == 20); |
| 310 |
check(() => a.subarray(0, 2).length == 2); |
| 311 |
check(() => a.subarray().length == a.length); |
| 312 |
check(() => a.subarray(-7,-5).length == 2); |
| 313 |
check(() => a.subarray(-5,-7).length == 0); |
| 314 |
check(() => a.subarray(15).length == 5); |
| 315 |
|
| 316 |
a = new Uint8Array([0xaa, 0xbb, 0xcc]); |
| 317 |
check(() => a.length == 3); |
| 318 |
check(() => a.byteLength == 3); |
| 319 |
check(() => a[1] == 0xbb); |
| 320 |
|
| 321 |
// not sure if this is supposed to throw or to treat "foo"" as 0. |
| 322 |
checkThrowsTODO(() => new Int32Array([0xaa, "foo", 0xbb]), Error); |
| 323 |
|
| 324 |
checkThrows(() => new Int32Array(-100)); |
| 325 |
|
| 326 |
a = new Uint8Array(3); |
| 327 |
// XXX these are ignored now and return undefined |
| 328 |
//checkThrows(() => a[5000] = 0, RangeError); |
| 329 |
//checkThrows(() => a["hello"] = 0, TypeError); |
| 330 |
//checkThrows(() => a[-10] = 0, RangeError); |
| 331 |
check(() => (a[0] = "10") && (a[0] == 10)); |
| 332 |
|
| 333 |
// check Uint8ClampedArray, which is an extension to this extension |
| 334 |
a = new Uint8ClampedArray(4); |
| 335 |
a[0] = 128; |
| 336 |
a[1] = 512; |
| 337 |
a[2] = -123.723; |
| 338 |
a[3] = "foopy"; |
| 339 |
|
| 340 |
check(() => a[0] == 128); |
| 341 |
check(() => a[1] == 255); |
| 342 |
check(() => a[2] == 0); |
| 343 |
check(() => a[3] == 0); |
| 344 |
|
| 345 |
// check handling of holes and non-numeric values |
| 346 |
var x = Array(5); |
| 347 |
x[0] = "hello"; |
| 348 |
x[1] = { }; |
| 349 |
//x[2] is a hole |
| 350 |
x[3] = undefined; |
| 351 |
x[4] = true; |
| 352 |
|
| 353 |
a = new Uint8Array(x); |
| 354 |
check(() => a[0] == 0); |
| 355 |
check(() => a[1] == 0); |
| 356 |
check(() => a[2] == 0); |
| 357 |
check(() => a[3] == 0); |
| 358 |
check(() => a[4] == 1); |
| 359 |
|
| 360 |
a = new Float32Array(x); |
| 361 |
check(() => !(a[0] == a[0])); |
| 362 |
check(() => !(a[1] == a[1])); |
| 363 |
check(() => !(a[2] == a[2])); |
| 364 |
check(() => !(a[3] == a[3])); |
| 365 |
check(() => a[4] == 1); |
| 366 |
|
| 367 |
// test set() |
| 368 |
var empty = new Int32Array(0); |
| 369 |
a = new Int32Array(9); |
| 370 |
|
| 371 |
empty.set([]); |
| 372 |
empty.set([], 0); |
| 373 |
empty.set(empty); |
| 374 |
|
| 375 |
checkThrows(() => empty.set([1])); |
| 376 |
checkThrows(() => empty.set([1], 0)); |
| 377 |
checkThrows(() => empty.set([1], 1)); |
| 378 |
|
| 379 |
a.set([]); |
| 380 |
a.set([], 3); |
| 381 |
a.set([], 9); |
| 382 |
a.set(a); |
| 383 |
|
| 384 |
a.set(empty); |
| 385 |
a.set(empty, 3); |
| 386 |
a.set(empty, 9); |
| 387 |
a.set(Array.prototype); |
| 388 |
checkThrows(() => a.set(empty, 100)); |
| 389 |
|
| 390 |
checkThrows(() => a.set([1,2,3,4,5,6,7,8,9,10])); |
| 391 |
checkThrows(() => a.set([1,2,3,4,5,6,7,8,9,10], 0)); |
| 392 |
checkThrows(() => a.set([1,2,3,4,5,6,7,8,9,10], 0x7fffffff)); |
| 393 |
checkThrows(() => a.set([1,2,3,4,5,6,7,8,9,10], 0xffffffff)); |
| 394 |
checkThrows(() => a.set([1,2,3,4,5,6], 6)); |
| 395 |
|
| 396 |
checkThrows(() => a.set(new Array(0x7fffffff))); |
| 397 |
checkThrows(() => a.set([1,2,3], 2147483647)); |
| 398 |
|
| 399 |
a.set(ArrayBuffer.prototype); |
| 400 |
checkThrows(() => a.set(Int16Array.prototype), TypeError); |
| 401 |
checkThrows(() => a.set(Int32Array.prototype), TypeError); |
| 402 |
|
| 403 |
a.set([1,2,3]); |
| 404 |
a.set([4,5,6], 3); |
| 405 |
check(() => |
| 406 |
a[0] == 1 && a[1] == 2 && a[2] == 3 && |
| 407 |
a[3] == 4 && a[4] == 5 && a[5] == 6 && |
| 408 |
a[6] == 0 && a[7] == 0 && a[8] == 0); |
| 409 |
|
| 410 |
b = new Float32Array([7,8,9]); |
| 411 |
a.set(b, 0); |
| 412 |
a.set(b, 3); |
| 413 |
check(() => |
| 414 |
a[0] == 7 && a[1] == 8 && a[2] == 9 && |
| 415 |
a[3] == 7 && a[4] == 8 && a[5] == 9 && |
| 416 |
a[6] == 0 && a[7] == 0 && a[8] == 0); |
| 417 |
a.set(a.subarray(0,3), 6); |
| 418 |
check(() => |
| 419 |
a[0] == 7 && a[1] == 8 && a[2] == 9 && |
| 420 |
a[3] == 7 && a[4] == 8 && a[5] == 9 && |
| 421 |
a[6] == 7 && a[7] == 8 && a[8] == 9); |
| 422 |
|
| 423 |
a.set([1,2,3,4,5,6,7,8,9]); |
| 424 |
a.set(a.subarray(0,6), 3); |
| 425 |
check(() => |
| 426 |
a[0] == 1 && a[1] == 2 && a[2] == 3 && |
| 427 |
a[3] == 1 && a[4] == 2 && a[5] == 3 && |
| 428 |
a[6] == 4 && a[7] == 5 && a[8] == 6); |
| 429 |
|
| 430 |
a.set(a.subarray(3,9), 0); |
| 431 |
check(() => |
| 432 |
a[0] == 1 && a[1] == 2 && a[2] == 3 && |
| 433 |
a[3] == 4 && a[4] == 5 && a[5] == 6 && |
| 434 |
a[6] == 4 && a[7] == 5 && a[8] == 6); |
| 435 |
|
| 436 |
// verify that subarray() returns a new view that |
| 437 |
// references the same buffer |
| 438 |
a.subarray(0,3).set(a.subarray(3,6), 0); |
| 439 |
check(() => |
| 440 |
a[0] == 4 && a[1] == 5 && a[2] == 6 && |
| 441 |
a[3] == 4 && a[4] == 5 && a[5] == 6 && |
| 442 |
a[6] == 4 && a[7] == 5 && a[8] == 6); |
| 443 |
|
| 444 |
a = new ArrayBuffer(0x10); |
| 445 |
checkThrows(() => new Uint32Array(buffer, 4, 0x3FFFFFFF)); |
| 446 |
|
| 447 |
check(() => new Float32Array(null).length === 0); |
| 448 |
|
| 449 |
a = new Uint8Array(0x100); |
| 450 |
b = Uint32Array.prototype.subarray.apply(a, [0, 0x100]); |
| 451 |
check(() => Object.prototype.toString.call(b) === "[object Uint8Array]"); |
| 452 |
check(() => b.buffer === a.buffer); |
| 453 |
check(() => b.length === a.length); |
| 454 |
check(() => b.byteLength === a.byteLength); |
| 455 |
check(() => b.byteOffset === a.byteOffset); |
| 456 |
check(() => b.BYTES_PER_ELEMENT === a.BYTES_PER_ELEMENT); |
| 457 |
|
| 458 |
// webidl section 4.4.6, getter bullet point 2.2: prototypes are not |
| 459 |
// platform objects, and calling the getter of any attribute defined on the |
| 460 |
// interface should throw a TypeError according to |
| 461 |
checkThrows(() => ArrayBuffer.prototype.byteLength, TypeError); |
| 462 |
checkThrows(() => Int32Array.prototype.length, TypeError); |
| 463 |
checkThrows(() => Int32Array.prototype.byteLength, TypeError); |
| 464 |
checkThrows(() => Int32Array.prototype.byteOffset, TypeError); |
| 465 |
checkThrows(() => Float64Array.prototype.length, TypeError); |
| 466 |
checkThrows(() => Float64Array.prototype.byteLength, TypeError); |
| 467 |
checkThrows(() => Float64Array.prototype.byteOffset, TypeError); |
| 468 |
|
| 469 |
// webidl 4.4.6: a readonly attribute's setter is undefined. From |
| 470 |
// observation, that seems to mean it silently does nothing, and returns |
| 471 |
// the value that you tried to set it to. |
| 472 |
check(() => Int32Array.prototype.length = true); |
| 473 |
check(() => Float64Array.prototype.length = true); |
| 474 |
check(() => Int32Array.prototype.byteLength = true); |
| 475 |
check(() => Float64Array.prototype.byteLength = true); |
| 476 |
check(() => Int32Array.prototype.byteOffset = true); |
| 477 |
check(() => Float64Array.prototype.byteOffset = true); |
| 478 |
|
| 479 |
// ArrayBuffer, Int32Array and Float64Array are native functions and have a |
| 480 |
// .length, so none of these should throw: |
| 481 |
check(() => (new Int32Array(ArrayBuffer)).length >= 0); |
| 482 |
check(() => (new Int32Array(Int32Array)).length >= 0); |
| 483 |
check(() => (new Int32Array(Float64Array)).length >= 0); |
| 484 |
|
| 485 |
// webidl 4.4.6, under getters: "The value of the Function object’s |
| 486 |
// 'length' property is the Number value 0" |
| 487 |
// |
| 488 |
// Except this fails in getOwnPropertyDescriptor, I think because |
| 489 |
// Int32Array.prototype does not provide a lookup hook, and the fallback |
| 490 |
// case ends up calling the getter. Which seems odd to me, but much of this |
| 491 |
// stuff baffles me. It does seem strange that there's no way to do |
| 492 |
// getOwnPropertyDescriptor on any of these attributes. |
| 493 |
// |
| 494 |
//check(Object.getOwnPropertyDescriptor(Int32Array.prototype, 'byteOffset')['get'].length == 0); |
| 495 |
|
| 496 |
check(() => Int32Array.BYTES_PER_ELEMENT == 4); |
| 497 |
check(() => (new Int32Array(4)).BYTES_PER_ELEMENT == 4); |
| 498 |
check(() => (new Int32Array()).BYTES_PER_ELEMENT == 4); |
| 499 |
check(() => (new Int32Array(0)).BYTES_PER_ELEMENT == 4); |
| 500 |
check(() => Int16Array.BYTES_PER_ELEMENT == Uint16Array.BYTES_PER_ELEMENT); |
| 501 |
|
| 502 |
// test various types of args; numberToDouble(2) is used to ensure that the |
| 503 |
// function gets a double, and not a demoted int |
| 504 |
check(() => (new Float32Array(numberToDouble(2))).length == 2); |
| 505 |
check(() => (new Float32Array({ length: 10 })).length == 10); |
| 506 |
check(() => (new Float32Array({})).length == 0); |
| 507 |
check(() => new Float32Array("3").length === 3); |
| 508 |
check(() => new Float32Array(null).length === 0); |
| 509 |
check(() => new Float32Array(undefined).length === 0); |
| 510 |
|
| 511 |
// check that NaN conversions happen correctly with array conversions |
| 512 |
check(() => (new Int32Array([NaN])[0]) == 0); |
| 513 |
check(() => { var q = new Float32Array([NaN])[0]; return q != q; }); |
| 514 |
|
| 515 |
// check that setting and reading arbitrary properties works |
| 516 |
// this is not something that will be done in real world |
| 517 |
// situations, but it should work when done just like in |
| 518 |
// regular objects |
| 519 |
buf = new ArrayBuffer(128); |
| 520 |
a = new Uint32Array(buf, 0, 4); |
| 521 |
check(() => a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0); |
| 522 |
buf.a = 42; |
| 523 |
buf.b = "abcdefgh"; |
| 524 |
buf.c = {a:'literal'}; |
| 525 |
check(() => a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0); |
| 526 |
|
| 527 |
check(() => buf.a == 42); |
| 528 |
delete buf.a; |
| 529 |
check(() => !buf.a); |
| 530 |
|
| 531 |
// check edge cases for small arrays |
| 532 |
// 16 reserved slots |
| 533 |
a = new Uint8Array(120); |
| 534 |
check(() => a.byteLength == 120); |
| 535 |
check(() => a.length == 120); |
| 536 |
for (var i = 0; i < a.length; i++) |
| 537 |
check(() => a[i] == 0) |
| 538 |
|
| 539 |
a = new Uint8Array(121); |
| 540 |
check(() => a.byteLength == 121); |
| 541 |
check(() => a.length == 121); |
| 542 |
for (var i = 0; i < a.length; i++) |
| 543 |
check(() => a[i] == 0) |
| 544 |
|
| 545 |
// check that TM generated byte offset is right (requires run with -j) |
| 546 |
a = new Uint8Array(100); |
| 547 |
a[99] = 5; |
| 548 |
b = new Uint8Array(a.buffer, 9); // force a offset |
| 549 |
// use a loop to invoke the TM |
| 550 |
for (var i = 0; i < b.length; i++) |
| 551 |
check(() => b[90] == 5) |
| 552 |
|
| 553 |
// Protos and proxies, oh my! |
| 554 |
var alien = newGlobal({newCompartment: true}); |
| 555 |
|
| 556 |
var alien_view = alien.eval('view = new Uint8Array(7)'); |
| 557 |
var alien_buffer = alien.eval('buffer = view.buffer'); |
| 558 |
|
| 559 |
// when creating a view of a buffer in a different compartment, the view |
| 560 |
// itself should be created in the other compartment and wrapped for use in |
| 561 |
// this compartment. (There should never be a compartment boundary between |
| 562 |
// an ArrayBufferView and its ArrayBuffer.) |
| 563 |
var view = new Int8Array(alien_buffer); |
| 564 |
|
| 565 |
// First make sure they're looking at the same data |
| 566 |
alien_view[3] = 77; |
| 567 |
check(() => view[3] == 77); |
| 568 |
|
| 569 |
// Now check that the proxy setup is as expected in the cross-compartment |
| 570 |
// case. |
| 571 |
if (isProxy(alien)) { |
| 572 |
check(() => isProxy(alien_view)); |
| 573 |
check(() => isProxy(alien_buffer)); |
| 574 |
check(() => isProxy(view)); // the real test |
| 575 |
} |
| 576 |
|
| 577 |
// cross-realm property access |
| 578 |
check(() => alien_buffer.byteLength == 7); |
| 579 |
check(() => alien_view.byteLength == 7); |
| 580 |
check(() => view.byteLength == 7); |
| 581 |
|
| 582 |
// typed array protos should be equal |
| 583 |
simple = new Int8Array(12); |
| 584 |
check(() => Object.getPrototypeOf(view) == Object.getPrototypeOf(simple)); |
| 585 |
check(() => Object.getPrototypeOf(view) == Int8Array.prototype); |
| 586 |
|
| 587 |
// Most named properties are defined on %TypedArray%.prototype. |
| 588 |
check(() => !simple.hasOwnProperty('byteLength')); |
| 589 |
check(() => !Int8Array.prototype.hasOwnProperty('byteLength')); |
| 590 |
check(() => Object.getPrototypeOf(Int8Array.prototype).hasOwnProperty('byteLength')); |
| 591 |
|
| 592 |
check(() => !simple.hasOwnProperty("BYTES_PER_ELEMENT")); |
| 593 |
check(() => Int8Array.prototype.hasOwnProperty("BYTES_PER_ELEMENT")); |
| 594 |
check(() => !Object.getPrototypeOf(Int8Array.prototype).hasOwnProperty("BYTES_PER_ELEMENT")); |
| 595 |
|
| 596 |
// crazy as it sounds, the named properties are configurable per WebIDL. |
| 597 |
// But we are currently discussing the situation, and typed arrays may be |
| 598 |
// pulled into the ES spec, so for now this is disallowed. |
| 599 |
if (false) { |
| 600 |
check(() => simple.byteLength == 12); |
| 601 |
getter = Object.getOwnPropertyDescriptor(Int8Array.prototype, 'byteLength').get; |
| 602 |
Object.defineProperty(Int8Array.prototype, 'byteLength', { get: function () { return 1 + getter.apply(this) } }); |
| 603 |
check(() => simple.byteLength == 13); |
| 604 |
} |
| 605 |
|
| 606 |
// test copyWithin() |
| 607 |
var numbers = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]; |
| 608 |
|
| 609 |
function tastring(tarray) { |
| 610 |
return [...tarray].toString(); |
| 611 |
} |
| 612 |
|
| 613 |
function checkCopyWithin(offset, start, end, dest, want) { |
| 614 |
var numbers_buffer = new Uint8Array(numbers).buffer; |
| 615 |
var view = new Int8Array(numbers_buffer, offset); |
| 616 |
view.copyWithin(dest, start, end); |
| 617 |
check(() => tastring(view) == want.toString()); |
| 618 |
if (tastring(view) != want.toString()) { |
| 619 |
print("Wanted: " + want.toString()); |
| 620 |
print("Got : " + tastring(view)); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
// basic copyWithin [2,5) -> 4 |
| 625 |
checkCopyWithin(0, 2, 5, 4, [ 0, 1, 2, 3, 2, 3, 4, 7, 8 ]); |
| 626 |
|
| 627 |
// negative values should count from end |
| 628 |
checkCopyWithin(0, -7, 5, 4, [ 0, 1, 2, 3, 2, 3, 4, 7, 8 ]); |
| 629 |
checkCopyWithin(0, 2, -4, 4, [ 0, 1, 2, 3, 2, 3, 4, 7, 8 ]); |
| 630 |
checkCopyWithin(0, 2, 5, -5, [ 0, 1, 2, 3, 2, 3, 4, 7, 8 ]); |
| 631 |
checkCopyWithin(0, -7, -4, -5, [ 0, 1, 2, 3, 2, 3, 4, 7, 8 ]); |
| 632 |
|
| 633 |
// offset |
| 634 |
checkCopyWithin(2, 0, 3, 4, [ 2, 3, 4, 5, 2, 3, 4 ]); |
| 635 |
|
| 636 |
// clipping |
| 637 |
checkCopyWithin(0, 5000, 6000, 0, [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]); |
| 638 |
checkCopyWithin(0, -5000, -6000, 0, [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]); |
| 639 |
checkCopyWithin(0, -5000, 6000, 0, [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]); |
| 640 |
checkCopyWithin(0, 5000, 6000, 1, [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]); |
| 641 |
checkCopyWithin(0, -5000, -6000, 1, [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]); |
| 642 |
checkCopyWithin(0, 5000, 6000, 0, [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]); |
| 643 |
checkCopyWithin(2, -5000, -6000, 0, [ 2, 3, 4, 5, 6, 7, 8 ]); |
| 644 |
checkCopyWithin(2, -5000, 6000, 0, [ 2, 3, 4, 5, 6, 7, 8 ]); |
| 645 |
checkCopyWithin(2, 5000, 6000, 1, [ 2, 3, 4, 5, 6, 7, 8 ]); |
| 646 |
checkCopyWithin(2, -5000, -6000, 1, [ 2, 3, 4, 5, 6, 7, 8 ]); |
| 647 |
|
| 648 |
checkCopyWithin(2, -5000, 3, 1, [ 2, 2, 3, 4, 6, 7, 8 ]); |
| 649 |
checkCopyWithin(2, 1, 6000, 0, [ 3, 4, 5, 6, 7, 8, 8 ]); |
| 650 |
checkCopyWithin(2, 1, 6000, -4000, [ 3, 4, 5, 6, 7, 8, 8 ]); |
| 651 |
|
| 652 |
testBufferManagement(); |
| 653 |
|
| 654 |
print ("done"); |
| 655 |
|
| 656 |
reportCompare(0, TestFailCount, "typed array tests"); |
| 657 |
} |