Simplify arg parsing in String.write
authorRyan Dahl <ry@tinyclouds.org>
Thu, 6 Oct 2011 23:57:35 +0000 (16:57 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Fri, 7 Oct 2011 20:38:23 +0000 (13:38 -0700)
lib/net_uv.js

index 6fb09342303f74ab5d37ac3713770616c7549a31..29a23519831ac2cb977943218bd4707ca6d5da9e 100644 (file)
@@ -363,29 +363,26 @@ Socket.prototype.__defineGetter__('remotePort', function() {
 });
 
 
-Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {
-  var encoding, fd, cb;
+/*
+ * Arguments data, [encoding], [cb]
+ */
+Socket.prototype.write = function(data, arg1, arg2) {
+  var encoding, cb;
 
   // parse arguments
-  if (typeof arguments[3] == 'function') {
-    cb = arguments[3];
-    fd = arguments[2];
-    encoding = arguments[1];
-  } else if (typeof arguments[2] == 'function') {
-    cb = arguments[2];
-    if (typeof arguments[1] == 'number') {
-      fd = arguments[1];
-    } else {
-      encoding = arguments[1];
-    }
-  } else if (typeof arguments[1] == 'function') {
-    cb = arguments[1];
-  } else {
-    if (typeof arguments[1] == 'number') {
-      fd = arguments[1];
-    } else {
-      encoding = arguments[1];
-      fd = arguments[2];
+  if (arg1) {
+    switch (typeof arg1) {
+      case 'string':
+        encoding = arg1;
+        cb = arg2;
+        break;
+
+      case 'function':
+        cb = arg1;
+        break;
+
+      default:
+        throw new Error("bad arg");
     }
   }
 
@@ -400,9 +397,9 @@ Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {
   if (this._connecting) {
     this._connectQueueSize += data.length;
     if (this._connectQueue) {
-      this._connectQueue.push([data, encoding, fd, cb]);
+      this._connectQueue.push([data, encoding, cb]);
     } else {
-      this._connectQueue = [[data, encoding, fd, cb]];
+      this._connectQueue = [[data, encoding, cb]];
     }
     return false;
   }