Bug 980965. Stop using DOM constructors as functions in chrome code. r=bholley

This commit is contained in:
Boris Zbarsky 2014-03-10 17:38:14 -04:00
parent 0efc7b78ef
commit ad635be417
16 changed files with 30 additions and 39 deletions

View File

@ -58,7 +58,7 @@ function Buffer(subject, encoding /*, bufferLength */) {
// If string encode it and use buffer for the returned Uint8Array
// to create a local patched version that acts like node buffer.
encoding = encoding || 'utf8';
return Uint8Array(TextEncoder(encoding).encode(subject).buffer);
return Uint8Array(new TextEncoder(encoding).encode(subject).buffer);
case 'object':
// This form of the constructor uses the form of
// Uint8Array(buffer, offset, length);
@ -84,7 +84,7 @@ Buffer.isBuffer = value => value instanceof Buffer
Buffer.isEncoding = function (encoding) {
if (!encoding) return false;
try {
TextDecoder(encoding);
new TextDecoder(encoding);
} catch(e) {
return false;
}
@ -95,7 +95,7 @@ Buffer.isEncoding = function (encoding) {
// This is not the same as String.prototype.length since that returns the
// number of characters in a string.
Buffer.byteLength = (value, encoding = 'utf8') =>
TextEncoder(encoding).encode(value).byteLength
new TextEncoder(encoding).encode(value).byteLength
// Direct copy of the nodejs's buffer implementation:
// https://github.com/joyent/node/blob/b255f4c10a80343f9ce1cee56d0288361429e214/lib/buffer.js#L146-L177
@ -156,7 +156,7 @@ Object.defineProperties(Buffer.prototype, {
encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
start = Math.max(0, ~~start);
end = Math.min(this.length, end === void(0) ? this.length : ~~end);
return TextDecoder(encoding).decode(this.subarray(start, end));
return new TextDecoder(encoding).decode(this.subarray(start, end));
}
},
toJSON: {
@ -262,7 +262,7 @@ Object.defineProperties(Buffer.prototype, {
if (length == null || length + offset > this.length)
length = this.length - offset;
let buffer = TextEncoder(encoding).encode(string);
let buffer = new TextEncoder(encoding).encode(string);
let result = Math.min(buffer.length, length);
if (buffer.length !== length)
buffer = buffer.subarray(0, length);

View File

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
let xhr = XMLHttpRequest();
let xhr = new XMLHttpRequest();
xhr.open("GET", "data:text/plain,ok", true);
xhr.onload = function () {
postMessage(xhr.responseText);

View File

@ -492,7 +492,7 @@ function sendDragEvent(aEventType, aTarget, aData) {
* @return The drag event.
*/
function createDragEvent(aEventType, aData) {
let dataTransfer = new getContentWindow().DataTransfer("dragstart", false);
let dataTransfer = new (getContentWindow()).DataTransfer("dragstart", false);
dataTransfer.mozSetDataAt("text/x-moz-url", aData, 0);
let event = getContentDocument().createEvent("DragEvents");
event.initDragEvent(aEventType, true, true, getContentWindow(), 0, 0, 0, 0, 0,

View File

@ -39,7 +39,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=780199
function test() {
b = document.getElementById("b");
var m = MutationObserver(callback);
var m = new MutationObserver(callback);
m.observe(b, { attributes: true, attributeOldValue: true });
b.contentDocument.documentElement.textContent = "testvalue";
b.setAttribute("src", b.getAttribute("src"));

View File

@ -5,7 +5,7 @@
function boom()
{
AudioContext().createBufferSource().playbackRate.linearRampToValueAtTime(0, -1);
new AudioContext().createBufferSource().playbackRate.linearRampToValueAtTime(0, -1);
}
</script>

View File

@ -6,7 +6,7 @@
function boom()
{
var bufferSource = AudioContext().createScriptProcessor().context.createBufferSource();
var bufferSource = new AudioContext().createScriptProcessor().context.createBufferSource();
bufferSource.start(0, 0, 0);
bufferSource.stop(562949953421313);
}

View File

@ -1,4 +1,4 @@
<!DOCTYPE html>
<script>
OfflineAudioContext(0, 0, 3229622);
new OfflineAudioContext(0, 0, 3229622);
</script>

View File

@ -10,7 +10,7 @@ function boom()
var frameWin = frame.contentWindow;
frameWin.VTTCue;
document.body.removeChild(frame);
frameWin.VTTCue(0, 1, "Bug 894104").getCueAsHTML();
new frameWin.VTTCue(0, 1, "Bug 894104").getCueAsHTML();
}
</script>

View File

@ -108,13 +108,13 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=741267
}
try {
var xhr = Components.utils.evalInSandbox("new XMLHttpRequest()", sandbox);
is("" + xhr, "[object XrayWrapper " + XMLHttpRequest() + "]", "'XMLHttpRequest()' in a sandbox should create an XMLHttpRequest object");
is("" + xhr, "[object XrayWrapper " + new XMLHttpRequest() + "]", "'XMLHttpRequest()' in a sandbox should create an XMLHttpRequest object");
} catch (e) {
ok(false, "'new XMLHttpRequest()' shouldn't throw in a sandbox (1)");
}
try {
var xhr = Components.utils.evalInSandbox("XMLHttpRequest.prototype.toString = function () { return 'Failed'; }; new XMLHttpRequest();", sandbox);
is(xhr.toString(), "[object XrayWrapper " + XMLHttpRequest() + "]", "XMLHttpRequest.prototype.toString in the sandbox should not override the native toString behaviour");
is(xhr.toString(), "[object XrayWrapper " + new XMLHttpRequest() + "]", "XMLHttpRequest.prototype.toString in the sandbox should not override the native toString behaviour");
} catch (e) {
ok(false, "'new XMLHttpRequest()' shouldn't throw in a sandbox (2)");
}
@ -139,8 +139,8 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=741267
try {
Components.utils.evalInSandbox("document.defaultView.XMLHttpRequest = function() {};", sandbox);
var win = Components.utils.evalInSandbox("document.defaultView", sandbox);
var xhr = win.XMLHttpRequest();
is("" + xhr, "[object XrayWrapper " + XMLHttpRequest() + "]", "'XMLHttpRequest()' in a sandbox should create an XMLHttpRequest object");
var xhr = new win.XMLHttpRequest();
is("" + xhr, "[object XrayWrapper " + new XMLHttpRequest() + "]", "'XMLHttpRequest()' in a sandbox should create an XMLHttpRequest object");
} catch (e) {
ok(false, "'XMLHttpRequest()' shouldn't throw in a sandbox");
}

View File

@ -25,7 +25,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=861493
addLoadEvent(function() {
ok(Components.utils.isXrayWrapper($("t").contentWindow),
"Should have xray");
var e = new $("t").contentWindow.Event("test", { bubbles: true });
var e = new ($("t").contentWindow).Event("test", { bubbles: true });
is(e.bubbles, true, "Dictionary should have worked");
SimpleTest.finish();
})

View File

@ -224,10 +224,12 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=821850
window.triggeredTrustedHandler = false;
var untrustedEvent = new CustomEvent('testtrusted');
ok(!untrustedEvent.isTrusted, "Created an untrusted event");
is(untrustedEvent.type, 'testtrusted', "Constructor should see type");
bound.dispatchEvent(untrustedEvent);
ok(!window.triggeredTrustedHandler, "untrusted events should not trigger trusted handler");
var trustedEvent = new chromeWin.CustomEvent('testtrusted');
ok(trustedEvent.isTrusted, "Created a trusted event");
is(trustedEvent.type, 'testtrusted', "Wrapped constructor should see type");
SpecialPowers.wrap(bound).dispatchEvent(trustedEvent);
ok(window.triggeredTrustedHandler, "trusted events should trigger trusted handler");

View File

@ -126,24 +126,13 @@ function wrapPrivileged(obj) {
// The arguments may or may not be wrappers. Unwrap them if necessary.
var unwrappedArgs = Array.prototype.slice.call(arguments).map(unwrapIfWrapped);
// Constructors are tricky, because we can't easily call apply on them.
// As a workaround, we create a wrapper constructor with the same
// |prototype| property. ES semantics dictate that the return value from
// |new| is the return value of the |new|-ed function i.f.f. the returned
// value is an object. We can thus mimic the behavior of |new|-ing the
// underlying constructor just be passing along its return value in our
// constructor.
var FakeConstructor = function() {
try {
return doApply(obj, this, unwrappedArgs);
} catch (e) {
// Wrap exceptions and re-throw them.
throw wrapIfUnwrapped(e);
}
};
FakeConstructor.prototype = obj.prototype;
return wrapPrivileged(new FakeConstructor());
// We want to invoke "obj" as a constructor, but using unwrappedArgs as
// the arguments. Make sure to wrap and re-throw exceptions!
try {
return wrapPrivileged(new obj(...unwrappedArgs));
} catch (e) {
throw wrapIfUnwrapped(e);
}
};
return Proxy.createFunction(handler, callTrap, constructTrap);

View File

@ -378,7 +378,7 @@ function SymbolicationRequest_fetchSymbols() {
"version" : 3};
let requestJSON = JSON.stringify(request);
this.symbolRequest = XMLHttpRequest();
this.symbolRequest = new XMLHttpRequest();
this.symbolRequest.open("POST", symbolServerURI, true);
this.symbolRequest.setRequestHeader("Content-type", "application/json");
this.symbolRequest.setRequestHeader("Content-length",

View File

@ -411,7 +411,7 @@ BoxModelHighlighter.prototype = {
_trackMutations: function() {
if (this.currentNode) {
let win = this.currentNode.ownerDocument.defaultView;
this.currentNodeObserver = win.MutationObserver(this._update);
this.currentNodeObserver = new win.MutationObserver(this._update);
this.currentNodeObserver.observe(this.currentNode, {attributes: true});
}
},

View File

@ -983,7 +983,7 @@ var WalkerActor = protocol.ActorClass({
let node = actor.rawNode;
// Create the observer on the node's actor. The node will make sure
// the observer is cleaned up when the actor is released.
actor.observer = actor.rawNode.defaultView.MutationObserver(this.onMutations);
actor.observer = new actor.rawNode.defaultView.MutationObserver(this.onMutations);
actor.observer.observe(node, {
attributes: true,
characterData: true,

View File

@ -47,7 +47,7 @@ function isFirstRunOrUpdate() {
function writeFile(aPath, aData) {
return Task.spawn(function() {
let data = TextEncoder().encode(aData);
let data = new TextEncoder().encode(aData);
yield OS.File.writeAtomic(aPath, data, { tmpPath: aPath + ".tmp" });
});
}