Bug 1012988 - Remove heritage dependency to packets.js. r=jimb
authorAlexandre Poirot <poirot.alex@gmail.com>
Thu, 05 Jun 2014 08:55:00 -0400
changeset 206410 1023a50167c5957c5924f9881703fe36ce1c6fef
parent 206409 59e2e6c3c1ec4e5d3ee1d546aaa3f41c9180def6
child 206411 939350a04d9feae944ff8898ba8caffeeb5d312c
push id3741
push userasasaki@mozilla.com
push dateMon, 21 Jul 2014 20:25:18 +0000
treeherdermozilla-beta@4d6f46f5af68 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimb
bugs1012988
milestone32.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1012988 - Remove heritage dependency to packets.js. r=jimb
toolkit/devtools/transport/packets.js
--- a/toolkit/devtools/transport/packets.js
+++ b/toolkit/devtools/transport/packets.js
@@ -20,17 +20,16 @@
  *     Called when the output stream is ready to write
  *   * get done()
  *     Returns true once the packet is done being read / written
  *   * destroy()
  *     Called to clean up at the end of use
  */
 
 const { Cc, Ci, Cu } = require("chrome");
-const Heritage = require("sdk/core/heritage");
 const DevToolsUtils = require("devtools/toolkit/DevToolsUtils");
 const { dumpn, dumpv } = DevToolsUtils;
 const StreamUtils = require("devtools/toolkit/transport/stream-utils");
 
 DevToolsUtils.defineLazyGetter(this, "unicodeConverter", () => {
   const unicodeConverter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
                            .createInstance(Ci.nsIScriptableUnicodeConverter);
   unicodeConverter.charset = "UTF-8";
@@ -123,93 +122,95 @@ JSONPacket.fromHeader = function(header,
   dumpv("Header matches JSON packet");
   let packet = new JSONPacket(transport);
   packet.length = +match[1];
   return packet;
 };
 
 JSONPacket.HEADER_PATTERN = /^(\d+):$/;
 
-JSONPacket.prototype = Heritage.extend(Packet.prototype, {
+JSONPacket.prototype = Object.create(Packet.prototype);
 
+Object.defineProperty(JSONPacket.prototype, "object", {
   /**
    * Gets the object (not the serialized string) being read or written.
    */
-  get object() { return this._object; },
+  get: function() { return this._object; },
 
   /**
    * Sets the object to be sent when write() is called.
    */
-  set object(object) {
+  set: function(object) {
     this._object = object;
     let data = JSON.stringify(object);
     this._data = unicodeConverter.ConvertFromUnicode(data);
     this.length = this._data.length;
-  },
-
-  read: function(stream, scriptableStream) {
-    dumpv("Reading JSON packet");
-
-    // Read in more packet data.
-    this._readData(stream, scriptableStream);
-
-    if (!this.done) {
-      // Don't have a complete packet yet.
-      return;
-    }
+  }
+});
 
-    let json = this._data;
-    try {
-      json = unicodeConverter.ConvertToUnicode(json);
-      this._object = JSON.parse(json);
-    } catch(e) {
-      let msg = "Error parsing incoming packet: " + json + " (" + e +
-                " - " + e.stack + ")";
-      if (Cu.reportError) {
-        Cu.reportError(msg);
-      }
-      dumpn(msg);
-      return;
-    }
+JSONPacket.prototype.read = function(stream, scriptableStream) {
+  dumpv("Reading JSON packet");
+
+  // Read in more packet data.
+  this._readData(stream, scriptableStream);
 
-    this._transport._onJSONObjectReady(this._object);
-  },
-
-  _readData: function(stream, scriptableStream) {
-    if (dumpv.wantVerbose) {
-      dumpv("Reading JSON data: _l: " + this.length + " dL: " +
-            this._data.length + " sA: " + stream.available());
-    }
-    let bytesToRead = Math.min(this.length - this._data.length,
-                               stream.available());
-    this._data += scriptableStream.readBytes(bytesToRead);
-    this._done = this._data.length === this.length;
-  },
+  if (!this.done) {
+    // Don't have a complete packet yet.
+    return;
+  }
 
-  write: function(stream) {
-    dumpv("Writing JSON packet");
-
-    if (this._outgoing === undefined) {
-      // Format the serialized packet to a buffer
-      this._outgoing = this.length + ":" + this._data;
+  let json = this._data;
+  try {
+    json = unicodeConverter.ConvertToUnicode(json);
+    this._object = JSON.parse(json);
+  } catch(e) {
+    let msg = "Error parsing incoming packet: " + json + " (" + e +
+              " - " + e.stack + ")";
+    if (Cu.reportError) {
+      Cu.reportError(msg);
     }
-
-    let written = stream.write(this._outgoing, this._outgoing.length);
-    this._outgoing = this._outgoing.slice(written);
-    this._done = !this._outgoing.length;
-  },
-
-  get done() { return this._done; },
-
-  toString: function() {
-    return JSON.stringify(this._object, null, 2);
+    dumpn(msg);
+    return;
   }
 
+  this._transport._onJSONObjectReady(this._object);
+}
+
+JSONPacket.prototype._readData = function(stream, scriptableStream) {
+  if (dumpv.wantVerbose) {
+    dumpv("Reading JSON data: _l: " + this.length + " dL: " +
+          this._data.length + " sA: " + stream.available());
+  }
+  let bytesToRead = Math.min(this.length - this._data.length,
+                             stream.available());
+  this._data += scriptableStream.readBytes(bytesToRead);
+  this._done = this._data.length === this.length;
+}
+
+JSONPacket.prototype.write = function(stream) {
+  dumpv("Writing JSON packet");
+
+  if (this._outgoing === undefined) {
+    // Format the serialized packet to a buffer
+    this._outgoing = this.length + ":" + this._data;
+  }
+
+  let written = stream.write(this._outgoing, this._outgoing.length);
+  this._outgoing = this._outgoing.slice(written);
+  this._done = !this._outgoing.length;
+}
+
+Object.defineProperty(JSONPacket.prototype, "done", {
+  get: function() { return this._done; }
 });
 
+JSONPacket.prototype.toString = function() {
+  return JSON.stringify(this._object, null, 2);
+}
+
 exports.JSONPacket = JSONPacket;
 
 /**
  * With a bulk packet, data is transferred by temporarily handing over the
  * transport's input or output stream to the application layer for writing data
  * directly.  This can be much faster for large data sets, and avoids various
  * stages of copies and data duplication inherent in the JSON packet type.  The
  * bulk packet looks like:
@@ -252,126 +253,131 @@ BulkPacket.fromHeader = function(header,
     type: match[2],
     length: +match[3]
   };
   return packet;
 };
 
 BulkPacket.HEADER_PATTERN = /^bulk ([^: ]+) ([^: ]+) (\d+):$/;
 
-BulkPacket.prototype = Heritage.extend(Packet.prototype, {
+BulkPacket.prototype = Object.create(Packet.prototype);
+
+BulkPacket.prototype.read = function(stream) {
+  dumpv("Reading bulk packet, handing off input stream");
 
-  read: function(stream) {
-    dumpv("Reading bulk packet, handing off input stream");
+  // Temporarily pause monitoring of the input stream
+  this._transport.pauseIncoming();
+
+  let deferred = promise.defer();
 
-    // Temporarily pause monitoring of the input stream
-    this._transport.pauseIncoming();
-
-    let deferred = promise.defer();
+  this._transport._onBulkReadReady({
+    actor: this.actor,
+    type: this.type,
+    length: this.length,
+    copyTo: (output) => {
+      dumpv("CT length: " + this.length);
+      deferred.resolve(StreamUtils.copyStream(stream, output, this.length));
+      return deferred.promise;
+    },
+    stream: stream,
+    done: deferred
+  });
 
-    this._transport._onBulkReadReady({
-      actor: this.actor,
-      type: this.type,
-      length: this.length,
-      copyTo: (output) => {
-        dumpv("CT length: " + this.length);
-        deferred.resolve(StreamUtils.copyStream(stream, output, this.length));
-        return deferred.promise;
-      },
-      stream: stream,
-      done: deferred
-    });
+  // Await the result of reading from the stream
+  deferred.promise.then(() => {
+    dumpv("onReadDone called, ending bulk mode");
+    this._done = true;
+    this._transport.resumeIncoming();
+  }, this._transport.close);
 
-    // Await the result of reading from the stream
-    deferred.promise.then(() => {
-      dumpv("onReadDone called, ending bulk mode");
-      this._done = true;
-      this._transport.resumeIncoming();
-    }, this._transport.close);
+  // Ensure this is only done once
+  this.read = () => {
+    throw new Error("Tried to read() a BulkPacket's stream multiple times.");
+  };
+}
 
-    // Ensure this is only done once
-    this.read = () => {
-      throw new Error("Tried to read() a BulkPacket's stream multiple times.");
-    };
-  },
+BulkPacket.prototype.write = function(stream) {
+  dumpv("Writing bulk packet");
 
-  write: function(stream) {
-    dumpv("Writing bulk packet");
+  if (this._outgoingHeader === undefined) {
+    dumpv("Serializing bulk packet header");
+    // Format the serialized packet header to a buffer
+    this._outgoingHeader = "bulk " + this.actor + " " + this.type + " " +
+                           this.length + ":";
+  }
 
-    if (this._outgoingHeader === undefined) {
-      dumpv("Serializing bulk packet header");
-      // Format the serialized packet header to a buffer
-      this._outgoingHeader = "bulk " + this.actor + " " + this.type + " " +
-                             this.length + ":";
-    }
+  // Write the header, or whatever's left of it to write.
+  if (this._outgoingHeader.length) {
+    dumpv("Writing bulk packet header");
+    let written = stream.write(this._outgoingHeader,
+                               this._outgoingHeader.length);
+    this._outgoingHeader = this._outgoingHeader.slice(written);
+    return;
+  }
 
-    // Write the header, or whatever's left of it to write.
-    if (this._outgoingHeader.length) {
-      dumpv("Writing bulk packet header");
-      let written = stream.write(this._outgoingHeader,
-                                 this._outgoingHeader.length);
-      this._outgoingHeader = this._outgoingHeader.slice(written);
-      return;
-    }
+  dumpv("Handing off output stream");
 
-    dumpv("Handing off output stream");
+  // Temporarily pause the monitoring of the output stream
+  this._transport.pauseOutgoing();
 
-    // Temporarily pause the monitoring of the output stream
-    this._transport.pauseOutgoing();
-
-    let deferred = promise.defer();
+  let deferred = promise.defer();
 
-    this._readyForWriting.resolve({
-      copyFrom: (input) => {
-        dumpv("CF length: " + this.length);
-        deferred.resolve(StreamUtils.copyStream(input, stream, this.length));
-        return deferred.promise;
-      },
-      stream: stream,
-      done: deferred
-    });
+  this._readyForWriting.resolve({
+    copyFrom: (input) => {
+      dumpv("CF length: " + this.length);
+      deferred.resolve(StreamUtils.copyStream(input, stream, this.length));
+      return deferred.promise;
+    },
+    stream: stream,
+    done: deferred
+  });
 
-    // Await the result of writing to the stream
-    deferred.promise.then(() => {
-      dumpv("onWriteDone called, ending bulk mode");
-      this._done = true;
-      this._transport.resumeOutgoing();
-    }, this._transport.close);
+  // Await the result of writing to the stream
+  deferred.promise.then(() => {
+    dumpv("onWriteDone called, ending bulk mode");
+    this._done = true;
+    this._transport.resumeOutgoing();
+  }, this._transport.close);
 
-    // Ensure this is only done once
-    this.write = () => {
-      throw new Error("Tried to write() a BulkPacket's stream multiple times.");
-    };
-  },
+  // Ensure this is only done once
+  this.write = () => {
+    throw new Error("Tried to write() a BulkPacket's stream multiple times.");
+  };
+}
 
-  get streamReadyForWriting() {
+Object.defineProperty(BulkPacket.prototype, "streamReadyForWriting", {
+  get: function() {
     return this._readyForWriting.promise;
-  },
+  }
+});
 
-  get header() {
+Object.defineProperty(BulkPacket.prototype, "header", {
+  get: function() {
     return {
       actor: this.actor,
       type: this.type,
       length: this.length
     };
   },
 
-  set header(header) {
+  set: function(header) {
     this.actor = header.actor;
     this.type = header.type;
     this.length = header.length;
   },
-
-  get done() { return this._done; },
+});
 
-  toString: function() {
-    return "Bulk: " + JSON.stringify(this.header, null, 2);
-  }
+Object.defineProperty(BulkPacket.prototype, "done", {
+  get: function() { return this._done; },
+});
 
-});
+
+BulkPacket.prototype.toString = function() {
+  return "Bulk: " + JSON.stringify(this.header, null, 2);
+}
 
 exports.BulkPacket = BulkPacket;
 
 /**
  * RawPacket is used to test the transport's error handling of malformed
  * packets, by writing data directly onto the stream.
  * @param transport DebuggerTransport
  *        The transport instance that will own the packet.
@@ -380,26 +386,26 @@ exports.BulkPacket = BulkPacket;
  */
 function RawPacket(transport, data) {
   Packet.call(this, transport);
   this._data = data;
   this.length = data.length;
   this._done = false;
 }
 
-RawPacket.prototype = Heritage.extend(Packet.prototype, {
+RawPacket.prototype = Object.create(Packet.prototype);
 
-  read: function(stream) {
-    // This hasn't yet been needed for testing.
-    throw Error("Not implmented.");
-  },
+RawPacket.prototype.read = function(stream) {
+  // This hasn't yet been needed for testing.
+  throw Error("Not implmented.");
+}
 
-  write: function(stream) {
-    let written = stream.write(this._data, this._data.length);
-    this._data = this._data.slice(written);
-    this._done = !this._data.length;
-  },
+RawPacket.prototype.write = function(stream) {
+  let written = stream.write(this._data, this._data.length);
+  this._data = this._data.slice(written);
+  this._done = !this._data.length;
+}
 
-  get done() { return this._done; }
-
+Object.defineProperty(RawPacket.prototype, "done", {
+  get: function() { return this._done; }
 });
 
 exports.RawPacket = RawPacket;