T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 19 | var Thrift = { |
| 20 | |
| 21 | 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 |
| 39 | }, |
| 40 | |
| 41 | MessageType : { |
| 42 | "CALL" : 1, |
| 43 | "REPLY" : 2, |
| 44 | "EXCEPTION" : 3 |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | Thrift.TException = {} |
| 49 | Thrift.TException.prototype = { |
| 50 | initialize: function( message, code ) { |
| 51 | this.message = message; |
| 52 | this.code = (code == null) ? 0 : code; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 57 | Thrift.TApplicationExceptionType = { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 58 | "UNKNOWN" : 0, |
| 59 | "UNKNOWN_METHOD" : 1, |
| 60 | "INVALID_MESSAGE_TYPE" : 2, |
| 61 | "WRONG_METHOD_NAME" : 3, |
| 62 | "BAD_SEQUENCE_ID" : 4, |
| 63 | "MISSING_RESULT" : 5 |
| 64 | } |
| 65 | |
| 66 | Thrift.TApplicationException = function(message, code){ |
| 67 | this.message = message |
| 68 | this.code = (code == null) ? 0 : code |
| 69 | } |
| 70 | |
| 71 | Thrift.TApplicationException.prototype = { |
| 72 | |
| 73 | read : function(input){ |
| 74 | |
| 75 | var ftype |
| 76 | var fid |
| 77 | var ret = input.readStructBegin(fname) |
| 78 | |
| 79 | this.fname = ret.fname |
| 80 | |
| 81 | while(1){ |
| 82 | |
| 83 | ret = input.readFieldBegin() |
| 84 | |
| 85 | if(ret.ftype == TType.STOP) |
| 86 | break |
| 87 | |
| 88 | var fid = ret.fid |
| 89 | |
| 90 | switch(fid){ |
| 91 | case 1: |
| 92 | if( ret.ftype == Type.STRING ){ |
| 93 | ret = input.readString() |
| 94 | this.message = ret.value |
| 95 | } else { |
| 96 | ret = input.skip(ret.ftype) |
| 97 | } |
| 98 | |
| 99 | break |
| 100 | case 2: |
| 101 | if( ret.ftype == Type.I32 ){ |
| 102 | ret = input.readI32() |
| 103 | this.code = ret.value |
| 104 | } else { |
| 105 | ret = input.skip(ret.ftype) |
| 106 | } |
| 107 | break |
| 108 | |
| 109 | default: |
| 110 | ret = input.skip(ret.ftype) |
| 111 | break |
| 112 | } |
| 113 | |
| 114 | input.readFieldEnd() |
| 115 | |
| 116 | } |
| 117 | |
| 118 | input.readStructEnd() |
| 119 | |
| 120 | }, |
| 121 | |
| 122 | write: function(output){ |
| 123 | var xfer = 0; |
| 124 | |
| 125 | output.writeStructBegin('TApplicationException'); |
| 126 | |
| 127 | if (this.message) { |
| 128 | output.writeFieldBegin('message', Type.STRING, 1) |
| 129 | output.writeString(this.getMessage()) |
| 130 | output.writeFieldEnd() |
| 131 | } |
| 132 | |
| 133 | if (this.code) { |
| 134 | output.writeFieldBegin('type', Type.I32, 2) |
| 135 | output.writeI32(this.code) |
| 136 | output.writeFieldEnd() |
| 137 | } |
| 138 | |
| 139 | output.writeFieldStop() |
| 140 | output.writeStructEnd() |
| 141 | |
| 142 | }, |
| 143 | |
| 144 | getCode : function() { |
| 145 | return this.code |
| 146 | }, |
| 147 | |
| 148 | getMessage : function() { |
| 149 | return this.message |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | /** |
| 156 | *If you do not specify a url then you must handle ajax on your own. |
| 157 | *This is how to use js bindings in a async fashion. |
| 158 | */ |
| 159 | Thrift.Transport = function(url){ |
| 160 | this.url = url |
| 161 | this.wpos = 0 |
| 162 | this.rpos = 0 |
| 163 | |
| 164 | this.send_buf = '' |
| 165 | this.recv_buf = '' |
| 166 | } |
| 167 | |
| 168 | Thrift.Transport.prototype = { |
| 169 | |
| 170 | //Gets the browser specific XmlHttpRequest Object |
| 171 | getXmlHttpRequestObject : function() { |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 172 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 173 | try { return new XMLHttpRequest() } catch(e) {} |
| 174 | try { return new ActiveXObject("Msxml2.XMLHTTP") } catch (e) {} |
| 175 | try { return new ActiveXObject("Microsoft.XMLHTTP") } catch (e) {} |
| 176 | |
| 177 | throw "Your browser doesn't support the XmlHttpRequest object. Try upgrading to Firefox." |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 178 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 179 | }, |
| 180 | |
| 181 | flush : function(){ |
| 182 | |
| 183 | //async mode |
| 184 | if(this.url == undefined || this.url == '') |
| 185 | return this.send_buf; |
| 186 | |
| 187 | var xreq = this.getXmlHttpRequestObject() |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 188 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 189 | if (xreq.overrideMimeType) |
| 190 | xreq.overrideMimeType("application/json") |
| 191 | |
| 192 | xreq.open("POST", this.url, false) |
| 193 | xreq.send(this.send_buf) |
| 194 | |
| 195 | if (xreq.readyState != 4) |
| 196 | throw "encountered an unknown ajax ready state: "+xreq.readyState |
| 197 | |
| 198 | if (xreq.status != 200) |
| 199 | throw "encountered a unknown request status: "+xreq.status |
| 200 | |
| 201 | this.recv_buf = xreq.responseText |
| 202 | this.recv_buf_sz = this.recv_buf.length |
| 203 | this.wpos = this.recv_buf.length |
| 204 | this.rpos = 0 |
| 205 | }, |
| 206 | |
| 207 | setRecvBuffer : function(buf){ |
| 208 | this.recv_buf = buf |
| 209 | this.recv_buf_sz = this.recv_buf.length |
| 210 | this.wpos = this.recv_buf.length |
| 211 | this.rpos = 0 |
| 212 | }, |
| 213 | |
| 214 | isOpen : function() { |
| 215 | return true |
| 216 | }, |
| 217 | |
| 218 | open : function() {}, |
| 219 | |
| 220 | close: function() {}, |
| 221 | |
| 222 | read : function(len) { |
| 223 | var avail = this.wpos - this.rpos |
| 224 | |
| 225 | if(avail == 0) |
| 226 | return '' |
| 227 | |
| 228 | var give = len |
| 229 | |
| 230 | if(avail < len) |
| 231 | give = avail |
| 232 | |
| 233 | var ret = this.read_buf.substr(this.rpos,give) |
| 234 | this.rpos += give |
| 235 | |
| 236 | //clear buf when complete? |
| 237 | return ret |
| 238 | }, |
| 239 | |
| 240 | readAll : function() { |
| 241 | return this.recv_buf |
| 242 | }, |
| 243 | |
| 244 | write : function(buf){ |
| 245 | this.send_buf = buf |
| 246 | }, |
| 247 | |
| 248 | getSendBuffer : function(){ |
| 249 | return this.send_buf |
| 250 | } |
| 251 | |
| 252 | } |
| 253 | |
| 254 | |
| 255 | |
| 256 | Thrift.Protocol = function(transport){ |
| 257 | this.transport = transport |
| 258 | } |
| 259 | |
| 260 | Thrift.Protocol.Type = {} |
| 261 | Thrift.Protocol.Type[ Thrift.Type.BOOL ] = '"tf"' |
| 262 | Thrift.Protocol.Type[ Thrift.Type.BYTE ] = '"i8"' |
| 263 | Thrift.Protocol.Type[ Thrift.Type.I16 ] = '"i16"' |
| 264 | Thrift.Protocol.Type[ Thrift.Type.I32 ] = '"i32"' |
| 265 | Thrift.Protocol.Type[ Thrift.Type.I64 ] = '"i64"' |
| 266 | Thrift.Protocol.Type[ Thrift.Type.DOUBLE ] = '"dbl"' |
| 267 | Thrift.Protocol.Type[ Thrift.Type.STRUCT ] = '"rec"' |
| 268 | Thrift.Protocol.Type[ Thrift.Type.STRING ] = '"str"' |
| 269 | Thrift.Protocol.Type[ Thrift.Type.MAP ] = '"map"' |
| 270 | Thrift.Protocol.Type[ Thrift.Type.LIST ] = '"lst"' |
| 271 | Thrift.Protocol.Type[ Thrift.Type.SET ] = '"set"' |
| 272 | |
| 273 | |
| 274 | Thrift.Protocol.RType = {} |
| 275 | Thrift.Protocol.RType[ "tf" ] = Thrift.Type.BOOL |
| 276 | Thrift.Protocol.RType[ "i8" ] = Thrift.Type.BYTE |
| 277 | Thrift.Protocol.RType[ "i16"] = Thrift.Type.I16 |
| 278 | Thrift.Protocol.RType[ "i32"] = Thrift.Type.I32 |
| 279 | Thrift.Protocol.RType[ "i64"] = Thrift.Type.I64 |
| 280 | Thrift.Protocol.RType[ "dbl"] = Thrift.Type.DOUBLE |
| 281 | Thrift.Protocol.RType[ "rec"] = Thrift.Type.STRUCT |
| 282 | Thrift.Protocol.RType[ "str"] = Thrift.Type.STRING |
| 283 | Thrift.Protocol.RType[ "map"] = Thrift.Type.MAP |
| 284 | Thrift.Protocol.RType[ "lst"] = Thrift.Type.LIST |
| 285 | Thrift.Protocol.RType[ "set"] = Thrift.Type.SET |
| 286 | |
| 287 | Thrift.Protocol.Version = 1 |
| 288 | |
| 289 | Thrift.Protocol.prototype = { |
| 290 | |
| 291 | getTransport : function(){ |
| 292 | return this.transport |
| 293 | }, |
| 294 | |
| 295 | //Write functions |
| 296 | writeMessageBegin : function(name,messageType,seqid){ |
| 297 | this.tstack = new Array() |
| 298 | this.tpos = new Array(); |
| 299 | |
| 300 | this.tstack.push([Thrift.Protocol.Version,'"'+name+'"',messageType,seqid]); |
| 301 | }, |
| 302 | |
| 303 | writeMessageEnd : function(){ |
| 304 | var obj = this.tstack.pop() |
| 305 | |
| 306 | this.wobj = this.tstack.pop() |
| 307 | this.wobj.push(obj) |
| 308 | |
| 309 | this.wbuf = "["+this.wobj.join(",")+"]"; |
| 310 | |
| 311 | this.transport.write(this.wbuf); |
| 312 | }, |
| 313 | |
| 314 | |
| 315 | writeStructBegin : function(name){ |
| 316 | this.tpos.push(this.tstack.length) |
| 317 | this.tstack.push({}) |
| 318 | }, |
| 319 | |
| 320 | writeStructEnd : function(){ |
| 321 | |
| 322 | var p = this.tpos.pop() |
| 323 | var struct = this.tstack[p] |
| 324 | var str = "{" |
| 325 | var first = true |
| 326 | for( var key in struct ){ |
| 327 | if(first) |
| 328 | first = false; |
| 329 | else |
| 330 | str += ","; |
| 331 | |
| 332 | str += key+":"+struct[key] |
| 333 | } |
| 334 | |
| 335 | str += "}" |
| 336 | this.tstack[p] = str; |
| 337 | }, |
| 338 | |
| 339 | writeFieldBegin : function(name,fieldType,fieldId){ |
| 340 | this.tpos.push(this.tstack.length) |
| 341 | this.tstack.push({"fieldId" : '"'+fieldId+'"', "fieldType" : Thrift.Protocol.Type[fieldType]}); |
| 342 | |
| 343 | }, |
| 344 | |
| 345 | writeFieldEnd : function(){ |
| 346 | var value = this.tstack.pop() |
| 347 | var fieldInfo = this.tstack.pop() |
| 348 | |
| 349 | this.tstack[this.tstack.length-1][fieldInfo.fieldId] = "{"+fieldInfo.fieldType+":"+value+"}" |
| 350 | this.tpos.pop() |
| 351 | }, |
| 352 | |
| 353 | writeFieldStop : function(){ |
| 354 | //na |
| 355 | }, |
| 356 | |
| 357 | writeMapBegin : function(keyType,valType,size){ |
| 358 | //size is invalid, we'll set it on end. |
| 359 | this.tpos.push(this.tstack.length) |
| 360 | this.tstack.push([Thrift.Protocol.Type[keyType],Thrift.Protocol.Type[valType],0]) |
| 361 | }, |
| 362 | |
| 363 | writeMapEnd : function(){ |
| 364 | var p = this.tpos.pop() |
| 365 | |
| 366 | if(p == this.tstack.length) |
| 367 | return; |
| 368 | |
| 369 | if((this.tstack.length - p - 1) % 2 != 0) |
| 370 | this.tstack.push(""); |
| 371 | |
| 372 | var size = (this.tstack.length - p - 1)/2 |
| 373 | |
| 374 | this.tstack[p][this.tstack[p].length-1] = size; |
| 375 | |
| 376 | var map = "{" |
| 377 | var first = true |
| 378 | while( this.tstack.length > p+1 ){ |
| 379 | var v = this.tstack.pop() |
| 380 | var k = this.tstack.pop() |
| 381 | if(first){ |
| 382 | first = false |
| 383 | }else{ |
| 384 | map += "," |
| 385 | } |
| 386 | |
| 387 | map += '"'+k+'":'+v |
| 388 | } |
| 389 | map += "}" |
| 390 | |
| 391 | this.tstack[p].push(map) |
| 392 | this.tstack[p] = "["+this.tstack[p].join(",")+"]" |
| 393 | }, |
| 394 | |
| 395 | writeListBegin : function(elemType,size){ |
| 396 | this.tpos.push(this.tstack.length) |
| 397 | this.tstack.push([Thrift.Protocol.Type[elemType],size]); |
| 398 | }, |
| 399 | |
| 400 | writeListEnd : function(){ |
| 401 | var p = this.tpos.pop() |
| 402 | |
| 403 | while( this.tstack.length > p+1 ){ |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 404 | var tmpVal = this.tstack[p+1] |
| 405 | this.tstack.splice(p+1, 1) |
| 406 | this.tstack[p].push(tmpVal) |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | this.tstack[p] = '['+this.tstack[p].join(",")+']'; |
| 410 | }, |
| 411 | |
| 412 | writeSetBegin : function(elemType,size){ |
| 413 | this.tpos.push(this.tstack.length) |
| 414 | this.tstack.push([Thrift.Protocol.Type[elemType],size]); |
| 415 | }, |
| 416 | |
| 417 | writeSetEnd : function(){ |
| 418 | var p = this.tpos.pop() |
| 419 | |
| 420 | while( this.tstack.length > p+1 ){ |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 421 | var tmpVal = this.tstack[p+1] |
| 422 | this.tstack.splice(p+1, 1) |
| 423 | this.tstack[p].push(tmpVal) |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | this.tstack[p] = '['+this.tstack[p].join(",")+']'; |
| 427 | }, |
| 428 | |
| 429 | writeBool : function(value){ |
| 430 | this.tstack.push( value ? 1 : 0 ); |
| 431 | }, |
| 432 | |
| 433 | writeByte : function(i8){ |
| 434 | this.tstack.push(i8); |
| 435 | }, |
| 436 | |
| 437 | writeI16 : function(i16){ |
| 438 | this.tstack.push(i16); |
| 439 | }, |
| 440 | |
| 441 | writeI32 : function(i32){ |
| 442 | this.tstack.push(i32); |
| 443 | }, |
| 444 | |
| 445 | writeI64 : function(i64){ |
| 446 | this.tstack.push(i64); |
| 447 | }, |
| 448 | |
| 449 | writeDouble : function(dbl){ |
| 450 | this.tstack.push(dbl); |
| 451 | }, |
| 452 | |
| 453 | writeString : function(str){ |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 454 | // We do not encode uri components for wire transfer: |
| 455 | if(str === null) { |
| 456 | this.tstack.push(null); |
| 457 | } else { |
| 458 | // concat may be slower than building a byte buffer |
| 459 | var escapedString = ""; |
| 460 | for(var i = 0; i < str.length; i++) { |
| 461 | var ch = str.charAt(i); // a single double quote: " |
| 462 | if(ch === '\"') { |
| 463 | escapedString += '\\\"'; // write out as: \" |
| 464 | } else if(ch === '\\') { // a single backslash: \ |
| 465 | escapedString += '\\\\'; // write out as: \\ |
| 466 | /* Currently escaped forward slashes break TJSONProtocol. |
| 467 | * As it stands, we can simply pass forward slashes into our strings |
| 468 | * across the wire without being escaped. |
| 469 | * I think this is the protocol's bug, not thrift.js |
| 470 | * } else if(ch === '/') { // a single forward slash: / |
| 471 | * escapedString += '\\/'; // write out as \/ |
| 472 | * } |
| 473 | */ |
| 474 | } else if(ch === '\b') { // a single backspace: invisible |
| 475 | escapedString += '\\b'; // write out as: \b" |
| 476 | } else if(ch === '\f') { // a single formfeed: invisible |
| 477 | escapedString += '\\f'; // write out as: \f" |
| 478 | } else if(ch === '\n') { // a single newline: invisible |
| 479 | escapedString += '\\n'; // write out as: \n" |
| 480 | } else if(ch === '\r') { // a single return: invisible |
| 481 | escapedString += '\\r'; // write out as: \r" |
| 482 | } else if(ch === '\t') { // a single tab: invisible |
| 483 | escapedString += '\\t'; // write out as: \t" |
| 484 | } else { |
| 485 | escapedString += ch; // Else it need not be escaped |
| 486 | } |
| 487 | } |
| 488 | this.tstack.push('"' + escapedString + '"'); |
| 489 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 490 | }, |
| 491 | |
| 492 | writeBinary : function(str){ |
| 493 | this.writeString(str); |
| 494 | }, |
| 495 | |
| 496 | |
| 497 | |
| 498 | // Reading functions |
| 499 | readMessageBegin : function(name, messageType, seqid){ |
| 500 | this.rstack = new Array() |
| 501 | this.rpos = new Array() |
| 502 | |
| 503 | this.robj = eval(this.transport.readAll()) |
| 504 | |
| 505 | var r = {} |
| 506 | var version = this.robj.shift() |
| 507 | |
| 508 | if(version != Thrift.Protocol.Version){ |
| 509 | throw "Wrong thrift protocol version: "+version |
| 510 | } |
| 511 | |
| 512 | r["fname"] = this.robj.shift() |
| 513 | r["mtype"] = this.robj.shift() |
| 514 | r["rseqid"] = this.robj.shift() |
| 515 | |
| 516 | |
| 517 | //get to the main obj |
| 518 | this.rstack.push(this.robj.shift()) |
| 519 | |
| 520 | return r |
| 521 | }, |
| 522 | |
| 523 | |
| 524 | readMessageEnd : function(){ |
| 525 | }, |
| 526 | |
| 527 | readStructBegin : function(name){ |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 528 | var r = {} |
| 529 | r["fname"] = '' |
| 530 | |
| 531 | //incase this is an array of structs |
| 532 | if(this.rstack[this.rstack.length-1] instanceof Array) |
| 533 | this.rstack.push(this.rstack[this.rstack.length-1].shift()) |
| 534 | |
| 535 | return r |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 536 | }, |
| 537 | |
| 538 | readStructEnd : function(){ |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 539 | if(this.rstack[this.rstack.length-2] instanceof Array) |
| 540 | this.rstack.pop() |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 541 | }, |
| 542 | |
| 543 | readFieldBegin : function(){ |
| 544 | var r = {}; |
| 545 | |
| 546 | var fid = -1 |
| 547 | var ftype = Thrift.Type.STOP |
| 548 | |
| 549 | //get a fieldId |
| 550 | for(var f in (this.rstack[this.rstack.length-1])){ |
| 551 | if(f == null) continue |
| 552 | |
| 553 | fid = parseInt(f) |
| 554 | this.rpos.push(this.rstack.length) |
| 555 | |
| 556 | var field = this.rstack[this.rstack.length-1][fid] |
| 557 | |
| 558 | //remove so we don't see it again |
| 559 | delete this.rstack[this.rstack.length-1][fid] |
| 560 | |
| 561 | this.rstack.push(field) |
| 562 | |
| 563 | break |
| 564 | } |
| 565 | |
| 566 | if(fid != -1){ |
| 567 | |
| 568 | //should only be 1 of these but this is the only |
| 569 | //way to match a key |
| 570 | for(var f in (this.rstack[this.rstack.length-1])){ |
| 571 | if(Thrift.Protocol.RType[f] == null ) continue |
| 572 | |
| 573 | ftype = Thrift.Protocol.RType[f] |
| 574 | this.rstack[this.rstack.length-1] = this.rstack[this.rstack.length-1][f] |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | r["fname"] = '' |
| 579 | r["ftype"] = ftype |
| 580 | r["fid"] = fid |
| 581 | |
| 582 | |
| 583 | return r |
| 584 | }, |
| 585 | |
| 586 | readFieldEnd : function(){ |
| 587 | var pos = this.rpos.pop() |
| 588 | |
| 589 | //get back to the right place in the stack |
| 590 | while(this.rstack.length > pos) |
| 591 | this.rstack.pop(); |
| 592 | |
| 593 | }, |
| 594 | |
| 595 | readMapBegin : function(keyType,valType,size){ |
| 596 | |
T Jake Luciani | b22b51e | 2010-08-06 02:37:45 +0000 | [diff] [blame] | 597 | var map = this.rstack.pop() |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 598 | |
| 599 | var r = {}; |
| 600 | r["ktype"] = Thrift.Protocol.RType[map.shift()] |
| 601 | r["vtype"] = Thrift.Protocol.RType[map.shift()] |
| 602 | r["size"] = map.shift() |
| 603 | |
| 604 | |
| 605 | this.rpos.push(this.rstack.length) |
| 606 | this.rstack.push(map.shift()) |
| 607 | |
| 608 | return r; |
| 609 | }, |
| 610 | |
| 611 | readMapEnd : function(){ |
| 612 | this.readFieldEnd() |
| 613 | }, |
| 614 | |
| 615 | readListBegin : function(elemType,size){ |
| 616 | |
| 617 | var list = this.rstack[this.rstack.length-1] |
| 618 | |
| 619 | var r = {}; |
| 620 | r["etype"] = Thrift.Protocol.RType[list.shift()]; |
| 621 | r["size" ] = list.shift(); |
| 622 | |
| 623 | |
| 624 | this.rpos.push(this.rstack.length); |
T Jake Luciani | 127909c | 2010-07-10 14:58:51 +0000 | [diff] [blame] | 625 | this.rstack.push(list) |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 626 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 627 | return r; |
| 628 | }, |
| 629 | |
| 630 | readListEnd : function(){ |
| 631 | this.readFieldEnd() |
| 632 | }, |
| 633 | |
| 634 | readSetBegin : function(elemType,size){ |
| 635 | return this.readListBegin(elemType,size) |
| 636 | }, |
| 637 | |
| 638 | readSetEnd : function(){ |
| 639 | return this.readListEnd() |
| 640 | }, |
| 641 | |
| 642 | readBool : function(){ |
| 643 | var r = this.readI32() |
| 644 | |
| 645 | if( r != null && r["value"] == "1" ){ |
| 646 | r["value"] = true |
| 647 | }else{ |
| 648 | r["value"] = false |
| 649 | } |
| 650 | |
| 651 | return r |
| 652 | }, |
| 653 | |
| 654 | readByte : function(){ |
| 655 | return this.readI32() |
| 656 | }, |
| 657 | |
| 658 | readI16 : function(){ |
| 659 | return this.readI32() |
| 660 | }, |
| 661 | |
| 662 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 663 | readI32 : function(f){ |
| 664 | if(f == undefined) |
| 665 | f = this.rstack[this.rstack.length-1] |
| 666 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 667 | var r = {} |
| 668 | |
| 669 | if(f instanceof Array){ |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 670 | if(f.length == 0) |
| 671 | r["value"] = undefined |
| 672 | else |
| 673 | r["value"] = f.shift() |
| 674 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 675 | }else if(f instanceof Object){ |
| 676 | for(var i in f){ |
| 677 | if(i == null) continue |
| 678 | this.rstack.push(f[i]) |
| 679 | delete f[i] |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 680 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 681 | r["value"] = i |
| 682 | break |
| 683 | } |
| 684 | } else { |
| 685 | r["value"] = f |
| 686 | } |
| 687 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 688 | return r |
| 689 | }, |
| 690 | |
| 691 | readI64 : function(){ |
| 692 | return this.readI32() |
| 693 | }, |
| 694 | |
| 695 | readDouble : function(){ |
| 696 | return this.readI32() |
| 697 | }, |
| 698 | |
| 699 | readString : function(){ |
| 700 | var r = this.readI32() |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 701 | return r |
| 702 | }, |
| 703 | |
| 704 | readBinary : function(){ |
| 705 | return this.readString() |
| 706 | }, |
| 707 | |
| 708 | |
| 709 | //Method to arbitrarily skip over data. |
| 710 | skip : function(type){ |
| 711 | throw "skip not supported yet" |
| 712 | } |
| 713 | |
| 714 | } |
| 715 | |
| 716 | |
| 717 | |