Bug 1126509 - Throw if fixIterator() was passed an unknown object. r=jcranmer
This commit is contained in:
Родитель
c45bfac5b9
Коммит
097b9b4f56
|
@ -84,6 +84,38 @@ function test_fixIterator() {
|
|||
do_check_eq(val, JSArray[i++]);
|
||||
}
|
||||
do_check_true(i > 0);
|
||||
|
||||
// Bug 1126509, test that fixIterator rejects unknown objects.
|
||||
let thrown = false;
|
||||
let tryIterate = { item: "An object, that is not supported by fixIterator." };
|
||||
try {
|
||||
for (let val in iteratorUtils.fixIterator(tryIterate)) { dump(val); }
|
||||
} catch (e) {
|
||||
// A specific exception is the correct behaviour here.
|
||||
if (e.message == "An unsupported object sent to fixIterator: [object Object]")
|
||||
thrown = true;
|
||||
}
|
||||
do_check_true(thrown);
|
||||
|
||||
thrown = false;
|
||||
try {
|
||||
for (let val of iteratorUtils.fixIterator(tryIterate)) { dump(val); }
|
||||
} catch (e) {
|
||||
// A specific exception is the correct behaviour here.
|
||||
if (e.message == "An unsupported object sent to fixIterator: [object Object]")
|
||||
thrown = true;
|
||||
}
|
||||
do_check_true(thrown);
|
||||
|
||||
thrown = false;
|
||||
try {
|
||||
let result = iteratorUtils.toXPCOMArray(tryIterate, Ci.nsIArray);
|
||||
} catch (e) {
|
||||
// A specific exception is the correct behaviour here.
|
||||
if (e.message == "An unsupported interface requested from toXPCOMArray: nsIArray")
|
||||
thrown = true;
|
||||
}
|
||||
do_check_true(thrown);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -119,6 +151,18 @@ function test_toArray_builtin_iterator() {
|
|||
do_check_eq(i, iteratorArray[i][0]);
|
||||
do_check_eq(val, iteratorArray[i][1]);
|
||||
}
|
||||
|
||||
// Bug 1126509, test that toArray rejects unknown objects.
|
||||
let thrown = false;
|
||||
let tryIterate = { item: "An object, that is not supported by toArray." };
|
||||
try {
|
||||
let result = iteratorUtils.toArray(tryIterate);
|
||||
} catch (e) {
|
||||
// A specific exception is the correct behaviour here.
|
||||
if (e.message == "An unsupported object sent to toArray: [object Object]")
|
||||
thrown = true;
|
||||
}
|
||||
do_check_true(thrown);
|
||||
}
|
||||
|
||||
const Symbol_iterator = typeof Symbol === "function" && Symbol.iterator ?
|
||||
|
@ -151,6 +195,6 @@ var gTests = [
|
|||
];
|
||||
|
||||
function run_test() {
|
||||
for (let [, test] in Iterator(gTests))
|
||||
for (let test of gTests)
|
||||
test();
|
||||
}
|
||||
|
|
|
@ -20,20 +20,20 @@ const ITERATOR_SYMBOL = JS_HAS_SYMBOLS ? Symbol.iterator : "@@iterator";
|
|||
* This function will take a number of objects and convert them to an array.
|
||||
*
|
||||
* Currently, we support the following objects:
|
||||
* NodeList (i.e. element.childNodes)
|
||||
* Anything you can for (let x of aObj) on
|
||||
* (e.g. toArray(fixIterator(enum))[4])
|
||||
* (e.g. toArray(fixIterator(enum))[4],
|
||||
* also a NodeList from element.childNodes)
|
||||
*
|
||||
* @param aObj The object to convert
|
||||
*/
|
||||
function toArray(aObj) {
|
||||
if (ITERATOR_SYMBOL in aObj) {
|
||||
return Array.from(aObj);
|
||||
} else if (aObj.constructor.contains("NodeList")) {
|
||||
return Array.slice(aObj);
|
||||
}
|
||||
|
||||
return null;
|
||||
// We got something unexpected, notify the caller loudly.
|
||||
throw new Error("An unsupported object sent to toArray: " +
|
||||
(("toString" in aObj) ? aObj.toString() : aObj));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -123,7 +123,9 @@ function fixIterator(aEnum, aIface) {
|
|||
})());
|
||||
}
|
||||
|
||||
return null;
|
||||
// We got something unexpected, notify the caller loudly.
|
||||
throw new Error("An unsupported object sent to fixIterator: " +
|
||||
(("toString" in aEnum) ? aEnum.toString() : aEnum));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -148,6 +150,7 @@ function toXPCOMArray(aArray, aInterface) {
|
|||
}
|
||||
return supportsArray;
|
||||
}
|
||||
|
||||
if (aInterface.equals(Ci.nsIMutableArray)) {
|
||||
let mutableArray = Components.classes["@mozilla.org/array;1"]
|
||||
.createInstance(Ci.nsIMutableArray);
|
||||
|
@ -157,5 +160,7 @@ function toXPCOMArray(aArray, aInterface) {
|
|||
return mutableArray;
|
||||
}
|
||||
|
||||
throw "no supports for interface conversion";
|
||||
// We got something unexpected, notify the caller loudly.
|
||||
throw new Error("An unsupported interface requested from toXPCOMArray: " +
|
||||
aInterface);
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче