2 <?xml-stylesheet type=
"text/css" href=
"chrome://global/skin"?>
3 <?xml-stylesheet type=
"text/css" href=
"chrome://mochikit/content/tests/SimpleTest/test.css"?>
4 <window title=
"Mozilla Bug 503926"
5 xmlns=
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
6 <script src=
"chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
8 <!-- test results are displayed in the html:body -->
9 <body xmlns=
"http://www.w3.org/1999/xhtml">
10 <a href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=964293"
11 target=
"_blank">Cu.cloneInto()
</a>
14 <!-- test code goes here -->
15 <script type=
"application/javascript">
18 const TypedArrayThings = [
30 function checkThrows(f, msg, rgxp) {
33 ok(false,
"Should have thrown: " + msg);
35 ok(true,
"Threw correctly - " + msg +
" - " + e);
37 ok(rgxp.test(e),
"Should throw correct exception: " + e);
42 if (a === null || a === undefined)
48 if (File.isInstance(a))
51 if (Blob.isInstance(a))
54 if (TypedArrayThings.includes(a.constructor.name))
55 return a.constructor.name;
57 if (typeof a == 'object')
60 if (typeof a == 'function')
66 function compare(a, b) {
67 is (getType(a), getType(b), 'Type matches');
69 var type = getType(a);
70 if (type == 'array') {
71 is (a.length, b.length, 'Array.length matches');
72 for (var i =
0; i < a.length; ++i) {
79 if (type == 'file' || type == 'blob') {
80 ok ( a === b, 'They should match');
84 if (type == 'object') {
85 ok ( a !== b, 'They should not match');
88 for (let p in a) aProps.push(p);
91 for (let p in b) bProps.push(p);
93 is (aProps.length, bProps.length, 'Props match');
94 is (aProps.sort().toString(), bProps.sort().toString(), 'Props names match');
103 if (type == 'function') {
104 ok ( a !== b, 'They should not match');
108 if (type != 'null') {
109 is (a, b, 'Same value');
113 var sandboxOptions = {
115 wantExportHelpers: true,
117 var sandbox = new Cu.Sandbox(window, sandboxOptions);
118 // The second sandbox is for testing the exportHelper version of the cloneInto
119 var sandbox2 = new Cu.Sandbox(
"https://example.com", sandboxOptions);
120 sandbox.sandbox2 = sandbox2;
121 sandbox2.sandbox = sandbox;
123 function cloneAndTest(test) {
124 var output = sandbox.test = Cu.cloneInto(test, sandbox);
125 compare(test, output);
127 output = Cu.evalInSandbox('cloneInto(test, sandbox2)', sandbox);
128 compare(test, output);
131 function cloneAndTestWithFunctions(test) {
132 var output = sandbox.test = Cu.cloneInto(test, sandbox, { cloneFunctions: true });
133 compare(test, output);
135 output = Cu.evalInSandbox('cloneInto(test, sandbox2, { cloneFunctions: true })', sandbox);
136 // Note - We need to waive here, because functions are filtered out by object Xrays.
137 compare(test, Cu.waiveXrays(output));
148 { a:
1, b: {}, c: [
1,
2,
3, {} ], e: 'hello world' },
151 for (var i =
0; i < tests.length; ++i) {
152 cloneAndTest(tests[i]);
155 checkThrows(function() { Cu.cloneInto({ a() {} }, sandbox); },
156 'Function should not be cloned by default');
158 checkThrows(function() { Cu.cloneInto({ a: document }, sandbox); },
159 'Reflectors should not be wrapped by default');
161 var withReflectors = Cu.cloneInto({ doc: document, win: window }, sandbox,
162 { wrapReflectors: true });
163 is(Cu.unwaiveXrays(Cu.waiveXrays(withReflectors).doc), document,
"Document passes");
164 is(Cu.unwaiveXrays(Cu.waiveXrays(withReflectors).win), window,
"Window passes");
167 checkThrows(function() { Cu.evalInSandbox('cloneInto({}, sandbox)', sandbox2); },
168 'CloneInto should only work on less privileged target scopes.',
171 var cloneTarget = new Cu.Sandbox(
"https://example.com");
172 var sameOriginSB = new Cu.Sandbox(
"https://example.com", { wantGlobalProperties: ['XMLHttpRequest'] });
173 var crossOriginSB = new Cu.Sandbox(
"https://example.net", { wantGlobalProperties: ['XMLHttpRequest'] });
174 sandbox2.cloneTarget = cloneTarget;
175 sandbox2.soXHR = Cu.evalInSandbox('new XMLHttpRequest()', sameOriginSB);
176 sandbox2.xoXHR = Cu.evalInSandbox('new XMLHttpRequest()', crossOriginSB);
177 sandbox2.chromeDoc = document;
178 Cu.evalInSandbox('function tryToClone(x) { return cloneInto({val: x}, cloneTarget, { wrapReflectors: true }).val; }', sandbox2);
179 is(Cu.evalInSandbox('tryToClone(soXHR)', sandbox2), sandbox2.soXHR, 'Same-origin wrapReflectors works');
180 checkThrows(function() { Cu.evalInSandbox('tryToClone(chromeDoc)', sandbox2); },
181 'wrapReflectors may not wrap cross-origin reflectors', /unsupported value type/);
184 var test = { a() { return
42; } };
185 cloneAndTestWithFunctions(test);
187 // Check that inputs are properly passed through cloned functions:
188 test = { a(obj) { return obj; } };
189 var clonedTest = Cu.cloneInto(test, sandbox, {cloneFunctions: true});
191 is(clonedTest.a(testInput), testInput,
"Objects should be identical");