Debian Codegraph @

js/src/tests/non262/extensions/clone-typed-array.js

raw
1 // |reftest| skip-if(!xulRuntime.shell)
2 // Any copyright is dedicated to the Public Domain.
3 // http://creativecommons.org/licenses/publicdomain/
4
5 function assertArraysEqual(a, b) {
6 assertEq(a.constructor, b.constructor);
7 assertEq(a.length, b.length);
8 for (var i = 0; i < a.length; i++)
9 assertEq(a[i], b[i]);
10 }
11
12 function check(b) {
13 var a = deserialize(serialize(b));
14 assertArraysEqual(a, b);
15 }
16
17 function checkPrototype(ctor) {
18 var threw = false;
19 try {
20 serialize(ctor.prototype);
21 throw new Error("serializing " + ctor.name + ".prototype should throw a TypeError");
22 } catch (exc) {
23 if (!(exc instanceof TypeError))
24 throw exc;
25 }
26 }
27
28 function test() {
29 // Test cloning ArrayBuffer objects.
30 check(new ArrayBuffer(0));
31 check(new ArrayBuffer(7));
32 checkPrototype(ArrayBuffer);
33
34 // Test cloning typed array objects.
35 var ctors = [
36 Int8Array,
37 Uint8Array,
38 Int16Array,
39 Uint16Array,
40 Int32Array,
41 Uint32Array,
42 Float32Array,
43 Float64Array,
44 Uint8ClampedArray];
45
46 var b;
47 for (var i = 0; i < ctors.length; i++) {
48 var ctor = ctors[i];
49
50 // check empty array
51 b = new ctor(0);
52 check(b);
53
54 // check array with some elements
55 b = new ctor(100);
56 var v = 1;
57 for (var j = 0; j < 100; j++) {
58 b[j] = v;
59 v *= 7;
60 }
61 b[99] = NaN; // check serializing NaNs too
62 check(b);
63
64 // try the prototype
65 checkPrototype(ctor);
66 }
67
68 // Two TypedArrays backed by the same ArrayBuffer should be cloned into two
69 // TypedArrays still sharing a buffer. This also tests cloning TypedArrays
70 // where the arr->data pointer is not 8-byte-aligned.
71
72 var base = new Int8Array([0, 1, 2, 3]);
73 b = [new Int8Array(base.buffer, 0, 3), new Int8Array(base.buffer, 1, 3)];
74 var a = deserialize(serialize(b));
75 base[1] = -1;
76 a[0][2] = -2;
77 assertArraysEqual(b[0], new Int8Array([0, -1, 2])); // shared with base
78 assertArraysEqual(b[1], new Int8Array([-1, 2, 3])); // shared with base
79 assertArraysEqual(a[0], new Int8Array([0, 1, -2])); // not shared with base
80 assertArraysEqual(a[1], new Int8Array([1, -2, 3])); // not shared with base, shared with a[0]
81
82 assertEq(b[0].buffer, b[1].buffer);
83 assertEq(b[1].byteOffset, 1);
84 assertEq(b[1].byteLength, 3);
85 assertEq(b[1].buffer.byteLength, 4);
86
87 // ArrayBuffer clones do not preserve properties
88
89 base = new Int8Array([0, 1, 2, 3]);
90 b = [new Int8Array(base.buffer, 0, 3), new Int8Array(base.buffer, 1, 3)];
91 base.buffer.prop = "yes";
92 base.buffer.loop = b[0];
93 base.buffer.loops = [ b[0], b[1] ];
94 a = deserialize(serialize(b));
95 assertEq("prop" in a[0].buffer, false);
96 assertEq("prop" in a[1].buffer, false);
97 assertEq("loop" in a[0].buffer, false);
98 assertEq("loop" in a[1].buffer, false);
99 assertEq("loops" in a[0].buffer, false);
100 assertEq("loops" in a[1].buffer, false);
101 }
102
103 test();
104 reportCompare(0, 0, 'ok');