weechat-protocol.js: refactor with prototype

This commit is contained in:
Philippe Proulx 2013-10-05 15:01:51 -04:00
parent 9bdcb814ab
commit 3a9ccc4098

View file

@ -1,23 +1,43 @@
var WeeChatProtocol = function() { var WeeChatProtocol = function() {
var self = this; this.types = {
'chr': this.getChar,
'int': this.getInt,
'str': this.getString,
'inf': this.getInfo,
'hda': this.getHdata,
'ptr': this.getPointer,
'lon': this.getPointer,
'tim': this.getPointer,
'buf': this.getString,
'arr': this.getArray
};
};
WeeChatProtocol._uiatos = function(uia) {
var _str = [];
for (var c = 0; c < uia.length; c++) {
_str[c] = String.fromCharCode(uia[c]);
}
var getInfo = function() { return decodeURIComponent(escape(_str.join("")));
};
WeeChatProtocol.prototype = {
getInfo: function() {
var info = {}; var info = {};
info.key = getString(); info.key = this.getString();
info.value = getString(); info.value = this.getString();
return info; return info;
}; },
getHdata: function() {
var getHdata = function() { var self = this;
var paths; var paths;
var count; var count;
var objs = []; var objs = [];
var hpath = getString(); var hpath = this.getString();
keys = getString().split(','); keys = this.getString().split(',');
paths = hpath.split('/'); paths = hpath.split('/');
count = getInt(); count = this.getInt();
keys = keys.map(function(key) { keys = keys.map(function(key) {
return key.split(':'); return key.split(':');
@ -27,116 +47,101 @@ var WeeChatProtocol = function() {
var tmp = {}; var tmp = {};
tmp.pointers = paths.map(function(path) { tmp.pointers = paths.map(function(path) {
return getPointer(); return self.getPointer();
}); });
keys.forEach(function(key) { keys.forEach(function(key) {
tmp[key[0]] = runType(key[1]); tmp[key[0]] = self.runType(key[1]);
}); });
objs.push(tmp); objs.push(tmp);
}; };
return objs; return objs;
}; },
getPointer: function() {
function getPointer() { var l = this.getChar();
var l = getChar(); var pointer = this.getSlice(l)
var pointer = getSlice(l)
var parsed_data = new Uint8Array(pointer); var parsed_data = new Uint8Array(pointer);
return _uiatos(parsed_data); return WeeChatProtocol._uiatos(parsed_data);
}; },
getInt: function() {
var parsed_data = new Uint8Array(this.getSlice(4));
var _uiatos = function(uia) { return ((parsed_data[0] & 0xff) << 24) |
var _str = []; ((parsed_data[1] & 0xff) << 16) |
for (var c = 0; c < uia.length; c++) { ((parsed_data[2] & 0xff) << 8) |
_str[c] = String.fromCharCode(uia[c]); (parsed_data[3] & 0xff);
} },
getChar: function() {
return decodeURIComponent(escape(_str.join(""))); var parsed_data = new Uint8Array(this.getSlice(1));
};
var getInt = function() {
var parsed_data = new Uint8Array(getSlice(4));
var i = ((parsed_data[0] & 0xff) << 24) | ((parsed_data[1] & 0xff) << 16) | ((parsed_data[2] & 0xff) << 8) | (parsed_data[3] & 0xff);
return i;
};
var getChar = function() {
var parsed_data = new Uint8Array(getSlice(1));
return parsed_data[0]; return parsed_data[0];
}; },
getString: function() {
var getString = function() { var l = this.getInt();
var l = getInt();
if (l > 0) { if (l > 0) {
var s = getSlice(l); var s = this.getSlice(l);
var parsed_data = new Uint8Array(s); var parsed_data = new Uint8Array(s);
return _uiatos(parsed_data); return WeeChatProtocol._uiatos(parsed_data);
} }
return ""; return "";
}; },
getSlice: function(length) {
var slice = this.data.slice(0,length);
var getSlice = function(length) { this.data = this.data.slice(length);
var slice = self.data.slice(0,length);
self.data = self.data.slice(length);
return slice; return slice;
}; },
getType: function() {
var t = this.getSlice(3);
var getType = function() { return WeeChatProtocol._uiatos(new Uint8Array(t));
var t = getSlice(3); },
runType: function(type) {
var cb = this.types[type];
var boundCb = cb.bind(this);
return _uiatos(new Uint8Array(t)); return boundCb();
}; },
getHeader: function() {
var runType = function(type) { var len = this.getInt();
if (type in types) { var comp = this.getChar();
return types[type]();
}
};
var getHeader = function() {
var len = getInt();
var comp = getChar();
return { return {
length: len, length: len,
compression: comp, compression: comp,
}; };
}; },
getId: function() {
var getId = function() { return this.getString();
return getString(); },
} getObject: function() {
var self = this;
var getObject = function() { var type = this.getType();
var type = getType();
if (type) { if (type) {
return object = { return object = {
type: type, type: type,
content: runType(type), content: self.runType(type),
}
} }
} }
},
parse: function(data) {
var self = this;
this.setData(data);
self.parse = function(data) { var header = this.getHeader();
self.setData(data); var id = this.getId();
var header = getHeader();
var id = getId();
var objects = []; var objects = [];
var object = getObject(); var object = this.getObject();
while (object) { while (object) {
objects.push(object); objects.push(object);
object = getObject(); object = self.getObject();
} }
return { return {
@ -144,51 +149,24 @@ var WeeChatProtocol = function() {
id: id, id: id,
objects: objects, objects: objects,
}; };
} },
setData: function (data) {
self.setData = function (data) { this.data = data;
self.data = data; },
}; getArray: function() {
var self = this;
function array() {
var type; var type;
var count; var count;
var values; var values;
type = getType(); type = this.getType();
count = getInt(); count = this.getInt();
values = []; values = [];
for (var i = 0; i < count; i++) { for (var i = 0; i < count; i++) {
values.push(runType(type)); values.push(self.runType(type));
}; };
return values; return values;
} }
var types = {
chr: getChar,
"int": getInt,
str: getString,
inf: getInfo,
hda: getHdata,
ptr: getPointer,
lon: getPointer,
tim: getPointer,
buf: getString,
arr: array
};
//TODO: IMPLEMENT THIS STUFF
// chr: this.getChar,
// 'int': getInt,
// hacks
// hacks
// htb: getHashtable,
// inf: Protocol.getInfo,
// inl: getInfolist,
// },
}; };