}
}
- out << indent() << "if (args != null) {" << endl;
+ out << indent() << "if (args) {" << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- out << indent() << indent() << "if (null != args." << (*m_iter)->get_name() << ") {" << endl
+ out << indent() << indent() << "if (!args." << (*m_iter)->get_name() << ") {" << endl
<< indent() << indent() << indent() << "this." << (*m_iter)->get_name() << " = args." << (*m_iter)->get_name() << ";" << endl
<< indent() << indent() << "}" << endl;
}
// Check for field STOP marker and break
- indent(out) << "if (ftype == Thrift.Type.STOP)" << endl;
+ indent(out) << "if (ftype == Thrift.Type.STOP) {" << endl;
indent_up();
indent(out) << "break;" << endl;
indent_down();
+ indent(out) << "}" << endl;
+ if (!fields.empty()) {
+ // Switch statement on the field we are reading
+ indent(out) << "switch (fid)" << endl;
- // Switch statement on the field we are reading
- indent(out) << "switch (fid)" << endl;
+ scope_up(out);
- scope_up(out);
+ // Generate deserialization code for known cases
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- // Generate deserialization code for known cases
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ indent(out) << "case " << (*f_iter)->get_key() << ":" << endl;
+ indent(out) << "if (ftype == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
- indent(out) << "case " << (*f_iter)->get_key() << ":" << endl;
- indent(out) << "if (ftype == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
+ indent_up();
+ generate_deserialize_field(out, *f_iter, "this.");
+ indent_down();
- indent_up();
- generate_deserialize_field(out, *f_iter, "this.");
- indent_down();
+ indent(out) << "} else {" << endl;
- indent(out) << "} else {" << endl;
+ indent(out) << " input.skip(ftype);" << endl;
- indent(out) << " input.skip(ftype);" << endl;
+ out <<
+ indent() << "}" << endl <<
+ indent() << "break;" << endl;
- out <<
- indent() << "}" << endl <<
- indent() << "break;" << endl;
+ }
+ if (fields.size() == 1) {
+ // pseudo case to make jslint happy
+ indent(out) << "case 0:" << endl;
+ indent(out) << " input.skip(ftype);" << endl;
+ indent(out) << " break;" << endl;
+ }
+ // In the default case we skip the field
+ indent(out) << "default:" << endl;
+ indent(out) << " input.skip(ftype);" << endl;
+ scope_down(out);
+ } else {
+ indent(out) << "input.skip(ftype);" << endl;
}
- // In the default case we skip the field
- indent(out) << "default:" << endl;
- indent(out) << " input.skip(ftype);" << endl;
-
- scope_down(out);
indent(out) << "input.readFieldEnd();" << endl;
indent(out) << "output.writeStructBegin('" << name << "');" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- out << indent() << "if (null != this." << (*f_iter)->get_name() << ") {" << endl;
+ out << indent() << "if (this." << (*f_iter)->get_name() << ") {" << endl;
indent_up();
indent(out) <<
} else {
f_service_ <<
indent() << " this.input = input;" << endl <<
- indent() << " this.output = null == output ? input : output;" << endl <<
+ indent() << " this.output = (!output) ? input : output;" << endl <<
indent() << " this.seqid = 0;" << endl;
}
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
f_service_ <<
- indent() << "if (null != result." << (*x_iter)->get_name() << ") {" << endl <<
+ indent() << "if (null !== result." << (*x_iter)->get_name() << ") {" << endl <<
indent() << " " << render_recv_throw("result." + (*x_iter)->get_name()) << endl <<
indent() << "}" << endl;
}
// Careful, only return result if not a void function
if (!(*f_iter)->get_returntype()->is_void()) {
f_service_ <<
- indent() << "if (null != result.success) {" << endl <<
+ indent() << "if (null !== result.success) {" << endl <<
indent() << " " << render_recv_return("result.success") << endl <<
indent() << "}" << endl;
f_service_ <<
string ktype = tmp("_ktype");
string vtype = tmp("_vtype");
string etype = tmp("_etype");
+ string rtmp3 = tmp("_rtmp3");
t_field fsize(g_type_i32, size);
t_field fktype(g_type_byte, ktype);
t_field fetype(g_type_byte, etype);
out << indent() << "var " << size << " = 0;" << endl;
- out << indent() << "var rtmp3;" << endl;
+ out << indent() << "var " << rtmp3 << ";" << endl;
// Declare variables, read header
indent() << "var " << ktype << " = 0;" << endl <<
indent() << "var " << vtype << " = 0;" << endl;
- out << indent() << "rtmp3 = input.readMapBegin();" << endl;
- out << indent() << ktype << " = rtmp3.ktype;" << endl;
- out << indent() << vtype << " = rtmp3.vtype;" << endl;
- out << indent() << size << " = rtmp3.size;" << endl;
+ out << indent() << rtmp3 << " = input.readMapBegin();" << endl;
+ out << indent() << ktype << " = " << rtmp3 << ".ktype;" << endl;
+ out << indent() << vtype << " = " << rtmp3 << ".vtype;" << endl;
+ out << indent() << size << " = " << rtmp3 << ".size;" << endl;
} else if (ttype->is_set()) {
out <<
indent() << prefix << " = [];" << endl <<
indent() << "var " << etype << " = 0;" << endl <<
- indent() << "rtmp3 = input.readSetBegin();" << endl <<
- indent() << etype << " = rtmp3.etype;"<<endl<<
- indent() << size << " = rtmp3.size;"<<endl;
+ indent() << rtmp3 << " = input.readSetBegin();" << endl <<
+ indent() << etype << " = " << rtmp3 << ".etype;"<<endl<<
+ indent() << size << " = " << rtmp3 << ".size;"<<endl;
} else if (ttype->is_list()) {
out <<
indent() << prefix << " = [];" << endl <<
indent() << "var " << etype << " = 0;" << endl <<
- indent() << "rtmp3 = input.readListBegin();" << endl <<
- indent() << etype << " = rtmp3.etype;"<<endl<<
- indent() << size << " = rtmp3.size;"<<endl;
+ indent() << rtmp3 << " = input.readListBegin();" << endl <<
+ indent() << etype << " = " << rtmp3 << ".etype;"<<endl<<
+ indent() << size << " = " << rtmp3 << ".size;"<<endl;
}
// For loop iterates over elements
objectLength: function(obj) {
var length = 0;
- for (k in obj)
- if (obj.hasOwnProperty(k))
+ for (var k in obj) {
+ if (obj.hasOwnProperty(k)) {
length++;
+ }
+ }
+
return length;
},
- inherits: function(constructor, superConstructor) {\r
+ inherits: function(constructor, superConstructor) {
//Prototypal Inheritance http://javascript.crockford.com/prototypal.html
function F() {}
F.prototype = superConstructor.prototype;
Thrift.TException.prototype = {
initialize: function(message, code) {
this.message = message;
- this.code = (code == null) ? 0 : code;
+ this.code = (code === null) ? 0 : code;
}
};
Thrift.TApplicationException = function(message, code) {
this.message = message;
- this.code = (code == null) ? 0 : code;
+ this.code = (code === null) ? 0 : code;
};
Thrift.TApplicationException.prototype = {
read: function(input) {
while (1) {
- ret = input.readFieldBegin();
+ var ret = input.readFieldBegin();
- if (ret.ftype == Thrift.Type.STOP)
+ if (ret.ftype == Thrift.Type.STOP) {
break;
+ }
var fid = ret.fid;
//Gets the browser specific XmlHttpRequest Object
getXmlHttpRequestObject: function() {
- try { return new XMLHttpRequest() } catch (e) {}
- try { return new ActiveXObject('Msxml2.XMLHTTP') } catch (e) {}
- try { return new ActiveXObject('Microsoft.XMLHTTP') } catch (e) {}
+ try { return new XMLHttpRequest(); } catch (e1) { }
+ try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e2) { }
+ try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e3) { }
throw "Your browser doesn't support the XmlHttpRequest object.";
flush: function() {
//async mode
- if (this.url == undefined || this.url == '')
+ if (this.url === undefined || this.url === '') {
return this.send_buf;
+ }
var xreq = this.getXmlHttpRequestObject();
- if (xreq.overrideMimeType)
+ if (xreq.overrideMimeType) {
xreq.overrideMimeType('application/json');
+ }
xreq.open('POST', this.url, false);
xreq.send(this.send_buf);
- if (xreq.readyState != 4)
+ if (xreq.readyState != 4) {
throw 'encountered an unknown ajax ready state: ' + xreq.readyState;
+ }
- if (xreq.status != 200)
+ if (xreq.status != 200) {
throw 'encountered a unknown request status: ' + xreq.status;
+ }
this.recv_buf = xreq.responseText;
this.recv_buf_sz = this.recv_buf.length;
read: function(len) {
var avail = this.wpos - this.rpos;
- if (avail == 0)
+ if (avail === 0) {
return '';
+ }
var give = len;
- if (avail < len)
+ if (avail < len) {
give = avail;
+ }
var ret = this.read_buf.substr(this.rpos, give);
this.rpos += give;
},
readAll: function() {
- return this.recv_buf;
+ return this.recv_buf;
},
write: function(buf) {
Thrift.Protocol.RType = {};
-Thrift.Protocol.RType['tf'] = Thrift.Type.BOOL;
-Thrift.Protocol.RType['i8'] = Thrift.Type.BYTE;
-Thrift.Protocol.RType['i16'] = Thrift.Type.I16;
-Thrift.Protocol.RType['i32'] = Thrift.Type.I32;
-Thrift.Protocol.RType['i64'] = Thrift.Type.I64;
-Thrift.Protocol.RType['dbl'] = Thrift.Type.DOUBLE;
-Thrift.Protocol.RType['rec'] = Thrift.Type.STRUCT;
-Thrift.Protocol.RType['str'] = Thrift.Type.STRING;
-Thrift.Protocol.RType['map'] = Thrift.Type.MAP;
-Thrift.Protocol.RType['lst'] = Thrift.Type.LIST;
-Thrift.Protocol.RType['set'] = Thrift.Type.SET;
+Thrift.Protocol.RType.tf = Thrift.Type.BOOL;
+Thrift.Protocol.RType.i8 = Thrift.Type.BYTE;
+Thrift.Protocol.RType.i16 = Thrift.Type.I16;
+Thrift.Protocol.RType.i32 = Thrift.Type.I32;
+Thrift.Protocol.RType.i64 = Thrift.Type.I64;
+Thrift.Protocol.RType.dbl = Thrift.Type.DOUBLE;
+Thrift.Protocol.RType.rec = Thrift.Type.STRUCT;
+Thrift.Protocol.RType.str = Thrift.Type.STRING;
+Thrift.Protocol.RType.map = Thrift.Type.MAP;
+Thrift.Protocol.RType.lst = Thrift.Type.LIST;
+Thrift.Protocol.RType.set = Thrift.Type.SET;
Thrift.Protocol.Version = 1;
//Write functions
writeMessageBegin: function(name, messageType, seqid) {
- this.tstack = new Array();
- this.tpos = new Array();\r
-\r
- this.tstack.push([Thrift.Protocol.Version, '"' +\r
+ this.tstack = [];
+ this.tpos = [];
+
+ this.tstack.push([Thrift.Protocol.Version, '"' +
name + '"', messageType, seqid]);
},
var str = '{';
var first = true;
for (var key in struct) {
- if (first)
+ if (first) {
first = false;
- else
+ } else {
str += ',';
+ }
str += key + ':' + struct[key];
}
},
writeFieldBegin: function(name, fieldType, fieldId) {
- this.tpos.push(this.tstack.length);\r
- this.tstack.push({ 'fieldId': '"' +\r
- fieldId + '"', 'fieldType': Thrift.Protocol.Type[fieldType]\r
+ this.tpos.push(this.tstack.length);
+ this.tstack.push({ 'fieldId': '"' +
+ fieldId + '"', 'fieldType': Thrift.Protocol.Type[fieldType]
});
},
writeFieldEnd: function() {
var value = this.tstack.pop();
- var fieldInfo = this.tstack.pop();\r
-\r
- this.tstack[this.tstack.length - 1][fieldInfo.fieldId] = '{' +\r
+ var fieldInfo = this.tstack.pop();
+
+ this.tstack[this.tstack.length - 1][fieldInfo.fieldId] = '{' +
fieldInfo.fieldType + ':' + value + '}';
this.tpos.pop();
},
writeMapBegin: function(keyType, valType, size) {
//size is invalid, we'll set it on end.
- this.tpos.push(this.tstack.length);\r
+ this.tpos.push(this.tstack.length);
this.tstack.push([Thrift.Protocol.Type[keyType],
Thrift.Protocol.Type[valType], 0]);
},
writeMapEnd: function() {
var p = this.tpos.pop();
- if (p == this.tstack.length)
+ if (p == this.tstack.length) {
return;
+ }
- if ((this.tstack.length - p - 1) % 2 != 0)
+ if ((this.tstack.length - p - 1) % 2 !== 0) {
this.tstack.push('');
+ }
var size = (this.tstack.length - p - 1) / 2;
var k = this.tstack.pop();
if (first) {
first = false;
- }else {
+ } else {
map = ',' + map;
}
// Reading functions
readMessageBegin: function(name, messageType, seqid) {
- this.rstack = new Array();
- this.rpos = new Array();
+ this.rstack = [];
+ this.rpos = [];
this.robj = eval(this.transport.readAll());
throw 'Wrong thrift protocol version: ' + version;
}
- r['fname'] = this.robj.shift();
- r['mtype'] = this.robj.shift();
- r['rseqid'] = this.robj.shift();
+ r.fname = this.robj.shift();
+ r.mtype = this.robj.shift();
+ r.rseqid = this.robj.shift();
//get to the main obj
readStructBegin: function(name) {
var r = {};
- r['fname'] = '';
+ r.fname = '';
//incase this is an array of structs
- if (this.rstack[this.rstack.length - 1] instanceof Array)
+ if (this.rstack[this.rstack.length - 1] instanceof Array) {
this.rstack.push(this.rstack[this.rstack.length - 1].shift());
+ }
return r;
},
readStructEnd: function() {
- if (this.rstack[this.rstack.length - 2] instanceof Array)
+ if (this.rstack[this.rstack.length - 2] instanceof Array) {
this.rstack.pop();
+ }
},
readFieldBegin: function() {
//get a fieldId
for (var f in (this.rstack[this.rstack.length - 1])) {
- if (f == null) continue;
+ if (f === null) {
+ continue;
+ }
- fid = parseInt(f);
+ fid = parseInt(f, 10);
this.rpos.push(this.rstack.length);
- var field = this.rstack[this.rstack.length - 1][fid]
+ var field = this.rstack[this.rstack.length - 1][fid];
//remove so we don't see it again
delete this.rstack[this.rstack.length - 1][fid];
//should only be 1 of these but this is the only
//way to match a key
- for (var f in (this.rstack[this.rstack.length - 1])) {
- if (Thrift.Protocol.RType[f] == null) continue;
+ for (var i in (this.rstack[this.rstack.length - 1])) {
+ if (Thrift.Protocol.RType[i] === null) {
+ continue;
+ }
- ftype = Thrift.Protocol.RType[f];\r
+ ftype = Thrift.Protocol.RType[i];
this.rstack[this.rstack.length - 1] =
- this.rstack[this.rstack.length - 1][f];
+ this.rstack[this.rstack.length - 1][i];
}
}
- r['fname'] = '';
- r['ftype'] = ftype;
- r['fid'] = fid;
+ r.fname = '';
+ r.ftype = ftype;
+ r.fid = fid;
return r;
var pos = this.rpos.pop();
//get back to the right place in the stack
- while (this.rstack.length > pos)
+ while (this.rstack.length > pos) {
this.rstack.pop();
+ }
},
var map = this.rstack.pop();
var r = {};
- r['ktype'] = Thrift.Protocol.RType[map.shift()];
- r['vtype'] = Thrift.Protocol.RType[map.shift()];
- r['size'] = map.shift();
+ r.ktype = Thrift.Protocol.RType[map.shift()];
+ r.vtype = Thrift.Protocol.RType[map.shift()];
+ r.size = map.shift();
this.rpos.push(this.rstack.length);
var list = this.rstack[this.rstack.length - 1];
var r = {};
- r['etype'] = Thrift.Protocol.RType[list.shift()];
- r['size'] = list.shift();
+ r.etype = Thrift.Protocol.RType[list.shift()];
+ r.size = list.shift();
this.rpos.push(this.rstack.length);
readBool: function() {
var r = this.readI32();
- if (r != null && r['value'] == '1') {
- r['value'] = true;
- }else {
- r['value'] = false;
+ if (r !== null && r.value == '1') {
+ r.value = true;
+ } else {
+ r.value = false;
}
return r;
readI32: function(f) {
- if (f == undefined)
+ if (f === undefined) {
f = this.rstack[this.rstack.length - 1];
+ }
var r = {};
if (f instanceof Array) {
- if (f.length == 0)
- r['value'] = undefined;
- else
- r['value'] = f.shift();
-
- }else if (f instanceof Object) {
+ if (f.length === 0) {
+ r.value = undefined;
+ } else {
+ r.value = f.shift();
+ }
+ } else if (f instanceof Object) {
for (var i in f) {
- if (i == null) continue;
- this.rstack.push(f[i])
+ if (i === null) {
+ continue;
+ }
+ this.rstack.push(f[i]);
delete f[i];
- r['value'] = i;
+ r.value = i;
break;
}
} else {
- r['value'] = f;
+ r.value = f;
this.rstack.pop();
}