Bug 974546 - SpecialPowers wrappers should catch, wrap, and rethrow exceptions for callables. r=mrbkap

This commit is contained in:
Bobby Holley 2014-02-20 08:57:20 -08:00
parent c2040dc3c9
commit 37381cb9d4
2 changed files with 30 additions and 2 deletions

View File

@ -141,6 +141,24 @@ function starttest(){
var returnsNull = function() { return null; }
is(SpecialPowers.wrap(returnsNull)(), null, "Should be able to handle functions that return null.");
// Check a function that throws.
var thrower = function() { throw new Error('hah'); }
try {
SpecialPowers.wrap(thrower)();
ok(false, "Should have thrown");
} catch (e) {
ok(SpecialPowers.isWrapper(e), "Exceptions should be wrapped for call");
is(e.message, 'hah', "Correct message");
}
try {
var ctor = SpecialPowers.wrap(thrower);
new ctor();
ok(false, "Should have thrown");
} catch (e) {
ok(SpecialPowers.isWrapper(e), "Exceptions should be wrapped for construct");
is(e.message, 'hah', "Correct message");
}
// Play around with a JS object to check the non-xray path.
var noxray_proto = {a: 3, b: 12};
var noxray = {a: 5, c: 32};

View File

@ -115,7 +115,12 @@ function wrapPrivileged(obj) {
var invocant = unwrapIfWrapped(this);
var unwrappedArgs = Array.prototype.slice.call(arguments).map(unwrapIfWrapped);
return wrapPrivileged(doApply(obj, invocant, unwrappedArgs));
try {
return wrapPrivileged(doApply(obj, invocant, unwrappedArgs));
} catch (e) {
// Wrap exceptions and re-throw them.
throw wrapIfUnwrapped(e);
}
};
var constructTrap = function() {
// The arguments may or may not be wrappers. Unwrap them if necessary.
@ -129,7 +134,12 @@ function wrapPrivileged(obj) {
// underlying constructor just be passing along its return value in our
// constructor.
var FakeConstructor = function() {
return doApply(obj, this, unwrappedArgs);
try {
return doApply(obj, this, unwrappedArgs);
} catch (e) {
// Wrap exceptions and re-throw them.
throw wrapIfUnwrapped(e);
}
};
FakeConstructor.prototype = obj.prototype;