blob: 5c1fdf19f80a5a18584b6e416d75259b096d00ac [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 = {
Roger Meier0244e932011-03-09 15:25:01 +000020 Version: '0.7.0-dev',
21/*
22 Description: 'JavaScript bindings for the Apache Thrift RPC system',
23 License: 'http://www.apache.org/licenses/LICENSE-2.0',
24 Homepage: 'http://thrift.apache.org',
25 BugReports: 'https://issues.apache.org/jira/browse/THRIFT',
26 Maintainer: 'dev@thrift.apache.org',
27*/
T Jake Luciani322caa22010-02-15 03:24:55 +000028
Roger Meierb4bcbe32011-03-07 19:37:46 +000029 Type: {
30 'STOP' : 0,
31 'VOID' : 1,
32 'BOOL' : 2,
33 'BYTE' : 3,
34 'I08' : 3,
35 'DOUBLE' : 4,
36 'I16' : 6,
37 'I32' : 8,
38 'I64' : 10,
39 'STRING' : 11,
40 'UTF7' : 11,
41 'STRUCT' : 12,
42 'MAP' : 13,
43 'SET' : 14,
44 'LIST' : 15,
45 'UTF8' : 16,
46 'UTF16' : 17
T Jake Luciani322caa22010-02-15 03:24:55 +000047 },
48
Roger Meierb4bcbe32011-03-07 19:37:46 +000049 MessageType: {
50 'CALL' : 1,
51 'REPLY' : 2,
52 'EXCEPTION' : 3
Roger Meier55ea68f2011-02-16 19:29:50 +000053 },
Roger Meierb4bcbe32011-03-07 19:37:46 +000054
55 objectLength: function(obj) {
56 var length = 0;
Roger Meier55ea68f2011-02-16 19:29:50 +000057 for (k in obj)
58 if (obj.hasOwnProperty(k))
Roger Meierb4bcbe32011-03-07 19:37:46 +000059 length++;
60 return length;
Roger Meier55ea68f2011-02-16 19:29:50 +000061 },
62
Roger Meierb4bcbe32011-03-07 19:37:46 +000063 inherits: function(constructor, superConstructor) {
64 //Prototypal Inheritance http://javascript.crockford.com/prototypal.html
65 function F() {}
66 F.prototype = superConstructor.prototype;
67 constructor.prototype = new F();
T Jake Luciani322caa22010-02-15 03:24:55 +000068 }
Roger Meierb4bcbe32011-03-07 19:37:46 +000069};
T Jake Luciani322caa22010-02-15 03:24:55 +000070
Roger Meier55ea68f2011-02-16 19:29:50 +000071
Roger Meierb4bcbe32011-03-07 19:37:46 +000072Thrift.TException = {};
73Thrift.TException.prototype = {
74 initialize: function(message, code) {
T Jake Luciani322caa22010-02-15 03:24:55 +000075 this.message = message;
Roger Meierb4bcbe32011-03-07 19:37:46 +000076 this.code = (code == null) ? 0 : code;
T Jake Luciani322caa22010-02-15 03:24:55 +000077 }
Roger Meierb4bcbe32011-03-07 19:37:46 +000078};
T Jake Luciani322caa22010-02-15 03:24:55 +000079
80
T Jake Lucianiefabb892010-07-28 22:31:12 +000081Thrift.TApplicationExceptionType = {
Roger Meierb4bcbe32011-03-07 19:37:46 +000082 'UNKNOWN' : 0,
83 'UNKNOWN_METHOD' : 1,
84 'INVALID_MESSAGE_TYPE' : 2,
85 'WRONG_METHOD_NAME' : 3,
86 'BAD_SEQUENCE_ID' : 4,
87 'MISSING_RESULT' : 5
88};
T Jake Luciani322caa22010-02-15 03:24:55 +000089
Roger Meierb4bcbe32011-03-07 19:37:46 +000090Thrift.TApplicationException = function(message, code) {
91 this.message = message;
92 this.code = (code == null) ? 0 : code;
93};
T Jake Luciani322caa22010-02-15 03:24:55 +000094
Roger Meierb4bcbe32011-03-07 19:37:46 +000095Thrift.TApplicationException.prototype = {
96
97 read: function(input) {
98 while (1) {
99 ret = input.readFieldBegin();
100
101 if (ret.ftype == Thrift.Type.STOP)
102 break;
103
104 var fid = ret.fid;
105
106 switch (fid) {
107 case 1:
108 if (ret.ftype == Thrift.Type.STRING) {
109 ret = input.readString();
110 this.message = ret.value;
T Jake Luciani322caa22010-02-15 03:24:55 +0000111 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000112 ret = input.skip(ret.ftype);
T Jake Luciani322caa22010-02-15 03:24:55 +0000113 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000114
115 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000116 case 2:
Roger Meierb4bcbe32011-03-07 19:37:46 +0000117 if (ret.ftype == Thrift.Type.I32) {
118 ret = input.readI32();
119 this.code = ret.value;
T Jake Luciani322caa22010-02-15 03:24:55 +0000120 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000121 ret = input.skip(ret.ftype);
T Jake Luciani322caa22010-02-15 03:24:55 +0000122 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000123 break;
124
T Jake Luciani322caa22010-02-15 03:24:55 +0000125 default:
Roger Meierb4bcbe32011-03-07 19:37:46 +0000126 ret = input.skip(ret.ftype);
127 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000128 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000129
130 input.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000131 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000132
133 input.readStructEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000134 },
Roger Meierb4bcbe32011-03-07 19:37:46 +0000135
136 write: function(output) {
137 var xfer = 0;
138
T Jake Luciani322caa22010-02-15 03:24:55 +0000139 output.writeStructBegin('TApplicationException');
Roger Meierb4bcbe32011-03-07 19:37:46 +0000140
T Jake Luciani322caa22010-02-15 03:24:55 +0000141 if (this.message) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000142 output.writeFieldBegin('message', Thrift.Type.STRING, 1);
143 output.writeString(this.getMessage());
144 output.writeFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000145 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000146
T Jake Luciani322caa22010-02-15 03:24:55 +0000147 if (this.code) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000148 output.writeFieldBegin('type', Thrift.Type.I32, 2);
149 output.writeI32(this.code);
150 output.writeFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000151 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000152
153 output.writeFieldStop();
154 output.writeStructEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000155 },
Roger Meierb4bcbe32011-03-07 19:37:46 +0000156
157 getCode: function() {
158 return this.code;
T Jake Luciani322caa22010-02-15 03:24:55 +0000159 },
Roger Meierb4bcbe32011-03-07 19:37:46 +0000160
161 getMessage: function() {
162 return this.message;
T Jake Luciani322caa22010-02-15 03:24:55 +0000163 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000164};
T Jake Luciani322caa22010-02-15 03:24:55 +0000165
166
167
168/**
169 *If you do not specify a url then you must handle ajax on your own.
170 *This is how to use js bindings in a async fashion.
171 */
Roger Meierb4bcbe32011-03-07 19:37:46 +0000172Thrift.Transport = function(url) {
173 this.url = url;
174 this.wpos = 0;
175 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000176
Roger Meierb4bcbe32011-03-07 19:37:46 +0000177 this.send_buf = '';
178 this.recv_buf = '';
179};
T Jake Luciani322caa22010-02-15 03:24:55 +0000180
181Thrift.Transport.prototype = {
182
183 //Gets the browser specific XmlHttpRequest Object
Roger Meierb4bcbe32011-03-07 19:37:46 +0000184 getXmlHttpRequestObject: function() {
T Jake Luciani322caa22010-02-15 03:24:55 +0000185
Roger Meierb4bcbe32011-03-07 19:37:46 +0000186 try { return new XMLHttpRequest() } catch (e) {}
187 try { return new ActiveXObject('Msxml2.XMLHTTP') } catch (e) {}
188 try { return new ActiveXObject('Microsoft.XMLHTTP') } catch (e) {}
189
190 throw "Your browser doesn't support the XmlHttpRequest object.";
191
T Jake Luciani322caa22010-02-15 03:24:55 +0000192 },
193
Roger Meierb4bcbe32011-03-07 19:37:46 +0000194 flush: function() {
T Jake Luciani322caa22010-02-15 03:24:55 +0000195
196 //async mode
Roger Meierb4bcbe32011-03-07 19:37:46 +0000197 if (this.url == undefined || this.url == '')
T Jake Luciani322caa22010-02-15 03:24:55 +0000198 return this.send_buf;
199
Roger Meierb4bcbe32011-03-07 19:37:46 +0000200 var xreq = this.getXmlHttpRequestObject();
201
T Jake Luciani322caa22010-02-15 03:24:55 +0000202 if (xreq.overrideMimeType)
Roger Meierb4bcbe32011-03-07 19:37:46 +0000203 xreq.overrideMimeType('application/json');
204
205 xreq.open('POST', this.url, false);
206 xreq.send(this.send_buf);
207
T Jake Luciani322caa22010-02-15 03:24:55 +0000208 if (xreq.readyState != 4)
Roger Meierb4bcbe32011-03-07 19:37:46 +0000209 throw 'encountered an unknown ajax ready state: ' + xreq.readyState;
210
T Jake Luciani322caa22010-02-15 03:24:55 +0000211 if (xreq.status != 200)
Roger Meierb4bcbe32011-03-07 19:37:46 +0000212 throw 'encountered a unknown request status: ' + xreq.status;
T Jake Luciani322caa22010-02-15 03:24:55 +0000213
Roger Meierb4bcbe32011-03-07 19:37:46 +0000214 this.recv_buf = xreq.responseText;
215 this.recv_buf_sz = this.recv_buf.length;
216 this.wpos = this.recv_buf.length;
217 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000218 },
219
Roger Meierb4bcbe32011-03-07 19:37:46 +0000220 setRecvBuffer: function(buf) {
221 this.recv_buf = buf;
222 this.recv_buf_sz = this.recv_buf.length;
223 this.wpos = this.recv_buf.length;
224 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000225 },
226
Roger Meierb4bcbe32011-03-07 19:37:46 +0000227 isOpen: function() {
228 return true;
T Jake Luciani322caa22010-02-15 03:24:55 +0000229 },
230
Roger Meierb4bcbe32011-03-07 19:37:46 +0000231 open: function() {},
T Jake Luciani322caa22010-02-15 03:24:55 +0000232
233 close: function() {},
234
Roger Meierb4bcbe32011-03-07 19:37:46 +0000235 read: function(len) {
236 var avail = this.wpos - this.rpos;
T Jake Luciani322caa22010-02-15 03:24:55 +0000237
Roger Meierb4bcbe32011-03-07 19:37:46 +0000238 if (avail == 0)
239 return '';
T Jake Luciani322caa22010-02-15 03:24:55 +0000240
Roger Meierb4bcbe32011-03-07 19:37:46 +0000241 var give = len;
T Jake Luciani322caa22010-02-15 03:24:55 +0000242
Roger Meierb4bcbe32011-03-07 19:37:46 +0000243 if (avail < len)
244 give = avail;
245
246 var ret = this.read_buf.substr(this.rpos, give);
247 this.rpos += give;
T Jake Luciani322caa22010-02-15 03:24:55 +0000248
249 //clear buf when complete?
Roger Meierb4bcbe32011-03-07 19:37:46 +0000250 return ret;
T Jake Luciani322caa22010-02-15 03:24:55 +0000251 },
252
Roger Meierb4bcbe32011-03-07 19:37:46 +0000253 readAll: function() {
254 return this.recv_buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000255 },
256
Roger Meierb4bcbe32011-03-07 19:37:46 +0000257 write: function(buf) {
258 this.send_buf = buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000259 },
260
Roger Meierb4bcbe32011-03-07 19:37:46 +0000261 getSendBuffer: function() {
262 return this.send_buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000263 }
264
Roger Meierb4bcbe32011-03-07 19:37:46 +0000265};
T Jake Luciani322caa22010-02-15 03:24:55 +0000266
267
268
Roger Meierb4bcbe32011-03-07 19:37:46 +0000269Thrift.Protocol = function(transport) {
270 this.transport = transport;
271};
T Jake Luciani322caa22010-02-15 03:24:55 +0000272
Roger Meierb4bcbe32011-03-07 19:37:46 +0000273Thrift.Protocol.Type = {};
274Thrift.Protocol.Type[Thrift.Type.BOOL] = '"tf"';
275Thrift.Protocol.Type[Thrift.Type.BYTE] = '"i8"';
276Thrift.Protocol.Type[Thrift.Type.I16] = '"i16"';
277Thrift.Protocol.Type[Thrift.Type.I32] = '"i32"';
278Thrift.Protocol.Type[Thrift.Type.I64] = '"i64"';
279Thrift.Protocol.Type[Thrift.Type.DOUBLE] = '"dbl"';
280Thrift.Protocol.Type[Thrift.Type.STRUCT] = '"rec"';
281Thrift.Protocol.Type[Thrift.Type.STRING] = '"str"';
282Thrift.Protocol.Type[Thrift.Type.MAP] = '"map"';
283Thrift.Protocol.Type[Thrift.Type.LIST] = '"lst"';
284Thrift.Protocol.Type[Thrift.Type.SET] = '"set"';
T Jake Luciani322caa22010-02-15 03:24:55 +0000285
286
Roger Meierb4bcbe32011-03-07 19:37:46 +0000287Thrift.Protocol.RType = {};
288Thrift.Protocol.RType['tf'] = Thrift.Type.BOOL;
289Thrift.Protocol.RType['i8'] = Thrift.Type.BYTE;
290Thrift.Protocol.RType['i16'] = Thrift.Type.I16;
291Thrift.Protocol.RType['i32'] = Thrift.Type.I32;
292Thrift.Protocol.RType['i64'] = Thrift.Type.I64;
293Thrift.Protocol.RType['dbl'] = Thrift.Type.DOUBLE;
294Thrift.Protocol.RType['rec'] = Thrift.Type.STRUCT;
295Thrift.Protocol.RType['str'] = Thrift.Type.STRING;
296Thrift.Protocol.RType['map'] = Thrift.Type.MAP;
297Thrift.Protocol.RType['lst'] = Thrift.Type.LIST;
298Thrift.Protocol.RType['set'] = Thrift.Type.SET;
T Jake Luciani322caa22010-02-15 03:24:55 +0000299
Roger Meierb4bcbe32011-03-07 19:37:46 +0000300Thrift.Protocol.Version = 1;
T Jake Luciani322caa22010-02-15 03:24:55 +0000301
302Thrift.Protocol.prototype = {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000303
304 getTransport: function() {
305 return this.transport;
T Jake Luciani322caa22010-02-15 03:24:55 +0000306 },
307
308 //Write functions
Roger Meierb4bcbe32011-03-07 19:37:46 +0000309 writeMessageBegin: function(name, messageType, seqid) {
310 this.tstack = new Array();
311 this.tpos = new Array();
312
313 this.tstack.push([Thrift.Protocol.Version, '"' +
314 name + '"', messageType, seqid]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000315 },
316
Roger Meierb4bcbe32011-03-07 19:37:46 +0000317 writeMessageEnd: function() {
318 var obj = this.tstack.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000319
Roger Meierb4bcbe32011-03-07 19:37:46 +0000320 this.wobj = this.tstack.pop();
321 this.wobj.push(obj);
322
323 this.wbuf = '[' + this.wobj.join(',') + ']';
324
325 this.transport.write(this.wbuf);
T Jake Luciani322caa22010-02-15 03:24:55 +0000326 },
327
328
Roger Meierb4bcbe32011-03-07 19:37:46 +0000329 writeStructBegin: function(name) {
330 this.tpos.push(this.tstack.length);
331 this.tstack.push({});
T Jake Luciani322caa22010-02-15 03:24:55 +0000332 },
333
Roger Meierb4bcbe32011-03-07 19:37:46 +0000334 writeStructEnd: function() {
335
336 var p = this.tpos.pop();
337 var struct = this.tstack[p];
338 var str = '{';
339 var first = true;
340 for (var key in struct) {
341 if (first)
T Jake Luciani322caa22010-02-15 03:24:55 +0000342 first = false;
343 else
Roger Meierb4bcbe32011-03-07 19:37:46 +0000344 str += ',';
T Jake Luciani322caa22010-02-15 03:24:55 +0000345
Roger Meierb4bcbe32011-03-07 19:37:46 +0000346 str += key + ':' + struct[key];
347 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000348
Roger Meierb4bcbe32011-03-07 19:37:46 +0000349 str += '}';
T Jake Luciani322caa22010-02-15 03:24:55 +0000350 this.tstack[p] = str;
351 },
352
Roger Meierb4bcbe32011-03-07 19:37:46 +0000353 writeFieldBegin: function(name, fieldType, fieldId) {
354 this.tpos.push(this.tstack.length);
355 this.tstack.push({ 'fieldId': '"' +
356 fieldId + '"', 'fieldType': Thrift.Protocol.Type[fieldType]
357 });
358
T Jake Luciani322caa22010-02-15 03:24:55 +0000359 },
360
Roger Meierb4bcbe32011-03-07 19:37:46 +0000361 writeFieldEnd: function() {
362 var value = this.tstack.pop();
363 var fieldInfo = this.tstack.pop();
364
365 this.tstack[this.tstack.length - 1][fieldInfo.fieldId] = '{' +
366 fieldInfo.fieldType + ':' + value + '}';
367 this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000368 },
369
Roger Meierb4bcbe32011-03-07 19:37:46 +0000370 writeFieldStop: function() {
T Jake Luciani322caa22010-02-15 03:24:55 +0000371 //na
372 },
373
Roger Meierb4bcbe32011-03-07 19:37:46 +0000374 writeMapBegin: function(keyType, valType, size) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000375 //size is invalid, we'll set it on end.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000376 this.tpos.push(this.tstack.length);
377 this.tstack.push([Thrift.Protocol.Type[keyType],
378 Thrift.Protocol.Type[valType], 0]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000379 },
380
Roger Meierb4bcbe32011-03-07 19:37:46 +0000381 writeMapEnd: function() {
382 var p = this.tpos.pop();
383
384 if (p == this.tstack.length)
T Jake Luciani322caa22010-02-15 03:24:55 +0000385 return;
T Jake Luciani322caa22010-02-15 03:24:55 +0000386
Roger Meierb4bcbe32011-03-07 19:37:46 +0000387 if ((this.tstack.length - p - 1) % 2 != 0)
388 this.tstack.push('');
T Jake Luciani322caa22010-02-15 03:24:55 +0000389
Roger Meierb4bcbe32011-03-07 19:37:46 +0000390 var size = (this.tstack.length - p - 1) / 2;
391
392 this.tstack[p][this.tstack[p].length - 1] = size;
393
394 var map = '}';
395 var first = true;
396 while (this.tstack.length > p + 1) {
397 var v = this.tstack.pop();
398 var k = this.tstack.pop();
399 if (first) {
400 first = false;
401 }else {
402 map = ',' + map;
T Jake Luciani322caa22010-02-15 03:24:55 +0000403 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000404
405 map = '"' + k + '":' + v + map;
T Jake Luciani322caa22010-02-15 03:24:55 +0000406 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000407 map = '{' + map;
408
409 this.tstack[p].push(map);
410 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000411 },
412
Roger Meierb4bcbe32011-03-07 19:37:46 +0000413 writeListBegin: function(elemType, size) {
414 this.tpos.push(this.tstack.length);
415 this.tstack.push([Thrift.Protocol.Type[elemType], size]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000416 },
417
Roger Meierb4bcbe32011-03-07 19:37:46 +0000418 writeListEnd: function() {
419 var p = this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000420
Roger Meierb4bcbe32011-03-07 19:37:46 +0000421 while (this.tstack.length > p + 1) {
422 var tmpVal = this.tstack[p + 1];
423 this.tstack.splice(p + 1, 1);
424 this.tstack[p].push(tmpVal);
T Jake Luciani322caa22010-02-15 03:24:55 +0000425 }
426
Roger Meierb4bcbe32011-03-07 19:37:46 +0000427 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000428 },
429
Roger Meierb4bcbe32011-03-07 19:37:46 +0000430 writeSetBegin: function(elemType, size) {
431 this.tpos.push(this.tstack.length);
432 this.tstack.push([Thrift.Protocol.Type[elemType], size]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000433 },
434
Roger Meierb4bcbe32011-03-07 19:37:46 +0000435 writeSetEnd: function() {
436 var p = this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000437
Roger Meierb4bcbe32011-03-07 19:37:46 +0000438 while (this.tstack.length > p + 1) {
439 var tmpVal = this.tstack[p + 1];
440 this.tstack.splice(p + 1, 1);
441 this.tstack[p].push(tmpVal);
T Jake Luciani322caa22010-02-15 03:24:55 +0000442 }
443
Roger Meierb4bcbe32011-03-07 19:37:46 +0000444 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000445 },
446
Roger Meierb4bcbe32011-03-07 19:37:46 +0000447 writeBool: function(value) {
448 this.tstack.push(value ? 1 : 0);
T Jake Luciani322caa22010-02-15 03:24:55 +0000449 },
450
Roger Meierb4bcbe32011-03-07 19:37:46 +0000451 writeByte: function(i8) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000452 this.tstack.push(i8);
453 },
454
Roger Meierb4bcbe32011-03-07 19:37:46 +0000455 writeI16: function(i16) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000456 this.tstack.push(i16);
457 },
458
Roger Meierb4bcbe32011-03-07 19:37:46 +0000459 writeI32: function(i32) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000460 this.tstack.push(i32);
461 },
462
Roger Meierb4bcbe32011-03-07 19:37:46 +0000463 writeI64: function(i64) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000464 this.tstack.push(i64);
465 },
466
Roger Meierb4bcbe32011-03-07 19:37:46 +0000467 writeDouble: function(dbl) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000468 this.tstack.push(dbl);
469 },
470
Roger Meierb4bcbe32011-03-07 19:37:46 +0000471 writeString: function(str) {
472 // We do not encode uri components for wire transfer:
473 if (str === null) {
T Jake Luciani416eea92010-09-17 23:38:25 +0000474 this.tstack.push(null);
475 } else {
476 // concat may be slower than building a byte buffer
Roger Meierb4bcbe32011-03-07 19:37:46 +0000477 var escapedString = '';
478 for (var i = 0; i < str.length; i++) {
479 var ch = str.charAt(i); // a single double quote: "
480 if (ch === '\"') {
481 escapedString += '\\\"'; // write out as: \"
482 } else if (ch === '\\') { // a single backslash: \
483 escapedString += '\\\\'; // write out as: \\
T Jake Luciani416eea92010-09-17 23:38:25 +0000484 /* Currently escaped forward slashes break TJSONProtocol.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000485 * As it stands, we can simply pass forward slashes into
486 * our strings across the wire without being escaped.
T Jake Luciani416eea92010-09-17 23:38:25 +0000487 * I think this is the protocol's bug, not thrift.js
Roger Meierb4bcbe32011-03-07 19:37:46 +0000488 * } else if(ch === '/') { // a single forward slash: /
489 * escapedString += '\\/'; // write out as \/
490 * }
T Jake Luciani416eea92010-09-17 23:38:25 +0000491 */
Roger Meierb4bcbe32011-03-07 19:37:46 +0000492 } else if (ch === '\b') { // a single backspace: invisible
493 escapedString += '\\b'; // write out as: \b"
494 } else if (ch === '\f') { // a single formfeed: invisible
495 escapedString += '\\f'; // write out as: \f"
496 } else if (ch === '\n') { // a single newline: invisible
497 escapedString += '\\n'; // write out as: \n"
498 } else if (ch === '\r') { // a single return: invisible
499 escapedString += '\\r'; // write out as: \r"
500 } else if (ch === '\t') { // a single tab: invisible
501 escapedString += '\\t'; // write out as: \t"
T Jake Luciani416eea92010-09-17 23:38:25 +0000502 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000503 escapedString += ch; // Else it need not be escaped
T Jake Luciani416eea92010-09-17 23:38:25 +0000504 }
505 }
506 this.tstack.push('"' + escapedString + '"');
507 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000508 },
509
Roger Meierb4bcbe32011-03-07 19:37:46 +0000510 writeBinary: function(str) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000511 this.writeString(str);
512 },
513
514
Roger Meierb4bcbe32011-03-07 19:37:46 +0000515
T Jake Luciani322caa22010-02-15 03:24:55 +0000516 // Reading functions
Roger Meierb4bcbe32011-03-07 19:37:46 +0000517 readMessageBegin: function(name, messageType, seqid) {
518 this.rstack = new Array();
519 this.rpos = new Array();
T Jake Luciani322caa22010-02-15 03:24:55 +0000520
Roger Meierb4bcbe32011-03-07 19:37:46 +0000521 this.robj = eval(this.transport.readAll());
T Jake Luciani322caa22010-02-15 03:24:55 +0000522
T Jake Luciani322caa22010-02-15 03:24:55 +0000523 var r = {};
Roger Meierb4bcbe32011-03-07 19:37:46 +0000524 var version = this.robj.shift();
525
526 if (version != Thrift.Protocol.Version) {
527 throw 'Wrong thrift protocol version: ' + version;
T Jake Luciani322caa22010-02-15 03:24:55 +0000528 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000529
530 r['fname'] = this.robj.shift();
531 r['mtype'] = this.robj.shift();
532 r['rseqid'] = this.robj.shift();
533
534
535 //get to the main obj
536 this.rstack.push(this.robj.shift());
537
538 return r;
539 },
540
541
542 readMessageEnd: function() {
543 },
544
545 readStructBegin: function(name) {
546 var r = {};
547 r['fname'] = '';
548
549 //incase this is an array of structs
550 if (this.rstack[this.rstack.length - 1] instanceof Array)
551 this.rstack.push(this.rstack[this.rstack.length - 1].shift());
552
553 return r;
554 },
555
556 readStructEnd: function() {
557 if (this.rstack[this.rstack.length - 2] instanceof Array)
558 this.rstack.pop();
559 },
560
561 readFieldBegin: function() {
562 var r = {};
563
564 var fid = -1;
565 var ftype = Thrift.Type.STOP;
566
567 //get a fieldId
568 for (var f in (this.rstack[this.rstack.length - 1])) {
569 if (f == null) continue;
570
571 fid = parseInt(f);
572 this.rpos.push(this.rstack.length);
573
574 var field = this.rstack[this.rstack.length - 1][fid]
575
576 //remove so we don't see it again
577 delete this.rstack[this.rstack.length - 1][fid];
578
579 this.rstack.push(field);
580
581 break;
582 }
583
584 if (fid != -1) {
585
T Jake Luciani322caa22010-02-15 03:24:55 +0000586 //should only be 1 of these but this is the only
587 //way to match a key
Roger Meierb4bcbe32011-03-07 19:37:46 +0000588 for (var f in (this.rstack[this.rstack.length - 1])) {
589 if (Thrift.Protocol.RType[f] == null) continue;
T Jake Luciani322caa22010-02-15 03:24:55 +0000590
Roger Meierb4bcbe32011-03-07 19:37:46 +0000591 ftype = Thrift.Protocol.RType[f];
592 this.rstack[this.rstack.length - 1] =
593 this.rstack[this.rstack.length - 1][f];
594 }
595 }
596
597 r['fname'] = '';
598 r['ftype'] = ftype;
599 r['fid'] = fid;
600
601
602 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000603 },
604
Roger Meierb4bcbe32011-03-07 19:37:46 +0000605 readFieldEnd: function() {
606 var pos = this.rpos.pop();
607
T Jake Luciani322caa22010-02-15 03:24:55 +0000608 //get back to the right place in the stack
Roger Meierb4bcbe32011-03-07 19:37:46 +0000609 while (this.rstack.length > pos)
T Jake Luciani322caa22010-02-15 03:24:55 +0000610 this.rstack.pop();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000611
T Jake Luciani322caa22010-02-15 03:24:55 +0000612 },
613
Roger Meierb4bcbe32011-03-07 19:37:46 +0000614 readMapBegin: function(keyType, valType, size) {
615
616 var map = this.rstack.pop();
617
T Jake Luciani322caa22010-02-15 03:24:55 +0000618 var r = {};
Roger Meierb4bcbe32011-03-07 19:37:46 +0000619 r['ktype'] = Thrift.Protocol.RType[map.shift()];
620 r['vtype'] = Thrift.Protocol.RType[map.shift()];
621 r['size'] = map.shift();
T Jake Luciani322caa22010-02-15 03:24:55 +0000622
T Jake Luciani322caa22010-02-15 03:24:55 +0000623
T Jake Luciani322caa22010-02-15 03:24:55 +0000624 this.rpos.push(this.rstack.length);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000625 this.rstack.push(map.shift());
626
T Jake Luciani322caa22010-02-15 03:24:55 +0000627 return r;
628 },
629
Roger Meierb4bcbe32011-03-07 19:37:46 +0000630 readMapEnd: function() {
631 this.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000632 },
633
Roger Meierb4bcbe32011-03-07 19:37:46 +0000634 readListBegin: function(elemType, size) {
635
636 var list = this.rstack[this.rstack.length - 1];
637
638 var r = {};
639 r['etype'] = Thrift.Protocol.RType[list.shift()];
640 r['size'] = list.shift();
641
642
643 this.rpos.push(this.rstack.length);
644 this.rstack.push(list);
645
646 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000647 },
648
Roger Meierb4bcbe32011-03-07 19:37:46 +0000649 readListEnd: function() {
650 this.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000651 },
652
Roger Meierb4bcbe32011-03-07 19:37:46 +0000653 readSetBegin: function(elemType, size) {
654 return this.readListBegin(elemType, size);
655 },
656
657 readSetEnd: function() {
658 return this.readListEnd();
659 },
660
661 readBool: function() {
662 var r = this.readI32();
663
664 if (r != null && r['value'] == '1') {
665 r['value'] = true;
666 }else {
667 r['value'] = false;
T Jake Luciani322caa22010-02-15 03:24:55 +0000668 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000669
670 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000671 },
672
Roger Meierb4bcbe32011-03-07 19:37:46 +0000673 readByte: function() {
674 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000675 },
676
Roger Meierb4bcbe32011-03-07 19:37:46 +0000677 readI16: function() {
678 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000679 },
T Jake Luciani322caa22010-02-15 03:24:55 +0000680
Roger Meierb4bcbe32011-03-07 19:37:46 +0000681
682 readI32: function(f) {
683 if (f == undefined)
684 f = this.rstack[this.rstack.length - 1];
685
686 var r = {};
687
688 if (f instanceof Array) {
689 if (f.length == 0)
690 r['value'] = undefined;
T Jake Lucianiefabb892010-07-28 22:31:12 +0000691 else
Roger Meierb4bcbe32011-03-07 19:37:46 +0000692 r['value'] = f.shift();
T Jake Lucianiefabb892010-07-28 22:31:12 +0000693
Roger Meierb4bcbe32011-03-07 19:37:46 +0000694 }else if (f instanceof Object) {
695 for (var i in f) {
696 if (i == null) continue;
T Jake Luciani322caa22010-02-15 03:24:55 +0000697 this.rstack.push(f[i])
Roger Meierb4bcbe32011-03-07 19:37:46 +0000698 delete f[i];
699
700 r['value'] = i;
701 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000702 }
703 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000704 r['value'] = f;
705 this.rstack.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000706 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000707
708 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000709 },
710
Roger Meierb4bcbe32011-03-07 19:37:46 +0000711 readI64: function() {
712 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000713 },
714
Roger Meierb4bcbe32011-03-07 19:37:46 +0000715 readDouble: function() {
716 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000717 },
718
Roger Meierb4bcbe32011-03-07 19:37:46 +0000719 readString: function() {
720 var r = this.readI32();
721 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000722 },
723
Roger Meierb4bcbe32011-03-07 19:37:46 +0000724 readBinary: function() {
725 return this.readString();
T Jake Luciani322caa22010-02-15 03:24:55 +0000726 },
727
Roger Meierb4bcbe32011-03-07 19:37:46 +0000728
T Jake Luciani322caa22010-02-15 03:24:55 +0000729 //Method to arbitrarily skip over data.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000730 skip: function(type) {
731 throw 'skip not supported yet';
T Jake Luciani322caa22010-02-15 03:24:55 +0000732 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000733
734};