blob: 4f828cb93ecc20a374531937d06ffbbda2736b08 [file] [log] [blame]
T Jake Luciani322caa22010-02-15 03:24:55 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19var Thrift = {
20
Roger Meierb4bcbe32011-03-07 19:37:46 +000021 Type: {
22 'STOP' : 0,
23 'VOID' : 1,
24 'BOOL' : 2,
25 'BYTE' : 3,
26 'I08' : 3,
27 'DOUBLE' : 4,
28 'I16' : 6,
29 'I32' : 8,
30 'I64' : 10,
31 'STRING' : 11,
32 'UTF7' : 11,
33 'STRUCT' : 12,
34 'MAP' : 13,
35 'SET' : 14,
36 'LIST' : 15,
37 'UTF8' : 16,
38 'UTF16' : 17
T Jake Luciani322caa22010-02-15 03:24:55 +000039 },
40
Roger Meierb4bcbe32011-03-07 19:37:46 +000041 MessageType: {
42 'CALL' : 1,
43 'REPLY' : 2,
44 'EXCEPTION' : 3
Roger Meier55ea68f2011-02-16 19:29:50 +000045 },
Roger Meierb4bcbe32011-03-07 19:37:46 +000046
47 objectLength: function(obj) {
48 var length = 0;
Roger Meier55ea68f2011-02-16 19:29:50 +000049 for (k in obj)
50 if (obj.hasOwnProperty(k))
Roger Meierb4bcbe32011-03-07 19:37:46 +000051 length++;
52 return length;
Roger Meier55ea68f2011-02-16 19:29:50 +000053 },
54
Roger Meierb4bcbe32011-03-07 19:37:46 +000055 inherits: function(constructor, superConstructor) {
56 //Prototypal Inheritance http://javascript.crockford.com/prototypal.html
57 function F() {}
58 F.prototype = superConstructor.prototype;
59 constructor.prototype = new F();
T Jake Luciani322caa22010-02-15 03:24:55 +000060 }
Roger Meierb4bcbe32011-03-07 19:37:46 +000061};
T Jake Luciani322caa22010-02-15 03:24:55 +000062
Roger Meier55ea68f2011-02-16 19:29:50 +000063
Roger Meierb4bcbe32011-03-07 19:37:46 +000064Thrift.TException = {};
65Thrift.TException.prototype = {
66 initialize: function(message, code) {
T Jake Luciani322caa22010-02-15 03:24:55 +000067 this.message = message;
Roger Meierb4bcbe32011-03-07 19:37:46 +000068 this.code = (code == null) ? 0 : code;
T Jake Luciani322caa22010-02-15 03:24:55 +000069 }
Roger Meierb4bcbe32011-03-07 19:37:46 +000070};
T Jake Luciani322caa22010-02-15 03:24:55 +000071
72
T Jake Lucianiefabb892010-07-28 22:31:12 +000073Thrift.TApplicationExceptionType = {
Roger Meierb4bcbe32011-03-07 19:37:46 +000074 'UNKNOWN' : 0,
75 'UNKNOWN_METHOD' : 1,
76 'INVALID_MESSAGE_TYPE' : 2,
77 'WRONG_METHOD_NAME' : 3,
78 'BAD_SEQUENCE_ID' : 4,
79 'MISSING_RESULT' : 5
80};
T Jake Luciani322caa22010-02-15 03:24:55 +000081
Roger Meierb4bcbe32011-03-07 19:37:46 +000082Thrift.TApplicationException = function(message, code) {
83 this.message = message;
84 this.code = (code == null) ? 0 : code;
85};
T Jake Luciani322caa22010-02-15 03:24:55 +000086
Roger Meierb4bcbe32011-03-07 19:37:46 +000087Thrift.TApplicationException.prototype = {
88
89 read: function(input) {
90 while (1) {
91 ret = input.readFieldBegin();
92
93 if (ret.ftype == Thrift.Type.STOP)
94 break;
95
96 var fid = ret.fid;
97
98 switch (fid) {
99 case 1:
100 if (ret.ftype == Thrift.Type.STRING) {
101 ret = input.readString();
102 this.message = ret.value;
T Jake Luciani322caa22010-02-15 03:24:55 +0000103 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000104 ret = input.skip(ret.ftype);
T Jake Luciani322caa22010-02-15 03:24:55 +0000105 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000106
107 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000108 case 2:
Roger Meierb4bcbe32011-03-07 19:37:46 +0000109 if (ret.ftype == Thrift.Type.I32) {
110 ret = input.readI32();
111 this.code = ret.value;
T Jake Luciani322caa22010-02-15 03:24:55 +0000112 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000113 ret = input.skip(ret.ftype);
T Jake Luciani322caa22010-02-15 03:24:55 +0000114 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000115 break;
116
T Jake Luciani322caa22010-02-15 03:24:55 +0000117 default:
Roger Meierb4bcbe32011-03-07 19:37:46 +0000118 ret = input.skip(ret.ftype);
119 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000120 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000121
122 input.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000123 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000124
125 input.readStructEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000126 },
Roger Meierb4bcbe32011-03-07 19:37:46 +0000127
128 write: function(output) {
129 var xfer = 0;
130
T Jake Luciani322caa22010-02-15 03:24:55 +0000131 output.writeStructBegin('TApplicationException');
Roger Meierb4bcbe32011-03-07 19:37:46 +0000132
T Jake Luciani322caa22010-02-15 03:24:55 +0000133 if (this.message) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000134 output.writeFieldBegin('message', Thrift.Type.STRING, 1);
135 output.writeString(this.getMessage());
136 output.writeFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000137 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000138
T Jake Luciani322caa22010-02-15 03:24:55 +0000139 if (this.code) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000140 output.writeFieldBegin('type', Thrift.Type.I32, 2);
141 output.writeI32(this.code);
142 output.writeFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000143 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000144
145 output.writeFieldStop();
146 output.writeStructEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000147 },
Roger Meierb4bcbe32011-03-07 19:37:46 +0000148
149 getCode: function() {
150 return this.code;
T Jake Luciani322caa22010-02-15 03:24:55 +0000151 },
Roger Meierb4bcbe32011-03-07 19:37:46 +0000152
153 getMessage: function() {
154 return this.message;
T Jake Luciani322caa22010-02-15 03:24:55 +0000155 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000156};
T Jake Luciani322caa22010-02-15 03:24:55 +0000157
158
159
160/**
161 *If you do not specify a url then you must handle ajax on your own.
162 *This is how to use js bindings in a async fashion.
163 */
Roger Meierb4bcbe32011-03-07 19:37:46 +0000164Thrift.Transport = function(url) {
165 this.url = url;
166 this.wpos = 0;
167 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000168
Roger Meierb4bcbe32011-03-07 19:37:46 +0000169 this.send_buf = '';
170 this.recv_buf = '';
171};
T Jake Luciani322caa22010-02-15 03:24:55 +0000172
173Thrift.Transport.prototype = {
174
175 //Gets the browser specific XmlHttpRequest Object
Roger Meierb4bcbe32011-03-07 19:37:46 +0000176 getXmlHttpRequestObject: function() {
T Jake Luciani322caa22010-02-15 03:24:55 +0000177
Roger Meierb4bcbe32011-03-07 19:37:46 +0000178 try { return new XMLHttpRequest() } catch (e) {}
179 try { return new ActiveXObject('Msxml2.XMLHTTP') } catch (e) {}
180 try { return new ActiveXObject('Microsoft.XMLHTTP') } catch (e) {}
181
182 throw "Your browser doesn't support the XmlHttpRequest object.";
183
T Jake Luciani322caa22010-02-15 03:24:55 +0000184 },
185
Roger Meierb4bcbe32011-03-07 19:37:46 +0000186 flush: function() {
T Jake Luciani322caa22010-02-15 03:24:55 +0000187
188 //async mode
Roger Meierb4bcbe32011-03-07 19:37:46 +0000189 if (this.url == undefined || this.url == '')
T Jake Luciani322caa22010-02-15 03:24:55 +0000190 return this.send_buf;
191
Roger Meierb4bcbe32011-03-07 19:37:46 +0000192 var xreq = this.getXmlHttpRequestObject();
193
T Jake Luciani322caa22010-02-15 03:24:55 +0000194 if (xreq.overrideMimeType)
Roger Meierb4bcbe32011-03-07 19:37:46 +0000195 xreq.overrideMimeType('application/json');
196
197 xreq.open('POST', this.url, false);
198 xreq.send(this.send_buf);
199
T Jake Luciani322caa22010-02-15 03:24:55 +0000200 if (xreq.readyState != 4)
Roger Meierb4bcbe32011-03-07 19:37:46 +0000201 throw 'encountered an unknown ajax ready state: ' + xreq.readyState;
202
T Jake Luciani322caa22010-02-15 03:24:55 +0000203 if (xreq.status != 200)
Roger Meierb4bcbe32011-03-07 19:37:46 +0000204 throw 'encountered a unknown request status: ' + xreq.status;
T Jake Luciani322caa22010-02-15 03:24:55 +0000205
Roger Meierb4bcbe32011-03-07 19:37:46 +0000206 this.recv_buf = xreq.responseText;
207 this.recv_buf_sz = this.recv_buf.length;
208 this.wpos = this.recv_buf.length;
209 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000210 },
211
Roger Meierb4bcbe32011-03-07 19:37:46 +0000212 setRecvBuffer: function(buf) {
213 this.recv_buf = buf;
214 this.recv_buf_sz = this.recv_buf.length;
215 this.wpos = this.recv_buf.length;
216 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000217 },
218
Roger Meierb4bcbe32011-03-07 19:37:46 +0000219 isOpen: function() {
220 return true;
T Jake Luciani322caa22010-02-15 03:24:55 +0000221 },
222
Roger Meierb4bcbe32011-03-07 19:37:46 +0000223 open: function() {},
T Jake Luciani322caa22010-02-15 03:24:55 +0000224
225 close: function() {},
226
Roger Meierb4bcbe32011-03-07 19:37:46 +0000227 read: function(len) {
228 var avail = this.wpos - this.rpos;
T Jake Luciani322caa22010-02-15 03:24:55 +0000229
Roger Meierb4bcbe32011-03-07 19:37:46 +0000230 if (avail == 0)
231 return '';
T Jake Luciani322caa22010-02-15 03:24:55 +0000232
Roger Meierb4bcbe32011-03-07 19:37:46 +0000233 var give = len;
T Jake Luciani322caa22010-02-15 03:24:55 +0000234
Roger Meierb4bcbe32011-03-07 19:37:46 +0000235 if (avail < len)
236 give = avail;
237
238 var ret = this.read_buf.substr(this.rpos, give);
239 this.rpos += give;
T Jake Luciani322caa22010-02-15 03:24:55 +0000240
241 //clear buf when complete?
Roger Meierb4bcbe32011-03-07 19:37:46 +0000242 return ret;
T Jake Luciani322caa22010-02-15 03:24:55 +0000243 },
244
Roger Meierb4bcbe32011-03-07 19:37:46 +0000245 readAll: function() {
246 return this.recv_buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000247 },
248
Roger Meierb4bcbe32011-03-07 19:37:46 +0000249 write: function(buf) {
250 this.send_buf = buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000251 },
252
Roger Meierb4bcbe32011-03-07 19:37:46 +0000253 getSendBuffer: function() {
254 return this.send_buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000255 }
256
Roger Meierb4bcbe32011-03-07 19:37:46 +0000257};
T Jake Luciani322caa22010-02-15 03:24:55 +0000258
259
260
Roger Meierb4bcbe32011-03-07 19:37:46 +0000261Thrift.Protocol = function(transport) {
262 this.transport = transport;
263};
T Jake Luciani322caa22010-02-15 03:24:55 +0000264
Roger Meierb4bcbe32011-03-07 19:37:46 +0000265Thrift.Protocol.Type = {};
266Thrift.Protocol.Type[Thrift.Type.BOOL] = '"tf"';
267Thrift.Protocol.Type[Thrift.Type.BYTE] = '"i8"';
268Thrift.Protocol.Type[Thrift.Type.I16] = '"i16"';
269Thrift.Protocol.Type[Thrift.Type.I32] = '"i32"';
270Thrift.Protocol.Type[Thrift.Type.I64] = '"i64"';
271Thrift.Protocol.Type[Thrift.Type.DOUBLE] = '"dbl"';
272Thrift.Protocol.Type[Thrift.Type.STRUCT] = '"rec"';
273Thrift.Protocol.Type[Thrift.Type.STRING] = '"str"';
274Thrift.Protocol.Type[Thrift.Type.MAP] = '"map"';
275Thrift.Protocol.Type[Thrift.Type.LIST] = '"lst"';
276Thrift.Protocol.Type[Thrift.Type.SET] = '"set"';
T Jake Luciani322caa22010-02-15 03:24:55 +0000277
278
Roger Meierb4bcbe32011-03-07 19:37:46 +0000279Thrift.Protocol.RType = {};
280Thrift.Protocol.RType['tf'] = Thrift.Type.BOOL;
281Thrift.Protocol.RType['i8'] = Thrift.Type.BYTE;
282Thrift.Protocol.RType['i16'] = Thrift.Type.I16;
283Thrift.Protocol.RType['i32'] = Thrift.Type.I32;
284Thrift.Protocol.RType['i64'] = Thrift.Type.I64;
285Thrift.Protocol.RType['dbl'] = Thrift.Type.DOUBLE;
286Thrift.Protocol.RType['rec'] = Thrift.Type.STRUCT;
287Thrift.Protocol.RType['str'] = Thrift.Type.STRING;
288Thrift.Protocol.RType['map'] = Thrift.Type.MAP;
289Thrift.Protocol.RType['lst'] = Thrift.Type.LIST;
290Thrift.Protocol.RType['set'] = Thrift.Type.SET;
T Jake Luciani322caa22010-02-15 03:24:55 +0000291
Roger Meierb4bcbe32011-03-07 19:37:46 +0000292Thrift.Protocol.Version = 1;
T Jake Luciani322caa22010-02-15 03:24:55 +0000293
294Thrift.Protocol.prototype = {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000295
296 getTransport: function() {
297 return this.transport;
T Jake Luciani322caa22010-02-15 03:24:55 +0000298 },
299
300 //Write functions
Roger Meierb4bcbe32011-03-07 19:37:46 +0000301 writeMessageBegin: function(name, messageType, seqid) {
302 this.tstack = new Array();
303 this.tpos = new Array();
304
305 this.tstack.push([Thrift.Protocol.Version, '"' +
306 name + '"', messageType, seqid]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000307 },
308
Roger Meierb4bcbe32011-03-07 19:37:46 +0000309 writeMessageEnd: function() {
310 var obj = this.tstack.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000311
Roger Meierb4bcbe32011-03-07 19:37:46 +0000312 this.wobj = this.tstack.pop();
313 this.wobj.push(obj);
314
315 this.wbuf = '[' + this.wobj.join(',') + ']';
316
317 this.transport.write(this.wbuf);
T Jake Luciani322caa22010-02-15 03:24:55 +0000318 },
319
320
Roger Meierb4bcbe32011-03-07 19:37:46 +0000321 writeStructBegin: function(name) {
322 this.tpos.push(this.tstack.length);
323 this.tstack.push({});
T Jake Luciani322caa22010-02-15 03:24:55 +0000324 },
325
Roger Meierb4bcbe32011-03-07 19:37:46 +0000326 writeStructEnd: function() {
327
328 var p = this.tpos.pop();
329 var struct = this.tstack[p];
330 var str = '{';
331 var first = true;
332 for (var key in struct) {
333 if (first)
T Jake Luciani322caa22010-02-15 03:24:55 +0000334 first = false;
335 else
Roger Meierb4bcbe32011-03-07 19:37:46 +0000336 str += ',';
T Jake Luciani322caa22010-02-15 03:24:55 +0000337
Roger Meierb4bcbe32011-03-07 19:37:46 +0000338 str += key + ':' + struct[key];
339 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000340
Roger Meierb4bcbe32011-03-07 19:37:46 +0000341 str += '}';
T Jake Luciani322caa22010-02-15 03:24:55 +0000342 this.tstack[p] = str;
343 },
344
Roger Meierb4bcbe32011-03-07 19:37:46 +0000345 writeFieldBegin: function(name, fieldType, fieldId) {
346 this.tpos.push(this.tstack.length);
347 this.tstack.push({ 'fieldId': '"' +
348 fieldId + '"', 'fieldType': Thrift.Protocol.Type[fieldType]
349 });
350
T Jake Luciani322caa22010-02-15 03:24:55 +0000351 },
352
Roger Meierb4bcbe32011-03-07 19:37:46 +0000353 writeFieldEnd: function() {
354 var value = this.tstack.pop();
355 var fieldInfo = this.tstack.pop();
356
357 this.tstack[this.tstack.length - 1][fieldInfo.fieldId] = '{' +
358 fieldInfo.fieldType + ':' + value + '}';
359 this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000360 },
361
Roger Meierb4bcbe32011-03-07 19:37:46 +0000362 writeFieldStop: function() {
T Jake Luciani322caa22010-02-15 03:24:55 +0000363 //na
364 },
365
Roger Meierb4bcbe32011-03-07 19:37:46 +0000366 writeMapBegin: function(keyType, valType, size) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000367 //size is invalid, we'll set it on end.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000368 this.tpos.push(this.tstack.length);
369 this.tstack.push([Thrift.Protocol.Type[keyType],
370 Thrift.Protocol.Type[valType], 0]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000371 },
372
Roger Meierb4bcbe32011-03-07 19:37:46 +0000373 writeMapEnd: function() {
374 var p = this.tpos.pop();
375
376 if (p == this.tstack.length)
T Jake Luciani322caa22010-02-15 03:24:55 +0000377 return;
T Jake Luciani322caa22010-02-15 03:24:55 +0000378
Roger Meierb4bcbe32011-03-07 19:37:46 +0000379 if ((this.tstack.length - p - 1) % 2 != 0)
380 this.tstack.push('');
T Jake Luciani322caa22010-02-15 03:24:55 +0000381
Roger Meierb4bcbe32011-03-07 19:37:46 +0000382 var size = (this.tstack.length - p - 1) / 2;
383
384 this.tstack[p][this.tstack[p].length - 1] = size;
385
386 var map = '}';
387 var first = true;
388 while (this.tstack.length > p + 1) {
389 var v = this.tstack.pop();
390 var k = this.tstack.pop();
391 if (first) {
392 first = false;
393 }else {
394 map = ',' + map;
T Jake Luciani322caa22010-02-15 03:24:55 +0000395 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000396
397 map = '"' + k + '":' + v + map;
T Jake Luciani322caa22010-02-15 03:24:55 +0000398 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000399 map = '{' + map;
400
401 this.tstack[p].push(map);
402 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000403 },
404
Roger Meierb4bcbe32011-03-07 19:37:46 +0000405 writeListBegin: function(elemType, size) {
406 this.tpos.push(this.tstack.length);
407 this.tstack.push([Thrift.Protocol.Type[elemType], size]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000408 },
409
Roger Meierb4bcbe32011-03-07 19:37:46 +0000410 writeListEnd: function() {
411 var p = this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000412
Roger Meierb4bcbe32011-03-07 19:37:46 +0000413 while (this.tstack.length > p + 1) {
414 var tmpVal = this.tstack[p + 1];
415 this.tstack.splice(p + 1, 1);
416 this.tstack[p].push(tmpVal);
T Jake Luciani322caa22010-02-15 03:24:55 +0000417 }
418
Roger Meierb4bcbe32011-03-07 19:37:46 +0000419 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000420 },
421
Roger Meierb4bcbe32011-03-07 19:37:46 +0000422 writeSetBegin: function(elemType, size) {
423 this.tpos.push(this.tstack.length);
424 this.tstack.push([Thrift.Protocol.Type[elemType], size]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000425 },
426
Roger Meierb4bcbe32011-03-07 19:37:46 +0000427 writeSetEnd: function() {
428 var p = this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000429
Roger Meierb4bcbe32011-03-07 19:37:46 +0000430 while (this.tstack.length > p + 1) {
431 var tmpVal = this.tstack[p + 1];
432 this.tstack.splice(p + 1, 1);
433 this.tstack[p].push(tmpVal);
T Jake Luciani322caa22010-02-15 03:24:55 +0000434 }
435
Roger Meierb4bcbe32011-03-07 19:37:46 +0000436 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000437 },
438
Roger Meierb4bcbe32011-03-07 19:37:46 +0000439 writeBool: function(value) {
440 this.tstack.push(value ? 1 : 0);
T Jake Luciani322caa22010-02-15 03:24:55 +0000441 },
442
Roger Meierb4bcbe32011-03-07 19:37:46 +0000443 writeByte: function(i8) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000444 this.tstack.push(i8);
445 },
446
Roger Meierb4bcbe32011-03-07 19:37:46 +0000447 writeI16: function(i16) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000448 this.tstack.push(i16);
449 },
450
Roger Meierb4bcbe32011-03-07 19:37:46 +0000451 writeI32: function(i32) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000452 this.tstack.push(i32);
453 },
454
Roger Meierb4bcbe32011-03-07 19:37:46 +0000455 writeI64: function(i64) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000456 this.tstack.push(i64);
457 },
458
Roger Meierb4bcbe32011-03-07 19:37:46 +0000459 writeDouble: function(dbl) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000460 this.tstack.push(dbl);
461 },
462
Roger Meierb4bcbe32011-03-07 19:37:46 +0000463 writeString: function(str) {
464 // We do not encode uri components for wire transfer:
465 if (str === null) {
T Jake Luciani416eea92010-09-17 23:38:25 +0000466 this.tstack.push(null);
467 } else {
468 // concat may be slower than building a byte buffer
Roger Meierb4bcbe32011-03-07 19:37:46 +0000469 var escapedString = '';
470 for (var i = 0; i < str.length; i++) {
471 var ch = str.charAt(i); // a single double quote: "
472 if (ch === '\"') {
473 escapedString += '\\\"'; // write out as: \"
474 } else if (ch === '\\') { // a single backslash: \
475 escapedString += '\\\\'; // write out as: \\
T Jake Luciani416eea92010-09-17 23:38:25 +0000476 /* Currently escaped forward slashes break TJSONProtocol.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000477 * As it stands, we can simply pass forward slashes into
478 * our strings across the wire without being escaped.
T Jake Luciani416eea92010-09-17 23:38:25 +0000479 * I think this is the protocol's bug, not thrift.js
Roger Meierb4bcbe32011-03-07 19:37:46 +0000480 * } else if(ch === '/') { // a single forward slash: /
481 * escapedString += '\\/'; // write out as \/
482 * }
T Jake Luciani416eea92010-09-17 23:38:25 +0000483 */
Roger Meierb4bcbe32011-03-07 19:37:46 +0000484 } else if (ch === '\b') { // a single backspace: invisible
485 escapedString += '\\b'; // write out as: \b"
486 } else if (ch === '\f') { // a single formfeed: invisible
487 escapedString += '\\f'; // write out as: \f"
488 } else if (ch === '\n') { // a single newline: invisible
489 escapedString += '\\n'; // write out as: \n"
490 } else if (ch === '\r') { // a single return: invisible
491 escapedString += '\\r'; // write out as: \r"
492 } else if (ch === '\t') { // a single tab: invisible
493 escapedString += '\\t'; // write out as: \t"
T Jake Luciani416eea92010-09-17 23:38:25 +0000494 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000495 escapedString += ch; // Else it need not be escaped
T Jake Luciani416eea92010-09-17 23:38:25 +0000496 }
497 }
498 this.tstack.push('"' + escapedString + '"');
499 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000500 },
501
Roger Meierb4bcbe32011-03-07 19:37:46 +0000502 writeBinary: function(str) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000503 this.writeString(str);
504 },
505
506
Roger Meierb4bcbe32011-03-07 19:37:46 +0000507
T Jake Luciani322caa22010-02-15 03:24:55 +0000508 // Reading functions
Roger Meierb4bcbe32011-03-07 19:37:46 +0000509 readMessageBegin: function(name, messageType, seqid) {
510 this.rstack = new Array();
511 this.rpos = new Array();
T Jake Luciani322caa22010-02-15 03:24:55 +0000512
Roger Meierb4bcbe32011-03-07 19:37:46 +0000513 this.robj = eval(this.transport.readAll());
T Jake Luciani322caa22010-02-15 03:24:55 +0000514
T Jake Luciani322caa22010-02-15 03:24:55 +0000515 var r = {};
Roger Meierb4bcbe32011-03-07 19:37:46 +0000516 var version = this.robj.shift();
517
518 if (version != Thrift.Protocol.Version) {
519 throw 'Wrong thrift protocol version: ' + version;
T Jake Luciani322caa22010-02-15 03:24:55 +0000520 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000521
522 r['fname'] = this.robj.shift();
523 r['mtype'] = this.robj.shift();
524 r['rseqid'] = this.robj.shift();
525
526
527 //get to the main obj
528 this.rstack.push(this.robj.shift());
529
530 return r;
531 },
532
533
534 readMessageEnd: function() {
535 },
536
537 readStructBegin: function(name) {
538 var r = {};
539 r['fname'] = '';
540
541 //incase this is an array of structs
542 if (this.rstack[this.rstack.length - 1] instanceof Array)
543 this.rstack.push(this.rstack[this.rstack.length - 1].shift());
544
545 return r;
546 },
547
548 readStructEnd: function() {
549 if (this.rstack[this.rstack.length - 2] instanceof Array)
550 this.rstack.pop();
551 },
552
553 readFieldBegin: function() {
554 var r = {};
555
556 var fid = -1;
557 var ftype = Thrift.Type.STOP;
558
559 //get a fieldId
560 for (var f in (this.rstack[this.rstack.length - 1])) {
561 if (f == null) continue;
562
563 fid = parseInt(f);
564 this.rpos.push(this.rstack.length);
565
566 var field = this.rstack[this.rstack.length - 1][fid]
567
568 //remove so we don't see it again
569 delete this.rstack[this.rstack.length - 1][fid];
570
571 this.rstack.push(field);
572
573 break;
574 }
575
576 if (fid != -1) {
577
T Jake Luciani322caa22010-02-15 03:24:55 +0000578 //should only be 1 of these but this is the only
579 //way to match a key
Roger Meierb4bcbe32011-03-07 19:37:46 +0000580 for (var f in (this.rstack[this.rstack.length - 1])) {
581 if (Thrift.Protocol.RType[f] == null) continue;
T Jake Luciani322caa22010-02-15 03:24:55 +0000582
Roger Meierb4bcbe32011-03-07 19:37:46 +0000583 ftype = Thrift.Protocol.RType[f];
584 this.rstack[this.rstack.length - 1] =
585 this.rstack[this.rstack.length - 1][f];
586 }
587 }
588
589 r['fname'] = '';
590 r['ftype'] = ftype;
591 r['fid'] = fid;
592
593
594 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000595 },
596
Roger Meierb4bcbe32011-03-07 19:37:46 +0000597 readFieldEnd: function() {
598 var pos = this.rpos.pop();
599
T Jake Luciani322caa22010-02-15 03:24:55 +0000600 //get back to the right place in the stack
Roger Meierb4bcbe32011-03-07 19:37:46 +0000601 while (this.rstack.length > pos)
T Jake Luciani322caa22010-02-15 03:24:55 +0000602 this.rstack.pop();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000603
T Jake Luciani322caa22010-02-15 03:24:55 +0000604 },
605
Roger Meierb4bcbe32011-03-07 19:37:46 +0000606 readMapBegin: function(keyType, valType, size) {
607
608 var map = this.rstack.pop();
609
T Jake Luciani322caa22010-02-15 03:24:55 +0000610 var r = {};
Roger Meierb4bcbe32011-03-07 19:37:46 +0000611 r['ktype'] = Thrift.Protocol.RType[map.shift()];
612 r['vtype'] = Thrift.Protocol.RType[map.shift()];
613 r['size'] = map.shift();
T Jake Luciani322caa22010-02-15 03:24:55 +0000614
T Jake Luciani322caa22010-02-15 03:24:55 +0000615
T Jake Luciani322caa22010-02-15 03:24:55 +0000616 this.rpos.push(this.rstack.length);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000617 this.rstack.push(map.shift());
618
T Jake Luciani322caa22010-02-15 03:24:55 +0000619 return r;
620 },
621
Roger Meierb4bcbe32011-03-07 19:37:46 +0000622 readMapEnd: function() {
623 this.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000624 },
625
Roger Meierb4bcbe32011-03-07 19:37:46 +0000626 readListBegin: function(elemType, size) {
627
628 var list = this.rstack[this.rstack.length - 1];
629
630 var r = {};
631 r['etype'] = Thrift.Protocol.RType[list.shift()];
632 r['size'] = list.shift();
633
634
635 this.rpos.push(this.rstack.length);
636 this.rstack.push(list);
637
638 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000639 },
640
Roger Meierb4bcbe32011-03-07 19:37:46 +0000641 readListEnd: function() {
642 this.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000643 },
644
Roger Meierb4bcbe32011-03-07 19:37:46 +0000645 readSetBegin: function(elemType, size) {
646 return this.readListBegin(elemType, size);
647 },
648
649 readSetEnd: function() {
650 return this.readListEnd();
651 },
652
653 readBool: function() {
654 var r = this.readI32();
655
656 if (r != null && r['value'] == '1') {
657 r['value'] = true;
658 }else {
659 r['value'] = false;
T Jake Luciani322caa22010-02-15 03:24:55 +0000660 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000661
662 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000663 },
664
Roger Meierb4bcbe32011-03-07 19:37:46 +0000665 readByte: function() {
666 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000667 },
668
Roger Meierb4bcbe32011-03-07 19:37:46 +0000669 readI16: function() {
670 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000671 },
T Jake Luciani322caa22010-02-15 03:24:55 +0000672
Roger Meierb4bcbe32011-03-07 19:37:46 +0000673
674 readI32: function(f) {
675 if (f == undefined)
676 f = this.rstack[this.rstack.length - 1];
677
678 var r = {};
679
680 if (f instanceof Array) {
681 if (f.length == 0)
682 r['value'] = undefined;
T Jake Lucianiefabb892010-07-28 22:31:12 +0000683 else
Roger Meierb4bcbe32011-03-07 19:37:46 +0000684 r['value'] = f.shift();
T Jake Lucianiefabb892010-07-28 22:31:12 +0000685
Roger Meierb4bcbe32011-03-07 19:37:46 +0000686 }else if (f instanceof Object) {
687 for (var i in f) {
688 if (i == null) continue;
T Jake Luciani322caa22010-02-15 03:24:55 +0000689 this.rstack.push(f[i])
Roger Meierb4bcbe32011-03-07 19:37:46 +0000690 delete f[i];
691
692 r['value'] = i;
693 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000694 }
695 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000696 r['value'] = f;
697 this.rstack.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000698 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000699
700 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000701 },
702
Roger Meierb4bcbe32011-03-07 19:37:46 +0000703 readI64: function() {
704 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000705 },
706
Roger Meierb4bcbe32011-03-07 19:37:46 +0000707 readDouble: function() {
708 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000709 },
710
Roger Meierb4bcbe32011-03-07 19:37:46 +0000711 readString: function() {
712 var r = this.readI32();
713 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000714 },
715
Roger Meierb4bcbe32011-03-07 19:37:46 +0000716 readBinary: function() {
717 return this.readString();
T Jake Luciani322caa22010-02-15 03:24:55 +0000718 },
719
Roger Meierb4bcbe32011-03-07 19:37:46 +0000720
T Jake Luciani322caa22010-02-15 03:24:55 +0000721 //Method to arbitrarily skip over data.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000722 skip: function(type) {
723 throw 'skip not supported yet';
T Jake Luciani322caa22010-02-15 03:24:55 +0000724 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000725
726};