Debian Codegraph @

js/src/tests/non262/extensions/clone-v1-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 // This file is a copy of clone-typed-array.js from before v2 structured clone
6 // was implemented. If you run this test under a v1-writing engine with the
7 // environment variable JS_RECORD_RESULTS set, then it will output a log of
8 // structured clone buffers resulting from running this test. You can then use
9 // that log as input to another run of this same test on a newer engine, to
10 // verify that older-format structured clone data can be deserialized properly.
11
12 var old_serialize = serialize;
13 var captured = [];
14
15 if (os.getenv("JS_RECORD_RESULTS") !== undefined) {
16 serialize = function(o) {
17 var data;
18 try {
19 data = old_serialize(o);
20 captured.push(data);
21 return data;
22 } catch(e) {
23 captured.push(e);
24 throw(e);
25 }
26 };
27 } else {
28 loadRelativeToScript("clone-v1-typed-array-data.dat");
29 serialize = function(d) {
30 var data = captured.shift();
31 if (data instanceof Error)
32 throw(data);
33 else
34 return data;
35 };
36 }
37
38 function assertArraysEqual(a, b) {
39 assertEq(a.constructor, b.constructor);
40 assertEq(a.length, b.length);
41 for (var i = 0; i < a.length; i++)
42 assertEq(a[i], b[i]);
43 }
44
45 function check(b) {
46 var a = deserialize(serialize(b));
47 assertArraysEqual(a, b);
48 }
49
50 function checkPrototype(ctor) {
51 var threw = false;
52 try {
53 serialize(ctor.prototype);
54 throw new Error("serializing " + ctor.name + ".prototype should throw a TypeError");
55 } catch (exc) {
56 if (!(exc instanceof TypeError))
57 throw exc;
58 }
59 }
60
61 function test() {
62 // Test cloning ArrayBuffer objects.
63 check(new ArrayBuffer(0));
64 check(new ArrayBuffer(7));
65 checkPrototype(ArrayBuffer);
66
67 // Test cloning typed array objects.
68 var ctors = [
69 Int8Array,
70 Uint8Array,
71 Int16Array,
72 Uint16Array,
73 Int32Array,
74 Uint32Array,
75 Float32Array,
76 Float64Array,
77 Uint8ClampedArray];
78
79 var b;
80 for (var i = 0; i < ctors.length; i++) {
81 var ctor = ctors[i];
82
83 // check empty array
84 b = new ctor(0);
85 check(b);
86
87 // check array with some elements
88 b = new ctor(100);
89 var v = 1;
90 for (var j = 0; j < 100; j++) {
91 b[j] = v;
92 v *= 7;
93 }
94 b[99] = NaN; // check serializing NaNs too
95 check(b);
96
97 // try the prototype
98 checkPrototype(ctor);
99 }
100
101 // Cloning should separately copy two TypedArrays backed by the same
102 // ArrayBuffer. This also tests cloning TypedArrays where the arr->data
103 // pointer is not 8-byte-aligned.
104
105 var base = new Int8Array([0, 1, 2, 3]);
106 b = [new Int8Array(base.buffer, 0, 3), new Int8Array(base.buffer, 1, 3)];
107 var a = deserialize(serialize(b));
108 base[1] = -1;
109 a[0][2] = -2;
110 assertArraysEqual(b[0], new Int8Array([0, -1, 2])); // shared with base
111 assertArraysEqual(b[1], new Int8Array([-1, 2, 3])); // shared with base
112 assertArraysEqual(a[0], new Int8Array([0, 1, -2])); // not shared with base
113 assertArraysEqual(a[1], new Int8Array([1, 2, 3])); // not shared with base or a[0]
114 }
115
116 test();
117 reportCompare(0, 0, 'ok');
118
119 if (os.getenv("JS_RECORD_RESULTS") !== undefined) {
120 print("var captured = [];");
121 for (var i in captured) {
122 var s = "captured[" + i + "] = ";
123 if (captured[i] instanceof Error) {
124 print(s + captured[i].toString() + ";");
125 } else {
126 data = captured[i].clonebuffer.split('').map(c => c.charCodeAt(0));
127 print(s + "serialize(0); captured[" + i + "].clonebuffer = String.fromCharCode(" + data.join(", ") + ");");
128 }
129 }
130 }