Skip to content

Commit 55a991e

Browse files
committed
Revert "Jon remove function constructor"
1 parent 0cd0a5b commit 55a991e

File tree

1 file changed

+34
-72
lines changed

1 file changed

+34
-72
lines changed

lib/connection.js

Lines changed: 34 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,29 @@ function Connection() {
3333
this.on("pipe", function(source) {
3434
that.stream = source;
3535
});
36-
37-
var self = this;
38-
39-
Object.keys(protocol.types).forEach(function(k) {
40-
var v = protocol.types[k];
41-
self[v] = function shim_generator(opts) {
42-
var p = generate[v](opts);
43-
if (p instanceof Error) {
44-
self.emit('error', p)
45-
} else {
46-
self.stream.write(p);
47-
}
48-
};
49-
});
5036
};
51-
5237
util.inherits(Connection, Writable);
5338

5439
Connection.prototype.setPacketEncoding = function (encoding) {
5540
this._packetEncoding = encoding;
5641
return this;
5742
};
5843

44+
for (var k in protocol.types) {
45+
var v = protocol.types[k];
46+
47+
var fun = "" +
48+
" var p = this.generate." + v +"(opts); " +
49+
" if (p instanceof Error) { " +
50+
" this.emit('error', p) " +
51+
" } else { " +
52+
" this.stream.write(p); " +
53+
" } "
54+
" } ";
55+
56+
Connection.prototype[v] = new Function("opts", fun);
57+
}
58+
5959
Connection.prototype._newPacket = function() {
6060
this.packet = {};
6161
this.tmp = { pos: 1, mul: 1, length: 0};
@@ -138,64 +138,26 @@ Connection.prototype._readPayload = function() {
138138
};
139139

140140
(function() {
141+
var v = protocol.types[k];
142+
143+
var fun = "" +
144+
" 'use strict'; \n" +
145+
" var buf = this.data.slice(this.index, this.index + this.packet.length); \n" +
146+
" var result = null; \n" +
147+
" this.index += this.packet.length; \n" +
148+
" switch(this.packet.cmd) { \n";
149+
150+
Object.keys(parse).forEach(function(key) {
151+
fun = fun +
152+
" case '" + key + "': \n" +
153+
" result = parse." + key + "(buf, this.packet, this._packetEncoding); \n" +
154+
" break; \n ";
155+
});
141156

142-
Connection.prototype._parsePayload = function() {
143-
var buf = this.data.slice(this.index, this.index + this.packet.length);
144-
var result = null;
145-
this.index += this.packet.length;
146-
var cmd = this.packet.cmd;
147-
switch(cmd) {
148-
case protocol.types[0]:
149-
result = parse[cmd](buf, this.packet, this._packetEncoding);
150-
break;
151-
case protocol.types[1]:
152-
result = parse[cmd](buf, this.packet, this._packetEncoding);
153-
break;
154-
case protocol.types[2]:
155-
result = parse[cmd](buf, this.packet, this._packetEncoding);
156-
break;
157-
case protocol.types[3]:
158-
result = parse[cmd](buf, this.packet, this._packetEncoding);
159-
break;
160-
case protocol.types[4]:
161-
result = parse[cmd](buf, this.packet, this._packetEncoding);
162-
break;
163-
case protocol.types[5]:
164-
result = parse[cmd](buf, this.packet, this._packetEncoding);
165-
break;
166-
case protocol.types[6]:
167-
result = parse[cmd](buf, this.packet, this._packetEncoding);
168-
break;
169-
case protocol.types[7]:
170-
result = parse[cmd](buf, this.packet, this._packetEncoding);
171-
break;
172-
case protocol.types[8]:
173-
result = parse[cmd](buf, this.packet, this._packetEncoding);
174-
break;
175-
case protocol.types[9]:
176-
result = parse[cmd](buf, this.packet, this._packetEncoding);
177-
break;
178-
case protocol.types[10]:
179-
result = parse[cmd](buf, this.packet, this._packetEncoding);
180-
break;
181-
case protocol.types[11]:
182-
result = parse[cmd](buf, this.packet, this._packetEncoding);
183-
break;
184-
case protocol.types[12]:
185-
result = parse[cmd](buf, this.packet, this._packetEncoding);
186-
break;
187-
case protocol.types[13]:
188-
result = parse[cmd](buf, this.packet, this._packetEncoding);
189-
break;
190-
case protocol.types[14]:
191-
result = parse[cmd](buf, this.packet, this._packetEncoding);
192-
break;
193-
case protocol.types[15]:
194-
result = parse[cmd](buf, this.packet, this._packetEncoding);
195-
break;
196-
}
197-
return result;
198-
}
157+
fun += "} \n";
158+
fun += "return result; \n";
159+
160+
Connection.prototype._parsePayload = new Function("parse", fun);
199161
})();
200162

201163
Connection.prototype._write = function(data, encoding, done) {

0 commit comments

Comments
 (0)