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