2014-01-30 04:45:48 -08:00
|
|
|
<?xml version="1.0"?>
|
|
|
|
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
|
|
|
|
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
|
|
|
|
<window title="Mozilla Bug 503926"
|
|
|
|
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
|
|
|
|
<script type="application/javascript"
|
|
|
|
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
|
|
|
|
|
|
|
|
<!-- test results are displayed in the html:body -->
|
|
|
|
<body xmlns="http://www.w3.org/1999/xhtml">
|
|
|
|
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=964293"
|
|
|
|
target="_blank">Cu.cloneInto()</a>
|
|
|
|
</body>
|
|
|
|
|
|
|
|
<!-- test code goes here -->
|
|
|
|
<script type="application/javascript">
|
|
|
|
<![CDATA[
|
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
|
|
|
const TypedArrayThings = [
|
|
|
|
'Int8Array',
|
|
|
|
'Uint8Array',
|
|
|
|
'Uint8ClampedArray',
|
|
|
|
'Int16Array',
|
|
|
|
'Uint16Array',
|
|
|
|
'Int32Array',
|
|
|
|
'Uint32Array',
|
|
|
|
'Float32Array',
|
|
|
|
'Float64Array',
|
|
|
|
];
|
|
|
|
|
|
|
|
function getType(a) {
|
|
|
|
if (a === null || a === undefined)
|
|
|
|
return 'null';
|
|
|
|
|
|
|
|
if (Array.isArray(a))
|
|
|
|
return 'array';
|
|
|
|
|
|
|
|
if (a instanceof Ci.nsIDOMFile)
|
|
|
|
return 'file';
|
|
|
|
|
|
|
|
if (a instanceof Ci.nsIDOMBlob)
|
|
|
|
return 'blob';
|
|
|
|
|
|
|
|
if (TypedArrayThings.indexOf(a.constructor.name) !== -1)
|
|
|
|
return a.constructor.name;
|
|
|
|
|
|
|
|
if (typeof a == 'object')
|
|
|
|
return 'object';
|
|
|
|
|
|
|
|
if (typeof a == 'function')
|
|
|
|
return 'function';
|
|
|
|
|
|
|
|
return 'primitive';
|
|
|
|
}
|
|
|
|
|
|
|
|
function compare(a, b) {
|
|
|
|
is (getType(a), getType(b), 'Type matches');
|
|
|
|
|
|
|
|
var type = getType(a);
|
|
|
|
if (type == 'array') {
|
|
|
|
is (a.length, b.length, 'Array.length matches');
|
|
|
|
for (var i = 0; i < a.length; ++i) {
|
|
|
|
compare(a[i], b[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == 'file' || type == 'blob') {
|
|
|
|
ok ( a === b, 'They should match');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == 'object') {
|
|
|
|
ok ( a !== b, 'They should not match');
|
|
|
|
|
|
|
|
var aProps = [];
|
|
|
|
for (var p in a) aProps.push(p);
|
|
|
|
|
|
|
|
var bProps = [];
|
|
|
|
for (var p in b) bProps.push(p);
|
|
|
|
|
|
|
|
is (aProps.length, bProps.length, 'Props match');
|
|
|
|
is (aProps.sort().toSource(), bProps.sort().toSource(), 'Props match - using toSource()');
|
|
|
|
|
|
|
|
for (var p in a) {
|
|
|
|
compare(a[p], b[p]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == 'function') {
|
|
|
|
ok ( a !== b, 'They should not match');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type != 'null') {
|
|
|
|
is (a.toSource(), b.toSource(), 'Matching using toSource()');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 08:21:56 -07:00
|
|
|
var sandboxOptions = {
|
|
|
|
wantXrays: true,
|
|
|
|
wantExportHelpers: true,
|
|
|
|
};
|
|
|
|
var sandbox = new Cu.Sandbox(window, sandboxOptions);
|
|
|
|
// The second sandbox is for testing the exportHelper version of the cloneInto
|
|
|
|
var sandbox2 = new Cu.Sandbox("http://example.com", sandboxOptions);
|
|
|
|
sandbox.sandbox2 = sandbox2;
|
|
|
|
|
2014-05-28 11:14:27 -07:00
|
|
|
function cloneAndTest(test) {
|
|
|
|
var output = sandbox.test = Cu.cloneInto(test, sandbox);
|
2014-03-25 08:21:56 -07:00
|
|
|
compare(test, output);
|
|
|
|
|
2014-05-28 11:14:27 -07:00
|
|
|
output = Cu.evalInSandbox('cloneInto(test, sandbox2)', sandbox);
|
2014-03-25 08:21:56 -07:00
|
|
|
compare(test, output);
|
|
|
|
}
|
2014-01-30 04:45:48 -08:00
|
|
|
|
2014-05-28 11:14:27 -07:00
|
|
|
function cloneAndTestWithFunctions(test) {
|
|
|
|
var output = sandbox.test = Cu.cloneInto(test, sandbox, { cloneFunctions: true });
|
|
|
|
compare(test, output);
|
|
|
|
|
|
|
|
output = Cu.evalInSandbox('cloneInto(test, sandbox2, { cloneFunctions: true })', sandbox);
|
|
|
|
// Note - We need to waive here, because functions are filtered out by object Xrays.
|
|
|
|
compare(test, Cu.waiveXrays(output));
|
|
|
|
}
|
|
|
|
|
2014-01-30 04:45:48 -08:00
|
|
|
var tests = [
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
true,
|
|
|
|
'hello world',
|
|
|
|
[1, 2, 3],
|
|
|
|
{ a: 1, b: 2 },
|
|
|
|
{ blob: new Blob([]), file: new File(new Blob([])) },
|
|
|
|
new Date(),
|
|
|
|
{ a: 1, b: {}, c: [1, 2, 3, { d: new Blob([]) } ], e: 'hello world' },
|
|
|
|
];
|
|
|
|
|
|
|
|
for (var i = 0; i < tests.length; ++i) {
|
2014-03-25 08:21:56 -07:00
|
|
|
cloneAndTest(tests[i]);
|
2014-01-30 04:45:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var output = Cu.cloneInto({ a: function() {} }, sandbox);
|
|
|
|
ok(false, 'Function should not be cloned by default');
|
|
|
|
} catch(e) {
|
|
|
|
ok(true, 'Function should not be cloned by default');
|
|
|
|
}
|
|
|
|
|
2014-03-25 08:21:56 -07:00
|
|
|
try {
|
|
|
|
sandbox2.sandbox = sandbox;
|
|
|
|
Cu.evalInSandbox('cloneInto({}, sandbox)', sandbox2);
|
|
|
|
ok(false, 'CloneInto should only work on less privileged target scopes.');
|
|
|
|
} catch(e) {
|
|
|
|
ok(/denied|insecure/.test(e),
|
|
|
|
'CloneInto should only work on less privileged target scopes.');
|
|
|
|
}
|
2014-01-30 04:45:48 -08:00
|
|
|
|
2014-06-09 11:09:51 -07:00
|
|
|
var test = { a: function() { return 42; } };
|
2014-05-28 11:14:27 -07:00
|
|
|
cloneAndTestWithFunctions(test);
|
2014-06-09 11:09:51 -07:00
|
|
|
|
|
|
|
// Check that we're cloning input to functions:
|
|
|
|
test = { a: function(obj) { return obj; } };
|
|
|
|
var clonedTest = Cu.cloneInto(test, sandbox, {cloneFunctions: true});
|
|
|
|
var testInput = {};
|
|
|
|
isnot(clonedTest.a(testInput), testInput, "Objects should not be identical");
|
2014-01-30 04:45:48 -08:00
|
|
|
]]>
|
|
|
|
</script>
|
|
|
|
</window>
|